ExegesisEmitter.cpp 7.3 KB

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