@@ -1072,17 +1072,17 @@ llvm::FunctionType *swift::getRuntimeFnType(llvm::Module &Module,
10721072}
10731073
10741074llvm::Constant *swift::getRuntimeFn (
1075- llvm::Module &Module, llvm::Constant *&cache, const char *name ,
1076- llvm::CallingConv::ID cc, RuntimeAvailability availability ,
1077- llvm::ArrayRef<llvm::Type *> retTypes,
1075+ llvm::Module &Module, llvm::Constant *&cache, const char *ModuleName ,
1076+ const char *FunctionName, llvm::CallingConv::ID cc,
1077+ RuntimeAvailability availability, llvm::ArrayRef<llvm::Type *> retTypes,
10781078 llvm::ArrayRef<llvm::Type *> argTypes, ArrayRef<Attribute::AttrKind> attrs,
10791079 ArrayRef<llvm::MemoryEffects> memEffects, IRGenModule *IGM) {
10801080
10811081 if (cache)
10821082 return cache;
10831083
10841084 bool isWeakLinked = false ;
1085- std::string functionName ( name);
1085+ std::string name (FunctionName );
10861086
10871087 switch (availability) {
10881088 case RuntimeAvailability::AlwaysAvailable:
@@ -1093,7 +1093,7 @@ llvm::Constant *swift::getRuntimeFn(
10931093 break ;
10941094 }
10951095 case RuntimeAvailability::AvailableByCompatibilityLibrary: {
1096- functionName .append (" 50" );
1096+ name .append (" 50" );
10971097 break ;
10981098 }
10991099 }
@@ -1109,7 +1109,7 @@ llvm::Constant *swift::getRuntimeFn(
11091109 {argTypes.begin (), argTypes.end ()},
11101110 /* isVararg*/ false );
11111111
1112- auto addr = Module.getOrInsertFunction (functionName .c_str (), fnTy).getCallee ();
1112+ auto addr = Module.getOrInsertFunction (name .c_str (), fnTy).getCallee ();
11131113 auto fnptr = addr;
11141114 // Strip off any bitcast we might have due to this function being declared of
11151115 // a different type previously.
@@ -1126,12 +1126,20 @@ llvm::Constant *swift::getRuntimeFn(
11261126 (fn->getLinkage () == llvm::GlobalValue::ExternalLinkage &&
11271127 fn->isDeclaration ());
11281128
1129- if (!isStandardLibrary (Module) && IsExternal &&
1130- ::useDllStorage (llvm::Triple(Module.getTargetTriple())))
1131- fn->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass);
1129+ if (IGM && useDllStorage (IGM->Triple ) && IsExternal) {
1130+ bool bIsImported = true ;
1131+ if (IGM->getSwiftModule ()->getPublicModuleName (true ).str () == ModuleName)
1132+ bIsImported = false ;
1133+ else if (ModuleDecl *MD = IGM->Context .getModuleByName (ModuleName))
1134+ bIsImported = !MD->isStaticLibrary ();
11321135
1133- if (IsExternal && isWeakLinked
1134- && !::useDllStorage (llvm::Triple (Module.getTargetTriple ())))
1136+ if (bIsImported)
1137+ fn->setDLLStorageClass (llvm::GlobalValue::DLLImportStorageClass);
1138+ }
1139+
1140+ // Windows does not allow multiple definitions of weak symbols.
1141+ if (IsExternal && isWeakLinked &&
1142+ !llvm::Triple (Module.getTargetTriple ()).isOSWindows ())
11351143 fn->setLinkage (llvm::GlobalValue::ExternalWeakLinkage);
11361144
11371145 llvm::AttrBuilder buildFnAttr (Module.getContext ());
@@ -1165,7 +1173,7 @@ llvm::Constant *swift::getRuntimeFn(
11651173 // This mismatch of attributes would be issue when lowering to WebAssembly.
11661174 // While lowering, LLVM counts how many dummy params are necessary to match
11671175 // callee and caller signature. So we need to add them correctly.
1168- if (functionName == " swift_willThrow" ) {
1176+ if (name == " swift_willThrow" ) {
11691177 assert (IGM && " IGM is required for swift_willThrow." );
11701178 fn->addParamAttr (0 , Attribute::AttrKind::SwiftSelf);
11711179 if (IGM->ShouldUseSwiftError ) {
@@ -1201,10 +1209,10 @@ void IRGenModule::registerRuntimeEffect(ArrayRef<RuntimeEffect> effect,
12011209#define QUOTE (...) __VA_ARGS__
12021210#define STR (X ) #X
12031211
1204- #define FUNCTION (ID, NAME, CC, AVAILABILITY, RETURNS, ARGS, ATTRS, EFFECT , \
1205- MEMEFFECTS) \
1206- FUNCTION_IMPL (ID, NAME, CC, AVAILABILITY, QUOTE(RETURNS), QUOTE(ARGS), \
1207- QUOTE(ATTRS), QUOTE(EFFECT), QUOTE(MEMEFFECTS))
1212+ #define FUNCTION (ID, MODULE, NAME, CC, AVAILABILITY, RETURNS, ARGS, ATTRS, \
1213+ EFFECT, MEMEFFECTS) \
1214+ FUNCTION_IMPL (ID, MODULE, NAME, CC, AVAILABILITY, QUOTE(RETURNS), \
1215+ QUOTE(ARGS), QUOTE( ATTRS), QUOTE(EFFECT), QUOTE(MEMEFFECTS))
12081216
12091217#define RETURNS (...) { __VA_ARGS__ }
12101218#define ARGS (...) { __VA_ARGS__ }
@@ -1217,12 +1225,12 @@ void IRGenModule::registerRuntimeEffect(ArrayRef<RuntimeEffect> effect,
12171225#define MEMEFFECTS (...) \
12181226 { __VA_ARGS__ }
12191227
1220- #define FUNCTION_IMPL (ID, NAME, CC, AVAILABILITY, RETURNS, ARGS, ATTRS, \
1228+ #define FUNCTION_IMPL (ID, MODULE, NAME, CC, AVAILABILITY, RETURNS, ARGS, ATTRS,\
12211229 EFFECT, MEMEFFECTS) \
12221230 llvm::Constant *IRGenModule::get##ID##Fn() { \
12231231 using namespace RuntimeConstants ; \
12241232 registerRuntimeEffect (EFFECT, #NAME); \
1225- return getRuntimeFn (Module, ID##Fn, #NAME, CC, \
1233+ return getRuntimeFn (Module, ID##Fn, #MODULE, # NAME, CC, \
12261234 AVAILABILITY (this ->Context ), RETURNS, ARGS, ATTRS, \
12271235 MEMEFFECTS, this ); \
12281236 } \
@@ -1289,7 +1297,8 @@ IRGenModule::createStringConstant(StringRef Str, bool willBeRelativelyAddressed,
12891297 if (NAME) \
12901298 return NAME; \
12911299 NAME = Module.getOrInsertGlobal (SYM, FullExistentialTypeMetadataStructTy); \
1292- if (!getSwiftModule ()->isStdlibModule ()) \
1300+ if (!getSwiftModule ()->isStdlibModule () || \
1301+ !getSwiftModule ()->isStaticLibrary ()) \
12931302 ApplyIRLinkage (IRLinkage::ExternalImport) \
12941303 .to (cast<llvm::GlobalVariable>(NAME)); \
12951304 return NAME; \
0 commit comments