[llvm] [IR] Store Metadata attachments in vector (PR #189551)

Alexis Engelke via llvm-commits llvm-commits at lists.llvm.org
Tue Mar 31 06:09:53 PDT 2026


================
@@ -1606,56 +1566,58 @@ MDNode *Value::getMetadata(StringRef Kind) const {
 
 MDNode *Value::getMetadataImpl(unsigned KindID) const {
   const LLVMContext &Ctx = getContext();
-  const MDAttachments &Attachements = Ctx.pImpl->ValueMetadata.at(this);
-  return Attachements.lookup(KindID);
+  unsigned Idx = getMetadataIndex();
+  while (Idx) {
+    const MDAttachment &A = Ctx.pImpl->Metadatas[Idx];
+    if (A.MDKind == KindID)
+      return A.Node;
+    Idx = A.Next;
+  }
+  return nullptr;
 }
 
 void Value::getMetadata(unsigned KindID, SmallVectorImpl<MDNode *> &MDs) const {
-  if (hasMetadata())
-    getContext().pImpl->ValueMetadata.at(this).get(KindID, MDs);
+  const LLVMContext &Ctx = getContext();
+  unsigned Idx = getMetadataIndex();
+  while (Idx) {
+    const MDAttachment &A = Ctx.pImpl->Metadatas[Idx];
+    if (A.MDKind == KindID)
+      MDs.push_back(A.Node);
+    Idx = A.Next;
+  }
+  // We store metadata in reverse order, so reverse for output.
+  std::reverse(MDs.begin(), MDs.end());
----------------
aengelke wrote:

Yes. Instructions shouldn't have multiple metadata of the same kind, so this affects only globals. Tests pass... is there documentation on which metadata kinds exactly occur multiple times?

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


More information about the llvm-commits mailing list