Skip to content

Commit f6e728c

Browse files
stefan-iligcbot
authored andcommitted
Avoid recursion in isRegionInvariant
Switch to stack in isRegionInvariant.
1 parent ab814f2 commit f6e728c

File tree

2 files changed

+38
-23
lines changed

2 files changed

+38
-23
lines changed

IGC/Compiler/CISACodeGen/WIAnalysis.cpp

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -891,33 +891,48 @@ void WIAnalysisRunner::calculate_dep(const Value* val)
891891
}
892892
}
893893

894-
bool WIAnalysisRunner::isRegionInvariant(const llvm::Instruction* defi, BranchInfo* brInfo, unsigned level)
894+
bool WIAnalysisRunner::isRegionInvariant(const llvm::Instruction* defi, BranchInfo* brInfo)
895895
{
896-
if (level >= 4)
897-
{
898-
return false;
899-
}
900-
if (isa<PHINode>(defi))
901-
{
902-
return false;
903-
}
904-
const unsigned nOps = defi->getNumOperands();
905-
for (unsigned i = 0; i < nOps; ++i)
896+
constexpr uint8_t MAX_DEPTH = 4;
897+
struct RegionOperand{
898+
const llvm::Instruction* inst;
899+
uint8_t operandNum;
900+
};
901+
902+
llvm::SmallVector<RegionOperand, MAX_DEPTH> operands;
903+
operands.push_back({defi, 0});
904+
905+
while (!operands.empty())
906906
{
907-
Value* op = defi->getOperand(i);
908-
Instruction* srci = dyn_cast<Instruction>(op);
909-
if (srci)
907+
auto& rop = operands.back();
908+
if (isa<PHINode>(rop.inst))
910909
{
911-
if (!brInfo->influence_region.count(srci->getParent()))
912-
{
913-
// go on to check the next operand
914-
continue;
915-
}
916-
else if (!isRegionInvariant(srci, brInfo, level + 1))
910+
return false;
911+
}
912+
913+
if (rop.inst->getNumOperands() < rop.operandNum)
914+
{
915+
Value* op = rop.inst->getOperand(rop.operandNum);
916+
rop.operandNum++;
917+
auto* srci = dyn_cast<Instruction>(op);
918+
if (srci)
917919
{
918-
return false;
920+
if (!brInfo->influence_region.count(srci->getParent()))
921+
{
922+
// go on to check the next operand
923+
continue;
924+
}
925+
if (operands.size() + 1 > MAX_DEPTH)
926+
{
927+
return false;
928+
}
929+
operands.push_back({srci, 0});
919930
}
920931
}
932+
else
933+
{
934+
operands.pop_back();
935+
}
921936
}
922937
return true;
923938
}
@@ -1038,7 +1053,7 @@ void WIAnalysisRunner::update_cf_dep(const IGCLLVM::TerminatorInst* inst)
10381053
// all the sources are outside the region.
10391054
// However this is only as good as we can get because we
10401055
// only search limited depth
1041-
if (isRegionInvariant(defi, &br_info, 0))
1056+
if (isRegionInvariant(defi, &br_info))
10421057
{
10431058
continue;
10441059
}

IGC/Compiler/CISACodeGen/WIAnalysis.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ namespace IGC
232232
bool isInstructionSimple(const llvm::Instruction* inst);
233233

234234
/// @brief return true if all the source operands are defined outside the region
235-
bool isRegionInvariant(const llvm::Instruction* inst, BranchInfo* brInfo, unsigned level);
235+
bool isRegionInvariant(const llvm::Instruction* inst, BranchInfo* brInfo);
236236

237237
/// @brief return true if instruction is used as lane ID in subgroup broadcast
238238
bool isUsedByWaveBroadcastAsLocalID(const llvm::Instruction* inst);

0 commit comments

Comments
 (0)