Commit fa8dba0e authored by clemensh's avatar clemensh Committed by Commit bot

[wasm] Several unrelated cleanups

Most are minor performance optimizations that aggregated while implementing
other changes. Those fixes will probably not be visible in perf graphs, but
they bothered me anyway.

R=titzer@chromium.org, ahaas@chromium.org

Review-Url: https://codereview.chromium.org/2714373003
Cr-Commit-Position: refs/heads/master@{#43535}
parent 9fd418b9
...@@ -179,7 +179,15 @@ class WasmTrapHelper : public ZoneObject { ...@@ -179,7 +179,15 @@ class WasmTrapHelper : public ZoneObject {
} }
Builtins::Name GetBuiltinIdForTrap(wasm::TrapReason reason) { Builtins::Name GetBuiltinIdForTrap(wasm::TrapReason reason) {
if (builder_->module_ && !builder_->module_->instance->context.is_null()) { bool in_cctest =
!builder_->module_ || builder_->module_->instance->context.is_null();
if (in_cctest) {
// We use Builtins::builtin_count as a marker to tell the code generator
// to generate a call to a testing c-function instead of a runtime
// function. This code should only be called from a cctest.
return Builtins::builtin_count;
}
switch (reason) { switch (reason) {
#define TRAPREASON_TO_MESSAGE(name) \ #define TRAPREASON_TO_MESSAGE(name) \
case wasm::k##name: \ case wasm::k##name: \
...@@ -190,12 +198,6 @@ class WasmTrapHelper : public ZoneObject { ...@@ -190,12 +198,6 @@ class WasmTrapHelper : public ZoneObject {
UNREACHABLE(); UNREACHABLE();
return Builtins::builtin_count; return Builtins::builtin_count;
} }
} else {
// We use Runtime::kNumFunctions as a marker to tell the code generator
// to generate a call to a testing c-function instead of a runtime
// function. This code should only be called from a cctest.
return Builtins::builtin_count;
}
} }
#if V8_TARGET_ARCH_X64 || V8_TARGET_ARCH_IA32 || V8_TARGET_ARCH_ARM || \ #if V8_TARGET_ARCH_X64 || V8_TARGET_ARCH_IA32 || V8_TARGET_ARCH_ARM || \
...@@ -372,11 +374,11 @@ WasmGraphBuilder::WasmGraphBuilder( ...@@ -372,11 +374,11 @@ WasmGraphBuilder::WasmGraphBuilder(
trap_(new (zone) WasmTrapHelper(this)), trap_(new (zone) WasmTrapHelper(this)),
sig_(sig), sig_(sig),
source_position_table_(source_position_table) { source_position_table_(source_position_table) {
for (size_t i = 0; i < sig->parameter_count(); i++) { for (size_t i = sig->parameter_count(); i > 0 && !has_simd_; --i) {
if (sig->GetParam(i) == wasm::kWasmS128) has_simd_ = true; if (sig->GetParam(i - 1) == wasm::kWasmS128) has_simd_ = true;
} }
for (size_t i = 0; i < sig->return_count(); i++) { for (size_t i = sig->return_count(); i > 0 && !has_simd_; --i) {
if (sig->GetReturn(i) == wasm::kWasmS128) has_simd_ = true; if (sig->GetReturn(i - 1) == wasm::kWasmS128) has_simd_ = true;
} }
DCHECK_NOT_NULL(jsgraph_); DCHECK_NOT_NULL(jsgraph_);
} }
...@@ -3064,13 +3066,12 @@ Node* WasmGraphBuilder::CurrentMemoryPages() { ...@@ -3064,13 +3066,12 @@ Node* WasmGraphBuilder::CurrentMemoryPages() {
CallDescriptor* desc = Linkage::GetRuntimeCallDescriptor( CallDescriptor* desc = Linkage::GetRuntimeCallDescriptor(
jsgraph()->zone(), function_id, function->nargs, Operator::kNoThrow, jsgraph()->zone(), function_id, function->nargs, Operator::kNoThrow,
CallDescriptor::kNoFlags); CallDescriptor::kNoFlags);
wasm::ModuleEnv* module = module_;
Node* inputs[] = { Node* inputs[] = {
jsgraph()->CEntryStubConstant(function->result_size), // C entry jsgraph()->CEntryStubConstant(function->result_size), // C entry
jsgraph()->ExternalConstant( jsgraph()->ExternalConstant(
ExternalReference(function_id, jsgraph()->isolate())), // ref ExternalReference(function_id, jsgraph()->isolate())), // ref
jsgraph()->Int32Constant(function->nargs), // arity jsgraph()->Int32Constant(function->nargs), // arity
jsgraph()->HeapConstant(module->instance->context), // context jsgraph()->HeapConstant(module_->instance->context), // context
*effect_, *effect_,
*control_}; *control_};
Node* call = graph()->NewNode(jsgraph()->common()->Call(desc), Node* call = graph()->NewNode(jsgraph()->common()->Call(desc),
......
...@@ -1274,7 +1274,7 @@ class BuiltinFrame final : public JavaScriptFrame { ...@@ -1274,7 +1274,7 @@ class BuiltinFrame final : public JavaScriptFrame {
friend class StackFrameIteratorBase; friend class StackFrameIteratorBase;
}; };
class WasmCompiledFrame : public StandardFrame { class WasmCompiledFrame final : public StandardFrame {
public: public:
Type type() const override { return WASM_COMPILED; } Type type() const override { return WASM_COMPILED; }
...@@ -1316,7 +1316,7 @@ class WasmCompiledFrame : public StandardFrame { ...@@ -1316,7 +1316,7 @@ class WasmCompiledFrame : public StandardFrame {
friend class StackFrameIteratorBase; friend class StackFrameIteratorBase;
}; };
class WasmInterpreterEntryFrame : public StandardFrame { class WasmInterpreterEntryFrame final : public StandardFrame {
public: public:
Type type() const override { return WASM_INTERPRETER_ENTRY; } Type type() const override { return WASM_INTERPRETER_ENTRY; }
......
...@@ -575,9 +575,9 @@ class ModuleDecoder : public Decoder { ...@@ -575,9 +575,9 @@ class ModuleDecoder : public Decoder {
WasmIndirectFunctionTable* table = nullptr; WasmIndirectFunctionTable* table = nullptr;
if (table_index >= module->function_tables.size()) { if (table_index >= module->function_tables.size()) {
error(pos, pos, "out of bounds table index %u", table_index); error(pos, pos, "out of bounds table index %u", table_index);
} else { break;
table = &module->function_tables[table_index];
} }
table = &module->function_tables[table_index];
WasmInitExpr offset = consume_init_expr(module, kWasmI32); WasmInitExpr offset = consume_init_expr(module, kWasmI32);
uint32_t num_elem = uint32_t num_elem =
consume_count("number of elements", kV8MaxWasmTableEntries); consume_count("number of elements", kV8MaxWasmTableEntries);
...@@ -587,11 +587,12 @@ class ModuleDecoder : public Decoder { ...@@ -587,11 +587,12 @@ class ModuleDecoder : public Decoder {
for (uint32_t j = 0; ok() && j < num_elem; j++) { for (uint32_t j = 0; ok() && j < num_elem; j++) {
WasmFunction* func = nullptr; WasmFunction* func = nullptr;
uint32_t index = consume_func_index(module, &func); uint32_t index = consume_func_index(module, &func);
DCHECK_EQ(func != nullptr, ok());
if (!func) break;
DCHECK_EQ(index, func->func_index);
init->entries.push_back(index); init->entries.push_back(index);
if (table && index < module->functions.size()) {
// Canonicalize signature indices during decoding. // Canonicalize signature indices during decoding.
table->map.FindOrInsert(module->functions[index].sig); table->map.FindOrInsert(func->sig);
}
} }
} }
......
...@@ -828,7 +828,6 @@ WasmInstanceObject* wasm::GetOwningWasmInstance(Code* code) { ...@@ -828,7 +828,6 @@ WasmInstanceObject* wasm::GetOwningWasmInstance(Code* code) {
DCHECK(code->kind() == Code::WASM_FUNCTION || DCHECK(code->kind() == Code::WASM_FUNCTION ||
code->kind() == Code::WASM_INTERPRETER_ENTRY); code->kind() == Code::WASM_INTERPRETER_ENTRY);
FixedArray* deopt_data = code->deoptimization_data(); FixedArray* deopt_data = code->deoptimization_data();
DCHECK_NOT_NULL(deopt_data);
DCHECK_EQ(code->kind() == Code::WASM_INTERPRETER_ENTRY ? 1 : 2, DCHECK_EQ(code->kind() == Code::WASM_INTERPRETER_ENTRY ? 1 : 2,
deopt_data->length()); deopt_data->length());
Object* weak_link = deopt_data->get(0); Object* weak_link = deopt_data->get(0);
...@@ -846,7 +845,9 @@ int wasm::GetFunctionCodeOffset(Handle<WasmCompiledModule> compiled_module, ...@@ -846,7 +845,9 @@ int wasm::GetFunctionCodeOffset(Handle<WasmCompiledModule> compiled_module,
WasmModule::WasmModule(Zone* owned) WasmModule::WasmModule(Zone* owned)
: owned_zone(owned), pending_tasks(new base::Semaphore(0)) {} : owned_zone(owned), pending_tasks(new base::Semaphore(0)) {}
static WasmFunction* GetWasmFunctionForImportWrapper(Isolate* isolate, namespace {
WasmFunction* GetWasmFunctionForImportWrapper(Isolate* isolate,
Handle<Object> target) { Handle<Object> target) {
if (target->IsJSFunction()) { if (target->IsJSFunction()) {
Handle<JSFunction> func = Handle<JSFunction>::cast(target); Handle<JSFunction> func = Handle<JSFunction>::cast(target);
...@@ -884,8 +885,7 @@ static Handle<Code> UnwrapImportWrapper(Handle<Object> import_wrapper) { ...@@ -884,8 +885,7 @@ static Handle<Code> UnwrapImportWrapper(Handle<Object> import_wrapper) {
return Handle<Code>::null(); return Handle<Code>::null();
} }
static Handle<Code> CompileImportWrapper(Isolate* isolate, int index, Handle<Code> CompileImportWrapper(Isolate* isolate, int index, FunctionSig* sig,
FunctionSig* sig,
Handle<JSReceiver> target, Handle<JSReceiver> target,
Handle<String> module_name, Handle<String> module_name,
MaybeHandle<String> import_name, MaybeHandle<String> import_name,
...@@ -906,10 +906,9 @@ static Handle<Code> CompileImportWrapper(Isolate* isolate, int index, ...@@ -906,10 +906,9 @@ static Handle<Code> CompileImportWrapper(Isolate* isolate, int index,
} }
} }
static void UpdateDispatchTablesInternal(Isolate* isolate, void UpdateDispatchTablesInternal(Isolate* isolate,
Handle<FixedArray> dispatch_tables, Handle<FixedArray> dispatch_tables, int index,
int index, WasmFunction* function, WasmFunction* function, Handle<Code> code) {
Handle<Code> code) {
DCHECK_EQ(0, dispatch_tables->length() % 4); DCHECK_EQ(0, dispatch_tables->length() % 4);
for (int i = 0; i < dispatch_tables->length(); i += 4) { for (int i = 0; i < dispatch_tables->length(); i += 4) {
int table_index = Smi::cast(dispatch_tables->get(i + 1))->value(); int table_index = Smi::cast(dispatch_tables->get(i + 1))->value();
...@@ -922,19 +921,19 @@ static void UpdateDispatchTablesInternal(Isolate* isolate, ...@@ -922,19 +921,19 @@ static void UpdateDispatchTablesInternal(Isolate* isolate,
// a dangling pointer in the signature map. // a dangling pointer in the signature map.
Handle<WasmInstanceObject> instance( Handle<WasmInstanceObject> instance(
WasmInstanceObject::cast(dispatch_tables->get(i)), isolate); WasmInstanceObject::cast(dispatch_tables->get(i)), isolate);
int sig_index = static_cast<int>( auto func_table = instance->module()->function_tables[table_index];
instance->module()->function_tables[table_index].map.FindOrInsert( uint32_t sig_index = func_table.map.FindOrInsert(function->sig);
function->sig)); signature_table->set(index, Smi::FromInt(static_cast<int>(sig_index)));
signature_table->set(index, Smi::FromInt(sig_index));
function_table->set(index, *code); function_table->set(index, *code);
} else { } else {
Code* code = nullptr;
signature_table->set(index, Smi::FromInt(-1)); signature_table->set(index, Smi::FromInt(-1));
function_table->set(index, code); function_table->set(index, Smi::kZero);
} }
} }
} }
} // namespace
void wasm::UpdateDispatchTables(Isolate* isolate, void wasm::UpdateDispatchTables(Isolate* isolate,
Handle<FixedArray> dispatch_tables, int index, Handle<FixedArray> dispatch_tables, int index,
Handle<JSFunction> function) { Handle<JSFunction> function) {
...@@ -1024,8 +1023,7 @@ class InstantiationHelper { ...@@ -1024,8 +1023,7 @@ class InstantiationHelper {
// Clone the code for WASM functions and exports. // Clone the code for WASM functions and exports.
for (int i = 0; i < code_table->length(); ++i) { for (int i = 0; i < code_table->length(); ++i) {
Handle<Code> orig_code = Handle<Code> orig_code(Code::cast(code_table->get(i)), isolate_);
code_table->GetValueChecked<Code>(isolate_, i);
switch (orig_code->kind()) { switch (orig_code->kind()) {
case Code::WASM_TO_JS_FUNCTION: case Code::WASM_TO_JS_FUNCTION:
// Imports will be overwritten with newly compiled wrappers. // Imports will be overwritten with newly compiled wrappers.
...@@ -1187,14 +1185,14 @@ class InstantiationHelper { ...@@ -1187,14 +1185,14 @@ class InstantiationHelper {
//-------------------------------------------------------------------------- //--------------------------------------------------------------------------
Handle<WeakCell> weak_link = factory->NewWeakCell(instance); Handle<WeakCell> weak_link = factory->NewWeakCell(instance);
for (int i = num_imported_functions + FLAG_skip_compiling_wasm_funcs; for (int i = num_imported_functions + FLAG_skip_compiling_wasm_funcs,
i < code_table->length(); ++i) { num_functions = code_table->length();
Handle<Code> code = code_table->GetValueChecked<Code>(isolate_, i); i < num_functions; ++i) {
Handle<Code> code = handle(Code::cast(code_table->get(i)), isolate_);
if (code->kind() == Code::WASM_FUNCTION) { if (code->kind() == Code::WASM_FUNCTION) {
Handle<FixedArray> deopt_data = factory->NewFixedArray(2, TENURED); Handle<FixedArray> deopt_data = factory->NewFixedArray(2, TENURED);
deopt_data->set(0, *weak_link); deopt_data->set(0, *weak_link);
deopt_data->set(1, Smi::FromInt(static_cast<int>(i))); deopt_data->set(1, Smi::FromInt(i));
deopt_data->set_length(2);
code->set_deoptimization_data(*deopt_data); code->set_deoptimization_data(*deopt_data);
} }
} }
...@@ -1303,8 +1301,8 @@ class InstantiationHelper { ...@@ -1303,8 +1301,8 @@ class InstantiationHelper {
if (module_->start_function_index >= 0) { if (module_->start_function_index >= 0) {
HandleScope scope(isolate_); HandleScope scope(isolate_);
int start_index = module_->start_function_index; int start_index = module_->start_function_index;
Handle<Code> startup_code = Handle<Code> startup_code(Code::cast(code_table->get(start_index)),
code_table->GetValueChecked<Code>(isolate_, start_index); isolate_);
FunctionSig* sig = module_->functions[start_index].sig; FunctionSig* sig = module_->functions[start_index].sig;
Handle<Code> wrapper_code = Handle<Code> wrapper_code =
js_to_wasm_cache_.CloneOrCompileJSToWasmWrapper( js_to_wasm_cache_.CloneOrCompileJSToWasmWrapper(
...@@ -2002,7 +2000,8 @@ class InstantiationHelper { ...@@ -2002,7 +2000,8 @@ class InstantiationHelper {
uint32_t base = EvalUint32InitExpr(table_init.offset); uint32_t base = EvalUint32InitExpr(table_init.offset);
DCHECK(in_bounds(base, static_cast<uint32_t>(table_init.entries.size()), DCHECK(in_bounds(base, static_cast<uint32_t>(table_init.entries.size()),
table_instance.function_table->length())); table_instance.function_table->length()));
for (int i = 0; i < static_cast<int>(table_init.entries.size()); ++i) { for (int i = 0, e = static_cast<int>(table_init.entries.size()); i < e;
++i) {
uint32_t func_index = table_init.entries[i]; uint32_t func_index = table_init.entries[i];
WasmFunction* function = &module_->functions[func_index]; WasmFunction* function = &module_->functions[func_index];
int table_index = static_cast<int>(i + base); int table_index = static_cast<int>(i + base);
...@@ -2642,6 +2641,8 @@ MaybeHandle<WasmInstanceObject> wasm::SyncInstantiate( ...@@ -2642,6 +2641,8 @@ MaybeHandle<WasmInstanceObject> wasm::SyncInstantiate(
return helper.Build(); return helper.Build();
} }
namespace {
void RejectPromise(Isolate* isolate, ErrorThrower* thrower, void RejectPromise(Isolate* isolate, ErrorThrower* thrower,
Handle<JSPromise> promise) { Handle<JSPromise> promise) {
v8::Local<v8::Promise::Resolver> resolver = v8::Local<v8::Promise::Resolver> resolver =
...@@ -2659,6 +2660,8 @@ void ResolvePromise(Isolate* isolate, Handle<JSPromise> promise, ...@@ -2659,6 +2660,8 @@ void ResolvePromise(Isolate* isolate, Handle<JSPromise> promise,
resolver->Resolve(v8::Utils::ToLocal(context), v8::Utils::ToLocal(result)); resolver->Resolve(v8::Utils::ToLocal(context), v8::Utils::ToLocal(result));
} }
} // namespace
void wasm::AsyncCompile(Isolate* isolate, Handle<JSPromise> promise, void wasm::AsyncCompile(Isolate* isolate, Handle<JSPromise> promise,
const ModuleWireBytes& bytes) { const ModuleWireBytes& bytes) {
ErrorThrower thrower(isolate, nullptr); ErrorThrower thrower(isolate, nullptr);
......
...@@ -33,16 +33,16 @@ static const uint8_t kMultivalBlock = 0x41; ...@@ -33,16 +33,16 @@ static const uint8_t kMultivalBlock = 0x41;
// We reuse the internal machine type to represent WebAssembly types. // We reuse the internal machine type to represent WebAssembly types.
// A typedef improves readability without adding a whole new type system. // A typedef improves readability without adding a whole new type system.
typedef MachineRepresentation ValueType; typedef MachineRepresentation ValueType;
const ValueType kWasmStmt = MachineRepresentation::kNone; constexpr ValueType kWasmStmt = MachineRepresentation::kNone;
const ValueType kWasmI32 = MachineRepresentation::kWord32; constexpr ValueType kWasmI32 = MachineRepresentation::kWord32;
const ValueType kWasmI64 = MachineRepresentation::kWord64; constexpr ValueType kWasmI64 = MachineRepresentation::kWord64;
const ValueType kWasmF32 = MachineRepresentation::kFloat32; constexpr ValueType kWasmF32 = MachineRepresentation::kFloat32;
const ValueType kWasmF64 = MachineRepresentation::kFloat64; constexpr ValueType kWasmF64 = MachineRepresentation::kFloat64;
const ValueType kWasmS128 = MachineRepresentation::kSimd128; constexpr ValueType kWasmS128 = MachineRepresentation::kSimd128;
const ValueType kWasmS1x4 = MachineRepresentation::kSimd1x4; constexpr ValueType kWasmS1x4 = MachineRepresentation::kSimd1x4;
const ValueType kWasmS1x8 = MachineRepresentation::kSimd1x8; constexpr ValueType kWasmS1x8 = MachineRepresentation::kSimd1x8;
const ValueType kWasmS1x16 = MachineRepresentation::kSimd1x16; constexpr ValueType kWasmS1x16 = MachineRepresentation::kSimd1x16;
const ValueType kWasmVar = MachineRepresentation::kTagged; constexpr ValueType kWasmVar = MachineRepresentation::kTagged;
typedef Signature<ValueType> FunctionSig; typedef Signature<ValueType> FunctionSig;
std::ostream& operator<<(std::ostream& os, const FunctionSig& function); std::ostream& operator<<(std::ostream& os, const FunctionSig& function);
...@@ -50,7 +50,7 @@ std::ostream& operator<<(std::ostream& os, const FunctionSig& function); ...@@ -50,7 +50,7 @@ std::ostream& operator<<(std::ostream& os, const FunctionSig& function);
typedef Vector<const char> WasmName; typedef Vector<const char> WasmName;
typedef int WasmCodePosition; typedef int WasmCodePosition;
const WasmCodePosition kNoCodePosition = -1; constexpr WasmCodePosition kNoCodePosition = -1;
// Control expressions and blocks. // Control expressions and blocks.
#define FOREACH_CONTROL_OPCODE(V) \ #define FOREACH_CONTROL_OPCODE(V) \
......
...@@ -205,7 +205,7 @@ class TestingModule : public ModuleEnv { ...@@ -205,7 +205,7 @@ class TestingModule : public ModuleEnv {
if (interpreter_) { if (interpreter_) {
const WasmFunction* function = &module->functions.back(); const WasmFunction* function = &module->functions.back();
int interpreter_index = interpreter_->AddFunctionForTesting(function); int interpreter_index = interpreter_->AddFunctionForTesting(function);
CHECK_EQ(index, static_cast<uint32_t>(interpreter_index)); CHECK_EQ(index, interpreter_index);
} }
DCHECK_LT(index, kMaxFunctions); // limited for testing. DCHECK_LT(index, kMaxFunctions); // limited for testing.
return index; return index;
...@@ -628,7 +628,6 @@ class WasmFunctionCompiler : private GraphAndBuilders { ...@@ -628,7 +628,6 @@ class WasmFunctionCompiler : private GraphAndBuilders {
isolate()->factory()->NewWeakCell(testing_module_->instance_object()); isolate()->factory()->NewWeakCell(testing_module_->instance_object());
deopt_data->set(0, *weak_instance); deopt_data->set(0, *weak_instance);
deopt_data->set(1, Smi::FromInt(static_cast<int>(function_index()))); deopt_data->set(1, Smi::FromInt(static_cast<int>(function_index())));
deopt_data->set_length(2);
code->set_deoptimization_data(*deopt_data); code->set_deoptimization_data(*deopt_data);
#ifdef ENABLE_DISASSEMBLER #ifdef ENABLE_DISASSEMBLER
......
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