-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Extend AllocBoxToStack to handle apply #31974
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
Conversation
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.
Some quick notes. I want to look through it in more detail.
@swift-ci test |
@swift-ci Please Test Source Compatibility |
Build failed |
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.
Some more comments around efficiency. I think I need to read it in source to understand the algorithmic change.
private functions on specialization were being given shared linkage. Use swift::getSpecializeLinkage to correctly get the linkage for the specialized function based on the linkage of the original function.
@swift-ci test |
@swift-ci Please Test Source Compatibility |
Build failed |
Build failed |
AllocBoxToStack analyzes the uses of boxes and promotes them to stack if it is safe to do so. Currently the analysis is limited to only a few known users including partial_apply. With this change, the pass also analyzes apply users, where the callee is a local private function. The analysis is recursive and bound by a threshold. Fixes rdar://59070139
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 have a few more comments but we can do them in a follow on commit. I think this code is beautiful. = ). The tests you added handled all the cases. Large +1 from me! = ).
} | ||
|
||
static void rewritePartialApplies(AllocBoxToStackState &pass) { | ||
llvm::DenseMap<PartialApplyInst *, ArgIndexList> IndexMap; | ||
static void rewriteApplies(AllocBoxToStackState &pass) { |
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.
As a follow on commit, can you rename this to something more descriptive like: rewriteApplySites? If you use Applies here it would mean an ApplyInst to me. But again, I don't want that to hold up this commit.
AllocBoxToStackState &pass) { | ||
auto *FRI = cast<FunctionRefInst>(PartialApply->getCallee()); | ||
assert(FRI && "Expected a direct partial_apply!"); | ||
static SILInstruction *specializeApply(SILOptFunctionBuilder &FuncBuilder, |
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.
In a follow on commit, can you rename this to specializeApplySite?
/// Specialize a partial_apply by promoting the parameters indicated by | ||
// While cloning during specialization, make sure apply instructions do not have | ||
// box arguments that need to be promoted. | ||
// This is an assertion in debug builds only. This should never be true because |
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.
In a follow on commit, can you add that the reason why this is true? My thought:
This is an assertion in debug builds only. The reason why this should never be true is that we have cloned our callees in DFS order meaning that any of our callees that had a promotable box will have already have been promoted away by the time this runs.
The part about those promotable boxes having already been handled is missing from the comment!
/// true if this partial apply will not block our promoting the box. | ||
static bool checkPartialApplyBody(Operand *O) { | ||
/// true if this apply will not block our promoting the box. | ||
static bool checkLocalApplyBody(Operand *O, |
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.
In a follow on commit, can you give this a more descriptive name/improve the comments? The big thing here missing from the comment and from the name of the function is that we are /not/ only checking for blocking uses. Specifically, we are also using this to recursively specialize a chain of applies and that since we are visiting recursively, we add the operands (which are in different functions) to the list in DFS order due to the nature of stack recursion.
My hope is that I get that information from the name of this function/the comment. I have a similar issue with findUnexpectedBoxUse. With those names/descriptions it is confusing that you are seeding the algorithm.
//===----------------------------------------------------------------------===// | ||
static llvm::cl::opt<unsigned> MaxLocalApplyRecurDepth( | ||
"max-local-apply-recur-depth", llvm::cl::init(4), | ||
llvm::cl::desc("Mac recursove depth for analyzing local functions")); |
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.
In a follow on commit: Mac
=> Max
.
case ApplySiteKind::ApplyInst: | ||
case ApplySiteKind::BeginApplyInst: | ||
case ApplySiteKind::TryApplyInst: | ||
if (isEligibleApply(Apply) && |
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.
Question, shouldn't isEligibleApply really be isOptimizableLocalApply? Anyways, just a random thought. Feel free to change it in a follow on commit if it makes sense to you. Otherwise, I am ok with this.
@@ -364,12 +422,13 @@ static InFlightDiagnostic diagnose(ASTContext &Context, SourceLoc loc, | |||
/// canPromoteAllocBox - Can we promote this alloc_box to an alloc_stack? | |||
static bool canPromoteAllocBox(AllocBoxInst *ABI, | |||
SmallVectorImpl<Operand *> &PromotedOperands) { | |||
SmallPtrSet<SILFunction *, 8> VisitedCallees; | |||
// Scan all of the uses of the address of the box to see if any | |||
// disqualifies the box from being promoted to the stack. | |||
if (auto *User = findUnexpectedBoxUse(ABI, |
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.
In a follow on commit, can you add a comment here saying that this initializes PromotedOperands from our recursive dfs walk of the call graph. I just want that to be very clear. Right now you need to read the code to understand that. I shouldn't have to read the code.
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.
Just saw you did an update rather than follow on commits. Took a quick look. All of this can be done in a follow on commit as well.
findUnexpectedBoxUse(SILValue Box, bool examinePartialApply, | ||
bool inAppliedFunction, | ||
SmallVectorImpl<Operand *> &PromotedOperands) { | ||
findUnexpectedBoxUseInDFSOrder(SILValue Box, bool inAppliedFunction, |
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.
My thought:
recursivelyFindBoxOperandsPromotableToAddress
The reason I am pushing on this is that the important thing here is computing PromotedOperands. That we return the unexpected use is just a side-effect. Your thoughts?
That being said. The comment is beautiful and is exactly what I hoped for!
// AllocBoxStack opt promotes boxes passed to a chain of applies when it is | ||
// safe to do so. All such applies have to be specialized to take pointer | ||
// arguments instead of box arguments This has to be done in dfs order. | ||
// PromotedOperands is already in dfs order. Build AppliesToSpecialize in |
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 would make it clear that is it already in dfs order due to how it is constructed by find**. Someone reading this code needs a back reference to the code above.
@swift-ci test |
@swift-ci test source compatibility |
Build failed |
@swift-ci smoke test |
@swift-ci test |
Build failed |
@swift-ci Please Test Source Compatibility |
Build failed |
AllocBoxToStack analyzes the uses of boxes and promotes them to stack if it is safe to do so. Currently the analysis is limited to only a few known users including partial_apply.
With this change, the pass also analyzes apply users, where the callee is a local private function. The analysis is recursive and bound by a threshold.
Fixes rdar://59070139