This is an archive of the discontinued LLVM Phabricator instance.

[WIP][LoopUnrolling] Add flag to restrict the unroll with large loop size
AbandonedPublic

Authored by Allen on Jun 21 2021, 7:01 PM.

Details

Summary

Disable large loop unroll as it will exhaust the stack if without setting with ulimit -s unlimited
while getting loop count in recursion function computeBackedgeTakenCount etc.

related issue:
https://bugs.llvm.org/show_bug.cgi?id=49783

Diff Detail

Event Timeline

Allen created this revision.Jun 21 2021, 7:01 PM
Allen requested review of this revision.Jun 21 2021, 7:01 PM
Herald added a project: Restricted Project. · View Herald TranscriptJun 21 2021, 7:01 PM
nikic added a subscriber: nikic.

Skipping very large loops might make sense as a compile-time optimization (don't bother analyzing loops that definitely can't be unrolled), but I don't think it's a solution to a stack overflow in SCEV. It can happen through other ways than loop unrolling. SCEV uses recursive construction and stack overflows are a known issue -- it deals with them through various depth cutoffs. Maybe one is missing somewhere?

Allen added a comment.Jun 22 2021, 2:12 AM

Skipping very large loops might make sense as a compile-time optimization (don't bother analyzing loops that definitely can't be unrolled), but I don't think it's a solution to a stack overflow in SCEV. It can happen through other ways than loop unrolling. SCEV uses recursive construction and stack overflows are a known issue -- it deals with them through various depth cutoffs. Maybe one is missing somewhere?

Yes, if a case unroll the loops manually may also happen such issue. the root reason is too many node to dispose SCEV
in this case, the recursive call between createSCEV and getSCEV, and both them don't have a depth cutoffs now. So need a cutoff in createSCEV ?

I'm still not fine with this approach:

  1. 1000 is way too low here. E.g. for znver3 the ideal loop size after unroll is 4096, which means that even partial unroll will blow past this threshold.
  2. The threshold will depend on the underlying libc, since the default stack size differs between them.
  3. for testing purposes implies this never happens in real world, while the very existence of the patch implies that it does..

It would be really best to deal with the actual stack overflows.

llvm/lib/Transforms/Scalar/LoopUnrollPass.cpp
1129

Agree with others, this seems like the wrong approach.

And yes, if we need to add a depth parameter to getSCEV, we should. I really doubt we do though. We probably *do* need to add one to all of the getXExpr variants though.

Yes, In fact, we already have many cutoffs , but they are only limit special type of getExpr , such as:

MaxArithDepth and MaxAddRecSize is used for getMulExpr, MaxCastDepth is used for getTruncateExpr, so it is natural that
we don't cover all the path of getXExpr variants.

if I add a new cutoff for getSCEV, there will still some other cases need to add, do we have some more general idea to dispose this ?
If no, I can just add one for getSCEV firstly.

Let me take a look, i might have an idea how to un-recurse it..

Allen added a comment.Jul 16 2021, 7:52 PM

Let me take a look, i might have an idea how to un-recurse it..

hi , do you have some idea ?

Allen retitled this revision from [LoopUnrolling] Add flag to restrict the unroll with large loop size to [WIP][LoopUnrolling] Add flag to restrict the unroll with large loop size.Mar 5 2022, 2:31 AM
Herald added a project: Restricted Project. · View Herald TranscriptMar 5 2022, 2:31 AM
reames resigned from this revision.Mar 5 2022, 12:50 PM