Skip to content
Closed
Show file tree
Hide file tree
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 Mar 26, 2026
5eac04f
:test_tube: Added test cases for controlled Pauli Z lifting
LiRem101 Mar 27, 2026
4b76cb6
:construction: Added framework for hadamard lifting pass
LiRem101 Mar 27, 2026
cfd5d36
:construction: Started exchange of Hadamard and Pauli
LiRem101 Mar 30, 2026
6e28a1e
:construction: Lifting uncontrolled Hadamard gates above Pauli
LiRem101 Mar 31, 2026
e7226e3
:white_check_mark: Added canonicalization to references
LiRem101 Mar 31, 2026
e57efd3
:white_check_mark: Corrected gates
LiRem101 Mar 31, 2026
024a665
:construction: Added handling of controlled gates
LiRem101 Apr 1, 2026
1c8dc3d
:recycle: Refactored handling of controlled gates
LiRem101 Apr 1, 2026
7a7c4f4
:construction: Added handling of controlled Pauli Z gates
LiRem101 Apr 1, 2026
a511116
:recycle: Refactored handling of controlled Pauli Z gates
LiRem101 Apr 1, 2026
a55ca13
:construction: Added CNOT Hadamard handling
LiRem101 Apr 1, 2026
6dc5439
✅ Corrected test
LiRem101 Apr 2, 2026
2a4a26d
:rotating_light: Fixed linter warnings
LiRem101 Apr 2, 2026
4a90aa7
:rotating_light: Fixed linter warnings
LiRem101 Apr 2, 2026
159cc25
:construction: Added infrastructure to use hadamard lifting
LiRem101 Apr 2, 2026
4a8cbe2
:pencil2: Changed Preceeding to preceding
LiRem101 Apr 2, 2026
f7a5607
🎨 pre-commit fixes
pre-commit-ci[bot] Apr 2, 2026
9f73280
:rotating_light: Fixed linter warnings
LiRem101 Apr 7, 2026
4d328fb
:rotating_light: Optimized imports
LiRem101 Apr 7, 2026
7b96671
:rotating_light: Changed dyn_cast to llvm
LiRem101 Apr 7, 2026
10a6943
:construction: Removed double MLIRParser in CMakeLists
LiRem101 Apr 8, 2026
00554d4
:recycle: use is_permutation
LiRem101 Apr 8, 2026
b67526c
:bug: Controlled Pauli Y cannot be lifted
LiRem101 Apr 8, 2026
5c119ea
:rotating_light: Optimized imports and test naming
LiRem101 Apr 8, 2026
62757af
:construction: Apply suggestions from code review
LiRem101 Apr 15, 2026
2883681
:construction: Adapated code to suggestions from code review
LiRem101 Apr 15, 2026
ddd9a78
:construction: Implemented changes suggested by review, removed handl…
LiRem101 Apr 15, 2026
a375669
:construction: Added global phase for Y-H commutation
LiRem101 Apr 16, 2026
f8e0bf4
:construction: Added TypeSwitch
LiRem101 Apr 16, 2026
2c60097
:memo: Improved documentation
LiRem101 Apr 16, 2026
9a51157
:recycle: Removed temporary value and branchings, assisted-by: GPT 5 …
LiRem101 Apr 16, 2026
7d35dea
:construction: Implemented changes requested in review
LiRem101 Apr 16, 2026
bb2ed7f
:white_check_mark: Added test for measurement after target of CNOT
LiRem101 Apr 16, 2026
fe5c9b9
:construction: Implemented changes requested in review
LiRem101 Apr 17, 2026
00b32dc
:rotating_light: Fix linter errors
LiRem101 Apr 17, 2026
0335023
:rotating_light: Fix linter errors
LiRem101 Apr 17, 2026
05f7b27
:rotating_light: Fix linter errors
LiRem101 Apr 17, 2026
b40ad9c
:rotating_light: Fix linter errors
LiRem101 Apr 17, 2026
699fea7
:rotating_light: Fix linter errors
LiRem101 Apr 17, 2026
1915c52
:bug: Fix dangling range, assisted-by: GPT 5 via KI:connect
LiRem101 Apr 20, 2026
e94d224
:bug: Fixed dangling hadamard gate object
LiRem101 Apr 20, 2026
2f22a20
:bricks: Add files for tracking QuantumStates
LiRem101 Jun 9, 2026
e4b8666
:construction: Added GateToMap.h
LiRem101 Jun 10, 2026
0f27d85
:recycle: Refactored GateToMap.h, Assisted-by: GPT OSS via KI:connect
LiRem101 Jun 10, 2026
ffbf98e
📝 Added Docstring
LiRem101 Jun 10, 2026
4448c47
:construction: Added Header File for QuantumState
LiRem101 Jun 10, 2026
b0e0991
:recycle: Refactored QuantumState.hpp, assisted-by GPT OSS via KI:con…
LiRem101 Jun 10, 2026
6241f67
:construction: Added QuantumState class
LiRem101 Jun 11, 2026
dff4b83
:recycle: Refactored QuantumState.hpp and QuantumState.cpp, assisted-…
LiRem101 Jun 11, 2026
49f8829
✅ Added tests for QuantumState
LiRem101 Jun 11, 2026
bcf0659
:white_check_mark: Passes first test
LiRem101 Jun 12, 2026
31da770
:white_check_mark: Passed tests
LiRem101 Jun 12, 2026
d2e5095
:recycle: Refactored QuantumState.hpp
LiRem101 Jun 12, 2026
0a78ff8
:recycle: Refactored QuantumState.hpp, assisted-by GPT-OSS va KI:connect
LiRem101 Jun 12, 2026
fb4e85d
:construction: Corrected macros
LiRem101 Jun 12, 2026
0098669
:rotating_light: Corrected clang errors
LiRem101 Jun 12, 2026
b4f1fb5
🚧 Created HybridState.hpp
LiRem101 Jun 12, 2026
4987976
🚧 Adapted HybridState and created ClassicalArithOperation
LiRem101 Jun 16, 2026
3bab3ce
🚧 Added HybridState implementation
LiRem101 Jun 17, 2026
76cd4d0
🚧 Added tests for HybridState
LiRem101 Jun 17, 2026
e463022
:white_check_mark: Added tests for classical operations
LiRem101 Jun 18, 2026
7374fc5
:recycle: Refactored HybridState
LiRem101 Jun 18, 2026
56c7e1c
:construction: Added UnionTable infrastructure
LiRem101 Jun 18, 2026
d95797f
:white_check_mark: Added UnionTable tests
LiRem101 Jun 18, 2026
4ef17bb
:construction: Added UnionTable methods
LiRem101 Jun 19, 2026
062f3bf
:construction: Added UnionTable methods
LiRem101 Jun 19, 2026
dae1e83
:construction: Corrected handling of maps
LiRem101 Jun 19, 2026
a053ff0
:construction: Corrected handling of maps
LiRem101 Jun 19, 2026
47070a1
:construction: Corrected several test problems
LiRem101 Jun 22, 2026
76bdfe7
:construction: Added additional tests for UnionTable
LiRem101 Jun 22, 2026
9211659
:construction: Added getValueThatIsEquivalentToQubit
LiRem101 Jun 23, 2026
f17003b
:construction: Added UnionTableFunctionality
LiRem101 Jun 24, 2026
760ec3e
:construction: Fixed test results
LiRem101 Jun 24, 2026
edd708b
:construction: Fixed tests
LiRem101 Jun 25, 2026
0f57eae
:construction: Added infrastructure for constant propagation passes
LiRem101 Jun 26, 2026
c78de89
:test_tube: Added first test for constant propagation
LiRem101 Jun 26, 2026
762c1b6
:construction: Continued constant propagation infrastructure
LiRem101 Jun 26, 2026
eaedac0
:test_tube: Added constant propagation test
LiRem101 Jun 29, 2026
fbcbc05
:construction: Global phase as double
LiRem101 Jun 29, 2026
b0b84ad
:construction: Handling of global phases without control
LiRem101 Jun 29, 2026
7b2a7e4
:construction: Ctrl qubits changed after gate
LiRem101 Jun 29, 2026
f9b18ca
:construction: Start of ctrl handling
LiRem101 Jun 29, 2026
14a741d
:construction: Handling of ctrl continued
LiRem101 Jun 30, 2026
878b6a3
:construction: Handling of ctrl
LiRem101 Jun 30, 2026
b12db95
:memo: Added docstrings
LiRem101 Jun 30, 2026
427d7bc
:construction: Added creation of classical value inside measurement
LiRem101 Jun 30, 2026
553ac91
:construction: Added reset and measurement handling
LiRem101 Jun 30, 2026
aaaa004
:construction: Started removal of ctrl of always executed gates
LiRem101 Jun 30, 2026
f8df9f9
:construction: Removal of ctrl of always executed gates
LiRem101 Jun 30, 2026
1ac6c4a
:construction: Started removal of ctrls that are superfluous
LiRem101 Jun 30, 2026
7ca1cc3
✅ Fixed test
LiRem101 Jun 30, 2026
25f1331
:memo: Added docstring
LiRem101 Jul 1, 2026
fce3a4c
:construction: Continued removal of ctrls in CtrlOp
LiRem101 Jul 1, 2026
7a25ed3
:construction: Fixed creation of uncontrolled gates
LiRem101 Jul 1, 2026
9d63dc7
:test_tube: Added test to check propagation in classical branching
LiRem101 Jul 1, 2026
57da507
:construction: Added propagation of classical branching
LiRem101 Jul 1, 2026
dfd85b7
:test_tube: Extended test for removal of classical condition
LiRem101 Jul 2, 2026
6dbe893
:construction: Removal of branch if condition is one
LiRem101 Jul 2, 2026
a2c7161
:construction: Added handling to remove classical branches
LiRem101 Jul 2, 2026
dfa3786
:construction: First draft of creating branches
LiRem101 Jul 2, 2026
cab21cc
:construction: Create branches during constant propagation
LiRem101 Jul 2, 2026
fed2841
:test_tube: Added tests for movement of measurement
LiRem101 Jul 2, 2026
2ee5519
:construction: Added moving measurement in front
LiRem101 Jul 2, 2026
b3ed30f
:construction: Added handling of classical operations
LiRem101 Jul 2, 2026
0199caa
:construction: Creating then and else branches
LiRem101 Jul 2, 2026
d0dea4e
:construction: Added usage of parameters
LiRem101 Jul 2, 2026
ed40d43
:construction: Added pipeline frame
LiRem101 Jul 2, 2026
e45f3b5
:construction: Adapted to changes from main
LiRem101 Jul 2, 2026
cb94911
:memo: Added change information to CHANGELOG.md
LiRem101 Jul 2, 2026
d827393
🎨 pre-commit fixes
pre-commit-ci[bot] Jul 2, 2026
c3d0f30
:rotating_light: Fixed linter warnings
LiRem101 Jul 14, 2026
19698db
🎨 pre-commit fixes
pre-commit-ci[bot] Jul 2, 2026
0ec41a1
🎨 pre-commit fixes
pre-commit-ci[bot] Jul 14, 2026
ded9cb5
:pencil2: Fixed typo
LiRem101 Jul 14, 2026
813a21c
:rotating_light: Fixed linter warnings
LiRem101 Jul 14, 2026
9664214
🎨 pre-commit fixes
pre-commit-ci[bot] Jul 14, 2026
678a2a0
:rotating_light: Fixed linter warnings
LiRem101 Jul 14, 2026
9e75e06
:rotating_light: Fixed linter warnings
LiRem101 Jul 14, 2026
625e38a
:rotating_light: Fixed linter warnings
LiRem101 Jul 14, 2026
6101128
:poop: Checking if only one test fails on macos
LiRem101 Jul 14, 2026
47dbd6f
:rotating_light: Fixed linter warnings
LiRem101 Jul 14, 2026
aae1b08
:construction: Added const keyword
LiRem101 Jul 14, 2026
1f15741
:construction: Fixed span creation
LiRem101 Jul 14, 2026
9ef1878
:poop: Finding error cause for macos
LiRem101 Jul 14, 2026
7423621
:poop: Finding error cause for macos
LiRem101 Jul 14, 2026
3f07d43
:poop: Finding error cause for macos
LiRem101 Jul 14, 2026
1a920a6
:construction: Check satisfiability earlier
LiRem101 Jul 14, 2026
47230cc
:construction: Check satisfiability earlier
LiRem101 Jul 14, 2026
c52d3d5
:construction: Fixed span creation
LiRem101 Jul 14, 2026
5d97c9d
:construction: Fixed erasure of variable, Assisted-by: Copilot via Gi…
LiRem101 Jul 14, 2026
312ee45
:poop: Find macos Segfault cause
LiRem101 Jul 14, 2026
f1e8537
🎨 pre-commit fixes
pre-commit-ci[bot] Jul 14, 2026
14fcdc0
:poop: Find macos Segfault cause
LiRem101 Jul 14, 2026
5f00eef
:poop: Find macos Segfault cause
LiRem101 Jul 15, 2026
0114bf5
:construction: Fix string
LiRem101 Jul 15, 2026
df57fcf
:poop: Find segfault on macos
LiRem101 Jul 15, 2026
cfee5e7
:poop: Find segfault on macos
LiRem101 Jul 15, 2026
f4b48ee
:poop: Find segfault on macos
LiRem101 Jul 15, 2026
a1fbbdc
:poop: Find segfault on macos
LiRem101 Jul 15, 2026
e9e64e1
:poop: Find segfault on macos
LiRem101 Jul 15, 2026
b038a5e
:poop: Find macos Segfault cause
LiRem101 Jul 15, 2026
f142205
:poop: Find macos Segfault cause
LiRem101 Jul 15, 2026
18d4d3d
:poop: Find macos Segfault cause
LiRem101 Jul 15, 2026
38ad358
:construction: Put yield operations always to null
LiRem101 Jul 15, 2026
8b17960
:fire: Remove Debug Strings
LiRem101 Jul 15, 2026
9b048ab
:bug: Prevented HS from being copied and not changed
LiRem101 Jul 17, 2026
b34644e
:white_check_mark: Added test to remove class ctrl from various gates
LiRem101 Jul 17, 2026
aaaded2
:white_check_mark: Added test for reset
LiRem101 Jul 17, 2026
b1cce36
:white_check_mark: Added test for classical operations
LiRem101 Jul 17, 2026
4c3d660
:white_check_mark: Added test to remove only some q ctrls
LiRem101 Jul 17, 2026
3f232f4
:white_check_mark: Added additional tests
LiRem101 Jul 17, 2026
3fc3c46
:pencil2: Fixed typo
LiRem101 Jul 17, 2026
caebb61
:white_check_mark: Added tests
LiRem101 Jul 17, 2026
4b71d9d
:white_check_mark: Added tests for gates
LiRem101 Jul 17, 2026
dbc94cc
:white_check_mark: Added tests
LiRem101 Jul 17, 2026
4cc8d34
:white_check_mark: Fixed test
LiRem101 Jul 17, 2026
bccd752
:memo: Corrected CHANGELOG.md formatting
LiRem101 Aug 16, 2026
29a98b4
🐛 Corrected matrix representations of gates
LiRem101 Aug 16, 2026
b9d8d63
:construction: Improved consistency in HybridState.hpp
LiRem101 Aug 16, 2026
85dc765
:construction: Adapted QuantumState.hpp to the style guidelines
LiRem101 Aug 16, 2026
f3d634b
🐛 Added check that prevents too large union tables to be created.
LiRem101 Aug 16, 2026
871ec96
🐛 Prevent overflowing
LiRem101 Aug 16, 2026
6089674
:construction: Added additional checks.
LiRem101 Aug 16, 2026
0eff94c
:zap: Improved HybridState == operator
LiRem101 Aug 16, 2026
fd5eacd
:construction: Do not alter non-owning span
LiRem101 Aug 16, 2026
0717d59
:construction: Added checks for edge-cases
LiRem101 Aug 16, 2026
eb6568f
:bug: Fixed that bt size is used instead of byte-size.
LiRem101 Aug 16, 2026
a121ce8
:white_check_mark: Adapted tests to contain more cases and have bette…
LiRem101 Aug 16, 2026
dba3930
:construction: Adapated code such that no errors are thrown
LiRem101 Aug 17, 2026
711fc2d
:construction: Optimized imports
LiRem101 Aug 17, 2026
74c646e
🎨 pre-commit fixes
pre-commit-ci[bot] Aug 17, 2026
36fff56
:construction: Adapted handling of arith operation, assisted-by GPT 5…
LiRem101 Aug 17, 2026
48bc069
:construction: Corrected handling of arith operation, assisted-by GPT…
LiRem101 Aug 17, 2026
21c1dc2
:construction: Added handling of multiple conditions in a new branch
LiRem101 Aug 18, 2026
90c2d5d
:construction: Removed check if arguments are empty
LiRem101 Aug 18, 2026
4c3d5b3
:construction: Do not insert in empty vector anymore
LiRem101 Aug 18, 2026
b73d441
:construction: Prevented thenArgs and elseArgs from staying empty
LiRem101 Aug 19, 2026
66fe885
:construction: Prevented thenArgs and elseArgs from staying empty
LiRem101 Aug 19, 2026
b5dcdf6
:memo: Fixed issues in CHANGELOG.md
LiRem101 Aug 19, 2026
3b0a3b2
:construction: Fixed files that rebase broke
LiRem101 Aug 19, 2026
0fa42bb
:construction: Fixed conversion to float and DivSIOp
LiRem101 Aug 19, 2026
84a852e
🎨 pre-commit fixes
pre-commit-ci[bot] Aug 19, 2026
3b4041b
:construction: Removed redundant guard
LiRem101 Aug 19, 2026
bc9deb0
:boom: Removed Optimization folders
LiRem101 Aug 19, 2026
76a6470
:construction: Corrected Optimizations/CMakeLists.txt
LiRem101 Aug 19, 2026
7b9a7ac
🎨 pre-commit fixes
pre-commit-ci[bot] Aug 19, 2026
baf7d1e
:construction: Corrected imports
LiRem101 Aug 19, 2026
ce2b26a
🎨 pre-commit fixes
pre-commit-ci[bot] Aug 19, 2026
ed6fbb3
:construction: Corrected nodiscard message
LiRem101 Aug 19, 2026
681fcc9
:construction: Prevented coping of unordered_map
LiRem101 Aug 19, 2026
35ad068
🎨 pre-commit fixes
pre-commit-ci[bot] Aug 19, 2026
69fa7a3
:construction: Prevented multiplication by zero and change of span, c…
LiRem101 Aug 19, 2026
4102ff8
:twisted_rightwards_arrows: Merge branch 'mlir/constant-propagation' …
LiRem101 Aug 19, 2026
d276db4
:construction: Corrected Passes.td
LiRem101 Aug 19, 2026
8c58376
🎨 pre-commit fixes
pre-commit-ci[bot] Aug 19, 2026
08ed321
:memo: Removed redundant copyright header
LiRem101 Aug 19, 2026
20ad3d9
Merge branch 'mlir/constant-propagation' of github.com:LiRem101/mqt-c…
LiRem101 Aug 19, 2026
3826b81
:construction: Safeguard against nullptr
LiRem101 Aug 19, 2026
b19709a
:boom: Remove constant propagation test
LiRem101 Aug 19, 2026
80ea810
:construction: Prevent inverted branches and clean formatting
LiRem101 Aug 19, 2026
f2cd251
:boom: Removed unnecessary guards
LiRem101 Aug 19, 2026
d83a32f
:construction: Correct constructor for QuantumState
LiRem101 Aug 19, 2026
5c6c993
:construction: Preventing certain answers if top and adapt to all llv…
LiRem101 Aug 19, 2026
5c15e9a
🎨 pre-commit fixes
pre-commit-ci[bot] Aug 19, 2026
7db5031
:boom: Removed empty teardown
LiRem101 Aug 19, 2026
14c9d08
Merge branch 'mlir/constant-propagation' of github.com:LiRem101/mqt-c…
LiRem101 Aug 19, 2026
b4195b2
:construction: Adapted docstring and used reference value
LiRem101 Aug 19, 2026
ab4bc4a
:white_check_mark: Added tests for double handling
LiRem101 Aug 19, 2026
41373e0
🎨 pre-commit fixes
pre-commit-ci[bot] Aug 19, 2026
f963806
:white_check_mark: Changed test for handling of classical operations
LiRem101 Aug 19, 2026
335b2c6
Merge branch 'mlir/constant-propagation' of github.com:LiRem101/mqt-c…
LiRem101 Aug 19, 2026
d5b5876
:boom: Removed empty TearDown()
LiRem101 Aug 19, 2026
b897e37
:construction: Ctrl not superfluous if it is TOP
LiRem101 Aug 19, 2026
18f2b59
🎨 pre-commit fixes
pre-commit-ci[bot] Aug 19, 2026
f2049d4
:construction: Continue to propagate ctrls if TOP is reached
LiRem101 Aug 19, 2026
65b2caf
:twisted_rightwards_arrows: Merged with main
LiRem101 Aug 20, 2026
d49b6df
:rotating_light: Removed unused references
LiRem101 Aug 20, 2026
3e7f235
:construction: Adapted ClassicalArithOperation.h to review
LiRem101 Aug 20, 2026
d9be1d1
:construction: Put constants into namespace
LiRem101 Aug 20, 2026
9bebec9
:construction: Preventing race condition
LiRem101 Aug 20, 2026
59d6ae7
:pencil2: Fixed typos
LiRem101 Aug 20, 2026
63526b7
🎨 pre-commit fixes
pre-commit-ci[bot] Aug 20, 2026
ae8bb0c
:construction: Fixed return type of id
LiRem101 Aug 20, 2026
0132bfb
:construction: Consider GPhase and classical yields
LiRem101 Aug 20, 2026
dd44774
🎨 pre-commit fixes
pre-commit-ci[bot] Aug 20, 2026
b50e7d5
:construction: Changed guard to pragma once
LiRem101 Aug 20, 2026
7fc46c6
:construction: Prevents copying of hybridState
LiRem101 Aug 20, 2026
b3e86f1
:construction: Fixed value shadowing
LiRem101 Aug 20, 2026
8590c36
:construction: Verify module before applying pass, fixed docstring
LiRem101 Aug 20, 2026
f175fe4
Merge branch 'mlir/constant-propagation' of github.com:LiRem101/mqt-c…
LiRem101 Aug 20, 2026
6c383f1
:construction: Correct test name
LiRem101 Aug 20, 2026
e452227
:construction: Changed counter type to unsigned int
LiRem101 Aug 20, 2026
c7e1bc6
:boom: Removed unnecessary guard
LiRem101 Aug 20, 2026
aa880ee
:construction: Use linearResults for results of ifOp
LiRem101 Aug 20, 2026
5be615e
:construction: Prevent to apply only classically controlled gphase
LiRem101 Aug 20, 2026
9c73508
✅ Added test for classical controlled gphase gate
LiRem101 Aug 20, 2026
823dcb0
:white_check_mark: Check if whole union table remains unchanged by gl…
LiRem101 Aug 20, 2026
9cff859
:construction: Make sure only linear result types are used from then …
LiRem101 Aug 20, 2026
91d5b85
:construction: Conder order of yielded values.
LiRem101 Aug 20, 2026
fc83327
:construction: Added test for swapped yielded values
LiRem101 Aug 20, 2026
31f0713
:twisted_rightwards_arrows: Merge branch 'main' into mlir/constant-pr…
LiRem101 Aug 22, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
229 changes: 158 additions & 71 deletions CHANGELOG.md

Large diffs are not rendered by default.

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,

Copy link
Copy Markdown
Member

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::fold already defines the semantics of these operations, and mqt::valueToConstantAttr shows 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.

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();
})
Comment thread
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;
})
Comment thread
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.");
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
const double folded = result.convertToDouble();

return folded;
}
Loading
Loading