BenchmarkResult.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. //===-- BenchmarkResult.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 classes to represent measurements and serialize/deserialize them to
  11. // Yaml.
  12. ///
  13. //===----------------------------------------------------------------------===//
  14. #ifndef LLVM_TOOLS_LLVM_EXEGESIS_BENCHMARKRESULT_H
  15. #define LLVM_TOOLS_LLVM_EXEGESIS_BENCHMARKRESULT_H
  16. #include "LlvmState.h"
  17. #include "RegisterValue.h"
  18. #include "llvm/ADT/StringMap.h"
  19. #include "llvm/ADT/StringRef.h"
  20. #include "llvm/ADT/StringSet.h"
  21. #include "llvm/MC/MCInst.h"
  22. #include "llvm/MC/MCInstBuilder.h"
  23. #include "llvm/Support/YAMLTraits.h"
  24. #include <limits>
  25. #include <set>
  26. #include <string>
  27. #include <unordered_map>
  28. #include <vector>
  29. namespace llvm {
  30. class Error;
  31. namespace exegesis {
  32. enum class BenchmarkPhaseSelectorE {
  33. PrepareSnippet,
  34. PrepareAndAssembleSnippet,
  35. AssembleMeasuredCode,
  36. Measure,
  37. };
  38. enum class InstructionBenchmarkFilter { All, RegOnly, WithMem };
  39. struct InstructionBenchmarkKey {
  40. // The LLVM opcode name.
  41. std::vector<MCInst> Instructions;
  42. // The initial values of the registers.
  43. std::vector<RegisterValue> RegisterInitialValues;
  44. // An opaque configuration, that can be used to separate several benchmarks of
  45. // the same instruction under different configurations.
  46. std::string Config;
  47. };
  48. struct BenchmarkMeasure {
  49. // A helper to create an unscaled BenchmarkMeasure.
  50. static BenchmarkMeasure Create(std::string Key, double Value) {
  51. return {Key, Value, Value};
  52. }
  53. std::string Key;
  54. // This is the per-instruction value, i.e. measured quantity scaled per
  55. // instruction.
  56. double PerInstructionValue;
  57. // This is the per-snippet value, i.e. measured quantity for one repetition of
  58. // the whole snippet.
  59. double PerSnippetValue;
  60. };
  61. // The result of an instruction benchmark.
  62. struct InstructionBenchmark {
  63. InstructionBenchmarkKey Key;
  64. enum ModeE { Unknown, Latency, Uops, InverseThroughput };
  65. ModeE Mode;
  66. std::string CpuName;
  67. std::string LLVMTriple;
  68. // Which instruction is being benchmarked here?
  69. const MCInst &keyInstruction() const { return Key.Instructions[0]; }
  70. // The number of instructions inside the repeated snippet. For example, if a
  71. // snippet of 3 instructions is repeated 4 times, this is 12.
  72. unsigned NumRepetitions = 0;
  73. enum RepetitionModeE { Duplicate, Loop, AggregateMin };
  74. // Note that measurements are per instruction.
  75. std::vector<BenchmarkMeasure> Measurements;
  76. std::string Error;
  77. std::string Info;
  78. std::vector<uint8_t> AssembledSnippet;
  79. // How to aggregate measurements.
  80. enum ResultAggregationModeE { Min, Max, Mean, MinVariance };
  81. InstructionBenchmark() = default;
  82. InstructionBenchmark(InstructionBenchmark &&) = default;
  83. InstructionBenchmark(const InstructionBenchmark &) = delete;
  84. InstructionBenchmark &operator=(const InstructionBenchmark &) = delete;
  85. InstructionBenchmark &operator=(InstructionBenchmark &&) = delete;
  86. // Read functions.
  87. static Expected<InstructionBenchmark> readYaml(const LLVMState &State,
  88. MemoryBufferRef Buffer);
  89. static Expected<std::vector<InstructionBenchmark>>
  90. readYamls(const LLVMState &State, MemoryBufferRef Buffer);
  91. // Given a set of serialized instruction benchmarks, returns the set of
  92. // triples and CPUs that appear in the list of benchmarks.
  93. struct TripleAndCpu {
  94. std::string LLVMTriple;
  95. std::string CpuName;
  96. bool operator<(const TripleAndCpu &O) const {
  97. return std::tie(LLVMTriple, CpuName) < std::tie(O.LLVMTriple, O.CpuName);
  98. }
  99. };
  100. static Expected<std::set<TripleAndCpu>>
  101. readTriplesAndCpusFromYamls(MemoryBufferRef Buffer);
  102. class Error readYamlFrom(const LLVMState &State, StringRef InputContent);
  103. // Write functions, non-const because of YAML traits.
  104. // NOTE: we intentionally do *NOT* have a variant of this function taking
  105. // filename, because it's behaviour is bugprone with regards to
  106. // accidentally using it more than once and overriding previous YAML.
  107. class Error writeYamlTo(const LLVMState &State, raw_ostream &S);
  108. };
  109. bool operator==(const BenchmarkMeasure &A, const BenchmarkMeasure &B);
  110. //------------------------------------------------------------------------------
  111. // Utilities to work with Benchmark measures.
  112. // A class that measures stats over benchmark measures.
  113. class PerInstructionStats {
  114. public:
  115. void push(const BenchmarkMeasure &BM);
  116. double avg() const {
  117. assert(NumValues);
  118. return SumValues / NumValues;
  119. }
  120. double min() const { return MinValue; }
  121. double max() const { return MaxValue; }
  122. const std::string &key() const { return Key; }
  123. private:
  124. std::string Key;
  125. double SumValues = 0.0;
  126. int NumValues = 0;
  127. double MaxValue = std::numeric_limits<double>::min();
  128. double MinValue = std::numeric_limits<double>::max();
  129. };
  130. } // namespace exegesis
  131. } // namespace llvm
  132. #endif // LLVM_TOOLS_LLVM_EXEGESIS_BENCHMARKRESULT_H