Issue 237735
Summary [mlir][bug][fuzzer-found] --affine-expand-index-ops-as-affine asserts on linearize_index with a loop-IV dynamic basis
Labels mlir
Assignees
Reporter Kiritobl
    
#### Version

git commit hash: 982197fad060f461277020c0ca413d93a5f2a8a6

LLVM/MLIR 24.0.0git, optimized build with assertions.

#### Bug Description

`--affine-expand-index-ops-as-affine` aborts while expanding an `affine.linearize_index` with a dynamic basis value defined by a loop IV. The input below parses and verifies successfully without the pass. It was found by fuzzing and reduced to one live `affine.linearize_index` inside three loops.

The expansion builds `%i * %k + %j`. Both factors of the product become affine dimensions, but `getSemiAffineExprFromFlatForm` assumes the right-hand factor is an `AffineSymbolExpr` and performs an invalid cast.

#### Crash Chain & Root Cause

Crash chain:

```text
`--affine-expand-index-ops-as-affine`
  → LowerLinearizeIndexOps::matchAndRewrite
  → affine::linearizeIndex → computeLinearIndex
  → makeComposedFoldedAffineApply → composeAffineMapAndOperands
  → canonicalizeMapAndOperands demotes loop IV operands to dimensions
  → simplifyAffineMap → simplifyAffineExpr
  → getSemiAffineExprFromFlatForm casts RHS dimension to AffineSymbolExpr
  → cast assertion and SIGABRT
```

`mlir/lib/Dialect/Affine/Transforms/AffineExpandIndexOpsAsAffine.cpp:94–148` lowers `affine.linearize_index` through `linearizeIndex`. `computeLinearIndex` builds an index-times-stride term. In this input, the first index `%i`, dynamic basis `%k`, and additive index `%j` are loop IVs. `legalizeDemotedDims` in `mlir/lib/Dialect/Affine/IR/AffineOps.cpp:1632–1675` moves operands that are valid dimensions but not valid symbols into the dimension list, yielding `(d0, d1, d2) -> (d0 * d1 + d2)`.

`simplifyAffineExpr` enters semi-affine reconstruction because its flattened ordering differs from the original _expression_. `getSemiAffineExprFromFlatForm` sees a dimension LHS and a nonconstant RHS, then unconditionally executes `cast<AffineSymbolExpr>(rhs)` at `mlir/lib/IR/AffineExpr.cpp:1271`. The RHS is `d1`, so the cast asserts. Making the basis static or making the first index constant lets the pass complete on this build. The linearization result must remain live; otherwise the greedy rewrite driver can erase the dead operation before this pattern runs.


#### Steps to Reproduce

```bash
mlir-opt --affine-expand-index-ops-as-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(%out: memref<1xi64>) {
  %c0 = arith.constant 0 : index
  affine.for %i = 0 to 8 {
    affine.for %j = 0 to 8 {
      affine.for %k = 0 to 8 {
        %l = affine.linearize_index [%i, %j] by (%k) : index
        %c = arith.index_cast %l : index to i64
        memref.store %c, %out[%c0] : memref<1xi64>
      }
    }
  }
  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/llvm/include/llvm/Support/Casting.h:566: decltype(auto) llvm::cast(From&) [with To = mlir::AffineSymbolExpr; From = mlir::AffineExpr]: Assertion `isa<To>(Val) && "cast<Ty>() argument of incompatible type!"' 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 --affine-expand-index-ops-as-affine reproducer.mlir -o /dev/null
 #0 0x000055593afd9819 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/12T/qin/Fuzzing/MLIR_latest_20261028/install/bin/mlir-opt+0x1fbb819)
 #1 0x000055593afd601c llvm::sys::RunSignalHandlers() (/12T/qin/Fuzzing/MLIR_latest_20261028/install/bin/mlir-opt+0x1fb801c)
 #2 0x000055593afd6186 SignalHandler(int, siginfo_t*, void*) Signals.cpp:0:0
 #3 0x00007fe75e6bd520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
 #4 0x00007fe75e7119bc __pthread_kill_implementation ./nptl/pthread_kill.c:44:76
 #5 0x00007fe75e7119bc __pthread_kill_internal ./nptl/pthread_kill.c:78:10
 #6 0x00007fe75e7119bc pthread_kill ./nptl/pthread_kill.c:89:10
 #7 0x00007fe75e6bd476 gsignal ./signal/../sysdeps/posix/raise.c:27:6
 #8 0x00007fe75e6a37f3 abort ./stdlib/abort.c:81:7
 #9 0x00007fe75e6a371b _nl_load_domain ./intl/loadmsgcat.c:1177:9
