Debian Codegraph @

js/src/jit/JitZone.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_JitZone_h
8 #define jit_JitZone_h
9
10 #include "mozilla/Assertions.h"
11 #include "mozilla/EnumeratedArray.h"
12 #include "mozilla/HashFunctions.h"
13 #include "mozilla/LinkedList.h"
14 #include "mozilla/Maybe.h"
15 #include "mozilla/MemoryReporting.h"
16
17 #include <stddef.h>
18 #include <stdint.h>
19 #include <utility>
20
21 #include "gc/Barrier.h"
22 #include "gc/Marking.h"
23 #include "jit/CacheIRAOT.h"
24 #include "jit/ExecutableAllocator.h"
25 #include "jit/ICStubSpace.h"
26 #include "jit/Invalidation.h"
27 #include "jit/JitScript.h"
28 #include "js/AllocPolicy.h"
29 #include "js/GCHashTable.h"
30 #include "js/HashTable.h"
31 #include "js/TracingAPI.h"
32 #include "js/TypeDecls.h"
33 #include "js/UniquePtr.h"
34 #include "js/Utility.h"
35 #include "threading/ProtectedData.h"
36
37 namespace JS {
38 struct CodeSizes;
39 }
40
41 namespace js {
42 namespace jit {
43
44 enum class CacheKind : uint8_t;
45 class CacheIRStubInfo;
46 class JitCode;
47 class JitScript;
48
49 enum class ICStubEngine : uint8_t {
50 // Baseline IC, see BaselineIC.h.
51 Baseline = 0,
52
53 // Ion IC, see IonIC.h.
54 IonIC
55 };
56
57 struct CacheIRStubKey : public DefaultHasher<CacheIRStubKey> {
58 struct Lookup {
59 CacheKind kind;
60 ICStubEngine engine;
61 const uint8_t* code;
62 uint32_t length;
63
64 Lookup(CacheKind kind, ICStubEngine engine, const uint8_t* code,
65 uint32_t length)
66 : kind(kind), engine(engine), code(code), length(length) {}
67 };
68
69 static HashNumber hash(const Lookup& l);
70 static bool match(const CacheIRStubKey& entry, const Lookup& l);
71
72 UniquePtr<CacheIRStubInfo, JS::FreePolicy> stubInfo;
73
74 explicit CacheIRStubKey(CacheIRStubInfo* info) : stubInfo(info) {}
75 CacheIRStubKey(CacheIRStubKey&& other)
76 : stubInfo(std::move(other.stubInfo)) {}
77
78 void operator=(CacheIRStubKey&& other) {
79 stubInfo = std::move(other.stubInfo);
80 }
81 };
82
83 struct BaselineCacheIRStubCodeMapGCPolicy {
84 static bool traceWeak(JSTracer* trc, CacheIRStubKey*,
85 WeakHeapPtr<JitCode*>* value) {
86 return TraceWeakEdge(trc, value, "traceWeak");
87 }
88 };
89
90 enum JitScriptFilter : bool { SkipDyingScripts, IncludeDyingScripts };
91
92 class JitZone {
93 public:
94 enum class StubKind : uint32_t {
95 StringConcat = 0,
96 RegExpMatcher,
97 RegExpSearcher,
98 RegExpExecMatch,
99 RegExpExecTest,
100 Count
101 };
102 template <typename Code>
103 using Stubs =
104 mozilla::EnumeratedArray<StubKind, Code, size_t(StubKind::Count)>;
105
106 private:
107 // Allocated space for CacheIR stubs.
108 ICStubSpace stubSpace_;
109
110 // Set of CacheIRStubInfo instances used by Ion stubs in this Zone.
111 using IonCacheIRStubInfoSet =
112 HashSet<CacheIRStubKey, CacheIRStubKey, SystemAllocPolicy>;
113 IonCacheIRStubInfoSet ionCacheIRStubInfoSet_;
114
115 // Map CacheIRStubKey to shared JitCode objects.
116 using BaselineCacheIRStubCodeMap =
117 GCHashMap<CacheIRStubKey, WeakHeapPtr<JitCode*>, CacheIRStubKey,
118 SystemAllocPolicy, BaselineCacheIRStubCodeMapGCPolicy>;
119 BaselineCacheIRStubCodeMap baselineCacheIRStubCodes_;
120
121 // Executable allocator for all code except wasm code.
122 MainThreadData<ExecutableAllocator> execAlloc_;
123
124 // HashMap that maps scripts to compilations inlining those scripts.
125 using InlinedScriptMap =
126 GCHashMap<WeakHeapPtr<BaseScript*>, RecompileInfoVector,
127 StableCellHasher<WeakHeapPtr<BaseScript*>>, SystemAllocPolicy>;
128 InlinedScriptMap inlinedCompilations_;
129
130 mozilla::LinkedList<JitScript> jitScripts_;
131
132 // The following two fields are a pair of associated scripts. If they are
133 // non-null, the child has been inlined into the parent, and we have bailed
134 // out due to a MonomorphicInlinedStubFolding bailout. If it wasn't
135 // trial-inlined, we need to track for the parent if we attach a new case to
136 // the corresponding folded stub which belongs to the child.
137 WeakHeapPtr<JSScript*> lastStubFoldingBailoutChild_;
138 WeakHeapPtr<JSScript*> lastStubFoldingBailoutParent_;
139
140 // The JitZone stores stubs to concatenate strings inline and perform RegExp
141 // calls inline. These bake in zone specific pointers and can't be stored in
142 // JitRuntime. They also are dependent on the value of 'initialStringHeap' and
143 // must be flushed when its value changes.
144 //
145 // These are weak pointers. Ion compilations store strong references to stubs
146 // they depend on in WarpSnapshot.
147 Stubs<WeakHeapPtr<JitCode*>> stubs_;
148
149 mozilla::Maybe<IonCompilationId> currentCompilationId_;
150 bool keepJitScripts_ = false;
151
152 // Whether AOT IC loading failed due to OOM; if so, disable
153 // enforcing-AOT checks.
154 bool incompleteAOTICs_ = false;
155
156 gc::Heap initialStringHeap = gc::Heap::Tenured;
157
158 JitCode* generateStringConcatStub(JSContext* cx);
159 JitCode* generateRegExpMatcherStub(JSContext* cx);
160 JitCode* generateRegExpSearcherStub(JSContext* cx);
161 JitCode* generateRegExpExecMatchStub(JSContext* cx);
162 JitCode* generateRegExpExecTestStub(JSContext* cx);
163
164 public:
165 explicit JitZone(JSContext* cx, bool zoneHasNurseryStrings) {
166 setStringsCanBeInNursery(zoneHasNurseryStrings);
167 #ifdef ENABLE_JS_AOT_ICS
168 js::jit::FillAOTICs(cx, this);
169 #endif
170 }
171 ~JitZone() {
172 MOZ_ASSERT(jitScripts_.isEmpty());
173 MOZ_ASSERT(!keepJitScripts_);
174 }
175
176 void traceWeak(JSTracer* trc, Zone* zone);
177
178 void addSizeOfIncludingThis(mozilla::MallocSizeOf mallocSizeOf,
179 JS::CodeSizes* code, size_t* jitZone,
180 size_t* cacheIRStubs) const;
181
182 ICStubSpace* stubSpace() { return &stubSpace_; }
183
184 JitCode* getBaselineCacheIRStubCode(const CacheIRStubKey::Lookup& key,
185 CacheIRStubInfo** stubInfo) {
186 auto p = baselineCacheIRStubCodes_.lookup(key);
187 if (p) {
188 *stubInfo = p->key().stubInfo.get();
189 return p->value();
190 }
191 *stubInfo = nullptr;
192 return nullptr;
193 }
194 [[nodiscard]] bool putBaselineCacheIRStubCode(
195 const CacheIRStubKey::Lookup& lookup, CacheIRStubKey& key,
196 JitCode* stubCode) {
197 auto p = baselineCacheIRStubCodes_.lookupForAdd(lookup);
198 MOZ_ASSERT(!p);
199 return baselineCacheIRStubCodes_.add(p, std::move(key), stubCode);
200 }
201
202 CacheIRStubInfo* getIonCacheIRStubInfo(const CacheIRStubKey::Lookup& key) {
203 IonCacheIRStubInfoSet::Ptr p = ionCacheIRStubInfoSet_.lookup(key);
204 return p ? p->stubInfo.get() : nullptr;
205 }
206 [[nodiscard]] bool putIonCacheIRStubInfo(const CacheIRStubKey::Lookup& lookup,
207 CacheIRStubKey& key) {
208 IonCacheIRStubInfoSet::AddPtr p =
209 ionCacheIRStubInfoSet_.lookupForAdd(lookup);
210 MOZ_ASSERT(!p);
211 return ionCacheIRStubInfoSet_.add(p, std::move(key));
212 }
213 void purgeIonCacheIRStubInfo() { ionCacheIRStubInfoSet_.clearAndCompact(); }
214
215 ExecutableAllocator& execAlloc() { return execAlloc_.ref(); }
216 const ExecutableAllocator& execAlloc() const { return execAlloc_.ref(); }
217
218 [[nodiscard]] bool addInlinedCompilation(const RecompileInfo& info,
219 JSScript* inlined);
220
221 RecompileInfoVector* maybeInlinedCompilations(JSScript* inlined) {
222 auto p = inlinedCompilations_.lookup(inlined);
223 return p ? &p->value() : nullptr;
224 }
225
226 void removeInlinedCompilations(JSScript* inlined) {
227 inlinedCompilations_.remove(inlined);
228 }
229
230 void noteStubFoldingBailout(JSScript* child, JSScript* parent) {
231 lastStubFoldingBailoutChild_ = child;
232 lastStubFoldingBailoutParent_ = parent;
233 }
234 bool hasStubFoldingBailoutData(JSScript* child) const {
235 return lastStubFoldingBailoutChild_ &&
236 lastStubFoldingBailoutChild_.get() == child &&
237 lastStubFoldingBailoutParent_;
238 }
239 JSScript* stubFoldingBailoutParent() const {
240 MOZ_ASSERT(lastStubFoldingBailoutChild_);
241 return lastStubFoldingBailoutParent_.get();
242 }
243 void clearStubFoldingBailoutData() {
244 lastStubFoldingBailoutChild_ = nullptr;
245 lastStubFoldingBailoutParent_ = nullptr;
246 }
247
248 void registerJitScript(JitScript* script) { jitScripts_.insertBack(script); }
249
250 // Iterate over all JitScripts in this zone calling |f| on each, allowing |f|
251 // to remove the script. The template parameter |filter| controls whether to
252 // include dying JitScripts during GC sweeping. Be careful when using this not
253 // to let GC things reachable from the JitScript escape - they may be gray.
254 template <JitScriptFilter filter = SkipDyingScripts, typename F>
255 void forEachJitScript(F&& f) {
256 JitScript* script = jitScripts_.getFirst();
257 while (script) {
258 JitScript* next = script->getNext();
259 if (filter == IncludeDyingScripts ||
260 !gc::IsAboutToBeFinalizedUnbarriered(script->owningScript())) {
261 f(script);
262 }
263 script = next;
264 }
265 }
266
267 // Like forEachJitScript above, but abort if |f| returns false.
268 template <JitScriptFilter filter = SkipDyingScripts, typename F>
269 bool forEachJitScriptFallible(F&& f) {
270 JitScript* script = jitScripts_.getFirst();
271 while (script) {
272 JitScript* next = script->getNext();
273 if (filter == IncludeDyingScripts ||
274 !gc::IsAboutToBeFinalizedUnbarriered(script->owningScript())) {
275 if (!f(script)) {
276 return false;
277 }
278 }
279 script = next;
280 }
281 return true;
282 }
283
284 bool keepJitScripts() const { return keepJitScripts_; }
285 void setKeepJitScripts(bool keep) { keepJitScripts_ = keep; }
286
287 mozilla::Maybe<IonCompilationId> currentCompilationId() const {
288 return currentCompilationId_;
289 }
290 mozilla::Maybe<IonCompilationId>& currentCompilationIdRef() {
291 return currentCompilationId_;
292 }
293
294 void setIncompleteAOTICs() { incompleteAOTICs_ = true; }
295 bool isIncompleteAOTICs() const { return incompleteAOTICs_; }
296
297 void traceWeak(JSTracer* trc, JS::Realm* realm);
298
299 void discardStubs() {
300 for (WeakHeapPtr<JitCode*>& stubRef : stubs_) {
301 stubRef = nullptr;
302 }
303 }
304
305 bool hasStubs() const {
306 for (const WeakHeapPtr<JitCode*>& stubRef : stubs_) {
307 if (stubRef) {
308 return true;
309 }
310 }
311 return false;
312 }
313
314 void setStringsCanBeInNursery(bool allow) {
315 MOZ_ASSERT(!hasStubs());
316 initialStringHeap = allow ? gc::Heap::Default : gc::Heap::Tenured;
317 }
318
319 [[nodiscard]] JitCode* ensureStubExists(JSContext* cx, StubKind kind) {
320 if (JitCode* code = stubs_[kind]) {
321 return code;
322 }
323 switch (kind) {
324 case StubKind::StringConcat:
325 stubs_[kind] = generateStringConcatStub(cx);
326 break;
327 case StubKind::RegExpMatcher:
328 stubs_[kind] = generateRegExpMatcherStub(cx);
329 break;
330 case StubKind::RegExpSearcher:
331 stubs_[kind] = generateRegExpSearcherStub(cx);
332 break;
333 case StubKind::RegExpExecMatch:
334 stubs_[kind] = generateRegExpExecMatchStub(cx);
335 break;
336 case StubKind::RegExpExecTest:
337 stubs_[kind] = generateRegExpExecTestStub(cx);
338 break;
339 case StubKind::Count:
340 MOZ_CRASH("Invalid kind");
341 }
342 return stubs_[kind];
343 }
344
345 static constexpr size_t offsetOfStringConcatStub() {
346 return offsetof(JitZone, stubs_) +
347 size_t(StubKind::StringConcat) * sizeof(uintptr_t);
348 }
349 static constexpr size_t offsetOfRegExpMatcherStub() {
350 return offsetof(JitZone, stubs_) +
351 size_t(StubKind::RegExpMatcher) * sizeof(uintptr_t);
352 }
353 static constexpr size_t offsetOfRegExpSearcherStub() {
354 return offsetof(JitZone, stubs_) +
355 size_t(StubKind::RegExpSearcher) * sizeof(uintptr_t);
356 }
357 static constexpr size_t offsetOfRegExpExecMatchStub() {
358 return offsetof(JitZone, stubs_) +
359 size_t(StubKind::RegExpExecMatch) * sizeof(uintptr_t);
360 }
361 static constexpr size_t offsetOfRegExpExecTestStub() {
362 return offsetof(JitZone, stubs_) +
363 size_t(StubKind::RegExpExecTest) * sizeof(uintptr_t);
364 }
365 };
366
367 } // namespace jit
368 } // namespace js
369
370 #endif /* jit_JitZone_h */