Rename performOnExecutor into deinitOnExecutor.

It cannot be used for executing general-purpose work, because such function would need to have a different signature to pass isolated actor instance.

And being explicit about using this method only for deinit allows to use object pointer for comparison with executor identity.
This commit is contained in:
Mykola Pokhylets
2023-04-24 12:24:35 +02:00
parent a043ebf97a
commit 35f0334eb6
13 changed files with 51 additions and 50 deletions

View File

@@ -291,7 +291,7 @@ using ThrowingTaskFutureWaitContinuationFunction =
SWIFT_CC(swiftasync) SWIFT_CC(swiftasync)
void (SWIFT_ASYNC_CONTEXT AsyncContext *, SWIFT_CONTEXT void *); void (SWIFT_ASYNC_CONTEXT AsyncContext *, SWIFT_CONTEXT void *);
using AdHocWorkFunction = SWIFT_CC(swift) void(void *); using DeinitWorkFunction = SWIFT_CC(swift) void(void *);
template <class AsyncSignature> template <class AsyncSignature>
class AsyncFunctionPointer; class AsyncFunctionPointer;

View File

@@ -2491,7 +2491,7 @@ enum class JobKind : size_t {
DefaultActorSeparate, DefaultActorSeparate,
DefaultActorOverride, DefaultActorOverride,
NullaryContinuation, NullaryContinuation,
AdHoc, IsolatedDeinit,
}; };
/// The priority of a job. Higher priorities are larger values. /// The priority of a job. Higher priorities are larger values.

View File

