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

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

Issue 1961973002: [wasm] Implement parallel compilation. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Add tests. Created 4 years, 7 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/macro-assembler.h" 5 #include "src/macro-assembler.h"
6 #include "src/objects.h" 6 #include "src/objects.h"
7 #include "src/property-descriptor.h" 7 #include "src/property-descriptor.h"
8 #include "src/v8.h" 8 #include "src/v8.h"
9 9
10 #include "src/simulator.h" 10 #include "src/simulator.h"
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after
123 : isolate_(isolate), placeholder_code_(size), function_code_(size) {} 123 : isolate_(isolate), placeholder_code_(size), function_code_(size) {}
124 124
125 // Get the code object for a function, allocating a placeholder if it has 125 // Get the code object for a function, allocating a placeholder if it has
126 // not yet been compiled. 126 // not yet been compiled.
127 Handle<Code> GetFunctionCode(uint32_t index) { 127 Handle<Code> GetFunctionCode(uint32_t index) {
128 DCHECK(index < function_code_.size()); 128 DCHECK(index < function_code_.size());
129 if (function_code_[index].is_null()) { 129 if (function_code_[index].is_null()) {
130 // Create a placeholder code object and encode the corresponding index in 130 // Create a placeholder code object and encode the corresponding index in
131 // the {constant_pool_offset} field of the code object. 131 // the {constant_pool_offset} field of the code object.
132 // TODO(titzer): placeholder code objects are somewhat dangerous. 132 // TODO(titzer): placeholder code objects are somewhat dangerous.
133 Handle<Code> self(nullptr, isolate_);
134 byte buffer[] = {0, 0, 0, 0, 0, 0, 0, 0}; // fake instructions. 133 byte buffer[] = {0, 0, 0, 0, 0, 0, 0, 0}; // fake instructions.
135 CodeDesc desc = {buffer, 8, 8, 0, 0, nullptr}; 134 CodeDesc desc = {buffer, 8, 8, 0, 0, nullptr};
136 Handle<Code> code = isolate_->factory()->NewCode( 135 Handle<Code> code = isolate_->factory()->NewCode(
137 desc, Code::KindField::encode(Code::WASM_FUNCTION), self); 136 desc, Code::KindField::encode(Code::WASM_FUNCTION),
137 Handle<Object>::null());
138 code->set_constant_pool_offset(index + kPlaceholderMarker); 138 code->set_constant_pool_offset(index + kPlaceholderMarker);
139 placeholder_code_[index] = code; 139 placeholder_code_[index] = code;
140 function_code_[index] = code; 140 function_code_[index] = code;
141 } 141 }
142 return function_code_[index]; 142 return function_code_[index];
143 } 143 }
144 144
145 void Finish(uint32_t index, Handle<Code> code) { 145 void Finish(uint32_t index, Handle<Code> code) {
146 DCHECK(index < function_code_.size()); 146 DCHECK(index < function_code_.size());
147 function_code_[index] = code; 147 function_code_[index] = code;
(...skipping 243 matching lines...) Expand 10 before | Expand all | Expand 10 after
391 } 391 }
392 392
393 if (!function->IsJSFunction()) { 393 if (!function->IsJSFunction()) {
394 return ReportFFIError(thrower, "not a function", index, module_name, 394 return ReportFFIError(thrower, "not a function", index, module_name,
395 function_name); 395 function_name);
396 } 396 }
397 397
398 return Handle<JSFunction>::cast(function); 398 return Handle<JSFunction>::cast(function);
399 } 399 }
400 400
401 // Fetches the compilation unit of a wasm function and executes its parallel
402 // phase.
403 bool FetchAndExecuteCompilation(
404 Isolate* isolate,
405 std::vector<compiler::WasmCompilationUnit*>* compilation_units,
406 std::queue<compiler::WasmCompilationUnit*>* executed_units,
407 base::Mutex* result_mutex, base::Atomic32* next_unit) {
408 DisallowHeapAllocation no_allocation;
409 DisallowHandleAllocation no_handles;
410 DisallowHandleDereference no_deref;
411 DisallowCodeDependencyChange no_dependency_change;
412
413 // - 1 because AtomicIntrement returns the value after the atomic increment.
414 size_t index =
415 static_cast<size_t>(base::NoBarrier_AtomicIncrement(next_unit, 1)) - 1;
Michael Lippautz 2016/05/10 07:36:41 nit: Don't know if you require the use of a 32bit
ahaas 2016/05/10 09:45:49 Done, thanks.
416 if (index >= compilation_units->size()) {
417 return false;
418 }
419
420 compiler::WasmCompilationUnit* unit = compilation_units->at(index);
421 if (unit != nullptr) {
422 compiler::ExecuteCompilation(unit);
423 {
424 base::LockGuard<base::Mutex> guard(result_mutex);
425 executed_units->push(unit);
426 }
427 }
428 return true;
429 }
430
431 class WasmCompilationTask : public CancelableTask {
432 public:
433 WasmCompilationTask(
434 Isolate* isolate,
435 std::vector<compiler::WasmCompilationUnit*>* compilation_units,
436 std::queue<compiler::WasmCompilationUnit*>* executed_units,
437 base::Semaphore* on_finished, base::Mutex* result_mutex,
438 base::Atomic32* next_unit)
439 : CancelableTask(isolate),
440 isolate_(isolate),
441 compilation_units_(compilation_units),
442 executed_units_(executed_units),
443 on_finished_(on_finished),
444 result_mutex_(result_mutex),
445 next_unit_(next_unit) {}
446
447 void RunInternal() override {
448 while (FetchAndExecuteCompilation(isolate_, compilation_units_,
449 executed_units_, result_mutex_,
450 next_unit_)) {
451 }
452 on_finished_->Signal();
453 }
454
455 Isolate* isolate_;
456 std::vector<compiler::WasmCompilationUnit*>* compilation_units_;
457 std::queue<compiler::WasmCompilationUnit*>* executed_units_;
458 base::Semaphore* on_finished_;
459 base::Mutex* result_mutex_;
460 base::Atomic32* next_unit_;
461 };
462
401 // Instantiates a wasm module as a JSObject. 463 // Instantiates a wasm module as a JSObject.
402 // * allocates a backing store of {mem_size} bytes. 464 // * allocates a backing store of {mem_size} bytes.
403 // * installs a named property "memory" for that buffer if exported 465 // * installs a named property "memory" for that buffer if exported
404 // * installs named properties on the object for exported functions 466 // * installs named properties on the object for exported functions
405 // * compiles wasm code to machine code 467 // * compiles wasm code to machine code
406 MaybeHandle<JSObject> WasmModule::Instantiate(Isolate* isolate, 468 MaybeHandle<JSObject> WasmModule::Instantiate(Isolate* isolate,
407 Handle<JSReceiver> ffi, 469 Handle<JSReceiver> ffi,
408 Handle<JSArrayBuffer> memory) { 470 Handle<JSArrayBuffer> memory) {
409 HistogramTimerScope wasm_instantiate_module_time_scope( 471 HistogramTimerScope wasm_instantiate_module_time_scope(
410 isolate->counters()->wasm_instantiate_module_time()); 472 isolate->counters()->wasm_instantiate_module_time());
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
500 } 562 }
501 } 563 }
502 564
503 //------------------------------------------------------------------------- 565 //-------------------------------------------------------------------------
504 // Compile all functions in the module. 566 // Compile all functions in the module.
505 //------------------------------------------------------------------------- 567 //-------------------------------------------------------------------------
506 { 568 {
507 isolate->counters()->wasm_functions_per_module()->AddSample( 569 isolate->counters()->wasm_functions_per_module()->AddSample(
508 static_cast<int>(functions.size())); 570 static_cast<int>(functions.size()));
509 571
572 //-----------------------------------------------------------------------
573 // For parallel compilation:
574 // 1) The main thread allocates a compilation unit for each wasm function
575 // and stores them in the vector compilation_units.
576 // 2) The main thread spawns WasmCompilationTasks which run on the
577 // background threads.
578 // 3.a) The background threads and the main thread pick one compilation unit
579 // at a time and execute the parallel phase of the compilation unit.
580 // After finishing the execution of the parallel phase, the result is
581 // enqueued in executed_units.
582 // 3.b) If executed_units contains a compilation unit, the main thread
583 // dequeues it and finishes the compilation.
584 // 4) After the parallel phase of all compilation units has started, the
585 // main thread waits for all WasmCompilationTasks to finish.
586 // 5) The main thread finishes the compilation.
587
510 std::vector<compiler::WasmCompilationUnit*> compilation_units( 588 std::vector<compiler::WasmCompilationUnit*> compilation_units(
511 functions.size()); 589 functions.size());
512 std::queue<compiler::WasmCompilationUnit*> executed_units; 590 std::queue<compiler::WasmCompilationUnit*> executed_units;
513 std::vector<Handle<Code>> results(functions.size()); 591 std::vector<Handle<Code>> results(functions.size());
514 592
515 if (FLAG_wasm_parallel_compilation) { 593 if (FLAG_wasm_num_compilation_tasks != 0) {
594 CanonicalHandleScope canonical(isolate);
595
516 // Create a placeholder code object for all functions. 596 // Create a placeholder code object for all functions.
517 // TODO(ahaas): Maybe we could skip this for external functions. 597 // TODO(ahaas): Maybe we could skip this for external functions.
518 for (uint32_t i = 0; i < functions.size(); i++) { 598 for (uint32_t i = 0; i < functions.size(); i++) {
519 linker.GetFunctionCode(i); 599 linker.GetFunctionCode(i);
520 } 600 }
521 601
602 // 1) The main thread allocates a compilation unit for each wasm function
603 // and stores them in the vector compilation_units.
522 for (uint32_t i = FLAG_skip_compiling_wasm_funcs; i < functions.size(); 604 for (uint32_t i = FLAG_skip_compiling_wasm_funcs; i < functions.size();
523 i++) { 605 i++) {
524 if (!functions[i].external) { 606 if (!functions[i].external) {
525 compilation_units[i] = compiler::CreateWasmCompilationUnit( 607 compilation_units[i] = compiler::CreateWasmCompilationUnit(
526 &thrower, isolate, &module_env, &functions[i], i); 608 &thrower, isolate, &module_env, &functions[i], i);
609 } else {
610 compilation_units[i] = nullptr;
527 } 611 }
528 } 612 }
529 613
614 // 2) The main thread spawns WasmCompilationTasks which run on the
615 // background threads.
616 const size_t max_num_tasks =
617 Min(static_cast<size_t>(FLAG_wasm_num_compilation_tasks),
618 V8::GetCurrentPlatform()->NumberOfAvailableBackgroundThreads());
619
620 base::SmartArrayPointer<uint32_t> task_ids(new uint32_t[max_num_tasks]);
621
622 base::Semaphore pending_tasks(0);
Michael Lippautz 2016/05/10 07:36:41 base:Semaphore and friends do not make sure the in
ahaas 2016/05/10 09:45:49 Done. I allocate the Semaphore on the heap now.
623 base::Mutex result_mutex;
624 base::Atomic32 next_unit = FLAG_skip_compiling_wasm_funcs;
625
626 for (size_t i = 0; i < max_num_tasks; i++) {
627 WasmCompilationTask* task = new WasmCompilationTask(
628 isolate, &compilation_units, &executed_units, &pending_tasks,
629 &result_mutex, &next_unit);
630 task_ids[i] = task->id();
631 V8::GetCurrentPlatform()->CallOnBackgroundThread(
632 task, v8::Platform::kShortRunningTask);
633 }
634
530 index = FLAG_skip_compiling_wasm_funcs; 635 index = FLAG_skip_compiling_wasm_funcs;
531 while (true) { 636 bool done = false;
Michael Lippautz 2016/05/10 07:36:41 nit: Wouldn't it make more sense to clearly separa
ahaas 2016/05/10 09:45:48 I don't see a way to clearly separate these phases
Michael Lippautz 2016/05/10 10:35:51 Alright, then memory consumption is the reason to
637 while (!done) {
638 // 3.a) The background threads and the main thread pick one compilation
639 // unit at a time and execute the parallel phase of the compilation
640 // unit. After finishing the execution of the parallel phase, the
641 // result is enqueued in executed_units.
642 if (!FetchAndExecuteCompilation(isolate, &compilation_units,
643 &executed_units, &result_mutex,
644 &next_unit)) {
645 // 4) After the parallel phase of all compilation units has started,
646 // the main thread waits for all WasmCompilationTasks to finish.
647 for (size_t i = 0; i < max_num_tasks; i++) {
648 if (!isolate->cancelable_task_manager()->TryAbort(task_ids[i])) {
649 pending_tasks.Wait();
650 }
651 }
652 done = true;
653 }
654 // 3.b) If executed_units contains a compilation unit, the main thread
655 // dequeues it and finishes the compilation.
532 while (!executed_units.empty()) { 656 while (!executed_units.empty()) {
533 compiler::WasmCompilationUnit* unit = executed_units.front(); 657 compiler::WasmCompilationUnit* unit = nullptr;
534 executed_units.pop(); 658 {
535 int i = compiler::GetIndexOfWasmCompilationUnit(unit); 659 base::LockGuard<base::Mutex> guard(&result_mutex);
536 results[i] = compiler::FinishCompilation(unit); 660 if (!executed_units.empty()) {
537 } 661 unit = executed_units.front();
538 if (index < functions.size()) { 662 executed_units.pop();
539 if (!functions[index].external) { 663 }
540 compiler::ExecuteCompilation(compilation_units[index]);
541 executed_units.push(compilation_units[index]);
542 index++;
543 } 664 }
544 } else { 665 if (unit != nullptr) {
545 break; 666 int j = compiler::GetIndexOfWasmCompilationUnit(unit);
667 if (!functions[j].external) {
668 results[j] = compiler::FinishCompilation(unit);
669 }
670 }
546 } 671 }
547 } 672 }
548 } 673 }
549 674 // 5) The main thread finishes the compilation.
550 // First pass: compile each function and initialize the code table. 675 // First pass: compile each function and initialize the code table.
551 for (uint32_t i = FLAG_skip_compiling_wasm_funcs; i < functions.size(); 676 for (uint32_t i = FLAG_skip_compiling_wasm_funcs; i < functions.size();
552 i++) { 677 i++) {
553 const WasmFunction& func = functions[i]; 678 const WasmFunction& func = functions[i];
554 if (thrower.error()) break; 679 if (thrower.error()) break;
555 DCHECK_EQ(i, func.func_index); 680 DCHECK_EQ(i, func.func_index);
556 681
557 WasmName str = GetName(func.name_offset, func.name_length); 682 WasmName str = GetName(func.name_offset, func.name_length);
558 WasmName str_null = {nullptr, 0}; 683 WasmName str_null = {nullptr, 0};
559 Handle<String> name = factory->InternalizeUtf8String(str); 684 Handle<String> name = factory->InternalizeUtf8String(str);
560 Handle<Code> code = Handle<Code>::null(); 685 Handle<Code> code = Handle<Code>::null();
561 Handle<JSFunction> function = Handle<JSFunction>::null(); 686 Handle<JSFunction> function = Handle<JSFunction>::null();
562 if (func.external) { 687 if (func.external) {
563 // Lookup external function in FFI object. 688 // Lookup external function in FFI object.
564 MaybeHandle<JSFunction> function = 689 MaybeHandle<JSFunction> function =
565 LookupFunction(thrower, factory, ffi, i, str, str_null); 690 LookupFunction(thrower, factory, ffi, i, str, str_null);
566 if (function.is_null()) return MaybeHandle<JSObject>(); 691 if (function.is_null()) return MaybeHandle<JSObject>();
567 code = compiler::CompileWasmToJSWrapper(isolate, &module_env, 692 code = compiler::CompileWasmToJSWrapper(isolate, &module_env,
568 function.ToHandleChecked(), 693 function.ToHandleChecked(),
569 func.sig, str, str_null); 694 func.sig, str, str_null);
570 } else { 695 } else {
571 if (FLAG_wasm_parallel_compilation) { 696 if (FLAG_wasm_num_compilation_tasks != 0) {
572 code = results[i]; 697 code = results[i];
573 } else { 698 } else {
574 // Compile the function. 699 // Compile the function.
700 CanonicalHandleScope canonical(isolate);
575 code = compiler::CompileWasmFunction(&thrower, isolate, &module_env, 701 code = compiler::CompileWasmFunction(&thrower, isolate, &module_env,
576 &func); 702 &func);
577 } 703 }
578 if (code.is_null()) { 704 if (code.is_null()) {
579 thrower.Error("Compilation of #%d:%.*s failed.", i, str.length(), 705 thrower.Error("Compilation of #%d:%.*s failed.", i, str.length(),
580 str.start()); 706 str.start());
581 return MaybeHandle<JSObject>(); 707 return MaybeHandle<JSObject>();
582 } 708 }
583 if (func.exported) { 709 if (func.exported) {
584 function = compiler::CompileJSToWasmWrapper( 710 function = compiler::CompileJSToWasmWrapper(
(...skipping 230 matching lines...) Expand 10 before | Expand all | Expand 10 after
815 wasm->GetInternalField(kWasmFunctionNamesArray), wasm->GetIsolate()); 941 wasm->GetInternalField(kWasmFunctionNamesArray), wasm->GetIsolate());
816 if (func_names_arr_obj->IsUndefined()) 942 if (func_names_arr_obj->IsUndefined())
817 return func_names_arr_obj; // Return undefined. 943 return func_names_arr_obj; // Return undefined.
818 return GetWasmFunctionNameFromTable( 944 return GetWasmFunctionNameFromTable(
819 Handle<ByteArray>::cast(func_names_arr_obj), func_index); 945 Handle<ByteArray>::cast(func_names_arr_obj), func_index);
820 } 946 }
821 947
822 } // namespace wasm 948 } // namespace wasm
823 } // namespace internal 949 } // namespace internal
824 } // namespace v8 950 } // namespace v8
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698