Commit ae6e9cc7 authored by Ben L. Titzer's avatar Ben L. Titzer Committed by Commit Bot

[wasm] Inline helpers of WasmModule::origin

R=mstarzinger@chromium.org

Change-Id: I0976bfa57b9ec48fae2b912e78bacfee4f8eeafb
Reviewed-on: https://chromium-review.googlesource.com/1072654
Commit-Queue: Ben Titzer <titzer@chromium.org>
Reviewed-by: 's avatarMichael Starzinger <mstarzinger@chromium.org>
Cr-Commit-Position: refs/heads/master@{#53368}
parent d3c02d30
......@@ -2945,7 +2945,7 @@ Node* WasmGraphBuilder::MemBuffer(uint32_t offset) {
Node* WasmGraphBuilder::CurrentMemoryPages() {
// CurrentMemoryPages can not be called from asm.js.
DCHECK_EQ(wasm::kWasmOrigin, env_->module->origin());
DCHECK_EQ(wasm::kWasmOrigin, env_->module->origin);
DCHECK_NOT_NULL(instance_cache_);
Node* mem_size = instance_cache_->mem_size;
DCHECK_NOT_NULL(mem_size);
......@@ -5112,7 +5112,7 @@ void TurbofanWasmCompilationUnit::ExecuteCompilation() {
source_positions, node_origins, &wasm_compilation_data_,
wasm_unit_->func_body_,
const_cast<wasm::WasmModule*>(wasm_unit_->env_->module),
wasm_unit_->env_->module->origin()));
wasm_unit_->env_->module->origin));
ok_ = job_->ExecuteJob() == CompilationJob::SUCCEEDED;
// TODO(bradnelson): Improve histogram handling of size_t.
wasm_unit_->counters_->wasm_compile_function_peak_memory_bytes()->AddSample(
......
......@@ -38,13 +38,13 @@ struct WasmException;
}())
#define RET_ON_PROTOTYPE_OPCODE(flag) \
DCHECK(!this->module_ || !this->module_->is_asm_js()); \
DCHECK(!this->module_ || this->module_->origin == kWasmOrigin); \
if (!FLAG_experimental_wasm_##flag) { \
this->error("Invalid opcode (enable with --experimental-wasm-" #flag ")"); \
}
#define CHECK_PROTOTYPE_OPCODE(flag) \
DCHECK(!this->module_ || !this->module_->is_asm_js()); \
DCHECK(!this->module_ || this->module_->origin == kWasmOrigin); \
if (!FLAG_experimental_wasm_##flag) { \
this->error("Invalid opcode (enable with --experimental-wasm-" #flag ")"); \
break; \
......@@ -1830,7 +1830,7 @@ class WasmFullDecoder : public WasmDecoder<validate> {
MemoryIndexImmediate<validate> imm(this, this->pc_);
len = 1 + imm.length;
DCHECK_NOT_NULL(this->module_);
if (!VALIDATE(this->module_->is_wasm())) {
if (!VALIDATE(this->module_->origin == kWasmOrigin)) {
this->error("grow_memory is not supported for asmjs modules");
break;
}
......@@ -1910,7 +1910,8 @@ class WasmFullDecoder : public WasmDecoder<validate> {
}
default: {
// Deal with special asmjs opcodes.
if (this->module_ != nullptr && this->module_->is_asm_js()) {
if (this->module_ != nullptr &&
this->module_->origin == kAsmJsOrigin) {
sig = WasmOpcodes::AsmjsSignature(opcode);
if (sig) {
BuildSimpleOperator(opcode, sig);
......
......@@ -846,11 +846,12 @@ DecodeResult VerifyWasmCode(AccountingAllocator* allocator,
DecodeResult VerifyWasmCodeWithStats(AccountingAllocator* allocator,
const wasm::WasmModule* module,
FunctionBody& body, bool is_wasm,
FunctionBody& body, ModuleOrigin origin,
Counters* counters) {
CHECK_LE(0, body.end - body.start);
auto time_counter = is_wasm ? counters->wasm_decode_wasm_function_time()
: counters->wasm_decode_asm_function_time();
auto time_counter = origin == kWasmOrigin
? counters->wasm_decode_wasm_function_time()
: counters->wasm_decode_asm_function_time();
TimedHistogramScope wasm_decode_function_time_scope(time_counter);
return VerifyWasmCode(allocator, module, body);
}
......
......@@ -27,6 +27,7 @@ namespace wasm {
typedef compiler::WasmGraphBuilder TFBuilder;
struct WasmModule; // forward declaration of module interface.
enum ModuleOrigin : uint8_t;
// A wrapper around the signature and bytes of a function.
struct FunctionBody {
......@@ -48,7 +49,7 @@ V8_EXPORT_PRIVATE DecodeResult VerifyWasmCode(AccountingAllocator* allocator,
// isolate::async_counters() to guarantee usability of counters argument.
DecodeResult VerifyWasmCodeWithStats(AccountingAllocator* allocator,
const wasm::WasmModule* module,
FunctionBody& body, bool is_wasm,
FunctionBody& body, ModuleOrigin origin,
Counters* counters);
DecodeResult BuildTFGraph(AccountingAllocator* allocator, TFBuilder* builder,
......
......@@ -55,7 +55,7 @@ WasmCompilationUnit::WasmCompilationUnit(Isolate* isolate, ModuleEnv* env,
// Always disable Liftoff for asm.js, for two reasons:
// 1) asm-specific opcodes are not implemented, and
// 2) tier-up does not work with lazy compilation.
if (env->module->is_asm_js()) mode = CompilationMode::kTurbofan;
if (env->module->origin == kAsmJsOrigin) mode = CompilationMode::kTurbofan;
SwitchMode(mode);
}
......@@ -64,14 +64,12 @@ WasmCompilationUnit::WasmCompilationUnit(Isolate* isolate, ModuleEnv* env,
WasmCompilationUnit::~WasmCompilationUnit() {}
void WasmCompilationUnit::ExecuteCompilation() {
auto size_histogram = env_->module->is_wasm()
? counters_->wasm_wasm_function_size_bytes()
: counters_->wasm_asm_function_size_bytes();
auto size_histogram = SELECT_WASM_COUNTER(counters_, env_->module->origin,
wasm, function_size_bytes);
size_histogram->AddSample(
static_cast<int>(func_body_.end - func_body_.start));
auto timed_histogram = env_->module->is_wasm()
? counters_->wasm_compile_wasm_function_time()
: counters_->wasm_compile_asm_function_time();
auto timed_histogram = SELECT_WASM_COUNTER(counters_, env_->module->origin,
wasm_compile, function_time);
TimedHistogramScope wasm_compile_function_time_scope(timed_histogram);
if (FLAG_trace_wasm_compiler) {
......
This diff is collapsed.
......@@ -296,7 +296,7 @@ class ModuleDecoderImpl : public Decoder {
module_->initial_pages = 0;
module_->maximum_pages = 0;
module_->mem_export = false;
module_->set_origin(origin_);
module_->origin = origin_;
}
void DecodeModuleHeader(Vector<const uint8_t> bytes, uint8_t offset) {
......@@ -518,9 +518,9 @@ class ModuleDecoderImpl : public Decoder {
void DecodeFunctionSection() {
uint32_t functions_count =
consume_count("functions count", kV8MaxWasmFunctions);
(IsWasm() ? GetCounters()->wasm_functions_per_wasm_module()
: GetCounters()->wasm_functions_per_asm_module())
->AddSample(static_cast<int>(functions_count));
auto counter =
SELECT_WASM_COUNTER(GetCounters(), origin_, wasm_functions_per, module);
counter->AddSample(static_cast<int>(functions_count));
module_->functions.reserve(functions_count);
module_->num_declared_functions = functions_count;
for (uint32_t i = 0; ok() && i < functions_count; ++i) {
......@@ -891,8 +891,6 @@ class ModuleDecoderImpl : public Decoder {
WasmModule* module() { return module_.get(); }
bool IsWasm() { return origin_ == kWasmOrigin; }
Counters* GetCounters() {
DCHECK_NOT_NULL(counters_);
return counters_;
......@@ -987,7 +985,7 @@ class ModuleDecoderImpl : public Decoder {
void CalculateGlobalOffsets(WasmModule* module) {
uint32_t offset = 0;
if (module->globals.size() == 0) {
module->globals_size = 0;
module->globals_buffer_size = 0;
module->num_imported_mutable_globals = 0;
return;
}
......@@ -1002,7 +1000,7 @@ class ModuleDecoderImpl : public Decoder {
offset += size;
}
}
module->globals_size = offset;
module->globals_buffer_size = offset;
}
// Verifies the body (code) of a given function.
......@@ -1020,7 +1018,7 @@ class ModuleDecoderImpl : public Decoder {
start_ + GetBufferRelativeOffset(function->code.offset()),
start_ + GetBufferRelativeOffset(function->code.end_offset())};
DecodeResult result = VerifyWasmCodeWithStats(allocator, module, body,
IsWasm(), GetCounters());
origin_, GetCounters());
if (result.failed()) {
// Wrap the error message from the function decoder.
std::ostringstream wrapped;
......@@ -1255,7 +1253,7 @@ class ModuleDecoderImpl : public Decoder {
case kLocalF64:
return kWasmF64;
default:
if (IsWasm()) {
if (origin_ == kWasmOrigin) {
switch (t) {
case kLocalS128:
if (FLAG_experimental_wasm_simd) return kWasmS128;
......@@ -1325,18 +1323,16 @@ class ModuleDecoderImpl : public Decoder {
ModuleResult DecodeWasmModule(Isolate* isolate, const byte* module_start,
const byte* module_end, bool verify_functions,
ModuleOrigin origin, Counters* counters) {
auto counter = origin == kWasmOrigin
? counters->wasm_decode_wasm_module_time()
: counters->wasm_decode_asm_module_time();
auto counter =
SELECT_WASM_COUNTER(counters, origin, wasm_decode, module_time);
TimedHistogramScope wasm_decode_module_time_scope(counter);
size_t size = module_end - module_start;
if (module_start > module_end) return ModuleResult::Error("start > end");
if (size >= kV8MaxWasmModuleSize)
return ModuleResult::Error("size > maximum module size: %zu", size);
// TODO(bradnelson): Improve histogram handling of size_t.
auto size_counter = origin == kWasmOrigin
? counters->wasm_wasm_module_size_bytes()
: counters->wasm_asm_module_size_bytes();
auto size_counter =
SELECT_WASM_COUNTER(counters, origin, wasm, module_size_bytes);
size_counter->AddSample(static_cast<int>(size));
// Signatures are stored in zone memory, which have the same lifetime
// as the {module}.
......@@ -1347,10 +1343,8 @@ ModuleResult DecodeWasmModule(Isolate* isolate, const byte* module_start,
// allocated on the C++ heap.
// https://bugs.chromium.org/p/chromium/issues/detail?id=657320
if (result.ok()) {
auto peak_counter =
origin == kWasmOrigin
? counters->wasm_decode_wasm_module_peak_memory_bytes()
: counters->wasm_decode_asm_module_peak_memory_bytes();
auto peak_counter = SELECT_WASM_COUNTER(counters, origin, wasm_decode,
module_peak_memory_bytes);
peak_counter->AddSample(
static_cast<int>(result.val->signature_zone->allocation_size()));
}
......@@ -1454,9 +1448,8 @@ FunctionResult DecodeWasmFunction(Isolate* isolate, Zone* zone,
size_t size = function_end - function_start;
if (function_start > function_end)
return FunctionResult::Error("start > end");
auto size_histogram = module->is_wasm()
? counters->wasm_wasm_function_size_bytes()
: counters->wasm_asm_function_size_bytes();
auto size_histogram =
SELECT_WASM_COUNTER(counters, module->origin, wasm, function_size_bytes);
// TODO(bradnelson): Improve histogram handling of ptrdiff_t.
size_histogram->AddSample(static_cast<int>(size));
if (size > kV8MaxWasmFunctionSize)
......
......@@ -128,6 +128,10 @@ struct WasmExport {
enum ModuleOrigin : uint8_t { kWasmOrigin, kAsmJsOrigin };
#define SELECT_WASM_COUNTER(counters, origin, prefix, suffix) \
((origin) == wasm::kWasmOrigin ? (counters)->prefix##_wasm_##suffix() \
: (counters)->prefix##_asm_##suffix())
struct ModuleWireBytes;
// Static representation of a module.
......@@ -146,14 +150,12 @@ struct V8_EXPORT_PRIVATE WasmModule {
std::vector<WasmGlobal> globals;
// Size of the buffer required for all globals that are not imported and
// mutable.
// TODO(wasm): Rename for clarity?
uint32_t globals_size = 0;
uint32_t globals_buffer_size = 0;
uint32_t num_imported_mutable_globals = 0;
uint32_t num_imported_functions = 0;
uint32_t num_declared_functions = 0;
uint32_t num_exported_functions = 0;
WireBytesRef name = {0, 0};
// TODO(wasm): Add url here, for spec'ed location information.
std::vector<FunctionSig*> signatures; // by signature index
std::vector<uint32_t> signature_ids; // by signature index
std::vector<WasmFunction> functions;
......@@ -165,24 +167,17 @@ struct V8_EXPORT_PRIVATE WasmModule {
std::vector<WasmTableInit> table_inits;
SignatureMap signature_map; // canonicalizing map for signature indexes.
ModuleOrigin origin = kWasmOrigin; // origin of the module
mutable std::unique_ptr<std::unordered_map<uint32_t, WireBytesRef>> names_;
WasmModule() : WasmModule(nullptr) {}
WasmModule(std::unique_ptr<Zone> owned);
ModuleOrigin origin() const { return origin_; }
void set_origin(ModuleOrigin new_value) { origin_ = new_value; }
bool is_wasm() const { return origin_ == kWasmOrigin; }
bool is_asm_js() const { return origin_ == kAsmJsOrigin; }
WireBytesRef LookupName(const ModuleWireBytes* wire_bytes,
uint32_t function_index) const;
WireBytesRef LookupName(SeqOneByteString* wire_bytes,
uint32_t function_index) const;
void AddNameForTesting(int function_index, WireBytesRef name);
private:
// TODO(kschimpf) - Encapsulate more fields.
ModuleOrigin origin_ = kWasmOrigin; // origin of the module
mutable std::unique_ptr<std::unordered_map<uint32_t, WireBytesRef>> names_;
};
// Interface to the storage (wire bytes) of a wasm module.
......
......@@ -988,7 +988,7 @@ Handle<WasmSharedModuleData> WasmSharedModuleData::New(
}
bool WasmSharedModuleData::is_asm_js() {
bool asm_js = module()->is_asm_js();
bool asm_js = module()->origin == wasm::kAsmJsOrigin;
DCHECK_EQ(asm_js, script()->IsUserJavaScript());
DCHECK_EQ(asm_js, has_asm_js_offset_table());
return asm_js;
......@@ -1195,7 +1195,7 @@ int WasmSharedModuleData::GetSourcePosition(Handle<WasmSharedModuleData> shared,
Isolate* isolate = shared->GetIsolate();
const WasmModule* module = shared->module();
if (!module->is_asm_js()) {
if (module->origin != wasm::kAsmJsOrigin) {
// for non-asm.js modules, we just add the function's start offset
// to make a module-relative position.
return byte_offset + shared->GetFunctionOffset(func_index);
......
......@@ -24,7 +24,7 @@ TestingModuleBuilder::TestingModuleBuilder(
runtime_exception_support_(exception_support),
lower_simd_(lower_simd) {
WasmJs::Install(isolate_, true);
test_module_->globals_size = kMaxGlobalsSize;
test_module_->globals_buffer_size = kMaxGlobalsSize;
memset(globals_data_, 0, sizeof(globals_data_));
uint32_t maybe_import_index = 0;
......@@ -45,7 +45,7 @@ TestingModuleBuilder::TestingModuleBuilder(
CodeSpaceMemoryModificationScope modification_scope(isolate_->heap());
Handle<Code> code = compiler::CompileWasmToJSWrapper(
isolate_, maybe_import->js_function, maybe_import->sig,
maybe_import_index, test_module_->origin(),
maybe_import_index, test_module_->origin,
trap_handler::IsTrapHandlerEnabled() ? kUseTrapHandler
: kNoTrapHandler);
native_module_->ResizeCodeTableForTesting(maybe_import_index + 1,
......@@ -67,7 +67,7 @@ byte* TestingModuleBuilder::AddMemory(uint32_t size) {
CHECK_NULL(mem_start_);
CHECK_EQ(0, mem_size_);
DCHECK(!instance_object_->has_memory_object());
DCHECK_IMPLIES(test_module_->origin() == kWasmOrigin,
DCHECK_IMPLIES(test_module_->origin == kWasmOrigin,
size % kWasmPageSize == 0);
test_module_->has_memory = true;
uint32_t alloc_size = RoundUp(size, kWasmPageSize);
......
......@@ -94,7 +94,7 @@ class TestingModuleBuilder {
TestingModuleBuilder(Zone*, ManuallyImportedJSFunction*, WasmExecutionMode,
RuntimeExceptionSupport, LowerSimd);
void ChangeOriginToAsmjs() { test_module_->set_origin(kAsmJsOrigin); }
void ChangeOriginToAsmjs() { test_module_->origin = kAsmJsOrigin; }
byte* AddMemory(uint32_t size);
......
......@@ -205,7 +205,7 @@ constexpr size_t kMaxByteSizedLeb128 = 127;
class TestModuleBuilder {
public:
explicit TestModuleBuilder(ModuleOrigin origin = kWasmOrigin) {
mod.set_origin(origin);
mod.origin = origin;
}
byte AddGlobal(ValueType type, bool mutability = true) {
mod.globals.push_back(
......
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