-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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 -C hint-mostly-unused
to tell rustc that most of a crate will go unused
#135656
base: master
Are you sure you want to change the base?
Conversation
rustbot has assigned @GuillaumeGomez. Use |
Some changes occurred to MIR optimizations cc @rust-lang/wg-mir-opt |
r? compiler |
This should be added as an unstable option first? |
@workingjubilee This option has already existed in nightly as The only difference being made between that option and this one is that this one respects |
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
I see. I suppose that makes sense, though it brings these thoughts to mind:
As a result, I am curious if the rest of T-compiler believes changing the behavior with respect to Perhaps this should land with a test that demonstrates that it respects |
( I'm also kinda curious if we should adjust how we handle cross-crate inlining by default, tbh. ) |
You may have missed it Jubilee, so hop on to https://rust-lang.zulipchat.com/#narrow/stream/233931-xxx/topic/Add.20.60-C.20hint-mostly-unused.60.20option.20compiler-team.23829 to ask your cool questions and comments :) |
…o unused This hint allows the compiler to optimize its operation based on this assumption, in order to compile faster. This is a hint, and does not guarantee any particular behavior. This option can substantially speed up compilation if applied to a large dependency where the majority of the dependency does not get used. This flag may slow down compilation in other cases. Currently, this option makes the compiler defer as much code generation as possible from functions in the crate, until later crates invoke those functions. Functions that never get invoked will never have code generated for them. For instance, if a crate provides thousands of functions, but only a few of them will get called, this flag will result in the compiler only doing code generation for the called functions. (This uses the same mechanisms as cross-crate inlining of functions.) This does not affect `extern` functions, or functions marked as `#[inline(never)]`. Some performance numbers, based on a crate with many dependencies having just *one* large dependency set to `-C hint-mostly-unused` (using Cargo's `profile-rustflags` option): A release build went from 4m32s to 2m06s. A non-release build went from 2m13s to 1m24s.
4d1b2cb
to
e96e15f
Compare
@@ -172,6 +172,25 @@ values: | |||
|
|||
The default if not specified depends on the target. | |||
|
|||
## hint-mostly-unused |
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.
Given that the flag is now unstable/nightly-gated, this documentation should be moved to the unstable-book (under src/doc/unstable-book/src/compiler-flags
) and a tracking issue should be created.
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.
No need for a tracking issue before consensus tho right ? This still needs fcp anyways. (And given the reservations in the thread some more tests and all will be needed before stabilization.)
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.
Sure, the creation of the tracking issue can wait until the MCP is accepted and the PR nearing approval.
r? compiler |
Discussed in T-compiler triage on Zulip. MCP#829 still needs to be seconded and there seems to be still a few open design questions. Also, do we have some numbers about the real world impact (both positive and negative)?. Maybe it makes sense to nominate again once design and scope are clearer. By the way @saethlin do you think you have some capacity to dedicate here? @rustbot label -I-compiler-nominated |
// If the crate is likely to be mostly unused, use cross-crate inlining to defer codegen until | ||
// the function is referenced, in order to skip codegen for unused functions. | ||
if tcx.sess.opts.cg.hint_mostly_unused { | ||
return true; | ||
} | ||
|
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.
Placing this logic below the check for InlineAttr::Never
does not change whether a call to a #[inline(never)]
function can be inlined, it changes when and where #[inline(never)]
functions get compiled. With the logic down here, #[inline(never)]
functions and their callees (recursively) are not delayed.
The exact motivation for putting this logic here instead of above the check for codegen_fn_attrs.inline
should be clearly recorded. I don't rightly remember what it was, but for example panic backtrace trimming still works even if it is moved.
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.
Making sure inline-never functions don't get affected was an intentional goal here. I'll add a comment.
This option can substantially speed up compilation if applied to a large | ||
dependency where the majority of the dependency does not get used. This flag |
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 provide any indication of how to do this? I've suggested some people try adding -Zcross-crate-inline-threshold=always
to just one dependency and nobody seems to know how to do this offhand. If we're going to suggest people do a thing that's poorly understood I'd like to at least have a link to the Cargo docs about profile overrides.
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.
Sure, will do.
This hint allows the compiler to optimize its operation based on this assumption, in order to compile faster. This is a hint, and does not guarantee any particular behavior.
This option can substantially speed up compilation if applied to a large dependency where the majority of the dependency does not get used. This flag may slow down compilation in other cases.
Currently, this option makes the compiler defer as much code generation as possible from functions in the crate, until later crates invoke those functions. Functions that never get invoked will never have code generated for them. For instance, if a crate provides thousands of functions, but only a few of them will get called, this flag will result in the compiler only doing code generation for the called functions. (This uses the same mechanisms as cross-crate inlining of functions.) This does not affect
extern
functions, or functions marked as#[inline(never)]
.This option has already existed in nightly as
-Zcross-crate-inline-threshold=always
for some time, and has gotten testing in that form. However, this option is still unstable, to give an opportunity for wider testing in this form.Some performance numbers, based on a crate with many dependencies having just one large dependency set to
-C hint-mostly-unused
(using Cargo'sprofile-rustflags
option):A release build went from 4m32s to 2m06s.
A non-release build went from 2m13s to 1m24s.