Skip to content

Commit d3d856a

Browse files
authored
Clean up external users of GlobalValue::getGUID(StringRef) (#129644)
See https://discourse.llvm.org/t/rfc-keep-globalvalue-guids-stable/84801 for context. This is a non-functional change which just changes the interface of GlobalValue, in preparation for future functional changes. This part touches a fair few users, so is split out for ease of review. Future changes to the GlobalValue implementation can then be focused purely on that class. This does the following: * Rename GlobalValue::getGUID(StringRef) to getGUIDAssumingExternalLinkage. This is simply making explicit at the callsite what is currently implicit. * Where possible, migrate users to directly calling getGUID on a GlobalValue instance. * Otherwise, where possible, have them call the newly renamed getGUIDAssumingExternalLinkage, to make the assumption explicit. There are a few cases where neither of the above are possible, as the caller saves and reconstructs the necessary information to compute the GUID themselves. We want to migrate these callers eventually, but for this first step we leave them be.
1 parent ed3c870 commit d3d856a

31 files changed

+130
-94
lines changed

bolt/lib/Rewrite/PseudoProbeRewriter.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ void PseudoProbeRewriter::parsePseudoProbe(bool ProfiledOnly) {
147147
if (!Name)
148148
continue;
149149
SymName = *Name;
150-
uint64_t GUID = Function::getGUID(SymName);
150+
uint64_t GUID = Function::getGUIDAssumingExternalLinkage(SymName);
151151
FuncStartAddrs[GUID] = F->getAddress();
152152
if (ProfiledOnly && HasProfile)
153153
GuidFilter.insert(GUID);
@@ -173,7 +173,7 @@ void PseudoProbeRewriter::parsePseudoProbe(bool ProfiledOnly) {
173173
const GUIDProbeFunctionMap &GUID2Func = ProbeDecoder.getGUID2FuncDescMap();
174174
// Checks GUID in GUID2Func and returns it if it's present or null otherwise.
175175
auto checkGUID = [&](StringRef SymName) -> uint64_t {
176-
uint64_t GUID = Function::getGUID(SymName);
176+
uint64_t GUID = Function::getGUIDAssumingExternalLinkage(SymName);
177177
if (GUID2Func.find(GUID) == GUID2Func.end())
178178
return 0;
179179
return GUID;
@@ -435,7 +435,7 @@ void PseudoProbeRewriter::encodePseudoProbes() {
435435
for (const BinaryFunction *F : BC.getAllBinaryFunctions()) {
436436
const uint64_t Addr =
437437
F->isEmitted() ? F->getOutputAddress() : F->getAddress();
438-
FuncStartAddrs[Function::getGUID(
438+
FuncStartAddrs[Function::getGUIDAssumingExternalLinkage(
439439
NameResolver::restore(F->getOneName()))] = Addr;
440440
}
441441
DummyDecoder.buildAddress2ProbeMap(

llvm/examples/OrcV2Examples/LLJITWithThinLTOSummaries/LLJITWithThinLTOSummaries.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,9 @@ class DuplicateDefinitionInSummary
7777

7878
void log(raw_ostream &OS) const override {
7979
OS << "Duplicate symbol for global value '" << GlobalValueName
80-
<< "' (GUID: " << GlobalValue::getGUID(GlobalValueName) << ") in:\n";
80+
<< "' (GUID: "
81+
<< GlobalValue::getGUIDAssumingExternalLinkage(GlobalValueName)
82+
<< ") in:\n";
8183
for (const std::string &Path : ModulePaths) {
8284
OS << " " << Path << "\n";
8385
}
@@ -110,8 +112,9 @@ class DefinitionNotFoundInSummary
110112
}
111113

112114
void log(raw_ostream &OS) const override {
113-
OS << "No symbol for global value '" << GlobalValueName
114-
<< "' (GUID: " << GlobalValue::getGUID(GlobalValueName) << ") in:\n";
115+
OS << "No symbol for global value '" << GlobalValueName << "' (GUID: "
116+
<< GlobalValue::getGUIDAssumingExternalLinkage(GlobalValueName)
117+
<< ") in:\n";
115118
for (const std::string &Path : ModulePaths) {
116119
OS << " " << Path << "\n";
117120
}
@@ -135,7 +138,8 @@ char DefinitionNotFoundInSummary::ID = 0;
135138
Expected<StringRef> getMainModulePath(StringRef FunctionName,
136139
ModuleSummaryIndex &Index) {
137140
// Summaries use unmangled names.
138-
GlobalValue::GUID G = GlobalValue::getGUID(FunctionName);
141+
GlobalValue::GUID G =
142+
GlobalValue::getGUIDAssumingExternalLinkage(FunctionName);
139143
ValueInfo VI = Index.getValueInfo(G);
140144

141145
// We need a unique definition, otherwise don't try further.

llvm/include/llvm/IR/GlobalValue.h

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,11 @@ class GlobalValue : public Constant {
570570
return Name;
571571
}
572572

573+
/// Declare a type to represent a global unique identifier for a global value.
574+
/// This is a 64 bits hash that is used by PGO and ThinLTO to have a compact
575+
/// unique way to identify a symbol.
576+
using GUID = uint64_t;
577+
573578
/// Return the modified name for a global value suitable to be
574579
/// used as the key for a global lookup (e.g. profile or ThinLTO).
575580
/// The value's original name is \c Name and has linkage of type
@@ -578,22 +583,22 @@ class GlobalValue : public Constant {
578583
GlobalValue::LinkageTypes Linkage,
579584
StringRef FileName);
580585

586+
private:
581587
/// Return the modified name for this global value suitable to be
582588
/// used as the key for a global lookup (e.g. profile or ThinLTO).
583589
std::string getGlobalIdentifier() const;
584590

585-
/// Declare a type to represent a global unique identifier for a global value.
586-
/// This is a 64 bits hash that is used by PGO and ThinLTO to have a compact
587-
/// unique way to identify a symbol.
588-
using GUID = uint64_t;
589-
590-
/// Return a 64-bit global unique ID constructed from global value name
591-
/// (i.e. returned by getGlobalIdentifier()).
592-
static GUID getGUID(StringRef GlobalName);
591+
public:
592+
/// Return a 64-bit global unique ID constructed from the name of a global
593+
/// symbol. Since this call doesn't supply the linkage or defining filename,
594+
/// the GUID computation will assume that the global has external linkage.
595+
static GUID getGUIDAssumingExternalLinkage(StringRef GlobalName);
593596

594597
/// Return a 64-bit global unique ID constructed from global value name
595598
/// (i.e. returned by getGlobalIdentifier()).
596-
GUID getGUID() const { return getGUID(getGlobalIdentifier()); }
599+
GUID getGUID() const {
600+
return getGUIDAssumingExternalLinkage(getGlobalIdentifier());
601+
}
597602

598603
/// @name Materialization
599604
/// Materialization is used to construct functions only as they're needed.

llvm/include/llvm/IR/ModuleSummaryIndex.h

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1321,14 +1321,14 @@ class CfiFunctionIndex {
13211321

13221322
template <typename... Args> void emplace(Args &&...A) {
13231323
StringRef S(std::forward<Args>(A)...);
1324-
GlobalValue::GUID GUID =
1325-
GlobalValue::getGUID(GlobalValue::dropLLVMManglingEscape(S));
1324+
GlobalValue::GUID GUID = GlobalValue::getGUIDAssumingExternalLinkage(
1325+
GlobalValue::dropLLVMManglingEscape(S));
13261326
Index[GUID].emplace(S);
13271327
}
13281328

13291329
size_t count(StringRef S) const {
1330-
GlobalValue::GUID GUID =
1331-
GlobalValue::getGUID(GlobalValue::dropLLVMManglingEscape(S));
1330+
GlobalValue::GUID GUID = GlobalValue::getGUIDAssumingExternalLinkage(
1331+
GlobalValue::dropLLVMManglingEscape(S));
13321332
auto I = Index.find(GUID);
13331333
if (I == Index.end())
13341334
return 0;
@@ -1731,8 +1731,10 @@ class ModuleSummaryIndex {
17311731
/// Add a global value summary for a value of the given name.
17321732
void addGlobalValueSummary(StringRef ValueName,
17331733
std::unique_ptr<GlobalValueSummary> Summary) {
1734-
addGlobalValueSummary(getOrInsertValueInfo(GlobalValue::getGUID(ValueName)),
1735-
std::move(Summary));
1734+
addGlobalValueSummary(
1735+
getOrInsertValueInfo(
1736+
GlobalValue::getGUIDAssumingExternalLinkage(ValueName)),
1737+
std::move(Summary));
17361738
}
17371739

17381740
/// Add a global value summary for the given ValueInfo.
@@ -1869,19 +1871,22 @@ class ModuleSummaryIndex {
18691871
/// This accessor can mutate the map and therefore should not be used in
18701872
/// the ThinLTO backends.
18711873
TypeIdSummary &getOrInsertTypeIdSummary(StringRef TypeId) {
1872-
auto TidIter = TypeIdMap.equal_range(GlobalValue::getGUID(TypeId));
1874+
auto TidIter = TypeIdMap.equal_range(
1875+
GlobalValue::getGUIDAssumingExternalLinkage(TypeId));
18731876
for (auto &[GUID, TypeIdPair] : make_range(TidIter))
18741877
if (TypeIdPair.first == TypeId)
18751878
return TypeIdPair.second;
1876-
auto It = TypeIdMap.insert({GlobalValue::getGUID(TypeId),
1877-
{TypeIdSaver.save(TypeId), TypeIdSummary()}});
1879+
auto It =
1880+
TypeIdMap.insert({GlobalValue::getGUIDAssumingExternalLinkage(TypeId),
1881+
{TypeIdSaver.save(TypeId), TypeIdSummary()}});
18781882
return It->second.second;
18791883
}
18801884

18811885
/// This returns either a pointer to the type id summary (if present in the
18821886
/// summary map) or null (if not present). This may be used when importing.
18831887
const TypeIdSummary *getTypeIdSummary(StringRef TypeId) const {
1884-
auto TidIter = TypeIdMap.equal_range(GlobalValue::getGUID(TypeId));
1888+
auto TidIter = TypeIdMap.equal_range(
1889+
GlobalValue::getGUIDAssumingExternalLinkage(TypeId));
18851890
for (const auto &[GUID, TypeIdPair] : make_range(TidIter))
18861891
if (TypeIdPair.first == TypeId)
18871892
return &TypeIdPair.second;

llvm/include/llvm/IR/ModuleSummaryIndexYAML.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ template <> struct CustomMappingTraits<TypeIdSummaryMapTy> {
314314
static void inputOne(IO &io, StringRef Key, TypeIdSummaryMapTy &V) {
315315
TypeIdSummary TId;
316316
io.mapRequired(Key.str().c_str(), TId);
317-
V.insert({GlobalValue::getGUID(Key), {Key, TId}});
317+
V.insert({GlobalValue::getGUIDAssumingExternalLinkage(Key), {Key, TId}});
318318
}
319319
static void output(IO &io, TypeIdSummaryMapTy &V) {
320320
for (auto &TidIter : V)

llvm/include/llvm/ProfileData/SampleProf.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1299,7 +1299,7 @@ class FunctionSamples {
12991299
static inline FunctionId getRepInFormat(StringRef Name) {
13001300
if (Name.empty() || !FunctionSamples::UseMD5)
13011301
return FunctionId(Name);
1302-
return FunctionId(Function::getGUID(Name));
1302+
return FunctionId(Function::getGUIDAssumingExternalLinkage(Name));
13031303
}
13041304

13051305
raw_ostream &operator<<(raw_ostream &OS, const FunctionSamples &FS);

llvm/include/llvm/Transforms/Utils/SampleProfileLoaderBaseImpl.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,12 @@ class PseudoProbeManager {
109109
}
110110

111111
const PseudoProbeDescriptor *getDesc(StringRef FProfileName) const {
112-
return getDesc(Function::getGUID(FProfileName));
112+
return getDesc(Function::getGUIDAssumingExternalLinkage(FProfileName));
113113
}
114114

115115
const PseudoProbeDescriptor *getDesc(const Function &F) const {
116-
return getDesc(Function::getGUID(FunctionSamples::getCanonicalFnName(F)));
116+
return getDesc(Function::getGUIDAssumingExternalLinkage(
117+
FunctionSamples::getCanonicalFnName(F)));
117118
}
118119

119120
bool profileIsHashMismatched(const PseudoProbeDescriptor &FuncDesc,

llvm/lib/Analysis/CtxProfAnalysis.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ PreservedAnalyses AssignGUIDPass::run(Module &M, ModuleAnalysisManager &MAM) {
436436
GlobalValue::GUID AssignGUIDPass::getGUID(const Function &F) {
437437
if (F.isDeclaration()) {
438438
assert(GlobalValue::isExternalLinkage(F.getLinkage()));
439-
return GlobalValue::getGUID(F.getGlobalIdentifier());
439+
return F.getGUID();
440440
}
441441
auto *MD = F.getMetadata(GUIDMetadataName);
442442
assert(MD && "guid not found for defined function");

llvm/lib/Analysis/ModuleSummaryAnalysis.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,8 @@ static void addIntrinsicToSummary(
222222
auto *TypeId = dyn_cast<MDString>(TypeMDVal->getMetadata());
223223
if (!TypeId)
224224
break;
225-
GlobalValue::GUID Guid = GlobalValue::getGUID(TypeId->getString());
225+
GlobalValue::GUID Guid =
226+
GlobalValue::getGUIDAssumingExternalLinkage(TypeId->getString());
226227

227228
// Produce a summary from type.test intrinsics. We only summarize type.test
228229
// intrinsics that are used other than by an llvm.assume intrinsic.
@@ -250,7 +251,8 @@ static void addIntrinsicToSummary(
250251
auto *TypeId = dyn_cast<MDString>(TypeMDVal->getMetadata());
251252
if (!TypeId)
252253
break;
253-
GlobalValue::GUID Guid = GlobalValue::getGUID(TypeId->getString());
254+
GlobalValue::GUID Guid =
255+
GlobalValue::getGUIDAssumingExternalLinkage(TypeId->getString());
254256

255257
SmallVector<DevirtCallSite, 4> DevirtCalls;
256258
SmallVector<Instruction *, 4> LoadedPtrs;
@@ -904,7 +906,8 @@ static void computeAliasSummary(ModuleSummaryIndex &Index, const GlobalAlias &A,
904906

905907
// Set LiveRoot flag on entries matching the given value name.
906908
static void setLiveRoot(ModuleSummaryIndex &Index, StringRef Name) {
907-
if (ValueInfo VI = Index.getValueInfo(GlobalValue::getGUID(Name)))
909+
if (ValueInfo VI =
910+
Index.getValueInfo(GlobalValue::getGUIDAssumingExternalLinkage(Name)))
908911
for (const auto &Summary : VI.getSummaryList())
909912
Summary->setLive(true);
910913
}

llvm/lib/AsmParser/LLParser.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9049,7 +9049,7 @@ bool LLParser::parseTypeIdEntry(unsigned ID) {
90499049
for (auto TIDRef : FwdRefTIDs->second) {
90509050
assert(!*TIDRef.first &&
90519051
"Forward referenced type id GUID expected to be 0");
9052-
*TIDRef.first = GlobalValue::getGUID(Name);
9052+
*TIDRef.first = GlobalValue::getGUIDAssumingExternalLinkage(Name);
90539053
}
90549054
ForwardRefTypeIds.erase(FwdRefTIDs);
90559055
}
@@ -9154,7 +9154,7 @@ bool LLParser::parseTypeIdCompatibleVtableEntry(unsigned ID) {
91549154
for (auto TIDRef : FwdRefTIDs->second) {
91559155
assert(!*TIDRef.first &&
91569156
"Forward referenced type id GUID expected to be 0");
9157-
*TIDRef.first = GlobalValue::getGUID(Name);
9157+
*TIDRef.first = GlobalValue::getGUIDAssumingExternalLinkage(Name);
91589158
}
91599159
ForwardRefTypeIds.erase(FwdRefTIDs);
91609160
}
@@ -9470,7 +9470,7 @@ bool LLParser::addGlobalValueToIndex(
94709470
assert(
94719471
(!GlobalValue::isLocalLinkage(Linkage) || !SourceFileName.empty()) &&
94729472
"Need a source_filename to compute GUID for local");
9473-
GUID = GlobalValue::getGUID(
9473+
GUID = GlobalValue::getGUIDAssumingExternalLinkage(
94749474
GlobalValue::getGlobalIdentifier(Name, Linkage, SourceFileName));
94759475
VI = Index->getOrInsertValueInfo(GUID, Index->saveString(Name));
94769476
}

0 commit comments

Comments
 (0)