| Issue |
237552
|
| Summary |
[SCEV][LoopUnroll] Stack overflow: SCEV recursion depth grows with the number of unrolled loop copies
|
| Labels |
new issue
|
| Assignees |
|
| Reporter |
OfekShilon
|
`loop-unroll` partially unrolls an outer loop by a large factor. Each unrolled copy contains the inner loop. During `simplifyLoopAfterUnroll` ScalarEvolution recurses through the inner-loop copies, clang and opt crash after ~1500-1700 copies.
godbolt: https://godbolt.org/z/E6x7E5nYb
### C++ reproducer
```cpp
void g();
void f(long s) {
#pragma clang loop unroll(enable)
for (int k = 0; k < 20000; k++)
for (long j = s; j < 0; j += 3)
g();
}
```
```
$ ulimit -s 8192
$ clang++ -O3 -c repro.cpp -fno-integrated-cc1
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace and dumped files.
Stack dump:
0. Program arguments: clang-24 -cc1 -triple x86_64-unknown-linux-gnu -O3 -emit-obj ...
1. <eof> parser at end of file
2. Optimizer
3. Running pass "function<eager-inv>(...,loop-unroll<O3>,...)" on module "repro.cpp"
4. Running pass "loop-unroll<O3>" on function "_Z1fl"
#0 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int)
...
#4 llvm::ScalarEvolution::computeSCEVAtScope(llvm::SCEV const*, llvm::Loop const*)
...
```
Without `-fno-integrated-cc1`, clang dies with SIGSEGV and prints nothing. `-O2` fails the same way. With `ulimit -s unlimited`, clang passes and reports `unrolled loop by a factor of 2500`.
Backtrace (gdb on cc1, trimmed). 32714 frames in total. The cycle is:
```
#24 llvm::ScalarEvolution::isBasicBlockEntryGuardedByCond
#25 llvm::ScalarEvolution::howManyLessThans
#26 llvm::ScalarEvolution::computeExitLimitFromICmp
#27 llvm::ScalarEvolution::computeExitLimitFromICmp
#28 llvm::ScalarEvolution::computeExitLimitFromCondImpl
#29 llvm::ScalarEvolution::computeExitLimitFromCondCached
#30 llvm::ScalarEvolution::computeExitLimitFromCond
#31 llvm::ScalarEvolution::computeExitLimit
#32 llvm::ScalarEvolution::computeBackedgeTakenCount
#33 llvm::ScalarEvolution::getBackedgeTakenInfo
#34 llvm::ScalarEvolution::getRangeRef
#35 llvm::ScalarEvolution::getSignedRange
#36 llvm::ScalarEvolution::isKnownPredicateViaConstantRanges
#37 llvm::ScalarEvolution::isKnownViaNonRecursiveReasoning
#38 llvm::ScalarEvolution::isImpliedCondOperandsHelper
#39 llvm::ScalarEvolution::isImpliedCondBalancedTypes
#40 llvm::ScalarEvolution::isImpliedCond
#41 llvm::ScalarEvolution::isImpliedCond
#42 std::_Function_handler<...> (lambda in isBasicBlockEntryGuardedByCond)
#43 llvm::ScalarEvolution::isBasicBlockEntryGuardedByCond
... repeats 1720 times ...
#32690 llvm::simplifyLoopIVs
#32691 llvm::simplifyLoopAfterUnroll
#32692 llvm::UnrollLoop
#32693 tryToUnrollLoop
#32694 llvm::LoopUnrollPass::run
```
### IR reproducer
```llvm
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"
define void @f(i64 %0) {
.lr.ph2876:
br label %.lr.ph.us
.lr.ph.us:
%.09792581.us = phi i16 [ %3, %._crit_edge.split.us.us ], [ 0, %.lr.ph2876 ]
br label %1
1:
%indvars.iv3735 = phi i64 [ %indvars.iv.next3736, %1 ], [ %0, %.lr.ph.us ]
%indvars.iv.next3736 = add i64 %indvars.iv3735, 1
%2 = icmp slt i64 %indvars.iv3735, 0
br i1 %2, label %1, label %._crit_edge.split.us.us
._crit_edge.split.us.us:
%3 = add i16 %.09792581.us, 1
%exitcond3739.not = icmp eq i16 %3, 0
br i1 %exitcond3739.not, label %._crit_edge2584.split2586.us, label %.lr.ph.us, !llvm.loop !0
._crit_edge2584.split2586.us:
ret void
}
!0 = distinct !{!0, !1, !2}
!1 = !{!"llvm.loop.mustprogress"}
!2 = !{!"llvm.loop.unroll.enable"}
```
```
$ ulimit -s 8192
$ opt -passes=loop-unroll -disable-output repro.ll
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: opt -passes=loop-unroll repro.ll -disable-output
1. Running pass "function(loop-unroll<O2>)" on module "repro.ll"
2. Running pass "loop-unroll<O2>" on function "f"
...
Segmentation fault
```
The unroll factor is 2048. Here the SCEVs for the IVs do not exist yet, so the cycle also goes through SCEV creation: `createAddRecFromPHI` → `createSimpleAffineAddRec` → `inferNoWrapViaConstantRanges` → `getSignedRange` → `getRangeRef` → `getBackedgeTakenInfo` → ... → `isImpliedCond` → `createSCEVIter` → `createNodeForPHI`. It repeats 1518 times (28853 frames).
### Related
- #85547 and #83296: stack overflows in SCEV after loop unrolling. Both are open. The backtraces differ: there, the recursion goes through `createNodeFromSelectLikePHI` and `getBackedgeTakenCount`.
- PR #226624 "[SCEV] Make PHI handling fully iterative" (open). I did not test it. The C++ case does not go through SCEV creation for PHIs at all, so that PR probably does not fix it.
### Version
```
clang version 24.0.0git (https://github.com/llvm/llvm-project.git 0dc8e3c1e8e8eed71db535d9ee242a22c7b51ca3)
Target: x86_64-unknown-linux-gnu
```
Found with a fork of the yarpgen fuzzer.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs