Debian Codegraph @

js/src/jit/CompilationDependencyTracker.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_CompilationDependencyTracker_h
8 #define jit_CompilationDependencyTracker_h
9
10 #include "mozilla/Vector.h"
11
12 #include "jstypes.h"
13 #include "NamespaceImports.h"
14
15 struct JSContext;
16
17 namespace js::jit {
18 class MIRGenerator;
19
20 struct CompilationDependency {
21 enum class Type {
22 GetIterator,
23 ArraySpecies,
24 RegExpPrototype,
25 StringPrototypeSymbols,
26 EmulatesUndefined,
27 ArrayExceedsInt32Length,
28 Limit
29 };
30
31 Type type;
32
33 CompilationDependency(Type type) : type(type) {}
34 virtual bool operator==(const CompilationDependency& other) const = 0;
35
36 // Return true iff this dependency still holds. May only be called on main
37 // thread.
38 virtual bool checkDependency(JSContext* cx) = 0;
39 [[nodiscard]] virtual bool registerDependency(JSContext* cx,
40 HandleScript script) = 0;
41
42 virtual UniquePtr<CompilationDependency> clone() const = 0;
43 virtual ~CompilationDependency() = default;
44 };
45
46 // For a given Warp compilation keep track of the dependencies this compilation
47 // is depending on. These dependencies will be checked on main thread during
48 // link time, causing abandonment of a compilation if they no longer hold.
49 struct CompilationDependencyTracker {
50 mozilla::Vector<UniquePtr<CompilationDependency>, 8, SystemAllocPolicy>
51 dependencies;
52
53 [[nodiscard]] bool addDependency(const CompilationDependency& dep) {
54 // Ensure we don't add duplicates. We expect this list to be short,
55 // and so iteration is preferred over a more costly hashset.
56 MOZ_ASSERT(dependencies.length() <= 32);
57 for (auto& existingDep : dependencies) {
58 if (dep == *existingDep) {
59 return true;
60 }
61 }
62
63 auto clone = dep.clone();
64 if (!clone) {
65 return false;
66 }
67
68 return dependencies.append(std::move(clone));
69 }
70
71 // Check all dependencies. May only be checked on main thread.
72 bool checkDependencies(JSContext* cx) {
73 for (auto& dep : dependencies) {
74 if (!dep->checkDependency(cx)) {
75 return false;
76 }
77 }
78 return true;
79 }
80
81 void reset() { dependencies.clearAndFree(); }
82 };
83
84 } // namespace js::jit
85 #endif /* jit_CompilationDependencyTracker_h */