Skip to content

Commit 1cf9bc2

Browse files
committed
Clean up external users of GlobalValue::getGUID(StringRef)
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: * Make global identifier computation nominally private. There are a few users we can't remove yet, but we rename the method to make it clear that new users shouldn't be added. * 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. * Where users don't actually need the gloalIdentifier and just need the name, call getName() instead. * There are a few cases where none 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 and allow them to call our nominally private global identifier computation method.
1 parent 4fde8c3 commit 1cf9bc2

29 files changed

+122
-86
lines changed

llvm/include/llvm/IR/GlobalValue.h

Lines changed: 16 additions & 6 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,18 +583,23 @@ 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-
590591
/// Return a 64-bit global unique ID constructed from global value name
591592
/// (i.e. returned by getGlobalIdentifier()).
592-
static GUID getGUID(StringRef GlobalName);
593+
static GUID getGUID(StringRef GlobalIdentifier);
594+
595+
public:
596+
/// Return a 64-bit global unique ID constructed from the name of a global
597+
/// symbol. Since this call doesn't supply the linkage or defining filename,
598+
/// the GUID computation will assume that the global has external linkage.
599+
static GUID getGUIDAssumingExternalLinkage(StringRef GlobalName) {
600+
return getGUID(
601+
getGlobalIdentifier(GlobalName, GlobalValue::ExternalLinkage, ""));
602+
}
593603

594604
/// Return a 64-bit global unique ID constructed from global value name
595605
/// (i.e. returned by getGlobalIdentifier()).

