[clang] [llvm] Compute GUIDs once and store in metadata (PR #184065)
Teresa Johnson via cfe-commits
cfe-commits at lists.llvm.org
Fri May 1 12:17:03 PDT 2026
================
@@ -81,6 +81,67 @@ GlobalValue::getGUIDAssumingExternalLinkage(StringRef GlobalIdentifier) {
return MD5Hash(GlobalIdentifier);
}
+void GlobalValue::assignGUID() {
+ if (getGUIDMetadata() != 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 {
+ auto MaybeGUID = getGUIDIfAssigned();
+ assert(MaybeGUID.has_value() &&
+ "GUID was not assigned before calling GetGUID()");
+ return *MaybeGUID;
+}
+
+GlobalValue::GUID GlobalValue::getGUIDOrFallback() const {
+ if (auto MaybeGUID = getGUIDIfAssigned(); MaybeGUID)
+ return *MaybeGUID;
+ return getGUIDAssumingExternalLinkage(getGlobalIdentifier());
+}
+
+std::optional<GlobalValue::GUID> GlobalValue::getGUIDIfAssigned() const {
+ // First check the metadata.
+ auto *MD = getGUIDMetadata();
+ if (MD != nullptr)
+ return cast<ConstantInt>(cast<ConstantAsMetadata>(MD->getOperand(0))
+ ->getValue()
+ ->stripPointerCasts())
+ ->getZExtValue();
+
+ // Handle a few special cases where we just want to compute it based on the
+ // current properties.
+ // TODO: Maybe we should use a more robust check for intrinsics than just
+ // matching on the name?
+ if (isDeclaration() || isa<GlobalAlias>(this) ||
+ getName().starts_with("llvm.")) {
+ return GlobalValue::getGUIDAssumingExternalLinkage(getGlobalIdentifier());
+ }
+
+ // Otherwise we try to look it up in the module, for cases where we've read
+ // the GUID table but not the metadata. This happens when lazy-loading a
+ // module.
+ if (getParent() == nullptr)
+ return {};
+ const Module &M = *getParent();
+
----------------
teresajohnson wrote:
nit remove blank line between these
https://github.com/llvm/llvm-project/pull/184065
More information about the cfe-commits
mailing list