Chromium Code Reviews| OLD | NEW |
|---|---|
| 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/macro-assembler.h" | 6 #include "src/macro-assembler.h" |
| 6 #include "src/objects.h" | 7 #include "src/objects.h" |
| 7 #include "src/property-descriptor.h" | 8 #include "src/property-descriptor.h" |
| 8 #include "src/v8.h" | 9 #include "src/v8.h" |
| 9 | 10 |
| 10 #include "src/simulator.h" | 11 #include "src/simulator.h" |
| 11 | 12 |
| 12 #include "src/wasm/ast-decoder.h" | 13 #include "src/wasm/ast-decoder.h" |
| 13 #include "src/wasm/module-decoder.h" | 14 #include "src/wasm/module-decoder.h" |
| 14 #include "src/wasm/wasm-function-name-table.h" | 15 #include "src/wasm/wasm-function-name-table.h" |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 123 : isolate_(isolate), placeholder_code_(size), function_code_(size) {} | 124 : isolate_(isolate), placeholder_code_(size), function_code_(size) {} |
| 124 | 125 |
| 125 // Get the code object for a function, allocating a placeholder if it has | 126 // Get the code object for a function, allocating a placeholder if it has |
| 126 // not yet been compiled. | 127 // not yet been compiled. |
| 127 Handle<Code> GetFunctionCode(uint32_t index) { | 128 Handle<Code> GetFunctionCode(uint32_t index) { |
| 128 DCHECK(index < function_code_.size()); | 129 DCHECK(index < function_code_.size()); |
| 129 if (function_code_[index].is_null()) { | 130 if (function_code_[index].is_null()) { |
| 130 // Create a placeholder code object and encode the corresponding index in | 131 // Create a placeholder code object and encode the corresponding index in |
| 131 // the {constant_pool_offset} field of the code object. | 132 // the {constant_pool_offset} field of the code object. |
| 132 // TODO(titzer): placeholder code objects are somewhat dangerous. | 133 // 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. | 134 byte buffer[] = {0, 0, 0, 0, 0, 0, 0, 0}; // fake instructions. |
| 135 CodeDesc desc = {buffer, 8, 8, 0, 0, nullptr}; | 135 CodeDesc desc = {buffer, 8, 8, 0, 0, nullptr}; |
| 136 Handle<Code> code = isolate_->factory()->NewCode( | 136 Handle<Code> code = isolate_->factory()->NewCode( |
| 137 desc, Code::KindField::encode(Code::WASM_FUNCTION), self); | 137 desc, Code::KindField::encode(Code::WASM_FUNCTION), |
| 138 Handle<Object>::null()); | |
| 138 code->set_constant_pool_offset(index + kPlaceholderMarker); | 139 code->set_constant_pool_offset(index + kPlaceholderMarker); |
| 139 placeholder_code_[index] = code; | 140 placeholder_code_[index] = code; |
| 140 function_code_[index] = code; | 141 function_code_[index] = code; |
| 141 } | 142 } |
| 142 return function_code_[index]; | 143 return function_code_[index]; |
| 143 } | 144 } |
| 144 | 145 |
| 145 void Finish(uint32_t index, Handle<Code> code) { | 146 void Finish(uint32_t index, Handle<Code> code) { |
| 146 DCHECK(index < function_code_.size()); | 147 DCHECK(index < function_code_.size()); |
| 147 function_code_[index] = code; | 148 function_code_[index] = code; |
| (...skipping 243 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 391 } | 392 } |
| 392 | 393 |
| 393 if (!function->IsJSFunction()) { | 394 if (!function->IsJSFunction()) { |
| 394 return ReportFFIError(thrower, "not a function", index, module_name, | 395 return ReportFFIError(thrower, "not a function", index, module_name, |
| 395 function_name); | 396 function_name); |
| 396 } | 397 } |
| 397 | 398 |
| 398 return Handle<JSFunction>::cast(function); | 399 return Handle<JSFunction>::cast(function); |
| 399 } | 400 } |
| 400 | 401 |
| 402 // Fetches the compilation unit of a wasm function and executes its parallel | |
| 403 // phase. | |
| 404 bool FetchAndExecuteCompilation( | |
|
titzer
2016/05/10 12:32:10
Put this into an anonymous namespace.
ahaas
2016/05/11 09:52:21
done.
| |
| 405 Isolate* isolate, | |
| 406 std::vector<compiler::WasmCompilationUnit*>* compilation_units, | |
| 407 std::queue<compiler::WasmCompilationUnit*>* executed_units, | |
| 408 base::Mutex* result_mutex, base::AtomicNumber<size_t>* next_unit) { | |
| 409 DisallowHeapAllocation no_allocation; | |
| 410 DisallowHandleAllocation no_handles; | |
| 411 DisallowHandleDereference no_deref; | |
| 412 DisallowCodeDependencyChange no_dependency_change; | |
| 413 | |
| 414 // - 1 because AtomicIntrement returns the value after the atomic increment. | |
| 415 size_t index = next_unit->Increment(1) - 1; | |
| 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::AtomicNumber<size_t>* 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::AtomicNumber<size_t>* 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 Loading... | |
| 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. | |
|
titzer
2016/05/10 12:32:10
We've adopted the {} style for comments, so s/comp
ahaas
2016/05/11 09:52:21
done.
| |
| 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) { |
|
titzer
2016/05/10 12:32:10
This function is getting huge. Can you split out t
ahaas
2016/05/11 09:52:21
Done.
| |
| 594 CanonicalHandleScope canonical(isolate); | |
|
titzer
2016/05/10 12:32:10
Comment on why we need a canonical handle scope he
ahaas
2016/05/11 09:52:21
Done.
| |
| 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::SmartPointer<base::Semaphore> pending_tasks(new base::Semaphore(0)); | |
| 623 base::Mutex result_mutex; | |
| 624 base::AtomicNumber<size_t> next_unit( | |
| 625 static_cast<size_t>(FLAG_skip_compiling_wasm_funcs)); | |
| 626 | |
| 627 for (size_t i = 0; i < max_num_tasks; i++) { | |
| 628 WasmCompilationTask* task = new WasmCompilationTask( | |
| 629 isolate, &compilation_units, &executed_units, pending_tasks.get(), | |
| 630 &result_mutex, &next_unit); | |
| 631 task_ids[i] = task->id(); | |
| 632 V8::GetCurrentPlatform()->CallOnBackgroundThread( | |
| 633 task, v8::Platform::kShortRunningTask); | |
| 634 } | |
| 635 | |
| 530 index = FLAG_skip_compiling_wasm_funcs; | 636 index = FLAG_skip_compiling_wasm_funcs; |
| 531 while (true) { | 637 bool done = false; |
| 638 while (!done) { | |
| 639 // The steps loop contains the steps 3.a, 4, and 3.b, are intertwined in | |
| 640 // this loop for the following reasons: | |
| 641 // * We cannot wait with the sequential fixup (3b) until after the | |
| 642 // parallel execution has finished because we would run out of memory. | |
| 643 // * The fixup has to done after the waiting because otherwise we would | |
| 644 // miss the last functions. | |
| 645 // * The main thread should to participate in the parallel execution to | |
| 646 // guarantee progress even if the worker threads don't do any work. | |
| 647 | |
| 648 // 3.a) The background threads and the main thread pick one compilation | |
| 649 // unit at a time and execute the parallel phase of the compilation | |
| 650 // unit. After finishing the execution of the parallel phase, the | |
| 651 // result is enqueued in executed_units. | |
| 652 if (!FetchAndExecuteCompilation(isolate, &compilation_units, | |
| 653 &executed_units, &result_mutex, | |
| 654 &next_unit)) { | |
| 655 // 4) After the parallel phase of all compilation units has started, | |
| 656 // the main thread waits for all WasmCompilationTasks to finish. | |
| 657 for (size_t i = 0; i < max_num_tasks; i++) { | |
| 658 if (!isolate->cancelable_task_manager()->TryAbort(task_ids[i])) { | |
| 659 pending_tasks->Wait(); | |
| 660 } | |
| 661 } | |
| 662 done = true; | |
| 663 } | |
| 664 // 3.b) If executed_units contains a compilation unit, the main thread | |
| 665 // dequeues it and finishes the compilation. | |
| 532 while (!executed_units.empty()) { | 666 while (!executed_units.empty()) { |
| 533 compiler::WasmCompilationUnit* unit = executed_units.front(); | 667 compiler::WasmCompilationUnit* unit = nullptr; |
| 534 executed_units.pop(); | 668 { |
| 535 int i = compiler::GetIndexOfWasmCompilationUnit(unit); | 669 base::LockGuard<base::Mutex> guard(&result_mutex); |
| 536 results[i] = compiler::FinishCompilation(unit); | 670 if (!executed_units.empty()) { |
| 537 } | 671 unit = executed_units.front(); |
| 538 if (index < functions.size()) { | 672 executed_units.pop(); |
| 539 if (!functions[index].external) { | 673 } |
| 540 compiler::ExecuteCompilation(compilation_units[index]); | |
| 541 executed_units.push(compilation_units[index]); | |
| 542 index++; | |
| 543 } | 674 } |
| 544 } else { | 675 if (unit != nullptr) { |
| 545 break; | 676 int j = compiler::GetIndexOfWasmCompilationUnit(unit); |
| 677 if (!functions[j].external) { | |
| 678 results[j] = compiler::FinishCompilation(unit); | |
| 679 } | |
| 680 } | |
| 546 } | 681 } |
| 547 } | 682 } |
| 548 } | 683 } |
| 549 | 684 // 5) The main thread finishes the compilation. |
| 550 // First pass: compile each function and initialize the code table. | 685 // First pass: compile each function and initialize the code table. |
|
titzer
2016/05/10 12:32:10
This comment is now out of date.
ahaas
2016/05/11 09:52:21
Done.
| |
| 551 for (uint32_t i = FLAG_skip_compiling_wasm_funcs; i < functions.size(); | 686 for (uint32_t i = FLAG_skip_compiling_wasm_funcs; i < functions.size(); |
| 552 i++) { | 687 i++) { |
| 553 const WasmFunction& func = functions[i]; | 688 const WasmFunction& func = functions[i]; |
| 554 if (thrower.error()) break; | 689 if (thrower.error()) break; |
| 555 DCHECK_EQ(i, func.func_index); | 690 DCHECK_EQ(i, func.func_index); |
| 556 | 691 |
| 557 WasmName str = GetName(func.name_offset, func.name_length); | 692 WasmName str = GetName(func.name_offset, func.name_length); |
| 558 WasmName str_null = {nullptr, 0}; | 693 WasmName str_null = {nullptr, 0}; |
| 559 Handle<String> name = factory->InternalizeUtf8String(str); | 694 Handle<String> name = factory->InternalizeUtf8String(str); |
| 560 Handle<Code> code = Handle<Code>::null(); | 695 Handle<Code> code = Handle<Code>::null(); |
| 561 Handle<JSFunction> function = Handle<JSFunction>::null(); | 696 Handle<JSFunction> function = Handle<JSFunction>::null(); |
| 562 if (func.external) { | 697 if (func.external) { |
| 563 // Lookup external function in FFI object. | 698 // Lookup external function in FFI object. |
| 564 MaybeHandle<JSFunction> function = | 699 MaybeHandle<JSFunction> function = |
| 565 LookupFunction(thrower, factory, ffi, i, str, str_null); | 700 LookupFunction(thrower, factory, ffi, i, str, str_null); |
| 566 if (function.is_null()) return MaybeHandle<JSObject>(); | 701 if (function.is_null()) return MaybeHandle<JSObject>(); |
| 567 code = compiler::CompileWasmToJSWrapper(isolate, &module_env, | 702 code = compiler::CompileWasmToJSWrapper(isolate, &module_env, |
| 568 function.ToHandleChecked(), | 703 function.ToHandleChecked(), |
| 569 func.sig, str, str_null); | 704 func.sig, str, str_null); |
| 570 } else { | 705 } else { |
| 571 if (FLAG_wasm_parallel_compilation) { | 706 if (FLAG_wasm_num_compilation_tasks != 0) { |
| 572 code = results[i]; | 707 code = results[i]; |
| 573 } else { | 708 } else { |
| 574 // Compile the function. | 709 // Compile the function. |
| 710 CanonicalHandleScope canonical(isolate); | |
| 575 code = compiler::CompileWasmFunction(&thrower, isolate, &module_env, | 711 code = compiler::CompileWasmFunction(&thrower, isolate, &module_env, |
| 576 &func); | 712 &func); |
| 577 } | 713 } |
| 578 if (code.is_null()) { | 714 if (code.is_null()) { |
| 579 thrower.Error("Compilation of #%d:%.*s failed.", i, str.length(), | 715 thrower.Error("Compilation of #%d:%.*s failed.", i, str.length(), |
| 580 str.start()); | 716 str.start()); |
| 581 return MaybeHandle<JSObject>(); | 717 return MaybeHandle<JSObject>(); |
| 582 } | 718 } |
| 583 if (func.exported) { | 719 if (func.exported) { |
| 584 function = compiler::CompileJSToWasmWrapper( | 720 function = compiler::CompileJSToWasmWrapper( |
| (...skipping 230 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 815 wasm->GetInternalField(kWasmFunctionNamesArray), wasm->GetIsolate()); | 951 wasm->GetInternalField(kWasmFunctionNamesArray), wasm->GetIsolate()); |
| 816 if (func_names_arr_obj->IsUndefined()) | 952 if (func_names_arr_obj->IsUndefined()) |
| 817 return func_names_arr_obj; // Return undefined. | 953 return func_names_arr_obj; // Return undefined. |
| 818 return GetWasmFunctionNameFromTable( | 954 return GetWasmFunctionNameFromTable( |
| 819 Handle<ByteArray>::cast(func_names_arr_obj), func_index); | 955 Handle<ByteArray>::cast(func_names_arr_obj), func_index); |
| 820 } | 956 } |
| 821 | 957 |
| 822 } // namespace wasm | 958 } // namespace wasm |
| 823 } // namespace internal | 959 } // namespace internal |
| 824 } // namespace v8 | 960 } // namespace v8 |
| OLD | NEW |