| 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/WarpOracle.h" |
| 8 |
|
| 9 |
#include "mozilla/ScopeExit.h" |
| 10 |
#include "mozilla/Try.h" |
| 11 |
|
| 12 |
#include <algorithm> |
| 13 |
|
| 14 |
#include "jit/CacheIR.h" |
| 15 |
#include "jit/CacheIRCompiler.h" |
| 16 |
#include "jit/CacheIRReader.h" |
| 17 |
#include "jit/CompileInfo.h" |
| 18 |
#include "jit/InlineScriptTree.h" |
| 19 |
#include "jit/JitHints.h" |
| 20 |
#include "jit/JitRuntime.h" |
| 21 |
#include "jit/JitScript.h" |
| 22 |
#include "jit/JitSpewer.h" |
| 23 |
#include "jit/JitZone.h" |
| 24 |
#include "jit/MIRGenerator.h" |
| 25 |
#include "jit/TrialInlining.h" |
| 26 |
#include "jit/TypeData.h" |
| 27 |
#include "jit/WarpBuilder.h" |
| 28 |
#include "js/ColumnNumber.h" // JS::LimitedColumnNumberOneOrigin |
| 29 |
#include "util/DifferentialTesting.h" |
| 30 |
#include "vm/BuiltinObjectKind.h" |
| 31 |
#include "vm/BytecodeIterator.h" |
| 32 |
#include "vm/BytecodeLocation.h" |
| 33 |
|
| 34 |
#include "jit/InlineScriptTree-inl.h" |
| 35 |
#include "vm/BytecodeIterator-inl.h" |
| 36 |
#include "vm/BytecodeLocation-inl.h" |
| 37 |
#include "vm/EnvironmentObject-inl.h" |
| 38 |
#include "vm/Interpreter-inl.h" |
| 39 |
|
| 40 |
using namespace js; |
| 41 |
using namespace js::jit; |
| 42 |
|
| 43 |
using mozilla::Maybe; |
| 44 |
|
| 45 |
// WarpScriptOracle creates a WarpScriptSnapshot for a single JSScript. Note |
| 46 |
// that a single WarpOracle can use multiple WarpScriptOracles when scripts are |
| 47 |
// inlined. |
| 48 |
class MOZ_STACK_CLASS WarpScriptOracle { |
| 49 |
JSContext* cx_; |
| 50 |
WarpOracle* oracle_; |
| 51 |
MIRGenerator& mirGen_; |
| 52 |
TempAllocator& alloc_; |
| 53 |
HandleScript script_; |
| 54 |
const CompileInfo* info_; |
| 55 |
ICScript* icScript_; |
| 56 |
|
| 57 |
// Index of the next ICEntry for getICEntry. This assumes the script's |
| 58 |
// bytecode is processed from first to last instruction. |
| 59 |
uint32_t icEntryIndex_ = 0; |
| 60 |
|
| 61 |
template <typename... Args> |
| 62 |
mozilla::GenericErrorResult<AbortReason> abort(Args&&... args) { |
| 63 |
return oracle_->abort(script_, args...); |
| 64 |
} |
| 65 |
|
| 66 |
WarpEnvironment createEnvironment(); |
| 67 |
AbortReasonOr<Ok> maybeInlineIC(WarpOpSnapshotList& snapshots, |
| 68 |
BytecodeLocation loc); |
| 69 |
AbortReasonOr<bool> maybeInlineCall(WarpOpSnapshotList& snapshots, |
| 70 |
BytecodeLocation loc, ICCacheIRStub* stub, |
| 71 |
ICFallbackStub* fallbackStub, |
| 72 |
uint8_t* stubDataCopy); |
| 73 |
AbortReasonOr<bool> maybeInlinePolymorphicTypes(WarpOpSnapshotList& snapshots, |
| 74 |
BytecodeLocation loc, |
| 75 |
ICCacheIRStub* firstStub, |
| 76 |
ICFallbackStub* fallbackStub); |
| 77 |
[[nodiscard]] bool replaceNurseryAndAllocSitePointers( |
| 78 |
ICCacheIRStub* stub, const CacheIRStubInfo* stubInfo, |
| 79 |
uint8_t* stubDataCopy); |
| 80 |
bool maybeReplaceNurseryPointer(const CacheIRStubInfo* stubInfo, |
| 81 |
uint8_t* stubDataCopy, JSObject* obj, |
| 82 |
size_t offset); |
| 83 |
|
| 84 |
public: |
| 85 |
WarpScriptOracle(JSContext* cx, WarpOracle* oracle, HandleScript script, |
| 86 |
const CompileInfo* info, ICScript* icScript) |
| 87 |
: cx_(cx), |
| 88 |
oracle_(oracle), |
| 89 |
mirGen_(oracle->mirGen()), |
| 90 |
alloc_(mirGen_.alloc()), |
| 91 |
script_(script), |
| 92 |
info_(info), |
| 93 |
icScript_(icScript) {} |
| 94 |
|
| 95 |
AbortReasonOr<WarpScriptSnapshot*> createScriptSnapshot(); |
| 96 |
|
| 97 |
ICEntry& getICEntryAndFallback(BytecodeLocation loc, |
| 98 |
ICFallbackStub** fallback); |
| 99 |
}; |
| 100 |
|
| 101 |
WarpOracle::WarpOracle(JSContext* cx, MIRGenerator& mirGen, |
| 102 |
HandleScript outerScript) |
| 103 |
: cx_(cx), |
| 104 |
mirGen_(mirGen), |
| 105 |
alloc_(mirGen.alloc()), |
| 106 |
outerScript_(outerScript) {} |
| 107 |
|
| 108 |
mozilla::GenericErrorResult<AbortReason> WarpOracle::abort(HandleScript script, |
| 109 |
AbortReason r) { |
| 110 |
auto res = mirGen_.abort(r); |
| 111 |
JitSpew(JitSpew_IonAbort, "aborted @ %s", script->filename()); |
| 112 |
return res; |
| 113 |
} |
| 114 |
|
| 115 |
mozilla::GenericErrorResult<AbortReason> WarpOracle::abort(HandleScript script, |
| 116 |
AbortReason r, |
| 117 |
const char* message, |
| 118 |
...) { |
| 119 |
va_list ap; |
| 120 |
va_start(ap, message); |
| 121 |
auto res = mirGen_.abortFmt(r, message, ap); |
| 122 |
va_end(ap); |
| 123 |
JitSpew(JitSpew_IonAbort, "aborted @ %s", script->filename()); |
| 124 |
return res; |
| 125 |
} |
| 126 |
|
| 127 |
void WarpOracle::addScriptSnapshot(WarpScriptSnapshot* scriptSnapshot, |
| 128 |
ICScript* icScript, size_t bytecodeLength) { |
| 129 |
scriptSnapshots_.insertBack(scriptSnapshot); |
| 130 |
accumulatedBytecodeSize_ += bytecodeLength; |
| 131 |
#ifdef DEBUG |
| 132 |
runningScriptHash_ = mozilla::AddToHash(runningScriptHash_, icScript->hash()); |
| 133 |
#endif |
| 134 |
} |
| 135 |
|
| 136 |
AbortReasonOr<WarpSnapshot*> WarpOracle::createSnapshot() { |
| 137 |
#ifdef JS_JITSPEW |
| 138 |
const char* mode; |
| 139 |
if (outerScript_->hasIonScript()) { |
| 140 |
mode = "Recompiling"; |
| 141 |
} else { |
| 142 |
mode = "Compiling"; |
| 143 |
} |
| 144 |
JitSpew(JitSpew_IonScripts, |
| 145 |
"Warp %s script %s:%u:%u (%p) (warmup-counter=%" PRIu32 ",%s%s)", |
| 146 |
mode, outerScript_->filename(), outerScript_->lineno(), |
| 147 |
outerScript_->column().oneOriginValue(), |
| 148 |
static_cast<JSScript*>(outerScript_), outerScript_->getWarmUpCount(), |
| 149 |
outerScript_->isGenerator() ? " isGenerator" : "", |
| 150 |
outerScript_->isAsync() ? " isAsync" : ""); |
| 151 |
#endif |
| 152 |
|
| 153 |
accumulatedBytecodeSize_ = outerScript_->length(); |
| 154 |
|
| 155 |
MOZ_ASSERT(outerScript_->hasJitScript()); |
| 156 |
ICScript* icScript = outerScript_->jitScript()->icScript(); |
| 157 |
WarpScriptOracle scriptOracle(cx_, this, outerScript_, &mirGen_.outerInfo(), |
| 158 |
icScript); |
| 159 |
|
| 160 |
WarpScriptSnapshot* scriptSnapshot; |
| 161 |
MOZ_TRY_VAR(scriptSnapshot, scriptOracle.createScriptSnapshot()); |
| 162 |
|
| 163 |
// Insert the outermost scriptSnapshot at the front of the list. |
| 164 |
scriptSnapshots_.insertFront(scriptSnapshot); |
| 165 |
|
| 166 |
bool recordFinalWarmUpCount = false; |
| 167 |
#ifdef JS_CACHEIR_SPEW |
| 168 |
recordFinalWarmUpCount = outerScript_->needsFinalWarmUpCount(); |
| 169 |
#endif |
| 170 |
|
| 171 |
auto* snapshot = new (alloc_.fallible()) |
| 172 |
WarpSnapshot(cx_, alloc_, std::move(scriptSnapshots_), zoneStubs_, |
| 173 |
bailoutInfo_, recordFinalWarmUpCount); |
| 174 |
if (!snapshot) { |
| 175 |
return abort(outerScript_, AbortReason::Alloc); |
| 176 |
} |
| 177 |
|
| 178 |
if (!snapshot->nurseryObjects().appendAll(nurseryObjects_)) { |
| 179 |
return abort(outerScript_, AbortReason::Alloc); |
| 180 |
} |
| 181 |
|
| 182 |
#ifdef JS_JITSPEW |
| 183 |
if (JitSpewEnabled(JitSpew_WarpSnapshots)) { |
| 184 |
Fprinter& out = JitSpewPrinter(); |
| 185 |
snapshot->dump(out); |
| 186 |
} |
| 187 |
#endif |
| 188 |
|
| 189 |
#ifdef DEBUG |
| 190 |
// When transpiled CacheIR bails out, we do not want to recompile |
| 191 |
// with the exact same data and get caught in an invalidation loop. |
| 192 |
// |
| 193 |
// To avoid this, we store a hash of the stub pointers and entry |
| 194 |
// counts in this snapshot, save that hash in the JitScript if we |
| 195 |
// have a TranspiledCacheIR or MonomorphicInlinedStubFolding bailout, |
| 196 |
// and assert that the hash has changed when we recompile. |
| 197 |
// |
| 198 |
// Note: this assertion catches potential performance issues. |
| 199 |
// Failing this assertion is not a correctness/security problem. |
| 200 |
// We therefore ignore cases involving resource exhaustion (OOM, |
| 201 |
// stack overflow, etc), or stubs purged by GC. |
| 202 |
HashNumber hash = mozilla::AddToHash(icScript->hash(), runningScriptHash_); |
| 203 |
if (outerScript_->jitScript()->hasFailedICHash()) { |
| 204 |
HashNumber oldHash = outerScript_->jitScript()->getFailedICHash(); |
| 205 |
MOZ_ASSERT_IF(hash == oldHash && !js::SupportDifferentialTesting(), |
| 206 |
cx_->hadResourceExhaustion()); |
| 207 |
} |
| 208 |
snapshot->setICHash(hash); |
| 209 |
#endif |
| 210 |
|
| 211 |
return snapshot; |
| 212 |
} |
| 213 |
|
| 214 |
template <typename T, typename... Args> |
| 215 |
[[nodiscard]] static bool AddOpSnapshot(TempAllocator& alloc, |
| 216 |
WarpOpSnapshotList& snapshots, |
| 217 |
uint32_t offset, Args&&... args) { |
| 218 |
T* snapshot = new (alloc.fallible()) T(offset, std::forward<Args>(args)...); |
| 219 |
if (!snapshot) { |
| 220 |
return false; |
| 221 |
} |
| 222 |
|
| 223 |
snapshots.insertBack(snapshot); |
| 224 |
return true; |
| 225 |
} |
| 226 |
|
| 227 |
[[nodiscard]] static bool AddWarpGetImport(TempAllocator& alloc, |
| 228 |
WarpOpSnapshotList& snapshots, |
| 229 |
uint32_t offset, JSScript* script, |
| 230 |
PropertyName* name) { |
| 231 |
ModuleEnvironmentObject* env = GetModuleEnvironmentForScript(script); |
| 232 |
MOZ_ASSERT(env); |
| 233 |
|
| 234 |
mozilla::Maybe<PropertyInfo> prop; |
| 235 |
ModuleEnvironmentObject* targetEnv; |
| 236 |
MOZ_ALWAYS_TRUE(env->lookupImport(NameToId(name), &targetEnv, &prop)); |
| 237 |
|
| 238 |
uint32_t numFixedSlots = targetEnv->numFixedSlots(); |
| 239 |
uint32_t slot = prop->slot(); |
| 240 |
|
| 241 |
// In the rare case where this import hasn't been initialized already (we have |
| 242 |
// an import cycle where modules reference each other's imports), we need a |
| 243 |
// check. |
| 244 |
bool needsLexicalCheck = |
| 245 |
targetEnv->getSlot(slot).isMagic(JS_UNINITIALIZED_LEXICAL); |
| 246 |
|
| 247 |
return AddOpSnapshot<WarpGetImport>(alloc, snapshots, offset, targetEnv, |
| 248 |
numFixedSlots, slot, needsLexicalCheck); |
| 249 |
} |
| 250 |
|
| 251 |
ICEntry& WarpScriptOracle::getICEntryAndFallback(BytecodeLocation loc, |
| 252 |
ICFallbackStub** fallback) { |
| 253 |
const uint32_t offset = loc.bytecodeToOffset(script_); |
| 254 |
|
| 255 |
do { |
| 256 |
*fallback = icScript_->fallbackStub(icEntryIndex_); |
| 257 |
icEntryIndex_++; |
| 258 |
} while ((*fallback)->pcOffset() < offset); |
| 259 |
|
| 260 |
MOZ_ASSERT((*fallback)->pcOffset() == offset); |
| 261 |
return icScript_->icEntry(icEntryIndex_ - 1); |
| 262 |
} |
| 263 |
|
| 264 |
WarpEnvironment WarpScriptOracle::createEnvironment() { |
| 265 |
// Don't do anything if the script doesn't use the environment chain. |
| 266 |
if (!script_->jitScript()->usesEnvironmentChain()) { |
| 267 |
return WarpEnvironment(NoEnvironment()); |
| 268 |
} |
| 269 |
|
| 270 |
if (script_->isModule()) { |
| 271 |
ModuleObject* module = script_->module(); |
| 272 |
JSObject* obj = &module->initialEnvironment(); |
| 273 |
return WarpEnvironment(ConstantObjectEnvironment(obj)); |
| 274 |
} |
| 275 |
|
| 276 |
JSFunction* fun = script_->function(); |
| 277 |
if (!fun) { |
| 278 |
// For global scripts without a non-syntactic global scope, the environment |
| 279 |
// chain is the global lexical environment. |
| 280 |
MOZ_ASSERT(!script_->isForEval()); |
| 281 |
MOZ_ASSERT(!script_->hasNonSyntacticScope()); |
| 282 |
JSObject* obj = &script_->global().lexicalEnvironment(); |
| 283 |
return WarpEnvironment(ConstantObjectEnvironment(obj)); |
| 284 |
} |
| 285 |
|
| 286 |
auto [callObjectTemplate, namedLambdaTemplate] = |
| 287 |
script_->jitScript()->functionEnvironmentTemplates(fun); |
| 288 |
|
| 289 |
gc::Heap initialHeap = gc::Heap::Default; |
| 290 |
JitScript* jitScript = script_->jitScript(); |
| 291 |
if (jitScript->hasEnvAllocSite()) { |
| 292 |
initialHeap = jitScript->icScript()->maybeEnvAllocSite()->initialHeap(); |
| 293 |
} |
| 294 |
|
| 295 |
return WarpEnvironment(FunctionEnvironment(callObjectTemplate, |
| 296 |
namedLambdaTemplate, initialHeap)); |
| 297 |
} |
| 298 |
|
| 299 |
AbortReasonOr<WarpScriptSnapshot*> WarpScriptOracle::createScriptSnapshot() { |
| 300 |
MOZ_ASSERT(script_->hasJitScript()); |
| 301 |
|
| 302 |
if (!script_->jitScript()->ensureHasCachedIonData(cx_, script_)) { |
| 303 |
return abort(AbortReason::Error); |
| 304 |
} |
| 305 |
|
| 306 |
if (script_->failedBoundsCheck()) { |
| 307 |
oracle_->bailoutInfo().setFailedBoundsCheck(); |
| 308 |
} |
| 309 |
if (script_->failedLexicalCheck()) { |
| 310 |
oracle_->bailoutInfo().setFailedLexicalCheck(); |
| 311 |
} |
| 312 |
|
| 313 |
WarpEnvironment environment = createEnvironment(); |
| 314 |
|
| 315 |
// Unfortunately LinkedList<> asserts the list is empty in its destructor. |
| 316 |
// Clear the list if we abort compilation. |
| 317 |
WarpOpSnapshotList opSnapshots; |
| 318 |
auto autoClearOpSnapshots = |
| 319 |
mozilla::MakeScopeExit([&] { opSnapshots.clear(); }); |
| 320 |
|
| 321 |
ModuleObject* moduleObject = nullptr; |
| 322 |
|
| 323 |
// Analyze the bytecode. Abort compilation for unsupported ops and create |
| 324 |
// WarpOpSnapshots. |
| 325 |
for (BytecodeLocation loc : AllBytecodesIterable(script_)) { |
| 326 |
JSOp op = loc.getOp(); |
| 327 |
uint32_t offset = loc.bytecodeToOffset(script_); |
| 328 |
switch (op) { |
| 329 |
case JSOp::Arguments: { |
| 330 |
MOZ_ASSERT(script_->needsArgsObj()); |
| 331 |
bool mapped = script_->hasMappedArgsObj(); |
| 332 |
ArgumentsObject* templateObj = |
| 333 |
script_->global().maybeArgumentsTemplateObject(mapped); |
| 334 |
if (!AddOpSnapshot<WarpArguments>(alloc_, opSnapshots, offset, |
| 335 |
templateObj)) { |
| 336 |
return abort(AbortReason::Alloc); |
| 337 |
} |
| 338 |
break; |
| 339 |
} |
| 340 |
case JSOp::RegExp: { |
| 341 |
bool hasShared = loc.getRegExp(script_)->hasShared(); |
| 342 |
if (!AddOpSnapshot<WarpRegExp>(alloc_, opSnapshots, offset, |
| 343 |
hasShared)) { |
| 344 |
return abort(AbortReason::Alloc); |
| 345 |
} |
| 346 |
break; |
| 347 |
} |
| 348 |
|
| 349 |
case JSOp::FunctionThis: |
| 350 |
if (!script_->strict() && script_->hasNonSyntacticScope()) { |
| 351 |
// Abort because MBoxNonStrictThis doesn't support non-syntactic |
| 352 |
// scopes (a deprecated SpiderMonkey mechanism). If this becomes an |
| 353 |
// issue we could support it by refactoring GetFunctionThis to not |
| 354 |
// take a frame pointer and then call that. |
| 355 |
return abort(AbortReason::Disable, |
| 356 |
"JSOp::FunctionThis with non-syntactic scope"); |
| 357 |
} |
| 358 |
break; |
| 359 |
|
| 360 |
case JSOp::GlobalThis: |
| 361 |
MOZ_ASSERT(!script_->hasNonSyntacticScope()); |
| 362 |
break; |
| 363 |
|
| 364 |
case JSOp::BuiltinObject: { |
| 365 |
// If we already resolved this built-in we can bake it in. |
| 366 |
auto kind = loc.getBuiltinObjectKind(); |
| 367 |
if (JSObject* proto = MaybeGetBuiltinObject(cx_->global(), kind)) { |
| 368 |
if (!AddOpSnapshot<WarpBuiltinObject>(alloc_, opSnapshots, offset, |
| 369 |
proto)) { |
| 370 |
return abort(AbortReason::Alloc); |
| 371 |
} |
| 372 |
} |
| 373 |
break; |
| 374 |
} |
| 375 |
|
| 376 |
case JSOp::GetIntrinsic: { |
| 377 |
// If we already cloned this intrinsic we can bake it in. |
| 378 |
// NOTE: When the initializer runs in a content global, we also have to |
| 379 |
// worry about nursery objects. These quickly tenure and stay that |
| 380 |
// way so this is only a temporary problem. |
| 381 |
PropertyName* name = loc.getPropertyName(script_); |
| 382 |
Value val; |
| 383 |
if (cx_->global()->maybeGetIntrinsicValue(name, &val, cx_) && |
| 384 |
JS::GCPolicy<Value>::isTenured(val)) { |
| 385 |
if (!AddOpSnapshot<WarpGetIntrinsic>(alloc_, opSnapshots, offset, |
| 386 |
val)) { |
| 387 |
return abort(AbortReason::Alloc); |
| 388 |
} |
| 389 |
} |
| 390 |
break; |
| 391 |
} |
| 392 |
|
| 393 |
case JSOp::ImportMeta: { |
| 394 |
if (!moduleObject) { |
| 395 |
moduleObject = GetModuleObjectForScript(script_); |
| 396 |
MOZ_ASSERT(moduleObject->isTenured()); |
| 397 |
} |
| 398 |
break; |
| 399 |
} |
| 400 |
|
| 401 |
case JSOp::GetImport: { |
| 402 |
PropertyName* name = loc.getPropertyName(script_); |
| 403 |
if (!AddWarpGetImport(alloc_, opSnapshots, offset, script_, name)) { |
| 404 |
return abort(AbortReason::Alloc); |
| 405 |
} |
| 406 |
break; |
| 407 |
} |
| 408 |
|
| 409 |
case JSOp::Lambda: { |
| 410 |
JSFunction* fun = loc.getFunction(script_); |
| 411 |
if (IsAsmJSModule(fun)) { |
| 412 |
return abort(AbortReason::Disable, "asm.js module function lambda"); |
| 413 |
} |
| 414 |
MOZ_TRY(maybeInlineIC(opSnapshots, loc)); |
| 415 |
break; |
| 416 |
} |
| 417 |
|
| 418 |
case JSOp::GetElemSuper: { |
| 419 |
#if defined(JS_CODEGEN_X86) |
| 420 |
// x86 does not have enough registers. |
| 421 |
return abort(AbortReason::Disable, |
| 422 |
"GetElemSuper is not supported on x86"); |
| 423 |
#else |
| 424 |
MOZ_TRY(maybeInlineIC(opSnapshots, loc)); |
| 425 |
break; |
| 426 |
#endif |
| 427 |
} |
| 428 |
|
| 429 |
case JSOp::Rest: { |
| 430 |
if (Shape* shape = |
| 431 |
script_->global().maybeArrayShapeWithDefaultProto()) { |
| 432 |
if (!AddOpSnapshot<WarpRest>(alloc_, opSnapshots, offset, shape)) { |
| 433 |
return abort(AbortReason::Alloc); |
| 434 |
} |
| 435 |
} |
| 436 |
break; |
| 437 |
} |
| 438 |
|
| 439 |
case JSOp::BindUnqualifiedGName: { |
| 440 |
GlobalObject* global = &script_->global(); |
| 441 |
PropertyName* name = loc.getPropertyName(script_); |
| 442 |
if (JSObject* env = |
| 443 |
MaybeOptimizeBindUnqualifiedGlobalName(global, name)) { |
| 444 |
MOZ_ASSERT(env->isTenured()); |
| 445 |
if (!AddOpSnapshot<WarpBindUnqualifiedGName>(alloc_, opSnapshots, |
| 446 |
offset, env)) { |
| 447 |
return abort(AbortReason::Alloc); |
| 448 |
} |
| 449 |
} else { |
| 450 |
MOZ_TRY(maybeInlineIC(opSnapshots, loc)); |
| 451 |
} |
| 452 |
break; |
| 453 |
} |
| 454 |
|
| 455 |
case JSOp::PushVarEnv: { |
| 456 |
Rooted<VarScope*> scope(cx_, &loc.getScope(script_)->as<VarScope>()); |
| 457 |
|
| 458 |
auto* templateObj = |
| 459 |
VarEnvironmentObject::createTemplateObject(cx_, scope); |
| 460 |
if (!templateObj) { |
| 461 |
return abort(AbortReason::Alloc); |
| 462 |
} |
| 463 |
MOZ_ASSERT(templateObj->isTenured()); |
| 464 |
|
| 465 |
if (!AddOpSnapshot<WarpVarEnvironment>(alloc_, opSnapshots, offset, |
| 466 |
templateObj)) { |
| 467 |
return abort(AbortReason::Alloc); |
| 468 |
} |
| 469 |
break; |
| 470 |
} |
| 471 |
|
| 472 |
case JSOp::PushLexicalEnv: |
| 473 |
case JSOp::FreshenLexicalEnv: |
| 474 |
case JSOp::RecreateLexicalEnv: { |
| 475 |
Rooted<LexicalScope*> scope(cx_, |
| 476 |
&loc.getScope(script_)->as<LexicalScope>()); |
| 477 |
|
| 478 |
auto* templateObj = |
| 479 |
BlockLexicalEnvironmentObject::createTemplateObject(cx_, scope); |
| 480 |
if (!templateObj) { |
| 481 |
return abort(AbortReason::Alloc); |
| 482 |
} |
| 483 |
MOZ_ASSERT(templateObj->isTenured()); |
| 484 |
|
| 485 |
if (!AddOpSnapshot<WarpLexicalEnvironment>(alloc_, opSnapshots, offset, |
| 486 |
templateObj)) { |
| 487 |
return abort(AbortReason::Alloc); |
| 488 |
} |
| 489 |
break; |
| 490 |
} |
| 491 |
|
| 492 |
case JSOp::PushClassBodyEnv: { |
| 493 |
Rooted<ClassBodyScope*> scope( |
| 494 |
cx_, &loc.getScope(script_)->as<ClassBodyScope>()); |
| 495 |
|
| 496 |
auto* templateObj = |
| 497 |
ClassBodyLexicalEnvironmentObject::createTemplateObject(cx_, scope); |
| 498 |
if (!templateObj) { |
| 499 |
return abort(AbortReason::Alloc); |
| 500 |
} |
| 501 |
MOZ_ASSERT(templateObj->isTenured()); |
| 502 |
|
| 503 |
if (!AddOpSnapshot<WarpClassBodyEnvironment>(alloc_, opSnapshots, |
| 504 |
offset, templateObj)) { |
| 505 |
return abort(AbortReason::Alloc); |
| 506 |
} |
| 507 |
break; |
| 508 |
} |
| 509 |
|
| 510 |
case JSOp::String: |
| 511 |
if (!loc.atomizeString(cx_, script_)) { |
| 512 |
return abort(AbortReason::Alloc); |
| 513 |
} |
| 514 |
break; |
| 515 |
case JSOp::GetName: |
| 516 |
case JSOp::GetGName: |
| 517 |
case JSOp::GetProp: |
| 518 |
case JSOp::GetElem: |
| 519 |
case JSOp::SetProp: |
| 520 |
case JSOp::StrictSetProp: |
| 521 |
case JSOp::Call: |
| 522 |
case JSOp::CallContent: |
| 523 |
case JSOp::CallIgnoresRv: |
| 524 |
case JSOp::CallIter: |
| 525 |
case JSOp::CallContentIter: |
| 526 |
case JSOp::New: |
| 527 |
case JSOp::NewContent: |
| 528 |
case JSOp::SuperCall: |
| 529 |
case JSOp::SpreadCall: |
| 530 |
case JSOp::SpreadNew: |
| 531 |
case JSOp::SpreadSuperCall: |
| 532 |
case JSOp::ToNumeric: |
| 533 |
case JSOp::Pos: |
| 534 |
case JSOp::Inc: |
| 535 |
case JSOp::Dec: |
| 536 |
case JSOp::Neg: |
| 537 |
case JSOp::BitNot: |
| 538 |
case JSOp::Iter: |
| 539 |
case JSOp::Eq: |
| 540 |
case JSOp::Ne: |
| 541 |
case JSOp::Lt: |
| 542 |
case JSOp::Le: |
| 543 |
case JSOp::Gt: |
| 544 |
case JSOp::Ge: |
| 545 |
case JSOp::StrictEq: |
| 546 |
case JSOp::StrictNe: |
| 547 |
case JSOp::BindName: |
| 548 |
case JSOp::BindUnqualifiedName: |
| 549 |
case JSOp::GetBoundName: |
| 550 |
case JSOp::Add: |
| 551 |
case JSOp::Sub: |
| 552 |
case JSOp::Mul: |
| 553 |
case JSOp::Div: |
| 554 |
case JSOp::Mod: |
| 555 |
case JSOp::Pow: |
| 556 |
case JSOp::BitAnd: |
| 557 |
case JSOp::BitOr: |
| 558 |
case JSOp::BitXor: |
| 559 |
case JSOp::Lsh: |
| 560 |
case JSOp::Rsh: |
| 561 |
case JSOp::Ursh: |
| 562 |
case JSOp::In: |
| 563 |
case JSOp::HasOwn: |
| 564 |
case JSOp::CheckPrivateField: |
| 565 |
case JSOp::Instanceof: |
| 566 |
case JSOp::GetPropSuper: |
| 567 |
case JSOp::InitProp: |
| 568 |
case JSOp::InitLockedProp: |
| 569 |
case JSOp::InitHiddenProp: |
| 570 |
case JSOp::InitElem: |
| 571 |
case JSOp::InitHiddenElem: |
| 572 |
case JSOp::InitLockedElem: |
| 573 |
case JSOp::InitElemInc: |
| 574 |
case JSOp::SetName: |
| 575 |
case JSOp::StrictSetName: |
| 576 |
case JSOp::SetGName: |
| 577 |
case JSOp::StrictSetGName: |
| 578 |
case JSOp::InitGLexical: |
| 579 |
case JSOp::SetElem: |
| 580 |
case JSOp::StrictSetElem: |
| 581 |
case JSOp::ToPropertyKey: |
| 582 |
case JSOp::OptimizeSpreadCall: |
| 583 |
case JSOp::Typeof: |
| 584 |
case JSOp::TypeofExpr: |
| 585 |
case JSOp::TypeofEq: |
| 586 |
case JSOp::NewObject: |
| 587 |
case JSOp::NewInit: |
| 588 |
case JSOp::NewArray: |
| 589 |
case JSOp::JumpIfFalse: |
| 590 |
case JSOp::JumpIfTrue: |
| 591 |
case JSOp::And: |
| 592 |
case JSOp::Or: |
| 593 |
case JSOp::Not: |
| 594 |
case JSOp::CloseIter: |
| 595 |
case JSOp::OptimizeGetIterator: |
| 596 |
MOZ_TRY(maybeInlineIC(opSnapshots, loc)); |
| 597 |
break; |
| 598 |
|
| 599 |
case JSOp::Nop: |
| 600 |
case JSOp::NopDestructuring: |
| 601 |
case JSOp::NopIsAssignOp: |
| 602 |
case JSOp::TryDestructuring: |
| 603 |
case JSOp::Lineno: |
| 604 |
case JSOp::DebugLeaveLexicalEnv: |
| 605 |
case JSOp::Undefined: |
| 606 |
case JSOp::Void: |
| 607 |
case JSOp::Null: |
| 608 |
case JSOp::Hole: |
| 609 |
case JSOp::Uninitialized: |
| 610 |
case JSOp::IsConstructing: |
| 611 |
case JSOp::False: |
| 612 |
case JSOp::True: |
| 613 |
case JSOp::Zero: |
| 614 |
case JSOp::One: |
| 615 |
case JSOp::Int8: |
| 616 |
case JSOp::Uint16: |
| 617 |
case JSOp::Uint24: |
| 618 |
case JSOp::Int32: |
| 619 |
case JSOp::Double: |
| 620 |
case JSOp::BigInt: |
| 621 |
case JSOp::Symbol: |
| 622 |
case JSOp::Pop: |
| 623 |
case JSOp::PopN: |
| 624 |
case JSOp::Dup: |
| 625 |
case JSOp::Dup2: |
| 626 |
case JSOp::DupAt: |
| 627 |
case JSOp::Swap: |
| 628 |
case JSOp::Pick: |
| 629 |
case JSOp::Unpick: |
| 630 |
case JSOp::GetLocal: |
| 631 |
case JSOp::SetLocal: |
| 632 |
case JSOp::InitLexical: |
| 633 |
case JSOp::GetArg: |
| 634 |
case JSOp::GetFrameArg: |
| 635 |
case JSOp::SetArg: |
| 636 |
case JSOp::ArgumentsLength: |
| 637 |
case JSOp::GetActualArg: |
| 638 |
case JSOp::JumpTarget: |
| 639 |
case JSOp::LoopHead: |
| 640 |
case JSOp::Case: |
| 641 |
case JSOp::Default: |
| 642 |
case JSOp::Coalesce: |
| 643 |
case JSOp::Goto: |
| 644 |
case JSOp::DebugCheckSelfHosted: |
| 645 |
case JSOp::DynamicImport: |
| 646 |
case JSOp::ToString: |
| 647 |
case JSOp::GlobalOrEvalDeclInstantiation: |
| 648 |
case JSOp::BindVar: |
| 649 |
case JSOp::MutateProto: |
| 650 |
case JSOp::Callee: |
| 651 |
case JSOp::ToAsyncIter: |
| 652 |
case JSOp::ObjWithProto: |
| 653 |
case JSOp::GetAliasedVar: |
| 654 |
case JSOp::SetAliasedVar: |
| 655 |
case JSOp::InitAliasedLexical: |
| 656 |
case JSOp::EnvCallee: |
| 657 |
case JSOp::MoreIter: |
| 658 |
case JSOp::EndIter: |
| 659 |
case JSOp::IsNoIter: |
| 660 |
case JSOp::IsNullOrUndefined: |
| 661 |
case JSOp::DelProp: |
| 662 |
case JSOp::StrictDelProp: |
| 663 |
case JSOp::DelElem: |
| 664 |
case JSOp::StrictDelElem: |
| 665 |
case JSOp::SetFunName: |
| 666 |
case JSOp::PopLexicalEnv: |
| 667 |
case JSOp::ImplicitThis: |
| 668 |
case JSOp::CheckClassHeritage: |
| 669 |
case JSOp::CheckThis: |
| 670 |
case JSOp::CheckThisReinit: |
| 671 |
case JSOp::Generator: |
| 672 |
case JSOp::AfterYield: |
| 673 |
case JSOp::FinalYieldRval: |
| 674 |
case JSOp::AsyncResolve: |
| 675 |
case JSOp::AsyncReject: |
| 676 |
case JSOp::CheckResumeKind: |
| 677 |
case JSOp::CanSkipAwait: |
| 678 |
case JSOp::MaybeExtractAwaitValue: |
| 679 |
case JSOp::AsyncAwait: |
| 680 |
case JSOp::Await: |
| 681 |
case JSOp::CheckReturn: |
| 682 |
case JSOp::CheckLexical: |
| 683 |
case JSOp::CheckAliasedLexical: |
| 684 |
case JSOp::InitHomeObject: |
| 685 |
case JSOp::SuperBase: |
| 686 |
case JSOp::SuperFun: |
| 687 |
case JSOp::InitElemArray: |
| 688 |
case JSOp::InitPropGetter: |
| 689 |
case JSOp::InitPropSetter: |
| 690 |
case JSOp::InitHiddenPropGetter: |
| 691 |
case JSOp::InitHiddenPropSetter: |
| 692 |
case JSOp::InitElemGetter: |
| 693 |
case JSOp::InitElemSetter: |
| 694 |
case JSOp::InitHiddenElemGetter: |
| 695 |
case JSOp::InitHiddenElemSetter: |
| 696 |
case JSOp::NewTarget: |
| 697 |
case JSOp::Object: |
| 698 |
case JSOp::CallSiteObj: |
| 699 |
case JSOp::CheckIsObj: |
| 700 |
case JSOp::CheckObjCoercible: |
| 701 |
case JSOp::FunWithProto: |
| 702 |
case JSOp::Debugger: |
| 703 |
case JSOp::TableSwitch: |
| 704 |
case JSOp::Exception: |
| 705 |
case JSOp::ExceptionAndStack: |
| 706 |
case JSOp::Throw: |
| 707 |
case JSOp::ThrowWithStack: |
| 708 |
case JSOp::ThrowSetConst: |
| 709 |
case JSOp::SetRval: |
| 710 |
case JSOp::GetRval: |
| 711 |
case JSOp::Return: |
| 712 |
case JSOp::RetRval: |
| 713 |
case JSOp::InitialYield: |
| 714 |
case JSOp::Yield: |
| 715 |
case JSOp::ResumeKind: |
| 716 |
case JSOp::ThrowMsg: |
| 717 |
case JSOp::Try: |
| 718 |
case JSOp::Finally: |
| 719 |
case JSOp::NewPrivateName: |
| 720 |
case JSOp::StrictConstantEq: |
| 721 |
case JSOp::StrictConstantNe: |
| 722 |
#ifdef ENABLE_EXPLICIT_RESOURCE_MANAGEMENT |
| 723 |
case JSOp::AddDisposable: |
| 724 |
case JSOp::TakeDisposeCapability: |
| 725 |
case JSOp::CreateSuppressedError: |
| 726 |
#endif |
| 727 |
// Supported by WarpBuilder. Nothing to do. |
| 728 |
break; |
| 729 |
|
| 730 |
// Unsupported ops. Don't use a 'default' here, we want to trigger a |
| 731 |
// compiler warning when adding a new JSOp. |
| 732 |
#define DEF_CASE(OP) case JSOp::OP: |
| 733 |
WARP_UNSUPPORTED_OPCODE_LIST(DEF_CASE) |
| 734 |
#undef DEF_CASE |
| 735 |
#ifdef DEBUG |
| 736 |
return abort(AbortReason::Disable, "Unsupported opcode: %s", |
| 737 |
CodeName(op)); |
| 738 |
#else |
| 739 |
return abort(AbortReason::Disable, "Unsupported opcode: %u", |
| 740 |
uint8_t(op)); |
| 741 |
#endif |
| 742 |
} |
| 743 |
} |
| 744 |
|
| 745 |
auto* scriptSnapshot = new (alloc_.fallible()) WarpScriptSnapshot( |
| 746 |
script_, environment, std::move(opSnapshots), moduleObject); |
| 747 |
if (!scriptSnapshot) { |
| 748 |
return abort(AbortReason::Alloc); |
| 749 |
} |
| 750 |
|
| 751 |
autoClearOpSnapshots.release(); |
| 752 |
return scriptSnapshot; |
| 753 |
} |
| 754 |
|
| 755 |
static void LineNumberAndColumn(HandleScript script, BytecodeLocation loc, |
| 756 |
unsigned* line, |
| 757 |
JS::LimitedColumnNumberOneOrigin* column) { |
| 758 |
#ifdef DEBUG |
| 759 |
*line = PCToLineNumber(script, loc.toRawBytecode(), column); |
| 760 |
#else |
| 761 |
*line = script->lineno(); |
| 762 |
*column = script->column(); |
| 763 |
#endif |
| 764 |
} |
| 765 |
|
| 766 |
static void MaybeSetInliningStateFromJitHints(JSContext* cx, |
| 767 |
ICFallbackStub* fallbackStub, |
| 768 |
JSScript* script, |
| 769 |
BytecodeLocation loc) { |
| 770 |
// Only update the state if it has already been marked as a candidate. |
| 771 |
if (fallbackStub->trialInliningState() != TrialInliningState::Candidate) { |
| 772 |
return; |
| 773 |
} |
| 774 |
|
| 775 |
// Make sure the op is inlineable. |
| 776 |
if (!TrialInliner::IsValidInliningOp(loc.getOp())) { |
| 777 |
return; |
| 778 |
} |
| 779 |
|
| 780 |
if (!cx->runtime()->jitRuntime()->hasJitHintsMap()) { |
| 781 |
return; |
| 782 |
} |
| 783 |
|
| 784 |
JitHintsMap* jitHints = cx->runtime()->jitRuntime()->getJitHintsMap(); |
| 785 |
uint32_t offset = loc.bytecodeToOffset(script); |
| 786 |
|
| 787 |
if (jitHints->hasMonomorphicInlineHintAtOffset(script, offset)) { |
| 788 |
fallbackStub->setTrialInliningState(TrialInliningState::MonomorphicInlined); |
| 789 |
} |
| 790 |
} |
| 791 |
|
| 792 |
AbortReasonOr<Ok> WarpScriptOracle::maybeInlineIC(WarpOpSnapshotList& snapshots, |
| 793 |
BytecodeLocation loc) { |
| 794 |
// Do one of the following: |
| 795 |
// |
| 796 |
// * If the Baseline IC has a single ICStub we can inline, add a WarpCacheIR |
| 797 |
// snapshot to transpile it to MIR. |
| 798 |
// |
| 799 |
// * If that single ICStub is a call IC with a known target, instead add a |
| 800 |
// WarpInline snapshot to transpile the guards to MIR and inline the target. |
| 801 |
// |
| 802 |
// * If the Baseline IC is cold (never executed), add a WarpBailout snapshot |
| 803 |
// so that we can collect information in Baseline. |
| 804 |
// |
| 805 |
// * Else, don't add a snapshot and rely on WarpBuilder adding an Ion IC. |
| 806 |
|
| 807 |
MOZ_ASSERT(loc.opHasIC()); |
| 808 |
|
| 809 |
// Don't create snapshots when testing ICs. |
| 810 |
if (JitOptions.forceInlineCaches) { |
| 811 |
return Ok(); |
| 812 |
} |
| 813 |
|
| 814 |
ICFallbackStub* fallbackStub; |
| 815 |
const ICEntry& entry = getICEntryAndFallback(loc, &fallbackStub); |
| 816 |
ICStub* firstStub = entry.firstStub(); |
| 817 |
|
| 818 |
uint32_t offset = loc.bytecodeToOffset(script_); |
| 819 |
|
| 820 |
// Set the trial inlining state directly if there is a hint cached from a |
| 821 |
// previous compilation. |
| 822 |
MaybeSetInliningStateFromJitHints(cx_, fallbackStub, script_, loc); |
| 823 |
|
| 824 |
// Clear the used-by-transpiler flag on the IC. It can still be set from a |
| 825 |
// previous compilation because we don't clear the flag on every IC when |
| 826 |
// invalidating. |
| 827 |
fallbackStub->clearUsedByTranspiler(); |
| 828 |
|
| 829 |
if (firstStub == fallbackStub) { |
| 830 |
[[maybe_unused]] unsigned line; |
| 831 |
[[maybe_unused]] JS::LimitedColumnNumberOneOrigin column; |
| 832 |
LineNumberAndColumn(script_, loc, &line, &column); |
| 833 |
|
| 834 |
// No optimized stubs. |
| 835 |
JitSpew(JitSpew_WarpTranspiler, |
| 836 |
"fallback stub (entered-count: %" PRIu32 |
| 837 |
") for JSOp::%s @ %s:%u:%u", |
| 838 |
fallbackStub->enteredCount(), CodeName(loc.getOp()), |
| 839 |
script_->filename(), line, column.oneOriginValue()); |
| 840 |
|
| 841 |
// If the fallback stub was used but there's no optimized stub, use an IC. |
| 842 |
if (fallbackStub->enteredCount() != 0) { |
| 843 |
return Ok(); |
| 844 |
} |
| 845 |
|
| 846 |
// Cold IC. Bailout to collect information. |
| 847 |
if (!AddOpSnapshot<WarpBailout>(alloc_, snapshots, offset)) { |
| 848 |
return abort(AbortReason::Alloc); |
| 849 |
} |
| 850 |
return Ok(); |
| 851 |
} |
| 852 |
|
| 853 |
ICCacheIRStub* stub = firstStub->toCacheIRStub(); |
| 854 |
|
| 855 |
// Don't transpile if this IC ever encountered a case where it had |
| 856 |
// no stub to attach. |
| 857 |
if (fallbackStub->state().hasFailures()) { |
| 858 |
[[maybe_unused]] unsigned line; |
| 859 |
[[maybe_unused]] JS::LimitedColumnNumberOneOrigin column; |
| 860 |
LineNumberAndColumn(script_, loc, &line, &column); |
| 861 |
|
| 862 |
JitSpew(JitSpew_WarpTranspiler, "Failed to attach for JSOp::%s @ %s:%u:%u", |
| 863 |
CodeName(loc.getOp()), script_->filename(), line, |
| 864 |
column.oneOriginValue()); |
| 865 |
return Ok(); |
| 866 |
} |
| 867 |
|
| 868 |
// Don't transpile if there are other stubs with entered-count > 0. Counters |
| 869 |
// are reset when a new stub is attached so this means the stub that was added |
| 870 |
// most recently didn't handle all cases. |
| 871 |
// If this code is changed, ICScript::hash may also need changing. |
| 872 |
bool firstStubHandlesAllCases = true; |
| 873 |
for (ICStub* next = stub->next(); next; next = next->maybeNext()) { |
| 874 |
if (next->enteredCount() != 0) { |
| 875 |
firstStubHandlesAllCases = false; |
| 876 |
break; |
| 877 |
} |
| 878 |
} |
| 879 |
|
| 880 |
if (!firstStubHandlesAllCases) { |
| 881 |
// In some polymorphic cases, we can generate better code than the |
| 882 |
// default fallback if we know the observed types of the operands |
| 883 |
// and their relative frequency. |
| 884 |
if (ICSupportsPolymorphicTypeData(loc.getOp()) && |
| 885 |
fallbackStub->enteredCount() == 0) { |
| 886 |
bool inlinedPolymorphicTypes = false; |
| 887 |
MOZ_TRY_VAR( |
| 888 |
inlinedPolymorphicTypes, |
| 889 |
maybeInlinePolymorphicTypes(snapshots, loc, stub, fallbackStub)); |
| 890 |
if (inlinedPolymorphicTypes) { |
| 891 |
return Ok(); |
| 892 |
} |
| 893 |
} |
| 894 |
|
| 895 |
[[maybe_unused]] unsigned line; |
| 896 |
[[maybe_unused]] JS::LimitedColumnNumberOneOrigin column; |
| 897 |
LineNumberAndColumn(script_, loc, &line, &column); |
| 898 |
|
| 899 |
JitSpew(JitSpew_WarpTranspiler, |
| 900 |
"multiple active stubs for JSOp::%s @ %s:%u:%u", |
| 901 |
CodeName(loc.getOp()), script_->filename(), line, |
| 902 |
column.oneOriginValue()); |
| 903 |
return Ok(); |
| 904 |
} |
| 905 |
|
| 906 |
const CacheIRStubInfo* stubInfo = stub->stubInfo(); |
| 907 |
const uint8_t* stubData = stub->stubDataStart(); |
| 908 |
|
| 909 |
// Only create a snapshot if all opcodes are supported by the transpiler. |
| 910 |
CacheIRReader reader(stubInfo); |
| 911 |
while (reader.more()) { |
| 912 |
CacheOp op = reader.readOp(); |
| 913 |
CacheIROpInfo opInfo = CacheIROpInfos[size_t(op)]; |
| 914 |
reader.skip(opInfo.argLength); |
| 915 |
|
| 916 |
if (!opInfo.transpile) { |
| 917 |
[[maybe_unused]] unsigned line; |
| 918 |
[[maybe_unused]] JS::LimitedColumnNumberOneOrigin column; |
| 919 |
LineNumberAndColumn(script_, loc, &line, &column); |
| 920 |
|
| 921 |
MOZ_ASSERT( |
| 922 |
fallbackStub->trialInliningState() != TrialInliningState::Inlined, |
| 923 |
"Trial-inlined stub not supported by transpiler"); |
| 924 |
|
| 925 |
// Unsupported CacheIR opcode. |
| 926 |
JitSpew(JitSpew_WarpTranspiler, |
| 927 |
"unsupported CacheIR opcode %s for JSOp::%s @ %s:%u:%u", |
| 928 |
CacheIROpNames[size_t(op)], CodeName(loc.getOp()), |
| 929 |
script_->filename(), line, column.oneOriginValue()); |
| 930 |
return Ok(); |
| 931 |
} |
| 932 |
|
| 933 |
// While on the main thread, ensure code stubs exist for ops that require |
| 934 |
// them. |
| 935 |
switch (op) { |
| 936 |
case CacheOp::CallRegExpMatcherResult: |
| 937 |
if (!oracle_->snapshotJitZoneStub(JitZone::StubKind::RegExpMatcher)) { |
| 938 |
return abort(AbortReason::Error); |
| 939 |
} |
| 940 |
break; |
| 941 |
case CacheOp::CallRegExpSearcherResult: |
| 942 |
if (!oracle_->snapshotJitZoneStub(JitZone::StubKind::RegExpSearcher)) { |
| 943 |
return abort(AbortReason::Error); |
| 944 |
} |
| 945 |
break; |
| 946 |
case CacheOp::RegExpBuiltinExecMatchResult: |
| 947 |
if (!oracle_->snapshotJitZoneStub(JitZone::StubKind::RegExpExecMatch)) { |
| 948 |
return abort(AbortReason::Error); |
| 949 |
} |
| 950 |
break; |
| 951 |
case CacheOp::RegExpBuiltinExecTestResult: |
| 952 |
if (!oracle_->snapshotJitZoneStub(JitZone::StubKind::RegExpExecTest)) { |
| 953 |
return abort(AbortReason::Error); |
| 954 |
} |
| 955 |
break; |
| 956 |
case CacheOp::ConcatStringsResult: |
| 957 |
if (!oracle_->snapshotJitZoneStub(JitZone::StubKind::StringConcat)) { |
| 958 |
return abort(AbortReason::Error); |
| 959 |
} |
| 960 |
break; |
| 961 |
default: |
| 962 |
break; |
| 963 |
} |
| 964 |
} |
| 965 |
|
| 966 |
// Check GC is not possible between updating stub pointers and creating the |
| 967 |
// snapshot. |
| 968 |
JS::AutoAssertNoGC nogc; |
| 969 |
|
| 970 |
// Copy the ICStub data to protect against the stub being unlinked or mutated. |
| 971 |
// We don't need to copy the CacheIRStubInfo: because we store and trace the |
| 972 |
// stub's JitCode*, the baselineCacheIRStubCodes_ map in JitZone will keep it |
| 973 |
// alive. |
| 974 |
uint8_t* stubDataCopy = nullptr; |
| 975 |
size_t bytesNeeded = stubInfo->stubDataSize(); |
| 976 |
if (bytesNeeded > 0) { |
| 977 |
stubDataCopy = alloc_.allocateArray<uint8_t>(bytesNeeded); |
| 978 |
if (!stubDataCopy) { |
| 979 |
return abort(AbortReason::Alloc); |
| 980 |
} |
| 981 |
|
| 982 |
// Note: nursery pointers are handled below and the read barrier for weak |
| 983 |
// pointers is handled above so we can do a bitwise copy here. |
| 984 |
std::copy_n(stubData, bytesNeeded, stubDataCopy); |
| 985 |
|
| 986 |
if (!replaceNurseryAndAllocSitePointers(stub, stubInfo, stubDataCopy)) { |
| 987 |
return abort(AbortReason::Alloc); |
| 988 |
} |
| 989 |
} |
| 990 |
|
| 991 |
JitCode* jitCode = stub->jitCode(); |
| 992 |
|
| 993 |
if (fallbackStub->trialInliningState() == TrialInliningState::Inlined || |
| 994 |
fallbackStub->trialInliningState() == |
| 995 |
TrialInliningState::MonomorphicInlined) { |
| 996 |
bool inlinedCall; |
| 997 |
MOZ_TRY_VAR(inlinedCall, maybeInlineCall(snapshots, loc, stub, fallbackStub, |
| 998 |
stubDataCopy)); |
| 999 |
if (inlinedCall) { |
| 1000 |
return Ok(); |
| 1001 |
} |
| 1002 |
} |
| 1003 |
|
| 1004 |
if (!AddOpSnapshot<WarpCacheIR>(alloc_, snapshots, offset, jitCode, stubInfo, |
| 1005 |
stubDataCopy)) { |
| 1006 |
return abort(AbortReason::Alloc); |
| 1007 |
} |
| 1008 |
|
| 1009 |
fallbackStub->setUsedByTranspiler(); |
| 1010 |
|
| 1011 |
return Ok(); |
| 1012 |
} |
| 1013 |
|
| 1014 |
AbortReasonOr<bool> WarpScriptOracle::maybeInlineCall( |
| 1015 |
WarpOpSnapshotList& snapshots, BytecodeLocation loc, ICCacheIRStub* stub, |
| 1016 |
ICFallbackStub* fallbackStub, uint8_t* stubDataCopy) { |
| 1017 |
Maybe<InlinableOpData> inlineData = FindInlinableOpData(stub, loc); |
| 1018 |
if (inlineData.isNothing()) { |
| 1019 |
return false; |
| 1020 |
} |
| 1021 |
|
| 1022 |
RootedFunction targetFunction(cx_, inlineData->target); |
| 1023 |
if (!TrialInliner::canInline(targetFunction, script_, loc)) { |
| 1024 |
return false; |
| 1025 |
} |
| 1026 |
|
| 1027 |
bool isTrialInlined = |
| 1028 |
fallbackStub->trialInliningState() == TrialInliningState::Inlined; |
| 1029 |
MOZ_ASSERT_IF(!isTrialInlined, fallbackStub->trialInliningState() == |
| 1030 |
TrialInliningState::MonomorphicInlined); |
| 1031 |
|
| 1032 |
RootedScript targetScript(cx_, targetFunction->nonLazyScript()); |
| 1033 |
ICScript* icScript = nullptr; |
| 1034 |
if (isTrialInlined) { |
| 1035 |
icScript = inlineData->icScript; |
| 1036 |
} else { |
| 1037 |
JitScript* jitScript = targetScript->jitScript(); |
| 1038 |
icScript = jitScript->icScript(); |
| 1039 |
} |
| 1040 |
|
| 1041 |
if (!icScript) { |
| 1042 |
return false; |
| 1043 |
} |
| 1044 |
|
| 1045 |
// This is just a cheap check to limit the damage we can do to ourselves if |
| 1046 |
// we try to monomorphically inline an indirectly recursive call. |
| 1047 |
const uint32_t maxInliningDepth = 8; |
| 1048 |
if (!isTrialInlined && |
| 1049 |
info_->inlineScriptTree()->depth() > maxInliningDepth) { |
| 1050 |
return false; |
| 1051 |
} |
| 1052 |
|
| 1053 |
// And this is a second cheap check to ensure monomorphic inlining doesn't |
| 1054 |
// cause us to blow past our script size budget. |
| 1055 |
if (oracle_->accumulatedBytecodeSize() + targetScript->length() > |
| 1056 |
JitOptions.ionMaxScriptSize) { |
| 1057 |
return false; |
| 1058 |
} |
| 1059 |
|
| 1060 |
// Add the inlined script to the inline script tree. |
| 1061 |
LifoAlloc* lifoAlloc = alloc_.lifoAlloc(); |
| 1062 |
InlineScriptTree* inlineScriptTree = info_->inlineScriptTree()->addCallee( |
| 1063 |
&alloc_, loc.toRawBytecode(), targetScript, !isTrialInlined); |
| 1064 |
if (!inlineScriptTree) { |
| 1065 |
return abort(AbortReason::Alloc); |
| 1066 |
} |
| 1067 |
|
| 1068 |
// Create a CompileInfo for the inlined script. |
| 1069 |
jsbytecode* osrPc = nullptr; |
| 1070 |
bool needsArgsObj = targetScript->needsArgsObj(); |
| 1071 |
CompileInfo* info = lifoAlloc->new_<CompileInfo>( |
| 1072 |
mirGen_.runtime, targetScript, targetFunction, osrPc, needsArgsObj, |
| 1073 |
inlineScriptTree); |
| 1074 |
if (!info) { |
| 1075 |
return abort(AbortReason::Alloc); |
| 1076 |
} |
| 1077 |
|
| 1078 |
// Take a snapshot of the CacheIR. |
| 1079 |
uint32_t offset = loc.bytecodeToOffset(script_); |
| 1080 |
JitCode* jitCode = stub->jitCode(); |
| 1081 |
const CacheIRStubInfo* stubInfo = stub->stubInfo(); |
| 1082 |
WarpCacheIR* cacheIRSnapshot = new (alloc_.fallible()) |
| 1083 |
WarpCacheIR(offset, jitCode, stubInfo, stubDataCopy); |
| 1084 |
if (!cacheIRSnapshot) { |
| 1085 |
return abort(AbortReason::Alloc); |
| 1086 |
} |
| 1087 |
|
| 1088 |
// Read barrier for weak stub data copied into the snapshot. |
| 1089 |
Zone* zone = jitCode->zone(); |
| 1090 |
if (zone->needsIncrementalBarrier()) { |
| 1091 |
TraceWeakCacheIRStub(zone->barrierTracer(), stub, stub->stubInfo()); |
| 1092 |
} |
| 1093 |
|
| 1094 |
// Take a snapshot of the inlined script (which may do more |
| 1095 |
// inlining recursively). |
| 1096 |
WarpScriptOracle scriptOracle(cx_, oracle_, targetScript, info, icScript); |
| 1097 |
|
| 1098 |
AbortReasonOr<WarpScriptSnapshot*> maybeScriptSnapshot = |
| 1099 |
scriptOracle.createScriptSnapshot(); |
| 1100 |
|
| 1101 |
if (maybeScriptSnapshot.isErr()) { |
| 1102 |
JitSpew(JitSpew_WarpTranspiler, "Can't create snapshot for JSOp::%s", |
| 1103 |
CodeName(loc.getOp())); |
| 1104 |
|
| 1105 |
switch (maybeScriptSnapshot.unwrapErr()) { |
| 1106 |
case AbortReason::Disable: { |
| 1107 |
// If the target script can't be warp-compiled, mark it as |
| 1108 |
// uninlineable, clean up, and fall through to the non-inlined path. |
| 1109 |
|
| 1110 |
// If we monomorphically inline mutually recursive functions, |
| 1111 |
// we can reach this point more than once for the same stub. |
| 1112 |
// We should only unlink the stub once. |
| 1113 |
ICEntry* entry = icScript_->icEntryForStub(fallbackStub); |
| 1114 |
MOZ_ASSERT_IF(entry->firstStub() != stub, |
| 1115 |
entry->firstStub() == stub->next()); |
| 1116 |
if (entry->firstStub() == stub) { |
| 1117 |
fallbackStub->unlinkStub(cx_->zone(), entry, /*prev=*/nullptr, stub); |
| 1118 |
} |
| 1119 |
targetScript->setUninlineable(); |
| 1120 |
info_->inlineScriptTree()->removeCallee(inlineScriptTree); |
| 1121 |
if (isTrialInlined) { |
| 1122 |
icScript_->removeInlinedChild(loc.bytecodeToOffset(script_)); |
| 1123 |
} |
| 1124 |
fallbackStub->setTrialInliningState(TrialInliningState::Failure); |
| 1125 |
return false; |
| 1126 |
} |
| 1127 |
case AbortReason::Error: |
| 1128 |
case AbortReason::Alloc: |
| 1129 |
return Err(maybeScriptSnapshot.unwrapErr()); |
| 1130 |
default: |
| 1131 |
MOZ_CRASH("Unexpected abort reason"); |
| 1132 |
} |
| 1133 |
} |
| 1134 |
|
| 1135 |
WarpScriptSnapshot* scriptSnapshot = maybeScriptSnapshot.unwrap(); |
| 1136 |
oracle_->addScriptSnapshot(scriptSnapshot, icScript, targetScript->length()); |
| 1137 |
#ifdef DEBUG |
| 1138 |
if (!isTrialInlined && targetScript->jitScript()->hasPurgedStubs()) { |
| 1139 |
oracle_->ignoreFailedICHash(); |
| 1140 |
} |
| 1141 |
#endif |
| 1142 |
|
| 1143 |
if (!AddOpSnapshot<WarpInlinedCall>(alloc_, snapshots, offset, |
| 1144 |
cacheIRSnapshot, scriptSnapshot, info)) { |
| 1145 |
return abort(AbortReason::Alloc); |
| 1146 |
} |
| 1147 |
fallbackStub->setUsedByTranspiler(); |
| 1148 |
|
| 1149 |
// Store the location of this monomorphic inline as a hint for future |
| 1150 |
// compilations. |
| 1151 |
if (!isTrialInlined && cx_->runtime()->jitRuntime()->hasJitHintsMap()) { |
| 1152 |
JitHintsMap* jitHints = cx_->runtime()->jitRuntime()->getJitHintsMap(); |
| 1153 |
if (!jitHints->addMonomorphicInlineLocation(script_, loc)) { |
| 1154 |
return abort(AbortReason::Alloc); |
| 1155 |
} |
| 1156 |
} |
| 1157 |
|
| 1158 |
return true; |
| 1159 |
} |
| 1160 |
|
| 1161 |
bool WarpOracle::snapshotJitZoneStub(JitZone::StubKind kind) { |
| 1162 |
if (zoneStubs_[kind]) { |
| 1163 |
return true; |
| 1164 |
} |
| 1165 |
JitCode* stub = cx_->zone()->jitZone()->ensureStubExists(cx_, kind); |
| 1166 |
if (!stub) { |
| 1167 |
return false; |
| 1168 |
} |
| 1169 |
zoneStubs_[kind] = stub; |
| 1170 |
return true; |
| 1171 |
} |
| 1172 |
|
| 1173 |
void WarpOracle::ignoreFailedICHash() { |
| 1174 |
outerScript_->jitScript()->notePurgedStubs(); |
| 1175 |
} |
| 1176 |
|
| 1177 |
struct TypeFrequency { |
| 1178 |
TypeData typeData_; |
| 1179 |
uint32_t successCount_; |
| 1180 |
TypeFrequency(TypeData typeData, uint32_t successCount) |
| 1181 |
: typeData_(typeData), successCount_(successCount) {} |
| 1182 |
|
| 1183 |
// Sort highest frequency first. |
| 1184 |
bool operator<(const TypeFrequency& other) const { |
| 1185 |
return other.successCount_ < successCount_; |
| 1186 |
} |
| 1187 |
}; |
| 1188 |
|
| 1189 |
AbortReasonOr<bool> WarpScriptOracle::maybeInlinePolymorphicTypes( |
| 1190 |
WarpOpSnapshotList& snapshots, BytecodeLocation loc, |
| 1191 |
ICCacheIRStub* firstStub, ICFallbackStub* fallbackStub) { |
| 1192 |
MOZ_ASSERT(ICSupportsPolymorphicTypeData(loc.getOp())); |
| 1193 |
|
| 1194 |
// We use polymorphic type data if there are multiple active stubs, |
| 1195 |
// all of which have type data available. |
| 1196 |
Vector<TypeFrequency, 6, SystemAllocPolicy> candidates; |
| 1197 |
for (ICStub* stub = firstStub; !stub->isFallback(); |
| 1198 |
stub = stub->maybeNext()) { |
| 1199 |
ICCacheIRStub* cacheIRStub = stub->toCacheIRStub(); |
| 1200 |
uint32_t successCount = |
| 1201 |
cacheIRStub->enteredCount() - cacheIRStub->next()->enteredCount(); |
| 1202 |
if (successCount == 0) { |
| 1203 |
continue; |
| 1204 |
} |
| 1205 |
TypeData types = cacheIRStub->typeData(); |
| 1206 |
if (!types.hasData()) { |
| 1207 |
return false; |
| 1208 |
} |
| 1209 |
if (!candidates.append(TypeFrequency(types, successCount))) { |
| 1210 |
return abort(AbortReason::Alloc); |
| 1211 |
} |
| 1212 |
} |
| 1213 |
if (candidates.length() < 2) { |
| 1214 |
return false; |
| 1215 |
} |
| 1216 |
|
| 1217 |
// Sort candidates by success frequency. |
| 1218 |
std::sort(candidates.begin(), candidates.end()); |
| 1219 |
|
| 1220 |
TypeDataList list; |
| 1221 |
for (auto& candidate : candidates) { |
| 1222 |
list.addTypeData(candidate.typeData_); |
| 1223 |
} |
| 1224 |
|
| 1225 |
uint32_t offset = loc.bytecodeToOffset(script_); |
| 1226 |
if (!AddOpSnapshot<WarpPolymorphicTypes>(alloc_, snapshots, offset, list)) { |
| 1227 |
return abort(AbortReason::Alloc); |
| 1228 |
} |
| 1229 |
|
| 1230 |
return true; |
| 1231 |
} |
| 1232 |
|
| 1233 |
bool WarpScriptOracle::replaceNurseryAndAllocSitePointers( |
| 1234 |
ICCacheIRStub* stub, const CacheIRStubInfo* stubInfo, |
| 1235 |
uint8_t* stubDataCopy) { |
| 1236 |
// If the stub data contains nursery object pointers, replace them with the |
| 1237 |
// corresponding nursery index. See WarpObjectField. |
| 1238 |
// |
| 1239 |
// If the stub data contains allocation site pointers replace them with the |
| 1240 |
// initial heap to use, because the site's state may be mutated by the main |
| 1241 |
// thread while we are compiling. |
| 1242 |
// |
| 1243 |
// If the stub data contains weak pointers then trigger a read barrier. This |
| 1244 |
// is necessary as these will now be strong references in the snapshot. |
| 1245 |
// |
| 1246 |
// If the stub data contains strings then atomize them. This ensures we don't |
| 1247 |
// try to access potentially unstable characters from a background thread and |
| 1248 |
// also facilitates certain optimizations. |
| 1249 |
// |
| 1250 |
// Also asserts non-object fields don't contain nursery pointers. |
| 1251 |
|
| 1252 |
uint32_t field = 0; |
| 1253 |
size_t offset = 0; |
| 1254 |
while (true) { |
| 1255 |
StubField::Type fieldType = stubInfo->fieldType(field); |
| 1256 |
switch (fieldType) { |
| 1257 |
case StubField::Type::RawInt32: |
| 1258 |
case StubField::Type::RawPointer: |
| 1259 |
case StubField::Type::ICScript: |
| 1260 |
case StubField::Type::RawInt64: |
| 1261 |
case StubField::Type::Double: |
| 1262 |
break; |
| 1263 |
case StubField::Type::Shape: |
| 1264 |
static_assert(std::is_convertible_v<Shape*, gc::TenuredCell*>, |
| 1265 |
"Code assumes shapes are tenured"); |
| 1266 |
break; |
| 1267 |
case StubField::Type::WeakShape: { |
| 1268 |
static_assert(std::is_convertible_v<Shape*, gc::TenuredCell*>, |
| 1269 |
"Code assumes shapes are tenured"); |
| 1270 |
stubInfo->getStubField<StubField::Type::WeakShape>(stub, offset).get(); |
| 1271 |
break; |
| 1272 |
} |
| 1273 |
case StubField::Type::WeakGetterSetter: { |
| 1274 |
static_assert(std::is_convertible_v<GetterSetter*, gc::TenuredCell*>, |
| 1275 |
"Code assumes GetterSetters are tenured"); |
| 1276 |
stubInfo->getStubField<StubField::Type::WeakGetterSetter>(stub, offset) |
| 1277 |
.get(); |
| 1278 |
break; |
| 1279 |
} |
| 1280 |
case StubField::Type::Symbol: |
| 1281 |
static_assert(std::is_convertible_v<JS::Symbol*, gc::TenuredCell*>, |
| 1282 |
"Code assumes symbols are tenured"); |
| 1283 |
break; |
| 1284 |
case StubField::Type::WeakBaseScript: { |
| 1285 |
static_assert(std::is_convertible_v<BaseScript*, gc::TenuredCell*>, |
| 1286 |
"Code assumes scripts are tenured"); |
| 1287 |
stubInfo->getStubField<StubField::Type::WeakBaseScript>(stub, offset) |
| 1288 |
.get(); |
| 1289 |
break; |
| 1290 |
} |
| 1291 |
case StubField::Type::JitCode: |
| 1292 |
static_assert(std::is_convertible_v<JitCode*, gc::TenuredCell*>, |
| 1293 |
"Code assumes JitCodes are tenured"); |
| 1294 |
break; |
| 1295 |
case StubField::Type::JSObject: { |
| 1296 |
JSObject* obj = |
| 1297 |
stubInfo->getStubField<StubField::Type::JSObject>(stub, offset); |
| 1298 |
if (!maybeReplaceNurseryPointer(stubInfo, stubDataCopy, obj, offset)) { |
| 1299 |
return false; |
| 1300 |
} |
| 1301 |
break; |
| 1302 |
} |
| 1303 |
case StubField::Type::WeakObject: { |
| 1304 |
JSObject* obj = |
| 1305 |
stubInfo->getStubField<StubField::Type::WeakObject>(stub, offset); |
| 1306 |
if (!maybeReplaceNurseryPointer(stubInfo, stubDataCopy, obj, offset)) { |
| 1307 |
return false; |
| 1308 |
} |
| 1309 |
break; |
| 1310 |
} |
| 1311 |
case StubField::Type::String: { |
| 1312 |
uintptr_t oldWord = stubInfo->getStubRawWord(stub, offset); |
| 1313 |
JSString* str = reinterpret_cast<JSString*>(oldWord); |
| 1314 |
MOZ_ASSERT(!IsInsideNursery(str)); |
| 1315 |
JSAtom* atom = AtomizeString(cx_, str); |
| 1316 |
if (!atom) { |
| 1317 |
return false; |
| 1318 |
} |
| 1319 |
if (atom != str) { |
| 1320 |
uintptr_t newWord = reinterpret_cast<uintptr_t>(atom); |
| 1321 |
stubInfo->replaceStubRawWord(stubDataCopy, offset, oldWord, newWord); |
| 1322 |
} |
| 1323 |
break; |
| 1324 |
} |
| 1325 |
case StubField::Type::Id: { |
| 1326 |
#ifdef DEBUG |
| 1327 |
// jsid never contains nursery-allocated things. |
| 1328 |
jsid id = stubInfo->getStubField<StubField::Type::Id>(stub, offset); |
| 1329 |
MOZ_ASSERT_IF(id.isGCThing(), |
| 1330 |
!IsInsideNursery(id.toGCCellPtr().asCell())); |
| 1331 |
#endif |
| 1332 |
break; |
| 1333 |
} |
| 1334 |
case StubField::Type::Value: { |
| 1335 |
Value v = |
| 1336 |
stubInfo->getStubField<StubField::Type::Value>(stub, offset).get(); |
| 1337 |
MOZ_ASSERT_IF(v.isGCThing(), !IsInsideNursery(v.toGCThing())); |
| 1338 |
if (v.isString()) { |
| 1339 |
Value newVal; |
| 1340 |
JSAtom* atom = AtomizeString(cx_, v.toString()); |
| 1341 |
if (!atom) { |
| 1342 |
return false; |
| 1343 |
} |
| 1344 |
newVal.setString(atom); |
| 1345 |
stubInfo->replaceStubRawValueBits(stubDataCopy, offset, v.asRawBits(), |
| 1346 |
newVal.asRawBits()); |
| 1347 |
} |
| 1348 |
break; |
| 1349 |
} |
| 1350 |
case StubField::Type::AllocSite: { |
| 1351 |
uintptr_t oldWord = stubInfo->getStubRawWord(stub, offset); |
| 1352 |
auto* site = reinterpret_cast<gc::AllocSite*>(oldWord); |
| 1353 |
gc::Heap initialHeap = site->initialHeap(); |
| 1354 |
uintptr_t newWord = uintptr_t(initialHeap); |
| 1355 |
stubInfo->replaceStubRawWord(stubDataCopy, offset, oldWord, newWord); |
| 1356 |
break; |
| 1357 |
} |
| 1358 |
case StubField::Type::Limit: |
| 1359 |
return true; // Done. |
| 1360 |
} |
| 1361 |
field++; |
| 1362 |
offset += StubField::sizeInBytes(fieldType); |
| 1363 |
} |
| 1364 |
} |
| 1365 |
|
| 1366 |
bool WarpScriptOracle::maybeReplaceNurseryPointer( |
| 1367 |
const CacheIRStubInfo* stubInfo, uint8_t* stubDataCopy, JSObject* obj, |
| 1368 |
size_t offset) { |
| 1369 |
if (!IsInsideNursery(obj)) { |
| 1370 |
return true; |
| 1371 |
} |
| 1372 |
|
| 1373 |
uint32_t nurseryIndex; |
| 1374 |
if (!oracle_->registerNurseryObject(obj, &nurseryIndex)) { |
| 1375 |
return false; |
| 1376 |
} |
| 1377 |
|
| 1378 |
uintptr_t oldWord = WarpObjectField::fromObject(obj).rawData(); |
| 1379 |
uintptr_t newWord = WarpObjectField::fromNurseryIndex(nurseryIndex).rawData(); |
| 1380 |
stubInfo->replaceStubRawWord(stubDataCopy, offset, oldWord, newWord); |
| 1381 |
return true; |
| 1382 |
} |
| 1383 |
|
| 1384 |
bool WarpOracle::registerNurseryObject(JSObject* obj, uint32_t* nurseryIndex) { |
| 1385 |
MOZ_ASSERT(IsInsideNursery(obj)); |
| 1386 |
|
| 1387 |
auto p = nurseryObjectsMap_.lookupForAdd(obj); |
| 1388 |
if (p) { |
| 1389 |
*nurseryIndex = p->value(); |
| 1390 |
return true; |
| 1391 |
} |
| 1392 |
|
| 1393 |
if (!nurseryObjects_.append(obj)) { |
| 1394 |
return false; |
| 1395 |
} |
| 1396 |
*nurseryIndex = nurseryObjects_.length() - 1; |
| 1397 |
return nurseryObjectsMap_.add(p, obj, *nurseryIndex); |
| 1398 |
} |