Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions llvm/lib/Target/AMDGPU/SIFoldOperands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1068,6 +1068,20 @@ void SIFoldOperandsImpl::foldOperand(
if (MovOp == AMDGPU::COPY)
return;

// Check for common register subclass between destination (DestRC) and MOV
// result (ResRC). If exists, verify this common subclass is a superclass of
// (or equal to) the destination register class, otherwise folding is
// illegal.

const MCInstrDesc &MovDesc = TII->get(MovOp);
assert(MovDesc.getNumDefs() > 0 && MovDesc.operands()[0].RegClass != -1);
const TargetRegisterClass *ResRC =
TRI->getRegClass(MovDesc.operands()[0].RegClass);
const TargetRegisterClass *CommonRC = TRI->getCommonSubClass(DestRC, ResRC);

if (!CommonRC || !DestRC->hasSuperClassEq(CommonRC))
return;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (!CommonRC || !DestRC->hasSuperClassEq(CommonRC))
return;
if (!CommonRC)
return;

hasSuperClassEq should be redundant with finding the common class

Copy link
Contributor Author

@mssefat mssefat Apr 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think hasSuperClassEq is not redundant with finding common subclass. Consider this basic block where the copy operation is illegal:

bb.0:
  %1:av_32 = V_MOV_B32_e32 32, implicit $exec
  $agpr0 = COPY %1:av_32
  S_ENDPGM 0

In this example:

DestRC is AV_32
ResRC is VGPR_32
CommonRC is VGPR_32

If we only check for CommonRC, the check would allow folding in this case since CommonRC exists (VGPR_32). However, folding here is illegal. And I think we need the condition if (!DestRC->hasSuperClassEq(CommonRC)) return;.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you need CommonRC? Isn't the whole check equivalent to DestRC->hasSuperClassEq(ResRC)?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code as you have written it is weird, because CommonRC is by construction a subclass of DestRC, and then you are checking that it is a superclass of DestRC. These can only both be true if CommonRC is DestRC.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They should be equivalent check. I am reverting to the previous use of DestRC->hasSuperClassEq(ResRC). @arsenm please let me know if I am missing something.

MachineInstr::mop_iterator ImpOpI = UseMI->implicit_operands().begin();
MachineInstr::mop_iterator ImpOpE = UseMI->implicit_operands().end();
while (ImpOpI != ImpOpE) {
Expand Down
116 changes: 67 additions & 49 deletions llvm/test/CodeGen/AMDGPU/fold-imm-copy.mir
Original file line number Diff line number Diff line change
Expand Up @@ -202,52 +202,70 @@ body: |

...

# FIXME: Register class restrictions of av register not respected,
# issue 130020

# ---
# name: s_mov_b32_inlineimm_copy_s_to_av_32
# tracksRegLiveness: true
# body: |
# bb.0:
# %0:sreg_32 = S_MOV_B32 32
# %1:av_32 = COPY %0
# $agpr0 = COPY %1
# S_ENDPGM 0

# ...

# ---
# name: v_mov_b32_inlineimm_copy_v_to_av_32
# tracksRegLiveness: true
# body: |
# bb.0:
# %0:vgpr_32 = V_MOV_B32_e32 32, implicit $exec
# %1:av_32 = COPY %0
# $agpr0 = COPY %1
# S_ENDPGM 0
# ...

# ---
# name: s_mov_b32_imm_literal_copy_s_to_av_32
# tracksRegLiveness: true
# body: |
# bb.0:
# %0:sreg_32 = S_MOV_B32 999
# %1:av_32 = COPY %0
# $agpr0 = COPY %1
# S_ENDPGM 0

# ...

# ---
# name: v_mov_b32_imm_literal_copy_v_to_av_32
# tracksRegLiveness: true
# body: |
# bb.0:
# %0:vgpr_32 = V_MOV_B32_e32 999, implicit $exec
# %1:av_32 = COPY %0
# $agpr0 = COPY %1
# S_ENDPGM 0

# ...
---
name: s_mov_b32_inlineimm_copy_s_to_av_32
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New tests need checks for the expected output. Maybe convert the while file to use update_mir_test_checks?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have pushed the updated changes. Could you please review?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ping

tracksRegLiveness: true
body: |
bb.0:
; GCN-LABEL: name: s_mov_b32_inlineimm_copy_s_to_av_32
; GCN: [[S_MOV_B32_:%[0-9]+]]:sreg_32 = S_MOV_B32 32
; GCN-NEXT: [[COPY:%[0-9]+]]:av_32 = COPY [[S_MOV_B32_]]
; GCN-NEXT: $agpr0 = COPY [[COPY]]
; GCN-NEXT: S_ENDPGM 0
%0:sreg_32 = S_MOV_B32 32
%1:av_32 = COPY %0
$agpr0 = COPY %1
S_ENDPGM 0

...

---
name: v_mov_b32_inlineimm_copy_v_to_av_32
tracksRegLiveness: true
body: |
bb.0:
; GCN-LABEL: name: v_mov_b32_inlineimm_copy_v_to_av_32
; GCN: [[V_MOV_B32_e32_:%[0-9]+]]:vgpr_32 = V_MOV_B32_e32 32, implicit $exec
; GCN-NEXT: [[COPY:%[0-9]+]]:av_32 = COPY [[V_MOV_B32_e32_]]
; GCN-NEXT: $agpr0 = COPY [[COPY]]
; GCN-NEXT: S_ENDPGM 0
%0:vgpr_32 = V_MOV_B32_e32 32, implicit $exec
%1:av_32 = COPY %0
$agpr0 = COPY %1
S_ENDPGM 0

...

---
name: s_mov_b32_imm_literal_copy_s_to_av_32
tracksRegLiveness: true
body: |
bb.0:
; GCN-LABEL: name: s_mov_b32_imm_literal_copy_s_to_av_32
; GCN: [[S_MOV_B32_:%[0-9]+]]:sreg_32 = S_MOV_B32 999
; GCN-NEXT: [[COPY:%[0-9]+]]:av_32 = COPY [[S_MOV_B32_]]
; GCN-NEXT: $agpr0 = COPY [[COPY]]
; GCN-NEXT: S_ENDPGM 0
%0:sreg_32 = S_MOV_B32 999
%1:av_32 = COPY %0
$agpr0 = COPY %1
S_ENDPGM 0

...

---
name: v_mov_b32_imm_literal_copy_v_to_av_32
tracksRegLiveness: true
body: |
bb.0:
; GCN-LABEL: name: v_mov_b32_imm_literal_copy_v_to_av_32
; GCN: [[V_MOV_B32_e32_:%[0-9]+]]:vgpr_32 = V_MOV_B32_e32 999, implicit $exec
; GCN-NEXT: [[COPY:%[0-9]+]]:av_32 = COPY [[V_MOV_B32_e32_]]
; GCN-NEXT: $agpr0 = COPY [[COPY]]
; GCN-NEXT: S_ENDPGM 0
%0:vgpr_32 = V_MOV_B32_e32 999, implicit $exec
%1:av_32 = COPY %0
$agpr0 = COPY %1
S_ENDPGM 0

...
Loading