2018-06-19 19:28:59 +08:00
|
|
|
//===-- Target.cpp ----------------------------------------------*- C++ -*-===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08: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 19:28:59 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "Target.h"
|
|
|
|
|
2018-06-26 16:49:30 +08:00
|
|
|
#include "Latency.h"
|
|
|
|
#include "Uops.h"
|
|
|
|
|
2018-10-23 01:10:47 +08:00
|
|
|
namespace llvm {
|
2018-06-19 19:28:59 +08:00
|
|
|
namespace exegesis {
|
|
|
|
|
2018-06-25 19:22:23 +08:00
|
|
|
ExegesisTarget::~ExegesisTarget() {} // anchor.
|
2018-06-19 19:28:59 +08:00
|
|
|
|
2018-06-25 19:22:23 +08:00
|
|
|
static ExegesisTarget *FirstTarget = nullptr;
|
2018-06-19 19:28:59 +08:00
|
|
|
|
2018-06-20 19:54:35 +08:00
|
|
|
const ExegesisTarget *ExegesisTarget::lookup(llvm::Triple TT) {
|
2018-06-25 19:22:23 +08:00
|
|
|
for (const ExegesisTarget *T = FirstTarget; T != nullptr; T = T->Next) {
|
2018-06-20 19:54:35 +08:00
|
|
|
if (T->matchesArch(TT.getArch()))
|
|
|
|
return T;
|
2018-06-19 19:28:59 +08:00
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-06-25 19:22:23 +08:00
|
|
|
void ExegesisTarget::registerTarget(ExegesisTarget *Target) {
|
2018-06-19 19:28:59 +08:00
|
|
|
if (FirstTarget == nullptr) {
|
|
|
|
FirstTarget = Target;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (Target->Next != nullptr)
|
2018-08-01 22:41:45 +08:00
|
|
|
return; // Already registered.
|
2018-06-19 19:28:59 +08:00
|
|
|
Target->Next = FirstTarget;
|
|
|
|
FirstTarget = Target;
|
|
|
|
}
|
2018-06-26 16:49:30 +08:00
|
|
|
|
2018-09-13 15:40:53 +08:00
|
|
|
std::unique_ptr<SnippetGenerator>
|
|
|
|
ExegesisTarget::createSnippetGenerator(InstructionBenchmark::ModeE Mode,
|
|
|
|
const LLVMState &State) const {
|
|
|
|
switch (Mode) {
|
|
|
|
case InstructionBenchmark::Unknown:
|
|
|
|
return nullptr;
|
|
|
|
case InstructionBenchmark::Latency:
|
|
|
|
return createLatencySnippetGenerator(State);
|
|
|
|
case InstructionBenchmark::Uops:
|
2019-01-31 00:02:20 +08:00
|
|
|
case InstructionBenchmark::InverseThroughput:
|
2018-09-13 15:40:53 +08:00
|
|
|
return createUopsSnippetGenerator(State);
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-06-26 16:49:30 +08:00
|
|
|
std::unique_ptr<BenchmarkRunner>
|
|
|
|
ExegesisTarget::createBenchmarkRunner(InstructionBenchmark::ModeE Mode,
|
|
|
|
const LLVMState &State) const {
|
|
|
|
switch (Mode) {
|
|
|
|
case InstructionBenchmark::Unknown:
|
|
|
|
return nullptr;
|
|
|
|
case InstructionBenchmark::Latency:
|
2019-01-31 00:02:20 +08:00
|
|
|
case InstructionBenchmark::InverseThroughput:
|
|
|
|
return createLatencyBenchmarkRunner(State, Mode);
|
2018-06-26 16:49:30 +08:00
|
|
|
case InstructionBenchmark::Uops:
|
|
|
|
return createUopsBenchmarkRunner(State);
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-11-08 20:09:45 +08:00
|
|
|
std::unique_ptr<SnippetGenerator>
|
|
|
|
ExegesisTarget::createLatencySnippetGenerator(const LLVMState &State) const {
|
2019-08-15 23:54:37 +08:00
|
|
|
return std::make_unique<LatencySnippetGenerator>(State);
|
2018-11-08 20:09:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<SnippetGenerator>
|
|
|
|
ExegesisTarget::createUopsSnippetGenerator(const LLVMState &State) const {
|
2019-08-15 23:54:37 +08:00
|
|
|
return std::make_unique<UopsSnippetGenerator>(State);
|
2018-11-08 20:09:45 +08:00
|
|
|
}
|
|
|
|
|
2019-01-31 00:02:20 +08:00
|
|
|
std::unique_ptr<BenchmarkRunner> ExegesisTarget::createLatencyBenchmarkRunner(
|
|
|
|
const LLVMState &State, InstructionBenchmark::ModeE Mode) const {
|
2019-08-15 23:54:37 +08:00
|
|
|
return std::make_unique<LatencyBenchmarkRunner>(State, Mode);
|
2018-11-08 20:09:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<BenchmarkRunner>
|
|
|
|
ExegesisTarget::createUopsBenchmarkRunner(const LLVMState &State) const {
|
2019-08-15 23:54:37 +08:00
|
|
|
return std::make_unique<UopsBenchmarkRunner>(State);
|
2018-11-08 20:09:45 +08:00
|
|
|
}
|
|
|
|
|
2019-04-06 22:16:26 +08:00
|
|
|
void ExegesisTarget::randomizeMCOperand(
|
|
|
|
const Instruction &Instr, const Variable &Var,
|
|
|
|
llvm::MCOperand &AssignedValue,
|
|
|
|
const llvm::BitVector &ForbiddenRegs) const {
|
|
|
|
const Operand &Op = Instr.getPrimaryOperand(Var);
|
|
|
|
switch (Op.getExplicitOperandInfo().OperandType) {
|
|
|
|
case llvm::MCOI::OperandType::OPERAND_IMMEDIATE:
|
|
|
|
// FIXME: explore immediate values too.
|
|
|
|
AssignedValue = llvm::MCOperand::createImm(1);
|
|
|
|
break;
|
|
|
|
case llvm::MCOI::OperandType::OPERAND_REGISTER: {
|
|
|
|
assert(Op.isReg());
|
|
|
|
auto AllowedRegs = Op.getRegisterAliasing().sourceBits();
|
|
|
|
assert(AllowedRegs.size() == ForbiddenRegs.size());
|
|
|
|
for (auto I : ForbiddenRegs.set_bits())
|
|
|
|
AllowedRegs.reset(I);
|
|
|
|
AssignedValue = llvm::MCOperand::createReg(randomBit(AllowedRegs));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-25 15:44:01 +08:00
|
|
|
static_assert(std::is_pod<PfmCountersInfo>::value,
|
|
|
|
"We shouldn't have dynamic initialization here");
|
2018-11-09 21:15:32 +08:00
|
|
|
const PfmCountersInfo PfmCountersInfo::Default = {nullptr, nullptr, nullptr,
|
|
|
|
0u};
|
2018-10-25 15:44:01 +08:00
|
|
|
|
|
|
|
const PfmCountersInfo &
|
|
|
|
ExegesisTarget::getPfmCounters(llvm::StringRef CpuName) const {
|
|
|
|
assert(std::is_sorted(
|
|
|
|
CpuPfmCounters.begin(), CpuPfmCounters.end(),
|
|
|
|
[](const CpuAndPfmCounters &LHS, const CpuAndPfmCounters &RHS) {
|
|
|
|
return strcmp(LHS.CpuName, RHS.CpuName) < 0;
|
|
|
|
}) &&
|
|
|
|
"CpuPfmCounters table is not sorted");
|
|
|
|
|
|
|
|
// Find entry
|
|
|
|
auto Found =
|
|
|
|
std::lower_bound(CpuPfmCounters.begin(), CpuPfmCounters.end(), CpuName);
|
|
|
|
if (Found == CpuPfmCounters.end() ||
|
|
|
|
llvm::StringRef(Found->CpuName) != CpuName) {
|
2018-11-09 21:15:32 +08:00
|
|
|
// Use the default.
|
|
|
|
if (CpuPfmCounters.begin() != CpuPfmCounters.end() &&
|
|
|
|
CpuPfmCounters.begin()->CpuName[0] == '\0') {
|
|
|
|
Found = CpuPfmCounters.begin(); // The target specifies a default.
|
|
|
|
} else {
|
|
|
|
return PfmCountersInfo::Default; // No default for the target.
|
|
|
|
}
|
2018-10-25 15:44:01 +08:00
|
|
|
}
|
|
|
|
assert(Found->PCI && "Missing counters");
|
|
|
|
return *Found->PCI;
|
|
|
|
}
|
|
|
|
|
2018-06-26 16:49:30 +08:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
// Default implementation.
|
|
|
|
class ExegesisDefaultTarget : public ExegesisTarget {
|
2018-10-25 15:44:01 +08:00
|
|
|
public:
|
|
|
|
ExegesisDefaultTarget() : ExegesisTarget({}) {}
|
|
|
|
|
2018-06-26 16:49:30 +08:00
|
|
|
private:
|
2018-09-18 19:26:27 +08:00
|
|
|
std::vector<llvm::MCInst> setRegTo(const llvm::MCSubtargetInfo &STI,
|
2018-09-20 20:22:18 +08:00
|
|
|
unsigned Reg,
|
|
|
|
const llvm::APInt &Value) const override {
|
2018-09-18 19:26:27 +08:00
|
|
|
llvm_unreachable("Not yet implemented");
|
|
|
|
}
|
|
|
|
|
2018-06-26 16:49:30 +08:00
|
|
|
bool matchesArch(llvm::Triple::ArchType Arch) const override {
|
|
|
|
llvm_unreachable("never called");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
const ExegesisTarget &ExegesisTarget::getDefault() {
|
|
|
|
static ExegesisDefaultTarget Target;
|
|
|
|
return Target;
|
|
|
|
}
|
|
|
|
|
2018-06-25 19:22:23 +08:00
|
|
|
} // namespace exegesis
|
2018-10-23 01:10:47 +08:00
|
|
|
} // namespace llvm
|