| Issue |
243210
|
| Summary |
[mlir][affine] `--affine-super-vectorize` use-after-free in `computeIntersectionBuckets` / `vectorizeLoops` on sibling loops
|
| Labels |
mlir
|
| Assignees |
|
| Reporter |
quiet-maple-47
|
## Description
`--affine-super-vectorize` crashes with a use-after-free (`SIGSEGV`) when an outer `affine.for` loop encloses multiple sibling inner `affine.for` matches.
`computeIntersectionBuckets` attaches the outer loop to only the first sibling's bucket without merging the remaining sibling buckets. Vectorizing the first bucket erases the outer loop and all of its children, leaving dangling `Operation *` pointers in subsequent buckets.
## Reproducer
`input.mlir`:
```mlir
#map = affine_map<()[s0] -> ((s0 floordiv 2) * 2)>
func.func @f(%alloc: memref<?x?xi8>, %dim: index, %dim_1: index) {
%c0 = arith.constant 0 : index
%c1 = arith.constant 1 : index
%c0_i8 = arith.constant 0 : i8
affine.for %arg5 = 0 to %dim {
%13 = affine.for %arg6 = 0 to #map()[%dim_1] step 2 iter_args(%arg7 = %c0) -> (index) {
%24 = arith.addi %arg7, %c1 : index
affine.yield %24 : index
}
%14 = affine.for %arg6 = #map()[%dim_1] to %dim_1 iter_args(%arg7 = %13) -> (index) {
affine.store %c0_i8, %alloc[%arg5, %arg6] : memref<?x?xi8>
%24 = arith.addi %arg7, %c1 : index
affine.yield %24 : index
}
}
return
}
```
```bash
mlir-opt '--affine-super-vectorize=virtual-vector-size=8 vectorize-reductions' input.mlir
```
[Reproduce on Compiler Explorer (LLVM trunk)](https://godbolt.org/z/7xMzdsn7P)
## Actual Behavior
`mlir-opt` crashes with `SIGSEGV` (exit code 139) on LLVM trunk, Homebrew LLVM 23.1.5, and assertions-enabled LLVM 23 (`b3df0665`):
```text
#3 0x0000000009841069 std::_Function_handler<bool (mlir::Operation&), isVectorizableLoopBodyWithOpCond(mlir::affine::AffineForOp, std::function<bool (mlir::affine::AffineForOp, mlir::Operation&)> const&, mlir::affine::NestedPattern&)::'lambda'(mlir::Operation&)>::_M_invoke(std::_Any_data const&, mlir::Operation&) LoopAnalysis.cpp:0:0
#4 0x0000000009842972 mlir::affine::NestedPattern::matchOne(mlir::Operation*, llvm::SmallVectorImpl<mlir::affine::NestedMatch>*)
#5 0x000000000983c3fb isVectorizableLoopBodyWithOpCond(mlir::affine::AffineForOp, std::function<bool (mlir::affine::AffineForOp, mlir::Operation&)> const&, mlir::affine::NestedPattern&) LoopAnalysis.cpp:0:0
#6 0x000000000983e65e mlir::affine::isVectorizableLoopBody(mlir::affine::AffineForOp, mlir::affine::NestedPattern&)
```
## Expected Behavior
`--affine-super-vectorize` should not touch erased sibling loops after vectorizing an enclosing outer loop.
## Root Cause
In [`computeIntersectionBuckets`](https://github.com/llvm/llvm-project/blob/e0c9350a3f88ca5a37aaac4c7cc9caebf5c309e0/mlir/lib/Dialect/Affine/Transforms/SuperVectorize.cpp#L1722-L1758):
1. The two sibling inner `affine.for` loops (`%13` and `%14`) do not intersect each other, so they are placed in separate buckets (bucket 0 and bucket 1).
2. When the enclosing outer `affine.for %arg5` match is processed, lines 1742–1747 add it to bucket 0 (`bucketRoots[0] = matchRoot; break;`) and exit the loop immediately, without merging bucket 1 (whose root is also a descendant of `matchRoot`).
3. [`vectorizeLoops`](https://github.com/llvm/llvm-project/blob/e0c9350a3f88ca5a37aaac4c7cc9caebf5c309e0/mlir/lib/Dialect/Affine/Transforms/SuperVectorize.cpp#L1798-L1817) vectorizes `%arg5` in bucket 0 and erases the entire `%arg5` loop nest (including `%14`), leaving a dangling `Operation *` in bucket 1 that is subsequently dereferenced when analyzing bucket 1.
Merging all buckets whose roots are enclosed by `matchRoot` in `computeIntersectionBuckets` prevents distinct buckets from sharing overlapping loop nests.
## Version
- LLVM trunk on Compiler Explorer (`mliropttrunk`, LLVM 24.0.0git)
- Homebrew LLVM 23.1.5 (macOS arm64)
- `llvm-project` `b3df0665` (Linux x86_64, assertions enabled)
- Relevant code verified on `main` at `e0c9350a3f88` (2026-11-16)
## Claim
This investigation and write-up were AI-assisted. I've reviewed and verified everything above, and I'm happy to answer questions.
**Assisted-by:** Gemini / Antigravity
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs