Analysis.h 4.3 KB

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