Skip to content

Commit c932f60

Browse files
authored
Merge pull request #21533 from compnerd/excessive-force
IRGen: COMDAT the force load thunks
2 parents 41636dd + e6354fe commit c932f60

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

lib/IRGen/IRGenModule.cpp

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -960,19 +960,25 @@ void IRGenModule::addLinkLibrary(const LinkLibrary &linkLib) {
960960
encodeForceLoadSymbolName(buf, linkLib.getName());
961961
auto ForceImportThunk =
962962
Module.getOrInsertFunction(buf, llvm::FunctionType::get(VoidTy, false));
963-
if (useDllStorage())
964-
cast<llvm::GlobalValue>(ForceImportThunk)
965-
->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass);
963+
ApplyIRLinkage({llvm::GlobalValue::ExternalLinkage,
964+
llvm::GlobalValue::DefaultVisibility,
965+
useDllStorage()
966+
? llvm::GlobalValue::DLLImportStorageClass
967+
: llvm::GlobalValue::DefaultStorageClass})
968+
.to(cast<llvm::GlobalValue>(ForceImportThunk));
966969

967970
buf += "_$";
968971
appendEncodedName(buf, IRGen.Opts.ModuleName);
969972

970973
if (!Module.getGlobalVariable(buf.str())) {
971974
auto ref = new llvm::GlobalVariable(Module, ForceImportThunk->getType(),
972975
/*isConstant=*/true,
973-
llvm::GlobalValue::WeakAnyLinkage,
976+
llvm::GlobalValue::WeakODRLinkage,
974977
ForceImportThunk, buf.str());
975-
ref->setVisibility(llvm::GlobalValue::HiddenVisibility);
978+
ApplyIRLinkage({llvm::GlobalValue::WeakODRLinkage,
979+
llvm::GlobalValue::HiddenVisibility,
980+
llvm::GlobalValue::DefaultStorageClass})
981+
.to(ref);
976982
auto casted = llvm::ConstantExpr::getBitCast(ref, Int8PtrTy);
977983
LLVMUsed.push_back(casted);
978984
}
@@ -1083,9 +1089,12 @@ void IRGenModule::emitAutolinkInfo() {
10831089
llvm::Function::Create(llvm::FunctionType::get(VoidTy, false),
10841090
llvm::GlobalValue::ExternalLinkage, buf,
10851091
&Module);
1086-
if (useDllStorage())
1087-
ForceImportThunk
1088-
->setDLLStorageClass(llvm::GlobalValue::DLLExportStorageClass);
1092+
ApplyIRLinkage({llvm::GlobalValue::ExternalLinkage,
1093+
llvm::GlobalValue::DefaultVisibility,
1094+
useDllStorage()
1095+
? llvm::GlobalValue::DLLExportStorageClass
1096+
: llvm::GlobalValue::DefaultStorageClass})
1097+
.to(ForceImportThunk);
10891098

10901099
auto BB = llvm::BasicBlock::Create(getLLVMContext(), "", ForceImportThunk);
10911100
llvm::IRBuilder<> IRB(BB);

test/Serialization/autolinking.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ import someModule
4545
// FORCE-LOAD-HEX: ret void
4646
// FORCE-LOAD-HEX: }
4747

48-
// FORCE-LOAD-CLIENT: @"_swift_FORCE_LOAD_$_module_$_autolinking" = weak hidden constant void ()* @"_swift_FORCE_LOAD_$_module"
48+
// FORCE-LOAD-CLIENT: @"_swift_FORCE_LOAD_$_module_$_autolinking" = weak_odr hidden constant void ()* @"_swift_FORCE_LOAD_$_module"
4949
// FORCE-LOAD-CLIENT: @llvm.used = appending global [{{[0-9]+}} x i8*] [
5050
// FORCE-LOAD-CLIENT: i8* bitcast (void ()** @"_swift_FORCE_LOAD_$_module_$_autolinking" to i8*)
5151
// FORCE-LOAD-CLIENT: ], section "llvm.metadata"

0 commit comments

Comments
 (0)