@@ -607,7 +607,7 @@ swift_task_createNullaryContinuationJob(
SWIFT_EXPORT_FROM(swift_Concurrency) SWIFT_EXPORT_FROM(swift_Concurrency)
SWIFT_CC(swift) SWIFT_CC(swift)
void swift_task_performOnExecutor(void *context, AdHocWorkFunction *work, void swift_task_deinitOnExecutor(void *object, DeinitWorkFunction *work,
SerialExecutorRef newExecutor); SerialExecutorRef newExecutor);
/// Report error about attempting to bind a task-local value from an illegal context. /// Report error about attempting to bind a task-local value from an illegal context.

View File

@@ -2333,11 +2333,11 @@ FUNCTION(TaskSwitchFunc,
EFFECT(Concurrency), EFFECT(Concurrency),
UNKNOWN_MEMEFFECTS) UNKNOWN_MEMEFFECTS)
// void swift_task_performOnExecutor(void *context, // void swift_task_deinitOnExecutor(void *object,
// AdHocWorkFunction *work, // DeinitWorkFunction *work,
// SerialExecutorRef newExecutor); // SerialExecutorRef newExecutor);
FUNCTION(PerformOnExecutorFunc, FUNCTION(DeinitOnExecutorFunc,
swift_task_performOnExecutor, SwiftCC, swift_task_deinitOnExecutor, SwiftCC,
ConcurrencyAvailability, ConcurrencyAvailability,
RETURNS(VoidTy), RETURNS(VoidTy),
ARGS(Int8PtrTy, Int8PtrTy, ExecutorFirstTy, ExecutorSecondTy), ARGS(Int8PtrTy, Int8PtrTy, ExecutorFirstTy, ExecutorSecondTy),

View File

@@ -448,8 +448,8 @@ FuncDecl *SILGenModule::getSwiftJobRun() {
return lookupConcurrencyIntrinsic(getASTContext(), "_swiftJobRun"); return lookupConcurrencyIntrinsic(getASTContext(), "_swiftJobRun");
} }
FuncDecl *SILGenModule::getPerformOnExecutor() { FuncDecl *SILGenModule::getDeinitOnExecutor() {
return lookupConcurrencyIntrinsic(getASTContext(), "_performOnExecutor"); return lookupConcurrencyIntrinsic(getASTContext(), "_deinitOnExecutor");
} }
FuncDecl *SILGenModule::getExit() { FuncDecl *SILGenModule::getExit() {

View File

@@ -552,8 +552,8 @@ public:
FuncDecl *getAsyncMainDrainQueue(); FuncDecl *getAsyncMainDrainQueue();
/// Retrieve the _Concurrency._swiftJobRun intrinsic. /// Retrieve the _Concurrency._swiftJobRun intrinsic.
FuncDecl *getSwiftJobRun(); FuncDecl *getSwiftJobRun();
/// Retrieve the _Concurrency._performOnExecutor intrinsic. /// Retrieve the _Concurrency._deinitOnExecutor intrinsic.
FuncDecl *getPerformOnExecutor(); FuncDecl *getDeinitOnExecutor();
// Retrieve the _SwiftConcurrencyShims.exit intrinsic. // Retrieve the _SwiftConcurrencyShims.exit intrinsic.
FuncDecl *getExit(); FuncDecl *getExit();

View File

@@ -371,15 +371,15 @@ void SILGenFunction::emitIsolatingDestructor(DestructorDecl *dd) {
executor = B.createExtractExecutor(loc, actor); executor = B.createExtractExecutor(loc, actor);
} }
// Get performOnExecutor // Get deinitOnExecutor
FuncDecl *swiftPerformOnExecutorDecl = SGM.getPerformOnExecutor(); FuncDecl *swiftDeinitOnExecutorDecl = SGM.getDeinitOnExecutor();
assert(swiftPerformOnExecutorDecl && assert(swiftDeinitOnExecutorDecl &&
"Failed to find swift_task_performOnExecutor function decl"); "Failed to find swift_task_deinitOnExecutor function decl");
SILFunction *swiftPerformOnExecutorSILFunc = SGM.getFunction( SILFunction *swiftDeinitOnExecutorSILFunc = SGM.getFunction(
SILDeclRef(swiftPerformOnExecutorDecl, SILDeclRef::Kind::Func), SILDeclRef(swiftDeinitOnExecutorDecl, SILDeclRef::Kind::Func),
NotForDefinition); NotForDefinition);
SILValue swiftPerformOnExecutorFunc = SILValue swiftDeinitOnExecutorFunc =
B.createFunctionRefFor(loc, swiftPerformOnExecutorSILFunc); B.createFunctionRefFor(loc, swiftDeinitOnExecutorSILFunc);
// Cast self to AnyObject preserving owned ownership // Cast self to AnyObject preserving owned ownership
CanType selfType = selfValue->getType().getASTType(); CanType selfType = selfValue->getType().getASTType();
@@ -408,7 +408,7 @@ void SILGenFunction::emitIsolatingDestructor(DestructorDecl *dd) {
B.createConvertFunction(loc, dtx, workFuncType, false); B.createConvertFunction(loc, dtx, workFuncType, false);
// Schedule isolated execution // Schedule isolated execution
B.createApply(loc, swiftPerformOnExecutorFunc, {}, B.createApply(loc, swiftDeinitOnExecutorFunc, {},
{castedSelf, castedDeallocator, executor}); {castedSelf, castedDeallocator, executor});
}); });
} }

View File

