Skip to content

Commit efeb818

Browse files
committed
Clean up the TaskGroup ABI:
- stop storing the parent task in the TaskGroup at the .swift level - make sure that swift_taskGroup_isCancelled is implied by the parent task being cancelled - make the TaskGroup structs frozen - make the withTaskGroup functions inlinable - remove swift_taskGroup_create - teach IRGen to allocate memory for the task group - don't deallocate the task group in swift_taskGroup_destroy To achieve the allocation change, introduce paired create/destroy builtins. Furthermore, remove the _swiftRetain and _swiftRelease functions and several calls to them. Replace them with uses of the appropriate builtins. I should probably change the builtins to return retained, since they're working with a managed type, but I'll do that in a separate commit.
1 parent 6c11713 commit efeb818

File tree

20 files changed

+185
-165
lines changed

20 files changed

+185
-165
lines changed

include/swift/AST/Builtins.def

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -740,6 +740,14 @@ BUILTIN_MISC_OPERATION(ResumeThrowingContinuationThrowing,
740740
BUILTIN_MISC_OPERATION(BuildSerialExecutorRef,
741741
"buildSerialExecutorRef", "", Special)
742742

743+
/// Create a task group.
744+
BUILTIN_MISC_OPERATION(CreateTaskGroup,
745+
"createTaskGroup", "", Special)
746+
747+
/// Destroy a task group.
748+
BUILTIN_MISC_OPERATION(DestroyTaskGroup,
749+
"destroyTaskGroup", "", Special)
750+
743751
// BUILTIN_MISC_OPERATION_WITH_SILGEN - Miscellaneous operations that are
744752
// specially emitted during SIL generation.
745753
//

include/swift/Basic/Features.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,5 +44,6 @@ LANGUAGE_FEATURE(GlobalActors, 0, "Global actors", langOpts.EnableExperimentalCo
4444
LANGUAGE_FEATURE(BuiltinJob, 0, "Builtin.Job type", true)
4545
LANGUAGE_FEATURE(Sendable, 0, "Sendable and @Sendable", true)
4646
LANGUAGE_FEATURE(BuiltinContinuation, 0, "Continuation builtins", true)
47+
LANGUAGE_FEATURE(BuiltinTaskGroup, 0, "TaskGroup builtins", true)
4748

4849
#undef LANGUAGE_FEATURE

include/swift/Runtime/Concurrency.h

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -178,17 +178,6 @@ void swift_taskGroup_wait_next_throwing(
178178
OpaqueValue *resultPointer, SWIFT_ASYNC_CONTEXT AsyncContext *rawContext,
179179
TaskGroup *group, const Metadata *successType);
180180

181-
/// Create a new `TaskGroup`.
182-
/// The caller is responsible for retaining and managing the group's lifecycle.
183-
///
184-
/// Its Swift signature is
185-
///
186-
/// \code
187-
/// func swift_taskGroup_create() -> Builtin.RawPointer
188-
/// \endcode
189-
SWIFT_EXPORT_FROM(swift_Concurrency) SWIFT_CC(swift)
190-
TaskGroup* swift_taskGroup_create(); // TODO: probably remove this call, and just use the initialize always
191-
192181
/// Initialize a `TaskGroup` in the passed `group` memory location.
193182
/// The caller is responsible for retaining and managing the group's lifecycle.
194183
///

include/swift/Runtime/RuntimeFunctions.def

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1672,21 +1672,21 @@ FUNCTION(DefaultActorDeallocateResilient,
16721672
ARGS(RefCountedPtrTy),
16731673
ATTRS(NoUnwind))
16741674

1675-
//// void swift_taskGroup_initialize(AsyncTask *task, TaskGroup *group);
1676-
//FUNCTION(TaskGroupInitialize,
1677-
// swift_taskGroup_initialize, SwiftCC,
1678-
// ConcurrencyAvailability,
1679-
// RETURNS(VoidTy),
1680-
// ARGS(RefCountedPtrTy, RefCountedPtrTy),
1681-
// ATTRS(NoUnwind))
1682-
//
1683-
//// void swift_taskGroup_destroy(AsyncTask *task, TaskGroup *group);
1684-
//FUNCTION(TaskGroupDestroy,
1685-
// swift_taskGroup_destroy, SwiftCC,
1686-
// ConcurrencyAvailability,
1687-
// RETURNS(VoidTy),
1688-
// ARGS(RefCountedPtrTy, RefCountedPtrTy),
1689-
// ATTRS(NoUnwind))
1675+
// void swift_taskGroup_initialize(TaskGroup *group);
1676+
FUNCTION(TaskGroupInitialize,
1677+
swift_taskGroup_initialize, SwiftCC,
1678+
ConcurrencyAvailability,
1679+
RETURNS(VoidTy),
1680+
ARGS(Int8PtrTy),
1681+
ATTRS(NoUnwind))
1682+
1683+
// void swift_taskGroup_destroy(TaskGroup *group);
1684+
FUNCTION(TaskGroupDestroy,
1685+
swift_taskGroup_destroy, SwiftCC,
1686+
ConcurrencyAvailability,
1687+
RETURNS(VoidTy),
1688+
ARGS(Int8PtrTy),
1689+
ATTRS(NoUnwind))
16901690

16911691
// AutoDiffLinearMapContext *swift_autoDiffCreateLinearMapContext(size_t);
16921692
FUNCTION(AutoDiffCreateLinearMapContext,

lib/AST/ASTPrinter.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2724,6 +2724,10 @@ static bool usesFeatureBuiltinContinuation(Decl *decl) {
27242724
return false;
27252725
}
27262726

2727+
static bool usesFeatureBuiltinTaskGroup(Decl *decl) {
2728+
return false;
2729+
}
2730+
27272731
/// Determine the set of "new" features used on a given declaration.
27282732
///
27292733
/// Note: right now, all features we check for are "new". At some point, we'll

lib/AST/Builtins.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1481,6 +1481,18 @@ static ValueDecl *getResumeContinuationThrowing(ASTContext &ctx,
14811481
_void);
14821482
}
14831483

1484+
static ValueDecl *getCreateTaskGroup(ASTContext &ctx, Identifier id) {
1485+
return getBuiltinFunction(ctx, id, _thin,
1486+
_parameters(),
1487+
_rawPointer);
1488+
}
1489+
1490+
static ValueDecl *getDestroyTaskGroup(ASTContext &ctx, Identifier id) {
1491+
return getBuiltinFunction(ctx, id, _thin,
1492+
_parameters(_rawPointer),
1493+
_void);
1494+
}
1495+
14841496
static ValueDecl *getBuildSerialExecutorRef(ASTContext &ctx, Identifier id) {
14851497
// TODO: restrict the generic parameter to the SerialExecutor protocol
14861498
return getBuiltinFunction(ctx, id, _thin,
@@ -2728,6 +2740,12 @@ ValueDecl *swift::getBuiltinValueDecl(ASTContext &Context, Identifier Id) {
27282740
case BuiltinValueKind::DestroyDefaultActor:
27292741
return getDefaultActorInitDestroy(Context, Id);
27302742

2743+
case BuiltinValueKind::CreateTaskGroup:
2744+
return getCreateTaskGroup(Context, Id);
2745+
2746+
case BuiltinValueKind::DestroyTaskGroup:
2747+
return getDestroyTaskGroup(Context, Id);
2748+
27312749
case BuiltinValueKind::ResumeNonThrowingContinuationReturning:
27322750
case BuiltinValueKind::ResumeThrowingContinuationReturning:
27332751
return getResumeContinuationReturning(Context, Id);

lib/IRGen/GenBuiltin.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,17 @@ void irgen::emitBuiltinCall(IRGenFunction &IGF, const BuiltinInfo &Builtin,
223223
// getCurrentActor has no arguments.
224224
if (Builtin.ID == BuiltinValueKind::GetCurrentExecutor) {
225225
emitGetCurrentExecutor(IGF, out);
226+
227+
return;
228+
}
229+
230+
if (Builtin.ID == BuiltinValueKind::CreateTaskGroup) {
231+
out.add(emitCreateTaskGroup(IGF));
232+
return;
233+
}
234+
235+
if (Builtin.ID == BuiltinValueKind::DestroyTaskGroup) {
236+
emitDestroyTaskGroup(IGF, args.claimNext());
226237
return;
227238
}
228239

lib/IRGen/GenConcurrency.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,3 +170,27 @@ void irgen::emitGetCurrentExecutor(IRGenFunction &IGF, Explosion &out) {
170170

171171
IGF.emitAllExtractValues(call, IGF.IGM.SwiftExecutorTy, out);
172172
}
173+
174+
llvm::Value *irgen::emitCreateTaskGroup(IRGenFunction &IGF) {
175+
auto ty = llvm::ArrayType::get(IGF.IGM.Int8PtrTy, NumWords_TaskGroup);
176+
auto address = IGF.createAlloca(ty, Alignment(Alignment_TaskGroup));
177+
auto group = IGF.Builder.CreateBitCast(address.getAddress(),
178+
IGF.IGM.Int8PtrTy);
179+
IGF.Builder.CreateLifetimeStart(group);
180+
181+
auto *call = IGF.Builder.CreateCall(IGF.IGM.getTaskGroupInitializeFn(),
182+
{group});
183+
call->setDoesNotThrow();
184+
call->setCallingConv(IGF.IGM.SwiftCC);
185+
186+
return group;
187+
}
188+
189+
void irgen::emitDestroyTaskGroup(IRGenFunction &IGF, llvm::Value *group) {
190+
auto *call = IGF.Builder.CreateCall(IGF.IGM.getTaskGroupDestroyFn(),
191+
{group});
192+
call->setDoesNotThrow();
193+
call->setCallingConv(IGF.IGM.SwiftCC);
194+
195+
IGF.Builder.CreateLifetimeEnd(group);
196+
}

lib/IRGen/GenConcurrency.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ void emitBuildSerialExecutorRef(IRGenFunction &IGF, llvm::Value *actor,
3636
/// Emit the getCurrentExecutor builtin.
3737
void emitGetCurrentExecutor(IRGenFunction &IGF, Explosion &out);
3838

39+
/// Emit the createTaskGroup builtin.
40+
llvm::Value *emitCreateTaskGroup(IRGenFunction &IGF);
41+
42+
/// Emit the destroyTaskGroup builtin.
43+
void emitDestroyTaskGroup(IRGenFunction &IGF, llvm::Value *group);
44+
3945
} // end namespace irgen
4046
} // end namespace swift
4147

lib/SIL/IR/OperandOwnership.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -744,6 +744,8 @@ BUILTIN_OPERAND_OWNERSHIP(InstantaneousUse, PoundAssert)
744744
BUILTIN_OPERAND_OWNERSHIP(InstantaneousUse, GlobalStringTablePointer)
745745
BUILTIN_OPERAND_OWNERSHIP(InstantaneousUse, TypePtrAuthDiscriminator)
746746
BUILTIN_OPERAND_OWNERSHIP(InstantaneousUse, IntInstrprofIncrement)
747+
BUILTIN_OPERAND_OWNERSHIP(InstantaneousUse, CreateTaskGroup)
748+
BUILTIN_OPERAND_OWNERSHIP(InstantaneousUse, DestroyTaskGroup)
747749

748750
BUILTIN_OPERAND_OWNERSHIP(ForwardingConsume, COWBufferForReading)
749751
BUILTIN_OPERAND_OWNERSHIP(ForwardingConsume, UnsafeGuaranteed)

0 commit comments

Comments
 (0)