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
13#include "llvm/ADT/STLExtras.h"
14#include "llvm/ADT/SmallSet.h"
15#include "llvm/ADT/StringRef.h"
16#include "llvm/Support/raw_ostream.h"
17#include "llvm/TableGen/Error.h"
18#include "llvm/TableGen/Record.h"
19#include "llvm/TableGen/TableGenBackend.h"
20#include <cassert>
21#include <map>
22#include <string>
23#include <vector>
24
25using namespace llvm;
26
27#define DEBUG_TYPE "exegesis-emitter"
28
29namespace {
30
31class ExegesisEmitter {
32public:
33 ExegesisEmitter(RecordKeeper &RK);
34
35 void run(raw_ostream &OS) const;
36
37private:
38 unsigned getPfmCounterId(llvm::StringRef Name) const {
39 const auto It = PfmCounterNameTable.find(x: Name);
40 if (It == PfmCounterNameTable.end())
41 PrintFatalError(Msg: "no pfm counter id for " + Name);
42 return It->second;
43 }
44
45 // Collects all the ProcPfmCounters definitions available in this target.
46 void emitPfmCounters(raw_ostream &OS) const;
47
48 void emitPfmCountersInfo(const Record &Def,
49 unsigned &IssueCountersTableOffset,
50 raw_ostream &OS) const;
51
52 void emitPfmCountersLookupTable(raw_ostream &OS) const;
53
54 RecordKeeper &Records;
55 std::string Target;
56
57 // Table of counter name -> counter index.
58 const std::map<llvm::StringRef, unsigned> PfmCounterNameTable;
59};
60
61static std::map<llvm::StringRef, unsigned>
62collectPfmCounters(const RecordKeeper &Records) {
63 std::map<llvm::StringRef, unsigned> PfmCounterNameTable;
64 const auto AddPfmCounterName = [&PfmCounterNameTable](
65 const Record *PfmCounterDef) {
66 const llvm::StringRef Counter = PfmCounterDef->getValueAsString(FieldName: "Counter");
67 if (!Counter.empty())
68 PfmCounterNameTable.emplace(args: Counter, args: 0);
69 };
70 for (Record *Def : Records.getAllDerivedDefinitions(ClassName: "ProcPfmCounters")) {
71 // Check that ResourceNames are unique.
72 llvm::SmallSet<llvm::StringRef, 16> Seen;
73 for (const Record *IssueCounter :
74 Def->getValueAsListOfDefs(FieldName: "IssueCounters")) {
75 const llvm::StringRef ResourceName =
76 IssueCounter->getValueAsString(FieldName: "ResourceName");
77 if (ResourceName.empty())
78 PrintFatalError(ErrorLoc: IssueCounter->getLoc(), Msg: "invalid empty ResourceName");
79 if (!Seen.insert(V: ResourceName).second)
80 PrintFatalError(ErrorLoc: IssueCounter->getLoc(),
81 Msg: "duplicate ResourceName " + ResourceName);
82 AddPfmCounterName(IssueCounter);
83 }
84
85 for (const Record *ValidationCounter :
86 Def->getValueAsListOfDefs(FieldName: "ValidationCounters"))
87 AddPfmCounterName(ValidationCounter);
88
89 AddPfmCounterName(Def->getValueAsDef(FieldName: "CycleCounter"));
90 AddPfmCounterName(Def->getValueAsDef(FieldName: "UopsCounter"));
91 }
92 unsigned Index = 0;
93 for (auto &NameAndIndex : PfmCounterNameTable)
94 NameAndIndex.second = Index++;
95 return PfmCounterNameTable;
96}
97
98ExegesisEmitter::ExegesisEmitter(RecordKeeper &RK)
99 : Records(RK), PfmCounterNameTable(collectPfmCounters(Records: RK)) {
100 std::vector<Record *> Targets = Records.getAllDerivedDefinitions(ClassName: "Target");
101 if (Targets.size() == 0)
102 PrintFatalError(Msg: "No 'Target' subclasses defined!");
103 if (Targets.size() != 1)
104 PrintFatalError(Msg: "Multiple subclasses of Target defined!");
105 Target = std::string(Targets[0]->getName());
106}
107
108struct ValidationCounterInfo {
109 int64_t EventNumber;
110 StringRef EventName;
111 unsigned PfmCounterID;
112};
113
114bool EventNumberLess(const ValidationCounterInfo &LHS,
115 const ValidationCounterInfo &RHS) {
116 return LHS.EventNumber < RHS.EventNumber;
117}
118
119void ExegesisEmitter::emitPfmCountersInfo(const Record &Def,
120 unsigned &IssueCountersTableOffset,
121 raw_ostream &OS) const {
122 const auto CycleCounter =
123 Def.getValueAsDef(FieldName: "CycleCounter")->getValueAsString(FieldName: "Counter");
124 const auto UopsCounter =
125 Def.getValueAsDef(FieldName: "UopsCounter")->getValueAsString(FieldName: "Counter");
126 const size_t NumIssueCounters =
127 Def.getValueAsListOfDefs(FieldName: "IssueCounters").size();
128 const size_t NumValidationCounters =
129 Def.getValueAsListOfDefs(FieldName: "ValidationCounters").size();
130
131 // Emit Validation Counters Array
132 if (NumValidationCounters != 0) {
133 std::vector<ValidationCounterInfo> ValidationCounters;
134 ValidationCounters.reserve(n: NumValidationCounters);
135 for (const Record *ValidationCounter :
136 Def.getValueAsListOfDefs(FieldName: "ValidationCounters")) {
137 ValidationCounters.push_back(
138 x: {.EventNumber: ValidationCounter->getValueAsDef(FieldName: "EventType")
139 ->getValueAsInt(FieldName: "EventNumber"),
140 .EventName: ValidationCounter->getValueAsDef(FieldName: "EventType")->getName(),
141 .PfmCounterID: getPfmCounterId(Name: ValidationCounter->getValueAsString(FieldName: "Counter"))});
142 }
143 std::sort(first: ValidationCounters.begin(), last: ValidationCounters.end(),
144 comp: EventNumberLess);
145 OS << "\nstatic const std::pair<ValidationEvent, const char*> " << Target
146 << Def.getName() << "ValidationCounters[] = {\n";
147 for (const ValidationCounterInfo &VCI : ValidationCounters) {
148 OS << " { " << VCI.EventName << ", " << Target << "PfmCounterNames["
149 << VCI.PfmCounterID << "]},\n";
150 }
151 OS << "};\n";
152 }
153
154 OS << "\nstatic const PfmCountersInfo " << Target << Def.getName()
155 << " = {\n";
156
157 // Cycle Counter.
158 if (CycleCounter.empty())
159 OS << " nullptr, // No cycle counter.\n";
160 else
161 OS << " " << Target << "PfmCounterNames[" << getPfmCounterId(Name: CycleCounter)
162 << "], // Cycle counter\n";
163
164 // Uops Counter.
165 if (UopsCounter.empty())
166 OS << " nullptr, // No uops counter.\n";
167 else
168 OS << " " << Target << "PfmCounterNames[" << getPfmCounterId(Name: UopsCounter)
169 << "], // Uops counter\n";
170
171 // Issue Counters
172 if (NumIssueCounters == 0)
173 OS << " nullptr, 0, // No issue counters\n";
174 else
175 OS << " " << Target << "PfmIssueCounters + " << IssueCountersTableOffset
176 << ", " << NumIssueCounters << ", // Issue counters.\n";
177
178 // Validation Counters
179 if (NumValidationCounters == 0)
180 OS << " nullptr, 0 // No validation counters.\n";
181 else
182 OS << " " << Target << Def.getName() << "ValidationCounters, "
183 << NumValidationCounters << " // Validation counters.\n";
184
185 OS << "};\n";
186 IssueCountersTableOffset += NumIssueCounters;
187}
188
189void ExegesisEmitter::emitPfmCounters(raw_ostream &OS) const {
190 // Emit the counter name table.
191 OS << "\nstatic const char *" << Target << "PfmCounterNames[] = {\n";
192 for (const auto &NameAndIndex : PfmCounterNameTable)
193 OS << " \"" << NameAndIndex.first << "\", // " << NameAndIndex.second
194 << "\n";
195 OS << "};\n\n";
196
197 // Emit the IssueCounters table.
198 const auto PfmCounterDefs =
199 Records.getAllDerivedDefinitions(ClassName: "ProcPfmCounters");
200 // Only emit if non-empty.
201 const bool HasAtLeastOnePfmIssueCounter =
202 llvm::any_of(Range: PfmCounterDefs, P: [](const Record *Def) {
203 return !Def->getValueAsListOfDefs(FieldName: "IssueCounters").empty();
204 });
205 if (HasAtLeastOnePfmIssueCounter) {
206 OS << "static const PfmCountersInfo::IssueCounter " << Target
207 << "PfmIssueCounters[] = {\n";
208 for (const Record *Def : PfmCounterDefs) {
209 for (const Record *ICDef : Def->getValueAsListOfDefs(FieldName: "IssueCounters"))
210 OS << " { " << Target << "PfmCounterNames["
211 << getPfmCounterId(Name: ICDef->getValueAsString(FieldName: "Counter")) << "], \""
212 << ICDef->getValueAsString(FieldName: "ResourceName") << "\"},\n";
213 }
214 OS << "};\n";
215 }
216
217 // Now generate the PfmCountersInfo.
218 unsigned IssueCountersTableOffset = 0;
219 for (const Record *Def : PfmCounterDefs)
220 emitPfmCountersInfo(Def: *Def, IssueCountersTableOffset, OS);
221
222 OS << "\n";
223} // namespace
224
225void ExegesisEmitter::emitPfmCountersLookupTable(raw_ostream &OS) const {
226 std::vector<Record *> Bindings =
227 Records.getAllDerivedDefinitions(ClassName: "PfmCountersBinding");
228 assert(!Bindings.empty() && "there must be at least one binding");
229 llvm::sort(C&: Bindings, Comp: [](const Record *L, const Record *R) {
230 return L->getValueAsString(FieldName: "CpuName") < R->getValueAsString(FieldName: "CpuName");
231 });
232
233 OS << "// Sorted (by CpuName) array of pfm counters.\n"
234 << "static const CpuAndPfmCounters " << Target << "CpuPfmCounters[] = {\n";
235 for (Record *Binding : Bindings) {
236 // Emit as { "cpu", procinit },
237 OS << " { \"" //
238 << Binding->getValueAsString(FieldName: "CpuName") << "\"," //
239 << " &" << Target << Binding->getValueAsDef(FieldName: "Counters")->getName() //
240 << " },\n";
241 }
242 OS << "};\n\n";
243}
244
245void ExegesisEmitter::run(raw_ostream &OS) const {
246 emitSourceFileHeader(Desc: "Exegesis Tables", OS);
247 emitPfmCounters(OS);
248 emitPfmCountersLookupTable(OS);
249}
250
251} // end anonymous namespace
252
253static TableGen::Emitter::OptClass<ExegesisEmitter>
254 X("gen-exegesis", "Generate llvm-exegesis tables");
255

source code of llvm/utils/TableGen/ExegesisEmitter.cpp