Issue 237734
Summary [mlir][bug][fuzzer-found] -lower-affine asserts on affine.parallel bounds containing mod and floordiv by the same symbol
Labels mlir
Assignees
Reporter Kiritobl
    

#### Version

git commit hash: 982197fad060f461277020c0ca413d93a5f2a8a6

LLVM/MLIR 24.0.0git, optimized build with assertions.

#### Bug Description

`-lower-affine` aborts in `getSemiAffineExprFromFlatForm` when an `affine.parallel` bound contains both `d0 mod s0` and `d0 floordiv s0`. The input below parses and verifies successfully without the pass. It was found by fuzzing and reduced to an outer `affine.for` and one `affine.parallel`.

The two distinct local expressions receive the same `(dimension position, symbol position)` key, `(0, 0)`. Inserting the second key trips the `Key is already present in indices vector` assertion.

#### Crash Chain & Root Cause

Crash chain:

```text
`-lower-affine`
  → dialect conversion tries to fold affine.parallel
  → AffineParallelOp::fold → canonicalizeLoopBounds
  → AffineValueMap::canonicalize → composeAffineMapAndOperands
  → simplifyAffineMap → simplifyAffineExpr
  → getSemiAffineExprFromFlatForm::addEntry
  → duplicate-key assertion and SIGABRT
```

In `mlir/lib/Dialect/Affine/IR/AffineOps.cpp:4379–4401`, folding the `affine.parallel` canonicalizes its bounds. `composeAffineMapAndOperands` then simplifies the bound map. The outer loop IV `%i` is a valid dimension but not a valid symbol; `%s` remains a symbol. The resulting _expression_ has the form `(d0 mod s0) + d0 + (d0 floordiv s0)`.

`simplifyAffineExpr` flattens `d0 mod s0` and `d0 floordiv s0` into separate locals. Its unchanged-_expression_ early return does not apply because reconstruction puts the plain dimension first, changing the _expression_'s tree order. In `mlir/lib/IR/AffineExpr.cpp:1264–1273`, `getSemiAffineExprFromFlatForm` uses only the dimension and symbol positions to key each local. Both locals get `(0, 0)`, so `addEntry` asserts at `AffineExpr.cpp:1194`. Replacing either symbolic divisor with a constant avoids the collision on the original fuzzer input; placing the plain dimension first avoids the reconstruction path in the minimized input.

This is related to #58419, which reported the same assertion and identified duplicate indices in this helper. The minimized reproducer from #58419 succeeds on this build, but the distinct `mod`/`floordiv` locals in this reproducer still collide. Expected behavior is for `-lower-affine` to handle the verifier-accepted bound without aborting.

#### Steps to Reproduce

```bash
mlir-opt -lower-affine reproducer.mlir -o /dev/null
```

The process terminates with `SIGABRT` (shell exit status 134). Running `mlir-opt reproducer.mlir -o /dev/null` without the pass succeeds.

#### Reproducer (`reproducer.mlir`)

