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. | |
2960 Handle<Code> CompileWasmFunction(wasm::ErrorThrower* thrower, Isolate* isolate, | |
2961 wasm::ModuleEnv* module_env, | |
2962 const wasm::WasmFunction* function) { | |
2963 HistogramTimerScope wasm_compile_function_time_scope( | |
2964 isolate->counters()->wasm_compile_function_time()); | |
2965 if (FLAG_trace_wasm_compiler) { | |
2966 OFStream os(stdout); | |
2967 os << "Compiling WASM function " | |
2968 << wasm::WasmFunctionName(function, module_env) << std::endl; | |
2969 os << std::endl; | |
2970 } | |
2971 | |
2972 compiler::ZonePool zone_pool(isolate->allocator()); | |
2973 compiler::ZonePool::Scope graph_zone_scope(&zone_pool); | |
2974 double decode_ms = 0; | |
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 } | |
3015 } | |
3016 CompilationInfo info(func_name, isolate, jsgraph->graph()->zone(), flags); | |
3017 base::SmartPointer<CompilationJob> job(Pipeline::NewWasmCompilationJob( | |
3018 &info, jsgraph->graph(), descriptor, source_positions)); | |
3019 Handle<Code> code = Handle<Code>::null(); | |
3020 if (job->OptimizeGraph() == CompilationJob::SUCCEEDED && | |
3021 job->GenerateCode() == CompilationJob::SUCCEEDED) { | |
3022 code = info.code(); | |
3023 } | |
3024 | |
3025 buffer.Dispose(); | |
3026 | |
3027 if (!code.is_null()) { | |
3028 DCHECK(code->deoptimization_data() == nullptr || | |
3029 code->deoptimization_data()->length() == 0); | |
3030 Handle<FixedArray> deopt_data = | |
3031 isolate->factory()->NewFixedArray(2, TENURED); | |
3032 if (!module_env->instance->js_object.is_null()) { | |
3033 deopt_data->set(0, *module_env->instance->js_object); | |
3034 deopt_data->set(1, Smi::FromInt(function->func_index)); | |
3035 } else if (func_name.start() != nullptr) { | |
3036 MaybeHandle<String> maybe_name = | |
3037 isolate->factory()->NewStringFromUtf8(func_name); | |
3038 if (!maybe_name.is_null()) | |
3039 deopt_data->set(0, *maybe_name.ToHandleChecked()); | |
3040 } | |
3041 deopt_data->set_length(2); | |
3042 code->set_deoptimization_data(*deopt_data); | |
3043 | |
3044 RecordFunctionCompilation( | |
3045 Logger::FUNCTION_TAG, &info, "WASM_function", function->func_index, | |
3046 module_env->module->GetName(function->name_offset, | |
3047 function->name_length)); | |
3048 } | |
3049 | |
3050 if (FLAG_trace_wasm_decode_time) { | |
3051 double compile_ms = compile_timer.Elapsed().InMillisecondsF(); | |
3052 PrintF( | |
3053 "wasm-compile ok: %d bytes, %0.3f ms decode, %d nodes, %0.3f ms " | |
3054 "compile\n", | |
3055 static_cast<int>(function->code_end_offset - | |
3056 function->code_start_offset), | |
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 | |
3066 class WasmCompilationUnit { | 2959 class WasmCompilationUnit { |
3067 public: | 2960 public: |
3068 WasmCompilationUnit(wasm::ErrorThrower* thrower, Isolate* isolate, | 2961 WasmCompilationUnit(wasm::ErrorThrower* thrower, Isolate* isolate, |
3069 wasm::ModuleEnv* module_env, | 2962 wasm::ModuleEnv* module_env, |
3070 const wasm::WasmFunction* function) | 2963 const wasm::WasmFunction* function, uint32_t index) |
3071 : thrower_(thrower), | 2964 : thrower_(thrower), |
3072 isolate_(isolate), | 2965 isolate_(isolate), |
3073 module_env_(module_env), | 2966 module_env_(module_env), |
3074 function_(function) {} | 2967 function_(function), |
2968 compilation_zone_(isolate->allocator()), | |
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) {} | |
3075 | 2978 |
3076 void ExecuteCompilation() { | 2979 void ExecuteCompilation() { |
3077 if (function_->external) { | 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; | |
3078 return; | 3001 return; |
3079 } | 3002 } |
3080 // TODO(ahaas): The parallelizable parts of the compilation should move from | 3003 |
3081 // FinishCompilation to here. | 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 } | |
3082 } | 3033 } |
3083 | 3034 |
3084 Handle<Code> FinishCompilation() { | 3035 Handle<Code> FinishCompilation() { |
3085 return CompileWasmFunction(thrower_, isolate_, module_env_, function_); | 3036 if (!ok_) { |
3037 return Handle<Code>::null(); | |
3038 } | |
3039 base::ElapsedTimer compile_timer; | |
titzer
2016/05/02 14:49:45
Move this timer after the check for success (other
ahaas
2016/05/02 16:21:59
Done.
| |
3040 if (FLAG_trace_wasm_decode_time) { | |
3041 compile_timer.Start(); | |
3042 } | |
3043 if (job_->GenerateCode() != CompilationJob::SUCCEEDED) { | |
3044 return Handle<Code>::null(); | |
3045 } | |
3046 Handle<Code> code = info_.code(); | |
3047 | |
3048 if (!code.is_null()) { | |
titzer
2016/05/02 14:49:45
If the job succeeded, shouldn't the code always be
ahaas
2016/05/02 16:21:59
I replaced the "if (!code.is_null())" by a "DCHECK
| |
3049 DCHECK(code->deoptimization_data() == nullptr || | |
3050 code->deoptimization_data()->length() == 0); | |
3051 Handle<FixedArray> deopt_data = | |
3052 isolate_->factory()->NewFixedArray(2, TENURED); | |
3053 if (!module_env_->instance->js_object.is_null()) { | |
3054 deopt_data->set(0, *module_env_->instance->js_object); | |
3055 deopt_data->set(1, Smi::FromInt(function_->func_index)); | |
3056 } else if (info_.GetDebugName().get() != nullptr) { | |
3057 MaybeHandle<String> maybe_name = isolate_->factory()->NewStringFromUtf8( | |
3058 CStrVector(info_.GetDebugName().get())); | |
3059 if (!maybe_name.is_null()) | |
3060 deopt_data->set(0, *maybe_name.ToHandleChecked()); | |
3061 } | |
3062 deopt_data->set_length(2); | |
3063 code->set_deoptimization_data(*deopt_data); | |
3064 | |
3065 RecordFunctionCompilation( | |
3066 Logger::FUNCTION_TAG, &info_, "WASM_function", function_->func_index, | |
3067 module_env_->module->GetName(function_->name_offset, | |
3068 function_->name_length)); | |
3069 } | |
3070 | |
3071 if (FLAG_trace_wasm_decode_time) { | |
3072 double compile_ms = compile_timer.Elapsed().InMillisecondsF(); | |
3073 PrintF("wasm-code-generation ok: %d bytes, %0.3f ms code generation\n", | |
3074 static_cast<int>(function_->code_end_offset - | |
3075 function_->code_start_offset), | |
3076 compile_ms); | |
3077 } | |
3078 | |
3079 return info_.code(); | |
titzer
2016/05/02 14:49:45
You can you use the local variable {code} here.
ahaas
2016/05/02 16:21:59
Done.
| |
3086 } | 3080 } |
3087 | 3081 |
3088 wasm::ErrorThrower* thrower_; | 3082 wasm::ErrorThrower* thrower_; |
3089 Isolate* isolate_; | 3083 Isolate* isolate_; |
3090 wasm::ModuleEnv* module_env_; | 3084 wasm::ModuleEnv* module_env_; |
3091 const wasm::WasmFunction* function_; | 3085 const wasm::WasmFunction* function_; |
3086 Zone compilation_zone_; | |
3087 CompilationInfo info_; | |
3088 base::SmartPointer<CompilationJob> job_; | |
3089 uint32_t index_; | |
3090 bool ok_; | |
3092 }; | 3091 }; |
3093 | 3092 |
3094 WasmCompilationUnit* CreateWasmCompilationUnit( | 3093 WasmCompilationUnit* CreateWasmCompilationUnit( |
3095 wasm::ErrorThrower* thrower, Isolate* isolate, wasm::ModuleEnv* module_env, | 3094 wasm::ErrorThrower* thrower, Isolate* isolate, wasm::ModuleEnv* module_env, |
3096 const wasm::WasmFunction* function) { | 3095 const wasm::WasmFunction* function, uint32_t index) { |
3097 return new WasmCompilationUnit(thrower, isolate, module_env, function); | 3096 return new WasmCompilationUnit(thrower, isolate, module_env, function, index); |
3098 } | 3097 } |
3099 | 3098 |
3100 void ExecuteCompilation(WasmCompilationUnit* unit) { | 3099 void ExecuteCompilation(WasmCompilationUnit* unit) { |
3101 unit->ExecuteCompilation(); | 3100 unit->ExecuteCompilation(); |
3102 } | 3101 } |
3103 | 3102 |
3103 uint32_t GetIndexOfWasmCompilationUnit(WasmCompilationUnit* unit) { | |
3104 return unit->index_; | |
3105 } | |
3106 | |
3104 Handle<Code> FinishCompilation(WasmCompilationUnit* unit) { | 3107 Handle<Code> FinishCompilation(WasmCompilationUnit* unit) { |
3105 Handle<Code> result = unit->FinishCompilation(); | 3108 Handle<Code> result = unit->FinishCompilation(); |
3106 delete unit; | 3109 delete unit; |
3107 return result; | 3110 return result; |
3108 } | 3111 } |
3109 | 3112 |
3113 // Helper function to compile a single function. | |
3114 Handle<Code> CompileWasmFunction(wasm::ErrorThrower* thrower, Isolate* isolate, | |
3115 wasm::ModuleEnv* module_env, | |
3116 const wasm::WasmFunction* function) { | |
3117 WasmCompilationUnit* unit = | |
3118 CreateWasmCompilationUnit(thrower, isolate, module_env, function, 0); | |
3119 ExecuteCompilation(unit); | |
3120 return FinishCompilation(unit); | |
3121 } | |
3122 | |
3110 } // namespace compiler | 3123 } // namespace compiler |
3111 } // namespace internal | 3124 } // namespace internal |
3112 } // namespace v8 | 3125 } // namespace v8 |
OLD | NEW |