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

[wasm] Run sync compilation with kUserBlocking priority

With the recent switch from the tasks API to the jobs API for thread
management within WebAssembly compilation we got the problem that
TurboFan compilation of a previous compilation can block the Liftoff
compilation of a new compilation. With synchronous compilation, this can
cause significant delays for the user. With this CL we increase the
priority of synchronous compilation so that at least synchronous
compilation can only be blocked by other synchronous compilation. This
fixes issues that we saw on autocad.

Why is it okay to increase the priority of synchronous compilation?
* On the main thread, synchronous compilation is only allowed for small
  modules which should not take long to compile. Also, the compilation
  blocks the main thread and should finish as fast as possible.
* On worker threads, delaying other background work is not such a big
  issue.

Downsides:
* This does not only increase the priority of the initial compilation,
  but also for the TurboFan optimization. Similar to above, for small
  modules on the main thread this should not be a big deal because
  compilation is fast. Big modules can only be compiled on worker
  threads.

Note that this is supposed to be a fix for the problems we see at the
moment with autocad on stable and beta. Eventually compilation job
management should be done by the wasm engine for all WebAssembly
compilation, so that not each WebAssemly module has to do its own
compilation job management.

R=clemensb@chromium.org

Bug: chromium:1142686, v8:11088
Change-Id: Iee4948b2fcad944f587918e9452e6888258857f9
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2512911
Commit-Queue: Andreas Haas <ahaas@chromium.org>
Reviewed-by: 's avatarClemens Backes <clemensb@chromium.org>
Cr-Commit-Position: refs/heads/master@{#70923}
parent cf3a842e
...@@ -131,6 +131,9 @@ class V8_EXPORT_PRIVATE CompilationState { ...@@ -131,6 +131,9 @@ class V8_EXPORT_PRIVATE CompilationState {
// Wait until top tier compilation finished, or compilation failed. // Wait until top tier compilation finished, or compilation failed.
void WaitForTopTierFinished(); void WaitForTopTierFinished();
// Set a higher priority for the compilation job.
void SetHighPriority();
bool failed() const; bool failed() const;
bool baseline_compilation_finished() const; bool baseline_compilation_finished() const;
bool top_tier_compilation_finished() const; bool top_tier_compilation_finished() const;
......
...@@ -595,6 +595,8 @@ class CompilationStateImpl { ...@@ -595,6 +595,8 @@ class CompilationStateImpl {
void WaitForCompilationEvent(CompilationEvent event); void WaitForCompilationEvent(CompilationEvent event);
void SetHighPriority() { has_priority_ = true; }
bool failed() const { bool failed() const {
return compile_failed_.load(std::memory_order_relaxed); return compile_failed_.load(std::memory_order_relaxed);
} }
...@@ -663,6 +665,8 @@ class CompilationStateImpl { ...@@ -663,6 +665,8 @@ class CompilationStateImpl {
std::vector<std::shared_ptr<JSToWasmWrapperCompilationUnit>> std::vector<std::shared_ptr<JSToWasmWrapperCompilationUnit>>
js_to_wasm_wrapper_units_; js_to_wasm_wrapper_units_;
bool has_priority_ = false;
// This mutex protects all information of this {CompilationStateImpl} which is // This mutex protects all information of this {CompilationStateImpl} which is
// being accessed concurrently. // being accessed concurrently.
mutable base::Mutex mutex_; mutable base::Mutex mutex_;
...@@ -792,6 +796,8 @@ void CompilationState::WaitForTopTierFinished() { ...@@ -792,6 +796,8 @@ void CompilationState::WaitForTopTierFinished() {
top_tier_finished_semaphore->Wait(); top_tier_finished_semaphore->Wait();
} }
void CompilationState::SetHighPriority() { Impl(this)->SetHighPriority(); }
void CompilationState::InitializeAfterDeserialization() { void CompilationState::InitializeAfterDeserialization() {
Impl(this)->InitializeCompilationProgressAfterDeserialization(); Impl(this)->InitializeCompilationProgressAfterDeserialization();
} }
...@@ -1614,6 +1620,8 @@ std::shared_ptr<NativeModule> CompileToNativeModule( ...@@ -1614,6 +1620,8 @@ std::shared_ptr<NativeModule> CompileToNativeModule(
native_module = isolate->wasm_engine()->NewNativeModule( native_module = isolate->wasm_engine()->NewNativeModule(
isolate, enabled, module, code_size_estimate); isolate, enabled, module, code_size_estimate);
native_module->SetWireBytes(std::move(wire_bytes_copy)); native_module->SetWireBytes(std::move(wire_bytes_copy));
// Sync compilation is user blocking, so we increase the priority.
native_module->compilation_state()->SetHighPriority();
v8::metrics::Recorder::ContextId context_id = v8::metrics::Recorder::ContextId context_id =
isolate->GetOrRegisterRecorderContextId(isolate->native_context()); isolate->GetOrRegisterRecorderContextId(isolate->native_context());
...@@ -3211,8 +3219,13 @@ void CompilationStateImpl::ScheduleCompileJobForNewUnits() { ...@@ -3211,8 +3219,13 @@ void CompilationStateImpl::ScheduleCompileJobForNewUnits() {
async_counters_); async_counters_);
// TODO(wasm): Lower priority for TurboFan-only jobs. // TODO(wasm): Lower priority for TurboFan-only jobs.
current_compile_job_ = V8::GetCurrentPlatform()->PostJob( current_compile_job_ = V8::GetCurrentPlatform()->PostJob(
TaskPriority::kUserVisible, std::move(new_compile_job)); has_priority_ ? TaskPriority::kUserBlocking : TaskPriority::kUserVisible,
std::move(new_compile_job));
native_module_->engine()->ShepherdCompileJobHandle(current_compile_job_); native_module_->engine()->ShepherdCompileJobHandle(current_compile_job_);
// Reset the priority. Later uses of the compilation state, e.g. for
// debugging, should compile with the default priority again.
has_priority_ = false;
} }
size_t CompilationStateImpl::NumOutstandingCompilations() const { size_t CompilationStateImpl::NumOutstandingCompilations() const {
......
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