llvm-profgen.cpp 2.0 KB

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