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 #include "src/maglev/maglev-compilation-info.h"
6
7 #include "src/compiler/compilation-dependencies.h"
8 #include "src/compiler/js-heap-broker.h"
9 #include "src/execution/isolate.h"
10 #include "src/flags/flags.h"
11 #include "src/handles/persistent-handles.h"
12 #include "src/maglev/maglev-compilation-unit.h"
13 #include "src/maglev/maglev-compiler.h"
14 #include "src/maglev/maglev-concurrent-dispatcher.h"
15 #include "src/maglev/maglev-graph-labeller.h"
16 #include "src/objects/js-function-inl.h"
17 #include "src/utils/identity-map.h"
18 #include "src/utils/locked-queue-inl.h"
19
20 namespace v8 {
21 namespace internal {
22 namespace maglev {
23
24 namespace {
25
26 constexpr char kMaglevZoneName[] = "maglev-compilation-job-zone";
27
28 class V8_NODISCARD MaglevCompilationHandleScope final {
29 public:
MaglevCompilationHandleScope(Isolate * isolate,maglev::MaglevCompilationInfo * info)30 MaglevCompilationHandleScope(Isolate* isolate,
31 maglev::MaglevCompilationInfo* info)
32 : info_(info),
33 persistent_(isolate),
34 exported_info_(info),
35 canonical_(isolate, &exported_info_) {
36 info->ReopenHandlesInNewHandleScope(isolate);
37 }
38
~MaglevCompilationHandleScope()39 ~MaglevCompilationHandleScope() {
40 info_->set_persistent_handles(persistent_.Detach());
41 }
42
43 private:
44 maglev::MaglevCompilationInfo* const info_;
45 PersistentHandlesScope persistent_;
46 ExportedMaglevCompilationInfo exported_info_;
47 CanonicalHandleScopeForMaglev canonical_;
48 };
49
50 } // namespace
51
MaglevCompilationInfo(Isolate * isolate,Handle<JSFunction> function)52 MaglevCompilationInfo::MaglevCompilationInfo(Isolate* isolate,
53 Handle<JSFunction> function)
54 : zone_(isolate->allocator(), kMaglevZoneName),
55 isolate_(isolate),
56 broker_(new compiler::JSHeapBroker(
57 isolate, zone(), FLAG_trace_heap_broker, CodeKind::MAGLEV)),
58 shared_(function->shared(), isolate),
59 function_(function)
60 #define V(Name) , Name##_(FLAG_##Name)
61 MAGLEV_COMPILATION_FLAG_LIST(V)
62 #undef V
63 {
64 DCHECK(FLAG_maglev);
65
66 MaglevCompilationHandleScope compilation(isolate, this);
67
68 compiler::CompilationDependencies* deps =
69 zone()->New<compiler::CompilationDependencies>(broker(), zone());
70 USE(deps); // The deps register themselves in the heap broker.
71
72 // Heap broker initialization may already use IsPendingAllocation.
73 isolate->heap()->PublishPendingAllocations();
74
75 broker()->SetTargetNativeContextRef(
76 handle(function->native_context(), isolate));
77 broker()->InitializeAndStartSerializing();
78 broker()->StopSerializing();
79
80 // Serialization may have allocated.
81 isolate->heap()->PublishPendingAllocations();
82
83 toplevel_compilation_unit_ =
84 MaglevCompilationUnit::New(zone(), this, function);
85 }
86
87 MaglevCompilationInfo::~MaglevCompilationInfo() = default;
88
set_graph_labeller(MaglevGraphLabeller * graph_labeller)89 void MaglevCompilationInfo::set_graph_labeller(
90 MaglevGraphLabeller* graph_labeller) {
91 graph_labeller_.reset(graph_labeller);
92 }
93
ReopenHandlesInNewHandleScope(Isolate * isolate)94 void MaglevCompilationInfo::ReopenHandlesInNewHandleScope(Isolate* isolate) {
95 DCHECK(!shared_.is_null());
96 shared_ = handle(*shared_, isolate);
97 DCHECK(!function_.is_null());
98 function_ = handle(*function_, isolate);
99 }
100
set_persistent_handles(std::unique_ptr<PersistentHandles> && persistent_handles)101 void MaglevCompilationInfo::set_persistent_handles(
102 std::unique_ptr<PersistentHandles>&& persistent_handles) {
103 DCHECK_NULL(ph_);
104 ph_ = std::move(persistent_handles);
105 DCHECK_NOT_NULL(ph_);
106 }
107
108 std::unique_ptr<PersistentHandles>
DetachPersistentHandles()109 MaglevCompilationInfo::DetachPersistentHandles() {
110 DCHECK_NOT_NULL(ph_);
111 return std::move(ph_);
112 }
113
set_canonical_handles(std::unique_ptr<CanonicalHandlesMap> && canonical_handles)114 void MaglevCompilationInfo::set_canonical_handles(
115 std::unique_ptr<CanonicalHandlesMap>&& canonical_handles) {
116 DCHECK_NULL(canonical_handles_);
117 canonical_handles_ = std::move(canonical_handles);
118 DCHECK_NOT_NULL(canonical_handles_);
119 }
120
121 std::unique_ptr<CanonicalHandlesMap>
DetachCanonicalHandles()122 MaglevCompilationInfo::DetachCanonicalHandles() {
123 DCHECK_NOT_NULL(canonical_handles_);
124 return std::move(canonical_handles_);
125 }
126
127 } // namespace maglev
128 } // namespace internal
129 } // namespace v8
130