Skip to content

[AMDGPU] Fix crash due to missing check for FLAT instructions that dont use vector registers when computing VALU hazard. #123627

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions llvm/lib/Target/AMDGPU/GCNHazardRecognizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -858,9 +858,12 @@ int GCNHazardRecognizer::createsVALUHazard(const MachineInstr &MI) {
}

if (TII->isFLAT(MI)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the MIMG case above doing anything? It looks like it's just an assert? I'm also surprised we're querying the operand before the instruction type

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the MIMG case above doing anything? It looks like it's just an assert?

Yes, seems that way. The comment suggests the code exists more as a reminder to include the assert than check anything -

All our MIMG definitions use a 256-bit T#, so we can skip checking for them.

int DataIdx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::vdata);
if (AMDGPU::getRegBitWidth(Desc.operands()[DataIdx].RegClass) > 64)
return DataIdx;
// There is no hazard if the instruction does not use vector regs
if (VDataIdx == -1)
return -1;

if (AMDGPU::getRegBitWidth(VDataRCID) > 64)
return VDataIdx;
}

return -1;
Expand Down
67 changes: 67 additions & 0 deletions llvm/test/CodeGen/AMDGPU/fix-crash-valu-hazard.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 5
; RUN: llc -mtriple=amdgcn -mcpu=gfx942 < %s | FileCheck -check-prefix=GFX942 %s
; RUN: llc -mtriple=amdgcn -mcpu=gfx90a -O0 < %s | FileCheck -check-prefix=GFX90A %s

@G = addrspace(1) global <2 x i32> splat (i32 5)

