[llvm] Store GUIDs in metadata (PR #133682)

Owen Rodley via llvm-commits llvm-commits at lists.llvm.org
Sun May 11 21:04:06 PDT 2025


================
@@ -78,6 +78,46 @@ GlobalValue::getGUIDAssumingExternalLinkage(StringRef GlobalIdentifier) {
   return MD5Hash(GlobalIdentifier);
 }
 
+void GlobalValue::assignGUID() {
+  if (isDeclaration()) {
+    // Declarations don't get an assigned GUID. Their definition is in another
+    // module, and it may have been promoted from local to external linkage
+    // during LTO. Because of this, we can only determine their GUID once the
+    // definition is available.
+    return;
+  }
+  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 (isa<GlobalAlias>(this)) {
+    return GlobalValue::getGUIDAssumingExternalLinkage(getGlobalIdentifier());
+  }
+
+  if (isDeclaration()) {
----------------
orodley wrote:

Ok, makes sense.

https://github.com/llvm/llvm-project/pull/133682


More information about the llvm-commits mailing list