Skip to content

Commit 701b1c4

Browse files
authored
Merge pull request swiftlang#38021 from DougGregor/centralize-task-creation
Centralize task creation
2 parents eaf604f + 141ae26 commit 701b1c4

30 files changed

+497
-570
lines changed

include/swift/ABI/MetadataValues.h

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2022,6 +2022,43 @@ enum class JobPriority : size_t {
20222022
Unspecified = 0x00,
20232023
};
20242024

2025+
/// Flags for task creation.
2026+
class TaskCreateFlags : public FlagSet<size_t> {
2027+
public:
2028+
enum {
2029+
Priority = 0,
2030+
Priority_width = 8,
2031+
2032+
Task_IsChildTask = 8,
2033+
// bit 9 is unused
2034+
Task_CopyTaskLocals = 10,
2035+
Task_InheritContext = 11,
2036+
Task_EnqueueJob = 12,
2037+
Task_AddPendingGroupTaskUnconditionally = 13,
2038+
};
2039+
2040+
explicit constexpr TaskCreateFlags(size_t bits) : FlagSet(bits) {}
2041+
constexpr TaskCreateFlags() {}
2042+
2043+
FLAGSET_DEFINE_FIELD_ACCESSORS(Priority, Priority_width, JobPriority,
2044+
getPriority, setPriority)
2045+
FLAGSET_DEFINE_FLAG_ACCESSORS(Task_IsChildTask,
2046+
isChildTask,
2047+
setIsChildTask)
2048+
FLAGSET_DEFINE_FLAG_ACCESSORS(Task_CopyTaskLocals,
2049+
copyTaskLocals,
2050+
setCopyTaskLocals)
2051+
FLAGSET_DEFINE_FLAG_ACCESSORS(Task_InheritContext,
2052+
inheritContext,
2053+
setInheritContext)
2054+
FLAGSET_DEFINE_FLAG_ACCESSORS(Task_EnqueueJob,
2055+
enqueueJob,
2056+
setEnqueueJob)
2057+
FLAGSET_DEFINE_FLAG_ACCESSORS(Task_AddPendingGroupTaskUnconditionally,
2058+
addPendingGroupTaskUnconditionally,
2059+
setAddPendingGroupTaskUnconditionally)
2060+
};
2061+
20252062
/// Flags for schedulable jobs.
20262063
class JobFlags : public FlagSet<uint32_t> {
20272064
public:
@@ -2039,7 +2076,7 @@ class JobFlags : public FlagSet<uint32_t> {
20392076
Task_IsChildTask = 24,
20402077
Task_IsFuture = 25,
20412078
Task_IsGroupChildTask = 26,
2042-
Task_IsContinuingAsyncTask = 27,
2079+
// 27 is currently unused
20432080
Task_IsAsyncLetTask = 28,
20442081
};
20452082

@@ -2070,9 +2107,6 @@ class JobFlags : public FlagSet<uint32_t> {
20702107
FLAGSET_DEFINE_FLAG_ACCESSORS(Task_IsGroupChildTask,
20712108
task_isGroupChildTask,
20722109
task_setIsGroupChildTask)
2073-
FLAGSET_DEFINE_FLAG_ACCESSORS(Task_IsContinuingAsyncTask,
2074-
task_isContinuingAsyncTask,
2075-
task_setIsContinuingAsyncTask)
20762110
FLAGSET_DEFINE_FLAG_ACCESSORS(Task_IsAsyncLetTask,
20772111
task_isAsyncLetTask,
20782112
task_setIsAsyncLetTask)
@@ -2111,6 +2145,8 @@ enum class TaskOptionRecordKind : uint8_t {
21112145
Executor = 0,
21122146
/// Request a child task to be part of a specific task group.
21132147
TaskGroup = 1,
2148+
/// Request a child task for an 'async let'.
2149+
AsyncLet = 2,
21142150
};
21152151

21162152
/// Flags for cancellation records.

include/swift/ABI/TaskOptions.h

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "swift/ABI/MetadataValues.h"
2525
#include "swift/Runtime/Config.h"
2626
#include "swift/Basic/STLExtras.h"
27+
#include "llvm/Support/Casting.h"
2728

2829
namespace swift {
2930

@@ -34,14 +35,12 @@ namespace swift {
3435
/// to configure a newly spawned task.
3536
class TaskOptionRecord {
3637
public:
37-
TaskOptionRecordFlags Flags;
38+
const TaskOptionRecordFlags Flags;
3839
TaskOptionRecord *Parent;
3940

4041
TaskOptionRecord(TaskOptionRecordKind kind,
4142
TaskOptionRecord *parent = nullptr)
42-
: Flags(kind) {
43-
Parent = parent;
44-
}
43+
: Flags(kind), Parent(parent) { }
4544

4645
TaskOptionRecord(const TaskOptionRecord &) = delete;
4746
TaskOptionRecord &operator=(const TaskOptionRecord &) = delete;
@@ -53,15 +52,14 @@ class TaskOptionRecord {
5352
TaskOptionRecord *getParent() const {
5453
return Parent;
5554
}
56-
5755
};
5856

5957
/******************************************************************************/
6058
/****************************** TASK OPTIONS **********************************/
6159
/******************************************************************************/
6260

6361
class TaskGroupTaskOptionRecord : public TaskOptionRecord {
64-
TaskGroup *Group;
62+
TaskGroup * const Group;
6563

6664
public:
6765
TaskGroupTaskOptionRecord(TaskGroup *group)
@@ -71,6 +69,10 @@ class TaskGroupTaskOptionRecord : public TaskOptionRecord {
7169
TaskGroup *getGroup() const {
7270
return Group;
7371
}
72+
73+
static bool classof(const TaskOptionRecord *record) {
74+
return record->getKind() == TaskOptionRecordKind::TaskGroup;
75+
}
7476
};
7577

7678

@@ -80,16 +82,38 @@ class TaskGroupTaskOptionRecord : public TaskOptionRecord {
8082
/// executor should be used instead, most often this may mean the global
8183
/// concurrent executor, or the enclosing actor's executor.
8284
class ExecutorTaskOptionRecord : public TaskOptionRecord {
83-
ExecutorRef *Executor;
85+
const ExecutorRef Executor;
8486

8587
public:
86-
ExecutorTaskOptionRecord(ExecutorRef *executor)
88+
ExecutorTaskOptionRecord(ExecutorRef executor)
8789
: TaskOptionRecord(TaskOptionRecordKind::Executor),
8890
Executor(executor) {}
8991

90-
ExecutorRef *getExecutor() const {
92+
ExecutorRef getExecutor() const {
9193
return Executor;
9294
}
95+
96+
static bool classof(const TaskOptionRecord *record) {
97+
return record->getKind() == TaskOptionRecordKind::Executor;
98+
}
99+
};
100+
101+
/// Task option to specify that the created task is for an 'async let'.
102+
class AsyncLetTaskOptionRecord : public TaskOptionRecord {
103+
AsyncLet *asyncLet;
104+
105+
public:
106+
AsyncLetTaskOptionRecord(AsyncLet *asyncLet)
107+
: TaskOptionRecord(TaskOptionRecordKind::AsyncLet),
108+
asyncLet(asyncLet) {}
109+
110+
AsyncLet *getAsyncLet() const {
111+
return asyncLet;
112+
}
113+
114+
static bool classof(const TaskOptionRecord *record) {
115+
return record->getKind() == TaskOptionRecordKind::AsyncLet;
116+
}
93117
};
94118

95119
} // end namespace swift

include/swift/AST/Builtins.def

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -805,28 +805,26 @@ BUILTIN_MISC_OPERATION(StartAsyncLet, "startAsyncLet", "", Special)
805805
/// until the endAsyncLet.
806806
BUILTIN_MISC_OPERATION_WITH_SILGEN(EndAsyncLet, "endAsyncLet", "", Special)
807807

808-
/// createAsyncTaskFuture(): (
809-
/// Int, // flags
810-
/// Builtin.RawPointer?, // options (TaskOptionRecord*)
808+
/// createAsyncTask(): (
809+
/// Int, // task-creation flags
811810
/// @escaping () async throws -> T // function
812811
/// ) -> Builtin.NativeObject
813812
///
814-
/// Create a new asynchronous task future, given flags, an (optional) parent
815-
/// task and a function to execute.
816-
BUILTIN_MISC_OPERATION_WITH_SILGEN(CreateAsyncTaskFuture,
817-
"createAsyncTaskFuture", "", Special)
813+
/// Create a new asynchronous task, given flags, options, and a function to
814+
/// execute.
815+
BUILTIN_MISC_OPERATION_WITH_SILGEN(CreateAsyncTask,
816+
"createAsyncTask", "", Special)
818817

819-
/// createAsyncTaskGroupFuture(): (
818+
/// createAsyncTaskInGroup(): (
820819
/// Int, // flags
821-
/// Builtin.RawPointer?, // group
822-
/// Builtin.RawPointer?, // options (TaskOptionRecord*)
820+
/// Builtin.RawPointer, // group
823821
/// @escaping () async throws -> T // function
824822
/// ) -> Builtin.NativeObject
825823
///
826824
/// Create a new asynchronous task future, given flags, a parent task,
827825
/// task group and a function to execute.
828-
BUILTIN_MISC_OPERATION_WITH_SILGEN(CreateAsyncTaskGroupFuture,
829-
"createAsyncTaskGroupFuture", "", Special)
826+
BUILTIN_MISC_OPERATION_WITH_SILGEN(CreateAsyncTaskInGroup,
827+
"createAsyncTaskInGroup", "", Special)
830828

831829
/// globalStringTablePointer has type String -> Builtin.RawPointer.
832830
/// It returns an immortal, global string table pointer for strings constructed

include/swift/Basic/Features.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,5 +52,6 @@ LANGUAGE_FEATURE(InheritActorContext, 0, "@_inheritActorContext attribute", true
5252
LANGUAGE_FEATURE(ImplicitSelfCapture, 0, "@_implicitSelfCapture attribute", true)
5353
LANGUAGE_FEATURE(BuiltinBuildExecutor, 0, "Executor-building builtins", true)
5454
LANGUAGE_FEATURE(BuiltinBuildMainExecutor, 0, "MainActor executor building builtin", true)
55+
LANGUAGE_FEATURE(BuiltinCreateAsyncTaskInGroup, 0, "MainActor executor building builtin", true)
5556

5657
#undef LANGUAGE_FEATURE

include/swift/Runtime/Concurrency.h

Lines changed: 11 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -36,53 +36,27 @@ struct AsyncTaskAndContext {
3636
AsyncContext *InitialContext;
3737
};
3838

39-
/// Create a task object with no future which will run the given function.
39+
/// Create a task object.
4040
SWIFT_EXPORT_FROM(swift_Concurrency) SWIFT_CC(swift)
41-
AsyncTaskAndContext swift_task_create_f(
42-
size_t flags,
41+
AsyncTaskAndContext swift_task_create(
42+
size_t taskCreateFlags,
4343
TaskOptionRecord *options,
44-
ThinNullaryAsyncSignature::FunctionType *function, size_t initialContextSize);
44+
const Metadata *futureResultType,
45+
void *closureEntry, HeapObject *closureContext);
4546

4647
/// Caution: not all future-initializing functions actually throw, so
4748
/// this signature may be incorrect.
4849
using FutureAsyncSignature =
4950
AsyncSignature<void(void*), /*throws*/ true>;
5051

51-
/// Create a task object with a future which will run the given closure.
52-
SWIFT_EXPORT_FROM(swift_Concurrency) SWIFT_CC(swift)
53-
AsyncTaskAndContext swift_task_create_future(
54-
size_t flags,
55-
TaskOptionRecord *options,
56-
const Metadata *futureResultType,
57-
void *closureEntryPoint, HeapObject * /* +1 */ closureContext);
58-
59-
/// Create a task object with a future which will run the given function.
60-
SWIFT_EXPORT_FROM(swift_Concurrency) SWIFT_CC(swift)
61-
AsyncTaskAndContext swift_task_create_future_f(
62-
size_t flags,
63-
TaskOptionRecord *options,
64-
const Metadata *futureResultType,
65-
FutureAsyncSignature::FunctionType *function, size_t initialContextSize);
66-
67-
/// Create a task object with a future which will run the given
68-
/// closure, and offer its result to the task group
69-
SWIFT_EXPORT_FROM(swift_Concurrency) SWIFT_CC(swift)
70-
AsyncTaskAndContext swift_task_create_group_future(
71-
size_t flags,
72-
TaskGroup *group,
73-
TaskOptionRecord *options,
74-
const Metadata *futureResultType,
75-
void *closureEntryPoint, HeapObject * /* +1 */ closureContext);
76-
77-
/// Create a task object with a future which will run the given
78-
/// function, and offer its result to the task group
52+
/// Create a task object.
7953
SWIFT_EXPORT_FROM(swift_Concurrency) SWIFT_CC(swift)
80-
AsyncTaskAndContext swift_task_create_group_future_f(
81-
size_t flags,
82-
TaskGroup *group,
54+
AsyncTaskAndContext swift_task_create_common(
55+
size_t taskCreateFlags,
8356
TaskOptionRecord *options,
8457
const Metadata *futureResultType,
85-
FutureAsyncSignature::FunctionType *function, size_t initialContextSize);
58+
FutureAsyncSignature::FunctionType *function, void *closureContext,
59+
size_t initialContextSize);
8660

8761
/// Allocate memory in a task.
8862
///
@@ -279,7 +253,7 @@ SWIFT_EXPORT_FROM(swift_Concurrency) SWIFT_CC(swift)
279253
void swift_asyncLet_start(AsyncLet *alet,
280254
TaskOptionRecord *options,
281255
const Metadata *futureResultType,
282-
void *closureEntryPoint, void *closureContext);
256+
void *closureEntryPoint, HeapObject *closureContext);
283257

284258
/// This matches the ABI of a closure `<T>(Builtin.RawPointer) async -> T`
285259
using AsyncLetWaitSignature =

include/swift/Runtime/RuntimeFunctions.def

Lines changed: 5 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1547,88 +1547,22 @@ FUNCTION(TaskCancel,
15471547
ARGS(SwiftTaskPtrTy),
15481548
ATTRS(NoUnwind, ArgMemOnly))
15491549

1550-
// AsyncTaskAndContext swift_task_create_f(
1551-
// size_t flags,
1550+
// AsyncTaskAndContext swift_task_create(
1551+
// size_t taskCreateFlags,
15521552
// TaskOptionRecord *options,
1553-
// TaskContinuationFunction* function, size_t contextSize);
1554-
FUNCTION(TaskCreateFunc,
1555-
swift_task_create_f, SwiftCC,
1556-
ConcurrencyAvailability,
1557-
RETURNS(AsyncTaskAndContextTy),
1558-
ARGS(SizeTy,
1559-
SwiftTaskOptionRecordPtrTy,
1560-
TaskContinuationFunctionPtrTy,
1561-
SizeTy),
1562-
ATTRS(NoUnwind, ArgMemOnly))
1563-
1564-
// AsyncTaskAndContext swift_task_create_future(
1565-
// size_t flags,
1566-
// TaskOptionRecord *options,
1567-
// const Metadata *futureResultType,
1568-
// void *closureEntry, HeapObject *closureContext);
1569-
FUNCTION(TaskCreateFuture,
1570-
swift_task_create_future, SwiftCC,
1571-
ConcurrencyAvailability,
1572-
RETURNS(AsyncTaskAndContextTy),
1573-
ARGS(SizeTy,
1574-
SwiftTaskOptionRecordPtrTy,
1575-
TypeMetadataPtrTy,
1576-
Int8PtrTy,
1577-
RefCountedPtrTy),
1578-
ATTRS(NoUnwind, ArgMemOnly))
1579-
1580-
// AsyncTaskAndContext swift_task_create_future_f(
1581-
// size_t flags,
1582-
// TaskOptionRecord *options,
1583-
// const Metadata *futureResultType,
1584-
// TaskContinuationFunction *function, size_t contextSize);
1585-
FUNCTION(TaskCreateFutureFunc,
1586-
swift_task_create_future_f, SwiftCC,
1587-
ConcurrencyAvailability,
1588-
RETURNS(AsyncTaskAndContextTy),
1589-
ARGS(SizeTy,
1590-
SwiftTaskOptionRecordPtrTy,
1591-
TypeMetadataPtrTy,
1592-
TaskContinuationFunctionPtrTy,
1593-
SizeTy),
1594-
ATTRS(NoUnwind, ArgMemOnly))
1595-
1596-
// AsyncTaskAndContext swift_task_create_group_future(
1597-
// size_t flags,
1598-
// TaskGroup *group,
1599-
// TaskOptionRecord *options
16001553
// const Metadata *futureResultType,
16011554
// void *closureEntry, HeapObject *closureContext);
1602-
FUNCTION(TaskCreateGroupFuture,
1603-
swift_task_create_group_future, SwiftCC,
1555+
FUNCTION(TaskCreate,
1556+
swift_task_create, SwiftCC,
16041557
ConcurrencyAvailability,
16051558
RETURNS(AsyncTaskAndContextTy),
16061559
ARGS(SizeTy,
1607-
SwiftTaskGroupPtrTy,
16081560
SwiftTaskOptionRecordPtrTy,
16091561
TypeMetadataPtrTy,
16101562
Int8PtrTy,
16111563
RefCountedPtrTy),
16121564
ATTRS(NoUnwind, ArgMemOnly))
16131565

1614-
// AsyncTaskAndContext swift_task_create_group_future_f(
1615-
// size_t flags,
1616-
// TaskGroup *group,
1617-
// TaskOptionRecord *options
1618-
// const Metadata *futureResultType,
1619-
// TaskContinuationFunction *function, size_t contextSize);
1620-
FUNCTION(TaskCreateGroupFutureFunc,
1621-
swift_task_create_group_future_f, SwiftCC,
1622-
ConcurrencyAvailability,
1623-
RETURNS(AsyncTaskAndContextTy),
1624-
ARGS(SizeTy,
1625-
SwiftTaskGroupPtrTy,
1626-
SwiftTaskOptionRecordPtrTy,
1627-
TypeMetadataPtrTy,
1628-
TaskContinuationFunctionPtrTy,
1629-
SizeTy),
1630-
ATTRS(NoUnwind, ArgMemOnly))
1631-
16321566
// void swift_task_switch(AsyncContext *resumeContext,
16331567
// TaskContinuationFunction *resumeFunction,
16341568
// ExecutorRef newExecutor);
@@ -1742,7 +1676,7 @@ FUNCTION(DistributedActorDestroy,
17421676
/// TaskOptionRecord *options,
17431677
/// const Metadata *futureResultType,
17441678
/// void *closureEntryPoint,
1745-
/// OpaqueValue *closureContext
1679+
/// HeapObject *closureContext
17461680
/// );
17471681
FUNCTION(AsyncLetStart,
17481682
swift_asyncLet_start, SwiftCC,

lib/AST/ASTPrinter.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2779,6 +2779,10 @@ static bool usesFeatureBuiltinTaskGroupWithArgument(Decl *decl) {
27792779
return false;
27802780
}
27812781

2782+
static bool usesFeatureBuiltinCreateAsyncTaskInGroup(Decl *decl) {
2783+
return false;
2784+
}
2785+
27822786
static bool usesFeatureInheritActorContext(Decl *decl) {
27832787
if (auto func = dyn_cast<AbstractFunctionDecl>(decl)) {
27842788
for (auto param : *func->getParameters()) {

0 commit comments

Comments
 (0)