Skip to content

Commit 71039bb

Browse files
authored
[AMDGPU] Fix register class constraints for si-fold-operands pass when folding immediate into copies (llvm#131387)
Fixes llvm#130020 This fixes an issue where the si-fold-operands pass would incorrectly fold immediate values into COPY instructions targeting av_32 registers. The pass now checks register class constraints before attempting to fold the immediate.
1 parent 1495d13 commit 71039bb

File tree

2 files changed

+77
-49
lines changed

2 files changed

+77
-49
lines changed

llvm/lib/Target/AMDGPU/SIFoldOperands.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1067,6 +1067,16 @@ void SIFoldOperandsImpl::foldOperand(
10671067
if (MovOp == AMDGPU::COPY)
10681068
return;
10691069

1070+
// Fold if the destination register class of the MOV instruction (ResRC)
1071+
// is a superclass of (or equal to) the destination register class of the
1072+
// COPY (DestRC). If this condition fails, folding would be illegal.
1073+
const MCInstrDesc &MovDesc = TII->get(MovOp);
1074+
assert(MovDesc.getNumDefs() > 0 && MovDesc.operands()[0].RegClass != -1);
1075+
const TargetRegisterClass *ResRC =
1076+
TRI->getRegClass(MovDesc.operands()[0].RegClass);
1077+
if (!DestRC->hasSuperClassEq(ResRC))
1078+
return;
1079+
10701080
MachineInstr::mop_iterator ImpOpI = UseMI->implicit_operands().begin();
10711081
MachineInstr::mop_iterator ImpOpE = UseMI->implicit_operands().end();
10721082
while (ImpOpI != ImpOpE) {

llvm/test/CodeGen/AMDGPU/fold-imm-copy.mir

Lines changed: 67 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -202,52 +202,70 @@ body: |
202202
203203
...
204204

205-
# FIXME: Register class restrictions of av register not respected,
206-
# issue 130020
207-
208-
# ---
209-
# name: s_mov_b32_inlineimm_copy_s_to_av_32
210-
# tracksRegLiveness: true
211-
# body: |
212-
# bb.0:
213-
# %0:sreg_32 = S_MOV_B32 32
214-
# %1:av_32 = COPY %0
215-
# $agpr0 = COPY %1
216-
# S_ENDPGM 0
217-
218-
# ...
219-
220-
# ---
221-
# name: v_mov_b32_inlineimm_copy_v_to_av_32
222-
# tracksRegLiveness: true
223-
# body: |
224-
# bb.0:
225-
# %0:vgpr_32 = V_MOV_B32_e32 32, implicit $exec
226-
# %1:av_32 = COPY %0
227-
# $agpr0 = COPY %1
228-
# S_ENDPGM 0
229-
# ...
230-
231-
# ---
232-
# name: s_mov_b32_imm_literal_copy_s_to_av_32
233-
# tracksRegLiveness: true
234-
# body: |
235-
# bb.0:
236-
# %0:sreg_32 = S_MOV_B32 999
237-
# %1:av_32 = COPY %0
238-
# $agpr0 = COPY %1
239-
# S_ENDPGM 0
240-
241-
# ...
242-
243-
# ---
244-
# name: v_mov_b32_imm_literal_copy_v_to_av_32
245-
# tracksRegLiveness: true
246-
# body: |
247-
# bb.0:
248-
# %0:vgpr_32 = V_MOV_B32_e32 999, implicit $exec
249-
# %1:av_32 = COPY %0
250-
# $agpr0 = COPY %1
251-
# S_ENDPGM 0
252-
253-
# ...
205+
---
206+
name: s_mov_b32_inlineimm_copy_s_to_av_32
207+
tracksRegLiveness: true
208+
body: |
209+
bb.0:
210+
; GCN-LABEL: name: s_mov_b32_inlineimm_copy_s_to_av_32
211+
; GCN: [[S_MOV_B32_:%[0-9]+]]:sreg_32 = S_MOV_B32 32
212+
; GCN-NEXT: [[COPY:%[0-9]+]]:av_32 = COPY [[S_MOV_B32_]]
213+
; GCN-NEXT: $agpr0 = COPY [[COPY]]
214+
; GCN-NEXT: S_ENDPGM 0
215+
%0:sreg_32 = S_MOV_B32 32
216+
%1:av_32 = COPY %0
217+
$agpr0 = COPY %1
218+
S_ENDPGM 0
219+
220+
...
221+
222+
---
223+
name: v_mov_b32_inlineimm_copy_v_to_av_32
224+
tracksRegLiveness: true
225+
body: |
226+
bb.0:
227+
; GCN-LABEL: name: v_mov_b32_inlineimm_copy_v_to_av_32
228+
; GCN: [[V_MOV_B32_e32_:%[0-9]+]]:vgpr_32 = V_MOV_B32_e32 32, implicit $exec
229+
; GCN-NEXT: [[COPY:%[0-9]+]]:av_32 = COPY [[V_MOV_B32_e32_]]
230+
; GCN-NEXT: $agpr0 = COPY [[COPY]]
231+
; GCN-NEXT: S_ENDPGM 0
232+
%0:vgpr_32 = V_MOV_B32_e32 32, implicit $exec
233+
%1:av_32 = COPY %0
234+
$agpr0 = COPY %1
235+
S_ENDPGM 0
236+
237+
...
238+
239+
---
240+
name: s_mov_b32_imm_literal_copy_s_to_av_32
241+
tracksRegLiveness: true
242+
body: |
243+
bb.0:
244+
; GCN-LABEL: name: s_mov_b32_imm_literal_copy_s_to_av_32
245+
; GCN: [[S_MOV_B32_:%[0-9]+]]:sreg_32 = S_MOV_B32 999
246+
; GCN-NEXT: [[COPY:%[0-9]+]]:av_32 = COPY [[S_MOV_B32_]]
247+
; GCN-NEXT: $agpr0 = COPY [[COPY]]
248+
; GCN-NEXT: S_ENDPGM 0
249+
%0:sreg_32 = S_MOV_B32 999
250+
%1:av_32 = COPY %0
251+
$agpr0 = COPY %1
252+
S_ENDPGM 0
253+
254+
...
255+
256+
---
257+
name: v_mov_b32_imm_literal_copy_v_to_av_32
258+
tracksRegLiveness: true
259+
body: |
260+
bb.0:
261+
; GCN-LABEL: name: v_mov_b32_imm_literal_copy_v_to_av_32
262+
; GCN: [[V_MOV_B32_e32_:%[0-9]+]]:vgpr_32 = V_MOV_B32_e32 999, implicit $exec
263+
; GCN-NEXT: [[COPY:%[0-9]+]]:av_32 = COPY [[V_MOV_B32_e32_]]
264+
; GCN-NEXT: $agpr0 = COPY [[COPY]]
265+
; GCN-NEXT: S_ENDPGM 0
266+
%0:vgpr_32 = V_MOV_B32_e32 999, implicit $exec
267+
%1:av_32 = COPY %0
268+
$agpr0 = COPY %1
269+
S_ENDPGM 0
270+
271+
...

0 commit comments

Comments
 (0)