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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
|
//===- GraphPrinter.cpp - Create a DOT output describing the Scop. --------===//
//
// 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
//
//===----------------------------------------------------------------------===//
//
// Create a DOT output describing the Scop.
//
// For each function a dot file is created that shows the control flow graph of
// the function and highlights the detected Scops.
//
//===----------------------------------------------------------------------===//
#include "polly/LinkAllPasses.h"
#include "polly/ScopDetection.h"
#include "polly/Support/ScopLocation.h"
#include "llvm/Analysis/DOTGraphTraitsPass.h"
#include "llvm/Analysis/RegionInfo.h"
#include "llvm/Analysis/RegionIterator.h"
#include "llvm/Support/CommandLine.h"
using namespace polly;
using namespace llvm;
static cl::opt<std::string>
ViewFilter("polly-view-only",
cl::desc("Only view functions that match this pattern"),
cl::Hidden, cl::init(""), cl::ZeroOrMore);
static cl::opt<bool> ViewAll("polly-view-all",
cl::desc("Also show functions without any scops"),
cl::Hidden, cl::init(false), cl::ZeroOrMore);
namespace llvm {
template <>
struct GraphTraits<ScopDetection *> : public GraphTraits<RegionInfo *> {
static NodeRef getEntryNode(ScopDetection *SD) {
return GraphTraits<RegionInfo *>::getEntryNode(SD->getRI());
}
static nodes_iterator nodes_begin(ScopDetection *SD) {
return nodes_iterator::begin(getEntryNode(SD));
}
static nodes_iterator nodes_end(ScopDetection *SD) {
return nodes_iterator::end(getEntryNode(SD));
}
};
template <>
struct GraphTraits<ScopDetectionWrapperPass *>
: public GraphTraits<ScopDetection *> {
static NodeRef getEntryNode(ScopDetectionWrapperPass *P) {
return GraphTraits<ScopDetection *>::getEntryNode(&P->getSD());
}
static nodes_iterator nodes_begin(ScopDetectionWrapperPass *P) {
return nodes_iterator::begin(getEntryNode(P));
}
static nodes_iterator nodes_end(ScopDetectionWrapperPass *P) {
return nodes_iterator::end(getEntryNode(P));
}
};
template <> struct DOTGraphTraits<RegionNode *> : public DefaultDOTGraphTraits {
DOTGraphTraits(bool isSimple = false) : DefaultDOTGraphTraits(isSimple) {}
std::string getNodeLabel(RegionNode *Node, RegionNode *Graph) {
if (!Node->isSubRegion()) {
BasicBlock *BB = Node->getNodeAs<BasicBlock>();
if (isSimple())
return DOTGraphTraits<DOTFuncInfo *>::getSimpleNodeLabel(BB, nullptr);
else
return DOTGraphTraits<DOTFuncInfo *>::getCompleteNodeLabel(BB, nullptr);
}
return "Not implemented";
}
};
template <>
struct DOTGraphTraits<ScopDetectionWrapperPass *>
: public DOTGraphTraits<RegionNode *> {
DOTGraphTraits(bool isSimple = false)
: DOTGraphTraits<RegionNode *>(isSimple) {}
static std::string getGraphName(ScopDetectionWrapperPass *SD) {
return "Scop Graph";
}
std::string getEdgeAttributes(RegionNode *srcNode,
GraphTraits<RegionInfo *>::ChildIteratorType CI,
ScopDetectionWrapperPass *P) {
RegionNode *destNode = *CI;
auto *SD = &P->getSD();
if (srcNode->isSubRegion() || destNode->isSubRegion())
return "";
// In case of a backedge, do not use it to define the layout of the nodes.
BasicBlock *srcBB = srcNode->getNodeAs<BasicBlock>();
BasicBlock *destBB = destNode->getNodeAs<BasicBlock>();
RegionInfo *RI = SD->getRI();
Region *R = RI->getRegionFor(destBB);
while (R && R->getParent())
if (R->getParent()->getEntry() == destBB)
R = R->getParent();
else
break;
if (R && R->getEntry() == destBB && R->contains(srcBB))
return "constraint=false";
return "";
}
std::string getNodeLabel(RegionNode *Node, ScopDetectionWrapperPass *P) {
return DOTGraphTraits<RegionNode *>::getNodeLabel(
Node, reinterpret_cast<RegionNode *>(
P->getSD().getRI()->getTopLevelRegion()));
}
static std::string escapeString(std::string String) {
std::string Escaped;
for (const auto &C : String) {
if (C == '"')
Escaped += '\\';
Escaped += C;
}
return Escaped;
}
// Print the cluster of the subregions. This groups the single basic blocks
// and adds a different background color for each group.
static void printRegionCluster(ScopDetection *SD, const Region *R,
raw_ostream &O, unsigned depth = 0) {
O.indent(2 * depth) << "subgraph cluster_" << static_cast<const void *>(R)
<< " {\n";
unsigned LineBegin, LineEnd;
std::string FileName;
getDebugLocation(R, LineBegin, LineEnd, FileName);
std::string Location;
if (LineBegin != (unsigned)-1) {
Location = escapeString(FileName + ":" + std::to_string(LineBegin) + "-" +
std::to_string(LineEnd) + "\n");
}
std::string ErrorMessage = SD->regionIsInvalidBecause(R);
ErrorMessage = escapeString(ErrorMessage);
O.indent(2 * (depth + 1))
<< "label = \"" << Location << ErrorMessage << "\";\n";
if (SD->isMaxRegionInScop(*R)) {
O.indent(2 * (depth + 1)) << "style = filled;\n";
// Set color to green.
O.indent(2 * (depth + 1)) << "color = 3";
} else {
O.indent(2 * (depth + 1)) << "style = solid;\n";
int color = (R->getDepth() * 2 % 12) + 1;
// We do not want green again.
if (color == 3)
color = 6;
O.indent(2 * (depth + 1)) << "color = " << color << "\n";
}
for (const auto &SubRegion : *R)
printRegionCluster(SD, SubRegion.get(), O, depth + 1);
RegionInfo *RI = R->getRegionInfo();
for (BasicBlock *BB : R->blocks())
if (RI->getRegionFor(BB) == R)
O.indent(2 * (depth + 1))
<< "Node"
<< static_cast<void *>(RI->getTopLevelRegion()->getBBNode(BB))
<< ";\n";
O.indent(2 * depth) << "}\n";
}
static void
addCustomGraphFeatures(const ScopDetectionWrapperPass *SD,
GraphWriter<ScopDetectionWrapperPass *> &GW) {
raw_ostream &O = GW.getOStream();
O << "\tcolorscheme = \"paired12\"\n";
printRegionCluster(&SD->getSD(), SD->getSD().getRI()->getTopLevelRegion(),
O, 4);
}
};
} // end namespace llvm
struct ScopViewer
: public DOTGraphTraitsViewer<ScopDetectionWrapperPass, false> {
static char ID;
ScopViewer()
: DOTGraphTraitsViewer<ScopDetectionWrapperPass, false>("scops", ID) {}
bool processFunction(Function &F, ScopDetectionWrapperPass &SD) override {
if (ViewFilter != "" && !F.getName().count(ViewFilter))
return false;
if (ViewAll)
return true;
// Check that at least one scop was detected.
return std::distance(SD.getSD().begin(), SD.getSD().end()) > 0;
}
};
char ScopViewer::ID = 0;
struct ScopOnlyViewer
: public DOTGraphTraitsViewer<ScopDetectionWrapperPass, true> {
static char ID;
ScopOnlyViewer()
: DOTGraphTraitsViewer<ScopDetectionWrapperPass, true>("scopsonly", ID) {}
};
char ScopOnlyViewer::ID = 0;
struct ScopPrinter
: public DOTGraphTraitsPrinter<ScopDetectionWrapperPass, false> {
static char ID;
ScopPrinter()
: DOTGraphTraitsPrinter<ScopDetectionWrapperPass, false>("scops", ID) {}
};
char ScopPrinter::ID = 0;
struct ScopOnlyPrinter
: public DOTGraphTraitsPrinter<ScopDetectionWrapperPass, true> {
static char ID;
ScopOnlyPrinter()
: DOTGraphTraitsPrinter<ScopDetectionWrapperPass, true>("scopsonly", ID) {
}
};
char ScopOnlyPrinter::ID = 0;
static RegisterPass<ScopViewer> X("view-scops",
"Polly - View Scops of function");
static RegisterPass<ScopOnlyViewer>
Y("view-scops-only",
"Polly - View Scops of function (with no function bodies)");
static RegisterPass<ScopPrinter> M("dot-scops",
"Polly - Print Scops of function");
static RegisterPass<ScopOnlyPrinter>
N("dot-scops-only",
"Polly - Print Scops of function (with no function bodies)");
Pass *polly::createDOTViewerPass() { return new ScopViewer(); }
Pass *polly::createDOTOnlyViewerPass() { return new ScopOnlyViewer(); }
Pass *polly::createDOTPrinterPass() { return new ScopPrinter(); }
Pass *polly::createDOTOnlyPrinterPass() { return new ScopOnlyPrinter(); }
|