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