-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Optimize dead private global variables #28324
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
Optimize dead private global variables #28324
Conversation
cc @eeckstein |
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 looks great. I have some optional suggestions for further generalizations.
I moved the collection of instructions / global variables into a I'll finish writing tests tomorrow. |
Any other review comments (or comments I missed)? |
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.
Sorry for the delay in responding to this. Thanksgiving week was a holiday for us.
lib/SILOptimizer/IPO/GlobalOpt.cpp
Outdated
addr->eraseFromParent(); | ||
} | ||
|
||
reset(); |
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 worry that calling collect() every time something changes is going to turn this pass into an O(n^2) algorithm. Is there a way to build a list of all instructions to be deleted, and then delete them at the end, and finally reset()?
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.
Before deleting globals, I need to recollect everything in order to figure out what's now-dead. This will make it an O(n2) -- is that OK?
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.
That's OK (and it answers one of my questions in the latest review) but my guess would be that it should be possible to determine in one pass both which instructions to delete, and which SIL globals become dead as a result.
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.
Yes, see my comment.
@swift-ci Please smoke test |
@slavapestov thanks for the review! I'll resolve your comment and look into the build failure. |
The build failure occurred because a) I didn't exit early in Also, I've updated all uses of |
… in the global accessor function
@slavapestov I've addressed your comments and fixed some of the tests. While debugging one of the tests, I discovered that I'll try to run all the tests tonight and fix the remaining failing tests. |
@@ -20,6 +20,7 @@ | |||
#include "swift/SIL/SILCloner.h" | |||
#include "swift/SIL/SILGlobalVariable.h" | |||
#include "swift/SIL/SILInstruction.h" | |||
#include "swift/SIL/SILInstructionWorklist.h" |
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 no longer needed.
@swift-ci Please test |
@swift-ci Please test source compatibility |
@swift-ci Please benchmark |
Build failed |
Build failed |
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
|
It looks like this patch caused a regression in |
Maybe... it could be just that the test needs to be updated because the global is optimised out? The test is not supported on macOS so it was skipped. (Also, do you mind squashing your commits? Patch has grown to nearly 30 commits). |
@zoecarver it looks like a real regression. Can you please investigate this before merging? |
Oh, CI logs said “UNSUPPORTED” so I assumed it was skipped. |
Wait, you are right. This test requires the stdlib to be built in no-assert mode. I think we are doing this only on linux. So it's in fact not running on our macOS CI bot. But it's not disabled for macOS in general. |
Alrighty, this will be fun to debug :) I suspect it has to do with the modified collection of globals. @theblixguy I'll rebase after I fix the regression. |
@@ -5,7 +5,7 @@ | |||
// RUN: %target-build-swift -O -module-name=test %s -o %t/a.out | |||
// RUN: %target-run %t/a.out | %FileCheck %s -check-prefix=CHECK-OUTPUT | |||
|
|||
// REQUIRES: executable_test,swift_stdlib_no_asserts,optimized_stdlib | |||
// REQUIRES: executable_test,swift_stdlib_no_asserts |
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.
@eeckstein git blame showed that you were the one who added this requirement. The test passes without optimized_stdlib
do you think this is an OK change?
I finally was able to reproduce/fix the issue. Thanks for the patience. Could someone re-trigger the bots? |
Sorry, we were all out on vacation, again. @swift-ci Please smoke test |
@swift-ci Please smoke test |
Friendly ping. Does this need any more reviewing or can it be merged? |
This optimization removes private global variables when the only use is an allocation and a store instruction. To make sure I didn't remove used variables, I updated
SILGlobalOpt
to collect instructions from all functions. Then, it bails out when trying to optimize, if it finds an instruction with a parent function that shouldn't be optimized.Resolves SR-6597.