Skip to content

Commit 2dc357c

Browse files
committed
Comments, Visited set with assert to make sure no recursion exists in the expression
1 parent e217863 commit 2dc357c

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

llvm/lib/Target/AMDGPU/AMDGPUMCResourceInfo.cpp

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,17 @@ MCSymbol *MCResourceInfo::getMaxSGPRSymbol(MCContext &OutContext) {
9191
return OutContext.getOrCreateSymbol("amdgpu.max_num_sgpr");
9292
}
9393

94+
// The (partially complete) expression should have no recursion in it. After
95+
// all, we're trying to avoid recursion using this codepath.
9496
static bool findSymbolInExpr(MCSymbol *Sym, const MCExpr *Expr,
95-
SmallVectorImpl<const MCExpr *> &Exprs) {
97+
SmallVectorImpl<const MCExpr *> &Exprs,
98+
SmallPtrSetImpl<const MCExpr *> &Visited) {
99+
// Assert if any of the expressions is already visited (i.e., there is
100+
// existing recursion).
101+
assert(!Visited.contains(Expr) &&
102+
"Expr should not exist in Visited as we're avoiding recursion");
103+
Visited.insert(Expr);
104+
96105
switch (Expr->getKind()) {
97106
default:
98107
return false;
@@ -107,17 +116,17 @@ static bool findSymbolInExpr(MCSymbol *Sym, const MCExpr *Expr,
107116
}
108117
case MCExpr::ExprKind::Binary: {
109118
const MCBinaryExpr *BExpr = cast<MCBinaryExpr>(Expr);
110-
return findSymbolInExpr(Sym, BExpr->getLHS(), Exprs) ||
111-
findSymbolInExpr(Sym, BExpr->getRHS(), Exprs);
119+
return findSymbolInExpr(Sym, BExpr->getLHS(), Exprs, Visited) ||
120+
findSymbolInExpr(Sym, BExpr->getRHS(), Exprs, Visited);
112121
}
113122
case MCExpr::ExprKind::Unary: {
114123
const MCUnaryExpr *UExpr = cast<MCUnaryExpr>(Expr);
115-
return findSymbolInExpr(Sym, UExpr->getSubExpr(), Exprs);
124+
return findSymbolInExpr(Sym, UExpr->getSubExpr(), Exprs, Visited);
116125
}
117126
case MCExpr::ExprKind::Target: {
118127
const AMDGPUMCExpr *AGVK = cast<AMDGPUMCExpr>(Expr);
119128
for (const MCExpr *E : AGVK->getArgs()) {
120-
if (findSymbolInExpr(Sym, E, Exprs))
129+
if (findSymbolInExpr(Sym, E, Exprs, Visited))
121130
return true;
122131
}
123132
return false;
@@ -132,11 +141,12 @@ static bool findSymbolInExpr(MCSymbol *Sym, const MCExpr *Expr,
132141
// contains the symbol Expr is associated with.
133142
static bool foundRecursiveSymbolDef(MCSymbol *Sym, const MCExpr *Expr) {
134143
SmallVector<const MCExpr *, 8> WorkList;
144+
SmallPtrSet<const MCExpr *, 8> Visited;
135145
WorkList.push_back(Expr);
136146

137147
while (!WorkList.empty()) {
138148
const MCExpr *CurExpr = WorkList.pop_back_val();
139-
if (findSymbolInExpr(Sym, CurExpr, WorkList))
149+
if (findSymbolInExpr(Sym, CurExpr, WorkList, Visited))
140150
return true;
141151
}
142152

0 commit comments

Comments
 (0)