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

Reassociate removes call but preserves CG, asserts

Open uabjean opened this issue 10 years ago • 7 comments

Bugzilla Link 30784
Resolution FIXED
Resolved on Oct 27, 2016 21:47
Version trunk
OS Linux
Attachments Reduced ll file exposing the bug
CC @bogner

Extended Description

The reassociate pass may remove a call and then falsely claim to preserve the CG. This later leads to this assert at dead function removal:

opt: ../include/llvm/Analysis/CallGraph.h:184: llvm::CallGraphNode::~CallGraphNode(): Assertion `NumReferences == 0 && "Node deleted while references remain"' failed.

One simple fix is to set the variable MadeChange to true in RecursivelyEraseDeadInsts() if operating on a CallInst. (However, the preservation code acting on MadeChange has a comment indicating that even when set to true it should preserve the CG, but currently it does not.)

The attached ll-file can be used to provoke the bug using: opt -inline -functionattrs -reassociate reassoc-reduced.ll

The attached file originates from our randomised csmith test suite, but was reduced to a minimum before inclusion here. I was unable to reduce the command line to a single pass one.

uabjean avatar Oct 25 '16 16:10 uabjean

r285380

llvmbot avatar Oct 28 '16 04:10 llvmbot

I'm only uncertain if we want to set MadeChange only in some cases or unconditionally when we delete a dead instruction.

Justin, what do you think?

For the legacy pass manager, I think setting MadeChange whenever we delete a dead instruction is the best we can do since it only cares about whether or not anything has changed. With the new pass manager we could presumably make this more fine grained if we want and preserve more in cases where our changes don't affect much, but we can always leave that as something we can improve later.

bogner avatar Oct 28 '16 03:10 bogner

I don't think changing runOnFunction to unconditionally return true is quite right. If we don't modify the IR as part of the transformation, we can preserve all the analyses. My proposed fix is the following:

diff --git a/lib/Transforms/Scalar/Reassociate.cpp b/lib/Transforms/Scalar/Reassociate.cpp index 1ec7fce..ac0d7b8 100644 --- a/lib/Transforms/Scalar/Reassociate.cpp +++ b/lib/Transforms/Scalar/Reassociate.cpp @@ -2198,8 +2198,10 @@ PreservedAnalyses ReassociatePass::run(Function &F, FunctionAnalysisManager &) { // trivially dead instructions have been removed. while (!ToRedo.empty()) { Instruction *I = ToRedo.pop_back_val();

  •  if (isInstructionTriviallyDead(I))
    
  •  if (isInstructionTriviallyDead(I)) {
       RecursivelyEraseDeadInsts(I, ToRedo);
    
  •    MadeChange = true;
    
  •  }
    

    }

    // Now that we have removed dead instructions, we can reoptimize the

I'm only uncertain if we want to set MadeChange only in some cases or unconditionally when we delete a dead instruction.

Justin, what do you think?

llvmbot avatar Oct 28 '16 02:10 llvmbot

Oh, yes. I also misread CFG instead of CG, that's why I was a little bit confused (I expected reassociate to preserve the CFG). Anyway, here's a reduced testcase

define internal i16 @​func1() noinline #​0 { ret i16 0 } define void @​main(i16 %argc, i16** %argv) #​0 { %_tmp0 = call i16 @​func1() %_tmp2 = zext i16 %_tmp0 to i32 ret void } attributes #​0 = { minsize nounwind optsize }

llvmbot avatar Oct 27 '16 23:10 llvmbot

CFG != CG. The comment about preserving the CFG is referring to the control flow graph and isn't about the call graph at all. The CFG is preserved as long as you don't add or remove basic blocks or modify terminator instructions in the function (and Reassociate never does either of those things).

I suspect the correct fix for this will be something like the isa approach you tried.

bogner avatar Oct 27 '16 00:10 bogner

Davide, thanks for the explanation. I'm not that well versed in the new vs old PassManager, so I'm not sure I understand all the implications. The PA construction seems unnecessary as it is only used to return true/false from runOnFunction(), but I take it that it will become meaningful later, as you suggest?

Anyway, I tried your suggestion for a quick fix, but it didn't help. A hack that does work is adding:

if (isa(I)) MadeChange = true;

to RecursivelyEraseDeadInsts(), which later results in true being returned from runOnFunction(). But perhaps runOnFunction() should return true regardless of what instruction is removed, so the MadeChange setting could be unconditional?

uabjean avatar Oct 26 '16 11:10 uabjean

So, if you're talking about:

if (MadeChange) { // FIXME: This should also 'preserve the CFG'. auto PA = PreservedAnalyses(); PA.preserve(); return PA; }

the meaning of the comment is slightly different. The new PM has currently no way to express "this transform preserves the CFG", so when I and others ported passes we added this FIXME to make sure this detail didn't get lost (and audit and fix all the affected passes once preservation sets will be implemented in the new PM).

In other words, I think that this comment was copied in the new PM because the current pass manager infrastructure does it. I'm not a reassociate expert, but as a quick hack, you can try to remove the setPreserveCFG() call from getAnalysisUsage and see if that fixes your problem.

void getAnalysisUsage(AnalysisUsage &AU) const override {
  AU.setPreservesCFG();
  AU.addPreserved<GlobalsAAWrapperPass>();
}

In case it does, then we might just evaluate what's the performance impact of recomputing the affected analyses on some testcases, and if it's negligible, then my take is that we shouldn't really bother.

llvmbot avatar Oct 25 '16 22:10 llvmbot