[Mlir-commits] [mlir] [MLIR] fix invalid `scf.index_switch` lowering to `cf.switch` when case values are large (PR #111590)

Keyi Zhang llvmlistbot at llvm.org
Thu Oct 10 08:52:42 PDT 2024


================
@@ -691,11 +691,14 @@ IndexSwitchLowering::matchAndRewrite(IndexSwitchOp op,
 
   // Cast switch index to integer case value.
   Value caseValue = rewriter.create<arith::IndexCastOp>(
-      op.getLoc(), rewriter.getI32Type(), op.getArg());
+      op.getLoc(), rewriter.getI64Type(), op.getArg());
----------------
Kuree wrote:

My take on this is that `LLVMConversionTarget` might not be the best way to resolve this. The `index` type is converted to integer types based on the data layout (or overridden by hand), as you suggested. On a 32-bit machine, e.g. `RV32`, `index` type is typically converted to `i32`. This still causes the overflow problem since we need `i64` for the switch argument. If the conversion bitwidth is overridden to `i64`, then it will affect other dialect conversion, such as `memref`, since `memref.extract_aligned_pointer_as_index` returns an `index` type. Having an `i64` casted to a `llvm.ptr` seems awkward on a 32-bit machine.

An alternative is to lower the LLVM in multiple steps with different conversion targets, but I am not sure if this approach is user friendly. Or am I missing something here?

https://github.com/llvm/llvm-project/pull/111590


More information about the Mlir-commits mailing list