-
Notifications
You must be signed in to change notification settings - Fork 10.5k
⚠️Add a new TempRValue optimization to the CopyForwarding pass. #11350
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
This is a separate optimization that detects short-lived temporaries that can be eliminated. This is necessary now that SILGen no longer performs basic RValue forwarding in some cases. SR-5508: Performance regression in benchmarks caused by removing SILGen peephole for LoadExpr in +0 context
…ization. This is a much more aggressive attempt at removing temporary "rvalues" inside the CopyForwarding pass. It is now handles basic switch CFGs and does some memory disambiguation. This is still insufficient to handle the case that caused the worst benchmark regressions, which are caused by UnfoldSequence. The problem is, that executes an escaping closure during the temporary rvalue's lifetime. The only way around that is to fully analyze both sides of the copy down through all uses to prove they are completely nonescaping. Note, the original intention of the CopyForwarding pass was a very quick Onone peephole that did not require escape analysis or alias analysis.
This version successfully optimizes UnfoldSequence, which is the holy grail. Unfortunately, there's a problem building the stdlib which seems to be a use-after-free. Note that we need to remove copied from CopiedDefs when we eliminate them, but it doesn't seem to be working properly. Once that's fixed, we need to run the benchmarks.
The problem here is that previously the patch was removing the copy instruction instead of the underlying alloc_stack from the def map. We track the alloc_stack, not the def.
The remaining crash can be caused via the following test case. To get it to crash, I needed to run with guard malloc and add -debug. One of the debug statements shows the crash.
|
Given that test case, I can get a ToT compiler to crash. This makes me think that with the changes in this PR that we are hitting an already existing copy forwarding correctness bug that is unrelated to Andy's change. |
Smaller test case:
|
We blow up while processing |
…ating it. Otherwise when we are emitting debug statements via -debug, we get a use after free.
The problem here is that CopyForwarding without Andy's patch tries to log that it is removing an instruction, after it has already erased the instruction. Easy 1 line fix that is unrelated to Andy's patch. Building again. |
Not sure why my build is doing a rebuild. While I am waiting, grabbing food real quick. |
This is the cause of the other crasher:
|
Like I thought the NRVO input values have been modified by eliminating the temporary. This makes the separate pass (or at least finding the copies in a separate loop rather than doing one large computation a safer change).
|
I am going to be at the doctor for an hour or so, I will be back in a bit. |
Erik extracted this optimization into its own pass to by pass the issue that we ran into here. He is doing it in #11361. Closing this PR. |
Thanks for debugging. I should have isolated the new optimization sooner. |
This PR continues the work from #11328. I closed the other PR since I can not push to Andy's branch. Andy is on vacation so is unable to make that change now.