Index: lib/LTO/ThinLTOCodeGenerator.cpp =================================================================== --- lib/LTO/ThinLTOCodeGenerator.cpp +++ lib/LTO/ThinLTOCodeGenerator.cpp @@ -34,7 +34,6 @@ #include "llvm/IR/Mangler.h" #include "llvm/IRReader/IRReader.h" #include "llvm/LTO/LTO.h" -#include "llvm/Linker/Linker.h" #include "llvm/MC/SubtargetFeature.h" #include "llvm/Object/IRObjectFile.h" #include "llvm/Object/ModuleSummaryIndexObjectFile.h" Index: lib/Linker/LinkModules.cpp =================================================================== --- lib/Linker/LinkModules.cpp +++ lib/Linker/LinkModules.cpp @@ -353,8 +353,18 @@ 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(DGV); @@ -573,7 +583,9 @@ if (shouldInternalizeLinkedSymbols()) { for (GlobalValue *GV : ValuesToLink) - Internalize.insert(GV->getName()); + if (!GV->hasAppendingLinkage()) { + Internalize.insert(GV->getName()); + } } // FIXME: Propagate Errors through to the caller instead of emitting Index: test/Linker/Inputs/only-needed-compiler-used.src.ll =================================================================== --- /dev/null +++ test/Linker/Inputs/only-needed-compiler-used.src.ll @@ -0,0 +1,7 @@ +@used1 = global i8 4 +@used2 = global i32 123 + +@llvm.compiler.used = appending global [2 x i8*] [ + i8* @used1, + i8* bitcast (i32* @used2 to i8*) +], section "llvm.metadata" Index: test/Linker/Inputs/only-needed-ctors.src.a.ll =================================================================== --- /dev/null +++ test/Linker/Inputs/only-needed-ctors.src.a.ll @@ -0,0 +1,16 @@ +define internal void @ctor1() { + call void @func1() + ret void +} + +define internal void @ctor2() { + ret void +} + +define void @func1() { + ret void +} + +@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}] Index: test/Linker/Inputs/only-needed-ctors.src.b.ll =================================================================== --- /dev/null +++ test/Linker/Inputs/only-needed-ctors.src.b.ll @@ -0,0 +1,16 @@ +define internal void @ctor3() { + call void @func1() + ret void +} + +define internal void @ctor4() { + ret void +} + +define void @func1() { + ret void +} + +@llvm.global_ctors = appending global[2 x{i32, void() *, i8 * }] [ + {i32, void() *, i8 * } { i32 1, void() *@ctor3, i8 *null}, + {i32, void() *, i8 * } { i32 9, void() *@ctor4, i8 *null}] Index: test/Linker/Inputs/only-needed-dtors.src.a.ll =================================================================== --- /dev/null +++ test/Linker/Inputs/only-needed-dtors.src.a.ll @@ -0,0 +1,16 @@ +define internal void @ctor1() { + call void @func1() + ret void +} + +define internal void @ctor2() { + ret void +} + +define void @func1() { + ret void +} + +@llvm.global_dtors = 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}] Index: test/Linker/Inputs/only-needed-dtors.src.b.ll =================================================================== --- /dev/null +++ test/Linker/Inputs/only-needed-dtors.src.b.ll @@ -0,0 +1,16 @@ +define internal void @ctor3() { + call void @func1() + ret void +} + +define internal void @ctor4() { + ret void +} + +define void @func1() { + ret void +} + +@llvm.global_dtors = appending global[2 x{i32, void() *, i8 * }] [ + {i32, void() *, i8 * } { i32 1, void() *@ctor3, i8 *null}, + {i32, void() *, i8 * } { i32 9, void() *@ctor4, i8 *null}] Index: test/Linker/Inputs/only-needed-used.src.ll =================================================================== --- /dev/null +++ test/Linker/Inputs/only-needed-used.src.ll @@ -0,0 +1,7 @@ +@used1 = global i8 4 +@used2 = global i32 123 + +@llvm.used = appending global [2 x i8*] [ + i8* @used1, + i8* bitcast (i32* @used2 to i8*) +], section "llvm.metadata" Index: test/Linker/only-needed-compiler-used.ll =================================================================== --- /dev/null +++ test/Linker/only-needed-compiler-used.ll @@ -0,0 +1,13 @@ +; RUN: llvm-link -S %s %p/Inputs/only-needed-compiler-used.src.ll | FileCheck %s --check-prefix=CHECK --check-prefix=NO-INTERNALIZE +; RUN: llvm-link -S -internalize %s %p/Inputs/only-needed-compiler-used.src.ll | FileCheck %s --check-prefix=CHECK --check-prefix=INTERNALIZE +; RUN: llvm-link -S -only-needed %s %p/Inputs/only-needed-compiler-used.src.ll | FileCheck %s --check-prefix=CHECK --check-prefix=NO-INTERNALIZE +; RUN: llvm-link -S -only-needed -internalize %s %p/Inputs/only-needed-compiler-used.src.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 Index: test/Linker/only-needed-ctors1.ll =================================================================== --- /dev/null +++ test/Linker/only-needed-ctors1.ll @@ -0,0 +1,13 @@ +; RUN: llvm-link -S %s %p/Inputs/only-needed-ctors.src.a.ll | FileCheck %s --check-prefix=CHECK --check-prefix=NO-INTERNALIZE +; RUN: llvm-link -S -internalize %s %p/Inputs/only-needed-ctors.src.a.ll | FileCheck %s --check-prefix=CHECK --check-prefix=INTERNALIZE +; RUN: llvm-link -S -only-needed %s %p/Inputs/only-needed-ctors.src.a.ll | FileCheck %s --check-prefix=CHECK --check-prefix=NO-INTERNALIZE +; RUN: llvm-link -S -only-needed -internalize %s %p/Inputs/only-needed-ctors.src.a.ll | FileCheck %s --check-prefix=CHECK --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() Index: test/Linker/only-needed-ctors2.ll =================================================================== --- /dev/null +++ test/Linker/only-needed-ctors2.ll @@ -0,0 +1,15 @@ +; RUN: llvm-link -S -internalize %s %p/Inputs/only-needed-ctors.src.a.ll %p/Inputs/only-needed-ctors.src.b.ll | FileCheck %s --check-prefix=CHECK --check-prefix=INTERNALIZE +; RUN: llvm-link -S -only-needed %s %p/Inputs/only-needed-ctors.src.a.ll %p/Inputs/only-needed-ctors.src.b.ll | FileCheck %s --check-prefix=CHECK --check-prefix=NO-INTERNALIZE +; RUN: llvm-link -S -only-needed -internalize %s %p/Inputs/only-needed-ctors.src.a.ll %p/Inputs/only-needed-ctors.src.b.ll | FileCheck %s --check-prefix=CHECK --check-prefix=INTERNALIZE + +; Empty destination module! + + +; CHECK: @llvm.global_ctors = appending global [4 x { i32, void ()*, i8* }] [{ i32, void ()*, i8* } { i32 2, void ()* @ctor1, i8* null }, { i32, void ()*, i8* } { i32 7, void ()* @ctor2, i8* null }, { i32, void ()*, i8* } { i32 1, void ()* @ctor3, i8* null }, { i32, void ()*, i8* } { i32 9, void ()* @ctor4, i8* null }] +; CHECK: define internal void @ctor1() +; CHECK: define internal void @ctor2() +; NO-INTERNALIZE: define void @func1() +; INTERNALIZE: define internal void @func1.{{[0-9]+}}() +; CHECK: define internal void @ctor3() +; CHECK: define internal void @ctor4() +; INTERNALIZE: define internal void @func1() Index: test/Linker/only-needed-ctors3.ll =================================================================== --- /dev/null +++ test/Linker/only-needed-ctors3.ll @@ -0,0 +1,26 @@ +; RUN: llvm-link -S %s %p/Inputs/only-needed-ctors.src.a.ll | FileCheck %s --check-prefix=CHECK --check-prefix=NO-INTERNALIZE +; RUN: llvm-link -S -internalize %s %p/Inputs/only-needed-ctors.src.a.ll | FileCheck %s --check-prefix=CHECK --check-prefix=INTERNALIZE +; RUN: llvm-link -S -only-needed %s %p/Inputs/only-needed-ctors.src.a.ll | FileCheck %s --check-prefix=CHECK --check-prefix=NO-INTERNALIZE +; RUN: llvm-link -S -only-needed -internalize %s %p/Inputs/only-needed-ctors.src.a.ll | FileCheck %s --check-prefix=CHECK --check-prefix=INTERNALIZE + +; Destination module: + +define void @foo() { + ret void +} + +define internal void @ctor1() { + ret void +} + +@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() Index: test/Linker/only-needed-ctors4.ll =================================================================== --- /dev/null +++ test/Linker/only-needed-ctors4.ll @@ -0,0 +1,28 @@ +; RUN: llvm-link -S -internalize %s %p/Inputs/only-needed-ctors.src.a.ll %p/Inputs/only-needed-ctors.src.b.ll | FileCheck %s --check-prefix=CHECK --check-prefix=INTERNALIZE +; RUN: llvm-link -S -only-needed %s %p/Inputs/only-needed-ctors.src.a.ll %p/Inputs/only-needed-ctors.src.b.ll | FileCheck %s --check-prefix=CHECK --check-prefix=NO-INTERNALIZE +; RUN: llvm-link -S -only-needed -internalize %s %p/Inputs/only-needed-ctors.src.a.ll %p/Inputs/only-needed-ctors.src.b.ll | FileCheck %s --check-prefix=CHECK --check-prefix=INTERNALIZE + +; Destination module: + +define void @foo() { + ret void +} + +define internal void @ctor1() { + ret void +} + +@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 [5 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 }, { i32, void ()*, i8* } { i32 1, void ()* @ctor3, i8* null }, { i32, void ()*, i8* } { i32 9, void ()* @ctor4, 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.{{[0-9]+}}() +; CHECK: define internal void @ctor3() +; CHECK: define internal void @ctor4() +; INTERNALIZE: define internal void @func1() Index: test/Linker/only-needed-dtors1.ll =================================================================== --- /dev/null +++ test/Linker/only-needed-dtors1.ll @@ -0,0 +1,13 @@ +; RUN: llvm-link -S %s %p/Inputs/only-needed-dtors.src.a.ll | FileCheck %s --check-prefix=CHECK --check-prefix=NO-INTERNALIZE +; RUN: llvm-link -S -internalize %s %p/Inputs/only-needed-dtors.src.a.ll | FileCheck %s --check-prefix=CHECK --check-prefix=INTERNALIZE +; RUN: llvm-link -S -only-needed %s %p/Inputs/only-needed-dtors.src.a.ll | FileCheck %s --check-prefix=CHECK --check-prefix=NO-INTERNALIZE +; RUN: llvm-link -S -only-needed -internalize %s %p/Inputs/only-needed-dtors.src.a.ll | FileCheck %s --check-prefix=CHECK --check-prefix=INTERNALIZE + +; Empty destination module! + + +; CHECK: @llvm.global_dtors = 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() Index: test/Linker/only-needed-dtors2.ll =================================================================== --- /dev/null +++ test/Linker/only-needed-dtors2.ll @@ -0,0 +1,15 @@ +; RUN: llvm-link -S -internalize %s %p/Inputs/only-needed-dtors.src.a.ll %p/Inputs/only-needed-dtors.src.b.ll | FileCheck %s --check-prefix=CHECK --check-prefix=INTERNALIZE +; RUN: llvm-link -S -only-needed %s %p/Inputs/only-needed-dtors.src.a.ll %p/Inputs/only-needed-dtors.src.b.ll | FileCheck %s --check-prefix=CHECK --check-prefix=NO-INTERNALIZE +; RUN: llvm-link -S -only-needed -internalize %s %p/Inputs/only-needed-dtors.src.a.ll %p/Inputs/only-needed-dtors.src.b.ll | FileCheck %s --check-prefix=CHECK --check-prefix=INTERNALIZE + +; Empty destination module! + + +; CHECK: @llvm.global_dtors = appending global [4 x { i32, void ()*, i8* }] [{ i32, void ()*, i8* } { i32 2, void ()* @ctor1, i8* null }, { i32, void ()*, i8* } { i32 7, void ()* @ctor2, i8* null }, { i32, void ()*, i8* } { i32 1, void ()* @ctor3, i8* null }, { i32, void ()*, i8* } { i32 9, void ()* @ctor4, i8* null }] +; CHECK: define internal void @ctor1() +; CHECK: define internal void @ctor2() +; NO-INTERNALIZE: define void @func1() +; INTERNALIZE: define internal void @func1.{{[0-9]+}}() +; CHECK: define internal void @ctor3() +; CHECK: define internal void @ctor4() +; INTERNALIZE: define internal void @func1() Index: test/Linker/only-needed-dtors3.ll =================================================================== --- /dev/null +++ test/Linker/only-needed-dtors3.ll @@ -0,0 +1,25 @@ +; RUN: llvm-link -S %s %p/Inputs/only-needed-dtors.src.a.ll | FileCheck %s --check-prefix=CHECK +; RUN: llvm-link -S -internalize %s %p/Inputs/only-needed-dtors.src.a.ll | FileCheck %s --check-prefix=CHECK --check-prefix=INTERNALIZE +; RUN: llvm-link -S -only-needed %s %p/Inputs/only-needed-dtors.src.a.ll | FileCheck %s --check-prefix=CHECK +; RUN: llvm-link -S -only-needed -internalize %s %p/Inputs/only-needed-dtors.src.a.ll | FileCheck %s --check-prefix=CHECK --check-prefix=INTERNALIZE + +; Destination module: + +define void @foo() { + ret void +} + +define internal void @ctor1() { + ret void +} + +@llvm.global_dtors = appending global[1 x{i32, void() *, i8 * }] [ + {i32, void() *, i8 * } { i32 4, void() *@ctor1, i8 *null}] + + +; CHECK: @llvm.global_dtors = 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() +; INTERNALIZE: define internal void @func1() Index: test/Linker/only-needed-dtors4.ll =================================================================== --- /dev/null +++ test/Linker/only-needed-dtors4.ll @@ -0,0 +1,28 @@ +; RUN: llvm-link -S -internalize %s %p/Inputs/only-needed-dtors.src.a.ll %p/Inputs/only-needed-dtors.src.b.ll | FileCheck %s --check-prefix=CHECK --check-prefix=INTERNALIZE +; RUN: llvm-link -S -only-needed %s %p/Inputs/only-needed-dtors.src.a.ll %p/Inputs/only-needed-dtors.src.b.ll | FileCheck %s --check-prefix=CHECK --check-prefix=NO-INTERNALIZE +; RUN: llvm-link -S -only-needed -internalize %s %p/Inputs/only-needed-dtors.src.a.ll %p/Inputs/only-needed-dtors.src.b.ll | FileCheck %s --check-prefix=CHECK --check-prefix=INTERNALIZE + +; Destination module: + +define void @foo() { + ret void +} + +define internal void @ctor1() { + ret void +} + +@llvm.global_dtors = appending global[1 x{i32, void() *, i8 * }] [ + {i32, void() *, i8 * } { i32 4, void() *@ctor1, i8 *null}] + + +; CHECK: @llvm.global_dtors = appending global [5 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 }, { i32, void ()*, i8* } { i32 1, void ()* @ctor3, i8* null }, { i32, void ()*, i8* } { i32 9, void ()* @ctor4, 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.{{[0-9]+}}() +; CHECK: define internal void @ctor3() +; CHECK: define internal void @ctor4() +; INTERNALIZE: define internal void @func1() Index: test/Linker/only-needed-used.ll =================================================================== --- /dev/null +++ test/Linker/only-needed-used.ll @@ -0,0 +1,13 @@ +; RUN: llvm-link -S %s %p/Inputs/only-needed-used.src.ll | FileCheck %s --check-prefix=CHECK --check-prefix=NO-INTERNALIZE +; RUN: llvm-link -S -internalize %s %p/Inputs/only-needed-used.src.ll | FileCheck %s --check-prefix=CHECK --check-prefix=INTERNALIZE +; RUN: llvm-link -S -only-needed %s %p/Inputs/only-needed-used.src.ll | FileCheck %s --check-prefix=CHECK --check-prefix=NO-INTERNALIZE +; RUN: llvm-link -S -only-needed -internalize %s %p/Inputs/only-needed-used.src.ll | FileCheck %s --check-prefix=CHECK --check-prefix=INTERNALIZE + +; Empty destination module! + + +; CHECK-DAG: @llvm.used = appending global [2 x i8*] [i8* @used1, i8* bitcast (i32* @used2 to i8*)], section "llvm.metadata" +; INTERNALIZE-DAG: @used1 = internal global i8 4 +; NO-INTERNALIZE-DAG: @used1 = global i8 4 +; INTERNALIZE-DAG: @used2 = internal global i32 123 +; NO-INTERNALIZE-DAG: @used2 = global i32 123