Skip to content

Commit 752678f

Browse files
committed
[AMDGPU] Fix AGPR_32 reg assign for mfma scale ops
In MFMA rewrite pass, prevent AGPR_32 reg class assignment for scale operands, not permitted by instruction format.
1 parent 8f6a28b commit 752678f

File tree

2 files changed

+27
-8
lines changed

2 files changed

+27
-8
lines changed

llvm/lib/Target/AMDGPU/AMDGPURewriteAGPRCopyMFMA.cpp

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@ class AMDGPURewriteAGPRCopyMFMAImpl {
9999

100100
/// Compute the register class constraints based on the uses of \p Reg,
101101
/// excluding MFMA uses from which can be rewritten to change the register
102-
/// class constraint. This should be nearly identical to
103-
/// MachineRegisterInfo::recomputeRegClass.
102+
/// class constraint. MFMA scale operands need to be constraint checked.
103+
/// This should be nearly identical to MachineRegisterInfo::recomputeRegClass.
104104

105105
/// \p RewriteCandidates will collect the set of MFMA instructions that need
106106
/// to have the opcode mutated to perform the replacement.
@@ -154,9 +154,26 @@ bool AMDGPURewriteAGPRCopyMFMAImpl::recomputeRegClassExceptRewritable(
154154

155155
// We can swap the classes of dst + src2 as a pair to AGPR, so ignore the
156156
// effects of rewrite candidates. It just so happens that we can use
157-
// either AGPR or VGPR in src0/src1, so don't bother checking the
158-
// constraint effects of the individual operands.
157+
// either AGPR or VGPR in src0/src1. We still need to check constraint
158+
// effects for scale variant, which does not allow AGPR.
159159
if (isRewriteCandidate(*MI)) {
160+
161+
int AGPROp = AMDGPU::getMFMASrcCVDstAGPROp(MI->getOpcode());
162+
MachineInstrBuilder TmpMIB =
163+
BuildMI(*MI->getParent(), MI->getIterator(), MI->getDebugLoc(),
164+
TII.get(AGPROp));
165+
for (const MachineOperand &TmpMO : MI->operands())
166+
TmpMIB.add(TmpMO);
167+
MachineInstr *TmpMI = TmpMIB.getInstr();
168+
unsigned OpNo = &MO - &MI->getOperand(0);
169+
const TargetRegisterClass *EquivalentAGPRRegClass =
170+
TRI.getEquivalentAGPRClass(MRI.getRegClass(Reg));
171+
const TargetRegisterClass *Allowed = TmpMI->getRegClassConstraintEffect(
172+
OpNo, EquivalentAGPRRegClass, &TII, &TRI);
173+
TmpMI->eraseFromParent();
174+
if (!Allowed || Allowed != EquivalentAGPRRegClass)
175+
return false;
176+
160177
const MachineOperand *VDst =
161178
TII.getNamedOperand(*MI, AMDGPU::OpName::vdst);
162179
const MachineOperand *Src2 =

llvm/test/CodeGen/AMDGPU/rewrite-vgpr-mfma-scale-to-agpr.mir

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
# RUN: not --crash llc -mtriple=amdgcn-amd-amdhsa -mcpu=gfx950 -run-pass=greedy,amdgpu-rewrite-agpr-copy-mfma -verify-machineinstrs -o - %s 2>&1 | FileCheck %s
2-
# CHECK: Illegal virtual register for instruction
3-
# CHECK: Expected a VGPR_32 register, but got a AGPR_32 register
4-
1+
# RUN: llc -mtriple=amdgcn-amd-amdhsa -mcpu=gfx950 -run-pass=greedy,amdgpu-rewrite-agpr-copy-mfma -verify-machineinstrs -o - %s 2>&1 | FileCheck %s
2+
# CHECK: bb.1:
3+
# CHECK: dead %{{[0-9]+}}:vreg_128_align2 = V_MFMA_SCALE_F32_16X16X128_F8F6F4_f4_f4_vgprcd_e64 %{{[0-9]+}}, %{{[0-9]+}}, %{{[0-9]+}}, 4, 4, %{{[0-9]+}}, %[[REG:[0-9]+]], 4, 0, implicit $mode, implicit $exec
4+
# CHECK: %{{[0-9]+}}:agpr_32 = IMPLICIT_DEF
5+
# CHECK: %[[REG]]:vgpr_32 = COPY %{{[0-9]+}}
6+
57
# Test for issue in amdgpu-rewrite-agpr-copy-mfma, which reassigns scale operand
68
# in vgpr_32 register to agpr_32, not permitted by instruction format.
79
---

0 commit comments

Comments
 (0)