• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===- ViewRegionGraph.cpp - View/write graphviz graphs -------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "mlir/Transforms/ViewRegionGraph.h"
10 #include "PassDetail.h"
11 #include "mlir/IR/RegionGraphTraits.h"
12 
13 using namespace mlir;
14 
15 namespace llvm {
16 
17 // Specialize DOTGraphTraits to produce more readable output.
18 template <> struct DOTGraphTraits<Region *> : public DefaultDOTGraphTraits {
19   using DefaultDOTGraphTraits::DefaultDOTGraphTraits;
20 
21   static std::string getNodeLabel(Block *Block, Region *);
22 };
23 
getNodeLabel(Block * Block,Region *)24 std::string DOTGraphTraits<Region *>::getNodeLabel(Block *Block, Region *) {
25   // Reuse the print output for the node labels.
26   std::string outStreamStr;
27   raw_string_ostream os(outStreamStr);
28   Block->print(os);
29   std::string &outStr = os.str();
30 
31   if (outStr[0] == '\n')
32     outStr.erase(outStr.begin());
33 
34   // Process string output to left justify the block.
35   for (unsigned i = 0; i != outStr.length(); ++i) {
36     if (outStr[i] == '\n') {
37       outStr[i] = '\\';
38       outStr.insert(outStr.begin() + i + 1, 'l');
39     }
40   }
41 
42   return outStr;
43 }
44 
45 } // end namespace llvm
46 
viewGraph(Region & region,const Twine & name,bool shortNames,const Twine & title,llvm::GraphProgram::Name program)47 void mlir::viewGraph(Region &region, const Twine &name, bool shortNames,
48                      const Twine &title, llvm::GraphProgram::Name program) {
49   llvm::ViewGraph(&region, name, shortNames, title, program);
50 }
51 
writeGraph(raw_ostream & os,Region & region,bool shortNames,const Twine & title)52 raw_ostream &mlir::writeGraph(raw_ostream &os, Region &region, bool shortNames,
53                               const Twine &title) {
54   return llvm::WriteGraph(os, &region, shortNames, title);
55 }
56 
viewGraph(const Twine & regionName)57 void mlir::Region::viewGraph(const Twine &regionName) {
58   ::mlir::viewGraph(*this, regionName);
59 }
viewGraph()60 void mlir::Region::viewGraph() { viewGraph("region"); }
61 
62 namespace {
63 struct PrintCFGPass : public PrintCFGBase<PrintCFGPass> {
PrintCFGPass__anon1305202f0111::PrintCFGPass64   PrintCFGPass(raw_ostream &os = llvm::errs(), bool shortNames = false,
65                const Twine &title = "")
66       : os(os), shortNames(shortNames), title(title.str()) {}
runOnFunction__anon1305202f0111::PrintCFGPass67   void runOnFunction() override {
68     mlir::writeGraph(os, getFunction().getBody(), shortNames, title);
69   }
70 
71 private:
72   raw_ostream &os;
73   bool shortNames;
74   std::string title;
75 };
76 } // namespace
77 
78 std::unique_ptr<mlir::OperationPass<mlir::FuncOp>>
createPrintCFGGraphPass(raw_ostream & os,bool shortNames,const Twine & title)79 mlir::createPrintCFGGraphPass(raw_ostream &os, bool shortNames,
80                               const Twine &title) {
81   return std::make_unique<PrintCFGPass>(os, shortNames, title);
82 }
83