Commit bffeab32 authored by Michael Starzinger's avatar Michael Starzinger Committed by Commit Bot

[wasm] Make {CompilationState} independent of module.

R=clemensh@chromium.org

Change-Id: I90992ca98765c22f918a612671b718bdd4b9a764
Reviewed-on: https://chromium-review.googlesource.com/995535
Commit-Queue: Michael Starzinger <mstarzinger@chromium.org>
Reviewed-by: 's avatarClemens Hammacher <clemensh@chromium.org>
Cr-Commit-Position: refs/heads/master@{#52355}
parent 86d84f5f
...@@ -5188,7 +5188,7 @@ void WasmCompilationUnit::ExecuteCompilation() { ...@@ -5188,7 +5188,7 @@ void WasmCompilationUnit::ExecuteCompilation() {
TimedHistogramScope wasm_compile_function_time_scope(timed_histogram); TimedHistogramScope wasm_compile_function_time_scope(timed_histogram);
if (FLAG_trace_wasm_compiler) { if (FLAG_trace_wasm_compiler) {
PrintF("Compiling wasm function %d\n\n", func_index()); PrintF("Compiling wasm function %d\n\n", func_index_);
} }
switch (mode_) { switch (mode_) {
......
...@@ -111,8 +111,6 @@ class WasmCompilationUnit final { ...@@ -111,8 +111,6 @@ class WasmCompilationUnit final {
~WasmCompilationUnit(); ~WasmCompilationUnit();
int func_index() const { return func_index_; }
void ExecuteCompilation(); void ExecuteCompilation();
wasm::WasmCode* FinishCompilation(wasm::ErrorThrower* thrower); wasm::WasmCode* FinishCompilation(wasm::ErrorThrower* thrower);
...@@ -123,6 +121,7 @@ class WasmCompilationUnit final { ...@@ -123,6 +121,7 @@ class WasmCompilationUnit final {
CompilationMode = GetDefaultCompilationMode()); CompilationMode = GetDefaultCompilationMode());
size_t memory_cost() const { return memory_cost_; } size_t memory_cost() const { return memory_cost_; }
wasm::NativeModule* native_module() const { return native_module_; }
private: private:
struct LiftoffData { struct LiftoffData {
......
...@@ -99,7 +99,7 @@ class CompilationState { ...@@ -99,7 +99,7 @@ class CompilationState {
base::AtomicNumber<size_t> allocated_memory_{0}; base::AtomicNumber<size_t> allocated_memory_{0};
}; };
CompilationState(internal::Isolate* isolate, NativeModule* native_module); explicit CompilationState(internal::Isolate* isolate);
~CompilationState(); ~CompilationState();
// Needs to be set before {AddCompilationUnits} is run, which triggers // Needs to be set before {AddCompilationUnits} is run, which triggers
...@@ -142,7 +142,6 @@ class CompilationState { ...@@ -142,7 +142,6 @@ class CompilationState {
void Abort(); void Abort();
Isolate* isolate() { return isolate_; } Isolate* isolate() { return isolate_; }
NativeModule* native_module() const { return native_module_; }
bool failed() { return failed_; } bool failed() { return failed_; }
...@@ -152,7 +151,6 @@ class CompilationState { ...@@ -152,7 +151,6 @@ class CompilationState {
void NotifyOnEvent(CompilationEvent event, Handle<Object> error); void NotifyOnEvent(CompilationEvent event, Handle<Object> error);
Isolate* isolate_; Isolate* isolate_;
NativeModule* native_module_;
std::vector<std::unique_ptr<compiler::WasmCompilationUnit>> std::vector<std::unique_ptr<compiler::WasmCompilationUnit>>
compilation_units_; compilation_units_;
...@@ -1160,25 +1158,14 @@ void InitializeCompilationUnits(const std::vector<WasmFunction>& functions, ...@@ -1160,25 +1158,14 @@ void InitializeCompilationUnits(const std::vector<WasmFunction>& functions,
builder.Commit(); builder.Commit();
} }
wasm::WasmCode* FinishCompilationUnit(CompilationState* compilation_state,
ErrorThrower* thrower, int* func_index) {
std::unique_ptr<compiler::WasmCompilationUnit> unit =
compilation_state->GetNextExecutedUnit();
if (unit == nullptr) return {};
*func_index = unit->func_index();
DCHECK_LE(0, *func_index);
return unit->FinishCompilation(thrower);
}
void FinishCompilationUnits(CompilationState* compilation_state, void FinishCompilationUnits(CompilationState* compilation_state,
ErrorThrower* thrower) { ErrorThrower* thrower) {
while (true) { while (true) {
if (compilation_state->failed()) break; if (compilation_state->failed()) break;
int func_index = -1; std::unique_ptr<compiler::WasmCompilationUnit> unit =
wasm::WasmCode* result = compilation_state->GetNextExecutedUnit();
FinishCompilationUnit(compilation_state, thrower, &func_index); if (unit == nullptr) break;
wasm::WasmCode* result = unit->FinishCompilation(thrower);
if (func_index < 0) break;
// Update the compilation state. // Update the compilation state.
compilation_state->OnFinishedUnit(NotifyCompilationCallback::kNoNotify); compilation_state->OnFinishedUnit(NotifyCompilationCallback::kNoNotify);
...@@ -1465,9 +1452,7 @@ class FinishCompileTask : public CancelableTask { ...@@ -1465,9 +1452,7 @@ class FinishCompileTask : public CancelableTask {
Isolate* isolate = compilation_state_->isolate(); Isolate* isolate = compilation_state_->isolate();
HandleScope scope(isolate); HandleScope scope(isolate);
SaveContext saved_context(isolate); SaveContext saved_context(isolate);
isolate->set_context(compilation_state_->native_module() isolate->set_context(nullptr);
->compiled_module()
->native_context());
TRACE_COMPILE("(4a) Finishing compilation units...\n"); TRACE_COMPILE("(4a) Finishing compilation units...\n");
if (compilation_state_->failed()) { if (compilation_state_->failed()) {
...@@ -1475,8 +1460,6 @@ class FinishCompileTask : public CancelableTask { ...@@ -1475,8 +1460,6 @@ class FinishCompileTask : public CancelableTask {
return; return;
} }
ErrorThrower thrower(compilation_state_->isolate(), "AsyncCompile");
// We execute for 1 ms and then reschedule the task, same as the GC. // We execute for 1 ms and then reschedule the task, same as the GC.
double deadline = MonotonicallyIncreasingTimeInMs() + 1.0; double deadline = MonotonicallyIncreasingTimeInMs() + 1.0;
while (true) { while (true) {
...@@ -1484,20 +1467,10 @@ class FinishCompileTask : public CancelableTask { ...@@ -1484,20 +1467,10 @@ class FinishCompileTask : public CancelableTask {
compilation_state_->RestartBackgroundTasks(); compilation_state_->RestartBackgroundTasks();
} }
int func_index = -1; std::unique_ptr<compiler::WasmCompilationUnit> unit =
wasm::WasmCode* result = compilation_state_->GetNextExecutedUnit();
FinishCompilationUnit(compilation_state_, &thrower, &func_index);
if (thrower.error()) {
DCHECK_NULL(result);
USE(result);
Handle<Object> error = thrower.Reify();
compilation_state_->OnError(error, NotifyCompilationCallback::kNotify);
compilation_state_->SetFinisherIsRunning(false);
break;
}
if (func_index < 0) { if (unit == nullptr) {
// It might happen that a background task just scheduled a unit to be // It might happen that a background task just scheduled a unit to be
// finished, but did not start a finisher task since the flag was still // finished, but did not start a finisher task since the flag was still
// set. Check for this case, and continue if there is more work. // set. Check for this case, and continue if there is more work.
...@@ -1509,6 +1482,21 @@ class FinishCompileTask : public CancelableTask { ...@@ -1509,6 +1482,21 @@ class FinishCompileTask : public CancelableTask {
break; break;
} }
ErrorThrower thrower(compilation_state_->isolate(), "AsyncCompile");
wasm::WasmCode* result = unit->FinishCompilation(&thrower);
if (thrower.error()) {
DCHECK_NULL(result);
USE(result);
SaveContext saved_context(isolate);
isolate->set_context(
unit->native_module()->compiled_module()->native_context());
Handle<Object> error = thrower.Reify();
compilation_state_->OnError(error, NotifyCompilationCallback::kNotify);
compilation_state_->SetFinisherIsRunning(false);
break;
}
// Update the compilation state, and possibly notify // Update the compilation state, and possibly notify
// threads waiting for events. // threads waiting for events.
compilation_state_->OnFinishedUnit(NotifyCompilationCallback::kNotify); compilation_state_->OnFinishedUnit(NotifyCompilationCallback::kNotify);
...@@ -3414,15 +3402,13 @@ void CompilationStateDeleter::operator()( ...@@ -3414,15 +3402,13 @@ void CompilationStateDeleter::operator()(
} }
std::unique_ptr<CompilationState, CompilationStateDeleter> NewCompilationState( std::unique_ptr<CompilationState, CompilationStateDeleter> NewCompilationState(
Isolate* isolate, NativeModule* native_module) { Isolate* isolate) {
return std::unique_ptr<CompilationState, CompilationStateDeleter>( return std::unique_ptr<CompilationState, CompilationStateDeleter>(
new CompilationState(isolate, native_module)); new CompilationState(isolate));
} }
CompilationState::CompilationState(internal::Isolate* isolate, CompilationState::CompilationState(internal::Isolate* isolate)
NativeModule* native_module)
: isolate_(isolate), : isolate_(isolate),
native_module_(native_module),
executed_units_(isolate->random_number_generator(), executed_units_(isolate->random_number_generator(),
GetMaxUsableMemorySize(isolate) / 2) { GetMaxUsableMemorySize(isolate) / 2) {
v8::Isolate* v8_isolate = reinterpret_cast<v8::Isolate*>(isolate_); v8::Isolate* v8_isolate = reinterpret_cast<v8::Isolate*>(isolate_);
......
...@@ -31,7 +31,7 @@ struct CompilationStateDeleter { ...@@ -31,7 +31,7 @@ struct CompilationStateDeleter {
// Wrapper to create a CompilationState exists in order to avoid having // Wrapper to create a CompilationState exists in order to avoid having
// the the CompilationState in the header file. // the the CompilationState in the header file.
std::unique_ptr<CompilationState, CompilationStateDeleter> NewCompilationState( std::unique_ptr<CompilationState, CompilationStateDeleter> NewCompilationState(
Isolate* isolate, NativeModule* native_module); Isolate* isolate);
MaybeHandle<WasmModuleObject> CompileToModuleObject( MaybeHandle<WasmModuleObject> CompileToModuleObject(
Isolate* isolate, ErrorThrower* thrower, std::unique_ptr<WasmModule> module, Isolate* isolate, ErrorThrower* thrower, std::unique_ptr<WasmModule> module,
......
...@@ -430,7 +430,7 @@ NativeModule::NativeModule(uint32_t num_functions, uint32_t num_imports, ...@@ -430,7 +430,7 @@ NativeModule::NativeModule(uint32_t num_functions, uint32_t num_imports,
code_table_(num_functions), code_table_(num_functions),
num_imported_functions_(num_imports), num_imported_functions_(num_imports),
compilation_state_(NewCompilationState( compilation_state_(NewCompilationState(
reinterpret_cast<Isolate*>(code_manager->isolate_), this)), reinterpret_cast<Isolate*>(code_manager->isolate_))),
free_memory_(reinterpret_cast<Address>(mem->address()), free_memory_(reinterpret_cast<Address>(mem->address()),
reinterpret_cast<Address>(mem->end())), reinterpret_cast<Address>(mem->end())),
wasm_code_manager_(code_manager), wasm_code_manager_(code_manager),
......
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