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

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Tue May 27 02:38:22 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:

This seems like something that would be reachable through `opt` with an "incorrect" pipeline? If so, this should use reportFatalUsageError instead.

Though generally, requiring that a certain pass runs before another pass and no new globals are introduced in between sounds fragile to me....

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


More information about the llvm-commits mailing list