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/compiler/wasm-compiler.h" | 5 #include "src/compiler/wasm-compiler.h" |
6 | 6 |
7 #include "src/isolate-inl.h" | 7 #include "src/isolate-inl.h" |
8 | 8 |
9 #include "src/base/platform/elapsed-timer.h" | 9 #include "src/base/platform/elapsed-timer.h" |
10 #include "src/base/platform/platform.h" | 10 #include "src/base/platform/platform.h" |
(...skipping 2938 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2949 int index = static_cast<int>(function->func_index); | 2949 int index = static_cast<int>(function->func_index); |
2950 if (index >= FLAG_trace_wasm_ast_start && index < FLAG_trace_wasm_ast_end) { | 2950 if (index >= FLAG_trace_wasm_ast_start && index < FLAG_trace_wasm_ast_end) { |
2951 PrintAst(isolate->allocator(), body); | 2951 PrintAst(isolate->allocator(), body); |
2952 } | 2952 } |
2953 if (FLAG_trace_wasm_decode_time) { | 2953 if (FLAG_trace_wasm_decode_time) { |
2954 *decode_ms = decode_timer.Elapsed().InMillisecondsF(); | 2954 *decode_ms = decode_timer.Elapsed().InMillisecondsF(); |
2955 } | 2955 } |
2956 return std::make_pair(jsgraph, source_position_table); | 2956 return std::make_pair(jsgraph, source_position_table); |
2957 } | 2957 } |
2958 | 2958 |
2959 // Helper function to compile a single function. | 2959 class WasmCompilationUnit { |
2960 Handle<Code> CompileWasmFunction(wasm::ErrorThrower* thrower, Isolate* isolate, | 2960 public: |
2961 wasm::ModuleEnv* module_env, | 2961 WasmCompilationUnit(wasm::ErrorThrower* thrower, Isolate* isolate, |
2962 const wasm::WasmFunction* function) { | 2962 wasm::ModuleEnv* module_env, |
2963 HistogramTimerScope wasm_compile_function_time_scope( | 2963 const wasm::WasmFunction* function, uint32_t index) |
2964 isolate->counters()->wasm_compile_function_time()); | 2964 : thrower_(thrower), |
2965 if (FLAG_trace_wasm_compiler) { | 2965 isolate_(isolate), |
2966 OFStream os(stdout); | 2966 module_env_(module_env), |
2967 os << "Compiling WASM function " | 2967 function_(function), |
2968 << wasm::WasmFunctionName(function, module_env) << std::endl; | 2968 compilation_zone_(isolate->allocator()), |
2969 os << std::endl; | 2969 info_(function->name_length != 0 |
| 2970 ? module_env->module->GetNameOrNull(function->name_offset, |
| 2971 function->name_length) |
| 2972 : ArrayVector("wasm"), |
| 2973 isolate, &compilation_zone_, |
| 2974 Code::ComputeFlags(Code::WASM_FUNCTION)), |
| 2975 job_(), |
| 2976 index_(index), |
| 2977 ok_(true) {} |
| 2978 |
| 2979 void ExecuteCompilation() { |
| 2980 HistogramTimerScope wasm_compile_function_time_scope( |
| 2981 isolate_->counters()->wasm_compile_function_time()); |
| 2982 if (FLAG_trace_wasm_compiler) { |
| 2983 OFStream os(stdout); |
| 2984 os << "Compiling WASM function " |
| 2985 << wasm::WasmFunctionName(function_, module_env_) << std::endl; |
| 2986 os << std::endl; |
| 2987 } |
| 2988 |
| 2989 double decode_ms = 0; |
| 2990 size_t node_count = 0; |
| 2991 |
| 2992 Zone zone(isolate_->allocator()); |
| 2993 std::pair<JSGraph*, SourcePositionTable*> graph_result = |
| 2994 BuildGraphForWasmFunction(&zone, thrower_, isolate_, module_env_, |
| 2995 function_, &decode_ms); |
| 2996 JSGraph* jsgraph = graph_result.first; |
| 2997 SourcePositionTable* source_positions = graph_result.second; |
| 2998 |
| 2999 if (jsgraph == nullptr) { |
| 3000 ok_ = false; |
| 3001 return; |
| 3002 } |
| 3003 |
| 3004 base::ElapsedTimer pipeline_timer; |
| 3005 if (FLAG_trace_wasm_decode_time) { |
| 3006 node_count = jsgraph->graph()->NodeCount(); |
| 3007 pipeline_timer.Start(); |
| 3008 } |
| 3009 |
| 3010 // Run the compiler pipeline to generate machine code. |
| 3011 CallDescriptor* descriptor = wasm::ModuleEnv::GetWasmCallDescriptor( |
| 3012 &compilation_zone_, function_->sig); |
| 3013 if (jsgraph->machine()->Is32()) { |
| 3014 descriptor = |
| 3015 module_env_->GetI32WasmCallDescriptor(&compilation_zone_, descriptor); |
| 3016 } |
| 3017 job_.Reset(Pipeline::NewWasmCompilationJob(&info_, jsgraph->graph(), |
| 3018 descriptor, source_positions)); |
| 3019 ok_ = job_->OptimizeGraph() == CompilationJob::SUCCEEDED; |
| 3020 // TODO(bradnelson): Improve histogram handling of size_t. |
| 3021 isolate_->counters()->wasm_compile_function_peak_memory_bytes()->AddSample( |
| 3022 static_cast<int>(jsgraph->graph()->zone()->allocation_size())); |
| 3023 |
| 3024 if (FLAG_trace_wasm_decode_time) { |
| 3025 double pipeline_ms = pipeline_timer.Elapsed().InMillisecondsF(); |
| 3026 PrintF( |
| 3027 "wasm-compilation phase 1 ok: %d bytes, %0.3f ms decode, %zu nodes, " |
| 3028 "%0.3f ms pipeline\n", |
| 3029 static_cast<int>(function_->code_end_offset - |
| 3030 function_->code_start_offset), |
| 3031 decode_ms, node_count, pipeline_ms); |
| 3032 } |
2970 } | 3033 } |
2971 | 3034 |
2972 compiler::ZonePool zone_pool(isolate->allocator()); | 3035 Handle<Code> FinishCompilation() { |
2973 compiler::ZonePool::Scope graph_zone_scope(&zone_pool); | 3036 if (!ok_) { |
2974 double decode_ms = 0; | 3037 return Handle<Code>::null(); |
2975 std::pair<JSGraph*, SourcePositionTable*> graph_result = | |
2976 BuildGraphForWasmFunction(graph_zone_scope.zone(), thrower, isolate, | |
2977 module_env, function, &decode_ms); | |
2978 JSGraph* jsgraph = graph_result.first; | |
2979 SourcePositionTable* source_positions = graph_result.second; | |
2980 | |
2981 if (jsgraph == nullptr) { | |
2982 return Handle<Code>::null(); | |
2983 } | |
2984 | |
2985 base::ElapsedTimer compile_timer; | |
2986 if (FLAG_trace_wasm_decode_time) { | |
2987 compile_timer.Start(); | |
2988 } | |
2989 // Run the compiler pipeline to generate machine code. | |
2990 CallDescriptor* descriptor = wasm::ModuleEnv::GetWasmCallDescriptor( | |
2991 jsgraph->graph()->zone(), function->sig); | |
2992 if (jsgraph->machine()->Is32()) { | |
2993 descriptor = module_env->GetI32WasmCallDescriptor(jsgraph->graph()->zone(), | |
2994 descriptor); | |
2995 } | |
2996 Code::Flags flags = Code::ComputeFlags(Code::WASM_FUNCTION); | |
2997 // add flags here if a meaningful name is helpful for debugging. | |
2998 bool debugging = | |
2999 #if DEBUG | |
3000 true; | |
3001 #else | |
3002 FLAG_print_opt_code || FLAG_trace_turbo || FLAG_trace_turbo_graph; | |
3003 #endif | |
3004 Vector<const char> func_name = module_env->module->GetNameOrNull( | |
3005 function->name_offset, function->name_length); | |
3006 Vector<char> buffer; | |
3007 if (func_name.is_empty()) { | |
3008 if (debugging) { | |
3009 buffer = Vector<char>::New(128); | |
3010 int chars = SNPrintF(buffer, "WASM_function_#%d", function->func_index); | |
3011 func_name = Vector<const char>::cast(buffer.SubVector(0, chars)); | |
3012 } else { | |
3013 func_name = ArrayVector("wasm"); | |
3014 } | 3038 } |
3015 } | 3039 if (job_->GenerateCode() != CompilationJob::SUCCEEDED) { |
3016 CompilationInfo info(func_name, isolate, jsgraph->graph()->zone(), flags); | 3040 return Handle<Code>::null(); |
3017 base::SmartPointer<CompilationJob> job(Pipeline::NewWasmCompilationJob( | 3041 } |
3018 &info, jsgraph->graph(), descriptor, source_positions)); | 3042 base::ElapsedTimer compile_timer; |
3019 Handle<Code> code = Handle<Code>::null(); | 3043 if (FLAG_trace_wasm_decode_time) { |
3020 if (job->OptimizeGraph() == CompilationJob::SUCCEEDED && | 3044 compile_timer.Start(); |
3021 job->GenerateCode() == CompilationJob::SUCCEEDED) { | 3045 } |
3022 code = info.code(); | 3046 Handle<Code> code = info_.code(); |
3023 } | 3047 DCHECK(!code.is_null()); |
3024 | |
3025 buffer.Dispose(); | |
3026 | |
3027 if (!code.is_null()) { | |
3028 DCHECK(code->deoptimization_data() == nullptr || | 3048 DCHECK(code->deoptimization_data() == nullptr || |
3029 code->deoptimization_data()->length() == 0); | 3049 code->deoptimization_data()->length() == 0); |
3030 Handle<FixedArray> deopt_data = | 3050 Handle<FixedArray> deopt_data = |
3031 isolate->factory()->NewFixedArray(2, TENURED); | 3051 isolate_->factory()->NewFixedArray(2, TENURED); |
3032 if (!module_env->instance->js_object.is_null()) { | 3052 if (!module_env_->instance->js_object.is_null()) { |
3033 deopt_data->set(0, *module_env->instance->js_object); | 3053 deopt_data->set(0, *module_env_->instance->js_object); |
3034 deopt_data->set(1, Smi::FromInt(function->func_index)); | 3054 deopt_data->set(1, Smi::FromInt(function_->func_index)); |
3035 } else if (func_name.start() != nullptr) { | 3055 } else if (info_.GetDebugName().get() != nullptr) { |
3036 MaybeHandle<String> maybe_name = | 3056 MaybeHandle<String> maybe_name = isolate_->factory()->NewStringFromUtf8( |
3037 isolate->factory()->NewStringFromUtf8(func_name); | 3057 CStrVector(info_.GetDebugName().get())); |
3038 if (!maybe_name.is_null()) | 3058 if (!maybe_name.is_null()) |
3039 deopt_data->set(0, *maybe_name.ToHandleChecked()); | 3059 deopt_data->set(0, *maybe_name.ToHandleChecked()); |
3040 } | 3060 } |
3041 deopt_data->set_length(2); | 3061 deopt_data->set_length(2); |
3042 code->set_deoptimization_data(*deopt_data); | 3062 code->set_deoptimization_data(*deopt_data); |
3043 | 3063 |
3044 RecordFunctionCompilation( | 3064 RecordFunctionCompilation( |
3045 Logger::FUNCTION_TAG, &info, "WASM_function", function->func_index, | 3065 Logger::FUNCTION_TAG, &info_, "WASM_function", function_->func_index, |
3046 module_env->module->GetName(function->name_offset, | 3066 module_env_->module->GetName(function_->name_offset, |
3047 function->name_length)); | 3067 function_->name_length)); |
3048 } | |
3049 | 3068 |
3050 if (FLAG_trace_wasm_decode_time) { | 3069 if (FLAG_trace_wasm_decode_time) { |
3051 double compile_ms = compile_timer.Elapsed().InMillisecondsF(); | 3070 double compile_ms = compile_timer.Elapsed().InMillisecondsF(); |
3052 PrintF( | 3071 PrintF("wasm-code-generation ok: %d bytes, %0.3f ms code generation\n", |
3053 "wasm-compile ok: %d bytes, %0.3f ms decode, %d nodes, %0.3f ms " | 3072 static_cast<int>(function_->code_end_offset - |
3054 "compile\n", | 3073 function_->code_start_offset), |
3055 static_cast<int>(function->code_end_offset - | 3074 compile_ms); |
3056 function->code_start_offset), | 3075 } |
3057 decode_ms, static_cast<int>(jsgraph->graph()->NodeCount()), compile_ms); | |
3058 } | |
3059 // TODO(bradnelson): Improve histogram handling of size_t. | |
3060 isolate->counters()->wasm_compile_function_peak_memory_bytes()->AddSample( | |
3061 static_cast<int>(jsgraph->graph()->zone()->allocation_size())); | |
3062 graph_zone_scope.Destroy(); | |
3063 return code; | |
3064 } | |
3065 | 3076 |
3066 class WasmCompilationUnit { | 3077 return code; |
3067 public: | |
3068 WasmCompilationUnit(wasm::ErrorThrower* thrower, Isolate* isolate, | |
3069 wasm::ModuleEnv* module_env, | |
3070 const wasm::WasmFunction* function) | |
3071 : thrower_(thrower), | |
3072 isolate_(isolate), | |
3073 module_env_(module_env), | |
3074 function_(function) {} | |
3075 | |
3076 void ExecuteCompilation() { | |
3077 if (function_->external) { | |
3078 return; | |
3079 } | |
3080 // TODO(ahaas): The parallelizable parts of the compilation should move from | |
3081 // FinishCompilation to here. | |
3082 } | |
3083 | |
3084 Handle<Code> FinishCompilation() { | |
3085 return CompileWasmFunction(thrower_, isolate_, module_env_, function_); | |
3086 } | 3078 } |
3087 | 3079 |
3088 wasm::ErrorThrower* thrower_; | 3080 wasm::ErrorThrower* thrower_; |
3089 Isolate* isolate_; | 3081 Isolate* isolate_; |
3090 wasm::ModuleEnv* module_env_; | 3082 wasm::ModuleEnv* module_env_; |
3091 const wasm::WasmFunction* function_; | 3083 const wasm::WasmFunction* function_; |
| 3084 Zone compilation_zone_; |
| 3085 CompilationInfo info_; |
| 3086 base::SmartPointer<CompilationJob> job_; |
| 3087 uint32_t index_; |
| 3088 bool ok_; |
3092 }; | 3089 }; |
3093 | 3090 |
3094 WasmCompilationUnit* CreateWasmCompilationUnit( | 3091 WasmCompilationUnit* CreateWasmCompilationUnit( |
3095 wasm::ErrorThrower* thrower, Isolate* isolate, wasm::ModuleEnv* module_env, | 3092 wasm::ErrorThrower* thrower, Isolate* isolate, wasm::ModuleEnv* module_env, |
3096 const wasm::WasmFunction* function) { | 3093 const wasm::WasmFunction* function, uint32_t index) { |
3097 return new WasmCompilationUnit(thrower, isolate, module_env, function); | 3094 return new WasmCompilationUnit(thrower, isolate, module_env, function, index); |
3098 } | 3095 } |
3099 | 3096 |
3100 void ExecuteCompilation(WasmCompilationUnit* unit) { | 3097 void ExecuteCompilation(WasmCompilationUnit* unit) { |
3101 unit->ExecuteCompilation(); | 3098 unit->ExecuteCompilation(); |
3102 } | 3099 } |
3103 | 3100 |
| 3101 uint32_t GetIndexOfWasmCompilationUnit(WasmCompilationUnit* unit) { |
| 3102 return unit->index_; |
| 3103 } |
| 3104 |
3104 Handle<Code> FinishCompilation(WasmCompilationUnit* unit) { | 3105 Handle<Code> FinishCompilation(WasmCompilationUnit* unit) { |
3105 Handle<Code> result = unit->FinishCompilation(); | 3106 Handle<Code> result = unit->FinishCompilation(); |
3106 delete unit; | 3107 delete unit; |
3107 return result; | 3108 return result; |
3108 } | 3109 } |
3109 | 3110 |
| 3111 // Helper function to compile a single function. |
| 3112 Handle<Code> CompileWasmFunction(wasm::ErrorThrower* thrower, Isolate* isolate, |
| 3113 wasm::ModuleEnv* module_env, |
| 3114 const wasm::WasmFunction* function) { |
| 3115 WasmCompilationUnit* unit = |
| 3116 CreateWasmCompilationUnit(thrower, isolate, module_env, function, 0); |
| 3117 ExecuteCompilation(unit); |
| 3118 return FinishCompilation(unit); |
| 3119 } |
| 3120 |
3110 } // namespace compiler | 3121 } // namespace compiler |
3111 } // namespace internal | 3122 } // namespace internal |
3112 } // namespace v8 | 3123 } // namespace v8 |
OLD | NEW |