BenchmarkRunner.h 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. virtual ~BenchmarkRunner();
  36. Expected<InstructionBenchmark>
  37. runConfiguration(const BenchmarkCode &Configuration, unsigned NumRepetitions,
  38. unsigned LoopUnrollFactor,
  39. ArrayRef<std::unique_ptr<const SnippetRepetitor>> Repetitors,
  40. bool DumpObjectToDisk) const;
  41. // Scratch space to run instructions that touch memory.
  42. struct ScratchSpace {
  43. static constexpr const size_t kAlignment = 1024;
  44. static constexpr const size_t kSize = 1 << 20; // 1MB.
  45. ScratchSpace()
  46. : UnalignedPtr(std::make_unique<char[]>(kSize + kAlignment)),
  47. AlignedPtr(
  48. UnalignedPtr.get() + kAlignment -
  49. (reinterpret_cast<intptr_t>(UnalignedPtr.get()) % kAlignment)) {}
  50. char *ptr() const { return AlignedPtr; }
  51. void clear() { std::memset(ptr(), 0, kSize); }
  52. private:
  53. const std::unique_ptr<char[]> UnalignedPtr;
  54. char *const AlignedPtr;
  55. };
  56. // A helper to measure counters while executing a function in a sandboxed
  57. // context.
  58. class FunctionExecutor {
  59. public:
  60. virtual ~FunctionExecutor();
  61. // FIXME deprecate this.
  62. virtual Expected<int64_t> runAndMeasure(const char *Counters) const = 0;
  63. virtual Expected<llvm::SmallVector<int64_t, 4>>
  64. runAndSample(const char *Counters) const = 0;
  65. };
  66. protected:
  67. const LLVMState &State;
  68. const InstructionBenchmark::ModeE Mode;
  69. private:
  70. virtual Expected<std::vector<BenchmarkMeasure>>
  71. runMeasurements(const FunctionExecutor &Executor) const = 0;
  72. Expected<std::string> writeObjectFile(const BenchmarkCode &Configuration,
  73. const FillFunction &Fill) const;
  74. const std::unique_ptr<ScratchSpace> Scratch;
  75. };
  76. } // namespace exegesis
  77. } // namespace llvm
  78. #endif // LLVM_TOOLS_LLVM_EXEGESIS_BENCHMARKRUNNER_H