| Issue |
237821
|
| Summary |
[mlir][bug][fuzzer-found] --test-affine-loop-unswitch asserts on a loop-local index.sizeof condition operand
|
| Labels |
mlir
|
| Assignees |
|
| Reporter |
Kiritobl
|
#### Version
git commit hash: 982197fad060f461277020c0ca413d93a5f2a8a6
LLVM/MLIR 24.0.0git, optimized build with assertions.
#### Bug Description
`--test-affine-loop-unswitch` aborts in `affine::hoistAffineIfOp` when an `affine.if` condition uses an `index.sizeof` defined inside the enclosing `affine.for`. The assertion expects every condition operand to be a top-level value or an affine induction variable:
```cpp
assert(llvm::all_of(ifOp.getOperands(), [](Value v) {
return isTopLevelValue(v) || isAffineInductionVar(v);
}) && "operands not composed");
```
`index.sizeof` is a `Pure`, operand-less `index` op. The `affine.if` verifier accepts it as a symbol, because `isValidSymbol` accepts pure ops whose operands are valid symbols (here, vacuously). But it is defined in the loop body, so it is neither a top-level value nor an IV. The assertion fires.
The minimized input below parses and verifies successfully without the pass. It was reduced by hand to one loop, one `index.sizeof`, and one side-effecting `affine.if`. The process aborted in all 10 repeated runs on this build.
#### Crash Chain & Root Cause
Crash chain:
```text
--test-affine-loop-unswitch
→ TestAffineLoopUnswitching::runOnOperation
→ walk over AffineIfOp
→ affine::hoistAffineIfOp
→ applyOpPatternsGreedily(AffineIfOp canonicalization)
→ assert all operands are top-level values or affine IVs
→ "operands not composed" and SIGABRT
```
In `mlir/lib/Dialect/Affine/Utils/Utils.cpp:415-447`, `hoistAffineIfOp` canonicalizes the `affine.if` and then asserts the operand-composition invariant at `:443-447`. `AffineIfOp::getCanonicalizationPatterns` (`AffineOps.cpp:3365-3368`) only registers `SimplifyDeadElse` and `AlwaysTrueOrFalseIf`; neither rewrites a loop-local `index.sizeof` into a top-level value or an IV.
`index.sizeof` is defined inside the `affine.for` body, so `isTopLevelValue` (`AffineOps.cpp:50-54`) returns false. It is not an induction variable (`isAffineInductionVar`, `AffineOps.cpp:2954-2956`). At the same time, `isValidSymbol` (`AffineOps.cpp:457-489`) accepts it because it is a pure op with no operands, which is why the input verifies before the pass.
The empty-`if` form does not reach the assertion: with an empty then block, canonicalization removes the `if`, so the reproducer keeps a `func.call` in the body to preserve it.
The pass should handle this verifier-accepted `affine.if` without aborting or moving it past the definition of its condition operand.
#### Steps to Reproduce
```bash
mlir-opt --test-affine-loop-unswitch reproducer.mlir -o /dev/null
```
The process terminates with `SIGABRT` (shell exit status 134). It aborted in 10/10 runs on this build. Running `mlir-opt reproducer.mlir -o /dev/null` without the pass succeeds.
#### Reproducer (`reproducer.mlir`)
```mlir
func.func private @g()
func.func @f() {
affine.for %i = 0 to 4 {
%s = index.sizeof
affine.if affine_set<(d0) : (d0 >= 0)>(%s) {
func.call @g() : () -> ()
}
}
return
}
```
#### Stack Dump
The following block is the unmodified stderr from running the command above with this build:
```text
mlir-opt: /12T/qin/Fuzzing/MLIR_latest_20261028/llvm-project/mlir/lib/Dialect/Affine/Utils/Utils.cpp:443: llvm::LogicalResult mlir::affine::hoistAffineIfOp(mlir::affine::AffineIfOp, bool*): Assertion `llvm::all_of(ifOp.getOperands(), [](Value v) { return isTopLevelValue(v) || isAffineInductionVar(v); }) && "operands not composed"' failed.
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_20261028/install/bin/mlir-opt --test-affine-loop-unswitch reproducer.mlir -o /dev/null
#0 0x000055a82bb1d819 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/12T/qin/Fuzzing/MLIR_latest_20261028/install/bin/mlir-opt+0x1fbb819)
#1 0x000055a82bb1a01c llvm::sys::RunSignalHandlers() (/12T/qin/Fuzzing/MLIR_latest_20261028/install/bin/mlir-opt+0x1fb801c)
#2 0x000055a82bb1a186 SignalHandler(int, siginfo_t*, void*) Signals.cpp:0:0
#3 0x00007fe6d556b520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
#4 0x00007fe6d55bf9bc __pthread_kill_implementation ./nptl/pthread_kill.c:44:76
#5 0x00007fe6d55bf9bc __pthread_kill_internal ./nptl/pthread_kill.c:78:10
#6 0x00007fe6d55bf9bc pthread_kill ./nptl/pthread_kill.c:89:10
#7 0x00007fe6d556b476 gsignal ./signal/../sysdeps/posix/raise.c:27:6
#8 0x00007fe6d55517f3 abort ./stdlib/abort.c:81:7
#9 0x00007fe6d555171b _nl_load_domain ./intl/loadmsgcat.c:1177:9
#10 0x00007fe6d5562e96 (/lib/x86_64-linux-gnu/libc.so.6+0x39e96)
#11 0x000055a832543110 mlir::affine::hoistAffineIfOp(mlir::affine::AffineIfOp, bool*) (/12T/qin/Fuzzing/MLIR_latest_20261028/install/bin/mlir-opt+0x89e1110)
#12 0x000055a82bc096eb mlir::WalkResult mlir::detail::walk<mlir::ForwardIterator>(mlir::Operation*, llvm::function_ref<mlir::WalkResult (mlir::Operation*)>, mlir::WalkOrder) (.constprop.0) TestAffineLoopUnswitching.cpp:0:0
#13 0x000055a82bc09699 mlir::WalkResult mlir::detail::walk<mlir::ForwardIterator>(mlir::Operation*, llvm::function_ref<mlir::WalkResult (mlir::Operation*)>, mlir::WalkOrder) (.constprop.0) TestAffineLoopUnswitching.cpp:0:0
#14 0x000055a82bc09699 mlir::WalkResult mlir::detail::walk<mlir::ForwardIterator>(mlir::Operation*, llvm::function_ref<mlir::WalkResult (mlir::Operation*)>, mlir::WalkOrder) (.constprop.0) TestAffineLoopUnswitching.cpp:0:0
#15 0x000055a82bc09699 mlir::WalkResult mlir::detail::walk<mlir::ForwardIterator>(mlir::Operation*, llvm::function_ref<mlir::WalkResult (mlir::Operation*)>, mlir::WalkOrder) (.constprop.0) TestAffineLoopUnswitching.cpp:0:0
#16 0x000055a82bc09807 (anonymous namespace)::TestAffineLoopUnswitching::runOnOperation() TestAffineLoopUnswitching.cpp:0:0
#17 0x000055a8340ef6d9 mlir::detail::OpToOpPassAdaptor::run(mlir::Pass*, mlir::Operation*, mlir::AnalysisManager, bool, unsigned int) (/12T/qin/Fuzzing/MLIR_latest_20261028/install/bin/mlir-opt+0xa58d6d9)
#18 0x000055a8340ef9b4 mlir::detail::OpToOpPassAdaptor::runPipeline(mlir::OpPassManager&, mlir::Operation*, mlir::AnalysisManager, bool, unsigned int, mlir::PassInstrumentor*, mlir::PassInstrumentation::PipelineParentInfo const*) (/12T/qin/Fuzzing/MLIR_latest_20261028/install/bin/mlir-opt+0xa58d9b4)
#19 0x000055a8340f1602 mlir::PassManager::runPasses(mlir::Operation*, mlir::AnalysisManager) (/12T/qin/Fuzzing/MLIR_latest_20261028/install/bin/mlir-opt+0xa58f602)
#20 0x000055a8340f25d9 mlir::PassManager::run(mlir::Operation*) (/12T/qin/Fuzzing/MLIR_latest_20261028/install/bin/mlir-opt+0xa5905d9)
#21 0x000055a82bbe5c69 performActions(llvm::raw_ostream&, std::shared_ptr<llvm::SourceMgr> const&, mlir::MLIRContext*, mlir::MlirOptMainConfig const&) MlirOptMain.cpp:0:0
#22 0x000055a82bbe714e 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
#23 0x000055a82bbe7391 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
#24 0x000055a8344675ed 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_20261028/install/bin/mlir-opt+0xa9055ed)
#25 0x000055a82bbe115a 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
#26 0x000055a82bbe79d1 mlir::MlirOptMain(int, char**, llvm::StringRef, llvm::StringRef, mlir::DialectRegistry&) (/12T/qin/Fuzzing/MLIR_latest_20261028/install/bin/mlir-opt+0x20859d1)
#27 0x000055a82bbe7c24 mlir::MlirOptMain(int, char**, llvm::StringRef, mlir::DialectRegistry&) (/12T/qin/Fuzzing/MLIR_latest_20261028/install/bin/mlir-opt+0x2085c24)
#28 0x000055a82bab88c7 main (/12T/qin/Fuzzing/MLIR_latest_20261028/install/bin/mlir-opt+0x1f568c7)
#29 0x00007fe6d5552d90 __libc_start_call_main ./csu/../sysdeps/nptl/libc_start_call_main.h:58:16
#30 0x00007fe6d5552e40 call_init ./csu/../csu/libc-start.c:128:20
#31 0x00007fe6d5552e40 __libc_start_main ./csu/../csu/libc-start.c:379:5
#32 0x000055a82bafd0b5 _start (/12T/qin/Fuzzing/MLIR_latest_20261028/install/bin/mlir-opt+0x1f9b0b5)
```
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs