LLVM 24.0.0git
AMDGPUCoExecInfo.h
Go to the documentation of this file.
1//===-- AMDGPUCoExecInfo.h - Co-execution info ------------------*- C++ -*-===//
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/// \file
10/// Shared types for co-execution modeling used by GCNHazardRecognizer and the
11/// schedulers.
12///
13/// Multi-cycle instructions (WMMA, TRANS, etc.) have execution windows where
14/// other instruction types can co-execute. For WMMA, slot patterns depend on
15/// the variant:
16///
17/// E0 (Issue): Control instructions only (s_delay_alu, s_set_vgpr_msb)
18/// E (External): Memory and SALU can co-execute, no VALU
19/// I (Internal): VALU, TRANS, memory, and SALU can all co-execute
20/// V (Vacant): Memory/SALU/next-WMMA ok, NO VALU/TRANS
21///
22//===----------------------------------------------------------------------===//
23
24#ifndef LLVM_LIB_TARGET_AMDGPU_AMDGPUCOEXECINFO_H
25#define LLVM_LIB_TARGET_AMDGPU_AMDGPUCOEXECINFO_H
26
27#include "SIDefines.h"
28#include "SIInstrInfo.h"
30#include "llvm/ADT/StringRef.h"
32#include <cassert>
33#include <cstdint>
34#include <optional>
35
36namespace llvm {
37
38namespace AMDGPU {
39
40//===----------------------------------------------------------------------===//
41// Co-execution Bitmasks
42//===----------------------------------------------------------------------===//
43
44/// Bitmask for instruction types allowed to co-execute at a stage.
45enum class CoExecMask : uint16_t {
46 None = 0,
47 CTRL = 1 << 0, // Control: s_delay_alu, s_set_vgpr_msb
48 VALU = 1 << 1, // Vector ALU
49 TRANS = 1 << 2, // Transcendentals (V_EXP etc)
50 SALU = 1 << 3, // Scalar ALU
51 DS = 1 << 4, // LDS read/write
52 VMEM = 1 << 5, // Global memory
53 SMEM = 1 << 6, // Scalar memory
54 WMMA = 1 << 7, // Next WMMA (V stages only), or MFMA
55 PK_F32 = 1 << 8, // V_PK*_{F32,B32} on gfx942/gfx950
56 All = 0xFFFF,
57
58 MEM = DS | VMEM | SMEM,
59 StageE0 = CTRL, // Issue: control only
60 StageE = CTRL | SALU | MEM, // External: mem/salu
61 StageI = CTRL | SALU | MEM | VALU | TRANS, // Internal: all ALU
62 // Internal + scaled-WMMA absorb: same as StageI but the next scaled
63 // WMMA may issue here - its LD_SCALE consumes the I cycle and the matrix
64 // multiply lands in the V slot that follows. Used for the last I before
65 // V of scaled patterns.
67 StageV = CTRL | SALU | MEM | WMMA, // Vacant: no valu/trans
68 StageTR = All & ~TRANS, // TRANS co-exec: no TRANS
69
70 LLVM_MARK_AS_BITMASK_ENUM(/*LargestValue=*/All)
71};
72
74
75//===----------------------------------------------------------------------===//
76// Instruction Flavor Classification
77//===----------------------------------------------------------------------===//
78
79/// Classification of instructions by execution characteristics.
80/// Used for scheduling decisions and co-execution slot preferences.
82 WMMA, // WMMA/MFMA matrix operations
83 SingleCycleVALU, // Single-cycle VALU (not TRANS, not multi-cycle CVT)
84 TRANS, // Transcendental ops (v_exp, v_log, etc.)
85 MultiCycleVALU, // VALU instructions with repeat rate > 1
86 PackedF32VALU, // V_PK*_{F32,B32} with packed FP32 ops
87 VMEM, // FLAT/GLOBAL memory operations
88 SMEM, // Scalar memory operations
89 DS, // LDS/GDS operations
90 SALU, // Scalar ALU
91 DMA, // Tensor DMA operations
92 Fence, // Fences and waits
93 Other, // Everything else
95};
96
98 switch (F) {
100 return "WMMA";
102 return "VALU(1c)";
104 return "TRANS";
106 return "VALU(Nc)";
108 return "VALU(PK_F32)";
110 return "VMEM";
112 return "SMEM";
114 return "DS";
116 return "SALU";
118 return "DMA";
120 return "Fence";
122 return "Other";
124 return "???";
125 }
126 llvm_unreachable("Unknown InstructionFlavor");
127}
128
129/// Classify \p MI into the execution flavor that drives both the scheduler's
130/// slot preferences and the hazard recognizer's co-execution masks.
131InstructionFlavor classifyFlavor(const MachineInstr &MI,
132 const SIInstrInfo &SII);
133
134/// Map a flavor to the co-execution class it occupies in a window slot.
136 switch (F) {
138 return CoExecMask::WMMA;
140 return CoExecMask::TRANS;
142 return CoExecMask::PK_F32;
145 // LDS DMA and tensor DMA issue on the VALU pipe.
147 return CoExecMask::VALU;
149 return CoExecMask::DS;
151 return CoExecMask::VMEM;
153 return CoExecMask::SMEM;
155 // Fences are s_barrier_*/s_wait_*, which issue on the scalar pipe.
157 return CoExecMask::SALU;
159 return CoExecMask::CTRL;
161 break;
162 }
163 llvm_unreachable("Unknown InstructionFlavor");
164}
165
166//===----------------------------------------------------------------------===//
167// Co-execution Stage Type
168//===----------------------------------------------------------------------===//
169
170/// Stage type for co-execution (for annotation/display).
172 NONE = 0, // Not in co-exec window
173 E0, // Issue cycle - control only
174 E, // External - MEM/SALU allowed
175 I, // Internal - MEM/SALU/VALU allowed
176 IS, // Internal + scaled-WMMA absorb (I plus next-WMMA issue)
177 V, // Vacant - MEM/SALU/WMMA allowed, no VALU
178 TR // TRANS co-exec - everything except TRANS
179};
180
181inline const char *getStageTypeName(CoExecStageType T) {
182 switch (T) {
184 return "--";
186 return "E0";
188 return "E";
190 return "I";
192 return "IS";
194 return "V";
196 return "TR";
197 }
198 llvm_unreachable("Unknown CoExecStageType");
199}
200
201/// Return a human-readable name for a mask holding a single instruction class,
202/// as produced by getCoExecMask().
203inline const char *getCoExecMaskName(CoExecMaskT Mask) {
204 switch (Mask) {
205 case CoExecMask::CTRL:
206 return "CTRL";
207 case CoExecMask::VALU:
208 return "VALU";
210 return "TRANS";
211 case CoExecMask::SALU:
212 return "SALU";
213 case CoExecMask::DS:
214 return "DS";
215 case CoExecMask::VMEM:
216 return "VMEM";
217 case CoExecMask::SMEM:
218 return "SMEM";
219 case CoExecMask::WMMA:
220 return "WMMA";
222 return "PK_F32";
223 default:
224 llvm_unreachable("Not a single instruction class");
225 }
226}
227
228/// Max stages: INT8 16x16x64 = 17 cycles, round up for safety.
229constexpr unsigned MaxCoExecStages = 32;
230
231//===----------------------------------------------------------------------===//
232// Co-execution Slot Info
233//===----------------------------------------------------------------------===//
234
235/// Per-slot info: which instruction classes may co-execute here.
237 CoExecMaskT Mask = CoExecMask::All; // What CAN execute (correctness)
238};
239
240//===----------------------------------------------------------------------===//
241// Co-execution Info
242//===----------------------------------------------------------------------===//
243
244/// Co-execution characteristics for a multi-cycle instruction.
246 /// Number of cycles for which the producing instruction occupies its
247 /// execution unit.
248 unsigned UnitOccupancy = 0;
249 /// Number of cycles in the co-execution window, counting any trailing
250 /// vacant stages.
251 unsigned TotalWindow = 0;
252 /// Per-stage slot info (capability mask).
254 /// Pattern string for display (e.g., "0EIIEEIIV").
256
257 /// Default constructor - initialize to safe defaults.
259 for (unsigned I = 0; I < MaxCoExecStages; ++I)
260 Slots[I].Mask = CoExecMask::All; // Default: permissive
261 }
262
263 /// Get capability mask for a stage.
264 CoExecMaskT getMask(unsigned Stage) const {
265 return Stage < TotalWindow ? Slots[Stage].Mask : CoExecMask::All;
266 }
267
268 /// Check if an instruction class mask can co-execute at a given stage.
269 bool canCoExec(CoExecMaskT InstMask, unsigned Stage) const {
270 if (Stage >= TotalWindow)
271 return true;
272 return any(Slots[Stage].Mask & InstMask);
273 }
274
275 /// Find next stage where the instruction class is allowed.
276 std::optional<unsigned> findNextAllowedStage(CoExecMaskT InstMask,
277 unsigned FromStage) const {
278 for (unsigned I = FromStage; I < TotalWindow; ++I) {
279 if (any(Slots[I].Mask & InstMask))
280 return I;
281 }
282 return std::nullopt;
283 }
284
285 /// Return the number of cycles an instruction matching \p InstMask must wait
286 /// at \p Stage. Returns zero when no stall is required.
287 unsigned getStallCycles(CoExecMaskT InstMask, unsigned Stage) const {
288 if (canCoExec(InstMask, Stage))
289 return 0;
290
291 // Stall until the next stage that accepts the instruction.
292 if (std::optional<unsigned> Next =
293 findNextAllowedStage(InstMask, Stage + 1))
294 return *Next - Stage;
295
296 // Stall until the window ends if no later stage accepts the instruction.
297 return TotalWindow - Stage;
298 }
299
300 /// Get stage type from mask for display.
302 if (Mask == CoExecMask::StageE0)
303 return CoExecStageType::E0;
304 if (Mask == CoExecMask::StageE)
305 return CoExecStageType::E;
306 if (Mask == CoExecMask::StageIS)
307 return CoExecStageType::IS;
308 if (Mask == CoExecMask::StageI)
309 return CoExecStageType::I;
310 if (Mask == CoExecMask::StageV)
311 return CoExecStageType::V;
312 if (Mask == CoExecMask::StageTR)
313 return CoExecStageType::TR;
314 // For 'All' or unknown, return based on what's allowed.
315 if (any(Mask & CoExecMask::VALU))
316 return CoExecStageType::I; // If VALU allowed, it's I-like
317 if (any(Mask & CoExecMask::WMMA))
318 return CoExecStageType::V; // If WMMA allowed (not VALU), V-like
319 return CoExecStageType::E; // Default to E
320 }
321
322 /// Get stage type for a specific stage.
323 CoExecStageType getType(unsigned Stage) const {
324 return getStageType(getMask(Stage));
325 }
326
327 /// Build a CoExecInfo from an occupancy and stage pattern.
328 static CoExecInfo build(unsigned UnitOccupancy, unsigned TotalWindow,
329 const char *Pattern);
330};
331
332//===----------------------------------------------------------------------===//
333// Co-execution Info Construction
334//===----------------------------------------------------------------------===//
335
336/// Build CoExecInfo from a pattern string.
337/// Pattern chars: '0'=E0, 'E'=External, 'I'=Internal, 'V'=Vacant,
338/// 'S'=Internal+ScaleWMMAAbsorb (I plus next scaled WMMA),
339/// 'T'=TRANS co-exec (all except TRANS), 'A'=Any
341 unsigned TotalWindow, const char *Pattern) {
342 CoExecInfo Info;
343 Info.UnitOccupancy = UnitOccupancy;
344 Info.TotalWindow = TotalWindow;
345 Info.Pattern = Pattern;
346 assert(Info.Pattern.size() == TotalWindow &&
347 "Pattern must describe every cycle of the co-execution window");
348 assert(TotalWindow <= MaxCoExecStages && "Co-execution window is too long");
349
350 for (unsigned I = 0; I < Info.TotalWindow; ++I) {
351 switch (Pattern[I]) {
352 case '0':
353 Info.Slots[I].Mask = CoExecMask::StageE0;
354 break;
355 case 'E':
356 Info.Slots[I].Mask = CoExecMask::StageE;
357 break;
358 case 'I':
359 Info.Slots[I].Mask = CoExecMask::StageI;
360 break;
361 case 'S':
362 Info.Slots[I].Mask = CoExecMask::StageIS;
363 break;
364 case 'V':
365 Info.Slots[I].Mask = CoExecMask::StageV;
366 break;
367 case 'T':
368 Info.Slots[I].Mask = CoExecMask::StageTR;
369 break;
370 case 'A':
371 default:
372 Info.Slots[I].Mask = CoExecMask::All;
373 break;
374 }
375 }
376 return Info;
377}
378
379/// Get co-execution info for a gfx942 or gfx950 MFMA instruction.
380CoExecInfo getMFMACoExecInfo(unsigned Opcode, const GCNSubtarget &ST);
381
382/// Get co-execution info for a WMMA instruction, selecting the per-cycle slot
383/// pattern from the opcode (and operand formats for the F8F6F4 variants).
385 const SIInstrInfo &TII) {
386 unsigned Opc = MI.getOpcode();
387 const WMMAInstInfo *InstInfo = getWMMAInstInfoHelper(Opc);
388 WMMAVariant Variant =
389 InstInfo ? InstInfo->CoExecVariant : WMMAVariant::Unknown;
390
391 if (TII.isMFMA(Opc))
392 return getMFMACoExecInfo(Opc, TII.getSubtarget());
393
394 // Scaled variants (LD_SCALE rule) absorb the next WMMA in the last I slot.
395 bool HasScaling = InstInfo && InstInfo->HasMatrixScale;
396
397 // The F8F6F4 family is the only WMMA carrying matrix format operands, and its
398 // window depends on them: both inputs f4 issue in 4 cycles, anything wider in
399 // 8. This matches the PredIsNotBothF4_WMMA_SCALE latency variant.
400 if (const MachineOperand *FmtA =
401 TII.getNamedOperand(MI, AMDGPU::OpName::matrix_a_fmt)) {
402 const MachineOperand *FmtB =
403 TII.getNamedOperand(MI, AMDGPU::OpName::matrix_b_fmt);
404 bool BothF4 = FmtB && FmtA->getImm() == AMDGPU::WMMA::MATRIX_FMT_FP4 &&
406 if (BothF4)
408 }
409
410 switch (Variant) {
411 // 16x16x64 IU8: 16-cycle occupancy, 17-cycle window.
413 return CoExecInfo::build(16, 17, "0EIIEEIIEEIIEEIIV");
414
415 // 16x16x128 F8/F6/F4 has occupancy of 8 cycles and a window of 10 cycles.
417 return CoExecInfo::build(8, 10, HasScaling ? "0EEIEEISVV" : "0EEIEEIIVV");
418
419 // 16x16x128 with two F4 inputs has occupancy of 4 cycles and a window of 6
420 // cycles.
422 return CoExecInfo::build(4, 6, HasScaling ? "0EESVV" : "0EEIVV");
423
424 // 16x16x64 FP8/BF8: 4-cycle occupancy, 6-cycle window.
426 return CoExecInfo::build(4, 6, "0EEIVV");
427
428 // 16x16x32 F16/BF16: 8-cycle occupancy, 9-cycle window.
430 return CoExecInfo::build(8, 9, "0EIIEEIIV");
431
432 // 16x16x128 FP8/BF8: 8-cycle occupancy, 10-cycle window.
434 return CoExecInfo::build(8, 10, "0EEIEEIIVV");
435
436 // 32x16x128 F4: 8-cycle occupancy, 10-cycle window.
438 return CoExecInfo::build(8, 10, HasScaling ? "0EEIEIESVV" : "0EEIEIEIVV");
439
441 // Permissive window for variants without a modeled slot pattern.
442 return CoExecInfo::build(0, 9, "AAAAAAAAA");
443 }
444 llvm_unreachable("unknown WMMA variant");
445}
446
447} // namespace AMDGPU
448} // namespace llvm
449
450#endif // LLVM_LIB_TARGET_AMDGPU_AMDGPUCOEXECINFO_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
const AArch64InstrInfo & TII
IRTranslator LLVM IR MI
unsigned Opc
#define F(x, y, z)
Definition MD5.cpp:54
#define T
Interface definition for SIInstrInfo.
Representation of each machine instruction.
MachineOperand class - Representation of each machine instruction operand.
int64_t getImm() const
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
CoExecMask
Bitmask for instruction types allowed to co-execute at a stage.
const char * getCoExecMaskName(CoExecMaskT Mask)
Return a human-readable name for a mask holding a single instruction class, as produced by getCoExecM...
InstructionFlavor
Classification of instructions by execution characteristics.
constexpr unsigned MaxCoExecStages
Max stages: INT8 16x16x64 = 17 cycles, round up for safety.
const char * getStageTypeName(CoExecStageType T)
CoExecInfo getMFMACoExecInfo(unsigned Opcode, const GCNSubtarget &ST)
Get co-execution info for a gfx942 or gfx950 MFMA instruction.
CoExecMask CoExecMaskT
CoExecStageType
Stage type for co-execution (for annotation/display).
CoExecInfo getCoExecInfo(const MachineInstr &MI, const SIInstrInfo &TII)
Get co-execution info for a WMMA instruction, selecting the per-cycle slot pattern from the opcode (a...
InstructionFlavor classifyFlavor(const MachineInstr &MI, const SIInstrInfo &SII)
Classify MI into the execution flavor that drives both the scheduler's slot preferences and the hazar...
WMMAVariant
Normalized WMMA or SWMMAC family used to select co-execution rules.
CoExecMaskT getCoExecMask(InstructionFlavor F)
Map a flavor to the co-execution class it occupies in a window slot.
StringRef getFlavorName(InstructionFlavor F)
This is an optimization pass for GlobalISel generic memory operations.
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Next
Definition InstrProf.h:147
Co-execution characteristics for a multi-cycle instruction.
CoExecStageType getType(unsigned Stage) const
Get stage type for a specific stage.
CoExecMaskT getMask(unsigned Stage) const
Get capability mask for a stage.
unsigned UnitOccupancy
Number of cycles for which the producing instruction occupies its execution unit.
unsigned TotalWindow
Number of cycles in the co-execution window, counting any trailing vacant stages.
unsigned getStallCycles(CoExecMaskT InstMask, unsigned Stage) const
Return the number of cycles an instruction matching InstMask must wait at Stage.
bool canCoExec(CoExecMaskT InstMask, unsigned Stage) const
Check if an instruction class mask can co-execute at a given stage.
StringRef Pattern
Pattern string for display (e.g., "0EIIEEIIV").
static CoExecInfo build(unsigned UnitOccupancy, unsigned TotalWindow, const char *Pattern)
Build a CoExecInfo from an occupancy and stage pattern.
CoExecInfo()
Default constructor - initialize to safe defaults.
std::optional< unsigned > findNextAllowedStage(CoExecMaskT InstMask, unsigned FromStage) const
Find next stage where the instruction class is allowed.
static CoExecStageType getStageType(CoExecMaskT Mask)
Get stage type from mask for display.
CoExecSlotInfo Slots[MaxCoExecStages]
Per-stage slot info (capability mask).
Per-slot info: which instruction classes may co-execute here.