Generalize the CFG graph printing for Functions to work on Regions instead.
authorRiver Riddle <riverriddle@google.com>
Mon, 1 Jul 2019 21:10:52 +0000 (14:10 -0700)
committerjpienaar <jpienaar@google.com>
Tue, 2 Jul 2019 00:02:51 +0000 (17:02 -0700)
PiperOrigin-RevId: 256029944

mlir/include/mlir/Analysis/Dominance.h
mlir/include/mlir/IR/Function.h
mlir/include/mlir/IR/Region.h
mlir/include/mlir/IR/RegionGraphTraits.h [moved from mlir/include/mlir/IR/FunctionGraphTraits.h with 67% similarity]
mlir/include/mlir/Transforms/ViewRegionGraph.h [moved from mlir/include/mlir/Transforms/ViewFunctionGraph.h with 84% similarity]
mlir/lib/Analysis/Dominance.cpp
mlir/lib/Transforms/CMakeLists.txt
mlir/lib/Transforms/ViewRegionGraph.cpp [moved from mlir/lib/Transforms/ViewFunctionGraph.cpp with 68% similarity]

index 8d7b2d5..11a5610 100644 (file)
@@ -18,7 +18,7 @@
 #ifndef MLIR_ANALYSIS_DOMINANCE_H
 #define MLIR_ANALYSIS_DOMINANCE_H
 
-#include "mlir/IR/FunctionGraphTraits.h"
+#include "mlir/IR/RegionGraphTraits.h"
 #include "llvm/Support/GenericDomTree.h"
 
 extern template class llvm::DominatorTreeBase<mlir::Block, false>;
