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

[wasm] Remove link from NativeModule to WasmEngine

See discussion after this CL: https://crrev.com/c/1297960
We want to avoid the link from NativeModule to WasmEngine to enforce
encapsulation. If someone needs access to the WasmEngine, we should
give them a direct pointer.

R=titzer@chromium.org

Bug: v8:8217
Change-Id: I5bb6f4bf9b56c43085786d7092151d51bd0ff3ca
Reviewed-on: https://chromium-review.googlesource.com/c/1304433Reviewed-by: 's avatarBen Titzer <titzer@chromium.org>
Commit-Queue: Clemens Hammacher <clemensh@chromium.org>
Cr-Commit-Position: refs/heads/master@{#57076}
parent 2f382887
......@@ -122,7 +122,6 @@ class CompilationStateImpl {
bool has_outstanding_units() const { return outstanding_units_ > 0; }
WasmEngine* wasm_engine() const { return native_module_->wasm_engine(); }
CompileMode compile_mode() const { return compile_mode_; }
WasmFeatures* detected_features() { return &detected_features_; }
......@@ -435,8 +434,9 @@ namespace {
// {CompilationStateImpl} when {Commit} is called.
class CompilationUnitBuilder {
public:
explicit CompilationUnitBuilder(NativeModule* native_module)
: native_module_(native_module) {}
explicit CompilationUnitBuilder(NativeModule* native_module,
WasmEngine* wasm_engine)
: native_module_(native_module), wasm_engine_(wasm_engine) {}
void AddUnit(const WasmFunction* function, uint32_t buffer_offset,
Vector<const uint8_t> bytes) {
......@@ -474,7 +474,7 @@ class CompilationUnitBuilder {
Vector<const uint8_t> bytes,
ExecutionTier mode) {
return base::make_unique<WasmCompilationUnit>(
compilation_state()->wasm_engine(), native_module_,
wasm_engine_, native_module_,
FunctionBody{function->sig, buffer_offset, bytes.begin(), bytes.end()},
function->func_index,
compilation_state()->isolate()->async_counters().get(), mode);
......@@ -485,6 +485,7 @@ class CompilationUnitBuilder {
}
NativeModule* const native_module_;
WasmEngine* const wasm_engine_;
std::vector<std::unique_ptr<WasmCompilationUnit>> baseline_units_;
std::vector<std::unique_ptr<WasmCompilationUnit>> tiering_units_;
};
......@@ -540,10 +541,11 @@ bool FetchAndExecuteCompilationUnit(CompilationEnv* env,
return true;
}
void InitializeCompilationUnits(NativeModule* native_module) {
void InitializeCompilationUnits(NativeModule* native_module,
WasmEngine* wasm_engine) {
ModuleWireBytes wire_bytes(native_module->wire_bytes());
const WasmModule* module = native_module->module();
CompilationUnitBuilder builder(native_module);
CompilationUnitBuilder builder(native_module, wasm_engine);
uint32_t start = module->num_imported_functions;
uint32_t end = start + module->num_declared_functions;
for (uint32_t i = start; i < end; ++i) {
......@@ -622,7 +624,7 @@ void CompileInParallel(Isolate* isolate, NativeModule* native_module,
// {compilation_state}. By adding units to the {compilation_state}, new
// {BackgroundCompileTask} instances are spawned which run on
// background threads.
InitializeCompilationUnits(native_module);
InitializeCompilationUnits(native_module, isolate->wasm_engine());
// 2.a) The background threads and the main thread pick one compilation
// unit at a time and execute the parallel phase of the compilation
......@@ -2601,7 +2603,8 @@ class AsyncCompileJob::PrepareAndStartCompile : public CompileStep {
compilation_state->SetNumberOfFunctionsToCompile(
module_->num_declared_functions);
// Add compilation units and kick off compilation.
InitializeCompilationUnits(job_->native_module_);
InitializeCompilationUnits(job_->native_module_,
job_->isolate()->wasm_engine());
}
}
};
......@@ -2766,8 +2769,8 @@ bool AsyncStreamingProcessor::ProcessCodeSectionHeader(size_t functions_count,
// Set outstanding_finishers_ to 2, because both the AsyncCompileJob and the
// AsyncStreamingProcessor have to finish.
job_->outstanding_finishers_.store(2);
compilation_unit_builder_.reset(
new CompilationUnitBuilder(job_->native_module_));
compilation_unit_builder_.reset(new CompilationUnitBuilder(
job_->native_module_, job_->isolate()->wasm_engine()));
return true;
}
......
......@@ -337,7 +337,7 @@ WasmCode::~WasmCode() {
NativeModule::NativeModule(Isolate* isolate, const WasmFeatures& enabled,
bool can_request_more, VirtualMemory code_space,
WasmEngine* wasm_engine,
WasmCodeManager* code_manager,
std::shared_ptr<const WasmModule> module)
: enabled_features_(enabled),
module_(std::move(module)),
......@@ -345,7 +345,7 @@ NativeModule::NativeModule(Isolate* isolate, const WasmFeatures& enabled,
import_wrapper_cache_(std::unique_ptr<WasmImportWrapperCache>(
new WasmImportWrapperCache(this))),
free_code_space_(code_space.region()),
wasm_engine_(wasm_engine),
code_manager_(code_manager),
can_request_more_memory_(can_request_more),
use_trap_handler_(trap_handler::IsTrapHandlerEnabled() ? kUseTrapHandler
: kNoTrapHandler) {
......@@ -680,15 +680,14 @@ Vector<byte> NativeModule::AllocateForCode(size_t size) {
Address hint = owned_code_space_.empty() ? kNullAddress
: owned_code_space_.back().end();
VirtualMemory new_mem = wasm_engine_->code_manager()->TryAllocate(
size, reinterpret_cast<void*>(hint));
VirtualMemory new_mem =
code_manager_->TryAllocate(size, reinterpret_cast<void*>(hint));
if (!new_mem.IsReserved()) {
V8::FatalProcessOutOfMemory(nullptr,
"NativeModule::AllocateForCode reservation");
UNREACHABLE();
}
wasm_engine_->code_manager()->AssignRanges(new_mem.address(), new_mem.end(),
this);
code_manager_->AssignRanges(new_mem.address(), new_mem.end(), this);
free_code_space_.Merge(new_mem.region());
owned_code_space_.emplace_back(std::move(new_mem));
......@@ -721,7 +720,7 @@ Vector<byte> NativeModule::AllocateForCode(size_t size) {
Address start = std::max(commit_start, vmem.address());
Address end = std::min(commit_end, vmem.end());
size_t commit_size = static_cast<size_t>(end - start);
if (!wasm_engine_->code_manager()->Commit(start, commit_size)) {
if (!code_manager_->Commit(start, commit_size)) {
V8::FatalProcessOutOfMemory(nullptr,
"NativeModule::AllocateForCode commit");
UNREACHABLE();
......@@ -733,8 +732,7 @@ Vector<byte> NativeModule::AllocateForCode(size_t size) {
if (commit_start >= commit_end) break;
}
#else
if (!wasm_engine_->code_manager()->Commit(commit_start,
commit_end - commit_start)) {
if (!code_manager_->Commit(commit_start, commit_end - commit_start)) {
V8::FatalProcessOutOfMemory(nullptr,
"NativeModule::AllocateForCode commit");
UNREACHABLE();
......@@ -802,7 +800,7 @@ NativeModule::~NativeModule() {
// Cancel all background compilation before resetting any field of the
// NativeModule or freeing anything.
compilation_state_->CancelAndWait();
wasm_engine_->code_manager()->FreeNativeModule(this);
code_manager_->FreeNativeModule(this);
}
WasmCodeManager::WasmCodeManager(WasmMemoryTracker* memory_tracker,
......@@ -975,7 +973,7 @@ std::unique_ptr<NativeModule> WasmCodeManager::NewNativeModule(
Address end = code_space.end();
std::unique_ptr<NativeModule> ret(new NativeModule(
isolate, enabled, can_request_more, std::move(code_space),
isolate->wasm_engine(), std::move(module)));
isolate->wasm_engine()->code_manager(), std::move(module)));
TRACE_HEAP("New NativeModule %p: Mem: %" PRIuPTR ",+%zu\n", this, start,
size);
AssignRangesAndAddModule(start, end, ret.get());
......
......@@ -333,7 +333,6 @@ class V8_EXPORT_PRIVATE NativeModule final {
wire_bytes_ = std::move(wire_bytes);
}
const WasmModule* module() const { return module_.get(); }
WasmEngine* wasm_engine() const { return wasm_engine_; }
WasmCode* Lookup(Address) const;
......@@ -353,7 +352,7 @@ class V8_EXPORT_PRIVATE NativeModule final {
NativeModule(Isolate* isolate, const WasmFeatures& enabled_features,
bool can_request_more, VirtualMemory code_space,
WasmEngine* wasm_engine,
WasmCodeManager* code_manager,
std::shared_ptr<const WasmModule> module);
WasmCode* AddAnonymousCode(Handle<Code>, WasmCode::Kind kind,
......@@ -454,7 +453,7 @@ class V8_EXPORT_PRIVATE NativeModule final {
// End of fields protected by {allocation_mutex_}.
//////////////////////////////////////////////////////////////////////////////
WasmEngine* const wasm_engine_;
WasmCodeManager* const code_manager_;
std::atomic<size_t> committed_code_space_{0};
int modification_scope_depth_ = 0;
bool can_request_more_memory_;
......
......@@ -188,7 +188,6 @@ std::shared_ptr<NativeModule> WasmEngine::ExportNativeModule(
Handle<WasmModuleObject> WasmEngine::ImportNativeModule(
Isolate* isolate, std::shared_ptr<NativeModule> shared_module) {
CHECK_EQ(this, shared_module->wasm_engine());
Vector<const byte> wire_bytes = shared_module->wire_bytes();
const WasmModule* module = shared_module->module();
Handle<Script> script =
......
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