1 commit b08a140a8fe8d0b0d16a93042b4952d6e34ab913
2 Author: Piotr Sobczak <Piotr.Sobczak@amd.com>
3 Date: Wed Jan 27 16:02:49 2021 +0100
5 [AMDGPU] Avoid an illegal operand in si-shrink-instructions
7 Before the patch it was possible to trigger a constant bus
8 violation when folding immediates into a shrunk instruction.
10 The patch adds a check to enforce the legality of the new operand.
12 Differential Revision: https://reviews.llvm.org/D95527
14 diff --git a/llvm/lib/Target/AMDGPU/SIShrinkInstructions.cpp b/llvm/lib/Target/AMDGPU/SIShrinkInstructions.cpp
15 index 9c6833a7dab6..6c1b16eddc84 100644
16 --- a/llvm/lib/Target/AMDGPU/SIShrinkInstructions.cpp
17 +++ b/llvm/lib/Target/AMDGPU/SIShrinkInstructions.cpp
18 @@ -84,21 +84,23 @@ static bool foldImmediates(MachineInstr &MI, const SIInstrInfo *TII,
19 MachineOperand &MovSrc = Def->getOperand(1);
20 bool ConstantFolded = false;
22 - if (MovSrc.isImm() && (isInt<32>(MovSrc.getImm()) ||
23 - isUInt<32>(MovSrc.getImm()))) {
24 - // It's possible to have only one component of a super-reg defined by
25 - // a single mov, so we need to clear any subregister flag.
27 - Src0.ChangeToImmediate(MovSrc.getImm());
28 - ConstantFolded = true;
29 - } else if (MovSrc.isFI()) {
31 - Src0.ChangeToFrameIndex(MovSrc.getIndex());
32 - ConstantFolded = true;
33 - } else if (MovSrc.isGlobal()) {
34 - Src0.ChangeToGA(MovSrc.getGlobal(), MovSrc.getOffset(),
35 - MovSrc.getTargetFlags());
36 - ConstantFolded = true;
37 + if (TII->isOperandLegal(MI, Src0Idx, &MovSrc)) {
38 + if (MovSrc.isImm() &&
39 + (isInt<32>(MovSrc.getImm()) || isUInt<32>(MovSrc.getImm()))) {
40 + // It's possible to have only one component of a super-reg defined
41 + // by a single mov, so we need to clear any subregister flag.
43 + Src0.ChangeToImmediate(MovSrc.getImm());
44 + ConstantFolded = true;
45 + } else if (MovSrc.isFI()) {
47 + Src0.ChangeToFrameIndex(MovSrc.getIndex());
48 + ConstantFolded = true;
49 + } else if (MovSrc.isGlobal()) {
50 + Src0.ChangeToGA(MovSrc.getGlobal(), MovSrc.getOffset(),
51 + MovSrc.getTargetFlags());
52 + ConstantFolded = true;
57 diff --git a/llvm/test/CodeGen/AMDGPU/shrink-instructions-illegal-fold.mir b/llvm/test/CodeGen/AMDGPU/shrink-instructions-illegal-fold.mir
59 index 000000000000..7889f437facf
61 +++ b/llvm/test/CodeGen/AMDGPU/shrink-instructions-illegal-fold.mir
63 +# RUN: llc -march=amdgcn -mcpu=gfx900 -run-pass=si-shrink-instructions --verify-machineinstrs %s -o - | FileCheck %s
65 +# Make sure immediate folding into V_CNDMASK respects constant bus restrictions.
68 +name: shrink_cndmask_illegal_imm_folding
69 +tracksRegLiveness: true
72 + liveins: $vgpr0, $vgpr1
73 + ; CHECK-LABEL: name: shrink_cndmask_illegal_imm_folding
74 + ; CHECK: [[COPY:%[0-9]+]]:vgpr_32 = COPY $vgpr0
75 + ; CHECK: [[MOV:%[0-9]+]]:vgpr_32 = V_MOV_B32_e32 32768, implicit $exec
76 + ; CHECK: V_CMP_EQ_U32_e32 0, [[COPY]], implicit-def $vcc, implicit $exec
77 + ; CHECK: V_CNDMASK_B32_e32 [[MOV]], killed [[COPY]], implicit $vcc, implicit $exec
79 + %0:vgpr_32 = COPY $vgpr0
80 + %1:vgpr_32 = V_MOV_B32_e32 32768, implicit $exec
81 + V_CMP_EQ_U32_e32 0, %0:vgpr_32, implicit-def $vcc, implicit $exec
82 + %2:vgpr_32 = V_CNDMASK_B32_e64 0, %1:vgpr_32, 0, killed %0:vgpr_32, $vcc, implicit $exec