ExegesisEmitter.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. //===- ExegesisEmitter.cpp - Generate exegesis target data ----------------===//
  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. // This tablegen backend emits llvm-exegesis information.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "llvm/ADT/STLExtras.h"
  13. #include "llvm/ADT/SmallSet.h"
  14. #include "llvm/ADT/StringRef.h"
  15. #include "llvm/Support/Debug.h"
  16. #include "llvm/Support/Format.h"
  17. #include "llvm/Support/raw_ostream.h"
  18. #include "llvm/TableGen/Error.h"
  19. #include "llvm/TableGen/Record.h"
  20. #include "llvm/TableGen/TableGenBackend.h"
  21. #include <algorithm>
  22. #include <cassert>
  23. #include <cstdint>
  24. #include <map>
  25. #include <string>
  26. #include <vector>
  27. using namespace llvm;
  28. #define DEBUG_TYPE "exegesis-emitter"
  29. namespace {
  30. class ExegesisEmitter {
  31. public:
  32. ExegesisEmitter(RecordKeeper &RK);
  33. void run(raw_ostream &OS) const;
  34. private:
  35. unsigned getPfmCounterId(llvm::StringRef Name) const {
  36. const auto It = PfmCounterNameTable.find(Name);
  37. if (It == PfmCounterNameTable.end())
  38. PrintFatalError("no pfm counter id for " + Name);
  39. return It->second;
  40. }
  41. // Collects all the ProcPfmCounters definitions available in this target.
  42. void emitPfmCounters(raw_ostream &OS) const;
  43. void emitPfmCountersInfo(const Record &Def,
  44. unsigned &IssueCountersTableOffset,
  45. raw_ostream &OS) const;
  46. void emitPfmCountersLookupTable(raw_ostream &OS) const;
  47. RecordKeeper &Records;
  48. std::string Target;
  49. // Table of counter name -> counter index.
  50. const std::map<llvm::StringRef, unsigned> PfmCounterNameTable;
  51. };
  52. static std::map<llvm::StringRef, unsigned>
  53. collectPfmCounters(const RecordKeeper &Records) {
  54. std::map<llvm::StringRef, unsigned> PfmCounterNameTable;
  55. const auto AddPfmCounterName = [&PfmCounterNameTable](
  56. const Record *PfmCounterDef) {
  57. const llvm::StringRef Counter = PfmCounterDef->getValueAsString("Counter");
  58. if (!Counter.empty())
  59. PfmCounterNameTable.emplace(Counter, 0);
  60. };
  61. for (Record *Def : Records.getAllDerivedDefinitions("ProcPfmCounters")) {
  62. // Check that ResourceNames are unique.
  63. llvm::SmallSet<llvm::StringRef, 16> Seen;
  64. for (const Record *IssueCounter :
  65. Def->getValueAsListOfDefs("IssueCounters")) {
  66. const llvm::StringRef ResourceName =
  67. IssueCounter->getValueAsString("ResourceName");
  68. if (ResourceName.empty())
  69. PrintFatalError(IssueCounter->getLoc(), "invalid empty ResourceName");
  70. if (!Seen.insert(ResourceName).second)
  71. PrintFatalError(IssueCounter->getLoc(),
  72. "duplicate ResourceName " + ResourceName);
  73. AddPfmCounterName(IssueCounter);
  74. }
  75. AddPfmCounterName(Def->getValueAsDef("CycleCounter"));
  76. AddPfmCounterName(Def->getValueAsDef("UopsCounter"));
  77. }
  78. unsigned Index = 0;
  79. for (auto &NameAndIndex : PfmCounterNameTable)
  80. NameAndIndex.second = Index++;
  81. return PfmCounterNameTable;
  82. }
  83. ExegesisEmitter::ExegesisEmitter(RecordKeeper &RK)
  84. : Records(RK), PfmCounterNameTable(collectPfmCounters(RK)) {
  85. std::vector<Record *> Targets = Records.getAllDerivedDefinitions("Target");
  86. if (Targets.size() == 0)
  87. PrintFatalError("No 'Target' subclasses defined!");
  88. if (Targets.size() != 1)
  89. PrintFatalError("Multiple subclasses of Target defined!");
  90. Target = std::string(Targets[0]->getName());
  91. }
  92. void ExegesisEmitter::emitPfmCountersInfo(const Record &Def,
  93. unsigned &IssueCountersTableOffset,
  94. raw_ostream &OS) const {
  95. const auto CycleCounter =
  96. Def.getValueAsDef("CycleCounter")->getValueAsString("Counter");
  97. const auto UopsCounter =
  98. Def.getValueAsDef("UopsCounter")->getValueAsString("Counter");
  99. const size_t NumIssueCounters =
  100. Def.getValueAsListOfDefs("IssueCounters").size();
  101. OS << "\nstatic const PfmCountersInfo " << Target << Def.getName()
  102. << " = {\n";
  103. // Cycle Counter.
  104. if (CycleCounter.empty())
  105. OS << " nullptr, // No cycle counter.\n";
  106. else
  107. OS << " " << Target << "PfmCounterNames[" << getPfmCounterId(CycleCounter)
  108. << "], // Cycle counter\n";
  109. // Uops Counter.
  110. if (UopsCounter.empty())
  111. OS << " nullptr, // No uops counter.\n";
  112. else
  113. OS << " " << Target << "PfmCounterNames[" << getPfmCounterId(UopsCounter)
  114. << "], // Uops counter\n";
  115. // Issue Counters
  116. if (NumIssueCounters == 0)
  117. OS << " nullptr, // No issue counters.\n 0\n";
  118. else
  119. OS << " " << Target << "PfmIssueCounters + " << IssueCountersTableOffset
  120. << ", " << NumIssueCounters << " // Issue counters.\n";
  121. OS << "};\n";
  122. IssueCountersTableOffset += NumIssueCounters;
  123. }
  124. void ExegesisEmitter::emitPfmCounters(raw_ostream &OS) const {
  125. // Emit the counter name table.
  126. OS << "\nstatic const char *" << Target << "PfmCounterNames[] = {\n";
  127. for (const auto &NameAndIndex : PfmCounterNameTable)
  128. OS << " \"" << NameAndIndex.first << "\", // " << NameAndIndex.second
  129. << "\n";
  130. OS << "};\n\n";
  131. // Emit the IssueCounters table.
  132. const auto PfmCounterDefs =
  133. Records.getAllDerivedDefinitions("ProcPfmCounters");
  134. // Only emit if non-empty.
  135. const bool HasAtLeastOnePfmIssueCounter =
  136. llvm::any_of(PfmCounterDefs, [](const Record *Def) {
  137. return !Def->getValueAsListOfDefs("IssueCounters").empty();
  138. });
  139. if (HasAtLeastOnePfmIssueCounter) {
  140. OS << "static const PfmCountersInfo::IssueCounter " << Target
  141. << "PfmIssueCounters[] = {\n";
  142. for (const Record *Def : PfmCounterDefs) {
  143. for (const Record *ICDef : Def->getValueAsListOfDefs("IssueCounters"))
  144. OS << " { " << Target << "PfmCounterNames["
  145. << getPfmCounterId(ICDef->getValueAsString("Counter")) << "], \""
  146. << ICDef->getValueAsString("ResourceName") << "\"},\n";
  147. }
  148. OS << "};\n";
  149. }
  150. // Now generate the PfmCountersInfo.
  151. unsigned IssueCountersTableOffset = 0;
  152. for (const Record *Def : PfmCounterDefs)
  153. emitPfmCountersInfo(*Def, IssueCountersTableOffset, OS);
  154. OS << "\n";
  155. } // namespace
  156. void ExegesisEmitter::emitPfmCountersLookupTable(raw_ostream &OS) const {
  157. std::vector<Record *> Bindings =
  158. Records.getAllDerivedDefinitions("PfmCountersBinding");
  159. assert(!Bindings.empty() && "there must be at least one binding");
  160. llvm::sort(Bindings, [](const Record *L, const Record *R) {
  161. return L->getValueAsString("CpuName") < R->getValueAsString("CpuName");
  162. });
  163. OS << "// Sorted (by CpuName) array of pfm counters.\n"
  164. << "static const CpuAndPfmCounters " << Target << "CpuPfmCounters[] = {\n";
  165. for (Record *Binding : Bindings) {
  166. // Emit as { "cpu", procinit },
  167. OS << " { \"" //
  168. << Binding->getValueAsString("CpuName") << "\"," //
  169. << " &" << Target << Binding->getValueAsDef("Counters")->getName() //
  170. << " },\n";
  171. }
  172. OS << "};\n\n";
  173. }
  174. void ExegesisEmitter::run(raw_ostream &OS) const {
  175. emitSourceFileHeader("Exegesis Tables", OS);
  176. emitPfmCounters(OS);
  177. emitPfmCountersLookupTable(OS);
  178. }
  179. } // end anonymous namespace
  180. namespace llvm {
  181. void EmitExegesis(RecordKeeper &RK, raw_ostream &OS) {
  182. ExegesisEmitter(RK).run(OS);
  183. }
  184. } // end namespace llvm