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