Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(362)

Side by Side Diff: src/wasm/wasm-module.cc

Issue 2385943002: Revert of [wasm] further simplification of WasmCompiledModule (Closed)
Patch Set: Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/wasm/wasm-module.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 the V8 project authors. All rights reserved. 1 // Copyright 2015 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <memory> 5 #include <memory>
6 6
7 #include "src/base/atomic-utils.h" 7 #include "src/base/atomic-utils.h"
8 #include "src/code-stubs.h" 8 #include "src/code-stubs.h"
9 9
10 #include "src/macro-assembler.h" 10 #include "src/macro-assembler.h"
(...skipping 834 matching lines...) Expand 10 before | Expand all | Expand 10 after
845 } 845 }
846 } 846 }
847 } 847 }
848 } 848 }
849 } 849 }
850 850
851 static void ResetCompiledModule(Isolate* isolate, JSObject* owner, 851 static void ResetCompiledModule(Isolate* isolate, JSObject* owner,
852 WasmCompiledModule* compiled_module) { 852 WasmCompiledModule* compiled_module) {
853 Object* undefined = *isolate->factory()->undefined_value(); 853 Object* undefined = *isolate->factory()->undefined_value();
854 uint32_t old_mem_size = compiled_module->mem_size(); 854 uint32_t old_mem_size = compiled_module->mem_size();
855 uint32_t default_mem_size = compiled_module->default_mem_size(); 855 Object* mem_start = compiled_module->ptr_to_mem_start();
856 Object* mem_start = compiled_module->ptr_to_heap();
857 Address old_mem_address = nullptr; 856 Address old_mem_address = nullptr;
858 Address globals_start = 857 Address globals_start =
859 GetGlobalStartAddressFromCodeTemplate(undefined, owner); 858 GetGlobalStartAddressFromCodeTemplate(undefined, owner);
860 859
861 if (old_mem_size > 0) { 860 if (old_mem_size > 0) {
862 CHECK_NE(mem_start, undefined); 861 CHECK_NE(mem_start, undefined);
863 old_mem_address = 862 old_mem_address =
864 static_cast<Address>(JSArrayBuffer::cast(mem_start)->backing_store()); 863 static_cast<Address>(JSArrayBuffer::cast(mem_start)->backing_store());
865 } 864 }
866 int mode_mask = RelocInfo::ModeMask(RelocInfo::WASM_MEMORY_REFERENCE) | 865 int mode_mask = RelocInfo::ModeMask(RelocInfo::WASM_MEMORY_REFERENCE) |
867 RelocInfo::ModeMask(RelocInfo::WASM_MEMORY_SIZE_REFERENCE) | 866 RelocInfo::ModeMask(RelocInfo::WASM_MEMORY_SIZE_REFERENCE) |
868 RelocInfo::ModeMask(RelocInfo::WASM_GLOBAL_REFERENCE); 867 RelocInfo::ModeMask(RelocInfo::WASM_GLOBAL_REFERENCE);
869 868
870 Object* fct_obj = compiled_module->ptr_to_code_table(); 869 Object* fct_obj = compiled_module->ptr_to_code_table();
871 if (fct_obj != nullptr && fct_obj != undefined && 870 if (fct_obj != nullptr && fct_obj != undefined &&
872 (old_mem_size > 0 || globals_start != nullptr)) { 871 (old_mem_size > 0 || globals_start != nullptr)) {
873 FixedArray* functions = FixedArray::cast(fct_obj); 872 FixedArray* functions = FixedArray::cast(fct_obj);
874 for (int i = 0; i < functions->length(); ++i) { 873 for (int i = 0; i < functions->length(); ++i) {
875 Code* code = Code::cast(functions->get(i)); 874 Code* code = Code::cast(functions->get(i));
876 bool changed = false; 875 bool changed = false;
877 for (RelocIterator it(code, mode_mask); !it.done(); it.next()) { 876 for (RelocIterator it(code, mode_mask); !it.done(); it.next()) {
878 RelocInfo::Mode mode = it.rinfo()->rmode(); 877 RelocInfo::Mode mode = it.rinfo()->rmode();
879 if (RelocInfo::IsWasmMemoryReference(mode) || 878 if (RelocInfo::IsWasmMemoryReference(mode) ||
880 RelocInfo::IsWasmMemorySizeReference(mode)) { 879 RelocInfo::IsWasmMemorySizeReference(mode)) {
881 it.rinfo()->update_wasm_memory_reference( 880 it.rinfo()->update_wasm_memory_reference(old_mem_address, nullptr,
882 old_mem_address, nullptr, old_mem_size, default_mem_size); 881 old_mem_size, old_mem_size);
883 changed = true; 882 changed = true;
884 } else { 883 } else {
885 CHECK(RelocInfo::IsWasmGlobalReference(mode)); 884 CHECK(RelocInfo::IsWasmGlobalReference(mode));
886 it.rinfo()->update_wasm_global_reference(globals_start, nullptr); 885 it.rinfo()->update_wasm_global_reference(globals_start, nullptr);
887 changed = true; 886 changed = true;
888 } 887 }
889 } 888 }
890 if (changed) { 889 if (changed) {
891 Assembler::FlushICache(isolate, code->instruction_start(), 890 Assembler::FlushICache(isolate, code->instruction_start(),
892 code->instruction_size()); 891 code->instruction_size());
893 } 892 }
894 } 893 }
895 } 894 }
896 compiled_module->reset_weak_owning_instance(); 895 compiled_module->reset_weak_owning_instance();
897 compiled_module->reset_heap(); 896 compiled_module->reset_mem_start();
898 } 897 }
899 898
900 static void InstanceFinalizer(const v8::WeakCallbackInfo<void>& data) { 899 static void InstanceFinalizer(const v8::WeakCallbackInfo<void>& data) {
901 JSObject** p = reinterpret_cast<JSObject**>(data.GetParameter()); 900 JSObject** p = reinterpret_cast<JSObject**>(data.GetParameter());
902 JSObject* owner = *p; 901 JSObject* owner = *p;
903 WasmCompiledModule* compiled_module = 902 WasmCompiledModule* compiled_module =
904 WasmCompiledModule::cast(owner->GetInternalField(kWasmCompiledModule)); 903 WasmCompiledModule::cast(owner->GetInternalField(kWasmCompiledModule));
905 Isolate* isolate = reinterpret_cast<Isolate*>(data.GetIsolate()); 904 Isolate* isolate = reinterpret_cast<Isolate*>(data.GetIsolate());
906 DCHECK(compiled_module->has_weak_module_object()); 905 DCHECK(compiled_module->has_weak_module_object());
907 WeakCell* weak_module_obj = compiled_module->ptr_to_weak_module_object(); 906 WeakCell* weak_module_obj = compiled_module->ptr_to_weak_module_object();
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after
1044 // TODO(mtrofin): do we need to flush the cache here? 1043 // TODO(mtrofin): do we need to flush the cache here?
1045 Assembler::FlushICache(isolate, code->instruction_start(), 1044 Assembler::FlushICache(isolate, code->instruction_start(),
1046 code->instruction_size()); 1045 code->instruction_size());
1047 } 1046 }
1048 } 1047 }
1049 1048
1050 // Create the compiled module object, and populate with compiled functions 1049 // Create the compiled module object, and populate with compiled functions
1051 // and information needed at instantiation time. This object needs to be 1050 // and information needed at instantiation time. This object needs to be
1052 // serializable. Instantiation may occur off a deserialized version of this 1051 // serializable. Instantiation may occur off a deserialized version of this
1053 // object. 1052 // object.
1054 Handle<WasmCompiledModule> ret = WasmCompiledModule::New( 1053 Handle<WasmCompiledModule> ret = WasmCompiledModule::New(isolate);
1055 isolate, min_mem_pages, globals_size, mem_export, origin);
1056 ret->set_code_table(code_table); 1054 ret->set_code_table(code_table);
1057 if (!indirect_table.is_null()) { 1055 if (!indirect_table.is_null()) {
1058 ret->set_indirect_function_tables(indirect_table.ToHandleChecked()); 1056 ret->set_indirect_function_tables(indirect_table.ToHandleChecked());
1059 } 1057 }
1060 Handle<FixedArray> import_data = GetImportsData(factory, this); 1058 Handle<FixedArray> import_data = GetImportsData(factory, this);
1061 ret->set_import_data(import_data); 1059 ret->set_import_data(import_data);
1062 1060
1063 // Compile exported function wrappers. 1061 // Compile exported function wrappers.
1064 int export_size = static_cast<int>(num_exported_functions); 1062 int export_size = static_cast<int>(num_exported_functions);
1065 if (export_size > 0) { 1063 if (export_size > 0) {
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
1119 static_cast<int>(module_bytes_len)); 1117 static_cast<int>(module_bytes_len));
1120 Handle<String> module_bytes_string = 1118 Handle<String> module_bytes_string =
1121 factory->NewStringFromOneByte(module_bytes_vec, TENURED) 1119 factory->NewStringFromOneByte(module_bytes_vec, TENURED)
1122 .ToHandleChecked(); 1120 .ToHandleChecked();
1123 ret->set_module_bytes(module_bytes_string); 1121 ret->set_module_bytes(module_bytes_string);
1124 } 1122 }
1125 1123
1126 Handle<ByteArray> function_name_table = 1124 Handle<ByteArray> function_name_table =
1127 BuildFunctionNamesTable(isolate, module_env.module); 1125 BuildFunctionNamesTable(isolate, module_env.module);
1128 ret->set_function_names(function_name_table); 1126 ret->set_function_names(function_name_table);
1127 ret->set_min_required_memory(min_mem_pages);
1129 if (data_segments.size() > 0) SaveDataSegmentInfo(factory, this, ret); 1128 if (data_segments.size() > 0) SaveDataSegmentInfo(factory, this, ret);
1130 DCHECK_EQ(ret->default_mem_size(), temp_instance.mem_size); 1129 ret->set_globals_size(globals_size);
1130 ret->set_export_memory(mem_export);
1131 ret->set_origin(origin);
1132 ret->set_mem_size(temp_instance.mem_size);
1131 return ret; 1133 return ret;
1132 } 1134 }
1133 1135
1134 void PatchJSWrapper(Isolate* isolate, Handle<Code> wrapper, 1136 void PatchJSWrapper(Isolate* isolate, Handle<Code> wrapper,
1135 Handle<Code> new_target) { 1137 Handle<Code> new_target) {
1136 AllowDeferredHandleDereference embedding_raw_address; 1138 AllowDeferredHandleDereference embedding_raw_address;
1137 bool seen = false; 1139 bool seen = false;
1138 for (RelocIterator it(*wrapper, 1 << RelocInfo::CODE_TARGET); !it.done(); 1140 for (RelocIterator it(*wrapper, 1 << RelocInfo::CODE_TARGET); !it.done();
1139 it.next()) { 1141 it.next()) {
1140 Code* target = Code::GetCodeFromTargetAddress(it.rinfo()->target_address()); 1142 Code* target = Code::GetCodeFromTargetAddress(it.rinfo()->target_address());
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after
1240 case Code::JS_TO_WASM_FUNCTION: 1242 case Code::JS_TO_WASM_FUNCTION:
1241 case Code::WASM_FUNCTION: { 1243 case Code::WASM_FUNCTION: {
1242 Handle<Code> code = factory->CopyCode(orig_code); 1244 Handle<Code> code = factory->CopyCode(orig_code);
1243 code_table->set(i, *code); 1245 code_table->set(i, *code);
1244 break; 1246 break;
1245 } 1247 }
1246 default: 1248 default:
1247 UNREACHABLE(); 1249 UNREACHABLE();
1248 } 1250 }
1249 } 1251 }
1252 compiled_module->set_mem_size(original->mem_size());
1250 RecordStats(isolate, code_table); 1253 RecordStats(isolate, code_table);
1251 } else { 1254 } else {
1252 // There was no owner, so we can reuse the original. 1255 // There was no owner, so we can reuse the original.
1253 compiled_module = original; 1256 compiled_module = original;
1254 } 1257 }
1255 compiled_module->set_code_table(code_table); 1258 compiled_module->set_code_table(code_table);
1256 } 1259 }
1257 1260
1258 //-------------------------------------------------------------------------- 1261 //--------------------------------------------------------------------------
1259 // Allocate the instance object. 1262 // Allocate the instance object.
1260 //-------------------------------------------------------------------------- 1263 //--------------------------------------------------------------------------
1261 Handle<Map> map = factory->NewMap( 1264 Handle<Map> map = factory->NewMap(
1262 JS_OBJECT_TYPE, 1265 JS_OBJECT_TYPE,
1263 JSObject::kHeaderSize + kWasmModuleInternalFieldCount * kPointerSize); 1266 JSObject::kHeaderSize + kWasmModuleInternalFieldCount * kPointerSize);
1264 Handle<JSObject> instance = factory->NewJSObjectFromMap(map, TENURED); 1267 Handle<JSObject> instance = factory->NewJSObjectFromMap(map, TENURED);
1265 instance->SetInternalField(kWasmModuleCodeTable, *code_table); 1268 instance->SetInternalField(kWasmModuleCodeTable, *code_table);
1266 1269
1267 //-------------------------------------------------------------------------- 1270 //--------------------------------------------------------------------------
1268 // Set up the memory for the new instance. 1271 // Set up the memory for the new instance.
1269 //-------------------------------------------------------------------------- 1272 //--------------------------------------------------------------------------
1270 MaybeHandle<JSArrayBuffer> old_memory; 1273 MaybeHandle<JSArrayBuffer> old_memory;
1271 // TODO(titzer): handle imported memory properly. 1274 // TODO(titzer): handle imported memory properly.
1272 1275
1273 uint32_t min_mem_pages = compiled_module->min_memory_pages(); 1276 uint32_t min_mem_pages = compiled_module->min_required_memory();
1274 isolate->counters()->wasm_min_mem_pages_count()->AddSample(min_mem_pages); 1277 isolate->counters()->wasm_min_mem_pages_count()->AddSample(min_mem_pages);
1275 // TODO(wasm): re-enable counter for max_mem_pages when we use that field. 1278 // TODO(wasm): re-enable counter for max_mem_pages when we use that field.
1276 1279
1277 if (memory.is_null() && min_mem_pages > 0) { 1280 if (memory.is_null() && min_mem_pages > 0) {
1278 memory = AllocateMemory(thrower, isolate, min_mem_pages); 1281 memory = AllocateMemory(thrower, isolate, min_mem_pages);
1279 if (memory.is_null()) return nothing; // failed to allocate memory 1282 if (memory.is_null()) return nothing; // failed to allocate memory
1280 } 1283 }
1281 1284
1282 if (!memory.is_null()) { 1285 if (!memory.is_null()) {
1283 instance->SetInternalField(kWasmMemArrayBuffer, *memory); 1286 instance->SetInternalField(kWasmMemArrayBuffer, *memory);
1284 Address mem_start = static_cast<Address>(memory->backing_store()); 1287 Address mem_start = static_cast<Address>(memory->backing_store());
1285 uint32_t mem_size = static_cast<uint32_t>(memory->byte_length()->Number()); 1288 uint32_t mem_size = static_cast<uint32_t>(memory->byte_length()->Number());
1286 LoadDataSegments(compiled_module, mem_start, mem_size); 1289 LoadDataSegments(compiled_module, mem_start, mem_size);
1287 1290
1288 uint32_t old_mem_size = compiled_module->has_heap() 1291 uint32_t old_mem_size = compiled_module->mem_size();
1289 ? compiled_module->mem_size()
1290 : compiled_module->default_mem_size();
1291 Address old_mem_start = 1292 Address old_mem_start =
1292 compiled_module->has_heap() 1293 compiled_module->has_mem_start()
1293 ? static_cast<Address>(compiled_module->heap()->backing_store()) 1294 ? static_cast<Address>(
1295 compiled_module->mem_start()->backing_store())
1294 : nullptr; 1296 : nullptr;
1295 RelocateInstanceCode(instance, old_mem_start, mem_start, old_mem_size, 1297 RelocateInstanceCode(instance, old_mem_start, mem_start, old_mem_size,
1296 mem_size); 1298 mem_size);
1297 compiled_module->set_heap(memory); 1299 compiled_module->set_mem_size(mem_size);
1300 compiled_module->set_mem_start(memory);
1298 } 1301 }
1299 1302
1300 //-------------------------------------------------------------------------- 1303 //--------------------------------------------------------------------------
1301 // Set up the globals for the new instance. 1304 // Set up the globals for the new instance.
1302 //-------------------------------------------------------------------------- 1305 //--------------------------------------------------------------------------
1303 MaybeHandle<JSArrayBuffer> old_globals; 1306 MaybeHandle<JSArrayBuffer> old_globals;
1304 MaybeHandle<JSArrayBuffer> globals; 1307 MaybeHandle<JSArrayBuffer> globals;
1305 uint32_t globals_size = compiled_module->globals_size(); 1308 uint32_t globals_size = compiled_module->globals_size();
1306 if (globals_size > 0) { 1309 if (globals_size > 0) {
1307 Handle<JSArrayBuffer> global_buffer = NewArrayBuffer(isolate, globals_size); 1310 Handle<JSArrayBuffer> global_buffer = NewArrayBuffer(isolate, globals_size);
(...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after
1527 } 1530 }
1528 GlobalHandles::MakeWeak(global_handle.location(), 1531 GlobalHandles::MakeWeak(global_handle.location(),
1529 global_handle.location(), &InstanceFinalizer, 1532 global_handle.location(), &InstanceFinalizer,
1530 v8::WeakCallbackType::kFinalizer); 1533 v8::WeakCallbackType::kFinalizer);
1531 } 1534 }
1532 } 1535 }
1533 1536
1534 return instance; 1537 return instance;
1535 } 1538 }
1536 1539
1537 Handle<WasmCompiledModule> WasmCompiledModule::New(Isolate* isolate,
1538 uint32_t min_memory_pages,
1539 uint32_t globals_size,
1540 bool export_memory,
1541 ModuleOrigin origin) {
1542 Handle<FixedArray> ret =
1543 isolate->factory()->NewFixedArray(PropertyIndices::Count, TENURED);
1544 // Globals size is expected to fit into an int without overflow. This is not
1545 // supported by the spec at the moment, however, we don't support array
1546 // buffer sizes over 1g, so, for now, we avoid alocating a HeapNumber for
1547 // the globals size. The CHECK guards this assumption.
1548 CHECK_GE(static_cast<int>(globals_size), 0);
1549 ret->set(kID_min_memory_pages,
1550 Smi::FromInt(static_cast<int>(min_memory_pages)));
1551 ret->set(kID_globals_size, Smi::FromInt(static_cast<int>(globals_size)));
1552 ret->set(kID_export_memory, Smi::FromInt(static_cast<int>(export_memory)));
1553 ret->set(kID_origin, Smi::FromInt(static_cast<int>(origin)));
1554 return handle(WasmCompiledModule::cast(*ret));
1555 }
1556
1557 compiler::CallDescriptor* ModuleEnv::GetCallDescriptor(Zone* zone, 1540 compiler::CallDescriptor* ModuleEnv::GetCallDescriptor(Zone* zone,
1558 uint32_t index) { 1541 uint32_t index) {
1559 DCHECK(IsValidFunction(index)); 1542 DCHECK(IsValidFunction(index));
1560 // Always make a direct call to whatever is in the table at that location. 1543 // Always make a direct call to whatever is in the table at that location.
1561 // A wrapper will be generated for FFI calls. 1544 // A wrapper will be generated for FFI calls.
1562 const WasmFunction* function = &module->functions[index]; 1545 const WasmFunction* function = &module->functions[index];
1563 return GetWasmCallDescriptor(zone, function->sig); 1546 return GetWasmCallDescriptor(zone, function->sig);
1564 } 1547 }
1565 1548
1566 Handle<Object> GetWasmFunctionNameOrNull(Isolate* isolate, Handle<Object> wasm, 1549 Handle<Object> GetWasmFunctionNameOrNull(Isolate* isolate, Handle<Object> wasm,
(...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after
1771 Object* mem = instance->GetInternalField(kWasmMemArrayBuffer); 1754 Object* mem = instance->GetInternalField(kWasmMemArrayBuffer);
1772 DCHECK(IsWasmObject(*instance)); 1755 DCHECK(IsWasmObject(*instance));
1773 if (mem->IsUndefined(isolate)) return MaybeHandle<JSArrayBuffer>(); 1756 if (mem->IsUndefined(isolate)) return MaybeHandle<JSArrayBuffer>();
1774 return Handle<JSArrayBuffer>(JSArrayBuffer::cast(mem)); 1757 return Handle<JSArrayBuffer>(JSArrayBuffer::cast(mem));
1775 } 1758 }
1776 1759
1777 void SetInstanceMemory(Handle<JSObject> instance, JSArrayBuffer* buffer) { 1760 void SetInstanceMemory(Handle<JSObject> instance, JSArrayBuffer* buffer) {
1778 DisallowHeapAllocation no_gc; 1761 DisallowHeapAllocation no_gc;
1779 DCHECK(IsWasmObject(*instance)); 1762 DCHECK(IsWasmObject(*instance));
1780 instance->SetInternalField(kWasmMemArrayBuffer, buffer); 1763 instance->SetInternalField(kWasmMemArrayBuffer, buffer);
1781 WasmCompiledModule* module = 1764 Object* module = instance->GetInternalField(kWasmCompiledModule);
1782 WasmCompiledModule::cast(instance->GetInternalField(kWasmCompiledModule)); 1765 if (module->IsFixedArray()) {
1783 module->set_ptr_to_heap(buffer); 1766 WasmCompiledModule::cast(module)->set_mem_size(
1767 buffer->byte_length()->Number());
1768 }
1784 } 1769 }
1785 1770
1786 namespace testing { 1771 namespace testing {
1787 1772
1788 void ValidateInstancesChain(Isolate* isolate, Handle<JSObject> module_obj, 1773 void ValidateInstancesChain(Isolate* isolate, Handle<JSObject> module_obj,
1789 int instance_count) { 1774 int instance_count) {
1790 CHECK_GE(instance_count, 0); 1775 CHECK_GE(instance_count, 0);
1791 DisallowHeapAllocation no_gc; 1776 DisallowHeapAllocation no_gc;
1792 WasmCompiledModule* compiled_module = 1777 WasmCompiledModule* compiled_module =
1793 WasmCompiledModule::cast(module_obj->GetInternalField(0)); 1778 WasmCompiledModule::cast(module_obj->GetInternalField(0));
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
1830 WasmCompiledModule* compiled_module = 1815 WasmCompiledModule* compiled_module =
1831 WasmCompiledModule::cast(instance->GetInternalField(kWasmCompiledModule)); 1816 WasmCompiledModule::cast(instance->GetInternalField(kWasmCompiledModule));
1832 CHECK(compiled_module->has_weak_module_object()); 1817 CHECK(compiled_module->has_weak_module_object());
1833 CHECK(compiled_module->ptr_to_weak_module_object()->cleared()); 1818 CHECK(compiled_module->ptr_to_weak_module_object()->cleared());
1834 } 1819 }
1835 1820
1836 } // namespace testing 1821 } // namespace testing
1837 } // namespace wasm 1822 } // namespace wasm
1838 } // namespace internal 1823 } // namespace internal
1839 } // namespace v8 1824 } // namespace v8
OLDNEW
« no previous file with comments | « src/wasm/wasm-module.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698