Index: include/llvm/IR/ModuleSummaryIndex.h =================================================================== --- include/llvm/IR/ModuleSummaryIndex.h +++ include/llvm/IR/ModuleSummaryIndex.h @@ -418,19 +418,24 @@ } /// Returns the first GlobalValueSummary for \p GV, asserting that there - /// is only one if \p PerModuleIndex. + /// is only one if \p PerModuleIndex. If \p AssertExists is true, then + /// the entry is expected to exist in the index, otherwise, return null + /// if not found. GlobalValueSummary *getGlobalValueSummary(const GlobalValue &GV, - bool PerModuleIndex = true) const { + bool PerModuleIndex = true, + bool AssertExists = true) const { assert(GV.hasName() && "Can't get GlobalValueSummary for GV with no name"); return getGlobalValueSummary(GlobalValue::getGUID(GV.getName()), - PerModuleIndex); + PerModuleIndex, AssertExists); } /// Returns the first GlobalValueSummary for \p ValueGUID, asserting that - /// there - /// is only one if \p PerModuleIndex. + /// there is only one if \p PerModuleIndex. If \p AssertExists is true, then + /// the entry is expected to exist in the index, otherwise, return null + /// if not found. GlobalValueSummary *getGlobalValueSummary(GlobalValue::GUID ValueGUID, - bool PerModuleIndex = true) const; + bool PerModuleIndex = true, + bool AssertExists = true) const; /// Table of modules, containing module hash and id. const StringMap> &modulePaths() const { Index: include/llvm/Support/TargetRegistry.h =================================================================== --- include/llvm/Support/TargetRegistry.h +++ include/llvm/Support/TargetRegistry.h @@ -280,6 +280,9 @@ /// hasMCAsmBackend - Check if this target supports .o generation. bool hasMCAsmBackend() const { return MCAsmBackendCtorFn != nullptr; } + /// hasMCAsmParser - Check if this target supports assembly parsing. + bool hasMCAsmParser() const { return MCAsmParserCtorFn != nullptr; } + /// @} /// @name Feature Constructors /// @{ Index: lib/Analysis/LLVMBuild.txt =================================================================== --- lib/Analysis/LLVMBuild.txt +++ lib/Analysis/LLVMBuild.txt @@ -19,4 +19,4 @@ type = Library name = Analysis parent = Libraries -required_libraries = Core Support ProfileData +required_libraries = Core Support ProfileData Object Index: lib/Analysis/ModuleSummaryAnalysis.cpp =================================================================== --- lib/Analysis/ModuleSummaryAnalysis.cpp +++ 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; @@ -238,12 +240,63 @@ for (const GlobalAlias &A : M.aliases()) computeAliasSummary(Index, A); + // Collect the set of llvm.compiler.used locak variables, which along with the + // llvm.used variables already in the LocalUsed set, cannot be renamed as + // they have opaque uses (e.g. in module level asm synthesized by the FE). + Used.clear(); + collectUsedGlobalVariables(M, Used, /*CompilerUsed*/ true); + for (auto *V : Used) { + if (V->hasLocalLinkage()) + LocalsUsed.insert(V); + } + for (auto *V : LocalsUsed) { auto *Summary = Index.getGlobalValueSummary(*V); assert(Summary && "Missing summary for global value"); 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))) { + GlobalValue *GV = M.getNamedValue(Name); + if (!GV) + return; + assert(GV->isDeclaration() && "Def in module asm already has definition"); + GlobalValueSummary::GVFlags Flags(GlobalValue::InternalLinkage, + /* NoRename */ true, + /*IsNotViableToInline */ true); + // Create the appropriate summary type. + if (dyn_cast(GV)) { + std::unique_ptr Summary = + llvm::make_unique(Flags, 0); + Summary->setNoRename(); + Index.addGlobalValueSummary(Name, std::move(Summary)); + } else { + std::unique_ptr Summary = + llvm::make_unique(Flags); + Summary->setNoRename(); + Index.addGlobalValueSummary(Name, std::move(Summary)); + } + } + }); + } + return Index; } Index: lib/Bitcode/Writer/BitcodeWriter.cpp =================================================================== --- lib/Bitcode/Writer/BitcodeWriter.cpp +++ lib/Bitcode/Writer/BitcodeWriter.cpp @@ -3327,11 +3327,15 @@ void ModuleBitcodeWriter::writeModuleLevelReferences( const GlobalVariable &V, SmallVector &NameVals, unsigned FSModRefsAbbrev) { - // Only interested in recording variable defs in the summary. - if (V.isDeclaration()) + auto *Summary = Index->getGlobalValueSummary(V, /* PerModuleIndex */ true, + /* AssertExists */ false); + if (!Summary) { + // Only declarations should not have a summary (a declaration might however + // have a summary if the def was in module level asm). + assert(V.isDeclaration()); return; + } NameVals.push_back(VE.getValueID(&V)); - auto *Summary = Index->getGlobalValueSummary(V); GlobalVarSummary *VS = cast(Summary); NameVals.push_back(getEncodedGVSummaryFlags(VS->flags())); @@ -3409,14 +3413,19 @@ // Iterate over the list of functions instead of the Index to // ensure the ordering is stable. for (const Function &F : M) { - if (F.isDeclaration()) - continue; // Summary emission does not support anonymous functions, they have to // renamed using the anonymous function renaming pass. if (!F.hasName()) report_fatal_error("Unexpected anonymous function when writing summary"); - auto *Summary = Index->getGlobalValueSummary(F); + auto *Summary = Index->getGlobalValueSummary(F, /* PerModuleIndex */ true, + /* AssertExists */ false); + if (!Summary) { + // Only declarations should not have a summary (a declaration might + // however have a summary if the def was in module level asm). + assert(F.isDeclaration()); + continue; + } writePerModuleFunctionSummaryRecord(NameVals, Summary, VE.getValueID(&F), FSCallsAbbrev, FSCallsProfileAbbrev, F); } Index: lib/IR/ModuleSummaryIndex.cpp =================================================================== --- lib/IR/ModuleSummaryIndex.cpp +++ lib/IR/ModuleSummaryIndex.cpp @@ -94,10 +94,11 @@ } } -GlobalValueSummary * -ModuleSummaryIndex::getGlobalValueSummary(uint64_t ValueGUID, - bool PerModuleIndex) const { +GlobalValueSummary *ModuleSummaryIndex::getGlobalValueSummary( + uint64_t ValueGUID, bool PerModuleIndex, bool AssertExists) const { auto SummaryList = findGlobalValueSummaryList(ValueGUID); + if (!AssertExists && SummaryList == end()) + return nullptr; assert(SummaryList != end() && "GlobalValue not found in index"); assert((!PerModuleIndex || SummaryList->second.size() == 1) && "Expected a single entry per global value in per-module index"); Index: lib/Object/IRObjectFile.cpp =================================================================== --- lib/Object/IRObjectFile.cpp +++ lib/Object/IRObjectFile.cpp @@ -54,8 +54,7 @@ std::string Err; const Target *T = TargetRegistry::lookupTarget(TT.str(), Err); - if (!T) - return; + assert(T && T->hasMCAsmParser()); std::unique_ptr MRI(T->createMCRegInfo(TT.str())); if (!MRI) Index: test/LTO/X86/current-section.ll =================================================================== --- test/LTO/X86/current-section.ll +++ test/LTO/X86/current-section.ll @@ -2,4 +2,7 @@ ; RUN: llvm-lto -o %t2 %t1 ; REQUIRES: default_triple +target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" +target triple = "x86_64-unknown-linux-gnu" + module asm ".align 4" Index: test/ThinLTO/X86/Inputs/module_asm2.ll =================================================================== --- /dev/null +++ test/ThinLTO/X86/Inputs/module_asm2.ll @@ -0,0 +1,12 @@ +target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" +target triple = "x86_64-unknown-linux-gnu" + +define i32 @main({ i64, { i64, i8* }* } %unnamed) #0 { + %1 = call i32 @func1() #1 + %2 = call i32 @func2() #1 + %3 = call i32 @func3() #1 + ret i32 %1 +} +declare i32 @func1() #1 +declare i32 @func2() #1 +declare i32 @func3() #1 Index: test/ThinLTO/X86/module_asm2.ll =================================================================== --- /dev/null +++ test/ThinLTO/X86/module_asm2.ll @@ -0,0 +1,73 @@ +; RUN: opt -module-summary %s -o %t1.bc +; RUN: opt -module-summary %p/Inputs/module_asm2.ll -o %t2.bc + +; RUN: llvm-lto -thinlto-action=run -exported-symbol=main -exported-symbol=func1 -exported-symbol=func2 -exported-symbol=func3 %t1.bc %t2.bc +; RUN: llvm-nm %t1.bc.thinlto.o | FileCheck %s --check-prefix=NM0 +; RUN: llvm-nm %t2.bc.thinlto.o | FileCheck %s --check-prefix=NM1 + +; RUN: llvm-lto2 %t1.bc %t2.bc -o %t.o -save-temps \ +; RUN: -r=%t1.bc,foo,plx \ +; RUN: -r=%t1.bc,b,pl \ +; RUN: -r=%t1.bc,x,pl \ +; RUN: -r=%t1.bc,func1,pl \ +; RUN: -r=%t1.bc,func2,pl \ +; RUN: -r=%t1.bc,func3,pl \ +; RUN: -r=%t2.bc,main,plx \ +; RUN: -r=%t2.bc,func1,l \ +; RUN: -r=%t2.bc,func2,l \ +; RUN: -r=%t2.bc,func3,l +; RUN: llvm-nm %t.o.0 | FileCheck %s --check-prefix=NM0 +; RUN: llvm-nm %t.o.1 | FileCheck %s --check-prefix=NM1 + +; NM0-DAG: d b +; NM0-DAG: d x +; NM0-DAG: t foo +; NM0-DAG: T func1 +; NM0-DAG: T func2 +; NM0-DAG: T func3 + +; NM1-NOT: foo +; NM1-NOT: b +; NM1-NOT: x +; NM1-DAG: U func1 +; NM1-DAG: U func2 +; NM1-DAG: U func3 +; NM1-DAG: T main +; NM1-NOT: foo +; NM1-NOT: b +; NM1-NOT: x + +target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" +target triple = "x86_64-unknown-linux-gnu" + +@b = internal global i32 1, align 4 +@x = internal global i32 1, align 4 + +@llvm.compiler.used = appending global [1 x i8*] [i8* bitcast (i32* @b to i8*)], section "llvm.metadata" +@llvm.used = appending global [1 x i8*] [i8* bitcast (i32* @x to i8*)], section "llvm.metadata" + +module asm "\09.text" +module asm "\09.type\09foo,@function" +module asm "foo:" +module asm "\09movl b, %eax" +module asm "\09movl x, %edx" +module asm "\09ret " +module asm "\09.size\09foo, .-foo" +module asm "" + +declare i16 @foo() #0 + +define i32 @func1() #1 { + call i16 @foo() + ret i32 1 +} + +define i32 @func2() #1 { + %1 = load i32, i32* @b, align 4 + ret i32 %1 +} + +define i32 @func3() #1 { + %1 = load i32, i32* @x, align 4 + ret i32 %1 +} Index: tools/opt/opt.cpp =================================================================== --- tools/opt/opt.cpp +++ tools/opt/opt.cpp @@ -364,6 +364,7 @@ InitializeAllTargets(); InitializeAllTargetMCs(); InitializeAllAsmPrinters(); + InitializeAllAsmParsers(); // Initialize passes PassRegistry &Registry = *PassRegistry::getPassRegistry();