[llvm] [AMDGPU] Avoid resource propagation for recursion through multiple functions (PR #111004)

Matt Arsenault via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 8 23:59:44 PDT 2024


================
@@ -91,13 +91,66 @@ MCSymbol *MCResourceInfo::getMaxSGPRSymbol(MCContext &OutContext) {
   return OutContext.getOrCreateSymbol("amdgpu.max_num_sgpr");
 }
 
+static bool findSymbolInExpr(MCSymbol *Sym, const MCExpr *Expr,
+                             SmallVectorImpl<const MCExpr *> &Exprs) {
+  switch (Expr->getKind()) {
+  default:
+    return false;
+  case MCExpr::ExprKind::SymbolRef: {
+    const MCSymbolRefExpr *SymRefExpr = cast<MCSymbolRefExpr>(Expr);
+    const MCSymbol &SymRef = SymRefExpr->getSymbol();
+    if (Sym == &SymRef)
+      return true;
+    if (SymRef.isVariable())
+      Exprs.push_back(SymRef.getVariableValue(/*isUsed=*/false));
+    return false;
+  }
+  case MCExpr::ExprKind::Binary: {
+    const MCBinaryExpr *BExpr = cast<MCBinaryExpr>(Expr);
+    return findSymbolInExpr(Sym, BExpr->getLHS(), Exprs) ||
+           findSymbolInExpr(Sym, BExpr->getRHS(), Exprs);
----------------
arsenm wrote:

Usually if you have to deal with recursive expressions, you track a visited set while walking through the expression, and give up when you encounter already visited entries. You're looking at all the visited expressions each time you visit an expression.

As an example, here's a similar situation for ConstantExprs: https://github.com/llvm/llvm-project/blob/e2dc50c92987e6886b1b1641ebdbe9cc53bae192/llvm/lib/Target/AMDGPU/AMDGPUAttributor.cpp#L221

https://github.com/llvm/llvm-project/pull/111004


More information about the llvm-commits mailing list