Debian Codegraph @

js/src/jit/WarpSnapshot.h

raw
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 #ifndef jit_WarpSnapshot_h
8 #define jit_WarpSnapshot_h
9
10 #include "mozilla/LinkedList.h"
11 #include "mozilla/Variant.h"
12
13 #include "builtin/ModuleObject.h"
14 #include "jit/JitAllocPolicy.h"
15 #include "jit/JitContext.h"
16 #include "jit/JitZone.h"
17 #include "jit/OffthreadSnapshot.h"
18 #include "jit/TypeData.h"
19 #include "vm/EnvironmentObject.h"
20 #include "vm/FunctionFlags.h" // js::FunctionFlags
21
22 namespace js {
23
24 class ArgumentsObject;
25 class CallObject;
26 class GlobalLexicalEnvironmentObject;
27 class LexicalEnvironmentObject;
28 class ModuleEnvironmentObject;
29 class NamedLambdaObject;
30
31 namespace jit {
32
33 class CacheIRStubInfo;
34 class CompileInfo;
35 class WarpScriptSnapshot;
36
37 #define WARP_OP_SNAPSHOT_LIST(_) \
38 _(WarpArguments) \
39 _(WarpRegExp) \
40 _(WarpBuiltinObject) \
41 _(WarpGetIntrinsic) \
42 _(WarpGetImport) \
43 _(WarpRest) \
44 _(WarpBindUnqualifiedGName) \
45 _(WarpVarEnvironment) \
46 _(WarpLexicalEnvironment) \
47 _(WarpClassBodyEnvironment) \
48 _(WarpBailout) \
49 _(WarpCacheIR) \
50 _(WarpInlinedCall) \
51 _(WarpPolymorphicTypes)
52
53 // WarpOpSnapshot is the base class for data attached to a single bytecode op by
54 // WarpOracle. This is typically data that WarpBuilder can't read off-thread
55 // without racing.
56 class WarpOpSnapshot : public TempObject,
57 public mozilla::LinkedListElement<WarpOpSnapshot> {
58 public:
59 enum class Kind : uint16_t {
60 #define DEF_KIND(KIND) KIND,
61 WARP_OP_SNAPSHOT_LIST(DEF_KIND)
62 #undef DEF_KIND
63 };
64
65 private:
66 // Bytecode offset.
67 uint32_t offset_ = 0;
68
69 Kind kind_;
70
71 protected:
72 WarpOpSnapshot(Kind kind, uint32_t offset) : offset_(offset), kind_(kind) {}
73
74 public:
75 uint32_t offset() const { return offset_; }
76 Kind kind() const { return kind_; }
77
78 template <typename T>
79 const T* as() const {
80 MOZ_ASSERT(kind_ == T::ThisKind);
81 return static_cast<const T*>(this);
82 }
83
84 template <typename T>
85 T* as() {
86 MOZ_ASSERT(kind_ == T::ThisKind);
87 return static_cast<T*>(this);
88 }
89
90 void trace(JSTracer* trc);
91
92 #ifdef JS_JITSPEW
93 void dump(GenericPrinter& out, JSScript* script) const;
94 #endif
95 };
96
97 using WarpOpSnapshotList = mozilla::LinkedList<WarpOpSnapshot>;
98
99 // Template object for JSOp::Arguments.
100 class WarpArguments : public WarpOpSnapshot {
101 // Note: this can be nullptr if the realm has no template object yet.
102 OffthreadGCPtr<ArgumentsObject*> templateObj_;
103
104 public:
105 static constexpr Kind ThisKind = Kind::WarpArguments;
106
107 WarpArguments(uint32_t offset, ArgumentsObject* templateObj)
108 : WarpOpSnapshot(ThisKind, offset), templateObj_(templateObj) {}
109 ArgumentsObject* templateObj() const { return templateObj_; }
110
111 void traceData(JSTracer* trc);
112
113 #ifdef JS_JITSPEW
114 void dumpData(GenericPrinter& out) const;
115 #endif
116 };
117
118 // The "has RegExpShared" state for JSOp::RegExp's template object.
119 class WarpRegExp : public WarpOpSnapshot {
120 bool hasShared_;
121
122 public:
123 static constexpr Kind ThisKind = Kind::WarpRegExp;
124
125 WarpRegExp(uint32_t offset, bool hasShared)
126 : WarpOpSnapshot(ThisKind, offset), hasShared_(hasShared) {}
127 bool hasShared() const { return hasShared_; }
128
129 void traceData(JSTracer* trc);
130
131 #ifdef JS_JITSPEW
132 void dumpData(GenericPrinter& out) const;
133 #endif
134 };
135
136 // The object for JSOp::BuiltinObject if it exists at compile-time.
137 class WarpBuiltinObject : public WarpOpSnapshot {
138 OffthreadGCPtr<JSObject*> builtin_;
139
140 public:
141 static constexpr Kind ThisKind = Kind::WarpBuiltinObject;
142
143 WarpBuiltinObject(uint32_t offset, JSObject* builtin)
144 : WarpOpSnapshot(ThisKind, offset), builtin_(builtin) {
145 MOZ_ASSERT(builtin);
146 }
147 JSObject* builtin() const { return builtin_; }
148
149 void traceData(JSTracer* trc);
150
151 #ifdef JS_JITSPEW
152 void dumpData(GenericPrinter& out) const;
153 #endif
154 };
155
156 // The intrinsic for JSOp::GetIntrinsic if it exists at compile-time.
157 class WarpGetIntrinsic : public WarpOpSnapshot {
158 OffthreadGCPtr<Value> intrinsic_;
159
160 public:
161 static constexpr Kind ThisKind = Kind::WarpGetIntrinsic;
162
163 WarpGetIntrinsic(uint32_t offset, const Value& intrinsic)
164 : WarpOpSnapshot(ThisKind, offset), intrinsic_(intrinsic) {}
165 Value intrinsic() const { return intrinsic_; }
166
167 void traceData(JSTracer* trc);
168
169 #ifdef JS_JITSPEW
170 void dumpData(GenericPrinter& out) const;
171 #endif
172 };
173
174 // Target module environment and slot information for JSOp::GetImport.
175 class WarpGetImport : public WarpOpSnapshot {
176 OffthreadGCPtr<ModuleEnvironmentObject*> targetEnv_;
177 uint32_t numFixedSlots_;
178 uint32_t slot_;
179 bool needsLexicalCheck_;
180
181 public:
182 static constexpr Kind ThisKind = Kind::WarpGetImport;
183
184 WarpGetImport(uint32_t offset, ModuleEnvironmentObject* targetEnv,
185 uint32_t numFixedSlots, uint32_t slot, bool needsLexicalCheck)
186 : WarpOpSnapshot(ThisKind, offset),
187 targetEnv_(targetEnv),
188 numFixedSlots_(numFixedSlots),
189 slot_(slot),
190 needsLexicalCheck_(needsLexicalCheck) {}
191 ModuleEnvironmentObject* targetEnv() const { return targetEnv_; }
192 uint32_t numFixedSlots() const { return numFixedSlots_; }
193 uint32_t slot() const { return slot_; }
194 bool needsLexicalCheck() const { return needsLexicalCheck_; }
195
196 void traceData(JSTracer* trc);
197
198 #ifdef JS_JITSPEW
199 void dumpData(GenericPrinter& out) const;
200 #endif
201 };
202
203 // Informs WarpBuilder that an IC site is cold and execution should bail out.
204 class WarpBailout : public WarpOpSnapshot {
205 public:
206 static constexpr Kind ThisKind = Kind::WarpBailout;
207
208 explicit WarpBailout(uint32_t offset) : WarpOpSnapshot(ThisKind, offset) {}
209
210 void traceData(JSTracer* trc);
211
212 #ifdef JS_JITSPEW
213 void dumpData(GenericPrinter& out) const;
214 #endif
215 };
216
217 // Information from a Baseline IC stub.
218 class WarpCacheIR : public WarpOpSnapshot {
219 // Baseline stub code. Stored here to keep the CacheIRStubInfo alive.
220 OffthreadGCPtr<JitCode*> stubCode_;
221 const CacheIRStubInfo* stubInfo_;
222
223 // Copied Baseline stub data. Allocated in the same LifoAlloc.
224 const uint8_t* stubData_;
225
226 public:
227 static constexpr Kind ThisKind = Kind::WarpCacheIR;
228
229 WarpCacheIR(uint32_t offset, JitCode* stubCode,
230 const CacheIRStubInfo* stubInfo, const uint8_t* stubData)
231 : WarpOpSnapshot(ThisKind, offset),
232 stubCode_(stubCode),
233 stubInfo_(stubInfo),
234 stubData_(stubData) {}
235
236 const CacheIRStubInfo* stubInfo() const { return stubInfo_; }
237 const uint8_t* stubData() const { return stubData_; }
238
239 void traceData(JSTracer* trc);
240
241 #ifdef JS_JITSPEW
242 void dumpData(GenericPrinter& out) const;
243 #endif
244 };
245
246 // [SMDOC] Warp Nursery Object support
247 //
248 // CacheIR stub data can contain nursery allocated objects. This can happen for
249 // example for GuardSpecificObject/GuardSpecificFunction or GuardProto.
250 //
251 // To support nursery GCs in parallel with off-thread compilation, we use the
252 // following mechanism:
253 //
254 // * When WarpOracle copies stub data, it builds a Vector of nursery objects.
255 // The nursery object pointers in the stub data are replaced with the
256 // corresponding index into this Vector.
257 // See WarpScriptOracle::replaceNurseryPointers.
258 //
259 // * The Vector is copied to the snapshot and, at the end of compilation, to
260 // the IonScript. The Vector is only accessed on the main thread.
261 //
262 // * The MIR backend never accesses the raw JSObject*. Instead, it uses
263 // MNurseryObject which will load the object at runtime from the IonScript.
264 //
265 // WarpObjectField is a helper class to encode/decode a stub data field that
266 // either stores an object or a nursery index.
267 class WarpObjectField {
268 // This is a nursery index if the low bit is set. Else it's a JSObject*.
269 static constexpr uintptr_t NurseryIndexTag = 0x1;
270 static constexpr uintptr_t NurseryIndexShift = 1;
271
272 uintptr_t data_;
273
274 explicit WarpObjectField(uintptr_t data) : data_(data) {}
275
276 public:
277 static WarpObjectField fromData(uintptr_t data) {
278 return WarpObjectField(data);
279 }
280 static WarpObjectField fromObject(JSObject* obj) {
281 return WarpObjectField(uintptr_t(obj));
282 }
283 static WarpObjectField fromNurseryIndex(uint32_t index) {
284 uintptr_t data = (uintptr_t(index) << NurseryIndexShift) | NurseryIndexTag;
285 return WarpObjectField(data);
286 }
287
288 uintptr_t rawData() const { return data_; }
289
290 bool isNurseryIndex() const { return (data_ & NurseryIndexTag) != 0; }
291
292 uint32_t toNurseryIndex() const {
293 MOZ_ASSERT(isNurseryIndex());
294 return data_ >> NurseryIndexShift;
295 }
296
297 JSObject* toObject() const {
298 MOZ_ASSERT(!isNurseryIndex());
299 return reinterpret_cast<JSObject*>(data_);
300 }
301 };
302
303 // Information for inlining a scripted call IC.
304 class WarpInlinedCall : public WarpOpSnapshot {
305 // Used for generating the correct guards.
306 WarpCacheIR* cacheIRSnapshot_;
307
308 // Used for generating the inlined code.
309 WarpScriptSnapshot* scriptSnapshot_;
310 CompileInfo* info_;
311
312 public:
313 static constexpr Kind ThisKind = Kind::WarpInlinedCall;
314
315 WarpInlinedCall(uint32_t offset, WarpCacheIR* cacheIRSnapshot,
316 WarpScriptSnapshot* scriptSnapshot, CompileInfo* info)
317 : WarpOpSnapshot(ThisKind, offset),
318 cacheIRSnapshot_(cacheIRSnapshot),
319 scriptSnapshot_(scriptSnapshot),
320 info_(info) {}
321
322 WarpCacheIR* cacheIRSnapshot() const { return cacheIRSnapshot_; }
323 WarpScriptSnapshot* scriptSnapshot() const { return scriptSnapshot_; }
324 CompileInfo* info() const { return info_; }
325
326 void traceData(JSTracer* trc);
327
328 #ifdef JS_JITSPEW
329 void dumpData(GenericPrinter& out) const;
330 #endif
331 };
332
333 // Information for inlining an ordered set of types
334 class WarpPolymorphicTypes : public WarpOpSnapshot {
335 TypeDataList list_;
336
337 public:
338 static constexpr Kind ThisKind = Kind::WarpPolymorphicTypes;
339
340 WarpPolymorphicTypes(uint32_t offset, TypeDataList list)
341 : WarpOpSnapshot(ThisKind, offset), list_(list) {}
342
343 const TypeDataList& list() const { return list_; }
344
345 void traceData(JSTracer* trc);
346
347 #ifdef JS_JITSPEW
348 void dumpData(GenericPrinter& out) const;
349 #endif
350 };
351
352 // Shape for JSOp::Rest.
353 class WarpRest : public WarpOpSnapshot {
354 OffthreadGCPtr<Shape*> shape_;
355
356 public:
357 static constexpr Kind ThisKind = Kind::WarpRest;
358
359 WarpRest(uint32_t offset, Shape* shape)
360 : WarpOpSnapshot(ThisKind, offset), shape_(shape) {}
361
362 Shape* shape() const { return shape_; }
363
364 void traceData(JSTracer* trc);
365
366 #ifdef JS_JITSPEW
367 void dumpData(GenericPrinter& out) const;
368 #endif
369 };
370
371 // Global environment for BindUnqualifiedGName
372 class WarpBindUnqualifiedGName : public WarpOpSnapshot {
373 OffthreadGCPtr<JSObject*> globalEnv_;
374
375 public:
376 static constexpr Kind ThisKind = Kind::WarpBindUnqualifiedGName;
377
378 WarpBindUnqualifiedGName(uint32_t offset, JSObject* globalEnv)
379 : WarpOpSnapshot(ThisKind, offset), globalEnv_(globalEnv) {}
380
381 JSObject* globalEnv() const { return globalEnv_; }
382
383 void traceData(JSTracer* trc);
384
385 #ifdef JS_JITSPEW
386 void dumpData(GenericPrinter& out) const;
387 #endif
388 };
389
390 // Block environment for PushVarEnv
391 class WarpVarEnvironment : public WarpOpSnapshot {
392 OffthreadGCPtr<VarEnvironmentObject*> templateObj_;
393
394 public:
395 static constexpr Kind ThisKind = Kind::WarpVarEnvironment;
396
397 WarpVarEnvironment(uint32_t offset, VarEnvironmentObject* templateObj)
398 : WarpOpSnapshot(ThisKind, offset), templateObj_(templateObj) {}
399
400 VarEnvironmentObject* templateObj() const { return templateObj_; }
401
402 void traceData(JSTracer* trc);
403
404 #ifdef JS_JITSPEW
405 void dumpData(GenericPrinter& out) const;
406 #endif
407 };
408
409 // Block environment for PushLexicalEnv, FreshenLexicalEnv, RecreateLexicalEnv
410 class WarpLexicalEnvironment : public WarpOpSnapshot {
411 OffthreadGCPtr<BlockLexicalEnvironmentObject*> templateObj_;
412
413 public:
414 static constexpr Kind ThisKind = Kind::WarpLexicalEnvironment;
415
416 WarpLexicalEnvironment(uint32_t offset,
417 BlockLexicalEnvironmentObject* templateObj)
418 : WarpOpSnapshot(ThisKind, offset), templateObj_(templateObj) {}
419
420 BlockLexicalEnvironmentObject* templateObj() const { return templateObj_; }
421
422 void traceData(JSTracer* trc);
423
424 #ifdef JS_JITSPEW
425 void dumpData(GenericPrinter& out) const;
426 #endif
427 };
428
429 // Class body lexical environment for PushClassBodyEnv
430 class WarpClassBodyEnvironment : public WarpOpSnapshot {
431 OffthreadGCPtr<ClassBodyLexicalEnvironmentObject*> templateObj_;
432
433 public:
434 static constexpr Kind ThisKind = Kind::WarpClassBodyEnvironment;
435
436 WarpClassBodyEnvironment(uint32_t offset,
437 ClassBodyLexicalEnvironmentObject* templateObj)
438 : WarpOpSnapshot(ThisKind, offset), templateObj_(templateObj) {}
439
440 ClassBodyLexicalEnvironmentObject* templateObj() const {
441 return templateObj_;
442 }
443
444 void traceData(JSTracer* trc);
445
446 #ifdef JS_JITSPEW
447 void dumpData(GenericPrinter& out) const;
448 #endif
449 };
450
451 struct NoEnvironment {};
452 using ConstantObjectEnvironment = OffthreadGCPtr<JSObject*>;
453 struct FunctionEnvironment {
454 OffthreadGCPtr<CallObject*> callObjectTemplate;
455 OffthreadGCPtr<NamedLambdaObject*> namedLambdaTemplate;
456 gc::Heap initialHeap;
457
458 public:
459 FunctionEnvironment(CallObject* callObjectTemplate,
460 NamedLambdaObject* namedLambdaTemplate,
461 gc::Heap initialHeap)
462 : callObjectTemplate(callObjectTemplate),
463 namedLambdaTemplate(namedLambdaTemplate),
464 initialHeap(initialHeap) {}
465 };
466
467 // Snapshot data for the environment object(s) created in the script's prologue.
468 //
469 // One of:
470 //
471 // * NoEnvironment: No environment object should be set. Leave the slot
472 // initialized to |undefined|.
473 //
474 // * ConstantObjectEnvironment: Use this JSObject* as environment object.
475 //
476 // * FunctionEnvironment: Use the callee's environment chain. Optionally
477 // allocate a new NamedLambdaObject and/or CallObject based on
478 // namedLambdaTemplate and callObjectTemplate.
479 using WarpEnvironment =
480 mozilla::Variant<NoEnvironment, ConstantObjectEnvironment,
481 FunctionEnvironment>;
482
483 // Snapshot data for a single JSScript.
484 class WarpScriptSnapshot
485 : public TempObject,
486 public mozilla::LinkedListElement<WarpScriptSnapshot> {
487 OffthreadGCPtr<JSScript*> script_;
488 WarpEnvironment environment_;
489 WarpOpSnapshotList opSnapshots_;
490
491 // If the script has a JSOp::ImportMeta op, this is the module to bake in.
492 OffthreadGCPtr<ModuleObject*> moduleObject_;
493
494 // Whether this script is for an arrow function.
495 bool isArrowFunction_;
496
497 public:
498 WarpScriptSnapshot(JSScript* script, const WarpEnvironment& env,
499 WarpOpSnapshotList&& opSnapshots,
500 ModuleObject* moduleObject);
501
502 JSScript* script() const { return script_; }
503 const WarpEnvironment& environment() const { return environment_; }
504 const WarpOpSnapshotList& opSnapshots() const { return opSnapshots_; }
505 ModuleObject* moduleObject() const { return moduleObject_; }
506
507 bool isArrowFunction() const { return isArrowFunction_; }
508
509 void trace(JSTracer* trc);
510
511 #ifdef JS_JITSPEW
512 void dump(GenericPrinter& out) const;
513 #endif
514 };
515
516 // Captures information from previous bailouts to prevent bailout/recompile
517 // loops.
518 class WarpBailoutInfo {
519 // True if any script in the compilation has the failedBoundsCheck flag. In
520 // this case mark bounds checks as non-movable to prevent hoisting them in
521 // TryEliminateBoundsCheck.
522 bool failedBoundsCheck_ = false;
523
524 // True if any script in the compilation has the failedLexicalCheck flag. In
525 // this case mark lexical checks as non-movable.
526 bool failedLexicalCheck_ = false;
527
528 public:
529 bool failedBoundsCheck() const { return failedBoundsCheck_; }
530 void setFailedBoundsCheck() { failedBoundsCheck_ = true; }
531
532 bool failedLexicalCheck() const { return failedLexicalCheck_; }
533 void setFailedLexicalCheck() { failedLexicalCheck_ = true; }
534 };
535
536 using WarpScriptSnapshotList = mozilla::LinkedList<WarpScriptSnapshot>;
537
538 using WarpZoneStubsSnapshot = JitZone::Stubs<JitCode*>;
539
540 // Data allocated by WarpOracle on the main thread that's used off-thread by
541 // WarpBuilder to build the MIR graph.
542 class WarpSnapshot : public TempObject {
543 // The scripts being compiled.
544 WarpScriptSnapshotList scriptSnapshots_;
545
546 // JitZone stubs this compilation depends on.
547 const WarpZoneStubsSnapshot zoneStubs_;
548
549 // The global lexical environment and its thisObject(). We don't inline
550 // cross-realm calls so this can be stored once per snapshot.
551 OffthreadGCPtr<GlobalLexicalEnvironmentObject*> globalLexicalEnv_;
552 OffthreadGCPtr<JSObject*> globalLexicalEnvThis_;
553
554 const WarpBailoutInfo bailoutInfo_;
555
556 // List of (originally) nursery-allocated objects. Must only be accessed on
557 // the main thread. See also WarpObjectField.
558 using NurseryObjectVector = Vector<JSObject*, 0, JitAllocPolicy>;
559 NurseryObjectVector nurseryObjects_;
560
561 #ifdef JS_CACHEIR_SPEW
562 bool needsFinalWarmUpCount_ = false;
563 #endif
564
565 #ifdef DEBUG
566 // A hash of the stub pointers and entry counts for each of the ICs
567 // in this snapshot.
568 mozilla::HashNumber icHash_ = 0;
569 #endif
570
571 public:
572 explicit WarpSnapshot(JSContext* cx, TempAllocator& alloc,
573 WarpScriptSnapshotList&& scriptSnapshots,
574 const WarpZoneStubsSnapshot& zoneStubs,
575 const WarpBailoutInfo& bailoutInfo,
576 bool recordWarmUpCount);
577
578 WarpScriptSnapshot* rootScript() { return scriptSnapshots_.getFirst(); }
579 const WarpScriptSnapshotList& scripts() const { return scriptSnapshots_; }
580
581 JitCode* getZoneStub(JitZone::StubKind kind) const {
582 MOZ_ASSERT(zoneStubs_[kind]);
583 return zoneStubs_[kind];
584 }
585
586 GlobalLexicalEnvironmentObject* globalLexicalEnv() const {
587 return globalLexicalEnv_;
588 }
589 JSObject* globalLexicalEnvThis() const { return globalLexicalEnvThis_; }
590
591 void trace(JSTracer* trc);
592
593 const WarpBailoutInfo& bailoutInfo() const { return bailoutInfo_; }
594
595 NurseryObjectVector& nurseryObjects() { return nurseryObjects_; }
596 const NurseryObjectVector& nurseryObjects() const { return nurseryObjects_; }
597
598 #ifdef DEBUG
599 mozilla::HashNumber icHash() const { return icHash_; }
600 void setICHash(mozilla::HashNumber hash) { icHash_ = hash; }
601 #endif
602
603 #ifdef JS_JITSPEW
604 void dump() const;
605 void dump(GenericPrinter& out) const;
606 #endif
607
608 #ifdef JS_CACHEIR_SPEW
609 bool needsFinalWarmUpCount() const { return needsFinalWarmUpCount_; }
610 #endif
611 };
612
613 } // namespace jit
614 } // namespace js
615
616 #endif /* jit_WarpSnapshot_h */