define amdgpu_ps void @global_load_lds_dword_saddr(ptr addrspace(1) inreg nocapture %gptr, ptr addrspace(3) nocapture %lptr) {
; GFX942-LABEL: global_load_lds_dword_saddr:
; GFX942: ; %bb.0: ; %main_body
; GFX942-NEXT: v_readfirstlane_b32 s2, v0
; GFX942-NEXT: v_mov_b32_e32 v2, 0
; GFX942-NEXT: s_mov_b32 m0, s2
; GFX942-NEXT: s_nop 0
; GFX942-NEXT: global_load_lds_dword v2, s[0:1] offset:32 nt
; GFX942-NEXT: s_getpc_b64 s[0:1]
; GFX942-NEXT: s_add_u32 s0, s0, G@gotpcrel32@lo+4
; GFX942-NEXT: s_addc_u32 s1, s1, G@gotpcrel32@hi+12
; GFX942-NEXT: s_load_dwordx2 s[0:1], s[0:1], 0x0
; GFX942-NEXT: s_waitcnt lgkmcnt(0)
; GFX942-NEXT: s_load_dwordx2 s[2:3], s[0:1], 0x0
; GFX942-NEXT: s_waitcnt lgkmcnt(0)
; GFX942-NEXT: s_mul_i32 s3, s3, 10
; GFX942-NEXT: s_mul_i32 s2, s2, 10
; GFX942-NEXT: v_mov_b32_e32 v0, s2
; GFX942-NEXT: v_mov_b32_e32 v1, s3
; GFX942-NEXT: global_store_dwordx2 v2, v[0:1], s[0:1]
; GFX942-NEXT: s_endpgm
;
; GFX90A-LABEL: global_load_lds_dword_saddr:
; GFX90A: ; %bb.0: ; %main_body
; GFX90A-NEXT: v_mov_b32_e32 v1, v0
; GFX90A-NEXT: s_mov_b32 s2, s0
; GFX90A-NEXT: ; kill: def $sgpr2 killed $sgpr2 def $sgpr2_sgpr3
; GFX90A-NEXT: s_mov_b32 s3, s1
; GFX90A-NEXT: ; kill: def $sgpr0_sgpr1 killed $sgpr2_sgpr3
; GFX90A-NEXT: s_getpc_b64 s[0:1]
; GFX90A-NEXT: s_add_u32 s0, s0, G@gotpcrel32@lo+4
; GFX90A-NEXT: s_addc_u32 s1, s1, G@gotpcrel32@hi+12
; GFX90A-NEXT: s_load_dwordx2 s[0:1], s[0:1], 0x0
; GFX90A-NEXT: s_waitcnt lgkmcnt(0)
; GFX90A-NEXT: s_load_dwordx2 s[8:9], s[0:1], 0x0
; GFX90A-NEXT: v_mov_b32_e32 v0, 0
; GFX90A-NEXT: s_waitcnt lgkmcnt(0)
; GFX90A-NEXT: s_mov_b32 s4, s9
; GFX90A-NEXT: s_mov_b32 s6, 10
; GFX90A-NEXT: s_mul_i32 s4, s4, s6
; GFX90A-NEXT: s_mov_b32 s5, s8
; GFX90A-NEXT: s_mul_i32 s5, s5, s6
; GFX90A-NEXT: v_mov_b32_e32 v2, s5
; GFX90A-NEXT: v_mov_b32_e32 v4, s4
; GFX90A-NEXT: ; kill: def $vgpr2 killed $vgpr2 def $vgpr2_vgpr3 killed $exec
; GFX90A-NEXT: v_mov_b32_e32 v3, v4
; GFX90A-NEXT: ; implicit-def: $sgpr4
; GFX90A-NEXT: v_readfirstlane_b32 s4, v1
; GFX90A-NEXT: s_mov_b32 m0, s4
; GFX90A-NEXT: s_nop 0
; GFX90A-NEXT: global_load_dword v0, s[2:3] offset:32 slc lds
; GFX90A-NEXT: s_waitcnt lgkmcnt(0)
; GFX90A-NEXT: global_store_dwordx2 v0, v[2:3], s[0:1]
; GFX90A-NEXT: s_endpgm
main_body:
%LGV = load <2 x i32>, ptr addrspace(1) @G, align 8
%B = mul <2 x i32> %LGV, splat (i32 10)
call void @llvm.amdgcn.global.load.lds(ptr addrspace(1) %gptr, ptr addrspace(3) %lptr, i32 4, i32 32, i32 2)
store <2 x i32> %B, ptr addrspace(1) @G, align 8
ret void
}
20 changes: 20 additions & 0 deletions llvm/test/CodeGen/AMDGPU/hazard-flat-instruction-valu-check.mir
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py UTC_ARGS: --version 5
# RUN: llc -mtriple=amdgcn -mcpu=gfx942 -verify-machineinstrs -run-pass=post-RA-hazard-rec %s -o - | FileCheck -check-prefix=GCN %s

---
name: test_flat_valu_hazard
noVRegs: true
body: |
bb.0:
liveins: $vgpr0, $vgpr1

; GCN-LABEL: name: test_flat_valu_hazard
; GCN: liveins: $vgpr0, $vgpr1
; GCN-NEXT: {{ $}}
; GCN-NEXT: GLOBAL_LOAD_LDS_DWORD_SADDR killed $sgpr0_sgpr1, killed $vgpr0, 32, 2, implicit $m0, implicit $exec
; GCN-NEXT: $vgpr0 = V_MOV_B32_e32 killed $vgpr1, implicit $exec, implicit $exec
; GCN-NEXT: FLAT_STORE_DWORDX2 killed renamable $vgpr2_vgpr3, killed renamable $vgpr0_vgpr1, 0, 0, implicit $exec, implicit $flat_scr
GLOBAL_LOAD_LDS_DWORD_SADDR killed $sgpr0_sgpr1, killed $vgpr0, 32, 2, implicit $m0, implicit $exec
$vgpr0 = V_MOV_B32_e32 killed $vgpr1, implicit $exec, implicit $exec
FLAT_STORE_DWORDX2 killed renamable $vgpr2_vgpr3, killed renamable $vgpr0_vgpr1, 0, 0, implicit $exec, implicit $flat_scr
...
Loading