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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
|
//===------------------ llvm-opt-report/OptReport.cpp ---------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
///
/// \file
/// \brief This file implements a tool that can parse the YAML optimization
/// records and generate an optimization summary annotated source listing
/// report.
///
//===----------------------------------------------------------------------===//
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/ErrorOr.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Format.h"
#include "llvm/Support/LineIterator.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/Program.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Support/Signals.h"
#include "llvm/Support/YAMLTraits.h"
using namespace llvm;
using namespace llvm::yaml;
static cl::opt<bool> Help("h", cl::desc("Alias for -help"), cl::Hidden);
// Mark all our options with this category, everything else (except for -version
// and -help) will be hidden.
static cl::OptionCategory
OptReportCategory("llvm-opt-report options");
static cl::opt<std::string>
InputFileName(cl::Positional, cl::desc("<input>"), cl::init("-"),
cl::cat(OptReportCategory));
static cl::opt<std::string>
OutputFileName("o", cl::desc("Output file"), cl::init("-"),
cl::cat(OptReportCategory));
static cl::opt<std::string>
InputRelDir("r", cl::desc("Root for relative input paths"), cl::init(""),
cl::cat(OptReportCategory));
static cl::opt<bool>
Succinct("s", cl::desc("Don't include vectorization factors, etc."),
cl::init(false), cl::cat(OptReportCategory));
namespace {
// For each location in the source file, the common per-transformation state
// collected.
struct OptReportLocationItemInfo {
bool Analyzed = false;
bool Transformed = false;
OptReportLocationItemInfo &operator |= (
const OptReportLocationItemInfo &RHS) {
Analyzed |= RHS.Analyzed;
Transformed |= RHS.Transformed;
return *this;
}
};
// The per-location information collected for producing an optimization report.
struct OptReportLocationInfo {
OptReportLocationItemInfo Inlined;
OptReportLocationItemInfo Unrolled;
OptReportLocationItemInfo Vectorized;
int VectorizationFactor = 1;
int InterleaveCount = 1;
int UnrollCount = 1;
OptReportLocationInfo &operator |= (const OptReportLocationInfo &RHS) {
Inlined |= RHS.Inlined;
Unrolled |= RHS.Unrolled;
Vectorized |= RHS.Vectorized;
VectorizationFactor =
std::max(VectorizationFactor, RHS.VectorizationFactor);
InterleaveCount = std::max(InterleaveCount, RHS.InterleaveCount);
UnrollCount = std::max(UnrollCount, RHS.UnrollCount);
return *this;
}
};
typedef std::map<std::string, std::map<int, std::map<int,
OptReportLocationInfo>>> LocationInfoTy;
} // anonymous namespace
static void collectLocationInfo(yaml::Stream &Stream,
LocationInfoTy &LocationInfo) {
SmallVector<char, 8> Tmp;
// Note: We're using the YAML parser here directly, instead of using the
// YAMLTraits implementation, because the YAMLTraits implementation does not
// support a way to handle only a subset of the input keys (it will error out
// if there is an input key that you don't map to your class), and
// furthermore, it does not provide a way to handle the Args sequence of
// key/value pairs, where the order must be captured and the 'String' key
// might be repeated.
for (auto &Doc : Stream) {
auto *Root = dyn_cast<yaml::MappingNode>(Doc.getRoot());
if (!Root)
continue;
bool Transformed = Root->getRawTag() == "!Passed";
std::string Pass, File;
int Line = 0, Column = 1;
int VectorizationFactor = 1;
int InterleaveCount = 1;
int UnrollCount = 1;
for (auto &RootChild : *Root) {
auto *Key = dyn_cast<yaml::ScalarNode>(RootChild.getKey());
if (!Key)
continue;
StringRef KeyName = Key->getValue(Tmp);
if (KeyName == "Pass") {
auto *Value = dyn_cast<yaml::ScalarNode>(RootChild.getValue());
if (!Value)
continue;
Pass = Value->getValue(Tmp);
} else if (KeyName == "DebugLoc") {
auto *DebugLoc = dyn_cast<yaml::MappingNode>(RootChild.getValue());
if (!DebugLoc)
continue;
for (auto &DLChild : *DebugLoc) {
auto *DLKey = dyn_cast<yaml::ScalarNode>(DLChild.getKey());
if (!DLKey)
continue;
StringRef DLKeyName = DLKey->getValue(Tmp);
if (DLKeyName == "File") {
auto *Value = dyn_cast<yaml::ScalarNode>(DLChild.getValue());
if (!Value)
continue;
File = Value->getValue(Tmp);
} else if (DLKeyName == "Line") {
auto *Value = dyn_cast<yaml::ScalarNode>(DLChild.getValue());
if (!Value)
continue;
Value->getValue(Tmp).getAsInteger(10, Line);
} else if (DLKeyName == "Column") {
auto *Value = dyn_cast<yaml::ScalarNode>(DLChild.getValue());
if (!Value)
continue;
Value->getValue(Tmp).getAsInteger(10, Column);
}
}
} else if (KeyName == "Args") {
auto *Args = dyn_cast<yaml::SequenceNode>(RootChild.getValue());
if (!Args)
continue;
for (auto &ArgChild : *Args) {
auto *ArgMap = dyn_cast<yaml::MappingNode>(&ArgChild);
if (!ArgMap)
continue;
for (auto &ArgKV : *ArgMap) {
auto *ArgKey = dyn_cast<yaml::ScalarNode>(ArgKV.getKey());
if (!ArgKey)
continue;
StringRef ArgKeyName = ArgKey->getValue(Tmp);
if (ArgKeyName == "VectorizationFactor") {
auto *Value = dyn_cast<yaml::ScalarNode>(ArgKV.getValue());
if (!Value)
continue;
Value->getValue(Tmp).getAsInteger(10, VectorizationFactor);
} else if (ArgKeyName == "InterleaveCount") {
auto *Value = dyn_cast<yaml::ScalarNode>(ArgKV.getValue());
if (!Value)
continue;
Value->getValue(Tmp).getAsInteger(10, InterleaveCount);
} else if (ArgKeyName == "UnrollCount") {
auto *Value = dyn_cast<yaml::ScalarNode>(ArgKV.getValue());
if (!Value)
continue;
Value->getValue(Tmp).getAsInteger(10, UnrollCount);
}
}
}
}
}
if (Line < 1 || File.empty())
continue;
// We track information on both actual and potential transformations. This
// way, if there are multiple possible things on a line that are, or could
// have been transformed, we can indicate that explicitly in the output.
auto UpdateLLII = [Transformed, VectorizationFactor,
InterleaveCount,
UnrollCount](OptReportLocationInfo &LI,
OptReportLocationItemInfo &LLII) {
LLII.Analyzed = true;
if (Transformed) {
LLII.Transformed = true;
LI.VectorizationFactor = VectorizationFactor;
LI.InterleaveCount = InterleaveCount;
LI.UnrollCount = UnrollCount;
}
};
if (Pass == "inline") {
auto &LI = LocationInfo[File][Line][Column];
UpdateLLII(LI, LI.Inlined);
} else if (Pass == "loop-unroll") {
auto &LI = LocationInfo[File][Line][Column];
UpdateLLII(LI, LI.Unrolled);
} else if (Pass == "loop-vectorize") {
auto &LI = LocationInfo[File][Line][Column];
UpdateLLII(LI, LI.Vectorized);
}
}
}
static bool readLocationInfo(LocationInfoTy &LocationInfo) {
ErrorOr<std::unique_ptr<MemoryBuffer>> Buf =
MemoryBuffer::getFileOrSTDIN(InputFileName);
if (std::error_code EC = Buf.getError()) {
errs() << "error: Can't open file " << InputFileName << ": " <<
EC.message() << "\n";
return false;
}
SourceMgr SM;
yaml::Stream Stream(Buf.get()->getBuffer(), SM);
collectLocationInfo(Stream, LocationInfo);
return true;
}
static bool writeReport(LocationInfoTy &LocationInfo) {
std::error_code EC;
llvm::raw_fd_ostream OS(OutputFileName, EC,
llvm::sys::fs::F_Text);
if (EC) {
errs() << "error: Can't open file " << OutputFileName << ": " <<
EC.message() << "\n";
return false;
}
bool FirstFile = true;
for (auto &FI : LocationInfo) {
SmallString<128> FileName(FI.first);
if (!InputRelDir.empty()) {
if (std::error_code EC = sys::fs::make_absolute(InputRelDir, FileName)) {
errs() << "error: Can't resolve file path to " << FileName << ": " <<
EC.message() << "\n";
return false;
}
}
const auto &FileInfo = FI.second;
ErrorOr<std::unique_ptr<MemoryBuffer>> Buf =
MemoryBuffer::getFile(FileName);
if (std::error_code EC = Buf.getError()) {
errs() << "error: Can't open file " << FileName << ": " <<
EC.message() << "\n";
return false;
}
if (FirstFile)
FirstFile = false;
else
OS << "\n";
OS << "< " << FileName << "\n";
// Figure out how many characters we need for the vectorization factors
// and similar.
OptReportLocationInfo MaxLI;
for (auto &FI : FileInfo)
for (auto &LI : FI.second)
MaxLI |= LI.second;
unsigned VFDigits = llvm::utostr(MaxLI.VectorizationFactor).size();
unsigned ICDigits = llvm::utostr(MaxLI.InterleaveCount).size();
unsigned UCDigits = llvm::utostr(MaxLI.UnrollCount).size();
// Figure out how many characters we need for the line numbers.
int64_t NumLines = 0;
for (line_iterator LI(*Buf.get(), false); LI != line_iterator(); ++LI)
++NumLines;
unsigned LNDigits = llvm::utostr(NumLines).size();
for (line_iterator LI(*Buf.get(), false); LI != line_iterator(); ++LI) {
int64_t L = LI.line_number();
OptReportLocationInfo LLI;
std::map<int, OptReportLocationInfo> ColsInfo;
unsigned InlinedCols = 0, UnrolledCols = 0, VectorizedCols = 0;
auto LII = FileInfo.find(L);
if (LII != FileInfo.end()) {
const auto &LineInfo = LII->second;
for (auto &CI : LineInfo) {
int Col = CI.first;
ColsInfo[Col] = CI.second;
InlinedCols += CI.second.Inlined.Analyzed;
UnrolledCols += CI.second.Unrolled.Analyzed;
VectorizedCols += CI.second.Vectorized.Analyzed;
LLI |= CI.second;
}
}
// We try to keep the output as concise as possible. If only one thing on
// a given line could have been inlined, vectorized, etc. then we can put
// the marker on the source line itself. If there are multiple options
// then we want to distinguish them by placing the marker for each
// transformation on a separate line following the source line. When we
// do this, we use a '^' character to point to the appropriate column in
// the source line.
std::string USpaces(Succinct ? 0 : UCDigits, ' ');
std::string VSpaces(Succinct ? 0 : VFDigits + ICDigits + 1, ' ');
auto UStr = [UCDigits](OptReportLocationInfo &LLI) {
std::string R;
raw_string_ostream RS(R);
if (!Succinct)
RS << llvm::format_decimal(LLI.UnrollCount, UCDigits);
return RS.str();
};
auto VStr = [VFDigits,
ICDigits](OptReportLocationInfo &LLI) -> std::string {
std::string R;
raw_string_ostream RS(R);
if (!Succinct)
RS << llvm::format_decimal(LLI.VectorizationFactor, VFDigits) <<
"," << llvm::format_decimal(LLI.InterleaveCount, ICDigits);
return RS.str();
};
OS << llvm::format_decimal(L + 1, LNDigits) << " ";
OS << (LLI.Inlined.Transformed && InlinedCols < 2 ? "I" : " ");
OS << (LLI.Unrolled.Transformed && UnrolledCols < 2 ?
"U" + UStr(LLI) : " " + USpaces);
OS << (LLI.Vectorized.Transformed && VectorizedCols < 2 ?
"V" + VStr(LLI) : " " + VSpaces);
OS << " | " << *LI << "\n";
for (auto &J : ColsInfo) {
if ((J.second.Inlined.Transformed && InlinedCols > 1) ||
(J.second.Unrolled.Transformed && UnrolledCols > 1) ||
(J.second.Vectorized.Transformed && VectorizedCols > 1)) {
OS << std::string(LNDigits + 1, ' ');
OS << (J.second.Inlined.Transformed &&
InlinedCols > 1 ? "I" : " ");
OS << (J.second.Unrolled.Transformed &&
UnrolledCols > 1 ? "U" + UStr(J.second) : " " + USpaces);
OS << (J.second.Vectorized.Transformed &&
VectorizedCols > 1 ? "V" + VStr(J.second) : " " + VSpaces);
OS << " | " << std::string(J.first - 1, ' ') << "^\n";
}
}
}
}
return true;
}
int main(int argc, const char **argv) {
sys::PrintStackTraceOnErrorSignal(argv[0]);
cl::HideUnrelatedOptions(OptReportCategory);
cl::ParseCommandLineOptions(
argc, argv,
"A tool to generate an optimization report from YAML optimization"
" record files.\n");
if (Help)
cl::PrintHelpMessage();
LocationInfoTy LocationInfo;
if (!readLocationInfo(LocationInfo))
return 1;
if (!writeReport(LocationInfo))
return 1;
return 0;
}
|