```mlir
func.func @f(%s: index) {
  affine.for %i = 0 to 4 {
    affine.parallel (%j) = ((%i mod symbol(%s)) + %i + (%i floordiv symbol(%s))) to (8) {
    }
  }
  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/IR/AffineExpr.cpp:1194: getSemiAffineExprFromFlatForm(llvm::ArrayRef<long int>, unsigned int, unsigned int, llvm::ArrayRef<mlir::AffineExpr>, mlir::MLIRContext*)::<lambda(std::pair<unsigned int, int>, int64_t, mlir::AffineExpr)>: Assertion `!llvm::is_contained(indices, index) && "Key is already present in indices vector and overwriting will " "happen in `indexToExprMap` and `coefficients`!"' 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 -lower-affine reproducer.mlir -o /dev/null
 #0 0x0000562d4e24b819 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/12T/qin/Fuzzing/MLIR_latest_20261028/install/bin/mlir-opt+0x1fbb819)
 #1 0x0000562d4e24801c llvm::sys::RunSignalHandlers() (/12T/qin/Fuzzing/MLIR_latest_20261028/install/bin/mlir-opt+0x1fb801c)
 #2 0x0000562d4e248186 SignalHandler(int, siginfo_t*, void*) Signals.cpp:0:0
 #3 0x00007f4cc211e520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
 #4 0x00007f4cc21729bc __pthread_kill_implementation ./nptl/pthread_kill.c:44:76
 #5 0x00007f4cc21729bc __pthread_kill_internal ./nptl/pthread_kill.c:78:10
 #6 0x00007f4cc21729bc pthread_kill ./nptl/pthread_kill.c:89:10
 #7 0x00007f4cc211e476 gsignal ./signal/../sysdeps/posix/raise.c:27:6
 #8 0x00007f4cc21047f3 abort ./stdlib/abort.c:81:7
 #9 0x00007f4cc210471b _nl_load_domain ./intl/loadmsgcat.c:1177:9
