Index: include/llvm/Transforms/Utils/FunctionImportUtils.h =================================================================== --- include/llvm/Transforms/Utils/FunctionImportUtils.h +++ include/llvm/Transforms/Utils/FunctionImportUtils.h @@ -21,6 +21,9 @@ namespace llvm { class Module; +// Set to true depending on option -force-import-weak +extern bool ForceImportWeakFlag; + /// Class to handle necessary GlobalValue changes required by ThinLTO /// function importing, including linkage changes and any necessary renaming. class FunctionImportGlobalProcessing { Index: lib/Transforms/IPO/FunctionImport.cpp =================================================================== --- lib/Transforms/IPO/FunctionImport.cpp +++ lib/Transforms/IPO/FunctionImport.cpp @@ -102,6 +102,12 @@ static cl::opt ComputeDead("compute-dead", cl::init(true), cl::Hidden, cl::desc("Compute dead symbols")); +bool llvm::ForceImportWeakFlag; +static cl::opt +ForceImportWeak("force-import-weak", cl::Hidden, + cl::desc("Allow weak functions to be imported"), + cl::location(ForceImportWeakFlag), cl::init(false)); + static cl::opt EnableImportMetadata( "enable-import-metadata", cl::init( #if !defined(NDEBUG) @@ -169,7 +175,7 @@ // filtered out. if (GVSummary->getSummaryKind() == GlobalValueSummary::GlobalVarKind) return false; - if (GlobalValue::isInterposableLinkage(GVSummary->linkage())) + if (!ForceImportWeakFlag && GlobalValue::isInterposableLinkage(GVSummary->linkage())) // There is no point in importing these, we can't inline them return false; if (isa(GVSummary)) Index: lib/Transforms/Utils/FunctionImportUtils.cpp =================================================================== --- lib/Transforms/Utils/FunctionImportUtils.cpp +++ lib/Transforms/Utils/FunctionImportUtils.cpp @@ -151,7 +151,8 @@ // program semantics, since the linker will pick the first weak_any // definition and importing would change the order they are seen by the // linker. The module linking caller needs to enforce this. - assert(!doImportAsDefinition(SGV)); + if(!ForceImportWeakFlag) + assert(!doImportAsDefinition(SGV)); // If imported as a declaration, it becomes external_weak. return SGV->getLinkage(); Index: test/ThinLTO/X86/Inputs/import_weak_type.ll =================================================================== --- /dev/null +++ test/ThinLTO/X86/Inputs/import_weak_type.ll @@ -0,0 +1,7 @@ +target datalayout = "e-p:64:64-p1:64:64-p2:64:64-p3:32:32-p4:32:32-p5:32:32-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:256-v512:512-v1024:1024-v2048:2048-n32:64-A5" +target triple = "amdgcn--amdhsa-hcc" + +define weak void @foo() { + ret void +} + Index: test/ThinLTO/X86/import_weak_type.ll =================================================================== --- /dev/null +++ test/ThinLTO/X86/import_weak_type.ll @@ -0,0 +1,19 @@ +; Do setup work for all below tests: generate bitcode and combined index +; RUN: opt -module-summary %s -o %t.bc +; RUN: opt -module-summary %p/Inputs/import_weak_type.ll -o %t2.bc +; RUN: llvm-lto -thinlto-action=thinlink -o %t3.bc %t.bc %t2.bc + +; Check that we import correctly the imported weak type to replace declaration here +; RUN: llvm-lto -thinlto-action=import %t.bc -thinlto-index=%t3.bc -force-import-weak -o - | llvm-dis -o - | FileCheck %s +; CHECK: define weak void @foo() + + +target datalayout = "e-p:64:64-p1:64:64-p2:64:64-p3:32:32-p4:32:32-p5:32:32-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:256-v512:512-v1024:1024-v2048:2048-n32:64-A5" +target triple = "amdgcn--amdhsa-hcc" + +declare extern_weak void @foo() +define weak_odr amdgpu_kernel void @main() { + call void @foo() + ret void +} +