1 //===------------------ llvm-opt-report/OptReport.cpp ---------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 ///
10 /// \file
11 /// This file implements a tool that can parse the YAML optimization
12 /// records and generate an optimization summary annotated source listing
13 /// report.
14 ///
15 //===----------------------------------------------------------------------===//
16
17 #include "llvm/Demangle/Demangle.h"
18 #include "llvm/Support/CommandLine.h"
19 #include "llvm/Support/Error.h"
20 #include "llvm/Support/ErrorOr.h"
21 #include "llvm/Support/FileSystem.h"
22 #include "llvm/Support/Format.h"
23 #include "llvm/Support/InitLLVM.h"
24 #include "llvm/Support/LineIterator.h"
25 #include "llvm/Support/MemoryBuffer.h"
26 #include "llvm/Support/Path.h"
27 #include "llvm/Support/Program.h"
28 #include "llvm/Support/WithColor.h"
29 #include "llvm/Support/YAMLTraits.h"
30 #include "llvm/Support/raw_ostream.h"
31 #include <cstdlib>
32 #include <map>
33 #include <set>
34
35 using namespace llvm;
36 using namespace llvm::yaml;
37
38 static cl::opt<bool> Help("h", cl::desc("Alias for -help"), cl::Hidden);
39
40 // Mark all our options with this category, everything else (except for -version
41 // and -help) will be hidden.
42 static cl::OptionCategory
43 OptReportCategory("llvm-opt-report options");
44
45 static cl::opt<std::string>
46 InputFileName(cl::Positional, cl::desc("<input>"), cl::init("-"),
47 cl::cat(OptReportCategory));
48
49 static cl::opt<std::string>
50 OutputFileName("o", cl::desc("Output file"), cl::init("-"),
51 cl::cat(OptReportCategory));
52
53 static cl::opt<std::string>
54 InputRelDir("r", cl::desc("Root for relative input paths"), cl::init(""),
55 cl::cat(OptReportCategory));
56
57 static cl::opt<bool>
58 Succinct("s", cl::desc("Don't include vectorization factors, etc."),
59 cl::init(false), cl::cat(OptReportCategory));
60
61 static cl::opt<bool>
62 NoDemangle("no-demangle", cl::desc("Don't demangle function names"),
63 cl::init(false), cl::cat(OptReportCategory));
64
65 namespace {
66 // For each location in the source file, the common per-transformation state
67 // collected.
68 struct OptReportLocationItemInfo {
69 bool Analyzed = false;
70 bool Transformed = false;
71
operator |=__anon7759fb260111::OptReportLocationItemInfo72 OptReportLocationItemInfo &operator |= (
73 const OptReportLocationItemInfo &RHS) {
74 Analyzed |= RHS.Analyzed;
75 Transformed |= RHS.Transformed;
76
77 return *this;
78 }
79
operator <__anon7759fb260111::OptReportLocationItemInfo80 bool operator < (const OptReportLocationItemInfo &RHS) const {
81 if (Analyzed < RHS.Analyzed)
82 return true;
83 else if (Analyzed > RHS.Analyzed)
84 return false;
85 else if (Transformed < RHS.Transformed)
86 return true;
87 return false;
88 }
89 };
90
91 // The per-location information collected for producing an optimization report.
92 struct OptReportLocationInfo {
93 OptReportLocationItemInfo Inlined;
94 OptReportLocationItemInfo Unrolled;
95 OptReportLocationItemInfo Vectorized;
96
97 int VectorizationFactor = 1;
98 int InterleaveCount = 1;
99 int UnrollCount = 1;
100
operator |=__anon7759fb260111::OptReportLocationInfo101 OptReportLocationInfo &operator |= (const OptReportLocationInfo &RHS) {
102 Inlined |= RHS.Inlined;
103 Unrolled |= RHS.Unrolled;
104 Vectorized |= RHS.Vectorized;
105
106 VectorizationFactor =
107 std::max(VectorizationFactor, RHS.VectorizationFactor);
108 InterleaveCount = std::max(InterleaveCount, RHS.InterleaveCount);
109 UnrollCount = std::max(UnrollCount, RHS.UnrollCount);
110
111 return *this;
112 }
113
operator <__anon7759fb260111::OptReportLocationInfo114 bool operator < (const OptReportLocationInfo &RHS) const {
115 if (Inlined < RHS.Inlined)
116 return true;
117 else if (RHS.Inlined < Inlined)
118 return false;
119 else if (Unrolled < RHS.Unrolled)
120 return true;
121 else if (RHS.Unrolled < Unrolled)
122 return false;
123 else if (Vectorized < RHS.Vectorized)
124 return true;
125 else if (RHS.Vectorized < Vectorized || Succinct)
126 return false;
127 else if (VectorizationFactor < RHS.VectorizationFactor)
128 return true;
129 else if (VectorizationFactor > RHS.VectorizationFactor)
130 return false;
131 else if (InterleaveCount < RHS.InterleaveCount)
132 return true;
133 else if (InterleaveCount > RHS.InterleaveCount)
134 return false;
135 else if (UnrollCount < RHS.UnrollCount)
136 return true;
137 return false;
138 }
139 };
140
141 typedef std::map<std::string, std::map<int, std::map<std::string, std::map<int,
142 OptReportLocationInfo>>>> LocationInfoTy;
143 } // anonymous namespace
144
collectLocationInfo(yaml::Stream & Stream,LocationInfoTy & LocationInfo)145 static void collectLocationInfo(yaml::Stream &Stream,
146 LocationInfoTy &LocationInfo) {
147 SmallVector<char, 8> Tmp;
148
149 // Note: We're using the YAML parser here directly, instead of using the
150 // YAMLTraits implementation, because the YAMLTraits implementation does not
151 // support a way to handle only a subset of the input keys (it will error out
152 // if there is an input key that you don't map to your class), and
153 // furthermore, it does not provide a way to handle the Args sequence of
154 // key/value pairs, where the order must be captured and the 'String' key
155 // might be repeated.
156 for (auto &Doc : Stream) {
157 auto *Root = dyn_cast<yaml::MappingNode>(Doc.getRoot());
158 if (!Root)
159 continue;
160
161 bool Transformed = Root->getRawTag() == "!Passed";
162 std::string Pass, File, Function;
163 int Line = 0, Column = 1;
164
165 int VectorizationFactor = 1;
166 int InterleaveCount = 1;
167 int UnrollCount = 1;
168
169 for (auto &RootChild : *Root) {
170 auto *Key = dyn_cast<yaml::ScalarNode>(RootChild.getKey());
171 if (!Key)
172 continue;
173 StringRef KeyName = Key->getValue(Tmp);
174 if (KeyName == "Pass") {
175 auto *Value = dyn_cast<yaml::ScalarNode>(RootChild.getValue());
176 if (!Value)
177 continue;
178 Pass = Value->getValue(Tmp);
179 } else if (KeyName == "Function") {
180 auto *Value = dyn_cast<yaml::ScalarNode>(RootChild.getValue());
181 if (!Value)
182 continue;
183 Function = Value->getValue(Tmp);
184 } else if (KeyName == "DebugLoc") {
185 auto *DebugLoc = dyn_cast<yaml::MappingNode>(RootChild.getValue());
186 if (!DebugLoc)
187 continue;
188
189 for (auto &DLChild : *DebugLoc) {
190 auto *DLKey = dyn_cast<yaml::ScalarNode>(DLChild.getKey());
191 if (!DLKey)
192 continue;
193 StringRef DLKeyName = DLKey->getValue(Tmp);
194 if (DLKeyName == "File") {
195 auto *Value = dyn_cast<yaml::ScalarNode>(DLChild.getValue());
196 if (!Value)
197 continue;
198 File = Value->getValue(Tmp);
199 } else if (DLKeyName == "Line") {
200 auto *Value = dyn_cast<yaml::ScalarNode>(DLChild.getValue());
201 if (!Value)
202 continue;
203 Value->getValue(Tmp).getAsInteger(10, Line);
204 } else if (DLKeyName == "Column") {
205 auto *Value = dyn_cast<yaml::ScalarNode>(DLChild.getValue());
206 if (!Value)
207 continue;
208 Value->getValue(Tmp).getAsInteger(10, Column);
209 }
210 }
211 } else if (KeyName == "Args") {
212 auto *Args = dyn_cast<yaml::SequenceNode>(RootChild.getValue());
213 if (!Args)
214 continue;
215 for (auto &ArgChild : *Args) {
216 auto *ArgMap = dyn_cast<yaml::MappingNode>(&ArgChild);
217 if (!ArgMap)
218 continue;
219 for (auto &ArgKV : *ArgMap) {
220 auto *ArgKey = dyn_cast<yaml::ScalarNode>(ArgKV.getKey());
221 if (!ArgKey)
222 continue;
223 StringRef ArgKeyName = ArgKey->getValue(Tmp);
224 if (ArgKeyName == "VectorizationFactor") {
225 auto *Value = dyn_cast<yaml::ScalarNode>(ArgKV.getValue());
226 if (!Value)
227 continue;
228 Value->getValue(Tmp).getAsInteger(10, VectorizationFactor);
229 } else if (ArgKeyName == "InterleaveCount") {
230 auto *Value = dyn_cast<yaml::ScalarNode>(ArgKV.getValue());
231 if (!Value)
232 continue;
233 Value->getValue(Tmp).getAsInteger(10, InterleaveCount);
234 } else if (ArgKeyName == "UnrollCount") {
235 auto *Value = dyn_cast<yaml::ScalarNode>(ArgKV.getValue());
236 if (!Value)
237 continue;
238 Value->getValue(Tmp).getAsInteger(10, UnrollCount);
239 }
240 }
241 }
242 }
243 }
244
245 if (Line < 1 || File.empty())
246 continue;
247
248 // We track information on both actual and potential transformations. This
249 // way, if there are multiple possible things on a line that are, or could
250 // have been transformed, we can indicate that explicitly in the output.
251 auto UpdateLLII = [Transformed](OptReportLocationItemInfo &LLII) {
252 LLII.Analyzed = true;
253 if (Transformed)
254 LLII.Transformed = true;
255 };
256
257 if (Pass == "inline") {
258 auto &LI = LocationInfo[File][Line][Function][Column];
259 UpdateLLII(LI.Inlined);
260 } else if (Pass == "loop-unroll") {
261 auto &LI = LocationInfo[File][Line][Function][Column];
262 LI.UnrollCount = UnrollCount;
263 UpdateLLII(LI.Unrolled);
264 } else if (Pass == "loop-vectorize") {
265 auto &LI = LocationInfo[File][Line][Function][Column];
266 LI.VectorizationFactor = VectorizationFactor;
267 LI.InterleaveCount = InterleaveCount;
268 UpdateLLII(LI.Vectorized);
269 }
270 }
271 }
272
readLocationInfo(LocationInfoTy & LocationInfo)273 static bool readLocationInfo(LocationInfoTy &LocationInfo) {
274 ErrorOr<std::unique_ptr<MemoryBuffer>> Buf =
275 MemoryBuffer::getFileOrSTDIN(InputFileName);
276 if (std::error_code EC = Buf.getError()) {
277 WithColor::error() << "Can't open file " << InputFileName << ": "
278 << EC.message() << "\n";
279 return false;
280 }
281
282 SourceMgr SM;
283 yaml::Stream Stream(Buf.get()->getBuffer(), SM);
284 collectLocationInfo(Stream, LocationInfo);
285
286 return true;
287 }
288
writeReport(LocationInfoTy & LocationInfo)289 static bool writeReport(LocationInfoTy &LocationInfo) {
290 std::error_code EC;
291 llvm::raw_fd_ostream OS(OutputFileName, EC,
292 llvm::sys::fs::F_Text);
293 if (EC) {
294 WithColor::error() << "Can't open file " << OutputFileName << ": "
295 << EC.message() << "\n";
296 return false;
297 }
298
299 bool FirstFile = true;
300 for (auto &FI : LocationInfo) {
301 SmallString<128> FileName(FI.first);
302 if (!InputRelDir.empty()) {
303 if (std::error_code EC = sys::fs::make_absolute(InputRelDir, FileName)) {
304 WithColor::error() << "Can't resolve file path to " << FileName << ": "
305 << EC.message() << "\n";
306 return false;
307 }
308 }
309
310 const auto &FileInfo = FI.second;
311
312 ErrorOr<std::unique_ptr<MemoryBuffer>> Buf =
313 MemoryBuffer::getFile(FileName);
314 if (std::error_code EC = Buf.getError()) {
315 WithColor::error() << "Can't open file " << FileName << ": "
316 << EC.message() << "\n";
317 return false;
318 }
319
320 if (FirstFile)
321 FirstFile = false;
322 else
323 OS << "\n";
324
325 OS << "< " << FileName << "\n";
326
327 // Figure out how many characters we need for the vectorization factors
328 // and similar.
329 OptReportLocationInfo MaxLI;
330 for (auto &FLI : FileInfo)
331 for (auto &FI : FLI.second)
332 for (auto &LI : FI.second)
333 MaxLI |= LI.second;
334
335 bool NothingInlined = !MaxLI.Inlined.Transformed;
336 bool NothingUnrolled = !MaxLI.Unrolled.Transformed;
337 bool NothingVectorized = !MaxLI.Vectorized.Transformed;
338
339 unsigned VFDigits = llvm::utostr(MaxLI.VectorizationFactor).size();
340 unsigned ICDigits = llvm::utostr(MaxLI.InterleaveCount).size();
341 unsigned UCDigits = llvm::utostr(MaxLI.UnrollCount).size();
342
343 // Figure out how many characters we need for the line numbers.
344 int64_t NumLines = 0;
345 for (line_iterator LI(*Buf.get(), false); LI != line_iterator(); ++LI)
346 ++NumLines;
347
348 unsigned LNDigits = llvm::utostr(NumLines).size();
349
350 for (line_iterator LI(*Buf.get(), false); LI != line_iterator(); ++LI) {
351 int64_t L = LI.line_number();
352 auto LII = FileInfo.find(L);
353
354 auto PrintLine = [&](bool PrintFuncName,
355 const std::set<std::string> &FuncNameSet) {
356 OptReportLocationInfo LLI;
357
358 std::map<int, OptReportLocationInfo> ColsInfo;
359 unsigned InlinedCols = 0, UnrolledCols = 0, VectorizedCols = 0;
360
361 if (LII != FileInfo.end() && !FuncNameSet.empty()) {
362 const auto &LineInfo = LII->second;
363
364 for (auto &CI : LineInfo.find(*FuncNameSet.begin())->second) {
365 int Col = CI.first;
366 ColsInfo[Col] = CI.second;
367 InlinedCols += CI.second.Inlined.Analyzed;
368 UnrolledCols += CI.second.Unrolled.Analyzed;
369 VectorizedCols += CI.second.Vectorized.Analyzed;
370 LLI |= CI.second;
371 }
372 }
373
374 if (PrintFuncName) {
375 OS << " > ";
376
377 bool FirstFunc = true;
378 for (const auto &FuncName : FuncNameSet) {
379 if (FirstFunc)
380 FirstFunc = false;
381 else
382 OS << ", ";
383
384 bool Printed = false;
385 if (!NoDemangle) {
386 int Status = 0;
387 char *Demangled =
388 itaniumDemangle(FuncName.c_str(), nullptr, nullptr, &Status);
389 if (Demangled && Status == 0) {
390 OS << Demangled;
391 Printed = true;
392 }
393
394 if (Demangled)
395 std::free(Demangled);
396 }
397
398 if (!Printed)
399 OS << FuncName;
400 }
401
402 OS << ":\n";
403 }
404
405 // We try to keep the output as concise as possible. If only one thing on
406 // a given line could have been inlined, vectorized, etc. then we can put
407 // the marker on the source line itself. If there are multiple options
408 // then we want to distinguish them by placing the marker for each
409 // transformation on a separate line following the source line. When we
410 // do this, we use a '^' character to point to the appropriate column in
411 // the source line.
412
413 std::string USpaces(Succinct ? 0 : UCDigits, ' ');
414 std::string VSpaces(Succinct ? 0 : VFDigits + ICDigits + 1, ' ');
415
416 auto UStr = [UCDigits](OptReportLocationInfo &LLI) {
417 std::string R;
418 raw_string_ostream RS(R);
419
420 if (!Succinct) {
421 RS << LLI.UnrollCount;
422 RS << std::string(UCDigits - RS.str().size(), ' ');
423 }
424
425 return RS.str();
426 };
427
428 auto VStr = [VFDigits,
429 ICDigits](OptReportLocationInfo &LLI) -> std::string {
430 std::string R;
431 raw_string_ostream RS(R);
432
433 if (!Succinct) {
434 RS << LLI.VectorizationFactor << "," << LLI.InterleaveCount;
435 RS << std::string(VFDigits + ICDigits + 1 - RS.str().size(), ' ');
436 }
437
438 return RS.str();
439 };
440
441 OS << llvm::format_decimal(L, LNDigits) << " ";
442 OS << (LLI.Inlined.Transformed && InlinedCols < 2 ? "I" :
443 (NothingInlined ? "" : " "));
444 OS << (LLI.Unrolled.Transformed && UnrolledCols < 2 ?
445 "U" + UStr(LLI) : (NothingUnrolled ? "" : " " + USpaces));
446 OS << (LLI.Vectorized.Transformed && VectorizedCols < 2 ?
447 "V" + VStr(LLI) : (NothingVectorized ? "" : " " + VSpaces));
448
449 OS << " | " << *LI << "\n";
450
451 for (auto &J : ColsInfo) {
452 if ((J.second.Inlined.Transformed && InlinedCols > 1) ||
453 (J.second.Unrolled.Transformed && UnrolledCols > 1) ||
454 (J.second.Vectorized.Transformed && VectorizedCols > 1)) {
455 OS << std::string(LNDigits + 1, ' ');
456 OS << (J.second.Inlined.Transformed &&
457 InlinedCols > 1 ? "I" : (NothingInlined ? "" : " "));
458 OS << (J.second.Unrolled.Transformed &&
459 UnrolledCols > 1 ? "U" + UStr(J.second) :
460 (NothingUnrolled ? "" : " " + USpaces));
461 OS << (J.second.Vectorized.Transformed &&
462 VectorizedCols > 1 ? "V" + VStr(J.second) :
463 (NothingVectorized ? "" : " " + VSpaces));
464
465 OS << " | " << std::string(J.first - 1, ' ') << "^\n";
466 }
467 }
468 };
469
470 // We need to figure out if the optimizations for this line were the same
471 // in each function context. If not, then we want to group the similar
472 // function contexts together and display each group separately. If
473 // they're all the same, then we only display the line once without any
474 // additional markings.
475 std::map<std::map<int, OptReportLocationInfo>,
476 std::set<std::string>> UniqueLIs;
477
478 OptReportLocationInfo AllLI;
479 if (LII != FileInfo.end()) {
480 const auto &FuncLineInfo = LII->second;
481 for (const auto &FLII : FuncLineInfo) {
482 UniqueLIs[FLII.second].insert(FLII.first);
483
484 for (const auto &OI : FLII.second)
485 AllLI |= OI.second;
486 }
487 }
488
489 bool NothingHappened = !AllLI.Inlined.Transformed &&
490 !AllLI.Unrolled.Transformed &&
491 !AllLI.Vectorized.Transformed;
492 if (UniqueLIs.size() > 1 && !NothingHappened) {
493 OS << " [[\n";
494 for (const auto &FSLI : UniqueLIs)
495 PrintLine(true, FSLI.second);
496 OS << " ]]\n";
497 } else if (UniqueLIs.size() == 1) {
498 PrintLine(false, UniqueLIs.begin()->second);
499 } else {
500 PrintLine(false, std::set<std::string>());
501 }
502 }
503 }
504
505 return true;
506 }
507
main(int argc,const char ** argv)508 int main(int argc, const char **argv) {
509 InitLLVM X(argc, argv);
510
511 cl::HideUnrelatedOptions(OptReportCategory);
512 cl::ParseCommandLineOptions(
513 argc, argv,
514 "A tool to generate an optimization report from YAML optimization"
515 " record files.\n");
516
517 if (Help) {
518 cl::PrintHelpMessage();
519 return 0;
520 }
521
522 LocationInfoTy LocationInfo;
523 if (!readLocationInfo(LocationInfo))
524 return 1;
525 if (!writeReport(LocationInfo))
526 return 1;
527
528 return 0;
529 }
530