diff --git a/llvm/lib/Transforms/Utils/FunctionImportUtils.cpp b/llvm/lib/Transforms/Utils/FunctionImportUtils.cpp --- a/llvm/lib/Transforms/Utils/FunctionImportUtils.cpp +++ b/llvm/lib/Transforms/Utils/FunctionImportUtils.cpp @@ -115,16 +115,22 @@ return SGV->getLinkage(); switch (SGV->getLinkage()) { - case GlobalValue::LinkOnceODRLinkage: case GlobalValue::ExternalLinkage: - // External and linkonce definitions are converted to available_externally - // definitions upon import, so that they are available for inlining - // and/or optimization, but are turned into declarations later + case GlobalValue::LinkOnceODRLinkage: + case GlobalValue::WeakODRLinkage: + // External and linkonce_odr definitions are converted to + // available_externally definitions upon import, so that they are available + // for inlining and/or optimization, but are turned into declarations later // during the EliminateAvailableExternally pass. - if (doImportAsDefinition(SGV) && !isa(SGV)) + // + // For weak_odr linkage, there is a guarantee that all copies will be + // equivalent, so the issue described below for weak_any does not exist, + // and the definition can be imported. It can be treated similarly + // to an imported externally visible global value. + if (!SGV->isDeclarationForLinker() && !isa(SGV)) return GlobalValue::AvailableExternallyLinkage; // An imported external declaration stays external. - return SGV->getLinkage(); + return GlobalValue::ExternalLinkage; case GlobalValue::AvailableExternallyLinkage: // An imported available_externally definition converts @@ -145,16 +151,6 @@ // If imported as a declaration, it becomes external_weak. return SGV->getLinkage(); - case GlobalValue::WeakODRLinkage: - // For weak_odr linkage, there is a guarantee that all copies will be - // equivalent, so the issue described above for weak_any does not exist, - // and the definition can be imported. It can be treated similarly - // to an imported externally visible global value. - if (doImportAsDefinition(SGV) && !isa(SGV)) - return GlobalValue::AvailableExternallyLinkage; - else - return GlobalValue::ExternalLinkage; - case GlobalValue::AppendingLinkage: // It would be incorrect to import an appending linkage variable, // since it would cause global constructors/destructors to be diff --git a/llvm/test/ThinLTO/X86/import-dsolocal.ll b/llvm/test/ThinLTO/X86/import-dsolocal.ll new file mode 100644 --- /dev/null +++ b/llvm/test/ThinLTO/X86/import-dsolocal.ll @@ -0,0 +1,85 @@ +; RUN: split-file %s %t +; RUN: opt -module-summary %t/a.ll -o %t/a.bc +; RUN: opt -module-summary %t/b.ll -o %t/b.bc + +;; With a small limit, *_aux are not imported. Check we discard dso_local. +; RUN: llvm-lto2 run %t/a.bc %t/b.bc -o %t1 -save-temps -import-instr-limit=3 \ +; RUN: -r=%t/a.bc,main,plx -r=%t/a.bc,extern, -r=%t/a.bc,linkonceodr, -r=%t/a.bc,weakodr, \ +; RUN: -r=%t/b.bc,a,pl -r=%t/b.bc,b,pl -r=%t/b.bc,extern,pl -r=%t/b.bc,extern_aux,pl \ +; RUN: -r=%t/b.bc,linkonceodr,pl -r=%t/b.bc,linkonceodr_aux,pl -r=%t/b.bc,weakodr,pl -r=%t/b.bc,weakodr_aux,pl +; RUN: llvm-dis %t1.1.3.import.bc -o - | FileCheck %s --check-prefixes=DEST,DEST1 + +;; With a large limit, *_aux are imported. Check we still discard dso_local. +; RUN: llvm-lto2 run %t/a.bc %t/b.bc -o %t2 -save-temps -import-instr-limit=10 \ +; RUN: -r=%t/a.bc,main,plx -r=%t/a.bc,extern, -r=%t/a.bc,linkonceodr, -r=%t/a.bc,weakodr, \ +; RUN: -r=%t/b.bc,a,pl -r=%t/b.bc,b,pl -r=%t/b.bc,extern,pl -r=%t/b.bc,extern_aux,pl \ +; RUN: -r=%t/b.bc,linkonceodr,pl -r=%t/b.bc,linkonceodr_aux,pl -r=%t/b.bc,weakodr,pl -r=%t/b.bc,weakodr_aux,pl +; RUN: llvm-dis %t2.1.3.import.bc -o - | FileCheck %s --check-prefixes=DEST,DEST2 + +; DEST: @a = available_externally global i32 42, align 4 +; DEST-NEXT: @b = external global i32*, align 8 +; DEST: define dso_local i32 @main() +; DEST: define available_externally void @extern() +; DEST1: declare i32 @extern_aux(i32*, i32**) +; DEST2: define available_externally i32 @extern_aux(i32* %a, i32** %b) +; DEST: define available_externally void @weakodr() +; DEST1: declare i32 @weakodr_aux(i32*, i32**) +; DEST2: define available_externally i32 @weakodr_aux(i32* %a, i32** %b) + +;--- a.ll +target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128" +target triple = "x86_64-unknown-linux-gnu" + +declare void @extern() +declare void @linkonceodr() +declare void @weakodr() + +define i32 @main() { + call void @extern() + call void @linkonceodr() + call void @weakodr() + ret i32 0 +} + +;--- b.ll +target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128" +target triple = "x86_64-unknown-linux-gnu" + +@a = dso_local global i32 42, align 4 +@b = dso_local global i32* @a, align 8 + +define dso_local void @extern() { + call i32 @extern_aux(i32* @a, i32** @b) + ret void +} + +define dso_local i32 @extern_aux(i32* %a, i32** %b) { + %p = load i32*, i32** %b, align 8 + store i32 33, i32* %p, align 4 + %v = load i32, i32* %a, align 4 + ret i32 %v +} + +define weak_odr dso_local void @linkonceodr() { + call i32 @linkonceodr_aux(i32* @a, i32** @b) + ret void +} + +define weak_odr i32 @linkonceodr_aux(i32* %a, i32** %b) { + %p = load i32*, i32** %b, align 8 + store i32 33, i32* %p, align 4 + %v = load i32, i32* %a, align 4 + ret i32 %v +} + +define weak_odr dso_local void @weakodr() { + call i32 @weakodr_aux(i32* @a, i32** @b) + ret void +} + +define weak_odr i32 @weakodr_aux(i32* %a, i32** %b) { + %p = load i32*, i32** %b, align 8 + store i32 33, i32* %p, align 4 + %v = load i32, i32* %a, align 4 + ret i32 %v +}