[Mlir-commits] [mlir] [mlir][LLVM] Verify address space in memref.alloca lowering (PR #202434)

Leo Garcia llvmlistbot at llvm.org
Mon Jun 8 13:53:12 PDT 2026


https://github.com/leogarci42 created https://github.com/llvm/llvm-project/pull/202434

Ensure that `memref.alloca` rejects lowering if its requested address space does not match the target's legal stack allocation address space defined in the DataLayout.

>From a7c4c63bc1901ee0601dfc0018869477b74937c9 Mon Sep 17 00:00:00 2001
From: leogarci42 <lg9752247 at gmail.com>
Date: Mon, 8 Jun 2026 22:31:00 +0200
Subject: [PATCH] [mlir][LLVM] Verify address space in memref.alloca lowering

Ensure that `memref.alloca` rejects lowering if its requested address space does not match the target's legal stack allocation address space defined in the DataLayout.
---
 mlir/lib/Conversion/MemRefToLLVM/MemRefToLLVM.cpp |  5 +++++
 mlir/test/Conversion/MemRefToLLVM/invalid.mlir    | 10 ++++++++++
 2 files changed, 15 insertions(+)

diff --git a/mlir/lib/Conversion/MemRefToLLVM/MemRefToLLVM.cpp b/mlir/lib/Conversion/MemRefToLLVM/MemRefToLLVM.cpp
index 61069fc4d660d..131a6df7aada3 100644
--- a/mlir/lib/Conversion/MemRefToLLVM/MemRefToLLVM.cpp
+++ b/mlir/lib/Conversion/MemRefToLLVM/MemRefToLLVM.cpp
@@ -360,6 +360,11 @@ struct AllocaOpLowering : public ConvertOpToLLVMPattern<memref::AllocaOp> {
         getTypeConverter()->getMemRefAddressSpace(op.getType());
     assert(succeeded(maybeAddressSpace) && "unsupported address space");
     unsigned addrSpace = *maybeAddressSpace;
+    if (addrSpace != getTypeConverter()->getDataLayout().getAllocaAddrSpace()) {
+        return rewriter.notifyMatchFailure(
+            op, "memref.alloca address space does not match the target's "
+                "legal stack allocation address space.");
+    }
     auto elementPtrType =
         LLVM::LLVMPointerType::get(rewriter.getContext(), addrSpace);
 
diff --git a/mlir/test/Conversion/MemRefToLLVM/invalid.mlir b/mlir/test/Conversion/MemRefToLLVM/invalid.mlir
index 5462d3278d9e6..e707dd821628b 100644
--- a/mlir/test/Conversion/MemRefToLLVM/invalid.mlir
+++ b/mlir/test/Conversion/MemRefToLLVM/invalid.mlir
@@ -52,3 +52,13 @@ func.func @test_atomic_exch(%arg0: memref<?xi32>, %idx: index, %value: i32) {
   }
   func.return
 }
+
+// -----
+
+func.func @invalid_alloca_address_space() {
+  // expected-error at +1 {{memref.alloca address space does not match the target's legal stack allocation address space.}}
+  %0 = memref.alloca() : memref<4xf32, 3>
+  func.return
+}
+
+}



More information about the Mlir-commits mailing list