[Mlir-commits] [mlir] [mlir][EmitC] Expand the MemRefToEmitC pass - Lowering `reinterpret_cast` (PR #152610)

Paul Kirth llvmlistbot at llvm.org
Fri Aug 8 11:13:10 PDT 2025


================
@@ -269,6 +272,75 @@ struct ConvertLoad final : public OpConversionPattern<memref::LoadOp> {
   }
 };
 
+struct ConvertReinterpretCastOp final
+    : public OpConversionPattern<memref::ReinterpretCastOp> {
+  using OpConversionPattern::OpConversionPattern;
+
+  LogicalResult
+  matchAndRewrite(memref::ReinterpretCastOp castOp, OpAdaptor adaptor,
+                  ConversionPatternRewriter &rewriter) const override {
+
+    MemRefType srcType = cast<MemRefType>(castOp.getSource().getType());
+
+    MemRefType targetMemRefType =
+        cast<MemRefType>(castOp.getResult().getType());
+
+    auto srcInEmitC = convertMemRefType(srcType, getTypeConverter());
+    auto targetInEmitC =
+        convertMemRefType(targetMemRefType, getTypeConverter());
+    if (!srcInEmitC || !targetInEmitC) {
+      return rewriter.notifyMatchFailure(castOp.getLoc(),
+                                         "cannot convert memref type");
+    }
+    Location loc = castOp.getLoc();
+
+    auto srcArrayValue =
+        cast<TypedValue<emitc::ArrayType>>(adaptor.getSource());
+
+    emitc::ConstantOp zeroIndex = rewriter.create<emitc::ConstantOp>(
+        loc, rewriter.getIndexType(), rewriter.getIndexAttr(0));
+
+    auto createPointerFromEmitcArray =
+        [loc, &rewriter, &zeroIndex](
+            mlir::TypedValue<emitc::ArrayType> arrayValue) -> emitc::ApplyOp {
+      int64_t rank = arrayValue.getType().getRank();
+      llvm::SmallVector<mlir::Value> indices;
+      for (int i = 0; i < rank; ++i) {
+        indices.push_back(zeroIndex);
+      }
----------------
ilovepi wrote:

```suggestion
      llvm::SmallVector<mlir::Value> indices(rank, zeroIndex);
```

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


More information about the Mlir-commits mailing list