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

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: silly fix 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
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 97 matching lines...) Expand 10 before | Expand all | Expand 10 after
108 } else { 108 } else {
109 os << "?"; 109 os << "?";
110 } 110 }
111 return os; 111 return os;
112 } 112 }
113 113
114 // A helper class for compiling multiple wasm functions that offers 114 // A helper class for compiling multiple wasm functions that offers
115 // placeholder code objects for calling functions that are not yet compiled. 115 // placeholder code objects for calling functions that are not yet compiled.
116 class WasmLinker { 116 class WasmLinker {
117 public: 117 public:
118 WasmLinker(Isolate* isolate, std::vector<Handle<Code>>* functions) 118 WasmLinker(Isolate* isolate, uint32_t function_count)
119 : isolate_(isolate), 119 : isolate_(isolate), placeholder_code_(function_count) {}
120 placeholder_code_(functions->size()), 120
121 function_code_(functions) { 121 void InitializePlaceholders() {
122 for (uint32_t i = 0; i < placeholder_code_.size(); ++i) { 122 for (uint32_t i = 0; i < placeholder_code_.size(); ++i) {
123 CreatePlaceholder(i); 123 placeholder_code_[i] =
124 CreatePlaceholder(isolate_->factory(), i, Code::WASM_FUNCTION);
124 } 125 }
125 } 126 }
126 127 // Lazy, not thread safe
127 Handle<Code> GetPlaceholderCode(uint32_t index) const { 128 Handle<Code> GetPlaceholderCode(uint32_t index) {
129 if (placeholder_code_[index].is_null()) {
130 placeholder_code_[index] =
131 CreatePlaceholder(isolate_->factory(), index, Code::WASM_FUNCTION);
132 }
128 return placeholder_code_[index]; 133 return placeholder_code_[index];
129 } 134 }
130 135
131 void Finish(uint32_t index, Handle<Code> code) { 136 static void LinkModuleFunctions(Isolate* isolate,
132 DCHECK(index < function_code().size()); 137 std::vector<Handle<Code>>& functions) {
133 function_code()[index] = code; 138 for (size_t i = 0; i < functions.size(); i++) {
134 } 139 Handle<Code> code = functions[i];
135 140 bool modified = LinkFunction(code, functions, Code::WASM_FUNCTION);
136 void Link(Handle<FixedArray> function_table, 141 if (modified) {
137 const std::vector<uint16_t>& functions) { 142 Assembler::FlushICache(isolate, code->instruction_start(),
138 for (size_t i = 0; i < function_code().size(); i++) { 143 code->instruction_size());
139 LinkFunction(function_code()[i]);
140 }
141 if (!function_table.is_null()) {
142 int table_size = static_cast<int>(functions.size());
143 DCHECK_EQ(function_table->length(), table_size * 2);
144 for (int i = 0; i < table_size; i++) {
145 function_table->set(i + table_size, *function_code()[functions[i]]);
146 } 144 }
147 } 145 }
148 } 146 }
149 147
150 private: 148 static void LinkImports(Isolate* isolate,
151 std::vector<Handle<Code>>& function_code() { return *function_code_; } 149 std::vector<Handle<Code>>& functions,
150 const std::vector<Handle<Code>>& imports) {
151 for (uint32_t i = 0; i < functions.size(); ++i) {
152 Handle<Code> code = functions[i];
153 bool modified =
154 WasmLinker::LinkFunction(code, imports, Code::WASM_TO_JS_FUNCTION);
155 if (modified) {
156 Assembler::FlushICache(isolate, code->instruction_start(),
157 code->instruction_size());
158 }
159 }
160 }
152 161
153 void CreatePlaceholder(uint32_t index) { 162 static Handle<Code> CreatePlaceholder(Factory* factory, uint32_t index,
154 DCHECK(index < function_code().size()); 163 Code::Kind kind) {
155 DCHECK(function_code()[index].is_null());
156 // Create a placeholder code object and encode the corresponding index in 164 // Create a placeholder code object and encode the corresponding index in
157 // the {constant_pool_offset} field of the code object. 165 // the {constant_pool_offset} field of the code object.
158 // TODO(titzer): placeholder code objects are somewhat dangerous. 166 // TODO(titzer): placeholder code objects are somewhat dangerous.
159 byte buffer[] = {0, 0, 0, 0, 0, 0, 0, 0}; // fake instructions. 167 static byte buffer[] = {0, 0, 0, 0, 0, 0, 0, 0}; // fake instructions.
160 CodeDesc desc = {buffer, 8, 8, 0, 0, nullptr}; 168 static CodeDesc desc = {buffer, 8, 8, 0, 0, nullptr};
161 Handle<Code> code = isolate_->factory()->NewCode( 169 Handle<Code> code = factory->NewCode(desc, Code::KindField::encode(kind),
162 desc, Code::KindField::encode(Code::WASM_FUNCTION), 170 Handle<Object>::null());
163 Handle<Object>::null());
164 code->set_constant_pool_offset(static_cast<int>(index) + 171 code->set_constant_pool_offset(static_cast<int>(index) +
165 kPlaceholderMarker); 172 kPlaceholderMarker);
166 placeholder_code_[index] = code; 173 return code;
167 function_code()[index] = code;
168 } 174 }
169 175
176 private:
170 Isolate* isolate_; 177 Isolate* isolate_;
171 std::vector<Handle<Code>> placeholder_code_; 178 std::vector<Handle<Code>> placeholder_code_;
172 std::vector<Handle<Code>>* function_code_;
173 179
174 void LinkFunction(Handle<Code> code) { 180 static bool LinkFunction(Handle<Code> unlinked,
181 const std::vector<Handle<Code>>& to_link,
titzer 2016/06/16 23:12:14 What does to_link mean? Is it the finished code ob
Mircea Trofin 2016/06/16 23:43:11 It's functions referenced from unlinked. When we l
titzer 2016/06/17 00:23:42 how about code_targets as a name?
Mircea Trofin 2016/06/17 00:39:30 That's a good name, I'll rename. Thanks!
182 Code::Kind kind) {
175 bool modified = false; 183 bool modified = false;
176 int mode_mask = RelocInfo::kCodeTargetMask; 184 int mode_mask = RelocInfo::kCodeTargetMask;
177 AllowDeferredHandleDereference embedding_raw_address; 185 AllowDeferredHandleDereference embedding_raw_address;
178 for (RelocIterator it(*code, mode_mask); !it.done(); it.next()) { 186 for (RelocIterator it(*unlinked, mode_mask); !it.done(); it.next()) {
179 RelocInfo::Mode mode = it.rinfo()->rmode(); 187 RelocInfo::Mode mode = it.rinfo()->rmode();
180 if (RelocInfo::IsCodeTarget(mode)) { 188 if (RelocInfo::IsCodeTarget(mode)) {
181 Code* target = 189 Code* target =
182 Code::GetCodeFromTargetAddress(it.rinfo()->target_address()); 190 Code::GetCodeFromTargetAddress(it.rinfo()->target_address());
183 if (target->kind() == Code::WASM_FUNCTION && 191 if (target->kind() == kind &&
184 target->constant_pool_offset() >= kPlaceholderMarker) { 192 target->constant_pool_offset() >= kPlaceholderMarker) {
185 // Patch direct calls to placeholder code objects. 193 // Patch direct calls to placeholder code objects.
186 uint32_t index = target->constant_pool_offset() - kPlaceholderMarker; 194 uint32_t index = target->constant_pool_offset() - kPlaceholderMarker;
187 CHECK(index < function_code().size()); 195 CHECK(index < to_link.size());
188 Handle<Code> new_target = function_code()[index]; 196 Handle<Code> new_target = to_link[index];
189 if (target != *new_target) { 197 if (target != *new_target) {
190 CHECK_EQ(*placeholder_code_[index], target);
191 it.rinfo()->set_target_address(new_target->instruction_start(), 198 it.rinfo()->set_target_address(new_target->instruction_start(),
192 SKIP_WRITE_BARRIER, 199 SKIP_WRITE_BARRIER,
193 SKIP_ICACHE_FLUSH); 200 SKIP_ICACHE_FLUSH);
194 modified = true; 201 modified = true;
195 } 202 }
196 } 203 }
197 } 204 }
198 } 205 }
199 if (modified) { 206 return modified;
200 Assembler::FlushICache(isolate_, code->instruction_start(),
201 code->instruction_size());
202 }
203 } 207 }
204 }; 208 };
205 209
206 namespace { 210 namespace {
207 // Internal constants for the layout of the module object. 211 // Internal constants for the layout of the module object.
208 const int kWasmModuleInternalFieldCount = 5; 212 const int kWasmModuleInternalFieldCount = 5;
209 const int kWasmModuleFunctionTable = 0; 213 const int kWasmModuleFunctionTable = 0;
210 const int kWasmModuleCodeTable = 1; 214 const int kWasmModuleCodeTable = 1;
211 const int kWasmMemArrayBuffer = 2; 215 const int kWasmMemArrayBuffer = 2;
212 const int kWasmGlobalsArrayBuffer = 3; 216 const int kWasmGlobalsArrayBuffer = 3;
213 const int kWasmFunctionNamesArray = 4; 217 const int kWasmFunctionNamesArray = 4;
214 218
219 uint32_t GetMinModuleMemSize(const WasmModule* module) {
220 return WasmModule::kPageSize * module->min_mem_pages;
221 }
222
215 void LoadDataSegments(const WasmModule* module, byte* mem_addr, 223 void LoadDataSegments(const WasmModule* module, byte* mem_addr,
216 size_t mem_size) { 224 size_t mem_size) {
217 for (const WasmDataSegment& segment : module->data_segments) { 225 for (const WasmDataSegment& segment : module->data_segments) {
218 if (!segment.init) continue; 226 if (!segment.init) continue;
219 if (!segment.source_size) continue; 227 if (!segment.source_size) continue;
220 CHECK_LT(segment.dest_addr, mem_size); 228 CHECK_LT(segment.dest_addr, mem_size);
221 CHECK_LE(segment.source_size, mem_size); 229 CHECK_LE(segment.source_size, mem_size);
222 CHECK_LE(segment.dest_addr + segment.source_size, mem_size); 230 CHECK_LE(segment.dest_addr + segment.source_size, mem_size);
223 byte* addr = mem_addr + segment.dest_addr; 231 byte* addr = mem_addr + segment.dest_addr;
224 memcpy(addr, module->module_start + segment.source_offset, 232 memcpy(addr, module->module_start + segment.source_offset,
(...skipping 11 matching lines...) Expand all
236 for (int i = 0; i < table_size; i++) { 244 for (int i = 0; i < table_size; i++) {
237 const WasmFunction* function = 245 const WasmFunction* function =
238 &module->functions[module->function_table[i]]; 246 &module->functions[module->function_table[i]];
239 fixed->set(i, Smi::FromInt(function->sig_index)); 247 fixed->set(i, Smi::FromInt(function->sig_index));
240 } 248 }
241 return fixed; 249 return fixed;
242 } 250 }
243 251
244 Handle<JSArrayBuffer> NewArrayBuffer(Isolate* isolate, size_t size, 252 Handle<JSArrayBuffer> NewArrayBuffer(Isolate* isolate, size_t size,
245 byte** backing_store) { 253 byte** backing_store) {
254 *backing_store = nullptr;
246 if (size > (WasmModule::kMaxMemPages * WasmModule::kPageSize)) { 255 if (size > (WasmModule::kMaxMemPages * WasmModule::kPageSize)) {
247 // TODO(titzer): lift restriction on maximum memory allocated here. 256 // TODO(titzer): lift restriction on maximum memory allocated here.
248 *backing_store = nullptr;
249 return Handle<JSArrayBuffer>::null(); 257 return Handle<JSArrayBuffer>::null();
250 } 258 }
251 void* memory = 259 void* memory = isolate->array_buffer_allocator()->Allocate(size);
252 isolate->array_buffer_allocator()->Allocate(static_cast<int>(size)); 260 if (memory == nullptr) {
253 if (!memory) {
254 *backing_store = nullptr;
255 return Handle<JSArrayBuffer>::null(); 261 return Handle<JSArrayBuffer>::null();
256 } 262 }
257 263
258 *backing_store = reinterpret_cast<byte*>(memory); 264 *backing_store = reinterpret_cast<byte*>(memory);
259 265
260 #if DEBUG 266 #if DEBUG
261 // Double check the API allocator actually zero-initialized the memory. 267 // Double check the API allocator actually zero-initialized the memory.
262 byte* bytes = reinterpret_cast<byte*>(*backing_store); 268 byte* bytes = reinterpret_cast<byte*>(*backing_store);
263 for (size_t i = 0; i < size; i++) { 269 for (size_t i = 0; i < size; i++) {
264 DCHECK_EQ(0, bytes[i]); 270 DCHECK_EQ(0, bytes[i]);
265 } 271 }
266 #endif 272 #endif
267 273
268 Handle<JSArrayBuffer> buffer = isolate->factory()->NewJSArrayBuffer(); 274 Handle<JSArrayBuffer> buffer = isolate->factory()->NewJSArrayBuffer();
269 JSArrayBuffer::Setup(buffer, isolate, false, memory, static_cast<int>(size)); 275 JSArrayBuffer::Setup(buffer, isolate, false, memory, static_cast<int>(size));
270 buffer->set_is_neuterable(false); 276 buffer->set_is_neuterable(false);
271 return buffer; 277 return buffer;
272 } 278 }
273 279
280 void RelocateInstanceCode(WasmModuleInstance* instance) {
281 for (uint32_t i = 0; i < instance->function_code.size(); ++i) {
282 Handle<Code> function = instance->function_code[i];
283 AllowDeferredHandleDereference embedding_raw_address;
284 int mask = 1 << RelocInfo::WASM_MEMORY_REFERENCE |
285 1 << RelocInfo::WASM_MEMORY_SIZE_REFERENCE;
286 for (RelocIterator it(*function, mask); !it.done(); it.next()) {
287 it.rinfo()->update_wasm_memory_reference(
288 nullptr, instance->mem_start, GetMinModuleMemSize(instance->module),
289 static_cast<uint32_t>(instance->mem_size));
290 }
291 }
292 }
293
274 // Set the memory for a module instance to be the {memory} array buffer. 294 // Set the memory for a module instance to be the {memory} array buffer.
275 void SetMemory(WasmModuleInstance* instance, Handle<JSArrayBuffer> memory) { 295 void SetMemory(WasmModuleInstance* instance, Handle<JSArrayBuffer> memory) {
276 memory->set_is_neuterable(false); 296 memory->set_is_neuterable(false);
277 instance->mem_start = reinterpret_cast<byte*>(memory->backing_store()); 297 instance->mem_start = reinterpret_cast<byte*>(memory->backing_store());
278 instance->mem_size = memory->byte_length()->Number(); 298 instance->mem_size = memory->byte_length()->Number();
279 instance->mem_buffer = memory; 299 instance->mem_buffer = memory;
300 RelocateInstanceCode(instance);
280 } 301 }
281 302
282 // Allocate memory for a module instance as a new JSArrayBuffer. 303 // Allocate memory for a module instance as a new JSArrayBuffer.
283 bool AllocateMemory(ErrorThrower* thrower, Isolate* isolate, 304 bool AllocateMemory(ErrorThrower* thrower, Isolate* isolate,
284 WasmModuleInstance* instance) { 305 WasmModuleInstance* instance) {
285 DCHECK(instance->module); 306 DCHECK(instance->module);
286 DCHECK(instance->mem_buffer.is_null()); 307 DCHECK(instance->mem_buffer.is_null());
287 308
288 if (instance->module->min_mem_pages > WasmModule::kMaxMemPages) { 309 if (instance->module->min_mem_pages > WasmModule::kMaxMemPages) {
289 thrower->Error("Out of memory: wasm memory too large"); 310 thrower->Error("Out of memory: wasm memory too large");
290 return false; 311 return false;
291 } 312 }
292 instance->mem_size = WasmModule::kPageSize * instance->module->min_mem_pages; 313 instance->mem_size = GetMinModuleMemSize(instance->module);
293 instance->mem_buffer = 314 instance->mem_buffer =
294 NewArrayBuffer(isolate, instance->mem_size, &instance->mem_start); 315 NewArrayBuffer(isolate, instance->mem_size, &instance->mem_start);
295 if (!instance->mem_start) { 316 if (instance->mem_start == nullptr) {
296 thrower->Error("Out of memory: wasm memory"); 317 thrower->Error("Out of memory: wasm memory");
297 instance->mem_size = 0; 318 instance->mem_size = 0;
298 return false; 319 return false;
299 } 320 }
321 RelocateInstanceCode(instance);
300 return true; 322 return true;
301 } 323 }
302 324
303 bool AllocateGlobals(ErrorThrower* thrower, Isolate* isolate, 325 bool AllocateGlobals(ErrorThrower* thrower, Isolate* isolate,
304 WasmModuleInstance* instance) { 326 WasmModuleInstance* instance) {
305 uint32_t globals_size = instance->module->globals_size; 327 uint32_t globals_size = instance->module->globals_size;
306 if (globals_size > 0) { 328 if (globals_size > 0) {
307 instance->globals_buffer = 329 instance->globals_buffer =
308 NewArrayBuffer(isolate, globals_size, &instance->globals_start); 330 NewArrayBuffer(isolate, globals_size, &instance->globals_start);
309 if (!instance->globals_start) { 331 if (!instance->globals_start) {
310 // Not enough space for backing store of globals. 332 // Not enough space for backing store of globals.
311 thrower->Error("Out of memory: wasm globals"); 333 thrower->Error("Out of memory: wasm globals");
312 return false; 334 return false;
313 } 335 }
336
337 for (uint32_t i = 0; i < instance->function_code.size(); ++i) {
338 Handle<Code> function = instance->function_code[i];
339 AllowDeferredHandleDereference embedding_raw_address;
340 int mask = 1 << RelocInfo::WASM_GLOBAL_REFERENCE;
341 for (RelocIterator it(*function, mask); !it.done(); it.next()) {
342 it.rinfo()->update_wasm_global_reference(nullptr,
343 instance->globals_start);
344 }
345 }
314 } 346 }
315 return true; 347 return true;
316 } 348 }
317 } // namespace 349 } // namespace
318 350
319 WasmModule::WasmModule() 351 WasmModule::WasmModule()
320 : module_start(nullptr), 352 : module_start(nullptr),
321 module_end(nullptr), 353 module_end(nullptr),
322 min_mem_pages(0), 354 min_mem_pages(0),
323 max_mem_pages(0), 355 max_mem_pages(0),
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after
469 PrintF("Total generated wasm code: %zu bytes\n", code_size); 501 PrintF("Total generated wasm code: %zu bytes\n", code_size);
470 PrintF("Total generated wasm reloc: %zu bytes\n", reloc_size); 502 PrintF("Total generated wasm reloc: %zu bytes\n", reloc_size);
471 } 503 }
472 } 504 }
473 }; 505 };
474 506
475 bool CompileWrappersToImportedFunctions( 507 bool CompileWrappersToImportedFunctions(
476 Isolate* isolate, const WasmModule* module, const Handle<JSReceiver> ffi, 508 Isolate* isolate, const WasmModule* module, const Handle<JSReceiver> ffi,
477 WasmModuleInstance* instance, ErrorThrower* thrower, Factory* factory, 509 WasmModuleInstance* instance, ErrorThrower* thrower, Factory* factory,
478 ModuleEnv* module_env, CodeStats& code_stats) { 510 ModuleEnv* module_env, CodeStats& code_stats) {
479 uint32_t index = 0;
480 if (module->import_table.size() > 0) { 511 if (module->import_table.size() > 0) {
481 instance->import_code.reserve(module->import_table.size()); 512 instance->import_code.reserve(module->import_table.size());
482 for (const WasmImport& import : module->import_table) { 513 for (uint32_t index = 0; index < module->import_table.size(); ++index) {
514 const WasmImport& import = module->import_table[index];
483 WasmName module_name = module->GetNameOrNull(import.module_name_offset, 515 WasmName module_name = module->GetNameOrNull(import.module_name_offset,
484 import.module_name_length); 516 import.module_name_length);
485 WasmName function_name = module->GetNameOrNull( 517 WasmName function_name = module->GetNameOrNull(
486 import.function_name_offset, import.function_name_length); 518 import.function_name_offset, import.function_name_length);
487 MaybeHandle<JSFunction> function = LookupFunction( 519 MaybeHandle<JSFunction> function = LookupFunction(
488 *thrower, factory, ffi, index, module_name, function_name); 520 *thrower, factory, ffi, index, module_name, function_name);
489 if (function.is_null()) return false; 521 if (function.is_null()) return false;
490 522
491 Handle<Code> code = compiler::CompileWasmToJSWrapper( 523 Handle<Code> code = compiler::CompileWasmToJSWrapper(
492 isolate, module_env, function.ToHandleChecked(), import.sig, 524 isolate, module_env, function.ToHandleChecked(), import.sig,
493 module_name, function_name); 525 module_name, function_name);
494 instance->import_code.push_back(code); 526 instance->import_code[index] = code;
495 code_stats.Record(*code); 527 code_stats.Record(*code);
496 index++;
497 } 528 }
498 } 529 }
499 return true; 530 return true;
500 } 531 }
501 532
502 void InitializeParallelCompilation( 533 void InitializeParallelCompilation(
503 Isolate* isolate, const std::vector<WasmFunction>& functions, 534 Isolate* isolate, const std::vector<WasmFunction>& functions,
504 std::vector<compiler::WasmCompilationUnit*>& compilation_units, 535 std::vector<compiler::WasmCompilationUnit*>& compilation_units,
505 ModuleEnv& module_env, ErrorThrower& thrower) { 536 ModuleEnv& module_env, ErrorThrower& thrower) {
506 for (uint32_t i = FLAG_skip_compiling_wasm_funcs; i < functions.size(); i++) { 537 for (uint32_t i = FLAG_skip_compiling_wasm_funcs; i < functions.size(); i++) {
507 compilation_units[i] = new compiler::WasmCompilationUnit( 538 compilation_units[i] = new compiler::WasmCompilationUnit(
508 &thrower, isolate, &module_env, &functions[i], i); 539 &thrower, isolate, &module_env, &functions[i], i);
509 } 540 }
541 module_env.linker->InitializePlaceholders();
510 } 542 }
511 543
512 uint32_t* StartCompilationTasks( 544 uint32_t* StartCompilationTasks(
513 Isolate* isolate, 545 Isolate* isolate,
514 std::vector<compiler::WasmCompilationUnit*>& compilation_units, 546 std::vector<compiler::WasmCompilationUnit*>& compilation_units,
515 std::queue<compiler::WasmCompilationUnit*>& executed_units, 547 std::queue<compiler::WasmCompilationUnit*>& executed_units,
516 const base::SmartPointer<base::Semaphore>& pending_tasks, 548 const base::SmartPointer<base::Semaphore>& pending_tasks,
517 base::Mutex& result_mutex, base::AtomicNumber<size_t>& next_unit) { 549 base::Mutex& result_mutex, base::AtomicNumber<size_t>& next_unit) {
518 const size_t num_tasks = 550 const size_t num_tasks =
519 Min(static_cast<size_t>(FLAG_wasm_num_compilation_tasks), 551 Min(static_cast<size_t>(FLAG_wasm_num_compilation_tasks),
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after
646 thrower, isolate, module_env, &func); 678 thrower, isolate, module_env, &func);
647 if (code.is_null()) { 679 if (code.is_null()) {
648 thrower->Error("Compilation of #%d:%.*s failed.", i, str.length(), 680 thrower->Error("Compilation of #%d:%.*s failed.", i, str.length(),
649 str.start()); 681 str.start());
650 break; 682 break;
651 } 683 }
652 // Install the code into the linker table. 684 // Install the code into the linker table.
653 functions[i] = code; 685 functions[i] = code;
654 } 686 }
655 } 687 }
688
689 void PopulateFunctionTable(WasmModuleInstance* instance) {
690 if (!instance->function_table.is_null()) {
691 int table_size = static_cast<int>(instance->module->function_table.size());
692 DCHECK_EQ(instance->function_table->length(), table_size * 2);
693 for (int i = 0; i < table_size; i++) {
694 instance->function_table->set(
695 i + table_size,
696 *instance->function_code[instance->module->function_table[i]]);
697 }
698 }
699 }
656 } // namespace 700 } // namespace
657 701
658 void SetDeoptimizationData(Factory* factory, Handle<JSObject> js_object, 702 void SetDeoptimizationData(Factory* factory, Handle<JSObject> js_object,
659 std::vector<Handle<Code>>& functions) { 703 std::vector<Handle<Code>>& functions) {
660 for (size_t i = FLAG_skip_compiling_wasm_funcs; i < functions.size(); ++i) { 704 for (size_t i = FLAG_skip_compiling_wasm_funcs; i < functions.size(); ++i) {
661 Handle<Code> code = functions[i]; 705 Handle<Code> code = functions[i];
662 DCHECK(code->deoptimization_data() == nullptr || 706 DCHECK(code->deoptimization_data() == nullptr ||
663 code->deoptimization_data()->length() == 0); 707 code->deoptimization_data()->length() == 0);
664 Handle<FixedArray> deopt_data = factory->NewFixedArray(2, TENURED); 708 Handle<FixedArray> deopt_data = factory->NewFixedArray(2, TENURED);
665 if (!js_object.is_null()) { 709 if (!js_object.is_null()) {
666 deopt_data->set(0, *js_object); 710 deopt_data->set(0, *js_object);
667 } 711 }
668 deopt_data->set(1, Smi::FromInt(static_cast<int>(i))); 712 deopt_data->set(1, Smi::FromInt(static_cast<int>(i)));
669 deopt_data->set_length(2); 713 deopt_data->set_length(2);
670 code->set_deoptimization_data(*deopt_data); 714 code->set_deoptimization_data(*deopt_data);
671 } 715 }
672 } 716 }
673 717
718 Handle<FixedArray> WasmModule::Compile(Isolate* isolate) const {
719 Factory* factory = isolate->factory();
720 ErrorThrower thrower(isolate, "WasmModule::Compile()");
721 CodeStats code_stats;
722
723 WasmModuleInstance temp_instance_for_compilation(this);
724 temp_instance_for_compilation.function_table =
725 BuildFunctionTable(isolate, this);
726 temp_instance_for_compilation.context = isolate->native_context();
727 temp_instance_for_compilation.mem_size = GetMinModuleMemSize(this);
728 temp_instance_for_compilation.mem_start = nullptr;
729 temp_instance_for_compilation.globals_start = nullptr;
730
731 WasmLinker linker(isolate,
732 static_cast<uint32_t>(
733 temp_instance_for_compilation.function_code.size()));
734 ModuleEnv module_env;
735 module_env.module = this;
736 module_env.linker = &linker;
737 module_env.instance = &temp_instance_for_compilation;
738 module_env.origin = origin;
739
740 Handle<FixedArray> ret =
741 factory->NewFixedArray(static_cast<int>(functions.size()), TENURED);
742
743 temp_instance_for_compilation.import_code.resize(import_table.size());
744 for (uint32_t i = 0; i < import_table.size(); ++i) {
745 temp_instance_for_compilation.import_code[i] =
746 WasmLinker::CreatePlaceholder(factory, i, Code::WASM_TO_JS_FUNCTION);
747 }
748 isolate->counters()->wasm_functions_per_module()->AddSample(
749 static_cast<int>(functions.size()));
750 if (FLAG_wasm_num_compilation_tasks != 0) {
751 CompileInParallel(isolate, this,
752 temp_instance_for_compilation.function_code, &thrower,
753 &module_env);
754 } else {
755 CompileSequentially(isolate, this,
756 temp_instance_for_compilation.function_code, &thrower,
757 &module_env);
758 }
759 if (thrower.error()) {
760 return Handle<FixedArray>::null();
761 }
762
763 WasmLinker::LinkModuleFunctions(isolate,
titzer 2016/06/16 23:12:14 Why not make this an instance method, since there
Mircea Trofin 2016/06/16 23:43:11 There doesn't seem to be anything the wasm linker
titzer 2016/06/17 00:23:42 That sounds good, but until then can we simply mov
Mircea Trofin 2016/06/17 00:39:30 You mean move the statics out as stand-alone funct
764 temp_instance_for_compilation.function_code);
765
766 // At this point, compilation has completed. Update the code table
767 // and record sizes.
768 for (size_t i = FLAG_skip_compiling_wasm_funcs;
769 i < temp_instance_for_compilation.function_code.size(); ++i) {
770 Code* code = *temp_instance_for_compilation.function_code[i];
771 ret->set(static_cast<int>(i), code);
772 code_stats.Record(code);
773 }
774
775 PopulateFunctionTable(&temp_instance_for_compilation);
776
777 return ret;
778 }
779
674 // Instantiates a wasm module as a JSObject. 780 // Instantiates a wasm module as a JSObject.
675 // * allocates a backing store of {mem_size} bytes. 781 // * allocates a backing store of {mem_size} bytes.
676 // * installs a named property "memory" for that buffer if exported 782 // * installs a named property "memory" for that buffer if exported
677 // * installs named properties on the object for exported functions 783 // * installs named properties on the object for exported functions
678 // * compiles wasm code to machine code 784 // * compiles wasm code to machine code
679 MaybeHandle<JSObject> WasmModule::Instantiate( 785 MaybeHandle<JSObject> WasmModule::Instantiate(
680 Isolate* isolate, Handle<JSReceiver> ffi, 786 Isolate* isolate, Handle<JSReceiver> ffi,
681 Handle<JSArrayBuffer> memory) const { 787 Handle<JSArrayBuffer> memory) const {
682 HistogramTimerScope wasm_instantiate_module_time_scope( 788 HistogramTimerScope wasm_instantiate_module_time_scope(
683 isolate->counters()->wasm_instantiate_module_time()); 789 isolate->counters()->wasm_instantiate_module_time());
684 ErrorThrower thrower(isolate, "WasmModule::Instantiate()"); 790 ErrorThrower thrower(isolate, "WasmModule::Instantiate()");
685 Factory* factory = isolate->factory(); 791 Factory* factory = isolate->factory();
686 792
687 // If FLAG_print_wasm_code_size is set, this aggregates the sum of all code 793 // If FLAG_print_wasm_code_size is set, this aggregates the sum of all code
688 // objects created for this module. 794 // objects created for this module.
689 // TODO(titzer): switch this to TRACE_EVENT 795 // TODO(titzer): switch this to TRACE_EVENT
690 CodeStats code_stats; 796 CodeStats code_stats;
691 797
692 //------------------------------------------------------------------------- 798 //-------------------------------------------------------------------------
693 // Allocate the instance and its JS counterpart. 799 // Allocate the instance and its JS counterpart.
694 //------------------------------------------------------------------------- 800 //-------------------------------------------------------------------------
695 Handle<Map> map = factory->NewMap( 801 Handle<Map> map = factory->NewMap(
696 JS_OBJECT_TYPE, 802 JS_OBJECT_TYPE,
697 JSObject::kHeaderSize + kWasmModuleInternalFieldCount * kPointerSize); 803 JSObject::kHeaderSize + kWasmModuleInternalFieldCount * kPointerSize);
698 WasmModuleInstance instance(this); 804 WasmModuleInstance instance(this);
699 instance.context = isolate->native_context(); 805 instance.context = isolate->native_context();
700 instance.js_object = factory->NewJSObjectFromMap(map, TENURED); 806 instance.js_object = factory->NewJSObjectFromMap(map, TENURED);
701 Handle<FixedArray> code_table = 807
702 factory->NewFixedArray(static_cast<int>(functions.size()), TENURED); 808 Handle<FixedArray> code_table = Compile(isolate);
809 if (code_table.is_null()) return Handle<JSObject>::null();
810
703 instance.js_object->SetInternalField(kWasmModuleCodeTable, *code_table); 811 instance.js_object->SetInternalField(kWasmModuleCodeTable, *code_table);
704 812
813 for (uint32_t i = 0; i < functions.size(); ++i) {
814 Handle<Code> code = Handle<Code>(Code::cast(code_table->get(i)));
815 instance.function_code[i] = code;
816 }
817
705 //------------------------------------------------------------------------- 818 //-------------------------------------------------------------------------
706 // Allocate and initialize the linear memory. 819 // Allocate and initialize the linear memory.
707 //------------------------------------------------------------------------- 820 //-------------------------------------------------------------------------
708 isolate->counters()->wasm_min_mem_pages_count()->AddSample( 821 isolate->counters()->wasm_min_mem_pages_count()->AddSample(
709 instance.module->min_mem_pages); 822 instance.module->min_mem_pages);
710 isolate->counters()->wasm_max_mem_pages_count()->AddSample( 823 isolate->counters()->wasm_max_mem_pages_count()->AddSample(
711 instance.module->max_mem_pages); 824 instance.module->max_mem_pages);
712 if (memory.is_null()) { 825 if (memory.is_null()) {
713 if (!AllocateMemory(&thrower, isolate, &instance)) { 826 if (!AllocateMemory(&thrower, isolate, &instance)) {
714 return MaybeHandle<JSObject>(); 827 return MaybeHandle<JSObject>();
(...skipping 12 matching lines...) Expand all
727 return MaybeHandle<JSObject>(); 840 return MaybeHandle<JSObject>();
728 } 841 }
729 if (!instance.globals_buffer.is_null()) { 842 if (!instance.globals_buffer.is_null()) {
730 instance.js_object->SetInternalField(kWasmGlobalsArrayBuffer, 843 instance.js_object->SetInternalField(kWasmGlobalsArrayBuffer,
731 *instance.globals_buffer); 844 *instance.globals_buffer);
732 } 845 }
733 846
734 HistogramTimerScope wasm_compile_module_time_scope( 847 HistogramTimerScope wasm_compile_module_time_scope(
735 isolate->counters()->wasm_compile_module_time()); 848 isolate->counters()->wasm_compile_module_time());
736 849
737 instance.function_table = BuildFunctionTable(isolate, this);
738 WasmLinker linker(isolate, &instance.function_code);
739 ModuleEnv module_env; 850 ModuleEnv module_env;
740 module_env.module = this; 851 module_env.module = this;
741 module_env.instance = &instance; 852 module_env.instance = &instance;
742 module_env.linker = &linker;
743 module_env.origin = origin; 853 module_env.origin = origin;
744 854
745 //------------------------------------------------------------------------- 855 //-------------------------------------------------------------------------
746 // Compile wrappers to imported functions. 856 // Compile wrappers to imported functions.
747 //------------------------------------------------------------------------- 857 //-------------------------------------------------------------------------
748 if (!CompileWrappersToImportedFunctions(isolate, this, ffi, &instance, 858 if (!CompileWrappersToImportedFunctions(isolate, this, ffi, &instance,
749 &thrower, factory, &module_env, 859 &thrower, factory, &module_env,
750 code_stats)) { 860 code_stats)) {
751 return MaybeHandle<JSObject>(); 861 return MaybeHandle<JSObject>();
752 } 862 }
753 //-------------------------------------------------------------------------
754 // Compile all functions in the module.
755 //-------------------------------------------------------------------------
756 { 863 {
757 isolate->counters()->wasm_functions_per_module()->AddSample(
758 static_cast<int>(functions.size()));
759 if (FLAG_wasm_num_compilation_tasks != 0) {
760 CompileInParallel(isolate, this, instance.function_code, &thrower,
761 &module_env);
762 } else {
763 // 5) The main thread finishes the compilation.
764 CompileSequentially(isolate, this, instance.function_code, &thrower,
765 &module_env);
766 }
767 if (thrower.error()) {
768 return Handle<JSObject>::null();
769 }
770
771 // At this point, compilation has completed. Update the code table
772 // and record sizes.
773 for (size_t i = FLAG_skip_compiling_wasm_funcs;
774 i < instance.function_code.size(); ++i) {
775 Code* code = *instance.function_code[i];
776 code_table->set(static_cast<int>(i), code);
777 code_stats.Record(code);
778 }
779
780 // Patch all direct call sites.
781 linker.Link(instance.function_table, this->function_table);
782 instance.js_object->SetInternalField(kWasmModuleFunctionTable, 864 instance.js_object->SetInternalField(kWasmModuleFunctionTable,
783 Smi::FromInt(0)); 865 Smi::FromInt(0));
866 WasmLinker::LinkImports(isolate, instance.function_code,
867 instance.import_code);
784 868
785 SetDeoptimizationData(factory, instance.js_object, instance.function_code); 869 SetDeoptimizationData(factory, instance.js_object, instance.function_code);
786 870
787 //------------------------------------------------------------------------- 871 //-------------------------------------------------------------------------
788 // Create and populate the exports object. 872 // Create and populate the exports object.
789 //------------------------------------------------------------------------- 873 //-------------------------------------------------------------------------
790 if (export_table.size() > 0 || mem_export) { 874 if (export_table.size() > 0 || mem_export) {
791 Handle<JSObject> exports_object; 875 Handle<JSObject> exports_object;
792 if (origin == kWasmOrigin) { 876 if (origin == kWasmOrigin) {
793 // Create the "exports" object. 877 // Create the "exports" object.
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after
907 } 991 }
908 992
909 int32_t retval = CompileAndRunWasmModule(isolate, result.val); 993 int32_t retval = CompileAndRunWasmModule(isolate, result.val);
910 delete result.val; 994 delete result.val;
911 return retval; 995 return retval;
912 } 996 }
913 997
914 int32_t CompileAndRunWasmModule(Isolate* isolate, const WasmModule* module) { 998 int32_t CompileAndRunWasmModule(Isolate* isolate, const WasmModule* module) {
915 ErrorThrower thrower(isolate, "CompileAndRunWasmModule"); 999 ErrorThrower thrower(isolate, "CompileAndRunWasmModule");
916 WasmModuleInstance instance(module); 1000 WasmModuleInstance instance(module);
1001 Handle<FixedArray> code_table = module->Compile(isolate);
1002
1003 if (code_table.is_null()) return -1;
1004
1005 for (uint32_t i = 0; i < module->functions.size(); ++i) {
1006 Handle<Code> code = Handle<Code>(Code::cast(code_table->get(i)));
1007 instance.function_code[i] = code;
1008 }
917 1009
918 // Allocate and initialize the linear memory. 1010 // Allocate and initialize the linear memory.
919 if (!AllocateMemory(&thrower, isolate, &instance)) { 1011 if (!AllocateMemory(&thrower, isolate, &instance)) {
920 return -1; 1012 return -1;
921 } 1013 }
922 LoadDataSegments(module, instance.mem_start, instance.mem_size); 1014 LoadDataSegments(module, instance.mem_start, instance.mem_size);
923 1015
924 // Allocate the globals area if necessary. 1016 // Allocate the globals area if necessary.
925 if (!AllocateGlobals(&thrower, isolate, &instance)) { 1017 if (!AllocateGlobals(&thrower, isolate, &instance)) {
926 return -1; 1018 return -1;
927 } 1019 }
928 1020
929 // Build the function table.
930 instance.function_table = BuildFunctionTable(isolate, module);
931
932 // Create module environment. 1021 // Create module environment.
933 WasmLinker linker(isolate, &instance.function_code); 1022 WasmLinker linker(isolate,
1023 static_cast<uint32_t>(instance.function_code.size()));
934 ModuleEnv module_env; 1024 ModuleEnv module_env;
935 module_env.module = module; 1025 module_env.module = module;
936 module_env.instance = &instance; 1026 module_env.instance = &instance;
937 module_env.linker = &linker; 1027 module_env.linker = &linker;
938 module_env.origin = module->origin; 1028 module_env.origin = module->origin;
939 1029
940 if (module->export_table.size() == 0) { 1030 if (module->export_table.size() == 0) {
941 thrower.Error("WASM.compileRun() failed: no exported functions"); 1031 thrower.Error("WASM.compileRun() failed: no exported functions");
942 return -2; 1032 return -2;
943 } 1033 }
944 1034
945 // Compile all functions. 1035 // Compile all functions.
946 for (const WasmFunction& func : module->functions) { 1036 for (const WasmFunction& func : module->functions) {
947 // Compile the function and install it in the linker. 1037 // Compile the function and install it in the linker.
948 Handle<Code> code = compiler::WasmCompilationUnit::CompileWasmFunction( 1038 Handle<Code> code = compiler::WasmCompilationUnit::CompileWasmFunction(
949 &thrower, isolate, &module_env, &func); 1039 &thrower, isolate, &module_env, &func);
950 if (!code.is_null()) linker.Finish(func.func_index, code); 1040 if (!code.is_null()) instance.function_code[func.func_index] = code;
951 if (thrower.error()) return -1; 1041 if (thrower.error()) return -1;
952 } 1042 }
953 1043
954 linker.Link(instance.function_table, instance.module->function_table); 1044 WasmLinker::LinkModuleFunctions(isolate, instance.function_code);
955 1045
956 // Wrap the main code so it can be called as a JS function. 1046 // Wrap the main code so it can be called as a JS function.
957 uint32_t main_index = module->export_table.back().func_index; 1047 uint32_t main_index = module->export_table.back().func_index;
958 Handle<Code> main_code = instance.function_code[main_index]; 1048 Handle<Code> main_code = instance.function_code[main_index];
959 Handle<String> name = isolate->factory()->NewStringFromStaticChars("main"); 1049 Handle<String> name = isolate->factory()->NewStringFromStaticChars("main");
960 Handle<JSObject> module_object = Handle<JSObject>(0, isolate); 1050 Handle<JSObject> module_object = Handle<JSObject>(0, isolate);
961 Handle<JSFunction> jsfunc = compiler::CompileJSToWasmWrapper( 1051 Handle<JSFunction> jsfunc = compiler::CompileJSToWasmWrapper(
962 isolate, &module_env, name, main_code, module_object, main_index); 1052 isolate, &module_env, name, main_code, module_object, main_index);
963 1053
964 // Call the JS function. 1054 // Call the JS function.
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
1014 // TODO(clemensh): Check wasm byte header once we store a copy of the bytes. 1104 // TODO(clemensh): Check wasm byte header once we store a copy of the bytes.
1015 return object->GetInternalFieldCount() == kWasmModuleInternalFieldCount && 1105 return object->GetInternalFieldCount() == kWasmModuleInternalFieldCount &&
1016 object->GetInternalField(kWasmModuleCodeTable)->IsFixedArray() && 1106 object->GetInternalField(kWasmModuleCodeTable)->IsFixedArray() &&
1017 object->GetInternalField(kWasmMemArrayBuffer)->IsJSArrayBuffer() && 1107 object->GetInternalField(kWasmMemArrayBuffer)->IsJSArrayBuffer() &&
1018 object->GetInternalField(kWasmFunctionNamesArray)->IsByteArray(); 1108 object->GetInternalField(kWasmFunctionNamesArray)->IsByteArray();
1019 } 1109 }
1020 1110
1021 } // namespace wasm 1111 } // namespace wasm
1022 } // namespace internal 1112 } // namespace internal
1023 } // namespace v8 1113 } // namespace v8
OLDNEW
« src/wasm/wasm-module.h ('K') | « 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