Analysis.h 4.5 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/Support/Error.h"
  24. #include "llvm/Support/TargetRegistry.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<MCInstrInfo> InstrInfo,
  36. const InstructionBenchmarkClustering &Clustering,
  37. double AnalysisInconsistencyEpsilon,
  38. bool AnalysisDisplayUnstableOpcodes,
  39. const std::string &ForceCpuName = "");
  40. // Prints a csv of instructions for each cluster.
  41. struct PrintClusters {};
  42. // Find potential errors in the scheduling information given measurements.
  43. struct PrintSchedClassInconsistencies {};
  44. template <typename Pass> Error run(raw_ostream &OS) const;
  45. private:
  46. using ClusterId = InstructionBenchmarkClustering::ClusterId;
  47. // Represents the intersection of a sched class and a cluster.
  48. class SchedClassCluster {
  49. public:
  50. const InstructionBenchmarkClustering::ClusterId &id() const {
  51. return ClusterId;
  52. }
  53. const std::vector<size_t> &getPointIds() const { return PointIds; }
  54. void addPoint(size_t PointId,
  55. const InstructionBenchmarkClustering &Clustering);
  56. // Return the cluster centroid.
  57. const SchedClassClusterCentroid &getCentroid() const { return Centroid; }
  58. // Returns true if the cluster representative measurements match that of SC.
  59. bool
  60. measurementsMatch(const MCSubtargetInfo &STI, const ResolvedSchedClass &SC,
  61. const InstructionBenchmarkClustering &Clustering,
  62. const double AnalysisInconsistencyEpsilonSquared_) const;
  63. private:
  64. InstructionBenchmarkClustering::ClusterId ClusterId;
  65. std::vector<size_t> PointIds;
  66. // Measurement stats for the points in the SchedClassCluster.
  67. SchedClassClusterCentroid Centroid;
  68. };
  69. void printInstructionRowCsv(size_t PointId, raw_ostream &OS) const;
  70. void printClusterRawHtml(const InstructionBenchmarkClustering::ClusterId &Id,
  71. StringRef display_name, llvm::raw_ostream &OS) const;
  72. void printPointHtml(const InstructionBenchmark &Point,
  73. llvm::raw_ostream &OS) const;
  74. void
  75. printSchedClassClustersHtml(const std::vector<SchedClassCluster> &Clusters,
  76. const ResolvedSchedClass &SC,
  77. raw_ostream &OS) const;
  78. void printSchedClassDescHtml(const ResolvedSchedClass &SC,
  79. raw_ostream &OS) const;
  80. // A pair of (Sched Class, indices of points that belong to the sched
  81. // class).
  82. struct ResolvedSchedClassAndPoints {
  83. explicit ResolvedSchedClassAndPoints(ResolvedSchedClass &&RSC);
  84. ResolvedSchedClass RSC;
  85. std::vector<size_t> PointIds;
  86. };
  87. // Builds a list of ResolvedSchedClassAndPoints.
  88. std::vector<ResolvedSchedClassAndPoints> makePointsPerSchedClass() const;
  89. template <typename EscapeTag, EscapeTag Tag>
  90. void writeSnippet(raw_ostream &OS, ArrayRef<uint8_t> Bytes,
  91. const char *Separator) const;
  92. const InstructionBenchmarkClustering &Clustering_;
  93. MCObjectFileInfo ObjectFileInfo_;
  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