Skip to content

Commit 6cc8018

Browse files
committed
[AMDGPU] Prevent cyclic behaviour in SIFoldOperands
In SIFoldOperands::foldOperand, the recursion in REG_SEQUENCE handling could result in infinite loop if UseMI and RSUseMI share a common use operand, flipflopping between two instructions until stack overflows. The fix is to prevent a cycle by using static seenMI set.
1 parent b366643 commit 6cc8018

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

llvm/lib/Target/AMDGPU/SIFoldOperands.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "llvm/ADT/DepthFirstIterator.h"
1616
#include "llvm/CodeGen/MachineFunctionPass.h"
1717
#include "llvm/CodeGen/MachineOperand.h"
18+
#include <unordered_set>
1819

1920
#define DEBUG_TYPE "si-fold-operands"
2021
using namespace llvm;
@@ -74,7 +75,7 @@ class SIFoldOperands : public MachineFunctionPass {
7475
const SIRegisterInfo *TRI;
7576
const GCNSubtarget *ST;
7677
const SIMachineFunctionInfo *MFI;
77-
78+
7879
bool frameIndexMayFold(const MachineInstr &UseMI, int OpNo,
7980
const MachineOperand &OpToFold) const;
8081

@@ -772,7 +773,7 @@ void SIFoldOperands::foldOperand(
772773
if (UseMI->isRegSequence()) {
773774
Register RegSeqDstReg = UseMI->getOperand(0).getReg();
774775
unsigned RegSeqDstSubReg = UseMI->getOperand(UseOpIdx + 1).getImm();
775-
776+
static std::unordered_set<MachineInstr*> seenMI;
776777
for (auto &RSUse : make_early_inc_range(MRI->use_nodbg_operands(RegSeqDstReg))) {
777778
MachineInstr *RSUseMI = RSUse.getParent();
778779

@@ -782,7 +783,11 @@ void SIFoldOperands::foldOperand(
782783

783784
if (RSUse.getSubReg() != RegSeqDstSubReg)
784785
continue;
785-
786+
787+
if (seenMI.count(RSUseMI) != 0)
788+
continue;
789+
seenMI.insert(RSUseMI);
790+
786791
foldOperand(OpToFold, RSUseMI, RSUseMI->getOperandNo(&RSUse), FoldList,
787792
CopiesToReplace);
788793
}

0 commit comments

Comments
 (0)