@@ -486,7 +486,7 @@ SILGenFunction::getOrCreateScope(const ast_scope::ASTScopeImpl *ASTScope,
486
486
// Collapse BraceStmtScopes whose parent is a .*BodyScope.
487
487
if (auto Parent = ASTScope->getParent ().getPtrOrNull ())
488
488
if (Parent->getSourceRangeOfThisASTNode () ==
489
- ASTScope->getSourceRangeOfThisASTNode ())
489
+ ASTScope->getSourceRangeOfThisASTNode ())
490
490
return cache (getOrCreateScope (Parent, FnScope, InlinedAt));
491
491
492
492
// The calls to defer closures have cleanup source locations pointing to the
@@ -1431,6 +1431,28 @@ void SILGenFunction::emitArtificialTopLevel(Decl *mainDecl) {
1431
1431
}
1432
1432
}
1433
1433
1434
+ static bool isCreateExecutorsFunctionAvailable (SILGenModule &SGM) {
1435
+ FuncDecl *createExecutorsFuncDecl = SGM.getCreateExecutors ();
1436
+ if (!createExecutorsFuncDecl)
1437
+ return false ;
1438
+
1439
+ auto &ctx = createExecutorsFuncDecl->getASTContext ();
1440
+
1441
+ if (ctx.LangOpts .hasFeature (Feature::Embedded))
1442
+ return true ;
1443
+
1444
+ if (!ctx.LangOpts .DisableAvailabilityChecking ) {
1445
+ auto deploymentAvailability = AvailabilityRange::forDeploymentTarget (ctx);
1446
+ auto runtimeAvailability = AvailabilityRange::forRuntimeTarget (ctx);
1447
+ auto declAvailability = ctx.getCustomExecutorsAvailability ();
1448
+ auto declRtAvailability = ctx.getCustomExecutorsRuntimeAvailability ();
1449
+ return deploymentAvailability.isContainedIn (declAvailability)
1450
+ && runtimeAvailability.isContainedIn (declRtAvailability);
1451
+ }
1452
+
1453
+ return true ;
1454
+ }
1455
+
1434
1456
void SILGenFunction::emitAsyncMainThreadStart (SILDeclRef entryPoint) {
1435
1457
auto moduleLoc = entryPoint.getAsRegularLocation ();
1436
1458
auto *entryBlock = B.getInsertionBB ();
@@ -1446,6 +1468,53 @@ void SILGenFunction::emitAsyncMainThreadStart(SILDeclRef entryPoint) {
1446
1468
1447
1469
B.setInsertionPoint (entryBlock);
1448
1470
1471
+ // If we're using a new enough deployment target, call swift_createExecutors()
1472
+ if (ctx.LangOpts .ExecutorFactory ) {
1473
+ printf (" Executor factory is %s\n " , ctx.LangOpts .ExecutorFactory ->c_str ());
1474
+
1475
+ if (!isCreateExecutorsFunctionAvailable (SGM)) {
1476
+ ctx.Diags .diagnose (SourceLoc (), diag::executor_factory_not_supported);
1477
+ } else {
1478
+ CanType factoryTy = SGM.getExecutorFactory ()->getCanonicalType ();
1479
+
1480
+ if (!factoryTy) {
1481
+ ctx.Diags .diagnose (SourceLoc (), diag::cannot_find_executor_factory_type,
1482
+ *ctx.LangOpts .ExecutorFactory );
1483
+ }
1484
+
1485
+ ProtocolDecl *executorFactoryProtocol = SGM.getExecutorFactoryProtocol ();
1486
+ auto conformance = lookupConformance (factoryTy, executorFactoryProtocol);
1487
+
1488
+ if (conformance.isInvalid ()) {
1489
+ // If this type doesn't conform, ignore it and use the default factory
1490
+ SourceLoc loc = extractNearestSourceLoc (factoryTy);
1491
+
1492
+ ctx.Diags .diagnose (loc, diag::executor_factory_must_conform, factoryTy);
1493
+
1494
+ factoryTy = SGM.getDefaultExecutorFactory ()->getCanonicalType ();
1495
+ conformance = lookupConformance (factoryTy, executorFactoryProtocol);
1496
+
1497
+ assert (!conformance.isInvalid ());
1498
+ }
1499
+
1500
+ FuncDecl *createExecutorsFuncDecl = SGM.getCreateExecutors ();
1501
+ assert (createExecutorsFuncDecl
1502
+ && " Failed to find swift_createExecutors function decl" );
1503
+ SILFunction *createExecutorsSILFunc =
1504
+ SGM.getFunction (SILDeclRef (createExecutorsFuncDecl, SILDeclRef::Kind::Func),
1505
+ NotForDefinition);
1506
+ SILValue createExecutorsFunc =
1507
+ B.createFunctionRefFor (moduleLoc, createExecutorsSILFunc);
1508
+ MetatypeType *factoryThickMetaTy
1509
+ = MetatypeType::get (factoryTy, MetatypeRepresentation::Thick);
1510
+ SILValue factorySILMetaTy
1511
+ = B.createMetatype (moduleLoc, getLoweredType (factoryThickMetaTy));
1512
+ auto ceSubs = SubstitutionMap::getProtocolSubstitutions (
1513
+ conformance.getRequirement (), factoryTy, conformance);
1514
+ B.createApply (moduleLoc, createExecutorsFunc, ceSubs, { factorySILMetaTy });
1515
+ }
1516
+ }
1517
+
1449
1518
auto wrapCallArgs = [this , &moduleLoc](SILValue originalValue, FuncDecl *fd,
1450
1519
uint32_t paramIndex) -> SILValue {
1451
1520
Type parameterType = fd->getParameters ()->get (paramIndex)->getTypeInContext ();
0 commit comments