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."));
37 cl::desc(
"Always rename promoted locals."));
40 "thinlto-move-symbols",
42 "Move the symbols with the given name. This will delete these symbols "
43 "wherever they are originally defined, and make sure their "
44 "linkage is External where they are imported. It is meant to be "
45 "used with the name of contextual profiling roots."),
53 : M(M), ImportIndex(Index), GlobalsToImport(GlobalsToImport),
54 ClearDSOLocalOnDeclarations(ClearDSOLocalOnDeclarations) {
60 HasExportedFunctions = ImportIndex.hasExportedFunctions(M);
75bool FunctionImportGlobalProcessing::doImportAsDefinition(
77 if (!isPerformingImport())
81 if (!GlobalsToImport->count(
const_cast<GlobalValue *
>(SGV)))
85 "Unexpected global alias in the import list.");
91bool FunctionImportGlobalProcessing::shouldPromoteLocalToGlobal(
103 if (!isPerformingImport() && !isModuleExporting())
106 if (isPerformingImport()) {
109 "Attempting to promote non-renamable local");
123 assert(Summary &&
"Missing summary for global value when exporting");
126 assert(!isNonRenamableLocal(*SGV) &&
127 "Attempting to promote non-renamable local");
135bool FunctionImportGlobalProcessing::isNonRenamableLocal(
142 if (Used.count(
const_cast<GlobalValue *
>(&GV)))
149FunctionImportGlobalProcessing::getPromotedName(
const GlobalValue *SGV) {
158 std::replace_if(std::begin(Suffix), std::end(Suffix),
159 [&](
char ch) {
return !
isAlnum(ch); },
'_');
170FunctionImportGlobalProcessing::getLinkage(
const GlobalValue *SGV,
176 if (isModuleExporting()) {
183 if (!isPerformingImport())
194 return SymbolsToMove.contains(SGV->
getGUID())
203 if (!doImportAsDefinition(SGV))
215 assert(!doImportAsDefinition(SGV));
253 assert(!doImportAsDefinition(SGV));
266void FunctionImportGlobalProcessing::processGlobalForThinLTO(
GlobalValue &GV) {
270 VI = ImportIndex.getValueInfo(GV.
getGUID());
275 (isPerformingImport() && !doImportAsDefinition(&GV)));
286 if (!GV.
isDeclaration() && VI && ImportIndex.withAttributePropagation()) {
298 ImportIndex.findSummaryInModule(VI, M.getModuleIdentifier()));
300 (ImportIndex.isReadOnly(GVS) || ImportIndex.isWriteOnly(GVS))) {
301 V->addAttribute(
"thinlto-internalize");
309 if (ImportIndex.isWriteOnly(GVS))
315 GlobalValueSummary *
Summary =
nullptr;
317 Summary = ImportIndex.findSummaryInModule(
320 if (GV.
hasLocalLinkage() && shouldPromoteLocalToGlobal(&GV, Summary)) {
324 !
Summary->noRenameOnPromotion())
325 GV.
setName(getPromotedName(&GV));
334 if (
C->getName() == Name)
335 RenamedComdats.try_emplace(
C, M.getOrInsertComdat(GV.
getName()));
342 if (ClearDSOLocalOnDeclarations &&
344 (isPerformingImport() && !doImportAsDefinition(&GV))) &&
347 }
else if (VI &&
VI.isDSOLocal(ImportIndex.withDSOLocalPropagation())) {
359 if (GO && GO->isDeclarationForLinker() && GO->hasComdat()) {
363 assert(GO->hasAvailableExternallyLinkage() &&
364 "Expected comdat on definition (possibly available external)");
365 GO->setComdat(
nullptr);
369void FunctionImportGlobalProcessing::processGlobalsForThinLTO() {
370 for (GlobalVariable &GV : M.globals())
371 processGlobalForThinLTO(GV);
372 for (Function &SF : M)
373 processGlobalForThinLTO(SF);
374 for (GlobalAlias &GA : M.aliases())
375 processGlobalForThinLTO(GA);
379 if (!RenamedComdats.empty())
380 for (
auto &GO : M.global_objects())
381 if (
auto *
C = GO.getComdat()) {
382 auto Replacement = RenamedComdats.find(
C);
383 if (Replacement != RenamedComdats.end())
384 GO.setComdat(Replacement->second);
391 bool ClearDSOLocalOnDeclarations,
395 ClearDSOLocalOnDeclarations);
396 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
void setDLLStorageClass(DLLStorageClassTypes C)
LLVM_ABI const Comdat * getComdat() const
bool hasDLLImportStorageClass() const
void setLinkage(LinkageTypes LT)
bool isDeclarationForLinker() const
GUID getGUID() const
Return a 64-bit global unique ID constructed from global value name (i.e.
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
str - 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.
initializer< Ty > init(const Ty &Val)
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)
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.
cl::opt< bool > AlwaysRenamePromotedLocals
FIXME: The current optimization that avoids unnecessary renaming of promoted locals is incompatible w...
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 ...