Commit df834f3f authored by titzer's avatar titzer Committed by Commit bot

[wasm] Split the compilation and instantiation API into sync and async methods.

This makes it easier to implement asynchronous compilation by hiding all the implementation details of both synchronous and asynchronous compilation within wasm-module.cc, whereas before the code in wasm-js.cc actually implemented asynchronous compilation in terms of synchronous.

BUG=

Review-Url: https://codereview.chromium.org/2695813005
Cr-Commit-Position: refs/heads/master@{#43310}
parent db624fc4
...@@ -7599,11 +7599,8 @@ MaybeLocal<WasmCompiledModule> WasmCompiledModule::Compile(Isolate* isolate, ...@@ -7599,11 +7599,8 @@ MaybeLocal<WasmCompiledModule> WasmCompiledModule::Compile(Isolate* isolate,
size_t length) { size_t length) {
i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate); i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
i::wasm::ErrorThrower thrower(i_isolate, "WasmCompiledModule::Deserialize()"); i::wasm::ErrorThrower thrower(i_isolate, "WasmCompiledModule::Deserialize()");
i::MaybeHandle<i::JSObject> maybe_compiled = i::MaybeHandle<i::JSObject> maybe_compiled = i::wasm::SyncCompile(
i::wasm::CreateModuleObjectFromBytes( i_isolate, &thrower, i::wasm::ModuleWireBytes(start, start + length));
i_isolate, start, start + length, &thrower,
i::wasm::ModuleOrigin::kWasmOrigin, i::Handle<i::Script>::null(),
i::Vector<const uint8_t>::empty());
if (maybe_compiled.is_null()) return MaybeLocal<WasmCompiledModule>(); if (maybe_compiled.is_null()) return MaybeLocal<WasmCompiledModule>();
return Local<WasmCompiledModule>::Cast( return Local<WasmCompiledModule>::Cast(
Utils::ToLocal(maybe_compiled.ToHandleChecked())); Utils::ToLocal(maybe_compiled.ToHandleChecked()));
......
...@@ -110,7 +110,8 @@ class RegisteredExtension { ...@@ -110,7 +110,8 @@ class RegisteredExtension {
V(Proxy, JSProxy) \ V(Proxy, JSProxy) \
V(NativeWeakMap, JSWeakMap) \ V(NativeWeakMap, JSWeakMap) \
V(debug::GeneratorObject, JSGeneratorObject) \ V(debug::GeneratorObject, JSGeneratorObject) \
V(debug::Script, Script) V(debug::Script, Script) \
V(Promise, JSPromise)
class Utils { class Utils {
public: public:
......
...@@ -188,9 +188,10 @@ MaybeHandle<FixedArray> AsmJs::CompileAsmViaWasm(CompilationInfo* info) { ...@@ -188,9 +188,10 @@ MaybeHandle<FixedArray> AsmJs::CompileAsmViaWasm(CompilationInfo* info) {
base::ElapsedTimer compile_timer; base::ElapsedTimer compile_timer;
compile_timer.Start(); compile_timer.Start();
MaybeHandle<JSObject> compiled = wasm::CreateModuleObjectFromBytes( MaybeHandle<JSObject> compiled = SyncCompileTranslatedAsmJs(
info->isolate(), module->begin(), module->end(), &thrower, info->isolate(), &thrower,
internal::wasm::kAsmJsOrigin, info->script(), asm_offsets_vec); wasm::ModuleWireBytes(module->begin(), module->end()), info->script(),
asm_offsets_vec);
DCHECK(!compiled.is_null()); DCHECK(!compiled.is_null());
double compile_time = compile_timer.Elapsed().InMillisecondsF(); double compile_time = compile_timer.Elapsed().InMillisecondsF();
DCHECK_GE(module->end(), module->begin()); DCHECK_GE(module->end(), module->begin());
...@@ -276,8 +277,7 @@ MaybeHandle<Object> AsmJs::InstantiateAsmWasm(i::Isolate* isolate, ...@@ -276,8 +277,7 @@ MaybeHandle<Object> AsmJs::InstantiateAsmWasm(i::Isolate* isolate,
} }
i::MaybeHandle<i::Object> maybe_module_object = i::MaybeHandle<i::Object> maybe_module_object =
i::wasm::WasmModule::Instantiate(isolate, &thrower, module, ffi_object, i::wasm::SyncInstantiate(isolate, &thrower, module, ffi_object, memory);
memory);
if (maybe_module_object.is_null()) { if (maybe_module_object.is_null()) {
return MaybeHandle<Object>(); return MaybeHandle<Object>();
} }
......
...@@ -1624,10 +1624,8 @@ MaybeHandle<JSObject> ValueDeserializer::ReadWasmModule() { ...@@ -1624,10 +1624,8 @@ MaybeHandle<JSObject> ValueDeserializer::ReadWasmModule() {
MaybeHandle<JSObject> result; MaybeHandle<JSObject> result;
{ {
wasm::ErrorThrower thrower(isolate_, "ValueDeserializer::ReadWasmModule"); wasm::ErrorThrower thrower(isolate_, "ValueDeserializer::ReadWasmModule");
result = wasm::CreateModuleObjectFromBytes( result = wasm::SyncCompile(isolate_, &thrower,
isolate_, wire_bytes.begin(), wire_bytes.end(), &thrower, wasm::ModuleWireBytes(wire_bytes));
wasm::ModuleOrigin::kWasmOrigin, Handle<Script>::null(),
Vector<const byte>::empty());
} }
RETURN_EXCEPTION_IF_SCHEDULED_EXCEPTION(isolate_, JSObject); RETURN_EXCEPTION_IF_SCHEDULED_EXCEPTION(isolate_, JSObject);
return result; return result;
......
This diff is collapsed.
This diff is collapsed.
...@@ -28,7 +28,6 @@ class WasmMemoryObject; ...@@ -28,7 +28,6 @@ class WasmMemoryObject;
namespace compiler { namespace compiler {
class CallDescriptor; class CallDescriptor;
class WasmCompilationUnit;
} }
namespace wasm { namespace wasm {
...@@ -217,18 +216,6 @@ struct V8_EXPORT_PRIVATE WasmModule { ...@@ -217,18 +216,6 @@ struct V8_EXPORT_PRIVATE WasmModule {
~WasmModule() { ~WasmModule() {
if (owned_zone) delete owned_zone; if (owned_zone) delete owned_zone;
} }
// Creates a new instantiation of the module in the given isolate.
static MaybeHandle<WasmInstanceObject> Instantiate(
Isolate* isolate, ErrorThrower* thrower,
Handle<WasmModuleObject> wasm_module, Handle<JSReceiver> ffi,
Handle<JSArrayBuffer> memory = Handle<JSArrayBuffer>::null());
MaybeHandle<WasmCompiledModule> CompileFunctions(
Isolate* isolate, Handle<Managed<WasmModule>> module_wrapper,
ErrorThrower* thrower, const ModuleWireBytes& wire_bytes,
Handle<Script> asm_js_script,
Vector<const byte> asm_js_offset_table_bytes) const;
}; };
typedef Managed<WasmModule> WasmModuleWrapper; typedef Managed<WasmModule> WasmModuleWrapper;
...@@ -359,6 +346,7 @@ struct V8_EXPORT_PRIVATE ModuleEnv { ...@@ -359,6 +346,7 @@ struct V8_EXPORT_PRIVATE ModuleEnv {
return instance->function_code[index]; return instance->function_code[index];
} }
// TODO(titzer): move these into src/compiler/wasm-compiler.cc
static compiler::CallDescriptor* GetWasmCallDescriptor(Zone* zone, static compiler::CallDescriptor* GetWasmCallDescriptor(Zone* zone,
FunctionSig* sig); FunctionSig* sig);
static compiler::CallDescriptor* GetI32WasmCallDescriptor( static compiler::CallDescriptor* GetI32WasmCallDescriptor(
...@@ -425,11 +413,6 @@ V8_EXPORT_PRIVATE Handle<JSArray> GetCustomSections( ...@@ -425,11 +413,6 @@ V8_EXPORT_PRIVATE Handle<JSArray> GetCustomSections(
Isolate* isolate, Handle<WasmModuleObject> module, Handle<String> name, Isolate* isolate, Handle<WasmModuleObject> module, Handle<String> name,
ErrorThrower* thrower); ErrorThrower* thrower);
V8_EXPORT_PRIVATE bool ValidateModuleBytes(Isolate* isolate, const byte* start,
const byte* end,
ErrorThrower* thrower,
ModuleOrigin origin);
// Get the offset of the code of a function within a module. // Get the offset of the code of a function within a module.
int GetFunctionCodeOffset(Handle<WasmCompiledModule> compiled_module, int GetFunctionCodeOffset(Handle<WasmCompiledModule> compiled_module,
int func_index); int func_index);
...@@ -464,15 +447,43 @@ void UpdateDispatchTables(Isolate* isolate, Handle<FixedArray> dispatch_tables, ...@@ -464,15 +447,43 @@ void UpdateDispatchTables(Isolate* isolate, Handle<FixedArray> dispatch_tables,
void GrowDispatchTables(Isolate* isolate, Handle<FixedArray> dispatch_tables, void GrowDispatchTables(Isolate* isolate, Handle<FixedArray> dispatch_tables,
uint32_t old_size, uint32_t count); uint32_t old_size, uint32_t count);
namespace testing { //============================================================================
//== Compilation and instantiation ===========================================
//============================================================================
V8_EXPORT_PRIVATE bool SyncValidate(Isolate* isolate, ErrorThrower* thrower,
const ModuleWireBytes& bytes);
V8_EXPORT_PRIVATE MaybeHandle<WasmModuleObject> SyncCompileTranslatedAsmJs(
Isolate* isolate, ErrorThrower* thrower, const ModuleWireBytes& bytes,
Handle<Script> asm_js_script, Vector<const byte> asm_js_offset_table_bytes);
V8_EXPORT_PRIVATE MaybeHandle<WasmModuleObject> SyncCompile(
Isolate* isolate, ErrorThrower* thrower, const ModuleWireBytes& bytes);
V8_EXPORT_PRIVATE MaybeHandle<WasmInstanceObject> SyncInstantiate(
Isolate* isolate, ErrorThrower* thrower,
Handle<WasmModuleObject> module_object, MaybeHandle<JSReceiver> imports,
MaybeHandle<JSArrayBuffer> memory);
V8_EXPORT_PRIVATE void AsyncCompile(Isolate* isolate, Handle<JSPromise> promise,
const ModuleWireBytes& bytes);
V8_EXPORT_PRIVATE void AsyncInstantiate(Isolate* isolate,
Handle<JSPromise> promise,
Handle<WasmModuleObject> module_object,
MaybeHandle<JSReceiver> imports);
V8_EXPORT_PRIVATE void AsyncCompileAndInstantiate(
Isolate* isolate, Handle<JSPromise> promise, const ModuleWireBytes& bytes,
MaybeHandle<JSReceiver> imports);
namespace testing {
void ValidateInstancesChain(Isolate* isolate, void ValidateInstancesChain(Isolate* isolate,
Handle<WasmModuleObject> module_obj, Handle<WasmModuleObject> module_obj,
int instance_count); int instance_count);
void ValidateModuleState(Isolate* isolate, Handle<WasmModuleObject> module_obj); void ValidateModuleState(Isolate* isolate, Handle<WasmModuleObject> module_obj);
void ValidateOrphanedInstance(Isolate* isolate, void ValidateOrphanedInstance(Isolate* isolate,
Handle<WasmInstanceObject> instance); Handle<WasmInstanceObject> instance);
} // namespace testing } // namespace testing
} // namespace wasm } // namespace wasm
} // namespace internal } // namespace internal
......
...@@ -271,9 +271,9 @@ class WasmSerializationTest { ...@@ -271,9 +271,9 @@ class WasmSerializationTest {
0); 0);
} }
Handle<JSObject> instance = Handle<JSObject> instance =
WasmModule::Instantiate(current_isolate(), &thrower, module_object, SyncInstantiate(current_isolate(), &thrower, module_object,
Handle<JSReceiver>::null(), Handle<JSReceiver>::null(),
Handle<JSArrayBuffer>::null()) MaybeHandle<JSArrayBuffer>())
.ToHandleChecked(); .ToHandleChecked();
Handle<Object> params[1] = { Handle<Object> params[1] = {
Handle<Object>(Smi::FromInt(41), current_isolate())}; Handle<Object>(Smi::FromInt(41), current_isolate())};
...@@ -318,19 +318,13 @@ class WasmSerializationTest { ...@@ -318,19 +318,13 @@ class WasmSerializationTest {
HandleScope scope(serialization_isolate); HandleScope scope(serialization_isolate);
testing::SetupIsolateForWasmModule(serialization_isolate); testing::SetupIsolateForWasmModule(serialization_isolate);
ModuleResult decoding_result = MaybeHandle<WasmModuleObject> module_object =
DecodeWasmModule(serialization_isolate, buffer.begin(), buffer.end(), SyncCompile(serialization_isolate, &thrower,
false, kWasmOrigin); ModuleWireBytes(buffer.begin(), buffer.end()));
CHECK(!decoding_result.failed());
Handle<WasmModuleWrapper> module_wrapper = WasmModuleWrapper::New( MaybeHandle<WasmCompiledModule> compiled_module(
serialization_isolate, const_cast<WasmModule*>(decoding_result.val)); module_object.ToHandleChecked()->compiled_module(),
serialization_isolate);
MaybeHandle<WasmCompiledModule> compiled_module =
decoding_result.val->CompileFunctions(
serialization_isolate, module_wrapper, &thrower,
ModuleWireBytes(buffer.begin(), buffer.end()),
Handle<Script>::null(), Vector<const byte>::empty());
CHECK(!compiled_module.is_null()); CHECK(!compiled_module.is_null());
Handle<JSObject> module_obj = WasmModuleObject::New( Handle<JSObject> module_obj = WasmModuleObject::New(
serialization_isolate, compiled_module.ToHandleChecked()); serialization_isolate, compiled_module.ToHandleChecked());
...@@ -437,10 +431,8 @@ TEST(BlockWasmCodeGen) { ...@@ -437,10 +431,8 @@ TEST(BlockWasmCodeGen) {
CcTest::isolate()->SetAllowCodeGenerationFromStringsCallback(False); CcTest::isolate()->SetAllowCodeGenerationFromStringsCallback(False);
ErrorThrower thrower(isolate, "block codegen"); ErrorThrower thrower(isolate, "block codegen");
MaybeHandle<WasmModuleObject> ret = wasm::CreateModuleObjectFromBytes( MaybeHandle<WasmModuleObject> ret = wasm::SyncCompile(
isolate, buffer.begin(), buffer.end(), &thrower, isolate, &thrower, ModuleWireBytes(buffer.begin(), buffer.end()));
wasm::ModuleOrigin::kWasmOrigin, Handle<v8::internal::Script>::null(),
Vector<const byte>::empty());
CcTest::isolate()->SetAllowCodeGenerationFromStringsCallback(nullptr); CcTest::isolate()->SetAllowCodeGenerationFromStringsCallback(nullptr);
CHECK(ret.is_null()); CHECK(ret.is_null());
CHECK(thrower.error()); CHECK(thrower.error());
......
...@@ -59,17 +59,15 @@ const Handle<WasmInstanceObject> InstantiateModuleForTesting( ...@@ -59,17 +59,15 @@ const Handle<WasmInstanceObject> InstantiateModuleForTesting(
// Although we decoded the module for some pre-validation, run the bytes // Although we decoded the module for some pre-validation, run the bytes
// again through the normal pipeline. // again through the normal pipeline.
// TODO(wasm): Use {module} instead of decoding the module bytes again. // TODO(wasm): Use {module} instead of decoding the module bytes again.
MaybeHandle<WasmModuleObject> module_object = CreateModuleObjectFromBytes( MaybeHandle<WasmModuleObject> module_object =
isolate, wire_bytes.start(), wire_bytes.end(), thrower, SyncCompile(isolate, thrower, wire_bytes);
ModuleOrigin::kWasmOrigin, Handle<Script>::null(),
Vector<const byte>::empty());
if (module_object.is_null()) { if (module_object.is_null()) {
thrower->CompileError("Module pre-validation failed."); thrower->CompileError("Module pre-validation failed.");
return Handle<WasmInstanceObject>::null(); return Handle<WasmInstanceObject>::null();
} }
MaybeHandle<WasmInstanceObject> maybe_instance = MaybeHandle<WasmInstanceObject> maybe_instance =
WasmModule::Instantiate(isolate, thrower, module_object.ToHandleChecked(), SyncInstantiate(isolate, thrower, module_object.ToHandleChecked(),
Handle<JSReceiver>::null()); Handle<JSReceiver>::null(), MaybeHandle<JSArrayBuffer>());
Handle<WasmInstanceObject> instance; Handle<WasmInstanceObject> instance;
if (!maybe_instance.ToHandle(&instance)) { if (!maybe_instance.ToHandle(&instance)) {
return Handle<WasmInstanceObject>::null(); return Handle<WasmInstanceObject>::null();
......
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