Move metadata linking after lazy global materialization/linking.
authorTeresa Johnson <tejohnson@google.com>
Tue, 3 Nov 2015 15:11:27 +0000 (15:11 +0000)
committerTeresa Johnson <tejohnson@google.com>
Tue, 3 Nov 2015 15:11:27 +0000 (15:11 +0000)
Summary:
Currently, named metadata is linked before the LazilyLinkGlobalValues
list is walked and materialized/linked. As a result, references
from DISubprogram and DIGlobalVariable metadata to yet unmaterialized
functions and variables cause them to be added to the lazy linking
list and their definitions are materialized and linked.

This makes the llvm-link -only-needed option not have the intended
effect when debug information is present, as the otherwise unneeded
functions/variables are still linked in.

Additionally, for ThinLTO I have implemented a mechanism to only link
in debug metadata needed by imported functions. Moving named metadata
linking after lazy GV linking will facilitate applying this mechanism
to the LTO and "llvm-link -only-needed" cases as well.

Reviewers: dexonsmith, tra, dblaikie

Subscribers: llvm-commits

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

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@251926 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Linker/LinkModules.cpp
test/Linker/Inputs/only-needed-named-metadata.ll [new file with mode: 0644]
test/Linker/only-needed-named-metadata.ll [new file with mode: 0644]

index b7aab9e964420cb4af2e23a26d2bbaf1dfc5207d..708300fa0c04cfc3d84a7bc92e6e41baf8831ee9 100644 (file)
@@ -1940,15 +1940,6 @@ bool ModuleLinker::run() {
     linkGlobalValueBody(Src);
   }
 
-  // Remap all of the named MDNodes in Src into the DstM module. We do this
-  // after linking GlobalValues so that MDNodes that reference GlobalValues
-  // are properly remapped.
-  linkNamedMDNodes();
-
-  // Merge the module flags into the DstM module.
-  if (linkModuleFlagsMetadata())
-    return true;
-
   // Update the initializers in the DstM module now that all globals that may
   // be referenced are in DstM.
   for (GlobalVariable &Src : SrcM->globals()) {
@@ -1975,6 +1966,15 @@ bool ModuleLinker::run() {
       return true;
   }
 
+  // Remap all of the named MDNodes in Src into the DstM module. We do this
+  // after linking GlobalValues so that MDNodes that reference GlobalValues
+  // are properly remapped.
+  linkNamedMDNodes();
+
+  // Merge the module flags into the DstM module.
+  if (linkModuleFlagsMetadata())
+    return true;
+
   return false;
 }
 
diff --git a/test/Linker/Inputs/only-needed-named-metadata.ll b/test/Linker/Inputs/only-needed-named-metadata.ll
new file mode 100644 (file)
index 0000000..fa7bc2e
--- /dev/null
@@ -0,0 +1,9 @@
+@X = external global i32
+
+declare i32 @foo()
+
+define void @bar() {
+       load i32, i32* @X
+       call i32 @foo()
+       ret void
+}
diff --git a/test/Linker/only-needed-named-metadata.ll b/test/Linker/only-needed-named-metadata.ll
new file mode 100644 (file)
index 0000000..f64637d
--- /dev/null
@@ -0,0 +1,16 @@
+; RUN: llvm-as %S/only-needed-named-metadata.ll -o %t.bc
+; RUN: llvm-as %S/Inputs/only-needed-named-metadata.ll -o %t2.bc
+; RUN: llvm-link -S -only-needed %t2.bc %t.bc | FileCheck %s
+; RUN: llvm-link -S -internalize -only-needed %t2.bc %t.bc | FileCheck %s
+
+; CHECK: @U = external global i32
+; CHECK: declare i32 @unused()
+
+@X = global i32 5
+@U = global i32 6
+define i32 @foo() { ret i32 7 }
+define i32 @unused() { ret i32 8 }
+
+!llvm.named = !{!0, !1}
+!0 = !{i32 ()* @unused}
+!1 = !{i32* @U}