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

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Tue May 27 02:41:05 PDT 2025


================
@@ -78,6 +78,27 @@ 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 {
+  auto *MD = getMetadata(LLVMContext::MD_unique_id);
+  assert(MD != nullptr && "GUID was not assigned before calling GetGUID()");
----------------
nikic wrote:

TBH I don't really get how this works as-implemented. You are assigning GUIDs at the very start of the pipeline, but there's lots of passes that can insert new globals after that...

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


More information about the llvm-commits mailing list