Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

While loop within kernel fails if there are two conditions #2639

Open
4 tasks done
mawolf2023 opened this issue Feb 19, 2025 · 2 comments
Open
4 tasks done

While loop within kernel fails if there are two conditions #2639

mawolf2023 opened this issue Feb 19, 2025 · 2 comments

Comments

@mawolf2023
Copy link
Contributor

Required prerequisites

  • Consult the security policy. If reporting a security vulnerability, do not report the bug using this form. Use the process described in the policy to report the issue.
  • Make sure you've read the documentation. Your issue may be addressed there.
  • Search the issue tracker to verify that this hasn't already been reported. +1 or comment there if it has.
  • If possible, make a PR with a failing test to give us a starting point to work on!

Describe the bug

If you use a while loop inside a kernel with two conditions, the code fails and produces the following error.

python: /usr/local/llvm/include/llvm/Support/Casting.h:579: decltype(auto) llvm::cast(From*) [with To = mlir::arith::CmpIOp; From = mlir::Operation]: Assertion `isa(Val) && "cast() argument of incompatible type!"' failed.
Aborted (core dumped)

Steps to reproduce the bug

import cudaq

@cudaq.kernel
def test():
    reg = cudaq.qvector(5)

    y=1
    x=0
    while x < 4 and y==1:
        h(reg)
        x+=1

print(cudaq.sample(test))

Expected behavior

Expect the while loop to execute with the given logic.

Is this a regression? If it is, put the last known working version (or commit) here.

Not a regression

Environment

Suggestions

No response

@schweitzpgi
Copy link
Collaborator

schweitzpgi commented Feb 20, 2025

I tried this on my machine and didn't repro the issue. The code generated is the correct short-circuit condition and while loop.

    cc.loop while {
      %3 = cc.load %2 : !cc.ptr<i64>
      %4 = arith.cmpi slt, %3, %c4_i64 : i64
      %5 = arith.cmpi eq, %4, %false : i1
      %6 = cc.if(%5) -> i1 {
        cc.continue %false : i1
      } else {
        %7 = cc.load %1 : !cc.ptr<i64>
        %8 = arith.cmpi eq, %7, %c1_i64 : i64
        cc.continue %8 : i1
      }
      cc.condition %6
    } do {

@bebora
Copy link
Contributor

bebora commented Feb 21, 2025

I can reproduce the issue while using docker images cu12-0.9.1 (stable) and cu12-latest (nightly).
The error can be bypassed by rewriting the loop condition, the following is equivalent but arguably a bit uglier:

-while x < 4 and y==1:
+while (x < 4) * (y==1):

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants