Skip to content

Commit 402b989

Browse files
authored
AMDGPU: Fix assert when multi operands to update after folding imm (llvm#148205)
In the original motivating test case, [FoldList](https://github.com/llvm/llvm-project/blob/d8a2141ff98ee35cd1886f536ccc3548b012820b/llvm/lib/Target/AMDGPU/SIFoldOperands.cpp#L1764) had entries: ``` #0: UseMI: %224:sreg_32 = S_OR_B32 %219.sub0:sreg_64, %219.sub1:sreg_64, implicit-def dead $scc UseOpNo: 1 #1: UseMI: %224:sreg_32 = S_OR_B32 %219.sub0:sreg_64, %219.sub1:sreg_64, implicit-def dead $scc UseOpNo: 2 ``` After calling [updateOperand(#0)](https://github.com/llvm/llvm-project/blob/d8a2141ff98ee35cd1886f536ccc3548b012820b/llvm/lib/Target/AMDGPU/SIFoldOperands.cpp#L1773), [tryConstantFoldOp(#0.UseMI)](https://github.com/llvm/llvm-project/blob/d8a2141ff98ee35cd1886f536ccc3548b012820b/llvm/lib/Target/AMDGPU/SIFoldOperands.cpp#L1786) removed operand 1, and entry #&llvm#8203;1.UseOpNo was no longer valid, resulting in an [assert](https://github.com/llvm/llvm-project/blob/4a35214bddbb67f9597a500d48ab8c4fb25af150/llvm/include/llvm/ADT/ArrayRef.h#L452). This change defers constant folding until all operands have been updated so that UseOpNo values remain stable.
1 parent 3b8a18c commit 402b989

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

llvm/lib/Target/AMDGPU/SIFoldOperands.cpp

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

1764+
SetVector<MachineInstr *> ConstantFoldCandidates;
17641765
for (FoldCandidate &Fold : FoldList) {
17651766
assert(!Fold.isReg() || Fold.Def.OpToFold);
17661767
if (Fold.isReg() && Fold.getReg().isVirtual()) {
@@ -1783,16 +1784,21 @@ bool SIFoldOperandsImpl::foldInstOperand(MachineInstr &MI,
17831784
<< static_cast<int>(Fold.UseOpNo) << " of "
17841785
<< *Fold.UseMI);
17851786

1786-
if (Fold.isImm() && tryConstantFoldOp(Fold.UseMI)) {
1787-
LLVM_DEBUG(dbgs() << "Constant folded " << *Fold.UseMI);
1788-
Changed = true;
1789-
}
1787+
if (Fold.isImm())
1788+
ConstantFoldCandidates.insert(Fold.UseMI);
17901789

17911790
} else if (Fold.Commuted) {
17921791
// Restoring instruction's original operand order if fold has failed.
17931792
TII->commuteInstruction(*Fold.UseMI, false);
17941793
}
17951794
}
1795+
1796+
for (MachineInstr *MI : ConstantFoldCandidates) {
1797+
if (tryConstantFoldOp(MI)) {
1798+
LLVM_DEBUG(dbgs() << "Constant folded " << *MI);
1799+
Changed = true;
1800+
}
1801+
}
17961802
return true;
17971803
}
17981804

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py UTC_ARGS: --version 5
2+
# RUN: llc -mtriple=amdgcn-amd-hsa -mcpu=gfx1031 -run-pass=si-fold-operands -o - %s | FileCheck %s
3+
---
4+
name: snork
5+
body: |
6+
bb.0:
7+
; CHECK-LABEL: name: snork
8+
; CHECK: [[S_MOV_B32_:%[0-9]+]]:sreg_32 = S_MOV_B32 0
9+
; CHECK-NEXT: [[REG_SEQUENCE:%[0-9]+]]:sgpr_128 = REG_SEQUENCE [[S_MOV_B32_]], %subreg.sub0, [[S_MOV_B32_]], %subreg.sub1, [[S_MOV_B32_]], %subreg.sub2, [[S_MOV_B32_]], %subreg.sub3
10+
; CHECK-NEXT: SI_RETURN
11+
%0:sreg_32 = S_MOV_B32 0
12+
%1:sgpr_128 = REG_SEQUENCE %0, %subreg.sub0, %0, %subreg.sub1, %0, %subreg.sub2, %0, %subreg.sub3
13+
%2:sreg_32 = S_OR_B32 %1.sub0, %1.sub3, implicit-def dead $scc
14+
SI_RETURN
15+
...

0 commit comments

Comments
 (0)