Skip to content

Commit

Permalink
[llvm-exegesis] Refactor InstructionBenchmark to Benchmark
Browse files Browse the repository at this point in the history
When llvm-exegesis was first introduced, it only supported benchmarking
individual instructions, hence the name for the data structure storing
the data corresponding to a benchmark being called InstructionBenchmark
made sense. However, now that benchmarking arbitrary snippets is
supported, InstructionBenchmark doesn't correspond to a single
instruction. This patch refactors InstructionBenchmark to be called
Benchmark to clean up this little bit of technical debt.

Reviewed By: courbet

Differential Revision: https://reviews.llvm.org/D146884
  • Loading branch information
boomanaiden154 committed Mar 27, 2023
1 parent 4a9dc94 commit 389bf5d
Show file tree
Hide file tree
Showing 24 changed files with 222 additions and 222 deletions.
26 changes: 13 additions & 13 deletions llvm/tools/llvm-exegesis/lib/Analysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ void writeEscaped<kEscapeHtmlString>(raw_ostream &OS, const StringRef S) {
template <EscapeTag Tag>
static void
writeClusterId(raw_ostream &OS,
const InstructionBenchmarkClustering::ClusterId &CID) {
const BenchmarkClustering::ClusterId &CID) {
if (CID.isNoise())
writeEscaped<Tag>(OS, "[noise]");
else if (CID.isError())
Expand Down Expand Up @@ -126,7 +126,7 @@ void Analysis::writeSnippet(raw_ostream &OS, ArrayRef<uint8_t> Bytes,
// point coordinates (measurements).
void Analysis::printInstructionRowCsv(const size_t PointId,
raw_ostream &OS) const {
const InstructionBenchmark &Point = Clustering_.getPoints()[PointId];
const Benchmark &Point = Clustering_.getPoints()[PointId];
writeClusterId<kEscapeCsv>(OS, Clustering_.getClusterIdForPoint(PointId));
OS << kCsvSep;
writeSnippet<EscapeTag, kEscapeCsv>(OS, Point.AssembledSnippet, "; ");
Expand All @@ -153,7 +153,7 @@ void Analysis::printInstructionRowCsv(const size_t PointId,
}

Analysis::Analysis(const LLVMState &State,
const InstructionBenchmarkClustering &Clustering,
const BenchmarkClustering &Clustering,
double AnalysisInconsistencyEpsilon,
bool AnalysisDisplayUnstableOpcodes)
: Clustering_(Clustering), State_(State),
Expand Down Expand Up @@ -215,7 +215,7 @@ Analysis::makePointsPerSchedClass() const {
std::unordered_map<unsigned, size_t> SchedClassIdToIndex;
const auto &Points = Clustering_.getPoints();
for (size_t PointId = 0, E = Points.size(); PointId < E; ++PointId) {
const InstructionBenchmark &Point = Points[PointId];
const Benchmark &Point = Points[PointId];
if (!Point.Error.empty())
continue;
assert(!Point.Key.Instructions.empty());
Expand Down Expand Up @@ -270,17 +270,17 @@ static void writeLatencySnippetHtml(raw_ostream &OS,
}
}

void Analysis::printPointHtml(const InstructionBenchmark &Point,
void Analysis::printPointHtml(const Benchmark &Point,
llvm::raw_ostream &OS) const {
OS << "<li><span class=\"mono\" title=\"";
writeSnippet<EscapeTag, kEscapeHtmlString>(OS, Point.AssembledSnippet, "\n");
OS << "\">";
switch (Point.Mode) {
case InstructionBenchmark::Latency:
case Benchmark::Latency:
writeLatencySnippetHtml(OS, Point.Key.Instructions, State_.getInstrInfo());
break;
case InstructionBenchmark::Uops:
case InstructionBenchmark::InverseThroughput:
case Benchmark::Uops:
case Benchmark::InverseThroughput:
writeParallelSnippetHtml(OS, Point.Key.Instructions, State_.getInstrInfo());
break;
default:
Expand Down Expand Up @@ -334,7 +334,7 @@ void Analysis::printSchedClassClustersHtml(
}

void Analysis::SchedClassCluster::addPoint(
size_t PointId, const InstructionBenchmarkClustering &Clustering) {
size_t PointId, const BenchmarkClustering &Clustering) {
PointIds.push_back(PointId);
const auto &Point = Clustering.getPoints()[PointId];
if (ClusterId.isUndef())
Expand All @@ -346,10 +346,10 @@ void Analysis::SchedClassCluster::addPoint(

bool Analysis::SchedClassCluster::measurementsMatch(
const MCSubtargetInfo &STI, const ResolvedSchedClass &RSC,
const InstructionBenchmarkClustering &Clustering,
const BenchmarkClustering &Clustering,
const double AnalysisInconsistencyEpsilonSquared_) const {
assert(!Clustering.getPoints().empty());
const InstructionBenchmark::ModeE Mode = Clustering.getPoints()[0].Mode;
const Benchmark::ModeE Mode = Clustering.getPoints()[0].Mode;

if (!Centroid.validate(Mode))
return false;
Expand Down Expand Up @@ -427,7 +427,7 @@ void Analysis::printSchedClassDescHtml(const ResolvedSchedClass &RSC,
}

void Analysis::printClusterRawHtml(
const InstructionBenchmarkClustering::ClusterId &Id, StringRef display_name,
const BenchmarkClustering::ClusterId &Id, StringRef display_name,
llvm::raw_ostream &OS) const {
const auto &Points = Clustering_.getPoints();
const auto &Cluster = Clustering_.getCluster(Id);
Expand Down Expand Up @@ -589,7 +589,7 @@ Error Analysis::run<Analysis::PrintSchedClassInconsistencies>(
OS << "</div>";
}

printClusterRawHtml(InstructionBenchmarkClustering::ClusterId::noise(),
printClusterRawHtml(BenchmarkClustering::ClusterId::noise(),
"[noise]", OS);

OS << "</body></html>";
Expand Down
18 changes: 9 additions & 9 deletions llvm/tools/llvm-exegesis/lib/Analysis.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ namespace exegesis {
class Analysis {
public:
Analysis(const LLVMState &State,
const InstructionBenchmarkClustering &Clustering,
const BenchmarkClustering &Clustering,
double AnalysisInconsistencyEpsilon,
bool AnalysisDisplayUnstableOpcodes);

Expand All @@ -49,42 +49,42 @@ class Analysis {
template <typename Pass> Error run(raw_ostream &OS) const;

private:
using ClusterId = InstructionBenchmarkClustering::ClusterId;
using ClusterId = BenchmarkClustering::ClusterId;

// Represents the intersection of a sched class and a cluster.
class SchedClassCluster {
public:
const InstructionBenchmarkClustering::ClusterId &id() const {
const BenchmarkClustering::ClusterId &id() const {
return ClusterId;
}

const std::vector<size_t> &getPointIds() const { return PointIds; }

void addPoint(size_t PointId,
const InstructionBenchmarkClustering &Clustering);
const BenchmarkClustering &Clustering);

// Return the cluster centroid.
const SchedClassClusterCentroid &getCentroid() const { return Centroid; }

// Returns true if the cluster representative measurements match that of SC.
bool
measurementsMatch(const MCSubtargetInfo &STI, const ResolvedSchedClass &SC,
const InstructionBenchmarkClustering &Clustering,
const BenchmarkClustering &Clustering,
const double AnalysisInconsistencyEpsilonSquared_) const;

private:
InstructionBenchmarkClustering::ClusterId ClusterId;
BenchmarkClustering::ClusterId ClusterId;
std::vector<size_t> PointIds;
// Measurement stats for the points in the SchedClassCluster.
SchedClassClusterCentroid Centroid;
};

void printInstructionRowCsv(size_t PointId, raw_ostream &OS) const;

void printClusterRawHtml(const InstructionBenchmarkClustering::ClusterId &Id,
void printClusterRawHtml(const BenchmarkClustering::ClusterId &Id,
StringRef display_name, llvm::raw_ostream &OS) const;

void printPointHtml(const InstructionBenchmark &Point,
void printPointHtml(const Benchmark &Point,
llvm::raw_ostream &OS) const;

void
Expand All @@ -110,7 +110,7 @@ class Analysis {
void writeSnippet(raw_ostream &OS, ArrayRef<uint8_t> Bytes,
const char *Separator) const;

const InstructionBenchmarkClustering &Clustering_;
const BenchmarkClustering &Clustering_;
const LLVMState &State_;
std::unique_ptr<MCContext> Context_;
std::unique_ptr<MCAsmInfo> AsmInfo_;
Expand Down
2 changes: 1 addition & 1 deletion llvm/tools/llvm-exegesis/lib/BenchmarkCode.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace exegesis {

// A collection of instructions that are to be assembled, executed and measured.
struct BenchmarkCode {
InstructionBenchmarkKey Key;
BenchmarkKey Key;

// We also need to provide the registers that are live on entry for the
// assembler to generate proper prologue/epilogue.
Expand Down
44 changes: 22 additions & 22 deletions llvm/tools/llvm-exegesis/lib/BenchmarkResult.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,14 +209,14 @@ template <> struct MappingTraits<exegesis::BenchmarkMeasure> {
};

template <>
struct ScalarEnumerationTraits<exegesis::InstructionBenchmark::ModeE> {
struct ScalarEnumerationTraits<exegesis::Benchmark::ModeE> {
static void enumeration(IO &Io,
exegesis::InstructionBenchmark::ModeE &Value) {
Io.enumCase(Value, "", exegesis::InstructionBenchmark::Unknown);
Io.enumCase(Value, "latency", exegesis::InstructionBenchmark::Latency);
Io.enumCase(Value, "uops", exegesis::InstructionBenchmark::Uops);
exegesis::Benchmark::ModeE &Value) {
Io.enumCase(Value, "", exegesis::Benchmark::Unknown);
Io.enumCase(Value, "latency", exegesis::Benchmark::Latency);
Io.enumCase(Value, "uops", exegesis::Benchmark::Uops);
Io.enumCase(Value, "inverse_throughput",
exegesis::InstructionBenchmark::InverseThroughput);
exegesis::Benchmark::InverseThroughput);
}
};

Expand Down Expand Up @@ -260,8 +260,8 @@ template <> struct ScalarTraits<exegesis::RegisterValue> {
};

template <>
struct MappingContextTraits<exegesis::InstructionBenchmarkKey, YamlContext> {
static void mapping(IO &Io, exegesis::InstructionBenchmarkKey &Obj,
struct MappingContextTraits<exegesis::BenchmarkKey, YamlContext> {
static void mapping(IO &Io, exegesis::BenchmarkKey &Obj,
YamlContext &Context) {
Io.setContext(&Context);
Io.mapRequired("instructions", Obj.Instructions);
Expand All @@ -271,7 +271,7 @@ struct MappingContextTraits<exegesis::InstructionBenchmarkKey, YamlContext> {
};

template <>
struct MappingContextTraits<exegesis::InstructionBenchmark, YamlContext> {
struct MappingContextTraits<exegesis::Benchmark, YamlContext> {
struct NormalizedBinary {
NormalizedBinary(IO &io) {}
NormalizedBinary(IO &, std::vector<uint8_t> &Data) : Binary(Data) {}
Expand All @@ -288,7 +288,7 @@ struct MappingContextTraits<exegesis::InstructionBenchmark, YamlContext> {
BinaryRef Binary;
};

static void mapping(IO &Io, exegesis::InstructionBenchmark &Obj,
static void mapping(IO &Io, exegesis::Benchmark &Obj,
YamlContext &Context) {
Io.mapRequired("mode", Obj.Mode);
Io.mapRequired("key", Obj.Key, Context);
Expand All @@ -305,9 +305,9 @@ struct MappingContextTraits<exegesis::InstructionBenchmark, YamlContext> {
}
};

template <> struct MappingTraits<exegesis::InstructionBenchmark::TripleAndCpu> {
template <> struct MappingTraits<exegesis::Benchmark::TripleAndCpu> {
static void mapping(IO &Io,
exegesis::InstructionBenchmark::TripleAndCpu &Obj) {
exegesis::Benchmark::TripleAndCpu &Obj) {
assert(!Io.outputting() && "can only read TripleAndCpu");
// Read triple.
Io.mapRequired("llvm_triple", Obj.LLVMTriple);
Expand All @@ -320,8 +320,8 @@ template <> struct MappingTraits<exegesis::InstructionBenchmark::TripleAndCpu> {

namespace exegesis {

Expected<std::set<InstructionBenchmark::TripleAndCpu>>
InstructionBenchmark::readTriplesAndCpusFromYamls(MemoryBufferRef Buffer) {
Expected<std::set<Benchmark::TripleAndCpu>>
Benchmark::readTriplesAndCpusFromYamls(MemoryBufferRef Buffer) {
// We're only mapping a field, drop other fields and silence the corresponding
// warnings.
yaml::Input Yin(
Expand All @@ -340,24 +340,24 @@ InstructionBenchmark::readTriplesAndCpusFromYamls(MemoryBufferRef Buffer) {
return Result;
}

Expected<InstructionBenchmark>
InstructionBenchmark::readYaml(const LLVMState &State, MemoryBufferRef Buffer) {
Expected<Benchmark>
Benchmark::readYaml(const LLVMState &State, MemoryBufferRef Buffer) {
yaml::Input Yin(Buffer);
YamlContext Context(State);
InstructionBenchmark Benchmark;
Benchmark Benchmark;
if (Yin.setCurrentDocument())
yaml::yamlize(Yin, Benchmark, /*unused*/ true, Context);
if (!Context.getLastError().empty())
return make_error<Failure>(Context.getLastError());
return std::move(Benchmark);
}

Expected<std::vector<InstructionBenchmark>>
InstructionBenchmark::readYamls(const LLVMState &State,
Expected<std::vector<Benchmark>>
Benchmark::readYamls(const LLVMState &State,
MemoryBufferRef Buffer) {
yaml::Input Yin(Buffer);
YamlContext Context(State);
std::vector<InstructionBenchmark> Benchmarks;
std::vector<Benchmark> Benchmarks;
while (Yin.setCurrentDocument()) {
Benchmarks.emplace_back();
yamlize(Yin, Benchmarks.back(), /*unused*/ true, Context);
Expand All @@ -370,7 +370,7 @@ InstructionBenchmark::readYamls(const LLVMState &State,
return std::move(Benchmarks);
}

Error InstructionBenchmark::writeYamlTo(const LLVMState &State,
Error Benchmark::writeYamlTo(const LLVMState &State,
raw_ostream &OS) {
auto Cleanup = make_scope_exit([&] { OS.flush(); });
yaml::Output Yout(OS, nullptr /*Ctx*/, 200 /*WrapColumn*/);
Expand All @@ -383,7 +383,7 @@ Error InstructionBenchmark::writeYamlTo(const LLVMState &State,
return Error::success();
}

Error InstructionBenchmark::readYamlFrom(const LLVMState &State,
Error Benchmark::readYamlFrom(const LLVMState &State,
StringRef InputContent) {
yaml::Input Yin(InputContent);
YamlContext Context(State);
Expand Down
22 changes: 11 additions & 11 deletions llvm/tools/llvm-exegesis/lib/BenchmarkResult.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ enum class BenchmarkPhaseSelectorE {
Measure,
};

enum class InstructionBenchmarkFilter { All, RegOnly, WithMem };
enum class BenchmarkFilter { All, RegOnly, WithMem };

struct InstructionBenchmarkKey {
struct BenchmarkKey {
// The LLVM opcode name.
std::vector<MCInst> Instructions;
// The initial values of the registers.
Expand All @@ -68,8 +68,8 @@ struct BenchmarkMeasure {
};

// The result of an instruction benchmark.
struct InstructionBenchmark {
InstructionBenchmarkKey Key;
struct Benchmark {
BenchmarkKey Key;
enum ModeE { Unknown, Latency, Uops, InverseThroughput };
ModeE Mode;
std::string CpuName;
Expand All @@ -88,18 +88,18 @@ struct InstructionBenchmark {
// How to aggregate measurements.
enum ResultAggregationModeE { Min, Max, Mean, MinVariance };

InstructionBenchmark() = default;
InstructionBenchmark(InstructionBenchmark &&) = default;
Benchmark() = default;
Benchmark(Benchmark &&) = default;

InstructionBenchmark(const InstructionBenchmark &) = delete;
InstructionBenchmark &operator=(const InstructionBenchmark &) = delete;
InstructionBenchmark &operator=(InstructionBenchmark &&) = delete;
Benchmark(const Benchmark &) = delete;
Benchmark &operator=(const Benchmark &) = delete;
Benchmark &operator=(Benchmark &&) = delete;

// Read functions.
static Expected<InstructionBenchmark> readYaml(const LLVMState &State,
static Expected<Benchmark> readYaml(const LLVMState &State,
MemoryBufferRef Buffer);

static Expected<std::vector<InstructionBenchmark>>
static Expected<std::vector<Benchmark>>
readYamls(const LLVMState &State, MemoryBufferRef Buffer);

// Given a set of serialized instruction benchmarks, returns the set of
Expand Down
8 changes: 4 additions & 4 deletions llvm/tools/llvm-exegesis/lib/BenchmarkRunner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace llvm {
namespace exegesis {

BenchmarkRunner::BenchmarkRunner(const LLVMState &State,
InstructionBenchmark::ModeE Mode,
Benchmark::ModeE Mode,
BenchmarkPhaseSelectorE BenchmarkPhaseSelector)
: State(State), Mode(Mode), BenchmarkPhaseSelector(BenchmarkPhaseSelector),
Scratch(std::make_unique<ScratchSpace>()) {}
Expand Down Expand Up @@ -155,7 +155,7 @@ BenchmarkRunner::getRunnableConfiguration(
const SnippetRepetitor &Repetitor) const {
RunnableConfiguration RC;

InstructionBenchmark &InstrBenchmark = RC.InstrBenchmark;
Benchmark &InstrBenchmark = RC.InstrBenchmark;
InstrBenchmark.Mode = Mode;
InstrBenchmark.CpuName = std::string(State.getTargetMachine().getTargetCPU());
InstrBenchmark.LLVMTriple =
Expand Down Expand Up @@ -196,10 +196,10 @@ BenchmarkRunner::getRunnableConfiguration(
return std::move(RC);
}

Expected<InstructionBenchmark>
Expected<Benchmark>
BenchmarkRunner::runConfiguration(RunnableConfiguration &&RC,
bool DumpObjectToDisk) const {
InstructionBenchmark &InstrBenchmark = RC.InstrBenchmark;
Benchmark &InstrBenchmark = RC.InstrBenchmark;
object::OwningBinary<object::ObjectFile> &ObjectFile = RC.ObjectFile;

if (DumpObjectToDisk &&
Expand Down

0 comments on commit 389bf5d

Please sign in to comment.