v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
maglev-compilation-info.h
Go to the documentation of this file.
1// Copyright 2022 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef V8_MAGLEV_MAGLEV_COMPILATION_INFO_H_
6#define V8_MAGLEV_MAGLEV_COMPILATION_INFO_H_
7
8#include <memory>
9#include <optional>
10
11#include "src/handles/handles.h"
13#include "src/utils/utils.h"
14#include "src/zone/zone.h"
15
16namespace v8 {
17
18namespace base {
19class DefaultAllocationPolicy;
20}
21
22namespace internal {
23
24class Isolate;
25class PersistentHandles;
26class SharedFunctionInfo;
27class TranslationArrayBuilder;
28
29namespace compiler {
30class JSHeapBroker;
31}
32
33namespace maglev {
34
35class MaglevCompilationUnit;
36class MaglevGraphLabeller;
37class MaglevCodeGenerator;
38
39// A list of v8_flag values copied into the MaglevCompilationInfo for
40// guaranteed {immutable,threadsafe} access.
41#define MAGLEV_COMPILATION_FLAG_LIST(V) \
42 V(code_comments) \
43 V(maglev) \
44 V(print_maglev_code) \
45 V(print_maglev_graph) \
46 V(trace_maglev_regalloc)
47
49 public:
50 static std::unique_ptr<MaglevCompilationInfo> NewForTurboshaft(
52 IndirectHandle<JSFunction> function, BytecodeOffset osr_offset,
54 // Doesn't use make_unique due to the private ctor.
55 return std::unique_ptr<MaglevCompilationInfo>(new MaglevCompilationInfo(
56 isolate, function, osr_offset, broker, specialize_to_function_context,
57 /*for_turboshaft_frontend*/ true));
58 }
59 static std::unique_ptr<MaglevCompilationInfo> New(
60 Isolate* isolate, IndirectHandle<JSFunction> function,
61 BytecodeOffset osr_offset) {
62 // Doesn't use make_unique due to the private ctor.
63 return std::unique_ptr<MaglevCompilationInfo>(
64 new MaglevCompilationInfo(isolate, function, osr_offset));
65 }
67
68 Zone* zone() { return &zone_; }
77 bool toplevel_is_osr() const { return osr_offset_ != BytecodeOffset::None(); }
79 DCHECK(code_.is_null());
80 code_ = code;
81 }
83
84 bool is_turbolev() const { return is_turbolev_; }
85
86 bool has_graph_labeller() const { return !!graph_labeller_; }
92
93#ifdef V8_ENABLE_MAGLEV
94 void set_code_generator(std::unique_ptr<MaglevCodeGenerator> code_generator);
95 MaglevCodeGenerator* code_generator() const { return code_generator_.get(); }
96#endif
97
98 // Flag accessors (for thread-safe access to global flags).
99 // TODO(v8:7700): Consider caching these.
100#define V(Name) \
101 bool Name() const { return Name##_; }
103#undef V
105
109
110 // Must be called from within a MaglevCompilationHandleScope. Transfers owned
111 // handles (e.g. shared_, function_) to the new scope.
113
114 // Persistent and canonical handles are passed back and forth between the
115 // Isolate, this info, and the LocalIsolate.
117 std::unique_ptr<PersistentHandles>&& persistent_handles);
118 std::unique_ptr<PersistentHandles> DetachPersistentHandles();
120 std::unique_ptr<CanonicalHandlesMap>&& canonical_handles);
121 std::unique_ptr<CanonicalHandlesMap> DetachCanonicalHandles();
122
123 bool is_detached();
124
131
132 private:
134 Isolate* isolate, IndirectHandle<JSFunction> function,
135 BytecodeOffset osr_offset,
136 std::optional<compiler::JSHeapBroker*> broker = std::nullopt,
137 std::optional<bool> specialize_to_function_context = std::nullopt,
138 bool for_turboshaft_frontend = false);
139
140 // Storing the raw pointer to the CanonicalHandlesMap is generally not safe.
141 // Use DetachCanonicalHandles() to transfer ownership instead.
142 // We explicitly allow the JSHeapBroker to store the raw pointer as it is
143 // guaranteed that the MaglevCompilationInfo's lifetime exceeds the lifetime
144 // of the broker.
147
150 // Must be initialized late since it requires an initialized heap broker.
155
156 // True if this MaglevCompilationInfo owns its broker and false otherwise. In
157 // particular, when used as Turboshaft front-end, this will use Turboshaft's
158 // broker.
159 bool owns_broker_ = true;
160
161 // When this MaglevCompilationInfo is created to be used in Turboshaft's
162 // frontend, {for_turboshaft_frontend_} is true.
163 bool is_turbolev_ = false;
164
165 // True if some inlinees were skipped due to total size constraints.
167
168 std::unique_ptr<MaglevGraphLabeller> graph_labeller_;
169
170#ifdef V8_ENABLE_MAGLEV
171 // Produced off-thread during ExecuteJobImpl.
172 std::unique_ptr<MaglevCodeGenerator> code_generator_;
173#endif
174
175#define V(Name) const bool Name##_;
177#undef V
179
180 // If enabled, the generated code can rely on the function context to be a
181 // constant (known at compile-time). This opens new optimization
182 // opportunities, but prevents code sharing between different function
183 // contexts.
185
186 // 1) PersistentHandles created via PersistentHandlesScope inside of
187 // CompilationHandleScope.
188 // 2) Owned by MaglevCompilationInfo.
189 // 3) Owned by the broker's LocalHeap when entering the LocalHeapScope.
190 // 4) Back to MaglevCompilationInfo when exiting the LocalHeapScope.
191 //
192 // TODO(jgruber,v8:7700): Update this comment:
193 //
194 // In normal execution it gets destroyed when PipelineData gets destroyed.
195 // There is a special case in GenerateCodeForTesting where the JSHeapBroker
196 // will not be retired in that same method. In this case, we need to re-attach
197 // the PersistentHandles container to the JSHeapBroker.
198 std::unique_ptr<PersistentHandles> ph_;
199
200 // Canonical handles follow the same path as described by the persistent
201 // handles above. The only difference is that is created in the
202 // CanonicalHandleScope(i.e step 1) is different).
203 std::unique_ptr<CanonicalHandlesMap> canonical_handles_;
204};
205
206} // namespace maglev
207} // namespace internal
208} // namespace v8
209
210#endif // V8_MAGLEV_MAGLEV_COMPILATION_INFO_H_
static constexpr BytecodeOffset None()
Definition utils.h:675
static std::unique_ptr< MaglevCompilationInfo > NewForTurboshaft(Isolate *isolate, compiler::JSHeapBroker *broker, IndirectHandle< JSFunction > function, BytecodeOffset osr_offset, bool specialize_to_function_context)
static std::unique_ptr< MaglevCompilationInfo > New(Isolate *isolate, IndirectHandle< JSFunction > function, BytecodeOffset osr_offset)
void set_graph_labeller(MaglevGraphLabeller *graph_labeller)
std::unique_ptr< MaglevGraphLabeller > graph_labeller_
void set_persistent_handles(std::unique_ptr< PersistentHandles > &&persistent_handles)
void set_canonical_handles(std::unique_ptr< CanonicalHandlesMap > &&canonical_handles)
std::unique_ptr< CanonicalHandlesMap > canonical_handles_
std::unique_ptr< PersistentHandles > DetachPersistentHandles()
IndirectHandle< JSFunction > toplevel_function() const
std::unique_ptr< CanonicalHandlesMap > DetachCanonicalHandles()
MaglevCompilationUnit * toplevel_compilation_unit() const
MaglevCompilationInfo(Isolate *isolate, IndirectHandle< JSFunction > function, BytecodeOffset osr_offset, std::optional< compiler::JSHeapBroker * > broker=std::nullopt, std::optional< bool > specialize_to_function_context=std::nullopt, bool for_turboshaft_frontend=false)
Handle< Code > code
#define MAGLEV_COMPILATION_FLAG_LIST(V)
#define DCHECK(condition)
Definition logging.h:482