Skip to content

Commit 59f4070

Browse files
authored
[MLIR] Fix printing of switch case for negative value (#129266)
This patch fixes the printer for the `llvm.switch` operation with negative values in a case. The previous behaviour printed the value as an unsigned integer, as the `getLimitedValue()` returns unsigned value. This caused the roundtrip to fail (assertion in `APInt`), as the printed unsigned integer could not be parsed into the same amount of bits in a signed integer. I don't see a good reason for keeping any restriction on the printed value, as LLVMIR `switch` afaik does not have a limit on the bitwidth of the values and `APInt` handles printing just fine.
1 parent cb113a7 commit 59f4070

File tree

2 files changed

+5
-3
lines changed

2 files changed

+5
-3
lines changed

mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -634,7 +634,7 @@ static void printSwitchOpCases(OpAsmPrinter &p, SwitchOp op, Type flagType,
634634
llvm::zip(caseValues, caseDestinations),
635635
[&](auto i) {
636636
p << " ";
637-
p << std::get<0>(i).getLimitedValue();
637+
p << std::get<0>(i);
638638
p << ": ";
639639
p.printSuccessorAndUseList(std::get<1>(i), caseOperands[index++]);
640640
},

mlir/test/Dialect/LLVMIR/roundtrip.mlir

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,12 +144,14 @@ func.func @ops(%arg0: i32, %arg1: f32,
144144
// CHECK: llvm.switch %0 : i32, ^[[BB3]] [
145145
// CHECK-NEXT: 1: ^[[BB4:.*]],
146146
// CHECK-NEXT: 2: ^[[BB5:.*]],
147-
// CHECK-NEXT: 3: ^[[BB6:.*]]
147+
// CHECK-NEXT: 3: ^[[BB6:.*]],
148+
// CHECK-NEXT: -3: ^[[BB6:.*]]
148149
// CHECK-NEXT: ]
149150
llvm.switch %0 : i32, ^bb3 [
150151
1: ^bb4,
151152
2: ^bb5,
152-
3: ^bb6
153+
3: ^bb6,
154+
-3: ^bb6
153155
]
154156

155157
// CHECK: ^[[BB3]]

0 commit comments

Comments
 (0)