Skip to content

Clean up external users of GlobalValue::getGUID(StringRef) #129644

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions bolt/lib/Rewrite/PseudoProbeRewriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ void PseudoProbeRewriter::parsePseudoProbe(bool ProfiledOnly) {
if (!Name)
continue;
SymName = *Name;
uint64_t GUID = Function::getGUID(SymName);
uint64_t GUID = Function::getGUIDAssumingExternalLinkage(SymName);
FuncStartAddrs[GUID] = F->getAddress();
if (ProfiledOnly && HasProfile)
GuidFilter.insert(GUID);
Expand All @@ -173,7 +173,7 @@ void PseudoProbeRewriter::parsePseudoProbe(bool ProfiledOnly) {
const GUIDProbeFunctionMap &GUID2Func = ProbeDecoder.getGUID2FuncDescMap();
// Checks GUID in GUID2Func and returns it if it's present or null otherwise.
auto checkGUID = [&](StringRef SymName) -> uint64_t {
uint64_t GUID = Function::getGUID(SymName);
uint64_t GUID = Function::getGUIDAssumingExternalLinkage(SymName);
if (GUID2Func.find(GUID) == GUID2Func.end())
return 0;
return GUID;
Expand Down Expand Up @@ -435,7 +435,7 @@ void PseudoProbeRewriter::encodePseudoProbes() {
for (const BinaryFunction *F : BC.getAllBinaryFunctions()) {
const uint64_t Addr =
F->isEmitted() ? F->getOutputAddress() : F->getAddress();
FuncStartAddrs[Function::getGUID(
FuncStartAddrs[Function::getGUIDAssumingExternalLinkage(
NameResolver::restore(F->getOneName()))] = Addr;
}
DummyDecoder.buildAddress2ProbeMap(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ class DuplicateDefinitionInSummary

void log(raw_ostream &OS) const override {
OS << "Duplicate symbol for global value '" << GlobalValueName
<< "' (GUID: " << GlobalValue::getGUID(GlobalValueName) << ") in:\n";
<< "' (GUID: "
<< GlobalValue::getGUIDAssumingExternalLinkage(GlobalValueName)
<< ") in:\n";
for (const std::string &Path : ModulePaths) {
OS << " " << Path << "\n";
}
Expand Down Expand Up @@ -110,8 +112,9 @@ class DefinitionNotFoundInSummary
}

void log(raw_ostream &OS) const override {
OS << "No symbol for global value '" << GlobalValueName
<< "' (GUID: " << GlobalValue::getGUID(GlobalValueName) << ") in:\n";
OS << "No symbol for global value '" << GlobalValueName << "' (GUID: "
<< GlobalValue::getGUIDAssumingExternalLinkage(GlobalValueName)
<< ") in:\n";
for (const std::string &Path : ModulePaths) {
OS << " " << Path << "\n";
}
Expand All @@ -135,7 +138,8 @@ char DefinitionNotFoundInSummary::ID = 0;
Expected<StringRef> getMainModulePath(StringRef FunctionName,
ModuleSummaryIndex &Index) {
// Summaries use unmangled names.
GlobalValue::GUID G = GlobalValue::getGUID(FunctionName);
GlobalValue::GUID G =
GlobalValue::getGUIDAssumingExternalLinkage(FunctionName);
ValueInfo VI = Index.getValueInfo(G);

// We need a unique definition, otherwise don't try further.
Expand Down
23 changes: 14 additions & 9 deletions llvm/include/llvm/IR/GlobalValue.h
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,11 @@ class GlobalValue : public Constant {
return Name;
}

/// Declare a type to represent a global unique identifier for a global value.
/// This is a 64 bits hash that is used by PGO and ThinLTO to have a compact
/// unique way to identify a symbol.
using GUID = uint64_t;

/// Return the modified name for a global value suitable to be
/// used as the key for a global lookup (e.g. profile or ThinLTO).
/// The value's original name is \c Name and has linkage of type
Expand All @@ -578,22 +583,22 @@ class GlobalValue : public Constant {
GlobalValue::LinkageTypes Linkage,
StringRef FileName);

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

/// Declare a type to represent a global unique identifier for a global value.
/// This is a 64 bits hash that is used by PGO and ThinLTO to have a compact
/// unique way to identify a symbol.
using GUID = uint64_t;

/// Return a 64-bit global unique ID constructed from global value name
/// (i.e. returned by getGlobalIdentifier()).
static GUID getGUID(StringRef GlobalName);
public:
/// Return a 64-bit global unique ID constructed from the name of a global
/// symbol. Since this call doesn't supply the linkage or defining filename,
/// the GUID computation will assume that the global has external linkage.
static GUID getGUIDAssumingExternalLinkage(StringRef GlobalName);

/// Return a 64-bit global unique ID constructed from global value name
/// (i.e. returned by getGlobalIdentifier()).
GUID getGUID() const { return getGUID(getGlobalIdentifier()); }
GUID getGUID() const {
return getGUIDAssumingExternalLinkage(getGlobalIdentifier());
}

/// @name Materialization
/// Materialization is used to construct functions only as they're needed.
Expand Down
25 changes: 15 additions & 10 deletions llvm/include/llvm/IR/ModuleSummaryIndex.h
Original file line number Diff line number Diff line change
Expand Up @@ -1339,14 +1339,14 @@ class CfiFunctionIndex {

template <typename... Args> void emplace(Args &&...A) {
StringRef S(std::forward<Args>(A)...);
GlobalValue::GUID GUID =
GlobalValue::getGUID(GlobalValue::dropLLVMManglingEscape(S));
GlobalValue::GUID GUID = GlobalValue::getGUIDAssumingExternalLinkage(
GlobalValue::dropLLVMManglingEscape(S));
Index[GUID].emplace(S);
}

size_t count(StringRef S) const {
GlobalValue::GUID GUID =
GlobalValue::getGUID(GlobalValue::dropLLVMManglingEscape(S));
GlobalValue::GUID GUID = GlobalValue::getGUIDAssumingExternalLinkage(
GlobalValue::dropLLVMManglingEscape(S));
auto I = Index.find(GUID);
if (I == Index.end())
return 0;
Expand Down Expand Up @@ -1749,8 +1749,10 @@ class ModuleSummaryIndex {
/// Add a global value summary for a value of the given name.
void addGlobalValueSummary(StringRef ValueName,
std::unique_ptr<GlobalValueSummary> Summary) {
addGlobalValueSummary(getOrInsertValueInfo(GlobalValue::getGUID(ValueName)),
std::move(Summary));
addGlobalValueSummary(
getOrInsertValueInfo(
GlobalValue::getGUIDAssumingExternalLinkage(ValueName)),
std::move(Summary));
}

/// Add a global value summary for the given ValueInfo.
Expand Down Expand Up @@ -1887,19 +1889,22 @@ class ModuleSummaryIndex {
/// This accessor can mutate the map and therefore should not be used in
/// the ThinLTO backends.
TypeIdSummary &getOrInsertTypeIdSummary(StringRef TypeId) {
auto TidIter = TypeIdMap.equal_range(GlobalValue::getGUID(TypeId));
auto TidIter = TypeIdMap.equal_range(
GlobalValue::getGUIDAssumingExternalLinkage(TypeId));
for (auto &[GUID, TypeIdPair] : make_range(TidIter))
if (TypeIdPair.first == TypeId)
return TypeIdPair.second;
auto It = TypeIdMap.insert({GlobalValue::getGUID(TypeId),
{TypeIdSaver.save(TypeId), TypeIdSummary()}});
auto It =
TypeIdMap.insert({GlobalValue::getGUIDAssumingExternalLinkage(TypeId),
{TypeIdSaver.save(TypeId), TypeIdSummary()}});
return It->second.second;
}

/// This returns either a pointer to the type id summary (if present in the
/// summary map) or null (if not present). This may be used when importing.
const TypeIdSummary *getTypeIdSummary(StringRef TypeId) const {
auto TidIter = TypeIdMap.equal_range(GlobalValue::getGUID(TypeId));
auto TidIter = TypeIdMap.equal_range(
GlobalValue::getGUIDAssumingExternalLinkage(TypeId));
for (const auto &[GUID, TypeIdPair] : make_range(TidIter))
if (TypeIdPair.first == TypeId)
return &TypeIdPair.second;
Expand Down
2 changes: 1 addition & 1 deletion llvm/include/llvm/IR/ModuleSummaryIndexYAML.h
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ template <> struct CustomMappingTraits<TypeIdSummaryMapTy> {
static void inputOne(IO &io, StringRef Key, TypeIdSummaryMapTy &V) {
TypeIdSummary TId;
io.mapRequired(Key.str().c_str(), TId);
V.insert({GlobalValue::getGUID(Key), {Key, TId}});
V.insert({GlobalValue::getGUIDAssumingExternalLinkage(Key), {Key, TId}});
}
static void output(IO &io, TypeIdSummaryMapTy &V) {
for (auto &TidIter : V)
Expand Down
2 changes: 1 addition & 1 deletion llvm/include/llvm/ProfileData/SampleProf.h
Original file line number Diff line number Diff line change
Expand Up @@ -1299,7 +1299,7 @@ class FunctionSamples {
static inline FunctionId getRepInFormat(StringRef Name) {
if (Name.empty() || !FunctionSamples::UseMD5)
return FunctionId(Name);
return FunctionId(Function::getGUID(Name));
return FunctionId(Function::getGUIDAssumingExternalLinkage(Name));
}

raw_ostream &operator<<(raw_ostream &OS, const FunctionSamples &FS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,12 @@ class PseudoProbeManager {
}

const PseudoProbeDescriptor *getDesc(StringRef FProfileName) const {
return getDesc(Function::getGUID(FProfileName));
return getDesc(Function::getGUIDAssumingExternalLinkage(FProfileName));
}

const PseudoProbeDescriptor *getDesc(const Function &F) const {
return getDesc(Function::getGUID(FunctionSamples::getCanonicalFnName(F)));
return getDesc(Function::getGUIDAssumingExternalLinkage(
FunctionSamples::getCanonicalFnName(F)));
}

bool profileIsHashMismatched(const PseudoProbeDescriptor &FuncDesc,
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Analysis/CtxProfAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ PreservedAnalyses AssignGUIDPass::run(Module &M, ModuleAnalysisManager &MAM) {
GlobalValue::GUID AssignGUIDPass::getGUID(const Function &F) {
if (F.isDeclaration()) {
assert(GlobalValue::isExternalLinkage(F.getLinkage()));
return GlobalValue::getGUID(F.getGlobalIdentifier());
return F.getGUID();
}
auto *MD = F.getMetadata(GUIDMetadataName);
assert(MD && "guid not found for defined function");
Expand Down
9 changes: 6 additions & 3 deletions llvm/lib/Analysis/ModuleSummaryAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,8 @@ static void addIntrinsicToSummary(
auto *TypeId = dyn_cast<MDString>(TypeMDVal->getMetadata());
if (!TypeId)
break;
GlobalValue::GUID Guid = GlobalValue::getGUID(TypeId->getString());
GlobalValue::GUID Guid =
GlobalValue::getGUIDAssumingExternalLinkage(TypeId->getString());

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

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

// Set LiveRoot flag on entries matching the given value name.
static void setLiveRoot(ModuleSummaryIndex &Index, StringRef Name) {
if (ValueInfo VI = Index.getValueInfo(GlobalValue::getGUID(Name)))
if (ValueInfo VI =
Index.getValueInfo(GlobalValue::getGUIDAssumingExternalLinkage(Name)))
for (const auto &Summary : VI.getSummaryList())
Summary->setLive(true);
}
Expand Down
6 changes: 3 additions & 3 deletions llvm/lib/AsmParser/LLParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9049,7 +9049,7 @@ bool LLParser::parseTypeIdEntry(unsigned ID) {
for (auto TIDRef : FwdRefTIDs->second) {
assert(!*TIDRef.first &&
"Forward referenced type id GUID expected to be 0");
*TIDRef.first = GlobalValue::getGUID(Name);
*TIDRef.first = GlobalValue::getGUIDAssumingExternalLinkage(Name);
}
ForwardRefTypeIds.erase(FwdRefTIDs);
}
Expand Down Expand Up @@ -9154,7 +9154,7 @@ bool LLParser::parseTypeIdCompatibleVtableEntry(unsigned ID) {
for (auto TIDRef : FwdRefTIDs->second) {
assert(!*TIDRef.first &&
"Forward referenced type id GUID expected to be 0");
*TIDRef.first = GlobalValue::getGUID(Name);
*TIDRef.first = GlobalValue::getGUIDAssumingExternalLinkage(Name);
}
ForwardRefTypeIds.erase(FwdRefTIDs);
}
Expand Down Expand Up @@ -9470,7 +9470,7 @@ bool LLParser::addGlobalValueToIndex(
assert(
(!GlobalValue::isLocalLinkage(Linkage) || !SourceFileName.empty()) &&
"Need a source_filename to compute GUID for local");
GUID = GlobalValue::getGUID(
GUID = GlobalValue::getGUIDAssumingExternalLinkage(
GlobalValue::getGlobalIdentifier(Name, Linkage, SourceFileName));
VI = Index->getOrInsertValueInfo(GUID, Index->saveString(Name));
}
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/Bitcode/Reader/BitcodeReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7166,10 +7166,10 @@ void ModuleSummaryIndexBitcodeReader::setValueGUID(
StringRef SourceFileName) {
std::string GlobalId =
GlobalValue::getGlobalIdentifier(ValueName, Linkage, SourceFileName);
auto ValueGUID = GlobalValue::getGUID(GlobalId);
auto ValueGUID = GlobalValue::getGUIDAssumingExternalLinkage(GlobalId);
auto OriginalNameID = ValueGUID;
if (GlobalValue::isLocalLinkage(Linkage))
OriginalNameID = GlobalValue::getGUID(ValueName);
OriginalNameID = GlobalValue::getGUIDAssumingExternalLinkage(ValueName);
if (PrintSummaryGUIDs)
dbgs() << "GUID " << ValueGUID << "(" << OriginalNameID << ") is "
<< ValueName << "\n";
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/CodeGen/AsmPrinter/PseudoProbePrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ void PseudoProbeHandler::emitPseudoProbe(uint64_t Guid, uint64_t Index,
// Use caching to avoid redundant md5 computation for build speed.
uint64_t &CallerGuid = NameGuidMap[Name];
if (!CallerGuid)
CallerGuid = Function::getGUID(Name);
CallerGuid = Function::getGUIDAssumingExternalLinkage(Name);
uint64_t CallerProbeId = PseudoProbeDwarfDiscriminator::extractProbeIndex(
InlinedAt->getDiscriminator());
ReversedInlineStack.emplace_back(CallerGuid, CallerProbeId);
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/CodeGen/PseudoProbeInserter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ class PseudoProbeInserter : public MachineFunctionPass {
private:
uint64_t getFuncGUID(Module *M, DILocation *DL) {
auto Name = DL->getSubprogramLinkageName();
return Function::getGUID(Name);
return Function::getGUIDAssumingExternalLinkage(Name);
}

bool ShouldRun = false;
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/IR/AsmWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3226,7 +3226,7 @@ void AssemblyWriter::printModuleSummaryIndex() {

// Print the TypeIdCompatibleVtableMap entries.
for (auto &TId : TheIndex->typeIdCompatibleVtableMap()) {
auto GUID = GlobalValue::getGUID(TId.first);
auto GUID = GlobalValue::getGUIDAssumingExternalLinkage(TId.first);
Out << "^" << Machine.getTypeIdCompatibleVtableSlot(TId.first)
<< " = typeidCompatibleVTable: (name: \"" << TId.first << "\"";
printTypeIdCompatibleVtableSummary(TId.second);
Expand Down
5 changes: 3 additions & 2 deletions llvm/lib/IR/Globals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,9 @@ void GlobalValue::copyAttributesFrom(const GlobalValue *Src) {
removeSanitizerMetadata();
}

GlobalValue::GUID GlobalValue::getGUID(StringRef GlobalName) {
return MD5Hash(GlobalName);
GlobalValue::GUID
GlobalValue::getGUIDAssumingExternalLinkage(StringRef GlobalIdentifier) {
return MD5Hash(GlobalIdentifier);
}

void GlobalValue::removeFromParent() {
Expand Down
14 changes: 8 additions & 6 deletions llvm/lib/LTO/LTO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1043,8 +1043,9 @@ Error LTO::addThinLTO(BitcodeModule BM, ArrayRef<InputFile::Symbol> Syms,
SymbolResolution Res = *ResITmp++;

if (!Sym.getIRName().empty()) {
auto GUID = GlobalValue::getGUID(GlobalValue::getGlobalIdentifier(
Sym.getIRName(), GlobalValue::ExternalLinkage, ""));
auto GUID = GlobalValue::getGUIDAssumingExternalLinkage(
GlobalValue::getGlobalIdentifier(Sym.getIRName(),
GlobalValue::ExternalLinkage, ""));
if (Res.Prevailing)
ThinLTO.PrevailingModuleForGUID[GUID] = BM.getModuleIdentifier();
}
Expand All @@ -1064,8 +1065,9 @@ Error LTO::addThinLTO(BitcodeModule BM, ArrayRef<InputFile::Symbol> Syms,
SymbolResolution Res = *ResI++;

if (!Sym.getIRName().empty()) {
auto GUID = GlobalValue::getGUID(GlobalValue::getGlobalIdentifier(
Sym.getIRName(), GlobalValue::ExternalLinkage, ""));
auto GUID = GlobalValue::getGUIDAssumingExternalLinkage(
GlobalValue::getGlobalIdentifier(Sym.getIRName(),
GlobalValue::ExternalLinkage, ""));
if (Res.Prevailing) {
assert(ThinLTO.PrevailingModuleForGUID[GUID] ==
BM.getModuleIdentifier());
Expand Down Expand Up @@ -1174,7 +1176,7 @@ Error LTO::run(AddStreamFn AddStream, FileCache Cache) {
if (Res.second.IRName.empty())
continue;

GlobalValue::GUID GUID = GlobalValue::getGUID(
GlobalValue::GUID GUID = GlobalValue::getGUIDAssumingExternalLinkage(
GlobalValue::dropLLVMManglingEscape(Res.second.IRName));

if (Res.second.VisibleOutsideSummary && Res.second.Prevailing)
Expand Down Expand Up @@ -1950,7 +1952,7 @@ Error LTO::runThinLTO(AddStreamFn AddStream, FileCache Cache,
if (Res.second.Partition != GlobalResolution::External ||
!Res.second.isPrevailingIRSymbol())
continue;
auto GUID = GlobalValue::getGUID(
auto GUID = GlobalValue::getGUIDAssumingExternalLinkage(
GlobalValue::dropLLVMManglingEscape(Res.second.IRName));
// Mark exported unless index-based analysis determined it to be dead.
if (ThinLTO.CombinedIndex.isGUIDLive(GUID))
Expand Down
8 changes: 5 additions & 3 deletions llvm/lib/LTO/ThinLTOCodeGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,8 @@ addUsedSymbolToPreservedGUID(const lto::InputFile &File,
DenseSet<GlobalValue::GUID> &PreservedGUID) {
for (const auto &Sym : File.symbols()) {
if (Sym.isUsed())
PreservedGUID.insert(GlobalValue::getGUID(Sym.getIRName()));
PreservedGUID.insert(
GlobalValue::getGUIDAssumingExternalLinkage(Sym.getIRName()));
}
}

Expand All @@ -308,8 +309,9 @@ static void computeGUIDPreservedSymbols(const lto::InputFile &File,
// compute the GUID for the symbol.
for (const auto &Sym : File.symbols()) {
if (PreservedSymbols.count(Sym.getName()) && !Sym.getIRName().empty())
GUIDs.insert(GlobalValue::getGUID(GlobalValue::getGlobalIdentifier(
Sym.getIRName(), GlobalValue::ExternalLinkage, "")));
GUIDs.insert(GlobalValue::getGUIDAssumingExternalLinkage(
GlobalValue::getGlobalIdentifier(Sym.getIRName(),
GlobalValue::ExternalLinkage, "")));
}
}

Expand Down
6 changes: 3 additions & 3 deletions llvm/lib/ProfileData/InstrProf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -516,8 +516,8 @@ Error InstrProfSymtab::addVTableWithName(GlobalVariable &VTable,
return E;

bool Inserted = true;
std::tie(std::ignore, Inserted) =
MD5VTableMap.try_emplace(GlobalValue::getGUID(Name), &VTable);
std::tie(std::ignore, Inserted) = MD5VTableMap.try_emplace(
GlobalValue::getGUIDAssumingExternalLinkage(Name), &VTable);
if (!Inserted)
LLVM_DEBUG(dbgs() << "GUID conflict within one module");
return Error::success();
Expand Down Expand Up @@ -635,7 +635,7 @@ Error InstrProfSymtab::addFuncWithName(Function &F, StringRef PGOFuncName,
auto NameToGUIDMap = [&](StringRef Name) -> Error {
if (Error E = addFuncName(Name))
return E;
MD5FuncMap.emplace_back(Function::getGUID(Name), &F);
MD5FuncMap.emplace_back(Function::getGUIDAssumingExternalLinkage(Name), &F);
return Error::success();
};
if (Error E = NameToGUIDMap(PGOFuncName))
Expand Down
Loading