Commit f619c7a5 authored by Andreas Haas's avatar Andreas Haas Committed by Commit Bot

[wasm] Store the CEntryStub handle in the ModuleCompiler

This makes reopening the handle easier, and also reduces the size of
the handle space.

Drive-by change: Make all fields of the ModuleCompiler private.

R=clemensh@chromium.org

Change-Id: I2f9553a00012b9ce7743bf68bdf1c3095ca69fe0
Reviewed-on: https://chromium-review.googlesource.com/548397
Commit-Queue: Andreas Haas <ahaas@chromium.org>
Reviewed-by: 's avatarClemens Hammacher <clemensh@chromium.org>
Cr-Commit-Position: refs/heads/master@{#46255}
parent 095132d6
...@@ -12,7 +12,6 @@ ...@@ -12,7 +12,6 @@
#include "src/base/platform/platform.h" #include "src/base/platform/platform.h"
#include "src/builtins/builtins.h" #include "src/builtins/builtins.h"
#include "src/code-factory.h" #include "src/code-factory.h"
#include "src/code-stubs.h"
#include "src/compiler/access-builder.h" #include "src/compiler/access-builder.h"
#include "src/compiler/common-operator.h" #include "src/compiler/common-operator.h"
#include "src/compiler/compiler-source-position-table.h" #include "src/compiler/compiler-source-position-table.h"
...@@ -3945,7 +3944,7 @@ Vector<const char> GetDebugName(Zone* zone, wasm::WasmName name, int index) { ...@@ -3945,7 +3944,7 @@ Vector<const char> GetDebugName(Zone* zone, wasm::WasmName name, int index) {
WasmCompilationUnit::WasmCompilationUnit(Isolate* isolate, WasmCompilationUnit::WasmCompilationUnit(Isolate* isolate,
wasm::ModuleBytesEnv* module_env, wasm::ModuleBytesEnv* module_env,
const wasm::WasmFunction* function, const wasm::WasmFunction* function,
bool is_sync) Handle<Code> centry_stub, bool is_sync)
: WasmCompilationUnit( : WasmCompilationUnit(
isolate, &module_env->module_env, isolate, &module_env->module_env,
wasm::FunctionBody{ wasm::FunctionBody{
...@@ -3953,19 +3952,19 @@ WasmCompilationUnit::WasmCompilationUnit(Isolate* isolate, ...@@ -3953,19 +3952,19 @@ WasmCompilationUnit::WasmCompilationUnit(Isolate* isolate,
module_env->wire_bytes.start() + function->code.offset(), module_env->wire_bytes.start() + function->code.offset(),
module_env->wire_bytes.start() + function->code.end_offset()}, module_env->wire_bytes.start() + function->code.end_offset()},
module_env->wire_bytes.GetNameOrNull(function), function->func_index, module_env->wire_bytes.GetNameOrNull(function), function->func_index,
is_sync) {} centry_stub, is_sync) {}
WasmCompilationUnit::WasmCompilationUnit(Isolate* isolate, WasmCompilationUnit::WasmCompilationUnit(Isolate* isolate,
wasm::ModuleEnv* module_env, wasm::ModuleEnv* module_env,
wasm::FunctionBody body, wasm::FunctionBody body,
wasm::WasmName name, int index, wasm::WasmName name, int index,
bool is_sync) Handle<Code> centry_stub, bool is_sync)
: isolate_(isolate), : isolate_(isolate),
module_env_(module_env), module_env_(module_env),
func_body_(body), func_body_(body),
func_name_(name), func_name_(name),
is_sync_(is_sync), is_sync_(is_sync),
centry_stub_(CEntryStub(isolate, 1).GetCode()), centry_stub_(centry_stub),
func_index_(index) {} func_index_(index) {}
void WasmCompilationUnit::ExecuteCompilation() { void WasmCompilationUnit::ExecuteCompilation() {
...@@ -4108,7 +4107,8 @@ Handle<Code> WasmCompilationUnit::FinishCompilation( ...@@ -4108,7 +4107,8 @@ Handle<Code> WasmCompilationUnit::FinishCompilation(
Handle<Code> WasmCompilationUnit::CompileWasmFunction( Handle<Code> WasmCompilationUnit::CompileWasmFunction(
wasm::ErrorThrower* thrower, Isolate* isolate, wasm::ErrorThrower* thrower, Isolate* isolate,
wasm::ModuleBytesEnv* module_env, const wasm::WasmFunction* function) { wasm::ModuleBytesEnv* module_env, const wasm::WasmFunction* function) {
WasmCompilationUnit unit(isolate, module_env, function); WasmCompilationUnit unit(isolate, module_env, function,
CEntryStub(isolate, 1).GetCode());
unit.ExecuteCompilation(); unit.ExecuteCompilation();
return unit.FinishCompilation(thrower); return unit.FinishCompilation(thrower);
} }
......
...@@ -48,14 +48,14 @@ namespace compiler { ...@@ -48,14 +48,14 @@ namespace compiler {
class WasmCompilationUnit final { class WasmCompilationUnit final {
public: public:
WasmCompilationUnit(Isolate* isolate, wasm::ModuleBytesEnv* module_env, WasmCompilationUnit(Isolate* isolate, wasm::ModuleBytesEnv* module_env,
const wasm::WasmFunction* function, bool is_sync = true); const wasm::WasmFunction* function,
Handle<Code> centry_stub, bool is_sync = true);
WasmCompilationUnit(Isolate* isolate, wasm::ModuleEnv* module_env, WasmCompilationUnit(Isolate* isolate, wasm::ModuleEnv* module_env,
wasm::FunctionBody body, wasm::WasmName name, int index, wasm::FunctionBody body, wasm::WasmName name, int index,
bool is_sync = true); Handle<Code> centry_stub, bool is_sync = true);
int func_index() const { return func_index_; } int func_index() const { return func_index_; }
void ReopenCentryStub() { centry_stub_ = handle(*centry_stub_, isolate_); }
void ExecuteCompilation(); void ExecuteCompilation();
Handle<Code> FinishCompilation(wasm::ErrorThrower* thrower); Handle<Code> FinishCompilation(wasm::ErrorThrower* thrower);
......
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
#include "src/asmjs/asm-js.h" #include "src/asmjs/asm-js.h"
#include "src/assembler-inl.h" #include "src/assembler-inl.h"
#include "src/code-stubs.h"
#include "src/counters.h" #include "src/counters.h"
#include "src/property-descriptor.h" #include "src/property-descriptor.h"
#include "src/wasm/compilation-manager.h" #include "src/wasm/compilation-manager.h"
...@@ -96,7 +97,8 @@ ModuleCompiler::ModuleCompiler(Isolate* isolate, ...@@ -96,7 +97,8 @@ ModuleCompiler::ModuleCompiler(Isolate* isolate,
num_background_tasks_( num_background_tasks_(
Min(static_cast<size_t>(FLAG_wasm_num_compilation_tasks), Min(static_cast<size_t>(FLAG_wasm_num_compilation_tasks),
V8::GetCurrentPlatform()->NumberOfAvailableBackgroundThreads())), V8::GetCurrentPlatform()->NumberOfAvailableBackgroundThreads())),
stopped_compilation_tasks_(num_background_tasks_) { stopped_compilation_tasks_(num_background_tasks_),
centry_stub_(CEntryStub(isolate, 1).GetCode()) {
counters_ = counters_shared_.get(); counters_ = counters_shared_.get();
} }
...@@ -163,11 +165,15 @@ size_t ModuleCompiler::InitializeParallelCompilation( ...@@ -163,11 +165,15 @@ size_t ModuleCompiler::InitializeParallelCompilation(
constexpr bool is_sync = true; constexpr bool is_sync = true;
compilation_units_.push_back(std::unique_ptr<compiler::WasmCompilationUnit>( compilation_units_.push_back(std::unique_ptr<compiler::WasmCompilationUnit>(
new compiler::WasmCompilationUnit(isolate_, &module_env, func, new compiler::WasmCompilationUnit(isolate_, &module_env, func,
!is_sync))); centry_stub_, !is_sync)));
} }
return funcs_to_compile; return funcs_to_compile;
} }
void ModuleCompiler::ReopenHandlesInDeferredScope() {
centry_stub_ = handle(*centry_stub_, isolate_);
}
void ModuleCompiler::RestartCompilationTasks() { void ModuleCompiler::RestartCompilationTasks() {
base::LockGuard<base::Mutex> guard(&tasks_mutex_); base::LockGuard<base::Mutex> guard(&tasks_mutex_);
for (; stopped_compilation_tasks_ > 0; --stopped_compilation_tasks_) { for (; stopped_compilation_tasks_ > 0; --stopped_compilation_tasks_) {
...@@ -1913,9 +1919,7 @@ void AsyncCompileJob::ReopenHandlesInDeferredScope() { ...@@ -1913,9 +1919,7 @@ void AsyncCompileJob::ReopenHandlesInDeferredScope() {
signature_tables_ = handle(*signature_tables_, isolate_); signature_tables_ = handle(*signature_tables_, isolate_);
code_table_ = handle(*code_table_, isolate_); code_table_ = handle(*code_table_, isolate_);
temp_instance_->ReopenHandles(isolate_); temp_instance_->ReopenHandles(isolate_);
for (auto& unit : compiler_->compilation_units_) { compiler_->ReopenHandlesInDeferredScope();
unit->ReopenCentryStub();
}
deferred_handles_.push_back(deferred.Detach()); deferred_handles_.push_back(deferred.Detach());
} }
...@@ -2120,11 +2124,13 @@ class AsyncCompileJob::PrepareAndStartCompile : public CompileStep { ...@@ -2120,11 +2124,13 @@ class AsyncCompileJob::PrepareAndStartCompile : public CompileStep {
new ModuleCompiler(job_->isolate_, std::move(module_), !is_sync)); new ModuleCompiler(job_->isolate_, std::move(module_), !is_sync));
job_->compiler_->EnableThrottling(); job_->compiler_->EnableThrottling();
// Reopen all handles which should survive in the DeferredHandleScope.
job_->ReopenHandlesInDeferredScope();
DCHECK_LE(module->num_imported_functions, module->functions.size()); DCHECK_LE(module->num_imported_functions, module->functions.size());
size_t num_functions = size_t num_functions =
module->functions.size() - module->num_imported_functions; module->functions.size() - module->num_imported_functions;
if (num_functions == 0) { if (num_functions == 0) {
job_->ReopenHandlesInDeferredScope();
// Degenerate case of an empty module. // Degenerate case of an empty module.
job_->DoSync<FinishCompile>(); job_->DoSync<FinishCompile>();
return; return;
...@@ -2142,8 +2148,6 @@ class AsyncCompileJob::PrepareAndStartCompile : public CompileStep { ...@@ -2142,8 +2148,6 @@ class AsyncCompileJob::PrepareAndStartCompile : public CompileStep {
job_->outstanding_units_ = job_->compiler_->InitializeParallelCompilation( job_->outstanding_units_ = job_->compiler_->InitializeParallelCompilation(
module->functions, *job_->module_bytes_env_); module->functions, *job_->module_bytes_env_);
// Reopen all handles which should survive in the DeferredHandleScope.
job_->ReopenHandlesInDeferredScope();
job_->DoAsync<ExecuteAndFinishCompilationUnits>(num_background_tasks); job_->DoAsync<ExecuteAndFinishCompilationUnits>(num_background_tasks);
} }
}; };
...@@ -2286,7 +2290,7 @@ class AsyncCompileJob::FinishCompile : public CompileStep { ...@@ -2286,7 +2290,7 @@ class AsyncCompileJob::FinishCompile : public CompileStep {
// The {module_wrapper} will take ownership of the {WasmModule} object, // The {module_wrapper} will take ownership of the {WasmModule} object,
// and it will be destroyed when the GC reclaims the wrapper object. // and it will be destroyed when the GC reclaims the wrapper object.
Handle<WasmModuleWrapper> module_wrapper = WasmModuleWrapper::New( Handle<WasmModuleWrapper> module_wrapper = WasmModuleWrapper::New(
job_->isolate_, job_->compiler_->module_.release()); job_->isolate_, job_->compiler_->ReleaseModule().release());
// Create the shared module data. // Create the shared module data.
// TODO(clemensh): For the same module (same bytes / same hash), we should // TODO(clemensh): For the same module (same bytes / same hash), we should
......
...@@ -67,21 +67,6 @@ class ModuleCompiler { ...@@ -67,21 +67,6 @@ class ModuleCompiler {
base::AtomicNumber<size_t> allocated_memory_{0}; base::AtomicNumber<size_t> allocated_memory_{0};
}; };
Isolate* isolate_;
std::unique_ptr<WasmModule> module_;
std::shared_ptr<Counters> counters_shared_;
Counters* counters_;
bool is_sync_;
std::vector<std::unique_ptr<compiler::WasmCompilationUnit>>
compilation_units_;
CodeGenerationSchedule executed_units_;
base::Mutex result_mutex_;
base::AtomicNumber<size_t> next_unit_;
const size_t num_background_tasks_;
// This flag should only be set while holding result_mutex_.
bool finisher_is_running_ = false;
CancelableTaskManager background_task_manager_;
// Run by each compilation task and by the main thread. The // Run by each compilation task and by the main thread. The
// no_finisher_callback is called within the result_mutex_ lock when no // no_finisher_callback is called within the result_mutex_ lock when no
// finishing task is running, i.e. when the finisher_is_running_ flag is not // finishing task is running, i.e. when the finisher_is_running_ flag is not
...@@ -102,6 +87,8 @@ class ModuleCompiler { ...@@ -102,6 +87,8 @@ class ModuleCompiler {
size_t InitializeParallelCompilation( size_t InitializeParallelCompilation(
const std::vector<WasmFunction>& functions, ModuleBytesEnv& module_env); const std::vector<WasmFunction>& functions, ModuleBytesEnv& module_env);
void ReopenHandlesInDeferredScope();
void RestartCompilationTasks(); void RestartCompilationTasks();
size_t FinishCompilationUnits(std::vector<Handle<Code>>& results, size_t FinishCompilationUnits(std::vector<Handle<Code>>& results,
...@@ -126,6 +113,8 @@ class ModuleCompiler { ...@@ -126,6 +113,8 @@ class ModuleCompiler {
Handle<Script> asm_js_script, Handle<Script> asm_js_script,
Vector<const byte> asm_js_offset_table_bytes); Vector<const byte> asm_js_offset_table_bytes);
std::unique_ptr<WasmModule> ReleaseModule() { return std::move(module_); }
private: private:
MaybeHandle<WasmModuleObject> CompileToModuleObjectInternal( MaybeHandle<WasmModuleObject> CompileToModuleObjectInternal(
ErrorThrower* thrower, const ModuleWireBytes& wire_bytes, ErrorThrower* thrower, const ModuleWireBytes& wire_bytes,
...@@ -134,8 +123,23 @@ class ModuleCompiler { ...@@ -134,8 +123,23 @@ class ModuleCompiler {
WasmInstance* temp_instance, Handle<FixedArray>* function_tables, WasmInstance* temp_instance, Handle<FixedArray>* function_tables,
Handle<FixedArray>* signature_tables); Handle<FixedArray>* signature_tables);
Isolate* isolate_;
std::unique_ptr<WasmModule> module_;
std::shared_ptr<Counters> counters_shared_;
Counters* counters_;
bool is_sync_;
std::vector<std::unique_ptr<compiler::WasmCompilationUnit>>
compilation_units_;
CodeGenerationSchedule executed_units_;
base::Mutex result_mutex_;
base::AtomicNumber<size_t> next_unit_;
const size_t num_background_tasks_;
// This flag should only be set while holding result_mutex_.
bool finisher_is_running_ = false;
CancelableTaskManager background_task_manager_;
size_t stopped_compilation_tasks_ = 0; size_t stopped_compilation_tasks_ = 0;
base::Mutex tasks_mutex_; base::Mutex tasks_mutex_;
Handle<Code> centry_stub_;
}; };
class JSToWasmWrapperCache { class JSToWasmWrapperCache {
......
...@@ -994,7 +994,8 @@ void LazyCompilationOrchestrator::CompileFunction( ...@@ -994,7 +994,8 @@ void LazyCompilationOrchestrator::CompileFunction(
} }
ErrorThrower thrower(isolate, "WasmLazyCompile"); ErrorThrower thrower(isolate, "WasmLazyCompile");
compiler::WasmCompilationUnit unit(isolate, &module_env, body, compiler::WasmCompilationUnit unit(isolate, &module_env, body,
CStrVector(func_name.c_str()), func_index); CStrVector(func_name.c_str()), func_index,
CEntryStub(isolate, 1).GetCode());
unit.ExecuteCompilation(); unit.ExecuteCompilation();
Handle<Code> code = unit.FinishCompilation(&thrower); Handle<Code> code = unit.FinishCompilation(&thrower);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment