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

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

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