123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- //===-- BenchmarkResult.h ---------------------------------------*- C++ -*-===//
- //
- // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
- // See https://llvm.org/LICENSE.txt for license information.
- // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
- //
- //===----------------------------------------------------------------------===//
- ///
- /// \file
- /// Defines classes to represent measurements and serialize/deserialize them to
- // Yaml.
- ///
- //===----------------------------------------------------------------------===//
- #ifndef LLVM_TOOLS_LLVM_EXEGESIS_BENCHMARKRESULT_H
- #define LLVM_TOOLS_LLVM_EXEGESIS_BENCHMARKRESULT_H
- #include "LlvmState.h"
- #include "RegisterValue.h"
- #include "llvm/ADT/StringMap.h"
- #include "llvm/ADT/StringRef.h"
- #include "llvm/ADT/StringSet.h"
- #include "llvm/MC/MCInst.h"
- #include "llvm/MC/MCInstBuilder.h"
- #include "llvm/Support/YAMLTraits.h"
- #include <limits>
- #include <set>
- #include <string>
- #include <unordered_map>
- #include <vector>
- namespace llvm {
- class Error;
- namespace exegesis {
- enum class BenchmarkPhaseSelectorE {
- PrepareSnippet,
- PrepareAndAssembleSnippet,
- AssembleMeasuredCode,
- Measure,
- };
- enum class InstructionBenchmarkFilter { All, RegOnly, WithMem };
- struct InstructionBenchmarkKey {
- // The LLVM opcode name.
- std::vector<MCInst> Instructions;
- // The initial values of the registers.
- std::vector<RegisterValue> RegisterInitialValues;
- // An opaque configuration, that can be used to separate several benchmarks of
- // the same instruction under different configurations.
- std::string Config;
- };
- struct BenchmarkMeasure {
- // A helper to create an unscaled BenchmarkMeasure.
- static BenchmarkMeasure Create(std::string Key, double Value) {
- return {Key, Value, Value};
- }
- std::string Key;
- // This is the per-instruction value, i.e. measured quantity scaled per
- // instruction.
- double PerInstructionValue;
- // This is the per-snippet value, i.e. measured quantity for one repetition of
- // the whole snippet.
- double PerSnippetValue;
- };
- // The result of an instruction benchmark.
- struct InstructionBenchmark {
- InstructionBenchmarkKey Key;
- enum ModeE { Unknown, Latency, Uops, InverseThroughput };
- ModeE Mode;
- std::string CpuName;
- std::string LLVMTriple;
- // Which instruction is being benchmarked here?
- const MCInst &keyInstruction() const { return Key.Instructions[0]; }
- // The number of instructions inside the repeated snippet. For example, if a
- // snippet of 3 instructions is repeated 4 times, this is 12.
- unsigned NumRepetitions = 0;
- enum RepetitionModeE { Duplicate, Loop, AggregateMin };
- // Note that measurements are per instruction.
- std::vector<BenchmarkMeasure> Measurements;
- std::string Error;
- std::string Info;
- std::vector<uint8_t> AssembledSnippet;
- // How to aggregate measurements.
- enum ResultAggregationModeE { Min, Max, Mean, MinVariance };
- InstructionBenchmark() = default;
- InstructionBenchmark(InstructionBenchmark &&) = default;
- InstructionBenchmark(const InstructionBenchmark &) = delete;
- InstructionBenchmark &operator=(const InstructionBenchmark &) = delete;
- InstructionBenchmark &operator=(InstructionBenchmark &&) = delete;
- // Read functions.
- static Expected<InstructionBenchmark> readYaml(const LLVMState &State,
- MemoryBufferRef Buffer);
- static Expected<std::vector<InstructionBenchmark>>
- readYamls(const LLVMState &State, MemoryBufferRef Buffer);
- // Given a set of serialized instruction benchmarks, returns the set of
- // triples and CPUs that appear in the list of benchmarks.
- struct TripleAndCpu {
- std::string LLVMTriple;
- std::string CpuName;
- bool operator<(const TripleAndCpu &O) const {
- return std::tie(LLVMTriple, CpuName) < std::tie(O.LLVMTriple, O.CpuName);
- }
- };
- static Expected<std::set<TripleAndCpu>>
- readTriplesAndCpusFromYamls(MemoryBufferRef Buffer);
- class Error readYamlFrom(const LLVMState &State, StringRef InputContent);
- // Write functions, non-const because of YAML traits.
- // NOTE: we intentionally do *NOT* have a variant of this function taking
- // filename, because it's behaviour is bugprone with regards to
- // accidentally using it more than once and overriding previous YAML.
- class Error writeYamlTo(const LLVMState &State, raw_ostream &S);
- };
- bool operator==(const BenchmarkMeasure &A, const BenchmarkMeasure &B);
- //------------------------------------------------------------------------------
- // Utilities to work with Benchmark measures.
- // A class that measures stats over benchmark measures.
- class PerInstructionStats {
- public:
- void push(const BenchmarkMeasure &BM);
- double avg() const {
- assert(NumValues);
- return SumValues / NumValues;
- }
- double min() const { return MinValue; }
- double max() const { return MaxValue; }
- const std::string &key() const { return Key; }
- private:
- std::string Key;
- double SumValues = 0.0;
- int NumValues = 0;
- double MaxValue = std::numeric_limits<double>::min();
- double MinValue = std::numeric_limits<double>::max();
- };
- } // namespace exegesis
- } // namespace llvm
- #endif // LLVM_TOOLS_LLVM_EXEGESIS_BENCHMARKRESULT_H
|