summaryrefslogtreecommitdiffstats
path: root/llvm/tools/llvm-exegesis/lib/Analysis.h
blob: 3c33cafc5fb981ddd4e4de00f25897fd950f55a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
//===-- Analysis.h ----------------------------------------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
///
/// \file
/// Analysis output for benchmark results.
///
//===----------------------------------------------------------------------===//

#ifndef LLVM_TOOLS_LLVM_EXEGESIS_ANALYSIS_H
#define LLVM_TOOLS_LLVM_EXEGESIS_ANALYSIS_H

#include "Clustering.h"
#include "llvm/MC/MCContext.h"
#include "llvm/MC/MCDisassembler/MCDisassembler.h"
#include "llvm/MC/MCInstPrinter.h"
#include "llvm/MC/MCInstrInfo.h"
#include "llvm/MC/MCObjectFileInfo.h"
#include "llvm/MC/MCSubtargetInfo.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/TargetRegistry.h"
#include "llvm/Support/raw_ostream.h"
#include <memory>
#include <set>
#include <string>
#include <unordered_map>

namespace exegesis {

// A helper class to analyze benchmark results for a target.
class Analysis {
public:
  Analysis(const llvm::Target &Target,
           const InstructionBenchmarkClustering &Clustering);

  // Prints a csv of instructions for each cluster.
  struct PrintClusters {};
  // Find potential errors in the scheduling information given measurements.
  struct PrintSchedClassInconsistencies {};

  template <typename Pass> llvm::Error run(llvm::raw_ostream &OS) const;

private:
  using ClusterId = InstructionBenchmarkClustering::ClusterId;

  // An llvm::MCSchedClassDesc augmented with some additional data.
  struct SchedClass {
    SchedClass(const llvm::MCSchedClassDesc &SD,
               const llvm::MCSubtargetInfo &STI);

    const llvm::MCSchedClassDesc *const SCDesc;
    const llvm::SmallVector<llvm::MCWriteProcResEntry, 8>
        NonRedundantWriteProcRes;
    const std::vector<std::pair<uint16_t, float>> IdealizedProcResPressure;
  };

  // Represents the intersection of a sched class and a cluster.
  class SchedClassCluster {
  public:
    const InstructionBenchmarkClustering::ClusterId &id() const {
      return ClusterId;
    }

    const std::vector<size_t> &getPointIds() const { return PointIds; }

    // Return the cluster centroid.
    const std::vector<BenchmarkMeasureStats> &getRepresentative() const {
      return Representative;
    }

    // Returns true if the cluster representative measurements match that of SC.
    bool
    measurementsMatch(const llvm::MCSubtargetInfo &STI, const SchedClass &SC,
                      const InstructionBenchmarkClustering &Clustering) const;

    void addPoint(size_t PointId,
                  const InstructionBenchmarkClustering &Clustering);

  private:
    InstructionBenchmarkClustering::ClusterId ClusterId;
    std::vector<size_t> PointIds;
    // Measurement stats for the points in the SchedClassCluster.
    std::vector<BenchmarkMeasureStats> Representative;
  };

  void printInstructionRowCsv(size_t PointId, llvm::raw_ostream &OS) const;

  void
  printSchedClassClustersHtml(const std::vector<SchedClassCluster> &Clusters,
                              const SchedClass &SC,
                              llvm::raw_ostream &OS) const;
  void printSchedClassDescHtml(const SchedClass &SC,
                               llvm::raw_ostream &OS) const;

  // Builds a map of Sched Class -> indices of points that belong to the sched
  // class.
  std::unordered_map<unsigned, std::vector<size_t>>
  makePointsPerSchedClass() const;

  template <typename EscapeTag, EscapeTag Tag>
  void writeSnippet(llvm::raw_ostream &OS, llvm::ArrayRef<uint8_t> Bytes,
                    const char *Separator) const;

  const InstructionBenchmarkClustering &Clustering_;
  llvm::MCObjectFileInfo ObjectFileInfo_;
  std::unique_ptr<llvm::MCContext> Context_;
  std::unique_ptr<llvm::MCSubtargetInfo> SubtargetInfo_;
  std::unique_ptr<llvm::MCInstrInfo> InstrInfo_;
  std::unique_ptr<llvm::MCRegisterInfo> RegInfo_;
  std::unique_ptr<llvm::MCAsmInfo> AsmInfo_;
  std::unique_ptr<llvm::MCInstPrinter> InstPrinter_;
  std::unique_ptr<llvm::MCDisassembler> Disasm_;
};

// Computes the idealized ProcRes Unit pressure. This is the expected
// distribution if the CPU scheduler can distribute the load as evenly as
// possible.
std::vector<std::pair<uint16_t, float>> computeIdealizedProcResPressure(
    const llvm::MCSchedModel &SM,
    llvm::SmallVector<llvm::MCWriteProcResEntry, 8> WPRS);

} // namespace exegesis

#endif // LLVM_TOOLS_LLVM_EXEGESIS_CLUSTERING_H
OpenPOWER on IntegriCloud