Index: include/llvm/IR/GlobalValue.h =================================================================== --- include/llvm/IR/GlobalValue.h +++ include/llvm/IR/GlobalValue.h @@ -143,12 +143,6 @@ llvm_unreachable("Fully covered switch above!"); } - void maybeSetDsoLocal() { - if (hasLocalLinkage() || - (!hasDefaultVisibility() && !hasExternalWeakLinkage())) - setDSOLocal(true); - } - protected: /// The intrinsic ID for this subclass (which must be a Function). /// @@ -197,6 +191,15 @@ Global, }; + void maybeSetDsoLocal() { + if (hasLocalLinkage() || + (!hasDefaultVisibility() && !hasExternalWeakLinkage())) { + setDSOLocal(true); + if (hasDLLImportStorageClass()) + setDLLStorageClass(DLLStorageClassTypes::DLLExportStorageClass); + } + } + bool hasGlobalUnnamedAddr() const { return getUnnamedAddr() == UnnamedAddr::Global; } Index: lib/Bitcode/Reader/BitcodeReader.cpp =================================================================== --- lib/Bitcode/Reader/BitcodeReader.cpp +++ lib/Bitcode/Reader/BitcodeReader.cpp @@ -2868,13 +2868,6 @@ return Error::success(); } -static void inferDSOLocal(GlobalValue *GV) { - // infer dso_local from linkage and visibility if it is not encoded. - if (GV->hasLocalLinkage() || - (!GV->hasDefaultVisibility() && !GV->hasExternalWeakLinkage())) - GV->setDSOLocal(true); -} - Error BitcodeReader::parseGlobalVarRecord(ArrayRef Record) { // v1: [pointer type, isconst, initid, linkage, alignment, section, // visibility, threadlocal, unnamed_addr, externally_initialized, @@ -2967,7 +2960,7 @@ if (Record.size() > 13) { NewGV->setDSOLocal(getDecodedDSOLocal(Record[13])); } - inferDSOLocal(NewGV); + NewGV->maybeSetDsoLocal(); return Error::success(); } @@ -3056,7 +3049,7 @@ if (Record.size() > 15) { Func->setDSOLocal(getDecodedDSOLocal(Record[15])); } - inferDSOLocal(Func); + Func->maybeSetDsoLocal(); ValueList.push_back(Func); @@ -3133,7 +3126,7 @@ } if (OpNum != Record.size()) NewGA->setDSOLocal(getDecodedDSOLocal(Record[OpNum++])); - inferDSOLocal(NewGA); + NewGA->maybeSetDsoLocal(); ValueList.push_back(NewGA); IndirectSymbolInits.push_back(std::make_pair(NewGA, Val)); Index: lib/LTO/LTO.cpp =================================================================== --- lib/LTO/LTO.cpp +++ lib/LTO/LTO.cpp @@ -691,8 +691,12 @@ } // Set the 'local' flag based on the linker resolution for this symbol. - if (Res.FinalDefinitionInLinkageUnit) + if (Res.FinalDefinitionInLinkageUnit) { GV->setDSOLocal(true); + if (GV->hasDLLImportStorageClass()) + GV->setDLLStorageClass(GlobalValue::DLLStorageClassTypes:: + DLLExportStorageClass); + } } // Common resolution: collect the maximum size/alignment over all commons. // We also record if we see an instance of a common as prevailing, so that Index: lib/Transforms/Utils/FunctionImportUtils.cpp =================================================================== --- lib/Transforms/Utils/FunctionImportUtils.cpp +++ lib/Transforms/Utils/FunctionImportUtils.cpp @@ -211,7 +211,7 @@ if (VI && VI.isDSOLocal()) { GV.setDSOLocal(true); if (GV.hasDLLImportStorageClass()) - GV.setDLLStorageClass(GlobalValue::DefaultStorageClass); + GV.setDLLStorageClass(GlobalValue::DLLExportStorageClass); } } Index: test/LTO/Resolution/X86/local-def-dllimport.ll =================================================================== --- /dev/null +++ test/LTO/Resolution/X86/local-def-dllimport.ll @@ -0,0 +1,32 @@ +; RUN: opt -thinlto-bc -o %t0.bc %s +; RUN: llvm-lto2 run -r %t0.bc,__imp_f,l \ +; RUN: -r %t0.bc,g,p \ +; RUN: -r %t0.bc,g,l \ +; RUN: -r %t0.bc,e,l \ +; RUN: -r %t0.bc,main,x \ +; RUN: -save-temps -o %t1 %t0.bc +; RUN: llvm-dis %t1.1.3.import.bc -o - | FileCheck %s +source_filename = "test.cpp" +target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" +target triple = "x86_64-unknown-linux-gnu" + +$g = comdat any +@g = global i8 42, comdat, !type !0 + +; CHECK: define +; CHECK-SAME: dllexport +; CHECK-SAME: @f +define available_externally dllimport i8* @f() { + ret i8* @g +} + +define i8* @e() { + ret i8* @g +} + +define i32 @main() { + %1 = call i8* @f() + %2 = ptrtoint i8* %1 to i32 + ret i32 %2 +} +!0 = !{i32 0, !"typeid"} Index: test/ThinLTO/X86/dsolocal_dllimport.ll =================================================================== --- test/ThinLTO/X86/dsolocal_dllimport.ll +++ test/ThinLTO/X86/dsolocal_dllimport.ll @@ -3,9 +3,10 @@ ; RUN: llvm-lto2 run %t1.bc %t2.bc -o %t -r=%t1.bc,bar,px -r=%t1.bc,__imp_foo, -r=%t2.bc,foo -save-temps ; RUN: llvm-dis < %t.1.3.import.bc | FileCheck %s -; If a user (dllimport) is LTOed with a library, check that we replace dllimport with dso_local. +; If a user (dllimport) is LTOed with a library, check that we replace dllimport with dllexport and mark +; the function as dso_local. -; CHECK: declare dso_local void @foo() +; CHECK: declare dso_local dllexport void @foo() target datalayout = "e-m:w-i64:64-f80:128-n8:16:32:64-S128" target triple = "x86_64-pc-windows-msvc19.11.0"