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

Marius Brehler llvmlistbot at llvm.org
Tue Jul 22 06:05:07 PDT 2025


================
@@ -77,6 +77,43 @@ struct ConvertAlloca final : public OpConversionPattern<memref::AllocaOp> {
   }
 };
 
+struct ConvertAlloc final : public OpConversionPattern<memref::AllocOp> {
+  using OpConversionPattern::OpConversionPattern;
+  LogicalResult
+  matchAndRewrite(memref::AllocOp allocOp, OpAdaptor operands,
+                  ConversionPatternRewriter &rewriter) const override {
+    mlir::Location loc = allocOp.getLoc();
+    auto memrefType = allocOp.getType();
+    if (!memrefType.hasStaticShape())
+      // TODO: Handle Dynamic shapes in the future. If the size
+      // of the allocation is the result of some function, we could
+      // potentially evaluate the function and use the result in the call to
+      // allocate.
+      return rewriter.notifyMatchFailure(
+          allocOp.getLoc(), "cannot transform alloc op with dynamic shape");
+
+    // TODO: Is there a better API to determine the number of bits in a byte in
+    // MLIR?
+    int64_t totalSize = memrefType.getNumElements() *
+                        memrefType.getElementTypeBitWidth() / CHAR_BIT;
+    if (auto alignment = allocOp.getAlignment()) {
+      int64_t alignVal = alignment.value();
+      totalSize = (totalSize + alignVal - 1) / alignVal * alignVal;
----------------
marbre wrote:

`aligend_alloc()` is indeed supported since C11 and C++17, even though required different headers.

While we have some of the constrains for the dialect itself and the emitted C and C++ code documented in https://mlir.llvm.org/docs/Dialects/EmitC/, we don't have this for the conversions yet. As @aniragil pointed the patch should document that C11 or rather C++17 will be required (the header _could_ be inserted via `emitc.include`) and add this at least to the beginning of the source file if we don't find a better place.

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


More information about the Mlir-commits mailing list