| Issue |
241119
|
| Summary |
[mlir][bug][fuzzer-found] affine-loop-fusion erases the source loop while the fused nest still uses a value defined inside it
|
| Labels |
mlir
|
| Assignees |
|
| Reporter |
Kiritobl
|
#### Version
git commit hash: 0fdfbb6f057f1a01a9bc6fc025c502471f106f23
LLVM/MLIR 24.0.0git, optimized build with assertions.
#### Bug Description
`affine-loop-fusion{mode=producer}` aborts with `LLVM ERROR: operation destroyed but still has uses` when the producer store is indexed by a value that is defined **inside** the source loop nest. Fusion clones the source nest into the destination but the destination still refers to the loop-local `index.sizeof`; `performFusionsIntoDest` then erases the source loop and destroys that value with live users.
The minimized input below parses and verifies successfully without the pass. It aborted in 5/5 runs (SIGABRT, shell exit 134).
A second input uses `memref.dim` (defined inside the source loop) instead of `index.sizeof`. It crashes in the same `srcAffineForOp.erase()` call, but the failing step is the diagnostic itself: while printing "operation destroyed but still has uses", the verifier walks the dying `memref.dim` through `isValidSymbol` and dereferences a stale value, producing SIGSEGV (5/5 runs, shell exit 139). Both inputs are given below.
Two ablations confirm that the loop-local definition is what matters: replacing the store index with the loop IV (`affine.store %d, %alloc_16[%i, %j]`) and hoisting `index.sizeof` above the loop both run cleanly on this build.
#### Crash Chain & Root Cause
Crash chain:
```text
affine-loop-fusion{mode=producer}
→ (anonymous namespace)::GreedyFusion::fuseProducerConsumerNodes
→ (anonymous namespace)::GreedyFusion::performFusionsIntoDest
→ mlir::affine::fuseLoops(srcForOp, dstForOp, srcSlice) // LoopFusion.cpp:1090
→ srcAffineForOp.erase() // LoopFusion.cpp:1155
→ mlir::Operation::~Operation()
→ "operation destroyed but still has uses"
```
In `mlir/lib/Dialect/Affine/Transforms/LoopFusion.cpp`, `performFusionsIntoDest` calls `fuseLoops` and then removes the source loop when `canRemoveSrcNodeAfterFusion` allows it (`:1153-1155`). `fuseLoops` (`mlir/lib/Dialect/Affine/Utils/LoopFusionUtils.cpp:425-455`) clones `srcForOp` into the destination and rewrites the cloned slice loops' bounds from `srcSlice.lbOperands` / `srcSlice.ubOperands`.
Here the producer store is `affine.store %d, %alloc_16[%i, %s]` with `%s = index.sizeof` defined inside the source `affine.for`, and the destination reads the same memref with a non-identity index (`affine.load %alloc_16[%j mod 20, %j]`). The slice-derived indices materialized in the destination therefore still refer to `%s`. Nothing moves or clones `%s` before the source loop is erased, so destroying the source loop destroys a value that the destination still uses. The diagnostic then prints the dead operation (the `<<UNKNOWN SSA VALUE>>` notes come from the freed op), which is why the second input turns the same bug into a SIGSEGV while printing.
Expected: fusion should either remap such operands to the cloned definition or decline to remove the source loop.
#### Steps to Reproduce
```bash
mlir-opt '--pass-pipeline=builtin.module(func.func(affine-loop-fusion{mode=producer}))' reproducer.mlir -o /dev/null
```
The process terminates with SIGABRT (exit status 134) in 5/5 runs. Running `mlir-opt reproducer.mlir -o /dev/null` without the pass succeeds.
#### Reproducer (`reproducer-*.mlir`)
`reproducer.mlir`:
```mlir
func.func @f() {
%alloc_13 = memref.alloc() : memref<20x10xf32>
%alloc_15 = memref.alloc() : memref<20x10xf32>
%alloc_16 = memref.alloc() : memref<20x10xf32>
affine.for %i = 0 to 20 {
affine.for %j = 0 to 10 {
%a = affine.load %alloc_13[%i, %j] : memref<20x10xf32>
%b = affine.load %alloc_15[%i, %j] : memref<20x10xf32>
%d = arith.divf %b, %a : f32
%s = index.sizeof
affine.store %d, %alloc_16[%i, %s] : memref<20x10xf32>
}
}
affine.for %i = 2 to 7 step 7 {
affine.for %j = 0 to 10 {
%v = affine.load %alloc_16[%j mod 20, %j] : memref<20x10xf32>
}
}
return
}
```
`reproducer-segv.mlir` (same command; terminates with SIGSEGV, exit status 139, in 5/5 runs):
```mlir
func.func @f(%arg1 : memref<20x10xf32>) {
%alloc_16 = memref.alloc() : memref<20x10xf32>
%cst = arith.constant 1.000000e+00 : f32
affine.for %i = 0 to 20 {
affine.for %j = 0 to 10 {
%c0 = arith.constant 0 : index
%dim = memref.dim %arg1, %c0 : memref<20x10xf32>
affine.store %cst, %alloc_16[%i, %dim] : memref<20x10xf32>
}
}
affine.for %i = 2 to 7 step 7 {
affine.for %j = 0 to 10 {
%v = affine.load %alloc_16[%j mod 20, %j] : memref<20x10xf32>
}
}
return
}
```
#### Stack Dump
The following block is the unmodified stderr from `reproducer.mlir`:
```text
reproducer.mlir:10:12: error: 'index.sizeof' op operation destroyed but still has uses
%s = index.sizeof
^
reproducer.mlir:10:12: note: see current operation: %0 = "index.sizeof"() : () -> index
reproducer.mlir:16:12: note: - use: %7 = "affine.load"(%0, %arg1, <<UNKNOWN SSA VALUE>>, %11) <{map = affine_map<(d0)[s0, s1] -> (d0 + (d0 floordiv 20) * -20 - s0, d0 - s1)>}> : (memref<1x1xf32>, index, index, index) -> f32
%v = affine.load %alloc_16[%j mod 20, %j] : memref<20x10xf32>
^
reproducer.mlir:5:3: note: - use: %4 = "affine.apply"(<<UNKNOWN SSA VALUE>>) <{map = affine_map<()[s0] -> (s0)>}> : (index) -> index
affine.for %i = 0 to 20 {
^
LLVM ERROR: operation destroyed but still has uses
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace and instructions to reproduce the bug.
Stack dump:
0. Program arguments: /12T/qin/Fuzzing/MLIR_latest_20261104/install/bin/mlir-opt --mlir-disable-threading reproducer.mlir --pass-pipeline=builtin.module(func.func(affine-loop-fusion{mode=producer})) -o /dev/null
#0 0x000055686665ca29 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/12T/qin/Fuzzing/MLIR_latest_20261104/install/bin/mlir-opt+0x1fb9a29)
#1 0x000055686665922c llvm::sys::RunSignalHandlers() (/12T/qin/Fuzzing/MLIR_latest_20261104/install/bin/mlir-opt+0x1fb622c)
#2 0x0000556866659396 SignalHandler(int, siginfo_t*, void*) Signals.cpp:0:0
#3 0x00007f56c4c08520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
#4 0x00007f56c4c5c9bc __pthread_kill_implementation ./nptl/pthread_kill.c:44:76
#5 0x00007f56c4c5c9bc __pthread_kill_internal ./nptl/pthread_kill.c:78:10
#6 0x00007f56c4c5c9bc pthread_kill ./nptl/pthread_kill.c:89:10
#7 0x00007f56c4c08476 gsignal ./signal/../sysdeps/posix/raise.c:27:6
#8 0x00007f56c4bee7f3 abort ./stdlib/abort.c:81:7
#9 0x00005568665f8785 llvm::ConvertUTF8toUTF16(unsigned char const**, unsigned char const*, unsigned short**, unsigned short*, llvm::ConversionFlags) (.cold) ConvertUTF.cpp:0:0
#10 0x000055686663ced5 (/12T/qin/Fuzzing/MLIR_latest_20261104/install/bin/mlir-opt+0x1f99ed5)
#11 0x000055686ef9ccea mlir::Operation::~Operation() (/12T/qin/Fuzzing/MLIR_latest_20261104/install/bin/mlir-opt+0xa8f9cea)
#12 0x000055686ef9cf59 llvm::ilist_traits<mlir::Operation>::deleteNode(mlir::Operation*) (/12T/qin/Fuzzing/MLIR_latest_20261104/install/bin/mlir-opt+0xa8f9f59)
#13 0x000055686eec93f3 mlir::Block::~Block() (/12T/qin/Fuzzing/MLIR_latest_20261104/install/bin/mlir-opt+0xa8263f3)
#14 0x000055686efadd61 mlir::Region::~Region() (/12T/qin/Fuzzing/MLIR_latest_20261104/install/bin/mlir-opt+0xa90ad61)
#15 0x000055686ef9cd8c mlir::Operation::~Operation() (/12T/qin/Fuzzing/MLIR_latest_20261104/install/bin/mlir-opt+0xa8f9d8c)
#16 0x000055686ef9cf59 llvm::ilist_traits<mlir::Operation>::deleteNode(mlir::Operation*) (/12T/qin/Fuzzing/MLIR_latest_20261104/install/bin/mlir-opt+0xa8f9f59)
#17 0x000055686eec93f3 mlir::Block::~Block() (/12T/qin/Fuzzing/MLIR_latest_20261104/install/bin/mlir-opt+0xa8263f3)
#18 0x000055686efadd61 mlir::Region::~Region() (/12T/qin/Fuzzing/MLIR_latest_20261104/install/bin/mlir-opt+0xa90ad61)
#19 0x000055686ef9cd8c mlir::Operation::~Operation() (/12T/qin/Fuzzing/MLIR_latest_20261104/install/bin/mlir-opt+0xa8f9d8c)
#20 0x000055686ef9d00b mlir::Operation::erase() (/12T/qin/Fuzzing/MLIR_latest_20261104/install/bin/mlir-opt+0xa8fa00b)
#21 0x000055686d4cbd12 (anonymous namespace)::GreedyFusion::performFusionsIntoDest(unsigned int, unsigned int) LoopFusion.cpp:0:0
#22 0x000055686d4cccea (anonymous namespace)::GreedyFusion::fuseProducerConsumerNodes(unsigned int) LoopFusion.cpp:0:0
#23 0x000055686d4cf7d7 (anonymous namespace)::LoopFusion::runOnBlock(mlir::Block*) LoopFusion.cpp:0:0
#24 0x000055686d4cfb67 void mlir::detail::walk<mlir::ForwardIterator>(mlir::Operation*, llvm::function_ref<void (mlir::Operation*)>, mlir::WalkOrder) (.constprop.0) LoopFusion.cpp:0:0
#25 0x000055686d4cfc1c (anonymous namespace)::LoopFusion::runOnOperation() LoopFusion.cpp:0:0
#26 0x000055686ecb96d9 mlir::detail::OpToOpPassAdaptor::run(mlir::Pass*, mlir::Operation*, mlir::AnalysisManager, bool, unsigned int) (/12T/qin/Fuzzing/MLIR_latest_20261104/install/bin/mlir-opt+0xa6166d9)
#27 0x000055686ecb99b4 mlir::detail::OpToOpPassAdaptor::runPipeline(mlir::OpPassManager&, mlir::Operation*, mlir::AnalysisManager, bool, unsigned int, mlir::PassInstrumentor*, mlir::PassInstrumentation::PipelineParentInfo const*) (/12T/qin/Fuzzing/MLIR_latest_20261104/install/bin/mlir-opt+0xa6169b4)
#28 0x000055686ecba8b4 mlir::detail::OpToOpPassAdaptor::runOnOperationImpl(bool) (/12T/qin/Fuzzing/MLIR_latest_20261104/install/bin/mlir-opt+0xa6178b4)
#29 0x000055686ecb95b1 mlir::detail::OpToOpPassAdaptor::run(mlir::Pass*, mlir::Operation*, mlir::AnalysisManager, bool, unsigned int) (/12T/qin/Fuzzing/MLIR_latest_20261104/install/bin/mlir-opt+0xa6165b1)
#30 0x000055686ecb99b4 mlir::detail::OpToOpPassAdaptor::runPipeline(mlir::OpPassManager&, mlir::Operation*, mlir::AnalysisManager, bool, unsigned int, mlir::PassInstrumentor*, mlir::PassInstrumentation::PipelineParentInfo const*) (/12T/qin/Fuzzing/MLIR_latest_20261104/install/bin/mlir-opt+0xa6169b4)
#31 0x000055686ecbb602 mlir::PassManager::runPasses(mlir::Operation*, mlir::AnalysisManager) (/12T/qin/Fuzzing/MLIR_latest_20261104/install/bin/mlir-opt+0xa618602)
#32 0x000055686ecbc5d9 mlir::PassManager::run(mlir::Operation*) (/12T/qin/Fuzzing/MLIR_latest_20261104/install/bin/mlir-opt+0xa6195d9)
#33 0x00005568667270e6 performActions(llvm::raw_ostream&, std::shared_ptr<llvm::SourceMgr> const&, mlir::MLIRContext*, mlir::MlirOptMainConfig const&) MlirOptMain.cpp:0:0
#34 0x000055686672869e processBuffer(llvm::raw_ostream&, std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer>>, llvm::MemoryBufferRef, mlir::MlirOptMainConfig const&, mlir::DialectRegistry&, mlir::SourceMgrDiagnosticVerifierHandler*, llvm::ThreadPoolInterface*) MlirOptMain.cpp:0:0
#35 0x00005568667288e1 llvm::LogicalResult llvm::function_ref<llvm::LogicalResult (std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer>>, llvm::MemoryBufferRef const&, llvm::raw_ostream&)>::callback_fn<mlir::MlirOptMain(llvm::raw_ostream&, std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer>>, mlir::DialectRegistry&, mlir::MlirOptMainConfig const&)::'lambda'(std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer>>, llvm::MemoryBufferRef, llvm::raw_ostream&)>(long, std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer>>, llvm::MemoryBufferRef const&, llvm::raw_ostream&) MlirOptMain.cpp:0:0
#36 0x000055686eff730d mlir::splitAndProcessBuffer(std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer>>, llvm::function_ref<llvm::LogicalResult (std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer>>, llvm::MemoryBufferRef const&, llvm::raw_ostream&)>, llvm::raw_ostream&, llvm::StringRef, llvm::StringRef) (/12T/qin/Fuzzing/MLIR_latest_20261104/install/bin/mlir-opt+0xa95430d)
#37 0x00005568667225da mlir::MlirOptMain(llvm::raw_ostream&, std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer>>, mlir::DialectRegistry&, mlir::MlirOptMainConfig const&) (.part.0) MlirOptMain.cpp:0:0
#38 0x0000556866728f21 mlir::MlirOptMain(int, char**, llvm::StringRef, llvm::StringRef, mlir::DialectRegistry&) (/12T/qin/Fuzzing/MLIR_latest_20261104/install/bin/mlir-opt+0x2085f21)
#39 0x0000556866729174 mlir::MlirOptMain(int, char**, llvm::StringRef, mlir::DialectRegistry&) (/12T/qin/Fuzzing/MLIR_latest_20261104/install/bin/mlir-opt+0x2086174)
#40 0x000055686660092c main (/12T/qin/Fuzzing/MLIR_latest_20261104/install/bin/mlir-opt+0x1f5d92c)
#41 0x00007f56c4befd90 __libc_start_call_main ./csu/../sysdeps/nptl/libc_start_call_main.h:58:16
#42 0x00007f56c4befe40 call_init ./csu/../csu/libc-start.c:128:20
#43 0x00007f56c4befe40 __libc_start_main ./csu/../csu/libc-start.c:379:5
#44 0x000055686663c2c5 _start (/12T/qin/Fuzzing/MLIR_latest_20261104/install/bin/mlir-opt+0x1f992c5)
```
The following block is the unmodified stderr from `reproducer-segv.mlir`:
```text
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace and instructions to reproduce the bug.
Stack dump:
0. Program arguments: /12T/qin/Fuzzing/MLIR_latest_20261104/install/bin/mlir-opt --mlir-disable-threading reproducer-segv.mlir --pass-pipeline=builtin.module(func.func(affine-loop-fusion{mode=producer})) -o /dev/null
#0 0x0000558380d12a29 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/12T/qin/Fuzzing/MLIR_latest_20261104/install/bin/mlir-opt+0x1fb9a29)
#1 0x0000558380d0f22c llvm::sys::RunSignalHandlers() (/12T/qin/Fuzzing/MLIR_latest_20261104/install/bin/mlir-opt+0x1fb622c)
#2 0x0000558380d0f396 SignalHandler(int, siginfo_t*, void*) Signals.cpp:0:0
#3 0x00007f6f238ac520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
#4 0x0000558388f8f40d mlir::memref::DimOp::getConstantIndex() (/12T/qin/Fuzzing/MLIR_latest_20261104/install/bin/mlir-opt+0xa23640d)
#5 0x0000558388f8f4e6 mlir::memref::DimOp::getSpeculatability() (/12T/qin/Fuzzing/MLIR_latest_20261104/install/bin/mlir-opt+0xa2364e6)
#6 0x0000558388f443aa mlir::detail::ConditionallySpeculatableInterfaceTraits::Model<mlir::memref::DimOp>::getSpeculatability(mlir::detail::ConditionallySpeculatableInterfaceTraits::Concept const*, mlir::Operation*) (/12T/qin/Fuzzing/MLIR_latest_20261104/install/bin/mlir-opt+0xa1eb3aa)
#7 0x00005583894818af mlir::isSpeculatable(mlir::Operation*) (/12T/qin/Fuzzing/MLIR_latest_20261104/install/bin/mlir-opt+0xa7288af)
#8 0x0000558389481a5d mlir::isPure(mlir::Operation*) (/12T/qin/Fuzzing/MLIR_latest_20261104/install/bin/mlir-opt+0xa728a5d)
#9 0x0000558388efe89a mlir::affine::isValidSymbol(mlir::Value, mlir::Region*) (/12T/qin/Fuzzing/MLIR_latest_20261104/install/bin/mlir-opt+0xa1a589a)
#10 0x0000558388eff535 mlir::affine::isValidDim(mlir::Value, mlir::Region*) (/12T/qin/Fuzzing/MLIR_latest_20261104/install/bin/mlir-opt+0xa1a6535)
#11 0x0000558388f00c7a mlir::affine::AffineApplyOp::verify() (/12T/qin/Fuzzing/MLIR_latest_20261104/install/bin/mlir-opt+0xa1a7c7a)
#12 0x0000558388f1530f mlir::RegisteredOperationName::Model<mlir::affine::AffineApplyOp<truncated>Please see the issue for the entire body.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs