| 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/WarpSnapshot.h" |
| 8 |
|
| 9 |
#include "mozilla/DebugOnly.h" |
| 10 |
|
| 11 |
#include <type_traits> |
| 12 |
|
| 13 |
#include "jit/CacheIRCompiler.h" |
| 14 |
#include "jit/CacheIRSpewer.h" |
| 15 |
#include "js/Printer.h" |
| 16 |
#include "vm/EnvironmentObject.h" |
| 17 |
#include "vm/GetterSetter.h" |
| 18 |
#include "vm/GlobalObject.h" |
| 19 |
#include "vm/JSContext.h" |
| 20 |
|
| 21 |
using namespace js; |
| 22 |
using namespace js::jit; |
| 23 |
|
| 24 |
static_assert(!std::is_polymorphic_v<WarpOpSnapshot>, |
| 25 |
"WarpOpSnapshot should not have any virtual methods"); |
| 26 |
|
| 27 |
WarpSnapshot::WarpSnapshot(JSContext* cx, TempAllocator& alloc, |
| 28 |
WarpScriptSnapshotList&& scriptSnapshots, |
| 29 |
const WarpZoneStubsSnapshot& zoneStubs, |
| 30 |
const WarpBailoutInfo& bailoutInfo, |
| 31 |
bool needsFinalWarmUpCount) |
| 32 |
: scriptSnapshots_(std::move(scriptSnapshots)), |
| 33 |
zoneStubs_(zoneStubs), |
| 34 |
globalLexicalEnv_(&cx->global()->lexicalEnvironment()), |
| 35 |
globalLexicalEnvThis_(globalLexicalEnv_->thisObject()), |
| 36 |
bailoutInfo_(bailoutInfo), |
| 37 |
nurseryObjects_(alloc) { |
| 38 |
#ifdef JS_CACHEIR_SPEW |
| 39 |
needsFinalWarmUpCount_ = needsFinalWarmUpCount; |
| 40 |
#endif |
| 41 |
} |
| 42 |
|
| 43 |
WarpScriptSnapshot::WarpScriptSnapshot(JSScript* script, |
| 44 |
const WarpEnvironment& env, |
| 45 |
WarpOpSnapshotList&& opSnapshots, |
| 46 |
ModuleObject* moduleObject) |
| 47 |
: script_(script), |
| 48 |
environment_(env), |
| 49 |
opSnapshots_(std::move(opSnapshots)), |
| 50 |
moduleObject_(moduleObject), |
| 51 |
isArrowFunction_(script->isFunction() && script->function()->isArrow()) {} |
| 52 |
|
| 53 |
#ifdef JS_JITSPEW |
| 54 |
void WarpSnapshot::dump() const { |
| 55 |
Fprinter out(stderr); |
| 56 |
dump(out); |
| 57 |
} |
| 58 |
|
| 59 |
void WarpSnapshot::dump(GenericPrinter& out) const { |
| 60 |
out.printf("WarpSnapshot (0x%p)\n", this); |
| 61 |
out.printf("------------------------------\n"); |
| 62 |
out.printf("globalLexicalEnv: 0x%p\n", globalLexicalEnv()); |
| 63 |
out.printf("globalLexicalEnvThis: 0x%p\n", globalLexicalEnvThis()); |
| 64 |
out.printf("failedBoundsCheck: %u\n", bailoutInfo().failedBoundsCheck()); |
| 65 |
out.printf("failedLexicalCheck: %u\n", bailoutInfo().failedLexicalCheck()); |
| 66 |
out.printf("\n"); |
| 67 |
|
| 68 |
out.printf("JitZone stubs:\n"); |
| 69 |
for (const auto& stub : zoneStubs_) { |
| 70 |
unsigned index = &stub - zoneStubs_.begin(); |
| 71 |
out.printf("Stub %u: 0x%p\n", index, stub); |
| 72 |
} |
| 73 |
out.printf("\n"); |
| 74 |
|
| 75 |
out.printf("Nursery objects (%u):\n", unsigned(nurseryObjects_.length())); |
| 76 |
for (size_t i = 0; i < nurseryObjects_.length(); i++) { |
| 77 |
out.printf(" %u: 0x%p\n", unsigned(i), nurseryObjects_[i]); |
| 78 |
} |
| 79 |
out.printf("\n"); |
| 80 |
|
| 81 |
for (auto* scriptSnapshot : scriptSnapshots_) { |
| 82 |
scriptSnapshot->dump(out); |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
void WarpScriptSnapshot::dump(GenericPrinter& out) const { |
| 87 |
out.printf("WarpScriptSnapshot (0x%p)\n", this); |
| 88 |
out.printf("------------------------------\n"); |
| 89 |
out.printf("Script: %s:%u:%u (0x%p)\n", script_->filename(), |
| 90 |
script_->lineno(), script_->column().oneOriginValue(), |
| 91 |
static_cast<JSScript*>(script_)); |
| 92 |
out.printf(" moduleObject: 0x%p\n", moduleObject()); |
| 93 |
out.printf(" isArrowFunction: %u\n", isArrowFunction()); |
| 94 |
|
| 95 |
out.printf(" environment: "); |
| 96 |
environment_.match( |
| 97 |
[&](const NoEnvironment&) { out.printf("None\n"); }, |
| 98 |
[&](JSObject* obj) { out.printf("Object: 0x%p\n", obj); }, |
| 99 |
[&](const FunctionEnvironment& env) { |
| 100 |
out.printf( |
| 101 |
"Function: callobject template 0x%p, named lambda template: 0x%p," |
| 102 |
" initial heap %u\n", |
| 103 |
static_cast<JSObject*>(env.callObjectTemplate), |
| 104 |
static_cast<JSObject*>(env.namedLambdaTemplate), |
| 105 |
unsigned(env.initialHeap)); |
| 106 |
}); |
| 107 |
|
| 108 |
out.printf("\n"); |
| 109 |
for (const WarpOpSnapshot* snapshot : opSnapshots()) { |
| 110 |
snapshot->dump(out, script_); |
| 111 |
out.printf("\n"); |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
static const char* OpSnapshotKindString(WarpOpSnapshot::Kind kind) { |
| 116 |
static const char* const names[] = { |
| 117 |
# define NAME(x) #x, |
| 118 |
WARP_OP_SNAPSHOT_LIST(NAME) |
| 119 |
# undef NAME |
| 120 |
}; |
| 121 |
return names[unsigned(kind)]; |
| 122 |
} |
| 123 |
|
| 124 |
void WarpOpSnapshot::dump(GenericPrinter& out, JSScript* script) const { |
| 125 |
jsbytecode* pc = script->offsetToPC(offset_); |
| 126 |
out.printf(" %s (offset %u, JSOp::%s)\n", OpSnapshotKindString(kind_), |
| 127 |
offset_, CodeName(JSOp(*pc))); |
| 128 |
|
| 129 |
// Dispatch to dumpData() methods. |
| 130 |
switch (kind_) { |
| 131 |
# define DUMP(kind) \ |
| 132 |
case Kind::kind: \ |
| 133 |
as<kind>()->dumpData(out); \ |
| 134 |
break; |
| 135 |
WARP_OP_SNAPSHOT_LIST(DUMP) |
| 136 |
# undef DUMP |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
void WarpArguments::dumpData(GenericPrinter& out) const { |
| 141 |
out.printf(" template: 0x%p\n", templateObj()); |
| 142 |
} |
| 143 |
|
| 144 |
void WarpRegExp::dumpData(GenericPrinter& out) const { |
| 145 |
out.printf(" hasShared: %u\n", hasShared()); |
| 146 |
} |
| 147 |
|
| 148 |
void WarpBuiltinObject::dumpData(GenericPrinter& out) const { |
| 149 |
out.printf(" builtin: 0x%p\n", builtin()); |
| 150 |
} |
| 151 |
|
| 152 |
void WarpGetIntrinsic::dumpData(GenericPrinter& out) const { |
| 153 |
out.printf(" intrinsic: 0x%016" PRIx64 "\n", intrinsic().asRawBits()); |
| 154 |
} |
| 155 |
|
| 156 |
void WarpGetImport::dumpData(GenericPrinter& out) const { |
| 157 |
out.printf(" targetEnv: 0x%p\n", targetEnv()); |
| 158 |
out.printf(" numFixedSlots: %u\n", numFixedSlots()); |
| 159 |
out.printf(" slot: %u\n", slot()); |
| 160 |
out.printf(" needsLexicalCheck: %u\n", needsLexicalCheck()); |
| 161 |
} |
| 162 |
|
| 163 |
void WarpRest::dumpData(GenericPrinter& out) const { |
| 164 |
out.printf(" shape: 0x%p\n", shape()); |
| 165 |
} |
| 166 |
|
| 167 |
void WarpBindUnqualifiedGName::dumpData(GenericPrinter& out) const { |
| 168 |
out.printf(" globalEnv: 0x%p\n", globalEnv()); |
| 169 |
} |
| 170 |
|
| 171 |
void WarpVarEnvironment::dumpData(GenericPrinter& out) const { |
| 172 |
out.printf(" template: 0x%p\n", templateObj()); |
| 173 |
} |
| 174 |
|
| 175 |
void WarpLexicalEnvironment::dumpData(GenericPrinter& out) const { |
| 176 |
out.printf(" template: 0x%p\n", templateObj()); |
| 177 |
} |
| 178 |
|
| 179 |
void WarpClassBodyEnvironment::dumpData(GenericPrinter& out) const { |
| 180 |
out.printf(" template: 0x%p\n", templateObj()); |
| 181 |
} |
| 182 |
|
| 183 |
void WarpBailout::dumpData(GenericPrinter& out) const { |
| 184 |
// No fields. |
| 185 |
} |
| 186 |
|
| 187 |
void WarpCacheIR::dumpData(GenericPrinter& out) const { |
| 188 |
out.printf(" stubCode: 0x%p\n", static_cast<JitCode*>(stubCode_)); |
| 189 |
out.printf(" stubInfo: 0x%p\n", stubInfo_); |
| 190 |
out.printf(" stubData: 0x%p\n", stubData_); |
| 191 |
# ifdef JS_CACHEIR_SPEW |
| 192 |
out.printf(" IR:\n"); |
| 193 |
SpewCacheIROps(out, " ", stubInfo_); |
| 194 |
# else |
| 195 |
out.printf("(CacheIR spew unavailable)\n"); |
| 196 |
# endif |
| 197 |
} |
| 198 |
|
| 199 |
void WarpInlinedCall::dumpData(GenericPrinter& out) const { |
| 200 |
out.printf(" scriptSnapshot: 0x%p\n", scriptSnapshot_); |
| 201 |
out.printf(" info: 0x%p\n", info_); |
| 202 |
cacheIRSnapshot_->dumpData(out); |
| 203 |
} |
| 204 |
|
| 205 |
void WarpPolymorphicTypes::dumpData(GenericPrinter& out) const { |
| 206 |
out.printf(" types:\n"); |
| 207 |
for (auto& typeData : list_) { |
| 208 |
out.printf(" %s\n", ValTypeToString(typeData.type())); |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
#endif // JS_JITSPEW |
| 213 |
|
| 214 |
void WarpSnapshot::trace(JSTracer* trc) { |
| 215 |
// Nursery objects can be tenured in parallel with Warp compilation. |
| 216 |
// Note: don't use TraceOffthreadGCPtr here as that asserts non-moving. |
| 217 |
for (size_t i = 0; i < nurseryObjects_.length(); i++) { |
| 218 |
TraceManuallyBarrieredEdge(trc, &nurseryObjects_[i], "warp-nursery-object"); |
| 219 |
} |
| 220 |
|
| 221 |
// Other GC things are not in the nursery. |
| 222 |
if (trc->runtime()->heapState() == JS::HeapState::MinorCollecting) { |
| 223 |
return; |
| 224 |
} |
| 225 |
|
| 226 |
for (auto* script : scriptSnapshots_) { |
| 227 |
script->trace(trc); |
| 228 |
} |
| 229 |
for (JitCode* stub : zoneStubs_) { |
| 230 |
if (stub) { |
| 231 |
OffthreadGCPtr<JitCode*> ptr(stub); |
| 232 |
TraceOffthreadGCPtr(trc, ptr, "warp-zone-stub"); |
| 233 |
} |
| 234 |
} |
| 235 |
TraceOffthreadGCPtr(trc, globalLexicalEnv_, "warp-lexical"); |
| 236 |
TraceOffthreadGCPtr(trc, globalLexicalEnvThis_, "warp-lexicalthis"); |
| 237 |
} |
| 238 |
|
| 239 |
void WarpScriptSnapshot::trace(JSTracer* trc) { |
| 240 |
TraceOffthreadGCPtr(trc, script_, "warp-script"); |
| 241 |
|
| 242 |
environment_.match([](const NoEnvironment&) {}, |
| 243 |
[trc](OffthreadGCPtr<JSObject*>& obj) { |
| 244 |
TraceOffthreadGCPtr(trc, obj, "warp-env-object"); |
| 245 |
}, |
| 246 |
[trc](FunctionEnvironment& env) { |
| 247 |
if (env.callObjectTemplate) { |
| 248 |
TraceOffthreadGCPtr(trc, env.callObjectTemplate, |
| 249 |
"warp-env-callobject"); |
| 250 |
} |
| 251 |
if (env.namedLambdaTemplate) { |
| 252 |
TraceOffthreadGCPtr(trc, env.namedLambdaTemplate, |
| 253 |
"warp-env-namedlambda"); |
| 254 |
} |
| 255 |
}); |
| 256 |
|
| 257 |
for (WarpOpSnapshot* snapshot : opSnapshots_) { |
| 258 |
snapshot->trace(trc); |
| 259 |
} |
| 260 |
|
| 261 |
if (moduleObject_) { |
| 262 |
TraceOffthreadGCPtr(trc, moduleObject_, "warp-module-obj"); |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
void WarpOpSnapshot::trace(JSTracer* trc) { |
| 267 |
// Dispatch to traceData() methods. |
| 268 |
switch (kind_) { |
| 269 |
#define TRACE(kind) \ |
| 270 |
case Kind::kind: \ |
| 271 |
as<kind>()->traceData(trc); \ |
| 272 |
break; |
| 273 |
WARP_OP_SNAPSHOT_LIST(TRACE) |
| 274 |
#undef TRACE |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
void WarpArguments::traceData(JSTracer* trc) { |
| 279 |
if (templateObj_) { |
| 280 |
TraceOffthreadGCPtr(trc, templateObj_, "warp-args-template"); |
| 281 |
} |
| 282 |
} |
| 283 |
|
| 284 |
void WarpRegExp::traceData(JSTracer* trc) { |
| 285 |
// No GC pointers. |
| 286 |
} |
| 287 |
|
| 288 |
void WarpBuiltinObject::traceData(JSTracer* trc) { |
| 289 |
TraceOffthreadGCPtr(trc, builtin_, "warp-builtin-object"); |
| 290 |
} |
| 291 |
|
| 292 |
void WarpGetIntrinsic::traceData(JSTracer* trc) { |
| 293 |
TraceOffthreadGCPtr(trc, intrinsic_, "warp-intrinsic"); |
| 294 |
} |
| 295 |
|
| 296 |
void WarpGetImport::traceData(JSTracer* trc) { |
| 297 |
TraceOffthreadGCPtr(trc, targetEnv_, "warp-import-env"); |
| 298 |
} |
| 299 |
|
| 300 |
void WarpRest::traceData(JSTracer* trc) { |
| 301 |
TraceOffthreadGCPtr(trc, shape_, "warp-rest-shape"); |
| 302 |
} |
| 303 |
|
| 304 |
void WarpBindUnqualifiedGName::traceData(JSTracer* trc) { |
| 305 |
TraceOffthreadGCPtr(trc, globalEnv_, "warp-bindunqualifiedgname-globalenv"); |
| 306 |
} |
| 307 |
|
| 308 |
void WarpVarEnvironment::traceData(JSTracer* trc) { |
| 309 |
TraceOffthreadGCPtr(trc, templateObj_, "warp-varenv-template"); |
| 310 |
} |
| 311 |
|
| 312 |
void WarpLexicalEnvironment::traceData(JSTracer* trc) { |
| 313 |
TraceOffthreadGCPtr(trc, templateObj_, "warp-lexenv-template"); |
| 314 |
} |
| 315 |
|
| 316 |
void WarpClassBodyEnvironment::traceData(JSTracer* trc) { |
| 317 |
TraceOffthreadGCPtr(trc, templateObj_, "warp-classbodyenv-template"); |
| 318 |
} |
| 319 |
|
| 320 |
void WarpBailout::traceData(JSTracer* trc) { |
| 321 |
// No GC pointers. |
| 322 |
} |
| 323 |
|
| 324 |
void WarpPolymorphicTypes::traceData(JSTracer* trc) { |
| 325 |
// No GC pointers. |
| 326 |
} |
| 327 |
|
| 328 |
template <typename T> |
| 329 |
static void TraceWarpStubPtr(JSTracer* trc, uintptr_t word, const char* name) { |
| 330 |
T* ptr = reinterpret_cast<T*>(word); |
| 331 |
TraceOffthreadGCPtr(trc, OffthreadGCPtr<T*>(ptr), name); |
| 332 |
} |
| 333 |
|
| 334 |
void WarpCacheIR::traceData(JSTracer* trc) { |
| 335 |
TraceOffthreadGCPtr(trc, stubCode_, "warp-stub-code"); |
| 336 |
if (stubData_) { |
| 337 |
uint32_t field = 0; |
| 338 |
size_t offset = 0; |
| 339 |
while (true) { |
| 340 |
StubField::Type fieldType = stubInfo_->fieldType(field); |
| 341 |
switch (fieldType) { |
| 342 |
case StubField::Type::RawInt32: |
| 343 |
case StubField::Type::RawPointer: |
| 344 |
case StubField::Type::ICScript: |
| 345 |
case StubField::Type::RawInt64: |
| 346 |
case StubField::Type::Double: |
| 347 |
break; |
| 348 |
case StubField::Type::Shape: |
| 349 |
case StubField::Type::WeakShape: { |
| 350 |
// WeakShape pointers are traced strongly in this context. |
| 351 |
uintptr_t word = stubInfo_->getStubRawWord(stubData_, offset); |
| 352 |
TraceWarpStubPtr<Shape>(trc, word, "warp-cacheir-shape"); |
| 353 |
break; |
| 354 |
} |
| 355 |
case StubField::Type::WeakGetterSetter: { |
| 356 |
// WeakGetterSetter pointers are traced strongly in this context. |
| 357 |
uintptr_t word = stubInfo_->getStubRawWord(stubData_, offset); |
| 358 |
TraceWarpStubPtr<GetterSetter>(trc, word, |
| 359 |
"warp-cacheir-getter-setter"); |
| 360 |
break; |
| 361 |
} |
| 362 |
case StubField::Type::JSObject: |
| 363 |
case StubField::Type::WeakObject: { |
| 364 |
// WeakObject pointers are traced strongly in this context. |
| 365 |
uintptr_t word = stubInfo_->getStubRawWord(stubData_, offset); |
| 366 |
WarpObjectField field = WarpObjectField::fromData(word); |
| 367 |
if (!field.isNurseryIndex()) { |
| 368 |
TraceWarpStubPtr<JSObject>(trc, word, "warp-cacheir-object"); |
| 369 |
} |
| 370 |
break; |
| 371 |
} |
| 372 |
case StubField::Type::Symbol: { |
| 373 |
uintptr_t word = stubInfo_->getStubRawWord(stubData_, offset); |
| 374 |
TraceWarpStubPtr<JS::Symbol>(trc, word, "warp-cacheir-symbol"); |
| 375 |
break; |
| 376 |
} |
| 377 |
case StubField::Type::String: { |
| 378 |
uintptr_t word = stubInfo_->getStubRawWord(stubData_, offset); |
| 379 |
TraceWarpStubPtr<JSString>(trc, word, "warp-cacheir-string"); |
| 380 |
break; |
| 381 |
} |
| 382 |
case StubField::Type::WeakBaseScript: { |
| 383 |
// WeakBaseScript pointers are traced strongly in this context. |
| 384 |
uintptr_t word = stubInfo_->getStubRawWord(stubData_, offset); |
| 385 |
TraceWarpStubPtr<BaseScript>(trc, word, "warp-cacheir-script"); |
| 386 |
break; |
| 387 |
} |
| 388 |
case StubField::Type::JitCode: { |
| 389 |
uintptr_t word = stubInfo_->getStubRawWord(stubData_, offset); |
| 390 |
TraceWarpStubPtr<JitCode>(trc, word, "warp-cacheir-jitcode"); |
| 391 |
break; |
| 392 |
} |
| 393 |
case StubField::Type::Id: { |
| 394 |
uintptr_t word = stubInfo_->getStubRawWord(stubData_, offset); |
| 395 |
jsid id = jsid::fromRawBits(word); |
| 396 |
TraceOffthreadGCPtr(trc, OffthreadGCPtr<jsid>(id), |
| 397 |
"warp-cacheir-jsid"); |
| 398 |
break; |
| 399 |
} |
| 400 |
case StubField::Type::Value: { |
| 401 |
uint64_t data = stubInfo_->getStubRawInt64(stubData_, offset); |
| 402 |
Value val = Value::fromRawBits(data); |
| 403 |
TraceOffthreadGCPtr(trc, OffthreadGCPtr<Value>(val), |
| 404 |
"warp-cacheir-value"); |
| 405 |
break; |
| 406 |
} |
| 407 |
case StubField::Type::AllocSite: { |
| 408 |
mozilla::DebugOnly<uintptr_t> word = |
| 409 |
stubInfo_->getStubRawWord(stubData_, offset); |
| 410 |
MOZ_ASSERT(word == uintptr_t(gc::Heap::Default) || |
| 411 |
word == uintptr_t(gc::Heap::Tenured)); |
| 412 |
break; |
| 413 |
} |
| 414 |
case StubField::Type::Limit: |
| 415 |
return; // Done. |
| 416 |
} |
| 417 |
field++; |
| 418 |
offset += StubField::sizeInBytes(fieldType); |
| 419 |
} |
| 420 |
} |
| 421 |
} |
| 422 |
|
| 423 |
void WarpInlinedCall::traceData(JSTracer* trc) { |
| 424 |
// Note: scriptSnapshot_ is traced through WarpSnapshot. |
| 425 |
cacheIRSnapshot_->trace(trc); |
| 426 |
} |