Skip to content

Commit 684dbe2

Browse files
varungandhi-appleaschwaighofer
authored andcommitted
[5.5] [Runtime] Remove FIXMEs and hacks for tail calls.
Reverts hack from 9b4f7d6. rdar://76652421
1 parent 519b1e9 commit 684dbe2

File tree

2 files changed

+18
-24
lines changed

2 files changed

+18
-24
lines changed

include/swift/ABI/Task.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,15 @@ class alignas(2 * alignof(void*)) Job : public HeapObject {
9999
/// Given that we've fully established the job context in the current
100100
/// thread, actually start running this job. To establish the context
101101
/// correctly, call swift_job_run or runJobInExecutorContext.
102+
SWIFT_CC(swiftasync)
102103
void runInFullyEstablishedContext();
103104

104105
/// Given that we've fully established the job context in the
105106
/// current thread, and that the job is a simple (non-task) job,
106107
/// actually start running this job.
108+
SWIFT_CC(swiftasync)
107109
void runSimpleInFullyEstablishedContext() {
108-
RunJob(this);
110+
return RunJob(this); // 'return' forces tail call
109111
}
110112
};
111113

@@ -224,8 +226,9 @@ class AsyncTask : public Job {
224226
/// in the current thread, start running this task. To establish
225227
/// the job context correctly, call swift_job_run or
226228
/// runInExecutorContext.
229+
SWIFT_CC(swiftasync)
227230
void runInFullyEstablishedContext() {
228-
ResumeTask(ResumeContext);
231+
return ResumeTask(ResumeContext); // 'return' forces tail call
229232
}
230233

231234
/// Check whether this task has been cancelled.
@@ -486,11 +489,12 @@ static_assert(sizeof(AsyncTask) == 14 * sizeof(void*),
486489
static_assert(alignof(AsyncTask) == 2 * alignof(void*),
487490
"AsyncTask alignment is wrong");
488491

492+
SWIFT_CC(swiftasync)
489493
inline void Job::runInFullyEstablishedContext() {
490494
if (auto task = dyn_cast<AsyncTask>(this))
491-
task->runInFullyEstablishedContext();
495+
return task->runInFullyEstablishedContext(); // 'return' forces tail call
492496
else
493-
runSimpleInFullyEstablishedContext();
497+
return runSimpleInFullyEstablishedContext(); // 'return' forces tail call
494498
}
495499

496500
/// An asynchronous context within a task. Generally contexts are

stdlib/public/Concurrency/Actor.cpp

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1473,6 +1473,7 @@ static void swift_job_runImpl(Job *job, ExecutorRef executor) {
14731473
/// the actor lives for the duration of job execution.
14741474
/// Note that this may conflict with the retain/release
14751475
/// design in the DefaultActorImpl, but it does fix bugs!
1476+
SWIFT_CC(swiftasync)
14761477
static void processDefaultActor(DefaultActorImpl *currentActor,
14771478
RunningJobInfo runner) {
14781479
#if SWIFT_TASK_PRINTF_DEBUG
@@ -1547,6 +1548,7 @@ static void processDefaultActor(DefaultActorImpl *currentActor,
15471548
swift_release(actor);
15481549
}
15491550

1551+
SWIFT_CC(swiftasync)
15501552
void ProcessInlineJob::process(Job *job) {
15511553
DefaultActorImpl *actor = DefaultActorImpl::fromInlineJob(job);
15521554

@@ -1555,11 +1557,11 @@ void ProcessInlineJob::process(Job *job) {
15551557
auto targetPriority = job->getPriority();
15561558
auto runner = RunningJobInfo::forInline(targetPriority);
15571559

1558-
// FIXME: force tail call
15591560
swift_retain(actor);
1560-
return processDefaultActor(actor, runner);
1561+
return processDefaultActor(actor, runner); // 'return' forces tail call
15611562
}
15621563

1564+
SWIFT_CC(swiftasync)
15631565
void ProcessOutOfLineJob::process(Job *job) {
15641566
auto self = cast<ProcessOutOfLineJob>(job);
15651567
DefaultActorImpl *actor = self->Actor;
@@ -1571,21 +1573,20 @@ void ProcessOutOfLineJob::process(Job *job) {
15711573

15721574
delete self;
15731575

1574-
// FIXME: force tail call
15751576
swift_retain(actor);
1576-
return processDefaultActor(actor, runner);
1577+
return processDefaultActor(actor, runner); // 'return' forces tail call
15771578
}
15781579

1580+
SWIFT_CC(swiftasync)
15791581
void ProcessOverrideJob::process(Job *job) {
15801582
auto self = cast<ProcessOverrideJob>(job);
15811583

15821584
// Pull the actor and priority out of the job.
15831585
auto actor = self->Actor;
15841586
auto runner = RunningJobInfo::forOverride(self);
15851587

1586-
// FIXME: force tail call
15871588
swift_retain(actor);
1588-
return processDefaultActor(actor, runner);
1589+
return processDefaultActor(actor, runner); // 'return' forces tail call
15891590
}
15901591

15911592
void DefaultActorImpl::enqueue(Job *job) {
@@ -1802,13 +1803,6 @@ static bool tryAssumeThreadForSwitch(ExecutorRef newExecutor,
18021803
return false;
18031804
}
18041805

1805-
__attribute__((noinline))
1806-
SWIFT_CC(swiftasync)
1807-
static void force_tail_call_hack(AsyncTask *task) {
1808-
// This *should* be executed as a tail call.
1809-
return task->runInFullyEstablishedContext();
1810-
}
1811-
18121806
/// Given that we've assumed control of an executor on this thread,
18131807
/// continue to run the given task on it.
18141808
SWIFT_CC(swiftasync)
@@ -1821,10 +1815,7 @@ static void runOnAssumedThread(AsyncTask *task, ExecutorRef executor,
18211815
if (oldTracking) {
18221816
oldTracking->setActiveExecutor(executor);
18231817

1824-
// FIXME: force tail call
1825-
// return task->runInFullyEstablishedContext();
1826-
// This hack "ensures" that this call gets executed as a tail call.
1827-
return force_tail_call_hack(task);
1818+
return task->runInFullyEstablishedContext(); // 'return' forces tail call
18281819
}
18291820

18301821
// Otherwise, set up tracking info.
@@ -1867,8 +1858,7 @@ static void swift_task_switchImpl(SWIFT_ASYNC_CONTEXT AsyncContext *resumeContex
18671858
// we can just immediately continue running with the resume function
18681859
// we were passed in.
18691860
if (!currentExecutor.mustSwitchToRun(newExecutor)) {
1870-
// FIXME: force tail call
1871-
return resumeFunction(resumeContext);
1861+
return resumeFunction(resumeContext); // 'return' forces tail call
18721862
}
18731863

18741864
auto task = swift_task_getCurrent();
@@ -1892,7 +1882,7 @@ static void swift_task_switchImpl(SWIFT_ASYNC_CONTEXT AsyncContext *resumeContex
18921882
fprintf(stderr, "[%p] switch succeeded, task %p assumed thread for executor %p\n", pthread_self(), task, newExecutor.getIdentity());
18931883
#endif
18941884
giveUpThreadForSwitch(currentExecutor, runner);
1895-
// FIXME: force tail call
1885+
// 'return' forces tail call
18961886
return runOnAssumedThread(task, newExecutor, trackingInfo, runner);
18971887
}
18981888

0 commit comments

Comments
 (0)