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