[Mlir-commits] [mlir] 4b267d5 - [MLIR][MemRef] Emit error on atomic generic result op defined outside the region (#172190)
llvmlistbot at llvm.org
llvmlistbot at llvm.org
Sun Dec 14 08:31:47 PST 2025
Author: Asher Mancinelli
Date: 2025-12-14T08:31:43-08:00
New Revision: 4b267d5caa9d9fe306915c530e639da05a9d0735
URL: https://github.com/llvm/llvm-project/commit/4b267d5caa9d9fe306915c530e639da05a9d0735
DIFF: https://github.com/llvm/llvm-project/commit/4b267d5caa9d9fe306915c530e639da05a9d0735.diff
LOG: [MLIR][MemRef] Emit error on atomic generic result op defined outside the region (#172190)
While figuring out how to perform an atomic exchange on a memref, I
tried the generic atomic rmw with the yielded value captured from the
enclosing scope (instead of a plain atomic_rmw with
`arith::AtomicRMWKind::assign`). Instead of segfaulting, this PR changes
the pass to produce an error when the result is not found in the
region's IR map.
It might be more useful to give a suggestion to the user, but giving an
error message instead of a crash is at least an imrovement, I think.
See: #172184
Added:
Modified:
mlir/lib/Conversion/MemRefToLLVM/MemRefToLLVM.cpp
mlir/test/Conversion/MemRefToLLVM/invalid.mlir
Removed:
################################################################################
diff --git a/mlir/lib/Conversion/MemRefToLLVM/MemRefToLLVM.cpp b/mlir/lib/Conversion/MemRefToLLVM/MemRefToLLVM.cpp
index a6f816aa07377..91a0c4b55fa84 100644
--- a/mlir/lib/Conversion/MemRefToLLVM/MemRefToLLVM.cpp
+++ b/mlir/lib/Conversion/MemRefToLLVM/MemRefToLLVM.cpp
@@ -741,7 +741,12 @@ struct GenericAtomicRMWOpLowering
Operation *clone = rewriter.clone(nestedOp, mapping);
mapping.map(nestedOp.getResults(), clone->getResults());
}
- Value result = mapping.lookup(entryBlock.getTerminator()->getOperand(0));
+
+ Value result =
+ mapping.lookupOrNull(entryBlock.getTerminator()->getOperand(0));
+ if (!result) {
+ return atomicOp.emitError("result not defined in region");
+ }
// Prepare the epilog of the loop block.
// Append the cmpxchg op to the end of the loop block.
diff --git a/mlir/test/Conversion/MemRefToLLVM/invalid.mlir b/mlir/test/Conversion/MemRefToLLVM/invalid.mlir
index 0d04bba96bcdb..5462d3278d9e6 100644
--- a/mlir/test/Conversion/MemRefToLLVM/invalid.mlir
+++ b/mlir/test/Conversion/MemRefToLLVM/invalid.mlir
@@ -40,3 +40,15 @@ func.func @issue_70160() {
memref.store %0, %alloc1[] : memref<i32>
func.return
}
+
+
+// -----
+
+func.func @test_atomic_exch(%arg0: memref<?xi32>, %idx: index, %value: i32) {
+ // expected-error @+1 {{result not defined in region}}
+ %1 = memref.generic_atomic_rmw %arg0[%idx] : memref<?xi32> {
+ ^bb0(%arg3: i32):
+ memref.atomic_yield %value : i32
+ }
+ func.return
+}
More information about the Mlir-commits
mailing list