Issue 237580
Summary [MLIR][Affine] `affine-data-copy-generate` inserts copy-out after `memref.dealloc`
Labels mlir
Assignees
Reporter pingshiyu
    Reproduced with the official LLVM/MLIR 23.1.4 optimized Linux x86-64 build.

This valid program accesses its allocation right before deallocating it:

```mlir
module {
  func.func @main() {
    %value = arith.constant 42 : i32
    %alloc = memref.alloc() : memref<1xi32>
    affine.store %value, %alloc[0] : memref<1xi32>
    memref.dealloc %alloc : memref<1xi32>
    return
  }
}
```

Run:

```console
mlir-opt reduced.mlir \
 '--pass-pipeline=builtin.module(func.func(affine-data-copy-generate{fast-mem-space=0}))' \
  -o after.mlir

mlir-opt after.mlir \
  --memref-expand --expand-strided-metadata --lower-affine \
  --convert-math-to-funcs --convert-math-to-llvm --arith-expand \
  --convert-scf-to-cf --convert-index-to-llvm=index-bitwidth=64 \
  --convert-arith-to-llvm --finalize-memref-to-llvm --convert-ub-to-llvm \
  --convert-func-to-llvm --convert-cf-to-llvm \
  --reconcile-unrealized-casts -o after-llvm.mlir

mlir-runner after-llvm.mlir -e main -entry-point-result=void
```

The relevant output is:

```mlir
%alloc = memref.alloc() : memref<1xi32>
%alloc_2 = memref.alloc() : memref<1xi32>
affine.store %c42_i32, %alloc_2[0] : memref<1xi32>
memref.dealloc %alloc : memref<1xi32>
%0 = affine.load %alloc_2[0] : memref<1xi32>
affine.store %0, %alloc[0] : memref<1xi32>
memref.dealloc %alloc_2 : memref<1xi32>
```

The generated store writes to `%alloc` after it was freed. After ordinary lowering and execution with `mlir-runner`, the baseline exits normally, while this transformed program aborts on my system with:

```text
malloc(): unaligned tcache chunk detected
```

The expected result is that this pass never inserts an access after the destination memref's deallocation (or declines the promotion when no valid copy-out point exists).

## LLM-Suggested Cause

`AffineDataCopyGeneration::runOnBlock` makes the final copy range extend from the first affine access to just before the terminator, so an existing `memref.dealloc` can lie inside that range. For a write region, `generateCopy` places the pointwise copy-out at the range end without checking whether the original memref remains live there. The original store is redirected to the fast buffer, but the original deallocation stays before the newly inserted copy-out.

The same logic is present on `main` at `24ea9d257831ee5d6f827511b35ddb09036859b8`: [final range](https://github.com/llvm/llvm-project/blob/24ea9d257831ee5d6f827511b35ddb09036859b8/mlir/lib/Dialect/Affine/Transforms/AffineDataCopyGeneration.cpp#L190-L198), [epilogue placement](https://github.com/llvm/llvm-project/blob/24ea9d257831ee5d6f827511b35ddb09036859b8/mlir/lib/Dialect/Affine/Utils/LoopUtils.cpp#L2006-L2014), and [pointwise copy-out](https://github.com/llvm/llvm-project/blob/24ea9d257831ee5d6f827511b35ddb09036859b8/mlir/lib/Dialect/Affine/Utils/LoopUtils.cpp#L2187-L2201).

## LLM Declaration

LLM was used to prepare the bug report, and the bug itself was found and verified by a human.

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to