Skip to content

Commit dff426c

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 dff426c

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

llvm/lib/Target/AMDGPU/SIFoldOperands.cpp

Lines changed: 6 additions & 1 deletion
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;
@@ -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

@@ -783,6 +784,10 @@ void SIFoldOperands::foldOperand(
783784
if (RSUse.getSubReg() != RegSeqDstSubReg)
784785
continue;
785786

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)