| Issue |
240192
|
| Summary |
[mlir][Bufferization][SCF] One-Shot Bufferize skips a required copy for a tensor yielded from `scf.for`
|
| Labels |
mlir
|
| Assignees |
|
| Reporter |
pingshiyu
|
Reproduced with the optimized LLVM Git `main` build at commit fb288ec4774f8664b165f6e302db5be8b30a41da from 04/Nov/2026.
The following valid input is canonicalized:
```mlir
module {
func.func @f(%arg0: tensor<?xi32>, %arg1: index) -> (tensor<1xi32>, tensor<1xi32>) {
%c1_i32 = arith.constant 1 : i32
%c1 = arith.constant 1 : index
%cast = tensor.cast %arg0 : tensor<?xi32> to tensor<1xi32>
%cast_0 = tensor.cast %arg0 : tensor<?xi32> to tensor<1xi32>
%0 = scf.for %arg2 = %arg1 to %c1 step %c1 iter_args(%arg3 = %cast_0) -> (tensor<1xi32>) {
scf.yield %cast : tensor<1xi32>
}
%inserted = tensor.insert %c1_i32 into %0[%arg1] : tensor<1xi32>
return %cast, %inserted : tensor<1xi32>, tensor<1xi32>
}
func.func @main() -> i32 {
%c0 = arith.constant 0 : index
%cst = arith.constant dense<0> : tensor<1xi32>
%cast = tensor.cast %cst : tensor<1xi32> to tensor<?xi32>
%0:2 = call @f(%cast, %c0) : (tensor<?xi32>, index) -> (tensor<1xi32>, tensor<1xi32>)
%extracted = tensor.extract %0#0[%c0] : tensor<1xi32>
%extracted_0 = tensor.extract %0#1[%c0] : tensor<1xi32>
%1 = arith.subi %extracted_0, %extracted : i32
return %1 : i32
}
}
```
The usual lowering and execution returns `0`:
```console
$ mlir-opt repro.mlir '--one-shot-bufferize=bufferize-function-boundaries=true allow-return-allocs-from-loops=true' --convert-scf-to-cf --convert-to-llvm -o affected.mlir
$ mlir-runner affected.mlir -e main -entry-point-result=i32 --shared-libs=libmlir_c_runner_utils.so,libmlir_runner_utils.so
0
```
However with copy-before-write, it returns `1`:
```console
$ mlir-opt repro.mlir '--one-shot-bufferize=bufferize-function-boundaries=true allow-return-allocs-from-loops=true copy-before-write=true' --convert-scf-to-cf --convert-to-llvm -o control.mlir
$ mlir-runner control.mlir -e main -entry-point-result=i32 --shared-libs=libmlir_c_runner_utils.so,libmlir_runner_utils.so
1
```
The trip count is one. The two casts are views of the initialized input `[0]`. The loop yields `%cast`, and `tensor.insert` creates a new value `[1]`. The function returns both the original `[0]` and the updated `[1]`. `main` observes both and computes `1 - 0`, so the correct output is `1`.
After `--one-shot-bufferize`, the first cast is copied into a fresh allocation, the loop yields that allocation, `tensor.insert` becomes a store through the loop result, and that same allocation is returned for both tensors. Both extracts therefore read `1`, causing `main` to incorrectly returns `0`.
## Related cases
[LLVM PR #87594](https://github.com/llvm/llvm-project/pull/87594) proposes preprocessing loops whose yielded tensor is not equivalent to the iter argument by inserting `bufferization.materialize_in_destination`. This case is accepted through `allow-return-allocs-from-loops=true` and silently miscompiles because a fresh value is missing from alias state; it does not hit the diagnostic that the proposal works around. It is related but distinct.
[LLVM issue #114657](https://github.com/llvm/llvm-project/issues/114657) is an open inconsistent-result report involving `tensor.from_elements`, `tensor.cast`, and One-Shot Bufferize. Its `scf.for` carries only scalar `i1`, not a tensor, and its behavioral difference is introduced when `--test-linalg-transform-patterns=test-patterns` produces a vector-transfer copy instead of `memref.copy`. This case instead deterministically omits a conflict copy at a tensor-valued `scf.yield` during One-Shot Bufferize itself. The historical command also uses the retired `--func-bufferize` option and cannot be replayed verbatim on current `main`. It is related by surface operations and symptom but has a distinct mechanism and is not a duplicate.
[LLVM issue #56915](https://github.com/llvm/llvm-project/issues/56915) also reaches an out-of-place allocation/copy through an SCF yield, but it reports an incompatible `memref.cast` and bufferization failure. This case silently miscompiles because a newly copied tensor is not treated as an external alias when resolving a later `scf.for` yield. It is related but not the same demonstrated defect.
[LLVM issue #236567](https://github.com/llvm/llvm-project/issues/236567), fixed by [PR #236644](https://github.com/llvm/llvm-project/pull/236644), concerns missing `scf.index_switch` alias registration. This case uses `scf.for`, whose alias interface is present, and reproduces in a build containing that fix. It is related but distinct.
## LLM-Suggested Cause and Fix
The likely failure is between generic conflict-copy materialization and SCF conflict resolution. [`BufferizableOpInterface::resolveTensorOpOperandConflicts`](https://github.com/llvm/llvm-project/blob/fb288ec4774f8664b165f6e302db5be8b30a41da/mlir/lib/Dialect/Bufferization/IR/BufferizableOpInterface.cpp#L247-L315) creates a fresh copied tensor for the out-of-place cast and rewrites its uses after one-shot analysis has built its alias sets. [`ForOpInterface::resolveConflicts`](https://github.com/llvm/llvm-project/blob/fb288ec4774f8664b165f6e302db5be8b30a41da/mlir/lib/Dialect/SCF/Transforms/BufferizableOpInterfaceImpl.cpp#L645-L696) then calls [`doesNotAliasExternalValue`](https://github.com/llvm/llvm-project/blob/fb288ec4774f8664b165f6e302db5be8b30a41da/mlir/lib/Dialect/SCF/Transforms/BufferizableOpInterfaceImpl.cpp#L43-L61) for the rewritten yield. That helper starts with `true` and changes the result only from callbacks made by [`OneShotAnalysisState::applyOnAliases`](https://github.com/llvm/llvm-project/blob/fb288ec4774f8664b165f6e302db5be8b30a41da/mlir/lib/Dialect/Bufferization/Transforms/OneShotAnalysis.cpp#L195-L201). A fresh value absent from the precomputed alias classes receives no callback, so the helper appears to accept it as non-external and omits the required loop-yield copy.
A proposed fix is for `doesNotAliasExternalValue` to classify the queried value itself before traversing known aliases, or for conflict-copy insertion to register the fresh value and its alias relation before SCF conflict resolution. This has not been tested as a patch. A regression should force one of two aliased tensor views out of place, yield the fresh copied value from an `scf.for` initialized by the other view, write in place through the loop result, and observe both the pre-write and post-write tensor values.
## 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