| Issue |
241708
|
| Summary |
[mlir][bufferization] static-memory-planner-analysis sizes sub-byte (i1) buffers by bit width -> buffers overlap and overrun the arena (heap overflow)
|
| Labels |
mlir
|
| Assignees |
|
| Reporter |
wildoranges
|
`computeSizeInBytes` in `-static-memory-planner-analysis` computes `ceil(numElements * elementBitWidth / 8)`:
https://github.com/llvm/llvm-project/blob/018b722b2d48cf42dccded46e1226f1d9c29f339/mlir/lib/Dialect/Bufferization/Transforms/StaticMemoryPlannerAnalysis.cpp#L91-L96
That does not match how memrefs are laid out after lowering: sub-byte elements are not bit-packed. A `memref<16xi1>` lowered to LLVM occupies 16 bytes (one byte per `i1`), but the planner reserves 2 bytes. Two live `memref<16xi1>` buffers are placed 2 bytes apart in a 4-byte arena, so they overlap each other **and** run past the end of the arena.
(Separately, `getElementTypeBitWidth()` asserted for `index`/`vector`/`complex` element types. The open PR #237123 makes the planner skip those types, but it does not change how sub-byte elements such as `i1` are sized, which is what this issue is about.)
`smp_i1.mlir`:
```mlir
// Two live i1 buffers of 16 elements each. Under the LLVM lowering each i1
// element occupies one byte (memref<16xi1> = 16 bytes), but the static memory
// planner sizes them as ceil(16*1/8) = 2 bytes and packs them 2 bytes apart.
func.func @main() {
%t = arith.constant true
%f = arith.constant false
%c0 = arith.constant 0 : index
%c1 = arith.constant 1 : index
%c16 = arith.constant 16 : index
%a = memref.alloc() : memref<16xi1>
%b = memref.alloc() : memref<16xi1>
linalg.fill ins(%t : i1) outs(%a : memref<16xi1>)
linalg.fill ins(%f : i1) outs(%b : memref<16xi1>)
%cnt = scf.for %i = %c0 to %c16 step %c1 iter_args(%acc = %c0) -> (index) {
%v = memref.load %a[%i] : memref<16xi1>
%vi = arith.index_castui %v : i1 to index
%n = arith.addi %acc, %vi : index
scf.yield %n : index
}
// Expected: 16 (all elements of %a are true).
vector.print %cnt : index
memref.dealloc %a : memref<16xi1>
memref.dealloc %b : memref<16xi1>
return
}
```
(`$LLVM_BUILD` is the LLVM/MLIR build directory.)
```console
$ LOWER="-convert-bufferization-to-memref -test-lower-to-llvm"
$ RUN="mlir-runner -e main -entry-point-result=void -O0 -shared-libs=$LLVM_BUILD/lib/libmlir_runner_utils.so,$LLVM_BUILD/lib/libmlir_c_runner_utils.so"
$ mlir-opt smp_i1.mlir $LOWER | $RUN
16
$ mlir-opt smp_i1.mlir -pass-pipeline="builtin.module(func.func(static-memory-planner-analysis))" | mlir-opt $LOWER | $RUN
2
$ mlir-opt smp_i1.mlir -pass-pipeline="builtin.module(func.func(static-memory-planner-analysis))"
...
%alloc = memref.alloc() alignment = 1 : memref<4xi8>
%c0_0 = arith.constant 0 : index
%view = memref.view %alloc[%c0_0][] : memref<4xi8> to memref<16xi1>
%c2 = arith.constant 2 : index
%view_1 = memref.view %alloc[%c2][] : memref<4xi8> to memref<16xi1>
linalg.fill ins(%true : i1) outs(%view : memref<16xi1>)
linalg.fill ins(%false : i1) outs(%view_1 : memref<16xi1>)
...
```
The expected output is 16, since all elements of `%a` are true. After planning, `linalg.fill` of `%b` (false) overwrites most of `%a`.
When the planned program is compiled to a native binary (`mlir-translate` → `llc -O0` → link with the runner utils) and run under valgrind, valgrind reports `Invalid write of size 1 ... 0 bytes after a block of size 5 alloc'd`: a heap overflow past the arena.
The input verifies and is UB-free: all accesses are in bounds of the original allocations.
Possible fix: compute sizes and alignments from the data layout (`mlir::DataLayout::getTypeSize` / `getTypeABIAlignment`) instead of the bit width, and skip element types that the data layout cannot size.
**Version**: llvm-project main @ 018b722b2d48cf42dccded46e1226f1d9c29f339 (2026-10-17), Release + assertions, x86_64 Linux. Also reproduced on main @ a60677dca3a8536a0cf5bec1b846610014a983fd (2026-11-12).
*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