Skip to content

[Concurrency] Provide a Swift interface for custom main and global executors. #79789

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 29 commits into from
Mar 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
090c375
[Concurrency] Swift interface for custom main and global executors.
al45tair Feb 7, 2025
55afa47
[Concurrency] More work on the custom executor implementation.
al45tair Mar 7, 2025
d89ea19
[Concurrency] Add clock traits.
al45tair Mar 10, 2025
5ae6de2
[Concurrency] Fix potential ABI breakages.
al45tair Mar 10, 2025
2298651
[Concurrency] Rename ExecutorJob back to PartialAsyncTask.
al45tair Mar 10, 2025
3a7fc7c
[Concurrency] Remove spurious printf().
al45tair Mar 10, 2025
ef0e09d
[Concurrency][Embedded] Remove MainActor/MainExecutor everywhere.
al45tair Mar 11, 2025
d14b937
[Concurrency] Don't use blocks.
al45tair Mar 11, 2025
b33666c
[Concurrency] Fixes from initial review.
al45tair Mar 11, 2025
fc67cc3
[Concurrency] Mark expressions as `unsafe`.
al45tair Mar 11, 2025
23d0ca7
[Concurrency] Update header file.
al45tair Mar 11, 2025
444bbd5
[Concurrency] Update following pitch comments.
al45tair Mar 12, 2025
00e7ef2
[Concurrency] Remove EventableExecutor, alter asSchedulable.
al45tair Mar 13, 2025
d197f38
[Concurrency] Make `currentExecutor` return a non-optional.
al45tair Mar 13, 2025
f782499
[Concurrency][Tests] Fix baseline-asserts.
al45tair Mar 13, 2025
ff0ce62
[Concurrency][Tests] Linux doesn't have DispatchTime.advanced(by:).
al45tair Mar 13, 2025
869622f
[Concurrency][Tests] Remove spurious line in baseline-asserts.
al45tair Mar 14, 2025
8caa5c5
[Concurrency] Add a missing `public`.
al45tair Mar 14, 2025
a4f79f3
[Concurrency][Linux] Fix WASI build.
al45tair Mar 14, 2025
14b0e73
[Concurrency][Tests] Add missing `traits` symbol to baseline-asserts.
al45tair Mar 14, 2025
f0defd8
[Concurrency] Add CooperativeExecutor, use it.
al45tair Mar 17, 2025
ed08858
[Concurrency] Fix availability.
al45tair Mar 17, 2025
ad5b76a
[Concurrency] Another missing availability annotation.
al45tair Mar 17, 2025
167449f
[Concurrency] Fix a typo and use `var` not `let`.
al45tair Mar 18, 2025
4576d53
[Concurrency][Tests] Require `libdispatch` in the sleep test.
al45tair Mar 18, 2025
60fb31c
[Concurrency][32-bit] Store timestamp out-of-line on 32-bit.
al45tair Mar 18, 2025
fb0396c
[Concurrency] Fall back to allocating from the heap, fix a test.
al45tair Mar 18, 2025
0da95eb
[Concurrency] Fix some warnings, use typed throws.
al45tair Mar 18, 2025
c20aa66
[Concurrency] Disable various things for task-to-thread model.
al45tair Mar 19, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions include/swift/AST/DiagnosticsSIL.def
Original file line number Diff line number Diff line change
Expand Up @@ -1133,6 +1133,14 @@ NOTE(rbi_add_generic_parameter_sendable_conformance,none,
"consider making generic parameter %0 conform to the 'Sendable' protocol",
(Type))

// Concurrency related diagnostics
ERROR(cannot_find_executor_factory_type, none,
"the specified executor factory '%0' could not be found", (StringRef))
ERROR(executor_factory_must_conform, none,
"the executor factory '%0' does not conform to 'ExecutorFactory'", (Type))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick, your call: Maybe better to get the type name passed in?

ERROR(executor_factory_not_supported, none,
"deployment target too low for executor factory specification", ())

