llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

Assertion failure in ScalarEvolution::getBackedgeTakenInfo

Open mveriksson opened this issue 10 years ago • 5 comments

Bugzilla Link 26207
Resolution FIXED
Resolved on Mar 01, 2019 16:09
Version trunk
OS Linux
Attachments reduced input file
CC @atrick,@fhahn,@hfinkel,@sanjoy

Extended Description

I am getting a crash in "indvars":

opt -S -indvars bugpoint-reduced-simplified.ll Attempt to use a SCEVCouldNotCompute object! UNREACHABLE executed at ../lib/Analysis/ScalarEvolution.cpp:9287! 0 opt 0x000000000190453e llvm::sys::PrintStackTrace(llvm::raw_ostream&) + 46 1 opt 0x0000000001904949 2 opt 0x0000000001903253 llvm::sys::RunSignalHandlers() + 131 3 opt 0x0000000001904d8f 4 libpthread.so.0 0x00007effc7f64340 5 libc.so.6 0x00007effc718ccc9 gsignal + 57 6 libc.so.6 0x00007effc71900d8 abort + 328 7 opt 0x0000000001895e16 8 opt 0x0000000000c3678d llvm::ScalarEvolution::computeLoopDisposition(llvm::SCEV const*, llvm::Loop const*) + 1085 9 opt 0x0000000000c3622f llvm::ScalarEvolution::getLoopDisposition(llvm::SCEV const*, llvm::Loop const*) + 239 10 opt 0x0000000000c178b5 llvm::ScalarEvolution::isLoopInvariant(llvm::SCEV const*, llvm::Loop const*) + 37 11 opt 0x0000000000c24dd9 llvm::ScalarEvolution::getBackedgeTakenInfo(llvm::Loop const*) + 377 12 opt 0x0000000000c2523f llvm::ScalarEvolution::getBackedgeTakenCount(llvm::Loop const*) + 47 13 opt 0x00000000016c6534 14 opt 0x0000000000ba7122 llvm::LPPassManager::runOnFunction(llvm::Function&) + 1138 15 opt 0x000000000134d260 llvm::FPPassManager::runOnFunction(llvm::Function&) + 416 16 opt 0x000000000134d595 llvm::FPPassManager::runOnModule(llvm::Module&) + 117 17 opt 0x000000000134dd81 18 opt 0x000000000134d87b llvm::legacy::PassManagerImpl::run(llvm::Module&) + 347 19 opt 0x000000000134e2c1 llvm::legacy::PassManager::run(llvm::Module&) + 33 20 opt 0x00000000007de450 main + 8576 21 libc.so.6 0x00007effc7177ec5 __libc_start_main + 245 22 opt 0x00000000007b8a48 Stack dump: 0. Program arguments: build/bin/opt -S -indvars bugpoint-reduced-simplified.ll

  1.  Running pass 'Function Pass Manager' on module 'bugpoint-reduced-simplified.ll'.
    
  2.  Running pass 'Loop Pass Manager' on function '@main'
    
  3.  Running pass 'Induction Variable Simplification' on basic block '%bb1.i'
    

Aborted

input file: ; ModuleID = 'bugpoint-reduced-simplified.bc' target triple = "x86_64-unknown-linux-gnu"

define void @​main() { br label %bb2

bb2: ; preds = %bb1.i, %bb2, %0 %_tmp44.i = icmp slt i16 undef, 2 br i1 %_tmp44.i, label %bb1.i, label %bb2

bb1.i: ; preds = %bb1.i, %bb2 %_tmp25.i = phi i16 [ undef, %bb2 ], [ %_tmp6.i, %bb1.i ] %_tmp6.i = add nsw i16 %_tmp25.i, 1 %_tmp10.i = icmp sge i16 %_tmp6.i, 2 %exitcond.i = icmp eq i16 %_tmp6.i, 2 %or.cond = and i1 %_tmp10.i, %exitcond.i br i1 %or.cond, label %bb2, label %bb1.i }

This check in ScalarEvolution.cpp looks suspicious:

if (Result.getExact(this) != getCouldNotCompute()) { assert(isLoopInvariant(Result.getExact(this), L) && isLoopInvariant(Result.getMax(this), L) && "Computed backedge-taken count isn't loop invariant for loop!"); ++NumTripCountsComputed; }

The assertion is guarded for the getExact() case, but not for the getMax() case. It is Result.getMax() that returns the SCEVCouldNotCompute here.

mveriksson avatar Jan 19 '16 14:01 mveriksson

Similar case surfaced for ORs https://reviews.llvm.org/D58853

fhahn avatar Mar 02 '19 00:03 fhahn

Thank you for fixing this!

I have confirmed that this also solves the original problem that I was debugging.

mveriksson avatar Jan 20 '16 10:01 mveriksson

Should be fixed in r258184

sanjoy avatar Jan 19 '16 22:01 sanjoy

Here's the reproducer, slightly altered to remove uses of undef:

target triple = "x86_64-unknown-linux-gnu"

define void @​main(i16 %in) { br label %bb2

bb2: ; preds = %bb1.i, %bb2, %0 %_tmp44.i = icmp slt i16 %in, 2 br i1 %_tmp44.i, label %bb1.i, label %bb2

bb1.i: ; preds = %bb1.i, %bb2 %_tmp25.i = phi i16 [ %in, %bb2 ], [ %_tmp6.i, %bb1.i ] %_tmp6.i = add nsw i16 %_tmp25.i, 1 %_tmp10.i = icmp sge i16 %_tmp6.i, 2 %exitcond.i = icmp eq i16 %_tmp6.i, 2 %or.cond = and i1 %_tmp10.i, %exitcond.i br i1 %or.cond, label %bb2, label %bb1.i }

The problem is with how the trip counts due to %_tmp10.i and %exitcond.i are combined. In both the cases, the exact trip count is (1 - %in), but when computing the worst case scenario for %_tmp10.i, SCEV assumes the worst case to be the IV going from INT_MIN to 1, while for %exitcond.i SCEV assumes the worst case to be the IV going from 3 to 1 (via wrapping). Then in computeExitLimitFromCond SCEV merges the two ExitLimit instances as:

    if (EL0.Max == EL1.Max)
      MaxBECount = EL0.Max;
    if (EL0.Exact == EL1.Exact)
      BECount = EL0.Exact;

creating a CouldNotCompute MaxBECount, but a computable exact BECount.

The specific issue in this example is that ScalarEvolution::getRange doesn't analyze control flow. If getRange could analyze the entry predicated as %_tmp44.i, it would know that the viable range for %in is [INT_MIN, 2), not the full i16 set, and EL0.Max would be equal to EL1.Max.

However, I think the best general fix is to make computeExitLimitFromCond more robust around cases like these -- if we end up with a non-computable max count but a computable exact count, then we should set the max count to the exact count.

sanjoy avatar Jan 19 '16 19:01 sanjoy

assigned to @sanjoy

mveriksson avatar Jan 19 '16 14:01 mveriksson