-
Notifications
You must be signed in to change notification settings - Fork 10.5k
SILOptimizer: restructure the apply(partial_apply) peephole and the dead partial_apply elimination optimizations #29703
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
SILOptimizer: restructure the apply(partial_apply) peephole and the dead partial_apply elimination optimizations #29703
Conversation
@swift-ci test |
@swift-ci benchmark |
Performance: -O
Code size: -OPerformance: -Osize
Code size: -OsizePerformance: -Onone
Code size: -swiftlibsHow to read the dataThe tables contain differences in performance which are larger than 8% and differences in code size which are larger than 1%.If you see any unexpected regressions, you should consider fixing the Noise: Sometimes the performance results (not code size!) contain false Hardware Overview
|
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.
Quick nit. I need to read this offline in my editor. I'll do a more in depth in a bit.
/// Returns true, if there are no other users beside those collected in \p | ||
/// destroys, i.e. if \p inst can be considered as "dead". | ||
bool collectDestroys(SingleValueInstruction *inst, | ||
SmallVectorImpl<SILInstruction *> &destroys); |
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.
Can you run this patch through git-clang-format?
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.
okay, done
ccef7b7
to
f170052
Compare
@swift-ci smoke test |
f170052
to
30beefe
Compare
@swift-ci smoke test |
1 similar comment
@swift-ci smoke test |
|
||
void swift::endLifetimeAtFrontier( | ||
SILValue valueOrStackLoc, const ValueLifetimeAnalysis::Frontier &frontier, | ||
SILBuilder &builder) { |
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.
Why pass in a SILBuilder here? Better to pass in a SILBuilderContext and use a locally created SILBuilderWithScope. Also, why are you messing with RegularLocations here?
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.
SILBuilder: ok, makes sense.
The frontier instruction can have a ReturnLocation. Therefore I have to construct a RegularLocation.
@@ -190,7 +197,8 @@ bool ValueLifetimeAnalysis::computeFrontier(Frontier &frontier, Mode mode, | |||
|
|||
for (unsigned i = 0, e = succBlocks.size(); i != e; ++i) { | |||
if (unhandledFrontierBlocks.count(succBlocks[i])) { | |||
assert(isCriticalEdge(term, i) && "actually not a critical edge?"); | |||
assert((isCriticalEdge(term, i) || userSet.count(term)) && |
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.
Can you explain this to me? Why is this change needed? Does userSet.count(term) mean a critical edge?
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.
This is a bug fix.
/// An edge is also considered as "critical" if it has a single precedessor
/// but the predecessor's terminal instruction is a user of the value.
///
// frontier (see below). | ||
assert(deadInSucc && "The final using TermInst must have successors"); | ||
// frontier (see below). Except it is a function exiting TermInst, like | ||
// 'return' or 'throw'. |
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.
Can you expand the comment here why the term inst is an exception. I understand why, it should just be explicit in the code.
} | ||
|
||
builder.setInsertionPoint(insertPoint); | ||
builder.setInsertionPoint(paiAI.getInstruction()); |
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.
Please change this to use a new SILBuilderWithScope that takes in a SILBuilderContext.
/// not be needed anymore with OSSA. | ||
static bool keepArgsOfPartialApplyAlive(PartialApplyInst *pai, | ||
ArrayRef<SILInstruction *> paiUsers, | ||
SILBuilder &builder) { |
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.
Another case where I would suggest passing in a SILBuilderContext and creating builders locally. Will prevent Debug Info bugs.
/// as they may be destroyed/deallocated before the last use by one of the | ||
/// apply instructions. | ||
bool PartialApplyCombiner::copyArgsToTemporaries( | ||
const SmallVectorImpl<FullApplySite> &applies) { |
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.
=> ArrayRef
continue; | ||
ArrayRef<SILParameterInfo> paramList = paiTy->getParameters(); | ||
auto argList = pai->getArgumentOperands(); | ||
paramList = paramList.drop_front(paramList.size() - argList.size()); |
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.
Can we make an API around this on ApplySite? Or on partial apply?
continue; | ||
for (Operand *argOp : argsToHandle) { | ||
SILValue arg = argOp->get(); | ||
int argIdx = argOp->getOperandNumber() - pai->getArgumentOperandNumber(); |
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.
Please don't hard code this. This should be sort sort of higher level API on function conventions or ApplySite.
…ead partial_apply elimination optimizations Changes: * Allow optimizing partial_apply capturing opened existential: we didn't do this originally because it was complicated to insert the required alloc/dealloc_stack instructions at the right places. Now we have the StackNesting utility, which makes this easier. * Support indirect-in parameters. Not super important, but why not? It's also easy to do with the StackNesting utility. * Share code between dead closure elimination and the apply(partial_apply) optimization. It's a bit of refactoring and allowed to eliminate some code which is not used anymore. * Fix an ownership problem: We inserted copies of partial_apply arguments _after_ the partial_apply (which consumes the arguments). * When replacing an apply(partial_apply) -> apply and the partial_apply becomes dead, avoid inserting copies of the arguments twice. These changes don't have any immediate effect on our current benchmarks, but will allow eliminating curry thunks for existentials.
30beefe
to
8578936
Compare
@gottesmm Thanks for the review. I have addressed your points in this new version. Much of the code is just copied from the original version, which is already quite old. That's why SILBuilderContext etc. was not used. But, yes, it makes sense to refresh the code. |
@swift-ci smoke test |
@@ -104,10 +104,20 @@ SILInstruction *SILCombiner::visitPartialApplyInst(PartialApplyInst *PAI) { | |||
if (foldInverseReabstractionThunks(PAI, this)) | |||
return nullptr; | |||
|
|||
tryOptimizeApplyOfPartialApply(PAI, Builder, getInstModCallbacks()); | |||
SILBuilderContext BuilderCtxt(Builder.getModule(), Builder.getTrackingList()); |
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 can get this directly from the builder.
Hi @eeckstein @gottesmm, I'm starting to see a use-before-init on the sanitizer bot after this PR:
This is here:
Logs: https://ci.swift.org/job/oss-lldb-asan-osx/3867/consoleText. The easiest way to repro might be to pass |
This also shows up when building the swift stdlib, so no lldb testing required to repro:
|
Changes:
Allow optimizing partial_apply capturing opened existential: we didn't do this originally because it was complicated to insert the required alloc/dealloc_stack instructions at the right places. Now we have the StackNesting utility, which makes this easier.
Support indirect-in parameters. Not super important, but why not? It's also easy to do with the StackNesting utility.
Share code between dead closure elimination and the apply(partial_apply) optimization. It's a bit of refactoring and allowed to eliminate some code which is not used anymore.
Fix an ownership problem: We inserted copies of partial_apply arguments after the partial_apply (which consumes the arguments).
When replacing an apply(partial_apply) -> apply and the partial_apply becomes dead, avoid inserting copies of the arguments twice.
These changes don't have any immediate effect on our current benchmarks, but will allow eliminating curry thunks for existentials.