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::list<std::string> PerfTraceFilenames(
22     "perfscript", cl::value_desc("perfscript"), cl::OneOrMore,
23     llvm::cl::MiscFlags::CommaSeparated,
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 
27 static cl::list<std::string>
28     BinaryFilenames("binary", cl::value_desc("binary"), cl::OneOrMore,
29                     llvm::cl::MiscFlags::CommaSeparated,
30                     cl::desc("Path of profiled binary files"));
31 
32 extern cl::opt<bool> ShowDisassemblyOnly;
33 
34 using namespace llvm;
35 using namespace sampleprof;
36 
37 int main(int argc, const char *argv[]) {
38   InitLLVM X(argc, argv);
39 
40   // Initialize targets and assembly printers/parsers.
41   InitializeAllTargetInfos();
42   InitializeAllTargetMCs();
43   InitializeAllDisassemblers();
44 
45   cl::ParseCommandLineOptions(argc, argv, "llvm SPGO profile generator\n");
46 
47   // Load binaries and parse perf events and samples
48   PerfReader Reader(BinaryFilenames, PerfTraceFilenames);
49   if (ShowDisassemblyOnly)
50     return EXIT_SUCCESS;
51   Reader.parsePerfTraces(PerfTraceFilenames);
52 
53   std::unique_ptr<ProfileGenerator> Generator = ProfileGenerator::create(
54       Reader.getBinarySampleCounters(), Reader.getPerfScriptType());
55   Generator->generateProfile();
56   Generator->write();
57 
58   return EXIT_SUCCESS;
59 }
60