Issue 240194
Summary [mlir][Bufferization] `--one-shot-bufferize` produces writeable tensor constant, producing segfaulting binary at runtime
Labels mlir
Assignees
Reporter pingshiyu
    Reproduced with the optimized LLVM Git `main` build at commit fb288ec4774f8664b165f6e302db5be8b30a41da from 04/Nov/2026.

This program is valid. The scatter creates an updated tensor value and should return `2`.
```mlir
module {
  func.func private @constant_tensor() -> tensor<1xi32> {
    %c = arith.constant dense<1> : tensor<1xi32>
    return %c : tensor<1xi32>
  }

  func.func @test() -> i32 {
    %c0 = arith.constant 0 : index
    %base = call @constant_tensor() : () -> tensor<1xi32>
    %indices = arith.constant dense<0> : vector<1xindex>
 %mask = arith.constant dense<true> : vector<1xi1>
    %value = arith.constant dense<2> : vector<1xi32>
    %updated = vector.scatter %base[%c0][%indices], %mask, %value : tensor<1xi32>, vector<1xindex>, vector<1xi1>, vector<1xi32> -> tensor<1xi32>
    %result = tensor.extract %updated[%c0] : tensor<1xi32>
    return %result : i32
 }
}
```

```bash
mlir-opt repro.mlir \
 '--one-shot-bufferize=bufferize-function-boundaries=true allow-return-allocs-from-loops=true function-boundary-type-conversion=identity-layout-map' \
 --test-vector-gather-lowering \
  '--convert-vector-to-scf=target-rank=0 full-unroll=true' \
  --convert-scf-to-cf --convert-vector-to-llvm \
 '--convert-index-to-llvm=index-bitwidth=64' --convert-arith-to-llvm \
 --finalize-memref-to-llvm --convert-func-to-llvm --convert-cf-to-llvm \
 --reconcile-unrealized-casts -o lowered.mlir
mlir-runner lowered.mlir -e test -entry-point-result=i32
```

The runner crashes with SIGSEGV. 

Inspecting the bufferized IR shows that the callee returns `memref.get_global` for a private constant `memref.global`, and the caller writes through that returned descriptor. A writable copy is not inserted.

On the other hand, inlining before bufferizing avoids the segfault at runtime:
```bash
mlir-opt repro.mlir --inline \
 '--one-shot-bufferize=bufferize-function-boundaries=true allow-return-allocs-from-loops=true function-boundary-type-conversion=identity-layout-map' \
 --test-vector-gather-lowering \
  '--convert-vector-to-scf=target-rank=0 full-unroll=true' \
  --convert-scf-to-cf --convert-vector-to-llvm \
 '--convert-index-to-llvm=index-bitwidth=64' --convert-arith-to-llvm \
 --finalize-memref-to-llvm --convert-func-to-llvm --convert-cf-to-llvm \
 --reconcile-unrealized-casts -o inline-lowered.mlir
mlir-runner inline-lowered.mlir -e test -entry-point-result=i32
```

Returning `2` as expected.

## Related cases

[Issue #233726](https://github.com/llvm/llvm-project/issues/233726), closed by [PR #234906](https://github.com/llvm/llvm-project/pull/234906), is related bufferization ownership context around transfer writes, but it does not cover a function result exposing constant global storage. [Issue #150441](https://github.com/llvm/llvm-project/issues/150441), closed by [PR #186446](https://github.com/llvm/llvm-project/pull/186446), was an analysis-time copy-before-write crash. [Issue #176739](https://github.com/llvm/llvm-project/issues/176739), closed by [PR #189249](https://github.com/llvm/llvm-project/pull/189249), concerned ranked function-result conversion. All are related but distinct, and their fixes are present in the tested build.

## LLM-Suggested Cause and Fix

The [`arith.constant` model](https://github.com/llvm/llvm-project/blob/fb288ec4774f8664b165f6e302db5be8b30a41da/mlir/lib/Dialect/Arith/Transforms/BufferizableOpInterfaceImpl.cpp#L42-L67) marks its tensor result non-writable, while the [`BufferizableOpInterface` default](https://github.com/llvm/llvm-project/blob/fb288ec4774f8664b165f6e302db5be8b30a41da/mlir/include/mlir/Dialect/Bufferization/IR/BufferizableOpInterface.td#L443-L466) treats operation results as writable. The [`func.call` model](https://github.com/llvm/llvm-project/blob/fb288ec4774f8664b165f6e302db5be8b30a41da/mlir/lib/Dialect/Bufferization/Transforms/FuncBufferizableOpInterfaceImpl.cpp#L162-L346) does not override `isWritable` or propagate that property from callee return values, so the same storage changes from non-writable in the callee to writable at the call result. A proposed fix is to propagate result writability across calls, conservatively treating a result as non-writable if any corresponding return value is non-writable or unknown. Existing conflict resolution should then insert a writable copy. A regression test should cover direct and indirect caller writes and multiple return sites. This is a proposed correction, not a tested patch.

## 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