Hal Finkel | 52031b7 | 2016-10-05 22:10:35 +0000 | [diff] [blame] | 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 | /// \brief 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/Support/CommandLine.h" |
Hal Finkel | 5aa0248 | 2016-10-05 22:25:33 +0000 | [diff] [blame] | 18 | #include "llvm/Demangle/Demangle.h" |
Hal Finkel | 52031b7 | 2016-10-05 22:10:35 +0000 | [diff] [blame] | 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/LineIterator.h" |
| 24 | #include "llvm/Support/FileSystem.h" |
| 25 | #include "llvm/Support/MemoryBuffer.h" |
| 26 | #include "llvm/Support/Path.h" |
| 27 | #include "llvm/Support/Program.h" |
| 28 | #include "llvm/Support/raw_ostream.h" |
| 29 | #include "llvm/Support/Signals.h" |
| 30 | #include "llvm/Support/YAMLTraits.h" |
Hal Finkel | 5aa0248 | 2016-10-05 22:25:33 +0000 | [diff] [blame] | 31 | #include <cstdlib> |
| 32 | #include <map> |
| 33 | #include <set> |
Hal Finkel | 52031b7 | 2016-10-05 22:10:35 +0000 | [diff] [blame] | 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 | |
Hal Finkel | 5aa0248 | 2016-10-05 22:25:33 +0000 | [diff] [blame] | 61 | static cl::opt<bool> |
Hal Finkel | 16d29e3 | 2016-10-07 01:30:59 +0000 | [diff] [blame] | 62 | NoDemangle("no-demangle", cl::desc("Don't demangle function names"), |
| 63 | cl::init(false), cl::cat(OptReportCategory)); |
Hal Finkel | 5aa0248 | 2016-10-05 22:25:33 +0000 | [diff] [blame] | 64 | |
Hal Finkel | 52031b7 | 2016-10-05 22:10:35 +0000 | [diff] [blame] | 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 | |
| 72 | OptReportLocationItemInfo &operator |= ( |
| 73 | const OptReportLocationItemInfo &RHS) { |
| 74 | Analyzed |= RHS.Analyzed; |
| 75 | Transformed |= RHS.Transformed; |
| 76 | |
| 77 | return *this; |
| 78 | } |
Hal Finkel | 5aa0248 | 2016-10-05 22:25:33 +0000 | [diff] [blame] | 79 | |
| 80 | 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 | } |
Hal Finkel | 52031b7 | 2016-10-05 22:10:35 +0000 | [diff] [blame] | 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 | |
| 101 | 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 | } |
Hal Finkel | 5aa0248 | 2016-10-05 22:25:33 +0000 | [diff] [blame] | 113 | |
| 114 | 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 (InterleaveCount < RHS.InterleaveCount) |
| 136 | return true; |
| 137 | return false; |
| 138 | } |
Hal Finkel | 52031b7 | 2016-10-05 22:10:35 +0000 | [diff] [blame] | 139 | }; |
| 140 | |
Hal Finkel | 5aa0248 | 2016-10-05 22:25:33 +0000 | [diff] [blame] | 141 | typedef std::map<std::string, std::map<int, std::map<std::string, std::map<int, |
| 142 | OptReportLocationInfo>>>> LocationInfoTy; |
Hal Finkel | 52031b7 | 2016-10-05 22:10:35 +0000 | [diff] [blame] | 143 | } // anonymous namespace |
| 144 | |
| 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"; |
Hal Finkel | 5aa0248 | 2016-10-05 22:25:33 +0000 | [diff] [blame] | 162 | std::string Pass, File, Function; |
Hal Finkel | 52031b7 | 2016-10-05 22:10:35 +0000 | [diff] [blame] | 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); |
Hal Finkel | 5aa0248 | 2016-10-05 22:25:33 +0000 | [diff] [blame] | 179 | } else if (KeyName == "Function") { |
| 180 | auto *Value = dyn_cast<yaml::ScalarNode>(RootChild.getValue()); |
| 181 | if (!Value) |
| 182 | continue; |
| 183 | Function = Value->getValue(Tmp); |
Hal Finkel | 52031b7 | 2016-10-05 22:10:35 +0000 | [diff] [blame] | 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. |
Hal Finkel | 4d6f308 | 2016-10-06 11:58:52 +0000 | [diff] [blame] | 251 | auto UpdateLLII = [Transformed](OptReportLocationItemInfo &LLII) { |
Hal Finkel | 52031b7 | 2016-10-05 22:10:35 +0000 | [diff] [blame] | 252 | LLII.Analyzed = true; |
Hal Finkel | 4d6f308 | 2016-10-06 11:58:52 +0000 | [diff] [blame] | 253 | if (Transformed) |
Hal Finkel | 52031b7 | 2016-10-05 22:10:35 +0000 | [diff] [blame] | 254 | LLII.Transformed = true; |
Hal Finkel | 52031b7 | 2016-10-05 22:10:35 +0000 | [diff] [blame] | 255 | }; |
| 256 | |
| 257 | if (Pass == "inline") { |
Hal Finkel | 5aa0248 | 2016-10-05 22:25:33 +0000 | [diff] [blame] | 258 | auto &LI = LocationInfo[File][Line][Function][Column]; |
Hal Finkel | 4d6f308 | 2016-10-06 11:58:52 +0000 | [diff] [blame] | 259 | UpdateLLII(LI.Inlined); |
Hal Finkel | 52031b7 | 2016-10-05 22:10:35 +0000 | [diff] [blame] | 260 | } else if (Pass == "loop-unroll") { |
Hal Finkel | 5aa0248 | 2016-10-05 22:25:33 +0000 | [diff] [blame] | 261 | auto &LI = LocationInfo[File][Line][Function][Column]; |
Hal Finkel | 4d6f308 | 2016-10-06 11:58:52 +0000 | [diff] [blame] | 262 | LI.UnrollCount = UnrollCount; |
| 263 | UpdateLLII(LI.Unrolled); |
Hal Finkel | 52031b7 | 2016-10-05 22:10:35 +0000 | [diff] [blame] | 264 | } else if (Pass == "loop-vectorize") { |
Hal Finkel | 5aa0248 | 2016-10-05 22:25:33 +0000 | [diff] [blame] | 265 | auto &LI = LocationInfo[File][Line][Function][Column]; |
Hal Finkel | 4d6f308 | 2016-10-06 11:58:52 +0000 | [diff] [blame] | 266 | LI.VectorizationFactor = VectorizationFactor; |
| 267 | LI.InterleaveCount = InterleaveCount; |
| 268 | UpdateLLII(LI.Vectorized); |
Hal Finkel | 52031b7 | 2016-10-05 22:10:35 +0000 | [diff] [blame] | 269 | } |
| 270 | } |
| 271 | } |
| 272 | |
| 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 | errs() << "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 | |
| 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 | errs() << "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 | errs() << "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 | errs() << "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; |
Hal Finkel | 5aa0248 | 2016-10-05 22:25:33 +0000 | [diff] [blame] | 330 | for (auto &FLI : FileInfo) |
| 331 | for (auto &FI : FLI.second) |
| 332 | for (auto &LI : FI.second) |
| 333 | MaxLI |= LI.second; |
Hal Finkel | 52031b7 | 2016-10-05 22:10:35 +0000 | [diff] [blame] | 334 | |
Hal Finkel | f495280 | 2016-10-08 00:26:54 +0000 | [diff] [blame^] | 335 | bool NothingInlined = !MaxLI.Inlined.Transformed; |
| 336 | bool NothingUnrolled = !MaxLI.Unrolled.Transformed; |
| 337 | bool NothingVectorized = !MaxLI.Vectorized.Transformed; |
| 338 | |
Hal Finkel | 52031b7 | 2016-10-05 22:10:35 +0000 | [diff] [blame] | 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(); |
Hal Finkel | 52031b7 | 2016-10-05 22:10:35 +0000 | [diff] [blame] | 352 | auto LII = FileInfo.find(L); |
Hal Finkel | 52031b7 | 2016-10-05 22:10:35 +0000 | [diff] [blame] | 353 | |
Hal Finkel | 5aa0248 | 2016-10-05 22:25:33 +0000 | [diff] [blame] | 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()) { |
| 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 | } |
Hal Finkel | 52031b7 | 2016-10-05 22:10:35 +0000 | [diff] [blame] | 372 | } |
Hal Finkel | 5aa0248 | 2016-10-05 22:25:33 +0000 | [diff] [blame] | 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; |
Hal Finkel | 16d29e3 | 2016-10-07 01:30:59 +0000 | [diff] [blame] | 385 | if (!NoDemangle) { |
Hal Finkel | 5aa0248 | 2016-10-05 22:25:33 +0000 | [diff] [blame] | 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); |
Hal Finkel | 5d41f03 | 2016-10-07 02:01:03 +0000 | [diff] [blame] | 419 | |
| 420 | if (!Succinct) { |
| 421 | RS << LLI.UnrollCount; |
| 422 | RS << std::string(UCDigits - RS.str().size(), ' '); |
| 423 | } |
| 424 | |
Hal Finkel | 5aa0248 | 2016-10-05 22:25:33 +0000 | [diff] [blame] | 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); |
Hal Finkel | 5d41f03 | 2016-10-07 02:01:03 +0000 | [diff] [blame] | 432 | |
| 433 | if (!Succinct) { |
| 434 | RS << LLI.VectorizationFactor << "," << LLI.InterleaveCount; |
| 435 | RS << std::string(VFDigits + ICDigits + 1 - RS.str().size(), ' '); |
| 436 | } |
| 437 | |
Hal Finkel | 5aa0248 | 2016-10-05 22:25:33 +0000 | [diff] [blame] | 438 | return RS.str(); |
| 439 | }; |
| 440 | |
Hal Finkel | 47faf3b | 2016-10-06 11:11:11 +0000 | [diff] [blame] | 441 | OS << llvm::format_decimal(L, LNDigits) << " "; |
Hal Finkel | f495280 | 2016-10-08 00:26:54 +0000 | [diff] [blame^] | 442 | OS << (LLI.Inlined.Transformed && InlinedCols < 2 ? "I" : |
| 443 | (NothingInlined ? "" : " ")); |
Hal Finkel | 5aa0248 | 2016-10-05 22:25:33 +0000 | [diff] [blame] | 444 | OS << (LLI.Unrolled.Transformed && UnrolledCols < 2 ? |
Hal Finkel | f495280 | 2016-10-08 00:26:54 +0000 | [diff] [blame^] | 445 | "U" + UStr(LLI) : (NothingUnrolled ? "" : " " + USpaces)); |
Hal Finkel | 5aa0248 | 2016-10-05 22:25:33 +0000 | [diff] [blame] | 446 | OS << (LLI.Vectorized.Transformed && VectorizedCols < 2 ? |
Hal Finkel | f495280 | 2016-10-08 00:26:54 +0000 | [diff] [blame^] | 447 | "V" + VStr(LLI) : (NothingVectorized ? "" : " " + VSpaces)); |
Hal Finkel | 5aa0248 | 2016-10-05 22:25:33 +0000 | [diff] [blame] | 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 && |
Hal Finkel | f495280 | 2016-10-08 00:26:54 +0000 | [diff] [blame^] | 457 | InlinedCols > 1 ? "I" : (NothingInlined ? "" : " ")); |
Hal Finkel | 5aa0248 | 2016-10-05 22:25:33 +0000 | [diff] [blame] | 458 | OS << (J.second.Unrolled.Transformed && |
Hal Finkel | f495280 | 2016-10-08 00:26:54 +0000 | [diff] [blame^] | 459 | UnrolledCols > 1 ? "U" + UStr(J.second) : |
| 460 | (NothingUnrolled ? "" : " " + USpaces)); |
Hal Finkel | 5aa0248 | 2016-10-05 22:25:33 +0000 | [diff] [blame] | 461 | OS << (J.second.Vectorized.Transformed && |
Hal Finkel | f495280 | 2016-10-08 00:26:54 +0000 | [diff] [blame^] | 462 | VectorizedCols > 1 ? "V" + VStr(J.second) : |
| 463 | (NothingVectorized ? "" : " " + VSpaces)); |
Hal Finkel | 5aa0248 | 2016-10-05 22:25:33 +0000 | [diff] [blame] | 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 | if (LII != FileInfo.end()) { |
| 479 | const auto &FuncLineInfo = LII->second; |
| 480 | for (const auto &FLII : FuncLineInfo) |
| 481 | UniqueLIs[FLII.second].insert(FLII.first); |
Hal Finkel | 52031b7 | 2016-10-05 22:10:35 +0000 | [diff] [blame] | 482 | } |
| 483 | |
Hal Finkel | 5aa0248 | 2016-10-05 22:25:33 +0000 | [diff] [blame] | 484 | if (UniqueLIs.size() > 1) { |
| 485 | OS << " [[\n"; |
| 486 | for (const auto &FSLI : UniqueLIs) |
| 487 | PrintLine(true, FSLI.second); |
| 488 | OS << " ]]\n"; |
| 489 | } else if (UniqueLIs.size() == 1) { |
| 490 | PrintLine(false, UniqueLIs.begin()->second); |
| 491 | } else { |
| 492 | PrintLine(false, std::set<std::string>()); |
Hal Finkel | 52031b7 | 2016-10-05 22:10:35 +0000 | [diff] [blame] | 493 | } |
| 494 | } |
| 495 | } |
| 496 | |
| 497 | return true; |
| 498 | } |
| 499 | |
| 500 | int main(int argc, const char **argv) { |
| 501 | sys::PrintStackTraceOnErrorSignal(argv[0]); |
| 502 | |
| 503 | cl::HideUnrelatedOptions(OptReportCategory); |
| 504 | cl::ParseCommandLineOptions( |
| 505 | argc, argv, |
| 506 | "A tool to generate an optimization report from YAML optimization" |
| 507 | " record files.\n"); |
| 508 | |
| 509 | if (Help) |
| 510 | cl::PrintHelpMessage(); |
| 511 | |
| 512 | LocationInfoTy LocationInfo; |
| 513 | if (!readLocationInfo(LocationInfo)) |
| 514 | return 1; |
| 515 | if (!writeReport(LocationInfo)) |
| 516 | return 1; |
| 517 | |
| 518 | return 0; |
| 519 | } |
| 520 | |