Skip to content

Commit 8b84de2

Browse files
authored
[llvm-exegesis][NFC] Refactor all ValidationEvent info in a single … (#82256)
…table. All data is derived from a single table rather than being spread out over an enum, a table and the main entry point. This is intended as a replacement for #82092.
1 parent 02fad05 commit 8b84de2

File tree

9 files changed

+127
-80
lines changed

9 files changed

+127
-80
lines changed

llvm/include/llvm/Target/TargetPfmCounters.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class ValidationEvent <int event_number> {
3535
int EventNumber = event_number;
3636
}
3737

38+
// TableGen names for events defined in `llvm::exegesis::ValidationEvent`.
3839
def InstructionRetired : ValidationEvent<0>;
3940
def L1DCacheLoadMiss : ValidationEvent<1>;
4041
def L1DCacheStoreMiss : ValidationEvent<2>;

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

Lines changed: 3 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "BenchmarkResult.h"
1010
#include "BenchmarkRunner.h"
1111
#include "Error.h"
12+
#include "ValidationEvent.h"
1213
#include "llvm/ADT/STLExtras.h"
1314
#include "llvm/ADT/ScopeExit.h"
1415
#include "llvm/ADT/StringRef.h"
@@ -198,7 +199,7 @@ struct CustomMappingTraits<std::map<exegesis::ValidationEvent, int64_t>> {
198199
static void inputOne(IO &Io, StringRef KeyStr,
199200
std::map<exegesis::ValidationEvent, int64_t> &VI) {
200201
Expected<exegesis::ValidationEvent> Key =
201-
exegesis::stringToValidationEvent(KeyStr);
202+
exegesis::getValidationEventByName(KeyStr);
202203
if (!Key) {
203204
Io.setError("Key is not a valid validation event");
204205
return;
@@ -208,7 +209,7 @@ struct CustomMappingTraits<std::map<exegesis::ValidationEvent, int64_t>> {
208209

209210
static void output(IO &Io, std::map<exegesis::ValidationEvent, int64_t> &VI) {
210211
for (auto &IndividualVI : VI) {
211-
Io.mapRequired(exegesis::validationEventToString(IndividualVI.first),
212+
Io.mapRequired(exegesis::getValidationEventName(IndividualVI.first),
212213
IndividualVI.second);
213214
}
214215
}
@@ -441,49 +442,5 @@ bool operator==(const BenchmarkMeasure &A, const BenchmarkMeasure &B) {
441442
std::tie(B.Key, B.PerInstructionValue, B.PerSnippetValue);
442443
}
443444

444-
const char *validationEventToString(ValidationEvent VE) {
445-
switch (VE) {
446-
case exegesis::ValidationEvent::InstructionRetired:
447-
return "instructions-retired";
448-
case exegesis::ValidationEvent::L1DCacheLoadMiss:
449-
return "l1d-cache-load-misses";
450-
case exegesis::ValidationEvent::L1DCacheStoreMiss:
451-
return "l1d-cache-store-misses";
452-
case exegesis::ValidationEvent::L1ICacheLoadMiss:
453-
return "l1i-cache-load-misses";
454-
case exegesis::ValidationEvent::DataTLBLoadMiss:
455-
return "data-tlb-load-misses";
456-
case exegesis::ValidationEvent::DataTLBStoreMiss:
457-
return "data-tlb-store-misses";
458-
case exegesis::ValidationEvent::InstructionTLBLoadMiss:
459-
return "instruction-tlb-load-misses";
460-
case exegesis::ValidationEvent::BranchPredictionMiss:
461-
return "branch-prediction-misses";
462-
}
463-
llvm_unreachable("Unhandled exegesis::ValidationEvent enum");
464-
}
465-
466-
Expected<ValidationEvent> stringToValidationEvent(StringRef Input) {
467-
if (Input == "instructions-retired")
468-
return exegesis::ValidationEvent::InstructionRetired;
469-
else if (Input == "l1d-cache-load-misses")
470-
return exegesis::ValidationEvent::L1DCacheLoadMiss;
471-
else if (Input == "l1d-cache-store-misses")
472-
return exegesis::ValidationEvent::L1DCacheStoreMiss;
473-
else if (Input == "l1i-cache-load-misses")
474-
return exegesis::ValidationEvent::L1ICacheLoadMiss;
475-
else if (Input == "data-tlb-load-misses")
476-
return exegesis::ValidationEvent::DataTLBLoadMiss;
477-
else if (Input == "data-tlb-store-misses")
478-
return exegesis::ValidationEvent::DataTLBStoreMiss;
479-
else if (Input == "instruction-tlb-load-misses")
480-
return exegesis::ValidationEvent::InstructionTLBLoadMiss;
481-
else if (Input == "branch-prediction-misses")
482-
return exegesis::ValidationEvent::BranchPredictionMiss;
483-
else
484-
return make_error<StringError>("Invalid validation event string",
485-
errc::invalid_argument);
486-
}
487-
488445
} // namespace exegesis
489446
} // namespace llvm

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

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
#include "LlvmState.h"
1919
#include "RegisterValue.h"
20+
#include "ValidationEvent.h"
2021
#include "llvm/ADT/StringRef.h"
2122
#include "llvm/MC/MCInst.h"
2223
#include "llvm/MC/MCInstBuilder.h"
@@ -32,20 +33,6 @@ class Error;
3233

3334
namespace exegesis {
3435

35-
enum ValidationEvent {
36-
InstructionRetired,
37-
L1DCacheLoadMiss,
38-
L1DCacheStoreMiss,
39-
L1ICacheLoadMiss,
40-
DataTLBLoadMiss,
41-
DataTLBStoreMiss,
42-
InstructionTLBLoadMiss,
43-
BranchPredictionMiss
44-
};
45-
46-
const char *validationEventToString(exegesis::ValidationEvent VE);
47-
Expected<ValidationEvent> stringToValidationEvent(StringRef Input);
48-
4936
enum class BenchmarkPhaseSelectorE {
5037
PrepareSnippet,
5138
PrepareAndAssembleSnippet,

llvm/tools/llvm-exegesis/lib/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ add_llvm_library(LLVMExegesis
7373
SubprocessMemory.cpp
7474
Target.cpp
7575
UopsBenchmarkRunner.cpp
76+
ValidationEvent.cpp
7677

7778
LINK_LIBS ${libs}
7879

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,8 @@ Expected<std::vector<BenchmarkMeasure>> LatencyBenchmarkRunner::runMeasurements(
107107
}
108108

109109
for (size_t I = 0; I < ValCounterValues.size(); ++I) {
110-
LLVM_DEBUG(dbgs() << validationEventToString(ValidationCounters[I])
111-
<< ": " << IterationValCounterValues[I] << "\n");
110+
LLVM_DEBUG(dbgs() << getValidationEventName(ValidationCounters[I]) << ": "
111+
<< IterationValCounterValues[I] << "\n");
112112
ValCounterValues[I] += IterationValCounterValues[I];
113113
}
114114
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "LlvmState.h"
2323
#include "PerfHelper.h"
2424
#include "SnippetGenerator.h"
25+
#include "ValidationEvent.h"
2526
#include "llvm/CodeGen/TargetPassConfig.h"
2627
#include "llvm/IR/CallingConv.h"
2728
#include "llvm/IR/LegacyPassManager.h"
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
2+
#include "ValidationEvent.h"
3+
#include "llvm/ADT/StringRef.h"
4+
#include "llvm/Support/Errc.h"
5+
#include "llvm/Support/Error.h"
6+
7+
namespace llvm {
8+
namespace exegesis {
9+
10+
namespace {
11+
12+
struct ValidationEventInfo {
13+
const char *const Name;
14+
const char *const Description;
15+
};
16+
17+
// Information about validation events, indexed by `ValidationEvent` enum
18+
// value.
19+
static constexpr ValidationEventInfo ValidationEventInfos[] = {
20+
{"instructions-retired", "Count retired instructions"},
21+
{"l1d-cache-load-misses", "Count L1D load cache misses"},
22+
{"l1d-cache-store-misses", "Count L1D store cache misses"},
23+
{"l1i-cache-load-misses", "Count L1I load cache misses"},
24+
{"data-tlb-load-misses", "Count DTLB load misses"},
25+
{"data-tlb-store-misses", "Count DTLB store misses"},
26+
{"instruction-tlb-load-misses", "Count ITLB load misses"},
27+
{"branch-prediction-misses", "Branch prediction misses"},
28+
};
29+
30+
static_assert(sizeof(ValidationEventInfos) ==
31+
NumValidationEvents * sizeof(ValidationEventInfo),
32+
"please update ValidationEventInfos");
33+
34+
} // namespace
35+
36+
const char *getValidationEventName(ValidationEvent VE) {
37+
return ValidationEventInfos[VE].Name;
38+
}
39+
const char *getValidationEventDescription(ValidationEvent VE) {
40+
return ValidationEventInfos[VE].Description;
41+
}
42+
43+
Expected<ValidationEvent> getValidationEventByName(StringRef Name) {
44+
int VE = 0;
45+
for (const ValidationEventInfo &Info : ValidationEventInfos) {
46+
if (Name == Info.Name)
47+
return static_cast<ValidationEvent>(VE);
48+
++VE;
49+
}
50+
51+
return make_error<StringError>("Invalid validation event string",
52+
errc::invalid_argument);
53+
}
54+
55+
} // namespace exegesis
56+
} // namespace llvm
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
//===-- ValidationEvent.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+
/// Definitions and utilities for Validation Events.
11+
///
12+
//===----------------------------------------------------------------------===//
13+
14+
#ifndef LLVM_TOOLS_LLVM_EXEGESIS_VALIDATIONEVENT_H
15+
#define LLVM_TOOLS_LLVM_EXEGESIS_VALIDATIONEVENT_H
16+
17+
#include "llvm/ADT/StringRef.h"
18+
#include "llvm/Support/Error.h"
19+
20+
namespace llvm {
21+
22+
namespace exegesis {
23+
24+
// The main list of supported validation events. The mapping between validation
25+
// events and pfm counters is defined in TableDef files for each target.
26+
enum ValidationEvent {
27+
InstructionRetired,
28+
L1DCacheLoadMiss,
29+
L1DCacheStoreMiss,
30+
L1ICacheLoadMiss,
31+
DataTLBLoadMiss,
32+
DataTLBStoreMiss,
33+
InstructionTLBLoadMiss,
34+
BranchPredictionMiss,
35+
// Number of events.
36+
NumValidationEvents,
37+
};
38+
39+
// Returns the name/description of the given event.
40+
const char *getValidationEventName(ValidationEvent VE);
41+
const char *getValidationEventDescription(ValidationEvent VE);
42+
43+
// Returns the ValidationEvent with the given name.
44+
Expected<ValidationEvent> getValidationEventByName(StringRef Name);
45+
46+
// Command-line options for validation events.
47+
struct ValidationEventOptions {
48+
template <class Opt> void apply(Opt &O) const {
49+
for (int I = 0; I < NumValidationEvents; ++I) {
50+
const auto VE = static_cast<ValidationEvent>(I);
51+
O.getParser().addLiteralOption(getValidationEventName(VE), VE,
52+
getValidationEventDescription(VE));
53+
}
54+
}
55+
};
56+
57+
} // namespace exegesis
58+
} // namespace llvm
59+
60+
#endif

llvm/tools/llvm-exegesis/llvm-exegesis.cpp

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "lib/SnippetRepetitor.h"
2626
#include "lib/Target.h"
2727
#include "lib/TargetSelect.h"
28+
#include "lib/ValidationEvent.h"
2829
#include "llvm/ADT/StringExtras.h"
2930
#include "llvm/ADT/Twine.h"
3031
#include "llvm/MC/MCInstBuilder.h"
@@ -278,24 +279,7 @@ static cl::list<ValidationEvent> ValidationCounters(
278279
cl::desc(
279280
"The name of a validation counter to run concurrently with the main "
280281
"counter to validate benchmarking assumptions"),
281-
cl::CommaSeparated, cl::cat(BenchmarkOptions),
282-
cl::values(
283-
clEnumValN(ValidationEvent::InstructionRetired, "instructions-retired",
284-
"Count retired instructions"),
285-
clEnumValN(ValidationEvent::L1DCacheLoadMiss, "l1d-cache-load-misses",
286-
"Count L1D load cache misses"),
287-
clEnumValN(ValidationEvent::L1DCacheStoreMiss, "l1d-cache-store-misses",
288-
"Count L1D store cache misses"),
289-
clEnumValN(ValidationEvent::L1ICacheLoadMiss, "l1i-cache-load-misses",
290-
"Count L1I load cache misses"),
291-
clEnumValN(ValidationEvent::DataTLBLoadMiss, "data-tlb-load-misses",
292-
"Count DTLB load misses"),
293-
clEnumValN(ValidationEvent::DataTLBStoreMiss, "data-tlb-store-misses",
294-
"Count DTLB store misses"),
295-
clEnumValN(ValidationEvent::InstructionTLBLoadMiss,
296-
"instruction-tlb-load-misses", "Count ITLB load misses"),
297-
clEnumValN(ValidationEvent::BranchPredictionMiss,
298-
"branch-prediction-misses", "Branch prediction misses")));
282+
cl::CommaSeparated, cl::cat(BenchmarkOptions), ValidationEventOptions());
299283

300284
static ExitOnError ExitOnErr("llvm-exegesis error: ");
301285

0 commit comments

Comments
 (0)