[Mlir-commits] [mlir] [wip][mlir][spirv] Add pattern matching for arith.index_cast i1 to index (PR #155729)

Md Abdullah Shahneous Bari llvmlistbot at llvm.org
Thu Aug 28 08:49:58 PDT 2025


================
@@ -607,6 +607,41 @@ struct UIToFPI1Pattern final : public OpConversionPattern<arith::UIToFPOp> {
   }
 };
 
+//===----------------------------------------------------------------------===//
+// IndexCastOp
+//===----------------------------------------------------------------------===//
+
+/// Converts arith.index_cast to spirv.Select if the type of source is i1 or
+/// vector of i1.
+struct IndexCastI1Pattern final : public OpConversionPattern<arith::IndexCastOp> {
+  using OpConversionPattern::OpConversionPattern;
+
+  LogicalResult
+  matchAndRewrite(arith::IndexCastOp op, OpAdaptor adaptor,
+                  ConversionPatternRewriter &rewriter) const override {
+    Type srcType = adaptor.getOperands().front().getType();
+    if (!srcType.isInteger(1))
+      return failure();
+
+    Type dstType = getTypeConverter()->convertType(op.getType());
+    if (!dstType)
+      return getTypeConversionFailure(rewriter, op);
+    // if (!dstType.isIndex()) {
+    //   llvm::errs() << "why doesnt this work?\n";
+    //   return failure();
+    // }
+
+    auto *converter = this->template getTypeConverter<SPIRVTypeConverter>();
+    Location loc = op.getLoc();
+    Type spirvI32T = converter->getIndexType();
+    Value zero = spirv::ConstantOp::getZero(spirvI32T, loc, rewriter);
----------------
mshahneo wrote:

You can actually use the dstType directly here to create the constant. The reason for this is, index type gets converted to i64 or i32 depending on addressing model in SPIR-V. Therefore, creating an i32 type constant could lead to failure in some cases where addressing mode is 64-bit addressing. Therefore, you can directly use the dstType, which is already converted to the correct type by the SPIR-V type converter.

```suggestion
    Value zero = spirv::ConstantOp::getZero(dstType, loc, rewriter);
```

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


More information about the Mlir-commits mailing list