v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
turbofan-graph-visualizer.h
Go to the documentation of this file.
1// Copyright 2013 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef V8_COMPILER_TURBOFAN_GRAPH_VISUALIZER_H_
6#define V8_COMPILER_TURBOFAN_GRAPH_VISUALIZER_H_
7
8#include <stdio.h>
9
10#include <fstream>
11#include <iosfwd>
12#include <memory>
13#include <optional>
14#include <vector>
15
16#include "src/common/globals.h"
17#include "src/handles/handles.h"
18#include "src/objects/code.h"
19
20namespace v8 {
21namespace internal {
22
23class OptimizedCompilationInfo;
24class SharedFunctionInfo;
25class SourcePosition;
26struct WasmInliningPosition;
27
28namespace wasm {
29struct WasmModule;
30class WireBytesStorage;
31} // namespace wasm
32
33namespace compiler {
34
35class TFGraph;
36class LiveRange;
37class TopLevelLiveRange;
38class Instruction;
39class InstructionBlock;
40class InstructionOperand;
41class InstructionSequence;
42class Node;
43class NodeOrigin;
44class NodeOriginTable;
45class RegisterAllocationData;
46class Schedule;
47class SourcePositionTable;
48class Type;
49
51 public:
52 template <typename T>
53 explicit JSONEscaped(const T& value) {
54 std::ostringstream s;
55 s << value;
56 str_ = s.str();
57 }
58 explicit JSONEscaped(std::string str) : str_(std::move(str)) {}
59 explicit JSONEscaped(const std::ostringstream& os) : str_(os.str()) {}
60
61 friend std::ostream& operator<<(std::ostream& os, const JSONEscaped& e) {
62 for (char c : e.str_) PipeCharacter(os, c);
63 return os;
64 }
65
66 private:
67 static std::ostream& PipeCharacter(std::ostream& os, char c) {
68 if (c == '"') return os << "\\\"";
69 if (c == '\\') return os << "\\\\";
70 if (c == '\b') return os << "\\b";
71 if (c == '\f') return os << "\\f";
72 if (c == '\n') return os << "\\n";
73 if (c == '\r') return os << "\\r";
74 if (c == '\t') return os << "\\t";
75 return os << c;
76 }
77
78 std::string str_;
79};
80
81struct V8_EXPORT_PRIVATE TurboJsonFile : public std::ofstream {
82 TurboJsonFile(OptimizedCompilationInfo* info, std::ios_base::openmode mode);
83 ~TurboJsonFile() override;
84};
85
86struct TurboCfgFile : public std::ofstream {
87 explicit TurboCfgFile(Isolate* isolate = nullptr);
88 ~TurboCfgFile() override;
89};
90
95
98 return SourcePositionAsJSON(sp);
99}
100
102 explicit NodeOriginAsJSON(const NodeOrigin& no) : no(no) {}
104};
105
109
110std::ostream& operator<<(std::ostream& out, const SourcePositionAsJSON& pos);
111std::ostream& operator<<(std::ostream& out, const NodeOriginAsJSON& asJSON);
112
113// Small helper that deduplicates SharedFunctionInfos.
115 public:
116 explicit SourceIdAssigner(size_t size) {
117 printed_.reserve(size);
118 source_ids_.reserve(size);
119 }
120 int GetIdFor(Handle<SharedFunctionInfo> shared);
121 int GetIdAt(size_t pos) const { return source_ids_[pos]; }
122
123 private:
124 std::vector<Handle<SharedFunctionInfo>> printed_;
125 std::vector<int> source_ids_;
126};
127
128void JsonPrintAllBytecodeSources(std::ostream& os,
130
131void JsonPrintBytecodeSource(std::ostream& os, int source_id,
132 std::unique_ptr<char[]> function_name,
133 DirectHandle<BytecodeArray> bytecode_array,
134 Tagged<FeedbackVector> feedback_vector = {});
135
136void JsonPrintAllSourceWithPositions(std::ostream& os,
138 Isolate* isolate);
139
140#if V8_ENABLE_WEBASSEMBLY
141void JsonPrintAllSourceWithPositionsWasm(
142 std::ostream& os, const wasm::WasmModule* module,
143 const wasm::WireBytesStorage* wire_bytes,
145#endif
146
147void JsonPrintFunctionSource(std::ostream& os, int source_id,
148 std::unique_ptr<char[]> function_name,
149 DirectHandle<Script> script, Isolate* isolate,
151 bool with_key = false);
152
153std::unique_ptr<char[]> GetVisualizerLogFileName(OptimizedCompilationInfo* info,
154 const char* optional_base_dir,
155 const char* phase,
156 const char* suffix);
157
159 public:
160 JSONGraphWriter(std::ostream& os, const TFGraph* graph,
161 const SourcePositionTable* positions,
162 const NodeOriginTable* origins);
163
166
167 void PrintPhase(const char* phase_name);
168 void Print();
169
170 protected:
171 void PrintNode(Node* node, bool is_live);
172 void PrintEdges(Node* node);
173 void PrintEdge(Node* from, int index, Node* to);
174 virtual std::optional<Type> GetType(Node* node);
175
176 protected:
177 std::ostream& os_;
184};
185
188 : graph(g), positions(p), origins(o) {}
192};
193
199
200V8_EXPORT_PRIVATE std::ostream& operator<<(std::ostream& os,
201 const GraphAsJSON& ad);
202
203struct AsRPO {
204 explicit AsRPO(const TFGraph& g) : graph(g) {}
206};
207
208V8_EXPORT_PRIVATE std::ostream& operator<<(std::ostream& os, const AsRPO& ad);
209
215
220
221std::ostream& operator<<(std::ostream& os, const AsScheduledGraph& scheduled);
222struct AsC1V {
223 AsC1V(const char* phase, const Schedule* schedule,
224 const SourcePositionTable* positions = nullptr,
225 const InstructionSequence* instructions = nullptr)
227 instructions_(instructions),
228 positions_(positions),
229 phase_(phase) {}
233 const char* phase_;
234};
235
238 const char* phase, const RegisterAllocationData* data = nullptr)
239 : phase_(phase), data_(data) {}
240 const char* phase_;
242};
243
244std::ostream& operator<<(std::ostream& os, const AsC1VCompilation& ac);
245std::ostream& operator<<(std::ostream& os, const AsC1V& ac);
246std::ostream& operator<<(std::ostream& os,
248
253
254std::ostream& operator<<(std::ostream& os,
255 const LiveRangeAsJSON& live_range_json);
256
261
262std::ostream& operator<<(
263 std::ostream& os, const TopLevelLiveRangeAsJSON& top_level_live_range_json);
264
269
270std::ostream& operator<<(std::ostream& os,
272
277
278std::ostream& operator<<(std::ostream& os, const InstructionOperandAsJSON& o);
279
285std::ostream& operator<<(std::ostream& os, const InstructionAsJSON& i);
286
291
292std::ostream& operator<<(std::ostream& os, const InstructionBlockAsJSON& b);
293
297std::ostream& operator<<(std::ostream& os, const InstructionSequenceAsJSON& s);
298
299} // namespace compiler
300} // namespace internal
301} // namespace v8
302
303#endif // V8_COMPILER_TURBOFAN_GRAPH_VISUALIZER_H_
Schedule * schedule
SourcePosition pos
JSONEscaped(const std::ostringstream &os)
friend std::ostream & operator<<(std::ostream &os, const JSONEscaped &e)
static std::ostream & PipeCharacter(std::ostream &os, char c)
JSONGraphWriter(const JSONGraphWriter &)=delete
JSONGraphWriter(std::ostream &os, const TFGraph *graph, const SourcePositionTable *positions, const NodeOriginTable *origins)
JSONGraphWriter & operator=(const JSONGraphWriter &)=delete
virtual std::optional< Type > GetType(Node *node)
void PrintEdge(Node *from, int index, Node *to)
std::vector< Handle< SharedFunctionInfo > > printed_
NodeOriginTable * origins
int s
Definition mul-fft.cc:297
STL namespace.
void JsonPrintBytecodeSource(std::ostream &os, int source_id, std::unique_ptr< char[]> function_name, DirectHandle< BytecodeArray > bytecode_array, Tagged< FeedbackVector > feedback_vector)
void JsonPrintAllBytecodeSources(std::ostream &os, OptimizedCompilationInfo *info)
void JsonPrintAllSourceWithPositions(std::ostream &os, OptimizedCompilationInfo *info, Isolate *isolate)
void JsonPrintFunctionSource(std::ostream &os, int source_id, std::unique_ptr< char[]> function_name, DirectHandle< Script > script, Isolate *isolate, DirectHandle< SharedFunctionInfo > shared, bool with_key)
std::unique_ptr< char[]> GetVisualizerLogFileName(OptimizedCompilationInfo *info, const char *optional_base_dir, const char *phase, const char *suffix)
V8_INLINE V8_EXPORT_PRIVATE SourcePositionAsJSON AsJSON(const SourcePosition &sp)
std::ostream & operator<<(std::ostream &os, AccessMode access_mode)
wasm::WasmModule WasmModule
return value
Definition map-inl.h:893
Definition c-api.cc:87
#define V8_EXPORT_PRIVATE
Definition macros.h:460
AsC1VCompilation(const OptimizedCompilationInfo *info)
AsC1VRegisterAllocationData(const char *phase, const RegisterAllocationData *data=nullptr)
const SourcePositionTable * positions_
AsC1V(const char *phase, const Schedule *schedule, const SourcePositionTable *positions=nullptr, const InstructionSequence *instructions=nullptr)
const InstructionSequence * instructions_
GraphAsJSON(const TFGraph &g, SourcePositionTable *p, NodeOriginTable *o)
#define V8_INLINE
Definition v8config.h:500