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/Support/CommandLine.h"
18 #include "llvm/Support/InitLLVM.h"
19 #include "llvm/Support/TargetSelect.h"
20 
21 static cl::OptionCategory ProfGenCategory("ProfGen Options");
22 
23 static cl::list<std::string> PerfTraceFilenames(
24     "perfscript", cl::value_desc("perfscript"), cl::OneOrMore,
25     llvm::cl::MiscFlags::CommaSeparated,
26     cl::desc("Path of perf-script trace created by Linux perf tool with "
27              "`script` command(the raw perf.data should be profiled with -b)"),
28     cl::cat(ProfGenCategory));
29 
30 static cl::list<std::string>
31     BinaryFilenames("binary", cl::value_desc("binary"), cl::OneOrMore,
32                     llvm::cl::MiscFlags::CommaSeparated,
33                     cl::desc("Path of profiled binary files"),
34                     cl::cat(ProfGenCategory));
35 
36 extern cl::opt<bool> ShowDisassemblyOnly;
37 
38 using namespace llvm;
39 using namespace sampleprof;
40 
41 int main(int argc, const char *argv[]) {
42   InitLLVM X(argc, argv);
43 
44   // Initialize targets and assembly printers/parsers.
45   InitializeAllTargetInfos();
46   InitializeAllTargetMCs();
47   InitializeAllDisassemblers();
48 
49   cl::HideUnrelatedOptions({&ProfGenCategory, &getColorCategory()});
50   cl::ParseCommandLineOptions(argc, argv, "llvm SPGO profile generator\n");
51 
52   // Load binaries and parse perf events and samples
53   PerfReader Reader(BinaryFilenames, PerfTraceFilenames);
54   if (ShowDisassemblyOnly)
55     return EXIT_SUCCESS;
56   Reader.parsePerfTraces(PerfTraceFilenames);
57 
58   std::unique_ptr<ProfileGenerator> Generator = ProfileGenerator::create(
59       Reader.getBinarySampleCounters(), Reader.getPerfScriptType());
60   Generator->generateProfile();
61   Generator->write();
62 
63   return EXIT_SUCCESS;
64 }
65