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