github.com/johnnyeven/libtools@v0.0.0-20191126065708-61829c1adf46/third_party/mlir/lib/Transforms/ViewRegionGraph.cpp (about)

     1  //===- ViewRegionGraph.cpp - View/write graphviz graphs -------------------===//
     2  //
     3  // Copyright 2019 The MLIR Authors.
     4  //
     5  // Licensed under the Apache License, Version 2.0 (the "License");
     6  // you may not use this file except in compliance with the License.
     7  // You may obtain a copy of the License at
     8  //
     9  //   http://www.apache.org/licenses/LICENSE-2.0
    10  //
    11  // Unless required by applicable law or agreed to in writing, software
    12  // distributed under the License is distributed on an "AS IS" BASIS,
    13  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  // See the License for the specific language governing permissions and
    15  // limitations under the License.
    16  // =============================================================================
    17  
    18  #include "mlir/Transforms/ViewRegionGraph.h"
    19  #include "mlir/IR/RegionGraphTraits.h"
    20  #include "mlir/Pass/Pass.h"
    21  
    22  using namespace mlir;
    23  
    24  namespace llvm {
    25  
    26  // Specialize DOTGraphTraits to produce more readable output.
    27  template <> struct DOTGraphTraits<Region *> : public DefaultDOTGraphTraits {
    28    using DefaultDOTGraphTraits::DefaultDOTGraphTraits;
    29  
    30    static std::string getNodeLabel(Block *Block, Region *);
    31  };
    32  
    33  std::string DOTGraphTraits<Region *>::getNodeLabel(Block *Block, Region *) {
    34    // Reuse the print output for the node labels.
    35    std::string outStreamStr;
    36    raw_string_ostream os(outStreamStr);
    37    Block->print(os);
    38    std::string &outStr = os.str();
    39  
    40    if (outStr[0] == '\n')
    41      outStr.erase(outStr.begin());
    42  
    43    // Process string output to left justify the block.
    44    for (unsigned i = 0; i != outStr.length(); ++i) {
    45      if (outStr[i] == '\n') {
    46        outStr[i] = '\\';
    47        outStr.insert(outStr.begin() + i + 1, 'l');
    48      }
    49    }
    50  
    51    return outStr;
    52  }
    53  
    54  } // end namespace llvm
    55  
    56  void mlir::viewGraph(Region &region, const llvm::Twine &name, bool shortNames,
    57                       const llvm::Twine &title,
    58                       llvm::GraphProgram::Name program) {
    59    llvm::ViewGraph(&region, name, shortNames, title, program);
    60  }
    61  
    62  llvm::raw_ostream &mlir::writeGraph(llvm::raw_ostream &os, Region &region,
    63                                      bool shortNames, const llvm::Twine &title) {
    64    return llvm::WriteGraph(os, &region, shortNames, title);
    65  }
    66  
    67  void mlir::Region::viewGraph(const llvm::Twine &regionName) {
    68    ::mlir::viewGraph(*this, regionName);
    69  }
    70  void mlir::Region::viewGraph() { viewGraph("region"); }
    71  
    72  namespace {
    73  struct PrintCFGPass : public FunctionPass<PrintCFGPass> {
    74    PrintCFGPass(llvm::raw_ostream &os = llvm::errs(), bool shortNames = false,
    75                 const llvm::Twine &title = "")
    76        : os(os), shortNames(shortNames), title(title.str()) {}
    77    void runOnFunction() {
    78      mlir::writeGraph(os, getFunction().getBody(), shortNames, title);
    79    }
    80  
    81  private:
    82    llvm::raw_ostream &os;
    83    bool shortNames;
    84    std::string title;
    85  };
    86  } // namespace
    87  
    88  FunctionPassBase *mlir::createPrintCFGGraphPass(llvm::raw_ostream &os,
    89                                                  bool shortNames,
    90                                                  const llvm::Twine &title) {
    91    return new PrintCFGPass(os, shortNames, title);
    92  }
    93  
    94  static PassRegistration<PrintCFGPass> pass("print-cfg-graph",
    95                                             "Print CFG graph per Function");