2018-06-19 11:28:59 +00:00
|
|
|
//===-- Target.cpp ----------------------------------------------*- C++ -*-===//
|
|
|
|
//
|
2019-01-19 08:50:56 +00:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2018-06-19 11:28:59 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "Target.h"
|
|
|
|
|
2020-01-17 14:28:54 +01:00
|
|
|
#include "LatencyBenchmarkRunner.h"
|
|
|
|
#include "ParallelSnippetGenerator.h"
|
2023-04-06 13:08:48 +03:00
|
|
|
#include "PerfHelper.h"
|
2020-01-17 14:28:54 +01:00
|
|
|
#include "SerialSnippetGenerator.h"
|
|
|
|
#include "UopsBenchmarkRunner.h"
|
2020-05-27 18:06:40 -04:00
|
|
|
#include "llvm/ADT/Twine.h"
|
|
|
|
#include "llvm/Support/Error.h"
|
2023-08-02 15:41:46 +03:00
|
|
|
#include "llvm/TargetParser/SubtargetFeature.h"
|
2018-06-26 08:49:30 +00:00
|
|
|
|
2018-10-22 17:10:47 +00:00
|
|
|
namespace llvm {
|
2018-06-19 11:28:59 +00:00
|
|
|
namespace exegesis {
|
|
|
|
|
2022-12-21 03:38:10 +03:00
|
|
|
cl::OptionCategory Options("llvm-exegesis options");
|
|
|
|
cl::OptionCategory BenchmarkOptions("llvm-exegesis benchmark options");
|
|
|
|
cl::OptionCategory AnalysisOptions("llvm-exegesis analysis options");
|
|
|
|
|
2018-06-25 11:22:23 +00:00
|
|
|
ExegesisTarget::~ExegesisTarget() {} // anchor.
|
2018-06-19 11:28:59 +00:00
|
|
|
|
2018-06-25 11:22:23 +00:00
|
|
|
static ExegesisTarget *FirstTarget = nullptr;
|
2018-06-19 11:28:59 +00:00
|
|
|
|
2019-10-09 11:58:42 +00:00
|
|
|
const ExegesisTarget *ExegesisTarget::lookup(Triple TT) {
|
2018-06-25 11:22:23 +00:00
|
|
|
for (const ExegesisTarget *T = FirstTarget; T != nullptr; T = T->Next) {
|
2018-06-20 11:54:35 +00:00
|
|
|
if (T->matchesArch(TT.getArch()))
|
|
|
|
return T;
|
2018-06-19 11:28:59 +00:00
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2024-01-16 01:24:22 -08:00
|
|
|
Expected<std::unique_ptr<pfm::CounterGroup>>
|
2023-06-25 23:46:22 +00:00
|
|
|
ExegesisTarget::createCounter(StringRef CounterName, const LLVMState &,
|
2024-01-19 02:00:33 -08:00
|
|
|
ArrayRef<const char *> ValidationCounters,
|
2023-06-25 23:46:22 +00:00
|
|
|
const pid_t ProcessID) const {
|
2020-05-27 18:06:40 -04:00
|
|
|
pfm::PerfEvent Event(CounterName);
|
|
|
|
if (!Event.valid())
|
2024-01-29 11:40:06 -08:00
|
|
|
return make_error<Failure>(Twine("Unable to create counter with name '")
|
|
|
|
.concat(CounterName)
|
|
|
|
.concat("'"));
|
2020-05-27 18:06:40 -04:00
|
|
|
|
2024-01-19 02:00:33 -08:00
|
|
|
std::vector<pfm::PerfEvent> ValidationEvents;
|
|
|
|
for (const char *ValCounterName : ValidationCounters) {
|
|
|
|
ValidationEvents.emplace_back(ValCounterName);
|
|
|
|
if (!ValidationEvents.back().valid())
|
2024-01-29 11:40:06 -08:00
|
|
|
return make_error<Failure>(
|
|
|
|
Twine("Unable to create validation counter with name '")
|
2024-01-19 02:00:33 -08:00
|
|
|
.concat(ValCounterName)
|
|
|
|
.concat("'"));
|
|
|
|
}
|
|
|
|
|
|
|
|
return std::make_unique<pfm::CounterGroup>(
|
|
|
|
std::move(Event), std::move(ValidationEvents), ProcessID);
|
2020-05-27 18:06:40 -04:00
|
|
|
}
|
|
|
|
|
2018-06-25 11:22:23 +00:00
|
|
|
void ExegesisTarget::registerTarget(ExegesisTarget *Target) {
|
2018-06-19 11:28:59 +00:00
|
|
|
if (FirstTarget == nullptr) {
|
|
|
|
FirstTarget = Target;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (Target->Next != nullptr)
|
2018-08-01 14:41:45 +00:00
|
|
|
return; // Already registered.
|
2018-06-19 11:28:59 +00:00
|
|
|
Target->Next = FirstTarget;
|
|
|
|
FirstTarget = Target;
|
|
|
|
}
|
2018-06-26 08:49:30 +00:00
|
|
|
|
2019-10-08 14:30:24 +00:00
|
|
|
std::unique_ptr<SnippetGenerator> ExegesisTarget::createSnippetGenerator(
|
2023-03-27 08:14:18 +00:00
|
|
|
Benchmark::ModeE Mode, const LLVMState &State,
|
2019-10-08 14:30:24 +00:00
|
|
|
const SnippetGenerator::Options &Opts) const {
|
2018-09-13 07:40:53 +00:00
|
|
|
switch (Mode) {
|
2023-03-27 08:14:18 +00:00
|
|
|
case Benchmark::Unknown:
|
2018-09-13 07:40:53 +00:00
|
|
|
return nullptr;
|
2023-03-27 08:14:18 +00:00
|
|
|
case Benchmark::Latency:
|
2020-01-17 14:28:54 +01:00
|
|
|
return createSerialSnippetGenerator(State, Opts);
|
2023-03-27 08:14:18 +00:00
|
|
|
case Benchmark::Uops:
|
|
|
|
case Benchmark::InverseThroughput:
|
2020-01-17 14:28:54 +01:00
|
|
|
return createParallelSnippetGenerator(State, Opts);
|
2018-09-13 07:40:53 +00:00
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2020-02-06 18:21:01 +01:00
|
|
|
Expected<std::unique_ptr<BenchmarkRunner>>
|
2020-06-25 11:15:16 -04:00
|
|
|
ExegesisTarget::createBenchmarkRunner(
|
2023-03-27 08:14:18 +00:00
|
|
|
Benchmark::ModeE Mode, const LLVMState &State,
|
2023-01-05 17:38:35 +03:00
|
|
|
BenchmarkPhaseSelectorE BenchmarkPhaseSelector,
|
2023-06-21 00:04:48 +00:00
|
|
|
BenchmarkRunner::ExecutionModeE ExecutionMode,
|
2024-01-19 02:00:33 -08:00
|
|
|
unsigned BenchmarkRepeatCount, ArrayRef<ValidationEvent> ValidationCounters,
|
2023-03-27 08:14:18 +00:00
|
|
|
Benchmark::ResultAggregationModeE ResultAggMode) const {
|
2019-12-31 14:14:41 +01:00
|
|
|
PfmCountersInfo PfmCounters = State.getPfmCounters();
|
2018-06-26 08:49:30 +00:00
|
|
|
switch (Mode) {
|
2023-03-27 08:14:18 +00:00
|
|
|
case Benchmark::Unknown:
|
2018-06-26 08:49:30 +00:00
|
|
|
return nullptr;
|
2023-03-27 08:14:18 +00:00
|
|
|
case Benchmark::Latency:
|
|
|
|
case Benchmark::InverseThroughput:
|
2023-01-05 17:38:35 +03:00
|
|
|
if (BenchmarkPhaseSelector == BenchmarkPhaseSelectorE::Measure &&
|
|
|
|
!PfmCounters.CycleCounter) {
|
2023-03-27 08:14:18 +00:00
|
|
|
const char *ModeName = Mode == Benchmark::Latency
|
2019-12-31 14:14:41 +01:00
|
|
|
? "latency"
|
|
|
|
: "inverse_throughput";
|
2020-02-06 18:21:01 +01:00
|
|
|
return make_error<Failure>(
|
|
|
|
Twine("can't run '")
|
|
|
|
.concat(ModeName)
|
2022-12-07 20:14:13 +03:00
|
|
|
.concat(
|
|
|
|
"' mode, sched model does not define a cycle counter. You "
|
2023-04-06 13:08:48 +03:00
|
|
|
"can pass --benchmark-phase=... to skip the actual "
|
|
|
|
"benchmarking or --use-dummy-perf-counters to not query "
|
|
|
|
"the kernel for real event counts."));
|
2019-12-31 14:14:41 +01:00
|
|
|
}
|
2024-01-19 02:00:33 -08:00
|
|
|
return createLatencyBenchmarkRunner(
|
|
|
|
State, Mode, BenchmarkPhaseSelector, ResultAggMode, ExecutionMode,
|
|
|
|
ValidationCounters, BenchmarkRepeatCount);
|
2023-03-27 08:14:18 +00:00
|
|
|
case Benchmark::Uops:
|
2023-01-05 17:38:35 +03:00
|
|
|
if (BenchmarkPhaseSelector == BenchmarkPhaseSelectorE::Measure &&
|
|
|
|
!PfmCounters.UopsCounter && !PfmCounters.IssueCounters)
|
2022-12-07 20:14:13 +03:00
|
|
|
return make_error<Failure>(
|
|
|
|
"can't run 'uops' mode, sched model does not define uops or issue "
|
2023-04-06 13:08:48 +03:00
|
|
|
"counters. You can pass --benchmark-phase=... to skip the actual "
|
|
|
|
"benchmarking or --use-dummy-perf-counters to not query the kernel "
|
|
|
|
"for real event counts.");
|
2023-01-05 17:38:35 +03:00
|
|
|
return createUopsBenchmarkRunner(State, BenchmarkPhaseSelector,
|
2024-01-19 02:00:33 -08:00
|
|
|
ResultAggMode, ExecutionMode,
|
|
|
|
ValidationCounters);
|
2018-06-26 08:49:30 +00:00
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2020-01-17 14:28:54 +01:00
|
|
|
std::unique_ptr<SnippetGenerator> ExegesisTarget::createSerialSnippetGenerator(
|
2019-10-08 14:30:24 +00:00
|
|
|
const LLVMState &State, const SnippetGenerator::Options &Opts) const {
|
2020-01-17 14:28:54 +01:00
|
|
|
return std::make_unique<SerialSnippetGenerator>(State, Opts);
|
2018-11-08 12:09:45 +00:00
|
|
|
}
|
|
|
|
|
2020-01-17 14:28:54 +01:00
|
|
|
std::unique_ptr<SnippetGenerator> ExegesisTarget::createParallelSnippetGenerator(
|
2019-10-08 14:30:24 +00:00
|
|
|
const LLVMState &State, const SnippetGenerator::Options &Opts) const {
|
2020-01-17 14:28:54 +01:00
|
|
|
return std::make_unique<ParallelSnippetGenerator>(State, Opts);
|
2018-11-08 12:09:45 +00:00
|
|
|
}
|
|
|
|
|
2019-01-30 16:02:20 +00:00
|
|
|
std::unique_ptr<BenchmarkRunner> ExegesisTarget::createLatencyBenchmarkRunner(
|
2023-03-27 08:14:18 +00:00
|
|
|
const LLVMState &State, Benchmark::ModeE Mode,
|
2023-01-05 17:38:35 +03:00
|
|
|
BenchmarkPhaseSelectorE BenchmarkPhaseSelector,
|
2023-06-21 00:04:48 +00:00
|
|
|
Benchmark::ResultAggregationModeE ResultAggMode,
|
2023-12-07 00:32:33 -08:00
|
|
|
BenchmarkRunner::ExecutionModeE ExecutionMode,
|
2024-01-19 02:00:33 -08:00
|
|
|
ArrayRef<ValidationEvent> ValidationCounters,
|
2023-12-07 00:32:33 -08:00
|
|
|
unsigned BenchmarkRepeatCount) const {
|
2022-12-07 20:14:13 +03:00
|
|
|
return std::make_unique<LatencyBenchmarkRunner>(
|
2023-12-07 00:32:33 -08:00
|
|
|
State, Mode, BenchmarkPhaseSelector, ResultAggMode, ExecutionMode,
|
2024-01-19 02:00:33 -08:00
|
|
|
ValidationCounters, BenchmarkRepeatCount);
|
2018-11-08 12:09:45 +00:00
|
|
|
}
|
|
|
|
|
2020-06-25 11:15:16 -04:00
|
|
|
std::unique_ptr<BenchmarkRunner> ExegesisTarget::createUopsBenchmarkRunner(
|
2023-01-05 17:38:35 +03:00
|
|
|
const LLVMState &State, BenchmarkPhaseSelectorE BenchmarkPhaseSelector,
|
2023-06-21 00:04:48 +00:00
|
|
|
Benchmark::ResultAggregationModeE /*unused*/,
|
2024-01-19 02:00:33 -08:00
|
|
|
BenchmarkRunner::ExecutionModeE ExecutionMode,
|
|
|
|
ArrayRef<ValidationEvent> ValidationCounters) const {
|
|
|
|
return std::make_unique<UopsBenchmarkRunner>(
|
|
|
|
State, BenchmarkPhaseSelector, ExecutionMode, ValidationCounters);
|
2018-11-08 12:09:45 +00:00
|
|
|
}
|
|
|
|
|
2023-01-26 00:14:30 +00:00
|
|
|
static_assert(std::is_trivial_v<PfmCountersInfo>,
|
2018-10-25 07:44:01 +00:00
|
|
|
"We shouldn't have dynamic initialization here");
|
2024-01-10 15:05:58 -08:00
|
|
|
|
2018-11-09 13:15:32 +00:00
|
|
|
const PfmCountersInfo PfmCountersInfo::Default = {nullptr, nullptr, nullptr,
|
2024-01-10 15:05:58 -08:00
|
|
|
0u, nullptr, 0u};
|
2023-04-06 13:08:48 +03:00
|
|
|
const PfmCountersInfo PfmCountersInfo::Dummy = {
|
2024-01-10 15:05:58 -08:00
|
|
|
pfm::PerfEvent::DummyEventString,
|
|
|
|
pfm::PerfEvent::DummyEventString,
|
|
|
|
nullptr,
|
|
|
|
0u,
|
|
|
|
nullptr,
|
2023-04-06 13:08:48 +03:00
|
|
|
0u};
|
2018-10-25 07:44:01 +00:00
|
|
|
|
2019-10-09 11:58:42 +00:00
|
|
|
const PfmCountersInfo &ExegesisTarget::getPfmCounters(StringRef CpuName) const {
|
2024-01-29 11:40:06 -08:00
|
|
|
assert(
|
|
|
|
is_sorted(CpuPfmCounters,
|
|
|
|
[](const CpuAndPfmCounters &LHS, const CpuAndPfmCounters &RHS) {
|
|
|
|
return strcmp(LHS.CpuName, RHS.CpuName) < 0;
|
|
|
|
}) &&
|
|
|
|
"CpuPfmCounters table is not sorted");
|
2018-10-25 07:44:01 +00:00
|
|
|
|
|
|
|
// Find entry
|
2024-01-29 11:40:06 -08:00
|
|
|
auto Found = lower_bound(CpuPfmCounters, CpuName);
|
2019-10-09 11:58:42 +00:00
|
|
|
if (Found == CpuPfmCounters.end() || StringRef(Found->CpuName) != CpuName) {
|
2018-11-09 13:15:32 +00:00
|
|
|
// Use the default.
|
2021-01-16 09:40:54 -08:00
|
|
|
if (!CpuPfmCounters.empty() && CpuPfmCounters.begin()->CpuName[0] == '\0') {
|
2018-11-09 13:15:32 +00:00
|
|
|
Found = CpuPfmCounters.begin(); // The target specifies a default.
|
|
|
|
} else {
|
|
|
|
return PfmCountersInfo::Default; // No default for the target.
|
|
|
|
}
|
2018-10-25 07:44:01 +00:00
|
|
|
}
|
|
|
|
assert(Found->PCI && "Missing counters");
|
|
|
|
return *Found->PCI;
|
|
|
|
}
|
|
|
|
|
2023-04-06 13:08:48 +03:00
|
|
|
const PfmCountersInfo &ExegesisTarget::getDummyPfmCounters() const {
|
|
|
|
return PfmCountersInfo::Dummy;
|
|
|
|
}
|
|
|
|
|
2020-11-04 08:56:27 +01:00
|
|
|
ExegesisTarget::SavedState::~SavedState() {} // anchor.
|
|
|
|
|
2018-06-26 08:49:30 +00:00
|
|
|
namespace {
|
|
|
|
|
2023-08-02 15:41:46 +03:00
|
|
|
bool opcodeIsNotAvailable(unsigned, const FeatureBitset &) { return false; }
|
|
|
|
|
2018-06-26 08:49:30 +00:00
|
|
|
// Default implementation.
|
|
|
|
class ExegesisDefaultTarget : public ExegesisTarget {
|
2018-10-25 07:44:01 +00:00
|
|
|
public:
|
2023-08-02 15:41:46 +03:00
|
|
|
ExegesisDefaultTarget() : ExegesisTarget({}, opcodeIsNotAvailable) {}
|
2018-10-25 07:44:01 +00:00
|
|
|
|
2018-06-26 08:49:30 +00:00
|
|
|
private:
|
2019-10-09 11:58:42 +00:00
|
|
|
std::vector<MCInst> setRegTo(const MCSubtargetInfo &STI, unsigned Reg,
|
|
|
|
const APInt &Value) const override {
|
2018-09-18 11:26:27 +00:00
|
|
|
llvm_unreachable("Not yet implemented");
|
|
|
|
}
|
|
|
|
|
2019-10-09 11:58:42 +00:00
|
|
|
bool matchesArch(Triple::ArchType Arch) const override {
|
2018-06-26 08:49:30 +00:00
|
|
|
llvm_unreachable("never called");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
const ExegesisTarget &ExegesisTarget::getDefault() {
|
|
|
|
static ExegesisDefaultTarget Target;
|
|
|
|
return Target;
|
|
|
|
}
|
|
|
|
|
2018-06-25 11:22:23 +00:00
|
|
|
} // namespace exegesis
|
2018-10-22 17:10:47 +00:00
|
|
|
} // namespace llvm
|