2018-06-19 19:28:59 +08:00
|
|
|
//===-- Target.h ------------------------------------------------*- 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
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
///
|
|
|
|
/// \file
|
|
|
|
///
|
|
|
|
/// Classes that handle the creation of target-specific objects. This is
|
|
|
|
/// similar to llvm::Target/TargetRegistry.
|
|
|
|
///
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_TOOLS_LLVM_EXEGESIS_TARGET_H
|
|
|
|
#define LLVM_TOOLS_LLVM_EXEGESIS_TARGET_H
|
|
|
|
|
2018-06-26 16:49:30 +08:00
|
|
|
#include "BenchmarkResult.h"
|
|
|
|
#include "BenchmarkRunner.h"
|
|
|
|
#include "LlvmState.h"
|
2018-09-13 15:40:53 +08:00
|
|
|
#include "SnippetGenerator.h"
|
2018-06-19 19:28:59 +08:00
|
|
|
#include "llvm/ADT/Triple.h"
|
2018-06-20 19:54:35 +08:00
|
|
|
#include "llvm/CodeGen/TargetPassConfig.h"
|
2018-08-01 22:41:45 +08:00
|
|
|
#include "llvm/IR/CallingConv.h"
|
2018-06-20 19:54:35 +08:00
|
|
|
#include "llvm/IR/LegacyPassManager.h"
|
2018-06-25 21:12:02 +08:00
|
|
|
#include "llvm/MC/MCInst.h"
|
|
|
|
#include "llvm/MC/MCRegisterInfo.h"
|
2018-06-19 19:28:59 +08:00
|
|
|
|
2018-10-23 01:10:47 +08:00
|
|
|
namespace llvm {
|
2018-06-19 19:28:59 +08:00
|
|
|
namespace exegesis {
|
|
|
|
|
2018-10-25 15:44:01 +08:00
|
|
|
struct PfmCountersInfo {
|
|
|
|
// An optional name of a performance counter that can be used to measure
|
|
|
|
// cycles.
|
2018-10-25 18:45:38 +08:00
|
|
|
const char *CycleCounter;
|
2018-10-25 15:44:01 +08:00
|
|
|
|
|
|
|
// An optional name of a performance counter that can be used to measure
|
|
|
|
// uops.
|
2018-10-25 18:45:38 +08:00
|
|
|
const char *UopsCounter;
|
2018-10-25 15:44:01 +08:00
|
|
|
|
|
|
|
// An IssueCounter specifies how to measure uops issued to specific proc
|
|
|
|
// resources.
|
|
|
|
struct IssueCounter {
|
2018-10-25 18:45:38 +08:00
|
|
|
const char *Counter;
|
2018-10-25 15:44:01 +08:00
|
|
|
// The name of the ProcResource that this counter measures.
|
2018-10-25 18:45:38 +08:00
|
|
|
const char *ProcResName;
|
2018-10-25 15:44:01 +08:00
|
|
|
};
|
|
|
|
// An optional list of IssueCounters.
|
2018-10-25 18:45:38 +08:00
|
|
|
const IssueCounter *IssueCounters;
|
|
|
|
unsigned NumIssueCounters;
|
2018-10-25 15:44:01 +08:00
|
|
|
|
|
|
|
static const PfmCountersInfo Default;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct CpuAndPfmCounters {
|
2018-10-25 18:45:38 +08:00
|
|
|
const char *CpuName;
|
|
|
|
const PfmCountersInfo *PCI;
|
2018-10-25 15:44:01 +08:00
|
|
|
bool operator<(llvm::StringRef S) const {
|
|
|
|
return llvm::StringRef(CpuName) < S;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-06-19 19:28:59 +08:00
|
|
|
class ExegesisTarget {
|
|
|
|
public:
|
2018-10-25 15:44:01 +08:00
|
|
|
explicit ExegesisTarget(llvm::ArrayRef<CpuAndPfmCounters> CpuPfmCounters)
|
|
|
|
: CpuPfmCounters(CpuPfmCounters) {}
|
|
|
|
|
2018-06-20 19:54:35 +08:00
|
|
|
// Targets can use this to add target-specific passes in assembleToStream();
|
|
|
|
virtual void addTargetSpecificPasses(llvm::PassManagerBase &PM) const {}
|
|
|
|
|
2018-09-18 23:38:16 +08:00
|
|
|
// Generates code to move a constant into a the given register.
|
2018-09-20 20:22:18 +08:00
|
|
|
// Precondition: Value must fit into Reg.
|
2018-09-18 23:38:16 +08:00
|
|
|
virtual std::vector<llvm::MCInst>
|
2018-09-20 20:22:18 +08:00
|
|
|
setRegTo(const llvm::MCSubtargetInfo &STI, unsigned Reg,
|
|
|
|
const llvm::APInt &Value) const = 0;
|
2018-09-18 19:26:27 +08:00
|
|
|
|
|
|
|
// Returns the register pointing to scratch memory, or 0 if this target
|
|
|
|
// does not support memory operands. The benchmark function uses the
|
|
|
|
// default calling convention.
|
2018-09-20 20:22:18 +08:00
|
|
|
virtual unsigned getScratchMemoryRegister(const llvm::Triple &) const {
|
|
|
|
return 0;
|
|
|
|
}
|
2018-08-01 22:41:45 +08:00
|
|
|
|
|
|
|
// Fills memory operands with references to the address at [Reg] + Offset.
|
2018-09-27 17:23:04 +08:00
|
|
|
virtual void fillMemoryOperands(InstructionTemplate &IT, unsigned Reg,
|
2018-09-20 20:22:18 +08:00
|
|
|
unsigned Offset) const {
|
|
|
|
llvm_unreachable(
|
|
|
|
"fillMemoryOperands() requires getScratchMemoryRegister() > 0");
|
|
|
|
}
|
2018-08-01 22:41:45 +08:00
|
|
|
|
2019-09-27 20:56:24 +08:00
|
|
|
// Returns a counter usable as a loop counter.
|
|
|
|
virtual unsigned getLoopCounterRegister(const llvm::Triple &) const {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Adds the code to decrement the loop counter and
|
|
|
|
virtual void decrementLoopCounterAndLoop(MachineBasicBlock &MBB,
|
|
|
|
const llvm::MCInstrInfo &MII) const {
|
|
|
|
llvm_unreachable("decrementLoopCounterAndBranch() requires "
|
|
|
|
"getLoopCounterRegister() > 0");
|
|
|
|
}
|
|
|
|
|
2019-03-26 23:44:57 +08:00
|
|
|
// Returns a list of unavailable registers.
|
|
|
|
// Targets can use this to prevent some registers to be automatically selected
|
|
|
|
// for use in snippets.
|
|
|
|
virtual ArrayRef<unsigned> getUnavailableRegisters() const { return {}; }
|
|
|
|
|
2018-08-01 22:41:45 +08:00
|
|
|
// Returns the maximum number of bytes a load/store instruction can access at
|
|
|
|
// once. This is typically the size of the largest register available on the
|
|
|
|
// processor. Note that this only used as a hint to generate independant
|
|
|
|
// load/stores to/from memory, so the exact returned value does not really
|
|
|
|
// matter as long as it's large enough.
|
2018-09-20 20:22:18 +08:00
|
|
|
virtual unsigned getMaxMemoryAccessSize() const { return 0; }
|
2018-08-01 22:41:45 +08:00
|
|
|
|
2019-04-06 22:16:26 +08:00
|
|
|
// Assigns a random operand of the right type to variable Var.
|
|
|
|
// The default implementation only handles generic operand types.
|
|
|
|
// The target is responsible for handling any operand
|
|
|
|
// starting from OPERAND_FIRST_TARGET.
|
|
|
|
virtual void randomizeMCOperand(const Instruction &Instr, const Variable &Var,
|
|
|
|
llvm::MCOperand &AssignedValue,
|
|
|
|
const llvm::BitVector &ForbiddenRegs) const;
|
|
|
|
|
2018-09-13 15:40:53 +08:00
|
|
|
// Creates a snippet generator for the given mode.
|
|
|
|
std::unique_ptr<SnippetGenerator>
|
|
|
|
createSnippetGenerator(InstructionBenchmark::ModeE Mode,
|
|
|
|
const LLVMState &State) const;
|
2018-06-26 16:49:30 +08:00
|
|
|
// Creates a benchmark runner for the given mode.
|
|
|
|
std::unique_ptr<BenchmarkRunner>
|
|
|
|
createBenchmarkRunner(InstructionBenchmark::ModeE Mode,
|
|
|
|
const LLVMState &State) const;
|
|
|
|
|
2018-06-19 19:28:59 +08:00
|
|
|
// Returns the ExegesisTarget for the given triple or nullptr if the target
|
|
|
|
// does not exist.
|
2018-06-20 19:54:35 +08:00
|
|
|
static const ExegesisTarget *lookup(llvm::Triple TT);
|
2018-06-26 16:49:30 +08:00
|
|
|
// Returns the default (unspecialized) ExegesisTarget.
|
|
|
|
static const ExegesisTarget &getDefault();
|
2018-06-19 19:28:59 +08:00
|
|
|
// Registers a target. Not thread safe.
|
|
|
|
static void registerTarget(ExegesisTarget *T);
|
|
|
|
|
2018-06-19 19:58:10 +08:00
|
|
|
virtual ~ExegesisTarget();
|
2018-06-19 19:28:59 +08:00
|
|
|
|
2018-10-25 15:44:01 +08:00
|
|
|
// Returns the Pfm counters for the given CPU (or the default if no pfm
|
|
|
|
// counters are defined for this CPU).
|
|
|
|
const PfmCountersInfo &getPfmCounters(llvm::StringRef CpuName) const;
|
|
|
|
|
2018-06-19 19:28:59 +08:00
|
|
|
private:
|
|
|
|
virtual bool matchesArch(llvm::Triple::ArchType Arch) const = 0;
|
2018-06-26 16:49:30 +08:00
|
|
|
|
2018-09-13 15:40:53 +08:00
|
|
|
// Targets can implement their own snippet generators/benchmarks runners by
|
2018-06-26 16:49:30 +08:00
|
|
|
// implementing these.
|
2018-09-13 15:40:53 +08:00
|
|
|
std::unique_ptr<SnippetGenerator> virtual createLatencySnippetGenerator(
|
|
|
|
const LLVMState &State) const;
|
|
|
|
std::unique_ptr<SnippetGenerator> virtual createUopsSnippetGenerator(
|
|
|
|
const LLVMState &State) const;
|
2018-06-26 16:49:30 +08:00
|
|
|
std::unique_ptr<BenchmarkRunner> virtual createLatencyBenchmarkRunner(
|
2019-01-31 00:02:20 +08:00
|
|
|
const LLVMState &State, InstructionBenchmark::ModeE Mode) const;
|
2018-06-26 16:49:30 +08:00
|
|
|
std::unique_ptr<BenchmarkRunner> virtual createUopsBenchmarkRunner(
|
|
|
|
const LLVMState &State) const;
|
|
|
|
|
2018-06-25 19:22:23 +08:00
|
|
|
const ExegesisTarget *Next = nullptr;
|
2018-10-25 15:44:01 +08:00
|
|
|
const llvm::ArrayRef<CpuAndPfmCounters> CpuPfmCounters;
|
2018-06-19 19:28:59 +08:00
|
|
|
};
|
|
|
|
|
2018-06-25 19:22:23 +08:00
|
|
|
} // namespace exegesis
|
2018-10-23 01:10:47 +08:00
|
|
|
} // namespace llvm
|
2018-06-19 19:28:59 +08:00
|
|
|
|
|
|
|
#endif // LLVM_TOOLS_LLVM_EXEGESIS_TARGET_H
|