Skip to content

Fix constant folding of booleans casted to integers #2805

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

Merged
merged 37 commits into from
May 23, 2025

Conversation

annagrin
Copy link
Collaborator

@annagrin annagrin commented Apr 10, 2025

Description

Fix constant folding of casts of integers.

Using the MLIR below

%0 = arith.constant <c> : i8
%1 = cc.cast unsigned %0: i32

The constant folding creates the following code without changing <c>:

%1 = arith.constant <c>: i32

This leads to incorrect values in constants when constant folding is invoked, for example,
(unsigned)(true) is currently converted to a -1 integer constant.

See added tests in targettests/execution/cast.cpp for more examples.

This PR updates constant folding to cast the source of the cast instruction to its original type before folding into a constant of a new type.

Example:

struct testConstBool {
  auto operator()() __qpu__ {
    cudaq::qubit q;
    unsigned i0 = (unsigned)(true);
    if (i0 == 1) {
      x(q);
    }
  }
};

int main() {
  auto counts = cudaq::sample(testConstBool{});
  counts.dump();
}

Quake before the fix:

func.func @__nvqpp__mlirgen__testConstBool () attributes {"cudaq-entrypoint", "cudaq-kernel"} {
    %c-1_i32 = arith.constant -1 : i32
    %c1_i32 = arith.constant 1 : i32
    %0 = quake.alloca !quake.ref
    %1 = cc.alloca i32
    cc.store %c-1_i32, %1 : !cc.ptr<i32>
    %2 = cc.load %1 : !cc.ptr<i32>
    %3 = arith.cmpi eq, %2, %c1_i32 : i32
    cc.if(%3) {
      quake.x %0 : (!quake.ref) -> ()
    }
    return
  }

Quake after the fix:

func.func @__nvqpp__mlirgen__testConstBool () attributes {"cudaq-entrypoint", "cudaq-kernel"} {
    %c1_i32 = arith.constant 1 : i32
    %0 = quake.alloca !quake.ref
    %1 = cc.alloca i32
    cc.store %c1_i32, %1 : !cc.ptr<i32>
    %2 = cc.load %1 : !cc.ptr<i32>
    %3 = arith.cmpi eq, %2, %c1_i32 : i32
    cc.if(%3) {
      quake.x %0 : (!quake.ref) -> ()
    }
    return
  }

Result before the fix:
{ 0:0000 }

Result after the fix:
{ 1:0000 }

Closes: #2804

Copy link

CUDA Quantum Docs Bot: A preview of the documentation can be found here.

github-actions bot pushed a commit that referenced this pull request Apr 11, 2025
Copy link
Collaborator

@nvidia-dobri nvidia-dobri left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change looks good, might be useful to know the context of when this is important / what the downstream breakage we saw was.

I'm a bit confused by the statement:

(unsigned)(true) is currently converted to a -1 constant in MLIR

What does -1 in unsigned mean? U..._MAX?

@annagrin
Copy link
Collaborator Author

annagrin commented Apr 14, 2025

Change looks good, might be useful to know the context of when this is important / what the downstream breakage we saw was.

I included an example in the description and the reference to the bug it fixes

I'm a bit confused by the statement:

(unsigned)(true) is currently converted to a -1 constant in MLIR

What does -1 in unsigned mean? U..._MAX?

Constant folding folds the cast, so in this particular example we end up with an integer constant with value of -1, of type i32 (see example in the description). It would behave the same as uint32 max, yes, which is incorrect in this case, we should const fold it to arith.constant 1 instead.

Signed-off-by: Anna Gringauze <[email protected]>
github-actions bot pushed a commit that referenced this pull request Apr 14, 2025
Copy link

CUDA Quantum Docs Bot: A preview of the documentation can be found here.

Signed-off-by: Anna Gringauze <[email protected]>
github-actions bot pushed a commit that referenced this pull request Apr 14, 2025
Copy link

CUDA Quantum Docs Bot: A preview of the documentation can be found here.

@annagrin annagrin requested review from bmhowe23 and 1tnguyen April 14, 2025 21:42
Signed-off-by: Anna Gringauze <[email protected]>
github-actions bot pushed a commit that referenced this pull request Apr 17, 2025
Copy link

CUDA Quantum Docs Bot: A preview of the documentation can be found here.

Signed-off-by: Anna Gringauze <[email protected]>
Copy link

CUDA Quantum Docs Bot: A preview of the documentation can be found here.

github-actions bot pushed a commit that referenced this pull request Apr 17, 2025
github-actions bot pushed a commit that referenced this pull request May 14, 2025
Copy link

CUDA Quantum Docs Bot: A preview of the documentation can be found here.

github-actions bot pushed a commit that referenced this pull request May 14, 2025
Copy link

CUDA Quantum Docs Bot: A preview of the documentation can be found here.

Signed-off-by: Anna Gringauze <[email protected]>
github-actions bot pushed a commit that referenced this pull request May 15, 2025
Copy link

CUDA Quantum Docs Bot: A preview of the documentation can be found here.

github-actions bot pushed a commit that referenced this pull request May 22, 2025
Copy link

CUDA Quantum Docs Bot: A preview of the documentation can be found here.

patch. Don't try to preserve NOPs, etc. but fix sign bit truncation
issue for all integral types in the IR.

Signed-off-by: Eric Schweitz <[email protected]>
Copy link
Collaborator

@schweitzpgi schweitzpgi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for all the tests!

github-actions bot pushed a commit that referenced this pull request May 22, 2025
Copy link

CUDA Quantum Docs Bot: A preview of the documentation can be found here.

github-actions bot pushed a commit that referenced this pull request May 23, 2025
Copy link

CUDA Quantum Docs Bot: A preview of the documentation can be found here.

@annagrin annagrin merged commit 0fe1f29 into NVIDIA:main May 23, 2025
195 checks passed
github-actions bot pushed a commit that referenced this pull request May 23, 2025
github-actions bot pushed a commit that referenced this pull request May 23, 2025
Copy link

CUDA Quantum Docs Bot: A preview of the documentation can be found here.

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

Successfully merging this pull request may close these issues.

Incorrect bool to integer cast
3 participants