blob: 70313a7a2f7ac463f109680ab554221623d60d10 [file] [log] [blame]
Clement Courbet44b4c542018-06-19 11:28:59 +00001//===-- Target.h ------------------------------------------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Clement Courbet44b4c542018-06-19 11:28:59 +00006//
7//===----------------------------------------------------------------------===//
8///
9/// \file
10///
11/// Classes that handle the creation of target-specific objects. This is
12/// similar to llvm::Target/TargetRegistry.
13///
14//===----------------------------------------------------------------------===//
15
16#ifndef LLVM_TOOLS_LLVM_EXEGESIS_TARGET_H
17#define LLVM_TOOLS_LLVM_EXEGESIS_TARGET_H
18
Clement Courbet4860b982018-06-26 08:49:30 +000019#include "BenchmarkResult.h"
20#include "BenchmarkRunner.h"
21#include "LlvmState.h"
Clement Courbetd939f6d2018-09-13 07:40:53 +000022#include "SnippetGenerator.h"
Clement Courbet44b4c542018-06-19 11:28:59 +000023#include "llvm/ADT/Triple.h"
Clement Courbet6fd00e32018-06-20 11:54:35 +000024#include "llvm/CodeGen/TargetPassConfig.h"
Guillaume Chateletfb943542018-08-01 14:41:45 +000025#include "llvm/IR/CallingConv.h"
Clement Courbet6fd00e32018-06-20 11:54:35 +000026#include "llvm/IR/LegacyPassManager.h"
Clement Courbeta51efc22018-06-25 13:12:02 +000027#include "llvm/MC/MCInst.h"
28#include "llvm/MC/MCRegisterInfo.h"
Clement Courbet44b4c542018-06-19 11:28:59 +000029
Fangrui Song32401af2018-10-22 17:10:47 +000030namespace llvm {
Clement Courbet44b4c542018-06-19 11:28:59 +000031namespace exegesis {
32
Clement Courbet41c8af32018-10-25 07:44:01 +000033struct PfmCountersInfo {
34 // An optional name of a performance counter that can be used to measure
35 // cycles.
Simon Pilgrim2a9c7282018-10-25 10:45:38 +000036 const char *CycleCounter;
Clement Courbet41c8af32018-10-25 07:44:01 +000037
38 // An optional name of a performance counter that can be used to measure
39 // uops.
Simon Pilgrim2a9c7282018-10-25 10:45:38 +000040 const char *UopsCounter;
Clement Courbet41c8af32018-10-25 07:44:01 +000041
42 // An IssueCounter specifies how to measure uops issued to specific proc
43 // resources.
44 struct IssueCounter {
Simon Pilgrim2a9c7282018-10-25 10:45:38 +000045 const char *Counter;
Clement Courbet41c8af32018-10-25 07:44:01 +000046 // The name of the ProcResource that this counter measures.
Simon Pilgrim2a9c7282018-10-25 10:45:38 +000047 const char *ProcResName;
Clement Courbet41c8af32018-10-25 07:44:01 +000048 };
49 // An optional list of IssueCounters.
Simon Pilgrim2a9c7282018-10-25 10:45:38 +000050 const IssueCounter *IssueCounters;
51 unsigned NumIssueCounters;
Clement Courbet41c8af32018-10-25 07:44:01 +000052
53 static const PfmCountersInfo Default;
54};
55
56struct CpuAndPfmCounters {
Simon Pilgrim2a9c7282018-10-25 10:45:38 +000057 const char *CpuName;
58 const PfmCountersInfo *PCI;
Clement Courbet41c8af32018-10-25 07:44:01 +000059 bool operator<(llvm::StringRef S) const {
60 return llvm::StringRef(CpuName) < S;
61 }
62};
63
Clement Courbet44b4c542018-06-19 11:28:59 +000064class ExegesisTarget {
65public:
Clement Courbet41c8af32018-10-25 07:44:01 +000066 explicit ExegesisTarget(llvm::ArrayRef<CpuAndPfmCounters> CpuPfmCounters)
67 : CpuPfmCounters(CpuPfmCounters) {}
68
Clement Courbet6fd00e32018-06-20 11:54:35 +000069 // Targets can use this to add target-specific passes in assembleToStream();
70 virtual void addTargetSpecificPasses(llvm::PassManagerBase &PM) const {}
71
Clement Courbeta51efc22018-06-25 13:12:02 +000072 // Generates code to move a constant into a the given register.
Guillaume Chateletc96a97b2018-09-20 12:22:18 +000073 // Precondition: Value must fit into Reg.
Simon Pilgrimf652ef32018-09-18 15:38:16 +000074 virtual std::vector<llvm::MCInst>
Guillaume Chateletc96a97b2018-09-20 12:22:18 +000075 setRegTo(const llvm::MCSubtargetInfo &STI, unsigned Reg,
76 const llvm::APInt &Value) const = 0;
Guillaume Chatelet5ad29092018-09-18 11:26:27 +000077
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.
Guillaume Chateletc96a97b2018-09-20 12:22:18 +000081 virtual unsigned getScratchMemoryRegister(const llvm::Triple &) const {
82 return 0;
83 }
Guillaume Chateletfb943542018-08-01 14:41:45 +000084
85 // Fills memory operands with references to the address at [Reg] + Offset.
Guillaume Chatelet70ac0192018-09-27 09:23:04 +000086 virtual void fillMemoryOperands(InstructionTemplate &IT, unsigned Reg,
Guillaume Chateletc96a97b2018-09-20 12:22:18 +000087 unsigned Offset) const {
Guillaume Chateletc96a97b2018-09-20 12:22:18 +000088 llvm_unreachable(
89 "fillMemoryOperands() requires getScratchMemoryRegister() > 0");
90 }
Guillaume Chateletfb943542018-08-01 14:41:45 +000091
Clement Courbet9431b722019-09-27 12:56:24 +000092 // Returns a counter usable as a loop counter.
93 virtual unsigned getLoopCounterRegister(const llvm::Triple &) const {
94 return 0;
95 }
96
97 // Adds the code to decrement the loop counter and
Clement Courbetc0292742019-10-03 07:56:56 +000098 virtual void decrementLoopCounterAndJump(MachineBasicBlock &MBB,
99 MachineBasicBlock &TargetMBB,
Clement Courbet9431b722019-09-27 12:56:24 +0000100 const llvm::MCInstrInfo &MII) const {
101 llvm_unreachable("decrementLoopCounterAndBranch() requires "
102 "getLoopCounterRegister() > 0");
103 }
104
Clement Courbet52da9382019-03-26 15:44:57 +0000105 // 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 {}; }
109
Guillaume Chateletfb943542018-08-01 14:41:45 +0000110 // 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.
Guillaume Chateletc96a97b2018-09-20 12:22:18 +0000115 virtual unsigned getMaxMemoryAccessSize() const { return 0; }
Guillaume Chateletfb943542018-08-01 14:41:45 +0000116
Roman Lebedev404bdb12019-04-06 14:16:26 +0000117 // 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;
124
Clement Courbetd939f6d2018-09-13 07:40:53 +0000125 // Creates a snippet generator for the given mode.
126 std::unique_ptr<SnippetGenerator>
127 createSnippetGenerator(InstructionBenchmark::ModeE Mode,
128 const LLVMState &State) const;
Clement Courbet4860b982018-06-26 08:49:30 +0000129 // Creates a benchmark runner for the given mode.
130 std::unique_ptr<BenchmarkRunner>
131 createBenchmarkRunner(InstructionBenchmark::ModeE Mode,
132 const LLVMState &State) const;
133
Clement Courbet44b4c542018-06-19 11:28:59 +0000134 // Returns the ExegesisTarget for the given triple or nullptr if the target
135 // does not exist.
Clement Courbet6fd00e32018-06-20 11:54:35 +0000136 static const ExegesisTarget *lookup(llvm::Triple TT);
Clement Courbet4860b982018-06-26 08:49:30 +0000137 // Returns the default (unspecialized) ExegesisTarget.
138 static const ExegesisTarget &getDefault();
Clement Courbet44b4c542018-06-19 11:28:59 +0000139 // Registers a target. Not thread safe.
140 static void registerTarget(ExegesisTarget *T);
141
Roman Lebedev3de96642018-06-19 11:58:10 +0000142 virtual ~ExegesisTarget();
Clement Courbet44b4c542018-06-19 11:28:59 +0000143
Clement Courbet41c8af32018-10-25 07:44:01 +0000144 // Returns the Pfm counters for the given CPU (or the default if no pfm
145 // counters are defined for this CPU).
146 const PfmCountersInfo &getPfmCounters(llvm::StringRef CpuName) const;
147
Clement Courbet44b4c542018-06-19 11:28:59 +0000148private:
149 virtual bool matchesArch(llvm::Triple::ArchType Arch) const = 0;
Clement Courbet4860b982018-06-26 08:49:30 +0000150
Clement Courbetd939f6d2018-09-13 07:40:53 +0000151 // Targets can implement their own snippet generators/benchmarks runners by
Clement Courbet4860b982018-06-26 08:49:30 +0000152 // implementing these.
Clement Courbetd939f6d2018-09-13 07:40:53 +0000153 std::unique_ptr<SnippetGenerator> virtual createLatencySnippetGenerator(
154 const LLVMState &State) const;
155 std::unique_ptr<SnippetGenerator> virtual createUopsSnippetGenerator(
156 const LLVMState &State) const;
Clement Courbet4860b982018-06-26 08:49:30 +0000157 std::unique_ptr<BenchmarkRunner> virtual createLatencyBenchmarkRunner(
Clement Courbet362653f2019-01-30 16:02:20 +0000158 const LLVMState &State, InstructionBenchmark::ModeE Mode) const;
Clement Courbet4860b982018-06-26 08:49:30 +0000159 std::unique_ptr<BenchmarkRunner> virtual createUopsBenchmarkRunner(
160 const LLVMState &State) const;
161
Clement Courbetcff2caa2018-06-25 11:22:23 +0000162 const ExegesisTarget *Next = nullptr;
Clement Courbet41c8af32018-10-25 07:44:01 +0000163 const llvm::ArrayRef<CpuAndPfmCounters> CpuPfmCounters;
Clement Courbet44b4c542018-06-19 11:28:59 +0000164};
165
Clement Courbetcff2caa2018-06-25 11:22:23 +0000166} // namespace exegesis
Fangrui Song32401af2018-10-22 17:10:47 +0000167} // namespace llvm
Clement Courbet44b4c542018-06-19 11:28:59 +0000168
169#endif // LLVM_TOOLS_LLVM_EXEGESIS_TARGET_H