1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
|
// Copyright 2022 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "src/compiler/graph-visualizer.h"
#include "src/base/small-vector.h"
#include "src/compiler/node-origin-table.h"
#include "src/compiler/turboshaft/graph-visualizer.h"
namespace v8::internal::compiler::turboshaft {
JSONTurboshaftGraphWriter::JSONTurboshaftGraphWriter(
std::ostream& os, const Graph& turboshaft_graph, NodeOriginTable* origins,
Zone* zone)
: os_(os),
zone_(zone),
turboshaft_graph_(turboshaft_graph),
origins_(origins) {}
void JSONTurboshaftGraphWriter::Print() {
os_ << "{\n\"nodes\":[";
PrintNodes();
os_ << "\n],\n\"edges\":[";
PrintEdges();
os_ << "\n],\n\"blocks\":[";
PrintBlocks();
os_ << "\n]}";
}
void JSONTurboshaftGraphWriter::PrintNodes() {
bool first = true;
for (const Block& block : turboshaft_graph_.blocks()) {
for (const Operation& op : turboshaft_graph_.operations(block)) {
OpIndex index = turboshaft_graph_.Index(op);
if (!first) os_ << ",\n";
first = false;
os_ << "{\"id\":" << index.id() << ",";
os_ << "\"title\":\"" << OpcodeName(op.opcode) << "\",";
os_ << "\"block_id\":" << block.index().id() << ",";
os_ << "\"op_effects\":\"" << op.Effects() << "\"";
if (origins_) {
NodeOrigin origin = origins_->GetNodeOrigin(index.id());
if (origin.IsKnown()) {
os_ << ", \"origin\":" << AsJSON(origin);
}
}
SourcePosition position = turboshaft_graph_.source_positions()[index];
if (position.IsKnown()) {
os_ << ", \"sourcePosition\":" << compiler::AsJSON(position);
}
os_ << "}";
}
}
}
void JSONTurboshaftGraphWriter::PrintEdges() {
bool first = true;
for (const Block& block : turboshaft_graph_.blocks()) {
for (const Operation& op : turboshaft_graph_.operations(block)) {
int target_id = turboshaft_graph_.Index(op).id();
base::SmallVector<OpIndex, 32> inputs{op.inputs()};
// Reorder the inputs to correspond to the order used in constructor and
// assembler functions.
if (auto* store = op.TryCast<StoreOp>()) {
if (store->index().valid()) {
DCHECK_EQ(store->input_count, 3);
inputs = {store->base(), store->index(), store->value()};
}
}
for (OpIndex input : inputs) {
if (!first) os_ << ",\n";
first = false;
os_ << "{\"source\":" << input.id() << ",";
os_ << "\"target\":" << target_id << "}";
}
}
}
}
void JSONTurboshaftGraphWriter::PrintBlocks() {
bool first_block = true;
for (const Block& block : turboshaft_graph_.blocks()) {
if (!first_block) os_ << ",\n";
first_block = false;
os_ << "{\"id\":" << block.index().id() << ",";
os_ << "\"type\":\"" << block.kind() << "\",";
os_ << "\"predecessors\":[";
bool first_predecessor = true;
for (const Block* pred : block.Predecessors()) {
if (!first_predecessor) os_ << ", ";
first_predecessor = false;
os_ << pred->index().id();
}
os_ << "]}";
}
}
std::ostream& operator<<(std::ostream& os, const TurboshaftGraphAsJSON& ad) {
JSONTurboshaftGraphWriter writer(os, ad.turboshaft_graph, ad.origins,
ad.temp_zone);
writer.Print();
return os;
}
void PrintTurboshaftCustomDataPerOperation(
OptimizedCompilationInfo* info, const char* data_name, const Graph& graph,
std::function<bool(std::ostream&, const Graph&, OpIndex)> printer) {
DCHECK(printer);
TurboJsonFile json_of(info, std::ios_base::app);
json_of << "{\"name\":\"" << data_name
<< "\", \"type\":\"turboshaft_custom_data\", "
"\"data_target\":\"operations\", \"data\":[";
bool first = true;
for (auto index : graph.AllOperationIndices()) {
std::stringstream stream;
if (printer(stream, graph, index)) {
json_of << (first ? "\n" : ",\n") << "{\"key\":" << index.id()
<< ", \"value\":\"" << stream.str() << "\"}";
first = false;
}
}
json_of << "]},\n";
}
void PrintTurboshaftCustomDataPerBlock(
OptimizedCompilationInfo* info, const char* data_name, const Graph& graph,
std::function<bool(std::ostream&, const Graph&, BlockIndex)> printer) {
DCHECK(printer);
TurboJsonFile json_of(info, std::ios_base::app);
json_of << "{\"name\":\"" << data_name
<< "\", \"type\":\"turboshaft_custom_data\", "
"\"data_target\":\"blocks\", \"data\":[";
bool first = true;
for (const Block& block : graph.blocks()) {
std::stringstream stream;
BlockIndex index = block.index();
if (printer(stream, graph, index)) {
json_of << (first ? "\n" : ",\n") << "{\"key\":" << index.id()
<< ", \"value\":\"" << stream.str() << "\"}";
first = false;
}
}
json_of << "]},\n";
}
} // namespace v8::internal::compiler::turboshaft
|