@@ -1072,17 +1072,17 @@ llvm::FunctionType *swift::getRuntimeFnType(llvm::Module &Module,
1072
1072
}
1073
1073
1074
1074
llvm::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,
1078
1078
llvm::ArrayRef<llvm::Type *> argTypes, ArrayRef<Attribute::AttrKind> attrs,
1079
1079
ArrayRef<llvm::MemoryEffects> memEffects, IRGenModule *IGM) {
1080
1080
1081
1081
if (cache)
1082
1082
return cache;
1083
1083
1084
1084
bool isWeakLinked = false ;
1085
- std::string functionName ( name);
1085
+ std::string name (FunctionName );
1086
1086
1087
1087
switch (availability) {
1088
1088
case RuntimeAvailability::AlwaysAvailable:
@@ -1093,7 +1093,7 @@ llvm::Constant *swift::getRuntimeFn(
1093
1093
break ;
1094
1094
}
1095
1095
case RuntimeAvailability::AvailableByCompatibilityLibrary: {
1096
- functionName .append (" 50" );
1096
+ name .append (" 50" );
1097
1097
break ;
1098
1098
}
1099
1099
}
@@ -1109,7 +1109,7 @@ llvm::Constant *swift::getRuntimeFn(
1109
1109
{argTypes.begin (), argTypes.end ()},
1110
1110
/* isVararg*/ false );
1111
1111
1112
- auto addr = Module.getOrInsertFunction (functionName .c_str (), fnTy).getCallee ();
1112
+ auto addr = Module.getOrInsertFunction (name .c_str (), fnTy).getCallee ();
1113
1113
auto fnptr = addr;
1114
1114
// Strip off any bitcast we might have due to this function being declared of
1115
1115
// a different type previously.
@@ -1126,12 +1126,20 @@ llvm::Constant *swift::getRuntimeFn(
1126
1126
(fn->getLinkage () == llvm::GlobalValue::ExternalLinkage &&
1127
1127
fn->isDeclaration ());
1128
1128
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 ();
1132
1135
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 ())
1135
1143
fn->setLinkage (llvm::GlobalValue::ExternalWeakLinkage);
1136
1144
1137
1145
llvm::AttrBuilder buildFnAttr (Module.getContext ());
@@ -1165,7 +1173,7 @@ llvm::Constant *swift::getRuntimeFn(
1165
1173
// This mismatch of attributes would be issue when lowering to WebAssembly.
1166
1174
// While lowering, LLVM counts how many dummy params are necessary to match
1167
1175
// callee and caller signature. So we need to add them correctly.
1168
- if (functionName == " swift_willThrow" ) {
1176
+ if (name == " swift_willThrow" ) {
1169
1177
assert (IGM && " IGM is required for swift_willThrow." );
1170
1178
fn->addParamAttr (0 , Attribute::AttrKind::SwiftSelf);
1171
1179
if (IGM->ShouldUseSwiftError ) {
@@ -1201,10 +1209,10 @@ void IRGenModule::registerRuntimeEffect(ArrayRef<RuntimeEffect> effect,
1201
1209
#define QUOTE (...) __VA_ARGS__
1202
1210
#define STR (X ) #X
1203
1211
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))
1208
1216
1209
1217
#define RETURNS (...) { __VA_ARGS__ }
1210
1218
#define ARGS (...) { __VA_ARGS__ }
@@ -1217,12 +1225,12 @@ void IRGenModule::registerRuntimeEffect(ArrayRef<RuntimeEffect> effect,
1217
1225
#define MEMEFFECTS (...) \
1218
1226
{ __VA_ARGS__ }
1219
1227
1220
- #define FUNCTION_IMPL (ID, NAME, CC, AVAILABILITY, RETURNS, ARGS, ATTRS, \
1228
+ #define FUNCTION_IMPL (ID, MODULE, NAME, CC, AVAILABILITY, RETURNS, ARGS, ATTRS,\
1221
1229
EFFECT, MEMEFFECTS) \
1222
1230
llvm::Constant *IRGenModule::get##ID##Fn() { \
1223
1231
using namespace RuntimeConstants ; \
1224
1232
registerRuntimeEffect (EFFECT, #NAME); \
1225
- return getRuntimeFn (Module, ID##Fn, #NAME, CC, \
1233
+ return getRuntimeFn (Module, ID##Fn, #MODULE, # NAME, CC, \
1226
1234
AVAILABILITY (this ->Context ), RETURNS, ARGS, ATTRS, \
1227
1235
MEMEFFECTS, this ); \
1228
1236
} \
@@ -1289,7 +1297,8 @@ IRGenModule::createStringConstant(StringRef Str, bool willBeRelativelyAddressed,
1289
1297
if (NAME) \
1290
1298
return NAME; \
1291
1299
NAME = Module.getOrInsertGlobal (SYM, FullExistentialTypeMetadataStructTy); \
1292
- if (!getSwiftModule ()->isStdlibModule ()) \
1300
+ if (!getSwiftModule ()->isStdlibModule () || \
1301
+ !getSwiftModule ()->isStaticLibrary ()) \
1293
1302
ApplyIRLinkage (IRLinkage::ExternalImport) \
1294
1303
.to (cast<llvm::GlobalVariable>(NAME)); \
1295
1304
return NAME; \
0 commit comments