Skip to content

Commit 35b9d01

Browse files
[Runtime] Remove FIXMEs and hacks for tail calls.
Reverts hack from 9b4f7d6.
1 parent 0e2e11d commit 35b9d01

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
@@ -113,13 +113,15 @@ class alignas(2 * alignof(void*)) Job :
113113
/// Given that we've fully established the job context in the current
114114
/// thread, actually start running this job. To establish the context
115115
/// correctly, call swift_job_run or runJobInExecutorContext.
116+
SWIFT_CC(swiftasync)
116117
void runInFullyEstablishedContext();
117118

118119
/// Given that we've fully established the job context in the
119120
/// current thread, and that the job is a simple (non-task) job,
120121
/// actually start running this job.
122+
SWIFT_CC(swiftasync)
121123
void runSimpleInFullyEstablishedContext() {
122-
RunJob(this);
124+
return RunJob(this); // 'return' forces tail call
123125
}
124126
};
125127

@@ -260,8 +262,9 @@ class AsyncTask : public Job {
260262
/// in the current thread, start running this task. To establish
261263
/// the job context correctly, call swift_job_run or
262264
/// runInExecutorContext.
265+
SWIFT_CC(swiftasync)
263266
void runInFullyEstablishedContext() {
264-
ResumeTask(ResumeContext);
267+
return ResumeTask(ResumeContext); // 'return' forces tail call
265268
}
266269

267270
/// Check whether this task has been cancelled.
@@ -534,11 +537,12 @@ static_assert(alignof(AsyncTask) == 2 * alignof(void*),
534537
static_assert(offsetof(AsyncTask, Id) == 4 * sizeof(void *) + 4,
535538
"AsyncTask::Id offset is wrong");
536539

540+
SWIFT_CC(swiftasync)
537541
inline void Job::runInFullyEstablishedContext() {
538542
if (auto task = dyn_cast<AsyncTask>(this))
539-
task->runInFullyEstablishedContext();
543+
return task->runInFullyEstablishedContext(); // 'return' forces tail call
540544
else
541-
runSimpleInFullyEstablishedContext();
545+
return runSimpleInFullyEstablishedContext(); // 'return' forces tail call
542546
}
543547

544548
/// 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
@@ -1483,6 +1483,7 @@ static void swift_job_runImpl(Job *job, ExecutorRef executor) {
14831483
/// the actor lives for the duration of job execution.
14841484
/// Note that this may conflict with the retain/release
14851485
/// design in the DefaultActorImpl, but it does fix bugs!
1486+
SWIFT_CC(swiftasync)
14861487
static void processDefaultActor(DefaultActorImpl *currentActor,
14871488
RunningJobInfo runner) {
14881489
#if SWIFT_TASK_PRINTF_DEBUG
@@ -1557,6 +1558,7 @@ static void processDefaultActor(DefaultActorImpl *currentActor,
15571558
swift_release(actor);
15581559
}
15591560

1561+
SWIFT_CC(swiftasync)
15601562
void ProcessInlineJob::process(Job *job) {
15611563
DefaultActorImpl *actor = DefaultActorImpl::fromInlineJob(job);
15621564

@@ -1565,11 +1567,11 @@ void ProcessInlineJob::process(Job *job) {
15651567
auto targetPriority = job->getPriority();
15661568
auto runner = RunningJobInfo::forInline(targetPriority);
15671569

1568-
// FIXME: force tail call
15691570
swift_retain(actor);
1570-
return processDefaultActor(actor, runner);
1571+
return processDefaultActor(actor, runner); // 'return' forces tail call
15711572
}
15721573

1574+
SWIFT_CC(swiftasync)
15731575
void ProcessOutOfLineJob::process(Job *job) {
15741576
auto self = cast<ProcessOutOfLineJob>(job);
15751577
DefaultActorImpl *actor = self->Actor;
@@ -1581,21 +1583,20 @@ void ProcessOutOfLineJob::process(Job *job) {
15811583

15821584
delete self;
15831585

1584-
// FIXME: force tail call
15851586
swift_retain(actor);
1586-
return processDefaultActor(actor, runner);
1587+
return processDefaultActor(actor, runner); // 'return' forces tail call
15871588
}
15881589

1590+
SWIFT_CC(swiftasync)
15891591
void ProcessOverrideJob::process(Job *job) {
15901592
auto self = cast<ProcessOverrideJob>(job);
15911593

15921594
// Pull the actor and priority out of the job.
15931595
auto actor = self->Actor;
15941596
auto runner = RunningJobInfo::forOverride(self);
15951597

1596-
// FIXME: force tail call
15971598
swift_retain(actor);
1598-
return processDefaultActor(actor, runner);
1599+
return processDefaultActor(actor, runner); // 'return' forces tail call
15991600
}
16001601

16011602
void DefaultActorImpl::enqueue(Job *job) {
@@ -1800,13 +1801,6 @@ static bool tryAssumeThreadForSwitch(ExecutorRef newExecutor,
18001801
return false;
18011802
}
18021803

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

1822-
// FIXME: force tail call
1823-
// return task->runInFullyEstablishedContext();
1824-
// This hack "ensures" that this call gets executed as a tail call.
1825-
return force_tail_call_hack(task);
1816+
return task->runInFullyEstablishedContext(); // 'return' forces tail call
18261817
}
18271818

18281819
// Otherwise, set up tracking info.
@@ -1865,8 +1856,7 @@ static void swift_task_switchImpl(SWIFT_ASYNC_CONTEXT AsyncContext *resumeContex
18651856
// we can just immediately continue running with the resume function
18661857
// we were passed in.
18671858
if (!currentExecutor.mustSwitchToRun(newExecutor)) {
1868-
// FIXME: force tail call
1869-
return resumeFunction(resumeContext);
1859+
return resumeFunction(resumeContext); // 'return' forces tail call
18701860
}
18711861

18721862
auto task = swift_task_getCurrent();
@@ -1890,7 +1880,7 @@ static void swift_task_switchImpl(SWIFT_ASYNC_CONTEXT AsyncContext *resumeContex
18901880
fprintf(stderr, "[%p] switch succeeded, task %p assumed thread for executor %p\n", pthread_self(), task, newExecutor.getIdentity());
18911881
#endif
18921882
giveUpThreadForSwitch(currentExecutor, runner);
1893-
// FIXME: force tail call
1883+
// 'return' forces tail call
18941884
return runOnAssumedThread(task, newExecutor, trackingInfo, runner);
18951885
}
18961886

0 commit comments

Comments
 (0)