-
Notifications
You must be signed in to change notification settings - Fork 13.6k
[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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ded865a
[AMDGPU] Avoid metadata propagation for recursion through multiple fu…
JanekvO e217863
Different approach, now walks over the sub-symbols and searches for u…
JanekvO 2dc357c
Comments, Visited set with assert to make sure no recursion exists in…
JanekvO 10382ea
Feedback, use WorkList instead of recurse, llvm_unreachable instead of
JanekvO 454c317
Feedback, comments, merge nested conditional
JanekvO 6d1ff45
Formatting
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,77 @@ 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) { | ||
// Assert if any of the expressions is already visited (i.e., there is | ||
// existing recursion). | ||
if (!Visited.insert(Expr).second) | ||
llvm_unreachable("already visited expression"); | ||
|
||
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 +174,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)); | ||
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)); | ||
|
@@ -165,9 +236,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 +255,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
85 changes: 85 additions & 0 deletions
85
llvm/test/CodeGen/AMDGPU/recursive-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,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 | ||
} | ||
|
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.
What's the advantage of this over
assert
? Is it becausellvm_unreachable
can be enabled separately?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.
Doesn't require the annoying variable only used in asserts builds problem
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 guess the compiler will optimize out the unused variable if
llvm_unreachable
is a no-op. Guess it'svs
Roughly equivalent so I guess it's up to taste.