Commit 03217f4b authored by Stephan Herhut's avatar Stephan Herhut Committed by Commit Bot

[wasm] Emit wasm disassembly for --trace-turbo

This adds basic support to emit wasm disassembly to the json files
digested by turbolizer.

Change-Id: Icd8fc92e9539dc336879ef6da76e31890b95e40e
Reviewed-on: https://chromium-review.googlesource.com/1069275
Commit-Queue: Stephan Herhut <herhut@chromium.org>
Reviewed-by: 's avatarMichael Starzinger <mstarzinger@chromium.org>
Reviewed-by: 's avatarSigurd Schneider <sigurds@chromium.org>
Cr-Commit-Position: refs/heads/master@{#53323}
parent a6c71508
...@@ -77,6 +77,7 @@ ...@@ -77,6 +77,7 @@
#include "src/parsing/parse-info.h" #include "src/parsing/parse-info.h"
#include "src/register-configuration.h" #include "src/register-configuration.h"
#include "src/utils.h" #include "src/utils.h"
#include "src/wasm/function-body-decoder.h"
namespace v8 { namespace v8 {
namespace internal { namespace internal {
...@@ -739,6 +740,35 @@ PipelineStatistics* CreatePipelineStatistics(Handle<Script> script, ...@@ -739,6 +740,35 @@ PipelineStatistics* CreatePipelineStatistics(Handle<Script> script,
return pipeline_statistics; return pipeline_statistics;
} }
PipelineStatistics* CreatePipelineStatistics(wasm::FunctionBody function_body,
wasm::WasmModule* wasm_module,
OptimizedCompilationInfo* info,
Isolate* isolate,
ZoneStats* zone_stats) {
PipelineStatistics* pipeline_statistics = nullptr;
if (FLAG_turbo_stats || FLAG_turbo_stats_nvp) {
pipeline_statistics = new PipelineStatistics(info, isolate, zone_stats);
pipeline_statistics->BeginPhaseKind("initializing");
}
if (info->trace_turbo_json_enabled()) {
TurboJsonFile json_of(info, std::ios_base::trunc);
std::unique_ptr<char[]> function_name = info->GetDebugName();
json_of << "{\"function\":\"" << function_name.get() << "\", \"source\":\"";
AccountingAllocator allocator;
std::ostringstream disassembly;
wasm::PrintRawWasmCode(&allocator, function_body, wasm_module,
wasm::kPrintLocals, disassembly);
for (const auto& c : disassembly.str()) {
json_of << AsEscapedUC16ForJSON(c);
}
json_of << "\",\n\"phases\":[";
}
return pipeline_statistics;
}
} // namespace } // namespace
class PipelineCompilationJob final : public OptimizedCompilationJob { class PipelineCompilationJob final : public OptimizedCompilationJob {
...@@ -897,12 +927,13 @@ class PipelineWasmCompilationJob final : public OptimizedCompilationJob { ...@@ -897,12 +927,13 @@ class PipelineWasmCompilationJob final : public OptimizedCompilationJob {
OptimizedCompilationInfo* info, Isolate* isolate, MachineGraph* mcgraph, OptimizedCompilationInfo* info, Isolate* isolate, MachineGraph* mcgraph,
CallDescriptor* call_descriptor, SourcePositionTable* source_positions, CallDescriptor* call_descriptor, SourcePositionTable* source_positions,
NodeOriginTable* node_origins, WasmCompilationData* wasm_compilation_data, NodeOriginTable* node_origins, WasmCompilationData* wasm_compilation_data,
wasm::FunctionBody function_body, wasm::WasmModule* wasm_module,
bool asmjs_origin) bool asmjs_origin)
: OptimizedCompilationJob(isolate->stack_guard()->real_climit(), info, : OptimizedCompilationJob(isolate->stack_guard()->real_climit(), info,
"TurboFan", State::kReadyToExecute), "TurboFan", State::kReadyToExecute),
zone_stats_(isolate->allocator()), zone_stats_(isolate->allocator()),
pipeline_statistics_(CreatePipelineStatistics( pipeline_statistics_(CreatePipelineStatistics(
Handle<Script>::null(), info, isolate, &zone_stats_)), function_body, wasm_module, info, isolate, &zone_stats_)),
data_(&zone_stats_, isolate, info, mcgraph, pipeline_statistics_.get(), data_(&zone_stats_, isolate, info, mcgraph, pipeline_statistics_.get(),
source_positions, node_origins, wasm_compilation_data), source_positions, node_origins, wasm_compilation_data),
pipeline_(&data_), pipeline_(&data_),
...@@ -939,13 +970,7 @@ PipelineWasmCompilationJob::Status PipelineWasmCompilationJob::PrepareJobImpl( ...@@ -939,13 +970,7 @@ PipelineWasmCompilationJob::Status PipelineWasmCompilationJob::PrepareJobImpl(
PipelineWasmCompilationJob::Status PipelineWasmCompilationJob::Status
PipelineWasmCompilationJob::ExecuteJobImpl() { PipelineWasmCompilationJob::ExecuteJobImpl() {
if (compilation_info()->trace_turbo_json_enabled()) { pipeline_.RunPrintAndVerify("Machine", true);
TurboJsonFile json_of(compilation_info(), std::ios_base::trunc);
json_of << "{\"function\":\"" << compilation_info()->GetDebugName().get()
<< "\", \"source\":\"\",\n\"phases\":[";
}
pipeline_.RunPrintAndVerify("machine", true);
if (FLAG_wasm_opt || asmjs_origin_) { if (FLAG_wasm_opt || asmjs_origin_) {
PipelineData* data = &data_; PipelineData* data = &data_;
PipelineRunScope scope(data, "wasm optimization"); PipelineRunScope scope(data, "wasm optimization");
...@@ -990,6 +1015,13 @@ PipelineWasmCompilationJob::Status PipelineWasmCompilationJob::FinalizeJobImpl( ...@@ -990,6 +1015,13 @@ PipelineWasmCompilationJob::Status PipelineWasmCompilationJob::FinalizeJobImpl(
code_generator->frame()->GetTotalFrameSlotCount(); code_generator->frame()->GetTotalFrameSlotCount();
wasm_code_desc->source_positions_table = wasm_code_desc->source_positions_table =
code_generator->GetSourcePositionTable(); code_generator->GetSourcePositionTable();
if (data_.info()->trace_turbo_json_enabled()) {
TurboJsonFile json_of(data_.info(), std::ios_base::app);
json_of << "{}\n]";
json_of << "\n}";
}
return SUCCEEDED; return SUCCEEDED;
} }
...@@ -2116,10 +2148,11 @@ OptimizedCompilationJob* Pipeline::NewWasmCompilationJob( ...@@ -2116,10 +2148,11 @@ OptimizedCompilationJob* Pipeline::NewWasmCompilationJob(
OptimizedCompilationInfo* info, Isolate* isolate, MachineGraph* mcgraph, OptimizedCompilationInfo* info, Isolate* isolate, MachineGraph* mcgraph,
CallDescriptor* call_descriptor, SourcePositionTable* source_positions, CallDescriptor* call_descriptor, SourcePositionTable* source_positions,
NodeOriginTable* node_origins, WasmCompilationData* wasm_compilation_data, NodeOriginTable* node_origins, WasmCompilationData* wasm_compilation_data,
wasm::FunctionBody function_body, wasm::WasmModule* wasm_module,
wasm::ModuleOrigin asmjs_origin) { wasm::ModuleOrigin asmjs_origin) {
return new PipelineWasmCompilationJob(info, isolate, mcgraph, call_descriptor, return new PipelineWasmCompilationJob(
source_positions, node_origins, info, isolate, mcgraph, call_descriptor, source_positions, node_origins,
wasm_compilation_data, asmjs_origin); wasm_compilation_data, function_body, wasm_module, asmjs_origin);
} }
bool Pipeline::AllocateRegistersForTesting(const RegisterConfiguration* config, bool Pipeline::AllocateRegistersForTesting(const RegisterConfiguration* config,
......
...@@ -22,6 +22,8 @@ class JumpOptimizationInfo; ...@@ -22,6 +22,8 @@ class JumpOptimizationInfo;
namespace wasm { namespace wasm {
enum ModuleOrigin : uint8_t; enum ModuleOrigin : uint8_t;
struct FunctionBody;
struct WasmModule;
} // namespace wasm } // namespace wasm
namespace compiler { namespace compiler {
...@@ -46,6 +48,7 @@ class Pipeline : public AllStatic { ...@@ -46,6 +48,7 @@ class Pipeline : public AllStatic {
OptimizedCompilationInfo* info, Isolate* isolate, MachineGraph* mcgraph, OptimizedCompilationInfo* info, Isolate* isolate, MachineGraph* mcgraph,
CallDescriptor* call_descriptor, SourcePositionTable* source_positions, CallDescriptor* call_descriptor, SourcePositionTable* source_positions,
NodeOriginTable* node_origins, WasmCompilationData* wasm_compilation_data, NodeOriginTable* node_origins, WasmCompilationData* wasm_compilation_data,
wasm::FunctionBody function_body, wasm::WasmModule* wasm_module,
wasm::ModuleOrigin wasm_origin); wasm::ModuleOrigin wasm_origin);
// Run the pipeline on a machine graph and generate code. The {schedule} must // Run the pipeline on a machine graph and generate code. The {schedule} must
......
...@@ -5111,7 +5111,7 @@ void TurbofanWasmCompilationUnit::ExecuteCompilation() { ...@@ -5111,7 +5111,7 @@ void TurbofanWasmCompilationUnit::ExecuteCompilation() {
wasm_unit_->func_index_), wasm_unit_->func_index_),
compilation_zone_.get(), Code::WASM_FUNCTION)); compilation_zone_.get(), Code::WASM_FUNCTION));
NodeOriginTable* node_origins = info_->trace_turbo_graph_enabled() NodeOriginTable* node_origins = info_->trace_turbo_json_enabled()
? new (&graph_zone) ? new (&graph_zone)
NodeOriginTable(mcgraph_->graph()) NodeOriginTable(mcgraph_->graph())
: nullptr; : nullptr;
...@@ -5119,6 +5119,8 @@ void TurbofanWasmCompilationUnit::ExecuteCompilation() { ...@@ -5119,6 +5119,8 @@ void TurbofanWasmCompilationUnit::ExecuteCompilation() {
job_.reset(Pipeline::NewWasmCompilationJob( job_.reset(Pipeline::NewWasmCompilationJob(
info_.get(), wasm_unit_->isolate_, mcgraph_, call_descriptor, info_.get(), wasm_unit_->isolate_, mcgraph_, call_descriptor,
source_positions, node_origins, &wasm_compilation_data_, source_positions, node_origins, &wasm_compilation_data_,
wasm_unit_->func_body_,
const_cast<wasm::WasmModule*>(wasm_unit_->env_->module),
wasm_unit_->env_->module->origin())); wasm_unit_->env_->module->origin()));
ok_ = job_->ExecuteJob() == CompilationJob::SUCCEEDED; ok_ = job_->ExecuteJob() == CompilationJob::SUCCEEDED;
// TODO(bradnelson): Improve histogram handling of size_t. // TODO(bradnelson): Improve histogram handling of size_t.
......
...@@ -901,6 +901,12 @@ bool PrintRawWasmCode(AccountingAllocator* allocator, const FunctionBody& body, ...@@ -901,6 +901,12 @@ bool PrintRawWasmCode(AccountingAllocator* allocator, const FunctionBody& body,
const wasm::WasmModule* module, const wasm::WasmModule* module,
PrintLocals print_locals) { PrintLocals print_locals) {
OFStream os(stdout); OFStream os(stdout);
return PrintRawWasmCode(allocator, body, module, print_locals, os);
}
bool PrintRawWasmCode(AccountingAllocator* allocator, const FunctionBody& body,
const wasm::WasmModule* module, PrintLocals print_locals,
std::ostream& os) {
Zone zone(allocator, ZONE_NAME); Zone zone(allocator, ZONE_NAME);
WasmDecoder<Decoder::kNoValidate> decoder(module, body.sig, body.start, WasmDecoder<Decoder::kNoValidate> decoder(module, body.sig, body.start,
body.end); body.end);
......
...@@ -58,6 +58,11 @@ V8_EXPORT_PRIVATE ...@@ -58,6 +58,11 @@ V8_EXPORT_PRIVATE
bool PrintRawWasmCode(AccountingAllocator* allocator, const FunctionBody& body, bool PrintRawWasmCode(AccountingAllocator* allocator, const FunctionBody& body,
const wasm::WasmModule* module, PrintLocals print_locals); const wasm::WasmModule* module, PrintLocals print_locals);
V8_EXPORT_PRIVATE
bool PrintRawWasmCode(AccountingAllocator* allocator, const FunctionBody& body,
const wasm::WasmModule* module, PrintLocals print_locals,
std::ostream& out);
// A simplified form of AST printing, e.g. from a debugger. // A simplified form of AST printing, e.g. from a debugger.
void PrintRawWasmCode(const byte* start, const byte* end); void PrintRawWasmCode(const byte* start, const byte* end);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment