[MLIR][Affine] Miscompilation: `affine-data-copy-generate` copies out a fast buffer it never filled, when the loop also has a non-affine access
- Dominant language
- LLVM
- Stars
- 40.5k
- Forks
- 18.7k
- PR merge metrics
- PR metrics pending
Description
## Reproducer
### small.mlir
```mlir
module {
memref.global "private" @m : memref<4xi32> = dense<[3, -7, 11, -13]>
func.func @main() {
%s = arith.constant 99 : i32
%c0 = arith.constant 0 : index
%m = memref.get_global @m : memref<4xi32>
affine.for %i = 0 to 4 {
affine.store %s, %m[0] : memref<4xi32>
memref.store %s, %m[%c0] : memref<4xi32>
}
%q = affine.load %m[0] : memref<4xi32>
vector.print %q : i32
return
}
}
```
Both stores write 99 to element 0, so the program prints 99.
### To reproduce
`$LLVM_LIB` is the `lib` directory of an `mlir-opt`/`mlir-runner` build.
Correct — the program prints 99:
```bash
mlir-opt small.mlir \
--lower-affine \
--one-shot-bufferize=bufferize-function-boundaries \
--convert-scf-to-cf --convert-vector-to-llvm --expand-strided-metadata \
--finalize-memref-to-llvm --convert-func-to-llvm --convert-index-to-llvm \
--convert-arith-to-llvm --convert-cf-to-llvm --reconcile-unrealized-casts \
| mlir-runner -e main --entry-point-result=void \
--shared-libs=$LLVM_LIB/libmlir_c_runner_utils.so,$LLVM_LIB/libmlir_runner_utils.so
```
```
99
```
Wrong — the same thing with `--affine-data-copy-generate` in front, run three times:
```bash
for i in 1 2 3; do
mlir-opt small.mlir \
--affine-data-copy-generate=fast-mem-space=0 \
--lower-affine \
--one-shot-bufferize=bufferize-function-boundaries \
--convert-scf-to-cf --convert-vector-to-llvm --expand-strided-metadata \
--finalize-memref-to-llvm --convert-func-to-llvm --convert-index-to-llvm \
--convert-arith-to-llvm --convert-cf-to-llvm --reconcile-unrealized-casts \
| mlir-runner -e main --entry-point-result=void \
--shared-libs=$LLVM_LIB/libmlir_c_runner_utils.so,$LLVM_LIB/libmlir_runner_utils.so
done
```
```
-2011957537
1263175833
1511181532
```
A different number each time, which is what reading uninitialised memory looks like.
Contributor guide
Assessment
This issue has not been assessed yet.