Skip to content

Commit 39c1d00

Browse files
authored
Bug fix for comparison of large cbits and integer literals (#168)
This PR fixes a bug that caused verification failure on the generated QUIR when comparing large cbits (> 32 bits) with integer literals. This was caused by failing to cast to match bit width because we weren't updating the tracked type after casting the reference to convert from cbit to int.
1 parent 3440a72 commit 39c1d00

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

lib/Frontend/OpenQASM3/QUIRGenQASM3Visitor.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1564,10 +1564,12 @@ ExpressionValueType QUIRGenQASM3Visitor::visit_(const ASTBinaryOpNode *node) {
15641564
if (leftType.isa<quir::CBitType>()) {
15651565
leftRef = builder.create<CastOp>(getLocation(left),
15661566
builder.getIntegerType(bits), leftRef);
1567+
leftType = leftRef.getType();
15671568
}
15681569
if (rightType.isa<quir::CBitType>()) {
15691570
rightRef = builder.create<CastOp>(getLocation(right),
15701571
builder.getIntegerType(bits), rightRef);
1572+
rightType = rightRef.getType();
15711573
}
15721574
// integer types of different sizes
15731575
if (leftType != rightType && leftType.isIntOrIndex() &&

test/Frontend/OpenQASM3/compare-bits.qasm

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,12 @@ if (bit_two != bit_one){
108108
if (bit_one != bitstring[0]) {
109109
U(0, 0, 0) $0;
110110
}
111+
112+
// MLIR-DAG: [[BIG_BITS:%.*]] = oq3.variable_load @big_bits : !quir.cbit<33>
113+
// MLIR-DAG: [[ZERO_32:%.*]] = arith.constant 0 : i32
114+
// MLIR-DAG: [[LHS:%.*]] = "oq3.cast"([[BIG_BITS]]) : (!quir.cbit<33>) -> i33
115+
// MLIR-DAG: [[RHS:%.*]] = "oq3.cast"([[ZERO_32]]) : (i32) -> i33
116+
// MLIR: [[CMP:%.*]] = arith.cmpi eq, [[LHS]], [[RHS]] : i33
117+
// MLIR: scf.if [[CMP]] {
118+
bit[33] big_bits;
119+
if (big_bits == 0) {}

0 commit comments

Comments
 (0)