Skip to content

Commit 2126c70

Browse files
committed
AMDGPU/GlobalISel: Don't mis-select vector index on a constant
Vector indexing with a constant index should be folded out in the legalizer, but this was accidentally falling through. This would produce the indexing operation with $noreg. Handle this case as a dynamic index just in case a bug like this happens again in the future.
1 parent f4a38c1 commit 2126c70

File tree

3 files changed

+124
-0
lines changed

3 files changed

+124
-0
lines changed

llvm/lib/Target/AMDGPU/AMDGPUInstructionSelector.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1721,6 +1721,12 @@ computeIndirectRegIndex(MachineRegisterInfo &MRI,
17211721

17221722
std::tie(IdxBaseReg, Offset, Unused)
17231723
= AMDGPU::getBaseWithConstantOffset(MRI, IdxReg);
1724+
if (IdxBaseReg == AMDGPU::NoRegister) {
1725+
// This will happen if the index is a known constant. This should ordinarily
1726+
// be legalized out, but handle it as a register just in case.
1727+
assert(Offset == 0);
1728+
IdxBaseReg = IdxReg;
1729+
}
17241730

17251731
ArrayRef<int16_t> SubRegs = TRI.getRegSplitParts(SuperRC, EltSize);
17261732

llvm/test/CodeGen/AMDGPU/GlobalISel/inst-select-extract-vector-elt.mir

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -784,3 +784,58 @@ body: |
784784
%4:vgpr(s32) = G_EXTRACT_VECTOR_ELT %0, %3
785785
S_ENDPGM 0, implicit %4
786786
...
787+
788+
---
789+
name: extract_vector_elt_s_s32_v4s32_const_idx
790+
legalized: true
791+
regBankSelected: true
792+
793+
body: |
794+
bb.0:
795+
liveins: $sgpr0_sgpr1_sgpr2_sgpr3
796+
797+
; MOVREL-LABEL: name: extract_vector_elt_s_s32_v4s32_const_idx
798+
; MOVREL: [[COPY:%[0-9]+]]:sgpr_128 = COPY $sgpr0_sgpr1_sgpr2_sgpr3
799+
; MOVREL: [[S_MOV_B32_:%[0-9]+]]:sreg_32 = S_MOV_B32 0
800+
; MOVREL: $m0 = COPY [[S_MOV_B32_]]
801+
; MOVREL: [[S_MOVRELS_B32_:%[0-9]+]]:sreg_32 = S_MOVRELS_B32 [[COPY]].sub0, implicit $m0, implicit [[COPY]]
802+
; MOVREL: S_ENDPGM 0, implicit [[S_MOVRELS_B32_]]
803+
; GPRIDX-LABEL: name: extract_vector_elt_s_s32_v4s32_const_idx
804+
; GPRIDX: [[COPY:%[0-9]+]]:sgpr_128 = COPY $sgpr0_sgpr1_sgpr2_sgpr3
805+
; GPRIDX: [[S_MOV_B32_:%[0-9]+]]:sreg_32 = S_MOV_B32 0
806+
; GPRIDX: $m0 = COPY [[S_MOV_B32_]]
807+
; GPRIDX: [[S_MOVRELS_B32_:%[0-9]+]]:sreg_32 = S_MOVRELS_B32 [[COPY]].sub0, implicit $m0, implicit [[COPY]]
808+
; GPRIDX: S_ENDPGM 0, implicit [[S_MOVRELS_B32_]]
809+
%0:sgpr(<4 x s32>) = COPY $sgpr0_sgpr1_sgpr2_sgpr3
810+
%1:sgpr(s32) = G_CONSTANT i32 0
811+
%2:sgpr(s32) = G_EXTRACT_VECTOR_ELT %0, %1
812+
S_ENDPGM 0, implicit %2
813+
...
814+
815+
---
816+
name: extract_vector_elt_v_s32_v4s32_const_idx
817+
legalized: true
818+
regBankSelected: true
819+
820+
body: |
821+
bb.0:
822+
liveins: $vgpr0_vgpr1_vgpr2_vgpr3
823+
824+
; MOVREL-LABEL: name: extract_vector_elt_v_s32_v4s32_const_idx
825+
; MOVREL: [[COPY:%[0-9]+]]:vreg_128 = COPY $sgpr0_sgpr1_sgpr2_sgpr3
826+
; MOVREL: [[S_MOV_B32_:%[0-9]+]]:sreg_32 = S_MOV_B32 0
827+
; MOVREL: $m0 = COPY [[S_MOV_B32_]]
828+
; MOVREL: [[V_MOVRELS_B32_e32_:%[0-9]+]]:vgpr_32 = V_MOVRELS_B32_e32 undef [[COPY]].sub0, implicit $m0, implicit $exec, implicit [[COPY]]
829+
; MOVREL: S_ENDPGM 0, implicit [[V_MOVRELS_B32_e32_]]
830+
; GPRIDX-LABEL: name: extract_vector_elt_v_s32_v4s32_const_idx
831+
; GPRIDX: [[COPY:%[0-9]+]]:vreg_128 = COPY $sgpr0_sgpr1_sgpr2_sgpr3
832+
; GPRIDX: [[S_MOV_B32_:%[0-9]+]]:sreg_32 = S_MOV_B32 0
833+
; GPRIDX: S_SET_GPR_IDX_ON [[S_MOV_B32_]], 1, implicit-def $m0, implicit $m0
834+
; GPRIDX: [[V_MOV_B32_e32_:%[0-9]+]]:vgpr_32 = V_MOV_B32_e32 undef [[COPY]].sub0, implicit $exec, implicit [[COPY]], implicit $m0
835+
; GPRIDX: S_SET_GPR_IDX_OFF
836+
; GPRIDX: S_ENDPGM 0, implicit [[V_MOV_B32_e32_]]
837+
%0:vgpr(<4 x s32>) = COPY $sgpr0_sgpr1_sgpr2_sgpr3
838+
%1:sgpr(s32) = G_CONSTANT i32 0
839+
%2:vgpr(s32) = G_EXTRACT_VECTOR_ELT %0, %1
840+
S_ENDPGM 0, implicit %2
841+
...

llvm/test/CodeGen/AMDGPU/GlobalISel/inst-select-insert-vector-elt.mir

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,3 +624,66 @@ body: |
624624
%5:sgpr(<8 x s32>) = G_INSERT_VECTOR_ELT %0, %1, %4
625625
S_ENDPGM 0, implicit %5
626626
...
627+
628+
# This should have been folded out in the legalizer, but make sure it
629+
# doesn't crash.
630+
---
631+
name: insert_vector_elt_s_s32_v4s32_const_idx
632+
legalized: true
633+
regBankSelected: true
634+
635+
body: |
636+
bb.0:
637+
liveins: $sgpr0_sgpr1_sgpr2_sgpr3, $sgpr4
638+
639+
; MOVREL-LABEL: name: insert_vector_elt_s_s32_v4s32_const_idx
640+
; MOVREL: [[COPY:%[0-9]+]]:sgpr_128 = COPY $sgpr0_sgpr1_sgpr2_sgpr3
641+
; MOVREL: [[COPY1:%[0-9]+]]:sreg_32 = COPY $sgpr4
642+
; MOVREL: [[S_MOV_B32_:%[0-9]+]]:sreg_32 = S_MOV_B32 0
643+
; MOVREL: $m0 = COPY [[S_MOV_B32_]]
644+
; MOVREL: [[S_INDIRECT_REG_WRITE_B32_V4_:%[0-9]+]]:sgpr_128 = S_INDIRECT_REG_WRITE_B32_V4 [[COPY]], [[COPY1]], 1, implicit $m0
645+
; MOVREL: S_ENDPGM 0, implicit [[S_INDIRECT_REG_WRITE_B32_V4_]]
646+
; GPRIDX-LABEL: name: insert_vector_elt_s_s32_v4s32_const_idx
647+
; GPRIDX: [[COPY:%[0-9]+]]:sgpr_128 = COPY $sgpr0_sgpr1_sgpr2_sgpr3
648+
; GPRIDX: [[COPY1:%[0-9]+]]:sreg_32 = COPY $sgpr4
649+
; GPRIDX: [[S_MOV_B32_:%[0-9]+]]:sreg_32 = S_MOV_B32 0
650+
; GPRIDX: $m0 = COPY [[S_MOV_B32_]]
651+
; GPRIDX: [[S_INDIRECT_REG_WRITE_B32_V4_:%[0-9]+]]:sgpr_128 = S_INDIRECT_REG_WRITE_B32_V4 [[COPY]], [[COPY1]], 1, implicit $m0
652+
; GPRIDX: S_ENDPGM 0, implicit [[S_INDIRECT_REG_WRITE_B32_V4_]]
653+
%0:sgpr(<4 x s32>) = COPY $sgpr0_sgpr1_sgpr2_sgpr3
654+
%1:sgpr(s32) = COPY $sgpr4
655+
%2:sgpr(s32) = G_CONSTANT i32 0
656+
%3:sgpr(<4 x s32>) = G_INSERT_VECTOR_ELT %0, %1, %2
657+
S_ENDPGM 0, implicit %3
658+
...
659+
660+
---
661+
name: insert_vector_elt_v_s32_v4s32_const_idx
662+
legalized: true
663+
regBankSelected: true
664+
665+
body: |
666+
bb.0:
667+
liveins: $sgpr0_sgpr1_sgpr2_sgpr3, $sgpr4
668+
669+
; MOVREL-LABEL: name: insert_vector_elt_v_s32_v4s32_const_idx
670+
; MOVREL: [[COPY:%[0-9]+]]:vreg_128 = COPY $sgpr0_sgpr1_sgpr2_sgpr3
671+
; MOVREL: [[COPY1:%[0-9]+]]:sreg_32 = COPY $sgpr4
672+
; MOVREL: [[S_MOV_B32_:%[0-9]+]]:sreg_32 = S_MOV_B32 0
673+
; MOVREL: $m0 = COPY [[S_MOV_B32_]]
674+
; MOVREL: [[V_INDIRECT_REG_WRITE_B32_V4_:%[0-9]+]]:vreg_128 = V_INDIRECT_REG_WRITE_B32_V4 [[COPY]], [[COPY1]], 1, implicit $m0, implicit $exec
675+
; MOVREL: S_ENDPGM 0, implicit [[V_INDIRECT_REG_WRITE_B32_V4_]]
676+
; GPRIDX-LABEL: name: insert_vector_elt_v_s32_v4s32_const_idx
677+
; GPRIDX: [[COPY:%[0-9]+]]:vreg_128 = COPY $sgpr0_sgpr1_sgpr2_sgpr3
678+
; GPRIDX: [[COPY1:%[0-9]+]]:sreg_32 = COPY $sgpr4
679+
; GPRIDX: [[S_MOV_B32_:%[0-9]+]]:sreg_32 = S_MOV_B32 0
680+
; GPRIDX: S_SET_GPR_IDX_ON [[S_MOV_B32_]], 8, implicit-def $m0, implicit $m0
681+
; GPRIDX: [[V_INDIRECT_REG_WRITE_B32_V4_:%[0-9]+]]:vreg_128 = V_INDIRECT_REG_WRITE_B32_V4 [[COPY]], [[COPY1]], 1, implicit $m0, implicit $exec
682+
; GPRIDX: S_SET_GPR_IDX_OFF
683+
; GPRIDX: S_ENDPGM 0, implicit [[V_INDIRECT_REG_WRITE_B32_V4_]]
684+
%0:vgpr(<4 x s32>) = COPY $sgpr0_sgpr1_sgpr2_sgpr3
685+
%1:sgpr(s32) = COPY $sgpr4
686+
%2:sgpr(s32) = G_CONSTANT i32 0
687+
%3:vgpr(<4 x s32>) = G_INSERT_VECTOR_ELT %0, %1, %2
688+
S_ENDPGM 0, implicit %3
689+
...

0 commit comments

Comments
 (0)