Commit 3ffdaaad authored by Kim-Anh Tran's avatar Kim-Anh Tran Committed by Commit Bot

[wasm] Extract compilation state from ModuleCompiler and AsyncCompileJob

The compilation state of a native module is now extracted into its own
datastructure. It reflects which functions are left to compile, and contains
task managers to accomplish parallel and asynchronous compilation.

Bug: 
Change-Id: I45308c7b32ba78e6c83f2d260990846a653bbd9c
Reviewed-on: https://chromium-review.googlesource.com/958865
Commit-Queue: Kim-Anh Tran <kimanh@google.com>
Reviewed-by: 's avatarBen Titzer <titzer@chromium.org>
Reviewed-by: 's avatarAndreas Haas <ahaas@chromium.org>
Reviewed-by: 's avatarClemens Hammacher <clemensh@chromium.org>
Cr-Commit-Position: refs/heads/master@{#52013}
parent 21e77157
......@@ -65,7 +65,6 @@ CancelableTaskManager::TryAbortResult CancelableTaskManager::TryAbort(
return kTaskRemoved;
}
void CancelableTaskManager::CancelAndWait() {
// Clean up all cancelable fore- and background tasks. Tasks are canceled on
// the way if possible, i.e., if they have not started yet. After each round
......
......@@ -2599,7 +2599,7 @@ void Isolate::Deinit() {
optimizing_compile_dispatcher_ = nullptr;
}
wasm_engine()->compilation_manager()->TearDown();
wasm_engine()->TearDown();
heap_.mark_compact_collector()->EnsureSweepingCompleted();
heap_.memory_allocator()->unmapper()->WaitUntilCompleted();
......
This diff is collapsed.
......@@ -22,6 +22,16 @@ namespace wasm {
class ModuleCompiler;
class WasmCode;
class CompilationState;
struct CompilationStateDeleter {
void operator()(CompilationState* compilation_state) const;
};
// Wrapper to create a CompilationState exists in order to avoid having
// the the CompilationState in the header file.
std::unique_ptr<CompilationState, CompilationStateDeleter> NewCompilationState(
Isolate* isolate);
MaybeHandle<WasmModuleObject> CompileToModuleObject(
Isolate* isolate, ErrorThrower* thrower, std::unique_ptr<WasmModule> module,
......@@ -120,7 +130,7 @@ class AsyncCompileJob {
class DecodeModule;
class DecodeFail;
class PrepareAndStartCompile;
class ExecuteAndFinishCompilationUnits;
class CompileFailed;
class WaitForBackgroundTasks;
class FinishCompilationUnits;
class FinishCompile;
......@@ -133,7 +143,7 @@ class AsyncCompileJob {
}
Counters* counters() const { return async_counters().get(); }
void AsyncCompileFailed(ErrorThrower& thrower);
void AsyncCompileFailed(Handle<Object> error_reason);
void AsyncCompileSucceeded(Handle<Object> result);
......@@ -168,16 +178,16 @@ class AsyncCompileJob {
ModuleWireBytes wire_bytes_;
Handle<Context> context_;
Handle<JSPromise> module_promise_;
std::unique_ptr<ModuleCompiler> compiler_;
std::unique_ptr<compiler::ModuleEnv> module_env_;
std::unique_ptr<WasmModule> module_;
std::vector<DeferredHandles*> deferred_handles_;
Handle<WasmModuleObject> module_object_;
Handle<WasmCompiledModule> compiled_module_;
size_t outstanding_units_ = 0;
std::unique_ptr<CompileStep> step_;
CancelableTaskManager background_task_manager_;
Handle<Code> centry_stub_;
std::shared_ptr<v8::TaskRunner> foreground_task_runner_;
std::shared_ptr<v8::TaskRunner> background_task_runner_;
......@@ -205,7 +215,6 @@ class AsyncCompileJob {
// StreamingDecoder.
std::shared_ptr<StreamingDecoder> stream_;
};
} // namespace wasm
} // namespace internal
} // namespace v8
......
......@@ -279,6 +279,8 @@ NativeModule::NativeModule(uint32_t num_functions, uint32_t num_imports,
: instance_id(next_id_.Increment(1)),
code_table_(num_functions),
num_imported_functions_(num_imports),
compilation_state_(NewCompilationState(
reinterpret_cast<Isolate*>(code_manager->isolate_))),
free_memory_(reinterpret_cast<Address>(mem->address()),
reinterpret_cast<Address>(mem->end())),
wasm_code_manager_(code_manager),
......
......@@ -15,6 +15,8 @@
#include "src/trap-handler/trap-handler.h"
#include "src/vector.h"
#include "src/wasm/module-compiler.h"
namespace v8 {
class Isolate;
namespace internal {
......@@ -242,6 +244,8 @@ class V8_EXPORT_PRIVATE NativeModule final {
void ResizeCodeTableForTest(size_t);
void LinkAll();
CompilationState* compilation_state() { return compilation_state_.get(); }
// TODO(mstarzinger): needed until we sort out source positions, which are
// still on the GC-heap.
WasmCompiledModule* compiled_module() const;
......@@ -306,6 +310,8 @@ class V8_EXPORT_PRIVATE NativeModule final {
std::unordered_map<Address, Address, AddressHasher> trampolines_;
std::unordered_map<uint32_t, WasmCode*> stubs_;
std::unique_ptr<CompilationState, CompilationStateDeleter> compilation_state_;
DisjointAllocationPool free_memory_;
DisjointAllocationPool allocated_memory_;
std::list<VirtualMemory> owned_memory_;
......
......@@ -119,6 +119,24 @@ void WasmEngine::AsyncCompile(Isolate* isolate, Handle<JSPromise> promise,
promise);
}
void WasmEngine::Register(CancelableTaskManager* task_manager) {
task_managers_.emplace_back(task_manager);
}
void WasmEngine::Unregister(CancelableTaskManager* task_manager) {
task_managers_.remove(task_manager);
}
void WasmEngine::TearDown() {
// Cancel all registered task managers.
for (auto task_manager : task_managers_) {
task_manager->CancelAndWait();
}
// Cancel all AsyncCompileJobs.
compilation_manager_.TearDown();
}
} // namespace wasm
} // namespace internal
} // namespace v8
......@@ -73,11 +73,23 @@ class V8_EXPORT_PRIVATE WasmEngine {
WasmAllocationTracker* allocation_tracker() { return &allocation_tracker_; }
// We register and unregister CancelableTaskManagers that run
// isolate-dependent tasks. These tasks need to be shutdown if the isolate is
// shut down.
void Register(CancelableTaskManager* task_manager);
void Unregister(CancelableTaskManager* task_manager);
void TearDown();
private:
CompilationManager compilation_manager_;
std::unique_ptr<WasmCodeManager> code_manager_;
WasmAllocationTracker allocation_tracker_;
// Contains all CancelableTaskManagers that run tasks that are dependent
// on the isolate.
std::list<CancelableTaskManager*> task_managers_;
DISALLOW_COPY_AND_ASSIGN(WasmEngine);
};
......
......@@ -151,7 +151,7 @@ TEST_F(DisjointAllocationPoolTest, MergingSkipLargerSrcWithGap) {
CheckLooksLike(a, {{10, 15}, {20, 35}, {36, 40}});
}
class WasmCodeManagerTest : public TestWithIsolate {
class WasmCodeManagerTest : public TestWithContext {
public:
using NativeModulePtr = std::unique_ptr<NativeModule>;
enum ModuleStyle : int { Fixed = 0, Growable = 1 };
......
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