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

[wasm] Start sharing JS-to-Wasm wrappers.

This shares JS-to-Wasm wrapper code across instances belonging to the
same module object. We no longer need to copy the wrappers since they
are by now independent of the concrete instance.

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

Change-Id: I54188eae6378e53cc274cd19f8e652ffdba72ee5
Reviewed-on: https://chromium-review.googlesource.com/1049607
Commit-Queue: Michael Starzinger <mstarzinger@chromium.org>
Reviewed-by: 's avatarBen Titzer <titzer@chromium.org>
Cr-Commit-Position: refs/heads/master@{#53058}
parent 55b70e86
......@@ -345,8 +345,7 @@ class InstanceBuilder {
void ProcessExports(Handle<WasmInstanceObject> instance,
Handle<WasmCompiledModule> compiled_module);
void InitializeTables(Handle<WasmInstanceObject> instance,
CodeSpecialization* code_specialization);
void InitializeTables(Handle<WasmInstanceObject> instance);
void LoadTableSegments(Handle<WasmInstanceObject> instance);
};
......@@ -899,14 +898,6 @@ void RecordStats(const wasm::WasmCode* code, Counters* counters) {
static_cast<int>(code->reloc_info().size()));
}
void RecordStats(Handle<FixedArray> functions, Counters* counters) {
DisallowHeapAllocation no_gc;
for (int i = 0; i < functions->length(); ++i) {
Object* val = functions->get(i);
if (val->IsCode()) RecordStats(Code::cast(val), counters);
}
}
void RecordStats(const wasm::NativeModule* native_module, Counters* counters) {
for (uint32_t i = 0, e = native_module->function_count(); i < e; ++i) {
const wasm::WasmCode* code = native_module->code(i);
......@@ -1605,7 +1596,6 @@ MaybeHandle<WasmInstanceObject> InstanceBuilder::Build() {
//--------------------------------------------------------------------------
// Reuse the compiled module (if no owner), otherwise clone.
//--------------------------------------------------------------------------
Handle<FixedArray> export_wrappers;
wasm::NativeModule* native_module = nullptr;
// Root the old instance, if any, in case later allocation causes GC,
// to prevent the finalizer running for the old instance.
......@@ -1624,19 +1614,10 @@ MaybeHandle<WasmInstanceObject> InstanceBuilder::Build() {
TRACE("Cloning from %zu\n", original->GetNativeModule()->instance_id);
compiled_module_ = WasmCompiledModule::Clone(isolate_, original);
native_module = compiled_module_->GetNativeModule();
export_wrappers = handle(compiled_module_->export_wrappers(), isolate_);
for (int i = 0; i < export_wrappers->length(); ++i) {
Handle<Code> orig_code(Code::cast(export_wrappers->get(i)), isolate_);
DCHECK_EQ(orig_code->kind(), Code::JS_TO_WASM_FUNCTION);
Handle<Code> code = factory->CopyCode(orig_code);
export_wrappers->set(i, *code);
}
RecordStats(native_module, counters());
RecordStats(export_wrappers, counters());
} else {
// No instance owned the original compiled module.
compiled_module_ = original;
export_wrappers = handle(compiled_module_->export_wrappers(), isolate_);
native_module = compiled_module_->GetNativeModule();
TRACE("Reusing existing instance %zu\n",
compiled_module_->GetNativeModule()->instance_id);
......@@ -1715,9 +1696,8 @@ MaybeHandle<WasmInstanceObject> InstanceBuilder::Build() {
//--------------------------------------------------------------------------
// Initialize the indirect tables.
//--------------------------------------------------------------------------
CodeSpecialization code_specialization;
if (function_table_count > 0) {
InitializeTables(instance, &code_specialization);
InitializeTables(instance);
}
//--------------------------------------------------------------------------
......@@ -1818,13 +1798,15 @@ MaybeHandle<WasmInstanceObject> InstanceBuilder::Build() {
LoadDataSegments(instance);
}
//--------------------------------------------------------------------------
// Patch all code with the relocations registered in code_specialization.
//--------------------------------------------------------------------------
CodeSpecialization code_specialization;
code_specialization.RelocateDirectCalls(native_module);
code_specialization.ApplyToWholeModule(
native_module, handle(instance->compiled_module()), SKIP_ICACHE_FLUSH);
code_specialization.ApplyToWholeModule(native_module, compiled_module_,
SKIP_ICACHE_FLUSH);
FlushICache(native_module);
FlushICache(export_wrappers);
FlushICache(handle(compiled_module_->export_wrappers()));
//--------------------------------------------------------------------------
// Unpack and notify signal handler of protected instructions.
......@@ -2651,9 +2633,7 @@ void InstanceBuilder::ProcessExports(
}
}
void InstanceBuilder::InitializeTables(
Handle<WasmInstanceObject> instance,
CodeSpecialization* code_specialization) {
void InstanceBuilder::InitializeTables(Handle<WasmInstanceObject> instance) {
size_t table_count = module_->function_tables.size();
for (size_t index = 0; index < table_count; ++index) {
WasmIndirectFunctionTable& table = module_->function_tables[index];
......
......@@ -1380,10 +1380,6 @@ Handle<WasmCompiledModule> WasmCompiledModule::Clone(
ret->set_weak_owning_instance(isolate->heap()->empty_weak_cell());
ret->set_native_module(module->native_module());
Handle<FixedArray> export_copy = isolate->factory()->CopyFixedArray(
handle(module->export_wrappers(), isolate));
ret->set_export_wrappers(*export_copy);
// construct the wrapper in 2 steps, because its construction may trigger GC,
// which would shift the this pointer in set_native_module.
Handle<Foreign> native_module_wrapper =
......
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