| 1 |
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
| 2 |
* vim: set ts=8 sts=2 et sw=2 tw=80: |
| 3 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 4 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 6 |
|
| 7 |
#include "jit/Ion.h" |
| 8 |
|
| 9 |
#include "mozilla/CheckedInt.h" |
| 10 |
#include "mozilla/DebugOnly.h" |
| 11 |
#include "mozilla/IntegerPrintfMacros.h" |
| 12 |
#include "mozilla/MemoryReporting.h" |
| 13 |
#include "mozilla/ThreadLocal.h" |
| 14 |
|
| 15 |
#include "gc/GCContext.h" |
| 16 |
#include "gc/PublicIterators.h" |
| 17 |
#include "jit/AliasAnalysis.h" |
| 18 |
#include "jit/AlignmentMaskAnalysis.h" |
| 19 |
#include "jit/AutoWritableJitCode.h" |
| 20 |
#include "jit/BacktrackingAllocator.h" |
| 21 |
#include "jit/BaselineFrame.h" |
| 22 |
#include "jit/BaselineJIT.h" |
| 23 |
#include "jit/BranchHinting.h" |
| 24 |
#include "jit/CodeGenerator.h" |
| 25 |
#include "jit/CompileInfo.h" |
| 26 |
#include "jit/DominatorTree.h" |
| 27 |
#include "jit/EdgeCaseAnalysis.h" |
| 28 |
#include "jit/EffectiveAddressAnalysis.h" |
| 29 |
#include "jit/ExecutableAllocator.h" |
| 30 |
#include "jit/FoldLinearArithConstants.h" |
| 31 |
#include "jit/InlineScriptTree.h" |
| 32 |
#include "jit/InstructionReordering.h" |
| 33 |
#include "jit/Invalidation.h" |
| 34 |
#include "jit/InvalidationScriptSet.h" |
| 35 |
#include "jit/IonAnalysis.h" |
| 36 |
#include "jit/IonCompileTask.h" |
| 37 |
#include "jit/IonIC.h" |
| 38 |
#include "jit/IonOptimizationLevels.h" |
| 39 |
#include "jit/IonScript.h" |
| 40 |
#include "jit/JitcodeMap.h" |
| 41 |
#include "jit/JitFrames.h" |
| 42 |
#include "jit/JitRuntime.h" |
| 43 |
#include "jit/JitSpewer.h" |
| 44 |
#include "jit/JitZone.h" |
| 45 |
#include "jit/LICM.h" |
| 46 |
#include "jit/Linker.h" |
| 47 |
#include "jit/LIR.h" |
| 48 |
#include "jit/Lowering.h" |
| 49 |
#include "jit/PerfSpewer.h" |
| 50 |
#include "jit/RangeAnalysis.h" |
| 51 |
#include "jit/ScalarReplacement.h" |
| 52 |
#include "jit/ScriptFromCalleeToken.h" |
| 53 |
#include "jit/SimpleAllocator.h" |
| 54 |
#include "jit/Sink.h" |
| 55 |
#include "jit/UnrollLoops.h" |
| 56 |
#include "jit/ValueNumbering.h" |
| 57 |
#include "jit/WarpBuilder.h" |
| 58 |
#include "jit/WarpOracle.h" |
| 59 |
#include "jit/WasmBCE.h" |
| 60 |
#include "js/Printf.h" |
| 61 |
#include "js/UniquePtr.h" |
| 62 |
#include "util/Memory.h" |
| 63 |
#include "util/WindowsWrapper.h" |
| 64 |
#include "vm/HelperThreads.h" |
| 65 |
#include "vm/Realm.h" |
| 66 |
#ifdef MOZ_VTUNE |
| 67 |
# include "vtune/VTuneWrapper.h" |
| 68 |
#endif |
| 69 |
|
| 70 |
#include "gc/GC-inl.h" |
| 71 |
#include "gc/StableCellHasher-inl.h" |
| 72 |
#include "jit/InlineScriptTree-inl.h" |
| 73 |
#include "jit/MacroAssembler-inl.h" |
| 74 |
#include "jit/SafepointIndex-inl.h" |
| 75 |
#include "vm/GeckoProfiler-inl.h" |
| 76 |
#include "vm/JSContext-inl.h" |
| 77 |
#include "vm/JSScript-inl.h" |
| 78 |
#include "vm/Realm-inl.h" |
| 79 |
|
| 80 |
#if defined(ANDROID) |
| 81 |
# include <sys/system_properties.h> |
| 82 |
#endif |
| 83 |
|
| 84 |
using mozilla::CheckedInt; |
| 85 |
using mozilla::DebugOnly; |
| 86 |
|
| 87 |
using namespace js; |
| 88 |
using namespace js::jit; |
| 89 |
|
| 90 |
JitRuntime::~JitRuntime() { |
| 91 |
MOZ_ASSERT(numFinishedOffThreadTasks_ == 0); |
| 92 |
MOZ_ASSERT(ionLazyLinkListSize_ == 0); |
| 93 |
MOZ_ASSERT(ionLazyLinkList_.ref().isEmpty()); |
| 94 |
|
| 95 |
MOZ_ASSERT(ionFreeTaskBatch_.ref().empty()); |
| 96 |
|
| 97 |
// By this point, the jitcode global table should be empty. |
| 98 |
MOZ_ASSERT_IF(jitcodeGlobalTable_, jitcodeGlobalTable_->empty()); |
| 99 |
js_delete(jitcodeGlobalTable_.ref()); |
| 100 |
|
| 101 |
// interpreterEntryMap should be cleared out during finishRoots() |
| 102 |
MOZ_ASSERT_IF(interpreterEntryMap_, interpreterEntryMap_->empty()); |
| 103 |
js_delete(interpreterEntryMap_.ref()); |
| 104 |
|
| 105 |
js_delete(jitHintsMap_.ref()); |
| 106 |
} |
| 107 |
|
| 108 |
uint32_t JitRuntime::startTrampolineCode(MacroAssembler& masm) { |
| 109 |
AutoCreatedBy acb(masm, "startTrampolineCode"); |
| 110 |
|
| 111 |
masm.assumeUnreachable("Shouldn't get here"); |
| 112 |
masm.flushBuffer(); |
| 113 |
masm.haltingAlign(CodeAlignment); |
| 114 |
masm.setFramePushed(0); |
| 115 |
return masm.currentOffset(); |
| 116 |
} |
| 117 |
|
| 118 |
bool JitRuntime::initialize(JSContext* cx) { |
| 119 |
MOZ_ASSERT(CurrentThreadCanAccessRuntime(cx->runtime())); |
| 120 |
|
| 121 |
AutoAllocInAtomsZone az(cx); |
| 122 |
JitContext jctx(cx); |
| 123 |
|
| 124 |
if (!generateTrampolines(cx)) { |
| 125 |
return false; |
| 126 |
} |
| 127 |
|
| 128 |
if (!generateBaselineICFallbackCode(cx)) { |
| 129 |
return false; |
| 130 |
} |
| 131 |
|
| 132 |
jitcodeGlobalTable_ = cx->new_<JitcodeGlobalTable>(); |
| 133 |
if (!jitcodeGlobalTable_) { |
| 134 |
return false; |
| 135 |
} |
| 136 |
|
| 137 |
if (!JitOptions.disableJitHints) { |
| 138 |
jitHintsMap_ = cx->new_<JitHintsMap>(); |
| 139 |
if (!jitHintsMap_) { |
| 140 |
return false; |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
if (JitOptions.emitInterpreterEntryTrampoline) { |
| 145 |
interpreterEntryMap_ = cx->new_<EntryTrampolineMap>(); |
| 146 |
if (!interpreterEntryMap_) { |
| 147 |
return false; |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
if (!GenerateBaselineInterpreter(cx, baselineInterpreter_)) { |
| 152 |
return false; |
| 153 |
} |
| 154 |
|
| 155 |
// Initialize the jitCodeRaw of the Runtime's canonical SelfHostedLazyScript |
| 156 |
// to point to the interpreter trampoline. |
| 157 |
cx->runtime()->selfHostedLazyScript.ref().jitCodeRaw_ = |
| 158 |
interpreterStub().value; |
| 159 |
|
| 160 |
return true; |
| 161 |
} |
| 162 |
|
| 163 |
bool JitRuntime::generateTrampolines(JSContext* cx) { |
| 164 |
TempAllocator temp(&cx->tempLifoAlloc()); |
| 165 |
StackMacroAssembler masm(cx, temp); |
| 166 |
PerfSpewerRangeRecorder rangeRecorder(masm); |
| 167 |
|
| 168 |
Label bailoutTail; |
| 169 |
JitSpew(JitSpew_Codegen, "# Emitting bailout tail stub"); |
| 170 |
generateBailoutTailStub(masm, &bailoutTail); |
| 171 |
|
| 172 |
JitSpew(JitSpew_Codegen, "# Emitting bailout handler"); |
| 173 |
generateBailoutHandler(masm, &bailoutTail); |
| 174 |
rangeRecorder.recordOffset("Trampoline: Bailout"); |
| 175 |
|
| 176 |
JitSpew(JitSpew_Codegen, "# Emitting invalidator"); |
| 177 |
generateInvalidator(masm, &bailoutTail); |
| 178 |
rangeRecorder.recordOffset("Trampoline: Invalidator"); |
| 179 |
|
| 180 |
// The arguments rectifier has to use the same frame layout as the function |
| 181 |
// frames it rectifies. |
| 182 |
static_assert(std::is_base_of_v<JitFrameLayout, RectifierFrameLayout>, |
| 183 |
"a rectifier frame can be used with jit frame"); |
| 184 |
static_assert(std::is_base_of_v<JitFrameLayout, WasmToJSJitFrameLayout>, |
| 185 |
"wasm frames simply are jit frames"); |
| 186 |
static_assert(sizeof(JitFrameLayout) == sizeof(WasmToJSJitFrameLayout), |
| 187 |
"thus a rectifier frame can be used with a wasm frame"); |
| 188 |
|
| 189 |
JitSpew(JitSpew_Codegen, "# Emitting arguments rectifier"); |
| 190 |
generateArgumentsRectifier(masm, ArgumentsRectifierKind::Normal); |
| 191 |
rangeRecorder.recordOffset("Trampoline: Arguments Rectifier"); |
| 192 |
|
| 193 |
JitSpew(JitSpew_Codegen, "# Emitting trial inlining arguments rectifier"); |
| 194 |
generateArgumentsRectifier(masm, ArgumentsRectifierKind::TrialInlining); |
| 195 |
rangeRecorder.recordOffset( |
| 196 |
"Trampoline: Arguments Rectifier (Trial Inlining)"); |
| 197 |
|
| 198 |
JitSpew(JitSpew_Codegen, "# Emitting EnterJIT sequence"); |
| 199 |
generateEnterJIT(cx, masm); |
| 200 |
rangeRecorder.recordOffset("Trampoline: EnterJIT"); |
| 201 |
|
| 202 |
JitSpew(JitSpew_Codegen, "# Emitting Pre Barrier for Value"); |
| 203 |
valuePreBarrierOffset_ = generatePreBarrier(cx, masm, MIRType::Value); |
| 204 |
rangeRecorder.recordOffset("Trampoline: PreBarrier Value"); |
| 205 |
|
| 206 |
JitSpew(JitSpew_Codegen, "# Emitting Pre Barrier for String"); |
| 207 |
stringPreBarrierOffset_ = generatePreBarrier(cx, masm, MIRType::String); |
| 208 |
rangeRecorder.recordOffset("Trampoline: PreBarrier String"); |
| 209 |
|
| 210 |
JitSpew(JitSpew_Codegen, "# Emitting Pre Barrier for Object"); |
| 211 |
objectPreBarrierOffset_ = generatePreBarrier(cx, masm, MIRType::Object); |
| 212 |
rangeRecorder.recordOffset("Trampoline: PreBarrier Object"); |
| 213 |
|
| 214 |
JitSpew(JitSpew_Codegen, "# Emitting Pre Barrier for Shape"); |
| 215 |
shapePreBarrierOffset_ = generatePreBarrier(cx, masm, MIRType::Shape); |
| 216 |
rangeRecorder.recordOffset("Trampoline: PreBarrier Shape"); |
| 217 |
|
| 218 |
JitSpew(JitSpew_Codegen, "# Emitting Pre Barrier for WasmAnyRef"); |
| 219 |
wasmAnyRefPreBarrierOffset_ = |
| 220 |
generatePreBarrier(cx, masm, MIRType::WasmAnyRef); |
| 221 |
rangeRecorder.recordOffset("Trampoline: PreBarrier WasmAnyRef"); |
| 222 |
|
| 223 |
JitSpew(JitSpew_Codegen, "# Emitting lazy link stub"); |
| 224 |
generateLazyLinkStub(masm); |
| 225 |
rangeRecorder.recordOffset("Trampoline: LazyLinkStub"); |
| 226 |
|
| 227 |
JitSpew(JitSpew_Codegen, "# Emitting interpreter stub"); |
| 228 |
generateInterpreterStub(masm); |
| 229 |
rangeRecorder.recordOffset("Trampoline: Interpreter"); |
| 230 |
|
| 231 |
JitSpew(JitSpew_Codegen, "# Emitting double-to-int32-value stub"); |
| 232 |
generateDoubleToInt32ValueStub(masm); |
| 233 |
rangeRecorder.recordOffset("Trampoline: DoubleToInt32ValueStub"); |
| 234 |
|
| 235 |
JitSpew(JitSpew_Codegen, "# Emitting VM function wrappers"); |
| 236 |
if (!generateVMWrappers(cx, masm, rangeRecorder)) { |
| 237 |
return false; |
| 238 |
} |
| 239 |
|
| 240 |
JitSpew(JitSpew_Codegen, "# Emitting profiler exit frame tail stub"); |
| 241 |
Label profilerExitTail; |
| 242 |
generateProfilerExitFrameTailStub(masm, &profilerExitTail); |
| 243 |
rangeRecorder.recordOffset("Trampoline: ProfilerExitFrameTailStub"); |
| 244 |
|
| 245 |
JitSpew(JitSpew_Codegen, "# Emitting exception tail stub"); |
| 246 |
generateExceptionTailStub(masm, &profilerExitTail, &bailoutTail); |
| 247 |
rangeRecorder.recordOffset("Trampoline: ExceptionTailStub"); |
| 248 |
|
| 249 |
JitSpew(JitSpew_Codegen, "# Emitting Ion generic call stub"); |
| 250 |
generateIonGenericCallStub(masm, IonGenericCallKind::Call); |
| 251 |
rangeRecorder.recordOffset("Trampoline: IonGenericCall"); |
| 252 |
|
| 253 |
JitSpew(JitSpew_Codegen, "# Emitting Ion generic construct stub"); |
| 254 |
generateIonGenericCallStub(masm, IonGenericCallKind::Construct); |
| 255 |
rangeRecorder.recordOffset("Trampoline: IonGenericConstruct"); |
| 256 |
|
| 257 |
JitSpew(JitSpew_Codegen, "# Emitting trampoline natives"); |
| 258 |
TrampolineNativeJitEntryOffsets nativeOffsets; |
| 259 |
generateTrampolineNatives(masm, nativeOffsets, rangeRecorder); |
| 260 |
|
| 261 |
Linker linker(masm); |
| 262 |
trampolineCode_ = linker.newCode(cx, CodeKind::Other); |
| 263 |
if (!trampolineCode_) { |
| 264 |
return false; |
| 265 |
} |
| 266 |
|
| 267 |
rangeRecorder.collectRangesForJitCode(trampolineCode_); |
| 268 |
#ifdef MOZ_VTUNE |
| 269 |
vtune::MarkStub(trampolineCode_, "Trampolines"); |
| 270 |
#endif |
| 271 |
|
| 272 |
// Initialize TrampolineNative JitEntry array. |
| 273 |
for (size_t i = 0; i < size_t(TrampolineNative::Count); i++) { |
| 274 |
TrampolineNative native = TrampolineNative(i); |
| 275 |
uint32_t offset = nativeOffsets[native]; |
| 276 |
MOZ_ASSERT(offset > 0 && offset < trampolineCode_->instructionsSize()); |
| 277 |
trampolineNativeJitEntries_[native] = trampolineCode_->raw() + offset; |
| 278 |
} |
| 279 |
|
| 280 |
return true; |
| 281 |
} |
| 282 |
|
| 283 |
bool JitRuntime::ensureDebugTrapHandler(JSContext* cx, |
| 284 |
DebugTrapHandlerKind kind) { |
| 285 |
if (debugTrapHandlers_[kind]) { |
| 286 |
return true; |
| 287 |
} |
| 288 |
|
| 289 |
// JitRuntime code stubs are shared across compartments and have to |
| 290 |
// be allocated in the atoms zone. |
| 291 |
mozilla::Maybe<AutoAllocInAtomsZone> az; |
| 292 |
if (!cx->zone()->isAtomsZone()) { |
| 293 |
az.emplace(cx); |
| 294 |
} |
| 295 |
debugTrapHandlers_[kind] = generateDebugTrapHandler(cx, kind); |
| 296 |
return debugTrapHandlers_[kind]; |
| 297 |
} |
| 298 |
|
| 299 |
JitRuntime::IonCompileTaskList& JitRuntime::ionLazyLinkList(JSRuntime* rt) { |
| 300 |
MOZ_ASSERT(CurrentThreadCanAccessRuntime(rt), |
| 301 |
"Should only be mutated by the main thread."); |
| 302 |
return ionLazyLinkList_.ref(); |
| 303 |
} |
| 304 |
|
| 305 |
void JitRuntime::ionLazyLinkListRemove(JSRuntime* rt, |
| 306 |
jit::IonCompileTask* task) { |
| 307 |
MOZ_ASSERT(CurrentThreadCanAccessRuntime(rt), |
| 308 |
"Should only be mutated by the main thread."); |
| 309 |
MOZ_ASSERT(rt == task->script()->runtimeFromMainThread()); |
| 310 |
MOZ_ASSERT(ionLazyLinkListSize_ > 0); |
| 311 |
|
| 312 |
task->removeFrom(ionLazyLinkList(rt)); |
| 313 |
ionLazyLinkListSize_--; |
| 314 |
|
| 315 |
MOZ_ASSERT(ionLazyLinkList(rt).isEmpty() == (ionLazyLinkListSize_ == 0)); |
| 316 |
} |
| 317 |
|
| 318 |
void JitRuntime::ionLazyLinkListAdd(JSRuntime* rt, jit::IonCompileTask* task) { |
| 319 |
MOZ_ASSERT(CurrentThreadCanAccessRuntime(rt), |
| 320 |
"Should only be mutated by the main thread."); |
| 321 |
MOZ_ASSERT(rt == task->script()->runtimeFromMainThread()); |
| 322 |
ionLazyLinkList(rt).insertFront(task); |
| 323 |
ionLazyLinkListSize_++; |
| 324 |
} |
| 325 |
|
| 326 |
uint8_t* JitRuntime::allocateIonOsrTempData(size_t size) { |
| 327 |
MOZ_ASSERT(size > 0); |
| 328 |
|
| 329 |
uint8_t* prevBuffer = ionOsrTempData_.ref().get(); |
| 330 |
size_t prevSize = ionOsrTempDataSize_.ref(); |
| 331 |
MOZ_ASSERT((prevSize > 0) == !!prevBuffer); |
| 332 |
|
| 333 |
// Reuse the previous buffer if possible. |
| 334 |
if (prevSize >= size) { |
| 335 |
return prevBuffer; |
| 336 |
} |
| 337 |
|
| 338 |
// Allocate or resize the buffer. |
| 339 |
uint8_t* buffer = js_pod_realloc<uint8_t>(prevBuffer, prevSize, size); |
| 340 |
if (!buffer) { |
| 341 |
// ionOsrTempData_ is still valid. |
| 342 |
return nullptr; |
| 343 |
} |
| 344 |
// ionOsrTempData_ is no longer valid. |
| 345 |
(void)ionOsrTempData_.ref().release(); |
| 346 |
ionOsrTempData_.ref().reset(buffer); |
| 347 |
ionOsrTempDataSize_ = size; |
| 348 |
return buffer; |
| 349 |
} |
| 350 |
|
| 351 |
void JitRuntime::freeIonOsrTempData() { |
| 352 |
ionOsrTempData_.ref().reset(); |
| 353 |
ionOsrTempDataSize_ = 0; |
| 354 |
} |
| 355 |
|
| 356 |
static bool LinkCodeGen(JSContext* cx, CodeGenerator* codegen, |
| 357 |
HandleScript script) { |
| 358 |
if (!codegen->link(cx)) { |
| 359 |
return false; |
| 360 |
} |
| 361 |
|
| 362 |
// Record Ion compile time in glean. |
| 363 |
if (mozilla::TimeDuration compileTime = codegen->getCompilationTime()) { |
| 364 |
cx->metrics().ION_COMPILE_TIME(compileTime); |
| 365 |
} |
| 366 |
|
| 367 |
return true; |
| 368 |
} |
| 369 |
|
| 370 |
static bool LinkBackgroundCodeGen(JSContext* cx, IonCompileTask* task) { |
| 371 |
CodeGenerator* codegen = task->backgroundCodegen(); |
| 372 |
if (!codegen) { |
| 373 |
return false; |
| 374 |
} |
| 375 |
|
| 376 |
JitContext jctx(cx); |
| 377 |
RootedScript script(cx, task->script()); |
| 378 |
return LinkCodeGen(cx, codegen, script); |
| 379 |
} |
| 380 |
|
| 381 |
void jit::LinkIonScript(JSContext* cx, HandleScript calleeScript) { |
| 382 |
// Get the pending IonCompileTask from the script. |
| 383 |
MOZ_ASSERT(calleeScript->hasBaselineScript()); |
| 384 |
IonCompileTask* task = |
| 385 |
calleeScript->baselineScript()->pendingIonCompileTask(); |
| 386 |
calleeScript->baselineScript()->removePendingIonCompileTask(cx->runtime(), |
| 387 |
calleeScript); |
| 388 |
|
| 389 |
// Remove from pending. |
| 390 |
cx->runtime()->jitRuntime()->ionLazyLinkListRemove(cx->runtime(), task); |
| 391 |
|
| 392 |
{ |
| 393 |
gc::AutoSuppressGC suppressGC(cx); |
| 394 |
if (!LinkBackgroundCodeGen(cx, task)) { |
| 395 |
// Silently ignore OOM during code generation. The assembly code |
| 396 |
// doesn't have code to handle it after linking happened. So it's |
| 397 |
// not OK to throw a catchable exception from there. |
| 398 |
cx->clearPendingException(); |
| 399 |
} |
| 400 |
} |
| 401 |
|
| 402 |
AutoStartIonFreeTask freeTask(cx->runtime()->jitRuntime()); |
| 403 |
FinishOffThreadTask(cx->runtime(), freeTask, task); |
| 404 |
} |
| 405 |
|
| 406 |
uint8_t* jit::LazyLinkTopActivation(JSContext* cx, |
| 407 |
LazyLinkExitFrameLayout* frame) { |
| 408 |
RootedScript calleeScript( |
| 409 |
cx, ScriptFromCalleeToken(frame->jsFrame()->calleeToken())); |
| 410 |
|
| 411 |
LinkIonScript(cx, calleeScript); |
| 412 |
|
| 413 |
MOZ_ASSERT(calleeScript->hasBaselineScript()); |
| 414 |
MOZ_ASSERT(calleeScript->jitCodeRaw()); |
| 415 |
|
| 416 |
return calleeScript->jitCodeRaw(); |
| 417 |
} |
| 418 |
|
| 419 |
/* static */ |
| 420 |
void JitRuntime::TraceAtomZoneRoots(JSTracer* trc) { |
| 421 |
// Shared stubs are allocated in the atoms zone, so do not iterate |
| 422 |
// them after the atoms heap after it has been "finished." |
| 423 |
if (trc->runtime()->atomsAreFinished()) { |
| 424 |
return; |
| 425 |
} |
| 426 |
|
| 427 |
Zone* zone = trc->runtime()->atomsZone(); |
| 428 |
for (auto i = zone->cellIterUnsafe<JitCode>(); !i.done(); i.next()) { |
| 429 |
JitCode* code = i; |
| 430 |
TraceRoot(trc, &code, "wrapper"); |
| 431 |
} |
| 432 |
} |
| 433 |
|
| 434 |
/* static */ |
| 435 |
bool JitRuntime::MarkJitcodeGlobalTableIteratively(GCMarker* marker) { |
| 436 |
if (marker->runtime()->hasJitRuntime() && |
| 437 |
marker->runtime()->jitRuntime()->hasJitcodeGlobalTable()) { |
| 438 |
return marker->runtime() |
| 439 |
->jitRuntime() |
| 440 |
->getJitcodeGlobalTable() |
| 441 |
->markIteratively(marker); |
| 442 |
} |
| 443 |
return false; |
| 444 |
} |
| 445 |
|
| 446 |
/* static */ |
| 447 |
void JitRuntime::TraceWeakJitcodeGlobalTable(JSRuntime* rt, JSTracer* trc) { |
| 448 |
if (rt->hasJitRuntime() && rt->jitRuntime()->hasJitcodeGlobalTable()) { |
| 449 |
rt->jitRuntime()->getJitcodeGlobalTable()->traceWeak(rt, trc); |
| 450 |
} |
| 451 |
} |
| 452 |
|
| 453 |
bool JitZone::addInlinedCompilation(const RecompileInfo& info, |
| 454 |
JSScript* inlined) { |
| 455 |
MOZ_ASSERT(inlined != info.script()); |
| 456 |
|
| 457 |
auto p = inlinedCompilations_.lookupForAdd(inlined); |
| 458 |
if (p) { |
| 459 |
auto& compilations = p->value(); |
| 460 |
if (!compilations.empty() && compilations.back() == info) { |
| 461 |
return true; |
| 462 |
} |
| 463 |
return compilations.append(info); |
| 464 |
} |
| 465 |
|
| 466 |
RecompileInfoVector compilations; |
| 467 |
if (!compilations.append(info)) { |
| 468 |
return false; |
| 469 |
} |
| 470 |
return inlinedCompilations_.add(p, inlined, std::move(compilations)); |
| 471 |
} |
| 472 |
|
| 473 |
void jit::AddPendingInvalidation(RecompileInfoVector& invalid, |
| 474 |
JSScript* script) { |
| 475 |
MOZ_ASSERT(script); |
| 476 |
|
| 477 |
CancelOffThreadIonCompile(script); |
| 478 |
|
| 479 |
// Let the script warm up again before attempting another compile. |
| 480 |
script->resetWarmUpCounterToDelayIonCompilation(); |
| 481 |
|
| 482 |
JitScript* jitScript = script->maybeJitScript(); |
| 483 |
if (!jitScript) { |
| 484 |
return; |
| 485 |
} |
| 486 |
|
| 487 |
auto addPendingInvalidation = [&invalid](const RecompileInfo& info) { |
| 488 |
AutoEnterOOMUnsafeRegion oomUnsafe; |
| 489 |
if (!invalid.append(info)) { |
| 490 |
// BUG 1536159: For diagnostics, compute the size of the failed |
| 491 |
// allocation. This presumes the vector growth strategy is to double. This |
| 492 |
// is only used for crash reporting so not a problem if we get it wrong. |
| 493 |
size_t allocSize = 2 * sizeof(RecompileInfo) * invalid.capacity(); |
| 494 |
oomUnsafe.crash(allocSize, "Could not update RecompileInfoVector"); |
| 495 |
} |
| 496 |
}; |
| 497 |
|
| 498 |
// Trigger invalidation of the IonScript. |
| 499 |
if (jitScript->hasIonScript()) { |
| 500 |
RecompileInfo info(script, jitScript->ionScript()->compilationId()); |
| 501 |
addPendingInvalidation(info); |
| 502 |
} |
| 503 |
|
| 504 |
// Trigger invalidation of any callers inlining this script. |
| 505 |
auto* inlinedCompilations = |
| 506 |
script->zone()->jitZone()->maybeInlinedCompilations(script); |
| 507 |
if (inlinedCompilations) { |
| 508 |
for (const RecompileInfo& info : *inlinedCompilations) { |
| 509 |
addPendingInvalidation(info); |
| 510 |
} |
| 511 |
script->zone()->jitZone()->removeInlinedCompilations(script); |
| 512 |
} |
| 513 |
} |
| 514 |
|
| 515 |
IonScript* RecompileInfo::maybeIonScriptToInvalidate() const { |
| 516 |
// Make sure this is not called under CodeGenerator::link (before the |
| 517 |
// IonScript is created). |
| 518 |
MOZ_ASSERT_IF( |
| 519 |
script_->zone()->jitZone()->currentCompilationId(), |
| 520 |
script_->zone()->jitZone()->currentCompilationId().ref() != id_); |
| 521 |
|
| 522 |
if (!script_->hasIonScript() || |
| 523 |
script_->ionScript()->compilationId() != id_) { |
| 524 |
return nullptr; |
| 525 |
} |
| 526 |
|
| 527 |
return script_->ionScript(); |
| 528 |
} |
| 529 |
|
| 530 |
bool RecompileInfo::traceWeak(JSTracer* trc) { |
| 531 |
// Sweep the RecompileInfo if either the script is dead or the IonScript has |
| 532 |
// been invalidated. |
| 533 |
|
| 534 |
if (!TraceManuallyBarrieredWeakEdge(trc, &script_, "RecompileInfo::script")) { |
| 535 |
return false; |
| 536 |
} |
| 537 |
|
| 538 |
return maybeIonScriptToInvalidate() != nullptr; |
| 539 |
} |
| 540 |
|
| 541 |
void JitZone::traceWeak(JSTracer* trc, Zone* zone) { |
| 542 |
MOZ_ASSERT(this == zone->jitZone()); |
| 543 |
|
| 544 |
for (WeakHeapPtr<JitCode*>& stub : stubs_) { |
| 545 |
TraceWeakEdge(trc, &stub, "JitZone::stubs_"); |
| 546 |
} |
| 547 |
|
| 548 |
baselineCacheIRStubCodes_.traceWeak(trc); |
| 549 |
inlinedCompilations_.traceWeak(trc); |
| 550 |
|
| 551 |
TraceWeakEdge(trc, &lastStubFoldingBailoutChild_, |
| 552 |
"JitZone::lastStubFoldingBailoutChild_"); |
| 553 |
TraceWeakEdge(trc, &lastStubFoldingBailoutParent_, |
| 554 |
"JitZone::lastStubFoldingBailoutParent_"); |
| 555 |
} |
| 556 |
|
| 557 |
void JitZone::addSizeOfIncludingThis(mozilla::MallocSizeOf mallocSizeOf, |
| 558 |
JS::CodeSizes* code, size_t* jitZone, |
| 559 |
size_t* cacheIRStubs) const { |
| 560 |
*jitZone += mallocSizeOf(this); |
| 561 |
*jitZone += |
| 562 |
baselineCacheIRStubCodes_.shallowSizeOfExcludingThis(mallocSizeOf); |
| 563 |
*jitZone += ionCacheIRStubInfoSet_.shallowSizeOfExcludingThis(mallocSizeOf); |
| 564 |
|
| 565 |
execAlloc().addSizeOfCode(code); |
| 566 |
|
| 567 |
*cacheIRStubs += stubSpace_.sizeOfExcludingThis(mallocSizeOf); |
| 568 |
} |
| 569 |
|
| 570 |
void JitCodeHeader::init(JitCode* jitCode) { |
| 571 |
// As long as JitCode isn't moveable, we can avoid tracing this and |
| 572 |
// mutating executable data. |
| 573 |
MOZ_ASSERT(!gc::IsMovableKind(gc::AllocKind::JITCODE)); |
| 574 |
jitCode_ = jitCode; |
| 575 |
} |
| 576 |
|
| 577 |
template <AllowGC allowGC> |
| 578 |
JitCode* JitCode::New(JSContext* cx, uint8_t* code, uint32_t totalSize, |
| 579 |
uint32_t headerSize, ExecutablePool* pool, |
| 580 |
CodeKind kind) { |
| 581 |
uint32_t bufferSize = totalSize - headerSize; |
| 582 |
JitCode* codeObj = |
| 583 |
cx->newCell<JitCode, allowGC>(code, bufferSize, headerSize, pool, kind); |
| 584 |
if (!codeObj) { |
| 585 |
// The caller already allocated `totalSize` bytes of executable memory. |
| 586 |
pool->release(totalSize, kind); |
| 587 |
return nullptr; |
| 588 |
} |
| 589 |
|
| 590 |
cx->zone()->incJitMemory(totalSize); |
| 591 |
|
| 592 |
return codeObj; |
| 593 |
} |
| 594 |
|
| 595 |
template JitCode* JitCode::New<CanGC>(JSContext* cx, uint8_t* code, |
| 596 |
uint32_t bufferSize, uint32_t headerSize, |
| 597 |
ExecutablePool* pool, CodeKind kind); |
| 598 |
|
| 599 |
template JitCode* JitCode::New<NoGC>(JSContext* cx, uint8_t* code, |
| 600 |
uint32_t bufferSize, uint32_t headerSize, |
| 601 |
ExecutablePool* pool, CodeKind kind); |
| 602 |
|
| 603 |
void JitCode::copyFrom(MacroAssembler& masm) { |
| 604 |
// Store the JitCode pointer in the JitCodeHeader so we can recover the |
| 605 |
// gcthing from relocation tables. |
| 606 |
JitCodeHeader::FromExecutable(raw())->init(this); |
| 607 |
|
| 608 |
insnSize_ = masm.instructionsSize(); |
| 609 |
masm.executableCopy(raw()); |
| 610 |
|
| 611 |
jumpRelocTableBytes_ = masm.jumpRelocationTableBytes(); |
| 612 |
masm.copyJumpRelocationTable(raw() + jumpRelocTableOffset()); |
| 613 |
|
| 614 |
dataRelocTableBytes_ = masm.dataRelocationTableBytes(); |
| 615 |
masm.copyDataRelocationTable(raw() + dataRelocTableOffset()); |
| 616 |
|
| 617 |
masm.processCodeLabels(raw()); |
| 618 |
} |
| 619 |
|
| 620 |
void JitCode::traceChildren(JSTracer* trc) { |
| 621 |
// Note that we cannot mark invalidated scripts, since we've basically |
| 622 |
// corrupted the code stream by injecting bailouts. |
| 623 |
if (invalidated()) { |
| 624 |
return; |
| 625 |
} |
| 626 |
|
| 627 |
if (jumpRelocTableBytes_) { |
| 628 |
uint8_t* start = raw() + jumpRelocTableOffset(); |
| 629 |
CompactBufferReader reader(start, start + jumpRelocTableBytes_); |
| 630 |
MacroAssembler::TraceJumpRelocations(trc, this, reader); |
| 631 |
} |
| 632 |
if (dataRelocTableBytes_) { |
| 633 |
uint8_t* start = raw() + dataRelocTableOffset(); |
| 634 |
CompactBufferReader reader(start, start + dataRelocTableBytes_); |
| 635 |
MacroAssembler::TraceDataRelocations(trc, this, reader); |
| 636 |
} |
| 637 |
} |
| 638 |
|
| 639 |
void JitCode::finalize(JS::GCContext* gcx) { |
| 640 |
// If this jitcode had a bytecode map, it must have already been removed. |
| 641 |
#ifdef DEBUG |
| 642 |
JSRuntime* rt = gcx->runtime(); |
| 643 |
if (hasBytecodeMap_) { |
| 644 |
MOZ_ASSERT(rt->jitRuntime()->hasJitcodeGlobalTable()); |
| 645 |
MOZ_ASSERT(!rt->jitRuntime()->getJitcodeGlobalTable()->lookup(raw())); |
| 646 |
} |
| 647 |
#endif |
| 648 |
|
| 649 |
#ifdef MOZ_VTUNE |
| 650 |
vtune::UnmarkCode(this); |
| 651 |
#endif |
| 652 |
|
| 653 |
MOZ_ASSERT(pool_); |
| 654 |
|
| 655 |
// With W^X JIT code, reprotecting memory for each JitCode instance is |
| 656 |
// slow, so we record the ranges and poison them later all at once. It's |
| 657 |
// safe to ignore OOM here, it just means we won't poison the code. |
| 658 |
if (gcx->appendJitPoisonRange(JitPoisonRange(pool_, raw() - headerSize_, |
| 659 |
headerSize_ + bufferSize_))) { |
| 660 |
pool_->addRef(); |
| 661 |
} |
| 662 |
setHeaderPtr(nullptr); |
| 663 |
|
| 664 |
pool_->release(headerSize_ + bufferSize_, CodeKind(kind_)); |
| 665 |
zone()->decJitMemory(headerSize_ + bufferSize_); |
| 666 |
|
| 667 |
pool_ = nullptr; |
| 668 |
} |
| 669 |
|
| 670 |
IonScript::IonScript(IonCompilationId compilationId, uint32_t localSlotsSize, |
| 671 |
uint32_t argumentSlotsSize, uint32_t frameSize) |
| 672 |
: localSlotsSize_(localSlotsSize), |
| 673 |
argumentSlotsSize_(argumentSlotsSize), |
| 674 |
frameSize_(frameSize), |
| 675 |
compilationId_(compilationId) {} |
| 676 |
|
| 677 |
IonScript* IonScript::New(JSContext* cx, IonCompilationId compilationId, |
| 678 |
uint32_t localSlotsSize, uint32_t argumentSlotsSize, |
| 679 |
uint32_t frameSize, size_t snapshotsListSize, |
| 680 |
size_t snapshotsRVATableSize, size_t recoversSize, |
| 681 |
size_t constants, size_t nurseryObjects, |
| 682 |
size_t safepointIndices, size_t osiIndices, |
| 683 |
size_t icEntries, size_t runtimeSize, |
| 684 |
size_t safepointsSize) { |
| 685 |
if (snapshotsListSize >= MAX_BUFFER_SIZE) { |
| 686 |
ReportOutOfMemory(cx); |
| 687 |
return nullptr; |
| 688 |
} |
| 689 |
|
| 690 |
// Verify the hardcoded sizes in header are accurate. |
| 691 |
static_assert(SizeOf_OsiIndex == sizeof(OsiIndex), |
| 692 |
"IonScript has wrong size for OsiIndex"); |
| 693 |
static_assert(SizeOf_SafepointIndex == sizeof(SafepointIndex), |
| 694 |
"IonScript has wrong size for SafepointIndex"); |
| 695 |
|
| 696 |
CheckedInt<Offset> allocSize = sizeof(IonScript); |
| 697 |
allocSize += CheckedInt<Offset>(constants) * sizeof(Value); |
| 698 |
allocSize += CheckedInt<Offset>(runtimeSize); |
| 699 |
allocSize += CheckedInt<Offset>(nurseryObjects) * sizeof(HeapPtr<JSObject*>); |
| 700 |
allocSize += CheckedInt<Offset>(osiIndices) * sizeof(OsiIndex); |
| 701 |
allocSize += CheckedInt<Offset>(safepointIndices) * sizeof(SafepointIndex); |
| 702 |
allocSize += CheckedInt<Offset>(icEntries) * sizeof(uint32_t); |
| 703 |
allocSize += CheckedInt<Offset>(safepointsSize); |
| 704 |
allocSize += CheckedInt<Offset>(snapshotsListSize); |
| 705 |
allocSize += CheckedInt<Offset>(snapshotsRVATableSize); |
| 706 |
allocSize += CheckedInt<Offset>(recoversSize); |
| 707 |
|
| 708 |
if (!allocSize.isValid()) { |
| 709 |
ReportAllocationOverflow(cx); |
| 710 |
return nullptr; |
| 711 |
} |
| 712 |
|
| 713 |
void* raw = cx->pod_malloc<uint8_t>(allocSize.value()); |
| 714 |
MOZ_ASSERT(uintptr_t(raw) % alignof(IonScript) == 0); |
| 715 |
if (!raw) { |
| 716 |
return nullptr; |
| 717 |
} |
| 718 |
IonScript* script = new (raw) |
| 719 |
IonScript(compilationId, localSlotsSize, argumentSlotsSize, frameSize); |
| 720 |
|
| 721 |
Offset offsetCursor = sizeof(IonScript); |
| 722 |
|
| 723 |
MOZ_ASSERT(offsetCursor % alignof(Value) == 0); |
| 724 |
script->constantTableOffset_ = offsetCursor; |
| 725 |
offsetCursor += constants * sizeof(Value); |
| 726 |
|
| 727 |
MOZ_ASSERT(offsetCursor % alignof(uint64_t) == 0); |
| 728 |
script->runtimeDataOffset_ = offsetCursor; |
| 729 |
offsetCursor += runtimeSize; |
| 730 |
|
| 731 |
MOZ_ASSERT(offsetCursor % alignof(HeapPtr<JSObject*>) == 0); |
| 732 |
script->initElements<HeapPtr<JSObject*>>(offsetCursor, nurseryObjects); |
| 733 |
script->nurseryObjectsOffset_ = offsetCursor; |
| 734 |
offsetCursor += nurseryObjects * sizeof(HeapPtr<JSObject*>); |
| 735 |
|
| 736 |
MOZ_ASSERT(offsetCursor % alignof(OsiIndex) == 0); |
| 737 |
script->osiIndexOffset_ = offsetCursor; |
| 738 |
offsetCursor += osiIndices * sizeof(OsiIndex); |
| 739 |
|
| 740 |
MOZ_ASSERT(offsetCursor % alignof(SafepointIndex) == 0); |
| 741 |
script->safepointIndexOffset_ = offsetCursor; |
| 742 |
offsetCursor += safepointIndices * sizeof(SafepointIndex); |
| 743 |
|
| 744 |
MOZ_ASSERT(offsetCursor % alignof(uint32_t) == 0); |
| 745 |
script->icIndexOffset_ = offsetCursor; |
| 746 |
offsetCursor += icEntries * sizeof(uint32_t); |
| 747 |
|
| 748 |
script->safepointsOffset_ = offsetCursor; |
| 749 |
offsetCursor += safepointsSize; |
| 750 |
|
| 751 |
script->snapshotsOffset_ = offsetCursor; |
| 752 |
offsetCursor += snapshotsListSize; |
| 753 |
|
| 754 |
script->rvaTableOffset_ = offsetCursor; |
| 755 |
offsetCursor += snapshotsRVATableSize; |
| 756 |
|
| 757 |
script->recoversOffset_ = offsetCursor; |
| 758 |
offsetCursor += recoversSize; |
| 759 |
|
| 760 |
script->allocBytes_ = offsetCursor; |
| 761 |
|
| 762 |
MOZ_ASSERT(script->numConstants() == constants); |
| 763 |
MOZ_ASSERT(script->runtimeSize() == runtimeSize); |
| 764 |
MOZ_ASSERT(script->numNurseryObjects() == nurseryObjects); |
| 765 |
MOZ_ASSERT(script->numOsiIndices() == osiIndices); |
| 766 |
MOZ_ASSERT(script->numSafepointIndices() == safepointIndices); |
| 767 |
MOZ_ASSERT(script->numICs() == icEntries); |
| 768 |
MOZ_ASSERT(script->safepointsSize() == safepointsSize); |
| 769 |
MOZ_ASSERT(script->snapshotsListSize() == snapshotsListSize); |
| 770 |
MOZ_ASSERT(script->snapshotsRVATableSize() == snapshotsRVATableSize); |
| 771 |
MOZ_ASSERT(script->recoversSize() == recoversSize); |
| 772 |
MOZ_ASSERT(script->endOffset() == offsetCursor); |
| 773 |
|
| 774 |
return script; |
| 775 |
} |
| 776 |
|
| 777 |
void IonScript::trace(JSTracer* trc) { |
| 778 |
if (method_) { |
| 779 |
TraceEdge(trc, &method_, "method"); |
| 780 |
} |
| 781 |
|
| 782 |
for (size_t i = 0; i < numConstants(); i++) { |
| 783 |
TraceEdge(trc, &getConstant(i), "constant"); |
| 784 |
} |
| 785 |
|
| 786 |
for (size_t i = 0; i < numNurseryObjects(); i++) { |
| 787 |
TraceEdge(trc, &nurseryObjects()[i], "nursery-object"); |
| 788 |
} |
| 789 |
|
| 790 |
// Trace caches so that the JSScript pointer can be updated if moved. |
| 791 |
for (size_t i = 0; i < numICs(); i++) { |
| 792 |
getICFromIndex(i).trace(trc, this); |
| 793 |
} |
| 794 |
} |
| 795 |
|
| 796 |
void IonScript::traceWeak(JSTracer* trc) { |
| 797 |
// IonICs do not currently contain weak pointers. If this is added then they |
| 798 |
// should be traced here. |
| 799 |
} |
| 800 |
|
| 801 |
/* static */ |
| 802 |
void IonScript::preWriteBarrier(Zone* zone, IonScript* ionScript) { |
| 803 |
PreWriteBarrier(zone, ionScript); |
| 804 |
} |
| 805 |
|
| 806 |
void IonScript::copySnapshots(const SnapshotWriter* writer) { |
| 807 |
MOZ_ASSERT(writer->listSize() == snapshotsListSize()); |
| 808 |
memcpy(offsetToPointer<uint8_t>(snapshotsOffset()), writer->listBuffer(), |
| 809 |
snapshotsListSize()); |
| 810 |
|
| 811 |
MOZ_ASSERT(snapshotsRVATableSize()); |
| 812 |
MOZ_ASSERT(writer->RVATableSize() == snapshotsRVATableSize()); |
| 813 |
memcpy(offsetToPointer<uint8_t>(rvaTableOffset()), writer->RVATableBuffer(), |
| 814 |
snapshotsRVATableSize()); |
| 815 |
} |
| 816 |
|
| 817 |
void IonScript::copyRecovers(const RecoverWriter* writer) { |
| 818 |
MOZ_ASSERT(writer->size() == recoversSize()); |
| 819 |
memcpy(offsetToPointer<uint8_t>(recoversOffset()), writer->buffer(), |
| 820 |
recoversSize()); |
| 821 |
} |
| 822 |
|
| 823 |
void IonScript::copySafepoints(const SafepointWriter* writer) { |
| 824 |
MOZ_ASSERT(writer->size() == safepointsSize()); |
| 825 |
memcpy(offsetToPointer<uint8_t>(safepointsOffset()), writer->buffer(), |
| 826 |
safepointsSize()); |
| 827 |
} |
| 828 |
|
| 829 |
void IonScript::copyConstants(const Value* vp) { |
| 830 |
for (size_t i = 0; i < numConstants(); i++) { |
| 831 |
constants()[i].init(vp[i]); |
| 832 |
} |
| 833 |
} |
| 834 |
|
| 835 |
void IonScript::copySafepointIndices(const CodegenSafepointIndex* si) { |
| 836 |
// Convert CodegenSafepointIndex to more compact form. |
| 837 |
SafepointIndex* table = safepointIndices(); |
| 838 |
for (size_t i = 0; i < numSafepointIndices(); ++i) { |
| 839 |
table[i] = SafepointIndex(si[i]); |
| 840 |
} |
| 841 |
} |
| 842 |
|
| 843 |
void IonScript::copyOsiIndices(const OsiIndex* oi) { |
| 844 |
memcpy(osiIndices(), oi, numOsiIndices() * sizeof(OsiIndex)); |
| 845 |
} |
| 846 |
|
| 847 |
void IonScript::copyRuntimeData(const uint8_t* data) { |
| 848 |
memcpy(runtimeData(), data, runtimeSize()); |
| 849 |
} |
| 850 |
|
| 851 |
void IonScript::copyICEntries(const uint32_t* icEntries) { |
| 852 |
memcpy(icIndex(), icEntries, numICs() * sizeof(uint32_t)); |
| 853 |
|
| 854 |
// Update the codeRaw_ field in the ICs now that we know the code address. |
| 855 |
for (size_t i = 0; i < numICs(); i++) { |
| 856 |
getICFromIndex(i).resetCodeRaw(this); |
| 857 |
} |
| 858 |
} |
| 859 |
|
| 860 |
const SafepointIndex* IonScript::getSafepointIndex(uint32_t disp) const { |
| 861 |
MOZ_ASSERT(numSafepointIndices() > 0); |
| 862 |
|
| 863 |
const SafepointIndex* table = safepointIndices(); |
| 864 |
if (numSafepointIndices() == 1) { |
| 865 |
MOZ_ASSERT(disp == table[0].displacement()); |
| 866 |
return &table[0]; |
| 867 |
} |
| 868 |
|
| 869 |
size_t minEntry = 0; |
| 870 |
size_t maxEntry = numSafepointIndices() - 1; |
| 871 |
uint32_t min = table[minEntry].displacement(); |
| 872 |
uint32_t max = table[maxEntry].displacement(); |
| 873 |
|
| 874 |
// Raise if the element is not in the list. |
| 875 |
MOZ_ASSERT(min <= disp && disp <= max); |
| 876 |
|
| 877 |
// Approximate the location of the FrameInfo. |
| 878 |
size_t guess = (disp - min) * (maxEntry - minEntry) / (max - min) + minEntry; |
| 879 |
uint32_t guessDisp = table[guess].displacement(); |
| 880 |
|
| 881 |
if (table[guess].displacement() == disp) { |
| 882 |
return &table[guess]; |
| 883 |
} |
| 884 |
|
| 885 |
// Doing a linear scan from the guess should be more efficient in case of |
| 886 |
// small group which are equally distributed on the code. |
| 887 |
// |
| 888 |
// such as: <... ... ... ... . ... ...> |
| 889 |
if (guessDisp > disp) { |
| 890 |
while (--guess >= minEntry) { |
| 891 |
guessDisp = table[guess].displacement(); |
| 892 |
MOZ_ASSERT(guessDisp >= disp); |
| 893 |
if (guessDisp == disp) { |
| 894 |
return &table[guess]; |
| 895 |
} |
| 896 |
} |
| 897 |
} else { |
| 898 |
while (++guess <= maxEntry) { |
| 899 |
guessDisp = table[guess].displacement(); |
| 900 |
MOZ_ASSERT(guessDisp <= disp); |
| 901 |
if (guessDisp == disp) { |
| 902 |
return &table[guess]; |
| 903 |
} |
| 904 |
} |
| 905 |
} |
| 906 |
|
| 907 |
MOZ_CRASH("displacement not found."); |
| 908 |
} |
| 909 |
|
| 910 |
const OsiIndex* IonScript::getOsiIndex(uint32_t disp) const { |
| 911 |
const OsiIndex* end = osiIndices() + numOsiIndices(); |
| 912 |
for (const OsiIndex* it = osiIndices(); it != end; ++it) { |
| 913 |
if (it->returnPointDisplacement() == disp) { |
| 914 |
return it; |
| 915 |
} |
| 916 |
} |
| 917 |
|
| 918 |
MOZ_CRASH("Failed to find OSI point return address"); |
| 919 |
} |
| 920 |
|
| 921 |
const OsiIndex* IonScript::getOsiIndex(uint8_t* retAddr) const { |
| 922 |
JitSpew(JitSpew_IonInvalidate, "IonScript %p has method %p raw %p", |
| 923 |
(void*)this, (void*)method(), method()->raw()); |
| 924 |
|
| 925 |
MOZ_ASSERT(containsCodeAddress(retAddr)); |
| 926 |
uint32_t disp = retAddr - method()->raw(); |
| 927 |
return getOsiIndex(disp); |
| 928 |
} |
| 929 |
|
| 930 |
void IonScript::Destroy(JS::GCContext* gcx, IonScript* script) { |
| 931 |
// Make sure there are no pointers into the IonScript's nursery objects list |
| 932 |
// in the store buffer. Because this can be called during sweeping when |
| 933 |
// discarding JIT code, we have to lock the store buffer when we find an |
| 934 |
// object that's (still) in the nursery. |
| 935 |
mozilla::Maybe<gc::AutoLockStoreBuffer> lock; |
| 936 |
for (size_t i = 0, len = script->numNurseryObjects(); i < len; i++) { |
| 937 |
JSObject* obj = script->nurseryObjects()[i]; |
| 938 |
if (!IsInsideNursery(obj)) { |
| 939 |
continue; |
| 940 |
} |
| 941 |
if (lock.isNothing()) { |
| 942 |
lock.emplace(gcx->runtimeFromAnyThread()); |
| 943 |
} |
| 944 |
script->nurseryObjects()[i] = HeapPtr<JSObject*>(); |
| 945 |
} |
| 946 |
|
| 947 |
// This allocation is tracked by JSScript::setIonScriptImpl. |
| 948 |
gcx->deleteUntracked(script); |
| 949 |
} |
| 950 |
|
| 951 |
void JS::DeletePolicy<js::jit::IonScript>::operator()( |
| 952 |
const js::jit::IonScript* script) { |
| 953 |
IonScript::Destroy(rt_->gcContext(), const_cast<IonScript*>(script)); |
| 954 |
} |
| 955 |
|
| 956 |
void IonScript::purgeICs(Zone* zone) { |
| 957 |
for (size_t i = 0; i < numICs(); i++) { |
| 958 |
getICFromIndex(i).reset(zone, this); |
| 959 |
} |
| 960 |
} |
| 961 |
|
| 962 |
namespace js { |
| 963 |
namespace jit { |
| 964 |
|
| 965 |
bool OptimizeMIR(MIRGenerator* mir) { |
| 966 |
MIRGraph& graph = mir->graph(); |
| 967 |
GraphSpewer& gs = mir->graphSpewer(); |
| 968 |
|
| 969 |
if (mir->shouldCancel("Start")) { |
| 970 |
return false; |
| 971 |
} |
| 972 |
|
| 973 |
gs.spewPass("BuildSSA"); |
| 974 |
AssertBasicGraphCoherency(graph); |
| 975 |
|
| 976 |
if (JitSpewEnabled(JitSpew_MIRExpressions)) { |
| 977 |
JitSpewCont(JitSpew_MIRExpressions, "\n"); |
| 978 |
DumpMIRExpressions(JitSpewPrinter(), graph, mir->outerInfo(), |
| 979 |
"BuildSSA (== input to OptimizeMIR)"); |
| 980 |
} |
| 981 |
|
| 982 |
if (!JitOptions.disablePruning && !mir->compilingWasm()) { |
| 983 |
JitSpewCont(JitSpew_Prune, "\n"); |
| 984 |
if (!PruneUnusedBranches(mir, graph)) { |
| 985 |
return false; |
| 986 |
} |
| 987 |
gs.spewPass("Prune Unused Branches"); |
| 988 |
AssertBasicGraphCoherency(graph); |
| 989 |
|
| 990 |
if (mir->shouldCancel("Prune Unused Branches")) { |
| 991 |
return false; |
| 992 |
} |
| 993 |
} |
| 994 |
|
| 995 |
{ |
| 996 |
bool dummy; |
| 997 |
if (!FoldEmptyBlocks(graph, &dummy)) { |
| 998 |
return false; |
| 999 |
} |
| 1000 |
gs.spewPass("Fold Empty Blocks"); |
| 1001 |
AssertBasicGraphCoherency(graph); |
| 1002 |
|
| 1003 |
if (mir->shouldCancel("Fold Empty Blocks")) { |
| 1004 |
return false; |
| 1005 |
} |
| 1006 |
} |
| 1007 |
|
| 1008 |
// Remove trivially dead resume point operands before folding tests, so the |
| 1009 |
// latter pass can optimize more aggressively. |
| 1010 |
if (!mir->compilingWasm()) { |
| 1011 |
if (!EliminateTriviallyDeadResumePointOperands(mir, graph)) { |
| 1012 |
return false; |
| 1013 |
} |
| 1014 |
gs.spewPass("Eliminate trivially dead resume point operands"); |
| 1015 |
AssertBasicGraphCoherency(graph); |
| 1016 |
|
| 1017 |
if (mir->shouldCancel("Eliminate trivially dead resume point operands")) { |
| 1018 |
return false; |
| 1019 |
} |
| 1020 |
} |
| 1021 |
|
| 1022 |
{ |
| 1023 |
if (!FoldTests(graph)) { |
| 1024 |
return false; |
| 1025 |
} |
| 1026 |
gs.spewPass("Fold Tests"); |
| 1027 |
AssertBasicGraphCoherency(graph); |
| 1028 |
|
| 1029 |
if (mir->shouldCancel("Fold Tests")) { |
| 1030 |
return false; |
| 1031 |
} |
| 1032 |
} |
| 1033 |
|
| 1034 |
{ |
| 1035 |
if (!SplitCriticalEdges(graph)) { |
| 1036 |
return false; |
| 1037 |
} |
| 1038 |
gs.spewPass("Split Critical Edges"); |
| 1039 |
AssertGraphCoherency(graph); |
| 1040 |
|
| 1041 |
if (mir->shouldCancel("Split Critical Edges")) { |
| 1042 |
return false; |
| 1043 |
} |
| 1044 |
} |
| 1045 |
|
| 1046 |
{ |
| 1047 |
RenumberBlocks(graph); |
| 1048 |
gs.spewPass("Renumber Blocks"); |
| 1049 |
AssertGraphCoherency(graph); |
| 1050 |
|
| 1051 |
if (mir->shouldCancel("Renumber Blocks")) { |
| 1052 |
return false; |
| 1053 |
} |
| 1054 |
} |
| 1055 |
|
| 1056 |
{ |
| 1057 |
if (!BuildDominatorTree(mir, graph)) { |
| 1058 |
return false; |
| 1059 |
} |
| 1060 |
// No spew: graph not changed. |
| 1061 |
|
| 1062 |
if (mir->shouldCancel("Dominator Tree")) { |
| 1063 |
return false; |
| 1064 |
} |
| 1065 |
} |
| 1066 |
|
| 1067 |
{ |
| 1068 |
// Aggressive phi elimination must occur before any code elimination. If the |
| 1069 |
// script contains a try-statement, we only compiled the try block and not |
| 1070 |
// the catch or finally blocks, so in this case it's also invalid to use |
| 1071 |
// aggressive phi elimination. |
| 1072 |
Observability observability = graph.hasTryBlock() |
| 1073 |
? ConservativeObservability |
| 1074 |
: AggressiveObservability; |
| 1075 |
if (!EliminatePhis(mir, graph, observability)) { |
| 1076 |
return false; |
| 1077 |
} |
| 1078 |
gs.spewPass("Eliminate phis"); |
| 1079 |
AssertGraphCoherency(graph); |
| 1080 |
|
| 1081 |
if (mir->shouldCancel("Eliminate phis")) { |
| 1082 |
return false; |
| 1083 |
} |
| 1084 |
|
| 1085 |
if (!BuildPhiReverseMapping(graph)) { |
| 1086 |
return false; |
| 1087 |
} |
| 1088 |
AssertExtendedGraphCoherency(graph); |
| 1089 |
// No spew: graph not changed. |
| 1090 |
|
| 1091 |
if (mir->shouldCancel("Phi reverse mapping")) { |
| 1092 |
return false; |
| 1093 |
} |
| 1094 |
} |
| 1095 |
|
| 1096 |
if (!mir->compilingWasm() && !JitOptions.disableIteratorIndices) { |
| 1097 |
if (!OptimizeIteratorIndices(mir, graph)) { |
| 1098 |
return false; |
| 1099 |
} |
| 1100 |
gs.spewPass("Iterator Indices"); |
| 1101 |
AssertGraphCoherency(graph); |
| 1102 |
|
| 1103 |
if (mir->shouldCancel("Iterator Indices")) { |
| 1104 |
return false; |
| 1105 |
} |
| 1106 |
} |
| 1107 |
|
| 1108 |
if (!JitOptions.disableRecoverIns && |
| 1109 |
mir->optimizationInfo().scalarReplacementEnabled()) { |
| 1110 |
JitSpewCont(JitSpew_Escape, "\n"); |
| 1111 |
if (!ScalarReplacement(mir, graph)) { |
| 1112 |
return false; |
| 1113 |
} |
| 1114 |
gs.spewPass("Scalar Replacement"); |
| 1115 |
AssertGraphCoherency(graph); |
| 1116 |
|
| 1117 |
if (mir->shouldCancel("Scalar Replacement")) { |
| 1118 |
return false; |
| 1119 |
} |
| 1120 |
} |
| 1121 |
|
| 1122 |
if (!mir->compilingWasm()) { |
| 1123 |
if (!ApplyTypeInformation(mir, graph)) { |
| 1124 |
return false; |
| 1125 |
} |
| 1126 |
gs.spewPass("Apply types"); |
| 1127 |
AssertExtendedGraphCoherency(graph); |
| 1128 |
|
| 1129 |
if (mir->shouldCancel("Apply types")) { |
| 1130 |
return false; |
| 1131 |
} |
| 1132 |
} |
| 1133 |
|
| 1134 |
if (mir->compilingWasm()) { |
| 1135 |
if (!TrackWasmRefTypes(graph)) { |
| 1136 |
return false; |
| 1137 |
} |
| 1138 |
gs.spewPass("Track Wasm ref types"); |
| 1139 |
AssertExtendedGraphCoherency(graph); |
| 1140 |
|
| 1141 |
if (mir->shouldCancel("Track Wasm ref types")) { |
| 1142 |
return false; |
| 1143 |
} |
| 1144 |
} |
| 1145 |
|
| 1146 |
if (mir->optimizationInfo().amaEnabled()) { |
| 1147 |
AlignmentMaskAnalysis ama(graph); |
| 1148 |
if (!ama.analyze()) { |
| 1149 |
return false; |
| 1150 |
} |
| 1151 |
gs.spewPass("Alignment Mask Analysis"); |
| 1152 |
AssertExtendedGraphCoherency(graph); |
| 1153 |
|
| 1154 |
if (mir->shouldCancel("Alignment Mask Analysis")) { |
| 1155 |
return false; |
| 1156 |
} |
| 1157 |
} |
| 1158 |
|
| 1159 |
ValueNumberer gvn(mir, graph); |
| 1160 |
|
| 1161 |
// Alias analysis is required for LICM and GVN so that we don't move |
| 1162 |
// loads across stores. We also use alias information when removing |
| 1163 |
// redundant shapeguards. |
| 1164 |
if (mir->optimizationInfo().licmEnabled() || |
| 1165 |
mir->optimizationInfo().gvnEnabled() || |
| 1166 |
mir->optimizationInfo().eliminateRedundantShapeGuardsEnabled()) { |
| 1167 |
{ |
| 1168 |
AliasAnalysis analysis(mir, graph); |
| 1169 |
JitSpewCont(JitSpew_Alias, "\n"); |
| 1170 |
if (!analysis.analyze()) { |
| 1171 |
return false; |
| 1172 |
} |
| 1173 |
|
| 1174 |
gs.spewPass("Alias analysis"); |
| 1175 |
AssertExtendedGraphCoherency(graph); |
| 1176 |
|
| 1177 |
if (mir->shouldCancel("Alias analysis")) { |
| 1178 |
return false; |
| 1179 |
} |
| 1180 |
} |
| 1181 |
|
| 1182 |
if (!mir->compilingWasm()) { |
| 1183 |
// Eliminating dead resume point operands requires basic block |
| 1184 |
// instructions to be numbered. Reuse the numbering computed during |
| 1185 |
// alias analysis. |
| 1186 |
if (!EliminateDeadResumePointOperands(mir, graph)) { |
| 1187 |
return false; |
| 1188 |
} |
| 1189 |
|
| 1190 |
gs.spewPass("Eliminate dead resume point operands"); |
| 1191 |
AssertExtendedGraphCoherency(graph); |
| 1192 |
|
| 1193 |
if (mir->shouldCancel("Eliminate dead resume point operands")) { |
| 1194 |
return false; |
| 1195 |
} |
| 1196 |
} |
| 1197 |
} |
| 1198 |
|
| 1199 |
if (mir->optimizationInfo().gvnEnabled()) { |
| 1200 |
JitSpewCont(JitSpew_GVN, "\n"); |
| 1201 |
if (!gvn.run(ValueNumberer::UpdateAliasAnalysis)) { |
| 1202 |
return false; |
| 1203 |
} |
| 1204 |
gs.spewPass("GVN"); |
| 1205 |
AssertExtendedGraphCoherency(graph); |
| 1206 |
|
| 1207 |
if (mir->shouldCancel("GVN")) { |
| 1208 |
return false; |
| 1209 |
} |
| 1210 |
} |
| 1211 |
|
| 1212 |
if (mir->branchHintingEnabled()) { |
| 1213 |
JitSpewCont(JitSpew_BranchHint, "\n"); |
| 1214 |
if (!BranchHinting(mir, graph)) { |
| 1215 |
return false; |
| 1216 |
} |
| 1217 |
gs.spewPass("BranchHinting"); |
| 1218 |
AssertBasicGraphCoherency(graph); |
| 1219 |
|
| 1220 |
if (mir->shouldCancel("BranchHinting")) { |
| 1221 |
return false; |
| 1222 |
} |
| 1223 |
} |
| 1224 |
|
| 1225 |
// LICM can hoist instructions from conditional branches and |
| 1226 |
// trigger bailouts. Disable it if bailing out of a hoisted |
| 1227 |
// instruction has previously invalidated this script. |
| 1228 |
if (mir->licmEnabled()) { |
| 1229 |
JitSpewCont(JitSpew_LICM, "\n"); |
| 1230 |
if (!LICM(mir, graph)) { |
| 1231 |
return false; |
| 1232 |
} |
| 1233 |
gs.spewPass("LICM"); |
| 1234 |
AssertExtendedGraphCoherency(graph); |
| 1235 |
|
| 1236 |
if (mir->shouldCancel("LICM")) { |
| 1237 |
return false; |
| 1238 |
} |
| 1239 |
} |
| 1240 |
|
| 1241 |
RangeAnalysis r(mir, graph); |
| 1242 |
if (mir->optimizationInfo().rangeAnalysisEnabled()) { |
| 1243 |
JitSpewCont(JitSpew_Range, "\n"); |
| 1244 |
if (!r.addBetaNodes()) { |
| 1245 |
return false; |
| 1246 |
} |
| 1247 |
gs.spewPass("Beta"); |
| 1248 |
AssertExtendedGraphCoherency(graph); |
| 1249 |
|
| 1250 |
if (mir->shouldCancel("RA Beta")) { |
| 1251 |
return false; |
| 1252 |
} |
| 1253 |
|
| 1254 |
if (!r.analyze() || !r.addRangeAssertions()) { |
| 1255 |
return false; |
| 1256 |
} |
| 1257 |
gs.spewPass("Range Analysis"); |
| 1258 |
AssertExtendedGraphCoherency(graph); |
| 1259 |
|
| 1260 |
if (mir->shouldCancel("Range Analysis")) { |
| 1261 |
return false; |
| 1262 |
} |
| 1263 |
|
| 1264 |
if (!r.removeBetaNodes()) { |
| 1265 |
return false; |
| 1266 |
} |
| 1267 |
gs.spewPass("De-Beta"); |
| 1268 |
AssertExtendedGraphCoherency(graph); |
| 1269 |
|
| 1270 |
if (mir->shouldCancel("RA De-Beta")) { |
| 1271 |
return false; |
| 1272 |
} |
| 1273 |
|
| 1274 |
if (mir->optimizationInfo().gvnEnabled()) { |
| 1275 |
bool shouldRunUCE = false; |
| 1276 |
if (!r.prepareForUCE(&shouldRunUCE)) { |
| 1277 |
return false; |
| 1278 |
} |
| 1279 |
gs.spewPass("RA check UCE"); |
| 1280 |
AssertExtendedGraphCoherency(graph); |
| 1281 |
|
| 1282 |
if (mir->shouldCancel("RA check UCE")) { |
| 1283 |
return false; |
| 1284 |
} |
| 1285 |
|
| 1286 |
if (shouldRunUCE) { |
| 1287 |
if (!gvn.run(ValueNumberer::DontUpdateAliasAnalysis)) { |
| 1288 |
return false; |
| 1289 |
} |
| 1290 |
gs.spewPass("UCE After RA"); |
| 1291 |
AssertExtendedGraphCoherency(graph); |
| 1292 |
|
| 1293 |
if (mir->shouldCancel("UCE After RA")) { |
| 1294 |
return false; |
| 1295 |
} |
| 1296 |
} |
| 1297 |
} |
| 1298 |
|
| 1299 |
if (mir->optimizationInfo().autoTruncateEnabled()) { |
| 1300 |
if (!r.truncate()) { |
| 1301 |
return false; |
| 1302 |
} |
| 1303 |
gs.spewPass("Truncate Doubles"); |
| 1304 |
AssertExtendedGraphCoherency(graph); |
| 1305 |
|
| 1306 |
if (mir->shouldCancel("Truncate Doubles")) { |
| 1307 |
return false; |
| 1308 |
} |
| 1309 |
} |
| 1310 |
} |
| 1311 |
|
| 1312 |
if (!JitOptions.disableRecoverIns) { |
| 1313 |
JitSpewCont(JitSpew_Sink, "\n"); |
| 1314 |
if (!Sink(mir, graph)) { |
| 1315 |
return false; |
| 1316 |
} |
| 1317 |
gs.spewPass("Sink"); |
| 1318 |
AssertExtendedGraphCoherency(graph); |
| 1319 |
|
| 1320 |
if (mir->shouldCancel("Sink")) { |
| 1321 |
return false; |
| 1322 |
} |
| 1323 |
} |
| 1324 |
|
| 1325 |
if (!JitOptions.disableRecoverIns && |
| 1326 |
mir->optimizationInfo().rangeAnalysisEnabled()) { |
| 1327 |
JitSpewCont(JitSpew_Range, "\n"); |
| 1328 |
if (!r.removeUnnecessaryBitops()) { |
| 1329 |
return false; |
| 1330 |
} |
| 1331 |
gs.spewPass("Remove Unnecessary Bitops"); |
| 1332 |
AssertExtendedGraphCoherency(graph); |
| 1333 |
|
| 1334 |
if (mir->shouldCancel("Remove Unnecessary Bitops")) { |
| 1335 |
return false; |
| 1336 |
} |
| 1337 |
} |
| 1338 |
|
| 1339 |
{ |
| 1340 |
JitSpewCont(JitSpew_FLAC, "\n"); |
| 1341 |
if (!FoldLinearArithConstants(mir, graph)) { |
| 1342 |
return false; |
| 1343 |
} |
| 1344 |
gs.spewPass("Fold Linear Arithmetic Constants"); |
| 1345 |
AssertBasicGraphCoherency(graph); |
| 1346 |
|
| 1347 |
if (mir->shouldCancel("Fold Linear Arithmetic Constants")) { |
| 1348 |
return false; |
| 1349 |
} |
| 1350 |
} |
| 1351 |
|
| 1352 |
if (mir->optimizationInfo().eaaEnabled()) { |
| 1353 |
EffectiveAddressAnalysis eaa(mir, graph); |
| 1354 |
JitSpewCont(JitSpew_EAA, "\n"); |
| 1355 |
if (!eaa.analyze()) { |
| 1356 |
return false; |
| 1357 |
} |
| 1358 |
gs.spewPass("Effective Address Analysis"); |
| 1359 |
AssertExtendedGraphCoherency(graph); |
| 1360 |
|
| 1361 |
if (mir->shouldCancel("Effective Address Analysis")) { |
| 1362 |
return false; |
| 1363 |
} |
| 1364 |
} |
| 1365 |
|
| 1366 |
// BCE marks bounds checks as dead, so do BCE before DCE. |
| 1367 |
if (mir->compilingWasm()) { |
| 1368 |
JitSpewCont(JitSpew_WasmBCE, "\n"); |
| 1369 |
if (!EliminateBoundsChecks(mir, graph)) { |
| 1370 |
return false; |
| 1371 |
} |
| 1372 |
gs.spewPass("Redundant Bounds Check Elimination"); |
| 1373 |
AssertGraphCoherency(graph); |
| 1374 |
|
| 1375 |
if (mir->shouldCancel("BCE")) { |
| 1376 |
return false; |
| 1377 |
} |
| 1378 |
} |
| 1379 |
|
| 1380 |
{ |
| 1381 |
if (!EliminateDeadCode(mir, graph)) { |
| 1382 |
return false; |
| 1383 |
} |
| 1384 |
gs.spewPass("DCE"); |
| 1385 |
AssertExtendedGraphCoherency(graph); |
| 1386 |
|
| 1387 |
if (mir->shouldCancel("DCE")) { |
| 1388 |
return false; |
| 1389 |
} |
| 1390 |
} |
| 1391 |
|
| 1392 |
if (!JitOptions.disableMarkLoadsUsedAsPropertyKeys && !mir->compilingWasm()) { |
| 1393 |
JitSpewCont(JitSpew_MarkLoadsUsedAsPropertyKeys, "\n"); |
| 1394 |
if (!MarkLoadsUsedAsPropertyKeys(graph)) { |
| 1395 |
return false; |
| 1396 |
} |
| 1397 |
if (mir->shouldCancel("MarkLoadsUsedAsPropertyKeys")) { |
| 1398 |
return false; |
| 1399 |
} |
| 1400 |
} |
| 1401 |
|
| 1402 |
if (mir->optimizationInfo().instructionReorderingEnabled() && |
| 1403 |
!mir->outerInfo().hadReorderingBailout()) { |
| 1404 |
if (!ReorderInstructions(mir, graph)) { |
| 1405 |
return false; |
| 1406 |
} |
| 1407 |
gs.spewPass("Reordering"); |
| 1408 |
|
| 1409 |
AssertExtendedGraphCoherency(graph); |
| 1410 |
|
| 1411 |
if (mir->shouldCancel("Reordering")) { |
| 1412 |
return false; |
| 1413 |
} |
| 1414 |
} |
| 1415 |
|
| 1416 |
// Make loops contiguous. We do this after GVN/UCE and range analysis, |
| 1417 |
// which can remove CFG edges, exposing more blocks that can be moved. |
| 1418 |
{ |
| 1419 |
if (!MakeLoopsContiguous(graph)) { |
| 1420 |
return false; |
| 1421 |
} |
| 1422 |
gs.spewPass("Make loops contiguous"); |
| 1423 |
AssertExtendedGraphCoherency(graph); |
| 1424 |
|
| 1425 |
if (mir->shouldCancel("Make loops contiguous")) { |
| 1426 |
return false; |
| 1427 |
} |
| 1428 |
} |
| 1429 |
AssertExtendedGraphCoherency(graph, /* underValueNumberer = */ false, |
| 1430 |
/* force = */ true); |
| 1431 |
|
| 1432 |
// Unroll and/or peel loops |
| 1433 |
if (mir->compilingWasm() && JS::Prefs::wasm_unroll_loops()) { |
| 1434 |
bool loopsChanged; |
| 1435 |
if (!UnrollLoops(mir, graph, &loopsChanged)) { |
| 1436 |
return false; |
| 1437 |
} |
| 1438 |
|
| 1439 |
gs.spewPass("Unroll loops"); |
| 1440 |
|
| 1441 |
AssertExtendedGraphCoherency(graph); |
| 1442 |
|
| 1443 |
if (mir->shouldCancel("Unroll loops")) { |
| 1444 |
return false; |
| 1445 |
} |
| 1446 |
|
| 1447 |
if (loopsChanged) { |
| 1448 |
// Rerun GVN in the hope that unrolling exposed more optimization |
| 1449 |
// opportunities. |
| 1450 |
if (!gvn.run(ValueNumberer::DontUpdateAliasAnalysis)) { |
| 1451 |
return false; |
| 1452 |
} |
| 1453 |
// And tidy up any empty blocks. |
| 1454 |
bool blocksFolded; |
| 1455 |
if (!FoldEmptyBlocks(graph, &blocksFolded)) { |
| 1456 |
return false; |
| 1457 |
} |
| 1458 |
if (blocksFolded) { |
| 1459 |
// Redo the dominator tree. |
| 1460 |
ClearDominatorTree(graph); |
| 1461 |
if (!BuildDominatorTree(mir, graph)) { |
| 1462 |
return false; |
| 1463 |
} |
| 1464 |
} |
| 1465 |
|
| 1466 |
AssertExtendedGraphCoherency(graph); |
| 1467 |
|
| 1468 |
if (mir->shouldCancel("Rerun GVN after loop unrolling")) { |
| 1469 |
return false; |
| 1470 |
} |
| 1471 |
} |
| 1472 |
} |
| 1473 |
|
| 1474 |
// Remove unreachable blocks created by MBasicBlock::NewFakeLoopPredecessor |
| 1475 |
// to ensure every loop header has two predecessors. (This only happens due |
| 1476 |
// to OSR.) After this point, it is no longer possible to build the |
| 1477 |
// dominator tree. |
| 1478 |
if (!mir->compilingWasm() && graph.osrBlock()) { |
| 1479 |
graph.removeFakeLoopPredecessors(); |
| 1480 |
gs.spewPass("Remove fake loop predecessors"); |
| 1481 |
AssertGraphCoherency(graph); |
| 1482 |
|
| 1483 |
if (mir->shouldCancel("Remove fake loop predecessors")) { |
| 1484 |
return false; |
| 1485 |
} |
| 1486 |
} |
| 1487 |
|
| 1488 |
// Passes after this point must not move instructions; these analyses |
| 1489 |
// depend on knowing the final order in which instructions will execute. |
| 1490 |
|
| 1491 |
if (mir->optimizationInfo().edgeCaseAnalysisEnabled()) { |
| 1492 |
EdgeCaseAnalysis edgeCaseAnalysis(mir, graph); |
| 1493 |
if (!edgeCaseAnalysis.analyzeLate()) { |
| 1494 |
return false; |
| 1495 |
} |
| 1496 |
gs.spewPass("Edge Case Analysis (Late)"); |
| 1497 |
AssertGraphCoherency(graph); |
| 1498 |
|
| 1499 |
if (mir->shouldCancel("Edge Case Analysis (Late)")) { |
| 1500 |
return false; |
| 1501 |
} |
| 1502 |
} |
| 1503 |
|
| 1504 |
if (mir->optimizationInfo().eliminateRedundantChecksEnabled()) { |
| 1505 |
// Note: check elimination has to run after all other passes that move |
| 1506 |
// instructions. Since check uses are replaced with the actual index, |
| 1507 |
// code motion after this pass could incorrectly move a load or store |
| 1508 |
// before its bounds check. |
| 1509 |
if (!EliminateRedundantChecks(graph)) { |
| 1510 |
return false; |
| 1511 |
} |
| 1512 |
gs.spewPass("Bounds Check Elimination"); |
| 1513 |
AssertGraphCoherency(graph); |
| 1514 |
|
| 1515 |
if (mir->shouldCancel("Bounds Check Elimination")) { |
| 1516 |
return false; |
| 1517 |
} |
| 1518 |
} |
| 1519 |
|
| 1520 |
if (mir->optimizationInfo().eliminateRedundantShapeGuardsEnabled()) { |
| 1521 |
if (!EliminateRedundantShapeGuards(graph)) { |
| 1522 |
return false; |
| 1523 |
} |
| 1524 |
gs.spewPass("Shape Guard Elimination"); |
| 1525 |
AssertGraphCoherency(graph); |
| 1526 |
|
| 1527 |
if (mir->shouldCancel("Shape Guard Elimination")) { |
| 1528 |
return false; |
| 1529 |
} |
| 1530 |
} |
| 1531 |
|
| 1532 |
// Run the GC Barrier Elimination pass after instruction reordering, to |
| 1533 |
// ensure we don't move instructions that can trigger GC between stores we |
| 1534 |
// optimize here. |
| 1535 |
if (mir->optimizationInfo().eliminateRedundantGCBarriersEnabled()) { |
| 1536 |
if (!EliminateRedundantGCBarriers(graph)) { |
| 1537 |
return false; |
| 1538 |
} |
| 1539 |
gs.spewPass("GC Barrier Elimination"); |
| 1540 |
AssertGraphCoherency(graph); |
| 1541 |
|
| 1542 |
if (mir->shouldCancel("GC Barrier Elimination")) { |
| 1543 |
return false; |
| 1544 |
} |
| 1545 |
} |
| 1546 |
|
| 1547 |
if (!mir->compilingWasm() && !mir->outerInfo().hadUnboxFoldingBailout()) { |
| 1548 |
if (!FoldLoadsWithUnbox(mir, graph)) { |
| 1549 |
return false; |
| 1550 |
} |
| 1551 |
gs.spewPass("FoldLoadsWithUnbox"); |
| 1552 |
AssertGraphCoherency(graph); |
| 1553 |
|
| 1554 |
if (mir->shouldCancel("FoldLoadsWithUnbox")) { |
| 1555 |
return false; |
| 1556 |
} |
| 1557 |
} |
| 1558 |
|
| 1559 |
if (!mir->compilingWasm()) { |
| 1560 |
if (!AddKeepAliveInstructions(graph)) { |
| 1561 |
return false; |
| 1562 |
} |
| 1563 |
gs.spewPass("Add KeepAlive Instructions"); |
| 1564 |
AssertGraphCoherency(graph); |
| 1565 |
|
| 1566 |
if (mir->shouldCancel("Add KeepAlive Instructions")) { |
| 1567 |
return false; |
| 1568 |
} |
| 1569 |
} |
| 1570 |
|
| 1571 |
AssertGraphCoherency(graph, /* force = */ true); |
| 1572 |
|
| 1573 |
if (JitSpewEnabled(JitSpew_MIRExpressions)) { |
| 1574 |
JitSpewCont(JitSpew_MIRExpressions, "\n"); |
| 1575 |
DumpMIRExpressions(JitSpewPrinter(), graph, mir->outerInfo(), |
| 1576 |
"BeforeLIR (== result of OptimizeMIR)"); |
| 1577 |
} |
| 1578 |
|
| 1579 |
return true; |
| 1580 |
} |
| 1581 |
|
| 1582 |
LIRGraph* GenerateLIR(MIRGenerator* mir) { |
| 1583 |
MIRGraph& graph = mir->graph(); |
| 1584 |
GraphSpewer& gs = mir->graphSpewer(); |
| 1585 |
|
| 1586 |
LIRGraph* lir = mir->alloc().lifoAlloc()->new_<LIRGraph>(&graph); |
| 1587 |
if (!lir || !lir->init()) { |
| 1588 |
return nullptr; |
| 1589 |
} |
| 1590 |
|
| 1591 |
LIRGenerator lirgen(mir, graph, *lir); |
| 1592 |
{ |
| 1593 |
if (!lirgen.generate()) { |
| 1594 |
return nullptr; |
| 1595 |
} |
| 1596 |
gs.spewPass("Generate LIR"); |
| 1597 |
|
| 1598 |
if (mir->shouldCancel("Generate LIR")) { |
| 1599 |
return nullptr; |
| 1600 |
} |
| 1601 |
} |
| 1602 |
|
| 1603 |
#ifdef DEBUG |
| 1604 |
AllocationIntegrityState integrity(*lir); |
| 1605 |
if (JitOptions.fullDebugChecks) { |
| 1606 |
if (!integrity.record()) { |
| 1607 |
return nullptr; |
| 1608 |
} |
| 1609 |
} |
| 1610 |
#endif |
| 1611 |
|
| 1612 |
IonRegisterAllocator allocator = mir->optimizationInfo().registerAllocator(); |
| 1613 |
switch (allocator) { |
| 1614 |
case RegisterAllocator_Backtracking: { |
| 1615 |
BacktrackingAllocator regalloc(mir, &lirgen, *lir); |
| 1616 |
if (!regalloc.go()) { |
| 1617 |
return nullptr; |
| 1618 |
} |
| 1619 |
gs.spewPass("Allocate Registers [Backtracking]"); |
| 1620 |
break; |
| 1621 |
} |
| 1622 |
case RegisterAllocator_Simple: { |
| 1623 |
SimpleAllocator regalloc(mir, &lirgen, *lir); |
| 1624 |
if (!regalloc.go()) { |
| 1625 |
return nullptr; |
| 1626 |
} |
| 1627 |
gs.spewPass("Allocate Registers [Simple]"); |
| 1628 |
break; |
| 1629 |
} |
| 1630 |
default: |
| 1631 |
MOZ_CRASH("Bad regalloc"); |
| 1632 |
} |
| 1633 |
|
| 1634 |
#ifdef DEBUG |
| 1635 |
if (JitOptions.fullDebugChecks) { |
| 1636 |
if (!integrity.check()) { |
| 1637 |
return nullptr; |
| 1638 |
} |
| 1639 |
} |
| 1640 |
#endif |
| 1641 |
|
| 1642 |
if (mir->shouldCancel("Allocate Registers")) { |
| 1643 |
return nullptr; |
| 1644 |
} |
| 1645 |
|
| 1646 |
return lir; |
| 1647 |
} |
| 1648 |
|
| 1649 |
static CodeGenerator* GenerateCode(MIRGenerator* mir, LIRGraph* lir, |
| 1650 |
const WarpSnapshot* snapshot) { |
| 1651 |
auto codegen = MakeUnique<CodeGenerator>(mir, lir); |
| 1652 |
if (!codegen) { |
| 1653 |
return nullptr; |
| 1654 |
} |
| 1655 |
|
| 1656 |
if (!codegen->generate(snapshot)) { |
| 1657 |
return nullptr; |
| 1658 |
} |
| 1659 |
|
| 1660 |
return codegen.release(); |
| 1661 |
} |
| 1662 |
|
| 1663 |
CodeGenerator* CompileBackEnd(MIRGenerator* mir, WarpSnapshot* snapshot) { |
| 1664 |
// Everything in CompileBackEnd can potentially run on a helper thread. |
| 1665 |
AutoEnterIonBackend enter; |
| 1666 |
AutoSpewEndFunction spewEndFunction(mir); |
| 1667 |
mozilla::TimeStamp compileStartTime = mozilla::TimeStamp::Now(); |
| 1668 |
|
| 1669 |
{ |
| 1670 |
WarpCompilation comp(mir->alloc()); |
| 1671 |
WarpBuilder builder(*snapshot, *mir, &comp); |
| 1672 |
if (!builder.build()) { |
| 1673 |
return nullptr; |
| 1674 |
} |
| 1675 |
} |
| 1676 |
|
| 1677 |
if (!OptimizeMIR(mir)) { |
| 1678 |
return nullptr; |
| 1679 |
} |
| 1680 |
|
| 1681 |
LIRGraph* lir = GenerateLIR(mir); |
| 1682 |
if (!lir) { |
| 1683 |
return nullptr; |
| 1684 |
} |
| 1685 |
|
| 1686 |
CodeGenerator* codegen = GenerateCode(mir, lir, snapshot); |
| 1687 |
if (codegen) { |
| 1688 |
codegen->setCompilationTime(mozilla::TimeStamp::Now() - compileStartTime); |
| 1689 |
} |
| 1690 |
return codegen; |
| 1691 |
} |
| 1692 |
|
| 1693 |
static AbortReasonOr<WarpSnapshot*> CreateWarpSnapshot(JSContext* cx, |
| 1694 |
MIRGenerator* mirGen, |
| 1695 |
HandleScript script) { |
| 1696 |
// Suppress GC during compilation. |
| 1697 |
gc::AutoSuppressGC suppressGC(cx); |
| 1698 |
|
| 1699 |
SpewBeginFunction(mirGen, script); |
| 1700 |
|
| 1701 |
WarpOracle oracle(cx, *mirGen, script); |
| 1702 |
|
| 1703 |
AbortReasonOr<WarpSnapshot*> result = oracle.createSnapshot(); |
| 1704 |
|
| 1705 |
MOZ_ASSERT_IF(result.isErr(), result.unwrapErr() == AbortReason::Alloc || |
| 1706 |
result.unwrapErr() == AbortReason::Error || |
| 1707 |
result.unwrapErr() == AbortReason::Disable); |
| 1708 |
MOZ_ASSERT_IF(!result.isErr(), result.unwrap()); |
| 1709 |
|
| 1710 |
return result; |
| 1711 |
} |
| 1712 |
|
| 1713 |
UniquePtr<LifoAlloc> JitRuntime::tryReuseIonLifoAlloc() { |
| 1714 |
// Try to reuse the LifoAlloc of a finished Ion compilation task for a new |
| 1715 |
// Ion compilation. If there are multiple tasks, we pick the one with the |
| 1716 |
// largest LifoAlloc. |
| 1717 |
|
| 1718 |
auto& batch = ionFreeTaskBatch_.ref(); |
| 1719 |
IonCompileTask* bestTask = nullptr; |
| 1720 |
size_t bestTaskIndex = 0; |
| 1721 |
size_t bestTaskSize = 0; |
| 1722 |
|
| 1723 |
for (size_t i = 0, len = batch.length(); i < len; i++) { |
| 1724 |
IonCompileTask* task = batch[i]; |
| 1725 |
if (task->alloc().lifoAlloc()->isHuge()) { |
| 1726 |
// Ignore 'huge' LifoAllocs. This avoids keeping a lot of memory alive and |
| 1727 |
// also avoids freeing all LifoAlloc memory (instead of reusing it) in |
| 1728 |
// freeAllIfHugeAndUnused. |
| 1729 |
continue; |
| 1730 |
} |
| 1731 |
size_t taskSize = task->alloc().lifoAlloc()->computedSizeOfExcludingThis(); |
| 1732 |
if (!bestTask || taskSize >= bestTaskSize) { |
| 1733 |
bestTask = task; |
| 1734 |
bestTaskIndex = i; |
| 1735 |
bestTaskSize = taskSize; |
| 1736 |
} |
| 1737 |
} |
| 1738 |
|
| 1739 |
if (bestTask) { |
| 1740 |
batch.erase(&batch[bestTaskIndex]); |
| 1741 |
return FreeIonCompileTaskAndReuseLifoAlloc(bestTask); |
| 1742 |
} |
| 1743 |
|
| 1744 |
return nullptr; |
| 1745 |
} |
| 1746 |
|
| 1747 |
static AbortReason IonCompile(JSContext* cx, HandleScript script, |
| 1748 |
jsbytecode* osrPc) { |
| 1749 |
cx->check(script); |
| 1750 |
|
| 1751 |
if (!cx->zone()->ensureJitZoneExists(cx)) { |
| 1752 |
return AbortReason::Error; |
| 1753 |
} |
| 1754 |
|
| 1755 |
UniquePtr<LifoAlloc> alloc = |
| 1756 |
cx->runtime()->jitRuntime()->tryReuseIonLifoAlloc(); |
| 1757 |
if (!alloc) { |
| 1758 |
alloc = cx->make_unique<LifoAlloc>(TempAllocator::PreferredLifoChunkSize, |
| 1759 |
js::MallocArena); |
| 1760 |
if (!alloc) { |
| 1761 |
return AbortReason::Error; |
| 1762 |
} |
| 1763 |
} |
| 1764 |
|
| 1765 |
TempAllocator* temp = alloc->new_<TempAllocator>(alloc.get()); |
| 1766 |
if (!temp) { |
| 1767 |
return AbortReason::Alloc; |
| 1768 |
} |
| 1769 |
|
| 1770 |
MIRGraph* graph = alloc->new_<MIRGraph>(temp); |
| 1771 |
if (!graph) { |
| 1772 |
return AbortReason::Alloc; |
| 1773 |
} |
| 1774 |
|
| 1775 |
InlineScriptTree* inlineScriptTree = |
| 1776 |
InlineScriptTree::New(temp, nullptr, nullptr, script); |
| 1777 |
if (!inlineScriptTree) { |
| 1778 |
return AbortReason::Alloc; |
| 1779 |
} |
| 1780 |
|
| 1781 |
CompileInfo* info = alloc->new_<CompileInfo>( |
| 1782 |
CompileRuntime::get(cx->runtime()), script, script->function(), osrPc, |
| 1783 |
script->needsArgsObj(), inlineScriptTree); |
| 1784 |
if (!info) { |
| 1785 |
return AbortReason::Alloc; |
| 1786 |
} |
| 1787 |
|
| 1788 |
const OptimizationInfo* optimizationInfo = |
| 1789 |
IonOptimizations.get(OptimizationLevel::Normal); |
| 1790 |
const JitCompileOptions options(cx); |
| 1791 |
|
| 1792 |
MIRGenerator* mirGen = |
| 1793 |
alloc->new_<MIRGenerator>(CompileRealm::get(cx->realm()), options, temp, |
| 1794 |
graph, info, optimizationInfo); |
| 1795 |
if (!mirGen) { |
| 1796 |
return AbortReason::Alloc; |
| 1797 |
} |
| 1798 |
|
| 1799 |
auto clearDependencies = |
| 1800 |
mozilla::MakeScopeExit([mirGen]() { mirGen->tracker.reset(); }); |
| 1801 |
|
| 1802 |
MOZ_ASSERT(!script->baselineScript()->hasPendingIonCompileTask()); |
| 1803 |
MOZ_ASSERT(!script->hasIonScript()); |
| 1804 |
MOZ_ASSERT(script->canIonCompile()); |
| 1805 |
|
| 1806 |
if (osrPc) { |
| 1807 |
script->jitScript()->setHadIonOSR(); |
| 1808 |
} |
| 1809 |
|
| 1810 |
AbortReasonOr<WarpSnapshot*> result = CreateWarpSnapshot(cx, mirGen, script); |
| 1811 |
if (result.isErr()) { |
| 1812 |
return result.unwrapErr(); |
| 1813 |
} |
| 1814 |
WarpSnapshot* snapshot = result.unwrap(); |
| 1815 |
|
| 1816 |
// If possible, compile the script off thread. |
| 1817 |
if (options.offThreadCompilationAvailable()) { |
| 1818 |
JitSpew(JitSpew_IonSyncLogs, |
| 1819 |
"Can't log script %s:%u:%u" |
| 1820 |
". (Compiled on background thread.)", |
| 1821 |
script->filename(), script->lineno(), |
| 1822 |
script->column().oneOriginValue()); |
| 1823 |
|
| 1824 |
IonCompileTask* task = alloc->new_<IonCompileTask>(cx, *mirGen, snapshot); |
| 1825 |
if (!task) { |
| 1826 |
return AbortReason::Alloc; |
| 1827 |
} |
| 1828 |
|
| 1829 |
AutoLockHelperThreadState lock; |
| 1830 |
if (!StartOffThreadIonCompile(task, lock)) { |
| 1831 |
JitSpew(JitSpew_IonAbort, "Unable to start off-thread ion compilation."); |
| 1832 |
mirGen->graphSpewer().endFunction(); |
| 1833 |
return AbortReason::Alloc; |
| 1834 |
} |
| 1835 |
|
| 1836 |
script->jitScript()->setIsIonCompilingOffThread(script); |
| 1837 |
|
| 1838 |
// The allocator and associated data will be destroyed after being |
| 1839 |
// processed in the finishedOffThreadCompilations list. |
| 1840 |
(void)alloc.release(); |
| 1841 |
clearDependencies.release(); |
| 1842 |
|
| 1843 |
return AbortReason::NoAbort; |
| 1844 |
} |
| 1845 |
|
| 1846 |
bool succeeded = false; |
| 1847 |
{ |
| 1848 |
gc::AutoSuppressGC suppressGC(cx); |
| 1849 |
JitContext jctx(cx); |
| 1850 |
UniquePtr<CodeGenerator> codegen(CompileBackEnd(mirGen, snapshot)); |
| 1851 |
if (!codegen) { |
| 1852 |
JitSpew(JitSpew_IonAbort, "Failed during back-end compilation."); |
| 1853 |
if (cx->isExceptionPending()) { |
| 1854 |
return AbortReason::Error; |
| 1855 |
} |
| 1856 |
return AbortReason::Disable; |
| 1857 |
} |
| 1858 |
|
| 1859 |
succeeded = LinkCodeGen(cx, codegen.get(), script); |
| 1860 |
} |
| 1861 |
|
| 1862 |
if (succeeded) { |
| 1863 |
return AbortReason::NoAbort; |
| 1864 |
} |
| 1865 |
if (cx->isExceptionPending()) { |
| 1866 |
return AbortReason::Error; |
| 1867 |
} |
| 1868 |
return AbortReason::Disable; |
| 1869 |
} |
| 1870 |
|
| 1871 |
static bool CheckFrame(JSContext* cx, BaselineFrame* frame) { |
| 1872 |
MOZ_ASSERT(!frame->isDebuggerEvalFrame()); |
| 1873 |
MOZ_ASSERT(!frame->isEvalFrame()); |
| 1874 |
|
| 1875 |
// This check is to not overrun the stack. |
| 1876 |
if (frame->isFunctionFrame()) { |
| 1877 |
if (TooManyActualArguments(frame->numActualArgs())) { |
| 1878 |
JitSpew(JitSpew_IonAbort, "too many actual arguments"); |
| 1879 |
return false; |
| 1880 |
} |
| 1881 |
|
| 1882 |
if (TooManyFormalArguments(frame->numFormalArgs())) { |
| 1883 |
JitSpew(JitSpew_IonAbort, "too many arguments"); |
| 1884 |
return false; |
| 1885 |
} |
| 1886 |
} |
| 1887 |
|
| 1888 |
return true; |
| 1889 |
} |
| 1890 |
|
| 1891 |
static bool CanIonCompileOrInlineScript(JSScript* script, const char** reason) { |
| 1892 |
if (script->isForEval()) { |
| 1893 |
// Eval frames are not yet supported. Supporting this will require new |
| 1894 |
// logic in pushBailoutFrame to deal with linking prev. |
| 1895 |
// Additionally, JSOp::GlobalOrEvalDeclInstantiation support will require |
| 1896 |
// baking in isEvalFrame(). |
| 1897 |
*reason = "eval script"; |
| 1898 |
return false; |
| 1899 |
} |
| 1900 |
|
| 1901 |
if (script->isAsync()) { |
| 1902 |
if (script->isModule()) { |
| 1903 |
*reason = "async module"; |
| 1904 |
return false; |
| 1905 |
} |
| 1906 |
} |
| 1907 |
|
| 1908 |
if (script->hasNonSyntacticScope() && !script->function()) { |
| 1909 |
// Support functions with a non-syntactic global scope but not other |
| 1910 |
// scripts. For global scripts, WarpBuilder currently uses the global |
| 1911 |
// object as scope chain, this is not valid when the script has a |
| 1912 |
// non-syntactic global scope. |
| 1913 |
*reason = "has non-syntactic global scope"; |
| 1914 |
return false; |
| 1915 |
} |
| 1916 |
|
| 1917 |
return true; |
| 1918 |
} // namespace jit |
| 1919 |
|
| 1920 |
static bool ScriptIsTooLarge(JSContext* cx, JSScript* script) { |
| 1921 |
if (!JitOptions.limitScriptSize) { |
| 1922 |
return false; |
| 1923 |
} |
| 1924 |
|
| 1925 |
size_t numLocalsAndArgs = NumLocalsAndArgs(script); |
| 1926 |
|
| 1927 |
bool canCompileOffThread = OffThreadCompilationAvailable(cx); |
| 1928 |
size_t maxScriptSize = canCompileOffThread |
| 1929 |
? JitOptions.ionMaxScriptSize |
| 1930 |
: JitOptions.ionMaxScriptSizeMainThread; |
| 1931 |
size_t maxLocalsAndArgs = canCompileOffThread |
| 1932 |
? JitOptions.ionMaxLocalsAndArgs |
| 1933 |
: JitOptions.ionMaxLocalsAndArgsMainThread; |
| 1934 |
|
| 1935 |
if (script->length() > maxScriptSize || numLocalsAndArgs > maxLocalsAndArgs) { |
| 1936 |
JitSpew(JitSpew_IonAbort, |
| 1937 |
"Script too large (%zu bytes) (%zu locals/args) @ %s:%u:%u", |
| 1938 |
script->length(), numLocalsAndArgs, script->filename(), |
| 1939 |
script->lineno(), script->column().oneOriginValue()); |
| 1940 |
return true; |
| 1941 |
} |
| 1942 |
|
| 1943 |
return false; |
| 1944 |
} |
| 1945 |
|
| 1946 |
bool CanIonCompileScript(JSContext* cx, JSScript* script) { |
| 1947 |
if (!script->canIonCompile()) { |
| 1948 |
return false; |
| 1949 |
} |
| 1950 |
|
| 1951 |
const char* reason = nullptr; |
| 1952 |
if (!CanIonCompileOrInlineScript(script, &reason)) { |
| 1953 |
JitSpew(JitSpew_IonAbort, "%s", reason); |
| 1954 |
return false; |
| 1955 |
} |
| 1956 |
|
| 1957 |
if (ScriptIsTooLarge(cx, script)) { |
| 1958 |
return false; |
| 1959 |
} |
| 1960 |
|
| 1961 |
return true; |
| 1962 |
} |
| 1963 |
|
| 1964 |
static MethodStatus Compile(JSContext* cx, HandleScript script, |
| 1965 |
BaselineFrame* osrFrame, jsbytecode* osrPc) { |
| 1966 |
MOZ_ASSERT(jit::IsIonEnabled(cx)); |
| 1967 |
MOZ_ASSERT(jit::IsBaselineJitEnabled(cx)); |
| 1968 |
|
| 1969 |
MOZ_ASSERT(script->hasBaselineScript()); |
| 1970 |
MOZ_ASSERT(!script->baselineScript()->hasPendingIonCompileTask()); |
| 1971 |
MOZ_ASSERT(!script->hasIonScript()); |
| 1972 |
|
| 1973 |
AutoGeckoProfilerEntry pseudoFrame( |
| 1974 |
cx, "Ion script compilation", |
| 1975 |
JS::ProfilingCategoryPair::JS_IonCompilation); |
| 1976 |
|
| 1977 |
if (script->isDebuggee() || (osrFrame && osrFrame->isDebuggee())) { |
| 1978 |
JitSpew(JitSpew_IonAbort, "debugging"); |
| 1979 |
return Method_Skipped; |
| 1980 |
} |
| 1981 |
|
| 1982 |
if (!CanIonCompileScript(cx, script)) { |
| 1983 |
JitSpew(JitSpew_IonAbort, "Aborted compilation of %s:%u:%u", |
| 1984 |
script->filename(), script->lineno(), |
| 1985 |
script->column().oneOriginValue()); |
| 1986 |
return Method_CantCompile; |
| 1987 |
} |
| 1988 |
|
| 1989 |
OptimizationLevel optimizationLevel = |
| 1990 |
IonOptimizations.levelForScript(cx, script, osrPc); |
| 1991 |
if (optimizationLevel == OptimizationLevel::DontCompile) { |
| 1992 |
return Method_Skipped; |
| 1993 |
} |
| 1994 |
|
| 1995 |
MOZ_ASSERT(optimizationLevel == OptimizationLevel::Normal); |
| 1996 |
|
| 1997 |
if (!CanLikelyAllocateMoreExecutableMemory()) { |
| 1998 |
script->resetWarmUpCounterToDelayIonCompilation(); |
| 1999 |
return Method_Skipped; |
| 2000 |
} |
| 2001 |
|
| 2002 |
MOZ_ASSERT(!script->hasIonScript()); |
| 2003 |
|
| 2004 |
AbortReason reason = IonCompile(cx, script, osrPc); |
| 2005 |
if (reason == AbortReason::Error) { |
| 2006 |
MOZ_ASSERT(cx->isExceptionPending()); |
| 2007 |
return Method_Error; |
| 2008 |
} |
| 2009 |
|
| 2010 |
if (reason == AbortReason::Disable) { |
| 2011 |
return Method_CantCompile; |
| 2012 |
} |
| 2013 |
|
| 2014 |
if (reason == AbortReason::Alloc) { |
| 2015 |
ReportOutOfMemory(cx); |
| 2016 |
return Method_Error; |
| 2017 |
} |
| 2018 |
|
| 2019 |
// Compilation succeeded or we invalidated right away or an inlining/alloc |
| 2020 |
// abort |
| 2021 |
if (script->hasIonScript()) { |
| 2022 |
return Method_Compiled; |
| 2023 |
} |
| 2024 |
return Method_Skipped; |
| 2025 |
} |
| 2026 |
|
| 2027 |
} // namespace jit |
| 2028 |
} // namespace js |
| 2029 |
|
| 2030 |
bool jit::OffThreadCompilationAvailable(JSContext* cx) { |
| 2031 |
// Even if off thread compilation is enabled, compilation must still occur |
| 2032 |
// on the main thread in some cases. |
| 2033 |
// |
| 2034 |
// Require cpuCount > 1 so that Ion compilation jobs and active-thread |
| 2035 |
// execution are not competing for the same resources. |
| 2036 |
return cx->runtime()->canUseOffthreadIonCompilation() && |
| 2037 |
GetHelperThreadCPUCount() > 1 && CanUseExtraThreads(); |
| 2038 |
} |
| 2039 |
|
| 2040 |
MethodStatus jit::CanEnterIon(JSContext* cx, RunState& state) { |
| 2041 |
MOZ_ASSERT(jit::IsIonEnabled(cx)); |
| 2042 |
|
| 2043 |
HandleScript script = state.script(); |
| 2044 |
MOZ_ASSERT(!script->hasIonScript()); |
| 2045 |
|
| 2046 |
// Skip if the script has been disabled. |
| 2047 |
if (!script->canIonCompile()) { |
| 2048 |
return Method_Skipped; |
| 2049 |
} |
| 2050 |
|
| 2051 |
// Skip if the script is being compiled off thread. |
| 2052 |
if (script->isIonCompilingOffThread()) { |
| 2053 |
return Method_Skipped; |
| 2054 |
} |
| 2055 |
|
| 2056 |
if (state.isInvoke()) { |
| 2057 |
InvokeState& invoke = *state.asInvoke(); |
| 2058 |
|
| 2059 |
if (TooManyActualArguments(invoke.args().length())) { |
| 2060 |
JitSpew(JitSpew_IonAbort, "too many actual args"); |
| 2061 |
ForbidCompilation(cx, script); |
| 2062 |
return Method_CantCompile; |
| 2063 |
} |
| 2064 |
|
| 2065 |
if (TooManyFormalArguments( |
| 2066 |
invoke.args().callee().as<JSFunction>().nargs())) { |
| 2067 |
JitSpew(JitSpew_IonAbort, "too many args"); |
| 2068 |
ForbidCompilation(cx, script); |
| 2069 |
return Method_CantCompile; |
| 2070 |
} |
| 2071 |
} |
| 2072 |
|
| 2073 |
// If --ion-eager is used, compile with Baseline first, so that we |
| 2074 |
// can directly enter IonMonkey. |
| 2075 |
if (JitOptions.eagerIonCompilation() && !script->hasBaselineScript()) { |
| 2076 |
MethodStatus status = |
| 2077 |
CanEnterBaselineMethod<BaselineTier::Compiler>(cx, state); |
| 2078 |
if (status != Method_Compiled) { |
| 2079 |
return status; |
| 2080 |
} |
| 2081 |
// Bytecode analysis may forbid compilation for a script. |
| 2082 |
if (!script->canIonCompile()) { |
| 2083 |
return Method_CantCompile; |
| 2084 |
} |
| 2085 |
} |
| 2086 |
|
| 2087 |
if (!script->hasBaselineScript()) { |
| 2088 |
return Method_Skipped; |
| 2089 |
} |
| 2090 |
|
| 2091 |
MOZ_ASSERT(!script->isIonCompilingOffThread()); |
| 2092 |
MOZ_ASSERT(script->canIonCompile()); |
| 2093 |
|
| 2094 |
// Attempt compilation. Returns Method_Compiled if already compiled. |
| 2095 |
MethodStatus status = Compile(cx, script, /* osrFrame = */ nullptr, |
| 2096 |
/* osrPc = */ nullptr); |
| 2097 |
if (status != Method_Compiled) { |
| 2098 |
if (status == Method_CantCompile) { |
| 2099 |
ForbidCompilation(cx, script); |
| 2100 |
} |
| 2101 |
return status; |
| 2102 |
} |
| 2103 |
|
| 2104 |
if (state.script()->baselineScript()->hasPendingIonCompileTask()) { |
| 2105 |
LinkIonScript(cx, state.script()); |
| 2106 |
if (!state.script()->hasIonScript()) { |
| 2107 |
return jit::Method_Skipped; |
| 2108 |
} |
| 2109 |
} |
| 2110 |
|
| 2111 |
return Method_Compiled; |
| 2112 |
} |
| 2113 |
|
| 2114 |
static MethodStatus BaselineCanEnterAtEntry(JSContext* cx, HandleScript script, |
| 2115 |
BaselineFrame* frame) { |
| 2116 |
MOZ_ASSERT(jit::IsIonEnabled(cx)); |
| 2117 |
MOZ_ASSERT(script->canIonCompile()); |
| 2118 |
MOZ_ASSERT(!script->isIonCompilingOffThread()); |
| 2119 |
MOZ_ASSERT(!script->hasIonScript()); |
| 2120 |
MOZ_ASSERT(frame->isFunctionFrame()); |
| 2121 |
|
| 2122 |
// Mark as forbidden if frame can't be handled. |
| 2123 |
if (!CheckFrame(cx, frame)) { |
| 2124 |
ForbidCompilation(cx, script); |
| 2125 |
return Method_CantCompile; |
| 2126 |
} |
| 2127 |
|
| 2128 |
if (script->baselineScript()->hasPendingIonCompileTask()) { |
| 2129 |
LinkIonScript(cx, script); |
| 2130 |
if (script->hasIonScript()) { |
| 2131 |
return Method_Compiled; |
| 2132 |
} |
| 2133 |
} |
| 2134 |
|
| 2135 |
// Attempt compilation. Returns Method_Compiled if already compiled. |
| 2136 |
MethodStatus status = Compile(cx, script, frame, nullptr); |
| 2137 |
if (status != Method_Compiled) { |
| 2138 |
if (status == Method_CantCompile) { |
| 2139 |
ForbidCompilation(cx, script); |
| 2140 |
} |
| 2141 |
return status; |
| 2142 |
} |
| 2143 |
|
| 2144 |
return Method_Compiled; |
| 2145 |
} |
| 2146 |
|
| 2147 |
// Decide if a transition from baseline execution to Ion code should occur. |
| 2148 |
// May compile or recompile the target JSScript. |
| 2149 |
static MethodStatus BaselineCanEnterAtBranch(JSContext* cx, HandleScript script, |
| 2150 |
BaselineFrame* osrFrame, |
| 2151 |
jsbytecode* pc) { |
| 2152 |
MOZ_ASSERT(jit::IsIonEnabled(cx)); |
| 2153 |
MOZ_ASSERT((JSOp)*pc == JSOp::LoopHead); |
| 2154 |
|
| 2155 |
// Skip if the script has been disabled. |
| 2156 |
if (!script->canIonCompile()) { |
| 2157 |
return Method_Skipped; |
| 2158 |
} |
| 2159 |
|
| 2160 |
// Skip if the script is being compiled off thread. |
| 2161 |
if (script->isIonCompilingOffThread()) { |
| 2162 |
return Method_Skipped; |
| 2163 |
} |
| 2164 |
|
| 2165 |
// Optionally ignore on user request. |
| 2166 |
if (!JitOptions.osr) { |
| 2167 |
return Method_Skipped; |
| 2168 |
} |
| 2169 |
|
| 2170 |
// Mark as forbidden if frame can't be handled. |
| 2171 |
if (!CheckFrame(cx, osrFrame)) { |
| 2172 |
ForbidCompilation(cx, script); |
| 2173 |
return Method_CantCompile; |
| 2174 |
} |
| 2175 |
|
| 2176 |
// Check if the jitcode still needs to get linked and do this |
| 2177 |
// to have a valid IonScript. |
| 2178 |
if (script->baselineScript()->hasPendingIonCompileTask()) { |
| 2179 |
LinkIonScript(cx, script); |
| 2180 |
} |
| 2181 |
|
| 2182 |
// By default a recompilation doesn't happen on osr mismatch. |
| 2183 |
// Decide if we want to force a recompilation if this happens too much. |
| 2184 |
if (script->hasIonScript()) { |
| 2185 |
if (pc == script->ionScript()->osrPc()) { |
| 2186 |
return Method_Compiled; |
| 2187 |
} |
| 2188 |
|
| 2189 |
uint32_t count = script->ionScript()->incrOsrPcMismatchCounter(); |
| 2190 |
if (count <= JitOptions.osrPcMismatchesBeforeRecompile && |
| 2191 |
!JitOptions.eagerIonCompilation()) { |
| 2192 |
return Method_Skipped; |
| 2193 |
} |
| 2194 |
|
| 2195 |
JitSpew(JitSpew_IonScripts, "Forcing OSR Mismatch Compilation"); |
| 2196 |
Invalidate(cx, script); |
| 2197 |
} |
| 2198 |
|
| 2199 |
// Attempt compilation. |
| 2200 |
// - Returns Method_Compiled if the right ionscript is present |
| 2201 |
// (Meaning it was present or a sequantial compile finished) |
| 2202 |
// - Returns Method_Skipped if pc doesn't match |
| 2203 |
// (This means a background thread compilation with that pc could have |
| 2204 |
// started or not.) |
| 2205 |
MethodStatus status = Compile(cx, script, osrFrame, pc); |
| 2206 |
if (status != Method_Compiled) { |
| 2207 |
if (status == Method_CantCompile) { |
| 2208 |
ForbidCompilation(cx, script); |
| 2209 |
} |
| 2210 |
return status; |
| 2211 |
} |
| 2212 |
|
| 2213 |
// Return the compilation was skipped when the osr pc wasn't adjusted. |
| 2214 |
// This can happen when there was still an IonScript available and a |
| 2215 |
// background compilation started, but hasn't finished yet. |
| 2216 |
// Or when we didn't force a recompile. |
| 2217 |
if (script->hasIonScript() && pc != script->ionScript()->osrPc()) { |
| 2218 |
return Method_Skipped; |
| 2219 |
} |
| 2220 |
|
| 2221 |
return Method_Compiled; |
| 2222 |
} |
| 2223 |
|
| 2224 |
static bool IonCompileScriptForBaseline(JSContext* cx, BaselineFrame* frame, |
| 2225 |
jsbytecode* pc) { |
| 2226 |
MOZ_ASSERT(IsIonEnabled(cx)); |
| 2227 |
|
| 2228 |
RootedScript script(cx, frame->script()); |
| 2229 |
bool isLoopHead = JSOp(*pc) == JSOp::LoopHead; |
| 2230 |
|
| 2231 |
// The Baseline JIT code checks for Ion disabled or compiling off-thread. |
| 2232 |
MOZ_ASSERT(script->canIonCompile()); |
| 2233 |
MOZ_ASSERT(!script->isIonCompilingOffThread()); |
| 2234 |
|
| 2235 |
// If Ion script exists, but PC is not at a loop entry, then Ion will be |
| 2236 |
// entered for this script at an appropriate LOOPENTRY or the next time this |
| 2237 |
// function is called. |
| 2238 |
if (script->hasIonScript() && !isLoopHead) { |
| 2239 |
JitSpew(JitSpew_BaselineOSR, "IonScript exists, but not at loop entry!"); |
| 2240 |
// TODO: ASSERT that a ion-script-already-exists checker stub doesn't exist. |
| 2241 |
// TODO: Clear all optimized stubs. |
| 2242 |
// TODO: Add a ion-script-already-exists checker stub. |
| 2243 |
return true; |
| 2244 |
} |
| 2245 |
|
| 2246 |
// Ensure that Ion-compiled code is available. |
| 2247 |
JitSpew(JitSpew_BaselineOSR, |
| 2248 |
"WarmUpCounter for %s:%u:%u reached %d at pc %p, trying to switch to " |
| 2249 |
"Ion!", |
| 2250 |
script->filename(), script->lineno(), |
| 2251 |
script->column().oneOriginValue(), (int)script->getWarmUpCount(), |
| 2252 |
(void*)pc); |
| 2253 |
|
| 2254 |
MethodStatus stat; |
| 2255 |
if (isLoopHead) { |
| 2256 |
JitSpew(JitSpew_BaselineOSR, " Compile at loop head!"); |
| 2257 |
stat = BaselineCanEnterAtBranch(cx, script, frame, pc); |
| 2258 |
} else if (frame->isFunctionFrame()) { |
| 2259 |
JitSpew(JitSpew_BaselineOSR, |
| 2260 |
" Compile function from top for later entry!"); |
| 2261 |
stat = BaselineCanEnterAtEntry(cx, script, frame); |
| 2262 |
} else { |
| 2263 |
return true; |
| 2264 |
} |
| 2265 |
|
| 2266 |
if (stat == Method_Error) { |
| 2267 |
JitSpew(JitSpew_BaselineOSR, " Compile with Ion errored!"); |
| 2268 |
return false; |
| 2269 |
} |
| 2270 |
|
| 2271 |
if (stat == Method_CantCompile) { |
| 2272 |
MOZ_ASSERT(!script->canIonCompile()); |
| 2273 |
JitSpew(JitSpew_BaselineOSR, " Can't compile with Ion!"); |
| 2274 |
} else if (stat == Method_Skipped) { |
| 2275 |
JitSpew(JitSpew_BaselineOSR, " Skipped compile with Ion!"); |
| 2276 |
} else if (stat == Method_Compiled) { |
| 2277 |
JitSpew(JitSpew_BaselineOSR, " Compiled with Ion!"); |
| 2278 |
} else { |
| 2279 |
MOZ_CRASH("Invalid MethodStatus!"); |
| 2280 |
} |
| 2281 |
|
| 2282 |
return true; |
| 2283 |
} |
| 2284 |
|
| 2285 |
bool jit::IonCompileScriptForBaselineAtEntry(JSContext* cx, |
| 2286 |
BaselineFrame* frame) { |
| 2287 |
JSScript* script = frame->script(); |
| 2288 |
return IonCompileScriptForBaseline(cx, frame, script->code()); |
| 2289 |
} |
| 2290 |
|
| 2291 |
/* clang-format off */ |
| 2292 |
// The following data is kept in a temporary heap-allocated buffer, stored in |
| 2293 |
// JitRuntime (high memory addresses at top, low at bottom): |
| 2294 |
// |
| 2295 |
// +----->+=================================+ -- <---- High Address |
| 2296 |
// | | | | |
| 2297 |
// | | ...BaselineFrame... | |-- Copy of BaselineFrame + stack values |
| 2298 |
// | | | | |
| 2299 |
// | +---------------------------------+ | |
| 2300 |
// | | | | |
| 2301 |
// | | ...Locals/Stack... | | |
| 2302 |
// | | | | |
| 2303 |
// | +=================================+ -- |
| 2304 |
// | | Padding(Maybe Empty) | |
| 2305 |
// | +=================================+ -- |
| 2306 |
// +------|-- baselineFrame | |-- IonOsrTempData |
| 2307 |
// | jitcode | | |
| 2308 |
// +=================================+ -- <---- Low Address |
| 2309 |
// |
| 2310 |
// A pointer to the IonOsrTempData is returned. |
| 2311 |
/* clang-format on */ |
| 2312 |
|
| 2313 |
static IonOsrTempData* PrepareOsrTempData(JSContext* cx, BaselineFrame* frame, |
| 2314 |
uint32_t frameSize, void* jitcode) { |
| 2315 |
uint32_t numValueSlots = frame->numValueSlots(frameSize); |
| 2316 |
|
| 2317 |
// Calculate the amount of space to allocate: |
| 2318 |
// BaselineFrame space: |
| 2319 |
// (sizeof(Value) * numValueSlots) |
| 2320 |
// + sizeof(BaselineFrame) |
| 2321 |
// |
| 2322 |
// IonOsrTempData space: |
| 2323 |
// sizeof(IonOsrTempData) |
| 2324 |
|
| 2325 |
size_t frameSpace = sizeof(BaselineFrame) + sizeof(Value) * numValueSlots; |
| 2326 |
size_t ionOsrTempDataSpace = sizeof(IonOsrTempData); |
| 2327 |
|
| 2328 |
size_t totalSpace = AlignBytes(frameSpace, sizeof(Value)) + |
| 2329 |
AlignBytes(ionOsrTempDataSpace, sizeof(Value)); |
| 2330 |
|
| 2331 |
JitRuntime* jrt = cx->runtime()->jitRuntime(); |
| 2332 |
uint8_t* buf = jrt->allocateIonOsrTempData(totalSpace); |
| 2333 |
if (!buf) { |
| 2334 |
ReportOutOfMemory(cx); |
| 2335 |
return nullptr; |
| 2336 |
} |
| 2337 |
|
| 2338 |
IonOsrTempData* info = new (buf) IonOsrTempData(); |
| 2339 |
info->jitcode = jitcode; |
| 2340 |
|
| 2341 |
// Copy the BaselineFrame + local/stack Values to the buffer. Arguments and |
| 2342 |
// |this| are not copied but left on the stack: the Baseline and Ion frame |
| 2343 |
// share the same frame prefix and Ion won't clobber these values. Note |
| 2344 |
// that info->baselineFrame will point to the *end* of the frame data, like |
| 2345 |
// the frame pointer register in baseline frames. |
| 2346 |
uint8_t* frameStart = |
| 2347 |
(uint8_t*)info + AlignBytes(ionOsrTempDataSpace, sizeof(Value)); |
| 2348 |
info->baselineFrame = frameStart + frameSpace; |
| 2349 |
|
| 2350 |
memcpy(frameStart, (uint8_t*)frame - numValueSlots * sizeof(Value), |
| 2351 |
frameSpace); |
| 2352 |
|
| 2353 |
JitSpew(JitSpew_BaselineOSR, "Allocated IonOsrTempData at %p", info); |
| 2354 |
JitSpew(JitSpew_BaselineOSR, "Jitcode is %p", info->jitcode); |
| 2355 |
|
| 2356 |
// All done. |
| 2357 |
return info; |
| 2358 |
} |
| 2359 |
|
| 2360 |
bool jit::IonCompileScriptForBaselineOSR(JSContext* cx, BaselineFrame* frame, |
| 2361 |
uint32_t frameSize, jsbytecode* pc, |
| 2362 |
IonOsrTempData** infoPtr) { |
| 2363 |
MOZ_ASSERT(infoPtr); |
| 2364 |
*infoPtr = nullptr; |
| 2365 |
|
| 2366 |
MOZ_ASSERT(frame->debugFrameSize() == frameSize); |
| 2367 |
MOZ_ASSERT(JSOp(*pc) == JSOp::LoopHead); |
| 2368 |
|
| 2369 |
if (!IonCompileScriptForBaseline(cx, frame, pc)) { |
| 2370 |
return false; |
| 2371 |
} |
| 2372 |
|
| 2373 |
RootedScript script(cx, frame->script()); |
| 2374 |
if (!script->hasIonScript() || script->ionScript()->osrPc() != pc || |
| 2375 |
frame->isDebuggee()) { |
| 2376 |
return true; |
| 2377 |
} |
| 2378 |
|
| 2379 |
IonScript* ion = script->ionScript(); |
| 2380 |
MOZ_ASSERT(cx->runtime()->geckoProfiler().enabled() == |
| 2381 |
ion->hasProfilingInstrumentation()); |
| 2382 |
MOZ_ASSERT(ion->osrPc() == pc); |
| 2383 |
|
| 2384 |
ion->resetOsrPcMismatchCounter(); |
| 2385 |
|
| 2386 |
JitSpew(JitSpew_BaselineOSR, " OSR possible!"); |
| 2387 |
void* jitcode = ion->method()->raw() + ion->osrEntryOffset(); |
| 2388 |
|
| 2389 |
// Prepare the temporary heap copy of the fake InterpreterFrame and actual |
| 2390 |
// args list. |
| 2391 |
JitSpew(JitSpew_BaselineOSR, "Got jitcode. Preparing for OSR into ion."); |
| 2392 |
IonOsrTempData* info = PrepareOsrTempData(cx, frame, frameSize, jitcode); |
| 2393 |
if (!info) { |
| 2394 |
return false; |
| 2395 |
} |
| 2396 |
|
| 2397 |
*infoPtr = info; |
| 2398 |
return true; |
| 2399 |
} |
| 2400 |
|
| 2401 |
static void InvalidateActivation(JS::GCContext* gcx, |
| 2402 |
const JitActivationIterator& activations, |
| 2403 |
bool invalidateAll) { |
| 2404 |
JitSpew(JitSpew_IonInvalidate, "BEGIN invalidating activation"); |
| 2405 |
|
| 2406 |
#ifdef CHECK_OSIPOINT_REGISTERS |
| 2407 |
if (JitOptions.checkOsiPointRegisters) { |
| 2408 |
activations->asJit()->setCheckRegs(false); |
| 2409 |
} |
| 2410 |
#endif |
| 2411 |
|
| 2412 |
size_t frameno = 1; |
| 2413 |
|
| 2414 |
for (OnlyJSJitFrameIter iter(activations); !iter.done(); ++iter, ++frameno) { |
| 2415 |
const JSJitFrameIter& frame = iter.frame(); |
| 2416 |
MOZ_ASSERT_IF(frameno == 1, |
| 2417 |
frame.isExitFrame() || frame.type() == FrameType::Bailout); |
| 2418 |
|
| 2419 |
#ifdef JS_JITSPEW |
| 2420 |
switch (frame.type()) { |
| 2421 |
case FrameType::Exit: |
| 2422 |
JitSpew(JitSpew_IonInvalidate, "#%zu exit frame @ %p", frameno, |
| 2423 |
frame.fp()); |
| 2424 |
break; |
| 2425 |
case FrameType::BaselineJS: |
| 2426 |
case FrameType::IonJS: |
| 2427 |
case FrameType::Bailout: { |
| 2428 |
MOZ_ASSERT(frame.isScripted()); |
| 2429 |
const char* type = "Unknown"; |
| 2430 |
if (frame.isIonJS()) { |
| 2431 |
type = "Optimized"; |
| 2432 |
} else if (frame.isBaselineJS()) { |
| 2433 |
type = "Baseline"; |
| 2434 |
} else if (frame.isBailoutJS()) { |
| 2435 |
type = "Bailing"; |
| 2436 |
} |
| 2437 |
JSScript* script = frame.maybeForwardedScript(); |
| 2438 |
JitSpew(JitSpew_IonInvalidate, |
| 2439 |
"#%zu %s JS frame @ %p, %s:%u:%u (fun: %p, script: %p, pc %p)", |
| 2440 |
frameno, type, frame.fp(), script->maybeForwardedFilename(), |
| 2441 |
script->lineno(), script->column().oneOriginValue(), |
| 2442 |
frame.maybeCallee(), script, frame.resumePCinCurrentFrame()); |
| 2443 |
break; |
| 2444 |
} |
| 2445 |
case FrameType::BaselineStub: |
| 2446 |
JitSpew(JitSpew_IonInvalidate, "#%zu baseline stub frame @ %p", frameno, |
| 2447 |
frame.fp()); |
| 2448 |
break; |
| 2449 |
case FrameType::BaselineInterpreterEntry: |
| 2450 |
JitSpew(JitSpew_IonInvalidate, |
| 2451 |
"#%zu baseline interpreter entry frame @ %p", frameno, |
| 2452 |
frame.fp()); |
| 2453 |
break; |
| 2454 |
case FrameType::Rectifier: |
| 2455 |
JitSpew(JitSpew_IonInvalidate, "#%zu rectifier frame @ %p", frameno, |
| 2456 |
frame.fp()); |
| 2457 |
break; |
| 2458 |
case FrameType::TrampolineNative: |
| 2459 |
JitSpew(JitSpew_IonInvalidate, "#%zu TrampolineNative frame @ %p", |
| 2460 |
frameno, frame.fp()); |
| 2461 |
break; |
| 2462 |
case FrameType::IonICCall: |
| 2463 |
JitSpew(JitSpew_IonInvalidate, "#%zu ion IC call frame @ %p", frameno, |
| 2464 |
frame.fp()); |
| 2465 |
break; |
| 2466 |
case FrameType::CppToJSJit: |
| 2467 |
JitSpew(JitSpew_IonInvalidate, "#%zu entry frame @ %p", frameno, |
| 2468 |
frame.fp()); |
| 2469 |
break; |
| 2470 |
case FrameType::WasmToJSJit: |
| 2471 |
JitSpew(JitSpew_IonInvalidate, "#%zu wasm frames @ %p", frameno, |
| 2472 |
frame.fp()); |
| 2473 |
break; |
| 2474 |
} |
| 2475 |
#endif // JS_JITSPEW |
| 2476 |
|
| 2477 |
if (!frame.isIonScripted()) { |
| 2478 |
continue; |
| 2479 |
} |
| 2480 |
|
| 2481 |
// See if the frame has already been invalidated. |
| 2482 |
if (frame.checkInvalidation()) { |
| 2483 |
continue; |
| 2484 |
} |
| 2485 |
|
| 2486 |
JSScript* script = frame.maybeForwardedScript(); |
| 2487 |
if (!script->hasIonScript()) { |
| 2488 |
continue; |
| 2489 |
} |
| 2490 |
|
| 2491 |
if (!invalidateAll && !script->ionScript()->invalidated()) { |
| 2492 |
continue; |
| 2493 |
} |
| 2494 |
|
| 2495 |
IonScript* ionScript = script->ionScript(); |
| 2496 |
|
| 2497 |
// Purge ICs before we mark this script as invalidated. This will |
| 2498 |
// prevent lastJump_ from appearing to be a bogus pointer, just |
| 2499 |
// in case anyone tries to read it. |
| 2500 |
ionScript->purgeICs(script->zone()); |
| 2501 |
|
| 2502 |
// This frame needs to be invalidated. We do the following: |
| 2503 |
// |
| 2504 |
// 1. Increment the reference counter to keep the ionScript alive |
| 2505 |
// for the invalidation bailout or for the exception handler. |
| 2506 |
// 2. Determine safepoint that corresponds to the current call. |
| 2507 |
// 3. From safepoint, get distance to the OSI-patchable offset. |
| 2508 |
// 4. From the IonScript, determine the distance between the |
| 2509 |
// call-patchable offset and the invalidation epilogue. |
| 2510 |
// 5. Patch the OSI point with a call-relative to the |
| 2511 |
// invalidation epilogue. |
| 2512 |
// |
| 2513 |
// The code generator ensures that there's enough space for us |
| 2514 |
// to patch in a call-relative operation at each invalidation |
| 2515 |
// point. |
| 2516 |
// |
| 2517 |
// Note: you can't simplify this mechanism to "just patch the |
| 2518 |
// instruction immediately after the call" because things may |
| 2519 |
// need to move into a well-defined register state (using move |
| 2520 |
// instructions after the call) in to capture an appropriate |
| 2521 |
// snapshot after the call occurs. |
| 2522 |
|
| 2523 |
ionScript->incrementInvalidationCount(); |
| 2524 |
|
| 2525 |
JitCode* ionCode = ionScript->method(); |
| 2526 |
|
| 2527 |
// We're about to remove edges from the JSScript to GC things embedded in |
| 2528 |
// the JitCode. Perform a barrier to let the GC know about those edges. |
| 2529 |
PreWriteBarrier(script->zone(), ionCode, [](JSTracer* trc, JitCode* code) { |
| 2530 |
code->traceChildren(trc); |
| 2531 |
}); |
| 2532 |
|
| 2533 |
ionCode->setInvalidated(); |
| 2534 |
|
| 2535 |
// Don't adjust OSI points in a bailout path. |
| 2536 |
if (frame.isBailoutJS()) { |
| 2537 |
continue; |
| 2538 |
} |
| 2539 |
|
| 2540 |
// Write the delta (from the return address offset to the |
| 2541 |
// IonScript pointer embedded into the invalidation epilogue) |
| 2542 |
// where the safepointed call instruction used to be. We rely on |
| 2543 |
// the call sequence causing the safepoint being >= the size of |
| 2544 |
// a uint32, which is checked during safepoint index |
| 2545 |
// construction. |
| 2546 |
AutoWritableJitCode awjc(ionCode); |
| 2547 |
const SafepointIndex* si = |
| 2548 |
ionScript->getSafepointIndex(frame.resumePCinCurrentFrame()); |
| 2549 |
CodeLocationLabel dataLabelToMunge(frame.resumePCinCurrentFrame()); |
| 2550 |
ptrdiff_t delta = ionScript->invalidateEpilogueDataOffset() - |
| 2551 |
(frame.resumePCinCurrentFrame() - ionCode->raw()); |
| 2552 |
Assembler::PatchWrite_Imm32(dataLabelToMunge, Imm32(delta)); |
| 2553 |
|
| 2554 |
CodeLocationLabel osiPatchPoint = |
| 2555 |
SafepointReader::InvalidationPatchPoint(ionScript, si); |
| 2556 |
CodeLocationLabel invalidateEpilogue( |
| 2557 |
ionCode, CodeOffset(ionScript->invalidateEpilogueOffset())); |
| 2558 |
|
| 2559 |
JitSpew( |
| 2560 |
JitSpew_IonInvalidate, |
| 2561 |
" ! Invalidate ionScript %p (inv count %zu) -> patching osipoint %p", |
| 2562 |
ionScript, ionScript->invalidationCount(), (void*)osiPatchPoint.raw()); |
| 2563 |
Assembler::PatchWrite_NearCall(osiPatchPoint, invalidateEpilogue); |
| 2564 |
} |
| 2565 |
|
| 2566 |
JitSpew(JitSpew_IonInvalidate, "END invalidating activation"); |
| 2567 |
} |
| 2568 |
|
| 2569 |
void jit::InvalidateAll(JS::GCContext* gcx, Zone* zone) { |
| 2570 |
// The caller should previously have cancelled off thread compilation. |
| 2571 |
MOZ_ASSERT(!HasOffThreadIonCompile(zone)); |
| 2572 |
if (zone->isAtomsZone()) { |
| 2573 |
return; |
| 2574 |
} |
| 2575 |
JSContext* cx = TlsContext.get(); |
| 2576 |
for (JitActivationIterator iter(cx); !iter.done(); ++iter) { |
| 2577 |
if (iter->compartment()->zone() == zone) { |
| 2578 |
JitSpew(JitSpew_IonInvalidate, "Invalidating all frames for GC"); |
| 2579 |
InvalidateActivation(gcx, iter, true); |
| 2580 |
} |
| 2581 |
} |
| 2582 |
} |
| 2583 |
|
| 2584 |
static void ClearIonScriptAfterInvalidation(JSContext* cx, JSScript* script, |
| 2585 |
IonScript* ionScript, |
| 2586 |
bool resetUses) { |
| 2587 |
// Null out the JitScript's IonScript pointer. The caller is responsible for |
| 2588 |
// destroying the IonScript using the invalidation count mechanism. |
| 2589 |
DebugOnly<IonScript*> clearedIonScript = |
| 2590 |
script->jitScript()->clearIonScript(cx->gcContext(), script); |
| 2591 |
MOZ_ASSERT(clearedIonScript == ionScript); |
| 2592 |
|
| 2593 |
// Wait for the scripts to get warm again before doing another |
| 2594 |
// compile, unless we are recompiling *because* a script got hot |
| 2595 |
// (resetUses is false). |
| 2596 |
if (resetUses) { |
| 2597 |
script->resetWarmUpCounterToDelayIonCompilation(); |
| 2598 |
} |
| 2599 |
} |
| 2600 |
|
| 2601 |
// Remove this script from pending invalidation script caches. |
| 2602 |
// |
| 2603 |
// This is done to avoid an invalidation leaving behind dependencies which |
| 2604 |
// may not actually be real on a recompilation, causing superflous invalidation |
| 2605 |
// of recompiled code. |
| 2606 |
// |
| 2607 |
// Note: This must be kept in sync with the various WeakScriptCaches. |
| 2608 |
static void ClearPendingInvalidationDependencies(JSScript* script) { |
| 2609 |
script->realm()->zone()->fuseDependencies.removeScript(script); |
| 2610 |
script->realm()->realmFuses.fuseDependencies.removeScript(script); |
| 2611 |
} |
| 2612 |
|
| 2613 |
void jit::Invalidate(JSContext* cx, const RecompileInfoVector& invalid, |
| 2614 |
bool resetUses, bool cancelOffThread) { |
| 2615 |
JitSpew(JitSpew_IonInvalidate, "Start invalidation."); |
| 2616 |
|
| 2617 |
// Add an invalidation reference to all invalidated IonScripts to indicate |
| 2618 |
// to the traversal which frames have been invalidated. |
| 2619 |
size_t numInvalidations = 0; |
| 2620 |
for (const RecompileInfo& info : invalid) { |
| 2621 |
if (cancelOffThread) { |
| 2622 |
CancelOffThreadIonCompile(info.script()); |
| 2623 |
} |
| 2624 |
|
| 2625 |
IonScript* ionScript = info.maybeIonScriptToInvalidate(); |
| 2626 |
if (!ionScript) { |
| 2627 |
continue; |
| 2628 |
} |
| 2629 |
|
| 2630 |
JitSpew(JitSpew_IonInvalidate, " Invalidate %s:%u:%u, IonScript %p", |
| 2631 |
info.script()->filename(), info.script()->lineno(), |
| 2632 |
info.script()->column().oneOriginValue(), ionScript); |
| 2633 |
|
| 2634 |
ClearPendingInvalidationDependencies(info.script()); |
| 2635 |
|
| 2636 |
// Keep the ion script alive during the invalidation and flag this |
| 2637 |
// ionScript as being invalidated. This increment is removed by the |
| 2638 |
// loop after the calls to InvalidateActivation. |
| 2639 |
ionScript->incrementInvalidationCount(); |
| 2640 |
numInvalidations++; |
| 2641 |
} |
| 2642 |
|
| 2643 |
if (!numInvalidations) { |
| 2644 |
JitSpew(JitSpew_IonInvalidate, " No IonScript invalidation."); |
| 2645 |
return; |
| 2646 |
} |
| 2647 |
|
| 2648 |
JS::GCContext* gcx = cx->gcContext(); |
| 2649 |
for (JitActivationIterator iter(cx); !iter.done(); ++iter) { |
| 2650 |
InvalidateActivation(gcx, iter, false); |
| 2651 |
} |
| 2652 |
|
| 2653 |
// Drop the references added above. If a script was never active, its |
| 2654 |
// IonScript will be immediately destroyed. Otherwise, it will be held live |
| 2655 |
// until its last invalidated frame is destroyed. |
| 2656 |
for (const RecompileInfo& info : invalid) { |
| 2657 |
IonScript* ionScript = info.maybeIonScriptToInvalidate(); |
| 2658 |
if (!ionScript) { |
| 2659 |
continue; |
| 2660 |
} |
| 2661 |
|
| 2662 |
if (ionScript->invalidationCount() == 1) { |
| 2663 |
// decrementInvalidationCount will destroy the IonScript so null out |
| 2664 |
// jitScript->ionScript_ now. We don't want to do this unconditionally |
| 2665 |
// because maybeIonScriptToInvalidate depends on script->ionScript() (we |
| 2666 |
// would leak the IonScript if |invalid| contains duplicates). |
| 2667 |
ClearIonScriptAfterInvalidation(cx, info.script(), ionScript, resetUses); |
| 2668 |
} |
| 2669 |
|
| 2670 |
ionScript->decrementInvalidationCount(gcx); |
| 2671 |
numInvalidations--; |
| 2672 |
} |
| 2673 |
|
| 2674 |
// Make sure we didn't leak references by invalidating the same IonScript |
| 2675 |
// multiple times in the above loop. |
| 2676 |
MOZ_ASSERT(!numInvalidations); |
| 2677 |
|
| 2678 |
// Finally, null out jitScript->ionScript_ for IonScripts that are still on |
| 2679 |
// the stack. |
| 2680 |
for (const RecompileInfo& info : invalid) { |
| 2681 |
if (IonScript* ionScript = info.maybeIonScriptToInvalidate()) { |
| 2682 |
ClearIonScriptAfterInvalidation(cx, info.script(), ionScript, resetUses); |
| 2683 |
} |
| 2684 |
} |
| 2685 |
} |
| 2686 |
|
| 2687 |
void jit::IonScript::invalidate(JSContext* cx, JSScript* script, bool resetUses, |
| 2688 |
const char* reason) { |
| 2689 |
// Note: we could short circuit here if we already invalidated this |
| 2690 |
// IonScript, but jit::Invalidate also cancels off-thread compilations of |
| 2691 |
// |script|. |
| 2692 |
MOZ_RELEASE_ASSERT(invalidated() || script->ionScript() == this); |
| 2693 |
|
| 2694 |
JitSpew(JitSpew_IonInvalidate, " Invalidate IonScript %p: %s", this, reason); |
| 2695 |
|
| 2696 |
// RecompileInfoVector has inline space for at least one element. |
| 2697 |
RecompileInfoVector list; |
| 2698 |
MOZ_RELEASE_ASSERT(list.reserve(1)); |
| 2699 |
list.infallibleEmplaceBack(script, compilationId()); |
| 2700 |
|
| 2701 |
Invalidate(cx, list, resetUses, true); |
| 2702 |
} |
| 2703 |
|
| 2704 |
void jit::Invalidate(JSContext* cx, JSScript* script, bool resetUses, |
| 2705 |
bool cancelOffThread) { |
| 2706 |
MOZ_ASSERT(script->hasIonScript()); |
| 2707 |
|
| 2708 |
if (cx->runtime()->geckoProfiler().enabled()) { |
| 2709 |
// Register invalidation with profiler. |
| 2710 |
// Format of event payload string: |
| 2711 |
// "<filename>:<lineno>" |
| 2712 |
|
| 2713 |
// Get the script filename, if any, and its length. |
| 2714 |
const char* filename = script->filename(); |
| 2715 |
if (filename == nullptr) { |
| 2716 |
filename = "<unknown>"; |
| 2717 |
} |
| 2718 |
|
| 2719 |
// Construct the descriptive string. |
| 2720 |
UniqueChars buf = JS_smprintf("%s:%u:%u", filename, script->lineno(), |
| 2721 |
script->column().oneOriginValue()); |
| 2722 |
|
| 2723 |
// Ignore the event on allocation failure. |
| 2724 |
if (buf) { |
| 2725 |
cx->runtime()->geckoProfiler().markEvent("Invalidate", buf.get()); |
| 2726 |
} |
| 2727 |
} |
| 2728 |
|
| 2729 |
// RecompileInfoVector has inline space for at least one element. |
| 2730 |
RecompileInfoVector scripts; |
| 2731 |
MOZ_ASSERT(script->hasIonScript()); |
| 2732 |
MOZ_RELEASE_ASSERT(scripts.reserve(1)); |
| 2733 |
scripts.infallibleEmplaceBack(script, script->ionScript()->compilationId()); |
| 2734 |
|
| 2735 |
Invalidate(cx, scripts, resetUses, cancelOffThread); |
| 2736 |
} |
| 2737 |
|
| 2738 |
void jit::FinishInvalidation(JS::GCContext* gcx, JSScript* script) { |
| 2739 |
if (!script->hasIonScript()) { |
| 2740 |
return; |
| 2741 |
} |
| 2742 |
|
| 2743 |
// In all cases, null out jitScript->ionScript_ to avoid re-entry. |
| 2744 |
IonScript* ion = script->jitScript()->clearIonScript(gcx, script); |
| 2745 |
|
| 2746 |
// If this script has Ion code on the stack, invalidated() will return |
| 2747 |
// true. In this case we have to wait until destroying it. |
| 2748 |
if (!ion->invalidated()) { |
| 2749 |
jit::IonScript::Destroy(gcx, ion); |
| 2750 |
} |
| 2751 |
} |
| 2752 |
|
| 2753 |
void jit::ForbidCompilation(JSContext* cx, JSScript* script) { |
| 2754 |
JitSpew(JitSpew_IonAbort, "Disabling Ion compilation of script %s:%u:%u", |
| 2755 |
script->filename(), script->lineno(), |
| 2756 |
script->column().oneOriginValue()); |
| 2757 |
|
| 2758 |
CancelOffThreadIonCompile(script); |
| 2759 |
|
| 2760 |
if (script->hasIonScript()) { |
| 2761 |
Invalidate(cx, script, false); |
| 2762 |
} |
| 2763 |
|
| 2764 |
script->disableIon(); |
| 2765 |
} |
| 2766 |
|
| 2767 |
size_t jit::SizeOfIonData(JSScript* script, |
| 2768 |
mozilla::MallocSizeOf mallocSizeOf) { |
| 2769 |
size_t result = 0; |
| 2770 |
|
| 2771 |
if (script->hasIonScript()) { |
| 2772 |
result += script->ionScript()->sizeOfIncludingThis(mallocSizeOf); |
| 2773 |
} |
| 2774 |
|
| 2775 |
return result; |
| 2776 |
} |
| 2777 |
|
| 2778 |
// If you change these, please also change the comment in TempAllocator. |
| 2779 |
/* static */ const size_t TempAllocator::BallastSize = 16 * 1024; |
| 2780 |
/* static */ const size_t TempAllocator::PreferredLifoChunkSize = 32 * 1024; |