[Mlir-commits] [mlir] 59f4070 - [MLIR] Fix printing of switch case for negative value (#129266)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Mon Mar 3 01:14:36 PST 2025


Author: Robert Konicar
Date: 2025-03-03T10:14:33+01:00
New Revision: 59f407020ea60d46af974563e4b87b8d9f188802

URL: https://github.com/llvm/llvm-project/commit/59f407020ea60d46af974563e4b87b8d9f188802
DIFF: https://github.com/llvm/llvm-project/commit/59f407020ea60d46af974563e4b87b8d9f188802.diff

LOG: [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.

Added: 
    

Modified: 
    mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp
    mlir/test/Dialect/LLVMIR/roundtrip.mlir

Removed: 
    


################################################################################
diff  --git a/mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp b/mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp
index ccf8f72b2b230..fb9236fcc640d 100644
--- a/mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp
+++ b/mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp
@@ -634,7 +634,7 @@ static void printSwitchOpCases(OpAsmPrinter &p, SwitchOp op, Type flagType,
       llvm::zip(caseValues, caseDestinations),
       [&](auto i) {
         p << "  ";
-        p << std::get<0>(i).getLimitedValue();
+        p << std::get<0>(i);
         p << ": ";
         p.printSuccessorAndUseList(std::get<1>(i), caseOperands[index++]);
       },

diff  --git a/mlir/test/Dialect/LLVMIR/roundtrip.mlir b/mlir/test/Dialect/LLVMIR/roundtrip.mlir
index 09a0cd57e2675..e0a17308af828 100644
--- a/mlir/test/Dialect/LLVMIR/roundtrip.mlir
+++ b/mlir/test/Dialect/LLVMIR/roundtrip.mlir
@@ -144,12 +144,14 @@ func.func @ops(%arg0: i32, %arg1: f32,
   // CHECK:      llvm.switch %0 : i32, ^[[BB3]] [
   // CHECK-NEXT:   1: ^[[BB4:.*]],
   // CHECK-NEXT:   2: ^[[BB5:.*]],
-  // CHECK-NEXT:   3: ^[[BB6:.*]]
+  // CHECK-NEXT:   3: ^[[BB6:.*]],
+  // CHECK-NEXT:   -3: ^[[BB6:.*]]
   // CHECK-NEXT: ]
   llvm.switch %0 : i32, ^bb3 [
     1: ^bb4,
     2: ^bb5,
-    3: ^bb6
+    3: ^bb6,
+    -3: ^bb6
   ]
 
 // CHECK: ^[[BB3]]


        


More information about the Mlir-commits mailing list