#10 0x00007f4cc2115e96 (/lib/x86_64-linux-gnu/libc.so.6+0x39e96)
#11 0x0000562d56a2d2a5 getSemiAffineExprFromFlatForm(llvm::ArrayRef<long>, unsigned int, unsigned int, llvm::ArrayRef<mlir::AffineExpr>, mlir::MLIRContext*)::'lambda'(std::pair<unsigned int, int>, long, mlir::AffineExpr)::operator()(std::pair<unsigned int, int>, long, mlir::AffineExpr) const AffineExpr.cpp:0:0
#12 0x0000562d56a2da25 mlir::simplifyAffineExpr(mlir::AffineExpr, unsigned int, unsigned int) (/12T/qin/Fuzzing/MLIR_latest_20261028/install/bin/mlir-opt+0xa79da25)
#13 0x0000562d56a33b4f mlir::simplifyAffineMap(mlir::AffineMap) (/12T/qin/Fuzzing/MLIR_latest_20261028/install/bin/mlir-opt+0xa7a3b4f)
#14 0x0000562d5638ab05 composeAffineMapAndOperands(mlir::AffineMap*, llvm::SmallVectorImpl<mlir::Value>*, bool) (.part.0) AffineOps.cpp:0:0
#15 0x0000562d5638c2df mlir::affine::AffineValueMap::canonicalize() (/12T/qin/Fuzzing/MLIR_latest_20261028/install/bin/mlir-opt+0xa0fc2df)
#16 0x0000562d5638c47a canonicalizeLoopBounds(mlir::affine::AffineParallelOp) AffineOps.cpp:0:0
#17 0x0000562d5638c670 mlir::RegisteredOperationName::Model<mlir::affine::AffineParallelOp>::foldHook(mlir::Operation*, llvm::ArrayRef<mlir::Attribute>, llvm::SmallVectorImpl<mlir::OpFoldResult>&) (/12T/qin/Fuzzing/MLIR_latest_20261028/install/bin/mlir-opt+0xa0fc670)
#18 0x0000562d56b3a614 mlir::Operation::fold(llvm::ArrayRef<mlir::Attribute>, llvm::SmallVectorImpl<mlir::OpFoldResult>&) (/12T/qin/Fuzzing/MLIR_latest_20261028/install/bin/mlir-opt+0xa8aa614)
#19 0x0000562d56b3aabb mlir::Operation::fold(llvm::SmallVectorImpl<mlir::OpFoldResult>&) (/12T/qin/Fuzzing/MLIR_latest_20261028/install/bin/mlir-opt+0xa8aaabb)
#20 0x0000562d56a70b07 mlir::OpBuilder::tryFold(mlir::Operation*, llvm::SmallVectorImpl<mlir::Value>&, llvm::SmallVectorImpl<mlir::Operation*>*) (/12T/qin/Fuzzing/MLIR_latest_20261028/install/bin/mlir-opt+0xa7e0b07)
#21 0x0000562d56760f62 (anonymous namespace)::OperationLegalizer::legalizeWithFold(mlir::Operation*) DialectConversion.cpp:0:0
#22 0x0000562d56761d3b (anonymous namespace)::OperationLegalizer::legalize(mlir::Operation*) DialectConversion.cpp:0:0
#23 0x0000562d567621a4 mlir::OperationConverter::convert(mlir::Operation*, bool) (/12T/qin/Fuzzing/MLIR_latest_20261028/install/bin/mlir-opt+0xa4d21a4)
#24 0x0000562d5676c5c6 mlir::OperationConverter::applyConversion(llvm::ArrayRef<mlir::Operation*>) (/12T/qin/Fuzzing/MLIR_latest_20261028/install/bin/mlir-opt+0xa4dc5c6)
#25 0x0000562d5676dbff applyConversion(llvm::ArrayRef<mlir::Operation*>, mlir::ConversionTarget const&, mlir::FrozenRewritePatternSet const&, mlir::ConversionConfig, (anonymous namespace)::OpConversionMode) DialectConversion.cpp:0:0
#26 0x0000562d5676ddb6 mlir::applyPartialConversion(mlir::Operation*, mlir::ConversionTarget const&, mlir::FrozenRewritePatternSet const&, mlir::ConversionConfig) (/12T/qin/Fuzzing/MLIR_latest_20261028/install/bin/mlir-opt+0xa4dddb6)
#27 0x0000562d52e7c560 (anonymous namespace)::LowerAffine::runOnOperation() AffineToStandard.cpp:0:0
#28 0x0000562d5681d6d9 mlir::detail::OpToOpPassAdaptor::run(mlir::Pass*, mlir::Operation*, mlir::AnalysisManager, bool, unsigned int) (/12T/qin/Fuzzing/MLIR_latest_20261028/install/bin/mlir-opt+0xa58d6d9)
#29 0x0000562d5681d9b4 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)
#30 0x0000562d5681f602 mlir::PassManager::runPasses(mlir::Operation*, mlir::AnalysisManager) (/12T/qin/Fuzzing/MLIR_latest_20261028/install/bin/mlir-opt+0xa58f602)
#31 0x0000562d568205d9 mlir::PassManager::run(mlir::Operation*) (/12T/qin/Fuzzing/MLIR_latest_20261028/install/bin/mlir-opt+0xa5905d9)
#32 0x0000562d4e313c69 performActions(llvm::raw_ostream&, std::shared_ptr<llvm::SourceMgr> const&, mlir::MLIRContext*, mlir::MlirOptMainConfig const&) MlirOptMain.cpp:0:0
#33 0x0000562d4e31514e 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
#34 0x0000562d4e315391 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
#35 0x0000562d56b955ed 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)
#36 0x0000562d4e30f15a 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
#37 0x0000562d4e3159d1 mlir::MlirOptMain(int, char**, llvm::StringRef, llvm::StringRef, mlir::DialectRegistry&) (/12T/qin/Fuzzing/MLIR_latest_20261028/install/bin/mlir-opt+0x20859d1)
#38 0x0000562d4e315c24 mlir::MlirOptMain(int, char**, llvm::StringRef, mlir::DialectRegistry&) (/12T/qin/Fuzzing/MLIR_latest_20261028/install/bin/mlir-opt+0x2085c24)
#39 0x0000562d4e1e68c7 main (/12T/qin/Fuzzing/MLIR_latest_20261028/install/bin/mlir-opt+0x1f568c7)
#40 0x00007f4cc2105d90 __libc_start_call_main ./csu/../sysdeps/nptl/libc_start_call_main.h:58:16
#41 0x00007f4cc2105e40 call_init ./csu/../csu/libc-start.c:128:20
#42 0x00007f4cc2105e40 __libc_start_main ./csu/../csu/libc-start.c:379:5
#43 0x0000562d4e22b0b5 _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

Reply via email to