[llvm] r317016 - [IndVarSimplify] Extract wrapper around SE-.isLoopInvariantPredicate [NFC]

David Jones via llvm-commits llvm-commits at lists.llvm.org
Wed Nov 1 17:05:52 PDT 2017


Philip,

FYI, I was able to crash a stage 1 build with this:


```
static void loops(int lim)
{
  for (int i = 0; i < 1;)
    for (int j = 0; j < lim; ++j)
      ++i;
}

typedef void (*handler)(int);
handler x = loops;
```

Here is the stack trace (edited for clarity):

#0  SignalHandler(int)
#1  __restore_rt
#2  llvm::simplifyUsersOfIV(llvm::PHINode*, llvm::ScalarEvolution*,
llvm::DominatorTree*, llvm::LoopInfo*,
llvm::SmallVectorImpl<llvm::WeakTrackingVH>&, llvm::SCEVExpander&,
llvm::IVVisitor*)
#3  (anonymous namespace)::IndVarSimplify::run(llvm::Loop*)
#4  (anonymous namespace)::IndVarSimplifyLegacyPass::runOnLoop(llvm::Loop*,
llvm::LPPassManager&)
#5  llvm::LPPassManager::runOnFunction(llvm::Function&)
#6  llvm::FPPassManager::runOnFunction(llvm::Function&)
#7  (anonymous namespace)::CGPassManager::runOnModule(llvm::Module&)
#8  llvm::legacy::PassManagerImpl::run(llvm::Module&)
#9  clang::EmitBackendOutput(clang::DiagnosticsEngine&,
clang::HeaderSearchOptions const&, clang::CodeGenOptions const&,
clang::TargetOptions const&, clang::LangOptions const&, llvm::DataLayout
const&, llvm::Module*, clang::BackendAction,
std::unique_ptr<llvm::raw_pwrite_stream,
std::default_delete<llvm::raw_pwrite_stream> >)
#10  clang::BackendConsumer::HandleTranslationUnit(clang::ASTContext&)
#11  clang::ParseAST(clang::Sema&, bool, bool)
#12  clang::FrontendAction::Execute()
#13  clang::CompilerInstance::ExecuteAction(clang::FrontendAction&)
#14  clang::ExecuteCompilerInvocation(clang::CompilerInstance*)
#15  cc1_main(llvm::ArrayRef<char const*>, char const*, void*)
#16  main
#17  __libc_start_main
#18  _start
Stack dump:
0. Program arguments: bin/clang -cc1 -triple x86_64-grtev4-linux-gnu
-emit-obj -O2 -x c c02475.c
1. <eof> parser at end of file
2. Per-module optimization passes
3. Running pass 'CallGraph Pass Manager' on module 'c02475.c'.
4. Running pass 'Loop Pass Manager' on function '@loops'
5. Running pass 'Induction Variable Simplification' on basic block '%2'



On Wed, Nov 1, 2017 at 12:35 PM Philip Reames via llvm-commits <
llvm-commits at lists.llvm.org> wrote:

