Skip to content

Commit 6de3d1d

Browse files
committed
[SIFoldOperands] Folding immediate into a copy invalidates candidates in the fold list
Fixes SWDEV-542372
1 parent b919f2e commit 6de3d1d

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

llvm/lib/Target/AMDGPU/SIFoldOperands.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1761,7 +1761,9 @@ bool SIFoldOperandsImpl::foldInstOperand(MachineInstr &MI,
17611761
for (MachineInstr *Copy : CopiesToReplace)
17621762
Copy->addImplicitDefUseOperands(*MF);
17631763

1764-
for (FoldCandidate &Fold : FoldList) {
1764+
for (auto FoldIt = FoldList.begin(), End = FoldList.end(); FoldIt != End;
1765+
++FoldIt) {
1766+
FoldCandidate &Fold = *FoldIt;
17651767
assert(!Fold.isReg() || Fold.Def.OpToFold);
17661768
if (Fold.isReg() && Fold.getReg().isVirtual()) {
17671769
Register Reg = Fold.getReg();
@@ -1785,9 +1787,13 @@ bool SIFoldOperandsImpl::foldInstOperand(MachineInstr &MI,
17851787

17861788
if (Fold.isImm() && tryConstantFoldOp(Fold.UseMI)) {
17871789
LLVM_DEBUG(dbgs() << "Constant folded " << *Fold.UseMI);
1790+
// The instruction was folded into a copy, we have to skip any other
1791+
// occurence of UseMI in the fold list
1792+
End = std::remove_if(FoldIt + 1, End, [&](const FoldCandidate &F) {
1793+
return F.UseMI == Fold.UseMI;
1794+
});
17881795
Changed = true;
17891796
}
1790-
17911797
} else if (Fold.Commuted) {
17921798
// Restoring instruction's original operand order if fold has failed.
17931799
TII->commuteInstruction(*Fold.UseMI, false);

llvm/test/CodeGen/AMDGPU/si-fold-operands-swdev-542372.ll

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ define amdgpu_kernel void @kernel() {
2222
; CHECK-NEXT: s_mov_b32 s1, s0
2323
; CHECK-NEXT: v_mov_b32_e32 v2, 0
2424
; CHECK-NEXT: v_pk_mov_b32 v[0:1], s[0:1], s[0:1] op_sel:[0,1]
25-
; CHECK-NEXT: s_or_b32 s0, 0, 0
2625
; CHECK-NEXT: s_cmp_lg_u32 s0, 0
2726
; CHECK-NEXT: ds_write_b64 v2, v[0:1]
2827
; CHECK-NEXT: .LBB0_3: ; %land.end59

0 commit comments

Comments
 (0)