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