Commit d71bcfcb authored by Michael Starzinger's avatar Michael Starzinger Committed by Commit Bot

[wasm] Make {NativeModule} independent of instance.

This removes the last reference from {NativeModule} that made it
specific to a concrete WebAssembly instance, by only referencing the
{WasmSharedModuleData} instead of a {WasmCompiledModule}. Note that
eventually we want to remove this reference completely to become even
independent of the underlying Isolate soon.

R=clemensh@chromium.org
BUG=v8:7424

Change-Id: I29b8cde8beadeef75c90e90fbff1830f2bf4e636
Reviewed-on: https://chromium-review.googlesource.com/1032433
Commit-Queue: Michael Starzinger <mstarzinger@chromium.org>
Reviewed-by: 's avatarClemens Hammacher <clemensh@chromium.org>
Cr-Commit-Position: refs/heads/master@{#52853}
parent 8ba3f136
This diff is collapsed.
......@@ -211,10 +211,10 @@ bool WasmCode::ShouldBeLogged(Isolate* isolate) {
void WasmCode::LogCode(Isolate* isolate) const {
DCHECK(ShouldBeLogged(isolate));
if (native_module()->compiled_module()->has_shared() && index_.IsJust()) {
if (native_module()->shared_module_data() && index_.IsJust()) {
uint32_t index = this->index();
Handle<WasmSharedModuleData> shared_handle(
native_module()->compiled_module()->shared(), isolate);
native_module()->shared_module_data(), isolate);
int name_length;
Handle<String> name(
WasmSharedModuleData::GetFunctionName(isolate, shared_handle, index));
......@@ -572,19 +572,16 @@ void NativeModule::SetLazyBuiltin(Handle<Code> code) {
}
}
WasmCompiledModule* NativeModule::compiled_module() const {
DCHECK_NOT_NULL(compiled_module_);
return *compiled_module_;
WasmSharedModuleData* NativeModule::shared_module_data() const {
DCHECK_NOT_NULL(shared_module_data_);
return *shared_module_data_;
}
void NativeModule::SetCompiledModule(
Handle<WasmCompiledModule> compiled_module) {
DCHECK_NULL(compiled_module_);
compiled_module_ = compiled_module->GetIsolate()
->global_handles()
->Create(*compiled_module)
.location();
GlobalHandles::MakeWeak(reinterpret_cast<Object***>(&compiled_module_));
void NativeModule::SetSharedModuleData(Handle<WasmSharedModuleData> shared) {
DCHECK_NULL(shared_module_data_);
shared_module_data_ =
shared->GetIsolate()->global_handles()->Create(*shared).location();
GlobalHandles::MakeWeak(reinterpret_cast<Object***>(&shared_module_data_));
}
WasmCode* NativeModule::AddAnonymousCode(Handle<Code> code,
......@@ -860,7 +857,7 @@ WasmCode* NativeModule::GetIndirectlyCallableCode(uint32_t func_index) {
}
#if DEBUG
auto num_imported_functions =
compiled_module()->shared()->module()->num_imported_functions;
shared_module_data()->module()->num_imported_functions;
if (func_index < num_imported_functions) {
DCHECK(!code->IsAnonymous());
}
......@@ -951,11 +948,11 @@ NativeModule::~NativeModule() {
TRACE_HEAP("Deleting native module: %p\n", reinterpret_cast<void*>(this));
// Clear the handle at the beginning of destructor to make it robust against
// potential GCs in the rest of the desctructor.
if (compiled_module_ != nullptr) {
Isolate* isolate = compiled_module()->GetIsolate();
if (shared_module_data_ != nullptr) {
Isolate* isolate = shared_module_data()->GetIsolate();
isolate->global_handles()->Destroy(
reinterpret_cast<Object**>(compiled_module_));
compiled_module_ = nullptr;
reinterpret_cast<Object**>(shared_module_data_));
shared_module_data_ = nullptr;
}
wasm_code_manager_->FreeNativeModuleMemories(this);
}
......
......@@ -269,10 +269,10 @@ class V8_EXPORT_PRIVATE NativeModule final {
CompilationState* compilation_state() { return compilation_state_.get(); }
// TODO(mstarzinger): The link to the {compiled_module} is deprecated and all
// uses should vanish to make {NativeModule} independent of the Isolate.
WasmCompiledModule* compiled_module() const;
void SetCompiledModule(Handle<WasmCompiledModule>);
// TODO(mstarzinger): The link to the {shared_module_data} is deprecated and
// all uses should vanish to make {NativeModule} independent of the Isolate.
WasmSharedModuleData* shared_module_data() const;
void SetSharedModuleData(Handle<WasmSharedModuleData>);
uint32_t num_imported_functions() const { return num_imported_functions_; }
......@@ -341,10 +341,10 @@ class V8_EXPORT_PRIVATE NativeModule final {
std::unique_ptr<CompilationState, CompilationStateDeleter> compilation_state_;
// A phantom reference to the {WasmCompiledModule}. It is intentionally not
// typed {Handle<WasmCompiledModule>} because this location will be cleared
// A phantom reference to the {WasmSharedModuleData}. It is intentionally not
// typed {Handle<WasmSharedModuleData>} because this location will be cleared
// when the phantom reference is cleared.
WasmCompiledModule** compiled_module_ = nullptr;
WasmSharedModuleData** shared_module_data_ = nullptr;
DisjointAllocationPool free_memory_;
DisjointAllocationPool allocated_memory_;
......
......@@ -46,10 +46,9 @@ class PatchDirectCallsHelper {
PatchDirectCallsHelper(NativeModule* native_module, const WasmCode* code)
: source_pos_it(code->source_positions()), decoder(nullptr, nullptr) {
uint32_t func_index = code->index();
WasmCompiledModule* comp_mod = native_module->compiled_module();
func_bytes =
comp_mod->shared()->module_bytes()->GetChars() +
comp_mod->shared()->module()->functions[func_index].code.offset();
WasmSharedModuleData* shared = native_module->shared_module_data();
func_bytes = shared->module_bytes()->GetChars() +
shared->module()->functions[func_index].code.offset();
}
SourcePositionTableIterator source_pos_it;
......@@ -77,10 +76,10 @@ void CodeSpecialization::RelocateDirectCalls(NativeModule* native_module) {
relocate_direct_calls_module_ = native_module;
}
bool CodeSpecialization::ApplyToWholeModule(NativeModule* native_module,
ICacheFlushMode icache_flush_mode) {
bool CodeSpecialization::ApplyToWholeModule(
NativeModule* native_module, Handle<WasmCompiledModule> compiled_module,
ICacheFlushMode icache_flush_mode) {
DisallowHeapAllocation no_gc;
WasmCompiledModule* compiled_module = native_module->compiled_module();
WasmSharedModuleData* shared = compiled_module->shared();
WasmModule* module = shared->module();
std::vector<WasmFunction>* wasm_functions = &shared->module()->functions;
......
......@@ -35,7 +35,7 @@ class CodeSpecialization {
void RelocateDirectCalls(NativeModule* module);
// Apply all relocations and patching to all code in the instance (wasm code
// and exported functions).
bool ApplyToWholeModule(NativeModule*,
bool ApplyToWholeModule(NativeModule*, Handle<WasmCompiledModule>,
ICacheFlushMode = FLUSH_ICACHE_IF_NEEDED);
// Apply all relocations and patching to one wasm code object.
bool ApplyToWasmCode(wasm::WasmCode*,
......
......@@ -1380,7 +1380,6 @@ Handle<WasmCompiledModule> WasmCompiledModule::New(
Managed<wasm::NativeModule>::FromUniquePtr(isolate,
std::move(native_module));
compiled_module->set_native_module(*native_module_wrapper);
compiled_module->GetNativeModule()->SetCompiledModule(compiled_module);
}
// TODO(mtrofin): copy the rest of the specialization parameters over.
......@@ -1412,7 +1411,7 @@ Handle<WasmCompiledModule> WasmCompiledModule::Clone(
Managed<wasm::NativeModule>::FromUniquePtr(isolate,
std::move(native_module));
ret->set_native_module(*native_module_wrapper);
ret->GetNativeModule()->SetCompiledModule(ret);
ret->GetNativeModule()->SetSharedModuleData(handle(module->shared()));
return ret;
}
......
......@@ -712,6 +712,7 @@ MaybeHandle<WasmCompiledModule> DeserializeNativeModule(
Handle<WasmCompiledModule> compiled_module =
WasmCompiledModule::New(isolate, shared->module(), export_wrappers, env);
compiled_module->set_shared(*shared);
compiled_module->GetNativeModule()->SetSharedModuleData(shared);
script->set_wasm_compiled_module(*compiled_module);
NativeModuleDeserializer deserializer(isolate,
compiled_module->GetNativeModule());
......
......@@ -65,10 +65,7 @@ class CWasmEntryArgTester {
Handle<Object> call_args[]{
Handle<Object>::cast(isolate_->factory()->NewForeign(
wasm_code_->instruction_start(), TENURED)),
handle(
wasm_code_->native_module()->compiled_module()->owning_instance(),
isolate_),
buffer_obj};
runner_.builder().instance_object(), buffer_obj};
static_assert(
arraysize(call_args) == compiler::CWasmEntryParameters::kNumParameters,
"adapt this test");
......
......@@ -227,6 +227,7 @@ Handle<WasmInstanceObject> TestingModuleBuilder::InitInstanceObject() {
Handle<WasmCompiledModule> compiled_module =
WasmCompiledModule::New(isolate_, test_module_ptr_, export_wrappers, env);
compiled_module->set_shared(*shared_module_data);
compiled_module->GetNativeModule()->SetSharedModuleData(shared_module_data);
// This method is called when we initialize TestEnvironment. We don't
// have a memory yet, so we won't create it here. We'll update the
// interpreter when we get a memory. We do have globals, though.
......
......@@ -207,9 +207,10 @@ class TestingModuleBuilder {
Address globals_start() { return reinterpret_cast<Address>(globals_data_); }
void Link() {
if (!linked_) {
Handle<WasmCompiledModule> compiled(instance_object()->compiled_module());
CodeSpecialization code_specialization;
code_specialization.RelocateDirectCalls(native_module_);
code_specialization.ApplyToWholeModule(native_module_);
code_specialization.ApplyToWholeModule(native_module_, compiled);
linked_ = true;
native_module_->SetExecutable(true);
}
......
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