@@ -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
@@ -1387,6 +1387,28 @@ void SILGenFunction::emitArtificialTopLevel(Decl *mainDecl) {
1387
1387
}
1388
1388
}
1389
1389
1390
+ static bool isCreateExecutorsFunctionAvailable(SILGenModule &SGM) {
1391
+ FuncDecl *createExecutorsFuncDecl = SGM.getCreateExecutors();
1392
+ if (!createExecutorsFuncDecl)
1393
+ return false;
1394
+
1395
+ auto &ctx = createExecutorsFuncDecl->getASTContext();
1396
+
1397
+ if (ctx.LangOpts.hasFeature(Feature::Embedded))
1398
+ return true;
1399
+
1400
+ if (!ctx.LangOpts.DisableAvailabilityChecking) {
1401
+ auto deploymentAvailability = AvailabilityRange::forDeploymentTarget(ctx);
1402
+ auto runtimeAvailability = AvailabilityRange::forRuntimeTarget(ctx);
1403
+ auto declAvailability = ctx.getCustomExecutorsAvailability();
1404
+ auto declRtAvailability = ctx.getCustomExecutorsRuntimeAvailability();
1405
+ return deploymentAvailability.isContainedIn(declAvailability)
1406
+ && runtimeAvailability.isContainedIn(declRtAvailability);
1407
+ }
1408
+
1409
+ return true;
1410
+ }
1411
+
1390
1412
void SILGenFunction::emitAsyncMainThreadStart(SILDeclRef entryPoint) {
1391
1413
auto moduleLoc = entryPoint.getAsRegularLocation();
1392
1414
auto *entryBlock = B.getInsertionBB();
@@ -1402,6 +1424,51 @@ void SILGenFunction::emitAsyncMainThreadStart(SILDeclRef entryPoint) {
1402
1424
1403
1425
B.setInsertionPoint(entryBlock);
1404
1426
1427
+ // If we're using a new enough deployment target, call swift_createExecutors()
1428
+ if (ctx.LangOpts.ExecutorFactory) {
1429
+ if (!isCreateExecutorsFunctionAvailable(SGM)) {
1430
+ ctx.Diags.diagnose(SourceLoc(), diag::executor_factory_not_supported);
1431
+ } else {
1432
+ CanType factoryTy = SGM.getExecutorFactory()->getCanonicalType();
1433
+
1434
+ if (!factoryTy) {
1435
+ ctx.Diags.diagnose(SourceLoc(), diag::cannot_find_executor_factory_type,
1436
+ *ctx.LangOpts.ExecutorFactory);
1437
+ }
1438
+
1439
+ ProtocolDecl *executorFactoryProtocol = SGM.getExecutorFactoryProtocol();
1440
+ auto conformance = lookupConformance(factoryTy, executorFactoryProtocol);
1441
+
1442
+ if (conformance.isInvalid()) {
1443
+ // If this type doesn't conform, ignore it and use the default factory
1444
+ SourceLoc loc = extractNearestSourceLoc(factoryTy);
1445
+
1446
+ ctx.Diags.diagnose(loc, diag::executor_factory_must_conform, factoryTy);
1447
+
1448
+ factoryTy = SGM.getDefaultExecutorFactory()->getCanonicalType();
1449
+ conformance = lookupConformance(factoryTy, executorFactoryProtocol);
1450
+
1451
+ assert(!conformance.isInvalid());
1452
+ }
1453
+
1454
+ FuncDecl *createExecutorsFuncDecl = SGM.getCreateExecutors();
1455
+ assert(createExecutorsFuncDecl
1456
+ && "Failed to find swift_createExecutors function decl");
1457
+ SILFunction *createExecutorsSILFunc =
1458
+ SGM.getFunction(SILDeclRef(createExecutorsFuncDecl, SILDeclRef::Kind::Func),
1459
+ NotForDefinition);
1460
+ SILValue createExecutorsFunc =
1461
+ B.createFunctionRefFor(moduleLoc, createExecutorsSILFunc);
1462
+ MetatypeType *factoryThickMetaTy
1463
+ = MetatypeType::get(factoryTy, MetatypeRepresentation::Thick);
1464
+ SILValue factorySILMetaTy
1465
+ = B.createMetatype(moduleLoc, getLoweredType(factoryThickMetaTy));
1466
+ auto ceSubs = SubstitutionMap::getProtocolSubstitutions(
1467
+ conformance.getRequirement(), factoryTy, conformance);
1468
+ B.createApply(moduleLoc, createExecutorsFunc, ceSubs, { factorySILMetaTy });
1469
+ }
1470
+ }
1471
+
1405
1472
auto wrapCallArgs = [this, &moduleLoc](SILValue originalValue, FuncDecl *fd,
1406
1473
uint32_t paramIndex) -> SILValue {
1407
1474
Type parameterType = fd->getParameters()->get(paramIndex)->getTypeInContext();
0 commit comments