diff options
author | Owen Rodley <orodley@google.com> | 2025-03-31 16:16:35 +1100 |
---|---|---|
committer | Owen Rodley <orodley@google.com> | 2025-08-05 09:54:41 +1000 |
commit | 3da1ceb81c1f17248babf0c0e2c07f23bd6b138b (patch) | |
tree | 03c39df6e5390d3eddcf5efac79f194c622f7a33 /llvm/lib/IR/Globals.cpp | |
parent | 9b195dc3ef66de2c1ff0048822b24a322ec3c52a (diff) | |
download | llvm-users/orodley/guid-2.zip llvm-users/orodley/guid-2.tar.gz llvm-users/orodley/guid-2.tar.bz2 |
Store GUIDs in metadatausers/orodley/guid-2
See https://discourse.llvm.org/t/rfc-keep-globalvalue-guids-stable/84801
for context.
This takes the existing AssignGUID pass from CtxProfAnalysis, and runs
it by default, at the appropriate stages of the LTO pipeline. It also
changes GlobalValue::getGUID() to retrieve the GUID from the metadata
instead of computing it.
We don't yet have the supporting downstream changes to make a dedicated
GUID table in bitcode, nor do we use the metadata as part of ThinLTO --
it retains its existing mechanisms of recomputing GUIDs from separately
saved data. That will be changed later.
Diffstat (limited to 'llvm/lib/IR/Globals.cpp')
-rw-r--r-- | llvm/lib/IR/Globals.cpp | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/llvm/lib/IR/Globals.cpp b/llvm/lib/IR/Globals.cpp index 7b799c7..4a4b5bb 100644 --- a/llvm/lib/IR/Globals.cpp +++ b/llvm/lib/IR/Globals.cpp @@ -78,6 +78,34 @@ GlobalValue::getGUIDAssumingExternalLinkage(StringRef GlobalIdentifier) { return MD5Hash(GlobalIdentifier); } +void GlobalValue::assignGUID() { + if (getMetadata(LLVMContext::MD_unique_id) != nullptr) + return; + + const GUID G = + GlobalValue::getGUIDAssumingExternalLinkage(getGlobalIdentifier()); + setMetadata( + LLVMContext::MD_unique_id, + MDNode::get(getContext(), {ConstantAsMetadata::get(ConstantInt::get( + Type::getInt64Ty(getContext()), G))})); +} + +GlobalValue::GUID GlobalValue::getGUID() const { + if (isDeclaration()) { + return GlobalValue::getGUIDAssumingExternalLinkage(getGlobalIdentifier()); + } + if (isa<GlobalAlias>(this)) { + return GlobalValue::getGUIDAssumingExternalLinkage(getGlobalIdentifier()); + } + + auto *MD = getMetadata(LLVMContext::MD_unique_id); + assert(MD != nullptr && "GUID was not assigned before calling GetGUID()"); + return cast<ConstantInt>(cast<ConstantAsMetadata>(MD->getOperand(0)) + ->getValue() + ->stripPointerCasts()) + ->getZExtValue(); +} + void GlobalValue::removeFromParent() { switch (getValueID()) { #define HANDLE_GLOBAL_VALUE(NAME) \ |