[llvm] r310522 - [Linker] PR33527 - Linker::LinkOnlyNeeded should import AppendingLinkage globals

Benoit Belley via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 9 13:58:39 PDT 2017


Author: belleyb
Date: Wed Aug  9 13:58:39 2017
New Revision: 310522

URL: http://llvm.org/viewvc/llvm-project?rev=310522&view=rev
Log:
[Linker] PR33527 - Linker::LinkOnlyNeeded should import AppendingLinkage globals

Linker::LinkOnlyNeeded should always import globals with
AppendingLinkage.

This resolves PR33527.

Differential Revision: https://reviews.llvm.org/D34448

Added:
    llvm/trunk/test/Linker/Inputs/only-needed-compiler-used.ll
    llvm/trunk/test/Linker/Inputs/only-needed-ctors.ll
    llvm/trunk/test/Linker/Inputs/only-needed-dtors.ll
    llvm/trunk/test/Linker/Inputs/only-needed-used.ll
    llvm/trunk/test/Linker/only-needed-compiler-used.ll
    llvm/trunk/test/Linker/only-needed-ctors1.ll
    llvm/trunk/test/Linker/only-needed-ctors2.ll
    llvm/trunk/test/Linker/only-needed-dtors1.ll
    llvm/trunk/test/Linker/only-needed-dtors2.ll
    llvm/trunk/test/Linker/only-needed-used.ll
Modified:
    llvm/trunk/lib/Linker/LinkModules.cpp

