[wasm] Do not store Counters in compilation units

The Counters are not specific to compilation units, they just happen to
be used in WasmCompilationUnit::ExecuteCompilation.
Remove it from the compilation unit and pass it explicitly where needed.
This saves another field on the compilation units.

R=titzer@chromium.org

Bug: v8:8343
Change-Id: Iad4fd8ae23b022c237535503e0e805db7e67071a
Reviewed-on: https://chromium-review.googlesource.com/c/1304297
Commit-Queue: Clemens Hammacher <clemensh@chromium.org>
Reviewed-by: Ben Titzer <titzer@chromium.org>
Cr-Commit-Position: refs/heads/master@{#57083}
This commit is contained in:
Clemens Hammacher 2018-10-29 13:14:12 +01:00 committed by Commit Bot
parent fc100f8ebf
commit 6e0706bcc0
8 changed files with 49 additions and 38 deletions

View File

@ -5264,7 +5264,8 @@ Vector<const char> GetDebugName(Zone* zone, int index) {
} // namespace
void TurbofanWasmCompilationUnit::ExecuteCompilation(
wasm::CompilationEnv* env, wasm::WasmFeatures* detected) {
wasm::CompilationEnv* env, Counters* counters,
wasm::WasmFeatures* detected) {
TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("v8.wasm"),
"ExecuteTurbofanCompilation");
double decode_ms = 0;
@ -5321,7 +5322,7 @@ void TurbofanWasmCompilationUnit::ExecuteCompilation(
const_cast<wasm::WasmModule*>(env->module), wasm_unit_->native_module_,
wasm_unit_->func_index_, env->module->origin));
if (job->ExecuteJob() == CompilationJob::SUCCEEDED) {
wasm_unit_->SetResult(info.wasm_code());
wasm_unit_->SetResult(info.wasm_code(), counters);
}
if (FLAG_trace_wasm_decode_time) {
double pipeline_ms = pipeline_timer.Elapsed().InMillisecondsF();
@ -5333,7 +5334,7 @@ void TurbofanWasmCompilationUnit::ExecuteCompilation(
decode_ms, node_count, pipeline_ms);
}
// TODO(bradnelson): Improve histogram handling of size_t.
wasm_unit_->counters_->wasm_compile_function_peak_memory_bytes()->AddSample(
counters->wasm_compile_function_peak_memory_bytes()->AddSample(
static_cast<int>(mcgraph->graph()->zone()->allocation_size()));
}

View File