llvm/include/llvm/IR/ModuleSummaryIndex.h

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1340,13 +1340,13 @@ class CfiFunctionIndex {
13401340
template <typename... Args> void emplace(Args &&...A) {
13411341
StringRef S(std::forward<Args>(A)...);
13421342
GlobalValue::GUID GUID =
1343-
GlobalValue::getGUID(GlobalValue::dropLLVMManglingEscape(S));
1343+
GlobalValue::getGUIDAssumingExternalLinkage(GlobalValue::dropLLVMManglingEscape(S));
13441344
Index[GUID].emplace(S);
13451345
}
13461346

13471347
size_t count(StringRef S) const {
13481348
GlobalValue::GUID GUID =
1349-
GlobalValue::getGUID(GlobalValue::dropLLVMManglingEscape(S));
1349+
GlobalValue::getGUIDAssumingExternalLinkage(GlobalValue::dropLLVMManglingEscape(S));
13501350
auto I = Index.find(GUID);
13511351
if (I == Index.end())
13521352
return 0;
@@ -1749,8 +1749,10 @@ class ModuleSummaryIndex {
17491749
/// Add a global value summary for a value of the given name.
17501750
void addGlobalValueSummary(StringRef ValueName,
17511751
std::unique_ptr<GlobalValueSummary> Summary) {
1752-
addGlobalValueSummary(getOrInsertValueInfo(GlobalValue::getGUID(ValueName)),
1753-
std::move(Summary));
1752+
addGlobalValueSummary(
1753+
getOrInsertValueInfo(
1754+
GlobalValue::getGUIDAssumingExternalLinkage(ValueName)),
1755+
std::move(Summary));
17541756
}
17551757

17561758
/// Add a global value summary for the given ValueInfo.
@@ -1887,19 +1889,22 @@ class ModuleSummaryIndex {
18871889
/// This accessor can mutate the map and therefore should not be used in
18881890
/// the ThinLTO backends.
18891891
TypeIdSummary &getOrInsertTypeIdSummary(StringRef TypeId) {
1890-
auto TidIter = TypeIdMap.equal_range(GlobalValue::getGUID(TypeId));
1892+
auto TidIter = TypeIdMap.equal_range(
1893+
GlobalValue::getGUIDAssumingExternalLinkage(TypeId));
18911894
for (auto &[GUID, TypeIdPair] : make_range(TidIter))
18921895
if (TypeIdPair.first == TypeId)
18931896
return TypeIdPair.second;
1894-
auto It = TypeIdMap.insert({GlobalValue::getGUID(TypeId),
1895-
{TypeIdSaver.save(TypeId), TypeIdSummary()}});
1897+
auto It =
1898+
TypeIdMap.insert({GlobalValue::getGUIDAssumingExternalLinkage(TypeId),
1899+
{TypeIdSaver.save(TypeId), TypeIdSummary()}});
18961900
return It->second.second;
18971901
}
18981902

18991903
/// This returns either a pointer to the type id summary (if present in the
19001904
/// summary map) or null (if not present). This may be used when importing.
19011905
const TypeIdSummary *getTypeIdSummary(StringRef TypeId) const {
1902-
auto TidIter = TypeIdMap.equal_range(GlobalValue::getGUID(TypeId));
1906+
auto TidIter = TypeIdMap.equal_range(
1907+
GlobalValue::getGUIDAssumingExternalLinkage(TypeId));
19031908
for (const auto &[GUID, TypeIdPair] : make_range(TidIter))
19041909
if (TypeIdPair.first == TypeId)
19051910
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
@@ -57,7 +57,7 @@ PreservedAnalyses AssignGUIDPass::run(Module &M, ModuleAnalysisManager &MAM) {
5757
GlobalValue::GUID AssignGUIDPass::getGUID(const Function &F) {
5858
if (F.isDeclaration()) {
5959
assert(GlobalValue::isExternalLinkage(F.getLinkage()));
60-
return GlobalValue::getGUID(F.getGlobalIdentifier());
60+
return F.getGUID();
6161
}
6262
auto *MD = F.getMetadata(GUIDMetadataName);
6363
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;
@@ -906,7 +908,8 @@ static void computeAliasSummary(ModuleSummaryIndex &Index, const GlobalAlias &A,
906908

907909
// Set LiveRoot flag on entries matching the given value name.
908910
static void setLiveRoot(ModuleSummaryIndex &Index, StringRef Name) {
909-
if (ValueInfo VI = Index.getValueInfo(GlobalValue::getGUID(Name)))
911+
if (ValueInfo VI =
912+
Index.getValueInfo(GlobalValue::getGUIDAssumingExternalLinkage(Name)))
910913
for (const auto &Summary : VI.getSummaryList())
911914
Summary->setLive(true);
912915
}

llvm/lib/AsmParser/LLParser.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9005,7 +9005,7 @@ bool LLParser::parseTypeIdEntry(unsigned ID) {
90059005
for (auto TIDRef : FwdRefTIDs->second) {
90069006
assert(!*TIDRef.first &&
90079007
"Forward referenced type id GUID expected to be 0");
9008-
*TIDRef.first = GlobalValue::getGUID(Name);
9008+
*TIDRef.first = GlobalValue::getGUIDAssumingExternalLinkage(Name);
90099009
}
90109010
ForwardRefTypeIds.erase(FwdRefTIDs);
90119011
}
@@ -9110,7 +9110,7 @@ bool LLParser::parseTypeIdCompatibleVtableEntry(unsigned ID) {
91109110
for (auto TIDRef : FwdRefTIDs->second) {
91119111
assert(!*TIDRef.first &&
91129112
"Forward referenced type id GUID expected to be 0");
9113-
*TIDRef.first = GlobalValue::getGUID(Name);
9113+
*TIDRef.first = GlobalValue::getGUIDAssumingExternalLinkage(Name);
91149114
}
91159115
ForwardRefTypeIds.erase(FwdRefTIDs);
91169116
}
@@ -9423,11 +9423,9 @@ bool LLParser::addGlobalValueToIndex(
94239423

94249424
VI = Index->getOrInsertValueInfo(GV);
94259425
} else {
9426-
assert(
9427-
(!GlobalValue::isLocalLinkage(Linkage) || !SourceFileName.empty()) &&
9428-
"Need a source_filename to compute GUID for local");
9429-
GUID = GlobalValue::getGUID(
9430-
GlobalValue::getGlobalIdentifier(Name, Linkage, SourceFileName));
9426+
assert(GlobalValue::isExternalLinkage(Linkage) &&
9427+
"Cannot compute GUID for local");
9428+
GUID = GlobalValue::getGUIDAssumingExternalLinkage(Name);
94319429
VI = Index->getOrInsertValueInfo(GUID, Index->saveString(Name));
94329430
}
94339431
}

llvm/lib/Bitcode/Reader/BitcodeReader.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7218,10 +7218,10 @@ void ModuleSummaryIndexBitcodeReader::setValueGUID(
72187218
StringRef SourceFileName) {
72197219
std::string GlobalId =
72207220
GlobalValue::getGlobalIdentifier(ValueName, Linkage, SourceFileName);
7221-
auto ValueGUID = GlobalValue::getGUID(GlobalId);
7221+
auto ValueGUID = GlobalValue::getGUIDAssumingExternalLinkage(GlobalId);
72227222
auto OriginalNameID = ValueGUID;
72237223
if (GlobalValue::isLocalLinkage(Linkage))
7224-
OriginalNameID = GlobalValue::getGUID(ValueName);
7224+
OriginalNameID = GlobalValue::getGUIDAssumingExternalLinkage(ValueName);
72257225
if (PrintSummaryGUIDs)
72267226
dbgs() << "GUID " << ValueGUID << "(" << OriginalNameID << ") is "
72277227
<< ValueName << "\n";

llvm/lib/CodeGen/AsmPrinter/PseudoProbePrinter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ void PseudoProbeHandler::emitPseudoProbe(uint64_t Guid, uint64_t Index,
3434
// Use caching to avoid redundant md5 computation for build speed.
3535
uint64_t &CallerGuid = NameGuidMap[Name];
3636
if (!CallerGuid)
37-
CallerGuid = Function::getGUID(Name);
37+
CallerGuid = Function::getGUIDAssumingExternalLinkage(Name);
3838
uint64_t CallerProbeId = PseudoProbeDwarfDiscriminator::extractProbeIndex(
3939
InlinedAt->getDiscriminator());
4040
ReversedInlineStack.emplace_back(CallerGuid, CallerProbeId);

0 commit comments

Comments
 (0)