[PATCH] D21624: Linker: Copy metadata when linking declarations.

Peter Collingbourne via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 23 12:14:30 PDT 2016


pcc updated this revision to Diff 61709.
pcc added a comment.

Increase stack allocation to 8


http://reviews.llvm.org/D21624

Files:
  include/llvm/IR/GlobalObject.h
  lib/IR/Metadata.cpp
  lib/Linker/IRMover.cpp
  test/Linker/metadata-attach.ll

Index: test/Linker/metadata-attach.ll
===================================================================
--- /dev/null
+++ test/Linker/metadata-attach.ll
@@ -0,0 +1,19 @@
+; RUN: llvm-link %s -S -o - | FileCheck %s
+
+; CHECK: @g1 = global i32 0, !attach !0
+ at g1 = global i32 0, !attach !0
+
+; CHECK: @g2 = external global i32, !attach !0
+ at g2 = external global i32, !attach !0
+
+; CHECK: define void @f1() !attach !0
+define void @f1() !attach !0 {
+  call void @f2()
+  store i32 0, i32* @g2
+  ret void
+}
+
+; CHECK: declare !attach !0 void @f2()
+declare !attach !0 void @f2()
+
+!0 = !{}
Index: lib/Linker/IRMover.cpp
===================================================================
--- lib/Linker/IRMover.cpp
+++ lib/Linker/IRMover.cpp
@@ -637,6 +637,12 @@
 
   NewGV->copyAttributesFrom(SGV);
 
+  if (auto *NewGO = dyn_cast<GlobalObject>(NewGV)) {
+    // Metadata for global variables and function declarations is copied eagerly.
+    if (isa<GlobalVariable>(SGV) || SGV->isDeclaration())
+      NewGO->copyMetadata(cast<GlobalObject>(SGV));
+  }
+
   // Remove these copied constants in case this stays a declaration, since
   // they point to the source module. If the def is linked the values will
   // be mapped in during linkFunctionBody.
@@ -952,10 +958,7 @@
     Dst.setPersonalityFn(Src.getPersonalityFn());
 
   // Copy over the metadata attachments without remapping.
-  SmallVector<std::pair<unsigned, MDNode *>, 8> MDs;
-  Src.getAllMetadata(MDs);
-  for (const auto &I : MDs)
-    Dst.setMetadata(I.first, I.second);
+  Dst.copyMetadata(&Src);
 
   // Steal arguments and splice the body of Src into Dst.
   Dst.stealArgumentListFrom(Src);
Index: lib/IR/Metadata.cpp
===================================================================
--- lib/IR/Metadata.cpp
+++ lib/IR/Metadata.cpp
@@ -1393,6 +1393,13 @@
   return getMetadata(getContext().getMDKindID(Kind));
 }
 
+void GlobalObject::copyMetadata(const GlobalObject *Other) {
+  SmallVector<std::pair<unsigned, MDNode *>, 8> MDs;
+  Other->getAllMetadata(MDs);
+  for (auto &MD : MDs)
+    addMetadata(MD.first, *MD.second);
+}
+
 void Function::setSubprogram(DISubprogram *SP) {
   setMetadata(LLVMContext::MD_dbg, SP);
 }
Index: include/llvm/IR/GlobalObject.h
===================================================================
--- include/llvm/IR/GlobalObject.h
+++ include/llvm/IR/GlobalObject.h
@@ -114,6 +114,9 @@
   /// Erase all metadata attachments with the given kind.
   void eraseMetadata(unsigned KindID);
 
+  /// Copy metadata from Src.
+  void copyMetadata(const GlobalObject *Src);
+
   void copyAttributesFrom(const GlobalValue *Src) override;
 
   // Methods for support type inquiry through isa, cast, and dyn_cast:


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D21624.61709.patch
Type: text/x-patch
Size: 2718 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160623/76a1bec6/attachment.bin>


More information about the llvm-commits mailing list