-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[AMDGPU] Avoid resource propagation for recursion through multiple functions #111004
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
ded865a
e217863
2dc357c
10382ea
454c317
6d1ff45
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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); | ||
| } | ||
| case MCExpr::ExprKind::Unary: { | ||
| const MCUnaryExpr *UExpr = cast<MCUnaryExpr>(Expr); | ||
| return findSymbolInExpr(Sym, UExpr->getSubExpr(), Exprs); | ||
| } | ||
| case MCExpr::ExprKind::Target: { | ||
| const AMDGPUMCExpr *AGVK = cast<AMDGPUMCExpr>(Expr); | ||
| for (const MCExpr *E : AGVK->getArgs()) { | ||
| if (findSymbolInExpr(Sym, E, Exprs)) | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Symbols whose values eventually are used through their defines (i.e., | ||
| // recursive) must be avoided. Do a walk over Expr to see if Sym will occur in | ||
| // it. The Expr is an MCExpr given through a callee's equivalent MCSymbol so if | ||
| // no recursion is found Sym can be safely assigned to a (sub-)expr which | ||
| // contains the symbol Expr is associated with. | ||
| static bool foundRecursiveSymbolDef(MCSymbol *Sym, const MCExpr *Expr) { | ||
| SmallVector<const MCExpr *, 8> WorkList; | ||
| WorkList.push_back(Expr); | ||
|
|
||
| while (!WorkList.empty()) { | ||
| const MCExpr *CurExpr = WorkList.pop_back_val(); | ||
| if (findSymbolInExpr(Sym, CurExpr, WorkList)) | ||
| return true; | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| void MCResourceInfo::assignResourceInfoExpr( | ||
| int64_t LocalValue, ResourceInfoKind RIK, AMDGPUMCExpr::VariantKind Kind, | ||
| const MachineFunction &MF, const SmallVectorImpl<const Function *> &Callees, | ||
| MCContext &OutContext) { | ||
| const MCConstantExpr *LocalConstExpr = | ||
| MCConstantExpr::create(LocalValue, OutContext); | ||
| const MCExpr *SymVal = LocalConstExpr; | ||
| MCSymbol *Sym = getSymbol(MF.getName(), RIK, OutContext); | ||
| if (!Callees.empty()) { | ||
| SmallVector<const MCExpr *, 8> ArgExprs; | ||
| // Avoid recursive symbol assignment. | ||
|
|
@@ -110,11 +163,18 @@ void MCResourceInfo::assignResourceInfoExpr( | |
| if (!Seen.insert(Callee).second) | ||
| continue; | ||
| MCSymbol *CalleeValSym = getSymbol(Callee->getName(), RIK, OutContext); | ||
| ArgExprs.push_back(MCSymbolRefExpr::create(CalleeValSym, OutContext)); | ||
| if (CalleeValSym->isVariable()) { | ||
| if (!foundRecursiveSymbolDef( | ||
| Sym, CalleeValSym->getVariableValue(/*isUsed=*/false))) { | ||
| ArgExprs.push_back(MCSymbolRefExpr::create(CalleeValSym, OutContext)); | ||
| } | ||
|
||
| } else { | ||
| ArgExprs.push_back(MCSymbolRefExpr::create(CalleeValSym, OutContext)); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are 2 paths to this push_back, so merge the push_back into one condition |
||
| } | ||
| } | ||
| SymVal = AMDGPUMCExpr::create(Kind, ArgExprs, OutContext); | ||
| if (ArgExprs.size() > 1) | ||
| SymVal = AMDGPUMCExpr::create(Kind, ArgExprs, OutContext); | ||
| } | ||
| MCSymbol *Sym = getSymbol(MF.getName(), RIK, OutContext); | ||
| Sym->setVariableValue(SymVal); | ||
| } | ||
|
|
||
|
|
@@ -155,6 +215,7 @@ void MCResourceInfo::gatherResourceInfo( | |
| // The expression for private segment size should be: FRI.PrivateSegmentSize | ||
| // + max(FRI.Callees, FRI.CalleeSegmentSize) | ||
| SmallVector<const MCExpr *, 8> ArgExprs; | ||
| MCSymbol *Sym = getSymbol(MF.getName(), RIK_PrivateSegSize, OutContext); | ||
| if (FRI.CalleeSegmentSize) | ||
| ArgExprs.push_back( | ||
| MCConstantExpr::create(FRI.CalleeSegmentSize, OutContext)); | ||
|
|
@@ -167,7 +228,15 @@ void MCResourceInfo::gatherResourceInfo( | |
| if (!Callee->isDeclaration()) { | ||
| MCSymbol *calleeValSym = | ||
| getSymbol(Callee->getName(), RIK_PrivateSegSize, OutContext); | ||
| ArgExprs.push_back(MCSymbolRefExpr::create(calleeValSym, OutContext)); | ||
| if (calleeValSym->isVariable()) { | ||
| if (!foundRecursiveSymbolDef( | ||
| Sym, calleeValSym->getVariableValue(/*isUsed=*/false))) { | ||
| ArgExprs.push_back( | ||
| MCSymbolRefExpr::create(calleeValSym, OutContext)); | ||
| } | ||
| } else { | ||
|
||
| ArgExprs.push_back(MCSymbolRefExpr::create(calleeValSym, OutContext)); | ||
| } | ||
| } | ||
| } | ||
| const MCExpr *localConstExpr = | ||
|
|
@@ -178,8 +247,7 @@ void MCResourceInfo::gatherResourceInfo( | |
| localConstExpr = | ||
| MCBinaryExpr::createAdd(localConstExpr, transitiveExpr, OutContext); | ||
| } | ||
| getSymbol(MF.getName(), RIK_PrivateSegSize, OutContext) | ||
| ->setVariableValue(localConstExpr); | ||
| Sym->setVariableValue(localConstExpr); | ||
| } | ||
|
|
||
| auto SetToLocal = [&](int64_t LocalValue, ResourceInfoKind RIK) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| ; RUN: llc -mtriple=amdgcn-amd-amdhsa -mcpu=gfx90a < %s | FileCheck %s | ||
|
|
||
| ; CHECK-LABEL: {{^}}qux | ||
| ; CHECK: .set qux.num_vgpr, max(41, foo.num_vgpr) | ||
| ; CHECK: .set qux.num_agpr, max(0, foo.num_agpr) | ||
| ; CHECK: .set qux.numbered_sgpr, max(34, foo.numbered_sgpr) | ||
| ; CHECK: .set qux.private_seg_size, 16 | ||
| ; CHECK: .set qux.uses_vcc, or(1, foo.uses_vcc) | ||
| ; CHECK: .set qux.uses_flat_scratch, or(0, foo.uses_flat_scratch) | ||
| ; CHECK: .set qux.has_dyn_sized_stack, or(0, foo.has_dyn_sized_stack) | ||
| ; CHECK: .set qux.has_recursion, or(1, foo.has_recursion) | ||
| ; CHECK: .set qux.has_indirect_call, or(0, foo.has_indirect_call) | ||
|
|
||
| ; CHECK-LABEL: {{^}}baz | ||
| ; CHECK: .set baz.num_vgpr, max(42, qux.num_vgpr) | ||
| ; CHECK: .set baz.num_agpr, max(0, qux.num_agpr) | ||
| ; CHECK: .set baz.numbered_sgpr, max(34, qux.numbered_sgpr) | ||
| ; CHECK: .set baz.private_seg_size, 16+(max(qux.private_seg_size)) | ||
| ; CHECK: .set baz.uses_vcc, or(1, qux.uses_vcc) | ||
| ; CHECK: .set baz.uses_flat_scratch, or(0, qux.uses_flat_scratch) | ||
| ; CHECK: .set baz.has_dyn_sized_stack, or(0, qux.has_dyn_sized_stack) | ||
| ; CHECK: .set baz.has_recursion, or(1, qux.has_recursion) | ||
| ; CHECK: .set baz.has_indirect_call, or(0, qux.has_indirect_call) | ||
|
|
||
| ; CHECK-LABEL: {{^}}bar | ||
| ; CHECK: .set bar.num_vgpr, max(42, baz.num_vgpr) | ||
| ; CHECK: .set bar.num_agpr, max(0, baz.num_agpr) | ||
| ; CHECK: .set bar.numbered_sgpr, max(34, baz.numbered_sgpr) | ||
| ; CHECK: .set bar.private_seg_size, 16+(max(baz.private_seg_size)) | ||
| ; CHECK: .set bar.uses_vcc, or(1, baz.uses_vcc) | ||
| ; CHECK: .set bar.uses_flat_scratch, or(0, baz.uses_flat_scratch) | ||
| ; CHECK: .set bar.has_dyn_sized_stack, or(0, baz.has_dyn_sized_stack) | ||
| ; CHECK: .set bar.has_recursion, or(1, baz.has_recursion) | ||
| ; CHECK: .set bar.has_indirect_call, or(0, baz.has_indirect_call) | ||
|
|
||
| ; CHECK-LABEL: {{^}}foo | ||
| ; CHECK: .set foo.num_vgpr, 42 | ||
| ; CHECK: .set foo.num_agpr, 0 | ||
| ; CHECK: .set foo.numbered_sgpr, 34 | ||
| ; CHECK: .set foo.private_seg_size, 16 | ||
| ; CHECK: .set foo.uses_vcc, 1 | ||
| ; CHECK: .set foo.uses_flat_scratch, 0 | ||
| ; CHECK: .set foo.has_dyn_sized_stack, 0 | ||
| ; CHECK: .set foo.has_recursion, 1 | ||
| ; CHECK: .set foo.has_indirect_call, 0 | ||
|
|
||
| define void @foo() { | ||
| entry: | ||
| call void @bar() | ||
| ret void | ||
| } | ||
|
|
||
| define void @bar() { | ||
| entry: | ||
| call void @baz() | ||
| ret void | ||
| } | ||
|
|
||
| define void @baz() { | ||
| entry: | ||
| call void @qux() | ||
| ret void | ||
| } | ||
|
|
||
| define void @qux() { | ||
| entry: | ||
| call void @foo() | ||
| ret void | ||
| } | ||
|
|
||
| ; CHECK-LABEL: {{^}}usefoo | ||
| ; CHECK: .set usefoo.num_vgpr, max(32, foo.num_vgpr) | ||
| ; CHECK: .set usefoo.num_agpr, max(0, foo.num_agpr) | ||
| ; CHECK: .set usefoo.numbered_sgpr, max(33, foo.numbered_sgpr) | ||
| ; CHECK: .set usefoo.private_seg_size, 0+(max(foo.private_seg_size)) | ||
| ; CHECK: .set usefoo.uses_vcc, or(1, foo.uses_vcc) | ||
| ; CHECK: .set usefoo.uses_flat_scratch, or(1, foo.uses_flat_scratch) | ||
| ; CHECK: .set usefoo.has_dyn_sized_stack, or(0, foo.has_dyn_sized_stack) | ||
| ; CHECK: .set usefoo.has_recursion, or(1, foo.has_recursion) | ||
| ; CHECK: .set usefoo.has_indirect_call, or(0, foo.has_indirect_call) | ||
| define amdgpu_kernel void @usefoo() { | ||
| call void @foo() | ||
| ret void | ||
| } | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You have the worklist, but aren't using it to avoid the recursive call? Couldn't this also add duplicate entries?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Correct me if my interpretation of the comment is wrong but AFAIK, it cannot recurse locally within
findSymbolInExprasExprsis only populated with Symbols that are assigned an MCExpr (which is what we're in the middle of doing for MCSymbolSym).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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:
llvm-project/llvm/lib/Target/AMDGPU/AMDGPUAttributor.cpp
Line 221 in e2dc50c
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I assume the
getConstantAccessexample walks over an already completed expression. My approach here is more preventative in terms of recursion so no recursion should exist, at all. I've added a Visited set but more so to ensure the invariant of no recursion to be upheld + added more comments that hopefully explain that this walk is to avoid recursion rather than detect and remove recursion.