26 "use-source-filename-for-promoted-locals",
cl::Hidden,
27 cl::desc(
"Uses the source file name instead of the Module hash. "
28 "This requires that the source filename has a unique name / "
29 "path to avoid name collisions."));
34 "thinlto-move-symbols",
36 "Move the symbols with the given name. This will delete these symbols "
37 "wherever they are originally defined, and make sure their "
38 "linkage is External where they are imported. It is meant to be "
39 "used with the name of contextual profiling roots."),
47 : M(M), ImportIndex(Index), GlobalsToImport(GlobalsToImport),
48 ClearDSOLocalOnDeclarations(ClearDSOLocalOnDeclarations) {
54 HasExportedFunctions = ImportIndex.hasExportedFunctions(M);
69bool FunctionImportGlobalProcessing::doImportAsDefinition(
71 if (!isPerformingImport())
75 if (!GlobalsToImport->count(
const_cast<GlobalValue *
>(SGV)))
79 "Unexpected global alias in the import list.");
85bool FunctionImportGlobalProcessing::shouldPromoteLocalToGlobal(
97 if (!isPerformingImport() && !isModuleExporting())
100 if (isPerformingImport()) {
103 "Attempting to promote non-renamable local");
117 assert(Summary &&
"Missing summary for global value when exporting");
120 assert(!isNonRenamableLocal(*SGV) &&
121 "Attempting to promote non-renamable local");
129bool FunctionImportGlobalProcessing::isNonRenamableLocal(
136 if (Used.count(
const_cast<GlobalValue *
>(&GV)))
143FunctionImportGlobalProcessing::getPromotedName(
const GlobalValue *SGV) {
152 std::replace_if(std::begin(Suffix), std::end(Suffix),
153 [&](
char ch) {
return !
isAlnum(ch); },
'_');
164FunctionImportGlobalProcessing::getLinkage(
const GlobalValue *SGV,
170 if (isModuleExporting()) {
177 if (!isPerformingImport())
197 if (!doImportAsDefinition(SGV))
209 assert(!doImportAsDefinition(SGV));
247 assert(!doImportAsDefinition(SGV));
260void FunctionImportGlobalProcessing::processGlobalForThinLTO(
GlobalValue &GV) {
269 (isPerformingImport() && !doImportAsDefinition(&GV)));
280 if (!GV.
isDeclaration() && VI && ImportIndex.withAttributePropagation()) {
292 ImportIndex.findSummaryInModule(VI, M.getModuleIdentifier()));
294 (ImportIndex.isReadOnly(GVS) || ImportIndex.isWriteOnly(GVS))) {
295 V->addAttribute(
"thinlto-internalize");
303 if (ImportIndex.isWriteOnly(GVS))
309 GlobalValueSummary *
Summary =
nullptr;
311 Summary = ImportIndex.findSummaryInModule(
315 shouldPromoteLocalToGlobal(&GV, Summary)) &&
316 "noRenameOnPromotion requires promotion to external linkage");
318 if (GV.
hasLocalLinkage() && shouldPromoteLocalToGlobal(&GV, Summary)) {
322 !
Summary->noRenameOnPromotion())
323 GV.
setName(getPromotedName(&GV));
332 if (
C->getName() == Name)
333 RenamedComdats.try_emplace(
C, M.getOrInsertComdat(GV.
getName()));
340 if (ClearDSOLocalOnDeclarations &&
342 (isPerformingImport() && !doImportAsDefinition(&GV))) &&
345 }
else if (VI &&
VI.isDSOLocal(ImportIndex.withDSOLocalPropagation())) {
357 if (GO && GO->isDeclarationForLinker() && GO->hasComdat()) {
361 assert(GO->hasAvailableExternallyLinkage() &&
362 "Expected comdat on definition (possibly available external)");
363 GO->setComdat(
nullptr);
367void FunctionImportGlobalProcessing::processGlobalsForThinLTO() {
368 for (GlobalVariable &GV : M.globals())
369 processGlobalForThinLTO(GV);
370 for (Function &SF : M)
371 processGlobalForThinLTO(SF);
372 for (GlobalAlias &GA : M.aliases())
373 processGlobalForThinLTO(GA);
377 if (!RenamedComdats.empty())
378 for (
auto &GO : M.global_objects())
379 if (
auto *
C = GO.getComdat()) {
380 auto Replacement = RenamedComdats.find(
C);
381 if (Replacement != RenamedComdats.end())
382 GO.setComdat(Replacement->second);
389 bool ClearDSOLocalOnDeclarations,
393 ClearDSOLocalOnDeclarations);
394 ThinLTOProcessing.
run();
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static bool isNonRenamableLocal(const GlobalValue &GV)
static LLVM_ABI Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
Class to handle necessary GlobalValue changes required by ThinLTO function importing,...
LLVM_ABI FunctionImportGlobalProcessing(Module &M, const ModuleSummaryIndex &Index, SetVector< GlobalValue * > *GlobalsToImport, bool ClearDSOLocalOnDeclarations)
Function and variable summary information to aid decisions and implementation of importing.
bool isImplicitDSOLocal() const
static bool isLocalLinkage(LinkageTypes Linkage)
LLVM_ABI bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
LinkageTypes getLinkage() const
bool hasLocalLinkage() const
LLVM_ABI GUID getGUIDOrFallback() const
Return the GUID for this value if it has been assigned, otherwise fall back to computing it based on ...
void setDLLStorageClass(DLLStorageClassTypes C)
LLVM_ABI const Comdat * getComdat() const
bool hasDLLImportStorageClass() const
void setLinkage(LinkageTypes LT)
bool isDeclarationForLinker() const
Module * getParent()
Get the module that this global value is contained inside of...
void setDSOLocal(bool Local)
@ HiddenVisibility
The GV is hidden.
void setVisibility(VisibilityTypes V)
LinkageTypes
An enumeration for the kinds of linkage for global values.
@ PrivateLinkage
Like Internal, but omit from symbol table.
@ CommonLinkage
Tentative definitions.
@ InternalLinkage
Rename collisions when linking (static functions).
@ LinkOnceAnyLinkage
Keep one copy of function when linking (inline)
@ WeakODRLinkage
Same, but only replaced by something equivalent.
@ ExternalLinkage
Externally visible function.
@ WeakAnyLinkage
Keep one copy of named function when linking (weak)
@ AppendingLinkage
Special purpose, only applies to global arrays.
@ AvailableExternallyLinkage
Available for inspection, not emission.
@ ExternalWeakLinkage
ExternalWeak linkage description.
@ LinkOnceODRLinkage
Same, but only replaced by something equivalent.
Class to hold module path string table and global value map, and encapsulate methods for operating on...
static std::string getGlobalNameForLocal(StringRef Name, ModuleHash ModHash)
Convenience method for creating a promoted global name for the given value name of a local,...
A Module instance is used to store all the information related to an LLVM module.
const std::string & getSourceFileName() const
Get the module's original source file name.
const std::string & getModuleIdentifier() const
Get the module identifier which is, essentially, the name of the module.
A vector that has set insertion semantics.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
std::string str() const
Get the contents as an std::string.
The TimeTraceScope is a helper class to call the begin and end functions of the time trace profiler.
LLVM_ABI void setName(const Twine &Name)
Change the name of the value.
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ C
The default llvm calling convention, compatible with C.
This is an optimization pass for GlobalISel generic memory operations.
cl::list< GlobalValue::GUID > MoveSymbolGUID
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
constexpr from_range_t from_range
LLVM_ABI void renameModuleForThinLTO(Module &M, const ModuleSummaryIndex &Index, bool ClearDSOLocalOnDeclarations, SetVector< GlobalValue * > *GlobalsToImport=nullptr)
Perform in-place global value handling on the given Module for exported local functions renamed and p...
auto dyn_cast_or_null(const Y &Val)
cl::opt< bool > AlwaysRenamePromotedLocals("always-rename-promoted-locals", cl::init(true), cl::Hidden, cl::desc("Always rename promoted locals."))
static cl::opt< bool > UseSourceFilenameForPromotedLocals("use-source-filename-for-promoted-locals", cl::Hidden, cl::desc("Uses the source file name instead of the Module hash. " "This requires that the source filename has a unique name / " "path to avoid name collisions."))
Uses the "source_filename" instead of a Module hash ID for the suffix of promoted locals during LTO.
bool isAlnum(char C)
Checks whether character C is either a decimal digit or an uppercase or lowercase letter as classifie...
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
LLVM_ABI GlobalVariable * collectUsedGlobalVariables(const Module &M, SmallVectorImpl< GlobalValue * > &Vec, bool CompilerUsed)
Given "llvm.used" or "llvm.compiler.used" as a global name, collect the initializer elements of that ...