Skip to content

Commit 4b267d5

Browse files
[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
1 parent dd33690 commit 4b267d5

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

mlir/lib/Conversion/MemRefToLLVM/MemRefToLLVM.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -741,7 +741,12 @@ struct GenericAtomicRMWOpLowering
741741
Operation *clone = rewriter.clone(nestedOp, mapping);
742742
mapping.map(nestedOp.getResults(), clone->getResults());
743743
}
744-
Value result = mapping.lookup(entryBlock.getTerminator()->getOperand(0));
744+
745+
Value result =
746+
mapping.lookupOrNull(entryBlock.getTerminator()->getOperand(0));
747+
if (!result) {
748+
return atomicOp.emitError("result not defined in region");
749+
}
745750

746751
// Prepare the epilog of the loop block.
747752
// Append the cmpxchg op to the end of the loop block.

mlir/test/Conversion/MemRefToLLVM/invalid.mlir

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,15 @@ func.func @issue_70160() {
4040
memref.store %0, %alloc1[] : memref<i32>
4141
func.return
4242
}
43+
44+
45+
// -----
46+
47+
func.func @test_atomic_exch(%arg0: memref<?xi32>, %idx: index, %value: i32) {
48+
// expected-error @+1 {{result not defined in region}}
49+
%1 = memref.generic_atomic_rmw %arg0[%idx] : memref<?xi32> {
50+
^bb0(%arg3: i32):
51+
memref.atomic_yield %value : i32
52+
}
53+
func.return
54+
}

0 commit comments

Comments
 (0)