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 <memory> | 7 #include <memory> |
8 | 8 |
9 #include "src/isolate-inl.h" | 9 #include "src/isolate-inl.h" |
10 | 10 |
(...skipping 3134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3145 Handle<String> name_str = | 3145 Handle<String> name_str = |
3146 isolate->factory()->NewStringFromAsciiChecked(buffer.start()); | 3146 isolate->factory()->NewStringFromAsciiChecked(buffer.start()); |
3147 Handle<String> script_str = | 3147 Handle<String> script_str = |
3148 isolate->factory()->NewStringFromAsciiChecked("(WASM)"); | 3148 isolate->factory()->NewStringFromAsciiChecked("(WASM)"); |
3149 Handle<SharedFunctionInfo> shared = | 3149 Handle<SharedFunctionInfo> shared = |
3150 isolate->factory()->NewSharedFunctionInfo(name_str, code, false); | 3150 isolate->factory()->NewSharedFunctionInfo(name_str, code, false); |
3151 PROFILE(isolate, CodeCreateEvent(tag, AbstractCode::cast(*code), *shared, | 3151 PROFILE(isolate, CodeCreateEvent(tag, AbstractCode::cast(*code), *shared, |
3152 *script_str, 0, 0)); | 3152 *script_str, 0, 0)); |
3153 } | 3153 } |
3154 | 3154 |
3155 Handle<Code> CompileJSToWasmWrapper(Isolate* isolate, wasm::ModuleEnv* module, | 3155 Handle<Code> CompileJSToWasmWrapper(Isolate* isolate, |
| 3156 const wasm::WasmModule* module, |
3156 Handle<Code> wasm_code, uint32_t index) { | 3157 Handle<Code> wasm_code, uint32_t index) { |
3157 const wasm::WasmFunction* func = &module->module->functions[index]; | 3158 const wasm::WasmFunction* func = &module->functions[index]; |
3158 | 3159 |
3159 //---------------------------------------------------------------------------- | 3160 //---------------------------------------------------------------------------- |
3160 // Create the Graph | 3161 // Create the Graph |
3161 //---------------------------------------------------------------------------- | 3162 //---------------------------------------------------------------------------- |
3162 Zone zone(isolate->allocator(), ZONE_NAME); | 3163 Zone zone(isolate->allocator(), ZONE_NAME); |
3163 Graph graph(&zone); | 3164 Graph graph(&zone); |
3164 CommonOperatorBuilder common(&zone); | 3165 CommonOperatorBuilder common(&zone); |
3165 MachineOperatorBuilder machine(&zone); | 3166 MachineOperatorBuilder machine(&zone); |
3166 JSGraph jsgraph(isolate, &graph, &common, nullptr, nullptr, &machine); | 3167 JSGraph jsgraph(isolate, &graph, &common, nullptr, nullptr, &machine); |
3167 | 3168 |
3168 Node* control = nullptr; | 3169 Node* control = nullptr; |
3169 Node* effect = nullptr; | 3170 Node* effect = nullptr; |
3170 | 3171 |
| 3172 wasm::ModuleEnv module_env(module, nullptr); |
3171 WasmGraphBuilder builder(&zone, &jsgraph, func->sig); | 3173 WasmGraphBuilder builder(&zone, &jsgraph, func->sig); |
3172 builder.set_control_ptr(&control); | 3174 builder.set_control_ptr(&control); |
3173 builder.set_effect_ptr(&effect); | 3175 builder.set_effect_ptr(&effect); |
3174 builder.set_module(module); | 3176 builder.set_module(&module_env); |
3175 builder.BuildJSToWasmWrapper(wasm_code, func->sig); | 3177 builder.BuildJSToWasmWrapper(wasm_code, func->sig); |
3176 | 3178 |
3177 //---------------------------------------------------------------------------- | 3179 //---------------------------------------------------------------------------- |
3178 // Run the compilation pipeline. | 3180 // Run the compilation pipeline. |
3179 //---------------------------------------------------------------------------- | 3181 //---------------------------------------------------------------------------- |
3180 if (FLAG_trace_turbo_graph) { // Simple textual RPO. | 3182 if (FLAG_trace_turbo_graph) { // Simple textual RPO. |
3181 OFStream os(stdout); | 3183 OFStream os(stdout); |
3182 os << "-- Graph after change lowering -- " << std::endl; | 3184 os << "-- Graph after change lowering -- " << std::endl; |
3183 os << AsRPO(graph); | 3185 os << AsRPO(graph); |
3184 } | 3186 } |
3185 | 3187 |
3186 // Schedule and compile to machine code. | 3188 // Schedule and compile to machine code. |
3187 int params = | 3189 int params = static_cast<int>( |
3188 static_cast<int>(module->GetFunctionSignature(index)->parameter_count()); | 3190 module_env.GetFunctionSignature(index)->parameter_count()); |
3189 CallDescriptor* incoming = Linkage::GetJSCallDescriptor( | 3191 CallDescriptor* incoming = Linkage::GetJSCallDescriptor( |
3190 &zone, false, params + 1, CallDescriptor::kNoFlags); | 3192 &zone, false, params + 1, CallDescriptor::kNoFlags); |
3191 Code::Flags flags = Code::ComputeFlags(Code::JS_TO_WASM_FUNCTION); | 3193 Code::Flags flags = Code::ComputeFlags(Code::JS_TO_WASM_FUNCTION); |
3192 bool debugging = | 3194 bool debugging = |
3193 #if DEBUG | 3195 #if DEBUG |
3194 true; | 3196 true; |
3195 #else | 3197 #else |
3196 FLAG_print_opt_code || FLAG_trace_turbo || FLAG_trace_turbo_graph; | 3198 FLAG_print_opt_code || FLAG_trace_turbo || FLAG_trace_turbo_graph; |
3197 #endif | 3199 #endif |
3198 Vector<const char> func_name = ArrayVector("js-to-wasm"); | 3200 Vector<const char> func_name = ArrayVector("js-to-wasm"); |
(...skipping 12 matching lines...) Expand all Loading... |
3211 if (FLAG_print_opt_code && !code.is_null()) { | 3213 if (FLAG_print_opt_code && !code.is_null()) { |
3212 OFStream os(stdout); | 3214 OFStream os(stdout); |
3213 code->Disassemble(buffer.start(), os); | 3215 code->Disassemble(buffer.start(), os); |
3214 } | 3216 } |
3215 #endif | 3217 #endif |
3216 if (debugging) { | 3218 if (debugging) { |
3217 buffer.Dispose(); | 3219 buffer.Dispose(); |
3218 } | 3220 } |
3219 | 3221 |
3220 if (isolate->logger()->is_logging_code_events() || isolate->is_profiling()) { | 3222 if (isolate->logger()->is_logging_code_events() || isolate->is_profiling()) { |
3221 RecordFunctionCompilation( | 3223 char func_name[32]; |
3222 CodeEventListener::FUNCTION_TAG, isolate, code, "js-to-wasm", index, | 3224 SNPrintF(ArrayVector(func_name), "js-to-wasm#%d", func->func_index); |
3223 wasm::WasmName("export"), | 3225 RecordFunctionCompilation(CodeEventListener::FUNCTION_TAG, isolate, code, |
3224 module->module->GetName(func->name_offset, func->name_length)); | 3226 "js-to-wasm", index, wasm::WasmName("export"), |
| 3227 CStrVector(func_name)); |
3225 } | 3228 } |
3226 return code; | 3229 return code; |
3227 } | 3230 } |
3228 | 3231 |
3229 Handle<Code> CompileWasmToJSWrapper(Isolate* isolate, Handle<JSReceiver> target, | 3232 Handle<Code> CompileWasmToJSWrapper(Isolate* isolate, Handle<JSReceiver> target, |
3230 wasm::FunctionSig* sig, uint32_t index, | 3233 wasm::FunctionSig* sig, uint32_t index, |
3231 Handle<String> module_name, | 3234 Handle<String> module_name, |
3232 MaybeHandle<String> import_name) { | 3235 MaybeHandle<String> import_name) { |
3233 //---------------------------------------------------------------------------- | 3236 //---------------------------------------------------------------------------- |
3234 // Create the Graph | 3237 // Create the Graph |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3314 } | 3317 } |
3315 // Create a TF graph during decoding. | 3318 // Create a TF graph during decoding. |
3316 | 3319 |
3317 Graph* graph = jsgraph_->graph(); | 3320 Graph* graph = jsgraph_->graph(); |
3318 CommonOperatorBuilder* common = jsgraph_->common(); | 3321 CommonOperatorBuilder* common = jsgraph_->common(); |
3319 MachineOperatorBuilder* machine = jsgraph_->machine(); | 3322 MachineOperatorBuilder* machine = jsgraph_->machine(); |
3320 SourcePositionTable* source_position_table = | 3323 SourcePositionTable* source_position_table = |
3321 new (jsgraph_->zone()) SourcePositionTable(graph); | 3324 new (jsgraph_->zone()) SourcePositionTable(graph); |
3322 WasmGraphBuilder builder(jsgraph_->zone(), jsgraph_, function_->sig, | 3325 WasmGraphBuilder builder(jsgraph_->zone(), jsgraph_, function_->sig, |
3323 source_position_table); | 3326 source_position_table); |
3324 wasm::FunctionBody body = { | 3327 const byte* module_start = module_env_->module_bytes.start(); |
3325 module_env_, function_->sig, module_env_->module->module_start, | 3328 wasm::FunctionBody body = {module_env_, function_->sig, module_start, |
3326 module_env_->module->module_start + function_->code_start_offset, | 3329 module_start + function_->code_start_offset, |
3327 module_env_->module->module_start + function_->code_end_offset}; | 3330 module_start + function_->code_end_offset}; |
3328 graph_construction_result_ = | 3331 graph_construction_result_ = |
3329 wasm::BuildTFGraph(isolate_->allocator(), &builder, body); | 3332 wasm::BuildTFGraph(isolate_->allocator(), &builder, body); |
3330 | 3333 |
3331 if (graph_construction_result_.failed()) { | 3334 if (graph_construction_result_.failed()) { |
3332 if (FLAG_trace_wasm_compiler) { | 3335 if (FLAG_trace_wasm_compiler) { |
3333 OFStream os(stdout); | 3336 OFStream os(stdout); |
3334 os << "Compilation failed: " << graph_construction_result_ << std::endl; | 3337 os << "Compilation failed: " << graph_construction_result_ << std::endl; |
3335 } | 3338 } |
3336 return nullptr; | 3339 return nullptr; |
3337 } | 3340 } |
3338 | 3341 |
3339 if (machine->Is32()) { | 3342 if (machine->Is32()) { |
3340 Int64Lowering r(graph, machine, common, jsgraph_->zone(), function_->sig); | 3343 Int64Lowering r(graph, machine, common, jsgraph_->zone(), function_->sig); |
3341 r.LowerGraph(); | 3344 r.LowerGraph(); |
3342 } | 3345 } |
3343 | 3346 |
3344 if (!CpuFeatures::SupportsSimd128()) { | 3347 if (!CpuFeatures::SupportsSimd128()) { |
3345 SimdScalarLowering(graph, machine, common, jsgraph_->zone(), function_->sig) | 3348 SimdScalarLowering(graph, machine, common, jsgraph_->zone(), function_->sig) |
3346 .LowerGraph(); | 3349 .LowerGraph(); |
3347 } | 3350 } |
3348 | 3351 |
3349 int index = static_cast<int>(function_->func_index); | 3352 int index = static_cast<int>(function_->func_index); |
3350 | 3353 |
3351 if (index >= FLAG_trace_wasm_ast_start && index < FLAG_trace_wasm_ast_end) { | 3354 if (index >= FLAG_trace_wasm_ast_start && index < FLAG_trace_wasm_ast_end) { |
3352 OFStream os(stdout); | 3355 OFStream os(stdout); |
3353 PrintAst(isolate_->allocator(), body, os, nullptr); | 3356 PrintAst(isolate_->allocator(), body, os, nullptr); |
3354 } | 3357 } |
3355 if (index >= FLAG_trace_wasm_text_start && index < FLAG_trace_wasm_text_end) { | 3358 if (index >= FLAG_trace_wasm_text_start && index < FLAG_trace_wasm_text_end) { |
3356 OFStream os(stdout); | 3359 OFStream os(stdout); |
3357 PrintWasmText(module_env_->module, function_->func_index, os, nullptr); | 3360 PrintWasmText(module_env_->module, *module_env_, function_->func_index, os, |
| 3361 nullptr); |
3358 } | 3362 } |
3359 if (FLAG_trace_wasm_decode_time) { | 3363 if (FLAG_trace_wasm_decode_time) { |
3360 *decode_ms = decode_timer.Elapsed().InMillisecondsF(); | 3364 *decode_ms = decode_timer.Elapsed().InMillisecondsF(); |
3361 } | 3365 } |
3362 return source_position_table; | 3366 return source_position_table; |
3363 } | 3367 } |
3364 | 3368 |
3365 WasmCompilationUnit::WasmCompilationUnit(wasm::ErrorThrower* thrower, | 3369 WasmCompilationUnit::WasmCompilationUnit(wasm::ErrorThrower* thrower, |
3366 Isolate* isolate, | 3370 Isolate* isolate, |
3367 wasm::ModuleEnv* module_env, | 3371 wasm::ModuleBytesEnv* module_env, |
3368 const wasm::WasmFunction* function, | 3372 const wasm::WasmFunction* function, |
3369 uint32_t index) | 3373 uint32_t index) |
3370 : thrower_(thrower), | 3374 : thrower_(thrower), |
3371 isolate_(isolate), | 3375 isolate_(isolate), |
3372 module_env_(module_env), | 3376 module_env_(module_env), |
3373 function_(function), | 3377 function_(&module_env->module->functions[index]), |
3374 graph_zone_(new Zone(isolate->allocator(), ZONE_NAME)), | 3378 graph_zone_(new Zone(isolate->allocator(), ZONE_NAME)), |
3375 jsgraph_(new (graph_zone()) JSGraph( | 3379 jsgraph_(new (graph_zone()) JSGraph( |
3376 isolate, new (graph_zone()) Graph(graph_zone()), | 3380 isolate, new (graph_zone()) Graph(graph_zone()), |
3377 new (graph_zone()) CommonOperatorBuilder(graph_zone()), nullptr, | 3381 new (graph_zone()) CommonOperatorBuilder(graph_zone()), nullptr, |
3378 nullptr, new (graph_zone()) MachineOperatorBuilder( | 3382 nullptr, new (graph_zone()) MachineOperatorBuilder( |
3379 graph_zone(), MachineType::PointerRepresentation(), | 3383 graph_zone(), MachineType::PointerRepresentation(), |
3380 InstructionSelector::SupportedMachineOperatorFlags(), | 3384 InstructionSelector::SupportedMachineOperatorFlags(), |
3381 InstructionSelector::AlignmentRequirements()))), | 3385 InstructionSelector::AlignmentRequirements()))), |
3382 compilation_zone_(isolate->allocator(), ZONE_NAME), | 3386 compilation_zone_(isolate->allocator(), ZONE_NAME), |
3383 info_(function->name_length != 0 | 3387 info_(function->name_length != 0 ? module_env->GetNameOrNull(function) |
3384 ? module_env->module->GetNameOrNull(function->name_offset, | 3388 : ArrayVector("wasm"), |
3385 function->name_length) | |
3386 : ArrayVector("wasm"), | |
3387 isolate, &compilation_zone_, | 3389 isolate, &compilation_zone_, |
3388 Code::ComputeFlags(Code::WASM_FUNCTION)), | 3390 Code::ComputeFlags(Code::WASM_FUNCTION)), |
3389 job_(), | 3391 job_(), |
3390 index_(index), | 3392 index_(index), |
3391 ok_(true) { | 3393 ok_(true) { |
3392 // Create and cache this node in the main thread. | 3394 // Create and cache this node in the main thread. |
3393 jsgraph_->CEntryStubConstant(1); | 3395 jsgraph_->CEntryStubConstant(1); |
3394 } | 3396 } |
3395 | 3397 |
3396 void WasmCompilationUnit::ExecuteCompilation() { | 3398 void WasmCompilationUnit::ExecuteCompilation() { |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3446 function_->code_start_offset), | 3448 function_->code_start_offset), |
3447 decode_ms, node_count, pipeline_ms); | 3449 decode_ms, node_count, pipeline_ms); |
3448 } | 3450 } |
3449 } | 3451 } |
3450 | 3452 |
3451 Handle<Code> WasmCompilationUnit::FinishCompilation() { | 3453 Handle<Code> WasmCompilationUnit::FinishCompilation() { |
3452 if (!ok_) { | 3454 if (!ok_) { |
3453 if (graph_construction_result_.failed()) { | 3455 if (graph_construction_result_.failed()) { |
3454 // Add the function as another context for the exception | 3456 // Add the function as another context for the exception |
3455 ScopedVector<char> buffer(128); | 3457 ScopedVector<char> buffer(128); |
3456 wasm::WasmName name = module_env_->module->GetName( | 3458 wasm::WasmName name = module_env_->GetName(function_); |
3457 function_->name_offset, function_->name_length); | |
3458 SNPrintF(buffer, "Compiling WASM function #%d:%.*s failed:", | 3459 SNPrintF(buffer, "Compiling WASM function #%d:%.*s failed:", |
3459 function_->func_index, name.length(), name.start()); | 3460 function_->func_index, name.length(), name.start()); |
3460 thrower_->CompileFailed(buffer.start(), graph_construction_result_); | 3461 thrower_->CompileFailed(buffer.start(), graph_construction_result_); |
3461 } | 3462 } |
3462 | 3463 |
3463 return Handle<Code>::null(); | 3464 return Handle<Code>::null(); |
3464 } | 3465 } |
3465 if (job_->FinalizeJob() != CompilationJob::SUCCEEDED) { | 3466 if (job_->FinalizeJob() != CompilationJob::SUCCEEDED) { |
3466 return Handle<Code>::null(); | 3467 return Handle<Code>::null(); |
3467 } | 3468 } |
3468 base::ElapsedTimer compile_timer; | 3469 base::ElapsedTimer compile_timer; |
3469 if (FLAG_trace_wasm_decode_time) { | 3470 if (FLAG_trace_wasm_decode_time) { |
3470 compile_timer.Start(); | 3471 compile_timer.Start(); |
3471 } | 3472 } |
3472 Handle<Code> code = info_.code(); | 3473 Handle<Code> code = info_.code(); |
3473 DCHECK(!code.is_null()); | 3474 DCHECK(!code.is_null()); |
3474 | 3475 |
3475 if (isolate_->logger()->is_logging_code_events() || | 3476 if (isolate_->logger()->is_logging_code_events() || |
3476 isolate_->is_profiling()) { | 3477 isolate_->is_profiling()) { |
3477 RecordFunctionCompilation( | 3478 RecordFunctionCompilation(CodeEventListener::FUNCTION_TAG, isolate_, code, |
3478 CodeEventListener::FUNCTION_TAG, isolate_, code, "WASM_function", | 3479 "WASM_function", function_->func_index, |
3479 function_->func_index, wasm::WasmName("module"), | 3480 wasm::WasmName("module"), |
3480 module_env_->module->GetName(function_->name_offset, | 3481 module_env_->GetName(function_)); |
3481 function_->name_length)); | |
3482 } | 3482 } |
3483 | 3483 |
3484 if (FLAG_trace_wasm_decode_time) { | 3484 if (FLAG_trace_wasm_decode_time) { |
3485 double compile_ms = compile_timer.Elapsed().InMillisecondsF(); | 3485 double compile_ms = compile_timer.Elapsed().InMillisecondsF(); |
3486 PrintF("wasm-code-generation ok: %d bytes, %0.3f ms code generation\n", | 3486 PrintF("wasm-code-generation ok: %d bytes, %0.3f ms code generation\n", |
3487 static_cast<int>(function_->code_end_offset - | 3487 static_cast<int>(function_->code_end_offset - |
3488 function_->code_start_offset), | 3488 function_->code_start_offset), |
3489 compile_ms); | 3489 compile_ms); |
3490 } | 3490 } |
3491 | 3491 |
3492 return code; | 3492 return code; |
3493 } | 3493 } |
3494 | 3494 |
3495 } // namespace compiler | 3495 } // namespace compiler |
3496 } // namespace internal | 3496 } // namespace internal |
3497 } // namespace v8 | 3497 } // namespace v8 |
OLD | NEW |