1 //===-- Target.h ------------------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
11 /// Classes that handle the creation of target-specific objects. This is
12 /// similar to llvm::Target/TargetRegistry.
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_TOOLS_LLVM_EXEGESIS_TARGET_H
17 #define LLVM_TOOLS_LLVM_EXEGESIS_TARGET_H
19 #include "BenchmarkResult.h"
20 #include "BenchmarkRunner.h"
21 #include "LlvmState.h"
22 #include "SnippetGenerator.h"
23 #include "llvm/ADT/Triple.h"
24 #include "llvm/CodeGen/TargetPassConfig.h"
25 #include "llvm/IR/CallingConv.h"
26 #include "llvm/IR/LegacyPassManager.h"
27 #include "llvm/MC/MCInst.h"
28 #include "llvm/MC/MCRegisterInfo.h"
33 struct PfmCountersInfo
{
34 // An optional name of a performance counter that can be used to measure
36 const char *CycleCounter
;
38 // An optional name of a performance counter that can be used to measure
40 const char *UopsCounter
;
42 // An IssueCounter specifies how to measure uops issued to specific proc
46 // The name of the ProcResource that this counter measures.
47 const char *ProcResName
;
49 // An optional list of IssueCounters.
50 const IssueCounter
*IssueCounters
;
51 unsigned NumIssueCounters
;
53 static const PfmCountersInfo Default
;
56 struct CpuAndPfmCounters
{
58 const PfmCountersInfo
*PCI
;
59 bool operator<(llvm::StringRef S
) const {
60 return llvm::StringRef(CpuName
) < S
;
64 class ExegesisTarget
{
66 explicit ExegesisTarget(llvm::ArrayRef
<CpuAndPfmCounters
> CpuPfmCounters
)
67 : CpuPfmCounters(CpuPfmCounters
) {}
69 // Targets can use this to add target-specific passes in assembleToStream();
70 virtual void addTargetSpecificPasses(llvm::PassManagerBase
&PM
) const {}
72 // Generates code to move a constant into a the given register.
73 // Precondition: Value must fit into Reg.
74 virtual std::vector
<llvm::MCInst
>
75 setRegTo(const llvm::MCSubtargetInfo
&STI
, unsigned Reg
,
76 const llvm::APInt
&Value
) const = 0;
78 // Returns the register pointing to scratch memory, or 0 if this target
79 // does not support memory operands. The benchmark function uses the
80 // default calling convention.
81 virtual unsigned getScratchMemoryRegister(const llvm::Triple
&) const {
85 // Fills memory operands with references to the address at [Reg] + Offset.
86 virtual void fillMemoryOperands(InstructionTemplate
&IT
, unsigned Reg
,
87 unsigned Offset
) const {
89 "fillMemoryOperands() requires getScratchMemoryRegister() > 0");
92 // Returns a counter usable as a loop counter.
93 virtual unsigned getLoopCounterRegister(const llvm::Triple
&) const {
97 // Adds the code to decrement the loop counter and
98 virtual void decrementLoopCounterAndJump(MachineBasicBlock
&MBB
,
99 MachineBasicBlock
&TargetMBB
,
100 const llvm::MCInstrInfo
&MII
) const {
101 llvm_unreachable("decrementLoopCounterAndBranch() requires "
102 "getLoopCounterRegister() > 0");
105 // Returns a list of unavailable registers.
106 // Targets can use this to prevent some registers to be automatically selected
107 // for use in snippets.
108 virtual ArrayRef
<unsigned> getUnavailableRegisters() const { return {}; }
110 // Returns the maximum number of bytes a load/store instruction can access at
111 // once. This is typically the size of the largest register available on the
112 // processor. Note that this only used as a hint to generate independant
113 // load/stores to/from memory, so the exact returned value does not really
114 // matter as long as it's large enough.
115 virtual unsigned getMaxMemoryAccessSize() const { return 0; }
117 // Assigns a random operand of the right type to variable Var.
118 // The default implementation only handles generic operand types.
119 // The target is responsible for handling any operand
120 // starting from OPERAND_FIRST_TARGET.
121 virtual void randomizeMCOperand(const Instruction
&Instr
, const Variable
&Var
,
122 llvm::MCOperand
&AssignedValue
,
123 const llvm::BitVector
&ForbiddenRegs
) const;
125 // Creates a snippet generator for the given mode.
126 std::unique_ptr
<SnippetGenerator
>
127 createSnippetGenerator(InstructionBenchmark::ModeE Mode
,
128 const LLVMState
&State
,
129 const SnippetGenerator::Options
&Opts
) const;
130 // Creates a benchmark runner for the given mode.
131 std::unique_ptr
<BenchmarkRunner
>
132 createBenchmarkRunner(InstructionBenchmark::ModeE Mode
,
133 const LLVMState
&State
) const;
135 // Returns the ExegesisTarget for the given triple or nullptr if the target
137 static const ExegesisTarget
*lookup(llvm::Triple TT
);
138 // Returns the default (unspecialized) ExegesisTarget.
139 static const ExegesisTarget
&getDefault();
140 // Registers a target. Not thread safe.
141 static void registerTarget(ExegesisTarget
*T
);
143 virtual ~ExegesisTarget();
145 // Returns the Pfm counters for the given CPU (or the default if no pfm
146 // counters are defined for this CPU).
147 const PfmCountersInfo
&getPfmCounters(llvm::StringRef CpuName
) const;
150 virtual bool matchesArch(llvm::Triple::ArchType Arch
) const = 0;
152 // Targets can implement their own snippet generators/benchmarks runners by
153 // implementing these.
154 std::unique_ptr
<SnippetGenerator
> virtual createLatencySnippetGenerator(
155 const LLVMState
&State
, const SnippetGenerator::Options
&Opts
) const;
156 std::unique_ptr
<SnippetGenerator
> virtual createUopsSnippetGenerator(
157 const LLVMState
&State
, const SnippetGenerator::Options
&Opts
) const;
158 std::unique_ptr
<BenchmarkRunner
> virtual createLatencyBenchmarkRunner(
159 const LLVMState
&State
, InstructionBenchmark::ModeE Mode
) const;
160 std::unique_ptr
<BenchmarkRunner
> virtual createUopsBenchmarkRunner(
161 const LLVMState
&State
) const;
163 const ExegesisTarget
*Next
= nullptr;
164 const llvm::ArrayRef
<CpuAndPfmCounters
> CpuPfmCounters
;
167 } // namespace exegesis
170 #endif // LLVM_TOOLS_LLVM_EXEGESIS_TARGET_H