Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 2 additions & 2 deletions include/swift/AST/Builtins.def
Original file line number Diff line number Diff line change
Expand Up @@ -953,19 +953,19 @@ BUILTIN_MISC_OPERATION(AddressOfBorrowOpaque, "addressOfBorrowOpaque", "", Speci
BUILTIN_MISC_OPERATION(UnprotectedAddressOfBorrowOpaque, "unprotectedAddressOfBorrowOpaque", "", Special)

/// createTask<T>(flags: Int,
/// initialSerialExecutor: (Builtin.Executor)? = nil,
/// taskGroup: Builtin.RawPointer? = nil,
/// initialTaskExecutor: (Builtin.Executor)? = nil,
/// initialExecutor: (Builtin.Executor)? = nil,
/// operation: @escaping () async throws -> T)
/// -> Builtin.NativeObject, Builtin.RawPointer)
///
/// Create a new task.
BUILTIN_SIL_OPERATION(CreateTask, "createTask", Special)

/// createDiscardingTask(flags: Int,
/// initialSerialExecutor: (Builtin.Executor)? = nil,
/// taskGroup: Builtin.RawPointer? = nil,
/// initialTaskExecutor: (Builtin.Executor)? = nil,
/// initialExecutor: (Builtin.Executor)? = nil,
/// operation: @escaping () async throws -> ())
/// -> (Builtin.NativeObject, Builtin.RawPointer)
///
Expand Down
4 changes: 2 additions & 2 deletions lib/AST/Builtins.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1531,8 +1531,8 @@ static ValueDecl *getCreateTask(ASTContext &ctx, Identifier id) {
ctx, id, _thin, _generics(_unrestricted, _conformsToDefaults(0)),
_parameters(
_label("flags", _swiftInt),
_label("initialSerialExecutor", _defaulted(_optional(_executor), _nil)),
_label("taskGroup", _defaulted(_optional(_rawPointer), _nil)),
//_label("initialExecutor", _defaulted(_optional(_executor), _nil)),
_label("initialTaskExecutor", _defaulted(_optional(_executor), _nil)),
_label("operation", _function(_async(_throws(_sendable(_thick))),
_typeparam(0), _parameters()))),
Expand All @@ -1544,8 +1544,8 @@ static ValueDecl *getCreateDiscardingTask(ASTContext &ctx, Identifier id) {
ctx, id, _thin,
_parameters(
_label("flags", _swiftInt),
_label("initialSerialExecutor", _defaulted(_optional(_executor), _nil)),
_label("taskGroup", _defaulted(_optional(_rawPointer), _nil)),
//_label("initialExecutor", _defaulted(_optional(_executor), _nil)),
_label("initialTaskExecutor", _defaulted(_optional(_executor), _nil)),
_label("operation", _function(_async(_throws(_sendable(_thick))),
_void, _parameters()))),
Expand Down
39 changes: 39 additions & 0 deletions lib/IRGen/GenConcurrency.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,31 @@ llvm::Value *irgen::maybeAddEmbeddedSwiftResultTypeInfo(IRGenFunction &IGF,

namespace {

struct InitialSerialExecutorRecordTraits {
static StringRef getLabel() {
return "initial_serial_executor";
}
static llvm::StructType *getRecordType(IRGenModule &IGM) {
return IGM.SwiftInitialSerialExecutorTaskOptionRecordTy;
}
static TaskOptionRecordFlags getRecordFlags() {
return TaskOptionRecordFlags(TaskOptionRecordKind::InitialSerialExecutor);
}
static CanType getValueType(ASTContext &ctx) {
return ctx.TheExecutorType;
}

void initialize(IRGenFunction &IGF, Address recordAddr,
Explosion &serialExecutor) const {
auto executorRecord =
IGF.Builder.CreateStructGEP(recordAddr, 1, 2 * IGF.IGM.getPointerSize());
IGF.Builder.CreateStore(serialExecutor.claimNext(),
IGF.Builder.CreateStructGEP(executorRecord, 0, Size()));
IGF.Builder.CreateStore(serialExecutor.claimNext(),
IGF.Builder.CreateStructGEP(executorRecord, 1, Size()));
}
};

struct TaskGroupRecordTraits {
static StringRef getLabel() {
return "task_group";
Expand Down Expand Up @@ -647,6 +672,15 @@ struct InitialTaskExecutorRecordTraits {

} // end anonymous namespace

static llvm::Value *
maybeAddInitialSerialExecutorOptionRecord(IRGenFunction &IGF,
llvm::Value *prevOptions,
OptionalExplosion &serialExecutor) {
return maybeAddOptionRecord(IGF, prevOptions,
InitialSerialExecutorRecordTraits(),
serialExecutor);
}

static llvm::Value *
maybeAddTaskGroupOptionRecord(IRGenFunction &IGF, llvm::Value *prevOptions,
OptionalExplosion &taskGroup) {
Expand All @@ -665,6 +699,7 @@ maybeAddInitialTaskExecutorOptionRecord(IRGenFunction &IGF,

std::pair<llvm::Value *, llvm::Value *>
irgen::emitTaskCreate(IRGenFunction &IGF, llvm::Value *flags,
OptionalExplosion &serialExecutor,
OptionalExplosion &taskGroup,
OptionalExplosion &taskExecutor,
Explosion &taskFunction,
Expand All @@ -686,6 +721,10 @@ irgen::emitTaskCreate(IRGenFunction &IGF, llvm::Value *flags,
resultTypeMetadata = IGF.emitTypeMetadataRef(resultType);
}

// Add an option record for the initial serial executor, if present.
taskOptions =
maybeAddInitialSerialExecutorOptionRecord(IGF, taskOptions, serialExecutor);

// Add an option record for the task group, if present.
taskOptions = maybeAddTaskGroupOptionRecord(IGF, taskOptions, taskGroup);

Expand Down
1 change: 1 addition & 0 deletions lib/IRGen/GenConcurrency.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ llvm::Value *maybeAddEmbeddedSwiftResultTypeInfo(IRGenFunction &IGF,
/// task function.
std::pair<llvm::Value *, llvm::Value *>
emitTaskCreate(IRGenFunction &IGF, llvm::Value *flags,
OptionalExplosion &initialExecutor,
OptionalExplosion &taskGroup,
OptionalExplosion &taskExecutor,
Explosion &taskFunction,
Expand Down
5 changes: 5 additions & 0 deletions lib/IRGen/IRGenModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -702,6 +702,11 @@ IRGenModule::IRGenModule(IRGenerator &irgen,
ExecutorFirstTy, // identity
ExecutorSecondTy, // implementation
});
SwiftInitialSerialExecutorTaskOptionRecordTy =
createStructType(*this, "swift.serial_executor_task_option", {
SwiftTaskOptionRecordTy, // Base option record
SwiftExecutorTy, // Executor
});
SwiftInitialTaskExecutorPreferenceTaskOptionRecordTy =
createStructType(*this, "swift.task_executor_task_option", {
SwiftTaskOptionRecordTy, // Base option record
Expand Down
1 change: 1 addition & 0 deletions lib/IRGen/IRGenModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -819,6 +819,7 @@ class IRGenModule {
llvm::IntegerType *SwiftTaskOptionRecordPtrTy;
llvm::PointerType *SwiftTaskGroupPtrTy;
llvm::StructType *SwiftTaskOptionRecordTy;
llvm::StructType *SwiftInitialSerialExecutorTaskOptionRecordTy;
llvm::StructType *SwiftTaskGroupTaskOptionRecordTy;
llvm::StructType *SwiftInitialTaskExecutorPreferenceTaskOptionRecordTy;
llvm::StructType *SwiftResultTypeInfoTaskOptionRecordTy;
Expand Down
11 changes: 6 additions & 5 deletions lib/IRGen/IRGenSIL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3568,13 +3568,14 @@ static void emitBuiltinStackDealloc(IRGenSILFunction &IGF,
static void emitBuiltinCreateAsyncTask(IRGenSILFunction &IGF,
swift::BuiltinInst *i) {
auto flags = IGF.getLoweredSingletonExplosion(i->getOperand(0));
auto taskGroup = IGF.getLoweredOptionalExplosion(i->getOperand(1));
auto taskExecutor = IGF.getLoweredOptionalExplosion(i->getOperand(2));
Explosion taskFunction = IGF.getLoweredExplosion(i->getOperand(3));
auto serialExecutor = IGF.getLoweredOptionalExplosion(i->getOperand(1));
auto taskGroup = IGF.getLoweredOptionalExplosion(i->getOperand(2));
auto taskExecutor = IGF.getLoweredOptionalExplosion(i->getOperand(3));
Explosion taskFunction = IGF.getLoweredExplosion(i->getOperand(4));

auto taskAndContext =
emitTaskCreate(IGF, flags, taskGroup, taskExecutor, taskFunction,
i->getSubstitutions());
emitTaskCreate(IGF, flags, serialExecutor, taskGroup, taskExecutor,
taskFunction, i->getSubstitutions());
Explosion out;
out.add(taskAndContext.first);
out.add(taskAndContext.second);
Expand Down
12 changes: 7 additions & 5 deletions lib/SIL/Verifier/SILVerifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2292,15 +2292,17 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {
if (builtinKind == BuiltinValueKind::CreateAsyncTask) {
requireType(BI->getType(), _object(_tuple(_nativeObject, _rawPointer)),
"result of createAsyncTask");
require(arguments.size() == 4,
"createAsyncTask expects four arguments");
require(arguments.size() == 5,
"createAsyncTask expects five arguments");
requireType(arguments[0]->getType(), _object(_swiftInt),
"first argument of createAsyncTask");
requireType(arguments[1]->getType(), _object(_optional(_rawPointer)),
requireType(arguments[1]->getType(), _object(_optional(_executor)),
"second argument of createAsyncTask");
requireType(arguments[2]->getType(), _object(_optional(_executor)),
requireType(arguments[2]->getType(), _object(_optional(_rawPointer)),
"third argument of createAsyncTask");
auto fnType = requireObjectType(SILFunctionType, arguments[3],
requireType(arguments[3]->getType(), _object(_optional(_executor)),
"fourth argument of createAsyncTask");
auto fnType = requireObjectType(SILFunctionType, arguments[4],
"result of createAsyncTask");
auto expectedExtInfo =
SILExtInfoBuilder().withAsync(true).withConcurrent(true).build();
Expand Down
9 changes: 9 additions & 0 deletions lib/SILGen/SILGenBuiltin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1576,6 +1576,14 @@ static ManagedValue emitCreateAsyncTask(SILGenFunction &SGF, SILLocation loc,

ManagedValue flags = nextArg().getAsSingleValue(SGF);

ManagedValue initialExecutor = [&] {
if (options & CreateTaskOptions::OptionalEverything) {
return nextArg().getAsSingleValue(SGF);
} else {
return emitOptionalNone(ctx.TheExecutorType);
}
}();

ManagedValue taskGroup = [&] {
if (options & CreateTaskOptions::OptionalEverything) {
return nextArg().getAsSingleValue(SGF);
Expand Down Expand Up @@ -1638,6 +1646,7 @@ static ManagedValue emitCreateAsyncTask(SILGenFunction &SGF, SILLocation loc,

SILValue builtinArgs[] = {
flags.getUnmanagedValue(),
initialExecutor.getUnmanagedValue(),
taskGroup.getUnmanagedValue(),
taskExecutor.getUnmanagedValue(),
functionValue.forward(SGF)
Expand Down
3 changes: 2 additions & 1 deletion test/Concurrency/async_main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,14 @@ func asyncFunc() async {
// CHECK-SIL-NEXT: [[ASYNC_MAIN_FN:%.*]] = function_ref @async_Main : $@convention(thin) @async () -> ()
// CHECK-SIL-NEXT: [[T0:%.*]] = integer_literal $Builtin.Int64, 2048
// CHECK-SIL-NEXT: [[FLAGS:%.*]] = struct $Int ([[T0]] : $Builtin.Int64)
// CHECK-SIL-NEXT: [[OPT_SERIAL_EXECUTOR:%.*]] = enum $Optional<Builtin.Executor>, #Optional.none
// CHECK-SIL-NEXT: [[GROUP:%.*]] = enum $Optional<Builtin.RawPointer>, #Optional.none
// CHECK-SIL-NEXT: [[TASK_EXECUTOR:%.*]] = enum $Optional<Builtin.Executor>, #Optional.none
// CHECK-SIL-NEXT: // function_ref thunk for @escaping @convention(thin) @async () -> ()
// CHECK-SIL-NEXT: [[THUNK_FN:%.*]] = function_ref @$sIetH_yts5Error_pIeghHrzo_TR : $@convention(thin) @Sendable @async (@convention(thin) @async () -> ()) -> (@out (), @error any Error)
// CHECK-SIL-NEXT: [[THUNK:%.*]] = partial_apply [callee_guaranteed] [[THUNK_FN]]([[ASYNC_MAIN_FN]]) : $@convention(thin) @Sendable @async (@convention(thin) @async () -> ()) -> (@out (), @error any Error)
// CHECK-SIL-NEXT: [[CONVERTED_THUNK:%.*]] = convert_function [[THUNK]] : $@Sendable @async @callee_guaranteed () -> (@out (), @error any Error) to $@Sendable @async @callee_guaranteed @substituted <τ_0_0> () -> (@out τ_0_0, @error any Error) for <()>
// CHECK-SIL-NEXT: [[TASK_RESULT:%.*]] = builtin "createAsyncTask"<()>([[FLAGS]] : $Int, [[GROUP]] : $Optional<Builtin.RawPointer>, [[TASK_EXECUTOR]] : $Optional<Builtin.Executor>, [[CONVERTED_THUNK]] : $@Sendable @async @callee_guaranteed @substituted <τ_0_0> () -> (@out τ_0_0, @error any Error) for <()>) : $(Builtin.NativeObject, Builtin.RawPointer)
// CHECK-SIL-NEXT: [[TASK_RESULT:%.*]] = builtin "createAsyncTask"<()>([[FLAGS]] : $Int, [[OPT_SERIAL_EXECUTOR]] : $Optional<Builtin.Executor>, [[GROUP]] : $Optional<Builtin.RawPointer>, [[TASK_EXECUTOR]] : $Optional<Builtin.Executor>, [[CONVERTED_THUNK]] : $@Sendable @async @callee_guaranteed @substituted <τ_0_0> () -> (@out τ_0_0, @error any Error) for <()>) : $(Builtin.NativeObject, Builtin.RawPointer)
// CHECK-SIL-NEXT: [[TASK:%.*]] = tuple_extract [[TASK_RESULT]] : $(Builtin.NativeObject, Builtin.RawPointer), 0
// CHECK-SIL-NEXT: // function_ref swift_job_run
// CHECK-SIL-NEXT: [[RUN_FN:%.*]] = function_ref @swift_job_run : $@convention(thin) (UnownedJob, UnownedSerialExecutor) -> ()
Expand Down
Loading