[llvm] [AMDGPU] Prevent cyclic behaviour in SIFoldOperands (PR #82099)

via llvm-commits llvm-commits at lists.llvm.org
Sat Feb 17 00:38:36 PST 2024


https://github.com/choikwa updated https://github.com/llvm/llvm-project/pull/82099

>From dff426cd86aa26c9d02c13ba368c46aa586fa7ff Mon Sep 17 00:00:00 2001
From: Kevin Choi <kevin.choi at amd.com>
Date: Sat, 17 Feb 2024 01:34:47 -0600
Subject: [PATCH] [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.
---
 llvm/lib/Target/AMDGPU/SIFoldOperands.cpp | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/llvm/lib/Target/AMDGPU/SIFoldOperands.cpp b/llvm/lib/Target/AMDGPU/SIFoldOperands.cpp
index 8bf05682cbe7ea..8ad94ee1de910a 100644
--- a/llvm/lib/Target/AMDGPU/SIFoldOperands.cpp
+++ b/llvm/lib/Target/AMDGPU/SIFoldOperands.cpp
@@ -15,6 +15,7 @@
 #include "llvm/ADT/DepthFirstIterator.h"
 #include "llvm/CodeGen/MachineFunctionPass.h"
 #include "llvm/CodeGen/MachineOperand.h"
+#include <unordered_set>
 
 #define DEBUG_TYPE "si-fold-operands"
 using namespace llvm;
@@ -772,7 +773,7 @@ void SIFoldOperands::foldOperand(
   if (UseMI->isRegSequence()) {
     Register RegSeqDstReg = UseMI->getOperand(0).getReg();
     unsigned RegSeqDstSubReg = UseMI->getOperand(UseOpIdx + 1).getImm();
-
+    static std::unordered_set<MachineInstr *> seenMI;
     for (auto &RSUse : make_early_inc_range(MRI->use_nodbg_operands(RegSeqDstReg))) {
       MachineInstr *RSUseMI = RSUse.getParent();
 
@@ -783,6 +784,10 @@ void SIFoldOperands::foldOperand(
       if (RSUse.getSubReg() != RegSeqDstSubReg)
         continue;
 
+      if (seenMI.count(RSUseMI) != 0)
+        continue;
+      seenMI.insert(RSUseMI);
+
       foldOperand(OpToFold, RSUseMI, RSUseMI->getOperandNo(&RSUse), FoldList,
                   CopiesToReplace);
     }



More information about the llvm-commits mailing list