[Mlir-commits] [mlir] [mlir][EmitC] Expand the MemRefToEmitC pass - Lowering `extract_strided_metadata` (PR #152208)
Paul Kirth
llvmlistbot at llvm.org
Thu Aug 7 09:32:40 PDT 2025
================
@@ -288,6 +290,70 @@ struct ConvertStore final : public OpConversionPattern<memref::StoreOp> {
return success();
}
};
+
+struct ConvertExtractStridedMetadata final
+ : public OpConversionPattern<memref::ExtractStridedMetadataOp> {
+ using OpConversionPattern::OpConversionPattern;
+
+ LogicalResult
+ matchAndRewrite(memref::ExtractStridedMetadataOp extractStridedMetadataOp,
+ OpAdaptor operands,
+ ConversionPatternRewriter &rewriter) const override {
+ Location loc = extractStridedMetadataOp.getLoc();
+ Value source = extractStridedMetadataOp.getSource();
+
+ MemRefType memrefType = cast<MemRefType>(source.getType());
+ if (!isMemRefTypeLegalForEmitC(memrefType))
+ return rewriter.notifyMatchFailure(
+ loc, "incompatible memref type for EmitC conversion");
+
+ emitc::ConstantOp zeroIndex = rewriter.create<emitc::ConstantOp>(
+ loc, rewriter.getIndexType(), rewriter.getIndexAttr(0));
+ TypedValue<emitc::ArrayType> srcArrayValue =
+ cast<TypedValue<emitc::ArrayType>>(operands.getSource());
+ auto createPointerFromEmitcArray = [loc, &rewriter, &zeroIndex,
+ srcArrayValue]() -> emitc::ApplyOp {
+ int64_t rank = srcArrayValue.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, srcArrayValue, mlir::ValueRange(indices));
+ emitc::ApplyOp ptr = rewriter.create<emitc::ApplyOp>(
+ loc,
+ emitc::PointerType::get(srcArrayValue.getType().getElementType()),
+ rewriter.getStringAttr("&"), subPtr);
+
+ return ptr;
+ };
+
+ emitc::ApplyOp srcPtr = createPointerFromEmitcArray();
+ auto [strides, offset] = memrefType.getStridesAndOffset();
+ Value offsetValue = rewriter.create<emitc::ConstantOp>(
+ loc, rewriter.getIndexType(), rewriter.getIndexAttr(offset));
+
+ SmallVector<Value> results;
----------------
ilovepi wrote:
```suggestion
SmallVector<Value> results;
unsigned rank = memrefType.getRank();
results.reserve(2 + 2*rank);
```
You push 2 vals immediately and another 2 items per iteration of the loop, so you know up front how many items you need, and can avoid potentially allocating in the loop below.
https://github.com/llvm/llvm-project/pull/152208
More information about the Mlir-commits
mailing list