Issue 241121
Summary [mlir][bug][fuzzer-found] affine-loop-fusion asserts in APInt::getSExtValue while computing slice bounds for a large symbolic index constant
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 at `APInt.h:1585` while computing the dependence slice bounds of a loop nest whose affine memory access contains a large symbolic index constant (`arith.constant 4607252787007324200 : index`, used as `symbol(%c)` in the index _expression_).

Assertion:

```cpp
assert(getSignificantBits() <= 64 && "Too many bits for int64_t");
```

The minimized input below parses and verifies successfully without the pass. It aborted in 5/5 runs (SIGABRT, shell exit 134).

Magnitude ablation: replacing the constant with `1000000000000` or `4294967296` makes the crash disappear, while `1000000000000000000` still crashes. The failure therefore comes from intermediate constraint coefficients exceeding the signed 64-bit range, not from the literal itself.

#### Crash Chain & Root Cause

Crash chain:

```text
affine-loop-fusion{mode=producer}
  → mlir::affine::canFuseLoops
  → mlir::affine::computeSliceUnion
  → mlir::affine::getComputationSliceState
  → FlatLinearConstraints::getSliceBounds
  → detectAsFloorDiv
  → presburger::computeSingleVarRepr(IntegerRelation const&, ArrayRef<bool>, unsigned, SmallVector<long,8>&, unsigned&)
  → presburger::getInt64Vec
  → llvm::APInt::getSExtValue
```

`getSliceBounds` tries to recognize loop-local variables as floor divisions. `detectAsFloorDiv` (`FlatLinearValueConstraints.cpp:417`) calls the `int64_t` overload of `computeSingleVarRepr` (`mlir/lib/Analysis/Presburger/Utils.cpp:268-278`), which computes the dividend with arbitrary-precision `DynamicAPInt` and then converts it to `int64_t`:

```cpp
SmallVector<DynamicAPInt, 8> dividendDynamicAPInt(cst.getNumCols());
DynamicAPInt divisorDynamicAPInt;
MaybeLocalRepr result = computeSingleVarRepr(cst, foundRepr, pos, dividendDynamicAPInt, divisorDynamicAPInt);
dividend = getInt64Vec(dividendDynamicAPInt);   // Utils.cpp:275
```

`getInt64Vec` maps each element through `int64fromDynamicAPInt`, i.e. `int64_t(DynamicAPInt)` → `APInt::getSExtValue()`, with no representability check. With the large symbolic constant in the access map, the coefficients obtained from the constraint rows do not fit in `int64_t`, so the conversion asserts.

Expected: the int64 overload should detect the overflow and report `ReprKind::None` (or `getInt64Vec` should check representability) so that floor-div detection declines instead of aborting.

#### 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`)

```mlir
func.func @f(%B : memref<20x10xf32>, %A : memref<20x10xf32>) {
  %c = arith.constant 4607252787007324200 : index
  %cst = arith.constant 1.000000e+00 : f32
  affine.for %i = 0 to 2 {
    affine.for %j = 0 to 10 {
      affine.store %cst, %B[%i, %j] : memref<20x10xf32>
    }
  }
  affine.for %i = 0 to 2 {
    affine.for %j = 0 to 10 {
      %0 = affine.load %B[%i, (symbol(%c) mod 21 + %j floordiv 27 + symbol(%c)) mod 10] : memref<20x10xf32>
      affine.store %0, %A[%i, %j] : memref<20x10xf32>
    }
  }
  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_20261104/llvm-project/llvm/include/llvm/ADT/APInt.h:1585: int64_t llvm::APInt::getSExtValue() const: Assertion `getSignificantBits() <= 64 && "Too many bits for int64_t"' 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_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 0x000055997800ca29 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/12T/qin/Fuzzing/MLIR_latest_20261104/install/bin/mlir-opt+0x1fb9a29)
 #1 0x000055997800922c llvm::sys::RunSignalHandlers() (/12T/qin/Fuzzing/MLIR_latest_20261104/install/bin/mlir-opt+0x1fb622c)
 #2 0x0000559978009396 SignalHandler(int, siginfo_t*, void*) Signals.cpp:0:0
 #3 0x00007f9c9b678520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
 #4 0x00007f9c9b6cc9bc __pthread_kill_implementation ./nptl/pthread_kill.c:44:76
 #5 0x00007f9c9b6cc9bc __pthread_kill_internal ./nptl/pthread_kill.c:78:10
 #6 0x00007f9c9b6cc9bc pthread_kill ./nptl/pthread_kill.c:89:10
 #7 0x00007f9c9b678476 gsignal ./signal/../sysdeps/posix/raise.c:27:6
 #8 0x00007f9c9b65e7f3 abort ./stdlib/abort.c:81:7
 #9 0x00007f9c9b65e71b _nl_load_domain ./intl/loadmsgcat.c:1177:9
