Skip to content

Commit a2d7c70

Browse files
authored
Merge pull request swiftlang#34600 from DougGregor/async-task-builtins
[Concurrency] Add cancelAsyncTask() builtin.
2 parents fdff522 + c291eb5 commit a2d7c70

File tree

16 files changed

+88
-7
lines changed

16 files changed

+88
-7
lines changed

include/swift/ABI/Task.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ class AsyncTask : public HeapObject, public Job {
182182
/// prevent it from being corrupted in flight.
183183
AsyncContext * __ptrauth_swift_task_resume_context ResumeContext;
184184

185-
/// The currntly-active information about cancellation.
185+
/// The currently-active information about cancellation.
186186
std::atomic<ActiveTaskStatus> Status;
187187

188188
/// Reserved for the use of the task-local stack allocator.

include/swift/AST/Builtins.def

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,12 @@ BUILTIN_MISC_OPERATION(IntInstrprofIncrement, "int_instrprof_increment", "", Spe
723723
// function is executing.
724724
BUILTIN_MISC_OPERATION_WITH_SILGEN(GetCurrentAsyncTask, "getCurrentAsyncTask", "n", Special)
725725

726+
/// cancelAsyncTask(): (Builtin.NativeObject) -> Void
727+
///
728+
/// Cancel the given asynchronous task.
729+
BUILTIN_MISC_OPERATION_WITH_SILGEN(CancelAsyncTask, "cancelAsyncTask", "", Special)
730+
731+
726732
/// globalStringTablePointer has type String -> Builtin.RawPointer.
727733
/// It returns an immortal, global string table pointer for strings constructed
728734
/// from string literals. We consider it effects as readnone meaning that it

include/swift/Runtime/RuntimeFunctions.def

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1489,6 +1489,14 @@ FUNCTION(TaskDealloc,
14891489
ARGS(SwiftTaskPtrTy, Int8PtrTy),
14901490
ATTRS(NoUnwind, ArgMemOnly))
14911491

1492+
// void swift_task_dealloc(AsyncTask *task, void *ptr);
1493+
FUNCTION(TaskCancel,
1494+
swift_task_cancel, SwiftCC,
1495+
ConcurrencyAvailability,
1496+
RETURNS(VoidTy),
1497+
ARGS(SwiftTaskPtrTy),
1498+
ATTRS(NoUnwind, ArgMemOnly))
1499+
14921500
#undef RETURNS
14931501
#undef ARGS
14941502
#undef ATTRS

lib/AST/Builtins.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1345,6 +1345,11 @@ static ValueDecl *getGetCurrentAsyncTask(ASTContext &ctx, Identifier id) {
13451345
return getBuiltinFunction(id, { }, ctx.TheNativeObjectType);
13461346
}
13471347

1348+
static ValueDecl *getCancelAsyncTask(ASTContext &ctx, Identifier id) {
1349+
return getBuiltinFunction(
1350+
id, { ctx.TheNativeObjectType }, ctx.TheEmptyTupleType);
1351+
}
1352+
13481353
static ValueDecl *getPoundAssert(ASTContext &Context, Identifier Id) {
13491354
auto int1Type = BuiltinIntegerType::get(1, Context);
13501355
auto optionalRawPointerType = BoundGenericEnumType::get(
@@ -2474,6 +2479,9 @@ ValueDecl *swift::getBuiltinValueDecl(ASTContext &Context, Identifier Id) {
24742479
case BuiltinValueKind::GetCurrentAsyncTask:
24752480
return getGetCurrentAsyncTask(Context, Id);
24762481

2482+
case BuiltinValueKind::CancelAsyncTask:
2483+
return getCancelAsyncTask(Context, Id);
2484+
24772485
case BuiltinValueKind::PoundAssert:
24782486
return getPoundAssert(Context, Id);
24792487

lib/IRGen/GenBuiltin.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,11 @@ void irgen::emitBuiltinCall(IRGenFunction &IGF, const BuiltinInfo &Builtin,
220220

221221
// Everything else cares about the (rvalue) argument.
222222

223+
if (Builtin.ID == BuiltinValueKind::CancelAsyncTask) {
224+
emitTaskCancel(IGF, args.claimNext());
225+
return;
226+
}
227+
223228
// If this is an LLVM IR intrinsic, lower it to an intrinsic call.
224229
const IntrinsicInfo &IInfo = IGF.getSILModule().getIntrinsicInfo(FnId);
225230
llvm::Intrinsic::ID IID = IInfo.ID;

lib/IRGen/GenCall.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3458,6 +3458,18 @@ void irgen::emitTaskDealloc(IRGenFunction &IGF, Address address,
34583458
llvm::Attribute::ReadNone);
34593459
}
34603460

3461+
void irgen::emitTaskCancel(IRGenFunction &IGF, llvm::Value *task) {
3462+
if (task->getType() != IGF.IGM.SwiftTaskPtrTy) {
3463+
task = IGF.Builder.CreateBitCast(task, IGF.IGM.SwiftTaskPtrTy);
3464+
}
3465+
3466+
auto *call = IGF.Builder.CreateCall(IGF.IGM.getTaskCancelFn(), {task});
3467+
call->setDoesNotThrow();
3468+
call->setCallingConv(IGF.IGM.SwiftCC);
3469+
call->addAttribute(llvm::AttributeList::FunctionIndex,
3470+
llvm::Attribute::ReadNone);
3471+
}
3472+
34613473
std::pair<Address, Size> irgen::emitAllocAsyncContext(IRGenFunction &IGF,
34623474
AsyncContextLayout layout,
34633475
llvm::Value *sizeValue,

lib/IRGen/GenCall.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,8 @@ namespace irgen {
388388
Address emitTaskAlloc(IRGenFunction &IGF, llvm::Value *size,
389389
Alignment alignment);
390390
void emitTaskDealloc(IRGenFunction &IGF, Address address, llvm::Value *size);
391+
void emitTaskCancel(IRGenFunction &IGF, llvm::Value *task);
392+
391393
/// Allocate task local storage for the specified layout but using the
392394
/// provided dynamic size. Allowing the size to be specified dynamically is
393395
/// necessary for applies of thick functions the sizes of whose async contexts

lib/SIL/IR/OperandOwnership.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ CONSTANT_OWNERSHIP_INST(None, MustBeLive, UncheckedTakeEnumDataAddr)
231231
CONSTANT_OWNERSHIP_INST(None, MustBeLive, UnconditionalCheckedCastAddr)
232232
CONSTANT_OWNERSHIP_INST(None, MustBeLive, AllocValueBuffer)
233233
CONSTANT_OWNERSHIP_INST(None, MustBeLive, DeallocValueBuffer)
234+
234235
#define NEVER_LOADABLE_CHECKED_REF_STORAGE(Name, ...) \
235236
CONSTANT_OWNERSHIP_INST(None, MustBeLive, Load##Name)
236237
#define ALWAYS_LOADABLE_CHECKED_REF_STORAGE(Name, ...) \
@@ -1033,6 +1034,8 @@ ANY_OWNERSHIP_BUILTIN(IntInstrprofIncrement)
10331034
}
10341035
CONSTANT_OWNERSHIP_BUILTIN(Owned, MustBeInvalidated, COWBufferForReading)
10351036
CONSTANT_OWNERSHIP_BUILTIN(Owned, MustBeInvalidated, UnsafeGuaranteed)
1037+
CONSTANT_OWNERSHIP_BUILTIN(Guaranteed, MustBeLive, CancelAsyncTask)
1038+
10361039
#undef CONSTANT_OWNERSHIP_BUILTIN
10371040

10381041
#define SHOULD_NEVER_VISIT_BUILTIN(ID) \

lib/SIL/IR/ValueOwnership.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,7 @@ CONSTANT_OWNERSHIP_BUILTIN(None, TypePtrAuthDiscriminator)
542542
CONSTANT_OWNERSHIP_BUILTIN(None, IntInstrprofIncrement)
543543
CONSTANT_OWNERSHIP_BUILTIN(None, GlobalStringTablePointer)
544544
CONSTANT_OWNERSHIP_BUILTIN(Owned, GetCurrentAsyncTask)
545+
CONSTANT_OWNERSHIP_BUILTIN(None, CancelAsyncTask)
545546

546547
#undef CONSTANT_OWNERSHIP_BUILTIN
547548

lib/SIL/Utils/MemAccessUtils.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1799,6 +1799,7 @@ static void visitBuiltinAddress(BuiltinInst *builtin,
17991799
case BuiltinValueKind::PoundAssert:
18001800
case BuiltinValueKind::IntInstrprofIncrement:
18011801
case BuiltinValueKind::TSanInoutAccess:
1802+
case BuiltinValueKind::CancelAsyncTask:
18021803
return;
18031804

18041805
// General memory access to a pointer in first operand position.

0 commit comments

Comments
 (0)