Commit 0324a4d4 authored by Clemens Hammacher's avatar Clemens Hammacher Committed by Commit Bot

[wasm] Shrink code table to only include wasm functions

The slots for imported functions are unused by now. Shrink the table to
only store pointers for non-imported functions (i.e. wasm functions
defined in this module).

R=mstarzinger@chromium.org

Change-Id: I6d13f889528b42beca73c860a800bde7a8e921ab
Reviewed-on: https://chromium-review.googlesource.com/1084845Reviewed-by: 's avatarMichael Starzinger <mstarzinger@chromium.org>
Commit-Queue: Clemens Hammacher <clemensh@chromium.org>
Cr-Commit-Position: refs/heads/master@{#53493}
parent 0c62c81d
......@@ -398,6 +398,7 @@ class RelocInfo {
PC_JUMP,
// Points to a wasm code table entry.
// TODO(clemensh): Remove this once we have the jump table (issue 7758).
WASM_CODE_TABLE_ENTRY,
// Pseudo-types
......
......@@ -1949,8 +1949,11 @@ bool LiftoffCompilationUnit::ExecuteCompilation() {
compiler::GetWasmCallDescriptor(&zone, wasm_unit_->func_body_.sig);
base::Optional<TimedHistogramScope> liftoff_compile_time_scope(
base::in_place, wasm_unit_->counters_->liftoff_compile_time());
// TODO(clemensh): Remove this once we have the jump table (issue 7758).
wasm::WasmCode** code_table_entry =
&wasm_unit_->native_module_->code_table()[wasm_unit_->func_index_];
&wasm_unit_->native_module_
->code_table()[wasm_unit_->func_index_ -
wasm_unit_->native_module_->num_imported_functions()];
DCHECK(!protected_instructions_);
protected_instructions_.reset(
new std::vector<trap_handler::ProtectedInstructionData>());
......
......@@ -383,8 +383,9 @@ NativeModule::NativeModule(uint32_t num_functions, uint32_t num_imports,
can_request_more_memory_(can_request_more),
use_trap_handler_(env.use_trap_handler) {
if (num_functions > 0) {
code_table_.reset(new WasmCode*[num_functions]);
memset(code_table_.get(), 0, num_functions * sizeof(WasmCode*));
uint32_t num_wasm_functions = num_functions - num_imports;
code_table_.reset(new WasmCode*[num_wasm_functions]);
memset(code_table_.get(), 0, num_wasm_functions * sizeof(WasmCode*));
}
VirtualMemory my_mem;
owned_code_space_.push_back(my_mem);
......@@ -394,9 +395,11 @@ NativeModule::NativeModule(uint32_t num_functions, uint32_t num_imports,
void NativeModule::ReserveCodeTableForTesting(uint32_t max_functions) {
DCHECK_LE(num_functions_, max_functions);
WasmCode** new_table = new WasmCode*[max_functions];
memset(new_table, 0, max_functions * sizeof(*new_table));
memcpy(new_table, code_table_.get(), num_functions_ * sizeof(*new_table));
uint32_t num_wasm = num_functions_ - num_imported_functions_;
uint32_t max_wasm = max_functions - num_imported_functions_;
WasmCode** new_table = new WasmCode*[max_wasm];
memset(new_table, 0, max_wasm * sizeof(*new_table));
memcpy(new_table, code_table_.get(), num_wasm * sizeof(*new_table));
code_table_.reset(new_table);
}
......@@ -406,8 +409,8 @@ void NativeModule::SetNumFunctionsForTesting(uint32_t num_functions) {
void NativeModule::SetCodeForTesting(uint32_t index, WasmCode* code) {
DCHECK_LT(index, num_functions_);
DCHECK_LT(num_imported_functions_, index);
code_table_[index] = code;
DCHECK_LE(num_imported_functions_, index);
code_table_[index - num_imported_functions_] = code;
}
WasmCode* NativeModule::AddOwnedCode(
......@@ -454,9 +457,11 @@ WasmCode* NativeModule::AddOwnedCode(
WasmCode* NativeModule::AddCodeCopy(Handle<Code> code, WasmCode::Kind kind,
uint32_t index) {
// TODO(wasm): Adding instance-specific wasm-to-js wrappers as owned code to
// this NativeModule is a memory leak until the whole NativeModule dies.
WasmCode* ret = AddAnonymousCode(code, kind);
code_table_[index] = ret;
ret->index_ = Just(index);
if (index >= num_imported_functions_) set_code(index, ret);
return ret;
}
......@@ -468,8 +473,8 @@ WasmCode* NativeModule::AddInterpreterEntry(Handle<Code> code, uint32_t index) {
void NativeModule::SetLazyBuiltin(Handle<Code> code) {
WasmCode* lazy_builtin = AddAnonymousCode(code, WasmCode::kLazyStub);
for (uint32_t i = num_imported_functions_, e = num_functions_; i < e; ++i) {
code_table_[i] = lazy_builtin;
for (WasmCode*& code_table_entry : code_table()) {
code_table_entry = lazy_builtin;
}
}
......@@ -586,7 +591,7 @@ WasmCode* NativeModule::AddCode(
safepoint_table_offset, handler_table_offset,
std::move(protected_instructions), tier, WasmCode::kNoFlushICache);
code_table_[index] = ret;
set_code(index, ret);
// Apply the relocation delta by iterating over the RelocInfo.
AllowDeferredHandleDereference embedding_raw_address;
......@@ -812,7 +817,7 @@ WasmCode* NativeModule::CloneCode(const WasmCode* original_code,
original_code->handler_table_offset_, std::move(protected_instructions),
original_code->tier(), flush_icache);
if (!ret->IsAnonymous()) {
code_table_[ret->index()] = ret;
set_code(ret->index(), ret);
}
return ret;
}
......
......@@ -266,13 +266,13 @@ class V8_EXPORT_PRIVATE NativeModule final {
WasmCode* code(uint32_t index) const {
DCHECK_LT(index, num_functions_);
DCHECK_LE(num_imported_functions_, index);
return code_table_[index];
return code_table_[index - num_imported_functions_];
}
bool has_code(uint32_t index) const {
DCHECK_LT(index, num_functions_);
DCHECK_LE(num_imported_functions_, index);
return code_table_[index] != nullptr;
return code_table_[index - num_imported_functions_] != nullptr;
}
WasmCode* runtime_stub(WasmCode::RuntimeStubId index) const {
......@@ -309,7 +309,7 @@ class V8_EXPORT_PRIVATE NativeModule final {
uint32_t num_functions() const { return num_functions_; }
uint32_t num_imported_functions() const { return num_imported_functions_; }
Vector<WasmCode*> code_table() const {
return {code_table_.get(), num_functions_};
return {code_table_.get(), num_functions_ - num_imported_functions_};
}
bool use_trap_handler() const { return use_trap_handler_; }
void set_lazy_compile_frozen(bool frozen) { lazy_compile_frozen_ = frozen; }
......@@ -351,6 +351,13 @@ class V8_EXPORT_PRIVATE NativeModule final {
Address GetLocalAddressFor(Handle<Code>);
Address CreateTrampolineTo(Handle<Code>);
void set_code(uint32_t index, WasmCode* code) {
DCHECK_LT(index, num_functions_);
DCHECK_LE(num_imported_functions_, index);
DCHECK_EQ(code->index(), index);
code_table_[index - num_imported_functions_] = code;
}
// Holds all allocated code objects, is maintained to be in ascending order
// according to the codes instruction start address to allow lookups.
std::vector<std::unique_ptr<WasmCode>> owned_code_;
......
......@@ -499,7 +499,7 @@ bool NativeModuleDeserializer::ReadCode(uint32_t fn_index, Reader* reader) {
constant_pool_offset, stack_slot_count, safepoint_table_offset,
handler_table_offset, std::move(protected_instructions), tier,
WasmCode::kNoFlushICache);
native_module_->code_table_[fn_index] = ret;
native_module_->set_code(fn_index, ret);
// Relocate the code.
int mask = RelocInfo::ModeMask(RelocInfo::CODE_TARGET) |
......@@ -538,8 +538,10 @@ bool NativeModuleDeserializer::ReadCode(uint32_t fn_index, Reader* reader) {
case RelocInfo::WASM_CODE_TABLE_ENTRY: {
DCHECK(FLAG_wasm_tier_up);
DCHECK(ret->is_liftoff());
uint32_t code_table_index =
ret->index() - native_module_->num_imported_functions_;
WasmCode** code_table_entry =
&native_module_->code_table()[ret->index()];
&native_module_->code_table()[code_table_index];
iter.rinfo()->set_wasm_code_table_entry(
reinterpret_cast<Address>(code_table_entry), SKIP_ICACHE_FLUSH);
break;
......
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