llvm-profgen.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. //===- llvm-profgen.cpp - LLVM SPGO profile generation tool -----*- C++ -*-===//
  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. // llvm-profgen generates SPGO profiles from perf script ouput.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "ErrorHandling.h"
  13. #include "PerfReader.h"
  14. #include "ProfileGenerator.h"
  15. #include "ProfiledBinary.h"
  16. #include "llvm/DebugInfo/Symbolize/SymbolizableModule.h"
  17. #include "llvm/Support/CommandLine.h"
  18. #include "llvm/Support/FileSystem.h"
  19. #include "llvm/Support/InitLLVM.h"
  20. #include "llvm/Support/TargetSelect.h"
  21. static cl::OptionCategory ProfGenCategory("ProfGen Options");
  22. static cl::opt<std::string> PerfScriptFilename(
  23. "perfscript", cl::value_desc("perfscript"),
  24. cl::desc("Path of perf-script trace created by Linux perf tool with "
  25. "`script` command(the raw perf.data should be profiled with -b)"),
  26. cl::cat(ProfGenCategory));
  27. static cl::alias PSA("ps", cl::desc("Alias for --perfscript"),
  28. cl::aliasopt(PerfScriptFilename));
  29. static cl::opt<std::string> PerfDataFilename(
  30. "perfdata", cl::value_desc("perfdata"),
  31. cl::desc("Path of raw perf data created by Linux perf tool (it should be "
  32. "profiled with -b)"),
  33. cl::cat(ProfGenCategory));
  34. static cl::alias PDA("pd", cl::desc("Alias for --perfdata"),
  35. cl::aliasopt(PerfDataFilename));
  36. static cl::opt<std::string> UnsymbolizedProfFilename(
  37. "unsymbolized-profile", cl::value_desc("unsymbolized profile"),
  38. cl::desc("Path of the unsymbolized profile created by "
  39. "`llvm-profgen` with `--skip-symbolization`"),
  40. cl::cat(ProfGenCategory));
  41. static cl::alias UPA("up", cl::desc("Alias for --unsymbolized-profile"),
  42. cl::aliasopt(UnsymbolizedProfFilename));
  43. static cl::opt<std::string> SampleProfFilename(
  44. "llvm-sample-profile", cl::value_desc("llvm sample profile"),
  45. cl::desc("Path of the LLVM sample profile"), cl::cat(ProfGenCategory));
  46. static cl::opt<std::string>
  47. BinaryPath("binary", cl::value_desc("binary"), cl::Required,
  48. cl::desc("Path of profiled executable binary."),
  49. cl::cat(ProfGenCategory));
  50. static cl::opt<uint32_t>
  51. ProcessId("pid", cl::value_desc("process Id"), cl::init(0),
  52. cl::desc("Process Id for the profiled executable binary."),
  53. cl::cat(ProfGenCategory));
  54. static cl::opt<std::string> DebugBinPath(
  55. "debug-binary", cl::value_desc("debug-binary"),
  56. cl::desc("Path of debug info binary, llvm-profgen will load the DWARF info "
  57. "from it instead of the executable binary."),
  58. cl::cat(ProfGenCategory));
  59. extern cl::opt<bool> ShowDisassemblyOnly;
  60. extern cl::opt<bool> ShowSourceLocations;
  61. extern cl::opt<bool> SkipSymbolization;
  62. using namespace llvm;
  63. using namespace sampleprof;
  64. // Validate the command line input.
  65. static void validateCommandLine() {
  66. // Allow the missing perfscript if we only use to show binary disassembly.
  67. if (!ShowDisassemblyOnly) {
  68. // Validate input profile is provided only once
  69. bool HasPerfData = PerfDataFilename.getNumOccurrences() > 0;
  70. bool HasPerfScript = PerfScriptFilename.getNumOccurrences() > 0;
  71. bool HasUnsymbolizedProfile =
  72. UnsymbolizedProfFilename.getNumOccurrences() > 0;
  73. bool HasSampleProfile = SampleProfFilename.getNumOccurrences() > 0;
  74. uint16_t S =
  75. HasPerfData + HasPerfScript + HasUnsymbolizedProfile + HasSampleProfile;
  76. if (S != 1) {
  77. std::string Msg =
  78. S > 1
  79. ? "`--perfscript`, `--perfdata` and `--unsymbolized-profile` "
  80. "cannot be used together."
  81. : "Perf input file is missing, please use one of `--perfscript`, "
  82. "`--perfdata` and `--unsymbolized-profile` for the input.";
  83. exitWithError(Msg);
  84. }
  85. auto CheckFileExists = [](bool H, StringRef File) {
  86. if (H && !llvm::sys::fs::exists(File)) {
  87. std::string Msg = "Input perf file(" + File.str() + ") doesn't exist.";
  88. exitWithError(Msg);
  89. }
  90. };
  91. CheckFileExists(HasPerfData, PerfDataFilename);
  92. CheckFileExists(HasPerfScript, PerfScriptFilename);
  93. CheckFileExists(HasUnsymbolizedProfile, UnsymbolizedProfFilename);
  94. CheckFileExists(HasSampleProfile, SampleProfFilename);
  95. }
  96. if (!llvm::sys::fs::exists(BinaryPath)) {
  97. std::string Msg = "Input binary(" + BinaryPath + ") doesn't exist.";
  98. exitWithError(Msg);
  99. }
  100. if (CSProfileGenerator::MaxCompressionSize < -1) {
  101. exitWithError("Value of --compress-recursion should >= -1");
  102. }
  103. if (ShowSourceLocations && !ShowDisassemblyOnly) {
  104. exitWithError("--show-source-locations should work together with "
  105. "--show-disassembly-only!");
  106. }
  107. }
  108. static PerfInputFile getPerfInputFile() {
  109. PerfInputFile File;
  110. if (PerfDataFilename.getNumOccurrences()) {
  111. File.InputFile = PerfDataFilename;
  112. File.Format = PerfFormat::PerfData;
  113. } else if (PerfScriptFilename.getNumOccurrences()) {
  114. File.InputFile = PerfScriptFilename;
  115. File.Format = PerfFormat::PerfScript;
  116. } else if (UnsymbolizedProfFilename.getNumOccurrences()) {
  117. File.InputFile = UnsymbolizedProfFilename;
  118. File.Format = PerfFormat::UnsymbolizedProfile;
  119. }
  120. return File;
  121. }
  122. int main(int argc, const char *argv[]) {
  123. InitLLVM X(argc, argv);
  124. // Initialize targets and assembly printers/parsers.
  125. InitializeAllTargetInfos();
  126. InitializeAllTargetMCs();
  127. InitializeAllDisassemblers();
  128. cl::HideUnrelatedOptions({&ProfGenCategory, &getColorCategory()});
  129. cl::ParseCommandLineOptions(argc, argv, "llvm SPGO profile generator\n");
  130. validateCommandLine();
  131. // Load symbols and disassemble the code of a given binary.
  132. std::unique_ptr<ProfiledBinary> Binary =
  133. std::make_unique<ProfiledBinary>(BinaryPath, DebugBinPath);
  134. if (ShowDisassemblyOnly)
  135. return EXIT_SUCCESS;
  136. if (SampleProfFilename.getNumOccurrences()) {
  137. LLVMContext Context;
  138. auto ReaderOrErr = SampleProfileReader::create(SampleProfFilename, Context);
  139. std::unique_ptr<sampleprof::SampleProfileReader> Reader =
  140. std::move(ReaderOrErr.get());
  141. Reader->read();
  142. std::unique_ptr<ProfileGeneratorBase> Generator =
  143. ProfileGeneratorBase::create(Binary.get(), Reader->getProfiles(),
  144. Reader->profileIsCS());
  145. Generator->generateProfile();
  146. Generator->write();
  147. } else {
  148. std::optional<uint32_t> PIDFilter;
  149. if (ProcessId.getNumOccurrences())
  150. PIDFilter = ProcessId;
  151. PerfInputFile PerfFile = getPerfInputFile();
  152. std::unique_ptr<PerfReaderBase> Reader =
  153. PerfReaderBase::create(Binary.get(), PerfFile, PIDFilter);
  154. // Parse perf events and samples
  155. Reader->parsePerfTraces();
  156. if (SkipSymbolization)
  157. return EXIT_SUCCESS;
  158. std::unique_ptr<ProfileGeneratorBase> Generator =
  159. ProfileGeneratorBase::create(Binary.get(), &Reader->getSampleCounters(),
  160. Reader->profileIsCS());
  161. Generator->generateProfile();
  162. Generator->write();
  163. }
  164. return EXIT_SUCCESS;
  165. }