@@ -142,10 +142,10 @@ OVERRIDE_ACTOR(task_switch, void,
TaskContinuationFunction *resumeFunction, SerialExecutorRef newExecutor), TaskContinuationFunction *resumeFunction, SerialExecutorRef newExecutor),
(resumeToContext, resumeFunction, newExecutor)) (resumeToContext, resumeFunction, newExecutor))
OVERRIDE_ACTOR(task_performOnExecutor, void, OVERRIDE_ACTOR(task_deinitOnExecutor, void,
SWIFT_EXPORT_FROM(swift_Concurrency), SWIFT_CC(swift), swift::, SWIFT_EXPORT_FROM(swift_Concurrency), SWIFT_CC(swift), swift::,
(void *context, AdHocWorkFunction *work, SerialExecutorRef newExecutor), (void *object, DeinitWorkFunction *work, SerialExecutorRef newExecutor),
(context, work, newExecutor)) (object, work, newExecutor))
OVERRIDE_TASK(task_create_common, AsyncTaskAndContext, OVERRIDE_TASK(task_create_common, AsyncTaskAndContext,
SWIFT_EXPORT_FROM(swift_Concurrency), SWIFT_CC(swift), swift::, SWIFT_EXPORT_FROM(swift_Concurrency), SWIFT_CC(swift), swift::,

View File

@@ -2143,34 +2143,35 @@ static void swift_task_switchImpl(SWIFT_ASYNC_CONTEXT AsyncContext *resumeContex
namespace { namespace {
/// Job that allows to use executor API to schedule a block of task-less /// Job that allows to use executor API to schedule a block of task-less
/// synchronous code. /// synchronous code.
class AdHocJob : public Job { class IsolatedDeinitJob : public Job {
private: private:
void *Context; void *Object;
AdHocWorkFunction *Work; DeinitWorkFunction *Work;
public: public:
AdHocJob(JobPriority priority, void *context, AdHocWorkFunction *work) IsolatedDeinitJob(JobPriority priority, void *object,
: Job({JobKind::AdHoc, priority}, &process), Context(context), DeinitWorkFunction *work)
: Job({JobKind::IsolatedDeinit, priority}, &process), Object(object),
Work(work) {} Work(work) {}
SWIFT_CC(swiftasync) SWIFT_CC(swiftasync)
static void process(Job *_job) { static void process(Job *_job) {
auto *job = cast<AdHocJob>(_job); auto *job = cast<IsolatedDeinitJob>(_job);
void *ctx = job->Context; void *object = job->Object;
AdHocWorkFunction *work = job->Work; DeinitWorkFunction *work = job->Work;
delete job; delete job;
return work(ctx); return work(object);
} }
static bool classof(const Job *job) { static bool classof(const Job *job) {
return job->Flags.getKind() == JobKind::AdHoc; return job->Flags.getKind() == JobKind::IsolatedDeinit;
} }
}; };
} // namespace } // namespace
SWIFT_CC(swift) SWIFT_CC(swift)
static void swift_task_performOnExecutorImpl(void *context, static void swift_task_deinitOnExecutorImpl(void *object,
AdHocWorkFunction *work, DeinitWorkFunction *work,
SerialExecutorRef newExecutor) { SerialExecutorRef newExecutor) {
// If the current executor is compatible with running the new executor, // If the current executor is compatible with running the new executor,
// we can just immediately continue running with the resume function // we can just immediately continue running with the resume function
@@ -2179,11 +2180,11 @@ static void swift_task_performOnExecutorImpl(void *context,
// Note that swift_task_isCurrentExecutor() returns true for @MainActor // Note that swift_task_isCurrentExecutor() returns true for @MainActor
// when running on the main thread without any executor // when running on the main thread without any executor
if (swift_task_isCurrentExecutor(newExecutor)) { if (swift_task_isCurrentExecutor(newExecutor)) {
return work(context); // 'return' forces tail call return work(object); // 'return' forces tail call
} }
// Optimize deallocation of the default actors // Optimize deallocation of the default actors
if (context == newExecutor.getIdentity() && newExecutor.isDefaultActor()) { if (newExecutor.isDefaultActor() && object == newExecutor.getIdentity()) {
// Try to take the lock. This should always succeed, unless someone is // Try to take the lock. This should always succeed, unless someone is
// running the actor using unsafe unowned reference. // running the actor using unsafe unowned reference.
if (asImpl(newExecutor.getDefaultActor())->tryLock(false)) { if (asImpl(newExecutor.getDefaultActor())->tryLock(false)) {
@@ -2202,7 +2203,7 @@ static void swift_task_performOnExecutorImpl(void *context,
trackingInfo.enterAndShadow(newExecutor, TaskExecutorRef::undefined()); trackingInfo.enterAndShadow(newExecutor, TaskExecutorRef::undefined());
// Run the work. // Run the work.
work(context); work(object);
// `work` is a synchronous function, it cannot call swift_task_switch() // `work` is a synchronous function, it cannot call swift_task_switch()
// If it calls any synchronous API that may change executor inside // If it calls any synchronous API that may change executor inside
@@ -2222,7 +2223,7 @@ static void swift_task_performOnExecutorImpl(void *context,
auto priority = currentTask ? swift_task_currentPriority(currentTask) auto priority = currentTask ? swift_task_currentPriority(currentTask)
: swift_task_getCurrentThreadPriority(); : swift_task_getCurrentThreadPriority();
auto job = new AdHocJob(priority, context, work); auto job = new IsolatedDeinitJob(priority, object, work);
swift_task_enqueue(job, newExecutor); swift_task_enqueue(job, newExecutor);
} }

View File

@@ -561,8 +561,8 @@ internal final class DispatchQueueShim: @unchecked Sendable, SerialExecutor {
@available(SwiftStdlib 5.6, *) // TODO: Clarify version @available(SwiftStdlib 5.6, *) // TODO: Clarify version
@_silgen_name("swift_task_performOnExecutor") @_silgen_name("swift_task_deinitOnExecutor")
@usableFromInline @usableFromInline
internal func _performOnExecutor(_ ctx: __owned AnyObject, internal func _deinitOnExecutor(_ object: __owned AnyObject,
_ work: @convention(thin) (__owned AnyObject) -> Void, _ work: @convention(thin) (__owned AnyObject) -> Void,
_ executor: Builtin.Executor) _ executor: Builtin.Executor)

View File

@@ -219,7 +219,7 @@ distributed actor MyDistActorIsolated {
// CHECK: [[LOCAL_ACTOR_DEINIT_BB]]: // CHECK: [[LOCAL_ACTOR_DEINIT_BB]]:
// CHECK: [[ISOLATED_REF:%[0-9]+]] = function_ref @$s14default_deinit19MyDistActorIsolatedCfZ : $@convention(thin) (@owned MyDistActorIsolated) -> () // CHECK: [[ISOLATED_REF:%[0-9]+]] = function_ref @$s14default_deinit19MyDistActorIsolatedCfZ : $@convention(thin) (@owned MyDistActorIsolated) -> ()
// CHECK: [[EXECUTOR:%[0-9]+]] = extract_executor [[SELF]] : $MyDistActorIsolated // CHECK: [[EXECUTOR:%[0-9]+]] = extract_executor [[SELF]] : $MyDistActorIsolated
// CHECK: [[PERFORM_REF:%[0-9]+]] = function_ref @swift_task_performOnExecutor : $@convention(thin) (@owned AnyObject, @convention(thin) (@owned AnyObject) -> (), Builtin.Executor) -> () // CHECK: [[PERFORM_REF:%[0-9]+]] = function_ref @swift_task_deinitOnExecutor : $@convention(thin) (@owned AnyObject, @convention(thin) (@owned AnyObject) -> (), Builtin.Executor) -> ()
// CHECK: [[SELF_AS_ANY_OBJECT:%[0-9]+]] = unchecked_bitwise_cast [[SELF]] : $MyDistActorIsolated to $AnyObject // CHECK: [[SELF_AS_ANY_OBJECT:%[0-9]+]] = unchecked_bitwise_cast [[SELF]] : $MyDistActorIsolated to $AnyObject
// CHECK: [[ISOLATED_CASTED:%[0-9]+]] = convert_function [[ISOLATED_REF]] : $@convention(thin) (@owned MyDistActorIsolated) -> () to $@convention(thin) (@owned AnyObject) -> () // CHECK: [[ISOLATED_CASTED:%[0-9]+]] = convert_function [[ISOLATED_REF]] : $@convention(thin) (@owned MyDistActorIsolated) -> () to $@convention(thin) (@owned AnyObject) -> ()
// CHECK: [[DROP:%[0-9]+]] = apply [[PERFORM_REF]]([[SELF_AS_ANY_OBJECT]], [[ISOLATED_CASTED]], [[EXECUTOR]]) : $@convention(thin) (@owned AnyObject, @convention(thin) (@owned AnyObject) -> (), Builtin.Executor) -> () // CHECK: [[DROP:%[0-9]+]] = apply [[PERFORM_REF]]([[SELF_AS_ANY_OBJECT]], [[ISOLATED_CASTED]], [[EXECUTOR]]) : $@convention(thin) (@owned AnyObject, @convention(thin) (@owned AnyObject) -> (), Builtin.Executor) -> ()
@@ -355,7 +355,7 @@ actor SimpleActorIsolated {
// CHECK: bb0([[SELF:%[0-9]+]] : $SimpleActorIsolated): // CHECK: bb0([[SELF:%[0-9]+]] : $SimpleActorIsolated):
// CHECK: [[ISOLATED_REF:%[0-9]+]] = function_ref @$s14default_deinit19SimpleActorIsolatedCfZ : $@convention(thin) (@owned SimpleActorIsolated) -> () // CHECK: [[ISOLATED_REF:%[0-9]+]] = function_ref @$s14default_deinit19SimpleActorIsolatedCfZ : $@convention(thin) (@owned SimpleActorIsolated) -> ()
// CHECK: [[EXECUTOR:%[0-9]+]] = extract_executor [[SELF]] : $SimpleActorIsolated // CHECK: [[EXECUTOR:%[0-9]+]] = extract_executor [[SELF]] : $SimpleActorIsolated
// CHECK: [[PERFORM_REF:%[0-9]+]] = function_ref @swift_task_performOnExecutor : $@convention(thin) (@owned AnyObject, @convention(thin) (@owned AnyObject) -> (), Builtin.Executor) -> () // CHECK: [[PERFORM_REF:%[0-9]+]] = function_ref @swift_task_deinitOnExecutor : $@convention(thin) (@owned AnyObject, @convention(thin) (@owned AnyObject) -> (), Builtin.Executor) -> ()
// CHECK: [[SELF_AS_ANY_OBJECT:%[0-9]+]] = unchecked_bitwise_cast [[SELF]] : $SimpleActorIsolated to $AnyObject // CHECK: [[SELF_AS_ANY_OBJECT:%[0-9]+]] = unchecked_bitwise_cast [[SELF]] : $SimpleActorIsolated to $AnyObject
// CHECK: [[ISOLATED_CASTED:%[0-9]+]] = convert_function [[ISOLATED_REF]] : $@convention(thin) (@owned SimpleActorIsolated) -> () to $@convention(thin) (@owned AnyObject) -> () // CHECK: [[ISOLATED_CASTED:%[0-9]+]] = convert_function [[ISOLATED_REF]] : $@convention(thin) (@owned SimpleActorIsolated) -> () to $@convention(thin) (@owned AnyObject) -> ()
// CHECK: [[DROP:%[0-9]+]] = apply [[PERFORM_REF]]([[SELF_AS_ANY_OBJECT]], [[ISOLATED_CASTED]], [[EXECUTOR]]) : $@convention(thin) (@owned AnyObject, @convention(thin) (@owned AnyObject) -> (), Builtin.Executor) -> () // CHECK: [[DROP:%[0-9]+]] = apply [[PERFORM_REF]]([[SELF_AS_ANY_OBJECT]], [[ISOLATED_CASTED]], [[EXECUTOR]]) : $@convention(thin) (@owned AnyObject, @convention(thin) (@owned AnyObject) -> (), Builtin.Executor) -> ()

View File

@@ -321,4 +321,4 @@ Added: _$ss9TaskLocalC9withValue_9operation9isolation4file4lineqd__x_qd__yYaKXES
Added: _$ss9TaskLocalC9withValue_9operation9isolation4file4lineqd__x_qd__yYaKXEScA_pSgYiSSSutYaKlFTu Added: _$ss9TaskLocalC9withValue_9operation9isolation4file4lineqd__x_qd__yYaKXEScA_pSgYiSSSutYaKlFTu
// isolated deinit // isolated deinit
Added: _swift_task_performOnExecutor Added: _swift_task_deinitOnExecutor

View File

@@ -321,4 +321,4 @@ Added: _$ss9TaskLocalC9withValue_9operation9isolation4file4lineqd__x_qd__yYaKXES
Added: _$ss9TaskLocalC9withValue_9operation9isolation4file4lineqd__x_qd__yYaKXEScA_pSgYiSSSutYaKlFTu Added: _$ss9TaskLocalC9withValue_9operation9isolation4file4lineqd__x_qd__yYaKXEScA_pSgYiSSSutYaKlFTu
// isolated deinit // isolated deinit
Added: _swift_task_performOnExecutor Added: _swift_task_deinitOnExecutor