-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathCodeGenWorkItem.cpp
234 lines (205 loc) · 7.33 KB
/
CodeGenWorkItem.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
//-------------------------------------------------------------------------------------------------------
// Copyright (C) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
//-------------------------------------------------------------------------------------------------------
#include "Backend.h"
#include "Language/SourceDynamicProfileManager.h"
#include "NativeEntryPointData.h"
CodeGenWorkItem::CodeGenWorkItem(
JsUtil::JobManager *const manager,
Js::FunctionBody *const functionBody,
Js::EntryPointInfo* entryPointInfo,
bool isJitInDebugMode,
CodeGenWorkItemType type)
: JsUtil::Job(manager)
, functionBody(functionBody)
, entryPointInfo(entryPointInfo)
, recyclableData(nullptr)
, isInJitQueue(false)
, isAllocationCommitted(false)
, queuedFullJitWorkItem(nullptr)
, allocation(nullptr)
#ifdef IR_VIEWER
, isRejitIRViewerFunction(false)
, irViewerOutput(nullptr)
, irViewerRequestContext(nullptr)
#endif
{
this->jitData = {0};
// work item data
this->jitData.type = type;
this->jitData.isJitInDebugMode = isJitInDebugMode;
ResetJitMode();
}
CodeGenWorkItem::~CodeGenWorkItem()
{
if(queuedFullJitWorkItem)
{
HeapDelete(queuedFullJitWorkItem);
}
}
//
// Helps determine whether a function should be speculatively jitted.
// This function is only used once and is used in a time-critical area, so
// be careful with it (moving it around actually caused around a 5% perf
// regression on a test).
//
bool CodeGenWorkItem::ShouldSpeculativelyJit(uint byteCodeSizeGenerated) const
{
if(PHASE_OFF(Js::FullJitPhase, this->functionBody))
{
return false;
}
byteCodeSizeGenerated += this->GetByteCodeCount();
if(CONFIG_FLAG(ProfileBasedSpeculativeJit))
{
Assert(!CONFIG_ISENABLED(Js::NoDynamicProfileInMemoryCacheFlag));
// JIT this now if we are under the speculation cap.
return
byteCodeSizeGenerated < (uint)CONFIG_FLAG(SpeculationCap) ||
(
byteCodeSizeGenerated < (uint)CONFIG_FLAG(ProfileBasedSpeculationCap) &&
this->ShouldSpeculativelyJitBasedOnProfile()
);
}
else
{
return byteCodeSizeGenerated < (uint)CONFIG_FLAG(SpeculationCap);
}
}
bool CodeGenWorkItem::ShouldSpeculativelyJitBasedOnProfile() const
{
Js::FunctionBody* functionBody = this->GetFunctionBody();
uint loopPercentage = (functionBody->GetByteCodeInLoopCount()*100) / (functionBody->GetByteCodeCount() + 1);
uint straightLineSize = functionBody->GetByteCodeCount() - functionBody->GetByteCodeInLoopCount();
// This ensures only small and loopy functions are prejitted.
if(loopPercentage >= 50 || straightLineSize < 300)
{
Js::SourceDynamicProfileManager* profileManager = functionBody->GetSourceContextInfo()->sourceDynamicProfileManager;
if(profileManager != nullptr)
{
functionBody->SetIsSpeculativeJitCandidate();
if(!functionBody->HasDynamicProfileInfo())
{
return false;
}
Js::ExecutionFlags executionFlags = profileManager->IsFunctionExecuted(functionBody->GetLocalFunctionId());
if(executionFlags == Js::ExecutionFlags_Executed)
{
return true;
}
}
}
return false;
}
/*
A comment about how to cause certain phases to only be on:
INT = Interpreted, SJ = SimpleJit, FJ = FullJit
To get only the following levels on, use the flags:
INT: -noNative
SJ : -forceNative -off:fullJit
FJ : -forceNative -off:simpleJit
INT, SJ: -off:fullJit
INT, FJ: -off:simpleJit
SJ, FG: -forceNative
INT, SJ, FG: (default)
*/
void CodeGenWorkItem::OnAddToJitQueue()
{
Assert(!this->isInJitQueue);
this->isInJitQueue = true;
VerifyJitMode();
this->entryPointInfo->SetCodeGenQueued();
if(IS_JS_ETW(EventEnabledJSCRIPT_FUNCTION_JIT_QUEUED()))
{
WCHAR displayNameBuffer[256];
WCHAR* displayName = displayNameBuffer;
size_t sizeInChars = this->GetDisplayName(displayName, 256);
if(sizeInChars > 256)
{
displayName = HeapNewArray(WCHAR, sizeInChars);
this->GetDisplayName(displayName, 256);
}
JS_ETW(EventWriteJSCRIPT_FUNCTION_JIT_QUEUED(
this->GetFunctionNumber(),
displayName,
this->GetScriptContext(),
this->GetInterpretedCount()));
if(displayName != displayNameBuffer)
{
HeapDeleteArray(sizeInChars, displayName);
}
}
}
void CodeGenWorkItem::OnRemoveFromJitQueue(NativeCodeGenerator* generator)
{
// This is called from within the lock
this->isInJitQueue = false;
this->entryPointInfo->SetCodeGenPending();
functionBody->GetScriptContext()->GetThreadContext()->UnregisterCodeGenRecyclableData(this->recyclableData);
this->recyclableData = nullptr;
if(IS_JS_ETW(EventEnabledJSCRIPT_FUNCTION_JIT_DEQUEUED()))
{
WCHAR displayNameBuffer[256];
WCHAR* displayName = displayNameBuffer;
size_t sizeInChars = this->GetDisplayName(displayName, 256);
if(sizeInChars > 256)
{
displayName = HeapNewArray(WCHAR, sizeInChars);
this->GetDisplayName(displayName, 256);
}
JS_ETW(EventWriteJSCRIPT_FUNCTION_JIT_DEQUEUED(
this->GetFunctionNumber(),
displayName,
this->GetScriptContext(),
this->GetInterpretedCount()));
if(displayName != displayNameBuffer)
{
HeapDeleteArray(sizeInChars, displayName);
}
}
if(this->Type() == JsLoopBodyWorkItemType)
{
// Go ahead and delete it and let it re-queue if more interpreting of the loop happens
auto loopBodyWorkItem = static_cast<JsLoopBodyCodeGen*>(this);
loopBodyWorkItem->loopHeader->ResetInterpreterCount();
loopBodyWorkItem->GetEntryPoint()->Reset(true);
HeapDelete(loopBodyWorkItem);
}
else
{
Assert(GetJitMode() == ExecutionMode::FullJit); // simple JIT work items are not removed from the queue
GetFunctionBody()->OnFullJitDequeued(static_cast<Js::FunctionEntryPointInfo *>(GetEntryPoint()));
// Add it back to the list of available functions to be jitted
generator->AddWorkItem(this);
}
}
void CodeGenWorkItem::OnWorkItemProcessFail(NativeCodeGenerator* codeGen)
{
if (!isAllocationCommitted && this->allocation != nullptr && this->allocation->allocation != nullptr)
{
#if DBG
this->allocation->allocation->isNotExecutableBecauseOOM = true;
#endif
#if PDATA_ENABLED & defined(_WIN32)
if (this->entryPointInfo)
{
this->entryPointInfo->GetNativeEntryPointData()->CleanupXDataInfo();
}
#endif
codeGen->FreeNativeCodeGenAllocation(this->allocation->allocation->address);
}
}
QueuedFullJitWorkItem *CodeGenWorkItem::GetQueuedFullJitWorkItem() const
{
return queuedFullJitWorkItem;
}
QueuedFullJitWorkItem *CodeGenWorkItem::EnsureQueuedFullJitWorkItem()
{
if(queuedFullJitWorkItem)
{
return queuedFullJitWorkItem;
}
queuedFullJitWorkItem = HeapNewNoThrow(QueuedFullJitWorkItem, this);
return queuedFullJitWorkItem;
}