-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
The optimize attribute #2412
The optimize attribute #2412
Conversation
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.
Nice to see progress on this!
text/0000-optimise-attr.md
Outdated
|
||
--- | ||
|
||
Alternative: `optimize` (American English) instead of `optimise`… or both? |
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.
Due to the precedence set by GCC and the fact that the vast majority of conversations I've read use american english spelling ("optimize", "optimizer") I'd prefer that over "optimise".
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.
For sure not both. As sad as this makes me (given that I prefer BrE), use of AmE is standard in the software industry, so we should stick with that.
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.
At the risk of sounding pedantic (or worse, boring), I feel I should point out that the OED considers 'ize' to be perfectly acceptable and indeed preferred BrE spelling.
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.
@jesskfullwood You always learn something useful; and today is such a day :)
text/0000-optimise-attr.md
Outdated
# Prior art | ||
[prior-art]: #prior-art | ||
|
||
* LLVM: `optsize`, `optnone`, `minsize` function attributes (exposed in Clang in some way); |
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.
in some way
I think Clang supports __attribute__((optnone))
syntax for these
text/0000-optimise-attr.md
Outdated
|
||
* LLVM: `optsize`, `optnone`, `minsize` function attributes (exposed in Clang in some way); | ||
* GCC: `__attribute__((optimize))` function attribute which allows setting the optimisation level | ||
and using certain(?) `-f` flags for each function; |
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.
Per-function optimization can also be configured by using #pragma GCC optimize (...)
.
(see https://gcc.gnu.org/onlinedocs/gcc/Function-Specific-Option-Pragmas.html)
text/0000-optimise-attr.md
Outdated
# Unresolved questions | ||
[unresolved]: #unresolved-questions | ||
|
||
* Should we support such an attribute at module-level? Crate-level? |
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 think so yes. At least, you should be able to specify #[optimize(size)]
on mod
and impl
with the semantics that it applies to every fn
inside those transitively.
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.
Counter point: people might assume that optimise(size)
at the crate level also asserts repr(packed)
. We don't do this with inline
so I'd assume not for optimise.
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 think it would be quite tedious to have to do this on every single function if a module if that is what you want. If we want to be more clear that it is about functions, you could write #[optimize(fn_size)] mod foobar { .. }
.
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 do think it makes sense to allow #![optimize(size)]
in a crate to apply to all functions (that don't override it).
I certainly don't think optimize(size)
should ever imply repr(packed)
, because the latter can affect semantics.
Two questions.
|
Interesting question. Precedent from
Should be an error. (With the possible exception of closure expressions.) |
Currently attributes that weren't used are warned by a unused_attribute (or
some such) lint. I don't see that changing or being any different in this
scenario.
…On Wed, Apr 25, 2018, 11:13 Robin Kruppe ***@***.***> wrote:
If I apply this attribute to a function that creates a closure, does that
closure also get optimized for size?
Interesting question. Precedent from inline would be "no" (closures are
always inline and not inline(never) regardless of where they are) with
the possibility of adding the attribute to the closure expression as well
(e.g. #[inline(never)] #[optimize(size)] |x| x + 1). On the other hand,
especially if the attribute can be applied to modules, it is very
reasonable to expect it to be passed down.
What happens when I apply this attribute to a non-function, such as a
struct.
Should be an error. (With the possible exception of closure expressions.)
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#2412 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AApc0hn-DLeqQc09LAJ2mS1yD1-8UqLsks5tsDAygaJpZM4TedOW>
.
|
Some misuses of known attributes are hard errors (example). Others aren't, but AFAIK that's mostly for backwards compatibility because rust 1.0 wasn't very comprehensive at identifying all such attributes. |
I insist on the American |
Most of the functions will be "cold", so I think it would make more sense to use |
Many of the "speed" optimisatios are global (cross-function), so even if
implementing such attribute was feasible, it wouldn't have nearly as much
effect as the usual -O.
Agreed with the sentiment though.
…On Thu, Apr 26, 2018, 04:50 Kornel ***@***.***> wrote:
Most of the code will be "cold", so I think it would make more sense to
use -C opt-level=s to make all of them small, and then add
#[optimize(speed)] on the few hot functions.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#2412 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AApc0vnjL6AE41lBc9Y6CD5B1zDEOCk-ks5tsSfHgaJpZM4TedOW>
.
|
@nagisa some attributes error when used in the wrong position, and I'd say precedent is that this should error when used on a non-function for now. |
@rfcbot fcp merge It seems like conversation here has reached a fixed point. I personally think exposing these sorts of knobs is a good idea — naturally they should come with no firm promises. I'd like to hear from some members of the Embedded Domain WG (cc @japaric) but I'm going to assume they are in favor. =) |
Team member @nikomatsakis has proposed to merge this. The next step is review by the rest of the tagged teams: Concerns:
Once a majority of reviewers approve (and none object), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up! See this document for info about what commands tagged team members can give me. |
It might be that this should be more of a @rust-lang/compiler RFC? In any case, cc compiler folks — take a look. |
Does it really make sense to globally optimize a program and then tag specific functions as size-optimized? I'd think for the embedded use case, you'd want the opposite: optimize the whole program for size and then tag a few hotspots as performance-optimized. I don't have a fundamental objection to this, but I'm curious to what degree it reflects "people actively want to use this right now" versus "people kinda think unspecified other people might want a thing kinda like this". |
@joshtriplett I'd definitely appreciate something like this and would use it if it were available, but i agree with you that I think what I really want is the ability to have opt-level=z by default, and opt into performance improvements in areas that need it. Note that @kornelski and @nagisa discussed this above, and it doesn't sound like it's necessarily feasible. |
@cramertj That was my expectation as well. Even despite the lack of global optimization, the ability to optimize specific functions for speed ought to help address hotspots found via profiling (with subsequent profiling after adding the attributes to see if it worked). |
I've been thinking about this RFC for a while, ever since it was proposed for FCP, and debating whether to check the box for it. After careful discussion: @rfcbot concern optimize-speed Most programs needing functionality like this will want global size optimization and selective I acknowledge the point that Based on that, I'd like to propose revising the RFC to define both |
I thought about The approach would basically involve always compiling with what is now |
If we decide to go towards |
This seems like something that users might want to select (e.g., by specifying |
I like the idea of having At the risk of bikeshedding: rather than |
@pnkfelix @scottmcm @withoutboats It has been 4 months since the pre-FCP has begun, and your checkboxes are still unchecked without any outstanding concerns from either of you. |
🔔 This is now entering its final comment period, as per the review above. 🔔 |
@nagisa I see no mention of the treatment of closures in the RFC but it has been discussed in comments without a resolution. Could you please record an unresolved question, in the text, to consider before stabilizing? EDIT: could you also clarify which unresolved questions are to be resolved during the evaluation/stabilization process and which would need subsequent RFCs? |
Additionally, clarify propagation of the attribute.
86ffe97
to
1de6b6d
Compare
I adjusted wording of the RFC to consider closures. Also clarified wording around propagation somewhat which should answer everything /wrt closures. My feeling is that these modifications are sufficiently simple and an obvious extension of the previous wording, but just to be safe, I also added an item in unresolved questions to make sure it ends up in the tracking issue.
I don’t think any of the unresolved questions will need to be discussed for stabilisation, except for the one I added in the most recent commit. They were intended to be more a question of whether we care about those use cases enough for this RFC, rather than that they are outright unresolved. |
@nagisa That also works for me :) |
PS: If you want to care of the nits in #2412 (comment) I can deal with the merge procedure once the FCP is over. |
"Rendered" link is 404.
Would there be an auto-suggestion if mistyped with |
Fixed. :) |
A better place for this would be some functionality common to attributes in general (similar to how we suggest similar names when variables are misspelled), but that would be another RFC altogether. For now you’d just get an unused attribute warning, I guess. |
@nagisa might just be a pure diagnostics issue (e.g. just apply levenshtein) in which case it doesn't need an RFC and the compiler team can just do it? |
@Centril attributes have more subtlety compared to local variables. This is especially notable when syntax plugins can introduce arbitrary attribute names into a program. |
@nagisa right; but shouldn't the set of attribute names be in scope such that when you try to apply an attribute, if resolution does not find it in the attr-macro sub-namespace then an error is emitted in which the compiler checks which paths look similar (using the familiar similarity algorithms...)? E.g. shouldn't it be similar to |
@Centril There is no scope for attribute names, they are all global AFAIK. Yet, attributes generally may only be used with certain logical constructs (e.g. optimize can only be used with function-like things). You don’t want to levenstein-suggest to change This is probably not the right place to discuss such a feature in depth. As far as the original question is concerned, I feel that the preexisting |
There are some namespaces attributes for tools now, right?
… On Sep 26, 2018, at 12:52 PM, Simonas Kazlauskas ***@***.***> wrote:
@Centril There is no scope for attribute names, they are all global AFAIK. Yet, attributes generally may only be used with certain logical constructs (e.g. optimize can only be used with function-like things). You don’t want to levenstein-suggest to change #[boline] into #[inline] on top of a struct, when there’s also plugin-introduced #[boolinate] which is applicable in that location :)
This is probably not the right place to discuss such a feature in depth. As far as the original question is concerned, I feel that the preexisting unused_attribute lint is an appropriate solution for the scope of this RFC. I’d love something more directed, but I’m of an opinion that such solution would be better off developed separately and in the context of attributes in general, not this specific attribute.
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub, or mute the thread.
|
@nagisa This may be too late in the process to bring it up, and maybe there's a subtlety I've missed, but in the pre-RFC thread you said "...I don’t think optimise(none) is applicable for the crypto use-case." The original questioner is correct about side-channel attacks (last rfc I could find about this kind of thing was this one. A particular case would be in implementing countermeasures against timing attacks) Many of these involve doing a bunch of things that a medium smart compiler of any language could trivially detect are not useful to the result and could be thrown away. I've come across one or two crates that use different contortions to confuse the compiler into avoiding some optimization, and one could recourse to something unsafe, but even sneaky ways may be amenable to optimization and thereafter attack. As a practical instance, it's not clear to me that this crate guaranteeably works now or will in the future. I, for one, would be more confident in it if it had an optimize(none). Being able to disable optimization of a particular routine as far as one can (which should get one to the same level as debug execution, I would think) should help. |
The final comment period, with a disposition to merge, as per the review above, is now complete. |
@jrpascucci you want something that would guarantee the properties you seek of your code and as far as I know the only way to get them is to write assembly, |
Huzzah! This RFC has been merged! Tracking issue: rust-lang/rust#54882 |
This is an RFC that has baked out after receiving feedback on the pre-RFC. Notably
optimise(no)
has been removed as it is not as important and seemed to have way more contention thanoptimise(size)
, while not being as useful or necessary asoptimise(size)
.Rendered
Tracking issue