[Mlir-commits] [mlir] [mlir][EmitC] Expand the MemRefToEmitC pass - Lowering `CopyOp` (PR #151206)
Gil Rapaport
llvmlistbot at llvm.org
Tue Aug 5 05:56:19 PDT 2025
================
@@ -159,6 +185,71 @@ struct ConvertAlloc final : public OpConversionPattern<memref::AllocOp> {
}
};
+struct ConvertCopy final : public OpConversionPattern<memref::CopyOp> {
+ using OpConversionPattern::OpConversionPattern;
+
+ LogicalResult
+ matchAndRewrite(memref::CopyOp copyOp, OpAdaptor operands,
+ ConversionPatternRewriter &rewriter) const override {
+ Location loc = copyOp.getLoc();
+ MemRefType srcMemrefType = cast<MemRefType>(copyOp.getSource().getType());
+ MemRefType targetMemrefType =
+ cast<MemRefType>(copyOp.getTarget().getType());
+
+ if (!isMemRefTypeLegalForEmitC(srcMemrefType)) {
+ return rewriter.notifyMatchFailure(
+ loc, "incompatible source memref type for EmitC conversion");
+ }
+ if (!isMemRefTypeLegalForEmitC(targetMemrefType)) {
+ return rewriter.notifyMatchFailure(
+ loc, "incompatible target memref type for EmitC conversion");
+ }
+
+ auto createPointerFromEmitcArray =
+ [&](mlir::Location loc, mlir::OpBuilder &rewriter,
+ mlir::TypedValue<emitc::ArrayType> arrayValue,
+ mlir::MemRefType memrefType,
+ emitc::ConstantOp zeroIndex) -> emitc::ApplyOp {
+ int64_t rank = arrayValue.getType().getRank();
+ llvm::SmallVector<mlir::Value> indices;
+ for (int i = 0; i < rank; ++i) {
+ indices.push_back(zeroIndex);
+ }
+
+ emitc::SubscriptOp subPtr = rewriter.create<emitc::SubscriptOp>(
+ loc, arrayValue, mlir::ValueRange(indices));
+ emitc::ApplyOp ptr = rewriter.create<emitc::ApplyOp>(
+ loc, emitc::PointerType::get(memrefType.getElementType()),
+ rewriter.getStringAttr("&"), subPtr);
+
+ return ptr;
+ };
+
+ emitc::ConstantOp zeroIndex = rewriter.create<emitc::ConstantOp>(
+ loc, rewriter.getIndexType(), rewriter.getIndexAttr(0));
+
+ auto srcArrayValue =
+ dyn_cast<TypedValue<emitc::ArrayType>>(operands.getSource());
----------------
aniragil wrote:
Use cast rather than dyn_cast when you expect cast to succeed (it will assert on failure).
https://github.com/llvm/llvm-project/pull/151206
More information about the Mlir-commits
mailing list