Skip to content

Commit 21a784f

Browse files
[llvm-exegesis] Add tablegen support for validation counters (llvm#76652)
This patch adds support in the llvm-exegesis tablegen emitter for validation counters. Full support for validation counters in llvm-exegesis will be added in a future patch.
1 parent 98e3d98 commit 21a784f

File tree

5 files changed

+97
-4
lines changed

5 files changed

+97
-4
lines changed

llvm/include/llvm/Target/TargetPfmCounters.td

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,27 @@ class PfmIssueCounter<string resource_name, string counter>
2828
string ResourceName = resource_name;
2929
}
3030

31+
// Definition of a validation event. A validation event represents a specific
32+
// event that can be measured using performance counters that is interesting
33+
// in regard to the snippet state.
34+
class ValidationEvent <int event_number> {
35+
int EventNumber = event_number;
36+
}
37+
38+
def L1DCacheLoadMiss : ValidationEvent<0>;
39+
def InstructionRetired : ValidationEvent<1>;
40+
def DataTLBLoadMiss : ValidationEvent<2>;
41+
def DataTLBStoreMiss : ValidationEvent<3>;
42+
43+
// PfmValidationCounter provides a mapping between the events that are
44+
// are interesting in regards to the snippet execution environment and
45+
// a concrete performance counter name that can be looked up in libpfm.
46+
class PfmValidationCounter<ValidationEvent event_type, string counter>
47+
: PfmCounter<counter> {
48+
// The name of the event that the validation counter detects.
49+
ValidationEvent EventType = event_type;
50+
}
51+
3152
def NoPfmCounter : PfmCounter <""> {}
3253

3354
// Set of PfmCounters for measuring sched model characteristics.
@@ -38,6 +59,9 @@ class ProcPfmCounters {
3859
PfmCounter UopsCounter = NoPfmCounter;
3960
// Processors can define how to measure issued uops by defining IssueCounters.
4061
list<PfmIssueCounter> IssueCounters = [];
62+
// Processor can list mappings between validation events and real counters
63+
// to measure the specified events.
64+
list<PfmValidationCounter> ValidationCounters = [];
4165
}
4266

4367
// A binding of a set of counters to a CPU.

llvm/lib/Target/X86/X86PfmCounters.td

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,9 @@ def ZnVer2PfmCounters : ProcPfmCounters {
275275
PfmIssueCounter<"Zn2AGU", "ls_dispatch:ld_st_dispatch + ls_dispatch:ld_dispatch + ls_dispatch:store_dispatch">,
276276
PfmIssueCounter<"Zn2Divider", "div_op_count">
277277
];
278+
let ValidationCounters = [
279+
PfmValidationCounter<InstructionRetired, "RETIRED_INSTRUCTIONS">
280+
];
278281
}
279282
def : PfmCountersBinding<"znver2", ZnVer2PfmCounters>;
280283

@@ -288,6 +291,9 @@ def ZnVer3PfmCounters : ProcPfmCounters {
288291
PfmIssueCounter<"Zn3Store", "ls_dispatch:store_dispatch">,
289292
PfmIssueCounter<"Zn3Divider", "div_op_count">
290293
];
294+
let ValidationCounters = [
295+
PfmValidationCounter<InstructionRetired, "RETIRED_INSTRUCTIONS">
296+
];
291297
}
292298
def : PfmCountersBinding<"znver3", ZnVer3PfmCounters>;
293299

llvm/tools/llvm-exegesis/lib/Target.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,15 @@ std::unique_ptr<BenchmarkRunner> ExegesisTarget::createUopsBenchmarkRunner(
149149

150150
static_assert(std::is_trivial_v<PfmCountersInfo>,
151151
"We shouldn't have dynamic initialization here");
152+
152153
const PfmCountersInfo PfmCountersInfo::Default = {nullptr, nullptr, nullptr,
153-
0u};
154+
0u, nullptr, 0u};
154155
const PfmCountersInfo PfmCountersInfo::Dummy = {
155-
pfm::PerfEvent::DummyEventString, pfm::PerfEvent::DummyEventString, nullptr,
156+
pfm::PerfEvent::DummyEventString,
157+
pfm::PerfEvent::DummyEventString,
158+
nullptr,
159+
0u,
160+
nullptr,
156161
0u};
157162

