Commit e4bff229 authored by Clemens Hammacher's avatar Clemens Hammacher Committed by Commit Bot

[wasm] Reduce number of background compile scopes

This reduces the number of {BackgroundCompileScope}s per compilation
unit from 2 to 1. This reduces the number of concurrent accesses to the
{SharedMutex}.

R=ahaas@chromium.org

Bug: v8:8916
Cq-Include-Trybots: luci.v8.try:v8_linux64_tsan_rel
Change-Id: I872683101dfc9361aa7054ea67f16e3f3f2b82b1
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1495989
Commit-Queue: Clemens Hammacher <clemensh@chromium.org>
Reviewed-by: 's avatarAndreas Haas <ahaas@chromium.org>
Cr-Commit-Position: refs/heads/master@{#60034}
parent 2222a9d6
...@@ -646,67 +646,76 @@ class BackgroundCompileTask : public CancelableTask { ...@@ -646,67 +646,76 @@ class BackgroundCompileTask : public CancelableTask {
TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("v8.wasm"), TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("v8.wasm"),
"BackgroundCompileTask::RunInternal"); "BackgroundCompileTask::RunInternal");
// These fields are initialized before getting the first unit of work. double deadline = MonotonicallyIncreasingTimeInMs() + 50.0;
// These fields are initialized in a {BackgroundCompileScope} before
// starting compilation.
base::Optional<CompilationEnv> env; base::Optional<CompilationEnv> env;
std::shared_ptr<WireBytesStorage> wire_bytes; std::shared_ptr<WireBytesStorage> wire_bytes;
std::shared_ptr<const WasmModule> module; std::shared_ptr<const WasmModule> module;
std::unique_ptr<WasmCompilationUnit> unit;
WasmFeatures detected_features = kNoWasmFeatures; WasmFeatures detected_features = kNoWasmFeatures;
double deadline = MonotonicallyIncreasingTimeInMs() + 50.0;
while (true) { // Preparation (synchronized): Initialize the fields above and get the first
// Step 1 (synchronized): Get a WasmCompilationUnit, and initialize some // compilation unit.
// fields if this is the first unit executed by this task. {
std::unique_ptr<WasmCompilationUnit> unit; BackgroundCompileScope compile_scope(token_);
{ if (compile_scope.cancelled()) return;
BackgroundCompileScope compile_scope(token_); env.emplace(compile_scope.native_module()->CreateCompilationEnv());
if (compile_scope.cancelled()) return; wire_bytes = compile_scope.compilation_state()->GetWireBytesStorage();
if (!env.has_value()) { module = compile_scope.native_module()->shared_module();
env.emplace(compile_scope.native_module()->CreateCompilationEnv()); unit = compile_scope.compilation_state()->GetNextCompilationUnit();
wire_bytes = compile_scope.compilation_state()->GetWireBytesStorage(); if (unit == nullptr) {
module = compile_scope.native_module()->shared_module(); compile_scope.compilation_state()->OnBackgroundTaskStopped(
} detected_features);
unit = compile_scope.compilation_state()->GetNextCompilationUnit(); return;
if (unit == nullptr) {
compile_scope.compilation_state()->OnBackgroundTaskStopped(
detected_features);
return;
}
} }
}
// Step 2: Execute the compilation. bool compilation_failed = false;
while (true) {
// (asynchronous): Execute the compilation.
WasmCompilationResult result = unit->ExecuteCompilation( WasmCompilationResult result = unit->ExecuteCompilation(
&env.value(), wire_bytes, async_counters_.get(), &detected_features); &env.value(), wire_bytes, async_counters_.get(), &detected_features);
// Step 3 (synchronized): Publish the compilation result. // (synchronized): Publish the compilation result and get the next unit.
bool cancel_compilation = false;
{ {
BackgroundCompileScope compile_scope(token_); BackgroundCompileScope compile_scope(token_);
if (compile_scope.cancelled()) return; if (compile_scope.cancelled()) return;
WasmCode* code = WasmCode* code =
unit->Publish(std::move(result), compile_scope.native_module()); unit->Publish(std::move(result), compile_scope.native_module());
if (code == nullptr) { if (code == nullptr) {
// Compile error.
compile_scope.compilation_state()->OnBackgroundTaskStopped( compile_scope.compilation_state()->OnBackgroundTaskStopped(
detected_features); detected_features);
// Also, cancel all remaining compilation. compilation_failed = true;
cancel_compilation = true; break;
} else { }
compile_scope.compilation_state()->OnFinishedUnit(
unit->requested_tier(), code); // Successfully finished one unit.
if (deadline < MonotonicallyIncreasingTimeInMs()) { compile_scope.compilation_state()->OnFinishedUnit(
compile_scope.compilation_state()->ReportDetectedFeatures( unit->requested_tier(), code);
detected_features); if (deadline < MonotonicallyIncreasingTimeInMs()) {
compile_scope.compilation_state()->RestartBackgroundCompileTask(); compile_scope.compilation_state()->ReportDetectedFeatures(
return; detected_features);
} compile_scope.compilation_state()->RestartBackgroundCompileTask();
return;
}
// Get next unit.
unit = compile_scope.compilation_state()->GetNextCompilationUnit();
if (unit == nullptr) {
compile_scope.compilation_state()->OnBackgroundTaskStopped(
detected_features);
return;
} }
}
if (cancel_compilation) {
token_->Cancel();
return;
} }
} }
UNREACHABLE(); // Loop exits via explicit return. // We only get here if compilation failed. Other exits return directly.
DCHECK(compilation_failed);
USE(compilation_failed);
token_->Cancel();
} }
private: private:
......
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