Skip to content

Commit 52d5b8e

Browse files
authored
[AMDGPU] Don't form sext/abs/neg fp8 cvt (#83843)
gfx940 does not allow abs/sext/neg on v_cvt_fp8/bf8 & pk variants. Fixes SWDEV-447468
1 parent 0e5bde0 commit 52d5b8e

File tree

4 files changed

+157
-0
lines changed

4 files changed

+157
-0
lines changed

llvm/lib/Target/AMDGPU/SIInstrInfo.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4726,6 +4726,20 @@ bool SIInstrInfo::verifyInstruction(const MachineInstr &MI,
47264726
}
47274727
}
47284728

4729+
if (Opcode == AMDGPU::V_CVT_F32_FP8_sdwa ||
4730+
Opcode == AMDGPU::V_CVT_F32_BF8_sdwa ||
4731+
Opcode == AMDGPU::V_CVT_PK_F32_FP8_sdwa ||
4732+
Opcode == AMDGPU::V_CVT_PK_F32_BF8_sdwa) {
4733+
const MachineOperand *Src0ModsMO =
4734+
getNamedOperand(MI, AMDGPU::OpName::src0_modifiers);
4735+
unsigned Mods = Src0ModsMO->getImm();
4736+
if (Mods & SISrcMods::ABS || Mods & SISrcMods::NEG ||
4737+
Mods & SISrcMods::SEXT) {
4738+
ErrInfo = "sext, abs and neg are not allowed on this instruction";
4739+
return false;
4740+
}
4741+
}
4742+
47294743
uint16_t BasicOpcode = AMDGPU::getBasicFromSDWAOp(Opcode);
47304744
if (isVOPC(BasicOpcode)) {
47314745
if (!ST.hasSDWASdst() && DstIdx != -1) {

llvm/lib/Target/AMDGPU/SIPeepholeSDWA.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,15 @@ MachineInstr *SDWASrcOperand::potentialToConvert(const SIInstrInfo *TII) {
338338
}
339339

340340
bool SDWASrcOperand::convertToSDWA(MachineInstr &MI, const SIInstrInfo *TII) {
341+
switch (MI.getOpcode()) {
342+
case AMDGPU::V_CVT_F32_FP8_sdwa:
343+
case AMDGPU::V_CVT_F32_BF8_sdwa:
344+
case AMDGPU::V_CVT_PK_F32_FP8_sdwa:
345+
case AMDGPU::V_CVT_PK_F32_BF8_sdwa:
346+
// Does not support input modifiers: noabs, noneg, nosext.
347+
return false;
348+
}
349+
341350
// Find operand in instruction that matches source operand and replace it with
342351
// target operand. Set corresponding src_sel
343352
bool IsPreserveSrc = false;

llvm/test/CodeGen/AMDGPU/llvm.amdgcn.cvt.fp8.ll

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,3 +534,99 @@ define i32 @test_cvt_sr_fp8_f32_byte3(float %x, i32 %r, i32 %old) {
534534
%ret = tail call i32 @llvm.amdgcn.cvt.sr.fp8.f32(float %x, i32 %r, i32 %old, i32 3)
535535
ret i32 %ret
536536
}
537+
538+
define float @test_sext_cvt_f32_fp8(i16 %a) {
539+
; GFX940-LABEL: test_sext_cvt_f32_fp8:
540+
; GFX940: ; %bb.0:
541+
; GFX940-NEXT: s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
542+
; GFX940-NEXT: v_bfe_i32 v0, v0, 0, 16
543+
; GFX940-NEXT: v_cvt_f32_fp8_sdwa v0, v0 src0_sel:BYTE_1
544+
; GFX940-NEXT: s_setpc_b64 s[30:31]
545+
;
546+
; GFX12-LABEL: test_sext_cvt_f32_fp8:
547+
; GFX12: ; %bb.0:
548+
; GFX12-NEXT: s_wait_loadcnt_dscnt 0x0
549+
; GFX12-NEXT: s_wait_expcnt 0x0
550+
; GFX12-NEXT: s_wait_samplecnt 0x0
551+
; GFX12-NEXT: s_wait_bvhcnt 0x0
552+
; GFX12-NEXT: s_wait_kmcnt 0x0
553+
; GFX12-NEXT: v_bfe_i32 v0, v0, 0, 16
554+
; GFX12-NEXT: s_delay_alu instid0(VALU_DEP_1)
555+
; GFX12-NEXT: v_cvt_f32_fp8_e64 v0, v0 op_sel:[0,1]
556+
; GFX12-NEXT: s_setpc_b64 s[30:31]
557+
%a.sext = sext i16 %a to i32
558+
%ret = tail call float @llvm.amdgcn.cvt.f32.fp8(i32 %a.sext, i32 1)
559+
ret float %ret
560+
}
561+
562+
define float @test_sext_cvt_f32_bf8(i16 %a) {
563+
; GFX940-LABEL: test_sext_cvt_f32_bf8:
564+
; GFX940: ; %bb.0:
565+
; GFX940-NEXT: s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
566+
; GFX940-NEXT: v_bfe_i32 v0, v0, 0, 16
567+
; GFX940-NEXT: v_cvt_f32_bf8_sdwa v0, v0 src0_sel:BYTE_1
568+
; GFX940-NEXT: s_setpc_b64 s[30:31]
569+
;
570+
; GFX12-LABEL: test_sext_cvt_f32_bf8:
571+
; GFX12: ; %bb.0:
572+
; GFX12-NEXT: s_wait_loadcnt_dscnt 0x0
573+
; GFX12-NEXT: s_wait_expcnt 0x0
574+
; GFX12-NEXT: s_wait_samplecnt 0x0
575+
; GFX12-NEXT: s_wait_bvhcnt 0x0
576+
; GFX12-NEXT: s_wait_kmcnt 0x0
577+
; GFX12-NEXT: v_bfe_i32 v0, v0, 0, 16
578+
; GFX12-NEXT: s_delay_alu instid0(VALU_DEP_1)
579+
; GFX12-NEXT: v_cvt_f32_bf8_e64 v0, v0 op_sel:[0,1]
580+
; GFX12-NEXT: s_setpc_b64 s[30:31]
581+
%a.sext = sext i16 %a to i32
582+
%ret = tail call float @llvm.amdgcn.cvt.f32.bf8(i32 %a.sext, i32 1)
583+
ret float %ret
584+
}
585+
586+
define <2 x float> @test_sext_cvt_pk_f32_bf8_word1(i16 %a) {
587+
; GFX940-LABEL: test_sext_cvt_pk_f32_bf8_word1:
588+
; GFX940: ; %bb.0:
589+
; GFX940-NEXT: s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
590+
; GFX940-NEXT: v_bfe_i32 v0, v0, 0, 16
591+
; GFX940-NEXT: v_cvt_pk_f32_bf8_sdwa v[0:1], v0 src0_sel:WORD_1
592+
; GFX940-NEXT: s_setpc_b64 s[30:31]
593+
;
594+
; GFX12-LABEL: test_sext_cvt_pk_f32_bf8_word1:
595+
; GFX12: ; %bb.0:
596+
; GFX12-NEXT: s_wait_loadcnt_dscnt 0x0
597+
; GFX12-NEXT: s_wait_expcnt 0x0
598+
; GFX12-NEXT: s_wait_samplecnt 0x0
599+
; GFX12-NEXT: s_wait_bvhcnt 0x0
600+
; GFX12-NEXT: s_wait_kmcnt 0x0
601+
; GFX12-NEXT: v_bfe_i32 v0, v0, 0, 16
602+
; GFX12-NEXT: s_delay_alu instid0(VALU_DEP_1)
603+
; GFX12-NEXT: v_cvt_pk_f32_bf8_e64 v[0:1], v0 op_sel:[1,0]
604+
; GFX12-NEXT: s_setpc_b64 s[30:31]
605+
%a.sext = sext i16 %a to i32
606+
%ret = tail call <2 x float> @llvm.amdgcn.cvt.pk.f32.bf8(i32 %a.sext, i1 true)
607+
ret <2 x float> %ret
608+
}
609+
610+
define <2 x float> @test_sext_cvt_pk_f32_fp8_word0(i16 %a) {
611+
; GFX940-LABEL: test_sext_cvt_pk_f32_fp8_word0:
612+
; GFX940: ; %bb.0:
613+
; GFX940-NEXT: s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
614+
; GFX940-NEXT: v_bfe_i32 v0, v0, 0, 16
615+
; GFX940-NEXT: v_cvt_pk_f32_fp8_e32 v[0:1], v0
616+
; GFX940-NEXT: s_setpc_b64 s[30:31]
617+
;
618+
; GFX12-LABEL: test_sext_cvt_pk_f32_fp8_word0:
619+
; GFX12: ; %bb.0:
620+
; GFX12-NEXT: s_wait_loadcnt_dscnt 0x0
621+
; GFX12-NEXT: s_wait_expcnt 0x0
622+
; GFX12-NEXT: s_wait_samplecnt 0x0
623+
; GFX12-NEXT: s_wait_bvhcnt 0x0
624+
; GFX12-NEXT: s_wait_kmcnt 0x0
625+
; GFX12-NEXT: v_bfe_i32 v0, v0, 0, 16
626+
; GFX12-NEXT: s_delay_alu instid0(VALU_DEP_1)
627+
; GFX12-NEXT: v_cvt_pk_f32_fp8_e32 v[0:1], v0
628+
; GFX12-NEXT: s_setpc_b64 s[30:31]
629+
%a.sext = sext i16 %a to i32
630+
%ret = tail call <2 x float> @llvm.amdgcn.cvt.pk.f32.fp8(i32 %a.sext, i1 false)
631+
ret <2 x float> %ret
632+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# RUN: not --crash llc -mtriple=amdgcn -mcpu=gfx940 -run-pass machineverifier -o /dev/null %s 2>&1 | FileCheck -implicit-check-not="Bad machine code" %s
2+
3+
# CHECK: *** Bad machine code: sext, abs and neg are not allowed on this instruction ***
4+
# CHECK: $vgpr0 = V_CVT_F32_FP8_sdwa 1, $vgpr0, 0, 0, 4, implicit $mode, implicit $exec
5+
# CHECK: *** Bad machine code: sext, abs and neg are not allowed on this instruction ***
6+
# CHECK: $vgpr0 = V_CVT_F32_BF8_sdwa 1, $vgpr0, 0, 0, 4, implicit $mode, implicit $exec
7+
# CHECK: *** Bad machine code: sext, abs and neg are not allowed on this instruction ***
8+
# CHECK: $vgpr0_vgpr1 = V_CVT_PK_F32_FP8_sdwa 1, $vgpr0, 0, 0, 4, implicit $mode, implicit $exec
9+
# CHECK: *** Bad machine code: sext, abs and neg are not allowed on this instruction ***
10+
# CHECK: $vgpr0_vgpr1 = V_CVT_PK_F32_BF8_sdwa 1, $vgpr0, 0, 0, 4, implicit $mode, implicit $exec
11+
# CHECK: *** Bad machine code: sext, abs and neg are not allowed on this instruction ***
12+
# CHECK: $vgpr0 = V_CVT_F32_FP8_sdwa 2, $vgpr0, 0, 0, 4, implicit $mode, implicit $exec
13+
# CHECK: *** Bad machine code: sext, abs and neg are not allowed on this instruction ***
14+
# CHECK: $vgpr0 = V_CVT_F32_BF8_sdwa 2, $vgpr0, 0, 0, 4, implicit $mode, implicit $exec
15+
# CHECK: *** Bad machine code: sext, abs and neg are not allowed on this instruction ***
16+
# CHECK: $vgpr0_vgpr1 = V_CVT_PK_F32_FP8_sdwa 2, $vgpr0, 0, 0, 4, implicit $mode, implicit $exec
17+
# CHECK: *** Bad machine code: sext, abs and neg are not allowed on this instruction ***
18+
# CHECK: $vgpr0_vgpr1 = V_CVT_PK_F32_BF8_sdwa 2, $vgpr0, 0, 0, 4, implicit $mode, implicit $exec
19+
20+
---
21+
name: test
22+
liveins:
23+
body: |
24+
bb.0:
25+
liveins: $vgpr0, $vgpr0_vgpr1
26+
27+
; sext/neg
28+
$vgpr0 = V_CVT_F32_FP8_sdwa 1, $vgpr0, 0, 0, 4, implicit $mode, implicit $exec
29+
$vgpr0 = V_CVT_F32_BF8_sdwa 1, $vgpr0, 0, 0, 4, implicit $mode, implicit $exec
30+
$vgpr0_vgpr1 = V_CVT_PK_F32_FP8_sdwa 1, $vgpr0, 0, 0, 4, implicit $mode, implicit $exec
31+
$vgpr0_vgpr1 = V_CVT_PK_F32_BF8_sdwa 1, $vgpr0, 0, 0, 4, implicit $mode, implicit $exec
32+
33+
; abs
34+
$vgpr0 = V_CVT_F32_FP8_sdwa 2, $vgpr0, 0, 0, 4, implicit $mode, implicit $exec
35+
$vgpr0 = V_CVT_F32_BF8_sdwa 2, $vgpr0, 0, 0, 4, implicit $mode, implicit $exec
36+
$vgpr0_vgpr1 = V_CVT_PK_F32_FP8_sdwa 2, $vgpr0, 0, 0, 4, implicit $mode, implicit $exec
37+
$vgpr0_vgpr1 = V_CVT_PK_F32_BF8_sdwa 2, $vgpr0, 0, 0, 4, implicit $mode, implicit $exec
38+
...

0 commit comments

Comments
 (0)