> FYI - I've gotten a report that this change has introduced a miscompile
> in a stage2 clang build.  I'm about to revert this and the following
> patch due to lack of time to investigate fully.
>
>
> On 10/31/2017 11:04 AM, Philip Reames via llvm-commits wrote:
> > Author: reames
> > Date: Tue Oct 31 11:04:57 2017
> > New Revision: 317016
> >
> > URL: http://llvm.org/viewvc/llvm-project?rev=317016&view=rev
> > Log:
> > [IndVarSimplify] Extract wrapper around SE-.isLoopInvariantPredicate
> [NFC]
> >
> > This an intermediate state, the next patch will re-inline the
> markLoopInvariantPredicate function to reduce code duplication.
> >
> >
> > Modified:
> >      llvm/trunk/lib/Transforms/Utils/SimplifyIndVar.cpp
> >
> > Modified: llvm/trunk/lib/Transforms/Utils/SimplifyIndVar.cpp
> > URL:
> http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Utils/SimplifyIndVar.cpp?rev=317016&r1=317015&r2=317016&view=diff
> >
> ==============================================================================
> > --- llvm/trunk/lib/Transforms/Utils/SimplifyIndVar.cpp (original)
> > +++ llvm/trunk/lib/Transforms/Utils/SimplifyIndVar.cpp Tue Oct 31
> 11:04:57 2017
> > @@ -83,6 +83,11 @@ namespace {
> >
> >       bool eliminateOverflowIntrinsic(CallInst *CI);
> >       bool eliminateIVUser(Instruction *UseInst, Instruction *IVOperand);
> > +    bool isCheapLoopInvariantPredicate(ICmpInst::Predicate Pred,
> > +           const SCEV *LHS, const SCEV *RHS, const Loop *L,
> > +           const SmallDenseMap<const SCEV*, Value*> &FreeExpansions,
> > +           ICmpInst::Predicate &InvariantPred,
> > +           Value *&LHSV, Value *& RHSV);
> >       bool makeIVComparisonInvariant(ICmpInst *ICmp, Value *IVOperand);
> >       void eliminateIVComparison(ICmpInst *ICmp, Value *IVOperand);
> >       void simplifyIVRemainder(BinaryOperator *Rem, Value *IVOperand,
> > @@ -162,6 +167,26 @@ Value *SimplifyIndvar::foldIVUser(Instru
> >     return IVSrc;
> >   }
> >
> > +bool SimplifyIndvar::isCheapLoopInvariantPredicate(ICmpInst::Predicate
> Pred,
> > +           const SCEV *LHS, const SCEV *RHS, const Loop *L,
> > +           const SmallDenseMap<const SCEV*, Value*> &FreeExpansions,
> > +           ICmpInst::Predicate &InvariantPred,
> > +           Value *&LHSV, Value *& RHSV) {
> > +
> > +  const SCEV *InvariantLHS, *InvariantRHS;
> > +  if (!SE->isLoopInvariantPredicate(Pred, LHS, RHS, L, InvariantPred,
> > +                                    InvariantLHS, InvariantRHS))
> > +    return false;
> > +
> > +  // Rewrite the comparison to a loop invariant comparison if it can be
> done
> > +  // cheaply, where cheaply means "we don't need to emit any new
> > +  // instructions".
> > +  LHSV = FreeExpansions.lookup(InvariantLHS);
> > +  RHSV = FreeExpansions.lookup(InvariantRHS);
> > +
> > +  return (LHSV && RHSV);
> > +}
> > +
> >   bool SimplifyIndvar::makeIVComparisonInvariant(ICmpInst *ICmp,
> >                                                  Value *IVOperand) {
> >     unsigned IVOperIdx = 0;
> > @@ -179,19 +204,9 @@ bool SimplifyIndvar::makeIVComparisonInv
> >     const SCEV *S = SE->getSCEVAtScope(ICmp->getOperand(IVOperIdx),
> ICmpLoop);
> >     const SCEV *X = SE->getSCEVAtScope(ICmp->getOperand(1 - IVOperIdx),
> ICmpLoop);
> >
> > -  ICmpInst::Predicate InvariantPredicate;
> > -  const SCEV *InvariantLHS, *InvariantRHS;
> > -
> >     auto *PN = dyn_cast<PHINode>(IVOperand);
> >     if (!PN)
> >       return false;
> > -  if (!SE->isLoopInvariantPredicate(Pred, S, X, L, InvariantPredicate,
> > -                                    InvariantLHS, InvariantRHS))
> > -    return false;
> > -
> > -  // Rewrite the comparison to a loop invariant comparison if it can be
> done
> > -  // cheaply, where cheaply means "we don't need to emit any new
> > -  // instructions".
> >
> >     SmallDenseMap<const SCEV*, Value*> CheapExpansions;
> >     CheapExpansions[S] = ICmp->getOperand(IVOperIdx);
> > @@ -204,17 +219,18 @@ bool SimplifyIndvar::makeIVComparisonInv
> >       const SCEV *IncomingS = SE->getSCEV(Incoming);
> >       CheapExpansions[IncomingS] = Incoming;
> >     }
> > -  Value *NewLHS = CheapExpansions[InvariantLHS];
> > -  Value *NewRHS = CheapExpansions[InvariantRHS];
> >
> > -  if (!NewLHS || !NewRHS)
> > -    // We could not find an existing value to replace either LHS or RHS.
> > -    // Generating new instructions has subtler tradeoffs, so avoid
> doing that
> > -    // for now.
> > +  ICmpInst::Predicate NewPred;
> > +  Value *NewLHS = nullptr, *NewRHS = nullptr;
> > +
> > +  if (!isCheapLoopInvariantPredicate(Pred, S, X, L, CheapExpansions,
> > +                                     NewPred, NewLHS, NewRHS))
> >       return false;
> > +
> > +  assert(NewLHS && NewRHS);
> >
> >     DEBUG(dbgs() << "INDVARS: Simplified comparison: " << *ICmp << '\n');
> > -  ICmp->setPredicate(InvariantPredicate);
> > +  ICmp->setPredicate(NewPred);
> >     ICmp->setOperand(0, NewLHS);
> >     ICmp->setOperand(1, NewRHS);
> >     return true;
> >
> >
> > _______________________________________________
> > llvm-commits mailing list
> > llvm-commits at lists.llvm.org
> > http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits
>
> _______________________________________________
> llvm-commits mailing list
> llvm-commits at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20171102/40bf49a6/attachment.html>


More information about the llvm-commits mailing list