Commit bddbdcca authored by Clemens Backes's avatar Clemens Backes Committed by Commit Bot

[wasm] Use Jobs API for wrapper compilation

Migrate wrapper compilation from the tasks API to the job API. This
avoids querying the platform for the number of available threads, and
makes the code much more idiomatic.

R=thibaudm@chromium.org
CC=etiennep@chromium.org

Bug: chromium:1101340
Change-Id: I2d84176fe729c065348fd479fe8fd1a0d2f19a50
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2471379
Commit-Queue: Clemens Backes <clemensb@chromium.org>
Reviewed-by: 's avatarThibaud Michaud <thibaudm@chromium.org>
Cr-Commit-Position: refs/heads/master@{#70620}
parent c75a02c5
...@@ -1510,7 +1510,7 @@ void CompileNativeModule(Isolate* isolate, ...@@ -1510,7 +1510,7 @@ void CompileNativeModule(Isolate* isolate,
} }
} }
class BackgroundCompileJob : public JobTask { class BackgroundCompileJob final : public JobTask {
public: public:
explicit BackgroundCompileJob(std::weak_ptr<NativeModule> native_module, explicit BackgroundCompileJob(std::weak_ptr<NativeModule> native_module,
std::shared_ptr<Counters> async_counters) std::shared_ptr<Counters> async_counters)
...@@ -2644,12 +2644,6 @@ bool AsyncStreamingProcessor::Deserialize(Vector<const uint8_t> module_bytes, ...@@ -2644,12 +2644,6 @@ bool AsyncStreamingProcessor::Deserialize(Vector<const uint8_t> module_bytes,
return true; return true;
} }
// TODO(wasm): Use the jobs API for wrapper compilation, remove this method.
int GetMaxCompileConcurrency() {
int num_worker_threads = V8::GetCurrentPlatform()->NumberOfWorkerThreads();
return std::min(FLAG_wasm_num_compilation_tasks, num_worker_threads);
}
CompilationStateImpl::CompilationStateImpl( CompilationStateImpl::CompilationStateImpl(
const std::shared_ptr<NativeModule>& native_module, const std::shared_ptr<NativeModule>& native_module,
std::shared_ptr<Counters> async_counters) std::shared_ptr<Counters> async_counters)
......
...@@ -68,9 +68,6 @@ bool CompileLazy(Isolate*, NativeModule*, int func_index); ...@@ -68,9 +68,6 @@ bool CompileLazy(Isolate*, NativeModule*, int func_index);
void TriggerTierUp(Isolate*, NativeModule*, int func_index); void TriggerTierUp(Isolate*, NativeModule*, int func_index);
// Get the maximum concurrency for parallel compilation.
int GetMaxCompileConcurrency();
template <typename Key, typename Hash> template <typename Key, typename Hash>
class WrapperQueue { class WrapperQueue {
public: public:
......
...@@ -58,25 +58,32 @@ uint32_t EvalUint32InitExpr(Handle<WasmInstanceObject> instance, ...@@ -58,25 +58,32 @@ uint32_t EvalUint32InitExpr(Handle<WasmInstanceObject> instance,
using ImportWrapperQueue = WrapperQueue<WasmImportWrapperCache::CacheKey, using ImportWrapperQueue = WrapperQueue<WasmImportWrapperCache::CacheKey,
WasmImportWrapperCache::CacheKeyHash>; WasmImportWrapperCache::CacheKeyHash>;
class CompileImportWrapperTask final : public CancelableTask { class CompileImportWrapperJob final : public JobTask {
public: public:
CompileImportWrapperTask( CompileImportWrapperJob(
CancelableTaskManager* task_manager, WasmEngine* engine, WasmEngine* engine, Counters* counters, NativeModule* native_module,
Counters* counters, NativeModule* native_module,
ImportWrapperQueue* queue, ImportWrapperQueue* queue,
WasmImportWrapperCache::ModificationScope* cache_scope) WasmImportWrapperCache::ModificationScope* cache_scope)
: CancelableTask(task_manager), : engine_(engine),
engine_(engine),
counters_(counters), counters_(counters),
native_module_(native_module), native_module_(native_module),
queue_(queue), queue_(queue),
cache_scope_(cache_scope) {} cache_scope_(cache_scope) {}
void RunInternal() override { size_t GetMaxConcurrency(size_t worker_count) const override {
size_t flag_limit =
static_cast<size_t>(std::max(1, FLAG_wasm_num_compilation_tasks));
// Add {worker_count} to the queue size because workers might still be
// processing units that have already been popped from the queue.
return std::min(flag_limit, worker_count + queue_->size());
}
void Run(JobDelegate* delegate) override {
while (base::Optional<WasmImportWrapperCache::CacheKey> key = while (base::Optional<WasmImportWrapperCache::CacheKey> key =
queue_->pop()) { queue_->pop()) {
CompileImportWrapper(engine_, native_module_, counters_, key->kind, CompileImportWrapper(engine_, native_module_, counters_, key->kind,
key->signature, key->expected_arity, cache_scope_); key->signature, key->expected_arity, cache_scope_);
if (delegate->ShouldYield()) return;
} }
} }
...@@ -1467,26 +1474,14 @@ void InstanceBuilder::CompileImportWrappers( ...@@ -1467,26 +1474,14 @@ void InstanceBuilder::CompileImportWrappers(
import_wrapper_queue.insert(key); import_wrapper_queue.insert(key);
} }
CancelableTaskManager task_manager; auto compile_job_task = std::make_unique<CompileImportWrapperJob>(
// TODO(wasm): Switch this to the Jobs API, remove {GetMaxCompileConcurrency}. isolate_->wasm_engine(), isolate_->counters(), native_module,
const int max_background_tasks = GetMaxCompileConcurrency(); &import_wrapper_queue, &cache_scope);
for (int i = 0; i < max_background_tasks; ++i) { auto compile_job = V8::GetCurrentPlatform()->PostJob(
auto task = std::make_unique<CompileImportWrapperTask>( TaskPriority::kUserVisible, std::move(compile_job_task));
&task_manager, isolate_->wasm_engine(), isolate_->counters(),
native_module, &import_wrapper_queue, &cache_scope);
V8::GetCurrentPlatform()->CallOnWorkerThread(std::move(task));
}
// Also compile in the current thread, in case there are no worker threads. // Wait for the job to finish, while contributing in this thread.
while (base::Optional<WasmImportWrapperCache::CacheKey> key = compile_job->Join();
import_wrapper_queue.pop()) {
// TODO(9495): When typed_funcref is enabled, reuse the already compiled
// wrappers inside WasmJSFunctions.
CompileImportWrapper(isolate_->wasm_engine(), native_module,
isolate_->counters(), key->kind, key->signature,
key->expected_arity, &cache_scope);
}
task_manager.CancelAndWait();
} }
// Process the imports, including functions, tables, globals, and memory, in // Process the imports, including functions, tables, globals, and memory, in
......
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