Skip to content

Commit fc8e741

Browse files
committed
[AMDGPU] Avoid an illegal operand in si-shrink-instructions
Before the patch it was possible to trigger a constant bus violation when folding immediates into a shrunk instruction. The patch adds a check to enforce the legality of the new operand. Differential Revision: https://reviews.llvm.org/D95527
1 parent 0da15ea commit fc8e741

File tree

2 files changed

+36
-11
lines changed

2 files changed

+36
-11
lines changed

llvm/lib/Target/AMDGPU/SIShrinkInstructions.cpp

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -75,17 +75,19 @@ static bool foldImmediates(MachineInstr &MI, const SIInstrInfo *TII,
7575
MachineOperand &MovSrc = Def->getOperand(1);
7676
bool ConstantFolded = false;
7777

78-
if (MovSrc.isImm() && (isInt<32>(MovSrc.getImm()) ||
79-
isUInt<32>(MovSrc.getImm()))) {
80-
Src0.ChangeToImmediate(MovSrc.getImm());
81-
ConstantFolded = true;
82-
} else if (MovSrc.isFI()) {
83-
Src0.ChangeToFrameIndex(MovSrc.getIndex());
84-
ConstantFolded = true;
85-
} else if (MovSrc.isGlobal()) {
86-
Src0.ChangeToGA(MovSrc.getGlobal(), MovSrc.getOffset(),
87-
MovSrc.getTargetFlags());
88-
ConstantFolded = true;
78+
if (TII->isOperandLegal(MI, Src0Idx, &MovSrc)) {
79+
if (MovSrc.isImm() &&
80+
(isInt<32>(MovSrc.getImm()) || isUInt<32>(MovSrc.getImm()))) {
81+
Src0.ChangeToImmediate(MovSrc.getImm());
82+
ConstantFolded = true;
83+
} else if (MovSrc.isFI()) {
84+
Src0.ChangeToFrameIndex(MovSrc.getIndex());
85+
ConstantFolded = true;
86+
} else if (MovSrc.isGlobal()) {
87+
Src0.ChangeToGA(MovSrc.getGlobal(), MovSrc.getOffset(),
88+
MovSrc.getTargetFlags());
89+
ConstantFolded = true;
90+
}
8991
}
9092

9193
if (ConstantFolded) {
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# RUN: llc -march=amdgcn -mcpu=gfx900 -run-pass=si-shrink-instructions --verify-machineinstrs %s -o - | FileCheck %s
2+
3+
# Make sure immediate folding into V_CNDMASK respects constant bus restrictions.
4+
---
5+
6+
name: shrink_cndmask_illegal_imm_folding
7+
tracksRegLiveness: true
8+
body: |
9+
bb.0:
10+
liveins: $vgpr0, $vgpr1
11+
; CHECK-LABEL: name: shrink_cndmask_illegal_imm_folding
12+
; CHECK: [[COPY:%[0-9]+]]:vgpr_32 = COPY $vgpr0
13+
; CHECK: [[MOV:%[0-9]+]]:vgpr_32 = V_MOV_B32_e32 32768, implicit $exec
14+
; CHECK: V_CMP_EQ_U32_e32 0, [[COPY]], implicit-def $vcc, implicit $exec
15+
; CHECK: V_CNDMASK_B32_e32 [[MOV]], killed [[COPY]], implicit $vcc, implicit $exec
16+
17+
%0:vgpr_32 = COPY $vgpr0
18+
%1:vgpr_32 = V_MOV_B32_e32 32768, implicit $exec
19+
V_CMP_EQ_U32_e32 0, %0:vgpr_32, implicit-def $vcc, implicit $exec
20+
%2:vgpr_32 = V_CNDMASK_B32_e64 0, %1:vgpr_32, 0, killed %0:vgpr_32, $vcc, implicit $exec
21+
S_NOP 0
22+
23+
...

0 commit comments

Comments
 (0)