BenchmarkResult.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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/MC/MCInst.h"
  21. #include "llvm/MC/MCInstBuilder.h"
  22. #include "llvm/Support/YAMLTraits.h"
  23. #include <limits>
  24. #include <string>
  25. #include <unordered_map>
  26. #include <vector>
  27. namespace llvm {
  28. class Error;
  29. namespace exegesis {
  30. struct InstructionBenchmarkKey {
  31. // The LLVM opcode name.
  32. std::vector<MCInst> Instructions;
  33. // The initial values of the registers.
  34. std::vector<RegisterValue> RegisterInitialValues;
  35. // An opaque configuration, that can be used to separate several benchmarks of
  36. // the same instruction under different configurations.
  37. std::string Config;
  38. };
  39. struct BenchmarkMeasure {
  40. // A helper to create an unscaled BenchmarkMeasure.
  41. static BenchmarkMeasure Create(std::string Key, double Value) {
  42. return {Key, Value, Value};
  43. }
  44. std::string Key;
  45. // This is the per-instruction value, i.e. measured quantity scaled per
  46. // instruction.
  47. double PerInstructionValue;
  48. // This is the per-snippet value, i.e. measured quantity for one repetition of
  49. // the whole snippet.
  50. double PerSnippetValue;
  51. };
  52. // The result of an instruction benchmark.
  53. struct InstructionBenchmark {
  54. InstructionBenchmarkKey Key;
  55. enum ModeE { Unknown, Latency, Uops, InverseThroughput };
  56. ModeE Mode;
  57. std::string CpuName;
  58. std::string LLVMTriple;
  59. // Which instruction is being benchmarked here?
  60. const MCInst &keyInstruction() const { return Key.Instructions[0]; }
  61. // The number of instructions inside the repeated snippet. For example, if a
  62. // snippet of 3 instructions is repeated 4 times, this is 12.
  63. unsigned NumRepetitions = 0;
  64. enum RepetitionModeE { Duplicate, Loop, AggregateMin };
  65. // Note that measurements are per instruction.
  66. std::vector<BenchmarkMeasure> Measurements;
  67. std::string Error;
  68. std::string Info;
  69. std::vector<uint8_t> AssembledSnippet;
  70. // How to aggregate measurements.
  71. enum ResultAggregationModeE { Min, Max, Mean, MinVariance };
  72. // Read functions.
  73. static Expected<InstructionBenchmark> readYaml(const LLVMState &State,
  74. StringRef Filename);
  75. static Expected<std::vector<InstructionBenchmark>>
  76. readYamls(const LLVMState &State, StringRef Filename);
  77. class Error readYamlFrom(const LLVMState &State, StringRef InputContent);
  78. // Write functions, non-const because of YAML traits.
  79. class Error writeYamlTo(const LLVMState &State, raw_ostream &S);
  80. class Error writeYaml(const LLVMState &State, const StringRef Filename);
  81. };
  82. bool operator==(const BenchmarkMeasure &A, const BenchmarkMeasure &B);
  83. //------------------------------------------------------------------------------
  84. // Utilities to work with Benchmark measures.
  85. // A class that measures stats over benchmark measures.
  86. class PerInstructionStats {
  87. public:
  88. void push(const BenchmarkMeasure &BM);
  89. double avg() const {
  90. assert(NumValues);
  91. return SumValues / NumValues;
  92. }
  93. double min() const { return MinValue; }
  94. double max() const { return MaxValue; }
  95. const std::string &key() const { return Key; }
  96. private:
  97. std::string Key;
  98. double SumValues = 0.0;
  99. int NumValues = 0;
  100. double MaxValue = std::numeric_limits<double>::min();
  101. double MinValue = std::numeric_limits<double>::max();
  102. };
  103. } // namespace exegesis
  104. } // namespace llvm
  105. #endif // LLVM_TOOLS_LLVM_EXEGESIS_BENCHMARKRESULT_H