Analysis.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. //===-- Analysis.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. /// Analysis output for benchmark results.
  11. ///
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_TOOLS_LLVM_EXEGESIS_ANALYSIS_H
  14. #define LLVM_TOOLS_LLVM_EXEGESIS_ANALYSIS_H
  15. #include "Clustering.h"
  16. #include "SchedClassResolution.h"
  17. #include "llvm/MC/MCContext.h"
  18. #include "llvm/MC/MCDisassembler/MCDisassembler.h"
  19. #include "llvm/MC/MCInstPrinter.h"
  20. #include "llvm/MC/MCInstrInfo.h"
  21. #include "llvm/MC/MCObjectFileInfo.h"
  22. #include "llvm/MC/MCSubtargetInfo.h"
  23. #include "llvm/MC/TargetRegistry.h"
  24. #include "llvm/Support/Error.h"
  25. #include "llvm/Support/raw_ostream.h"
  26. #include <memory>
  27. #include <set>
  28. #include <string>
  29. #include <unordered_map>
  30. namespace llvm {
  31. namespace exegesis {
  32. // A helper class to analyze benchmark results for a target.
  33. class Analysis {
  34. public:
  35. Analysis(const Target &Target, std::unique_ptr<MCSubtargetInfo> SubtargetInfo,
  36. std::unique_ptr<MCInstrInfo> InstrInfo,
  37. const InstructionBenchmarkClustering &Clustering,
  38. double AnalysisInconsistencyEpsilon,
  39. bool AnalysisDisplayUnstableOpcodes,
  40. const std::string &ForceCpuName = "");
  41. // Prints a csv of instructions for each cluster.
  42. struct PrintClusters {};
  43. // Find potential errors in the scheduling information given measurements.
  44. struct PrintSchedClassInconsistencies {};
  45. template <typename Pass> Error run(raw_ostream &OS) const;
  46. private:
  47. using ClusterId = InstructionBenchmarkClustering::ClusterId;
  48. // Represents the intersection of a sched class and a cluster.
  49. class SchedClassCluster {
  50. public:
  51. const InstructionBenchmarkClustering::ClusterId &id() const {
  52. return ClusterId;
  53. }
  54. const std::vector<size_t> &getPointIds() const { return PointIds; }
  55. void addPoint(size_t PointId,
  56. const InstructionBenchmarkClustering &Clustering);
  57. // Return the cluster centroid.
  58. const SchedClassClusterCentroid &getCentroid() const { return Centroid; }
  59. // Returns true if the cluster representative measurements match that of SC.
  60. bool
  61. measurementsMatch(const MCSubtargetInfo &STI, const ResolvedSchedClass &SC,
  62. const InstructionBenchmarkClustering &Clustering,
  63. const double AnalysisInconsistencyEpsilonSquared_) const;
  64. private:
  65. InstructionBenchmarkClustering::ClusterId ClusterId;
  66. std::vector<size_t> PointIds;
  67. // Measurement stats for the points in the SchedClassCluster.
  68. SchedClassClusterCentroid Centroid;
  69. };
  70. void printInstructionRowCsv(size_t PointId, raw_ostream &OS) const;
  71. void printClusterRawHtml(const InstructionBenchmarkClustering::ClusterId &Id,
  72. StringRef display_name, llvm::raw_ostream &OS) const;
  73. void printPointHtml(const InstructionBenchmark &Point,
  74. llvm::raw_ostream &OS) const;
  75. void
  76. printSchedClassClustersHtml(const std::vector<SchedClassCluster> &Clusters,
  77. const ResolvedSchedClass &SC,
  78. raw_ostream &OS) const;
  79. void printSchedClassDescHtml(const ResolvedSchedClass &SC,
  80. raw_ostream &OS) const;
  81. // A pair of (Sched Class, indices of points that belong to the sched
  82. // class).
  83. struct ResolvedSchedClassAndPoints {
  84. explicit ResolvedSchedClassAndPoints(ResolvedSchedClass &&RSC);
  85. ResolvedSchedClass RSC;
  86. std::vector<size_t> PointIds;
  87. };
  88. // Builds a list of ResolvedSchedClassAndPoints.
  89. std::vector<ResolvedSchedClassAndPoints> makePointsPerSchedClass() const;
  90. template <typename EscapeTag, EscapeTag Tag>
  91. void writeSnippet(raw_ostream &OS, ArrayRef<uint8_t> Bytes,
  92. const char *Separator) const;
  93. const InstructionBenchmarkClustering &Clustering_;
  94. std::unique_ptr<MCContext> Context_;
  95. std::unique_ptr<MCSubtargetInfo> SubtargetInfo_;
  96. std::unique_ptr<MCInstrInfo> InstrInfo_;
  97. std::unique_ptr<MCRegisterInfo> RegInfo_;
  98. std::unique_ptr<MCAsmInfo> AsmInfo_;
  99. std::unique_ptr<MCInstPrinter> InstPrinter_;
  100. std::unique_ptr<MCDisassembler> Disasm_;
  101. const double AnalysisInconsistencyEpsilonSquared_;
  102. const bool AnalysisDisplayUnstableOpcodes_;
  103. };
  104. } // namespace exegesis
  105. } // namespace llvm
  106. #endif // LLVM_TOOLS_LLVM_EXEGESIS_CLUSTERING_H