Skip to content

Commit 94b0544

Browse files
committed
[Concurrency] Remove -executor-factory option and replace with magic type.
We decided that using a magic typealias to set the executor factory was better than using a compiler option. Remove the `-executor-factory` option, and replace by looking up the `DefaultExecutorFactory` type, first in the main module, and then if that fails in Concurrency. rdar://149058236
1 parent ecb745e commit 94b0544

File tree

9 files changed

+46
-86
lines changed

9 files changed

+46
-86
lines changed

Diff for: include/swift/AST/DiagnosticsSIL.def

+2-5
Original file line numberDiff line numberDiff line change
@@ -1137,12 +1137,9 @@ NOTE(rbi_add_generic_parameter_sendable_conformance,none,
11371137

11381138
// Concurrency related diagnostics
11391139
ERROR(cannot_find_executor_factory_type, none,
1140-
"the specified executor factory '%0' could not be found", (StringRef))
1140+
"the DefaultExecutorFactory type could not be found", ())
11411141
ERROR(executor_factory_must_conform, none,
1142-
"the executor factory '%0' does not conform to 'ExecutorFactory'",
1143-
(StringRef))
1144-
ERROR(executor_factory_not_supported, none,
1145-
"deployment target too low for executor factory specification", ())
1142+
"the DefaultExecutorFactory does not conform to 'ExecutorFactory'", ())
11461143

11471144
//===----------------------------------------------------------------------===//
11481145
// MARK: Misc Diagnostics

Diff for: include/swift/Basic/LangOptions.h

-4
Original file line numberDiff line numberDiff line change
@@ -409,10 +409,6 @@ namespace swift {
409409
/// Specifies how strict concurrency checking will be.
410410
StrictConcurrency StrictConcurrencyLevel = StrictConcurrency::Minimal;
411411

412-
/// Specifies the name of the executor factory to use to create the
413-
/// default executors for Swift Concurrency.
414-
std::optional<std::string> ExecutorFactory;
415-
416412
/// Enable experimental concurrency model.
417413
bool EnableExperimentalConcurrency = false;
418414

Diff for: include/swift/Option/Options.td

-10
Original file line numberDiff line numberDiff line change
@@ -998,16 +998,6 @@ def default_isolation_EQ : Joined<["-"], "default-isolation=">,
998998
Flags<[FrontendOption]>,
999999
Alias<default_isolation>;
10001000

1001-
def executor_factory : JoinedOrSeparate<["-"], "executor-factory">,
1002-
Flags<[FrontendOption]>,
1003-
HelpText<"Specify the factory to use to create the default executors for "
1004-
"Swift Concurrency. This must be a type conforming to the "
1005-
"'ExecutorFactory' protocol.">,
1006-
MetaVarName<"<factory-type>">;
1007-
def executor_factory_EQ : Joined<["-"], "executor-factory=">,
1008-
Flags<[FrontendOption]>,
1009-
Alias<executor_factory>;
1010-
10111001
def enable_experimental_feature :
10121002
Separate<["-"], "enable-experimental-feature">,
10131003
Flags<[FrontendOption, ModuleInterfaceOption]>,

Diff for: lib/Driver/ToolChains.cpp

-4
Original file line numberDiff line numberDiff line change
@@ -379,10 +379,6 @@ void ToolChain::addCommonFrontendArgs(const OutputInfo &OI,
379379
arguments.push_back(inputArgs.MakeArgString(globalRemapping));
380380
}
381381

382-
if (inputArgs.hasArg(options::OPT_executor_factory)) {
383-
inputArgs.AddLastArg(arguments, options::OPT_executor_factory);
384-
}
385-
386382
// Pass through the values passed to -Xfrontend.
387383
inputArgs.AddAllArgValues(arguments, options::OPT_Xfrontend);
388384

Diff for: lib/Frontend/CompilerInvocation.cpp

-6
Original file line numberDiff line numberDiff line change
@@ -1364,12 +1364,6 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args,
13641364
Opts.enableFeature(Feature::RegionBasedIsolation);
13651365
}
13661366

1367-
// Get the executor factory name
1368-
if (const Arg *A = Args.getLastArg(OPT_executor_factory)) {
1369-
printf("Got executor-factory option\n");
1370-
Opts.ExecutorFactory = A->getValue();
1371-
}
1372-
13731367
Opts.WarnImplicitOverrides =
13741368
Args.hasArg(OPT_warn_implicit_overrides);
13751369