#10 0x00007fe75e6b4e96 (/lib/x86_64-linux-gnu/libc.so.6+0x39e96)
#11 0x00005559437bb9f3 mlir::simplifyAffineExpr(mlir::AffineExpr, unsigned int, unsigned int) (/12T/qin/Fuzzing/MLIR_latest_20261028/install/bin/mlir-opt+0xa79d9f3)
#12 0x00005559437c1b4f mlir::simplifyAffineMap(mlir::AffineMap) (/12T/qin/Fuzzing/MLIR_latest_20261028/install/bin/mlir-opt+0xa7a3b4f)
#13 0x0000555943118b05 composeAffineMapAndOperands(mlir::AffineMap*, llvm::SmallVectorImpl<mlir::Value>*, bool) (.part.0) AffineOps.cpp:0:0
#14 0x000055594311bab0 mlir::affine::makeComposedAffineApply(mlir::OpBuilder&, mlir::Location, mlir::AffineMap, llvm::ArrayRef<mlir::OpFoldResult>, bool) (/12T/qin/Fuzzing/MLIR_latest_20261028/install/bin/mlir-opt+0xa0fdab0)
#15 0x000055594311bc05 mlir::affine::makeComposedFoldedAffineApply(mlir::OpBuilder&, mlir::Location, mlir::AffineMap, llvm::ArrayRef<mlir::OpFoldResult>, bool) (/12T/qin/Fuzzing/MLIR_latest_20261028/install/bin/mlir-opt+0xa0fdc05)
#16 0x000055594311c370 mlir::affine::makeComposedFoldedAffineApply(mlir::OpBuilder&, mlir::Location, mlir::AffineExpr, llvm::ArrayRef<mlir::OpFoldResult>, bool) (/12T/qin/Fuzzing/MLIR_latest_20261028/install/bin/mlir-opt+0xa0fe370)
#17 0x00005559419fc455 mlir::affine::linearizeIndex(mlir::OpBuilder&, mlir::Location, llvm::ArrayRef<mlir::OpFoldResult>, llvm::ArrayRef<mlir::OpFoldResult>) (/12T/qin/Fuzzing/MLIR_latest_20261028/install/bin/mlir-opt+0x89de455)
#18 0x0000555941969f4b (anonymous namespace)::LowerLinearizeIndexOps::matchAndRewrite(mlir::affine::AffineLinearizeIndexOp, mlir::PatternRewriter&) const AffineExpandIndexOpsAsAffine.cpp:0:0
#19 0x00005559435436ac mlir::PatternApplicator::matchAndRewrite(mlir::Operation*, mlir::PatternRewriter&, llvm::function_ref<bool (mlir::Pattern const&)>, llvm::function_ref<void (mlir::Pattern const&)>, llvm::function_ref<llvm::LogicalResult (mlir::Pattern const&)>)::'lambda'()::operator()() const PatternApplicator.cpp:0:0
#20 0x0000555943544d8a mlir::PatternApplicator::matchAndRewrite(mlir::Operation*, mlir::PatternRewriter&, llvm::function_ref<bool (mlir::Pattern const&)>, llvm::function_ref<void (mlir::Pattern const&)>, llvm::function_ref<llvm::LogicalResult (mlir::Pattern const&)>) (/12T/qin/Fuzzing/MLIR_latest_20261028/install/bin/mlir-opt+0xa526d8a)
#21 0x000055594350adb3 (anonymous namespace)::GreedyPatternRewriteDriver::processWorklist() GreedyPatternRewriteDriver.cpp:0:0
#22 0x000055594350d5f7 mlir::applyPatternsGreedily(mlir::Region&, mlir::FrozenRewritePatternSet const&, mlir::GreedyRewriteConfig, bool*) (/12T/qin/Fuzzing/MLIR_latest_20261028/install/bin/mlir-opt+0xa4ef5f7)
#23 0x00005559419686da (anonymous namespace)::ExpandAffineIndexOpsAsAffinePass::runOnOperation() AffineExpandIndexOpsAsAffine.cpp:0:0
#24 0x00005559435ab6d9 mlir::detail::OpToOpPassAdaptor::run(mlir::Pass*, mlir::Operation*, mlir::AnalysisManager, bool, unsigned int) (/12T/qin/Fuzzing/MLIR_latest_20261028/install/bin/mlir-opt+0xa58d6d9)
#25 0x00005559435ab9b4 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)
#26 0x00005559435ad602 mlir::PassManager::runPasses(mlir::Operation*, mlir::AnalysisManager) (/12T/qin/Fuzzing/MLIR_latest_20261028/install/bin/mlir-opt+0xa58f602)
#27 0x00005559435ae5d9 mlir::PassManager::run(mlir::Operation*) (/12T/qin/Fuzzing/MLIR_latest_20261028/install/bin/mlir-opt+0xa5905d9)
#28 0x000055593b0a1c69 performActions(llvm::raw_ostream&, std::shared_ptr<llvm::SourceMgr> const&, mlir::MLIRContext*, mlir::MlirOptMainConfig const&) MlirOptMain.cpp:0:0
#29 0x000055593b0a314e 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
#30 0x000055593b0a3391 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
#31 0x00005559439235ed 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)
#32 0x000055593b09d15a 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
#33 0x000055593b0a39d1 mlir::MlirOptMain(int, char**, llvm::StringRef, llvm::StringRef, mlir::DialectRegistry&) (/12T/qin/Fuzzing/MLIR_latest_20261028/install/bin/mlir-opt+0x20859d1)
#34 0x000055593b0a3c24 mlir::MlirOptMain(int, char**, llvm::StringRef, mlir::DialectRegistry&) (/12T/qin/Fuzzing/MLIR_latest_20261028/install/bin/mlir-opt+0x2085c24)
#35 0x000055593af748c7 main (/12T/qin/Fuzzing/MLIR_latest_20261028/install/bin/mlir-opt+0x1f568c7)
#36 0x00007fe75e6a4d90 __libc_start_call_main ./csu/../sysdeps/nptl/libc_start_call_main.h:58:16
#37 0x00007fe75e6a4e40 call_init ./csu/../csu/libc-start.c:128:20
#38 0x00007fe75e6a4e40 __libc_start_main ./csu/../csu/libc-start.c:379:5
#39 0x000055593afb90b5 _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