@ -56,7 +56,7 @@ class TurbofanWasmCompilationUnit {
MachineGraph* mcgraph,
NodeOriginTable* node_origins);
void ExecuteCompilation(wasm::CompilationEnv* env,
void ExecuteCompilation(wasm::CompilationEnv*, Counters*,
wasm::WasmFeatures* detected);
private:

View File

@ -1873,6 +1873,7 @@ class LiftoffCompiler {
} // namespace
bool LiftoffCompilationUnit::ExecuteCompilation(CompilationEnv* env,
Counters* counters,
WasmFeatures* detected) {
TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("v8.wasm"),
"ExecuteLiftoffCompilation");
@ -1886,7 +1887,7 @@ bool LiftoffCompilationUnit::ExecuteCompilation(CompilationEnv* env,
auto call_descriptor =
compiler::GetWasmCallDescriptor(&zone, wasm_unit_->func_body_.sig);
base::Optional<TimedHistogramScope> liftoff_compile_time_scope(
base::in_place, wasm_unit_->counters_->liftoff_compile_time());
base::in_place, counters->liftoff_compile_time());
WasmFullDecoder<Decoder::kValidate, LiftoffCompiler> decoder(
&zone, module, wasm_unit_->native_module_->enabled_features(), detected,
wasm_unit_->func_body_, call_descriptor, env, &zone);
@ -1896,11 +1897,11 @@ bool LiftoffCompilationUnit::ExecuteCompilation(CompilationEnv* env,
if (decoder.failed()) return false; // validation error
if (!compiler->ok()) {
// Liftoff compilation failed.
wasm_unit_->counters_->liftoff_unsupported_functions()->Increment();
counters->liftoff_unsupported_functions()->Increment();
return false;
}
wasm_unit_->counters_->liftoff_compiled_functions()->Increment();
counters->liftoff_compiled_functions()->Increment();
if (FLAG_trace_wasm_decode_time) {
double compile_ms = compile_timer.Elapsed().InMillisecondsF();
@ -1924,7 +1925,7 @@ bool LiftoffCompilationUnit::ExecuteCompilation(CompilationEnv* env,
wasm_unit_->func_index_, desc, frame_slot_count, safepoint_table_offset,
0, std::move(protected_instructions), std::move(source_positions),
WasmCode::kLiftoff);
wasm_unit_->SetResult(code);
wasm_unit_->SetResult(code, counters);
return true;
}

View File

@ -9,6 +9,9 @@
namespace v8 {
namespace internal {
class Counters;
namespace wasm {
struct CompilationEnv;
@ -20,7 +23,7 @@ class LiftoffCompilationUnit final {
explicit LiftoffCompilationUnit(WasmCompilationUnit* wasm_unit)
: wasm_unit_(wasm_unit) {}
bool ExecuteCompilation(CompilationEnv*, WasmFeatures* detected);
bool ExecuteCompilation(CompilationEnv*, Counters*, WasmFeatures* detected);
private:
WasmCompilationUnit* const wasm_unit_;

View File

@ -38,10 +38,9 @@ ExecutionTier WasmCompilationUnit::GetDefaultExecutionTier() {
WasmCompilationUnit::WasmCompilationUnit(WasmEngine* wasm_engine,
NativeModule* native_module,
FunctionBody body, int index,
Counters* counters, ExecutionTier mode)
ExecutionTier mode)
: wasm_engine_(wasm_engine),
func_body_(body),
counters_(counters),
func_index_(index),
native_module_(native_module),
mode_(mode) {
@ -64,13 +63,14 @@ WasmCompilationUnit::WasmCompilationUnit(WasmEngine* wasm_engine,
WasmCompilationUnit::~WasmCompilationUnit() = default;
void WasmCompilationUnit::ExecuteCompilation(CompilationEnv* env,
Counters* counters,
WasmFeatures* detected) {
const WasmModule* module = native_module_->module();
auto size_histogram =
SELECT_WASM_COUNTER(counters_, module->origin, wasm, function_size_bytes);
SELECT_WASM_COUNTER(counters, module->origin, wasm, function_size_bytes);
size_histogram->AddSample(
static_cast<int>(func_body_.end - func_body_.start));
auto timed_histogram = SELECT_WASM_COUNTER(counters_, module->origin,
auto timed_histogram = SELECT_WASM_COUNTER(counters, module->origin,
wasm_compile, function_time);
TimedHistogramScope wasm_compile_function_time_scope(timed_histogram);
@ -81,12 +81,12 @@ void WasmCompilationUnit::ExecuteCompilation(CompilationEnv* env,
switch (mode_) {
case ExecutionTier::kBaseline:
if (liftoff_unit_->ExecuteCompilation(env, detected)) break;
if (liftoff_unit_->ExecuteCompilation(env, counters, detected)) break;
// Otherwise, fall back to turbofan.
SwitchMode(ExecutionTier::kOptimized);
V8_FALLTHROUGH;
case ExecutionTier::kOptimized:
turbofan_unit_->ExecuteCompilation(env, detected);
turbofan_unit_->ExecuteCompilation(env, counters, detected);
break;
case ExecutionTier::kInterpreter:
UNREACHABLE(); // TODO(titzer): compile interpreter entry stub.
@ -144,9 +144,9 @@ bool WasmCompilationUnit::CompileWasmFunction(
wire_bytes.start() + function->code.end_offset()};
WasmCompilationUnit unit(isolate->wasm_engine(), native_module, function_body,
function->func_index, isolate->counters(), mode);
function->func_index, mode);
CompilationEnv env = native_module->CreateCompilationEnv();
unit.ExecuteCompilation(&env, detected);
unit.ExecuteCompilation(&env, isolate->counters(), detected);
if (unit.failed()) {
unit.ReportError(thrower);
return false;
@ -154,15 +154,15 @@ bool WasmCompilationUnit::CompileWasmFunction(
return true;
}
void WasmCompilationUnit::SetResult(WasmCode* code) {
void WasmCompilationUnit::SetResult(WasmCode* code, Counters* counters) {
DCHECK(!result_.failed());
DCHECK_NULL(result_.value());
result_ = Result<WasmCode*>(code);
native_module()->PublishCode(code);
counters_->wasm_generated_code_size()->Increment(
counters->wasm_generated_code_size()->Increment(
static_cast<int>(code->instructions().size()));
counters_->wasm_reloc_size()->Increment(
counters->wasm_reloc_size()->Increment(
static_cast<int>(code->reloc_info().size()));
}

View File

@ -38,11 +38,11 @@ class WasmCompilationUnit final {
// If used exclusively from a foreground thread, Isolate::counters() may be
// used by callers to pass Counters.
WasmCompilationUnit(WasmEngine*, NativeModule*, FunctionBody, int index,
Counters*, ExecutionTier = GetDefaultExecutionTier());
ExecutionTier = GetDefaultExecutionTier());
~WasmCompilationUnit();
void ExecuteCompilation(CompilationEnv*, WasmFeatures* detected);
void ExecuteCompilation(CompilationEnv*, Counters*, WasmFeatures* detected);
NativeModule* native_module() const { return native_module_; }
ExecutionTier mode() const { return mode_; }
@ -66,7 +66,6 @@ class WasmCompilationUnit final {
WasmEngine* wasm_engine_;
FunctionBody func_body_;
Counters* counters_;
int func_index_;
NativeModule* native_module_;
ExecutionTier mode_;
@ -80,7 +79,7 @@ class WasmCompilationUnit final {
void SwitchMode(ExecutionTier new_mode);
// Called from {ExecuteCompilation} to set the result of compilation.
void SetResult(WasmCode*);
void SetResult(WasmCode*, Counters*);
DISALLOW_COPY_AND_ASSIGN(WasmCompilationUnit);
};

View File

@ -381,10 +381,11 @@ WasmCode* LazyCompileFunction(Isolate* isolate, NativeModule* native_module,
module_start + func->code.end_offset()};
WasmCompilationUnit unit(isolate->wasm_engine(), native_module, body,
func_index, isolate->counters());
func_index);
CompilationEnv env = native_module->CreateCompilationEnv();
unit.ExecuteCompilation(
&env, Impl(native_module->compilation_state())->detected_features());
&env, isolate->counters(),
Impl(native_module->compilation_state())->detected_features());
// If there is a pending error, something really went wrong. The module was
// verified before starting execution with lazy compilation.
@ -476,8 +477,7 @@ class CompilationUnitBuilder {
return base::make_unique<WasmCompilationUnit>(
wasm_engine_, native_module_,
FunctionBody{function->sig, buffer_offset, bytes.begin(), bytes.end()},
function->func_index,
compilation_state()->isolate()->async_counters().get(), mode);
function->func_index, mode);
}
CompilationStateImpl* compilation_state() const {
@ -522,7 +522,8 @@ double MonotonicallyIncreasingTimeInMs() {
// the finisher_is_running_ flag is not set.
bool FetchAndExecuteCompilationUnit(CompilationEnv* env,
CompilationStateImpl* compilation_state,
WasmFeatures* detected) {
WasmFeatures* detected,
Counters* counters) {
DisallowHeapAccess no_heap_access;
std::unique_ptr<WasmCompilationUnit> unit =
@ -535,7 +536,7 @@ bool FetchAndExecuteCompilationUnit(CompilationEnv* env,
// later as soon as Liftoff can compile any function. Then, we can directly
// access {unit->mode()} within {ScheduleUnitForFinishing()}.
ExecutionTier mode = unit->mode();
unit->ExecuteCompilation(env, detected);
unit->ExecuteCompilation(env, counters, detected);
compilation_state->ScheduleUnitForFinishing(std::move(unit), mode);
return true;
@ -635,7 +636,8 @@ void CompileInParallel(Isolate* isolate, NativeModule* native_module,
WasmFeatures detected_features;
CompilationEnv env = native_module->CreateCompilationEnv();
while (FetchAndExecuteCompilationUnit(&env, compilation_state,
&detected_features) &&
&detected_features,
isolate->counters()) &&
!compilation_state->baseline_compilation_finished()) {
// 2.b) If {baseline_finish_units_} contains a compilation unit, the main
// thread dequeues it and finishes the compilation unit. Compilation
@ -846,9 +848,12 @@ class FinishCompileTask : public CancelableTask {
// The runnable task that performs compilations in the background.
class BackgroundCompileTask : public CancelableTask {
public:
explicit BackgroundCompileTask(NativeModule* native_module,
CancelableTaskManager* task_manager)
: CancelableTask(task_manager), native_module_(native_module) {}
explicit BackgroundCompileTask(CancelableTaskManager* task_manager,
NativeModule* native_module,
Counters* counters)
: CancelableTask(task_manager),
native_module_(native_module),
counters_(counters) {}
void RunInternal() override {
TRACE_COMPILE("(3b) Compiling...\n");
@ -859,7 +864,7 @@ class BackgroundCompileTask : public CancelableTask {
WasmFeatures detected_features = kNoWasmFeatures;
while (!compilation_state->failed()) {
if (!FetchAndExecuteCompilationUnit(&env, compilation_state,
&detected_features)) {
&detected_features, counters_)) {
break;
}
}
@ -868,6 +873,7 @@ class BackgroundCompileTask : public CancelableTask {
private:
NativeModule* const native_module_;
Counters* const counters_;
};
} // namespace
@ -3052,7 +3058,7 @@ void CompilationStateImpl::RestartBackgroundTasks(size_t max) {
for (; num_restart > 0; --num_restart) {
auto task = base::make_unique<BackgroundCompileTask>(
native_module_, &background_task_manager_);
&background_task_manager_, native_module_, isolate_->counters());
// If --wasm-num-compilation-tasks=0 is passed, do only spawn foreground
// tasks. This is used to make timing deterministic.

View File

@ -420,9 +420,10 @@ void WasmFunctionCompiler::Build(const byte* start, const byte* end) {
NativeModule* native_module =
builder_->instance_object()->module_object()->native_module();
WasmCompilationUnit unit(isolate()->wasm_engine(), native_module, func_body,
function_->func_index, isolate()->counters(), tier);
function_->func_index, tier);
WasmFeatures unused_detected_features;
unit.ExecuteCompilation(&env, &unused_detected_features);
unit.ExecuteCompilation(&env, isolate()->counters(),
&unused_detected_features);
CHECK(!unit.failed());
if (WasmCode::ShouldBeLogged(isolate())) unit.result()->LogCode(isolate());
}