Modified: llvm/trunk/lib/Linker/LinkModules.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Linker/LinkModules.cpp?rev=310522&r1=310521&r2=310522&view=diff
==============================================================================
--- llvm/trunk/lib/Linker/LinkModules.cpp (original)
+++ llvm/trunk/lib/Linker/LinkModules.cpp Wed Aug  9 13:58:39 2017
@@ -329,8 +329,18 @@ bool ModuleLinker::shouldLinkFromSource(
 bool ModuleLinker::linkIfNeeded(GlobalValue &GV) {
   GlobalValue *DGV = getLinkedToGlobal(&GV);
 
-  if (shouldLinkOnlyNeeded() && !(DGV && DGV->isDeclaration()))
-    return false;
+  if (shouldLinkOnlyNeeded()) {
+    // Always import variables with appending linkage.
+    if (!GV.hasAppendingLinkage()) {
+      // Don't import globals unless they are referenced by the destination
+      // module.
+      if (!DGV)
+        return false;
+      // Don't import globals that are already defined in the destination module
+      if (!DGV->isDeclaration())
+        return false;
+    }
+  }
 
   if (DGV && !GV.hasLocalLinkage() && !GV.hasAppendingLinkage()) {
     auto *DGVar = dyn_cast<GlobalVariable>(DGV);

Added: llvm/trunk/test/Linker/Inputs/only-needed-compiler-used.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Linker/Inputs/only-needed-compiler-used.ll?rev=310522&view=auto
==============================================================================
--- llvm/trunk/test/Linker/Inputs/only-needed-compiler-used.ll (added)
+++ llvm/trunk/test/Linker/Inputs/only-needed-compiler-used.ll Wed Aug  9 13:58:39 2017
@@ -0,0 +1,7 @@
+ at used1 = global i8 4
+ at used2 = global i32 123
+
+ at llvm.compiler.used = appending global [2 x i8*] [
+   i8* @used1,
+   i8* bitcast (i32* @used2 to i8*)
+], section "llvm.metadata"

Added: llvm/trunk/test/Linker/Inputs/only-needed-ctors.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Linker/Inputs/only-needed-ctors.ll?rev=310522&view=auto
==============================================================================
--- llvm/trunk/test/Linker/Inputs/only-needed-ctors.ll (added)
+++ llvm/trunk/test/Linker/Inputs/only-needed-ctors.ll Wed Aug  9 13:58:39 2017
@@ -0,0 +1,20 @@
+define internal void @ctor1() {
+  call void @func1()
+  ret void
+}
+
+define internal void @ctor2() {
+  ret void
+}
+
+define void @func1() {
+  ret void
+}
+
+define void @unused() {
+  ret void
+}
+
+ at llvm.global_ctors = appending global[2 x{i32, void() *, i8 * }] [
+    {i32, void() *, i8 * } { i32 2, void() *@ctor1, i8 *null},
+    {i32, void() *, i8 * } { i32 7, void() *@ctor2, i8 *null}]

Added: llvm/trunk/test/Linker/Inputs/only-needed-dtors.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Linker/Inputs/only-needed-dtors.ll?rev=310522&view=auto
==============================================================================
--- llvm/trunk/test/Linker/Inputs/only-needed-dtors.ll (added)
+++ llvm/trunk/test/Linker/Inputs/only-needed-dtors.ll Wed Aug  9 13:58:39 2017
@@ -0,0 +1,20 @@
+define internal void @dtor1() {
+  call void @func1()
+  ret void
+}
+
+define internal void @dtor2() {
+  ret void
+}
+
+define void @func1() {
+  ret void
+}
+
+define void @unused() {
+  ret void
+}
+
+ at llvm.global_dtors = appending global[2 x{i32, void() *, i8 * }] [
+    {i32, void() *, i8 * } { i32 2, void() *@dtor1, i8 *null},
+    {i32, void() *, i8 * } { i32 7, void() *@dtor2, i8 *null}]

Added: llvm/trunk/test/Linker/Inputs/only-needed-used.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Linker/Inputs/only-needed-used.ll?rev=310522&view=auto
==============================================================================
--- llvm/trunk/test/Linker/Inputs/only-needed-used.ll (added)
+++ llvm/trunk/test/Linker/Inputs/only-needed-used.ll Wed Aug  9 13:58:39 2017
@@ -0,0 +1,7 @@
+ at used1 = global i8 4
+ at used2 = global i32 123
+
+ at llvm.used = appending global [2 x i8*] [
+   i8* @used1,
+   i8* bitcast (i32* @used2 to i8*)
+], section "llvm.metadata"

Added: llvm/trunk/test/Linker/only-needed-compiler-used.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Linker/only-needed-compiler-used.ll?rev=310522&view=auto
==============================================================================
--- llvm/trunk/test/Linker/only-needed-compiler-used.ll (added)
+++ llvm/trunk/test/Linker/only-needed-compiler-used.ll Wed Aug  9 13:58:39 2017
@@ -0,0 +1,13 @@
+; RUN: llvm-link -S                           %s %p/Inputs/only-needed-compiler-used.ll | FileCheck %s --check-prefix=CHECK --check-prefix=NO-INTERNALIZE
+; RUN: llvm-link -S              -internalize %s %p/Inputs/only-needed-compiler-used.ll | FileCheck %s --check-prefix=CHECK --check-prefix=INTERNALIZE
+; RUN: llvm-link -S -only-needed              %s %p/Inputs/only-needed-compiler-used.ll | FileCheck %s --check-prefix=CHECK --check-prefix=NO-INTERNALIZE
+; RUN: llvm-link -S -only-needed -internalize %s %p/Inputs/only-needed-compiler-used.ll | FileCheck %s --check-prefix=CHECK --check-prefix=INTERNALIZE
+
+; Empty destination module!
+
+
+; CHECK-DAG:          @llvm.compiler.used = appending global [2 x i8*] [i8* @used1, i8* bitcast (i32* @used2 to i8*)], section "llvm.metadata"
+; NO-INTERNALIZE-DAG: @used1 = global i8 4
+; INTERNALIZE-DAG:    @used1 = internal global i8 4
+; NO-INTERNALIZE-DAG: @used2 = global i32 123
+; INTERNALIZE-DAG:    @used2 = internal global i32 123

Added: llvm/trunk/test/Linker/only-needed-ctors1.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Linker/only-needed-ctors1.ll?rev=310522&view=auto
==============================================================================
--- llvm/trunk/test/Linker/only-needed-ctors1.ll (added)
+++ llvm/trunk/test/Linker/only-needed-ctors1.ll Wed Aug  9 13:58:39 2017
@@ -0,0 +1,15 @@
+; RUN: llvm-link -S                           %s %p/Inputs/only-needed-ctors.ll | FileCheck %s --check-prefix=CHECK --check-prefix=LINK-ALL    --check-prefix=NO-INTERNALIZE
+; RUN: llvm-link -S              -internalize %s %p/Inputs/only-needed-ctors.ll | FileCheck %s --check-prefix=CHECK --check-prefix=LINK_ALL    --check-prefix=INTERNALIZE
+; RUN: llvm-link -S -only-needed              %s %p/Inputs/only-needed-ctors.ll | FileCheck %s --check-prefix=CHECK --check-prefix=ONLY-NEEDED --check-prefix=NO-INTERNALIZE
+; RUN: llvm-link -S -only-needed -internalize %s %p/Inputs/only-needed-ctors.ll | FileCheck %s --check-prefix=CHECK --check-prefix=ONLY-NEEDED --check-prefix=INTERNALIZE
+
+; Empty destination module!
+
+
+; CHECK:           @llvm.global_ctors = appending global [2 x { i32, void ()*, i8* }] [{ i32, void ()*, i8* } { i32 2, void ()* @ctor1, i8* null }, { i32, void ()*, i8* } { i32 7, void ()* @ctor2, i8* null }]
+; CHECK:           define internal void @ctor1()
+; CHECK:           define internal void @ctor2()
+; NO-INTERNALIZE:  define void @func1()
+; INTERNALIZE:     define internal void @func1()
+; LINK-ALL:        define {{(internal )?}}void @unused()
+; ONLY-NEEDED-NOT: void @unused()

Added: llvm/trunk/test/Linker/only-needed-ctors2.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Linker/only-needed-ctors2.ll?rev=310522&view=auto
==============================================================================
--- llvm/trunk/test/Linker/only-needed-ctors2.ll (added)
+++ llvm/trunk/test/Linker/only-needed-ctors2.ll Wed Aug  9 13:58:39 2017
@@ -0,0 +1,28 @@
+; RUN: llvm-link -S                           %s %p/Inputs/only-needed-ctors.ll | FileCheck %s --check-prefix=CHECK --check-prefix=LINK-ALL    --check-prefix=NO-INTERNALIZE
+; RUN: llvm-link -S              -internalize %s %p/Inputs/only-needed-ctors.ll | FileCheck %s --check-prefix=CHECK --check-prefix=LINK_ALL    --check-prefix=INTERNALIZE
+; RUN: llvm-link -S -only-needed              %s %p/Inputs/only-needed-ctors.ll | FileCheck %s --check-prefix=CHECK --check-prefix=ONLY-NEEDED --check-prefix=NO-INTERNALIZE
+; RUN: llvm-link -S -only-needed -internalize %s %p/Inputs/only-needed-ctors.ll | FileCheck %s --check-prefix=CHECK --check-prefix=ONLY-NEEDED --check-prefix=INTERNALIZE
+
+; Destination module:
+
+define void @foo() {
+  ret void
+}
+
+define internal void @ctor1() {
+  ret void
+}
+
+ at llvm.global_ctors = appending global[1 x{i32, void() *, i8 * }] [
+    {i32, void() *, i8 * } { i32 4, void() *@ctor1, i8 *null}]
+
+
+; CHECK:           @llvm.global_ctors = appending global [3 x { i32, void ()*, i8* }] [{ i32, void ()*, i8* } { i32 4, void ()* @ctor1, i8* null }, { i32, void ()*, i8* } { i32 2, void ()* @ctor1.2, i8* null }, { i32, void ()*, i8* } { i32 7, void ()* @ctor2, i8* null }]
+; CHECK:           define internal void @ctor1()
+; CHECK:           define void @foo()
+; CHECK:           define internal void @ctor1.{{[0-9]+}}()
+; CHECK:           define internal void @ctor2()
+; NO-INTERNALIZE:  define void @func1()
+; INTERNALIZE:     define internal void @func1()
+; LINK-ALL:        define {{(internal )?}}void @unused()
+; ONLY-NEEDED-NOT: void @unused()

Added: llvm/trunk/test/Linker/only-needed-dtors1.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Linker/only-needed-dtors1.ll?rev=310522&view=auto
==============================================================================
--- llvm/trunk/test/Linker/only-needed-dtors1.ll (added)
+++ llvm/trunk/test/Linker/only-needed-dtors1.ll Wed Aug  9 13:58:39 2017
@@ -0,0 +1,15 @@
+; RUN: llvm-link -S                           %s %p/Inputs/only-needed-dtors.ll | FileCheck %s --check-prefix=CHECK --check-prefix=LINK-ALL    --check-prefix=NO-INTERNALIZE
+; RUN: llvm-link -S              -internalize %s %p/Inputs/only-needed-dtors.ll | FileCheck %s --check-prefix=CHECK --check-prefix=LINK_ALL    --check-prefix=INTERNALIZE
+; RUN: llvm-link -S -only-needed              %s %p/Inputs/only-needed-dtors.ll | FileCheck %s --check-prefix=CHECK --check-prefix=ONLY-NEEDED --check-prefix=NO-INTERNALIZE
+; RUN: llvm-link -S -only-needed -internalize %s %p/Inputs/only-needed-dtors.ll | FileCheck %s --check-prefix=CHECK --check-prefix=ONLY-NEEDED --check-prefix=INTERNALIZE
+
+; Empty destination module!
+
+
+; CHECK:           @llvm.global_dtors = appending global [2 x { i32, void ()*, i8* }] [{ i32, void ()*, i8* } { i32 2, void ()* @dtor1, i8* null }, { i32, void ()*, i8* } { i32 7, void ()* @dtor2, i8* null }]
+; CHECK:           define internal void @dtor1()
+; CHECK:           define internal void @dtor2()
+; NO-INTERNALIZE:  define void @func1()
+; INTERNALIZE:     define internal void @func1()
+; LINK-ALL:        define {{(internal )?}}void @unused()
+; ONLY-NEEDED-NOT: void @unused()

Added: llvm/trunk/test/Linker/only-needed-dtors2.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Linker/only-needed-dtors2.ll?rev=310522&view=auto
==============================================================================
--- llvm/trunk/test/Linker/only-needed-dtors2.ll (added)
+++ llvm/trunk/test/Linker/only-needed-dtors2.ll Wed Aug  9 13:58:39 2017
@@ -0,0 +1,28 @@
+; RUN: llvm-link -S                           %s %p/Inputs/only-needed-dtors.ll | FileCheck %s --check-prefix=CHECK --check-prefix=LINK-ALL    --check-prefix=NO-INTERNALIZE
+; RUN: llvm-link -S              -internalize %s %p/Inputs/only-needed-dtors.ll | FileCheck %s --check-prefix=CHECK --check-prefix=LINK_ALL    --check-prefix=INTERNALIZE
+; RUN: llvm-link -S -only-needed              %s %p/Inputs/only-needed-dtors.ll | FileCheck %s --check-prefix=CHECK --check-prefix=ONLY-NEEDED --check-prefix=NO-INTERNALIZE
+; RUN: llvm-link -S -only-needed -internalize %s %p/Inputs/only-needed-dtors.ll | FileCheck %s --check-prefix=CHECK --check-prefix=ONLY-NEEDED --check-prefix=INTERNALIZE
+
+; Destination module:
+
+define void @foo() {
+  ret void
+}
+
+define internal void @dtor1() {
+  ret void
+}
+
+ at llvm.global_dtors = appending global[1 x{i32, void() *, i8 * }] [
+    {i32, void() *, i8 * } { i32 4, void() *@dtor1, i8 *null}]
+
+
+; CHECK:           @llvm.global_dtors = appending global [3 x { i32, void ()*, i8* }] [{ i32, void ()*, i8* } { i32 4, void ()* @dtor1, i8* null }, { i32, void ()*, i8* } { i32 2, void ()* @dtor1.2, i8* null }, { i32, void ()*, i8* } { i32 7, void ()* @dtor2, i8* null }]
+; CHECK:           define internal void @dtor1()
+; CHECK:           define void @foo()
+; CHECK:           define internal void @dtor1.{{[0-9]+}}()
+; CHECK:           define internal void @dtor2()
+; NO-INTERNALIZE:  define void @func1()
+; INTERNALIZE:     define internal void @func1()
+; LINK-ALL:        define {{(internal )?}}void @unused()
+; ONLY-NEEDED-NOT: void @unused()

Added: llvm/trunk/test/Linker/only-needed-used.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Linker/only-needed-used.ll?rev=310522&view=auto
==============================================================================
--- llvm/trunk/test/Linker/only-needed-used.ll (added)
+++ llvm/trunk/test/Linker/only-needed-used.ll Wed Aug  9 13:58:39 2017
@@ -0,0 +1,11 @@
+; RUN: llvm-link -S                           %s %p/Inputs/only-needed-used.ll | FileCheck %s
+; RUN: llvm-link -S              -internalize %s %p/Inputs/only-needed-used.ll | FileCheck %s
+; RUN: llvm-link -S -only-needed              %s %p/Inputs/only-needed-used.ll | FileCheck %s
+; RUN: llvm-link -S -only-needed -internalize %s %p/Inputs/only-needed-used.ll | FileCheck %s
+
+; Empty destination module!
+
+
+; CHECK-DAG:          @llvm.used = appending global [2 x i8*] [i8* @used1, i8* bitcast (i32* @used2 to i8*)], section "llvm.metadata"
+; CHECK-DAG: @used1 = global i8 4
+; CHECK-DAG: @used2 = global i32 123




More information about the llvm-commits mailing list