Debian Codegraph @

js/src/jit/IonScript.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_IonScript_h
8 #define jit_IonScript_h
9
10 #include "mozilla/MemoryReporting.h" // MallocSizeOf
11
12 #include <stddef.h> // size_t
13 #include <stdint.h> // uint8_t, uint32_t
14
15 #include "jstypes.h"
16
17 #include "gc/Barrier.h" // HeapPtr{JitCode,Object}
18 #include "jit/IonTypes.h" // IonCompilationId
19 #include "jit/JitCode.h" // JitCode
20 #include "jit/JitOptions.h" // JitOptions
21 #include "js/TypeDecls.h" // jsbytecode
22 #include "util/TrailingArray.h" // TrailingArray
23
24 namespace js {
25 namespace jit {
26
27 class SnapshotWriter;
28 class RecoverWriter;
29 class SafepointWriter;
30 class CodegenSafepointIndex;
31 class SafepointIndex;
32 class OsiIndex;
33 class IonIC;
34
35 // An IonScript attaches Ion-generated information to a JSScript. The header
36 // structure is followed by several arrays of data. These trailing arrays have a
37 // layout based on offsets (bytes from 'this') stored in the IonScript header.
38 //
39 // <IonScript itself>
40 // --
41 // PreBarriered<Value>[] constantTable()
42 // uint8_t[] runtimeData()
43 // OsiIndex[] osiIndex()
44 // SafepointIndex[] safepointIndex()
45 // uint32_t[] icIndex()
46 // --
47 // uint8_t[] safepoints()
48 // uint8_t[] snapshots()
49 // uint8_t[] snapshotsRVATable()
50 // uint8_t[] recovers()
51 //
52 // Note: These are arranged in order of descending alignment requirements to
53 // avoid the need for padding. The `runtimeData` uses uint64_t alignement due to
54 // its use of mozilla::AlignedStorage2.
55 class alignas(8) IonScript final : public TrailingArray<IonScript> {
56 private:
57 // Offset (in bytes) from `this` to the start of each trailing array. Each
58 // array ends where following one begins. There is no implicit padding (except
59 // possible at very end).
60 Offset constantTableOffset_ = 0; // JS::Value aligned
61 Offset runtimeDataOffset_ = 0; // uint64_t aligned
62 Offset nurseryObjectsOffset_ = 0; // pointer aligned
63 Offset osiIndexOffset_ = 0;
64 Offset safepointIndexOffset_ = 0;
65 Offset icIndexOffset_ = 0;
66 Offset safepointsOffset_ = 0;
67 Offset snapshotsOffset_ = 0;
68 Offset rvaTableOffset_ = 0;
69 Offset recoversOffset_ = 0;
70 Offset allocBytes_ = 0;
71
72 // Code pointer containing the actual method.
73 HeapPtr<JitCode*> method_ = nullptr;
74
75 // Entrypoint for OSR, or nullptr.
76 jsbytecode* osrPc_ = nullptr;
77
78 // Offset to OSR entrypoint from method_->raw(), or 0.
79 uint32_t osrEntryOffset_ = 0;
80
81 // Offset of the invalidation epilogue (which pushes this IonScript
82 // and calls the invalidation thunk).
83 uint32_t invalidateEpilogueOffset_ = 0;
84
85 // The offset immediately after the IonScript immediate.
86 // NOTE: technically a constant delta from
87 // |invalidateEpilogueOffset_|, so we could hard-code this
88 // per-platform if we want.
89 uint32_t invalidateEpilogueDataOffset_ = 0;
90
91 // Number of bailouts that have occurred for reasons that could be
92 // fixed if we invalidated and recompiled.
93 uint16_t numFixableBailouts_ = 0;
94
95 // Number of bailouts that have occurred for reasons that can't be
96 // fixed by recompiling: for example, bailing out to catch an exception.
97 uint16_t numUnfixableBailouts_ = 0;
98
99 public:
100 enum class LICMState : uint8_t { NeverBailed, Bailed, BailedAndHitFallback };
101
102 private:
103 // Tracks the state of LICM bailouts.
104 LICMState licmState_ = LICMState::NeverBailed;
105
106 // Flag set if IonScript was compiled with profiling enabled.
107 bool hasProfilingInstrumentation_ = false;
108
109 // If true, this IonScript was active on the stack when we discarded JIT code
110 // and inactive ICScripts. This means we should use the generic ICScripts for
111 // inlined functions when we bail out.
112 bool purgedICScripts_ = false;
113
114 // Number of bytes this function reserves on the stack for slots spilled by
115 // the register allocator.
116 uint32_t localSlotsSize_ = 0;
117
118 // Number of bytes used passed in as formal arguments or |this|.
119 uint32_t argumentSlotsSize_ = 0;
120
121 // Frame size is the value that can be added to the StackPointer along
122 // with the frame prefix to get a valid JitFrameLayout.
123 uint32_t frameSize_ = 0;
124
125 // Number of references from invalidation records.
126 uint32_t invalidationCount_ = 0;
127
128 // Identifier of the compilation which produced this code.
129 IonCompilationId compilationId_;
130
131 // Number of times we tried to enter this script via OSR but failed due to
132 // a LOOPENTRY pc other than osrPc_.
133 uint32_t osrPcMismatchCounter_ = 0;
134
135 #ifdef DEBUG
136 // A hash of the ICScripts used in this compilation.
137 mozilla::HashNumber icHash_ = 0;
138 #endif
139
140 // End of fields.
141
142 private:
143 // Layout helpers
144 Offset constantTableOffset() const { return constantTableOffset_; }
145 Offset runtimeDataOffset() const { return runtimeDataOffset_; }
146 Offset nurseryObjectsOffset() const { return nurseryObjectsOffset_; }
147 Offset osiIndexOffset() const { return osiIndexOffset_; }
148 Offset safepointIndexOffset() const { return safepointIndexOffset_; }
149 Offset icIndexOffset() const { return icIndexOffset_; }
150 Offset safepointsOffset() const { return safepointsOffset_; }
151 Offset snapshotsOffset() const { return snapshotsOffset_; }
152 Offset rvaTableOffset() const { return rvaTableOffset_; }
153 Offset recoversOffset() const { return recoversOffset_; }
154 Offset endOffset() const { return allocBytes_; }
155
156 // Hardcode size of incomplete types. These are verified in Ion.cpp.
157 static constexpr size_t SizeOf_OsiIndex = 2 * sizeof(uint32_t);
158 static constexpr size_t SizeOf_SafepointIndex = 2 * sizeof(uint32_t);
159
160 public:
161 //
162 // Table of constants referenced in snapshots. (JS::Value alignment)
163 //
164 PreBarriered<Value>* constants() {
165 // Nursery constants are manually barriered in CodeGenerator::link() so a
166 // post barrier is not required..
167 return offsetToPointer<PreBarriered<Value>>(constantTableOffset());
168 }
169 size_t numConstants() const {
170 return numElements<PreBarriered<Value>>(constantTableOffset(),
171 runtimeDataOffset());
172 }
173
174 //
175 // IonIC data structures. (uint64_t alignment)
176 //
177 uint8_t* runtimeData() {
178 return offsetToPointer<uint8_t>(runtimeDataOffset());
179 }
180 size_t runtimeSize() const {
181 return numElements<uint8_t>(runtimeDataOffset(), nurseryObjectsOffset());
182 }
183
184 //
185 // List of (originally) nursery-allocated objects referenced from JIT code.
186 // (JSObject* alignment)
187 //
188 HeapPtr<JSObject*>* nurseryObjects() {
189 return offsetToPointer<HeapPtr<JSObject*>>(nurseryObjectsOffset());
190 }
191 size_t numNurseryObjects() const {
192 return numElements<HeapPtr<JSObject*>>(nurseryObjectsOffset(),
193 osiIndexOffset());
194 }
195 void* addressOfNurseryObject(uint32_t index) {
196 MOZ_ASSERT(index < numNurseryObjects());
197 return &nurseryObjects()[index];
198 }
199
200 //
201 // Map OSI-point displacement to snapshot.
202 //
203 OsiIndex* osiIndices() { return offsetToPointer<OsiIndex>(osiIndexOffset()); }
204 const OsiIndex* osiIndices() const {
205 return offsetToPointer<OsiIndex>(osiIndexOffset());
206 }
207 size_t numOsiIndices() const {
208 return numElements<SizeOf_OsiIndex>(osiIndexOffset(),
209 safepointIndexOffset());
210 }
211
212 //
213 // Map code displacement to safepoint / OSI-patch-delta.
214 //
215 SafepointIndex* safepointIndices() {
216 return offsetToPointer<SafepointIndex>(safepointIndexOffset());
217 }
218 const SafepointIndex* safepointIndices() const {
219 return offsetToPointer<SafepointIndex>(safepointIndexOffset());
220 }
221 size_t numSafepointIndices() const {
222 return numElements<SizeOf_SafepointIndex>(safepointIndexOffset(),
223 icIndexOffset());
224 }
225
226 //
227 // Offset into `runtimeData` for each (variable-length) IonIC.
228 //
229 uint32_t* icIndex() { return offsetToPointer<uint32_t>(icIndexOffset()); }
230 size_t numICs() const {
231 return numElements<uint32_t>(icIndexOffset(), safepointsOffset());
232 }
233
234 //
235 // Safepoint table as a CompactBuffer.
236 //
237 const uint8_t* safepoints() const {
238 return offsetToPointer<uint8_t>(safepointsOffset());
239 }
240 size_t safepointsSize() const {
241 return numElements<uint8_t>(safepointsOffset(), snapshotsOffset());
242 }
243
244 //
245 // Snapshot and RValueAllocation tables as CompactBuffers.
246 //
247 const uint8_t* snapshots() const {
248 return offsetToPointer<uint8_t>(snapshotsOffset());
249 }
250 size_t snapshotsListSize() const {
251 return numElements<uint8_t>(snapshotsOffset(), rvaTableOffset());
252 }
253 size_t snapshotsRVATableSize() const {
254 return numElements<uint8_t>(rvaTableOffset(), recoversOffset());
255 }
256
257 //
258 // Recover instruction table as a CompactBuffer.
259 //
260 const uint8_t* recovers() const {
261 return offsetToPointer<uint8_t>(recoversOffset());
262 }
263 size_t recoversSize() const {
264 return numElements<uint8_t>(recoversOffset(), endOffset());
265 }
266
267 private:
268 IonScript(IonCompilationId compilationId, uint32_t localSlotsSize,
269 uint32_t argumentSlotsSize, uint32_t frameSize);
270
271 public:
272 static IonScript* New(JSContext* cx, IonCompilationId compilationId,
273 uint32_t localSlotsSize, uint32_t argumentSlotsSize,
274 uint32_t frameSize, size_t snapshotsListSize,
275 size_t snapshotsRVATableSize, size_t recoversSize,
276 size_t constants, size_t nurseryObjects,
277 size_t safepointIndices, size_t osiIndices,
278 size_t icEntries, size_t runtimeSize,
279 size_t safepointsSize);
280
281 static void Destroy(JS::GCContext* gcx, IonScript* script);
282
283 void trace(JSTracer* trc);
284 void traceWeak(JSTracer* trc);
285
286 static inline size_t offsetOfInvalidationCount() {
287 return offsetof(IonScript, invalidationCount_);
288 }
289
290 public:
291 JitCode* method() const { return method_; }
292 void setMethod(JitCode* code) {
293 MOZ_ASSERT(!invalidated());
294 method_ = code;
295 }
296 void setOsrPc(jsbytecode* osrPc) { osrPc_ = osrPc; }
297 jsbytecode* osrPc() const { return osrPc_; }
298 void setOsrEntryOffset(uint32_t offset) {
299 MOZ_ASSERT(!osrEntryOffset_);
300 osrEntryOffset_ = offset;
301 }
302 uint32_t osrEntryOffset() const { return osrEntryOffset_; }
303 bool containsCodeAddress(uint8_t* addr) const {
304 return method()->raw() <= addr &&
305 addr <= method()->raw() + method()->instructionsSize();
306 }
307 bool containsReturnAddress(uint8_t* addr) const {
308 // This accounts for an off by one error caused by the return address of a
309 // bailout sitting outside the range of the containing function.
310 return method()->raw() <= addr &&
311 addr <= method()->raw() + method()->instructionsSize();
312 }
313 void setInvalidationEpilogueOffset(uint32_t offset) {
314 MOZ_ASSERT(!invalidateEpilogueOffset_);
315 invalidateEpilogueOffset_ = offset;
316 }
317 uint32_t invalidateEpilogueOffset() const {
318 MOZ_ASSERT(invalidateEpilogueOffset_);
319 return invalidateEpilogueOffset_;
320 }
321 void setInvalidationEpilogueDataOffset(uint32_t offset) {
322 MOZ_ASSERT(!invalidateEpilogueDataOffset_);
323 invalidateEpilogueDataOffset_ = offset;
324 }
325 uint32_t invalidateEpilogueDataOffset() const {
326 MOZ_ASSERT(invalidateEpilogueDataOffset_);
327 return invalidateEpilogueDataOffset_;
328 }
329
330 uint32_t numFixableBailouts() const { return numFixableBailouts_; }
331
332 void incNumFixableBailouts() { numFixableBailouts_++; }
333 void resetNumFixableBailouts() { numFixableBailouts_ = 0; }
334 void incNumUnfixableBailouts() { numUnfixableBailouts_++; }
335
336 bool shouldInvalidate() const {
337 return numFixableBailouts_ >= JitOptions.frequentBailoutThreshold;
338 }
339 bool shouldInvalidateAndDisable() const {
340 return numUnfixableBailouts_ >= JitOptions.frequentBailoutThreshold * 5;
341 }
342
343 LICMState licmState() const { return licmState_; }
344 void setHadLICMBailout() {
345 if (licmState_ == LICMState::NeverBailed) {
346 licmState_ = LICMState::Bailed;
347 }
348 }
349 void noteBaselineFallback() {
350 if (licmState_ == LICMState::Bailed) {
351 licmState_ = LICMState::BailedAndHitFallback;
352 }
353 }
354
355 void setHasProfilingInstrumentation() { hasProfilingInstrumentation_ = true; }
356 void clearHasProfilingInstrumentation() {
357 hasProfilingInstrumentation_ = false;
358 }
359 bool hasProfilingInstrumentation() const {
360 return hasProfilingInstrumentation_;
361 }
362
363 bool purgedICScripts() const { return purgedICScripts_; }
364 void notePurgedICScripts() { purgedICScripts_ = true; }
365
366 size_t sizeOfIncludingThis(mozilla::MallocSizeOf mallocSizeOf) const {
367 return mallocSizeOf(this);
368 }
369 PreBarriered<Value>& getConstant(size_t index) {
370 MOZ_ASSERT(index < numConstants());
371 return constants()[index];
372 }
373 uint32_t localSlotsSize() const { return localSlotsSize_; }
374 uint32_t argumentSlotsSize() const { return argumentSlotsSize_; }
375 uint32_t frameSize() const { return frameSize_; }
376 const SafepointIndex* getSafepointIndex(uint32_t disp) const;
377 const SafepointIndex* getSafepointIndex(uint8_t* retAddr) const {
378 MOZ_ASSERT(containsCodeAddress(retAddr));
379 return getSafepointIndex(retAddr - method()->raw());
380 }
381 const OsiIndex* getOsiIndex(uint32_t disp) const;
382 const OsiIndex* getOsiIndex(uint8_t* retAddr) const;
383
384 IonIC& getICFromIndex(uint32_t index) {
385 MOZ_ASSERT(index < numICs());
386 uint32_t offset = icIndex()[index];
387 return getIC(offset);
388 }
389 inline IonIC& getIC(uint32_t offset) {
390 MOZ_ASSERT(offset < runtimeSize());
391 return *reinterpret_cast<IonIC*>(runtimeData() + offset);
392 }
393 void purgeICs(Zone* zone);
394 void copySnapshots(const SnapshotWriter* writer);
395 void copyRecovers(const RecoverWriter* writer);
396 void copyConstants(const Value* vp);
397 void copySafepointIndices(const CodegenSafepointIndex* si);
398 void copyOsiIndices(const OsiIndex* oi);
399 void copyRuntimeData(const uint8_t* data);
400 void copyICEntries(const uint32_t* icEntries);
401 void copySafepoints(const SafepointWriter* writer);
402
403 bool invalidated() const { return invalidationCount_ != 0; }
404
405 // Invalidate the current compilation.
406 void invalidate(JSContext* cx, JSScript* script, bool resetUses,
407 const char* reason);
408
409 size_t invalidationCount() const { return invalidationCount_; }
410 void incrementInvalidationCount() { invalidationCount_++; }
411 void decrementInvalidationCount(JS::GCContext* gcx) {
412 MOZ_ASSERT(invalidationCount_);
413 invalidationCount_--;
414 if (!invalidationCount_) {
415 Destroy(gcx, this);
416 }
417 }
418 IonCompilationId compilationId() const { return compilationId_; }
419 uint32_t incrOsrPcMismatchCounter() { return ++osrPcMismatchCounter_; }
420 void resetOsrPcMismatchCounter() { osrPcMismatchCounter_ = 0; }
421
422 size_t allocBytes() const { return allocBytes_; }
423
424 static void preWriteBarrier(Zone* zone, IonScript* ionScript);
425
426 #ifdef DEBUG
427 mozilla::HashNumber icHash() const { return icHash_; }
428 void setICHash(mozilla::HashNumber hash) { icHash_ = hash; }
429 #endif
430 };
431
432 // Execution information for a basic block which may persist after the
433 // accompanying IonScript is destroyed, for use during profiling.
434 struct IonBlockCounts {
435 private:
436 uint32_t id_;
437
438 // Approximate bytecode in the outer (not inlined) script this block
439 // was generated from.
440 uint32_t offset_;
441
442 // File and line of the inner script this block was generated from.
443 char* description_;
444
445 // ids for successors of this block.
446 uint32_t numSuccessors_;
447 uint32_t* successors_;
448
449 // Hit count for this block.
450 uint64_t hitCount_;
451
452 // Text information about the code generated for this block.
453 char* code_;
454
455 public:
456 [[nodiscard]] bool init(uint32_t id, uint32_t offset, char* description,
457 uint32_t numSuccessors) {
458 id_ = id;
459 offset_ = offset;
460 description_ = description;
461 numSuccessors_ = numSuccessors;
462 if (numSuccessors) {
463 successors_ = js_pod_calloc<uint32_t>(numSuccessors);
464 if (!successors_) {
465 return false;
466 }
467 }
468 return true;
469 }
470
471 void destroy() {
472 js_free(description_);
473 js_free(successors_);
474 js_free(code_);
475 }
476
477 uint32_t id() const { return id_; }
478
479 uint32_t offset() const { return offset_; }
480
481 const char* description() const { return description_; }
482
483 size_t numSuccessors() const { return numSuccessors_; }
484
485 void setSuccessor(size_t i, uint32_t id) {
486 MOZ_ASSERT(i < numSuccessors_);
487 successors_[i] = id;
488 }
489
490 uint32_t successor(size_t i) const {
491 MOZ_ASSERT(i < numSuccessors_);
492 return successors_[i];
493 }
494
495 uint64_t* addressOfHitCount() { return &hitCount_; }
496
497 uint64_t hitCount() const { return hitCount_; }
498
499 void setCode(const char* code) {
500 char* ncode = js_pod_malloc<char>(strlen(code) + 1);
501 if (ncode) {
502 strcpy(ncode, code);
503 code_ = ncode;
504 }
505 }
506
507 const char* code() const { return code_; }
508
509 size_t sizeOfExcludingThis(mozilla::MallocSizeOf mallocSizeOf) const {
510 return mallocSizeOf(description_) + mallocSizeOf(successors_) +
511 mallocSizeOf(code_);
512 }
513 };
514
515 // Execution information for a compiled script which may persist after the
516 // IonScript is destroyed, for use during profiling.
517 struct IonScriptCounts {
518 private:
519 // Any previous invalidated compilation(s) for the script.
520 IonScriptCounts* previous_ = nullptr;
521
522 // Information about basic blocks in this script.
523 size_t numBlocks_ = 0;
524 IonBlockCounts* blocks_ = nullptr;
525
526 public:
527 IonScriptCounts() = default;
528
529 ~IonScriptCounts() {
530 for (size_t i = 0; i < numBlocks_; i++) {
531 blocks_[i].destroy();
532 }
533 js_free(blocks_);
534 // The list can be long in some corner cases (bug 1140084), so
535 // unroll the recursion.
536 IonScriptCounts* victims = previous_;
537 while (victims) {
538 IonScriptCounts* victim = victims;
539 victims = victim->previous_;
540 victim->previous_ = nullptr;
541 js_delete(victim);
542 }
543 }
544
545 [[nodiscard]] bool init(size_t numBlocks) {
546 blocks_ = js_pod_calloc<IonBlockCounts>(numBlocks);
547 if (!blocks_) {
548 return false;
549 }
550
551 numBlocks_ = numBlocks;
552 return true;
553 }
554
555 size_t numBlocks() const { return numBlocks_; }
556
557 IonBlockCounts& block(size_t i) {
558 MOZ_ASSERT(i < numBlocks_);
559 return blocks_[i];
560 }
561
562 void setPrevious(IonScriptCounts* previous) { previous_ = previous; }
563
564 IonScriptCounts* previous() const { return previous_; }
565
566 size_t sizeOfIncludingThis(mozilla::MallocSizeOf mallocSizeOf) const {
567 size_t size = 0;
568 auto currCounts = this;
569 do {
570 size += currCounts->sizeOfOneIncludingThis(mallocSizeOf);
571 currCounts = currCounts->previous_;
572 } while (currCounts);
573 return size;
574 }
575
576 size_t sizeOfOneIncludingThis(mozilla::MallocSizeOf mallocSizeOf) const {
577 size_t size = mallocSizeOf(this) + mallocSizeOf(blocks_);
578 for (size_t i = 0; i < numBlocks_; i++) {
579 blocks_[i].sizeOfExcludingThis(mallocSizeOf);
580 }
581 return size;
582 }
583 };
584
585 } // namespace jit
586 } // namespace js
587
588 namespace JS {
589
590 template <>
591 struct DeletePolicy<js::jit::IonScript> {
592 explicit DeletePolicy(JSRuntime* rt) : rt_(rt) {}
593 void operator()(const js::jit::IonScript* script);
594
595 private:
596 JSRuntime* rt_;
597 };
598
599 } // namespace JS
600
601 #endif /* jit_IonScript_h */