Commit 8ca3d065 authored by Michael Starzinger's avatar Michael Starzinger Committed by Commit Bot

[wasm] Remove obsolete code specialization support.

R=clemensh@chromium.org

Change-Id: Id8a0849d3456be1062a065a23f51bac250de4f7d
Reviewed-on: https://chromium-review.googlesource.com/1107936Reviewed-by: 's avatarClemens Hammacher <clemensh@chromium.org>
Commit-Queue: Michael Starzinger <mstarzinger@chromium.org>
Cr-Commit-Position: refs/heads/master@{#54026}
parent 6038f637
......@@ -2447,8 +2447,6 @@ v8_source_set("v8_base") {
"src/wasm/value-type.h",
"src/wasm/wasm-code-manager.cc",
"src/wasm/wasm-code-manager.h",
"src/wasm/wasm-code-specialization.cc",
"src/wasm/wasm-code-specialization.h",
"src/wasm/wasm-constants.h",
"src/wasm/wasm-debug.cc",
"src/wasm/wasm-engine.cc",
......
......@@ -356,8 +356,8 @@ Address RelocInfo::js_to_wasm_address() const {
return Assembler::target_address_at(pc_, constant_pool_);
}
uint32_t RelocInfo::wasm_stub_call_tag() const {
DCHECK_EQ(rmode_, WASM_STUB_CALL);
uint32_t RelocInfo::wasm_call_tag() const {
DCHECK(rmode_ == WASM_CALL || rmode_ == WASM_STUB_CALL);
return static_cast<uint32_t>(
Assembler::target_address_at(pc_, constant_pool_));
}
......
......@@ -203,8 +203,8 @@ Address RelocInfo::js_to_wasm_address() const {
return Assembler::target_address_at(pc_, constant_pool_);
}
uint32_t RelocInfo::wasm_stub_call_tag() const {
DCHECK_EQ(rmode_, WASM_STUB_CALL);
uint32_t RelocInfo::wasm_call_tag() const {
DCHECK(rmode_ == WASM_CALL || rmode_ == WASM_STUB_CALL);
Instruction* instr = reinterpret_cast<Instruction*>(pc_);
if (instr->IsLdrLiteralX()) {
return static_cast<uint32_t>(
......
......@@ -581,7 +581,7 @@ class RelocInfo {
Address wasm_stub_call_address() const;
Address js_to_wasm_address() const;
uint32_t wasm_stub_call_tag() const;
uint32_t wasm_call_tag() const;
void set_wasm_call_address(
Address, ICacheFlushMode icache_flush_mode = FLUSH_ICACHE_IF_NEEDED);
......
......@@ -223,8 +223,8 @@ Address RelocInfo::js_to_wasm_address() const {
return Assembler::target_address_at(pc_, constant_pool_);
}
uint32_t RelocInfo::wasm_stub_call_tag() const {
DCHECK_EQ(rmode_, WASM_STUB_CALL);
uint32_t RelocInfo::wasm_call_tag() const {
DCHECK(rmode_ == WASM_CALL || rmode_ == WASM_STUB_CALL);
return Memory::uint32_at(pc_);
}
......
......@@ -216,8 +216,8 @@ Address RelocInfo::js_to_wasm_address() const {
return Assembler::target_address_at(pc_, constant_pool_);
}
uint32_t RelocInfo::wasm_stub_call_tag() const {
DCHECK_EQ(rmode_, WASM_STUB_CALL);
uint32_t RelocInfo::wasm_call_tag() const {
DCHECK(rmode_ == WASM_CALL || rmode_ == WASM_STUB_CALL);
return static_cast<uint32_t>(
Assembler::target_address_at(pc_, constant_pool_));
}
......
......@@ -194,8 +194,8 @@ Address RelocInfo::js_to_wasm_address() const {
return Assembler::target_address_at(pc_, constant_pool_);
}
uint32_t RelocInfo::wasm_stub_call_tag() const {
DCHECK_EQ(rmode_, WASM_STUB_CALL);
uint32_t RelocInfo::wasm_call_tag() const {
DCHECK(rmode_ == WASM_CALL || rmode_ == WASM_STUB_CALL);
return static_cast<uint32_t>(
Assembler::target_address_at(pc_, constant_pool_));
}
......
......@@ -179,8 +179,8 @@ Address RelocInfo::js_to_wasm_address() const {
return Assembler::target_address_at(pc_, constant_pool_);
}
uint32_t RelocInfo::wasm_stub_call_tag() const {
DCHECK_EQ(rmode_, WASM_STUB_CALL);
uint32_t RelocInfo::wasm_call_tag() const {
DCHECK(rmode_ == WASM_CALL || rmode_ == WASM_STUB_CALL);
return static_cast<uint32_t>(
Assembler::target_address_at(pc_, constant_pool_));
}
......
......@@ -289,8 +289,8 @@ Address RelocInfo::js_to_wasm_address() const {
return Assembler::target_address_at(pc_, constant_pool_);
}
uint32_t RelocInfo::wasm_stub_call_tag() const {
DCHECK_EQ(rmode_, WASM_STUB_CALL);
uint32_t RelocInfo::wasm_call_tag() const {
DCHECK(rmode_ == WASM_CALL || rmode_ == WASM_STUB_CALL);
return static_cast<uint32_t>(
Assembler::target_address_at(pc_, constant_pool_));
}
......
......@@ -18,7 +18,6 @@
#include "src/wasm/module-decoder.h"
#include "src/wasm/streaming-decoder.h"
#include "src/wasm/wasm-code-manager.h"
#include "src/wasm/wasm-code-specialization.h"
#include "src/wasm/wasm-engine.h"
#include "src/wasm/wasm-js.h"
#include "src/wasm/wasm-limits.h"
......@@ -414,10 +413,6 @@ wasm::WasmCode* LazyCompileFunction(Isolate* isolate,
// module creation time, and return a function that always traps here.
CHECK(!thrower.error());
// Now specialize the generated code for this instance.
CodeSpecialization code_specialization;
code_specialization.RelocateDirectCalls(native_module);
code_specialization.ApplyToWasmCode(wasm_code, SKIP_ICACHE_FLUSH);
int64_t func_size =
static_cast<int64_t>(func->code.end_offset() - func->code.offset());
int64_t compilation_time = compilation_timer.Elapsed().InMicroseconds();
......@@ -873,7 +868,6 @@ class FinishCompileTask : public CancelableTask {
ErrorThrower thrower(compilation_state_->isolate(), "AsyncCompile");
wasm::WasmCode* result = unit->FinishCompilation(&thrower);
NativeModule* native_module = unit->native_module();
if (thrower.error()) {
DCHECK_NULL(result);
compilation_state_->OnError(&thrower);
......@@ -885,13 +879,9 @@ class FinishCompileTask : public CancelableTask {
if (compilation_state_->baseline_compilation_finished()) {
// If Liftoff compilation finishes it will directly start executing.
// As soon as we have Turbofan-compiled code available, it will
// directly be used by Liftoff-compiled code. Therefore we need
// to patch the compiled Turbofan function directly after finishing it.
// directly be used by Liftoff-compiled code via the jump table.
DCHECK_EQ(CompileMode::kTiering, compilation_state_->compile_mode());
DCHECK(!result->is_liftoff());
CodeSpecialization code_specialization;
code_specialization.RelocateDirectCalls(native_module);
code_specialization.ApplyToWasmCode(result);
if (wasm::WasmCode::ShouldBeLogged(isolate)) result->LogCode(isolate);
......@@ -1299,11 +1289,8 @@ MaybeHandle<WasmInstanceObject> InstanceBuilder::Build() {
}
//--------------------------------------------------------------------------
// Patch all code with the relocations registered in code_specialization.
// Flush the processors instruction cache.
//--------------------------------------------------------------------------
CodeSpecialization code_specialization;
code_specialization.RelocateDirectCalls(native_module);
code_specialization.ApplyToWholeModule(native_module, SKIP_ICACHE_FLUSH);
FlushICache(native_module);
//--------------------------------------------------------------------------
......
......@@ -183,10 +183,19 @@ void WasmCode::Validate() const {
!it.done(); it.next()) {
RelocInfo::Mode mode = it.rinfo()->rmode();
switch (mode) {
case RelocInfo::WASM_CALL: {
Address target = it.rinfo()->wasm_call_address();
WasmCode* code = native_module_->Lookup(target);
CHECK_NOT_NULL(code);
CHECK_EQ(WasmCode::kJumpTable, code->kind());
CHECK(code->contains(target));
break;
}
case RelocInfo::WASM_STUB_CALL: {
Address target = it.rinfo()->wasm_stub_call_address();
WasmCode* code = native_module_->Lookup(target);
CHECK(code && code->kind() == WasmCode::kRuntimeStub);
CHECK_NOT_NULL(code);
CHECK_EQ(WasmCode::kRuntimeStub, code->kind());
CHECK_EQ(target, code->instruction_start());
break;
}
......@@ -196,7 +205,6 @@ void WasmCode::Validate() const {
CHECK(contains(target));
break;
}
case RelocInfo::WASM_CALL:
case RelocInfo::JS_TO_WASM_CALL:
case RelocInfo::EXTERNAL_REFERENCE:
case RelocInfo::OFF_HEAP_TARGET:
......@@ -490,7 +498,7 @@ WasmCode* NativeModule::AddAnonymousCode(Handle<Code> code,
!it.done(); it.next(), orig_it.next()) {
RelocInfo::Mode mode = it.rinfo()->rmode();
if (RelocInfo::IsWasmStubCall(mode)) {
uint32_t stub_call_tag = orig_it.rinfo()->wasm_stub_call_tag();
uint32_t stub_call_tag = orig_it.rinfo()->wasm_call_tag();
DCHECK_LT(stub_call_tag, WasmCode::kRuntimeStubCount);
WasmCode* code =
runtime_stub(static_cast<WasmCode::RuntimeStubId>(stub_call_tag));
......@@ -530,16 +538,20 @@ WasmCode* NativeModule::AddCode(
std::move(protected_instructions), tier, WasmCode::kNoFlushICache);
// Apply the relocation delta by iterating over the RelocInfo.
AllowDeferredHandleDereference embedding_raw_address;
intptr_t delta = ret->instructions().start() - desc.buffer;
int mode_mask = RelocInfo::kApplyMask |
RelocInfo::ModeMask(RelocInfo::WASM_CALL) |
RelocInfo::ModeMask(RelocInfo::WASM_STUB_CALL);
for (RelocIterator it(ret->instructions(), ret->reloc_info(),
ret->constant_pool(), mode_mask);
!it.done(); it.next()) {
RelocInfo::Mode mode = it.rinfo()->rmode();
if (RelocInfo::IsWasmStubCall(mode)) {
uint32_t stub_call_tag = it.rinfo()->wasm_stub_call_tag();
if (RelocInfo::IsWasmCall(mode)) {
uint32_t call_tag = it.rinfo()->wasm_call_tag();
Address target = GetCallTargetForFunction(call_tag);
it.rinfo()->set_wasm_call_address(target, SKIP_ICACHE_FLUSH);
} else if (RelocInfo::IsWasmStubCall(mode)) {
uint32_t stub_call_tag = it.rinfo()->wasm_call_tag();
DCHECK_LT(stub_call_tag, WasmCode::kRuntimeStubCount);
WasmCode* code =
runtime_stub(static_cast<WasmCode::RuntimeStubId>(stub_call_tag));
......
// Copyright 2017 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "src/wasm/wasm-code-specialization.h"
#include "src/assembler-inl.h"
#include "src/base/optional.h"
#include "src/objects-inl.h"
#include "src/source-position-table.h"
#include "src/wasm/decoder.h"
#include "src/wasm/wasm-code-manager.h"
#include "src/wasm/wasm-module.h"
#include "src/wasm/wasm-objects-inl.h"
namespace v8 {
namespace internal {
namespace wasm {
uint32_t ExtractDirectCallIndex(wasm::Decoder& decoder, const byte* pc) {
DCHECK_EQ(static_cast<int>(kExprCallFunction), static_cast<int>(*pc));
decoder.Reset(pc + 1, pc + 6);
uint32_t call_idx = decoder.consume_u32v("call index");
DCHECK(decoder.ok());
DCHECK_GE(kMaxInt, call_idx);
return call_idx;
}
namespace {
int AdvanceSourcePositionTableIterator(SourcePositionTableIterator& iterator,
size_t offset_l) {
DCHECK_GE(kMaxInt, offset_l);
int offset = static_cast<int>(offset_l);
DCHECK(!iterator.done());
int byte_pos;
do {
byte_pos = iterator.source_position().ScriptOffset();
iterator.Advance();
} while (!iterator.done() && iterator.code_offset() <= offset);
return byte_pos;
}
class PatchDirectCallsHelper {
public:
PatchDirectCallsHelper(NativeModule* native_module, const WasmCode* code)
: source_pos_it(code->source_positions()), decoder(nullptr, nullptr) {
uint32_t func_index = code->index();
const WasmModule* module = native_module->module();
func_bytes = native_module->wire_bytes().start() +
module->functions[func_index].code.offset();
}
SourcePositionTableIterator source_pos_it;
Decoder decoder;
const byte* func_bytes;
};
} // namespace
CodeSpecialization::CodeSpecialization() {}
CodeSpecialization::~CodeSpecialization() {}
void CodeSpecialization::RelocateDirectCalls(NativeModule* native_module) {
DCHECK_NULL(relocate_direct_calls_module_);
DCHECK_NOT_NULL(native_module);
relocate_direct_calls_module_ = native_module;
}
bool CodeSpecialization::ApplyToWholeModule(NativeModule* native_module,
ICacheFlushMode icache_flush_mode) {
DisallowHeapAllocation no_gc;
bool changed = false;
// Patch all wasm functions.
for (WasmCode* wasm_code : native_module->code_table()) {
if (wasm_code == nullptr) continue;
if (wasm_code->kind() != WasmCode::kFunction) continue;
changed |= ApplyToWasmCode(wasm_code, icache_flush_mode);
}
return changed;
}
bool CodeSpecialization::ApplyToWasmCode(wasm::WasmCode* code,
ICacheFlushMode icache_flush_mode) {
DisallowHeapAllocation no_gc;
DCHECK_EQ(wasm::WasmCode::kFunction, code->kind());
bool reloc_direct_calls = relocate_direct_calls_module_ != nullptr;
int reloc_mode = 0;
if (reloc_direct_calls) {
reloc_mode |= RelocInfo::ModeMask(RelocInfo::WASM_CALL);
}
if (!reloc_mode) return false;
base::Optional<PatchDirectCallsHelper> patch_direct_calls_helper;
bool changed = false;
NativeModule* native_module = code->native_module();
RelocIterator it(code->instructions(), code->reloc_info(),
code->constant_pool(), reloc_mode);
for (; !it.done(); it.next()) {
RelocInfo::Mode mode = it.rinfo()->rmode();
switch (mode) {
case RelocInfo::WASM_CALL: {
DCHECK(reloc_direct_calls);
// Iterate simultaneously over the relocation information and the source
// position table. For each call in the reloc info, move the source
// position iterator forward to that position to find the byte offset of
// the respective call. Then extract the call index from the module wire
// bytes to find the new compiled function.
size_t offset = it.rinfo()->pc() - code->instruction_start();
if (!patch_direct_calls_helper) {
patch_direct_calls_helper.emplace(relocate_direct_calls_module_,
code);
}
int byte_pos = AdvanceSourcePositionTableIterator(
patch_direct_calls_helper->source_pos_it, offset);
uint32_t called_func_index = ExtractDirectCallIndex(
patch_direct_calls_helper->decoder,
patch_direct_calls_helper->func_bytes + byte_pos);
Address new_target =
native_module->GetCallTargetForFunction(called_func_index);
it.rinfo()->set_wasm_call_address(new_target, icache_flush_mode);
changed = true;
} break;
default:
UNREACHABLE();
}
}
return changed;
}
} // namespace wasm
} // namespace internal
} // namespace v8
// Copyright 2017 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef V8_WASM_WASM_CODE_SPECIALIZATION_H_
#define V8_WASM_WASM_CODE_SPECIALIZATION_H_
#include "src/assembler.h"
#include "src/wasm/decoder.h"
#include "src/wasm/wasm-objects.h"
namespace v8 {
namespace internal {
namespace wasm {
uint32_t ExtractDirectCallIndex(wasm::Decoder& decoder, const byte* pc);
// Helper class to specialize wasm code for a specific module.
//
// Note that code is shared among instances belonging to one module, hence all
// specialization actions will implicitly apply to all instances of a module.
//
// Set up all relocations / patching that should be performed by the Relocate* /
// Patch* methods, then apply all changes in one step using the Apply* methods.
class CodeSpecialization {
public:
CodeSpecialization();
~CodeSpecialization();
// Update all direct call sites based on the code table in the given module.
void RelocateDirectCalls(NativeModule* module);
// Apply all relocations and patching to all code in the module.
bool ApplyToWholeModule(NativeModule*,
ICacheFlushMode = FLUSH_ICACHE_IF_NEEDED);
// Apply all relocations and patching to one wasm code object.
bool ApplyToWasmCode(wasm::WasmCode*,
ICacheFlushMode = FLUSH_ICACHE_IF_NEEDED);
private:
NativeModule* relocate_direct_calls_module_ = nullptr;
};
} // namespace wasm
} // namespace internal
} // namespace v8
#endif // V8_WASM_WASM_CODE_SPECIALIZATION_H_
......@@ -17,7 +17,6 @@
#include "src/v8.h"
#include "src/wasm/module-decoder.h"
#include "src/wasm/wasm-code-manager.h"
#include "src/wasm/wasm-code-specialization.h"
#include "src/wasm/wasm-js.h"
#include "src/wasm/wasm-module.h"
#include "src/wasm/wasm-objects-inl.h"
......
......@@ -478,13 +478,20 @@ bool NativeModuleDeserializer::ReadCode(uint32_t fn_index, Reader* reader) {
WasmCode::kFlushICache);
// Relocate the code.
int mask = RelocInfo::ModeMask(RelocInfo::WASM_STUB_CALL) |
int mask = RelocInfo::ModeMask(RelocInfo::WASM_CALL) |
RelocInfo::ModeMask(RelocInfo::WASM_STUB_CALL) |
RelocInfo::ModeMask(RelocInfo::EXTERNAL_REFERENCE);
for (RelocIterator iter(ret->instructions(), ret->reloc_info(),
ret->constant_pool(), mask);
!iter.done(); iter.next()) {
RelocInfo::Mode mode = iter.rinfo()->rmode();
switch (mode) {
case RelocInfo::WASM_CALL: {
uint32_t tag = GetWasmCalleeTag(iter.rinfo());
Address target = native_module_->GetCallTargetForFunction(tag);
iter.rinfo()->set_wasm_call_address(target, SKIP_ICACHE_FLUSH);
break;
}
case RelocInfo::WASM_STUB_CALL: {
uint32_t tag = GetWasmCalleeTag(iter.rinfo());
DCHECK_LT(tag, WasmCode::kRuntimeStubCount);
......
......@@ -138,8 +138,8 @@ Address RelocInfo::js_to_wasm_address() const {
return Memory::Address_at(pc_);
}
uint32_t RelocInfo::wasm_stub_call_tag() const {
DCHECK_EQ(rmode_, WASM_STUB_CALL);
uint32_t RelocInfo::wasm_call_tag() const {
DCHECK(rmode_ == WASM_CALL || rmode_ == WASM_STUB_CALL);
return Memory::uint32_at(pc_);
}
......
......@@ -26,7 +26,6 @@
#include "src/wasm/function-body-decoder.h"
#include "src/wasm/local-decl-encoder.h"
#include "src/wasm/wasm-code-manager.h"
#include "src/wasm/wasm-code-specialization.h"
#include "src/wasm/wasm-external-refs.h"
#include "src/wasm/wasm-interpreter.h"
#include "src/wasm/wasm-js.h"
......@@ -212,9 +211,6 @@ class TestingModuleBuilder {
}
void Link() {
if (linked_) return;
CodeSpecialization code_specialization;
code_specialization.RelocateDirectCalls(native_module_);
code_specialization.ApplyToWholeModule(native_module_);
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