-
-
Notifications
You must be signed in to change notification settings - Fork 74
✨ Add Constant Propagation #1845
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
Closed
LiRem101
wants to merge
242
commits into
munich-quantum-toolkit:main
from
LiRem101:mlir/constant-propagation
Closed
Changes from all commits
Commits
Show all changes
242 commits
Select commit
Hold shift + click to select a range
4c54a2e
:test_tube: Added tests for Hadamard lifting
LiRem101 5eac04f
:test_tube: Added test cases for controlled Pauli Z lifting
LiRem101 4b76cb6
:construction: Added framework for hadamard lifting pass
LiRem101 cfd5d36
:construction: Started exchange of Hadamard and Pauli
LiRem101 6e28a1e
:construction: Lifting uncontrolled Hadamard gates above Pauli
LiRem101 e7226e3
:white_check_mark: Added canonicalization to references
LiRem101 e57efd3
:white_check_mark: Corrected gates
LiRem101 024a665
:construction: Added handling of controlled gates
LiRem101 1c8dc3d
:recycle: Refactored handling of controlled gates
LiRem101 7a7c4f4
:construction: Added handling of controlled Pauli Z gates
LiRem101 a511116
:recycle: Refactored handling of controlled Pauli Z gates
LiRem101 a55ca13
:construction: Added CNOT Hadamard handling
LiRem101 6dc5439
✅ Corrected test
LiRem101 2a4a26d
:rotating_light: Fixed linter warnings
LiRem101 4a90aa7
:rotating_light: Fixed linter warnings
LiRem101 159cc25
:construction: Added infrastructure to use hadamard lifting
LiRem101 4a8cbe2
:pencil2: Changed Preceeding to preceding
LiRem101 f7a5607
🎨 pre-commit fixes
pre-commit-ci[bot] 9f73280
:rotating_light: Fixed linter warnings
LiRem101 4d328fb
:rotating_light: Optimized imports
LiRem101 7b96671
:rotating_light: Changed dyn_cast to llvm
LiRem101 10a6943
:construction: Removed double MLIRParser in CMakeLists
LiRem101 00554d4
:recycle: use is_permutation
LiRem101 b67526c
:bug: Controlled Pauli Y cannot be lifted
LiRem101 5c119ea
:rotating_light: Optimized imports and test naming
LiRem101 62757af
:construction: Apply suggestions from code review
LiRem101 2883681
:construction: Adapated code to suggestions from code review
LiRem101 ddd9a78
:construction: Implemented changes suggested by review, removed handl…
LiRem101 a375669
:construction: Added global phase for Y-H commutation
LiRem101 f8e0bf4
:construction: Added TypeSwitch
LiRem101 2c60097
:memo: Improved documentation
LiRem101 9a51157
:recycle: Removed temporary value and branchings, assisted-by: GPT 5 …
LiRem101 7d35dea
:construction: Implemented changes requested in review
LiRem101 bb2ed7f
:white_check_mark: Added test for measurement after target of CNOT
LiRem101 fe5c9b9
:construction: Implemented changes requested in review
LiRem101 00b32dc
:rotating_light: Fix linter errors
LiRem101 0335023
:rotating_light: Fix linter errors
LiRem101 05f7b27
:rotating_light: Fix linter errors
LiRem101 b40ad9c
:rotating_light: Fix linter errors
LiRem101 699fea7
:rotating_light: Fix linter errors
LiRem101 1915c52
:bug: Fix dangling range, assisted-by: GPT 5 via KI:connect
LiRem101 e94d224
:bug: Fixed dangling hadamard gate object
LiRem101 2f22a20
:bricks: Add files for tracking QuantumStates
LiRem101 e4b8666
:construction: Added GateToMap.h
LiRem101 0f27d85
:recycle: Refactored GateToMap.h, Assisted-by: GPT OSS via KI:connect
LiRem101 ffbf98e
📝 Added Docstring
LiRem101 4448c47
:construction: Added Header File for QuantumState
LiRem101 b0e0991
:recycle: Refactored QuantumState.hpp, assisted-by GPT OSS via KI:con…
LiRem101 6241f67
:construction: Added QuantumState class
LiRem101 dff4b83
:recycle: Refactored QuantumState.hpp and QuantumState.cpp, assisted-…
LiRem101 49f8829
✅ Added tests for QuantumState
LiRem101 bcf0659
:white_check_mark: Passes first test
LiRem101 31da770
:white_check_mark: Passed tests
LiRem101 d2e5095
:recycle: Refactored QuantumState.hpp
LiRem101 0a78ff8
:recycle: Refactored QuantumState.hpp, assisted-by GPT-OSS va KI:connect
LiRem101 fb4e85d
:construction: Corrected macros
LiRem101 0098669
:rotating_light: Corrected clang errors
LiRem101 b4f1fb5
🚧 Created HybridState.hpp
LiRem101 4987976
🚧 Adapted HybridState and created ClassicalArithOperation
LiRem101 3bab3ce
🚧 Added HybridState implementation
LiRem101 76cd4d0
🚧 Added tests for HybridState
LiRem101 e463022
:white_check_mark: Added tests for classical operations
LiRem101 7374fc5
:recycle: Refactored HybridState
LiRem101 56c7e1c
:construction: Added UnionTable infrastructure
LiRem101 d95797f
:white_check_mark: Added UnionTable tests
LiRem101 4ef17bb
:construction: Added UnionTable methods
LiRem101 062f3bf
:construction: Added UnionTable methods
LiRem101 dae1e83
:construction: Corrected handling of maps
LiRem101 a053ff0
:construction: Corrected handling of maps
LiRem101 47070a1
:construction: Corrected several test problems
LiRem101 76bdfe7
:construction: Added additional tests for UnionTable
LiRem101 9211659
:construction: Added getValueThatIsEquivalentToQubit
LiRem101 f17003b
:construction: Added UnionTableFunctionality
LiRem101 760ec3e
:construction: Fixed test results
LiRem101 edd708b
:construction: Fixed tests
LiRem101 0f57eae
:construction: Added infrastructure for constant propagation passes
LiRem101 c78de89
:test_tube: Added first test for constant propagation
LiRem101 762c1b6
:construction: Continued constant propagation infrastructure
LiRem101 eaedac0
:test_tube: Added constant propagation test
LiRem101 fbcbc05
:construction: Global phase as double
LiRem101 b0b84ad
:construction: Handling of global phases without control
LiRem101 7b2a7e4
:construction: Ctrl qubits changed after gate
LiRem101 f9b18ca
:construction: Start of ctrl handling
LiRem101 14a741d
:construction: Handling of ctrl continued
LiRem101 878b6a3
:construction: Handling of ctrl
LiRem101 b12db95
:memo: Added docstrings
LiRem101 427d7bc
:construction: Added creation of classical value inside measurement
LiRem101 553ac91
:construction: Added reset and measurement handling
LiRem101 aaaa004
:construction: Started removal of ctrl of always executed gates
LiRem101 f8df9f9
:construction: Removal of ctrl of always executed gates
LiRem101 1ac6c4a
:construction: Started removal of ctrls that are superfluous
LiRem101 7ca1cc3
✅ Fixed test
LiRem101 25f1331
:memo: Added docstring
LiRem101 fce3a4c
:construction: Continued removal of ctrls in CtrlOp
LiRem101 7a25ed3
:construction: Fixed creation of uncontrolled gates
LiRem101 9d63dc7
:test_tube: Added test to check propagation in classical branching
LiRem101 57da507
:construction: Added propagation of classical branching
LiRem101 dfd85b7
:test_tube: Extended test for removal of classical condition
LiRem101 6dbe893
:construction: Removal of branch if condition is one
LiRem101 a2c7161
:construction: Added handling to remove classical branches
LiRem101 dfa3786
:construction: First draft of creating branches
LiRem101 cab21cc
:construction: Create branches during constant propagation
LiRem101 fed2841
:test_tube: Added tests for movement of measurement
LiRem101 2ee5519
:construction: Added moving measurement in front
LiRem101 b3ed30f
:construction: Added handling of classical operations
LiRem101 0199caa
:construction: Creating then and else branches
LiRem101 d0dea4e
:construction: Added usage of parameters
LiRem101 ed40d43
:construction: Added pipeline frame
LiRem101 e45f3b5
:construction: Adapted to changes from main
LiRem101 cb94911
:memo: Added change information to CHANGELOG.md
LiRem101 d827393
🎨 pre-commit fixes
pre-commit-ci[bot] c3d0f30
:rotating_light: Fixed linter warnings
LiRem101 19698db
🎨 pre-commit fixes
pre-commit-ci[bot] 0ec41a1
🎨 pre-commit fixes
pre-commit-ci[bot] ded9cb5
:pencil2: Fixed typo
LiRem101 813a21c
:rotating_light: Fixed linter warnings
LiRem101 9664214
🎨 pre-commit fixes
pre-commit-ci[bot] 678a2a0
:rotating_light: Fixed linter warnings
LiRem101 9e75e06
:rotating_light: Fixed linter warnings
LiRem101 625e38a
:rotating_light: Fixed linter warnings
LiRem101 6101128
:poop: Checking if only one test fails on macos
LiRem101 47dbd6f
:rotating_light: Fixed linter warnings
LiRem101 aae1b08
:construction: Added const keyword
LiRem101 1f15741
:construction: Fixed span creation
LiRem101 9ef1878
:poop: Finding error cause for macos
LiRem101 7423621
:poop: Finding error cause for macos
LiRem101 3f07d43
:poop: Finding error cause for macos
LiRem101 1a920a6
:construction: Check satisfiability earlier
LiRem101 47230cc
:construction: Check satisfiability earlier
LiRem101 c52d3d5
:construction: Fixed span creation
LiRem101 5d97c9d
:construction: Fixed erasure of variable, Assisted-by: Copilot via Gi…
LiRem101 312ee45
:poop: Find macos Segfault cause
LiRem101 f1e8537
🎨 pre-commit fixes
pre-commit-ci[bot] 14fcdc0
:poop: Find macos Segfault cause
LiRem101 5f00eef
:poop: Find macos Segfault cause
LiRem101 0114bf5
:construction: Fix string
LiRem101 df57fcf
:poop: Find segfault on macos
LiRem101 cfee5e7
:poop: Find segfault on macos
LiRem101 f4b48ee
:poop: Find segfault on macos
LiRem101 a1fbbdc
:poop: Find segfault on macos
LiRem101 e9e64e1
:poop: Find segfault on macos
LiRem101 b038a5e
:poop: Find macos Segfault cause
LiRem101 f142205
:poop: Find macos Segfault cause
LiRem101 18d4d3d
:poop: Find macos Segfault cause
LiRem101 38ad358
:construction: Put yield operations always to null
LiRem101 8b17960
:fire: Remove Debug Strings
LiRem101 9b048ab
:bug: Prevented HS from being copied and not changed
LiRem101 b34644e
:white_check_mark: Added test to remove class ctrl from various gates
LiRem101 aaaded2
:white_check_mark: Added test for reset
LiRem101 b1cce36
:white_check_mark: Added test for classical operations
LiRem101 4c3d660
:white_check_mark: Added test to remove only some q ctrls
LiRem101 3f232f4
:white_check_mark: Added additional tests
LiRem101 3fc3c46
:pencil2: Fixed typo
LiRem101 caebb61
:white_check_mark: Added tests
LiRem101 4b71d9d
:white_check_mark: Added tests for gates
LiRem101 dbc94cc
:white_check_mark: Added tests
LiRem101 4cc8d34
:white_check_mark: Fixed test
LiRem101 bccd752
:memo: Corrected CHANGELOG.md formatting
LiRem101 29a98b4
🐛 Corrected matrix representations of gates
LiRem101 b9d8d63
:construction: Improved consistency in HybridState.hpp
LiRem101 85dc765
:construction: Adapted QuantumState.hpp to the style guidelines
LiRem101 f3d634b
🐛 Added check that prevents too large union tables to be created.
LiRem101 871ec96
🐛 Prevent overflowing
LiRem101 6089674
:construction: Added additional checks.
LiRem101 0eff94c
:zap: Improved HybridState == operator
LiRem101 fd5eacd
:construction: Do not alter non-owning span
LiRem101 0717d59
:construction: Added checks for edge-cases
LiRem101 eb6568f
:bug: Fixed that bt size is used instead of byte-size.
LiRem101 a121ce8
:white_check_mark: Adapted tests to contain more cases and have bette…
LiRem101 dba3930
:construction: Adapated code such that no errors are thrown
LiRem101 711fc2d
:construction: Optimized imports
LiRem101 74c646e
🎨 pre-commit fixes
pre-commit-ci[bot] 36fff56
:construction: Adapted handling of arith operation, assisted-by GPT 5…
LiRem101 48bc069
:construction: Corrected handling of arith operation, assisted-by GPT…
LiRem101 21c1dc2
:construction: Added handling of multiple conditions in a new branch
LiRem101 90c2d5d
:construction: Removed check if arguments are empty
LiRem101 4c3d5b3
:construction: Do not insert in empty vector anymore
LiRem101 b73d441
:construction: Prevented thenArgs and elseArgs from staying empty
LiRem101 66fe885
:construction: Prevented thenArgs and elseArgs from staying empty
LiRem101 b5dcdf6
:memo: Fixed issues in CHANGELOG.md
LiRem101 3b0a3b2
:construction: Fixed files that rebase broke
LiRem101 0fa42bb
:construction: Fixed conversion to float and DivSIOp
LiRem101 84a852e
🎨 pre-commit fixes
pre-commit-ci[bot] 3b4041b
:construction: Removed redundant guard
LiRem101 bc9deb0
:boom: Removed Optimization folders
LiRem101 76a6470
:construction: Corrected Optimizations/CMakeLists.txt
LiRem101 7b9a7ac
🎨 pre-commit fixes
pre-commit-ci[bot] baf7d1e
:construction: Corrected imports
LiRem101 ce2b26a
🎨 pre-commit fixes
pre-commit-ci[bot] ed6fbb3
:construction: Corrected nodiscard message
LiRem101 681fcc9
:construction: Prevented coping of unordered_map
LiRem101 35ad068
🎨 pre-commit fixes
pre-commit-ci[bot] 69fa7a3
:construction: Prevented multiplication by zero and change of span, c…
LiRem101 4102ff8
:twisted_rightwards_arrows: Merge branch 'mlir/constant-propagation' …
LiRem101 d276db4
:construction: Corrected Passes.td
LiRem101 8c58376
🎨 pre-commit fixes
pre-commit-ci[bot] 08ed321
:memo: Removed redundant copyright header
LiRem101 20ad3d9
Merge branch 'mlir/constant-propagation' of github.com:LiRem101/mqt-c…
LiRem101 3826b81
:construction: Safeguard against nullptr
LiRem101 b19709a
:boom: Remove constant propagation test
LiRem101 80ea810
:construction: Prevent inverted branches and clean formatting
LiRem101 f2cd251
:boom: Removed unnecessary guards
LiRem101 d83a32f
:construction: Correct constructor for QuantumState
LiRem101 5c6c993
:construction: Preventing certain answers if top and adapt to all llv…
LiRem101 5c15e9a
🎨 pre-commit fixes
pre-commit-ci[bot] 7db5031
:boom: Removed empty teardown
LiRem101 14c9d08
Merge branch 'mlir/constant-propagation' of github.com:LiRem101/mqt-c…
LiRem101 b4195b2
:construction: Adapted docstring and used reference value
LiRem101 ab4bc4a
:white_check_mark: Added tests for double handling
LiRem101 41373e0
🎨 pre-commit fixes
pre-commit-ci[bot] f963806
:white_check_mark: Changed test for handling of classical operations
LiRem101 335b2c6
Merge branch 'mlir/constant-propagation' of github.com:LiRem101/mqt-c…
LiRem101 d5b5876
:boom: Removed empty TearDown()
LiRem101 b897e37
:construction: Ctrl not superfluous if it is TOP
LiRem101 18f2b59
🎨 pre-commit fixes
pre-commit-ci[bot] f2049d4
:construction: Continue to propagate ctrls if TOP is reached
LiRem101 65b2caf
:twisted_rightwards_arrows: Merged with main
LiRem101 d49b6df
:rotating_light: Removed unused references
LiRem101 3e7f235
:construction: Adapted ClassicalArithOperation.h to review
LiRem101 d9be1d1
:construction: Put constants into namespace
LiRem101 9bebec9
:construction: Preventing race condition
LiRem101 59d6ae7
:pencil2: Fixed typos
LiRem101 63526b7
🎨 pre-commit fixes
pre-commit-ci[bot] ae8bb0c
:construction: Fixed return type of id
LiRem101 0132bfb
:construction: Consider GPhase and classical yields
LiRem101 dd44774
🎨 pre-commit fixes
pre-commit-ci[bot] b50e7d5
:construction: Changed guard to pragma once
LiRem101 7fc46c6
:construction: Prevents copying of hybridState
LiRem101 b3e86f1
:construction: Fixed value shadowing
LiRem101 8590c36
:construction: Verify module before applying pass, fixed docstring
LiRem101 f175fe4
Merge branch 'mlir/constant-propagation' of github.com:LiRem101/mqt-c…
LiRem101 6c383f1
:construction: Correct test name
LiRem101 e452227
:construction: Changed counter type to unsigned int
LiRem101 c7e1bc6
:boom: Removed unnecessary guard
LiRem101 aa880ee
:construction: Use linearResults for results of ifOp
LiRem101 5be615e
:construction: Prevent to apply only classically controlled gphase
LiRem101 9c73508
✅ Added test for classical controlled gphase gate
LiRem101 823dcb0
:white_check_mark: Check if whole union table remains unchanged by gl…
LiRem101 9cff859
:construction: Make sure only linear result types are used from then …
LiRem101 91d5b85
:construction: Conder order of yielded values.
LiRem101 fc83327
:construction: Added test for swapped yielded values
LiRem101 31f0713
:twisted_rightwards_arrows: Merge branch 'main' into mlir/constant-pr…
LiRem101 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
Large diffs are not rendered by default.
Oops, something went wrong.
243 changes: 243 additions & 0 deletions
243
...e/mlir/Dialect/QCO/Transforms/Optimizations/ConstantPropagation/ClassicalArithOperation.h
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,243 @@ | ||
| /* | ||
| * Copyright (c) 2023 - 2026 Chair for Design Automation, TUM | ||
| * Copyright (c) 2025 - 2026 Munich Quantum Software Company GmbH | ||
| * All rights reserved. | ||
| * | ||
| * SPDX-License-Identifier: MIT | ||
| * | ||
| * Licensed under the MIT License | ||
| */ | ||
|
|
||
| #pragma once | ||
|
|
||
| #include "mlir/Dialect/QCO/IR/QCOOps.h" | ||
| #include "mlir/Dialect/QCO/Utils/Drivers.h" | ||
|
|
||
| #include <llvm/ADT/APFloat.h> | ||
| #include <llvm/ADT/APInt.h> | ||
| #include <llvm/Support/Casting.h> | ||
| #include <llvm/Support/ErrorHandling.h> | ||
| #include <mlir/Dialect/Arith/IR/Arith.h> | ||
| #include <mlir/IR/BuiltinTypeInterfaces.h> | ||
| #include <mlir/IR/BuiltinTypes.h> | ||
| #include <mlir/IR/Operation.h> | ||
| #include <mlir/Support/LLVM.h> | ||
|
|
||
| #include <cmath> | ||
| #include <cstdint> | ||
|
|
||
| /** | ||
| * This file provides information of available arith operations. It calculates | ||
| * the result of valid arith operations. Operations are only valid with one to | ||
| * two operands, not if they are applied to sequences. | ||
| */ | ||
| inline int64_t getArithIntegerOpResult(mlir::Operation* operation, | ||
| const int64_t value1, | ||
| const int64_t value2 = 0, | ||
| const int64_t value3 = 0) { | ||
|
|
||
| for (auto operand : operation->getOperands()) { | ||
| if (mlir::isa<mlir::VectorType>(operand.getType())) { | ||
| llvm::report_fatal_error( | ||
| "Constant propagation does not support vectors as classical types."); | ||
| } | ||
| } | ||
|
|
||
| const auto intTy = | ||
| dyn_cast<mlir::IntegerType>(operation->getResult(0).getType()); | ||
| if (!intTy) { | ||
| llvm::report_fatal_error( | ||
| "IntegerType is needed to apply arith::integer operation."); | ||
| } | ||
|
|
||
| const unsigned width = intTy.getWidth(); | ||
| if (width > 64) { | ||
| llvm::report_fatal_error( | ||
| "Result of an arith operation cannot be safely stored in a 64-bit " | ||
| "integer, which is required by constant propagation."); | ||
| } | ||
|
|
||
| // APInt respects the signedness of the underlying MLIR type. | ||
| const bool isSigned = intTy.isSigned(); | ||
| const llvm::APInt a(width, static_cast<uint64_t>(value1), isSigned); | ||
| const llvm::APInt b(width, static_cast<uint64_t>(value2), isSigned); | ||
| const llvm::APInt c(width, static_cast<uint64_t>(value3), isSigned); | ||
|
|
||
| return mlir::TypeSwitch<mlir::Operation*, int64_t>(operation) | ||
| .Case<mlir::arith::AddIOp>([&](auto) { return (a + b).getSExtValue(); }) | ||
| .Case<mlir::arith::AndIOp>([&](auto) { return (a & b).getSExtValue(); }) | ||
| .Case<mlir::arith::CmpIOp>([&](mlir::arith::CmpIOp cmp) { | ||
| using Pred = mlir::arith::CmpIPredicate; | ||
| bool result = false; | ||
| switch (cmp.getPredicate()) { | ||
| case Pred::eq: | ||
| result = (a == b); | ||
| break; | ||
| case Pred::ne: | ||
| result = (a != b); | ||
| break; | ||
| case Pred::slt: | ||
| result = a.slt(b); | ||
| break; | ||
| case Pred::sle: | ||
| result = a.sle(b); | ||
| break; | ||
| case Pred::sgt: | ||
| result = a.sgt(b); | ||
| break; | ||
| case Pred::sge: | ||
| result = a.sge(b); | ||
| break; | ||
| case Pred::ult: | ||
| result = a.ult(b); | ||
| break; | ||
| case Pred::ule: | ||
| result = a.ule(b); | ||
| break; | ||
| case Pred::ugt: | ||
| result = a.ugt(b); | ||
| break; | ||
| case Pred::uge: | ||
| result = a.uge(b); | ||
| break; | ||
| } | ||
|
|
||
| return result ? int64_t{1} : int64_t{0}; | ||
| }) | ||
| .Case<mlir::arith::DivSIOp>([&](auto) { | ||
| if (b.isZero()) { | ||
| llvm::report_fatal_error( | ||
| "Handling of division by zero in mlir::arith::DivSIOp is not " | ||
| "supported by constant propagation."); | ||
| } | ||
| if (b.isAllOnes() && a.isMinSignedValue()) { | ||
| llvm::report_fatal_error( | ||
| "Handling of INT_MIN / -1 in mlir::arith::DivSIOp is not " | ||
| "supported by constant propagation."); | ||
| } | ||
| return a.sdiv(b).getSExtValue(); | ||
| }) | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| .Case<mlir::arith::MaxSIOp>([&](auto) { | ||
| return a.getSExtValue() > b.getSExtValue() ? a.getSExtValue() | ||
| : b.getSExtValue(); | ||
| }) | ||
| .Case<mlir::arith::MinSIOp>([&](auto) { | ||
| return a.getSExtValue() < b.getSExtValue() ? a.getSExtValue() | ||
| : b.getSExtValue(); | ||
| }) | ||
| .Case<mlir::arith::MulIOp>([&](auto) { return (a * b).getSExtValue(); }) | ||
| .Case<mlir::arith::OrIOp>([&](auto) { return (a | b).getSExtValue(); }) | ||
| .Case<mlir::arith::SubIOp>([&](auto) { return (a - b).getSExtValue(); }) | ||
| .Case<mlir::arith::XOrIOp>([&](auto) { return (a ^ b).getSExtValue(); }) | ||
| .Case<mlir::arith::SelectOp>([&](auto) { | ||
| // SelectOp: first operand is the i1 condition. | ||
| // In our helper `value1` is the condition (0 == false). | ||
| return (a != llvm::APInt(width, 0, true)) ? b.getSExtValue() | ||
| : c.getSExtValue(); | ||
| }) | ||
| .Default([](auto*) { | ||
| llvm::report_fatal_error("Unsupported integer operation in " | ||
| "mlir::qco::classicalarithoperation"); | ||
| return 0; | ||
| }); | ||
| } | ||
|
|
||
| inline double getArithDoubleOpResult(mlir::Operation* operation, | ||
| const double value1, | ||
| const double value2 = 0.0) { | ||
| for (mlir::Value operand : operation->getOperands()) { | ||
| if (mlir::isa<mlir::VectorType>(operand.getType())) { | ||
| llvm::report_fatal_error( | ||
| "Constant propagation does not support vectors as classical types."); | ||
| } | ||
| } | ||
|
|
||
| const auto floatTy = | ||
| dyn_cast<mlir::FloatType>(operation->getResult(0).getType()); | ||
| if (!floatTy) { | ||
| llvm::report_fatal_error("Expected floating-point result type."); | ||
| } | ||
|
|
||
| const llvm::fltSemantics& sem = floatTy.getFloatSemantics(); | ||
| constexpr auto rm = llvm::APFloat::rmNearestTiesToEven; | ||
| bool losesInfo = false; | ||
|
|
||
| llvm::APFloat lhs(value1); | ||
| llvm::APFloat rhs(value2); | ||
| lhs.convert(sem, rm, &losesInfo); | ||
| if (losesInfo) { | ||
| llvm::report_fatal_error("value1 cannot be represented safely."); | ||
| } | ||
|
|
||
| losesInfo = false; | ||
| rhs.convert(sem, rm, &losesInfo); | ||
| if (losesInfo) { | ||
| llvm::report_fatal_error("value2 cannot be represented safely."); | ||
| } | ||
|
|
||
| llvm::APFloat result = lhs; | ||
|
|
||
| const bool supported = | ||
| mlir::TypeSwitch<mlir::Operation*, bool>(operation) | ||
| .Case<mlir::arith::AddFOp>([&](auto) { | ||
| result = lhs; | ||
| auto status = result.add(rhs, rm); | ||
| return (status & llvm::APFloat::opInvalidOp) == 0; | ||
| }) | ||
| .Case<mlir::arith::DivFOp>([&](auto) { | ||
| result = lhs; | ||
| auto status = result.divide(rhs, rm); | ||
| return (status & llvm::APFloat::opInvalidOp) == 0; | ||
| }) | ||
| .Case<mlir::arith::MaximumFOp>([&](auto) { | ||
| result = llvm::maximum(lhs, rhs); | ||
| return true; | ||
| }) | ||
| .Case<mlir::arith::MaxNumFOp>([&](auto) { | ||
| result = llvm::maxnum(lhs, rhs); | ||
| return true; | ||
| }) | ||
| .Case<mlir::arith::MinimumFOp>([&](auto) { | ||
| result = llvm::minimum(lhs, rhs); | ||
| return true; | ||
| }) | ||
| .Case<mlir::arith::MinNumFOp>([&](auto) { | ||
| result = llvm::minnum(lhs, rhs); | ||
| return true; | ||
| }) | ||
| .Case<mlir::arith::MulFOp>([&](auto) { | ||
| result = lhs; | ||
| auto status = result.multiply(rhs, rm); | ||
| return (status & llvm::APFloat::opInvalidOp) == 0; | ||
| }) | ||
| .Case<mlir::arith::NegFOp>([&](auto) { | ||
| result = lhs; | ||
| result.changeSign(); | ||
| return true; | ||
| }) | ||
| .Case<mlir::arith::RemFOp>([&](auto) { | ||
| result = lhs; | ||
| const auto status = result.mod(rhs); | ||
| return (status & llvm::APFloat::opInvalidOp) == 0; | ||
| }) | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| .Case<mlir::arith::SubFOp>([&](auto) { | ||
| result = lhs; | ||
| return result.subtract(rhs, rm) == llvm::APFloat::opOK; | ||
| }) | ||
| .Default([](auto) { return false; }); | ||
|
|
||
| if (!supported) { | ||
| llvm::report_fatal_error("Unsupported floating-point operation in " | ||
| "mlir::qco::classicalarithoperation"); | ||
| } | ||
|
|
||
| bool loseInfoResult = false; | ||
| result.convert(llvm::APFloat::IEEEdouble(), rm, &loseInfoResult); | ||
| if (loseInfoResult) { | ||
| llvm::report_fatal_error( | ||
| "Floating-point fold result cannot be represented safely as double."); | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| const double folded = result.convertToDouble(); | ||
|
|
||
| return folded; | ||
| } | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤖 AI text below 🤖
Please replace this handwritten arithmetic interpreter with MLIR's normal folding mechanism.
Operation::foldalready defines the semantics of these operations, andmqt::valueToConstantAttrshows how we use it in this repository. Storing classical values as MLIR attributes would also remove the parallel integer/double maps and make the analysis inherit upstream folding support instead of maintaining another operation list. This file should then disappear.