-
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 3 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,76 @@ MCSymbol *MCResourceInfo::getMaxSGPRSymbol(MCContext &OutContext) { | |
| return OutContext.getOrCreateSymbol("amdgpu.max_num_sgpr"); | ||
| } | ||
|
|
||
| // The (partially complete) expression should have no recursion in it. After | ||
| // all, we're trying to avoid recursion using this codepath. | ||
| static bool findSymbolInExpr(MCSymbol *Sym, const MCExpr *Expr, | ||
| SmallVectorImpl<const MCExpr *> &Exprs, | ||
| SmallPtrSetImpl<const MCExpr *> &Visited) { | ||
| // Assert if any of the expressions is already visited (i.e., there is | ||
| // existing recursion). | ||
| assert(!Visited.contains(Expr) && | ||
| "Expr should not exist in Visited as we're avoiding recursion"); | ||
| Visited.insert(Expr); | ||
JanekvO marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| 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, Visited) || | ||
| findSymbolInExpr(Sym, BExpr->getRHS(), Exprs, Visited); | ||
JanekvO marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| case MCExpr::ExprKind::Unary: { | ||
| const MCUnaryExpr *UExpr = cast<MCUnaryExpr>(Expr); | ||
| return findSymbolInExpr(Sym, UExpr->getSubExpr(), Exprs, Visited); | ||
| } | ||
| case MCExpr::ExprKind::Target: { | ||
| const AMDGPUMCExpr *AGVK = cast<AMDGPUMCExpr>(Expr); | ||
| for (const MCExpr *E : AGVK->getArgs()) { | ||
| if (findSymbolInExpr(Sym, E, Exprs, Visited)) | ||
| 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; | ||
| SmallPtrSet<const MCExpr *, 8> Visited; | ||
| WorkList.push_back(Expr); | ||
|
|
||
| while (!WorkList.empty()) { | ||
| const MCExpr *CurExpr = WorkList.pop_back_val(); | ||
| if (findSymbolInExpr(Sym, CurExpr, WorkList, Visited)) | ||
| 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 +173,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 +225,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 +238,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 +257,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.
Document return value