158163
const PfmCountersInfo &ExegesisTarget::getPfmCounters(StringRef CpuName) const {

llvm/tools/llvm-exegesis/lib/Target.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@ extern cl::OptionCategory Options;
3939
extern cl::OptionCategory BenchmarkOptions;
4040
extern cl::OptionCategory AnalysisOptions;
4141

42+
enum ValidationEvent {
43+
L1DCacheLoadMiss,
44+
InstructionRetired,
45+
DataTLBLoadMiss,
46+
DataTLBStoreMiss
47+
};
48+
4249
struct PfmCountersInfo {
4350
// An optional name of a performance counter that can be used to measure
4451
// cycles.
@@ -59,6 +66,9 @@ struct PfmCountersInfo {
5966
const IssueCounter *IssueCounters;
6067
unsigned NumIssueCounters;
6168

69+
const std::pair<ValidationEvent, const char *> *ValidationEvents;
70+
unsigned NumValidationEvents;
71+
6272
static const PfmCountersInfo Default;
6373
static const PfmCountersInfo Dummy;
6474
};

llvm/utils/TableGen/ExegesisEmitter.cpp

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,11 @@ collectPfmCounters(const RecordKeeper &Records) {
8181
"duplicate ResourceName " + ResourceName);
8282
AddPfmCounterName(IssueCounter);
8383
}
84+
85+
for (const Record *ValidationCounter :
86+
Def->getValueAsListOfDefs("ValidationCounters"))
87+
AddPfmCounterName(ValidationCounter);
88+
8489
AddPfmCounterName(Def->getValueAsDef("CycleCounter"));
8590
AddPfmCounterName(Def->getValueAsDef("UopsCounter"));
8691
}
@@ -100,6 +105,17 @@ ExegesisEmitter::ExegesisEmitter(RecordKeeper &RK)
100105
Target = std::string(Targets[0]->getName());
101106
}
102107

108+
struct ValidationCounterInfo {
109+
int64_t EventNumber;
110+
StringRef EventName;
111+
unsigned PfmCounterID;
112+
};
113+
114+
bool EventNumberLess(const ValidationCounterInfo &LHS,
115+
const ValidationCounterInfo &RHS) {
116+
return LHS.EventNumber < RHS.EventNumber;
117+
}
118+
103119
void ExegesisEmitter::emitPfmCountersInfo(const Record &Def,
104120
unsigned &IssueCountersTableOffset,
105121
raw_ostream &OS) const {
@@ -109,6 +125,31 @@ void ExegesisEmitter::emitPfmCountersInfo(const Record &Def,
109125
Def.getValueAsDef("UopsCounter")->getValueAsString("Counter");
110126
const size_t NumIssueCounters =
111127
Def.getValueAsListOfDefs("IssueCounters").size();
128+
const size_t NumValidationCounters =
129+
Def.getValueAsListOfDefs("ValidationCounters").size();
130+
131+
// Emit Validation Counters Array
132+
if (NumValidationCounters != 0) {
133+
std::vector<ValidationCounterInfo> ValidationCounters;
134+
ValidationCounters.reserve(NumValidationCounters);
135+
for (const Record *ValidationCounter :
136+
Def.getValueAsListOfDefs("ValidationCounters")) {
137+
ValidationCounters.push_back(
138+
{ValidationCounter->getValueAsDef("EventType")
139+
->getValueAsInt("EventNumber"),
140+
ValidationCounter->getValueAsDef("EventType")->getName(),
141+
getPfmCounterId(ValidationCounter->getValueAsString("Counter"))});
142+
}
143+
std::sort(ValidationCounters.begin(), ValidationCounters.end(),
144+
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+
}
112153

113154
OS << "\nstatic const PfmCountersInfo " << Target << Def.getName()
114155
<< " = {\n";
@@ -129,10 +170,17 @@ void ExegesisEmitter::emitPfmCountersInfo(const Record &Def,
129170

130171
// Issue Counters
131172
if (NumIssueCounters == 0)
132-
OS << " nullptr, // No issue counters.\n 0\n";
173+
OS << " nullptr, 0, // No issue counters\n";
133174
else
134175
OS << " " << Target << "PfmIssueCounters + " << IssueCountersTableOffset
135-
<< ", " << NumIssueCounters << " // Issue counters.\n";
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";
136184

137185
OS << "};\n";
138186
IssueCountersTableOffset += NumIssueCounters;

0 commit comments

Comments
 (0)