Commit 026ce285 authored by mtrofin's avatar mtrofin Committed by Commit bot

[wasm] Further simplify WasmCompiledModule.

Better demarcation between what's mutable because it is code-
specialization specific, and what is provided at initialization.

BUG=

Review-Url: https://codereview.chromium.org/2784233004
Cr-Commit-Position: refs/heads/master@{#44395}
parent 94ac72aa
......@@ -608,17 +608,10 @@ class CompilationHelper {
// and information needed at instantiation time. This object needs to be
// serializable. Instantiation may occur off a deserialized version of this
// object.
Handle<WasmCompiledModule> compiled_module =
WasmCompiledModule::New(isolate_, shared);
compiled_module->set_num_imported_functions(
module_->num_imported_functions);
compiled_module->set_code_table(code_table);
compiled_module->set_min_mem_pages(module_->min_mem_pages);
compiled_module->set_max_mem_pages(module_->max_mem_pages);
Handle<WasmCompiledModule> compiled_module = WasmCompiledModule::New(
isolate_, shared, code_table, function_tables, signature_tables);
if (function_table_count > 0) {
compiled_module->set_function_tables(function_tables);
compiled_module->set_signature_tables(signature_tables);
compiled_module->set_empty_function_tables(function_tables);
}
// If we created a wasm script, finish it now and make it public to the
......@@ -1098,6 +1091,9 @@ class InstantiationHelper {
// Reuse the compiled module (if no owner), otherwise clone.
//--------------------------------------------------------------------------
Handle<FixedArray> code_table;
// We keep around a copy of the old code table, because we'll be replacing
// imports for the new instance, and then we need the old imports to be
// able to relocate.
Handle<FixedArray> old_code_table;
MaybeHandle<WasmInstanceObject> owner;
......@@ -1119,11 +1115,6 @@ class InstantiationHelper {
}
}
DCHECK(!original.is_null());
// Always make a new copy of the code_table, since the old_code_table
// may still have placeholders for imports.
old_code_table = original->code_table();
code_table = factory->CopyFixedArray(old_code_table);
if (original->has_weak_owning_instance()) {
// Clone, but don't insert yet the clone in the instances chain.
// We do that last. Since we are holding on to the owner instance,
......@@ -1131,7 +1122,9 @@ class InstantiationHelper {
// won't be mutated by possible finalizer runs.
DCHECK(!owner.is_null());
TRACE("Cloning from %d\n", original->instance_id());
old_code_table = original->code_table();
compiled_module_ = WasmCompiledModule::Clone(isolate_, original);
code_table = compiled_module_->code_table();
// Avoid creating too many handles in the outer scope.
HandleScope scope(isolate_);
......@@ -1169,10 +1162,12 @@ class InstantiationHelper {
} else {
// There was no owner, so we can reuse the original.
compiled_module_ = original;
old_code_table =
factory->CopyFixedArray(compiled_module_->code_table());
code_table = compiled_module_->code_table();
TRACE("Reusing existing instance %d\n",
compiled_module_->instance_id());
}
compiled_module_->set_code_table(code_table);
compiled_module_->set_native_context(isolate_->native_context());
}
......
......@@ -947,24 +947,46 @@ void WasmSharedModuleData::PrepareForLazyCompilation(
}
Handle<WasmCompiledModule> WasmCompiledModule::New(
Isolate* isolate, Handle<WasmSharedModuleData> shared) {
Isolate* isolate, Handle<WasmSharedModuleData> shared,
Handle<FixedArray> code_table,
MaybeHandle<FixedArray> maybe_empty_function_tables,
MaybeHandle<FixedArray> maybe_signature_tables) {
Handle<FixedArray> ret =
isolate->factory()->NewFixedArray(PropertyIndices::Count, TENURED);
// WasmCompiledModule::cast would fail since fields are not set yet.
Handle<WasmCompiledModule> compiled_module(
reinterpret_cast<WasmCompiledModule*>(*ret), isolate);
compiled_module->InitId();
compiled_module->set_num_imported_functions(0);
compiled_module->set_shared(shared);
compiled_module->set_native_context(isolate->native_context());
compiled_module->set_code_table(code_table);
int function_table_count =
static_cast<int>(shared->module()->function_tables.size());
if (function_table_count > 0) {
compiled_module->set_signature_tables(
maybe_signature_tables.ToHandleChecked());
compiled_module->set_empty_function_tables(
maybe_empty_function_tables.ToHandleChecked());
}
// TODO(mtrofin): we copy these because the order of finalization isn't
// reliable, and we need some of these at Reset (which is called at
// finalization). If the order were reliable, and top-down, we could instead
// just get them from shared().
compiled_module->set_min_mem_pages(shared->module()->min_mem_pages);
compiled_module->set_max_mem_pages(shared->module()->max_mem_pages);
compiled_module->set_num_imported_functions(
shared->module()->num_imported_functions);
return compiled_module;
}
Handle<WasmCompiledModule> WasmCompiledModule::Clone(
Isolate* isolate, Handle<WasmCompiledModule> module) {
Handle<FixedArray> code_copy =
isolate->factory()->CopyFixedArray(module->code_table());
Handle<WasmCompiledModule> ret = Handle<WasmCompiledModule>::cast(
isolate->factory()->CopyFixedArray(module));
ret->InitId();
ret->set_code_table(code_copy);
ret->reset_weak_owning_instance();
ret->reset_weak_next_instance();
ret->reset_weak_prev_instance();
......@@ -1133,17 +1155,19 @@ bool WasmCompiledModule::IsWasmCompiledModule(Object* obj) {
// We're OK with undefined, generally, because maybe we don't
// have a value for that item. For example, we may not have a
// memory, or globals.
// We're not OK with the fixed numbers being undefined. We want
// to set once all of them.
// We're not OK with the const numbers being undefined. They are
// expected to be initialized at construction.
#define WCM_CHECK_OBJECT(TYPE, NAME) \
WCM_CHECK_TYPE(NAME, obj->IsUndefined(isolate) || obj->Is##TYPE())
#define WCM_CHECK_CONST_OBJECT(TYPE, NAME) \
WCM_CHECK_TYPE(NAME, obj->IsUndefined(isolate) || obj->Is##TYPE())
#define WCM_CHECK_WASM_OBJECT(TYPE, NAME) \
WCM_CHECK_TYPE(NAME, TYPE::Is##TYPE(obj))
#define WCM_CHECK_WEAK_LINK(TYPE, NAME) WCM_CHECK_OBJECT(WeakCell, NAME)
#define WCM_CHECK_SMALL_NUMBER(TYPE, NAME) \
WCM_CHECK_TYPE(NAME, obj->IsUndefined(isolate) || obj->IsSmi())
#define WCM_CHECK(KIND, TYPE, NAME) WCM_CHECK_##KIND(TYPE, NAME)
#define WCM_CHECK_SMALL_FIXED_NUMBER(TYPE, NAME) \
#define WCM_CHECK_SMALL_CONST_NUMBER(TYPE, NAME) \
WCM_CHECK_TYPE(NAME, obj->IsSmi())
#define WCM_CHECK_LARGE_NUMBER(TYPE, NAME) \
WCM_CHECK_TYPE(NAME, obj->IsUndefined(isolate) || obj->IsMutableHeapNumber())
......
This diff is collapsed.
......@@ -359,15 +359,11 @@ class TestingModule : public ModuleEnv {
Handle<WasmSharedModuleData> shared_module_data =
WasmSharedModuleData::New(isolate_, module_wrapper, empty_string,
script, Handle<ByteArray>::null());
Handle<WasmCompiledModule> compiled_module =
WasmCompiledModule::New(isolate_, shared_module_data);
// Minimally initialize the compiled module such that IsWasmCompiledModule
// passes.
// If tests need more (correct) information, add it later.
compiled_module->set_min_mem_pages(0);
compiled_module->set_max_mem_pages(Smi::kMaxValue);
Handle<FixedArray> code_table = isolate_->factory()->NewFixedArray(0);
compiled_module->set_code_table(code_table);
Handle<WasmCompiledModule> compiled_module = WasmCompiledModule::New(
isolate_, shared_module_data, code_table, MaybeHandle<FixedArray>(),
MaybeHandle<FixedArray>());
Handle<FixedArray> weak_exported = isolate_->factory()->NewFixedArray(0);
compiled_module->set_weak_exported_functions(weak_exported);
DCHECK(WasmCompiledModule::IsWasmCompiledModule(*compiled_module));
......@@ -587,7 +583,7 @@ class WasmFunctionCompiler : private GraphAndBuilders {
static_cast<int>(function_index()) + 1);
code_table->CopyTo(0, *new_arr, 0, code_table->length());
code_table = new_arr;
compiled_module->set_code_table(code_table);
compiled_module->ReplaceCodeTableForTesting(code_table);
}
DCHECK(code_table->get(static_cast<int>(function_index()))
->IsUndefined(isolate()));
......
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