[llvm] r273692 - Linker: Copy metadata when linking declarations.

Peter Collingbourne via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 24 10:42:22 PDT 2016


Author: pcc
Date: Fri Jun 24 12:42:21 2016
New Revision: 273692

URL: http://llvm.org/viewvc/llvm-project?rev=273692&view=rev
Log:
Linker: Copy metadata when linking declarations.

Differential Revision: http://reviews.llvm.org/D21624

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

Modified: llvm/trunk/include/llvm/IR/GlobalObject.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/IR/GlobalObject.h?rev=273692&r1=273691&r2=273692&view=diff
==============================================================================
--- llvm/trunk/include/llvm/IR/GlobalObject.h (original)
+++ llvm/trunk/include/llvm/IR/GlobalObject.h Fri Jun 24 12:42:21 2016
@@ -114,6 +114,9 @@ public:
   /// 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:

Modified: llvm/trunk/lib/IR/Metadata.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/IR/Metadata.cpp?rev=273692&r1=273691&r2=273692&view=diff
==============================================================================
--- llvm/trunk/lib/IR/Metadata.cpp (original)
+++ llvm/trunk/lib/IR/Metadata.cpp Fri Jun 24 12:42:21 2016
@@ -1393,6 +1393,13 @@ MDNode *GlobalObject::getMetadata(String
   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);
 }

Modified: llvm/trunk/lib/Linker/IRMover.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Linker/IRMover.cpp?rev=273692&r1=273691&r2=273692&view=diff
==============================================================================
--- llvm/trunk/lib/Linker/IRMover.cpp (original)
+++ llvm/trunk/lib/Linker/IRMover.cpp Fri Jun 24 12:42:21 2016
@@ -638,6 +638,12 @@ GlobalValue *IRLinker::copyGlobalValuePr
 
   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.
@@ -961,10 +967,7 @@ Error IRLinker::linkFunctionBody(Functio
     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);

Added: llvm/trunk/test/Linker/metadata-attach.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Linker/metadata-attach.ll?rev=273692&view=auto
==============================================================================
--- llvm/trunk/test/Linker/metadata-attach.ll (added)
+++ llvm/trunk/test/Linker/metadata-attach.ll Fri Jun 24 12:42:21 2016
@@ -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 = !{}




More information about the llvm-commits mailing list