#10 0x00007f9c9b66fe96 (/lib/x86_64-linux-gnu/libc.so.6+0x39e96)
#11 0x0000559980a0dbbe (/12T/qin/Fuzzing/MLIR_latest_20261104/install/bin/mlir-opt+0xa9babbe)
#12 0x000055998082aabb mlir::presburger::getInt64Vec(llvm::ArrayRef<llvm::DynamicAPInt>) (/12T/qin/Fuzzing/MLIR_latest_20261104/install/bin/mlir-opt+0xa7d7abb)
#13 0x000055998082e31a mlir::presburger::computeSingleVarRepr(mlir::presburger::IntegerRelation const&, llvm::ArrayRef<bool>, unsigned int, llvm::SmallVector<long, 8u>&, unsigned int&) (/12T/qin/Fuzzing/MLIR_latest_20261104/install/bin/mlir-opt+0xa7db31a)
#14 0x0000559978ac5c40 detectAsFloorDiv(mlir::FlatLinearConstraints const&, unsigned int, mlir::MLIRContext*, llvm::SmallVectorImpl<mlir::AffineExpr>&) FlatLinearValueConstraints.cpp:0:0
#15 0x0000559978ad1ff4 mlir::FlatLinearConstraints::getSliceBounds(unsigned int, unsigned int, mlir::MLIRContext*, llvm::SmallVectorImpl<mlir::AffineMap>*, llvm::SmallVectorImpl<mlir::AffineMap>*, bool, bool) (/12T/qin/Fuzzing/MLIR_latest_20261104/install/bin/mlir-opt+0x2a7eff4)
#16 0x000055997ef708db mlir::affine::getComputationSliceState(mlir::Operation*, mlir::Operation*, mlir::affine::FlatAffineValueConstraints const&, unsigned int, bool, mlir::affine::ComputationSliceState*) (/12T/qin/Fuzzing/MLIR_latest_20261104/install/bin/mlir-opt+0x8f1d8db)
#17 0x000055997ef73dca mlir::affine::computeSliceUnion(llvm::ArrayRef<mlir::Operation*>, llvm::ArrayRef<mlir::Operation*>, unsigned int, unsigned int, bool, mlir::affine::ComputationSliceState*) (/12T/qin/Fuzzing/MLIR_latest_20261104/install/bin/mlir-opt+0x8f20dca)
#18 0x000055997eec8c53 mlir::affine::canFuseLoops(mlir::affine::AffineForOp, mlir::affine::AffineForOp, unsigned int, mlir::affine::ComputationSliceState*, mlir::affine::FusionStrategy) (/12T/qin/Fuzzing/MLIR_latest_20261104/install/bin/mlir-opt+0x8e75c53)
#19 0x000055997ee7b220 (anonymous namespace)::GreedyFusion::performFusionsIntoDest(unsigned int, unsigned int) LoopFusion.cpp:0:0
#20 0x000055997ee7ccea (anonymous namespace)::GreedyFusion::fuseProducerConsumerNodes(unsigned int) LoopFusion.cpp:0:0
#21 0x000055997ee7f7d7 (anonymous namespace)::LoopFusion::runOnBlock(mlir::Block*) LoopFusion.cpp:0:0
#22 0x000055997ee7fb67 void mlir::detail::walk<mlir::ForwardIterator>(mlir::Operation*, llvm::function_ref<void (mlir::Operation*)>, mlir::WalkOrder) (.constprop.0) LoopFusion.cpp:0:0
#23 0x000055997ee7fc1c (anonymous namespace)::LoopFusion::runOnOperation() LoopFusion.cpp:0:0
#24 0x00005599806696d9 mlir::detail::OpToOpPassAdaptor::run(mlir::Pass*, mlir::Operation*, mlir::AnalysisManager, bool, unsigned int) (/12T/qin/Fuzzing/MLIR_latest_20261104/install/bin/mlir-opt+0xa6166d9)
#25 0x00005599806699b4 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)
#26 0x000055998066a8b4 mlir::detail::OpToOpPassAdaptor::runOnOperationImpl(bool) (/12T/qin/Fuzzing/MLIR_latest_20261104/install/bin/mlir-opt+0xa6178b4)
#27 0x00005599806695b1 mlir::detail::OpToOpPassAdaptor::run(mlir::Pass*, mlir::Operation*, mlir::AnalysisManager, bool, unsigned int) (/12T/qin/Fuzzing/MLIR_latest_20261104/install/bin/mlir-opt+0xa6165b1)
#28 0x00005599806699b4 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)
#29 0x000055998066b602 mlir::PassManager::runPasses(mlir::Operation*, mlir::AnalysisManager) (/12T/qin/Fuzzing/MLIR_latest_20261104/install/bin/mlir-opt+0xa618602)
#30 0x000055998066c5d9 mlir::PassManager::run(mlir::Operation*) (/12T/qin/Fuzzing/MLIR_latest_20261104/install/bin/mlir-opt+0xa6195d9)
#31 0x00005599780d70e6 performActions(llvm::raw_ostream&, std::shared_ptr<llvm::SourceMgr> const&, mlir::MLIRContext*, mlir::MlirOptMainConfig const&) MlirOptMain.cpp:0:0
#32 0x00005599780d869e 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
#33 0x00005599780d88e1 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
#34 0x00005599809a730d 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)
#35 0x00005599780d25da 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
#36 0x00005599780d8f21 mlir::MlirOptMain(int, char**, llvm::StringRef, llvm::StringRef, mlir::DialectRegistry&) (/12T/qin/Fuzzing/MLIR_latest_20261104/install/bin/mlir-opt+0x2085f21)
#37 0x00005599780d9174 mlir::MlirOptMain(int, char**, llvm::StringRef, mlir::DialectRegistry&) (/12T/qin/Fuzzing/MLIR_latest_20261104/install/bin/mlir-opt+0x2086174)
#38 0x0000559977fb092c main (/12T/qin/Fuzzing/MLIR_latest_20261104/install/bin/mlir-opt+0x1f5d92c)
#39 0x00007f9c9b65fd90 __libc_start_call_main ./csu/../sysdeps/nptl/libc_start_call_main.h:58:16
#40 0x00007f9c9b65fe40 call_init ./csu/../csu/libc-start.c:128:20
#41 0x00007f9c9b65fe40 __libc_start_main ./csu/../csu/libc-start.c:379:5
#42 0x0000559977fec2c5 _start (/12T/qin/Fuzzing/MLIR_latest_20261104/install/bin/mlir-opt+0x1f992c5)
```

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to