LLVM 24.0.0git
GCNVOPDUtils.cpp
Go to the documentation of this file.
1//===- GCNVOPDUtils.cpp - GCN VOPD Utils ------------------------===//
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 This file contains the AMDGPU DAG scheduling
10/// mutation to pair VOPD instructions back to back. It also contains
11// subroutines useful in the creation of VOPD instructions
12//
13//===----------------------------------------------------------------------===//
14
15#include "GCNVOPDUtils.h"
16#include "AMDGPUSubtarget.h"
17#include "GCNSubtarget.h"
18#include "SIInstrInfo.h"
30#include "llvm/MC/MCInst.h"
32
33using namespace llvm;
34
35#define DEBUG_TYPE "gcn-vopd-utils"
36
37// Return the register class of the VOPDOpc operand named
38// src/vsrc<SrcIdx><CompIdx>, which is the slot src<SrcIdx> of MI<CompIdx> maps
39// to.
41 int VOPDOpc,
42 unsigned CompIdx,
43 unsigned SrcIdx) {
44 using namespace AMDGPU;
45 int OpIdx = -1;
46 const bool IsX = CompIdx == VOPD::X;
47 switch (SrcIdx) {
48 case 0:
49 OpIdx = getNamedOperandIdx(VOPDOpc, IsX ? OpName::src0X : OpName::src0Y);
50 break;
51 case 1:
52 OpIdx = getNamedOperandIdx(VOPDOpc, IsX ? OpName::vsrc1X : OpName::vsrc1Y);
53 break;
54 case 2:
55 OpIdx = getNamedOperandIdx(VOPDOpc, IsX ? OpName::vsrc2X : OpName::vsrc2Y);
56 if (OpIdx == -1)
57 OpIdx = getNamedOperandIdx(VOPDOpc, IsX ? OpName::src2X : OpName::src2Y);
58 break;
59 default:
60 llvm_unreachable("unexpected VOPD source index");
61 }
62
63 assert(OpIdx != -1);
64 return TII.getRegClass(TII.get(VOPDOpc), OpIdx);
65}
66
67// Check if physical register from src<SrcIdx> operand of MI<CompIdx> matches
68// register class constraints in corresponding VOPDOpc operand with name
69// src/vsrc<SrcIdx><CompIdx>.
70static bool isValidVOPDSrc(const SIInstrInfo &TII, int VOPDOpc,
71 unsigned CompIdx, unsigned SrcIdx,
72 Register PhysSrcReg) {
73 return getVOPDSrcRegClass(TII, VOPDOpc, CompIdx, SrcIdx)
74 ->contains(PhysSrcReg);
75}
76
78 AMDGPU::OpName Name) {
79 return MI.getOperand(getNamedOperandIdx(MI.getOpcode(), Name));
80}
81
82// Check if MI is a VOP3P instruction with operands that satisfy the constraints
83// for mapping it to a VOP2/VOPD opcode: no modifiers, no clamp, src1 and src2
84// are registers (src0 can be register or literal), and src2 is same as dst.
85static bool canMapVOP3PToVOPD(const MachineInstr &MI) {
86 unsigned Opc = MI.getOpcode();
87 if (Opc != AMDGPU::V_DOT2_F32_F16 && Opc != AMDGPU::V_DOT2_F32_BF16)
88 return false;
89 // src0 can be register or literal
90 if (getNamedOp(MI, AMDGPU::OpName::src0_modifiers).getImm() !=
92 return false;
93 if (getNamedOp(MI, AMDGPU::OpName::src1_modifiers).getImm() !=
95 return false;
96 if (!getNamedOp(MI, AMDGPU::OpName::src1).isReg())
97 return false;
98 if (getNamedOp(MI, AMDGPU::OpName::src2_modifiers).getImm() !=
100 return false;
101 if (!getNamedOp(MI, AMDGPU::OpName::src2).isReg())
102 return false;
103 if (getNamedOp(MI, AMDGPU::OpName::clamp).getImm() != 0)
104 return false;
105 return getNamedOp(MI, AMDGPU::OpName::vdst).getReg() ==
106 getNamedOp(MI, AMDGPU::OpName::src2).getReg();
107}
108
109// In a VOPD3 whose OPX is a 64-bit operation, an OPY VGPR source operand reads
110// back the wrong value if it is the last VGPR the wave owns.
111// A wave always owns a whole number of VGPR allocation granules, so only a
112// register just below a granule boundary can be the last one. The wave also
113// owns at least as many VGPRs as this function uses, so a source which has a
114// register above it in use here cannot be the last one either.
115// The number of VGPRs the wave is actually given is not available until the
116// assembler has seen the whole module, but that can only come out above this
117// function's own usage, so this is conservatively correct.
119 const MachineInstr &MIX,
120 const MachineInstr &MIY) {
121 const MachineFunction &MF = *MIX.getMF();
122 const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>();
123 if (!ST.hasVOPD3F64OPYSrcHazard())
124 return false;
125
126 // Every 64-bit VOPD3 OPX opcode has a 64-bit vdst, and no 32-bit one has.
127 int VDstIdx =
128 AMDGPU::getNamedOperandIdx(MIX.getOpcode(), AMDGPU::OpName::vdst);
129 assert(VDstIdx != -1 && "VOPD3 OPX component must have a vdst");
130 if (TII.getOpSize(MIX, VDstIdx) != 8)
131 return false;
132
133 unsigned Granule =
134 AMDGPU::getVGPRAllocGranule(ST.getTargetID().getGPUKind(), ST.isWave32());
135 const SIRegisterInfo *TRI = ST.getRegisterInfo();
136 unsigned NumVGPRs = TRI->getNumUsedPhysRegs(
137 MF.getRegInfo(), AMDGPU::VGPR_32RegClass, /*IncludeCalls=*/false);
138 for (AMDGPU::OpName Name :
139 {AMDGPU::OpName::src0, AMDGPU::OpName::src1, AMDGPU::OpName::src2}) {
140 const MachineOperand *Src = TII.getNamedOperand(MIY, Name);
141 // Every OPY source which can be a VGPR is 32 bits wide.
142 if (!Src || !Src->isReg() ||
143 !AMDGPU::VGPR_32RegClass.contains(Src->getReg()))
144 continue;
145 unsigned Idx = TRI->getHWRegIndex(Src->getReg());
146 if ((Idx + 1) % Granule == 0 && Idx + 1 >= NumVGPRs)
147 return true;
148 }
149 return false;
150}
151
153 // A free register cannot be found without liveness. A move also makes the
154 // code longer, so a function which asked for small code keeps its literals.
155 return MF.getProperties().hasTracksLiveness() &&
156 !MF.getFunction().hasOptSize();
157}
158
159static bool
161 const MachineInstr &MIY, bool IsVOPD3,
162 bool AllowSameVGPR,
163 SmallVectorImpl<VOPDLiteralFixup> &LiteralFixups) {
164 namespace VOPD = AMDGPU::VOPD;
165
166 const MachineFunction *MF = MIX.getMF();
167 const GCNSubtarget &ST = MF->getSubtarget<GCNSubtarget>();
168
169 if (IsVOPD3 && !ST.hasVOPD3())
170 return false;
171 if (IsVOPD3 && isVOPD3F64OPYSrcHazard(TII, MIX, MIY))
172 return false;
173 if (!IsVOPD3 && ((TII.isVOP3(MIX) && !canMapVOP3PToVOPD(MIX)) ||
174 (TII.isVOP3(MIY) && !canMapVOP3PToVOPD(MIY))))
175 return false;
176 if (TII.isDPP(MIX) || TII.isDPP(MIY))
177 return false;
178
179 // Collected here and handed over only on success, so a failed check cannot
180 // leave anything behind.
182
183 const SIRegisterInfo *TRI = ST.getRegisterInfo();
184 const MachineRegisterInfo &MRI = MF->getRegInfo();
185 // Literals also count against scalar bus limit
187 auto AddLiteral = [&](const MachineOperand &Op) {
188 for (auto &Literal : UniqueLiterals) {
189 if (Literal->isIdenticalTo(Op))
190 return;
191 }
192 UniqueLiterals.push_back(&Op);
193 };
194 // Immediates which the caller will move into a scalar register. Identical
195 // values share one register, so they count like one scalar operand each.
196 SmallSet<int32_t, 2> MaterializedLiterals;
197 SmallSet<Register, 4> UniqueScalarRegs;
198
199 unsigned EncodingFamily = AMDGPU::getVOPDEncodingFamily(ST);
200 unsigned XOpc = AMDGPU::getVOPDOpcode(MIX.getOpcode(), IsVOPD3);
201 unsigned YOpc = AMDGPU::getVOPDOpcode(MIY.getOpcode(), IsVOPD3);
202 int VOPDOpc = AMDGPU::getVOPDFull(XOpc, YOpc, EncodingFamily, IsVOPD3);
203 assert(VOPDOpc != -1);
204
205 auto InstInfo = AMDGPU::getVOPDInstInfo(MIX.getDesc(), MIY.getDesc());
206
207 for (auto CompIdx : VOPD::COMPONENTS) {
208 const MachineInstr &MI = (CompIdx == VOPD::X) ? MIX : MIY;
209
210 const MachineOperand &Src0 = *TII.getNamedOperand(MI, AMDGPU::OpName::src0);
211 if (Src0.isReg()) {
212 if (!isValidVOPDSrc(TII, VOPDOpc, CompIdx, 0, Src0.getReg()))
213 return false;
214 if (TII.regUsesConstantBus(Src0, MRI))
215 UniqueScalarRegs.insert(Src0.getReg());
216 } else if (!TII.isInlineConstant(Src0)) {
217 if (!IsVOPD3) {
218 AddLiteral(Src0);
219 } else {
220 // A VOPD3 component cannot encode a literal, but src0 can read a
221 // scalar register. The pair is therefore still possible if the caller
222 // moves the value into one.
223 if (!canMaterializeVOPDLiterals(*MF) || !Src0.isImm())
224 return false;
225 // Only a 32-bit slot is handled, because the caller produces the value
226 // with a single S_MOV_B32.
227 int OpIdx = getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::src0);
228 if (TII.getOpSize(MI, OpIdx) != 4)
229 return false;
230 const TargetRegisterClass *SlotRC =
231 TRI->getCommonSubClass(getVOPDSrcRegClass(TII, VOPDOpc, CompIdx, 0),
232 &AMDGPU::SGPR_32RegClass);
233 if (!SlotRC)
234 return false;
235 // Only the low bits reach the register, so two operands which name
236 // the same value share one move whichever way they were written.
237 int32_t Imm = static_cast<int32_t>(Src0.getImm());
238 MaterializedLiterals.insert(Imm);
239 Fixups.push_back({CompIdx, static_cast<unsigned>(OpIdx), Imm, SlotRC});
240 }
241 }
242
243 // V_FMAMK_F32 (src1) and V_FMAAK_F32 (src2) have a mandatory literal.
244 // VOPD3 instructions don't set MandatoryLiteralIdx.
245 if (InstInfo[CompIdx].hasMandatoryLiteral()) {
246 auto CompOprIdx = InstInfo[CompIdx].getMandatoryLiteralCompOperandIndex();
247 AddLiteral(MI.getOperand(CompOprIdx));
248 }
249
250 // VOPD only. Affects V_CNDMASK_B32_e32.
251 if (MI.getDesc().hasImplicitUseOfPhysReg(AMDGPU::VCC))
252 UniqueScalarRegs.insert(AMDGPU::VCC_LO);
253
254 if (const MachineOperand *Src1 =
255 TII.getNamedOperand(MI, AMDGPU::OpName::src1)) {
256 if (Src1->isReg()) {
257 if (!isValidVOPDSrc(TII, VOPDOpc, CompIdx, 1, Src1->getReg()))
258 return false;
259 assert(TRI->isVectorRegister(MRI, Src1->getReg()));
260 } else if (IsVOPD3) {
261 return false;
262 }
263 }
264
265 if (IsVOPD3) {
266 if (const MachineOperand *Src2 =
267 TII.getNamedOperand(MI, AMDGPU::OpName::src2)) {
268 if (AMDGPU::hasNamedOperand(MI.getOpcode(), AMDGPU::OpName::bitop3)) {
269 // BITOP3 can be converted to DUAL_BITOP2 when src2 is zero.
270 if (!Src2->isImm() || Src2->getImm())
271 return false;
272 } else {
273 if (!Src2->isReg())
274 return false;
275 if (!isValidVOPDSrc(TII, VOPDOpc, CompIdx, 2, Src2->getReg()))
276 return false;
277 if (TII.regUsesConstantBus(*Src2, MRI)) {
278 assert(MI.getOpcode() == AMDGPU::V_CNDMASK_B32_e64);
279 UniqueScalarRegs.insert(Src2->getReg());
280 }
281 }
282 }
283 for (auto OpName : {AMDGPU::OpName::clamp, AMDGPU::OpName::omod,
284 AMDGPU::OpName::op_sel}) {
285 if (TII.hasModifiersSet(MI, OpName))
286 return false;
287 }
288
289 // Neg is allowed, other modifiers are not. NB: even though sext has the
290 // same value as neg, there are no combinable instructions with sext.
291 for (auto OpName :
292 {AMDGPU::OpName::src0_modifiers, AMDGPU::OpName::src1_modifiers,
293 AMDGPU::OpName::src2_modifiers}) {
294 const MachineOperand *Mods = TII.getNamedOperand(MI, OpName);
295 if (Mods && (Mods->getImm() & ~SISrcMods::NEG))
296 return false;
297 }
298 }
299 }
300
301 if (UniqueLiterals.size() > 1)
302 return false;
303 // Keep materialization pair-local and do not increase instruction count or
304 // register pressure by producing two different values for one pair.
305 if (MaterializedLiterals.size() > 1)
306 return false;
307 if ((UniqueLiterals.size() + MaterializedLiterals.size() +
308 UniqueScalarRegs.size()) > 2)
309 return false;
310
311 auto GetVRegIdx = [&](unsigned OpcodeIdx, unsigned OperandIdx) {
312 const MachineInstr &MI = (OpcodeIdx == VOPD::X) ? MIX : MIY;
313 const MachineOperand &Operand = MI.getOperand(OperandIdx);
314 if (Operand.isReg() && TRI->isVectorRegister(MRI, Operand.getReg()))
315 return Operand.getReg();
316 return Register();
317 };
318
319 // On GFX1170+ if both OpX and OpY are V_MOV_B32 then OPY uses SRC2
320 // source-cache.
321 bool SkipSrc = (ST.hasGFX11_7Insts() || ST.hasGFX12Insts()) &&
322 MIX.getOpcode() == AMDGPU::V_MOV_B32_e32 &&
323 MIY.getOpcode() == AMDGPU::V_MOV_B32_e32;
324
325 // Check VGPR bank constraints for operand registers across both instructions.
326 if (InstInfo.hasInvalidOperand(GetVRegIdx, *TRI, SkipSrc, AllowSameVGPR,
327 IsVOPD3, ST.hasVOPDInterlockHazard()))
328 return false;
329
330 LLVM_DEBUG(dbgs() << "VOPD Reg Constraints Passed\n\tX: " << MIX
331 << "\n\tY: " << MIY << "\n");
332 LiteralFixups.assign(Fixups);
333 return true;
334}
335
336/// Core pair-eligibility check for a single VOPD encoding variant (VOPD or
337/// VOPD3). Returns the X/Y assignment on success, or std::nullopt otherwise.
338static std::optional<VOPDMatchInfo>
339tryMatchVOPDPairVariant(const SIInstrInfo &TII, unsigned EncodingFamily,
340 MachineInstr &FirstMI, MachineInstr &SecondMI,
341 bool IsVOPD3) {
342 unsigned Opc = FirstMI.getOpcode();
343 unsigned Opc2 = SecondMI.getOpcode();
344 AMDGPU::CanBeVOPD FirstCanBeVOPD =
345 AMDGPU::getCanBeVOPD(Opc, EncodingFamily, IsVOPD3);
346 AMDGPU::CanBeVOPD SecondCanBeVOPD =
347 AMDGPU::getCanBeVOPD(Opc2, EncodingFamily, IsVOPD3);
348
349 if (!(FirstCanBeVOPD.X && SecondCanBeVOPD.Y) &&
350 !(FirstCanBeVOPD.Y && SecondCanBeVOPD.X))
351 return std::nullopt;
352
353 // If SecondMI depends on FirstMI they cannot execute at the same time.
354 if (TII.hasRAWDependency(FirstMI, SecondMI))
355 return std::nullopt;
356
357 const GCNSubtarget &ST = TII.getSubtarget();
358 bool AllowSameVGPR = ST.hasGFX12Insts();
359
360 // Only a VOPD3 component can need a fixup; a plain one may hold a literal.
361 // checkVOPDRegConstraints() only writes this when it succeeds.
363
364 if (FirstCanBeVOPD.X && SecondCanBeVOPD.Y) {
365 if (checkVOPDRegConstraints(TII, FirstMI, SecondMI, IsVOPD3, AllowSameVGPR,
366 Fixups))
367 return VOPDMatchInfo{
368 {&FirstMI, &SecondMI}, 0, IsVOPD3, std::move(Fixups)};
369 }
370
371 if (FirstCanBeVOPD.Y && SecondCanBeVOPD.X) {
372 // AllowSameVGPR relaxes the VGPR bank overlap check for source operands.
373 // Only enable it when there is no antidependency.
374 bool IsAntiDep = TII.hasRAWDependency(SecondMI, FirstMI);
375 AllowSameVGPR &= !IsAntiDep;
376 if (IsAntiDep && !TII.isVOPDAntidependencyAllowed(SecondMI))
377 return std::nullopt;
378 if (checkVOPDRegConstraints(TII, SecondMI, FirstMI, IsVOPD3, AllowSameVGPR,
379 Fixups))
380 return VOPDMatchInfo{
381 {&FirstMI, &SecondMI}, 1, IsVOPD3, std::move(Fixups)};
382 }
383
384 return std::nullopt;
385}
386
387std::optional<VOPDMatchInfo> llvm::tryMatchVOPDPair(const SIInstrInfo &TII,
388 MachineInstr &FirstMI,
389 MachineInstr &SecondMI) {
390 const GCNSubtarget &ST = TII.getSubtarget();
391 unsigned EncodingFamily = AMDGPU::getVOPDEncodingFamily(ST);
392 if (auto Match = tryMatchVOPDPairVariant(TII, EncodingFamily, FirstMI,
393 SecondMI, /*IsVOPD3=*/false))
394 return Match;
395 if (ST.hasVOPD3())
396 return tryMatchVOPDPairVariant(TII, EncodingFamily, FirstMI, SecondMI,
397 /*IsVOPD3=*/true);
398 return std::nullopt;
399}
400
401/// Check if the instr pair, FirstMI and SecondMI, should be scheduled
402/// together. Given SecondMI, when FirstMI is unspecified, then check if
403/// SecondMI may be part of a fused pair at all.
405 const TargetSubtargetInfo &TSI,
406 const MachineInstr *FirstMI,
407 const MachineInstr &SecondMI,
408 const SDep *) {
409 const SIInstrInfo &STII = static_cast<const SIInstrInfo &>(TII);
410 const GCNSubtarget &ST = STII.getSubtarget();
411
412 // One instruction case: just check whether SecondMI is eligible at all.
413 if (!FirstMI) {
414 unsigned EncodingFamily = AMDGPU::getVOPDEncodingFamily(ST);
415 unsigned Opc2 = SecondMI.getOpcode();
416 auto CheckCanBeVOPD = [&](bool VOPD3) {
417 AMDGPU::CanBeVOPD CanBeVOPD =
418 AMDGPU::getCanBeVOPD(Opc2, EncodingFamily, VOPD3);
419 return CanBeVOPD.Y || CanBeVOPD.X;
420 };
421 return CheckCanBeVOPD(false) || (ST.hasVOPD3() && CheckCanBeVOPD(true));
422 }
423
424#ifdef EXPENSIVE_CHECKS
425 assert([&]() -> bool {
426 for (auto MII = MachineBasicBlock::const_iterator(FirstMI);
427 MII != FirstMI->getParent()->instr_end(); ++MII) {
428 if (&*MII == &SecondMI)
429 return true;
430 }
431 return false;
432 }() && "Expected FirstMI to precede SecondMI");
433#endif
434
435 return tryMatchVOPDPair(STII, *const_cast<MachineInstr *>(FirstMI),
436 const_cast<MachineInstr &>(SecondMI))
437 .has_value();
438}
439
440/// Collect all load (dependents if \p Forward else dependencies) that connect
441/// to the \p Head SU.
442/// \p Visited should allocate enough bits for the number of SUnits, but its
443/// value can otherwise be uninitialized.
444static void collectLoads(SmallPtrSet<SUnit *, 8> &Loads, BitVector &Visited,
445 SUnit &Head, bool Forward, bool StopAtLoads) {
446 if (Head.isBoundaryNode())
447 return;
448
449 Visited.reset();
450
452 Stack.push_back(&Head);
453 while (!Stack.empty()) {
454 SUnit *SU = Stack.pop_back_val();
455 const SmallVector<SDep, 4> &Deps = Forward ? SU->Succs : SU->Preds;
456 for (const SDep &Edge : Deps) {
457 if (StopAtLoads && Edge.getKind() != SDep::Data)
458 continue;
459 SUnit *Dep = Edge.getSUnit();
460 if (Dep->isBoundaryNode() || Visited.test(Dep->NodeNum))
461 continue;
462 Visited.set(Dep->NodeNum);
463
464 if (Dep->isInstr() && Dep->getInstr()->mayLoad()) {
465 Loads.insert(Dep);
466 if (StopAtLoads)
467 continue;
468 }
469 Stack.push_back(Dep);
470 }
471 }
472}
473
474/// Checks whether fusing SU \p I with SU \p J would force the loads preceding
475/// \p J to complete before loads depending on \p I.
476///
477/// \p ILoadSuccs should hold all first load successors of \p I (via
478/// collectLoads with StopAtLoads=true). For set bits in \p LoadPredsComputed,
479/// the corresponding set in \p LoadPredsCache should hold all transitive load
480/// dependencies (via collectLoads with StopAtLoads=false). The \p Scratch
481/// bitvector should allocate enough bits for the number of SUnits.
482static bool loadsMayOverlap(
483 [[maybe_unused]] SUnit &I, const SmallPtrSet<SUnit *, 8> &ILoadSuccs,
484 SUnit &J, BitVector &LoadPredsComputed,
485 SmallVector<SmallPtrSet<SUnit *, 8>> &LoadPredsCache, BitVector &Scratch) {
486
487 if (ILoadSuccs.empty())
488 return false;
489
490 SmallPtrSet<SUnit *, 8> &JLoadPreds = LoadPredsCache[J.NodeNum];
491 if (!LoadPredsComputed.test(J.NodeNum)) {
492 collectLoads(JLoadPreds, Scratch, J, /*Forward=*/false,
493 /*StopAtLoads=*/true);
494 LoadPredsComputed.set(J.NodeNum);
495 }
496 if (JLoadPreds.empty())
497 return false;
498
499 for (SUnit *ILoad : ILoadSuccs) {
500 SmallPtrSet<SUnit *, 8> &ILoadDeps = LoadPredsCache[ILoad->NodeNum];
501 if (!LoadPredsComputed.test(ILoad->NodeNum)) {
502 collectLoads(ILoadDeps, Scratch, *ILoad, /*Forward=*/false,
503 /*StopAtLoads=*/false);
504 LoadPredsComputed.set(ILoad->NodeNum);
505 }
506
507 for (SUnit *JLoad : JLoadPreds) {
508 if (ILoad == JLoad) {
510 dbgs() << "Will not pair SU(" << I.NodeNum << ") with SU("
511 << J.NodeNum << ")\n"
512 << " Fusion would introduce a cyclic dependency with SU("
513 << ILoad->NodeNum << ")\n");
514 return true;
515 }
516
517 if (!ILoadDeps.contains(JLoad)) {
518 LLVM_DEBUG(dbgs() << "Will not pair SU(" << I.NodeNum << ") with SU("
519 << J.NodeNum << ")\n"
520 << " Fusion may force SU(" << JLoad->NodeNum
521 << ") to complete its load before dispatching SU("
522 << ILoad->NodeNum << ")\n");
523 return true;
524 }
525 }
526 }
527 return false;
528}
529
530namespace {
531/// Adapts design from MacroFusion
532/// Puts valid candidate instructions back-to-back so they can easily
533/// be turned into VOPD instructions
534/// Greedily pairs instruction candidates. O(n^2) algorithm.
535struct VOPDPairingMutation : ScheduleDAGMutation {
536 MacroFusionPredTy shouldScheduleAdjacent; // NOLINT: function pointer
537
538 VOPDPairingMutation(
539 MacroFusionPredTy shouldScheduleAdjacent) // NOLINT: function pointer
541
542 void apply(ScheduleDAGInstrs *DAG) override {
543 const TargetInstrInfo &TII = *DAG->TII;
544 const GCNSubtarget &ST = DAG->MF.getSubtarget<GCNSubtarget>();
545 if (!AMDGPU::hasVOPD(ST) || !ST.isWave32()) {
546 LLVM_DEBUG(dbgs() << "Target does not support VOPDPairingMutation\n");
547 return;
548 }
549
550 BitVector VOPDCapable(DAG->SUnits.size());
551 unsigned IIdx = 0;
552 // Pre-compute whether each individual instruction can be VOPD
553 for (auto ISUI = DAG->SUnits.begin(), E = DAG->SUnits.end(); ISUI != E;
554 ++ISUI, ++IIdx) {
555 const MachineInstr *IMI = ISUI->getInstr();
556 if (shouldScheduleAdjacent(TII, ST, nullptr, *IMI, nullptr) &&
557 hasLessThanNumFused(*ISUI, 2))
558 VOPDCapable[IIdx] = true;
559 }
560
561 IIdx = 0;
562 SmallPtrSet<SUnit *, 8> ILoadSuccs;
563
564 // Cache collected load predecessors.
565 // For VOPDCapable nodes, this caches collectLoads with StopAtLoads=true
566 // For loads, this caches collectLoads with StopAtLoads=false
567 BitVector LoadPredsComputed(DAG->SUnits.size());
568 SmallVector<SmallPtrSet<SUnit *, 8>> LoadPredsCache(DAG->SUnits.size());
569
570 BitVector Scratch(DAG->SUnits.size());
571 for (auto ISUI = DAG->SUnits.begin(), E = DAG->SUnits.end(); ISUI != E;
572 ++ISUI, ++IIdx) {
573 if (!VOPDCapable[IIdx])
574 continue;
575 const MachineInstr *IMI = ISUI->getInstr();
576
577 ILoadSuccs.clear();
578 collectLoads(ILoadSuccs, Scratch, *ISUI, /*Forward=*/true,
579 /*StopAtLoads=*/true);
580
581 unsigned JIdx = IIdx + 1;
582 for (auto JSUI = ISUI + 1; JSUI != E; ++JSUI, ++JIdx) {
583 if (!VOPDCapable[JIdx] || JSUI->isBoundaryNode())
584 continue;
585 const MachineInstr *JMI = JSUI->getInstr();
586 if (!hasLessThanNumFused(*JSUI, 2) ||
587 !shouldScheduleAdjacent(TII, ST, IMI, *JMI, nullptr))
588 continue;
589
590 if (loadsMayOverlap(*ISUI, ILoadSuccs, *JSUI, LoadPredsComputed,
591 LoadPredsCache, Scratch))
592 continue;
593
594 if (fuseInstructionPair(*DAG, *ISUI, *JSUI)) {
595 // Clear to prevent future checks/fusing
596 VOPDCapable[JIdx] = false;
597 break;
598 }
599 }
600 }
601 LLVM_DEBUG(dbgs() << "Completed VOPDPairingMutation\n");
602 }
603};
604} // namespace
605
606std::unique_ptr<ScheduleDAGMutation> llvm::createVOPDPairingMutation() {
607 return std::make_unique<VOPDPairingMutation>(shouldScheduleVOPDAdjacent);
608}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static bool shouldScheduleAdjacent(const TargetInstrInfo &TII, const TargetSubtargetInfo &TSI, const MachineInstr *FirstMI, const MachineInstr &SecondMI, const SDep *Dep)
Check if the instr pair, FirstMI and SecondMI, should be fused together.
unsigned Imm
Base class for AMDGPU specific classes of TargetSubtarget.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
AMD GCN specific subclass of TargetSubtarget.
static const MachineOperand & getNamedOp(const MachineInstr &MI, AMDGPU::OpName Name)
static void collectLoads(SmallPtrSet< SUnit *, 8 > &Loads, BitVector &Visited, SUnit &Head, bool Forward, bool StopAtLoads)
Collect all load (dependents if Forward else dependencies) that connect to the Head SU.
static bool canMapVOP3PToVOPD(const MachineInstr &MI)
static const TargetRegisterClass * getVOPDSrcRegClass(const SIInstrInfo &TII, int VOPDOpc, unsigned CompIdx, unsigned SrcIdx)
static bool isVOPD3F64OPYSrcHazard(const SIInstrInfo &TII, const MachineInstr &MIX, const MachineInstr &MIY)
static bool checkVOPDRegConstraints(const SIInstrInfo &TII, const MachineInstr &MIX, const MachineInstr &MIY, bool IsVOPD3, bool AllowSameVGPR, SmallVectorImpl< VOPDLiteralFixup > &LiteralFixups)
static bool canMaterializeVOPDLiterals(const MachineFunction &MF)
static std::optional< VOPDMatchInfo > tryMatchVOPDPairVariant(const SIInstrInfo &TII, unsigned EncodingFamily, MachineInstr &FirstMI, MachineInstr &SecondMI, bool IsVOPD3)
Core pair-eligibility check for a single VOPD encoding variant (VOPD or VOPD3).
static bool loadsMayOverlap(SUnit &I, const SmallPtrSet< SUnit *, 8 > &ILoadSuccs, SUnit &J, BitVector &LoadPredsComputed, SmallVector< SmallPtrSet< SUnit *, 8 > > &LoadPredsCache, BitVector &Scratch)
Checks whether fusing SU I with SU J would force the loads preceding J to complete before loads depen...
static bool shouldScheduleVOPDAdjacent(const TargetInstrInfo &TII, const TargetSubtargetInfo &TSI, const MachineInstr *FirstMI, const MachineInstr &SecondMI, const SDep *)
Check if the instr pair, FirstMI and SecondMI, should be scheduled together.
static bool isValidVOPDSrc(const SIInstrInfo &TII, int VOPDOpc, unsigned CompIdx, unsigned SrcIdx, Register PhysSrcReg)
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
#define I(x, y, z)
Definition MD5.cpp:57
Register const TargetRegisterInfo * TRI
Promote Memory to Register
Definition Mem2Reg.cpp:110
static bool isReg(const MCInst &MI, unsigned OpNo)
Interface definition for SIInstrInfo.
This file defines the SmallVector class.
#define LLVM_DEBUG(...)
Definition Debug.h:119
bool test(unsigned Idx) const
Returns true if bit Idx is set.
Definition BitVector.h:482
BitVector & reset()
Reset all bits in the bitvector.
Definition BitVector.h:409
BitVector & set()
Set all bits in the bitvector.
Definition BitVector.h:366
bool hasOptSize() const
Optimize this function for size (-Os) or minimum size (-Oz).
Definition Function.h:699
bool contains(MCRegister Reg) const
contains - Return true if the specified register is included in this register class.
MachineInstrBundleIterator< const MachineInstr > const_iterator
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Function & getFunction()
Return the LLVM function that this machine code represents.
const MachineFunctionProperties & getProperties() const
Get the function properties.
Representation of each machine instruction.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
const MachineBasicBlock * getParent() const
bool mayLoad(QueryType Type=AnyInBundle) const
Return true if this instruction could possibly read memory.
const MCInstrDesc & getDesc() const
Returns the target instruction descriptor of this MachineInstr.
LLVM_ABI const MachineFunction * getMF() const
Return the function that contains the basic block that this instruction belongs to.
MachineOperand class - Representation of each machine instruction operand.
int64_t getImm() const
bool isReg() const
isReg - Tests if this is a MO_Register operand.
bool isImm() const
isImm - Tests if this is a MO_Immediate operand.
Register getReg() const
getReg - Returns the register number.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
Wrapper class representing virtual and physical registers.
Definition Register.h:20
Scheduling dependency.
Definition ScheduleDAG.h:52
@ Data
Regular data dependence (aka true-dependence).
Definition ScheduleDAG.h:56
const GCNSubtarget & getSubtarget() const
Scheduling unit. This is a node in the scheduling DAG.
bool isInstr() const
Returns true if this SUnit refers to a machine instruction as opposed to an SDNode.
unsigned NodeNum
Entry # of node in the node vector.
bool isBoundaryNode() const
Boundary nodes are placeholders for the boundary of the scheduling region.
SmallVector< SDep, 4 > Succs
All sunit successors.
SmallVector< SDep, 4 > Preds
All sunit predecessors.
MachineInstr * getInstr() const
Returns the representative MachineInstr for this SUnit.
const TargetInstrInfo * TII
Target instruction information.
std::vector< SUnit > SUnits
The scheduling units.
MachineFunction & MF
Machine function.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
bool contains(ConstPtrType Ptr) const
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
SmallSet - This maintains a set of unique values, optimizing for the case when the set is small (less...
Definition SmallSet.h:134
std::pair< const_iterator, bool > insert(const T &V)
insert - Insert an element into the set if it isn't already there.
Definition SmallSet.h:184
size_type size() const
Definition SmallSet.h:171
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void assign(size_type NumElts, ValueParamT Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
TargetInstrInfo - Interface to description of machine instruction set.
TargetSubtargetInfo - Generic base class for all target subtargets.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
unsigned getVOPDOpcode(unsigned Opc, bool VOPD3)
CanBeVOPD getCanBeVOPD(unsigned Opc, unsigned EncodingFamily, bool VOPD3)
LLVM_READONLY bool hasNamedOperand(uint64_t Opcode, OpName NamedIdx)
unsigned getVOPDEncodingFamily(const MCSubtargetInfo &ST)
LLVM_ABI unsigned getVGPRAllocGranule(GPUKind AK, bool IsWave32)
VOPD::InstInfo getVOPDInstInfo(const MCInstrDesc &OpX, const MCInstrDesc &OpY)
bool hasVOPD(const MCSubtargetInfo &STI)
int getVOPDFull(unsigned OpX, unsigned OpY, unsigned EncodingFamily, bool VOPD3)
void apply(Opt *O, const Mod &M, const Mods &... Ms)
This is an optimization pass for GlobalISel generic memory operations.
std::unique_ptr< ScheduleDAGMutation > createVOPDPairingMutation()
LLVM_ABI bool fuseInstructionPair(ScheduleDAGInstrs &DAG, SUnit &FirstSU, SUnit &SecondSU)
Create an artificial edge between FirstSU and SecondSU.
MachineInstr * getImm(const MachineOperand &MO, const MachineRegisterInfo *MRI)
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
DWARFExpression::Operation Op
std::optional< VOPDMatchInfo > tryMatchVOPDPair(const SIInstrInfo &TII, MachineInstr &FirstMI, MachineInstr &SecondMI)
Check whether FirstMI and SecondMI can be combined into a VOPD instruction.
bool(*)(const TargetInstrInfo &TII, const TargetSubtargetInfo &STI, const MachineInstr *FirstMI, const MachineInstr &SecondMI, const SDep *Dep) MacroFusionPredTy
Check if the instr pair, FirstMI and SecondMI, should be fused together, based on the dependency betw...
Definition MacroFusion.h:35
LLVM_ABI bool hasLessThanNumFused(const SUnit &SU, unsigned FuseLimit)
Checks if the number of cluster edges between SU and its predecessors is less than FuseLimit.
MCRegisterClass TargetRegisterClass
Definition FastISel.h:58
Describes a matched VOPD pair.