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 SampleProfFilename("llvm-sample-profile", 54 cl::value_desc("llvm sample profile"), cl::ZeroOrMore, 55 cl::desc("Path of the LLVM sample profile"), 56 cl::cat(ProfGenCategory)); 57 58 static cl::opt<std::string> 59 BinaryPath("binary", cl::value_desc("binary"), cl::Required, 60 cl::desc("Path of profiled executable binary."), 61 cl::cat(ProfGenCategory)); 62 63 static cl::opt<uint32_t> 64 ProcessId("pid", cl::value_desc("process Id"), cl::ZeroOrMore, cl::init(0), 65 cl::desc("Process Id for the profiled executable binary."), 66 cl::cat(ProfGenCategory)); 67 68 static cl::opt<std::string> DebugBinPath( 69 "debug-binary", cl::value_desc("debug-binary"), cl::ZeroOrMore, 70 cl::desc("Path of debug info binary, llvm-profgen will load the DWARF info " 71 "from it instead of the executable binary."), 72 cl::cat(ProfGenCategory)); 73 74 extern cl::opt<bool> ShowDisassemblyOnly; 75 extern cl::opt<bool> ShowSourceLocations; 76 extern cl::opt<bool> SkipSymbolization; 77 78 using namespace llvm; 79 using namespace sampleprof; 80 81 // Validate the command line input. 82 static void validateCommandLine() { 83 // Allow the missing perfscript if we only use to show binary disassembly. 84 if (!ShowDisassemblyOnly) { 85 // Validate input profile is provided only once 86 uint16_t HasPerfData = PerfDataFilename.getNumOccurrences(); 87 uint16_t HasPerfScript = PerfScriptFilename.getNumOccurrences(); 88 uint16_t HasUnsymbolizedProfile = 89 UnsymbolizedProfFilename.getNumOccurrences(); 90 uint16_t HasSampleProfile = SampleProfFilename.getNumOccurrences(); 91 uint16_t S = 92 HasPerfData + HasPerfScript + HasUnsymbolizedProfile + HasSampleProfile; 93 if (S != 1) { 94 std::string Msg = 95 S > 1 96 ? "`--perfscript`, `--perfdata` and `--unsymbolized-profile` " 97 "cannot be used together." 98 : "Perf input file is missing, please use one of `--perfscript`, " 99 "`--perfdata` and `--unsymbolized-profile` for the input."; 100 exitWithError(Msg); 101 } 102 103 auto CheckFileExists = [](bool H, StringRef File) { 104 if (H && !llvm::sys::fs::exists(File)) { 105 std::string Msg = "Input perf file(" + File.str() + ") doesn't exist."; 106 exitWithError(Msg); 107 } 108 }; 109 110 CheckFileExists(HasPerfData, PerfDataFilename); 111 CheckFileExists(HasPerfScript, PerfScriptFilename); 112 CheckFileExists(HasUnsymbolizedProfile, UnsymbolizedProfFilename); 113 CheckFileExists(HasSampleProfile, SampleProfFilename); 114 } 115 116 if (!llvm::sys::fs::exists(BinaryPath)) { 117 std::string Msg = "Input binary(" + BinaryPath + ") doesn't exist."; 118 exitWithError(Msg); 119 } 120 121 if (CSProfileGenerator::MaxCompressionSize < -1) { 122 exitWithError("Value of --compress-recursion should >= -1"); 123 } 124 if (ShowSourceLocations && !ShowDisassemblyOnly) { 125 exitWithError("--show-source-locations should work together with " 126 "--show-disassembly-only!"); 127 } 128 } 129 130 static PerfInputFile getPerfInputFile() { 131 PerfInputFile File; 132 if (PerfDataFilename.getNumOccurrences()) { 133 File.InputFile = PerfDataFilename; 134 File.Format = PerfFormat::PerfData; 135 } else if (PerfScriptFilename.getNumOccurrences()) { 136 File.InputFile = PerfScriptFilename; 137 File.Format = PerfFormat::PerfScript; 138 } else if (UnsymbolizedProfFilename.getNumOccurrences()) { 139 File.InputFile = UnsymbolizedProfFilename; 140 File.Format = PerfFormat::UnsymbolizedProfile; 141 } 142 return File; 143 } 144 145 int main(int argc, const char *argv[]) { 146 InitLLVM X(argc, argv); 147 148 // Initialize targets and assembly printers/parsers. 149 InitializeAllTargetInfos(); 150 InitializeAllTargetMCs(); 151 InitializeAllDisassemblers(); 152 153 cl::HideUnrelatedOptions({&ProfGenCategory, &getColorCategory()}); 154 cl::ParseCommandLineOptions(argc, argv, "llvm SPGO profile generator\n"); 155 validateCommandLine(); 156 157 // Load symbols and disassemble the code of a given binary. 158 std::unique_ptr<ProfiledBinary> Binary = 159 std::make_unique<ProfiledBinary>(BinaryPath, DebugBinPath); 160 if (ShowDisassemblyOnly) 161 return EXIT_SUCCESS; 162 163 if (SampleProfFilename.getNumOccurrences()) { 164 LLVMContext Context; 165 auto ReaderOrErr = SampleProfileReader::create(SampleProfFilename, Context); 166 std::unique_ptr<sampleprof::SampleProfileReader> Reader = 167 std::move(ReaderOrErr.get()); 168 Reader->read(); 169 std::unique_ptr<ProfileGeneratorBase> Generator = 170 ProfileGeneratorBase::create(Binary.get(), 171 std::move(Reader->getProfiles()), 172 Reader->profileIsCS()); 173 Generator->generateProfile(); 174 Generator->write(); 175 } else { 176 Optional<uint32_t> PIDFilter; 177 if (ProcessId.getNumOccurrences()) 178 PIDFilter = ProcessId; 179 PerfInputFile PerfFile = getPerfInputFile(); 180 std::unique_ptr<PerfReaderBase> Reader = 181 PerfReaderBase::create(Binary.get(), PerfFile, PIDFilter); 182 // Parse perf events and samples 183 Reader->parsePerfTraces(); 184 185 if (SkipSymbolization) 186 return EXIT_SUCCESS; 187 188 std::unique_ptr<ProfileGeneratorBase> Generator = 189 ProfileGeneratorBase::create(Binary.get(), &Reader->getSampleCounters(), 190 Reader->profileIsCS()); 191 Generator->generateProfile(); 192 Generator->write(); 193 } 194 195 return EXIT_SUCCESS; 196 } 197