Skip to content

Commit f541a3a

Browse files
frederik-harsenm
andauthored
[AMDGPU] SIInstrInfo: Fix resultDependsOnExec for VOPC instructions (#134629)
SIInstrInfo::resultDependsOnExec assumes that operand 0 of a comparison is always the destination of the instruction. This is not true for instructions in VOPC form where it is "src0". This led to a crash in machine-cse. --------- Co-authored-by: Matt Arsenault <[email protected]>
1 parent a22ad65 commit f541a3a

File tree

3 files changed

+35
-3
lines changed

3 files changed

+35
-3
lines changed

llvm/lib/Target/AMDGPU/SIInstrInfo.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,14 +147,19 @@ bool SIInstrInfo::isReallyTriviallyReMaterializable(
147147
}
148148

149149
// Returns true if the scalar result of a VALU instruction depends on exec.
150-
static bool resultDependsOnExec(const MachineInstr &MI) {
150+
bool SIInstrInfo::resultDependsOnExec(const MachineInstr &MI) const {
151151
// Ignore comparisons which are only used masked with exec.
152152
// This allows some hoisting/sinking of VALU comparisons.
153153
if (MI.isCompare()) {
154-
const MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo();
155-
Register DstReg = MI.getOperand(0).getReg();
154+
const MachineOperand *Dst = getNamedOperand(MI, AMDGPU::OpName::sdst);
155+
if (!Dst)
156+
return true;
157+
158+
Register DstReg = Dst->getReg();
156159
if (!DstReg.isVirtual())
157160
return true;
161+
162+
const MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo();
158163
for (MachineInstr &Use : MRI.use_nodbg_instructions(DstReg)) {
159164
switch (Use.getOpcode()) {
160165
case AMDGPU::S_AND_SAVEEXEC_B32:

llvm/lib/Target/AMDGPU/SIInstrInfo.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,8 @@ class SIInstrInfo final : public AMDGPUGenInstrInfo {
183183
bool verifyCopy(const MachineInstr &MI, const MachineRegisterInfo &MRI,
184184
StringRef &ErrInfo) const;
185185

186+
bool resultDependsOnExec(const MachineInstr &MI) const;
187+
186188
protected:
187189
/// If the specific machine instruction is a instruction that moves/copies
188190
/// value from one register to another register return destination and source
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py UTC_ARGS: --version 5
2+
# RUN: llc -mtriple=amdgcn -mcpu=gfx1010 -run-pass=machine-cse %s -o - | FileCheck %s
3+
4+
# Check only that V_CMP_EQ_U32_e32 does not lead to a crash.
5+
6+
---
7+
name: depends_on_exec_check_can_handle_vopc
8+
tracksRegLiveness: true
9+
body: |
10+
bb.0:
11+
; CHECK-LABEL: name: depends_on_exec_check_can_handle_vopc
12+
; CHECK: [[DEF:%[0-9]+]]:vgpr_32 = IMPLICIT_DEF
13+
; CHECK-NEXT: V_CMP_EQ_U32_e32 1, killed undef [[DEF]], implicit-def $vcc_lo, implicit $exec
14+
; CHECK-NEXT: [[V_CNDMASK_B32_e32_:%[0-9]+]]:vgpr_32 = V_CNDMASK_B32_e32 16256, undef [[DEF]], implicit $vcc_lo, implicit $exec
15+
; CHECK-NEXT: [[V_LSHLREV_B32_e64_:%[0-9]+]]:vgpr_32 = V_LSHLREV_B32_e64 16, undef [[V_CNDMASK_B32_e32_]], implicit $exec
16+
; CHECK-NEXT: SI_RETURN
17+
%0:vgpr_32 = IMPLICIT_DEF
18+
V_CMP_EQ_U32_e32 1, killed undef %0, implicit-def $vcc, implicit $exec
19+
%1:vgpr_32 = V_CNDMASK_B32_e32 16256, undef %0, implicit $vcc, implicit $exec
20+
V_CMP_EQ_U32_e32 1, killed undef %0, implicit-def $vcc, implicit $exec
21+
%2:vgpr_32 = V_CNDMASK_B32_e32 16256, undef %0, implicit $vcc, implicit $exec
22+
%3:vgpr_32 = V_LSHLREV_B32_e64 16, killed undef %2, implicit $exec
23+
%4:vgpr_32 = V_LSHLREV_B32_e64 16, killed undef %1, implicit $exec
24+
SI_RETURN
25+
...

0 commit comments

Comments
 (0)