-
Notifications
You must be signed in to change notification settings - Fork 15.1k
Reapply [AMDGPU] Avoid resource propagation for recursion through multiple functions #112251
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
1b88285
Reapply [AMDGPU] Avoid resource propagation for recursion through mul…
JanekvO 5463971
Feedback, merge WorkList iteration in Callee iteration
JanekvO ba27cd9
Recursive walk instead of iterative over a WorkSet
JanekvO f99b859
Feedback: remove redundant check
JanekvO b18ed55
Move isSymbolUsedInExpression to MCExpr, use for recursion detection …
JanekvO a72d1ef
Merge remote-tracking branch 'fork-github/main' into reapply-fix-recu…
JanekvO a60c29f
Conservative register count for recursion, comments, add reg usage in…
JanekvO 8a38d54
Add register use for introduced tests
JanekvO File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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. Returns true if | ||
| // Sym is found within Expr without recursing on Expr, false otherwise. | ||
| static bool findSymbolInExpr(MCSymbol *Sym, const MCExpr *Expr, | ||
| SmallVectorImpl<const MCExpr *> &Exprs, | ||
| SmallPtrSetImpl<const MCExpr *> &Visited) { | ||
| // Skip duplicate visits | ||
| if (!Visited.insert(Expr).second) | ||
| return false; | ||
|
|
||
| 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); | ||
| Exprs.push_back(BExpr->getLHS()); | ||
| Exprs.push_back(BExpr->getRHS()); | ||
| return false; | ||
| } | ||
| case MCExpr::ExprKind::Unary: { | ||
| const MCUnaryExpr *UExpr = cast<MCUnaryExpr>(Expr); | ||
| Exprs.push_back(UExpr->getSubExpr()); | ||
| return false; | ||
| } | ||
| case MCExpr::ExprKind::Target: { | ||
| const AMDGPUMCExpr *AGVK = cast<AMDGPUMCExpr>(Expr); | ||
| for (const MCExpr *E : AGVK->getArgs()) | ||
| Exprs.push_back(E); | ||
| 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. Returns true if Sym exists | ||
| // in Expr or its sub-expressions, false otherwise. | ||
| 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,17 @@ void MCResourceInfo::assignResourceInfoExpr( | |
| if (!Seen.insert(Callee).second) | ||
| continue; | ||
| MCSymbol *CalleeValSym = getSymbol(Callee->getName(), RIK, OutContext); | ||
| ArgExprs.push_back(MCSymbolRefExpr::create(CalleeValSym, OutContext)); | ||
| bool CalleeIsVar = CalleeValSym->isVariable(); | ||
| if (!CalleeIsVar || | ||
| (CalleeIsVar && | ||
|
||
| !foundRecursiveSymbolDef( | ||
| Sym, CalleeValSym->getVariableValue(/*IsUsed=*/false)))) { | ||
| ArgExprs.push_back(MCSymbolRefExpr::create(CalleeValSym, OutContext)); | ||
| } | ||
| } | ||
| 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 +224,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)); | ||
|
|
@@ -165,9 +235,15 @@ void MCResourceInfo::gatherResourceInfo( | |
| if (!Seen.insert(Callee).second) | ||
| continue; | ||
| if (!Callee->isDeclaration()) { | ||
| MCSymbol *calleeValSym = | ||
| MCSymbol *CalleeValSym = | ||
| getSymbol(Callee->getName(), RIK_PrivateSegSize, OutContext); | ||
| ArgExprs.push_back(MCSymbolRefExpr::create(calleeValSym, OutContext)); | ||
| bool CalleeIsVar = CalleeValSym->isVariable(); | ||
| if (!CalleeIsVar || | ||
| (CalleeIsVar && | ||
|
||
| !foundRecursiveSymbolDef( | ||
| Sym, CalleeValSym->getVariableValue(/*IsUsed=*/false)))) { | ||
| ArgExprs.push_back(MCSymbolRefExpr::create(CalleeValSym, OutContext)); | ||
| } | ||
| } | ||
| } | ||
| const MCExpr *localConstExpr = | ||
|
|
@@ -178,8 +254,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) { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
llvm/test/CodeGen/AMDGPU/multi-call-resource-usage-mcexpr.ll
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| ; RUN: llc -mtriple=amdgcn-amd-amdhsa -mcpu=gfx90a < %s | FileCheck %s | ||
|
|
||
| ; CHECK-LABEL: {{^}}qux | ||
| ; CHECK: .set qux.num_vgpr, 0 | ||
| ; CHECK: .set qux.num_agpr, 0 | ||
| ; CHECK: .set qux.numbered_sgpr, 32 | ||
| ; CHECK: .set qux.private_seg_size, 0 | ||
| ; CHECK: .set qux.uses_vcc, 0 | ||
| ; CHECK: .set qux.uses_flat_scratch, 0 | ||
| ; CHECK: .set qux.has_dyn_sized_stack, 0 | ||
| ; CHECK: .set qux.has_recursion, 0 | ||
| ; CHECK: .set qux.has_indirect_call, 0 | ||
| define void @qux() { | ||
| entry: | ||
JanekvO marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ret void | ||
| } | ||
|
|
||
| ; CHECK-LABEL: {{^}}baz | ||
| ; CHECK: .set baz.num_vgpr, max(32, 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(0, 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) | ||
| define void @baz() { | ||
| entry: | ||
| call void @qux() | ||
| ret void | ||
| } | ||
|
|
||
| ; CHECK-LABEL: {{^}}bar | ||
| ; CHECK: .set bar.num_vgpr, max(32, baz.num_vgpr, qux.num_vgpr) | ||
| ; CHECK: .set bar.num_agpr, max(0, baz.num_agpr, qux.num_agpr) | ||
| ; CHECK: .set bar.numbered_sgpr, max(34, baz.numbered_sgpr, qux.numbered_sgpr) | ||
| ; CHECK: .set bar.private_seg_size, 16+(max(baz.private_seg_size, qux.private_seg_size)) | ||
| ; CHECK: .set bar.uses_vcc, or(0, baz.uses_vcc, qux.uses_vcc) | ||
| ; CHECK: .set bar.uses_flat_scratch, or(0, baz.uses_flat_scratch, qux.uses_flat_scratch) | ||
| ; CHECK: .set bar.has_dyn_sized_stack, or(0, baz.has_dyn_sized_stack, qux.has_dyn_sized_stack) | ||
| ; CHECK: .set bar.has_recursion, or(1, baz.has_recursion, qux.has_recursion) | ||
| ; CHECK: .set bar.has_indirect_call, or(0, baz.has_indirect_call, qux.has_indirect_call) | ||
| define void @bar() { | ||
| entry: | ||
| call void @baz() | ||
| call void @qux() | ||
| call void @baz() | ||
| ret void | ||
| } | ||
|
|
||
| ; CHECK-LABEL: {{^}}foo | ||
| ; CHECK: .set foo.num_vgpr, max(32, bar.num_vgpr) | ||
| ; CHECK: .set foo.num_agpr, max(0, bar.num_agpr) | ||
| ; CHECK: .set foo.numbered_sgpr, max(34, bar.numbered_sgpr) | ||
| ; CHECK: .set foo.private_seg_size, 16+(max(bar.private_seg_size)) | ||
| ; CHECK: .set foo.uses_vcc, or(0, bar.uses_vcc) | ||
| ; CHECK: .set foo.uses_flat_scratch, or(0, bar.uses_flat_scratch) | ||
| ; CHECK: .set foo.has_dyn_sized_stack, or(0, bar.has_dyn_sized_stack) | ||
| ; CHECK: .set foo.has_recursion, or(1, bar.has_recursion) | ||
| ; CHECK: .set foo.has_indirect_call, or(0, bar.has_indirect_call) | ||
| define void @foo() { | ||
| entry: | ||
| call void @bar() | ||
| 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(0, 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 | ||
| } | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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're still doing this worklist + visited set for each visited callee. Can you pull this out and merge with the loop over callees?
That is the Seen function set and the Visited MCExprs are probably redundant.
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.
Unfortunately, I wasn't able to remove the
Seenfunction set as creating a newMCSymbolRefExprwill always be unique, even if the sameMCSymbolis used. This means that the newWorkSetwouldn't detect the duplicate and the function resource info expressions may end up with duplicate callees' resource info.