//===----------------------------------------------------------------------===//
// MARK: Misc Diagnostics
//===----------------------------------------------------------------------===//
Expand Down
1 change: 1 addition & 0 deletions include/swift/AST/FeatureAvailability.def
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ FEATURE(IsolatedDeinit, (6, 1))

FEATURE(ValueGenericType, (6, 2))
FEATURE(InitRawStructMetadata2, (6, 2))
FEATURE(CustomExecutors, (6, 2))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this is CustomGlobalExecutors? Custom executor sounds like the actor ones hm...

Btw, i noticed TaskExecutor is FUTURE...? That's not right is it? 🤔


FEATURE(TaskExecutor, FUTURE)
FEATURE(Differentiation, FUTURE)
Expand Down
1 change: 1 addition & 0 deletions include/swift/AST/KnownProtocols.def
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ PROTOCOL(Executor)
PROTOCOL(SerialExecutor)
PROTOCOL(TaskExecutor)
PROTOCOL(GlobalActor)
PROTOCOL(ExecutorFactory)

PROTOCOL_(BridgedNSError)
PROTOCOL_(BridgedStoredNSError)
Expand Down
4 changes: 4 additions & 0 deletions include/swift/Basic/LangOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,10 @@ namespace swift {
/// Specifies how strict concurrency checking will be.
StrictConcurrency StrictConcurrencyLevel = StrictConcurrency::Minimal;

/// Specifies the name of the executor factory to use to create the
/// default executors for Swift Concurrency.
std::optional<std::string> ExecutorFactory;

/// Enable experimental concurrency model.
bool EnableExperimentalConcurrency = false;

Expand Down
10 changes: 10 additions & 0 deletions include/swift/Option/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -985,6 +985,16 @@ def strict_concurrency : Joined<["-"], "strict-concurrency=">,
"concurrency model, or 'complete' ('Sendable' and other checking is "
"enabled for all code in the module)">;

def executor_factory : JoinedOrSeparate<["-"], "executor-factory">,
Flags<[FrontendOption]>,
HelpText<"Specify the factory to use to create the default executors for "
"Swift Concurrency. This must be a type conforming to the "
"'ExecutorFactory' protocol.">,
MetaVarName<"<factory-type>">;
def executor_factory_EQ : Joined<["-"], "executor-factory=">,
Flags<[FrontendOption]>,
Alias<executor_factory>;

def enable_experimental_feature :
Separate<["-"], "enable-experimental-feature">,
Flags<[FrontendOption, ModuleInterfaceOption]>,
Expand Down
23 changes: 23 additions & 0 deletions include/swift/Runtime/Concurrency.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,29 @@ SWIFT_EXPORT_FROM(swift_Concurrency)
CoroAllocator *const _swift_coro_malloc_allocator;
// }} TODO: CoroutineAccessors

/// Deallocate memory in a task.
///
/// The pointer provided must be the last pointer allocated on
/// this task that has not yet been deallocated; that is, memory
/// must be allocated and deallocated in a strict stack discipline.
SWIFT_EXPORT_FROM(swift_Concurrency) SWIFT_CC(swift)
void swift_task_dealloc(void *ptr);

/// Allocate memory in a job.
///
/// All allocations will be rounded to a multiple of MAX_ALIGNMENT;
/// if the job does not support allocation, this will return NULL.
SWIFT_EXPORT_FROM(swift_Concurrency) SWIFT_CC(swift)
void *swift_job_allocate(Job *job, size_t size);

/// Deallocate memory in a job.
///
/// The pointer provided must be the last pointer allocated on
/// this task that has not yet been deallocated; that is, memory
/// must be allocated and deallocated in a strict stack discipline.
SWIFT_EXPORT_FROM(swift_Concurrency) SWIFT_CC(swift)
void swift_job_deallocate(Job *job, void *ptr);

/// Cancel a task and all of its child tasks.
///
/// This can be called from any thread.
Expand Down
1 change: 1 addition & 0 deletions lib/AST/ASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1435,6 +1435,7 @@ ProtocolDecl *ASTContext::getProtocol(KnownProtocolKind kind) const {
case KnownProtocolKind::Executor:
case KnownProtocolKind::TaskExecutor:
case KnownProtocolKind::SerialExecutor:
case KnownProtocolKind::ExecutorFactory:
M = getLoadedModule(Id_Concurrency);
break;
case KnownProtocolKind::DistributedActor:
Expand Down
2 changes: 1 addition & 1 deletion lib/ClangImporter/SortedCFDatabase.def.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ with codecs.open(CFDatabaseFile, encoding='utf-8', errors='strict') as f:
continue

# Otherwise, check for lines like FOO(BAR)
m = re.match('^\w+\((\w+)\)', line)
m = re.match(r'^\w+\((\w+)\)', line)
if m:
lineForName[m.group(1)] = line
}%
Expand Down
4 changes: 4 additions & 0 deletions lib/Driver/ToolChains.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,10 @@ void ToolChain::addCommonFrontendArgs(const OutputInfo &OI,
arguments.push_back(inputArgs.MakeArgString(globalRemapping));
}

if (inputArgs.hasArg(options::OPT_executor_factory)) {
inputArgs.AddLastArg(arguments, options::OPT_executor_factory);
}

// Pass through the values passed to -Xfrontend.
inputArgs.AddAllArgValues(arguments, options::OPT_Xfrontend);

Expand Down
6 changes: 6 additions & 0 deletions lib/Frontend/CompilerInvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1314,6 +1314,12 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args,
Opts.enableFeature(Feature::RegionBasedIsolation);
}

