[wasm] Remove WasmEngine* from compilation units

The wasm engine is the same for all units, thus we should store (or
get) it in the compilation task, and not store it duplicated in each
compilation unit.

R=mstarzinger@chromium.org

Bug: v8:8916, v8:8343
Change-Id: Id4b062b5b8a52228b4d6051a67e025088a61d466
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1559863
Commit-Queue: Clemens Hammacher <clemensh@chromium.org>
Reviewed-by: Michael Starzinger <mstarzinger@chromium.org>
Cr-Commit-Position: refs/heads/master@{#60754}
This commit is contained in:
Clemens Hammacher 2019-04-10 16:58:46 +02:00 committed by Commit Bot
parent 04156067a7
commit a6bdcacddd
8 changed files with 84 additions and 82 deletions

View File

@ -6115,9 +6115,10 @@ TurbofanWasmCompilationUnit::TurbofanWasmCompilationUnit(
TurbofanWasmCompilationUnit::~TurbofanWasmCompilationUnit() = default;
bool TurbofanWasmCompilationUnit::BuildGraphForWasmFunction(
wasm::CompilationEnv* env, const wasm::FunctionBody& func_body,
wasm::WasmFeatures* detected, double* decode_ms, MachineGraph* mcgraph,
NodeOriginTable* node_origins, SourcePositionTable* source_positions) {
AccountingAllocator* allocator, wasm::CompilationEnv* env,
const wasm::FunctionBody& func_body, wasm::WasmFeatures* detected,
double* decode_ms, MachineGraph* mcgraph, NodeOriginTable* node_origins,
SourcePositionTable* source_positions) {
base::ElapsedTimer decode_timer;
if (FLAG_trace_wasm_decode_time) {
decode_timer.Start();
@ -6126,9 +6127,9 @@ bool TurbofanWasmCompilationUnit::BuildGraphForWasmFunction(
// Create a TF graph during decoding.
WasmGraphBuilder builder(env, mcgraph->zone(), mcgraph, func_body.sig,
source_positions);
wasm::VoidResult graph_construction_result = wasm::BuildTFGraph(
wasm_unit_->wasm_engine_->allocator(), env->enabled_features, env->module,
&builder, detected, func_body, node_origins);
wasm::VoidResult graph_construction_result =
wasm::BuildTFGraph(allocator, env->enabled_features, env->module,
&builder, detected, func_body, node_origins);
if (graph_construction_result.failed()) {
if (FLAG_trace_wasm_compiler) {
StdoutStream{} << "Compilation failed: "
@ -6149,8 +6150,7 @@ bool TurbofanWasmCompilationUnit::BuildGraphForWasmFunction(
if (wasm_unit_->func_index_ >= FLAG_trace_wasm_ast_start &&
wasm_unit_->func_index_ < FLAG_trace_wasm_ast_end) {
PrintRawWasmCode(wasm_unit_->wasm_engine_->allocator(), func_body,
env->module, wasm::kPrintLocals);
PrintRawWasmCode(allocator, func_body, env->module, wasm::kPrintLocals);
}
if (FLAG_trace_wasm_decode_time) {
*decode_ms = decode_timer.Elapsed().InMillisecondsF();
@ -6174,14 +6174,15 @@ Vector<const char> GetDebugName(Zone* zone, int index) {
} // namespace
wasm::WasmCompilationResult TurbofanWasmCompilationUnit::ExecuteCompilation(
wasm::CompilationEnv* env, const wasm::FunctionBody& func_body,
Counters* counters, wasm::WasmFeatures* detected) {
wasm::WasmEngine* wasm_engine, wasm::CompilationEnv* env,
const wasm::FunctionBody& func_body, Counters* counters,
wasm::WasmFeatures* detected) {
TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("v8.wasm"),
"ExecuteTurbofanCompilation");
double decode_ms = 0;
size_t node_count = 0;
Zone zone(wasm_unit_->wasm_engine_->allocator(), ZONE_NAME);
Zone zone(wasm_engine->allocator(), ZONE_NAME);
MachineGraph* mcgraph = new (&zone) MachineGraph(
new (&zone) Graph(&zone), new (&zone) CommonOperatorBuilder(&zone),
new (&zone) MachineOperatorBuilder(
@ -6206,8 +6207,9 @@ wasm::WasmCompilationResult TurbofanWasmCompilationUnit::ExecuteCompilation(
: nullptr;
SourcePositionTable* source_positions =
new (mcgraph->zone()) SourcePositionTable(mcgraph->graph());
if (!BuildGraphForWasmFunction(env, func_body, detected, &decode_ms, mcgraph,
node_origins, source_positions)) {
if (!BuildGraphForWasmFunction(wasm_engine->allocator(), env, func_body,
detected, &decode_ms, mcgraph, node_origins,
source_positions)) {
return wasm::WasmCompilationResult{};
}
@ -6228,9 +6230,8 @@ wasm::WasmCompilationResult TurbofanWasmCompilationUnit::ExecuteCompilation(
}
Pipeline::GenerateCodeForWasmFunction(
&info, wasm_unit_->wasm_engine_, mcgraph, call_descriptor,
source_positions, node_origins, func_body, env->module,
wasm_unit_->func_index_);
&info, wasm_engine, mcgraph, call_descriptor, source_positions,
node_origins, func_body, env->module, wasm_unit_->func_index_);
if (FLAG_trace_wasm_decode_time) {
double pipeline_ms = pipeline_timer.Elapsed().InMillisecondsF();
@ -6249,18 +6250,19 @@ wasm::WasmCompilationResult TurbofanWasmCompilationUnit::ExecuteCompilation(
}
wasm::WasmCompilationResult InterpreterCompilationUnit::ExecuteCompilation(
wasm::CompilationEnv* env, const wasm::FunctionBody& func_body,
Counters* counters, wasm::WasmFeatures* detected) {
Zone zone(wasm_unit_->wasm_engine_->allocator(), ZONE_NAME);
wasm::WasmEngine* wasm_engine, wasm::CompilationEnv* env,
const wasm::FunctionBody& func_body, Counters* counters,
wasm::WasmFeatures* detected) {
Zone zone(wasm_engine->allocator(), ZONE_NAME);
const wasm::WasmModule* module = env ? env->module : nullptr;
wasm::WasmFullDecoder<wasm::Decoder::kValidate, wasm::EmptyInterface> decoder(
&zone, module, env->enabled_features, detected, func_body);
decoder.Decode();
if (decoder.failed()) return wasm::WasmCompilationResult{};
wasm::WasmCompilationResult result = CompileWasmInterpreterEntry(
wasm_unit_->wasm_engine_, env->enabled_features, wasm_unit_->func_index_,
func_body.sig);
wasm::WasmCompilationResult result =
CompileWasmInterpreterEntry(wasm_engine, env->enabled_features,
wasm_unit_->func_index_, func_body.sig);
DCHECK(result.succeeded());
DCHECK_EQ(wasm::ExecutionTier::kInterpreter, result.result_tier);

View File

@ -50,14 +50,16 @@ class TurbofanWasmCompilationUnit {
explicit TurbofanWasmCompilationUnit(wasm::WasmCompilationUnit* wasm_unit);
~TurbofanWasmCompilationUnit();
bool BuildGraphForWasmFunction(wasm::CompilationEnv* env,
bool BuildGraphForWasmFunction(AccountingAllocator* allocator,
wasm::CompilationEnv* env,
const wasm::FunctionBody& func_body,
wasm::WasmFeatures* detected,
double* decode_ms, MachineGraph* mcgraph,
NodeOriginTable* node_origins,
SourcePositionTable* source_positions);
wasm::WasmCompilationResult ExecuteCompilation(wasm::CompilationEnv*,
wasm::WasmCompilationResult ExecuteCompilation(wasm::WasmEngine*,
wasm::CompilationEnv*,
const wasm::FunctionBody&,
Counters*,
wasm::WasmFeatures* detected);
@ -73,7 +75,8 @@ class InterpreterCompilationUnit final {
explicit InterpreterCompilationUnit(wasm::WasmCompilationUnit* wasm_unit)
: wasm_unit_(wasm_unit) {}
wasm::WasmCompilationResult ExecuteCompilation(wasm::CompilationEnv*,
wasm::WasmCompilationResult ExecuteCompilation(wasm::WasmEngine*,
wasm::CompilationEnv*,
const wasm::FunctionBody&,
Counters*,
wasm::WasmFeatures* detected);

View File

@ -2033,8 +2033,8 @@ class LiftoffCompiler {
} // namespace
WasmCompilationResult LiftoffCompilationUnit::ExecuteCompilation(
CompilationEnv* env, const FunctionBody& func_body, Counters* counters,
WasmFeatures* detected) {
AccountingAllocator* allocator, CompilationEnv* env,
const FunctionBody& func_body, Counters* counters, WasmFeatures* detected) {
TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("v8.wasm"),
"ExecuteLiftoffCompilation");
base::ElapsedTimer compile_timer;
@ -2042,7 +2042,7 @@ WasmCompilationResult LiftoffCompilationUnit::ExecuteCompilation(
compile_timer.Start();
}
Zone zone(wasm_unit_->wasm_engine_->allocator(), "LiftoffCompilationZone");
Zone zone(allocator, "LiftoffCompilationZone");
const WasmModule* module = env ? env->module : nullptr;
auto call_descriptor = compiler::GetWasmCallDescriptor(&zone, func_body.sig);
base::Optional<TimedHistogramScope> liftoff_compile_time_scope(

View File

@ -11,6 +11,7 @@
namespace v8 {
namespace internal {
class AccountingAllocator;
class Counters;
namespace wasm {
@ -22,16 +23,14 @@ struct WasmFeatures;
class LiftoffCompilationUnit final {
public:
explicit LiftoffCompilationUnit(WasmCompilationUnit* wasm_unit)
: wasm_unit_(wasm_unit) {}
LiftoffCompilationUnit() = default;
WasmCompilationResult ExecuteCompilation(CompilationEnv*, const FunctionBody&,
WasmCompilationResult ExecuteCompilation(AccountingAllocator*,
CompilationEnv*, const FunctionBody&,
Counters*,
WasmFeatures* detected_features);
private:
WasmCompilationUnit* const wasm_unit_;
DISALLOW_COPY_AND_ASSIGN(LiftoffCompilationUnit);
};

View File

@ -115,9 +115,8 @@ ExecutionTier WasmCompilationUnit::GetDefaultExecutionTier(
return ExecutionTier::kTurbofan;
}
WasmCompilationUnit::WasmCompilationUnit(WasmEngine* wasm_engine, int index,
ExecutionTier tier)
: wasm_engine_(wasm_engine), func_index_(index), tier_(tier) {
WasmCompilationUnit::WasmCompilationUnit(int index, ExecutionTier tier)
: func_index_(index), tier_(tier) {
if (V8_UNLIKELY(FLAG_wasm_tier_mask_for_testing) && index < 32 &&
(FLAG_wasm_tier_mask_for_testing & (1 << index))) {
tier = ExecutionTier::kTurbofan;
@ -130,7 +129,7 @@ WasmCompilationUnit::WasmCompilationUnit(WasmEngine* wasm_engine, int index,
WasmCompilationUnit::~WasmCompilationUnit() = default;
WasmCompilationResult WasmCompilationUnit::ExecuteCompilation(
CompilationEnv* env,
WasmEngine* wasm_engine, CompilationEnv* env,
const std::shared_ptr<WireBytesStorage>& wire_bytes_storage,
Counters* counters, WasmFeatures* detected) {
auto* func = &env->module->functions[func_index_];
@ -156,8 +155,8 @@ WasmCompilationResult WasmCompilationUnit::ExecuteCompilation(
WasmCompilationResult result;
if (liftoff_unit_) {
result =
liftoff_unit_->ExecuteCompilation(env, func_body, counters, detected);
result = liftoff_unit_->ExecuteCompilation(wasm_engine->allocator(), env,
func_body, counters, detected);
if (!result.succeeded()) {
// If Liftoff failed, fall back to turbofan.
// TODO(wasm): We could actually stop or remove the tiering unit for this
@ -167,12 +166,12 @@ WasmCompilationResult WasmCompilationUnit::ExecuteCompilation(
}
}
if (turbofan_unit_) {
result =
turbofan_unit_->ExecuteCompilation(env, func_body, counters, detected);
result = turbofan_unit_->ExecuteCompilation(wasm_engine, env, func_body,
counters, detected);
}
if (interpreter_unit_) {
result = interpreter_unit_->ExecuteCompilation(env, func_body, counters,
detected);
result = interpreter_unit_->ExecuteCompilation(wasm_engine, env, func_body,
counters, detected);
}
result.func_index = func_index_;
result.requested_tier = tier_;
@ -196,7 +195,7 @@ void WasmCompilationUnit::SwitchTier(ExecutionTier new_tier) {
DCHECK(!turbofan_unit_);
DCHECK(!liftoff_unit_);
DCHECK(!interpreter_unit_);
liftoff_unit_.reset(new LiftoffCompilationUnit(this));
liftoff_unit_.reset(new LiftoffCompilationUnit());
return;
case ExecutionTier::kTurbofan:
DCHECK(!turbofan_unit_);
@ -227,10 +226,11 @@ void WasmCompilationUnit::CompileWasmFunction(Isolate* isolate,
wire_bytes.start() + function->code.offset(),
wire_bytes.start() + function->code.end_offset()};
WasmCompilationUnit unit(isolate->wasm_engine(), function->func_index, tier);
WasmCompilationUnit unit(function->func_index, tier);
CompilationEnv env = native_module->CreateCompilationEnv();
WasmCompilationResult result = unit.ExecuteCompilation(
&env, native_module->compilation_state()->GetWireBytesStorage(),
isolate->wasm_engine(), &env,
native_module->compilation_state()->GetWireBytesStorage(),
isolate->counters(), detected);
if (result.succeeded()) {
WasmCodeRefScope code_ref_scope;

View File

@ -70,13 +70,13 @@ class V8_EXPORT_PRIVATE WasmCompilationUnit final {
public:
static ExecutionTier GetDefaultExecutionTier(const WasmModule*);
WasmCompilationUnit(WasmEngine*, int index, ExecutionTier);
WasmCompilationUnit(int index, ExecutionTier);
~WasmCompilationUnit();
WasmCompilationResult ExecuteCompilation(
CompilationEnv*, const std::shared_ptr<WireBytesStorage>&, Counters*,
WasmFeatures* detected);
WasmEngine*, CompilationEnv*, const std::shared_ptr<WireBytesStorage>&,
Counters*, WasmFeatures* detected);
ExecutionTier tier() const { return tier_; }
@ -89,7 +89,6 @@ class V8_EXPORT_PRIVATE WasmCompilationUnit final {
friend class compiler::TurbofanWasmCompilationUnit;
friend class compiler::InterpreterCompilationUnit;
WasmEngine* const wasm_engine_;
const int func_index_;
ExecutionTier tier_;

View File

@ -553,10 +553,8 @@ ExecutionTierPair GetRequestedExecutionTiers(
// {CompilationStateImpl} when {Commit} is called.
class CompilationUnitBuilder {
public:
explicit CompilationUnitBuilder(NativeModule* native_module,
WasmEngine* wasm_engine)
explicit CompilationUnitBuilder(NativeModule* native_module)
: native_module_(native_module),
wasm_engine_(wasm_engine),
default_tier_(WasmCompilationUnit::GetDefaultExecutionTier(
native_module->module())) {}
@ -586,8 +584,7 @@ class CompilationUnitBuilder {
private:
std::unique_ptr<WasmCompilationUnit> CreateUnit(uint32_t func_index,
ExecutionTier tier) {
return base::make_unique<WasmCompilationUnit>(wasm_engine_, func_index,
tier);
return base::make_unique<WasmCompilationUnit>(func_index, tier);
}
CompilationStateImpl* compilation_state() const {
@ -595,7 +592,6 @@ class CompilationUnitBuilder {
}
NativeModule* const native_module_;
WasmEngine* const wasm_engine_;
const ExecutionTier default_tier_;
std::vector<std::unique_ptr<WasmCompilationUnit>> baseline_units_;
std::vector<std::unique_ptr<WasmCompilationUnit>> tiering_units_;
@ -633,18 +629,17 @@ void CompileLazy(Isolate* isolate, NativeModule* native_module,
native_module->module(), compilation_state->compile_mode(),
native_module->enabled_features(), func_index);
WasmCompilationUnit baseline_unit(isolate->wasm_engine(), func_index,
tiers.baseline_tier);
WasmCompilationUnit baseline_unit(func_index, tiers.baseline_tier);
CompilationEnv env = native_module->CreateCompilationEnv();
WasmCompilationResult result = baseline_unit.ExecuteCompilation(
&env, compilation_state->GetWireBytesStorage(), isolate->counters(),
compilation_state->detected_features());
isolate->wasm_engine(), &env, compilation_state->GetWireBytesStorage(),
isolate->counters(), compilation_state->detected_features());
WasmCodeRefScope code_ref_scope;
WasmCode* code = native_module->AddCompiledCode(std::move(result));
if (tiers.baseline_tier < tiers.top_tier) {
auto tiering_unit = base::make_unique<WasmCompilationUnit>(
isolate->wasm_engine(), func_index, tiers.top_tier);
auto tiering_unit =
base::make_unique<WasmCompilationUnit>(func_index, tiers.top_tier);
compilation_state->AddTopTierCompilationUnit(std::move(tiering_unit));
}
@ -678,7 +673,8 @@ void RecordStats(const Code code, Counters* counters) {
// Run by the main thread to take part in compilation. Only used for synchronous
// compilation.
bool FetchAndExecuteCompilationUnit(CompilationEnv* env,
bool FetchAndExecuteCompilationUnit(WasmEngine* wasm_engine,
CompilationEnv* env,
NativeModule* native_module,
CompilationStateImpl* compilation_state,
WasmFeatures* detected,
@ -694,7 +690,8 @@ bool FetchAndExecuteCompilationUnit(CompilationEnv* env,
if (unit == nullptr) return false;
WasmCompilationResult result = unit->ExecuteCompilation(
env, compilation_state->GetWireBytesStorage(), counters, detected);
wasm_engine, env, compilation_state->GetWireBytesStorage(), counters,
detected);
if (result.succeeded()) {
WasmCodeRefScope code_ref_scope;
@ -758,8 +755,7 @@ void ValidateSequentially(Counters* counters, AccountingAllocator* allocator,
// TODO(wasm): This function should not depend on an isolate. Internally, it is
// used for the ErrorThrower only.
bool InitializeCompilationUnits(Isolate* isolate, NativeModule* native_module,
WasmEngine* wasm_engine) {
bool InitializeCompilationUnits(Isolate* isolate, NativeModule* native_module) {
// Set number of functions that must be compiled to consider the module fully
// compiled.
auto wasm_module = native_module->module();
@ -775,7 +771,7 @@ bool InitializeCompilationUnits(Isolate* isolate, NativeModule* native_module,
ErrorThrower thrower(isolate, "WebAssembly.compile()");
ModuleWireBytes wire_bytes(native_module->wire_bytes());
const WasmModule* module = native_module->module();
CompilationUnitBuilder builder(native_module, wasm_engine);
CompilationUnitBuilder builder(native_module);
uint32_t start = module->num_imported_functions;
uint32_t end = start + module->num_declared_functions;
for (uint32_t func_index = start; func_index < end; func_index++) {
@ -834,8 +830,7 @@ void CompileNativeModule(Isolate* isolate, ErrorThrower* thrower,
DCHECK_GE(kMaxInt, native_module->module()->num_declared_functions);
// Initialize the compilation units and kick off background compile tasks.
if (!InitializeCompilationUnits(isolate, native_module,
isolate->wasm_engine())) {
if (!InitializeCompilationUnits(isolate, native_module)) {
// TODO(frgossen): Add test coverage for this path.
DCHECK(native_module->enabled_features().compilation_hints);
compilation_state->SetError();
@ -854,8 +849,9 @@ void CompileNativeModule(Isolate* isolate, ErrorThrower* thrower,
// deterministic compilation; in that case, the single background task will
// execute all compilation.
if (NeedsDeterministicCompile()) continue;
FetchAndExecuteCompilationUnit(&env, native_module, compilation_state,
&detected_features, isolate->counters());
FetchAndExecuteCompilationUnit(isolate->wasm_engine(), &env, native_module,
compilation_state, &detected_features,
isolate->counters());
}
// Publish features from the foreground and background tasks.
@ -897,6 +893,7 @@ class BackgroundCompileTask : public CancelableTask {
base::Optional<CompilationEnv> env;
std::shared_ptr<WireBytesStorage> wire_bytes;
std::shared_ptr<const WasmModule> module;
WasmEngine* wasm_engine = nullptr;
std::unique_ptr<WasmCompilationUnit> unit;
WasmFeatures detected_features = kNoWasmFeatures;
@ -908,6 +905,7 @@ class BackgroundCompileTask : public CancelableTask {
env.emplace(compile_scope.native_module()->CreateCompilationEnv());
wire_bytes = compile_scope.compilation_state()->GetWireBytesStorage();
module = compile_scope.native_module()->shared_module();
wasm_engine = compile_scope.native_module()->engine();
unit =
compile_scope.compilation_state()->GetNextCompilationUnit(task_id_);
if (unit == nullptr) {
@ -934,8 +932,9 @@ class BackgroundCompileTask : public CancelableTask {
bool compilation_failed = false;
while (true) {
// (asynchronous): Execute the compilation.
WasmCompilationResult result = unit->ExecuteCompilation(
&env.value(), wire_bytes, async_counters_.get(), &detected_features);
WasmCompilationResult result =
unit->ExecuteCompilation(wasm_engine, &env.value(), wire_bytes,
async_counters_.get(), &detected_features);
results_to_publish.emplace_back(std::move(result));
// (synchronized): Publish the compilation result and get the next unit.
@ -978,8 +977,8 @@ class BackgroundCompileTask : public CancelableTask {
}
private:
std::shared_ptr<BackgroundCompileToken> token_;
std::shared_ptr<Counters> async_counters_;
const std::shared_ptr<BackgroundCompileToken> token_;
const std::shared_ptr<Counters> async_counters_;
const int task_id_;
};
@ -1488,8 +1487,8 @@ class AsyncCompileJob::PrepareAndStartCompile : public CompileStep {
// Add compilation units and kick off compilation.
auto isolate = job->isolate();
bool success = InitializeCompilationUnits(
isolate, job->native_module_.get(), isolate->wasm_engine());
bool success =
InitializeCompilationUnits(isolate, job->native_module_.get());
if (!success) {
// TODO(frgossen): Add test coverage for this path.
DCHECK(job->native_module_->enabled_features().compilation_hints);
@ -1676,8 +1675,8 @@ bool AsyncStreamingProcessor::ProcessCodeSectionHeader(
// Set outstanding_finishers_ to 2, because both the AsyncCompileJob and the
// AsyncStreamingProcessor have to finish.
job_->outstanding_finishers_.store(2);
compilation_unit_builder_.reset(new CompilationUnitBuilder(
job_->native_module_.get(), job_->isolate()->wasm_engine()));
compilation_unit_builder_.reset(
new CompilationUnitBuilder(job_->native_module_.get()));
return true;
}

View File

@ -510,11 +510,11 @@ void WasmFunctionCompiler::Build(const byte* start, const byte* end) {
func_wire_bytes.start(), func_wire_bytes.end()};
NativeModule* native_module =
builder_->instance_object()->module_object()->native_module();
WasmCompilationUnit unit(isolate()->wasm_engine(), function_->func_index,
builder_->execution_tier());
WasmCompilationUnit unit(function_->func_index, builder_->execution_tier());
WasmFeatures unused_detected_features;
WasmCompilationResult result = unit.ExecuteCompilation(
&env, native_module->compilation_state()->GetWireBytesStorage(),
isolate()->wasm_engine(), &env,
native_module->compilation_state()->GetWireBytesStorage(),
isolate()->counters(), &unused_detected_features);
WasmCode* code = native_module->AddCompiledCode(std::move(result));
DCHECK_NOT_NULL(code);