Diff for: lib/SILGen/SILGen.cpp

+6-17
Original file line numberDiff line numberDiff line change
@@ -514,25 +514,14 @@ FuncDecl *SILGenModule::getExit() {
514514
Type SILGenModule::getConfiguredExecutorFactory() {
515515
auto &ctx = getASTContext();
516516

517-
ModuleDecl *module;
517+
// Look in the main module for a typealias
518+
Type factory = ctx.getNamedSwiftType(ctx.MainModule, "DefaultExecutorFactory");
518519

519-
// Parse the executor factory name
520-
StringRef qualifiedName = *ctx.LangOpts.ExecutorFactory;
521-
StringRef typeName;
520+
// If we don't find it, fall back to _Concurrency.PlatformExecutorFactory
521+
if (!factory)
522+
factory = getDefaultExecutorFactory();
522523

523-
auto parts = qualifiedName.split('.');
524-
525-
if (parts.second.empty()) {
526-
// This was an unqualified name; assume it's relative to the main module
527-
module = ctx.MainModule;
528-
typeName = qualifiedName;
529-
} else {
530-
Identifier moduleName = ctx.getIdentifier(parts.first);
531-
module = ctx.getModuleByIdentifier(moduleName);
532-
typeName = parts.second;
533-
}
534-
535-
return ctx.getNamedSwiftType(module, typeName);
524+
return factory;
536525
}
537526

538527
Type SILGenModule::getDefaultExecutorFactory() {

Diff for: lib/SILGen/SILGenFunction.cpp

+30-37
Original file line numberDiff line numberDiff line change
@@ -1424,51 +1424,44 @@ void SILGenFunction::emitAsyncMainThreadStart(SILDeclRef entryPoint) {
14241424

14251425
B.setInsertionPoint(entryBlock);
14261426

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.getConfiguredExecutorFactory()->getCanonicalType();
1427+
// If we're using a new enough deployment target, and we can find a
1428+
// DefaultExecutorFactory type, call swift_createExecutors()
1429+
Type factoryNonCanTy = SGM.getConfiguredExecutorFactory();
14331430

1434-
if (!factoryTy) {
1435-
ctx.Diags.diagnose(SourceLoc(), diag::cannot_find_executor_factory_type,
1436-
*ctx.LangOpts.ExecutorFactory);
1437-
}
1431+
if (isCreateExecutorsFunctionAvailable(SGM) && factoryNonCanTy) {
1432+
CanType factoryTy = factoryNonCanTy->getCanonicalType();
14381433

1439-
ProtocolDecl *executorFactoryProtocol
1440-
= ctx.getProtocol(KnownProtocolKind::ExecutorFactory);
1441-
auto conformance = lookupConformance(factoryTy, executorFactoryProtocol);
1434+
ProtocolDecl *executorFactoryProtocol
1435+
= ctx.getProtocol(KnownProtocolKind::ExecutorFactory);
1436+
auto conformance = lookupConformance(factoryTy, executorFactoryProtocol);
14421437

1443-
if (conformance.isInvalid()) {
1444-
// If this type doesn't conform, ignore it and use the default factory
1445-
SourceLoc loc = extractNearestSourceLoc(factoryTy);
1438+
if (conformance.isInvalid()) {
1439+
// If this type doesn't conform, ignore it and use the default factory
1440+
SourceLoc loc = extractNearestSourceLoc(factoryTy);
14461441

1447-
ctx.Diags.diagnose(loc, diag::executor_factory_must_conform,
1448-
*ctx.LangOpts.ExecutorFactory);
1442+
ctx.Diags.diagnose(loc, diag::executor_factory_must_conform);
14491443

1450-
factoryTy = SGM.getDefaultExecutorFactory()->getCanonicalType();
1451-
conformance = lookupConformance(factoryTy, executorFactoryProtocol);
1444+
factoryTy = SGM.getDefaultExecutorFactory()->getCanonicalType();
1445+
conformance = lookupConformance(factoryTy, executorFactoryProtocol);
14521446

1453-
assert(!conformance.isInvalid());
1454-
}
1447+
assert(!conformance.isInvalid());
1448+
}
14551449

1456-
FuncDecl *createExecutorsFuncDecl = SGM.getCreateExecutors();
1457-
assert(createExecutorsFuncDecl
1458-
&& "Failed to find swift_createExecutors function decl");
1459-
SILFunction *createExecutorsSILFunc =
1460-
SGM.getFunction(SILDeclRef(createExecutorsFuncDecl, SILDeclRef::Kind::Func),
1450+
FuncDecl *createExecutorsFuncDecl = SGM.getCreateExecutors();
1451+
assert(createExecutorsFuncDecl
1452+
&& "Failed to find swift_createExecutors function decl");
1453+
SILFunction *createExecutorsSILFunc =
1454+
SGM.getFunction(SILDeclRef(createExecutorsFuncDecl, SILDeclRef::Kind::Func),
14611455
NotForDefinition);
1462-
SILValue createExecutorsFunc =
1463-
B.createFunctionRefFor(moduleLoc, createExecutorsSILFunc);
1464-
MetatypeType *factoryThickMetaTy
1465-
= MetatypeType::get(factoryTy, MetatypeRepresentation::Thick);
1466-
SILValue factorySILMetaTy
1467-
= B.createMetatype(moduleLoc, getLoweredType(factoryThickMetaTy));
1468-
auto ceSubs = SubstitutionMap::getProtocolSubstitutions(
1469-
conformance.getProtocol(), factoryTy, conformance);
1470-
B.createApply(moduleLoc, createExecutorsFunc, ceSubs, { factorySILMetaTy });
1471-
}
1456+
SILValue createExecutorsFunc =
1457+
B.createFunctionRefFor(moduleLoc, createExecutorsSILFunc);
1458+
MetatypeType *factoryThickMetaTy
1459+
= MetatypeType::get(factoryTy, MetatypeRepresentation::Thick);
1460+
SILValue factorySILMetaTy
1461+
= B.createMetatype(moduleLoc, getLoweredType(factoryThickMetaTy));
1462+
auto ceSubs = SubstitutionMap::getProtocolSubstitutions(
1463+
conformance.getProtocol(), factoryTy, conformance);
1464+
B.createApply(moduleLoc, createExecutorsFunc, ceSubs, { factorySILMetaTy });
14721465
}
14731466

14741467
auto wrapCallArgs = [this, &moduleLoc](SILValue originalValue, FuncDecl *fd,

Diff for: stdlib/public/Concurrency/Executor.swift

+5-2
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,9 @@ public protocol ExecutorFactory {
534534
static var defaultExecutor: any TaskExecutor { get }
535535
}
536536

537+
@available(SwiftStdlib 6.2, *)
538+
typealias DefaultExecutorFactory = PlatformExecutorFactory
539+
537540
@available(SwiftStdlib 6.2, *)
538541
@_silgen_name("swift_createExecutors")
539542
public func _createExecutors<F: ExecutorFactory>(factory: F.Type) {
@@ -556,7 +559,7 @@ extension MainActor {
556559
@available(SwiftStdlib 6.2, *)
557560
public static var executor: any MainExecutor {
558561
if _executor == nil {
559-
_executor = PlatformExecutorFactory.mainExecutor
562+
_executor = DefaultExecutorFactory.mainExecutor
560563
}
561564
return _executor!
562565
}
@@ -575,7 +578,7 @@ extension Task where Success == Never, Failure == Never {
575578
@available(SwiftStdlib 6.2, *)
576579
public static var defaultExecutor: any TaskExecutor {
577580
if _defaultExecutor == nil {
578-
_defaultExecutor = PlatformExecutorFactory.defaultExecutor
581+
_defaultExecutor = DefaultExecutorFactory.defaultExecutor
579582
}
580583
return _defaultExecutor!
581584
}

Diff for: test/Concurrency/Runtime/custom_main_executor.swift

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %target-run-simple-swift(-Xfrontend -disable-availability-checking -g %import-libdispatch -parse-as-library -executor-factory SimpleExecutorFactory) | %FileCheck %s
1+
// RUN: %target-run-simple-swift(-Xfrontend -disable-availability-checking -g %import-libdispatch -parse-as-library) | %FileCheck %s
22

33
// REQUIRES: concurrency
44
// REQUIRES: executable_test
@@ -13,6 +13,8 @@
1313
import StdlibUnittest
1414
import Synchronization
1515

16+
typealias DefaultExecutorFactory = SimpleExecutorFactory
17+
1618
struct SimpleExecutorFactory: ExecutorFactory {
1719
public static var mainExecutor: any MainExecutor {
1820
print("Creating main executor")

0 commit comments

Comments
 (0)