summaryrefslogtreecommitdiffstats
path: root/llvm/lib/Analysis/ModuleSummaryAnalysis.cpp
diff options
context:
space:
mode:
authorTeresa Johnson <tejohnson@google.com>2016-11-08 21:53:35 +0000
committerTeresa Johnson <tejohnson@google.com>2016-11-08 21:53:35 +0000
commit6955feebf31bb75e7055c7c0c1e6a2c698dfbe77 (patch)
tree3af9b70b7995e0d7f8418df284c6a368c893768a /llvm/lib/Analysis/ModuleSummaryAnalysis.cpp
parenta49dcbb743d822d05e5e091bf97378d38f36140d (diff)
downloadbcm5719-llvm-6955feebf31bb75e7055c7c0c1e6a2c698dfbe77.tar.gz
bcm5719-llvm-6955feebf31bb75e7055c7c0c1e6a2c698dfbe77.zip
[ThinLTO] Prevent exporting of locals used/defined in module level asm
Summary: This patch uses the same approach added for inline asm in r285513 to similarly prevent promotion/renaming of locals used or defined in module level asm. All static global values defined in normal IR and used in module level asm should be included on either the llvm.used or llvm.compiler.used global. The former were already being flagged as NoRename in the summary, and I've simply added llvm.compiler.used values to this handling. Module level asm may also contain defs of values. We need to prevent export of any refs to local values defined in module level asm (e.g. a ref in normal IR), since that also requires renaming/promotion of the local. To do that, the summary index builder looks at all values in the module level asm string that are not marked Weak or Global, which is exactly the set of locals that are defined. A summary is created for each of these local defs and flagged as NoRename. This required adding handling to the BitcodeWriter to look at GV declarations to see if they have a summary (rather than skipping them all). Finally, added an assert to IRObjectFile::CollectAsmUndefinedRefs to ensure that an MCAsmParser is available, otherwise the module asm parse would silently fail. Initialized the asm parser in the opt tool for use in testing this fix. Fixes PR30610. Reviewers: mehdi_amini Subscribers: johanengelen, krasin, llvm-commits Differential Revision: https://reviews.llvm.org/D26146 llvm-svn: 286297
Diffstat (limited to 'llvm/lib/Analysis/ModuleSummaryAnalysis.cpp')
-rw-r--r--llvm/lib/Analysis/ModuleSummaryAnalysis.cpp61
1 files changed, 57 insertions, 4 deletions
diff --git a/llvm/lib/Analysis/ModuleSummaryAnalysis.cpp b/llvm/lib/Analysis/ModuleSummaryAnalysis.cpp
index 09d18eb3da1..5cf4a895c91 100644
--- a/llvm/lib/Analysis/ModuleSummaryAnalysis.cpp
+++ b/llvm/lib/Analysis/ModuleSummaryAnalysis.cpp
@@ -13,6 +13,7 @@
//===----------------------------------------------------------------------===//
#include "llvm/Analysis/ModuleSummaryAnalysis.h"
+#include "llvm/ADT/Triple.h"
#include "llvm/Analysis/BlockFrequencyInfo.h"
#include "llvm/Analysis/BlockFrequencyInfoImpl.h"
#include "llvm/Analysis/BranchProbabilityInfo.h"
@@ -24,6 +25,7 @@
#include "llvm/IR/InstIterator.h"
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/ValueSymbolTable.h"
+#include "llvm/Object/IRObjectFile.h"
#include "llvm/Pass.h"
using namespace llvm;
@@ -194,12 +196,22 @@ ModuleSummaryIndex llvm::buildModuleSummaryIndex(
ProfileSummaryInfo *PSI) {
ModuleSummaryIndex Index;
- // Identify the local values in the llvm.used set, which should not be
- // exported as they would then require renaming and promotion, but we
- // may have opaque uses e.g. in inline asm.
+ // Identify the local values in the llvm.used and llvm.compiler.used sets,
+ // which should not be exported as they would then require renaming and
+ // promotion, but we may have opaque uses e.g. in inline asm. We collect them
+ // here because we use this information to mark functions containing inline
+ // assembly calls as not importable.
+ SmallPtrSet<GlobalValue *, 8> LocalsUsed;
SmallPtrSet<GlobalValue *, 8> Used;
+ // First collect those in the llvm.used set.
collectUsedGlobalVariables(M, Used, /*CompilerUsed*/ false);
- SmallPtrSet<GlobalValue *, 8> LocalsUsed;
+ for (auto *V : Used) {
+ if (V->hasLocalLinkage())
+ LocalsUsed.insert(V);
+ }
+ Used.clear();
+ // Next collect those in the llvm.compiler.used set.
+ collectUsedGlobalVariables(M, Used, /*CompilerUsed*/ true);
for (auto *V : Used) {
if (V->hasLocalLinkage())
LocalsUsed.insert(V);
@@ -244,6 +256,47 @@ ModuleSummaryIndex llvm::buildModuleSummaryIndex(
Summary->setNoRename();
}
+ if (!M.getModuleInlineAsm().empty()) {
+ // Collect the local values defined by module level asm, and set up
+ // summaries for these symbols so that they can be marked as NoRename,
+ // to prevent export of any use of them in regular IR that would require
+ // renaming within the module level asm. Note we don't need to create a
+ // summary for weak or global defs, as they don't need to be flagged as
+ // NoRename, and defs in module level asm can't be imported anyway.
+ // Also, any values used but not defined within module level asm should
+ // be listed on the llvm.used or llvm.compiler.used global and marked as
+ // referenced from there.
+ // FIXME: Rename CollectAsmUndefinedRefs to something more general, as we
+ // are also using it to find the file-scope locals defined in module asm.
+ object::IRObjectFile::CollectAsmUndefinedRefs(
+ Triple(M.getTargetTriple()), M.getModuleInlineAsm(),
+ [&M, &Index](StringRef Name, object::BasicSymbolRef::Flags Flags) {
+ // Symbols not marked as Weak or Global are local definitions.
+ if (Flags & (object::BasicSymbolRef::SF_Weak ||
+ object::BasicSymbolRef::SF_Global))
+ return;
+ GlobalValue *GV = M.getNamedValue(Name);
+ if (!GV)
+ return;
+ assert(GV->isDeclaration() && "Def in module asm already has definition");
+ GlobalValueSummary::GVFlags GVFlags(GlobalValue::InternalLinkage,
+ /* NoRename */ true,
+ /*IsNotViableToInline */ true);
+ // Create the appropriate summary type.
+ if (isa<Function>(GV)) {
+ std::unique_ptr<FunctionSummary> Summary =
+ llvm::make_unique<FunctionSummary>(GVFlags, 0);
+ Summary->setNoRename();
+ Index.addGlobalValueSummary(Name, std::move(Summary));
+ } else {
+ std::unique_ptr<GlobalVarSummary> Summary =
+ llvm::make_unique<GlobalVarSummary>(GVFlags);
+ Summary->setNoRename();
+ Index.addGlobalValueSummary(Name, std::move(Summary));
+ }
+ });
+ }
+
return Index;
}
OpenPOWER on IntegriCloud