From 7fa695a2e4a3103d82918b60876bd4d1eb429c6a Mon Sep 17 00:00:00 2001 From: Andreas Haas Date: Mon, 21 Dec 2020 11:24:38 +0100 Subject: [PATCH] [wasm] Fix --single-threaded for WebAssembly compilation Due to the transition to the jobs API, WebAssembly compilation was using background threads, even when --single-threaded and therefore --wasm-num-compilation-tasks=0 was used. With this CL, the compilation job is started with a maximum concurrency of 0 when --wasm-num-compilation-tasks=0. To ensure compilation progress in asynchronous compilation, the main thread waits for baseline compilation to finish right after initializing all compilation units, and thereby participates in the compilation. R=clemensb@chromium.org Bug: v8:11279 Change-Id: I85f93f82c00cdbd6afd46110599089a052101a00 Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2599546 Reviewed-by: Clemens Backes Commit-Queue: Andreas Haas Cr-Commit-Position: refs/heads/master@{#71944} --- src/wasm/module-compiler.cc | 11 +++++--- .../wasm/single-threaded-compilation.js | 27 +++++++++++++++++++ 2 files changed, 35 insertions(+), 3 deletions(-) create mode 100644 test/mjsunit/wasm/single-threaded-compilation.js diff --git a/src/wasm/module-compiler.cc b/src/wasm/module-compiler.cc index f83e2d2e84..81df72faa5 100644 --- a/src/wasm/module-compiler.cc +++ b/src/wasm/module-compiler.cc @@ -1611,10 +1611,8 @@ class BackgroundCompileJob final : public JobTask { if (compile_scope.cancelled()) return 0; // NumOutstandingCompilations() does not reflect the units that running // workers are processing, thus add the current worker count to that number. - size_t flag_limit = - static_cast(std::max(1, FLAG_wasm_num_compilation_tasks)); return std::min( - flag_limit, + static_cast(FLAG_wasm_num_compilation_tasks), worker_count + compile_scope.compilation_state()->NumOutstandingCompilations()); } @@ -2319,6 +2317,13 @@ class AsyncCompileJob::PrepareAndStartCompile : public CompileStep { // Add compilation units and kick off compilation. InitializeCompilationUnits(job->isolate(), job->native_module_.get()); + // We are in single-threaded mode, so there are no worker tasks that will + // do the compilation. We call {WaitForCompilationEvent} here so that the + // main thread paticipates and finishes the compilation. + if (FLAG_wasm_num_compilation_tasks == 0) { + compilation_state->WaitForCompilationEvent( + CompilationEvent::kFinishedBaselineCompilation); + } } } }; diff --git a/test/mjsunit/wasm/single-threaded-compilation.js b/test/mjsunit/wasm/single-threaded-compilation.js new file mode 100644 index 0000000000..fa98768792 --- /dev/null +++ b/test/mjsunit/wasm/single-threaded-compilation.js @@ -0,0 +1,27 @@ +// Copyright 2020 the V8 project authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Flags: --wasm-num-compilation-tasks=0 + +load('test/mjsunit/wasm/wasm-module-builder.js'); + +(function testSyncCompilation() { + print(arguments.callee.name); + const builder = new WasmModuleBuilder(); + builder.addFunction("main", kSig_d_d) + .addBody([kExprLocalGet, 0]) + .exportFunc(); + + const instance = builder.instantiate(); +})(); + +(function testAsyncCompilation() { + print(arguments.callee.name); + const builder = new WasmModuleBuilder(); + builder.addFunction("main", kSig_i_i) + .addBody([kExprLocalGet, 0]) + .exportFunc(); + + const instance = builder.asyncInstantiate(); +})();