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

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

Issue 2056633002: [wasm] Separate compilation from instantiation (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: feedback Created 4 years, 6 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') | src/x64/assembler-x64.cc » ('j') | 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 "src/base/atomic-utils.h" 5 #include "src/base/atomic-utils.h"
6 #include "src/macro-assembler.h" 6 #include "src/macro-assembler.h"
7 #include "src/objects.h" 7 #include "src/objects.h"
8 #include "src/property-descriptor.h" 8 #include "src/property-descriptor.h"
9 #include "src/v8.h" 9 #include "src/v8.h"
10 10
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
105 os.write(name.start(), name.length()); 105 os.write(name.start(), name.length());
106 } else { 106 } else {
107 os << "+" << pair.function_->func_index; 107 os << "+" << pair.function_->func_index;
108 } 108 }
109 } else { 109 } else {
110 os << "?"; 110 os << "?";
111 } 111 }
112 return os; 112 return os;
113 } 113 }
114 114
115 // A helper class for compiling multiple wasm functions that offers
116 // placeholder code objects for calling functions that are not yet compiled.
117 class WasmLinker {
118 public:
119 WasmLinker(Isolate* isolate, std::vector<Handle<Code>>* functions)
120 : isolate_(isolate),
121 placeholder_code_(functions->size()),
122 function_code_(functions) {
123 for (uint32_t i = 0; i < placeholder_code_.size(); ++i) {
124 CreatePlaceholder(i);
125 }
126 }
127
128 Handle<Code> GetPlaceholderCode(uint32_t index) const {
129 return placeholder_code_[index];
130 }
131
132 void Finish(uint32_t index, Handle<Code> code) {
133 DCHECK(index < function_code().size());
134 function_code()[index] = code;
135 }
136
137 void Link(Handle<FixedArray> function_table,
138 const std::vector<uint16_t>& functions) {
139 for (size_t i = 0; i < function_code().size(); i++) {
140 LinkFunction(function_code()[i]);
141 }
142 if (!function_table.is_null()) {
143 int table_size = static_cast<int>(functions.size());
144 DCHECK_EQ(function_table->length(), table_size * 2);
145 for (int i = 0; i < table_size; i++) {
146 function_table->set(i + table_size, *function_code()[functions[i]]);
147 }
148 }
149 }
150
151 private:
152 std::vector<Handle<Code>>& function_code() { return *function_code_; }
153
154 void CreatePlaceholder(uint32_t index) {
155 DCHECK(index < function_code().size());
156 DCHECK(function_code()[index].is_null());
157 // Create a placeholder code object and encode the corresponding index in
158 // the {constant_pool_offset} field of the code object.
159 // TODO(titzer): placeholder code objects are somewhat dangerous.
160 byte buffer[] = {0, 0, 0, 0, 0, 0, 0, 0}; // fake instructions.
161 CodeDesc desc = {buffer, 8, 8, 0, 0, nullptr};
162 Handle<Code> code = isolate_->factory()->NewCode(
163 desc, Code::KindField::encode(Code::WASM_FUNCTION),
164 Handle<Object>::null());
165 code->set_constant_pool_offset(static_cast<int>(index) +
166 kPlaceholderMarker);
167 placeholder_code_[index] = code;
168 function_code()[index] = code;
169 }
170
171 Isolate* isolate_;
172 std::vector<Handle<Code>> placeholder_code_;
173 std::vector<Handle<Code>>* function_code_;
174
175 void LinkFunction(Handle<Code> code) {
176 bool modified = false;
177 int mode_mask = RelocInfo::kCodeTargetMask;
178 AllowDeferredHandleDereference embedding_raw_address;
179 for (RelocIterator it(*code, mode_mask); !it.done(); it.next()) {
180 RelocInfo::Mode mode = it.rinfo()->rmode();
181 if (RelocInfo::IsCodeTarget(mode)) {
182 Code* target =
183 Code::GetCodeFromTargetAddress(it.rinfo()->target_address());
184 if (target->kind() == Code::WASM_FUNCTION &&
185 target->constant_pool_offset() >= kPlaceholderMarker) {
186 // Patch direct calls to placeholder code objects.
187 uint32_t index = target->constant_pool_offset() - kPlaceholderMarker;
188 CHECK(index < function_code().size());
189 Handle<Code> new_target = function_code()[index];
190 if (target != *new_target) {
191 CHECK_EQ(*placeholder_code_[index], target);
192 it.rinfo()->set_target_address(new_target->instruction_start(),
193 SKIP_WRITE_BARRIER,
194 SKIP_ICACHE_FLUSH);
195 modified = true;
196 }
197 }
198 }
199 }
200 if (modified) {
201 Assembler::FlushICache(isolate_, code->instruction_start(),
202 code->instruction_size());
203 }
204 }
205 };
206
207 namespace { 115 namespace {
208 // Internal constants for the layout of the module object. 116 // Internal constants for the layout of the module object.
209 const int kWasmModuleFunctionTable = 0; 117 const int kWasmModuleFunctionTable = 0;
210 const int kWasmModuleCodeTable = 1; 118 const int kWasmModuleCodeTable = 1;
211 const int kWasmMemArrayBuffer = 2; 119 const int kWasmMemArrayBuffer = 2;
212 const int kWasmGlobalsArrayBuffer = 3; 120 const int kWasmGlobalsArrayBuffer = 3;
213 // TODO(clemensh): Remove function name array, extract names from module bytes. 121 // TODO(clemensh): Remove function name array, extract names from module bytes.
214 const int kWasmFunctionNamesArray = 4; 122 const int kWasmFunctionNamesArray = 4;
215 const int kWasmModuleBytesString = 5; 123 const int kWasmModuleBytesString = 5;
216 const int kWasmDebugInfo = 6; 124 const int kWasmDebugInfo = 6;
217 const int kWasmModuleInternalFieldCount = 7; 125 const int kWasmModuleInternalFieldCount = 7;
218 126
127 uint32_t GetMinModuleMemSize(const WasmModule* module) {
128 return WasmModule::kPageSize * module->min_mem_pages;
129 }
130
219 void LoadDataSegments(const WasmModule* module, byte* mem_addr, 131 void LoadDataSegments(const WasmModule* module, byte* mem_addr,
220 size_t mem_size) { 132 size_t mem_size) {
221 for (const WasmDataSegment& segment : module->data_segments) { 133 for (const WasmDataSegment& segment : module->data_segments) {
222 if (!segment.init) continue; 134 if (!segment.init) continue;
223 if (!segment.source_size) continue; 135 if (!segment.source_size) continue;
224 CHECK_LT(segment.dest_addr, mem_size); 136 CHECK_LT(segment.dest_addr, mem_size);
225 CHECK_LE(segment.source_size, mem_size); 137 CHECK_LE(segment.source_size, mem_size);
226 CHECK_LE(segment.dest_addr + segment.source_size, mem_size); 138 CHECK_LE(segment.dest_addr + segment.source_size, mem_size);
227 byte* addr = mem_addr + segment.dest_addr; 139 byte* addr = mem_addr + segment.dest_addr;
228 memcpy(addr, module->module_start + segment.source_offset, 140 memcpy(addr, module->module_start + segment.source_offset,
(...skipping 11 matching lines...) Expand all
240 for (int i = 0; i < table_size; i++) { 152 for (int i = 0; i < table_size; i++) {
241 const WasmFunction* function = 153 const WasmFunction* function =
242 &module->functions[module->function_table[i]]; 154 &module->functions[module->function_table[i]];
243 fixed->set(i, Smi::FromInt(function->sig_index)); 155 fixed->set(i, Smi::FromInt(function->sig_index));
244 } 156 }
245 return fixed; 157 return fixed;
246 } 158 }
247 159
248 Handle<JSArrayBuffer> NewArrayBuffer(Isolate* isolate, size_t size, 160 Handle<JSArrayBuffer> NewArrayBuffer(Isolate* isolate, size_t size,
249 byte** backing_store) { 161 byte** backing_store) {
162 *backing_store = nullptr;
250 if (size > (WasmModule::kMaxMemPages * WasmModule::kPageSize)) { 163 if (size > (WasmModule::kMaxMemPages * WasmModule::kPageSize)) {
251 // TODO(titzer): lift restriction on maximum memory allocated here. 164 // TODO(titzer): lift restriction on maximum memory allocated here.
252 *backing_store = nullptr;
253 return Handle<JSArrayBuffer>::null(); 165 return Handle<JSArrayBuffer>::null();
254 } 166 }
255 void* memory = 167 void* memory = isolate->array_buffer_allocator()->Allocate(size);
256 isolate->array_buffer_allocator()->Allocate(static_cast<int>(size)); 168 if (memory == nullptr) {
257 if (!memory) {
258 *backing_store = nullptr;
259 return Handle<JSArrayBuffer>::null(); 169 return Handle<JSArrayBuffer>::null();
260 } 170 }
261 171
262 *backing_store = reinterpret_cast<byte*>(memory); 172 *backing_store = reinterpret_cast<byte*>(memory);
263 173
264 #if DEBUG 174 #if DEBUG
265 // Double check the API allocator actually zero-initialized the memory. 175 // Double check the API allocator actually zero-initialized the memory.
266 byte* bytes = reinterpret_cast<byte*>(*backing_store); 176 byte* bytes = reinterpret_cast<byte*>(*backing_store);
267 for (size_t i = 0; i < size; i++) { 177 for (size_t i = 0; i < size; i++) {
268 DCHECK_EQ(0, bytes[i]); 178 DCHECK_EQ(0, bytes[i]);
269 } 179 }
270 #endif 180 #endif
271 181
272 Handle<JSArrayBuffer> buffer = isolate->factory()->NewJSArrayBuffer(); 182 Handle<JSArrayBuffer> buffer = isolate->factory()->NewJSArrayBuffer();
273 JSArrayBuffer::Setup(buffer, isolate, false, memory, static_cast<int>(size)); 183 JSArrayBuffer::Setup(buffer, isolate, false, memory, static_cast<int>(size));
274 buffer->set_is_neuterable(false); 184 buffer->set_is_neuterable(false);
275 return buffer; 185 return buffer;
276 } 186 }
277 187
188 void RelocateInstanceCode(WasmModuleInstance* instance) {
189 for (uint32_t i = 0; i < instance->function_code.size(); ++i) {
190 Handle<Code> function = instance->function_code[i];
191 AllowDeferredHandleDereference embedding_raw_address;
192 int mask = (1 << RelocInfo::WASM_MEMORY_REFERENCE) |
193 (1 << RelocInfo::WASM_MEMORY_SIZE_REFERENCE);
194 for (RelocIterator it(*function, mask); !it.done(); it.next()) {
195 it.rinfo()->update_wasm_memory_reference(
196 nullptr, instance->mem_start, GetMinModuleMemSize(instance->module),
197 static_cast<uint32_t>(instance->mem_size));
198 }
199 }
200 }
201
278 // Set the memory for a module instance to be the {memory} array buffer. 202 // Set the memory for a module instance to be the {memory} array buffer.
279 void SetMemory(WasmModuleInstance* instance, Handle<JSArrayBuffer> memory) { 203 void SetMemory(WasmModuleInstance* instance, Handle<JSArrayBuffer> memory) {
280 memory->set_is_neuterable(false); 204 memory->set_is_neuterable(false);
281 instance->mem_start = reinterpret_cast<byte*>(memory->backing_store()); 205 instance->mem_start = reinterpret_cast<byte*>(memory->backing_store());
282 instance->mem_size = memory->byte_length()->Number(); 206 instance->mem_size = memory->byte_length()->Number();
283 instance->mem_buffer = memory; 207 instance->mem_buffer = memory;
208 RelocateInstanceCode(instance);
284 } 209 }
285 210
286 // Allocate memory for a module instance as a new JSArrayBuffer. 211 // Allocate memory for a module instance as a new JSArrayBuffer.
287 bool AllocateMemory(ErrorThrower* thrower, Isolate* isolate, 212 bool AllocateMemory(ErrorThrower* thrower, Isolate* isolate,
288 WasmModuleInstance* instance) { 213 WasmModuleInstance* instance) {
289 DCHECK(instance->module); 214 DCHECK(instance->module);
290 DCHECK(instance->mem_buffer.is_null()); 215 DCHECK(instance->mem_buffer.is_null());
291 216
292 if (instance->module->min_mem_pages > WasmModule::kMaxMemPages) { 217 if (instance->module->min_mem_pages > WasmModule::kMaxMemPages) {
293 thrower->Error("Out of memory: wasm memory too large"); 218 thrower->Error("Out of memory: wasm memory too large");
294 return false; 219 return false;
295 } 220 }
296 instance->mem_size = WasmModule::kPageSize * instance->module->min_mem_pages; 221 instance->mem_size = GetMinModuleMemSize(instance->module);
297 instance->mem_buffer = 222 instance->mem_buffer =
298 NewArrayBuffer(isolate, instance->mem_size, &instance->mem_start); 223 NewArrayBuffer(isolate, instance->mem_size, &instance->mem_start);
299 if (!instance->mem_start) { 224 if (instance->mem_start == nullptr) {
300 thrower->Error("Out of memory: wasm memory"); 225 thrower->Error("Out of memory: wasm memory");
301 instance->mem_size = 0; 226 instance->mem_size = 0;
302 return false; 227 return false;
303 } 228 }
229 RelocateInstanceCode(instance);
304 return true; 230 return true;
305 } 231 }
306 232
307 bool AllocateGlobals(ErrorThrower* thrower, Isolate* isolate, 233 bool AllocateGlobals(ErrorThrower* thrower, Isolate* isolate,
308 WasmModuleInstance* instance) { 234 WasmModuleInstance* instance) {
309 uint32_t globals_size = instance->module->globals_size; 235 uint32_t globals_size = instance->module->globals_size;
310 if (globals_size > 0) { 236 if (globals_size > 0) {
311 instance->globals_buffer = 237 instance->globals_buffer =
312 NewArrayBuffer(isolate, globals_size, &instance->globals_start); 238 NewArrayBuffer(isolate, globals_size, &instance->globals_start);
313 if (!instance->globals_start) { 239 if (!instance->globals_start) {
314 // Not enough space for backing store of globals. 240 // Not enough space for backing store of globals.
315 thrower->Error("Out of memory: wasm globals"); 241 thrower->Error("Out of memory: wasm globals");
316 return false; 242 return false;
317 } 243 }
244
245 for (uint32_t i = 0; i < instance->function_code.size(); ++i) {
246 Handle<Code> function = instance->function_code[i];
247 AllowDeferredHandleDereference embedding_raw_address;
248 int mask = 1 << RelocInfo::WASM_GLOBAL_REFERENCE;
249 for (RelocIterator it(*function, mask); !it.done(); it.next()) {
250 it.rinfo()->update_wasm_global_reference(nullptr,
251 instance->globals_start);
252 }
253 }
318 } 254 }
319 return true; 255 return true;
320 } 256 }
257
258 Handle<Code> CreatePlaceholder(Factory* factory, uint32_t index,
259 Code::Kind kind) {
260 // Create a placeholder code object and encode the corresponding index in
261 // the {constant_pool_offset} field of the code object.
262 // TODO(titzer): placeholder code objects are somewhat dangerous.
263 static byte buffer[] = {0, 0, 0, 0, 0, 0, 0, 0}; // fake instructions.
264 static CodeDesc desc = {buffer, 8, 8, 0, 0, nullptr};
265 Handle<Code> code = factory->NewCode(desc, Code::KindField::encode(kind),
266 Handle<Object>::null());
267 code->set_constant_pool_offset(static_cast<int>(index) + kPlaceholderMarker);
268 return code;
269 }
270
271 // TODO(mtrofin): remove when we stop relying on placeholders.
272 void InitializePlaceholders(Factory* factory,
273 std::vector<Handle<Code>>* placeholders,
274 size_t size) {
275 DCHECK(placeholders->empty());
276 placeholders->reserve(size);
277
278 for (uint32_t i = 0; i < size; ++i) {
279 placeholders->push_back(CreatePlaceholder(factory, i, Code::WASM_FUNCTION));
280 }
281 }
282
283 bool LinkFunction(Handle<Code> unlinked,
284 const std::vector<Handle<Code>>& code_targets,
285 Code::Kind kind) {
286 bool modified = false;
287 int mode_mask = RelocInfo::kCodeTargetMask;
288 AllowDeferredHandleDereference embedding_raw_address;
289 for (RelocIterator it(*unlinked, mode_mask); !it.done(); it.next()) {
290 RelocInfo::Mode mode = it.rinfo()->rmode();
291 if (RelocInfo::IsCodeTarget(mode)) {
292 Code* target =
293 Code::GetCodeFromTargetAddress(it.rinfo()->target_address());
294 if (target->kind() == kind &&
295 target->constant_pool_offset() >= kPlaceholderMarker) {
296 // Patch direct calls to placeholder code objects.
297 uint32_t index = target->constant_pool_offset() - kPlaceholderMarker;
298 CHECK(index < code_targets.size());
299 Handle<Code> new_target = code_targets[index];
300 if (target != *new_target) {
301 it.rinfo()->set_target_address(new_target->instruction_start(),
302 SKIP_WRITE_BARRIER, SKIP_ICACHE_FLUSH);
303 modified = true;
304 }
305 }
306 }
307 }
308 return modified;
309 }
310
311 void LinkModuleFunctions(Isolate* isolate,
312 std::vector<Handle<Code>>& functions) {
313 for (size_t i = 0; i < functions.size(); i++) {
314 Handle<Code> code = functions[i];
315 bool modified = LinkFunction(code, functions, Code::WASM_FUNCTION);
316 if (modified) {
317 Assembler::FlushICache(isolate, code->instruction_start(),
318 code->instruction_size());
319 }
320 }
321 }
322
323 void LinkImports(Isolate* isolate, std::vector<Handle<Code>>& functions,
324 const std::vector<Handle<Code>>& imports) {
325 for (uint32_t i = 0; i < functions.size(); ++i) {
326 Handle<Code> code = functions[i];
327 bool modified = LinkFunction(code, imports, Code::WASM_TO_JS_FUNCTION);
328 if (modified) {
329 Assembler::FlushICache(isolate, code->instruction_start(),
330 code->instruction_size());
331 }
332 }
333 }
334
321 } // namespace 335 } // namespace
322 336
323 WasmModule::WasmModule() 337 WasmModule::WasmModule()
324 : module_start(nullptr), 338 : module_start(nullptr),
325 module_end(nullptr), 339 module_end(nullptr),
326 min_mem_pages(0), 340 min_mem_pages(0),
327 max_mem_pages(0), 341 max_mem_pages(0),
328 mem_export(false), 342 mem_export(false),
329 mem_external(false), 343 mem_external(false),
330 start_function_index(-1), 344 start_function_index(-1),
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after
473 PrintF("Total generated wasm code: %zu bytes\n", code_size); 487 PrintF("Total generated wasm code: %zu bytes\n", code_size);
474 PrintF("Total generated wasm reloc: %zu bytes\n", reloc_size); 488 PrintF("Total generated wasm reloc: %zu bytes\n", reloc_size);
475 } 489 }
476 } 490 }
477 }; 491 };
478 492
479 bool CompileWrappersToImportedFunctions( 493 bool CompileWrappersToImportedFunctions(
480 Isolate* isolate, const WasmModule* module, const Handle<JSReceiver> ffi, 494 Isolate* isolate, const WasmModule* module, const Handle<JSReceiver> ffi,
481 WasmModuleInstance* instance, ErrorThrower* thrower, Factory* factory, 495 WasmModuleInstance* instance, ErrorThrower* thrower, Factory* factory,
482 ModuleEnv* module_env, CodeStats& code_stats) { 496 ModuleEnv* module_env, CodeStats& code_stats) {
483 uint32_t index = 0;
484 if (module->import_table.size() > 0) { 497 if (module->import_table.size() > 0) {
485 instance->import_code.reserve(module->import_table.size()); 498 instance->import_code.reserve(module->import_table.size());
486 for (const WasmImport& import : module->import_table) { 499 for (uint32_t index = 0; index < module->import_table.size(); ++index) {
500 const WasmImport& import = module->import_table[index];
487 WasmName module_name = module->GetNameOrNull(import.module_name_offset, 501 WasmName module_name = module->GetNameOrNull(import.module_name_offset,
488 import.module_name_length); 502 import.module_name_length);
489 WasmName function_name = module->GetNameOrNull( 503 WasmName function_name = module->GetNameOrNull(
490 import.function_name_offset, import.function_name_length); 504 import.function_name_offset, import.function_name_length);
491 MaybeHandle<JSFunction> function = LookupFunction( 505 MaybeHandle<JSFunction> function = LookupFunction(
492 *thrower, factory, ffi, index, module_name, function_name); 506 *thrower, factory, ffi, index, module_name, function_name);
493 if (function.is_null()) return false; 507 if (function.is_null()) return false;
494 508
495 Handle<Code> code = compiler::CompileWasmToJSWrapper( 509 Handle<Code> code = compiler::CompileWasmToJSWrapper(
496 isolate, module_env, function.ToHandleChecked(), import.sig, 510 isolate, module_env, function.ToHandleChecked(), import.sig,
497 module_name, function_name); 511 module_name, function_name);
498 instance->import_code.push_back(code); 512 instance->import_code[index] = code;
499 code_stats.Record(*code); 513 code_stats.Record(*code);
500 index++;
501 } 514 }
502 } 515 }
503 return true; 516 return true;
504 } 517 }
505 518
506 void InitializeParallelCompilation( 519 void InitializeParallelCompilation(
507 Isolate* isolate, const std::vector<WasmFunction>& functions, 520 Isolate* isolate, const std::vector<WasmFunction>& functions,
508 std::vector<compiler::WasmCompilationUnit*>& compilation_units, 521 std::vector<compiler::WasmCompilationUnit*>& compilation_units,
509 ModuleEnv& module_env, ErrorThrower& thrower) { 522 ModuleEnv& module_env, ErrorThrower& thrower) {
510 for (uint32_t i = FLAG_skip_compiling_wasm_funcs; i < functions.size(); i++) { 523 for (uint32_t i = FLAG_skip_compiling_wasm_funcs; i < functions.size(); i++) {
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after
650 thrower, isolate, module_env, &func); 663 thrower, isolate, module_env, &func);
651 if (code.is_null()) { 664 if (code.is_null()) {
652 thrower->Error("Compilation of #%d:%.*s failed.", i, str.length(), 665 thrower->Error("Compilation of #%d:%.*s failed.", i, str.length(),
653 str.start()); 666 str.start());
654 break; 667 break;
655 } 668 }
656 // Install the code into the linker table. 669 // Install the code into the linker table.
657 functions[i] = code; 670 functions[i] = code;
658 } 671 }
659 } 672 }
673
674 void PopulateFunctionTable(WasmModuleInstance* instance) {
675 if (!instance->function_table.is_null()) {
676 int table_size = static_cast<int>(instance->module->function_table.size());
677 DCHECK_EQ(instance->function_table->length(), table_size * 2);
678 for (int i = 0; i < table_size; i++) {
679 instance->function_table->set(
680 i + table_size,
681 *instance->function_code[instance->module->function_table[i]]);
682 }
683 }
684 }
660 } // namespace 685 } // namespace
661 686
662 void SetDeoptimizationData(Factory* factory, Handle<JSObject> js_object, 687 void SetDeoptimizationData(Factory* factory, Handle<JSObject> js_object,
663 std::vector<Handle<Code>>& functions) { 688 std::vector<Handle<Code>>& functions) {
664 for (size_t i = FLAG_skip_compiling_wasm_funcs; i < functions.size(); ++i) { 689 for (size_t i = FLAG_skip_compiling_wasm_funcs; i < functions.size(); ++i) {
665 Handle<Code> code = functions[i]; 690 Handle<Code> code = functions[i];
666 DCHECK(code->deoptimization_data() == nullptr || 691 DCHECK(code->deoptimization_data() == nullptr ||
667 code->deoptimization_data()->length() == 0); 692 code->deoptimization_data()->length() == 0);
668 Handle<FixedArray> deopt_data = factory->NewFixedArray(2, TENURED); 693 Handle<FixedArray> deopt_data = factory->NewFixedArray(2, TENURED);
669 if (!js_object.is_null()) { 694 if (!js_object.is_null()) {
670 deopt_data->set(0, *js_object); 695 deopt_data->set(0, *js_object);
671 } 696 }
672 deopt_data->set(1, Smi::FromInt(static_cast<int>(i))); 697 deopt_data->set(1, Smi::FromInt(static_cast<int>(i)));
673 deopt_data->set_length(2); 698 deopt_data->set_length(2);
674 code->set_deoptimization_data(*deopt_data); 699 code->set_deoptimization_data(*deopt_data);
675 } 700 }
676 } 701 }
677 702
703 Handle<FixedArray> WasmModule::CompileFunctions(Isolate* isolate) const {
704 Factory* factory = isolate->factory();
705 ErrorThrower thrower(isolate, "WasmModule::CompileFunctions()");
706 CodeStats code_stats;
707
708 WasmModuleInstance temp_instance_for_compilation(this);
709 temp_instance_for_compilation.function_table =
710 BuildFunctionTable(isolate, this);
711 temp_instance_for_compilation.context = isolate->native_context();
712 temp_instance_for_compilation.mem_size = GetMinModuleMemSize(this);
713 temp_instance_for_compilation.mem_start = nullptr;
714 temp_instance_for_compilation.globals_start = nullptr;
715
716 ModuleEnv module_env;
717 module_env.module = this;
718 module_env.instance = &temp_instance_for_compilation;
719 module_env.origin = origin;
720 InitializePlaceholders(factory, &module_env.placeholders, functions.size());
721
722 Handle<FixedArray> ret =
723 factory->NewFixedArray(static_cast<int>(functions.size()), TENURED);
724
725 temp_instance_for_compilation.import_code.resize(import_table.size());
726 for (uint32_t i = 0; i < import_table.size(); ++i) {
727 temp_instance_for_compilation.import_code[i] =
728 CreatePlaceholder(factory, i, Code::WASM_TO_JS_FUNCTION);
729 }
730 isolate->counters()->wasm_functions_per_module()->AddSample(
731 static_cast<int>(functions.size()));
732 if (FLAG_wasm_num_compilation_tasks != 0) {
733 CompileInParallel(isolate, this,
734 temp_instance_for_compilation.function_code, &thrower,
735 &module_env);
736 } else {
737 CompileSequentially(isolate, this,
738 temp_instance_for_compilation.function_code, &thrower,
739 &module_env);
740 }
741 if (thrower.error()) {
742 return Handle<FixedArray>::null();
743 }
744
745 LinkModuleFunctions(isolate, temp_instance_for_compilation.function_code);
746
747 // At this point, compilation has completed. Update the code table
748 // and record sizes.
749 for (size_t i = FLAG_skip_compiling_wasm_funcs;
750 i < temp_instance_for_compilation.function_code.size(); ++i) {
751 Code* code = *temp_instance_for_compilation.function_code[i];
752 ret->set(static_cast<int>(i), code);
753 code_stats.Record(code);
754 }
755
756 PopulateFunctionTable(&temp_instance_for_compilation);
757
758 return ret;
759 }
760
678 // Instantiates a wasm module as a JSObject. 761 // Instantiates a wasm module as a JSObject.
679 // * allocates a backing store of {mem_size} bytes. 762 // * allocates a backing store of {mem_size} bytes.
680 // * installs a named property "memory" for that buffer if exported 763 // * installs a named property "memory" for that buffer if exported
681 // * installs named properties on the object for exported functions 764 // * installs named properties on the object for exported functions
682 // * compiles wasm code to machine code 765 // * compiles wasm code to machine code
683 MaybeHandle<JSObject> WasmModule::Instantiate( 766 MaybeHandle<JSObject> WasmModule::Instantiate(
684 Isolate* isolate, Handle<JSReceiver> ffi, 767 Isolate* isolate, Handle<JSReceiver> ffi,
685 Handle<JSArrayBuffer> memory) const { 768 Handle<JSArrayBuffer> memory) const {
686 HistogramTimerScope wasm_instantiate_module_time_scope( 769 HistogramTimerScope wasm_instantiate_module_time_scope(
687 isolate->counters()->wasm_instantiate_module_time()); 770 isolate->counters()->wasm_instantiate_module_time());
688 ErrorThrower thrower(isolate, "WasmModule::Instantiate()"); 771 ErrorThrower thrower(isolate, "WasmModule::Instantiate()");
689 Factory* factory = isolate->factory(); 772 Factory* factory = isolate->factory();
690 773
691 // If FLAG_print_wasm_code_size is set, this aggregates the sum of all code 774 // If FLAG_print_wasm_code_size is set, this aggregates the sum of all code
692 // objects created for this module. 775 // objects created for this module.
693 // TODO(titzer): switch this to TRACE_EVENT 776 // TODO(titzer): switch this to TRACE_EVENT
694 CodeStats code_stats; 777 CodeStats code_stats;
695 778
696 //------------------------------------------------------------------------- 779 //-------------------------------------------------------------------------
697 // Allocate the instance and its JS counterpart. 780 // Allocate the instance and its JS counterpart.
698 //------------------------------------------------------------------------- 781 //-------------------------------------------------------------------------
699 Handle<Map> map = factory->NewMap( 782 Handle<Map> map = factory->NewMap(
700 JS_OBJECT_TYPE, 783 JS_OBJECT_TYPE,
701 JSObject::kHeaderSize + kWasmModuleInternalFieldCount * kPointerSize); 784 JSObject::kHeaderSize + kWasmModuleInternalFieldCount * kPointerSize);
702 WasmModuleInstance instance(this); 785 WasmModuleInstance instance(this);
703 instance.context = isolate->native_context(); 786 instance.context = isolate->native_context();
704 instance.js_object = factory->NewJSObjectFromMap(map, TENURED); 787 instance.js_object = factory->NewJSObjectFromMap(map, TENURED);
705 Handle<FixedArray> code_table = 788
706 factory->NewFixedArray(static_cast<int>(functions.size()), TENURED); 789 Handle<FixedArray> code_table = CompileFunctions(isolate);
790 if (code_table.is_null()) return Handle<JSObject>::null();
791
707 instance.js_object->SetInternalField(kWasmModuleCodeTable, *code_table); 792 instance.js_object->SetInternalField(kWasmModuleCodeTable, *code_table);
708 size_t module_bytes_len = 793 size_t module_bytes_len =
709 instance.module->module_end - instance.module->module_start; 794 instance.module->module_end - instance.module->module_start;
710 DCHECK_LE(module_bytes_len, static_cast<size_t>(kMaxInt)); 795 DCHECK_LE(module_bytes_len, static_cast<size_t>(kMaxInt));
711 Vector<const uint8_t> module_bytes_vec(instance.module->module_start, 796 Vector<const uint8_t> module_bytes_vec(instance.module->module_start,
712 static_cast<int>(module_bytes_len)); 797 static_cast<int>(module_bytes_len));
713 Handle<String> module_bytes_string = 798 Handle<String> module_bytes_string =
714 factory->NewStringFromOneByte(module_bytes_vec, TENURED) 799 factory->NewStringFromOneByte(module_bytes_vec, TENURED)
715 .ToHandleChecked(); 800 .ToHandleChecked();
716 instance.js_object->SetInternalField(kWasmModuleBytesString, 801 instance.js_object->SetInternalField(kWasmModuleBytesString,
717 *module_bytes_string); 802 *module_bytes_string);
718 803
804 for (uint32_t i = 0; i < functions.size(); ++i) {
805 Handle<Code> code = Handle<Code>(Code::cast(code_table->get(i)));
806 instance.function_code[i] = code;
807 }
808
719 //------------------------------------------------------------------------- 809 //-------------------------------------------------------------------------
720 // Allocate and initialize the linear memory. 810 // Allocate and initialize the linear memory.
721 //------------------------------------------------------------------------- 811 //-------------------------------------------------------------------------
722 isolate->counters()->wasm_min_mem_pages_count()->AddSample( 812 isolate->counters()->wasm_min_mem_pages_count()->AddSample(
723 instance.module->min_mem_pages); 813 instance.module->min_mem_pages);
724 isolate->counters()->wasm_max_mem_pages_count()->AddSample( 814 isolate->counters()->wasm_max_mem_pages_count()->AddSample(
725 instance.module->max_mem_pages); 815 instance.module->max_mem_pages);
726 if (memory.is_null()) { 816 if (memory.is_null()) {
727 if (!AllocateMemory(&thrower, isolate, &instance)) { 817 if (!AllocateMemory(&thrower, isolate, &instance)) {
728 return MaybeHandle<JSObject>(); 818 return MaybeHandle<JSObject>();
(...skipping 12 matching lines...) Expand all
741 return MaybeHandle<JSObject>(); 831 return MaybeHandle<JSObject>();
742 } 832 }
743 if (!instance.globals_buffer.is_null()) { 833 if (!instance.globals_buffer.is_null()) {
744 instance.js_object->SetInternalField(kWasmGlobalsArrayBuffer, 834 instance.js_object->SetInternalField(kWasmGlobalsArrayBuffer,
745 *instance.globals_buffer); 835 *instance.globals_buffer);
746 } 836 }
747 837
748 HistogramTimerScope wasm_compile_module_time_scope( 838 HistogramTimerScope wasm_compile_module_time_scope(
749 isolate->counters()->wasm_compile_module_time()); 839 isolate->counters()->wasm_compile_module_time());
750 840
751 instance.function_table = BuildFunctionTable(isolate, this);
752 WasmLinker linker(isolate, &instance.function_code);
753 ModuleEnv module_env; 841 ModuleEnv module_env;
754 module_env.module = this; 842 module_env.module = this;
755 module_env.instance = &instance; 843 module_env.instance = &instance;
756 module_env.linker = &linker;
757 module_env.origin = origin; 844 module_env.origin = origin;
758 845
759 //------------------------------------------------------------------------- 846 //-------------------------------------------------------------------------
760 // Compile wrappers to imported functions. 847 // Compile wrappers to imported functions.
761 //------------------------------------------------------------------------- 848 //-------------------------------------------------------------------------
762 if (!CompileWrappersToImportedFunctions(isolate, this, ffi, &instance, 849 if (!CompileWrappersToImportedFunctions(isolate, this, ffi, &instance,
763 &thrower, factory, &module_env, 850 &thrower, factory, &module_env,
764 code_stats)) { 851 code_stats)) {
765 return MaybeHandle<JSObject>(); 852 return MaybeHandle<JSObject>();
766 } 853 }
767 //-------------------------------------------------------------------------
768 // Compile all functions in the module.
769 //-------------------------------------------------------------------------
770 { 854 {
771 isolate->counters()->wasm_functions_per_module()->AddSample(
772 static_cast<int>(functions.size()));
773 if (FLAG_wasm_num_compilation_tasks != 0) {
774 CompileInParallel(isolate, this, instance.function_code, &thrower,
775 &module_env);
776 } else {
777 // 5) The main thread finishes the compilation.
778 CompileSequentially(isolate, this, instance.function_code, &thrower,
779 &module_env);
780 }
781 if (thrower.error()) {
782 return Handle<JSObject>::null();
783 }
784
785 // At this point, compilation has completed. Update the code table
786 // and record sizes.
787 for (size_t i = FLAG_skip_compiling_wasm_funcs;
788 i < instance.function_code.size(); ++i) {
789 Code* code = *instance.function_code[i];
790 code_table->set(static_cast<int>(i), code);
791 code_stats.Record(code);
792 }
793
794 // Patch all direct call sites.
795 linker.Link(instance.function_table, this->function_table);
796 instance.js_object->SetInternalField(kWasmModuleFunctionTable, 855 instance.js_object->SetInternalField(kWasmModuleFunctionTable,
797 Smi::FromInt(0)); 856 Smi::FromInt(0));
857 LinkImports(isolate, instance.function_code, instance.import_code);
798 858
799 SetDeoptimizationData(factory, instance.js_object, instance.function_code); 859 SetDeoptimizationData(factory, instance.js_object, instance.function_code);
800 860
801 //------------------------------------------------------------------------- 861 //-------------------------------------------------------------------------
802 // Create and populate the exports object. 862 // Create and populate the exports object.
803 //------------------------------------------------------------------------- 863 //-------------------------------------------------------------------------
804 if (export_table.size() > 0 || mem_export) { 864 if (export_table.size() > 0 || mem_export) {
805 Handle<JSObject> exports_object; 865 Handle<JSObject> exports_object;
806 if (origin == kWasmOrigin) { 866 if (origin == kWasmOrigin) {
807 // Create the "exports" object. 867 // Create the "exports" object.
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
871 MaybeHandle<Object> retval = 931 MaybeHandle<Object> retval =
872 Execution::Call(isolate, jsfunc, undefined, 0, nullptr); 932 Execution::Call(isolate, jsfunc, undefined, 0, nullptr);
873 933
874 if (retval.is_null()) { 934 if (retval.is_null()) {
875 thrower.Error("WASM.instantiateModule(): start function failed"); 935 thrower.Error("WASM.instantiateModule(): start function failed");
876 } 936 }
877 } 937 }
878 return instance.js_object; 938 return instance.js_object;
879 } 939 }
880 940
941 // TODO(mtrofin): remove this once we move to WASM_DIRECT_CALL
881 Handle<Code> ModuleEnv::GetCodeOrPlaceholder(uint32_t index) const { 942 Handle<Code> ModuleEnv::GetCodeOrPlaceholder(uint32_t index) const {
882 DCHECK(IsValidFunction(index)); 943 DCHECK(IsValidFunction(index));
883 if (linker != nullptr) return linker->GetPlaceholderCode(index); 944 if (!placeholders.empty()) return placeholders[index];
884 DCHECK_NOT_NULL(instance); 945 DCHECK_NOT_NULL(instance);
885 return instance->function_code[index]; 946 return instance->function_code[index];
886 } 947 }
887 948
888 Handle<Code> ModuleEnv::GetImportCode(uint32_t index) { 949 Handle<Code> ModuleEnv::GetImportCode(uint32_t index) {
889 DCHECK(IsValidImport(index)); 950 DCHECK(IsValidImport(index));
890 return instance ? instance->import_code[index] : Handle<Code>::null(); 951 return instance ? instance->import_code[index] : Handle<Code>::null();
891 } 952 }
892 953
893 compiler::CallDescriptor* ModuleEnv::GetCallDescriptor(Zone* zone, 954 compiler::CallDescriptor* ModuleEnv::GetCallDescriptor(Zone* zone,
(...skipping 27 matching lines...) Expand all
921 } 982 }
922 983
923 int32_t retval = CompileAndRunWasmModule(isolate, result.val); 984 int32_t retval = CompileAndRunWasmModule(isolate, result.val);
924 delete result.val; 985 delete result.val;
925 return retval; 986 return retval;
926 } 987 }
927 988
928 int32_t CompileAndRunWasmModule(Isolate* isolate, const WasmModule* module) { 989 int32_t CompileAndRunWasmModule(Isolate* isolate, const WasmModule* module) {
929 ErrorThrower thrower(isolate, "CompileAndRunWasmModule"); 990 ErrorThrower thrower(isolate, "CompileAndRunWasmModule");
930 WasmModuleInstance instance(module); 991 WasmModuleInstance instance(module);
992 Handle<FixedArray> code_table = module->CompileFunctions(isolate);
993
994 if (code_table.is_null()) return -1;
995
996 for (uint32_t i = 0; i < module->functions.size(); ++i) {
997 Handle<Code> code = Handle<Code>(Code::cast(code_table->get(i)));
998 instance.function_code[i] = code;
999 }
931 1000
932 // Allocate and initialize the linear memory. 1001 // Allocate and initialize the linear memory.
933 if (!AllocateMemory(&thrower, isolate, &instance)) { 1002 if (!AllocateMemory(&thrower, isolate, &instance)) {
934 return -1; 1003 return -1;
935 } 1004 }
936 LoadDataSegments(module, instance.mem_start, instance.mem_size); 1005 LoadDataSegments(module, instance.mem_start, instance.mem_size);
937 1006
938 // Allocate the globals area if necessary. 1007 // Allocate the globals area if necessary.
939 if (!AllocateGlobals(&thrower, isolate, &instance)) { 1008 if (!AllocateGlobals(&thrower, isolate, &instance)) {
940 return -1; 1009 return -1;
941 } 1010 }
942 1011
943 // Build the function table.
944 instance.function_table = BuildFunctionTable(isolate, module);
945
946 // Create module environment.
947 WasmLinker linker(isolate, &instance.function_code);
948 ModuleEnv module_env; 1012 ModuleEnv module_env;
949 module_env.module = module; 1013 module_env.module = module;
950 module_env.instance = &instance; 1014 module_env.instance = &instance;
951 module_env.linker = &linker;
952 module_env.origin = module->origin; 1015 module_env.origin = module->origin;
953 1016 InitializePlaceholders(isolate->factory(), &module_env.placeholders,
1017 module->functions.size());
954 if (module->export_table.size() == 0) { 1018 if (module->export_table.size() == 0) {
955 thrower.Error("WASM.compileRun() failed: no exported functions"); 1019 thrower.Error("WASM.compileRun() failed: no exported functions");
956 return -2; 1020 return -2;
957 } 1021 }
958 1022
959 // Compile all functions. 1023 // Compile all functions.
960 for (const WasmFunction& func : module->functions) { 1024 for (const WasmFunction& func : module->functions) {
961 // Compile the function and install it in the linker. 1025 // Compile the function and install it in the linker.
962 Handle<Code> code = compiler::WasmCompilationUnit::CompileWasmFunction( 1026 Handle<Code> code = compiler::WasmCompilationUnit::CompileWasmFunction(
963 &thrower, isolate, &module_env, &func); 1027 &thrower, isolate, &module_env, &func);
964 if (!code.is_null()) linker.Finish(func.func_index, code); 1028 if (!code.is_null()) instance.function_code[func.func_index] = code;
965 if (thrower.error()) return -1; 1029 if (thrower.error()) return -1;
966 } 1030 }
967 1031
968 linker.Link(instance.function_table, instance.module->function_table); 1032 LinkModuleFunctions(isolate, instance.function_code);
969 1033
970 // Wrap the main code so it can be called as a JS function. 1034 // Wrap the main code so it can be called as a JS function.
971 uint32_t main_index = module->export_table.back().func_index; 1035 uint32_t main_index = module->export_table.back().func_index;
972 Handle<Code> main_code = instance.function_code[main_index]; 1036 Handle<Code> main_code = instance.function_code[main_index];
973 Handle<String> name = isolate->factory()->NewStringFromStaticChars("main"); 1037 Handle<String> name = isolate->factory()->NewStringFromStaticChars("main");
974 Handle<JSObject> module_object = Handle<JSObject>(0, isolate); 1038 Handle<JSObject> module_object = Handle<JSObject>(0, isolate);
975 Handle<JSFunction> jsfunc = compiler::CompileJSToWasmWrapper( 1039 Handle<JSFunction> jsfunc = compiler::CompileJSToWasmWrapper(
976 isolate, &module_env, name, main_code, module_object, main_index); 1040 isolate, &module_env, name, main_code, module_object, main_index);
977 1041
978 // Call the JS function. 1042 // Call the JS function.
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
1052 Object* info = wasm->GetInternalField(kWasmDebugInfo); 1116 Object* info = wasm->GetInternalField(kWasmDebugInfo);
1053 if (!info->IsUndefined(wasm->GetIsolate())) return WasmDebugInfo::cast(info); 1117 if (!info->IsUndefined(wasm->GetIsolate())) return WasmDebugInfo::cast(info);
1054 Handle<WasmDebugInfo> new_info = WasmDebugInfo::New(handle(wasm)); 1118 Handle<WasmDebugInfo> new_info = WasmDebugInfo::New(handle(wasm));
1055 wasm->SetInternalField(kWasmDebugInfo, *new_info); 1119 wasm->SetInternalField(kWasmDebugInfo, *new_info);
1056 return *new_info; 1120 return *new_info;
1057 } 1121 }
1058 1122
1059 } // namespace wasm 1123 } // namespace wasm
1060 } // namespace internal 1124 } // namespace internal
1061 } // namespace v8 1125 } // namespace v8
OLDNEW
« no previous file with comments | « src/wasm/wasm-module.h ('k') | src/x64/assembler-x64.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698