@@ -450,16 +450,51 @@ FuncDecl *SILGenModule::getSwiftJobRun() {
450450
451451FuncDecl *SILGenModule::getExit () {
452452 ASTContext &C = getASTContext ();
453- ModuleDecl *concurrencyShims =
454- C.getModuleByIdentifier (C.getIdentifier (" _SwiftConcurrencyShims" ));
455453
456- if (!concurrencyShims)
457- return nullptr ;
454+ // Try the most likely modules.
455+ Identifier mostLikelyIdentifier;
456+ const llvm::Triple &triple = C.LangOpts .Target ;
457+ if (triple.isOSDarwin ()) {
458+ mostLikelyIdentifier = C.getIdentifier (" Darwin" );
459+ } else if (triple.isOSWASI ()) {
460+ mostLikelyIdentifier = C.getIdentifier (" SwiftWASILibc" );
461+ } else if (triple.isWindowsMSVCEnvironment ()) {
462+ mostLikelyIdentifier = C.getIdentifier (" ucrt" );
463+ } else {
464+ mostLikelyIdentifier = C.getIdentifier (" SwiftGlibc" );
465+ }
466+ ModuleDecl *exitModule = C.getModuleByIdentifier (mostLikelyIdentifier);
467+ FuncDecl *exitFunction = nullptr ;
468+ if (exitModule) {
469+ exitFunction = evaluateOrDefault (
470+ C.evaluator ,
471+ LookupIntrinsicRequest{exitModule, C.getIdentifier (" exit" )},
472+ /* default=*/ nullptr );
473+ }
474+
475+ if (!exitFunction) {
476+ // No go, look for it in any loaded module. Several of the internal
477+ // Swift modules or swift-corelibs modules may have absorbed <stdlib.h>
478+ // when buliding without fully specified clang modules in the OS/SDK.
479+ for (const auto &loadedModuleVector : C.getLoadedModules ()) {
480+ if (loadedModuleVector.first == mostLikelyIdentifier) {
481+ continue ;
482+ }
458483
459- return evaluateOrDefault (
460- C.evaluator ,
461- LookupIntrinsicRequest{concurrencyShims, C.getIdentifier (" exit" )},
462- /* default=*/ nullptr );
484+ ModuleDecl *loadedModule = loadedModuleVector.second ;
485+ if (loadedModule) {
486+ exitFunction = evaluateOrDefault (
487+ C.evaluator ,
488+ LookupIntrinsicRequest{loadedModule, C.getIdentifier (" exit" )},
489+ /* default=*/ nullptr );
490+ }
491+ if (exitFunction) {
492+ break ;
493+ }
494+ }
495+ }
496+
497+ return exitFunction;
463498}
464499
465500ProtocolConformance *SILGenModule::getNSErrorConformanceToError () {
0 commit comments