xref: /freebsd/contrib/llvm-project/llvm/lib/Target/AMDGPU/SISchedule.td (revision dd41de95a84d979615a2ef11df6850622bf6184e)
1//===-- SISchedule.td - SI Scheduling definitions -------------------------===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8//
9// MachineModel definitions for Southern Islands (SI)
10//
11//===----------------------------------------------------------------------===//
12
13def : PredicateProlog<[{
14  const SIInstrInfo *TII =
15    static_cast<const SIInstrInfo*>(SchedModel->getInstrInfo());
16  (void)TII;
17}]>;
18
19def WriteBranch : SchedWrite;
20def WriteExport : SchedWrite;
21def WriteLDS    : SchedWrite;
22def WriteSALU   : SchedWrite;
23def WriteSMEM   : SchedWrite;
24def WriteVMEM   : SchedWrite;
25def WriteBarrier : SchedWrite;
26
27def MIVGPRRead  : SchedRead;
28def MIMFMARead  : SchedRead;
29
30// Normal 16 or 32 bit VALU instructions
31def Write32Bit         : SchedWrite;
32// Conversion to or from F32 (but not converting F64 to or from F32)
33def WriteFloatCvt      : SchedWrite;
34// F16 or F32 transcendental instructions (these are quarter rate)
35def WriteTrans32       : SchedWrite;
36// Other quarter rate VALU instructions
37def WriteQuarterRate32 : SchedWrite;
38
39def WriteFloatFMA   : SchedWrite;
40
41// Slow quarter rate f64 instruction.
42def WriteDouble : SchedWrite;
43
44// half rate f64 instruction (same as v_add_f64)
45def WriteDoubleAdd  : SchedWrite;
46
47// Conversion to or from f64 instruction
48def WriteDoubleCvt  : SchedWrite;
49
50// F64 "transcendental" (actually only reciprocal and/or square root)
51// instructions
52def WriteTrans64    : SchedWrite;
53
54// Half rate 64-bit instructions.
55def Write64Bit : SchedWrite;
56
57// mAI multipass instructions.
58def Write2PassMAI  : SchedWrite;
59def Write8PassMAI  : SchedWrite;
60def Write16PassMAI : SchedWrite;
61
62// FIXME: Should there be a class for instructions which are VALU
63// instructions and have VALU rates, but write to the SALU (i.e. VOPC
64// instructions)
65
66class SISchedMachineModel : SchedMachineModel {
67  let CompleteModel = 1;
68  // MicroOpBufferSize = 1 means that instructions will always be added
69  // the ready queue when they become available.  This exposes them
70  // to the register pressure analysis.
71  let MicroOpBufferSize = 1;
72  let IssueWidth = 1;
73  let PostRAScheduler = 1;
74
75  // FIXME:Approximate 2 * branch cost.  Try to hack around bad
76  // early-ifcvt heuristics. These need improvement to avoid the OOE
77  // heuristics.
78  int MispredictPenalty = 20;
79}
80
81def SIFullSpeedModel : SISchedMachineModel;
82def SIQuarterSpeedModel : SISchedMachineModel;
83def GFX10SpeedModel : SISchedMachineModel;
84
85// XXX: Are the resource counts correct?
86def HWBranch : ProcResource<1> {
87  let BufferSize = 1;
88}
89def HWExport : ProcResource<1> {
90  let BufferSize = 7; // Taken from S_WAITCNT
91}
92def HWLGKM   : ProcResource<1> {
93  let BufferSize = 31;  // Taken from S_WAITCNT
94}
95def HWSALU   : ProcResource<1> {
96  let BufferSize = 1;
97}
98def HWVMEM   : ProcResource<1> {
99  let BufferSize = 15;  // Taken from S_WAITCNT
100}
101def HWVALU   : ProcResource<1> {
102  let BufferSize = 1;
103}
104def HWRC   : ProcResource<1> { // Register destination cache
105  let BufferSize = 1;
106}
107
108class HWWriteRes<SchedWrite write, list<ProcResourceKind> resources,
109                 int latency> : WriteRes<write, resources> {
110  let Latency = latency;
111}
112
113class HWVALUWriteRes<SchedWrite write, int latency> :
114  HWWriteRes<write, [HWVALU], latency>;
115
116def PredMIReadVGPR : SchedPredicate<[{TII->hasVGPRUses(*MI)}]>;
117
118def MIReadVGPR : SchedReadVariant<[
119      SchedVar<PredMIReadVGPR, [MIVGPRRead]>,
120      SchedVar<NoSchedPred, [ReadDefault]>]>;
121
122// The latency numbers are taken from AMD Accelerated Parallel Processing
123// guide. They may not be accurate.
124
125// The latency values are 1 / (operations / cycle) / 4.
126multiclass SICommonWriteRes {
127
128  def : HWWriteRes<WriteBranch,  [HWBranch], 8>;
129  def : HWWriteRes<WriteExport,  [HWExport], 4>;
130  def : HWWriteRes<WriteLDS,     [HWLGKM],   5>; // Can be between 2 and 64
131  def : HWWriteRes<WriteSALU,    [HWSALU],   1>;
132  def : HWWriteRes<WriteSMEM,    [HWLGKM],   5>;
133  def : HWWriteRes<WriteVMEM,    [HWVMEM],   80>;
134  def : HWWriteRes<WriteBarrier, [HWBranch], 500>; // XXX: Guessed ???
135
136  def : HWVALUWriteRes<Write32Bit,         1>;
137  def : HWVALUWriteRes<Write64Bit,         2>;
138  def : HWVALUWriteRes<WriteFloatCvt,      4>;
139  def : HWVALUWriteRes<WriteTrans32,       4>;
140  def : HWVALUWriteRes<WriteQuarterRate32, 4>;
141  def : HWVALUWriteRes<Write2PassMAI,      2>;
142  def : HWVALUWriteRes<Write8PassMAI,      8>;
143  def : HWVALUWriteRes<Write16PassMAI,    16>;
144
145  def : ReadAdvance<MIVGPRRead, -2>;
146  def : InstRW<[Write64Bit, MIReadVGPR], (instregex "^V_ACCVGPR_WRITE_B32$")>;
147
148  // Technically mfma reads can be from 0 to 4 cycles but that does not make
149  // sense to model because its register setup is huge. In particular if we
150  // properly model read advance as -2 for a vgpr read it will result in a
151  // bad scheduling of acc writes before that mfma. To avoid it we would
152  // need to consume 2 or 4 more vgprs to be initialized before the acc
153  // write sequence. Just assume worst case here.
154  def : ReadAdvance<MIMFMARead, -4>;
155
156  def : InstRW<[Write2PassMAI,  MIMFMARead], (instregex "^V_MFMA_..._4X4X")>;
157  def : InstRW<[Write8PassMAI,  MIMFMARead], (instregex "^V_MFMA_..._16X16X")>;
158  def : InstRW<[Write16PassMAI, MIMFMARead], (instregex "^V_MFMA_..._32X32X")>;
159}
160
161def PredIsVGPR32Copy : SchedPredicate<[{TII->isVGPRCopy(*MI) && TII->getOpSize(*MI, 0) <= 32}]>;
162def PredIsVGPR64Copy : SchedPredicate<[{TII->isVGPRCopy(*MI) && TII->getOpSize(*MI, 0) > 32}]>;
163def WriteCopy : SchedWriteVariant<[
164    SchedVar<PredIsVGPR32Copy, [Write32Bit]>,
165    SchedVar<PredIsVGPR64Copy, [Write64Bit]>,
166    SchedVar<NoSchedPred, [WriteSALU]>]>;
167
168let SchedModel = SIFullSpeedModel in {
169
170defm : SICommonWriteRes;
171
172def : HWVALUWriteRes<WriteFloatFMA,   1>;
173def : HWVALUWriteRes<WriteDouble,     4>;
174def : HWVALUWriteRes<WriteDoubleAdd,  2>;
175def : HWVALUWriteRes<WriteDoubleCvt,  4>;
176def : HWVALUWriteRes<WriteTrans64,    4>;
177
178def : InstRW<[WriteCopy], (instrs COPY)>;
179
180} // End SchedModel = SIFullSpeedModel
181
182let SchedModel = SIQuarterSpeedModel in {
183
184defm : SICommonWriteRes;
185
186def : HWVALUWriteRes<WriteFloatFMA, 16>;
187def : HWVALUWriteRes<WriteDouble,   16>;
188def : HWVALUWriteRes<WriteDoubleAdd, 8>;
189def : HWVALUWriteRes<WriteDoubleCvt, 4>;
190def : HWVALUWriteRes<WriteTrans64,  16>;
191
192def : InstRW<[WriteCopy], (instrs COPY)>;
193
194}  // End SchedModel = SIQuarterSpeedModel
195
196let SchedModel = GFX10SpeedModel in {
197
198// The latency values are 1 / (operations / cycle).
199// Add 1 stall cycle for VGPR read.
200def : HWWriteRes<Write32Bit,         [HWVALU, HWRC],   5>;
201def : HWWriteRes<WriteFloatCvt,      [HWVALU, HWRC],   5>;
202def : HWWriteRes<Write64Bit,         [HWVALU, HWRC],   6>;
203def : HWWriteRes<WriteTrans32,       [HWVALU, HWRC],   10>;
204def : HWWriteRes<WriteQuarterRate32, [HWVALU, HWRC],   8>;
205def : HWWriteRes<WriteFloatFMA,      [HWVALU, HWRC],   5>;
206def : HWWriteRes<WriteDouble,        [HWVALU, HWRC],   22>;
207def : HWWriteRes<WriteDoubleAdd,     [HWVALU, HWRC],   22>;
208def : HWWriteRes<WriteDoubleCvt,     [HWVALU, HWRC],   22>;
209def : HWWriteRes<WriteTrans64,       [HWVALU, HWRC],   24>;
210
211def : HWWriteRes<WriteBranch,        [HWBranch],       32>;
212def : HWWriteRes<WriteExport,        [HWExport, HWRC], 16>;
213def : HWWriteRes<WriteLDS,           [HWLGKM,   HWRC], 20>;
214def : HWWriteRes<WriteSALU,          [HWSALU,   HWRC], 2>;
215def : HWWriteRes<WriteSMEM,          [HWLGKM,   HWRC], 20>;
216def : HWWriteRes<WriteVMEM,          [HWVMEM,   HWRC], 320>;
217def : HWWriteRes<WriteBarrier,       [HWBranch],       2000>;
218
219def : InstRW<[WriteCopy], (instrs COPY)>;
220
221}  // End SchedModel = GFX10SpeedModel
222