@@ -34,7 +34,7 @@ template <bool IsPostDom> class DominanceInfoBase {
   using base = llvm::DominatorTreeBase<Block, IsPostDom>;
 
 public:
-  DominanceInfoBase(Function function) { recalculate(function); }
+  DominanceInfoBase(Function function);
   DominanceInfoBase(Operation *op) { recalculate(op); }
   DominanceInfoBase(DominanceInfoBase &&) = default;
   DominanceInfoBase &operator=(DominanceInfoBase &&) = default;
index 0b04fb7..ab79831 100644 (file)
@@ -323,12 +323,6 @@ public:
   InFlightDiagnostic emitRemark();
   InFlightDiagnostic emitRemark(const Twine &message);
 
-  /// Displays the CFG in a window. This is for use from the debugger and
-  /// depends on Graphviz to generate the graph.
-  /// This function is defined in CFGFunctionViewGraph and only works with that
-  /// target linked.
-  void viewGraph();
-
   /// Create a deep copy of this function and all of its blocks, remapping
   /// any operands that use values outside of the function using the map that is
   /// provided (leaving them alone if no entry is present). If the mapper
index ad0692b..cce4f27 100644 (file)
@@ -119,6 +119,13 @@ public:
   /// each operation.
   void walk(const std::function<void(Operation *)> &callback);
 
+  /// Displays the CFG in a window. This is for use from the debugger and
+  /// depends on Graphviz to generate the graph.
+  /// This function is defined in ViewRegionGraph and only works with that
+  /// target linked.
+  void viewGraph(const llvm::Twine &regionName);
+  void viewGraph();
+
 private:
   RegionType blocks;
 
similarity index 67%
rename from mlir/include/mlir/IR/FunctionGraphTraits.h
rename to mlir/include/mlir/IR/RegionGraphTraits.h
index adc3fef..f45dcc4 100644 (file)
@@ -1,4 +1,4 @@
-//===- CFGFunctionGraphTraits.h - llvm::GraphTraits for CFGs ----*- C++ -*-===//
+//===- RegionGraphTraits.h - llvm::GraphTraits for CFGs ---------*- C++ -*-===//
 //
 // Copyright 2019 The MLIR Authors.
 //
 //
 //===----------------------------------------------------------------------===//
 
-#ifndef MLIR_IR_CFGFUNCTIONGRAPHTRAITS_H
-#define MLIR_IR_CFGFUNCTIONGRAPHTRAITS_H
+#ifndef MLIR_IR_REGIONGRAPHTRAITS_H
+#define MLIR_IR_REGIONGRAPHTRAITS_H
 
-#include "mlir/IR/Function.h"
+#include "mlir/IR/Region.h"
 #include "llvm/ADT/GraphTraits.h"
 
 namespace llvm {
@@ -57,46 +57,13 @@ template <> struct GraphTraits<Inverse<mlir::Block *>> {
 };
 
 template <>
-struct GraphTraits<mlir::Function *> : public GraphTraits<mlir::Block *> {
-  using GraphType = mlir::Function *;
-  using NodeRef = mlir::Block *;
-
-  static NodeRef getEntryNode(GraphType fn) { return &fn->front(); }
-
-  using nodes_iterator = pointer_iterator<mlir::Function::iterator>;
-  static nodes_iterator nodes_begin(GraphType fn) {
-    return nodes_iterator(fn->begin());
-  }
-  static nodes_iterator nodes_end(GraphType fn) {
-    return nodes_iterator(fn->end());
-  }
-};
-
-template <>
-struct GraphTraits<Inverse<mlir::Function *>>
-    : public GraphTraits<Inverse<mlir::Block *>> {
-  using GraphType = Inverse<mlir::Function *>;
-  using NodeRef = NodeRef;
-
-  static NodeRef getEntryNode(GraphType fn) { return &fn.Graph->front(); }
-
-  using nodes_iterator = pointer_iterator<mlir::Function::iterator>;
-  static nodes_iterator nodes_begin(GraphType fn) {
-    return nodes_iterator(fn.Graph->begin());
-  }
-  static nodes_iterator nodes_end(GraphType fn) {
-    return nodes_iterator(fn.Graph->end());
-  }
-};
-
-template <>
 struct GraphTraits<mlir::Region *> : public GraphTraits<mlir::Block *> {
   using GraphType = mlir::Region *;
   using NodeRef = mlir::Block *;
 
   static NodeRef getEntryNode(GraphType fn) { return &fn->front(); }
 
-  using nodes_iterator = pointer_iterator<mlir::Function::iterator>;
+  using nodes_iterator = pointer_iterator<mlir::Region::iterator>;
   static nodes_iterator nodes_begin(GraphType fn) {
     return nodes_iterator(fn->begin());
   }
@@ -113,7 +80,7 @@ struct GraphTraits<Inverse<mlir::Region *>>
 
   static NodeRef getEntryNode(GraphType fn) { return &fn.Graph->front(); }
 
-  using nodes_iterator = pointer_iterator<mlir::Function::iterator>;
+  using nodes_iterator = pointer_iterator<mlir::Region::iterator>;
   static nodes_iterator nodes_begin(GraphType fn) {
     return nodes_iterator(fn.Graph->begin());
   }
@@ -1,4 +1,4 @@
-//===- ViewFunctionGraph.h - View/write graphviz graphs ---------*- C++ -*-===//
+//===- ViewRegionGraph.h - View/write graphviz graphs -----------*- C++ -*-===//
 //
 // Copyright 2019 The MLIR Authors.
 //
@@ -15,7 +15,7 @@
 // limitations under the License.
 // =============================================================================
 //
-// Defines interface to produce Graphviz outputs of MLIR Functions.
+// Defines interface to produce Graphviz outputs of MLIR Regions.
 //
 //===----------------------------------------------------------------------===//
 
 #include "llvm/Support/raw_ostream.h"
 
 namespace mlir {
-
-class Function;
 class FunctionPassBase;
+class Region;
 
 /// Displays the CFG in a window. This is for use from the debugger and
 /// depends on Graphviz to generate the graph.
-void viewGraph(Function function, const Twine &name, bool shortNames = false,
+void viewGraph(Region &region, const Twine &name, bool shortNames = false,
                const Twine &title = "",
                llvm::GraphProgram::Name program = llvm::GraphProgram::DOT);
 
-llvm::raw_ostream &writeGraph(llvm::raw_ostream &os, Function function,
+llvm::raw_ostream &writeGraph(llvm::raw_ostream &os, Region &region,
                               bool shortNames = false, const Twine &title = "");
 
 /// Creates a pass to print CFG graphs.
index b4cdeb7..2eefe8f 100644 (file)
@@ -21,6 +21,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "mlir/Analysis/Dominance.h"
+#include "mlir/IR/Function.h"
 #include "mlir/IR/Operation.h"
 #include "llvm/Support/GenericDomTreeConstruction.h"
 
@@ -35,6 +36,11 @@ template class llvm::DomTreeNodeBase<Block>;
 // DominanceInfoBase
 //===----------------------------------------------------------------------===//
 
+template <bool IsPostDom>
+DominanceInfoBase<IsPostDom>::DominanceInfoBase(Function function) {
+  recalculate(function);
+}
+
 /// Recalculate the dominance info.
 template <bool IsPostDom>
 void DominanceInfoBase<IsPostDom>::recalculate(Function function) {
index 2860967..6f1769a 100644 (file)
@@ -18,7 +18,7 @@ add_llvm_library(MLIRTransforms
   SimplifyAffineStructures.cpp
   StripDebugInfo.cpp
   Vectorize.cpp
-  ViewFunctionGraph.cpp
+  ViewRegionGraph.cpp
 
   ADDITIONAL_HEADER_DIRS
   ${MLIR_MAIN_INCLUDE_DIR}/mlir/Transforms
similarity index 68%
rename from mlir/lib/Transforms/ViewFunctionGraph.cpp
rename to mlir/lib/Transforms/ViewRegionGraph.cpp
index 3c1a1b3..5a0e8e5 100644 (file)
@@ -1,4 +1,4 @@
-//===- ViewFunctionGraph.cpp - View/write graphviz graphs -----------------===//
+//===- ViewRegionGraph.cpp - View/write graphviz graphs -------------------===//
 //
 // Copyright 2019 The MLIR Authors.
 //
@@ -15,8 +15,8 @@
 // limitations under the License.
 // =============================================================================
 
-#include "mlir/Transforms/ViewFunctionGraph.h"
-#include "mlir/IR/FunctionGraphTraits.h"
+#include "mlir/Transforms/ViewRegionGraph.h"
+#include "mlir/IR/RegionGraphTraits.h"
 #include "mlir/Pass/Pass.h"
 
 using namespace mlir;
@@ -24,13 +24,13 @@ using namespace mlir;
 namespace llvm {
 
 // Specialize DOTGraphTraits to produce more readable output.
-template <> struct DOTGraphTraits<Function *> : public DefaultDOTGraphTraits {
+template <> struct DOTGraphTraits<Region *> : public DefaultDOTGraphTraits {
   using DefaultDOTGraphTraits::DefaultDOTGraphTraits;
 
-  static std::string getNodeLabel(Block *Block, Function *);
+  static std::string getNodeLabel(Block *Block, Region *);
 };
 
-std::string DOTGraphTraits<Function *>::getNodeLabel(Block *Block, Function *) {
+std::string DOTGraphTraits<Region *>::getNodeLabel(Block *Block, Region *) {
   // Reuse the print output for the node labels.
   std::string outStreamStr;
   raw_string_ostream os(outStreamStr);
@@ -53,34 +53,35 @@ std::string DOTGraphTraits<Function *>::getNodeLabel(Block *Block, Function *) {
 
 } // end namespace llvm
 
-void mlir::viewGraph(Function function, const llvm::Twine &name,
-                     bool shortNames, const llvm::Twine &title,
+void mlir::viewGraph(Region &region, const llvm::Twine &name, bool shortNames,
+                     const llvm::Twine &title,
                      llvm::GraphProgram::Name program) {
-  llvm::ViewGraph(&function, name, shortNames, title, program);
+  llvm::ViewGraph(&region, name, shortNames, title, program);
 }
 
-llvm::raw_ostream &mlir::writeGraph(llvm::raw_ostream &os, Function function,
+llvm::raw_ostream &mlir::writeGraph(llvm::raw_ostream &os, Region &region,
                                     bool shortNames, const llvm::Twine &title) {
-  return llvm::WriteGraph(os, &function, shortNames, title);
+  return llvm::WriteGraph(os, &region, shortNames, title);
 }
 
-void mlir::Function::viewGraph() {
-  ::mlir::viewGraph(*this, llvm::Twine("cfgfunc ") + getName().str());
+void mlir::Region::viewGraph(const llvm::Twine &regionName) {
+  ::mlir::viewGraph(*this, regionName);
 }
+void mlir::Region::viewGraph() { viewGraph("region"); }
 
 namespace {
 struct PrintCFGPass : public FunctionPass<PrintCFGPass> {
   PrintCFGPass(llvm::raw_ostream &os = llvm::errs(), bool shortNames = false,
                const llvm::Twine &title = "")
-      : os(os), shortNames(shortNames), title(title) {}
+      : os(os), shortNames(shortNames), title(title.str()) {}
   void runOnFunction() {
-    mlir::writeGraph(os, getFunction(), shortNames, title);
+    mlir::writeGraph(os, getFunction().getBody(), shortNames, title);
   }
 
 private:
   llvm::raw_ostream &os;
   bool shortNames;
-  const llvm::Twine &title;
+  std::string title;
 };
 } // namespace
 
@@ -91,4 +92,4 @@ FunctionPassBase *mlir::createPrintCFGGraphPass(llvm::raw_ostream &os,
 }
 
 static PassRegistration<PrintCFGPass> pass("print-cfg-graph",
-                                           "Print CFG graph per function");
+                                           "Print CFG graph per Function");