KEMBAR78
Fix constant folding of booleans casted to integers by annagrin · Pull Request #2805 · NVIDIA/cuda-quantum · GitHub
Skip to content

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

Signed-off-by: Anna Gringauze <agringauze@nvidia.com>
@github-actions
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
Contributor

@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?

Signed-off-by: Anna Gringauze <agringauze@nvidia.com>
…x-const-folding

Signed-off-by: Anna Gringauze <agringauze@nvidia.com>
Signed-off-by: Anna Gringauze <agringauze@nvidia.com>
@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 <agringauze@nvidia.com>
github-actions bot pushed a commit that referenced this pull request Apr 14, 2025
@github-actions
Copy link

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

Signed-off-by: Anna Gringauze <agringauze@nvidia.com>
github-actions bot pushed a commit that referenced this pull request Apr 14, 2025
@github-actions
Copy link

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

@annagrin annagrin requested review from 1tnguyen and bmhowe23 April 14, 2025 21:42
Signed-off-by: Anna Gringauze <agringauze@nvidia.com>
…x-const-folding

Signed-off-by: Anna Gringauze <agringauze@nvidia.com>
github-actions bot pushed a commit that referenced this pull request Apr 17, 2025
@github-actions
Copy link

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

Signed-off-by: Anna Gringauze <agringauze@nvidia.com>
@github-actions
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
@github-actions
Copy link

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

Signed-off-by: Anna Gringauze <agringauze@nvidia.com>
github-actions bot pushed a commit that referenced this pull request May 15, 2025
@github-actions
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
@github-actions
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 <eschweitz@nvidia.com>
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
@github-actions
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
@github-actions
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
@github-actions
Copy link

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

annagrin added a commit to annagrin/cuda-quantum that referenced this pull request Jun 17, 2025
* Fix constant folding of boolean casted to integers

Signed-off-by: Anna Gringauze <agringauze@nvidia.com>

* Fix constant folding of boolean casted to integers

Signed-off-by: Anna Gringauze <agringauze@nvidia.com>

* Address CR Comments

Signed-off-by: Anna Gringauze <agringauze@nvidia.com>

* Format

Signed-off-by: Anna Gringauze <agringauze@nvidia.com>

* Cleanup test

Signed-off-by: Anna Gringauze <agringauze@nvidia.com>

* Updated tests

Signed-off-by: Anna Gringauze <agringauze@nvidia.com>

* Updated tests

Signed-off-by: Anna Gringauze <agringauze@nvidia.com>

* Address CR comments

Signed-off-by: Anna Gringauze <agringauze@nvidia.com>

* Address CR comments

Signed-off-by: Anna Gringauze <agringauze@nvidia.com>

* Cleanup

Signed-off-by: Anna Gringauze <agringauze@nvidia.com>

* Cleanup

Signed-off-by: Anna Gringauze <agringauze@nvidia.com>

* Cleanup

Signed-off-by: Anna Gringauze <agringauze@nvidia.com>

* Cleanup

Signed-off-by: Anna Gringauze <agringauze@nvidia.com>

---------

Signed-off-by: Anna Gringauze <agringauze@nvidia.com>
@bettinaheim bettinaheim added the bug fix To be listed under Bug Fixes in the release notes label Jul 22, 2025
@bettinaheim bettinaheim added this to the release 0.12.0 milestone Jul 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug fix To be listed under Bug Fixes in the release notes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Incorrect bool to integer cast

4 participants