diff options
author | Wenlei He <aktoon@gmail.com> | 2021-07-27 18:00:55 -0700 |
---|---|---|
committer | Wenlei He <aktoon@gmail.com> | 2021-07-28 18:02:48 -0700 |
commit | 1a8087adaf1e34b695d420f62ff26d3d8489264d (patch) | |
tree | 6f164ee741373f69ff3ef74bd5a0360456a19071 /llvm/lib/Analysis/ModuleSummaryAnalysis.cpp | |
parent | c6ad3f2157ce374dbd2fd3e50699fc0303553714 (diff) | |
download | llvm-1a8087adaf1e34b695d420f62ff26d3d8489264d.zip llvm-1a8087adaf1e34b695d420f62ff26d3d8489264d.tar.gz llvm-1a8087adaf1e34b695d420f62ff26d3d8489264d.tar.bz2 |
[ThinLTO] Disallow importing for functions with indir branch to block address
We don't allowing inlining for functions with blockaddress with uses other than strictly callbr. This is because if the blockaddress escapes the function via a global variable, inlining may lead to an invalid cross-function reference.
We check against such cases during inlining, however the check can fail for ThinLTO post-link because CFG simplification can incorrectly removes blocks based on wrong block reachability.
When we import a function with blockaddress taken in a global variable but without importing that variable, we won't go through value mapping to reflect the real address-taken-ness of the cloned blocks. For the imported clone, this leads to blocks reachable from indirect branch through global variable being incorrectly treated as unreachable and removed by SimplifyCFG.
Since inlining for such cases shouldn't be allowed in the first place, I'm marking them as ineligible for importing during pre-link to save the problem of missing address-taken-ness of imported clone as well as bad DCE and inlining.
Differential Revision: https://reviews.llvm.org/D106930
Diffstat (limited to 'llvm/lib/Analysis/ModuleSummaryAnalysis.cpp')
-rw-r--r-- | llvm/lib/Analysis/ModuleSummaryAnalysis.cpp | 21 |
1 files changed, 18 insertions, 3 deletions
diff --git a/llvm/lib/Analysis/ModuleSummaryAnalysis.cpp b/llvm/lib/Analysis/ModuleSummaryAnalysis.cpp index e435532..0214f89 100644 --- a/llvm/lib/Analysis/ModuleSummaryAnalysis.cpp +++ b/llvm/lib/Analysis/ModuleSummaryAnalysis.cpp @@ -264,7 +264,20 @@ static void computeFunctionSummary( std::vector<const Instruction *> NonVolatileStores; bool HasInlineAsmMaybeReferencingInternal = false; - for (const BasicBlock &BB : F) + bool HasIndirBranchToBlockAddress = false; + for (const BasicBlock &BB : F) { + // We don't allow inlining of function with indirect branch to blockaddress. + // If the blockaddress escapes the function, e.g., via a global variable, + // inlining may lead to an invalid cross-function reference. So we shouldn't + // import such function either. + if (BB.hasAddressTaken()) { + for (User *U : BlockAddress::get(const_cast<BasicBlock *>(&BB))->users()) + if (!isa<CallBrInst>(*U)) { + HasIndirBranchToBlockAddress = true; + break; + } + } + for (const Instruction &I : BB) { if (isa<DbgInfoIntrinsic>(I)) continue; @@ -386,6 +399,7 @@ static void computeFunctionSummary( .updateHotness(getHotness(Candidate.Count, PSI)); } } + } Index.addBlockCount(F.size()); std::vector<ValueInfo> Refs; @@ -452,8 +466,9 @@ static void computeFunctionSummary( : CalleeInfo::HotnessType::Critical); bool NonRenamableLocal = isNonRenamableLocal(F); - bool NotEligibleForImport = - NonRenamableLocal || HasInlineAsmMaybeReferencingInternal; + bool NotEligibleForImport = NonRenamableLocal || + HasInlineAsmMaybeReferencingInternal || + HasIndirBranchToBlockAddress; GlobalValueSummary::GVFlags Flags( F.getLinkage(), F.getVisibility(), NotEligibleForImport, /* Live = */ false, F.isDSOLocal(), |