-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
JITWorklist.cpp
344 lines (290 loc) · 10.4 KB
/
JITWorklist.cpp
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/*
* Copyright (C) 2016-2019 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "JITWorklist.h"
#if ENABLE(JIT)
#include "JIT.h"
#include "VMInlines.h"
namespace JSC {
class JITWorklist::Plan : public ThreadSafeRefCounted<JITWorklist::Plan> {
public:
Plan(CodeBlock* codeBlock, BytecodeIndex loopOSREntryBytecodeIndex)
: m_codeBlock(codeBlock)
, m_jit(codeBlock->vm(), codeBlock, loopOSREntryBytecodeIndex)
{
m_jit.doMainThreadPreparationBeforeCompile();
}
void compileInThread()
{
m_jit.compileWithoutLinking(JITCompilationCanFail);
LockHolder locker(m_lock);
m_isFinishedCompiling = true;
}
void finalize()
{
CompilationResult result = m_jit.link();
switch (result) {
case CompilationFailed:
CODEBLOCK_LOG_EVENT(m_codeBlock, "delayJITCompile", ("compilation failed"));
dataLogLnIf(Options::verboseOSR(), " JIT compilation failed.");
m_codeBlock->dontJITAnytimeSoon();
m_codeBlock->m_didFailJITCompilation = true;
return;
case CompilationSuccessful:
dataLogLnIf(Options::verboseOSR(), " JIT compilation successful.");
m_codeBlock->ownerExecutable()->installCode(m_codeBlock);
m_codeBlock->jitSoon();
return;
default:
RELEASE_ASSERT_NOT_REACHED();
return;
}
}
CodeBlock* codeBlock() { return m_codeBlock; }
VM& vm() { return m_codeBlock->vm(); }
bool isFinishedCompiling()
{
LockHolder locker(m_lock);
return m_isFinishedCompiling;
}
static void compileNow(CodeBlock* codeBlock, BytecodeIndex loopOSREntryBytecodeIndex)
{
Plan plan(codeBlock, loopOSREntryBytecodeIndex);
plan.compileInThread();
plan.finalize();
}
private:
CodeBlock* m_codeBlock;
JIT m_jit;
Lock m_lock;
bool m_isFinishedCompiling { false };
};
class JITWorklist::Thread final : public AutomaticThread {
public:
Thread(const AbstractLocker& locker, JITWorklist& worklist)
: AutomaticThread(locker, worklist.m_lock, worklist.m_condition.copyRef())
, m_worklist(worklist)
{
m_worklist.m_numAvailableThreads++;
}
const char* name() const final
{
#if OS(LINUX)
return "JITWorker";
#else
return "JIT Worklist Helper Thread";
#endif
}
private:
PollResult poll(const AbstractLocker&) final
{
RELEASE_ASSERT(m_worklist.m_numAvailableThreads);
if (m_worklist.m_queue.isEmpty())
return PollResult::Wait;
m_myPlans = WTFMove(m_worklist.m_queue);
m_worklist.m_numAvailableThreads--;
return PollResult::Work;
}
WorkResult work() final
{
RELEASE_ASSERT(!m_myPlans.isEmpty());
for (RefPtr<Plan>& plan : m_myPlans) {
plan->compileInThread();
plan = nullptr;
// Make sure that the main thread realizes that we just compiled something. Notifying
// a condition is basically free if nobody is waiting.
LockHolder locker(*m_worklist.m_lock);
m_worklist.m_condition->notifyAll(locker);
}
m_myPlans.clear();
LockHolder locker(*m_worklist.m_lock);
m_worklist.m_numAvailableThreads++;
return WorkResult::Continue;
}
JITWorklist& m_worklist;
Plans m_myPlans;
};
JITWorklist::JITWorklist()
: m_lock(Box<Lock>::create())
, m_condition(AutomaticThreadCondition::create())
{
LockHolder locker(*m_lock);
m_thread = new Thread(locker, *this);
}
JITWorklist::~JITWorklist()
{
UNREACHABLE_FOR_PLATFORM();
}
bool JITWorklist::completeAllForVM(VM& vm)
{
bool result = false;
DeferGC deferGC(vm.heap);
for (;;) {
Vector<RefPtr<Plan>, 32> myPlans;
{
LockHolder locker(*m_lock);
for (;;) {
bool didFindUnfinishedPlan = false;
m_plans.removeAllMatching(
[&] (RefPtr<Plan>& plan) {
if (&plan->vm() != &vm)
return false;
if (!plan->isFinishedCompiling()) {
didFindUnfinishedPlan = true;
return false;
}
myPlans.append(WTFMove(plan));
return true;
});
// If we found plans then we should finalize them now.
if (!myPlans.isEmpty())
break;
// If we don't find plans, then we're either done or we need to wait, depending on
// whether we found some unfinished plans.
if (!didFindUnfinishedPlan)
return result;
m_condition->wait(*m_lock);
}
}
RELEASE_ASSERT(!myPlans.isEmpty());
result = true;
finalizePlans(myPlans);
}
}
void JITWorklist::poll(VM& vm)
{
DeferGC deferGC(vm.heap);
Plans myPlans;
{
LockHolder locker(*m_lock);
m_plans.removeAllMatching(
[&] (RefPtr<Plan>& plan) {
if (&plan->vm() != &vm)
return false;
if (!plan->isFinishedCompiling())
return false;
myPlans.append(WTFMove(plan));
return true;
});
}
finalizePlans(myPlans);
}
void JITWorklist::compileLater(CodeBlock* codeBlock, BytecodeIndex loopOSREntryBytecodeIndex)
{
DeferGC deferGC(codeBlock->vm().heap);
RELEASE_ASSERT(codeBlock->jitType() == JITType::InterpreterThunk);
if (codeBlock->m_didFailJITCompilation) {
codeBlock->dontJITAnytimeSoon();
return;
}
if (!Options::useConcurrentJIT()) {
Plan::compileNow(codeBlock, loopOSREntryBytecodeIndex);
return;
}
codeBlock->jitSoon();
{
LockHolder locker(*m_lock);
if (m_planned.contains(codeBlock))
return;
if (m_numAvailableThreads) {
m_planned.add(codeBlock);
RefPtr<Plan> plan = adoptRef(new Plan(codeBlock, loopOSREntryBytecodeIndex));
m_plans.append(plan);
m_queue.append(plan);
m_condition->notifyAll(locker);
return;
}
}
// Compiling on the main thread if the helper thread isn't available is a defense against this
// pathology:
//
// 1) Do something that is allowed to take a while, like load a giant piece of initialization
// code. This plans the compile of the init code, but doesn't finish it. It will take a
// while.
//
// 2) Do something that is supposed to be quick. Now all baseline compiles, and so all DFG and
// FTL compiles, of everything is blocked on the long-running baseline compile of that
// initialization code.
//
// The single-threaded concurrent JIT has this tendency to convoy everything while at the same
// time postponing when it happens, which means that the convoy delays are less predictable.
// This works around the issue. If the concurrent JIT thread is convoyed, we revert to main
// thread compiles. This is probably not as good as if we had multiple JIT threads. Maybe we
// can do that someday.
Plan::compileNow(codeBlock, loopOSREntryBytecodeIndex);
}
void JITWorklist::compileNow(CodeBlock* codeBlock, BytecodeIndex loopOSREntryBytecodeIndex)
{
VM& vm = codeBlock->vm();
DeferGC deferGC(vm.heap);
if (codeBlock->jitType() != JITType::InterpreterThunk)
return;
bool isPlanned;
{
LockHolder locker(*m_lock);
isPlanned = m_planned.contains(codeBlock);
}
if (isPlanned) {
RELEASE_ASSERT(Options::useConcurrentJIT());
// This is expensive, but probably good enough.
completeAllForVM(vm);
}
// Now it might be compiled!
if (codeBlock->jitType() != JITType::InterpreterThunk)
return;
// We do this in case we had previously attempted, and then failed, to compile with the
// baseline JIT.
codeBlock->resetJITData();
// OK, just compile it.
JIT::compile(vm, codeBlock, JITCompilationMustSucceed, loopOSREntryBytecodeIndex);
codeBlock->ownerExecutable()->installCode(codeBlock);
}
void JITWorklist::finalizePlans(Plans& myPlans)
{
for (RefPtr<Plan>& plan : myPlans) {
plan->finalize();
LockHolder locker(*m_lock);
m_planned.remove(plan->codeBlock());
}
}
static JITWorklist* theGlobalJITWorklist { nullptr };
JITWorklist* JITWorklist::existingGlobalWorklistOrNull()
{
return theGlobalJITWorklist;
}
JITWorklist& JITWorklist::ensureGlobalWorklist()
{
static std::once_flag once;
std::call_once(
once,
[] {
auto* worklist = new JITWorklist();
WTF::storeStoreFence();
theGlobalJITWorklist = worklist;
});
return *theGlobalJITWorklist;
}
} // namespace JSC
#endif // ENABLE(JIT)