Use friendly names for Ids
authorDavid Neto <dneto@google.com>
Wed, 10 Aug 2016 19:28:11 +0000 (15:28 -0400)
committerDavid Neto <dneto@google.com>
Thu, 11 Aug 2016 15:30:18 +0000 (11:30 -0400)
tools/cfg/bin_to_dot.cpp

index 41e7ea5..841d1f7 100644 (file)
@@ -31,6 +31,7 @@
 #include <vector>
 
 #include "assembly_grammar.h"
+#include "name_mapper.h"
 
 namespace {
 
@@ -41,7 +42,8 @@ const char* kContinueStyle = "style=dotted";
 // a SPIR-V module.
 class DotConverter {
  public:
-  DotConverter(std::iostream* out) : out_(*out) {}
+  DotConverter(libspirv::NameMapper name_mapper, std::iostream* out)
+      : name_mapper_(name_mapper), out_(*out) {}
 
   // Emits the graph preamble.
   void Begin() const {
@@ -81,6 +83,9 @@ class DotConverter {
   // otherwise.
   uint32_t continue_target_ = 0;
 
+  // An object for mapping Ids to names.
+  libspirv::NameMapper name_mapper_;
+
   // The output stream.
   std::ostream& out_;
 };
@@ -137,10 +142,11 @@ spv_result_t DotConverter::HandleInstruction(
 void DotConverter::FlushBlock(const std::vector<uint32_t>& successors) {
   out_ << current_block_id_;
   if (!seen_function_entry_block_) {
-    out_ << " [label=\"" << current_block_id_ << "\nFn " << current_function_id_
-         << " entry\", shape=box]";
+    out_ << " [label=\"" << name_mapper_(current_block_id_) << "\nFn "
+         << name_mapper_(current_function_id_) << " entry\", shape=box];\n";
+  } else {
+    out_ << " [label=\"" << name_mapper_(current_block_id_) << "\"];\n";
   }
-  out_ << ";\n";
 
   for (auto successor : successors) {
     out_ << current_block_id_ << " -> " << successor << ";\n";
@@ -179,7 +185,8 @@ spv_result_t BinaryToDot(const spv_const_context context, const uint32_t* words,
   const libspirv::AssemblyGrammar grammar(context);
   if (!grammar.isValid()) return SPV_ERROR_INVALID_TABLE;
 
-  DotConverter converter(out);
+  libspirv::FriendlyNameMapper friendly_mapper(context, words, num_words);
+  DotConverter converter(friendly_mapper.GetNameMapper(), out);
   converter.Begin();
   if (auto error = spvBinaryParse(context, &converter, words, num_words,
                                   nullptr, HandleInstruction, diagnostic)) {