forked from v8/v8
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbaseline-batch-compiler.cc
More file actions
171 lines (149 loc) · 5.71 KB
/
baseline-batch-compiler.cc
File metadata and controls
171 lines (149 loc) · 5.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// Copyright 2021 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "src/baseline/baseline-batch-compiler.h"
// TODO(v8:11421): Remove #if once baseline compiler is ported to other
// architectures.
#include "src/flags/flags.h"
#if ENABLE_SPARKPLUG
#include "src/baseline/baseline-compiler.h"
#include "src/codegen/compiler.h"
#include "src/execution/isolate.h"
#include "src/handles/global-handles-inl.h"
#include "src/heap/factory-inl.h"
#include "src/heap/heap-inl.h"
#include "src/objects/fixed-array-inl.h"
#include "src/objects/js-function-inl.h"
namespace v8 {
namespace internal {
namespace baseline {
BaselineBatchCompiler::BaselineBatchCompiler(Isolate* isolate)
: isolate_(isolate),
compilation_queue_(Handle<WeakFixedArray>::null()),
last_index_(0),
estimated_instruction_size_(0),
enabled_(true) {}
BaselineBatchCompiler::~BaselineBatchCompiler() {
if (!compilation_queue_.is_null()) {
GlobalHandles::Destroy(compilation_queue_.location());
compilation_queue_ = Handle<WeakFixedArray>::null();
}
}
bool BaselineBatchCompiler::EnqueueFunction(Handle<JSFunction> function) {
Handle<SharedFunctionInfo> shared(function->shared(), isolate_);
// Early return if the function is compiled with baseline already or it is not
// suitable for baseline compilation.
if (shared->HasBaselineCode()) return true;
if (!CanCompileWithBaseline(isolate_, *shared)) return false;
// Immediately compile the function if batch compilation is disabled.
if (!is_enabled()) {
IsCompiledScope is_compiled_scope(
function->shared().is_compiled_scope(isolate_));
return Compiler::CompileBaseline(
isolate_, function, Compiler::CLEAR_EXCEPTION, &is_compiled_scope);
}
int estimated_size;
{
DisallowHeapAllocation no_gc;
estimated_size = BaselineCompiler::EstimateInstructionSize(
shared->GetBytecodeArray(isolate_));
}
estimated_instruction_size_ += estimated_size;
if (FLAG_trace_baseline_batch_compilation) {
CodeTracer::Scope trace_scope(isolate_->GetCodeTracer());
PrintF(trace_scope.file(),
"[Baseline batch compilation] Enqueued function ");
function->PrintName(trace_scope.file());
PrintF(trace_scope.file(),
" with estimated size %d (current budget: %d/%d)\n", estimated_size,
estimated_instruction_size_,
FLAG_baseline_batch_compilation_threshold);
}
if (ShouldCompileBatch()) {
if (FLAG_trace_baseline_batch_compilation) {
CodeTracer::Scope trace_scope(isolate_->GetCodeTracer());
PrintF(trace_scope.file(),
"[Baseline batch compilation] Compiling current batch of %d "
"functions\n",
(last_index_ + 1));
}
CompileBatch(function);
return true;
}
EnsureQueueCapacity();
compilation_queue_->Set(last_index_++, HeapObjectReference::Weak(*shared));
return false;
}
void BaselineBatchCompiler::EnsureQueueCapacity() {
if (compilation_queue_.is_null()) {
compilation_queue_ = isolate_->global_handles()->Create(
*isolate_->factory()->NewWeakFixedArray(kInitialQueueSize,
AllocationType::kOld));
return;
}
if (last_index_ >= compilation_queue_->length()) {
Handle<WeakFixedArray> new_queue =
isolate_->factory()->CopyWeakFixedArrayAndGrow(compilation_queue_,
last_index_);
GlobalHandles::Destroy(compilation_queue_.location());
compilation_queue_ = isolate_->global_handles()->Create(*new_queue);
}
}
void BaselineBatchCompiler::CompileBatch(Handle<JSFunction> function) {
CodePageCollectionMemoryModificationScope batch_allocation(isolate_->heap());
{
IsCompiledScope is_compiled_scope(
function->shared().is_compiled_scope(isolate_));
Compiler::CompileBaseline(isolate_, function, Compiler::CLEAR_EXCEPTION,
&is_compiled_scope);
}
for (int i = 0; i < last_index_; i++) {
MaybeObject maybe_sfi = compilation_queue_->Get(i);
MaybeCompileFunction(maybe_sfi);
compilation_queue_->Set(i, HeapObjectReference::ClearedValue(isolate_));
}
ClearBatch();
}
bool BaselineBatchCompiler::ShouldCompileBatch() const {
return estimated_instruction_size_ >=
FLAG_baseline_batch_compilation_threshold;
}
bool BaselineBatchCompiler::MaybeCompileFunction(MaybeObject maybe_sfi) {
HeapObject heapobj;
// Skip functions where the weak reference is no longer valid.
if (!maybe_sfi.GetHeapObjectIfWeak(&heapobj)) return false;
Handle<SharedFunctionInfo> shared =
handle(SharedFunctionInfo::cast(heapobj), isolate_);
// Skip functions where the bytecode has been flushed.
if (!shared->is_compiled()) return false;
IsCompiledScope is_compiled_scope(shared->is_compiled_scope(isolate_));
return Compiler::CompileSharedWithBaseline(
isolate_, shared, Compiler::CLEAR_EXCEPTION, &is_compiled_scope);
}
void BaselineBatchCompiler::ClearBatch() {
estimated_instruction_size_ = 0;
last_index_ = 0;
}
} // namespace baseline
} // namespace internal
} // namespace v8
#else
namespace v8 {
namespace internal {
namespace baseline {
BaselineBatchCompiler::BaselineBatchCompiler(Isolate* isolate)
: isolate_(isolate),
compilation_queue_(Handle<WeakFixedArray>::null()),
last_index_(0),
estimated_instruction_size_(0),
enabled_(false) {}
BaselineBatchCompiler::~BaselineBatchCompiler() {
if (!compilation_queue_.is_null()) {
GlobalHandles::Destroy(compilation_queue_.location());
compilation_queue_ = Handle<WeakFixedArray>::null();
}
}
} // namespace baseline
} // namespace internal
} // namespace v8
#endif