Skip to content

Commit 5445432

Browse files
committed
check for vgpr16 putting into vgpr32 case in v2s lowering
1 parent f99e76b commit 5445432

File tree

3 files changed

+596
-259
lines changed

3 files changed

+596
-259
lines changed

llvm/lib/Target/AMDGPU/SIInstrInfo.cpp

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7227,24 +7227,44 @@ bool SIInstrWorklist::isDeferred(MachineInstr *MI) {
72277227
return DeferredList.contains(MI);
72287228
}
72297229

7230-
// 16bit SALU use sgpr32. If a 16bit SALU get lowered to VALU in true16 mode,
7231-
// sgpr32 is replaced to vgpr32 which is illegal in t16 inst. Need to add
7232-
// subreg access properly. This can be removed after we have sgpr16 in place
7233-
void SIInstrInfo::legalizeOperandsVALUt16(MachineInstr &Inst,
7230+
// legalize operand between 16bit and 32bit registers in v2s copy
7231+
// lowering (change spgr to vgpr).
7232+
// This is mainly caused by 16bit SALU and 16bit VALU using reg with different
7233+
// size. Need to legalize the size of the operands during the vgpr lowering
7234+
// chain. This can be removed after we have sgpr16 in place
7235+
void SIInstrInfo::legalizeOperandsVALUt16(MachineInstr &MI,
72347236
MachineRegisterInfo &MRI) const {
7235-
unsigned Opcode = Inst.getOpcode();
7236-
if (!AMDGPU::isTrue16Inst(Opcode) || !ST.useRealTrue16Insts())
7237+
if (!ST.useRealTrue16Insts())
72377238
return;
72387239

7239-
for (MachineOperand &Op : Inst.explicit_operands()) {
7240+
unsigned Opcode = MI.getOpcode();
7241+
MachineBasicBlock *MBB = MI.getParent();
7242+
7243+
// legalize operands and check for size mismatch
7244+
for (MachineOperand &Op : MI.explicit_operands()) {
72407245
unsigned OpIdx = Op.getOperandNo();
72417246
if (!OpIdx)
72427247
continue;
7243-
if (Op.isReg() && RI.isVGPR(MRI, Op.getReg())) {
7248+
if (Op.isReg() && Op.getReg().isVirtual() && RI.isVGPR(MRI, Op.getReg())) {
72447249
unsigned RCID = get(Opcode).operands()[OpIdx].RegClass;
7245-
const TargetRegisterClass *RC = RI.getRegClass(RCID);
7246-
if (RI.getRegSizeInBits(*RC) == 16) {
7250+
const TargetRegisterClass *ExpectedRC = RI.getRegClass(RCID);
7251+
const TargetRegisterClass *RC = MRI.getRegClass(Op.getReg());
7252+
if (32 == RI.getRegSizeInBits(*RC) &&
7253+
16 == RI.getRegSizeInBits(*ExpectedRC)) {
72477254
Op.setSubReg(AMDGPU::lo16);
7255+
} else if (16 == RI.getRegSizeInBits(*RC) &&
7256+
32 == RI.getRegSizeInBits(*ExpectedRC)) {
7257+
const DebugLoc &DL = MI.getDebugLoc();
7258+
Register NewDstReg =
7259+
MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
7260+
Register Undef = MRI.createVirtualRegister(&AMDGPU::VGPR_16RegClass);
7261+
BuildMI(*MBB, MI, DL, get(AMDGPU::IMPLICIT_DEF), Undef);
7262+
BuildMI(*MBB, MI, DL, get(AMDGPU::REG_SEQUENCE), NewDstReg)
7263+
.addReg(Op.getReg())
7264+
.addImm(AMDGPU::lo16)
7265+
.addReg(Undef)
7266+
.addImm(AMDGPU::hi16);
7267+
Op.setReg(NewDstReg);
72487268
}
72497269
}
72507270
}
@@ -7789,8 +7809,19 @@ void SIInstrInfo::moveToVALUImpl(SIInstrWorklist &Worklist,
77897809
.addReg(Undef)
77907810
.addImm(AMDGPU::hi16);
77917811
Inst.eraseFromParent();
7792-
77937812
MRI.replaceRegWith(DstReg, NewDstReg);
7813+
// legalize useMI with mismatched size
7814+
for (MachineRegisterInfo::use_iterator I = MRI.use_begin(NewDstReg),
7815+
E = MRI.use_end();
7816+
I != E; ++I) {
7817+
MachineInstr &UseMI = *I->getParent();
7818+
unsigned UseMIOpcode = UseMI.getOpcode();
7819+
if (AMDGPU::isTrue16Inst(UseMIOpcode) &&
7820+
(16 ==
7821+
RI.getRegSizeInBits(*getOpRegClass(UseMI, I.getOperandNo())))) {
7822+
I->setSubReg(AMDGPU::lo16);
7823+
}
7824+
}
77947825
addUsersToMoveToVALUWorklist(NewDstReg, MRI, Worklist);
77957826
return;
77967827
}

llvm/test/CodeGen/AMDGPU/fix-sgpr-copies-f16-true16.mir

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,57 @@ body: |
5757
%4:vgpr_16 = V_CVT_F16_U16_t16_e64 0, %3:sreg_32, 0, 0, 0, implicit $mode, implicit $exec
5858
...
5959

60+
---
61+
name: salu16_usedby_salu32
62+
body: |
63+
bb.0:
64+
; GCN-LABEL: name: salu16_usedby_salu32
65+
; GCN: [[DEF:%[0-9]+]]:vgpr_32 = IMPLICIT_DEF
66+
; GCN-NEXT: [[DEF1:%[0-9]+]]:sreg_32 = IMPLICIT_DEF
67+
; GCN-NEXT: [[V_TRUNC_F16_t16_e64_:%[0-9]+]]:vgpr_16 = V_TRUNC_F16_t16_e64 0, [[DEF]].lo16, 0, 0, 0, implicit $mode, implicit $exec
68+
; GCN-NEXT: [[DEF2:%[0-9]+]]:vgpr_16 = IMPLICIT_DEF
69+
; GCN-NEXT: [[REG_SEQUENCE:%[0-9]+]]:vgpr_32 = REG_SEQUENCE [[V_TRUNC_F16_t16_e64_]], %subreg.lo16, [[DEF2]], %subreg.hi16
70+
; GCN-NEXT: [[V_XOR_B32_e64_:%[0-9]+]]:vgpr_32 = V_XOR_B32_e64 [[REG_SEQUENCE]], [[DEF]], implicit $exec
71+
%0:vgpr_32 = IMPLICIT_DEF
72+
%1:sreg_32 = COPY %0:vgpr_32
73+
%2:sreg_32 = S_TRUNC_F16 %1:sreg_32, implicit $mode
74+
%3:sreg_32 = S_XOR_B32 %2:sreg_32, %1:sreg_32, implicit-def $scc
75+
...
76+
77+
---
78+
name: salu32_usedby_salu16
79+
body: |
80+
bb.0:
81+
; GCN-LABEL: name: salu32_usedby_salu16
82+
; GCN: [[DEF:%[0-9]+]]:vgpr_32 = IMPLICIT_DEF
83+
; GCN-NEXT: [[DEF1:%[0-9]+]]:sreg_32 = IMPLICIT_DEF
84+
; GCN-NEXT: [[V_XOR_B32_e64_:%[0-9]+]]:vgpr_32 = V_XOR_B32_e64 [[DEF]], [[DEF]], implicit $exec
85+
; GCN-NEXT: [[V_TRUNC_F16_t16_e64_:%[0-9]+]]:vgpr_16 = V_TRUNC_F16_t16_e64 0, [[V_XOR_B32_e64_]].lo16, 0, 0, 0, implicit $mode, implicit $exec
86+
%0:vgpr_32 = IMPLICIT_DEF
87+
%1:sreg_32 = COPY %0:vgpr_32
88+
%2:sreg_32 = S_XOR_B32 %1:sreg_32, %1:sreg_32, implicit-def $scc
89+
%3:sreg_32 = S_TRUNC_F16 %2:sreg_32, implicit $mode
90+
...
91+
92+
---
93+
name: S_FMAC_F16
94+
body: |
95+
bb.0:
96+
; GCN-LABEL: name: S_FMAC_F16
97+
; GCN: [[DEF:%[0-9]+]]:vgpr_16 = IMPLICIT_DEF
98+
; GCN-NEXT: [[DEF1:%[0-9]+]]:sgpr_lo16 = IMPLICIT_DEF
99+
; GCN-NEXT: [[DEF2:%[0-9]+]]:vgpr_16 = IMPLICIT_DEF
100+
; GCN-NEXT: [[REG_SEQUENCE:%[0-9]+]]:vgpr_32 = REG_SEQUENCE [[DEF]], %subreg.lo16, [[DEF2]], %subreg.hi16
101+
; GCN-NEXT: [[DEF3:%[0-9]+]]:vgpr_16 = IMPLICIT_DEF
102+
; GCN-NEXT: [[REG_SEQUENCE1:%[0-9]+]]:vgpr_32 = REG_SEQUENCE [[DEF]], %subreg.lo16, [[DEF3]], %subreg.hi16
103+
; GCN-NEXT: [[V_FMAC_F16_t16_e64_:%[0-9]+]]:vgpr_16 = V_FMAC_F16_t16_e64 0, [[REG_SEQUENCE1]].lo16, 0, [[REG_SEQUENCE1]].lo16, 0, [[REG_SEQUENCE]].lo16, 0, 0, 0, implicit $mode, implicit $exec
104+
%0:vgpr_16 = IMPLICIT_DEF
105+
%1:sgpr_lo16 = COPY %0:vgpr_16
106+
%2:sreg_32 = COPY %0:vgpr_16
107+
%3:sreg_32 = COPY %1:sgpr_lo16
108+
%4:sreg_32 = S_FMAC_F16 %3:sreg_32, %3:sreg_32, %2:sreg_32, implicit $mode
109+
...
110+
60111
---
61112
name: vgpr16_to_spgr32
62113
body: |

0 commit comments

Comments
 (0)