LibCallsShrinkWrap falsely claims to preserve CFG
| Bugzilla Link | 30929 |
| Resolution | FIXED |
| Resolved on | Nov 08, 2016 13:53 |
| Version | trunk |
| OS | Linux |
| CC | @vns-mn,@mikaelholmen |
Extended Description
We get multiple different fails in our randomized testing using the LibCallsShrinkWrap pass. The problem is that this pass claims to preserve the CFG, but it actually doesn't, which (at least) gives problems with the domtree.
The problem can be replicated with this tiny program:
define void @TEST__MAIN() { %_tmp31 = call float @acosf(float 2.000000e+00) ret void } declare float @acosf(float)
It fails in gvn using the following opt call:
opt -instcombine -libcalls-shrinkwrap -gvn ./shrink.ll
Failing assert:
opt: ../include/llvm/Support/GenericDomTree.h:591: void llvm::DominatorTreeBasellvm::BasicBlock::changeImmediateDominator(DomTreeNodeBase
One can also insert a -verify-dom-info after the -libcalls-shrinkwrap option, and that will report the problem as well: DominatorTree for function: TEST__MAIN is not up to date!
r286271
Oops, sorry, I sent a review in parallel without reading your message carefully. If you want to send yours, I'll abandon mine.
This is indeed a bug in the implementation - I don't mean to preserve CFG if the transformation occurs. I think a safer fix was to remove the AU.setPreservesCFG(); This also makes it in sync with the new passmanager behavior. I'll submit a patch for review.
-Rong
Indeed DomTree changes, which is a problem. In your case the domtree actual/expected is:
Computed: =============================-------------------------------- Inorder Dominator Tree: [1] %0 {0,1}
Actual: =============================-------------------------------- Inorder Dominator Tree: [1] %0 {0,5} [2] %cdce.call {1,2} [2] %cdce.end {3,4}
I think recomputing DomTree should be enough in this case. This (incomplete) patch is an attempt to fix. Rong, David, what do you think? If you're OK with the idea I'll update the patch and post for review. Otherwise, ideas welcome =)
diff --git a/lib/Transforms/Utils/LibCallsShrinkWrap.cpp b/lib/Transforms/Utils/LibCallsShrinkWrap.cpp
index c992498..ebdc76b 100644
--- a/lib/Transforms/Utils/LibCallsShrinkWrap.cpp
+++ b/lib/Transforms/Utils/LibCallsShrinkWrap.cpp
@@ -32,6 +32,7 @@
#include "llvm/Analysis/TargetLibraryInfo.h"
#include "llvm/IR/CFG.h"
#include "llvm/IR/Constants.h"
+#include "llvm/IR/Dominators.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/InstVisitor.h"
@@ -531,6 +532,7 @@ bool LibCallsShrinkWrap::perform(CallInst *CI) {
void LibCallsShrinkWrapLegacyPass::getAnalysisUsage(AnalysisUsage &AU) const {
AU.setPreservesCFG();
AU.addRequired
- AU.addRequired
(); }
bool runImpl(Function &F, const TargetLibraryInfo &TLI) { @@ -544,7 +546,12 @@ bool runImpl(Function &F, const TargetLibraryInfo &TLI) {
bool LibCallsShrinkWrapLegacyPass::runOnFunction(Function &F) {
auto &TLI = getAnalysis
- return runImpl(F, TLI);
- auto *DT = &getAnalysis
().getDomTree(); - if (runImpl(F, TLI)) {
- DT->recalculate(F);
- return true;
- }
- return false; }