@@ -2487,10 +2487,23 @@ void IRGenModule::emitGlobalDecl(Decl *D) {
24872487Address IRGenModule::getAddrOfSILGlobalVariable (SILGlobalVariable *var,
24882488 const TypeInfo &ti,
24892489 ForDefinition_t forDefinition) {
2490+ LinkEntity entity = LinkEntity::forSILGlobalVariable (var, *this );
2491+ LinkInfo link = LinkInfo::get (*this , entity, forDefinition);
2492+
24902493 if (auto clangDecl = var->getClangDecl ()) {
24912494 auto addr = getAddrOfClangGlobalDecl (cast<clang::VarDecl>(clangDecl),
24922495 forDefinition);
24932496
2497+ // Override the linkage computed by Clang if the decl is from another
2498+ // module that imported @_weakLinked.
2499+ //
2500+ // FIXME: We should be able to set the linkage unconditionally here but
2501+ // some fixes are needed for Cxx interop.
2502+ if (auto globalVar = dyn_cast<llvm::GlobalVariable>(addr)) {
2503+ if (getSwiftModule ()->isImportedAsWeakLinked (var->getDecl ()))
2504+ globalVar->setLinkage (link.getLinkage ());
2505+ }
2506+
24942507 // If we're not emitting this to define it, make sure we cast it to the
24952508 // right type.
24962509 if (!forDefinition) {
@@ -2503,7 +2516,6 @@ Address IRGenModule::getAddrOfSILGlobalVariable(SILGlobalVariable *var,
25032516 return Address (addr, alignment);
25042517 }
25052518
2506- LinkEntity entity = LinkEntity::forSILGlobalVariable (var, *this );
25072519 ResilienceExpansion expansion = getResilienceExpansionForLayout (var);
25082520
25092521 llvm::Type *storageType;
@@ -2549,7 +2561,6 @@ Address IRGenModule::getAddrOfSILGlobalVariable(SILGlobalVariable *var,
25492561
25502562 // Check whether we've created the global variable already.
25512563 // FIXME: We should integrate this into the LinkEntity cache more cleanly.
2552- LinkInfo link = LinkInfo::get (*this , entity, forDefinition);
25532564 auto gvar = Module.getGlobalVariable (link.getName (), /* allowInternal*/ true );
25542565 if (gvar) {
25552566 if (forDefinition)
@@ -3222,6 +3233,7 @@ llvm::Function *IRGenModule::getAddrOfSILFunction(
32223233 }
32233234 }
32243235
3236+ LinkInfo link = LinkInfo::get (*this , entity, forDefinition);
32253237 bool isDefinition = f->isDefinition ();
32263238 bool hasOrderNumber =
32273239 isDefinition && !shouldCallPreviousImplementation;
@@ -3242,8 +3254,16 @@ llvm::Function *IRGenModule::getAddrOfSILFunction(
32423254 if (clangAddr) {
32433255 fn = dyn_cast<llvm::Function>(clangAddr->stripPointerCasts ());
32443256
3245- // If we have a function, move it to the appropriate position.
32463257 if (fn) {
3258+ // Override the linkage computed by Clang if the decl is from another
3259+ // module that imported @_weakLinked.
3260+ //
3261+ // FIXME: We should be able to set the linkage unconditionally here but
3262+ // some fixes are needed for Cxx interop.
3263+ if (!forDefinition && f->isWeakImportedByModule ())
3264+ fn->setLinkage (link.getLinkage ());
3265+
3266+ // If we have a function, move it to the appropriate position.
32473267 if (hasOrderNumber) {
32483268 auto &fnList = Module.getFunctionList ();
32493269 fnList.remove (fn);
@@ -3264,8 +3284,6 @@ llvm::Function *IRGenModule::getAddrOfSILFunction(
32643284 getSignature (f->getLoweredFunctionType (), fpKind);
32653285 addLLVMFunctionAttributes (f, signature);
32663286
3267- LinkInfo link = LinkInfo::get (*this , entity, forDefinition);
3268-
32693287 fn = createFunction (*this , link, signature, insertBefore,
32703288 f->getOptimizationMode (), shouldEmitStackProtector (f));
32713289
0 commit comments