• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===------------------ llvm-opt-report/OptReport.cpp ---------------------===//
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 /// \file
10 /// This file implements a tool that can parse the YAML optimization
11 /// records and generate an optimization summary annotated source listing
12 /// report.
13 ///
14 //===----------------------------------------------------------------------===//
15 
16 #include "llvm-c/Remarks.h"
17 #include "llvm/Demangle/Demangle.h"
18 #include "llvm/Remarks/RemarkFormat.h"
19 #include "llvm/Remarks/RemarkParser.h"
20 #include "llvm/Support/CommandLine.h"
21 #include "llvm/Support/Error.h"
22 #include "llvm/Support/ErrorOr.h"
23 #include "llvm/Support/FileSystem.h"
24 #include "llvm/Support/Format.h"
25 #include "llvm/Support/InitLLVM.h"
26 #include "llvm/Support/LineIterator.h"
27 #include "llvm/Support/MemoryBuffer.h"
28 #include "llvm/Support/Path.h"
29 #include "llvm/Support/Program.h"
30 #include "llvm/Support/WithColor.h"
31 #include "llvm/Support/raw_ostream.h"
32 #include <cstdlib>
33 #include <map>
34 #include <set>
35 
36 using namespace llvm;
37 
38 // Mark all our options with this category, everything else (except for -version
39 // and -help) will be hidden.
40 static cl::OptionCategory
41     OptReportCategory("llvm-opt-report options");
42 
43 static cl::opt<std::string>
44   InputFileName(cl::Positional, cl::desc("<input>"), cl::init("-"),
45                 cl::cat(OptReportCategory));
46 
47 static cl::opt<std::string>
48   OutputFileName("o", cl::desc("Output file"), cl::init("-"),
49                  cl::cat(OptReportCategory));
50 
51 static cl::opt<std::string>
52   InputRelDir("r", cl::desc("Root for relative input paths"), cl::init(""),
53               cl::cat(OptReportCategory));
54 
55 static cl::opt<bool>
56   Succinct("s", cl::desc("Don't include vectorization factors, etc."),
57            cl::init(false), cl::cat(OptReportCategory));
58 
59 static cl::opt<bool>
60   NoDemangle("no-demangle", cl::desc("Don't demangle function names"),
61              cl::init(false), cl::cat(OptReportCategory));
62 
63 static cl::opt<std::string> ParserFormat("format",
64                                          cl::desc("The format of the remarks."),
65                                          cl::init("yaml"),
66                                          cl::cat(OptReportCategory));
67 
68 namespace {
69 // For each location in the source file, the common per-transformation state
70 // collected.
71 struct OptReportLocationItemInfo {
72   bool Analyzed = false;
73   bool Transformed = false;
74 
operator |=__anona7b966630111::OptReportLocationItemInfo75   OptReportLocationItemInfo &operator |= (
76     const OptReportLocationItemInfo &RHS) {
77     Analyzed |= RHS.Analyzed;
78     Transformed |= RHS.Transformed;
79 
80     return *this;
81   }
82 
operator <__anona7b966630111::OptReportLocationItemInfo83   bool operator < (const OptReportLocationItemInfo &RHS) const {
84     if (Analyzed < RHS.Analyzed)
85       return true;
86     else if (Analyzed > RHS.Analyzed)
87       return false;
88     else if (Transformed < RHS.Transformed)
89       return true;
90     return false;
91   }
92 };
93 
94 // The per-location information collected for producing an optimization report.
95 struct OptReportLocationInfo {
96   OptReportLocationItemInfo Inlined;
97   OptReportLocationItemInfo Unrolled;
98   OptReportLocationItemInfo Vectorized;
99 
100   int VectorizationFactor = 1;
101   int InterleaveCount = 1;
102   int UnrollCount = 1;
103 
operator |=__anona7b966630111::OptReportLocationInfo104   OptReportLocationInfo &operator |= (const OptReportLocationInfo &RHS) {
105     Inlined |= RHS.Inlined;
106     Unrolled |= RHS.Unrolled;
107     Vectorized |= RHS.Vectorized;
108 
109     VectorizationFactor =
110       std::max(VectorizationFactor, RHS.VectorizationFactor);
111     InterleaveCount = std::max(InterleaveCount, RHS.InterleaveCount);
112     UnrollCount = std::max(UnrollCount, RHS.UnrollCount);
113 
114     return *this;
115   }
116 
operator <__anona7b966630111::OptReportLocationInfo117   bool operator < (const OptReportLocationInfo &RHS) const {
118     if (Inlined < RHS.Inlined)
119       return true;
120     else if (RHS.Inlined < Inlined)
121       return false;
122     else if (Unrolled < RHS.Unrolled)
123       return true;
124     else if (RHS.Unrolled < Unrolled)
125       return false;
126     else if (Vectorized < RHS.Vectorized)
127       return true;
128     else if (RHS.Vectorized < Vectorized || Succinct)
129       return false;
130     else if (VectorizationFactor < RHS.VectorizationFactor)
131       return true;
132     else if (VectorizationFactor > RHS.VectorizationFactor)
133       return false;
134     else if (InterleaveCount < RHS.InterleaveCount)
135       return true;
136     else if (InterleaveCount > RHS.InterleaveCount)
137       return false;
138     else if (UnrollCount < RHS.UnrollCount)
139       return true;
140     return false;
141   }
142 };
143 
144 typedef std::map<std::string, std::map<int, std::map<std::string, std::map<int,
145           OptReportLocationInfo>>>> LocationInfoTy;
146 } // anonymous namespace
147 
readLocationInfo(LocationInfoTy & LocationInfo)148 static bool readLocationInfo(LocationInfoTy &LocationInfo) {
149   ErrorOr<std::unique_ptr<MemoryBuffer>> Buf =
150       MemoryBuffer::getFile(InputFileName.c_str());
151   if (std::error_code EC = Buf.getError()) {
152     WithColor::error() << "Can't open file " << InputFileName << ": "
153                        << EC.message() << "\n";
154     return false;
155   }
156 
157   Expected<remarks::Format> Format = remarks::parseFormat(ParserFormat);
158   if (!Format) {
159     handleAllErrors(Format.takeError(), [&](const ErrorInfoBase &PE) {
160       PE.log(WithColor::error());
161       errs() << '\n';
162     });
163     return false;
164   }
165 
166   Expected<std::unique_ptr<remarks::RemarkParser>> MaybeParser =
167       remarks::createRemarkParserFromMeta(*Format, (*Buf)->getBuffer());
168   if (!MaybeParser) {
169     handleAllErrors(MaybeParser.takeError(), [&](const ErrorInfoBase &PE) {
170       PE.log(WithColor::error());
171       errs() << '\n';
172     });
173     return false;
174   }
175   remarks::RemarkParser &Parser = **MaybeParser;
176 
177   while (true) {
178     Expected<std::unique_ptr<remarks::Remark>> MaybeRemark = Parser.next();
179     if (!MaybeRemark) {
180       Error E = MaybeRemark.takeError();
181       if (E.isA<remarks::EndOfFileError>()) {
182         // EOF.
183         consumeError(std::move(E));
184         break;
185       }
186       handleAllErrors(std::move(E), [&](const ErrorInfoBase &PE) {
187         PE.log(WithColor::error());
188         errs() << '\n';
189       });
190       return false;
191     }
192 
193     const remarks::Remark &Remark = **MaybeRemark;
194 
195     bool Transformed = Remark.RemarkType == remarks::Type::Passed;
196 
197     int VectorizationFactor = 1;
198     int InterleaveCount = 1;
199     int UnrollCount = 1;
200 
201     for (const remarks::Argument &Arg : Remark.Args) {
202       if (Arg.Key == "VectorizationFactor")
203         Arg.Val.getAsInteger(10, VectorizationFactor);
204       else if (Arg.Key == "InterleaveCount")
205         Arg.Val.getAsInteger(10, InterleaveCount);
206       else if (Arg.Key == "UnrollCount")
207         Arg.Val.getAsInteger(10, UnrollCount);
208     }
209 
210     const Optional<remarks::RemarkLocation> &Loc = Remark.Loc;
211     if (!Loc)
212       continue;
213 
214     StringRef File = Loc->SourceFilePath;
215     unsigned Line = Loc->SourceLine;
216     unsigned Column = Loc->SourceColumn;
217 
218     // We track information on both actual and potential transformations. This
219     // way, if there are multiple possible things on a line that are, or could
220     // have been transformed, we can indicate that explicitly in the output.
221     auto UpdateLLII = [Transformed](OptReportLocationItemInfo &LLII) {
222       LLII.Analyzed = true;
223       if (Transformed)
224         LLII.Transformed = true;
225     };
226 
227     if (Remark.PassName == "inline") {
228       auto &LI = LocationInfo[std::string(File)][Line]
229                              [std::string(Remark.FunctionName)][Column];
230       UpdateLLII(LI.Inlined);
231     } else if (Remark.PassName == "loop-unroll") {
232       auto &LI = LocationInfo[std::string(File)][Line]
233                              [std::string(Remark.FunctionName)][Column];
234       LI.UnrollCount = UnrollCount;
235       UpdateLLII(LI.Unrolled);
236     } else if (Remark.PassName == "loop-vectorize") {
237       auto &LI = LocationInfo[std::string(File)][Line]
238                              [std::string(Remark.FunctionName)][Column];
239       LI.VectorizationFactor = VectorizationFactor;
240       LI.InterleaveCount = InterleaveCount;
241       UpdateLLII(LI.Vectorized);
242     }
243   }
244 
245   return true;
246 }
247 
writeReport(LocationInfoTy & LocationInfo)248 static bool writeReport(LocationInfoTy &LocationInfo) {
249   std::error_code EC;
250   llvm::raw_fd_ostream OS(OutputFileName, EC, llvm::sys::fs::OF_Text);
251   if (EC) {
252     WithColor::error() << "Can't open file " << OutputFileName << ": "
253                        << EC.message() << "\n";
254     return false;
255   }
256 
257   bool FirstFile = true;
258   for (auto &FI : LocationInfo) {
259     SmallString<128> FileName(FI.first);
260     if (!InputRelDir.empty())
261       sys::fs::make_absolute(InputRelDir, FileName);
262 
263     const auto &FileInfo = FI.second;
264 
265     ErrorOr<std::unique_ptr<MemoryBuffer>> Buf =
266         MemoryBuffer::getFile(FileName);
267     if (std::error_code EC = Buf.getError()) {
268       WithColor::error() << "Can't open file " << FileName << ": "
269                          << EC.message() << "\n";
270       return false;
271     }
272 
273     if (FirstFile)
274       FirstFile = false;
275     else
276       OS << "\n";
277 
278     OS << "< " << FileName << "\n";
279 
280     // Figure out how many characters we need for the vectorization factors
281     // and similar.
282     OptReportLocationInfo MaxLI;
283     for (auto &FLI : FileInfo)
284       for (auto &FI : FLI.second)
285         for (auto &LI : FI.second)
286           MaxLI |= LI.second;
287 
288     bool NothingInlined = !MaxLI.Inlined.Transformed;
289     bool NothingUnrolled = !MaxLI.Unrolled.Transformed;
290     bool NothingVectorized = !MaxLI.Vectorized.Transformed;
291 
292     unsigned VFDigits = llvm::utostr(MaxLI.VectorizationFactor).size();
293     unsigned ICDigits = llvm::utostr(MaxLI.InterleaveCount).size();
294     unsigned UCDigits = llvm::utostr(MaxLI.UnrollCount).size();
295 
296     // Figure out how many characters we need for the line numbers.
297     int64_t NumLines = 0;
298     for (line_iterator LI(*Buf.get(), false); LI != line_iterator(); ++LI)
299       ++NumLines;
300 
301     unsigned LNDigits = llvm::utostr(NumLines).size();
302 
303     for (line_iterator LI(*Buf.get(), false); LI != line_iterator(); ++LI) {
304       int64_t L = LI.line_number();
305       auto LII = FileInfo.find(L);
306 
307       auto PrintLine = [&](bool PrintFuncName,
308                            const std::set<std::string> &FuncNameSet) {
309         OptReportLocationInfo LLI;
310 
311         std::map<int, OptReportLocationInfo> ColsInfo;
312         unsigned InlinedCols = 0, UnrolledCols = 0, VectorizedCols = 0;
313 
314         if (LII != FileInfo.end() && !FuncNameSet.empty()) {
315           const auto &LineInfo = LII->second;
316 
317           for (auto &CI : LineInfo.find(*FuncNameSet.begin())->second) {
318             int Col = CI.first;
319             ColsInfo[Col] = CI.second;
320             InlinedCols += CI.second.Inlined.Analyzed;
321             UnrolledCols += CI.second.Unrolled.Analyzed;
322             VectorizedCols += CI.second.Vectorized.Analyzed;
323             LLI |= CI.second;
324           }
325         }
326 
327         if (PrintFuncName) {
328           OS << "  > ";
329 
330           bool FirstFunc = true;
331           for (const auto &FuncName : FuncNameSet) {
332             if (FirstFunc)
333               FirstFunc = false;
334             else
335               OS << ", ";
336 
337             bool Printed = false;
338             if (!NoDemangle) {
339               int Status = 0;
340               char *Demangled =
341                 itaniumDemangle(FuncName.c_str(), nullptr, nullptr, &Status);
342               if (Demangled && Status == 0) {
343                 OS << Demangled;
344                 Printed = true;
345               }
346 
347               if (Demangled)
348                 std::free(Demangled);
349             }
350 
351             if (!Printed)
352               OS << FuncName;
353           }
354 
355           OS << ":\n";
356         }
357 
358         // We try to keep the output as concise as possible. If only one thing on
359         // a given line could have been inlined, vectorized, etc. then we can put
360         // the marker on the source line itself. If there are multiple options
361         // then we want to distinguish them by placing the marker for each
362         // transformation on a separate line following the source line. When we
363         // do this, we use a '^' character to point to the appropriate column in
364         // the source line.
365 
366         std::string USpaces(Succinct ? 0 : UCDigits, ' ');
367         std::string VSpaces(Succinct ? 0 : VFDigits + ICDigits + 1, ' ');
368 
369         auto UStr = [UCDigits](OptReportLocationInfo &LLI) {
370           std::string R;
371           raw_string_ostream RS(R);
372 
373           if (!Succinct) {
374             RS << LLI.UnrollCount;
375             RS << std::string(UCDigits - RS.str().size(), ' ');
376           }
377 
378           return RS.str();
379         };
380 
381         auto VStr = [VFDigits,
382                      ICDigits](OptReportLocationInfo &LLI) -> std::string {
383           std::string R;
384           raw_string_ostream RS(R);
385 
386           if (!Succinct) {
387             RS << LLI.VectorizationFactor << "," << LLI.InterleaveCount;
388             RS << std::string(VFDigits + ICDigits + 1 - RS.str().size(), ' ');
389           }
390 
391           return RS.str();
392         };
393 
394         OS << llvm::format_decimal(L, LNDigits) << " ";
395         OS << (LLI.Inlined.Transformed && InlinedCols < 2 ? "I" :
396                 (NothingInlined ? "" : " "));
397         OS << (LLI.Unrolled.Transformed && UnrolledCols < 2 ?
398                 "U" + UStr(LLI) : (NothingUnrolled ? "" : " " + USpaces));
399         OS << (LLI.Vectorized.Transformed && VectorizedCols < 2 ?
400                 "V" + VStr(LLI) : (NothingVectorized ? "" : " " + VSpaces));
401 
402         OS << " | " << *LI << "\n";
403 
404         for (auto &J : ColsInfo) {
405           if ((J.second.Inlined.Transformed && InlinedCols > 1) ||
406               (J.second.Unrolled.Transformed && UnrolledCols > 1) ||
407               (J.second.Vectorized.Transformed && VectorizedCols > 1)) {
408             OS << std::string(LNDigits + 1, ' ');
409             OS << (J.second.Inlined.Transformed &&
410                    InlinedCols > 1 ? "I" : (NothingInlined ? "" : " "));
411             OS << (J.second.Unrolled.Transformed &&
412                    UnrolledCols > 1 ? "U" + UStr(J.second) :
413                      (NothingUnrolled ? "" : " " + USpaces));
414             OS << (J.second.Vectorized.Transformed &&
415                    VectorizedCols > 1 ? "V" + VStr(J.second) :
416                      (NothingVectorized ? "" : " " + VSpaces));
417 
418             OS << " | " << std::string(J.first - 1, ' ') << "^\n";
419           }
420         }
421       };
422 
423       // We need to figure out if the optimizations for this line were the same
424       // in each function context. If not, then we want to group the similar
425       // function contexts together and display each group separately. If
426       // they're all the same, then we only display the line once without any
427       // additional markings.
428       std::map<std::map<int, OptReportLocationInfo>,
429                std::set<std::string>> UniqueLIs;
430 
431       OptReportLocationInfo AllLI;
432       if (LII != FileInfo.end()) {
433         const auto &FuncLineInfo = LII->second;
434         for (const auto &FLII : FuncLineInfo) {
435           UniqueLIs[FLII.second].insert(FLII.first);
436 
437           for (const auto &OI : FLII.second)
438             AllLI |= OI.second;
439         }
440       }
441 
442       bool NothingHappened = !AllLI.Inlined.Transformed &&
443                              !AllLI.Unrolled.Transformed &&
444                              !AllLI.Vectorized.Transformed;
445       if (UniqueLIs.size() > 1 && !NothingHappened) {
446         OS << " [[\n";
447         for (const auto &FSLI : UniqueLIs)
448           PrintLine(true, FSLI.second);
449         OS << " ]]\n";
450       } else if (UniqueLIs.size() == 1) {
451         PrintLine(false, UniqueLIs.begin()->second);
452       } else {
453         PrintLine(false, std::set<std::string>());
454       }
455     }
456   }
457 
458   return true;
459 }
460 
main(int argc,const char ** argv)461 int main(int argc, const char **argv) {
462   InitLLVM X(argc, argv);
463 
464   cl::HideUnrelatedOptions(OptReportCategory);
465   cl::ParseCommandLineOptions(
466       argc, argv,
467       "A tool to generate an optimization report from YAML optimization"
468       " record files.\n");
469 
470   LocationInfoTy LocationInfo;
471   if (!readLocationInfo(LocationInfo))
472     return 1;
473   if (!writeReport(LocationInfo))
474     return 1;
475 
476   return 0;
477 }
478