// Get the executor factory name
if (const Arg *A = Args.getLastArg(OPT_executor_factory)) {
printf("Got executor-factory option\n");
Opts.ExecutorFactory = A->getValue();
}

Opts.WarnImplicitOverrides =
Args.hasArg(OPT_warn_implicit_overrides);

Expand Down
1 change: 1 addition & 0 deletions lib/IRGen/GenMeta.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7063,6 +7063,7 @@ SpecialProtocol irgen::getSpecialProtocolID(ProtocolDecl *P) {
case KnownProtocolKind::Executor:
case KnownProtocolKind::SerialExecutor:
case KnownProtocolKind::TaskExecutor:
case KnownProtocolKind::ExecutorFactory:
case KnownProtocolKind::Sendable:
case KnownProtocolKind::UnsafeSendable:
case KnownProtocolKind::RangeReplaceableCollection:
Expand Down
44 changes: 44 additions & 0 deletions lib/SILGen/SILGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,10 @@ FuncDecl *SILGenModule::getDeinitOnExecutor() {
return lookupConcurrencyIntrinsic(getASTContext(), "_deinitOnExecutor");
}

FuncDecl *SILGenModule::getCreateExecutors() {
return lookupConcurrencyIntrinsic(getASTContext(), "_createExecutors");
}

FuncDecl *SILGenModule::getExit() {
ASTContext &C = getASTContext();

Expand Down Expand Up @@ -507,6 +511,46 @@ FuncDecl *SILGenModule::getExit() {
return exitFunction;
}

Type SILGenModule::getExecutorFactory() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this maybe getConfiguredExecutorFactory?

auto &ctx = getASTContext();

ModuleDecl *module;

// Parse the executor factory name
StringRef qualifiedName = *ctx.LangOpts.ExecutorFactory;
StringRef typeName;

auto parts = qualifiedName.split('.');

if (parts.second.empty()) {
// This was an unqualified name; assume it's relative to the main module
module = ctx.MainModule;
typeName = qualifiedName;
} else {
Identifier moduleName = ctx.getIdentifier(parts.first);
module = ctx.getModuleByIdentifier(moduleName);
typeName = parts.second;
}

return ctx.getNamedSwiftType(module, typeName);
}

Type SILGenModule::getDefaultExecutorFactory() {
auto &ctx = getASTContext();

ModuleDecl *module = ctx.getModuleByIdentifier(ctx.Id_Concurrency);
if (!module)
return Type();

return ctx.getNamedSwiftType(module, "DefaultExecutorFactory");
}

ProtocolDecl *SILGenModule::getExecutorFactoryProtocol() {
auto &ctx = getASTContext();

return ctx.getProtocol(KnownProtocolKind::ExecutorFactory);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this func? There should be ctx.getExecutorFactoryDecl() generated since you added it to KnownProtocols.def

}

ProtocolConformance *SILGenModule::getNSErrorConformanceToError() {
if (NSErrorConformanceToError)
return *NSErrorConformanceToError;
Expand Down
12 changes: 12 additions & 0 deletions lib/SILGen/SILGen.h
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,18 @@ class LLVM_LIBRARY_VISIBILITY SILGenModule : public ASTVisitor<SILGenModule> {
// Retrieve the _SwiftConcurrencyShims.exit intrinsic.
FuncDecl *getExit();

/// Get the ExecutorFactory type.
Type getExecutorFactory();

/// Get the DefaultExecutorFactory type.
Type getDefaultExecutorFactory();

/// Get the ExecutorFactory protocol.
ProtocolDecl *getExecutorFactoryProtocol();

/// Get the swift_createExecutors function.
FuncDecl *getCreateExecutors();

SILFunction *getKeyPathProjectionCoroutine(bool isReadAccess,
KeyPathTypeKind typeKind);

Expand Down
69 changes: 68 additions & 1 deletion lib/SILGen/SILGenFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ SILGenFunction::getOrCreateScope(const ast_scope::ASTScopeImpl *ASTScope,
// Collapse BraceStmtScopes whose parent is a .*BodyScope.
if (auto Parent = ASTScope->getParent().getPtrOrNull())
if (Parent->getSourceRangeOfThisASTNode() ==
ASTScope->getSourceRangeOfThisASTNode())
ASTScope->getSourceRangeOfThisASTNode())
return cache(getOrCreateScope(Parent, FnScope, InlinedAt));

// The calls to defer closures have cleanup source locations pointing to the
Expand Down Expand Up @@ -1387,6 +1387,28 @@ void SILGenFunction::emitArtificialTopLevel(Decl *mainDecl) {
}
}

static bool isCreateExecutorsFunctionAvailable(SILGenModule &SGM) {
FuncDecl *createExecutorsFuncDecl = SGM.getCreateExecutors();
if (!createExecutorsFuncDecl)
return false;

auto &ctx = createExecutorsFuncDecl->getASTContext();

if (ctx.LangOpts.hasFeature(Feature::Embedded))
return true;

if (!ctx.LangOpts.DisableAvailabilityChecking) {
auto deploymentAvailability = AvailabilityRange::forDeploymentTarget(ctx);
auto runtimeAvailability = AvailabilityRange::forRuntimeTarget(ctx);
auto declAvailability = ctx.getCustomExecutorsAvailability();
auto declRtAvailability = ctx.getCustomExecutorsRuntimeAvailability();
return deploymentAvailability.isContainedIn(declAvailability)
&& runtimeAvailability.isContainedIn(declRtAvailability);
}

return true;
}

void SILGenFunction::emitAsyncMainThreadStart(SILDeclRef entryPoint) {
auto moduleLoc = entryPoint.getAsRegularLocation();
auto *entryBlock = B.getInsertionBB();
Expand All @@ -1402,6 +1424,51 @@ void SILGenFunction::emitAsyncMainThreadStart(SILDeclRef entryPoint) {

B.setInsertionPoint(entryBlock);

// If we're using a new enough deployment target, call swift_createExecutors()
if (ctx.LangOpts.ExecutorFactory) {
if (!isCreateExecutorsFunctionAvailable(SGM)) {
ctx.Diags.diagnose(SourceLoc(), diag::executor_factory_not_supported);
} else {
CanType factoryTy = SGM.getExecutorFactory()->getCanonicalType();

if (!factoryTy) {
ctx.Diags.diagnose(SourceLoc(), diag::cannot_find_executor_factory_type,
*ctx.LangOpts.ExecutorFactory);
}

ProtocolDecl *executorFactoryProtocol = SGM.getExecutorFactoryProtocol();
auto conformance = lookupConformance(factoryTy, executorFactoryProtocol);

if (conformance.isInvalid()) {
// If this type doesn't conform, ignore it and use the default factory
SourceLoc loc = extractNearestSourceLoc(factoryTy);

ctx.Diags.diagnose(loc, diag::executor_factory_must_conform, factoryTy);

factoryTy = SGM.getDefaultExecutorFactory()->getCanonicalType();
conformance = lookupConformance(factoryTy, executorFactoryProtocol);

assert(!conformance.isInvalid());
}

FuncDecl *createExecutorsFuncDecl = SGM.getCreateExecutors();
assert(createExecutorsFuncDecl
&& "Failed to find swift_createExecutors function decl");
SILFunction *createExecutorsSILFunc =
SGM.getFunction(SILDeclRef(createExecutorsFuncDecl, SILDeclRef::Kind::Func),
NotForDefinition);
SILValue createExecutorsFunc =
B.createFunctionRefFor(moduleLoc, createExecutorsSILFunc);
MetatypeType *factoryThickMetaTy
= MetatypeType::get(factoryTy, MetatypeRepresentation::Thick);
SILValue factorySILMetaTy
= B.createMetatype(moduleLoc, getLoweredType(factoryThickMetaTy));
auto ceSubs = SubstitutionMap::getProtocolSubstitutions(
conformance.getRequirement(), factoryTy, conformance);
B.createApply(moduleLoc, createExecutorsFunc, ceSubs, { factorySILMetaTy });
}
}

auto wrapCallArgs = [this, &moduleLoc](SILValue originalValue, FuncDecl *fd,
uint32_t paramIndex) -> SILValue {
Type parameterType = fd->getParameters()->get(paramIndex)->getTypeInContext();
Expand Down
35 changes: 35 additions & 0 deletions stdlib/public/Concurrency/Actor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "../CompatibilityOverride/CompatibilityOverride.h"
#include "swift/ABI/Actor.h"
#include "swift/ABI/Task.h"
#include "ExecutorBridge.h"
#include "TaskPrivate.h"
#include "swift/Basic/HeaderFooterLayout.h"
#include "swift/Basic/PriorityQueue.h"
Expand Down Expand Up @@ -289,6 +290,40 @@ static SerialExecutorRef swift_task_getCurrentExecutorImpl() {
return result;
}

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wreturn-type-c-linkage"

extern "C" SWIFT_CC(swift)
SerialExecutorRef _swift_getActiveExecutor() {
auto currentTracking = ExecutorTrackingInfo::current();
if (currentTracking) {
SerialExecutorRef executor = currentTracking->getActiveExecutor();
// This might be an actor, in which case return nil ("generic")
if (executor.isDefaultActor())
return SerialExecutorRef::generic();
return executor;
}
return swift_getMainExecutor();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this really right? if yes, can you add a comment?

Say we're just on some random thread, there's no executor tracking, we're not on the main executor though -- this seems a bit off?

}

extern "C" SWIFT_CC(swift)
TaskExecutorRef _swift_getCurrentTaskExecutor() {
auto currentTracking = ExecutorTrackingInfo::current();
if (currentTracking)
return currentTracking->getTaskExecutor();
return TaskExecutorRef::undefined();
}

extern "C" SWIFT_CC(swift)
TaskExecutorRef _swift_getPreferredTaskExecutor() {
AsyncTask *task = swift_task_getCurrent();
if (!task)
return TaskExecutorRef::undefined();
return task->getPreferredTaskExecutor();
}

#pragma clang diagnostic pop

/// Determine whether we are currently executing on the main thread
/// independently of whether we know that we are on the main actor.
static bool isExecutingOnMainThread() {
Expand Down
Loading