-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Add CodeGen options to optimize for size. #32386
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @arielb1 (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. Please see the contribution instructions for more information. |
Are these orthogonal to the Also, I wonder if this could be a level thing too, e.g. |
I agree with @huonw that we likely want to fold this all into one parameter so we can play around with various values in the future. cc @rust-lang/tools, @rust-lang/compiler |
Shouldn't this also change inline threshold? See #29943. |
So, clang specifies Internally in clang -Os and -Oz I think the opt_size integer is an excellent idea, or we could encode the It's up to you all. How do I gate these commands on nightly, there was discussion on irc about Thanks!
|
It looks like clang's other special treatment of these function attributes is to optimize any |
Ah yes and I agree with @sanxiyn that we should mirror the inline threshold decisions in clang for now, and if the optimize-for-size flag factors into that we should likely do so too. |
b6d13ad
to
16dcdc6
Compare
I also noticed in clang lib/Frontend/CompilerInvocation.cpp +442 Looks like it also might be appropraite to disable unrolling loops when opt_size > 0. Still poking around in the Rust internals to see how that would be appropriate to do. |
Looking at |
This is looking great to me, thanks @brandonedens! We talked in person about perhaps adding a disabling of loop unrolling as well, but otherwise could you just add a test or two to the PR? This could both test acceptance of the option and also perhaps add a |
Ok, we discussed this at the tools triage meeting yesterday and here were some of our thoughts:
|
Excellent to hear of the conversation and it was a pleasure to chat the Super excited about this infrastructure work but bear with me; just As I mentioned before it might be prudent to follow a strategy similar to So via GCC you can execute So we have the following meta flags. Clang: I'd probably switch opt-level to mirror the naming in GCC / Clang, a Lately I've been integrating GCC output with other compilers and its really Can we gate this stuff on nightly only? I know you said it'd immediately be Regardless, I'll try to build up something that looks more akin to Clang on Brandon
|
Yeah we certainly have the ability to make things nightly only if necessary. For example we can have nightly-only compiler flags (which require an unstable opt-in) or we could gate specific values of specific flags (if we really want). It's basically up to do whatever. We do have some |
☔ The latest upstream changes (presumably #32432) made this pull request unmergeable. Please resolve the merge conflicts. |
Can you provide some size comparisons compiling (Servo for example) ? |
219410b
to
8230673
Compare
I've yet to be able to compile servo with my latest update to the branch. I'll see if I can figure out how to get servo mach to use my local rust build. I did however compile the time example from Iron under optimizations -opt-level=2,3,s,z on x86_64 as --release after I saw your request.
I started looking at the symbol size differences between O3 and Oz which is kinda interesting. Can produce diffs and/or the ELF files if you'd like to look at them yourself. |
Interesting! Looks like it's a <1% difference between O3 and Oz? |
Here are the differences between O3 to Oz sorted by size and alphabetic. Note the elements that came from the Rust compile. |
llvm::LLVMPassManagerBuilderUseInlinerWithThreshold(builder, t as u32); | ||
} | ||
(llvm::CodeGenLevelNone, _) => { | ||
(_, llvm::CodeGenOptSizeDefault, _) => { | ||
llvm::LLVMPassManagerBuilderUseInlinerWithThreshold(builder, 75); |
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.
It looks like this doesn't quite mirror the standard LLVM logic?
If O3 is enabled, the threshold seems to be 275, then there's size/min (75/25), and finally there's the default 225.
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.
There are 225 and 275 below. Yes, the test is in different order, but opt_level and opt_size are not independent. opt_level is O2 if opt_size is Os or Oz.
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 I'm pointing out is that this is different from clang, which we are intentionally mirroring for now.
@brson how do you feel about |
8230673
to
558eeb9
Compare
558eeb9
to
673147d
Compare
…attempts to mimic the behavior of clang's options Os and Oz.
…irm proper compiler of other opt-levels.
… nightly compiler check to prevent their inclusion in the stable / beta compilers.
673147d
to
49d2825
Compare
Bump Alex. |
Here's a comparison between two release builds of the iron library with and without optimize for minsize. Might be interesting to see what final binaries look like after building rust proper minsize optimized. Still haven't succeeded in getting rust built from a local rust; advice appreciated. ▶ size target/release-minsize/libiron.rlib ▶ size target/release/libiron.rlib |
(Some("2"), _) => OptLevel::Default, | ||
(Some("3"), _) => OptLevel::Aggressive, | ||
(Some("s"), true) => OptLevel::Size, | ||
(Some("z"), true) => OptLevel::SizeMin, |
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.
Could you tweak the error message here to indicate that nightly is required if s
or z
is passed on the stable/beta builds?
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.
Added below and tested locally.
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. I added a early exit for attempts to optimize for size on non-nightly.
Add CodeGen options to optimize for size. Add CodeGen options to annotate functions with the attributes OptimizeSize and/or MinSize used by LLVM to reduce .text size. Closes #32296
💔 Test failed - auto-win-gnu-64-nopt-t |
@bors: retry On Mon, May 2, 2016 at 1:13 PM, bors [email protected] wrote:
|
I'm sorry to maybe be asking a stupid question here but how would I go about using |
@spacekookie it's all good! However, we try to keep the issue tracker soley for bugs; getting help on a PR isn't the best way to go about it. Could you post your question to https://users.rust-lang.org/ instead? It's the best place to ask for questions and get help. Thanks! |
Add CodeGen options to annotate functions with the attributes OptimizeSize and/or MinSize used by LLVM to reduce .text size.
Closes #32296