| Issue |
233733
|
| Summary |
[mlir] affine-loop-invariant-code-motion and memref::eraseDeadAllocAndStores treat ops with unknown effects (func.call) as effect-free (miscompile)
|
| Labels |
mlir
|
| Assignees |
|
| Reporter |
wildoranges
|
`mlir::hasEffect<EffectTy>(op[, value])` is documented to return **false** when `op` has unknown effects. `mightHaveEffect` is the conservative query:
https://github.com/llvm/llvm-project/blob/018b722b2d48cf42dccded46e1226f1d9c29f339/mlir/include/mlir/Interfaces/SideEffectInterfaces.h#L497-L528
Two transformations use `hasEffect` where they need `mightHaveEffect`. An op without `MemoryEffectOpInterface`, such as `func.call`, is therefore treated as neither reading nor writing memory.
- **`-affine-loop-invariant-code-motion`** decides whether a load conflicts with other users of the same memref via `hasEffect<Write>(user, memref)`:
https://github.com/llvm/llvm-project/blob/018b722b2d48cf42dccded46e1226f1d9c29f339/mlir/lib/Dialect/Affine/Transforms/AffineLoopInvariantCodeMotion.cpp#L91-L100
(This is not the aliasing issue mentioned in the FIXME there. The call takes the *same* memref value as an operand.)
- **`memref::eraseDeadAllocAndStores`** (`resultIsNotRead`) counts a use as "not a read" if the user has no results, no regions and `!hasEffect<Read>(useOp)`. It then erases the alloc, its stores **and the call**:
https://github.com/llvm/llvm-project/blob/018b722b2d48cf42dccded46e1226f1d9c29f339/mlir/lib/Dialect/MemRef/Utils/MemRefUtils.cpp#L151-L164
(`$LLVM_BUILD` is the LLVM/MLIR build directory.)
### 1. affine LICM hoists a load above a call that writes the same memref
`licm.mlir`:
```mlir
func.func @bump(%m: memref<1xi32>) {
%c0 = arith.constant 0 : index
%c1 = arith.constant 1 : i32
%v = memref.load %m[%c0] : memref<1xi32>
%w = arith.addi %v, %c1 : i32
memref.store %w, %m[%c0] : memref<1xi32>
return
}
func.func @f(%A: memref<1xi32>, %out: memref<4xi32>) {
affine.for %i = 0 to 4 {
func.call @bump(%A) : (memref<1xi32>) -> ()
%v = affine.load %A[0] : memref<1xi32>
affine.store %v, %out[%i] : memref<4xi32>
}
return
}
func.func @main() {
%c0 = arith.constant 0 : index
%z = arith.constant 0 : i32
%A = memref.alloca() : memref<1xi32>
memref.store %z, %A[%c0] : memref<1xi32>
%out = memref.alloca() : memref<4xi32>
call @f(%A, %out) : (memref<1xi32>, memref<4xi32>) -> ()
%u = memref.cast %out : memref<4xi32> to memref<*xi32>
call @printMemrefI32(%u) : (memref<*xi32>) -> ()
return
}
func.func private @printMemrefI32(memref<*xi32>)
```
```console
$ LOWER="--lower-affine --convert-scf-to-cf --convert-to-llvm --reconcile-unrealized-casts"
$ RUN="mlir-runner -e main -entry-point-result=void -shared-libs=$LLVM_BUILD/lib/libmlir_c_runner_utils.so,$LLVM_BUILD/lib/libmlir_runner_utils.so"
$ mlir-opt licm.mlir $LOWER | $RUN
Unranked Memref base@ = 0x7ffccc6301e0 rank = 1 offset = 0 sizes = [4] strides = [1] data =
"" 2, 3, 4]
$ mlir-opt licm.mlir --pass-pipeline='builtin.module(func.func(affine-loop-invariant-code-motion))' | mlir-opt $LOWER | $RUN
Unranked Memref base@ = 0x7ffd8bf8d970 rank = 1 offset = 0 sizes = [4] strides = [1] data =
"" 0, 0, 0]
$ mlir-opt licm.mlir --pass-pipeline='builtin.module(func.func(affine-loop-invariant-code-motion))'
...
func.func @f(%arg0: memref<1xi32>, %arg1: memref<4xi32>) {
%0 = affine.load %arg0[0] : memref<1xi32>
affine.for %arg2 = 0 to 4 {
func.call @bump(%arg0) : (memref<1xi32>) -> ()
affine.store %0, %arg1[%arg2] : memref<4xi32>
}
return
}
...
```
### 2. erase_dead_alloc_and_stores deletes a call that reads the buffer
`erase.mlir`:
```mlir
func.func @consume(%m: memref<4xi32>) {
%u = memref.cast %m : memref<4xi32> to memref<*xi32>
call @printMemrefI32(%u) : (memref<*xi32>) -> ()
return
}
func.func @main() {
%c0 = arith.constant 0 : index
%c1 = arith.constant 1 : index
%c4 = arith.constant 4 : index
%buf = memref.alloc() : memref<4xi32>
scf.for %i = %c0 to %c4 step %c1 {
%v = arith.index_cast %i : index to i32
memref.store %v, %buf[%i] : memref<4xi32>
}
call @consume(%buf) : (memref<4xi32>) -> ()
memref.dealloc %buf : memref<4xi32>
return
}
func.func private @printMemrefI32(memref<*xi32>)
module attributes {transform.with_named_sequence} {
transform.named_sequence @__transform_main(%root: !transform.any_op {transform.readonly}) {
%f = transform.structured.match ops{["func.func"]} in %root : (!transform.any_op) -> !transform.any_op
transform.memref.erase_dead_alloc_and_stores %f : (!transform.any_op) -> ()
transform.yield
}
}
```
```console
$ mlir-opt erase.mlir --test-transform-dialect-erase-schedule $LOWER | $RUN
Unranked Memref base@ = 0x6391f65a8350 rank = 1 offset = 0 sizes = [4] strides = [1] data =
"" 1, 2, 3]
$ mlir-opt erase.mlir --transform-interpreter --test-transform-dialect-erase-schedule | mlir-opt $LOWER | $RUN
(no output)
$ mlir-opt erase.mlir --transform-interpreter --test-transform-dialect-erase-schedule
...
func.func @main() {
%c0 = arith.constant 0 : index
%c1 = arith.constant 1 : index
%c4 = arith.constant 4 : index
scf.for %arg0 = %c0 to %c4 step %c1 {
%0 = arith.index_cast %arg0 : index to i32
}
return
}
...
```
The second run prints nothing: `call @consume` has been erased together with the alloc and its stores.
Both inputs verify and are UB-free: the buffers are initialized, all accesses are in bounds, and everything is single-threaded.
Downstream, IREE's `iree-codegen-erase-dead-alloc-and-stores` and `iree-codegen-optimize-vector-transfer` (the latter runs in the CPU and GPU codegen pipelines) use the same utility and produce the same erased IR. iree-org/iree#23490 worked around this only for one IREE op, by giving it a `MemoryEffectOpInterface`.
Possible fix: use `mightHaveEffect<...>` (or bail out on `hasUnknownEffects`) in both places.
**Version**: llvm-project main @ 018b722b2d48cf42dccded46e1226f1d9c29f339 (2026-10-17), Release + assertions, x86_64 Linux. IREE check: iree-base-compiler 3.13.0rc20261017. Also reproduced on main @ 4c407e843c975298b6892d50afd9e72cfb0cbe46 (2026-10-20).
*Disclosure: I wrote this report with the help of Claude Code. I reproduced and reviewed everything in this report myself.*
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs