[PATCH] D125945: [BOLT][NFC] Dump function CFG into html, rendered in browser using d3-graphviz

Amir Ayupov via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu May 19 11:43:10 PDT 2022


Amir created this revision.
Herald added a reviewer: rafauler.
Herald added a subscriber: ayermolo.
Herald added a reviewer: maksfb.
Herald added a project: All.
Amir updated this revision to Diff 430759.
Amir added a comment.
Amir retitled this revision from "[BOLT][NFC] Dump dot into html using d3-graphviz" to "[BOLT][NFC] Dump function CFG into html, rendered in browser using d3-graphviz".
Amir edited the summary of this revision.
Amir published this revision for review.
Herald added subscribers: llvm-commits, yota9, aheejin.
Herald added a project: LLVM.

Fixes


Dump BinaryFunction CFG into html.
Usage:

  llvm-bolt <...> -dump-dot-all -dot-html

Then open resulting html in browser.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D125945

Files:
  bolt/include/bolt/Core/BinaryFunction.h
  bolt/lib/Core/BinaryFunction.cpp


Index: bolt/lib/Core/BinaryFunction.cpp
===================================================================
--- bolt/lib/Core/BinaryFunction.cpp
+++ bolt/lib/Core/BinaryFunction.cpp
@@ -80,6 +80,10 @@
   cl::Hidden,
   cl::cat(BoltCategory));
 
+static cl::opt<bool> DotHtml("dot-html",
+                             cl::desc("dump function DOT graph into html file"),
+                             cl::ZeroOrMore, cl::Hidden, cl::cat(BoltCategory));
+
 cl::opt<JumpTableSupportLevel>
 JumpTables("jump-tables",
   cl::desc("jump tables support (default=basic)"),
@@ -3054,6 +3058,24 @@
 
 } // namespace
 
+void BinaryFunction::dumpGraphD3(raw_ostream &OS) const {
+  OS << "<!DOCTYPE html>\n"
+     << "<meta charset=\"utf-8\">\n"
+     << "<body>\n"
+
+     << "<script src=\"https://d3js.org/d3.v5.min.js\"></script>\n"
+     << "<script "
+     << "src=\"https://unpkg.com/@hpcc-js/wasm@0.3.11/dist/index.min.js\">"
+     << "</script>\n"
+     << "<script "
+     << "src=\"https://unpkg.com/d3-graphviz@3.0.5/build/d3-graphviz.js\">"
+     << "</script>\n"
+     << "<div id=\"graph\" style=\"text-align: center;\"></div>\n"
+     << "<script>\nd3.select(\"#graph\").graphviz().renderDot(`";
+  dumpGraph(OS);
+  OS << "`);\n</script>";
+}
+
 void BinaryFunction::dumpGraph(raw_ostream &OS) const {
   OS << "strict digraph \"" << getPrintName() << "\" {\n";
   uint64_t Offset = Address;
@@ -3147,7 +3169,8 @@
 }
 
 void BinaryFunction::dumpGraphForPass(std::string Annotation) const {
-  std::string Filename = constructFilename(getPrintName(), Annotation, ".dot");
+  std::string Ext = opts::DotHtml ? ".html" : ".dot";
+  std::string Filename = constructFilename(getPrintName(), Annotation, Ext);
   outs() << "BOLT-DEBUG: Dumping CFG to " << Filename << "\n";
   dumpGraphToFile(Filename);
 }
@@ -3162,7 +3185,10 @@
     }
     return;
   }
-  dumpGraph(of);
+  if (opts::DotHtml)
+    dumpGraphD3(of);
+  else
+    dumpGraph(of);
 }
 
 bool BinaryFunction::validateCFG() const {
Index: bolt/include/bolt/Core/BinaryFunction.h
===================================================================
--- bolt/include/bolt/Core/BinaryFunction.h
+++ bolt/include/bolt/Core/BinaryFunction.h
@@ -896,6 +896,9 @@
   /// Dump CFG in graphviz format to file.
   void dumpGraphToFile(std::string Filename) const;
 
+  /// Dump CFG in d3.js format
+  void dumpGraphD3(raw_ostream &OS) const;
+
   /// Dump CFG in graphviz format to a file with a filename that is derived
   /// from the function name and Annotation strings.  Useful for dumping the
   /// CFG after an optimization pass.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D125945.430759.patch
Type: text/x-patch
Size: 2586 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220519/d0bf4bdf/attachment.bin>


More information about the llvm-commits mailing list