BenchmarkRunner.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. //===-- BenchmarkRunner.h ---------------------------------------*- 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. /// \file
  10. /// Defines the abstract BenchmarkRunner class for measuring a certain execution
  11. /// property of instructions (e.g. latency).
  12. ///
  13. //===----------------------------------------------------------------------===//
  14. #ifndef LLVM_TOOLS_LLVM_EXEGESIS_BENCHMARKRUNNER_H
  15. #define LLVM_TOOLS_LLVM_EXEGESIS_BENCHMARKRUNNER_H
  16. #include "Assembler.h"
  17. #include "BenchmarkCode.h"
  18. #include "BenchmarkResult.h"
  19. #include "LlvmState.h"
  20. #include "MCInstrDescView.h"
  21. #include "SnippetRepetitor.h"
  22. #include "llvm/ADT/SmallVector.h"
  23. #include "llvm/MC/MCInst.h"
  24. #include "llvm/Support/Error.h"
  25. #include <cstdlib>
  26. #include <memory>
  27. #include <vector>
  28. namespace llvm {
  29. namespace exegesis {
  30. // Common code for all benchmark modes.
  31. class BenchmarkRunner {
  32. public:
  33. explicit BenchmarkRunner(const LLVMState &State,
  34. InstructionBenchmark::ModeE Mode,
  35. BenchmarkPhaseSelectorE BenchmarkPhaseSelector);
  36. virtual ~BenchmarkRunner();
  37. class RunnableConfiguration {
  38. friend class BenchmarkRunner;
  39. public:
  40. ~RunnableConfiguration() = default;
  41. RunnableConfiguration(RunnableConfiguration &&) = default;
  42. RunnableConfiguration(const RunnableConfiguration &) = delete;
  43. RunnableConfiguration &operator=(RunnableConfiguration &&) = delete;
  44. RunnableConfiguration &operator=(const RunnableConfiguration &) = delete;
  45. private:
  46. RunnableConfiguration() = default;
  47. InstructionBenchmark InstrBenchmark;
  48. object::OwningBinary<object::ObjectFile> ObjectFile;
  49. };
  50. Expected<RunnableConfiguration>
  51. getRunnableConfiguration(const BenchmarkCode &Configuration,
  52. unsigned NumRepetitions, unsigned LoopUnrollFactor,
  53. const SnippetRepetitor &Repetitor) const;
  54. Expected<InstructionBenchmark> runConfiguration(RunnableConfiguration &&RC,
  55. bool DumpObjectToDisk) const;
  56. // Scratch space to run instructions that touch memory.
  57. struct ScratchSpace {
  58. static constexpr const size_t kAlignment = 1024;
  59. static constexpr const size_t kSize = 1 << 20; // 1MB.
  60. ScratchSpace()
  61. : UnalignedPtr(std::make_unique<char[]>(kSize + kAlignment)),
  62. AlignedPtr(
  63. UnalignedPtr.get() + kAlignment -
  64. (reinterpret_cast<intptr_t>(UnalignedPtr.get()) % kAlignment)) {}
  65. char *ptr() const { return AlignedPtr; }
  66. void clear() { std::memset(ptr(), 0, kSize); }
  67. private:
  68. const std::unique_ptr<char[]> UnalignedPtr;
  69. char *const AlignedPtr;
  70. };
  71. // A helper to measure counters while executing a function in a sandboxed
  72. // context.
  73. class FunctionExecutor {
  74. public:
  75. virtual ~FunctionExecutor();
  76. // FIXME deprecate this.
  77. virtual Expected<int64_t> runAndMeasure(const char *Counters) const = 0;
  78. virtual Expected<llvm::SmallVector<int64_t, 4>>
  79. runAndSample(const char *Counters) const = 0;
  80. };
  81. protected:
  82. const LLVMState &State;
  83. const InstructionBenchmark::ModeE Mode;
  84. const BenchmarkPhaseSelectorE BenchmarkPhaseSelector;
  85. private:
  86. virtual Expected<std::vector<BenchmarkMeasure>>
  87. runMeasurements(const FunctionExecutor &Executor) const = 0;
  88. Expected<SmallString<0>> assembleSnippet(const BenchmarkCode &BC,
  89. const SnippetRepetitor &Repetitor,
  90. unsigned MinInstructions,
  91. unsigned LoopBodySize) const;
  92. Expected<std::string> writeObjectFile(StringRef Buffer) const;
  93. const std::unique_ptr<ScratchSpace> Scratch;
  94. };
  95. } // namespace exegesis
  96. } // namespace llvm
  97. #endif // LLVM_TOOLS_LLVM_EXEGESIS_BENCHMARKRUNNER_H