Skip to content

Commit 4caed84

Browse files
committed
[AMDGPU][GlobalISel] Fix G_UNMERGE_VALUES combine
Fixes #139791. When trying to combine two G_UNMERGE_VALUES with a COPY between them, a crash occurs in tryCombineUnmergeValues() because getDefIndex() tries to find the index of the original source register in the def found by getDefIgnoringCopies(). In the case of a COPY in between, this register is not present in the def, only in the COPY.
1 parent 66a8341 commit 4caed84

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

llvm/include/llvm/CodeGen/GlobalISel/LegalizationArtifactCombiner.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1067,13 +1067,15 @@ class LegalizationArtifactCombiner {
10671067
GISelChangeObserver &Observer) {
10681068
unsigned NumDefs = MI.getNumDefs();
10691069
Register SrcReg = MI.getSourceReg();
1070-
MachineInstr *SrcDef = getDefIgnoringCopies(SrcReg, MRI);
1071-
if (!SrcDef)
1070+
std::optional<DefinitionAndSourceRegister> DefSrcReg =
1071+
getDefSrcRegIgnoringCopies(SrcReg, MRI);
1072+
if (!DefSrcReg)
10721073
return false;
1074+
MachineInstr *SrcDef = DefSrcReg->MI;
10731075

10741076
LLT OpTy = MRI.getType(SrcReg);
10751077
LLT DestTy = MRI.getType(MI.getReg(0));
1076-
unsigned SrcDefIdx = getDefIndex(*SrcDef, SrcReg);
1078+
unsigned SrcDefIdx = getDefIndex(*SrcDef, DefSrcReg->Reg);
10771079

10781080
Builder.setInstrAndDebugLoc(MI);
10791081

llvm/test/CodeGen/AMDGPU/GlobalISel/legalize-unmerge-values.mir

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1090,3 +1090,24 @@ body: |
10901090
$vgpr9_vgpr10_vgpr11 = COPY %8
10911091
10921092
...
1093+
1094+
---
1095+
name: test_unmerge_through_copy
1096+
body: |
1097+
bb.0:
1098+
liveins: $vgpr0
1099+
1100+
; CHECK-LABEL: name: test_unmerge_through_copy
1101+
; CHECK: liveins: $vgpr0
1102+
; CHECK-NEXT: {{ $}}
1103+
; CHECK-NEXT: [[COPY:%[0-9]+]]:_(s32) = COPY $vgpr0
1104+
; CHECK-NEXT: [[C:%[0-9]+]]:_(s32) = G_CONSTANT i32 255
1105+
; CHECK-NEXT: [[AND:%[0-9]+]]:_(s32) = G_AND [[COPY]], [[C]]
1106+
; CHECK-NEXT: $vgpr0 = COPY [[AND]](s32)
1107+
%0:_(s32) = COPY $vgpr0
1108+
%1:_(s16), %2:_(s16) = G_UNMERGE_VALUES %0:_(s32)
1109+
%3:_(s16) = COPY %1:_(s16)
1110+
%4:_(s8), %5:_(s8) = G_UNMERGE_VALUES %3:_(s16)
1111+
%6:_(s32) = G_ZEXT %4:_(s8)
1112+
$vgpr0 = COPY %6:_(s32)
1113+
...

0 commit comments

Comments
 (0)