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

[wasm] Don't store AsyncCompileJob in CompileStep

The CompileStep is only invoked via the {AsyncCompileJob} that owns it,
so we can just pass a pointer to the AsyncCompileJob instead of storing
it in the step itself.

R=ahaas@chromium.org

Bug: v8:8238, v8:7921
Change-Id: I92eda222ace2d5fef5af7663175c62fa7601810c
Reviewed-on: https://chromium-review.googlesource.com/c/1319759
Commit-Queue: Clemens Hammacher <clemensh@chromium.org>
Reviewed-by: 's avatarAndreas Haas <ahaas@chromium.org>
Cr-Commit-Position: refs/heads/master@{#57291}
parent ab5b52f9
...@@ -2412,21 +2412,19 @@ class AsyncCompileJob::CompileStep { ...@@ -2412,21 +2412,19 @@ class AsyncCompileJob::CompileStep {
public: public:
virtual ~CompileStep() = default; virtual ~CompileStep() = default;
void Run(bool on_foreground) { void Run(AsyncCompileJob* job, bool on_foreground) {
if (on_foreground) { if (on_foreground) {
HandleScope scope(job_->isolate_); HandleScope scope(job->isolate_);
SaveContext saved_context(job_->isolate_); SaveContext saved_context(job->isolate_);
job_->isolate_->set_context(*job_->native_context_); job->isolate_->set_context(*job->native_context_);
RunInForeground(); RunInForeground(job);
} else { } else {
RunInBackground(); RunInBackground(job);
} }
} }
virtual void RunInForeground() { UNREACHABLE(); } virtual void RunInForeground(AsyncCompileJob*) { UNREACHABLE(); }
virtual void RunInBackground() { UNREACHABLE(); } virtual void RunInBackground(AsyncCompileJob*) { UNREACHABLE(); }
AsyncCompileJob* job_ = nullptr;
}; };
class AsyncCompileJob::CompileTask : public CancelableTask { class AsyncCompileJob::CompileTask : public CancelableTask {
...@@ -2448,7 +2446,7 @@ class AsyncCompileJob::CompileTask : public CancelableTask { ...@@ -2448,7 +2446,7 @@ class AsyncCompileJob::CompileTask : public CancelableTask {
void RunInternal() final { void RunInternal() final {
if (!job_) return; if (!job_) return;
if (on_foreground_) ResetPendingForegroundTask(); if (on_foreground_) ResetPendingForegroundTask();
job_->step_->Run(on_foreground_); job_->step_->Run(job_, on_foreground_);
// After execution, reset {job_} such that we don't try to reset the pending // After execution, reset {job_} such that we don't try to reset the pending
// foreground task when the task is deleted. // foreground task when the task is deleted.
job_ = nullptr; job_ = nullptr;
...@@ -2525,7 +2523,6 @@ void AsyncCompileJob::DoAsync(Args&&... args) { ...@@ -2525,7 +2523,6 @@ void AsyncCompileJob::DoAsync(Args&&... args) {
template <typename Step, typename... Args> template <typename Step, typename... Args>
void AsyncCompileJob::NextStep(Args&&... args) { void AsyncCompileJob::NextStep(Args&&... args) {
step_.reset(new Step(std::forward<Args>(args)...)); step_.reset(new Step(std::forward<Args>(args)...));
step_->job_ = this;
} }
//========================================================================== //==========================================================================
...@@ -2535,7 +2532,7 @@ class AsyncCompileJob::DecodeModule : public AsyncCompileJob::CompileStep { ...@@ -2535,7 +2532,7 @@ class AsyncCompileJob::DecodeModule : public AsyncCompileJob::CompileStep {
public: public:
explicit DecodeModule(Counters* counters) : counters_(counters) {} explicit DecodeModule(Counters* counters) : counters_(counters) {}
void RunInBackground() override { void RunInBackground(AsyncCompileJob* job) override {
ModuleResult result; ModuleResult result;
{ {
DisallowHandleAllocation no_handle; DisallowHandleAllocation no_handle;
...@@ -2543,16 +2540,16 @@ class AsyncCompileJob::DecodeModule : public AsyncCompileJob::CompileStep { ...@@ -2543,16 +2540,16 @@ class AsyncCompileJob::DecodeModule : public AsyncCompileJob::CompileStep {
// Decode the module bytes. // Decode the module bytes.
TRACE_COMPILE("(1) Decoding module...\n"); TRACE_COMPILE("(1) Decoding module...\n");
result = DecodeWasmModule( result = DecodeWasmModule(
job_->enabled_features_, job_->wire_bytes_.start(), job->enabled_features_, job->wire_bytes_.start(),
job_->wire_bytes_.end(), false, kWasmOrigin, counters_, job->wire_bytes_.end(), false, kWasmOrigin, counters_,
job_->isolate()->wasm_engine()->allocator()); job->isolate()->wasm_engine()->allocator());
} }
if (result.failed()) { if (result.failed()) {
// Decoding failure; reject the promise and clean up. // Decoding failure; reject the promise and clean up.
job_->DoSync<DecodeFail>(std::move(result)); job->DoSync<DecodeFail>(std::move(result));
} else { } else {
// Decode passed. // Decode passed.
job_->DoSync<PrepareAndStartCompile>(std::move(result).value(), true); job->DoSync<PrepareAndStartCompile>(std::move(result).value(), true);
} }
} }
...@@ -2569,12 +2566,12 @@ class AsyncCompileJob::DecodeFail : public CompileStep { ...@@ -2569,12 +2566,12 @@ class AsyncCompileJob::DecodeFail : public CompileStep {
private: private:
ModuleResult result_; ModuleResult result_;
void RunInForeground() override { void RunInForeground(AsyncCompileJob* job) override {
TRACE_COMPILE("(1b) Decoding failed.\n"); TRACE_COMPILE("(1b) Decoding failed.\n");
ErrorThrower thrower(job_->isolate_, "AsyncCompile"); ErrorThrower thrower(job->isolate_, "AsyncCompile");
thrower.CompileFailed("Wasm decoding failed", result_); thrower.CompileFailed("Wasm decoding failed", result_);
// {job_} is deleted in AsyncCompileFailed, therefore the {return}. // {job_} is deleted in AsyncCompileFailed, therefore the {return}.
return job_->AsyncCompileFailed(thrower.Reify()); return job->AsyncCompileFailed(thrower.Reify());
} }
}; };
...@@ -2591,32 +2588,27 @@ class AsyncCompileJob::PrepareAndStartCompile : public CompileStep { ...@@ -2591,32 +2588,27 @@ class AsyncCompileJob::PrepareAndStartCompile : public CompileStep {
std::shared_ptr<const WasmModule> module_; std::shared_ptr<const WasmModule> module_;
bool start_compilation_; bool start_compilation_;
void RunInForeground() override { void RunInForeground(AsyncCompileJob* job) override {
TRACE_COMPILE("(2) Prepare and start compile...\n"); TRACE_COMPILE("(2) Prepare and start compile...\n");
// Make sure all compilation tasks stopped running. Decoding (async step) // Make sure all compilation tasks stopped running. Decoding (async step)
// is done. // is done.
job_->background_task_manager_.CancelAndWait(); job->background_task_manager_.CancelAndWait();
job_->PrepareRuntimeObjects(module_); job->PrepareRuntimeObjects(module_);
size_t num_functions = size_t num_functions =
module_->functions.size() - module_->num_imported_functions; module_->functions.size() - module_->num_imported_functions;
if (num_functions == 0) { if (num_functions == 0) {
// Degenerate case of an empty module. // Degenerate case of an empty module.
job_->FinishCompile(true); job->FinishCompile(true);
return; return;
} }
CompilationStateImpl* compilation_state = CompilationStateImpl* compilation_state =
Impl(job_->native_module_->compilation_state()); Impl(job->native_module_->compilation_state());
{ {
// Instance field {job_} cannot be captured by copy, therefore
// we need to add a local helper variable {job}. We want to
// capture the {job} pointer by copy, as it otherwise is dependent
// on the current step we are in.
AsyncCompileJob* job = job_;
compilation_state->SetCallback( compilation_state->SetCallback(
[job](CompilationEvent event, const VoidResult* error_result) { [job](CompilationEvent event, const VoidResult* error_result) {
// Callback is called from a foreground thread. // Callback is called from a foreground thread.
...@@ -2672,8 +2664,8 @@ class AsyncCompileJob::PrepareAndStartCompile : public CompileStep { ...@@ -2672,8 +2664,8 @@ class AsyncCompileJob::PrepareAndStartCompile : public CompileStep {
compilation_state->SetNumberOfFunctionsToCompile( compilation_state->SetNumberOfFunctionsToCompile(
module_->num_declared_functions); module_->num_declared_functions);
// Add compilation units and kick off compilation. // Add compilation units and kick off compilation.
InitializeCompilationUnits(job_->native_module_, InitializeCompilationUnits(job->native_module_,
job_->isolate()->wasm_engine()); job->isolate()->wasm_engine());
} }
} }
}; };
...@@ -2686,9 +2678,9 @@ class AsyncCompileJob::CompileFailed : public CompileStep { ...@@ -2686,9 +2678,9 @@ class AsyncCompileJob::CompileFailed : public CompileStep {
explicit CompileFailed(Handle<Object> error_reason) explicit CompileFailed(Handle<Object> error_reason)
: error_reason_(error_reason) {} : error_reason_(error_reason) {}
void RunInForeground() override { void RunInForeground(AsyncCompileJob* job) override {
TRACE_COMPILE("(4b) Compilation Failed...\n"); TRACE_COMPILE("(4b) Compilation Failed...\n");
return job_->AsyncCompileFailed(error_reason_); return job->AsyncCompileFailed(error_reason_);
} }
private: private:
...@@ -2701,13 +2693,13 @@ class AsyncCompileJob::CompileFailed : public CompileStep { ...@@ -2701,13 +2693,13 @@ class AsyncCompileJob::CompileFailed : public CompileStep {
class AsyncCompileJob::CompileWrappers : public CompileStep { class AsyncCompileJob::CompileWrappers : public CompileStep {
// TODO(wasm): Compile all wrappers here, including the start function wrapper // TODO(wasm): Compile all wrappers here, including the start function wrapper
// and the wrappers for the function table elements. // and the wrappers for the function table elements.
void RunInForeground() override { void RunInForeground(AsyncCompileJob* job) override {
TRACE_COMPILE("(5) Compile wrappers...\n"); TRACE_COMPILE("(5) Compile wrappers...\n");
// TODO(6792): No longer needed once WebAssembly code is off heap. // TODO(6792): No longer needed once WebAssembly code is off heap.
CodeSpaceMemoryModificationScope modification_scope(job_->isolate_->heap()); CodeSpaceMemoryModificationScope modification_scope(job->isolate_->heap());
// Compile JS->wasm wrappers for exported functions. // Compile JS->wasm wrappers for exported functions.
CompileJsToWasmWrappers(job_->isolate_, job_->module_object_); CompileJsToWasmWrappers(job->isolate_, job->module_object_);
job_->DoSync<FinishModule>(); job->DoSync<FinishModule>();
} }
}; };
...@@ -2715,23 +2707,23 @@ class AsyncCompileJob::CompileWrappers : public CompileStep { ...@@ -2715,23 +2707,23 @@ class AsyncCompileJob::CompileWrappers : public CompileStep {
// Step 6 (sync): Finish the module and resolve the promise. // Step 6 (sync): Finish the module and resolve the promise.
//========================================================================== //==========================================================================
class AsyncCompileJob::FinishModule : public CompileStep { class AsyncCompileJob::FinishModule : public CompileStep {
void RunInForeground() override { void RunInForeground(AsyncCompileJob* job) override {
TRACE_COMPILE("(6) Finish module...\n"); TRACE_COMPILE("(6) Finish module...\n");
job_->AsyncCompileSucceeded(job_->module_object_); job->AsyncCompileSucceeded(job->module_object_);
size_t num_functions = job_->native_module_->num_functions() - size_t num_functions = job->native_module_->num_functions() -
job_->native_module_->num_imported_functions(); job->native_module_->num_imported_functions();
auto* compilation_state = Impl(job_->native_module_->compilation_state()); auto* compilation_state = Impl(job->native_module_->compilation_state());
if (compilation_state->compile_mode() == CompileMode::kRegular || if (compilation_state->compile_mode() == CompileMode::kRegular ||
num_functions == 0) { num_functions == 0) {
// If we do not tier up, the async compile job is done here and // If we do not tier up, the async compile job is done here and
// can be deleted. // can be deleted.
job_->isolate_->wasm_engine()->RemoveCompileJob(job_); job->isolate_->wasm_engine()->RemoveCompileJob(job);
return; return;
} }
DCHECK_EQ(CompileMode::kTiering, compilation_state->compile_mode()); DCHECK_EQ(CompileMode::kTiering, compilation_state->compile_mode());
if (!compilation_state->has_outstanding_units()) { if (!compilation_state->has_outstanding_units()) {
job_->isolate_->wasm_engine()->RemoveCompileJob(job_); job->isolate_->wasm_engine()->RemoveCompileJob(job);
} }
} }
}; };
......
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