From a56900a9753bf898e08202d8d0341303b6ede08a Mon Sep 17 00:00:00 2001 From: "Ben L. Titzer" Date: Mon, 24 Nov 2014 12:54:59 +0100 Subject: [PATCH] [turbofan] Dump graph in RPO order as text. R=bmeurer@chromium.org BUG= Review URL: https://codereview.chromium.org/754803002 Cr-Commit-Position: refs/heads/master@{#25477} --- src/compiler/graph-visualizer.cc | 40 ++++++++++++++++++++++++++++++++ src/compiler/graph-visualizer.h | 8 +++++++ src/compiler/pipeline.cc | 39 +++++++++++++++++++------------ src/flag-definitions.h | 2 ++ 4 files changed, 74 insertions(+), 15 deletions(-) diff --git a/src/compiler/graph-visualizer.cc b/src/compiler/graph-visualizer.cc index c9be0ac5ac..032bc9fac0 100644 --- a/src/compiler/graph-visualizer.cc +++ b/src/compiler/graph-visualizer.cc @@ -27,6 +27,9 @@ namespace internal { namespace compiler { static int SafeId(Node* node) { return node == NULL ? -1 : node->id(); } +static const char* SafeMnemonic(Node* node) { + return node == NULL ? "null" : node->op()->mnemonic(); +} #define DEAD_COLOR "#999999" @@ -790,6 +793,43 @@ std::ostream& operator<<(std::ostream& os, const AsC1VAllocator& ac) { GraphC1Visualizer(os, &tmp_zone).PrintAllocator(ac.phase_, ac.allocator_); return os; } + +const int kUnvisited = 0; +const int kOnStack = 1; +const int kVisited = 2; + +std::ostream& operator<<(std::ostream& os, const AsRPO& ar) { + Zone local_zone(ar.graph.zone()->isolate()); + ZoneVector state(ar.graph.NodeCount(), kUnvisited, &local_zone); + ZoneStack stack(&local_zone); + + stack.push(ar.graph.end()); + state[ar.graph.end()->id()] = kOnStack; + while (!stack.empty()) { + Node* n = stack.top(); + bool pop = true; + for (Node* const i : n->inputs()) { + if (state[i->id()] == kUnvisited) { + state[i->id()] = kOnStack; + stack.push(i); + pop = false; + break; + } + } + if (pop) { + state[n->id()] = kVisited; + stack.pop(); + os << "#" << SafeId(n) << ":" << SafeMnemonic(n) << "("; + int j = 0; + for (Node* const i : n->inputs()) { + if (j++ > 0) os << ", "; + os << "#" << SafeId(i) << ":" << SafeMnemonic(i); + } + os << ")" << std::endl; + } + } + return os; +} } } } // namespace v8::internal::compiler diff --git a/src/compiler/graph-visualizer.h b/src/compiler/graph-visualizer.h index 7212a4f932..3dd66eaf41 100644 --- a/src/compiler/graph-visualizer.h +++ b/src/compiler/graph-visualizer.h @@ -36,6 +36,14 @@ struct AsJSON { std::ostream& operator<<(std::ostream& os, const AsJSON& ad); +struct AsRPO { + explicit AsRPO(const Graph& g) : graph(g) {} + const Graph& graph; +}; + +std::ostream& operator<<(std::ostream& os, const AsRPO& ad); + + struct AsC1VCompilation { explicit AsC1VCompilation(const CompilationInfo* info) : info_(info) {} const CompilationInfo* info_; diff --git a/src/compiler/pipeline.cc b/src/compiler/pipeline.cc index d9a9ec49d2..3a0d27b059 100644 --- a/src/compiler/pipeline.cc +++ b/src/compiler/pipeline.cc @@ -611,25 +611,34 @@ struct PrintGraphPhase { std::replace(filename.start(), filename.start() + filename.length(), ' ', '_'); - char dot_buffer[256]; - Vector dot_filename(dot_buffer, sizeof(dot_buffer)); - SNPrintF(dot_filename, "%s.dot", filename.start()); - FILE* dot_file = base::OS::FOpen(dot_filename.start(), "w+"); - OFStream dot_of(dot_file); - dot_of << AsDOT(*graph); - fclose(dot_file); + { // Print dot. + char dot_buffer[256]; + Vector dot_filename(dot_buffer, sizeof(dot_buffer)); + SNPrintF(dot_filename, "%s.dot", filename.start()); + FILE* dot_file = base::OS::FOpen(dot_filename.start(), "w+"); + OFStream dot_of(dot_file); + dot_of << AsDOT(*graph); + fclose(dot_file); + } - char json_buffer[256]; - Vector json_filename(json_buffer, sizeof(json_buffer)); - SNPrintF(json_filename, "%s.json", filename.start()); - FILE* json_file = base::OS::FOpen(json_filename.start(), "w+"); - OFStream json_of(json_file); - json_of << AsJSON(*graph); - fclose(json_file); + { // Print JSON. + char json_buffer[256]; + Vector json_filename(json_buffer, sizeof(json_buffer)); + SNPrintF(json_filename, "%s.json", filename.start()); + FILE* json_file = base::OS::FOpen(json_filename.start(), "w+"); + OFStream json_of(json_file); + json_of << AsJSON(*graph); + fclose(json_file); + } OFStream os(stdout); + if (FLAG_trace_turbo_graph) { // Simple textual RPO. + os << "-- Graph after " << phase << " -- " << std::endl; + os << AsRPO(*graph); + } + os << "-- " << phase << " graph printed to file " << filename.start() - << "\n"; + << std::endl; } }; diff --git a/src/flag-definitions.h b/src/flag-definitions.h index bc7354c5b2..4d22d838bb 100644 --- a/src/flag-definitions.h +++ b/src/flag-definitions.h @@ -366,6 +366,8 @@ DEFINE_BOOL(omit_map_checks_for_leaf_maps, true, // Flags for TurboFan. DEFINE_STRING(turbo_filter, "~", "optimization filter for TurboFan compiler") DEFINE_BOOL(trace_turbo, false, "trace generated TurboFan IR") +DEFINE_BOOL(trace_turbo_graph, false, "trace generated TurboFan graphs") +DEFINE_IMPLICATION(trace_turbo_graph, trace_turbo) DEFINE_STRING(trace_turbo_cfg_file, NULL, "trace turbo cfg graph (for C1 visualizer) to a given file name") DEFINE_BOOL(trace_turbo_types, true, "trace TurboFan's types")