[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:06 PDT 2026
================
@@ -79,6 +79,52 @@ 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 MaybeGUID = getGUIDIfAssigned();
+ assert(MaybeGUID.has_value() &&
+ "GUID was not assigned before calling GetGUID()");
+ return *MaybeGUID;
+}
+
+std::optional<GlobalValue::GUID> GlobalValue::getGUIDIfAssigned() const {
+ // First check the metadata.
+ auto *MD = getMetadata(LLVMContext::MD_unique_id);
+ 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) ||
----------------
teresajohnson wrote:
I spent some time digging into our handling of summary attribute propagation for declarations, and post-LTO link lookup of summaries by GUIDs to see if they might cause an issue if promoted. In short, I think we are ok for now (see details below), however, it would be good to add a big FIXME about supporting the metadata in cases like aliases where it is not currently supported.
I'm confused though about declarations - as noted in my follow on question I see in CloneModule where metadata is being cloned specifically for declarations. Why does that need special handling here?
However, for declarations (since we now support optional importing of declarations):
- GlobalVarSummary attributes are applied during FunctionImportGlobalProcessing::processGlobalForThinLTO, before promotion.
- FunctionSummary attributes are processed in llvm::thinLTOFinalizeInModule, and only for globals originally defined in that module (via the DefinedGlobals map passed in, which was computed from the summaries defined for that specific module).
For aliases:
- On the importing side we only import them as a local copy of the aliasee, which will come with its own guid metadata.
- On the exporting side (i.e. if a reference to a local alias was exported, resulting in the promotion of the alias), I don't think we ever attempt to look up their summary by guid afaict. We typically are only looking up summaries for things like defined functions (e.g. in MemProfContextDisambiguation), and for GVs defined in the main module (e.g. during llvm::thinLTOInternalizeModule, which consults the DefinedGlobals map I mentioned earlier in the context of declarations).
Can you figure out the contexts in which this is being called for declarations without metadata and for aliases, then add a comment (in addition to the suggested FIXME about aliases)?
https://github.com/llvm/llvm-project/pull/184065
More information about the cfe-commits
mailing list