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

llvmlistbot at llvm.org llvmlistbot at llvm.org
Mon Jun 8 13:54:05 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-mlir

Author: Leo Garcia (leogarci42)

<details>
<summary>Changes</summary>

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.

---
Full diff: https://github.com/llvm/llvm-project/pull/202434.diff


2 Files Affected:

- (modified) mlir/lib/Conversion/MemRefToLLVM/MemRefToLLVM.cpp (+5) 
- (modified) mlir/test/Conversion/MemRefToLLVM/invalid.mlir (+10) 


``````````diff
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
+}
+
+}

``````````

</details>


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


More information about the Mlir-commits mailing list