From a7e8c320c6f6f173e1c05421d40163a46b942ee4 Mon Sep 17 00:00:00 2001 From: Andreas Haas Date: Mon, 2 Nov 2020 16:01:46 +0100 Subject: [PATCH] [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 Reviewed-by: Clemens Backes Cr-Commit-Position: refs/heads/master@{#70923} --- src/wasm/compilation-environment.h | 3 +++ src/wasm/module-compiler.cc | 15 ++++++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/wasm/compilation-environment.h b/src/wasm/compilation-environment.h index 0eb0ba3497..d730161ad1 100644 --- a/src/wasm/compilation-environment.h +++ b/src/wasm/compilation-environment.h @@ -131,6 +131,9 @@ class V8_EXPORT_PRIVATE CompilationState { // Wait until top tier compilation finished, or compilation failed. void WaitForTopTierFinished(); + // Set a higher priority for the compilation job. + void SetHighPriority(); + bool failed() const; bool baseline_compilation_finished() const; bool top_tier_compilation_finished() const; diff --git a/src/wasm/module-compiler.cc b/src/wasm/module-compiler.cc index 94bfd3b5b1..ff4367e75f 100644 --- a/src/wasm/module-compiler.cc +++ b/src/wasm/module-compiler.cc @@ -595,6 +595,8 @@ class CompilationStateImpl { void WaitForCompilationEvent(CompilationEvent event); + void SetHighPriority() { has_priority_ = true; } + bool failed() const { return compile_failed_.load(std::memory_order_relaxed); } @@ -663,6 +665,8 @@ class CompilationStateImpl { std::vector> js_to_wasm_wrapper_units_; + bool has_priority_ = false; + // This mutex protects all information of this {CompilationStateImpl} which is // being accessed concurrently. mutable base::Mutex mutex_; @@ -792,6 +796,8 @@ void CompilationState::WaitForTopTierFinished() { top_tier_finished_semaphore->Wait(); } +void CompilationState::SetHighPriority() { Impl(this)->SetHighPriority(); } + void CompilationState::InitializeAfterDeserialization() { Impl(this)->InitializeCompilationProgressAfterDeserialization(); } @@ -1614,6 +1620,8 @@ std::shared_ptr CompileToNativeModule( native_module = isolate->wasm_engine()->NewNativeModule( isolate, enabled, module, code_size_estimate); 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 = isolate->GetOrRegisterRecorderContextId(isolate->native_context()); @@ -3211,8 +3219,13 @@ void CompilationStateImpl::ScheduleCompileJobForNewUnits() { async_counters_); // TODO(wasm): Lower priority for TurboFan-only jobs. 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_); + + // 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 {