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

[wasm] Hide code copying from Wasm compiler interface.

This refactoring hides the fact that some wrappers are first generated
in the GC'ed heap and then copied into the native module. It is a first
step towards avoiding the redundant copy.

R=clemensh@chromium.org

Change-Id: I531fa42e8b4c210948d306624007348a39b981e0
Reviewed-on: https://chromium-review.googlesource.com/c/1333673
Commit-Queue: Michael Starzinger <mstarzinger@chromium.org>
Reviewed-by: 's avatarClemens Hammacher <clemensh@chromium.org>
Cr-Commit-Position: refs/heads/master@{#57477}
parent 1ed5214c
......@@ -5031,7 +5031,8 @@ WasmImportCallKind GetWasmImportCallKind(Handle<JSReceiver> target,
return WasmImportCallKind::kUseCallBuiltin;
}
MaybeHandle<Code> CompileWasmImportCallWrapper(Isolate* isolate,
wasm::WasmCode* CompileWasmImportCallWrapper(Isolate* isolate,
wasm::NativeModule* native_module,
WasmImportCallKind kind,
wasm::FunctionSig* sig,
bool source_positions) {
......@@ -5080,10 +5081,7 @@ MaybeHandle<Code> CompileWasmImportCallWrapper(Isolate* isolate,
MaybeHandle<Code> maybe_code = Pipeline::GenerateCodeForWasmStub(
isolate, incoming, &graph, Code::WASM_TO_JS_FUNCTION, func_name,
AssemblerOptions::Default(isolate), source_position_table);
Handle<Code> code;
if (!maybe_code.ToHandle(&code)) {
return maybe_code;
}
Handle<Code> code = maybe_code.ToHandleChecked();
#ifdef ENABLE_DISASSEMBLER
if (FLAG_print_opt_code) {
CodeTracer::Scope tracing_scope(isolate->GetCodeTracer());
......@@ -5097,10 +5095,14 @@ MaybeHandle<Code> CompileWasmImportCallWrapper(Isolate* isolate,
func_name);
}
return code;
// TODO(wasm): No need to compile the code onto the heap and copy back.
wasm::WasmCode* wasm_code = native_module->AddImportCallWrapper(code);
return wasm_code;
}
MaybeHandle<Code> CompileWasmInterpreterEntry(Isolate* isolate,
wasm::WasmCode* CompileWasmInterpreterEntry(Isolate* isolate,
wasm::NativeModule* native_module,
uint32_t func_index,
wasm::FunctionSig* sig) {
//----------------------------------------------------------------------------
......@@ -5137,10 +5139,7 @@ MaybeHandle<Code> CompileWasmInterpreterEntry(Isolate* isolate,
MaybeHandle<Code> maybe_code = Pipeline::GenerateCodeForWasmStub(
isolate, incoming, &graph, Code::WASM_INTERPRETER_ENTRY,
func_name.start(), AssemblerOptions::Default(isolate));
Handle<Code> code;
if (!maybe_code.ToHandle(&code)) {
return maybe_code;
}
Handle<Code> code = maybe_code.ToHandleChecked();
#ifdef ENABLE_DISASSEMBLER
if (FLAG_print_opt_code) {
CodeTracer::Scope tracing_scope(isolate->GetCodeTracer());
......@@ -5154,7 +5153,11 @@ MaybeHandle<Code> CompileWasmInterpreterEntry(Isolate* isolate,
"%.*s", func_name.length(), func_name.start());
}
return maybe_code;
// TODO(wasm): No need to compile the code onto the heap and copy back.
wasm::WasmCode* wasm_code =
native_module->AddInterpreterEntry(code, func_index);
return wasm_code;
}
MaybeHandle<Code> CompileCWasmEntry(Isolate* isolate, wasm::FunctionSig* sig) {
......
......@@ -84,7 +84,8 @@ WasmImportCallKind GetWasmImportCallKind(Handle<JSReceiver> callable,
wasm::FunctionSig* sig);
// Compiles an import call wrapper, which allows WASM to call imports.
MaybeHandle<Code> CompileWasmImportCallWrapper(Isolate*, WasmImportCallKind,
wasm::WasmCode* CompileWasmImportCallWrapper(Isolate*, wasm::NativeModule*,
WasmImportCallKind,
wasm::FunctionSig*,
bool source_positions);
......@@ -96,7 +97,8 @@ V8_EXPORT_PRIVATE MaybeHandle<Code> CompileJSToWasmWrapper(Isolate*,
// Compiles a stub that redirects a call to a wasm function to the wasm
// interpreter. It's ABI compatible with the compiled wasm function.
MaybeHandle<Code> CompileWasmInterpreterEntry(Isolate*, uint32_t func_index,
wasm::WasmCode* CompileWasmInterpreterEntry(Isolate*, wasm::NativeModule*,
uint32_t func_index,
wasm::FunctionSig*);
enum CWasmEntryParameters {
......
......@@ -440,6 +440,11 @@ WasmCode* NativeModule::AddInterpreterEntry(Handle<Code> code,
return ret;
}
WasmCode* NativeModule::AddImportCallWrapper(Handle<Code> code) {
WasmCode* ret = AddAnonymousCode(code, WasmCode::kWasmToJsWrapper);
return ret;
}
WasmCode* NativeModule::AddCodeForTesting(Handle<Code> code) {
WasmCode* ret = AddAnonymousCode(code, WasmCode::kFunction);
return ret;
......
......@@ -238,6 +238,9 @@ class V8_EXPORT_PRIVATE NativeModule final {
// self-identify as the {index} function.
WasmCode* AddInterpreterEntry(Handle<Code> code, uint32_t index);
// Add an import wrapper for a JS-to-Wasm call.
WasmCode* AddImportCallWrapper(Handle<Code> code);
// Adds anonymous code for testing purposes.
WasmCode* AddCodeForTesting(Handle<Code> code);
......@@ -348,7 +351,6 @@ class V8_EXPORT_PRIVATE NativeModule final {
friend class WasmCode;
friend class WasmCodeManager;
friend class NativeModuleModificationScope;
friend class WasmImportWrapperCache;
NativeModule(Isolate* isolate, const WasmFeatures& enabled_features,
bool can_request_more, VirtualMemory code_space,
......
......@@ -628,10 +628,8 @@ void WasmDebugInfo::RedirectToInterpreter(Handle<WasmDebugInfo> debug_info,
DCHECK_GT(module->functions.size(), func_index);
if (!interpreted_functions->get(func_index)->IsUndefined(isolate)) continue;
MaybeHandle<Code> new_code = compiler::CompileWasmInterpreterEntry(
isolate, func_index, module->functions[func_index].sig);
const wasm::WasmCode* wasm_new_code = native_module->AddInterpreterEntry(
new_code.ToHandleChecked(), func_index);
const wasm::WasmCode* wasm_new_code = compiler::CompileWasmInterpreterEntry(
isolate, native_module, func_index, module->functions[func_index].sig);
Handle<Foreign> foreign_holder = isolate->factory()->NewForeign(
wasm_new_code->instruction_start(), TENURED);
interpreted_functions->set(func_index, *foreign_holder);
......
......@@ -27,17 +27,14 @@ class WasmImportWrapperCache {
WasmCode*& cached = entry_map_[key];
if (cached == nullptr) {
// TODO(wasm): no need to hold the lock while compiling an import wrapper.
// TODO(wasm): no need to compile the code onto the heap and copy back.
HandleScope scope(isolate);
bool source_positions = native_module_->module()->origin == kAsmJsOrigin;
Handle<Code> code = compiler::CompileWasmImportCallWrapper(
isolate, kind, sig, source_positions)
.ToHandleChecked();
cached = compiler::CompileWasmImportCallWrapper(
isolate, native_module_, kind, sig, source_positions);
auto counters = isolate->counters();
counters->wasm_generated_code_size()->Increment(code->body_size());
counters->wasm_reloc_size()->Increment(code->relocation_info()->length());
cached =
native_module_->AddAnonymousCode(code, WasmCode::kWasmToJsWrapper);
counters->wasm_generated_code_size()->Increment(
cached->instructions().length());
counters->wasm_reloc_size()->Increment(cached->reloc_info().length());
}
return cached;
}
......
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