xref: /freebsd/contrib/llvm-project/llvm/lib/Target/AMDGPU/SISchedule.td (revision cb14a3fe5122c879eae1fb480ed7ce82a699ddb6)
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// Integer multiplications.
58def WriteIntMul : SchedWrite;
59
60// mAI multipass instructions.
61def Write2PassMAI  : SchedWrite;
62def Write4PassMAI  : SchedWrite;
63def Write8PassMAI  : SchedWrite;
64def Write16PassMAI : SchedWrite;
65def Write4PassDGEMM : SchedWrite;
66def Write8PassDGEMM : SchedWrite;
67
68// Scalar float instructions
69def WriteSFPU : SchedWrite;
70
71// F16 or F32 pseudo scalar transcendental instructions
72def WritePseudoScalarTrans : SchedWrite;
73
74// FIXME: Should there be a class for instructions which are VALU
75// instructions and have VALU rates, but write to the SALU (i.e. VOPC
76// instructions)
77
78class SISchedMachineModel : SchedMachineModel {
79  let CompleteModel = 1;
80  // MicroOpBufferSize = 1 means that instructions will always be added
81  // the ready queue when they become available.  This exposes them
82  // to the register pressure analysis.
83  let MicroOpBufferSize = 1;
84  let IssueWidth = 1;
85  let PostRAScheduler = 1;
86
87  // FIXME:Approximate 2 * branch cost.  Try to hack around bad
88  // early-ifcvt heuristics. These need improvement to avoid the OOE
89  // heuristics.
90  int MispredictPenalty = 20;
91}
92
93def SIFullSpeedModel : SISchedMachineModel;
94def SIQuarterSpeedModel : SISchedMachineModel;
95def SIDPFullSpeedModel : SISchedMachineModel;
96def SIDPGFX940FullSpeedModel : SISchedMachineModel;
97def GFX10SpeedModel : SISchedMachineModel;
98def GFX11SpeedModel : SISchedMachineModel;
99def GFX12SpeedModel : SISchedMachineModel;
100
101// XXX: Are the resource counts correct?
102def HWBranch : ProcResource<1> {
103  let BufferSize = 1;
104}
105def HWExport : ProcResource<1> {
106  let BufferSize = 1;
107}
108def HWLGKM   : ProcResource<1> {
109  let BufferSize = 1;
110}
111def HWSALU   : ProcResource<1> {
112  let BufferSize = 1;
113}
114def HWVMEM   : ProcResource<1> {
115  let BufferSize = 1;
116}
117def HWVALU   : ProcResource<1> {
118  let BufferSize = 1;
119}
120def HWTransVALU : ProcResource<1> { // Transcendental VALU
121  let BufferSize = 1;
122}
123def HWRC   : ProcResource<1> { // Register destination cache
124  let BufferSize = 1;
125}
126def HWXDL   : ProcResource<1> { // MFMA CU
127  let BufferSize = 0;
128}
129
130class HWWriteRes<SchedWrite write, list<ProcResourceKind> resources,
131                 int latency> : WriteRes<write, resources> {
132  let Latency = latency;
133}
134
135class HWVALUWriteRes<SchedWrite write, int latency> :
136  HWWriteRes<write, [HWVALU], latency>;
137
138class UnsupportedWriteRes<SchedWrite write> : WriteRes<write, []> {
139  let Unsupported = 1;
140}
141
142def PredMIReadVGPR : SchedPredicate<[{TII->hasVGPRUses(*MI)}]>;
143
144def MIReadVGPR : SchedReadVariant<[
145      SchedVar<PredMIReadVGPR, [MIVGPRRead]>,
146      SchedVar<NoSchedPred, [ReadDefault]>]>;
147
148// The latency numbers are taken from AMD Accelerated Parallel Processing
149// guide. They may not be accurate.
150
151// The latency values are 1 / (operations / cycle) / 4.
152multiclass SICommonWriteRes {
153
154  let RetireOOO = 1 in { // llvm-mca specific flag
155  def : HWWriteRes<WriteBranch,  [HWBranch], 8>;
156  def : HWWriteRes<WriteExport,  [HWExport], 4>;
157  def : HWWriteRes<WriteLDS,     [HWLGKM],   5>; // Can be between 2 and 64
158  def : HWWriteRes<WriteSALU,    [HWSALU],   1>;
159  def : HWWriteRes<WriteSMEM,    [HWLGKM],   5>;
160  def : HWWriteRes<WriteVMEM,    [HWVMEM],   80>;
161  def : HWWriteRes<WriteBarrier, [HWBranch], 500>; // XXX: Guessed ???
162
163  def : HWVALUWriteRes<Write32Bit,         1>;
164  def : HWVALUWriteRes<WriteFloatCvt,      4>;
165  def : HWVALUWriteRes<WriteTrans32,       4>;
166  def : HWVALUWriteRes<WriteQuarterRate32, 4>;
167
168  def : HWVALUWriteRes<Write4PassDGEMM,    4>;
169  def : HWVALUWriteRes<Write8PassDGEMM,   16>;
170
171  let ReleaseAtCycles = [2] in
172  def : HWWriteRes<Write2PassMAI,  [HWXDL], 2>;
173  let ReleaseAtCycles = [4] in
174  def : HWWriteRes<Write4PassMAI,  [HWXDL], 4>;
175  let ReleaseAtCycles = [8] in
176  def : HWWriteRes<Write8PassMAI,  [HWXDL], 8>;
177  let ReleaseAtCycles = [16] in
178  def : HWWriteRes<Write16PassMAI, [HWXDL], 16>;
179
180  def : UnsupportedWriteRes<WriteSFPU>;
181  def : UnsupportedWriteRes<WritePseudoScalarTrans>;
182  } // End RetireOOO = 1
183
184  def : ReadAdvance<MIVGPRRead, -2>;
185
186  // Technically mfma reads can be from 0 to 4 cycles but that does not make
187  // sense to model because its register setup is huge. In particular if we
188  // properly model read advance as -2 for a vgpr read it will result in a
189  // bad scheduling of acc writes before that mfma. To avoid it we would
190  // need to consume 2 or 4 more vgprs to be initialized before the acc
191  // write sequence. Just assume worst case here.
192  def : ReadAdvance<MIMFMARead, -4>;
193}
194
195def PredIsVGPR32Copy : SchedPredicate<[{TII->isVGPRCopy(*MI) && TII->getOpSize(*MI, 0) <= 32}]>;
196def PredIsVGPR64Copy : SchedPredicate<[{TII->isVGPRCopy(*MI) && TII->getOpSize(*MI, 0) > 32}]>;
197def WriteCopy : SchedWriteVariant<[
198    SchedVar<PredIsVGPR32Copy, [Write32Bit]>,
199    SchedVar<PredIsVGPR64Copy, [Write64Bit]>,
200    SchedVar<NoSchedPred, [WriteSALU]>]>;
201
202let SchedModel = SIFullSpeedModel in {
203
204defm : SICommonWriteRes;
205
206let RetireOOO = 1 in { // llvm-mca specific flag
207def : HWVALUWriteRes<Write64Bit,       2>;
208def : HWVALUWriteRes<WriteIntMul,      4>;
209def : HWVALUWriteRes<WriteFloatFMA,    1>;
210def : HWVALUWriteRes<WriteDouble,      4>;
211def : HWVALUWriteRes<WriteDoubleAdd,   2>;
212def : HWVALUWriteRes<WriteDoubleCvt,   4>;
213def : HWVALUWriteRes<WriteTrans64,     4>;
214} // End RetireOOO = 1
215
216def : InstRW<[WriteCopy], (instrs COPY)>;
217
218} // End SchedModel = SIFullSpeedModel
219
220let SchedModel = SIQuarterSpeedModel in {
221
222defm : SICommonWriteRes;
223
224let RetireOOO = 1 in { // llvm-mca specific flag
225def : HWVALUWriteRes<Write64Bit,       2>;
226def : HWVALUWriteRes<WriteIntMul,      4>;
227def : HWVALUWriteRes<WriteFloatFMA,    16>;
228def : HWVALUWriteRes<WriteDouble,      16>;
229def : HWVALUWriteRes<WriteDoubleAdd,    8>;
230def : HWVALUWriteRes<WriteDoubleCvt,    4>;
231def : HWVALUWriteRes<WriteTrans64,     16>;
232} // End RetireOOO = 1
233
234def : InstRW<[WriteCopy], (instrs COPY)>;
235def : InstRW<[Write64Bit, MIReadVGPR], (instregex "^V_ACCVGPR_WRITE_B32_e64$")>;
236def : InstRW<[Write2PassMAI,  MIMFMARead], (instregex "^V_MFMA_..._4X4X")>;
237def : InstRW<[Write8PassMAI,  MIMFMARead], (instregex "^V_MFMA_..._16X16X")>;
238def : InstRW<[Write16PassMAI, MIMFMARead], (instregex "^V_MFMA_..._32X32X")>;
239
240}  // End SchedModel = SIQuarterSpeedModel
241
242let SchedModel = SIDPFullSpeedModel in {
243
244defm : SICommonWriteRes;
245
246let RetireOOO = 1 in { // llvm-mca specific flag
247def : HWVALUWriteRes<WriteFloatFMA,    1>;
248def : HWVALUWriteRes<WriteDouble,      1>;
249def : HWVALUWriteRes<WriteDoubleAdd,   1>;
250def : HWVALUWriteRes<WriteDoubleCvt,   1>;
251def : HWVALUWriteRes<WriteTrans64,     4>;
252def : HWVALUWriteRes<WriteIntMul,      1>;
253def : HWVALUWriteRes<Write64Bit,       1>;
254} // End RetireOOO = 1
255
256def : InstRW<[WriteCopy], (instrs COPY)>;
257def : InstRW<[Write64Bit], (instregex "^V_ACCVGPR_WRITE_B32_e64$")>;
258def : InstRW<[Write2PassMAI,   MIMFMARead], (instregex "^V_MFMA_.32_4X4X")>;
259def : InstRW<[Write8PassMAI,   MIMFMARead], (instregex "^V_MFMA_.32_16X16X")>;
260def : InstRW<[Write16PassMAI,  MIMFMARead], (instregex "^V_MFMA_.32_32X32X")>;
261def : InstRW<[Write4PassDGEMM, MIMFMARead], (instregex "^V_MFMA_.64_4X4X")>;
262def : InstRW<[Write8PassDGEMM, MIMFMARead], (instregex "^V_MFMA_.64_16X16X")>;
263
264} // End SchedModel = SIDPFullSpeedModel
265
266let SchedModel = SIDPGFX940FullSpeedModel in {
267
268defm : SICommonWriteRes;
269
270def : HWVALUWriteRes<WriteFloatFMA,    1>;
271def : HWVALUWriteRes<WriteDouble,      1>;
272def : HWVALUWriteRes<WriteDoubleAdd,   1>;
273def : HWVALUWriteRes<WriteDoubleCvt,   1>;
274def : HWVALUWriteRes<WriteTrans64,     4>;
275def : HWVALUWriteRes<WriteIntMul,      1>;
276def : HWVALUWriteRes<Write64Bit,       1>;
277
278def : InstRW<[WriteCopy], (instrs COPY)>;
279def : InstRW<[Write64Bit], (instregex "^V_ACCVGPR_WRITE_B32_e64$")>;
280def : InstRW<[Write2PassMAI,   MIMFMARead], (instregex "^V_MFMA_.32_4X4X")>;
281
282def : InstRW<[Write4PassMAI,   MIMFMARead], (instregex "^V_MFMA_.32_16X16X8X")>;
283def : InstRW<[Write4PassMAI,   MIMFMARead], (instregex "^V_MFMA_.32_16X16X16")>;
284def : InstRW<[Write4PassMAI,   MIMFMARead], (instregex "^V_MFMA_.32_16X16X32")>;
285def : InstRW<[Write8PassMAI,   MIMFMARead], (instregex "^V_MFMA_.32_16X16X[14][FBI]")>;
286
287def : InstRW<[Write8PassMAI,   MIMFMARead], (instregex "^V_MFMA_.32_32X32X4XF")>;
288def : InstRW<[Write8PassMAI,   MIMFMARead], (instregex "^V_MFMA_.32_32X32X8")>;
289def : InstRW<[Write8PassMAI,   MIMFMARead], (instregex "^V_MFMA_.32_32X32X16")>;
290def : InstRW<[Write16PassMAI,  MIMFMARead], (instregex "^V_MFMA_.32_32X32X[124][FBI]")>;
291
292def : InstRW<[Write4PassDGEMM, MIMFMARead], (instregex "^V_MFMA_.64_4X4X")>;
293def : InstRW<[Write8PassDGEMM, MIMFMARead], (instregex "^V_MFMA_.64_16X16X")>;
294
295def : InstRW<[Write4PassMAI,   MIMFMARead], (instregex "^V_SMFMAC_.32_16X16X")>;
296def : InstRW<[Write8PassMAI,   MIMFMARead], (instregex "^V_SMFMAC_.32_32X32X")>;
297
298} // End SchedModel = SIDPGFX940FullSpeedModel
299
300let SchedModel = GFX10SpeedModel in {
301
302// The latency values are 1 / (operations / cycle).
303// Add 1 stall cycle for VGPR read.
304let RetireOOO = 1 in { // llvm-mca specific flag
305def : HWWriteRes<Write32Bit,         [HWVALU, HWRC],   5>;
306def : HWWriteRes<WriteFloatCvt,      [HWVALU, HWRC],   5>;
307def : HWWriteRes<Write64Bit,         [HWVALU, HWRC],   6>;
308def : HWWriteRes<WriteTrans32,       [HWTransVALU, HWRC], 10>;
309def : HWWriteRes<WriteQuarterRate32, [HWVALU, HWRC],   8>;
310def : HWWriteRes<WriteFloatFMA,      [HWVALU, HWRC],   5>;
311def : HWWriteRes<WriteDouble,        [HWVALU, HWRC],   22>;
312def : HWWriteRes<WriteDoubleAdd,     [HWVALU, HWRC],   22>;
313def : HWWriteRes<WriteDoubleCvt,     [HWVALU, HWRC],   22>;
314def : HWWriteRes<WriteIntMul,        [HWVALU, HWRC],   8>;
315def : HWWriteRes<WriteTrans64,       [HWVALU, HWTransVALU, HWRC], 24>;
316
317def : HWWriteRes<WriteBranch,        [HWBranch],       32>;
318def : HWWriteRes<WriteExport,        [HWExport, HWRC], 16>;
319def : HWWriteRes<WriteLDS,           [HWLGKM,   HWRC], 20>;
320def : HWWriteRes<WriteSALU,          [HWSALU,   HWRC], 2>;
321def : HWWriteRes<WriteSMEM,          [HWLGKM,   HWRC], 20>;
322def : HWWriteRes<WriteVMEM,          [HWVMEM,   HWRC], 320>;
323def : HWWriteRes<WriteBarrier,       [HWBranch],       2000>;
324
325def : UnsupportedWriteRes<WriteSFPU>;
326def : UnsupportedWriteRes<WritePseudoScalarTrans>;
327} // End RetireOOO = 1
328
329def : InstRW<[WriteCopy], (instrs COPY)>;
330
331}  // End SchedModel = GFX10SpeedModel
332
333let SchedModel = GFX11SpeedModel in {
334
335// The latency values are 1 / (operations / cycle).
336// Add 1 stall cycle for VGPR read.
337let RetireOOO = 1 in { // llvm-mca specific flag
338def : HWWriteRes<Write32Bit,         [HWVALU, HWRC],   5>;
339def : HWWriteRes<WriteFloatCvt,      [HWVALU, HWRC],   5>;
340def : HWWriteRes<Write64Bit,         [HWVALU, HWRC],   6>;
341def : HWWriteRes<WriteTrans32,       [HWTransVALU, HWRC], 10>;
342def : HWWriteRes<WriteQuarterRate32, [HWVALU, HWRC],   8>;
343def : HWWriteRes<WriteFloatFMA,      [HWVALU, HWRC],   5>;
344def : HWWriteRes<WriteDouble,        [HWVALU, HWRC],   38>;
345def : HWWriteRes<WriteDoubleAdd,     [HWVALU, HWRC],   38>;
346def : HWWriteRes<WriteDoubleCvt,     [HWVALU, HWRC],   38>;
347def : HWWriteRes<WriteIntMul,        [HWVALU, HWRC],   8>;
348def : HWWriteRes<WriteTrans64,       [HWVALU, HWTransVALU, HWRC], 40>;
349
350def : HWWriteRes<WriteBranch,        [HWBranch],       32>;
351def : HWWriteRes<WriteExport,        [HWExport, HWRC], 16>;
352def : HWWriteRes<WriteLDS,           [HWLGKM,   HWRC], 20>;
353def : HWWriteRes<WriteSALU,          [HWSALU,   HWRC], 2>;
354def : HWWriteRes<WriteSFPU,          [HWSALU,   HWRC], 4>;
355def : HWWriteRes<WriteSMEM,          [HWLGKM,   HWRC], 20>;
356def : HWWriteRes<WriteVMEM,          [HWVMEM,   HWRC], 320>;
357def : HWWriteRes<WriteBarrier,       [HWBranch],       2000>;
358} // End RetireOOO = 1
359
360def : UnsupportedWriteRes<WritePseudoScalarTrans>;
361
362def : InstRW<[WriteCopy], (instrs COPY)>;
363
364}  // End SchedModel = GFX11SpeedModel
365
366let SchedModel = GFX12SpeedModel in {
367
368def : HWWriteRes<Write32Bit,             [HWVALU, HWRC],   5>;
369def : HWWriteRes<WriteFloatCvt,          [HWVALU, HWRC],   5>;
370def : HWWriteRes<Write64Bit,             [HWVALU, HWRC],   6>;
371def : HWWriteRes<WriteTrans32,           [HWVALU, HWRC],   10>;
372def : HWWriteRes<WriteQuarterRate32,     [HWVALU, HWRC],   8>;
373def : HWWriteRes<WriteFloatFMA,          [HWVALU, HWRC],   5>;
374def : HWWriteRes<WriteDouble,            [HWVALU, HWRC],   38>;
375def : HWWriteRes<WriteDoubleAdd,         [HWVALU, HWRC],   38>;
376def : HWWriteRes<WriteDoubleCvt,         [HWVALU, HWRC],   38>;
377def : HWWriteRes<WriteIntMul,            [HWVALU, HWRC],   8>;
378def : HWWriteRes<WriteTrans64,           [HWVALU, HWRC],   40>;
379def : HWWriteRes<WritePseudoScalarTrans, [HWVALU, HWRC],   7>;
380
381def : HWWriteRes<WriteBranch,            [HWBranch],       32>;
382def : HWWriteRes<WriteExport,            [HWExport, HWRC], 16>;
383def : HWWriteRes<WriteLDS,               [HWLGKM,   HWRC], 20>;
384def : HWWriteRes<WriteSALU,              [HWSALU,   HWRC], 2>;
385def : HWWriteRes<WriteSFPU,              [HWSALU,   HWRC], 4>;
386def : HWWriteRes<WriteSMEM,              [HWLGKM,   HWRC], 20>;
387def : HWWriteRes<WriteVMEM,              [HWVMEM,   HWRC], 320>;
388def : HWWriteRes<WriteBarrier,           [HWBranch],       2000>;
389
390def : InstRW<[WriteCopy], (instrs COPY)>;
391
392}  // End SchedModel = GFX12SpeedModel
393