1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
* vim: set ts=8 sts=2 et sw=2 tw=80:
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "jit/IonCompileTask.h"
#include "jit/CodeGenerator.h"
#include "jit/Ion.h"
#include "jit/JitRuntime.h"
#include "jit/JitScript.h"
#include "jit/WarpSnapshot.h"
#include "vm/HelperThreadState.h"
#include "vm/JSScript.h"
#include "vm/JSScript-inl.h"
#include "vm/Realm-inl.h"
using namespace js;
using namespace js::jit;
void IonCompileTask::runHelperThreadTask(AutoLockHelperThreadState& locked) {
// The build is taken by this thread. Unfreeze the LifoAlloc to allow
// mutations.
alloc().lifoAlloc()->setReadWrite();
{
AutoUnlockHelperThreadState unlock(locked);
runTask();
}
FinishOffThreadIonCompile(this, locked);
JSRuntime* rt = script()->runtimeFromAnyThread();
// Ping the main thread so that the compiled code can be incorporated at the
// next interrupt callback.
//
// This must happen before the current task is reset. DestroyContext
// cancels in progress Ion compilations before destroying its target
// context, and after we reset the current task we are no longer considered
// to be Ion compiling.
rt->mainContextFromAnyThread()->requestInterrupt(
InterruptReason::AttachIonCompilations);
}
void IonCompileTask::runTask() {
// This is the entry point when ion compiles are run offthread.
jit::JitContext jctx(mirGen_.realm->runtime());
setBackgroundCodegen(jit::CompileBackEnd(&mirGen_, snapshot_));
}
void IonCompileTask::trace(JSTracer* trc) {
if (!mirGen_.runtime->runtimeMatches(trc->runtime())) {
return;
}
snapshot_->trace(trc);
}
IonCompileTask::IonCompileTask(JSContext* cx, MIRGenerator& mirGen,
WarpSnapshot* snapshot)
: mirGen_(mirGen),
snapshot_(snapshot),
isExecuting_(cx->isExecutingRef()) {}
size_t IonCompileTask::sizeOfExcludingThis(mozilla::MallocSizeOf mallocSizeOf) {
// See js::jit::FreeIonCompileTask.
// The IonCompileTask and most of its contents live in the LifoAlloc we point
// to.
size_t result = alloc().lifoAlloc()->sizeOfIncludingThis(mallocSizeOf);
if (backgroundCodegen_) {
result += mallocSizeOf(backgroundCodegen_);
}
return result;
}
static inline bool TooManyUnlinkedTasks(JSRuntime* rt) {
static const size_t MaxUnlinkedTasks = 100;
return rt->jitRuntime()->ionLazyLinkListSize() > MaxUnlinkedTasks;
}
static void MoveFinishedTasksToLazyLinkList(
JSRuntime* rt, const AutoLockHelperThreadState& lock) {
// Incorporate any off thread compilations for the runtime which have
// finished, failed or have been cancelled.
GlobalHelperThreadState::IonCompileTaskVector& finished =
HelperThreadState().ionFinishedList(lock);
for (size_t i = 0; i < finished.length(); i++) {
// Find a finished task for the runtime.
IonCompileTask* task = finished[i];
if (task->script()->runtimeFromAnyThread() != rt) {
continue;
}
HelperThreadState().remove(finished, &i);
rt->jitRuntime()->numFinishedOffThreadTasksRef(lock)--;
JSScript* script = task->script();
MOZ_ASSERT(script->hasBaselineScript());
script->baselineScript()->setPendingIonCompileTask(rt, script, task);
rt->jitRuntime()->ionLazyLinkListAdd(rt, task);
}
}
static void EagerlyLinkExcessTasks(JSContext* cx,
AutoLockHelperThreadState& lock) {
JSRuntime* rt = cx->runtime();
MOZ_ASSERT(TooManyUnlinkedTasks(rt));
do {
jit::IonCompileTask* task = rt->jitRuntime()->ionLazyLinkList(rt).getLast();
RootedScript script(cx, task->script());
AutoUnlockHelperThreadState unlock(lock);
AutoRealm ar(cx, script);
jit::LinkIonScript(cx, script);
} while (TooManyUnlinkedTasks(rt));
}
void jit::AttachFinishedCompilations(JSContext* cx) {
JSRuntime* rt = cx->runtime();
MOZ_ASSERT(CurrentThreadCanAccessRuntime(rt));
if (!rt->jitRuntime() || !rt->jitRuntime()->numFinishedOffThreadTasks()) {
return;
}
AutoLockHelperThreadState lock;
while (true) {
MoveFinishedTasksToLazyLinkList(rt, lock);
if (!TooManyUnlinkedTasks(rt)) {
break;
}
EagerlyLinkExcessTasks(cx, lock);
// Linking releases the lock so we must now check the finished list
// again.
}
MOZ_ASSERT(!rt->jitRuntime()->numFinishedOffThreadTasks());
}
static void FreeIonCompileTask(IonCompileTask* task) {
// The task is allocated into its LifoAlloc, so destroying that will
// destroy the task and all other data accumulated during compilation,
// except any final codegen (which includes an assembler and needs to be
// explicitly destroyed).
js_delete(task->backgroundCodegen());
js_delete(task->alloc().lifoAlloc());
}
void jit::FreeIonCompileTasks(const IonFreeCompileTasks& tasks) {
MOZ_ASSERT(!tasks.empty());
for (auto* task : tasks) {
FreeIonCompileTask(task);
}
}
void IonFreeTask::runHelperThreadTask(AutoLockHelperThreadState& locked) {
{
AutoUnlockHelperThreadState unlock(locked);
jit::FreeIonCompileTasks(compileTasks());
}
js_delete(this);
}
void jit::FinishOffThreadTask(JSRuntime* runtime,
AutoStartIonFreeTask& freeTask,
IonCompileTask* task) {
MOZ_ASSERT(runtime);
MOZ_ASSERT(CurrentThreadCanAccessRuntime(runtime));
JSScript* script = task->script();
// Clean the references to the pending IonCompileTask, if we just finished it.
if (script->baselineScript()->hasPendingIonCompileTask() &&
script->baselineScript()->pendingIonCompileTask() == task) {
script->baselineScript()->removePendingIonCompileTask(runtime, script);
}
// If the task is still in one of the helper thread lists, then remove it.
if (task->isInList()) {
runtime->jitRuntime()->ionLazyLinkListRemove(runtime, task);
}
// Clean up if compilation did not succeed.
if (script->isIonCompilingOffThread()) {
script->jitScript()->clearIsIonCompilingOffThread(script);
const AbortReasonOr<Ok>& status = task->mirGen().getOffThreadStatus();
if (status.isErr() && status.inspectErr() == AbortReason::Disable) {
script->disableIon();
}
}
// Try to free the Ion LifoAlloc off-thread. Free on the main thread if this
// OOMs.
if (!freeTask.addIonCompileToFreeTaskBatch(task)) {
FreeIonCompileTask(task);
}
}
|