-
Notifications
You must be signed in to change notification settings - Fork 350
Make sure uccsd compiles for quantum devices #2458
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
Changes from 12 commits
Commits
Show all changes
76 commits
Select commit
Hold shift + click to select a range
00aa8d8
Temp
annagrin 1a300ba
Merged with main
annagrin b1b220c
Add tests
annagrin 0ca3674
Merge branch 'main' of https://github.com/NVIDIA/cuda-quantum into fi…
annagrin fac55bd
Revert temp changes
annagrin ae35d16
TEMP
annagrin ad486c1
Merge branch 'main' of https://github.com/NVIDIA/cuda-quantum into fi…
annagrin 4178f0e
Fix bugs
annagrin cf283a3
Merge branch 'main' of https://github.com/NVIDIA/cuda-quantum into fi…
annagrin 3c07cad
Add classic opts to LoopUnroll and add tests
annagrin ff887ea
Remove local mounts
annagrin 718d9ed
DCO Remediation Commit for Anna Gringauze <agringauze@nvidia.com>
annagrin f33a3b9
Use .inc files instead of splitting patterns out
annagrin 16d8d69
Merge branch 'main' of https://github.com/NVIDIA/cuda-quantum into fi…
annagrin 0a36b6b
temp
annagrin ec5468e
temp
annagrin 3dc023f
Merge branch 'main' of https://github.com/NVIDIA/cuda-quantum into lo…
annagrin 4648b9e
temp
annagrin 150a84e
Add more patterns to loop-unroll
annagrin 5a5f88a
Merge with main
annagrin 591f09d
cleanup
annagrin 377248b
cleanup
annagrin 0ab3a56
Merge with main
annagrin f08502e
Fix some failing tests
annagrin 2fb2cfe
Fix failing tests
annagrin 6d4291f
Merge branch 'main' of https://github.com/NVIDIA/cuda-quantum into lo…
annagrin e714387
DCO Remediation Commit for Anna Gringauze <agringauze@nvidia.com>
annagrin 68b9ba2
Remove parts handled in other CRs
annagrin 74178a8
Merged with main
annagrin ef0909c
Merged in loop-unroll-additional-opts
annagrin 1e1a4ca
Cleanup
annagrin b3ec379
Fix test failures
annagrin 9b6fc6a
Merge branch 'main' of https://github.com/NVIDIA/cuda-quantum into lo…
annagrin 3d3597d
Update write_after_write_elimination.qke
annagrin 6a17b19
Merge branch 'main' of https://github.com/NVIDIA/cuda-quantum into fi…
annagrin b537ffe
Fix test failures
annagrin ee2884c
Update write_after_write_elimination.qke
annagrin 7600781
Update test_kernel_features.py
annagrin e06980c
`DCO Remediation Commit for Anna Gringauze <agringauze@nvidia.com>
annagrin 032b465
Merge branch 'loop-unroll-additional-opts' of https://github.com/anna…
annagrin 0ddeccf
DCO Remediation Commit for Anna Gringauze <agringauze@nvidia.com>
annagrin e74b60b
Remove unnecessary changes
annagrin aba4409
Merge branch 'main' of https://github.com/NVIDIA/cuda-quantum into fi…
annagrin 1791226
Merge branch 'main' of https://github.com/NVIDIA/cuda-quantum into lo…
annagrin a746d83
Fix failed tests
annagrin b944077
Fix failed tests
annagrin 13cfcad
Fix failed tests
annagrin bfd4a4c
Run canonical patterns alongside lift-array-alloc
annagrin a9ec857
Fix failed tests
annagrin 44c845b
Merge branch 'lift-array-alloc-array-copy' of https://github.com/anna…
annagrin 3ce961f
Add classical-optimization pass and pipeline
annagrin 0bdf458
Add more tests
annagrin 0b58beb
Remove unneded comments
annagrin f9cca25
Remove unneded comments
annagrin 0cff560
Merged with classical-optimization-pipeline2
annagrin ba7b46d
Use classical optimization pipeline in quantum config files
annagrin a62cbc7
Revert lift-array-alloc changes
annagrin 96a62e6
Add classical-optimization-pipeline to more backends
annagrin 3b93567
Merge branch 'main' into classical-optimization-pipeline2
annagrin 5564a64
Fixed failing target config tests
annagrin b2cd5ce
Merge branch 'main' of https://github.com/NVIDIA/cuda-quantum into fi…
annagrin 75f4cae
Merge branch 'classical-optimization-pipeline2' of https://github.com…
annagrin 672d57f
Address CR comments
annagrin e178987
Cleanup
annagrin a93ba6e
Merge branch 'main' of https://github.com/NVIDIA/cuda-quantum into fi…
annagrin 80e00c2
Merge with main
annagrin afb6238
Fix failing test
annagrin f64a45c
Fix failing tests
annagrin 580e329
Fix failing tests
annagrin 6c075be
fix failing tests
annagrin 16e4e27
Fix failing tests
annagrin 0f0fc21
Fix failing tests
annagrin 246f8d9
DCO Remediation Commit for Anna Gringauze <agringauze@nvidia.com>
annagrin 322269f
Add temp printing
annagrin f1e4d14
Fix failing tests
annagrin 4408717
Merge branch 'main' of https://github.com/NVIDIA/cuda-quantum into fi…
annagrin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| /******************************************************************************* | ||
| * Copyright (c) 2022 - 2025 NVIDIA Corporation & Affiliates. * | ||
| * All rights reserved. * | ||
| * * | ||
| * This source code and the accompanying materials are made available under * | ||
| * the terms of the Apache License 2.0 which accompanies this distribution. * | ||
| ******************************************************************************/ | ||
|
|
||
| #include "CollapseStoresPatterns.h" | ||
| #include "PassDetails.h" | ||
| #include "cudaq/Optimizer/Builder/Intrinsics.h" | ||
| #include "cudaq/Optimizer/Dialect/CC/CCOps.h" | ||
| #include "cudaq/Optimizer/Dialect/Quake/QuakeOps.h" | ||
| #include "cudaq/Optimizer/Transforms/Passes.h" | ||
| #include "mlir/Dialect/Complex/IR/Complex.h" | ||
| #include "mlir/IR/BuiltinOps.h" | ||
| #include "mlir/IR/Dominance.h" | ||
| #include "mlir/IR/PatternMatch.h" | ||
| #include "mlir/Transforms/GreedyPatternRewriteDriver.h" | ||
| #include "mlir/Transforms/Passes.h" | ||
|
|
||
| namespace cudaq::opt { | ||
| #define GEN_PASS_DEF_COLLAPSESTORES | ||
| #include "cudaq/Optimizer/Transforms/Passes.h.inc" | ||
| } // namespace cudaq::opt | ||
|
|
||
| #define DEBUG_TYPE "collapse-stores" | ||
|
|
||
| using namespace mlir; | ||
|
|
||
| namespace { | ||
| class CollapseStoresPass | ||
| : public cudaq::opt::impl::CollapseStoresBase<CollapseStoresPass> { | ||
| public: | ||
| using CollapseStoresBase::CollapseStoresBase; | ||
|
|
||
| void runOnOperation() override { | ||
| auto *ctx = &getContext(); | ||
| auto func = getOperation(); | ||
|
|
||
| RewritePatternSet patterns(ctx); | ||
| patterns.insert<RemoveUselessStorePattern>(ctx); | ||
|
|
||
| LLVM_DEBUG(llvm::dbgs() << "Before collapsing stores: " << func << '\n'); | ||
|
|
||
| if (failed(applyPatternsAndFoldGreedily(func, std::move(patterns)))) | ||
| signalPassFailure(); | ||
|
|
||
| LLVM_DEBUG(llvm::dbgs() << "After collapsing stores: " << func << '\n'); | ||
| } | ||
| }; | ||
| } // namespace |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| /******************************************************************************* | ||
| * Copyright (c) 2022 - 2025 NVIDIA Corporation & Affiliates. * | ||
| * All rights reserved. * | ||
| * * | ||
| * This source code and the accompanying materials are made available under * | ||
| * the terms of the Apache License 2.0 which accompanies this distribution. * | ||
| ******************************************************************************/ | ||
|
|
||
| #include "CollapseStoresPatterns.h" | ||
| #include "PassDetails.h" | ||
| #include "cudaq/Optimizer/Builder/Intrinsics.h" | ||
| #include "cudaq/Optimizer/Dialect/CC/CCOps.h" | ||
| #include "cudaq/Optimizer/Dialect/Quake/QuakeOps.h" | ||
| #include "cudaq/Optimizer/Transforms/Passes.h" | ||
| #include "mlir/Dialect/Complex/IR/Complex.h" | ||
| #include "mlir/IR/BuiltinOps.h" | ||
| #include "mlir/IR/Dominance.h" | ||
| #include "mlir/IR/PatternMatch.h" | ||
| #include "mlir/Transforms/GreedyPatternRewriteDriver.h" | ||
| #include "mlir/Transforms/Passes.h" | ||
|
|
||
| #define DEBUG_TYPE "collapse-stores" | ||
|
|
||
| using namespace mlir; | ||
|
|
||
| /// Remove stores followed by a store to the same pointer | ||
| /// if the pointer is not used in between. | ||
| /// ``` | ||
| /// cc.store %c0_i64, %5 : !cc.ptr<i64> | ||
| /// // no use of %5 until next line | ||
| /// cc.store %3, %5 : !cc.ptr<i64> | ||
| /// ─────────────────────────────────────────── | ||
| /// cc.store %3, %5 : !cc.ptr<i64> | ||
| /// ``` | ||
| LogicalResult | ||
| RemoveUselessStorePattern::matchAndRewrite(cudaq::cc::StoreOp store, | ||
| PatternRewriter &rewriter) const { | ||
| if (isUselessStore(store)) { | ||
| rewriter.eraseOp(store); | ||
| return success(); | ||
| } | ||
| return failure(); | ||
| } | ||
|
|
||
| /// Detect if the current store can be removed. | ||
| bool RemoveUselessStorePattern::isUselessStore(cudaq::cc::StoreOp store) { | ||
| Value currentPtr; | ||
|
|
||
| if (!isStoreToStack(store)) | ||
| return false; | ||
|
|
||
| auto block = store.getOperation()->getBlock(); | ||
| for (auto &op : *block) { | ||
| if (auto s = dyn_cast<cudaq::cc::StoreOp>(&op)) { | ||
| auto nextPtr = s.getPtrvalue(); | ||
| if (store == s) { | ||
| // Start searching from the current store | ||
| currentPtr = nextPtr; | ||
| } else { | ||
| // Found an overriding store, the current store is useless | ||
| if (currentPtr == nextPtr) | ||
| return true; | ||
|
|
||
| // Found a use for a current ptr before the overriding store | ||
| if (currentPtr && isUsed(currentPtr, &op)) | ||
| return false; | ||
| } | ||
| } else { | ||
| // Found a use for a current ptr before the overriding store | ||
| if (currentPtr && isUsed(currentPtr, &op)) | ||
| return false; | ||
| } | ||
| } | ||
| // No multiple stores to the same location found | ||
| return false; | ||
| } | ||
|
|
||
| /// Detect stores to stack locations | ||
| /// ``` | ||
| /// %1 = cc.alloca !cc.array<i64 x 2> | ||
| /// | ||
| /// %2 = cc.cast %1 : (!cc.ptr<!cc.array<i64 x 2>>) -> !cc.ptr<i64> | ||
| /// cc.store %c0_i64, %2 : !cc.ptr<i64> | ||
| /// | ||
| /// %3 = cc.compute_ptr %1[1] : (!cc.ptr<!cc.array<i64 x 2>>) -> !cc.ptr<i64> | ||
| /// cc.store %c0_i64, %3 : !cc.ptr<i64> | ||
| /// ``` | ||
| bool RemoveUselessStorePattern::isStoreToStack(cudaq::cc::StoreOp store) { | ||
| auto ptrOp = store.getPtrvalue(); | ||
| if (auto cast = ptrOp.getDefiningOp<cudaq::cc::CastOp>()) | ||
| ptrOp = cast.getOperand(); | ||
|
|
||
| if (auto computePtr = ptrOp.getDefiningOp<cudaq::cc::ComputePtrOp>()) | ||
| ptrOp = computePtr.getBase(); | ||
|
|
||
| if (auto alloca = ptrOp.getDefiningOp<cudaq::cc::AllocaOp>()) | ||
| return true; | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| /// Detect if value is used in the op or its nested blocks. | ||
| bool RemoveUselessStorePattern::isUsed(Value v, Operation *op) { | ||
| for (auto opnd : op->getOperands()) | ||
| if (opnd == v) | ||
| return true; | ||
|
|
||
| for (auto ®ion : op->getRegions()) | ||
| for (auto &b : region) | ||
| for (auto &innerOp : b) | ||
| if (isUsed(v, &innerOp)) | ||
| return true; | ||
|
|
||
| return false; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| /****************************************************************-*- C++ -*-**** | ||
| * Copyright (c) 2022 - 2025 NVIDIA Corporation & Affiliates. * | ||
| * All rights reserved. * | ||
| * * | ||
| * This source code and the accompanying materials are made available under * | ||
| * the terms of the Apache License 2.0 which accompanies this distribution. * | ||
| ******************************************************************************/ | ||
|
|
||
| #include "PassDetails.h" | ||
| #include "cudaq/Optimizer/Builder/Intrinsics.h" | ||
| #include "cudaq/Optimizer/Dialect/CC/CCOps.h" | ||
| #include "cudaq/Optimizer/Dialect/Quake/QuakeOps.h" | ||
| #include "cudaq/Optimizer/Transforms/Passes.h" | ||
| #include "mlir/Dialect/Complex/IR/Complex.h" | ||
| #include "mlir/IR/BuiltinOps.h" | ||
| #include "mlir/IR/Dominance.h" | ||
| #include "mlir/IR/PatternMatch.h" | ||
| #include "mlir/Transforms/GreedyPatternRewriteDriver.h" | ||
| #include "mlir/Transforms/Passes.h" | ||
|
|
||
| /// Remove stores followed by a store to the same pointer | ||
| /// if the pointer is not used in between. | ||
| /// ``` | ||
| /// cc.store %c0_i64, %5 : !cc.ptr<i64> | ||
| /// // no use of %5 until next line | ||
| /// cc.store %3, %5 : !cc.ptr<i64> | ||
| /// ─────────────────────────────────────────── | ||
| /// cc.store %3, %5 : !cc.ptr<i64> | ||
| /// ``` | ||
| class RemoveUselessStorePattern | ||
| : public mlir::OpRewritePattern<cudaq::cc::StoreOp> { | ||
| public: | ||
| using OpRewritePattern::OpRewritePattern; | ||
|
|
||
| explicit RemoveUselessStorePattern(mlir::MLIRContext *ctx) | ||
| : OpRewritePattern(ctx) {} | ||
|
|
||
| mlir::LogicalResult | ||
| matchAndRewrite(cudaq::cc::StoreOp store, | ||
| mlir::PatternRewriter &rewriter) const override; | ||
|
|
||
| private: | ||
| /// Detect if the current store can be removed. | ||
| static bool isUselessStore(cudaq::cc::StoreOp store); | ||
|
|
||
| /// Detect stores to stack locations | ||
| /// ``` | ||
| /// %1 = cc.alloca !cc.array<i64 x 2> | ||
| /// | ||
| /// %2 = cc.cast %1 : (!cc.ptr<!cc.array<i64 x 2>>) -> !cc.ptr<i64> | ||
| /// cc.store %c0_i64, %2 : !cc.ptr<i64> | ||
| /// | ||
| /// %3 = cc.compute_ptr %1[1] : (!cc.ptr<!cc.array<i64 x 2>>) -> !cc.ptr<i64> | ||
| /// cc.store %c0_i64, %3 : !cc.ptr<i64> | ||
| /// ``` | ||
| static bool isStoreToStack(cudaq::cc::StoreOp store); | ||
|
|
||
| /// Detect if value is used in the op or its nested blocks. | ||
| static bool isUsed(mlir::Value v, mlir::Operation *op); | ||
| }; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.