Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upThe optimize attribute #2412
Conversation
jonas-schievink
reviewed
Apr 21, 2018
|
Nice to see progress on this! |
|
|
||
| --- | ||
|
|
||
| Alternative: `optimize` (American English) instead of `optimise`… or both? |
This comment has been minimized.
This comment has been minimized.
jonas-schievink
Apr 21, 2018
Member
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".
This comment has been minimized.
This comment has been minimized.
Centril
Apr 22, 2018
Contributor
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.
This comment has been minimized.
This comment has been minimized.
jesskfullwood
May 1, 2018
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.
This comment has been minimized.
This comment has been minimized.
Centril
May 2, 2018
Contributor
@jesskfullwood You always learn something useful; and today is such a day :)
| # Prior art | ||
| [prior-art]: #prior-art | ||
|
|
||
| * LLVM: `optsize`, `optnone`, `minsize` function attributes (exposed in Clang in some way); |
This comment has been minimized.
This comment has been minimized.
jonas-schievink
Apr 21, 2018
Member
in some way
I think Clang supports __attribute__((optnone)) syntax for these
|
|
||
| * 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; |
This comment has been minimized.
This comment has been minimized.
jonas-schievink
Apr 21, 2018
Member
Per-function optimization can also be configured by using #pragma GCC optimize (...).
(see https://gcc.gnu.org/onlinedocs/gcc/Function-Specific-Option-Pragmas.html)
Centril
added
the
T-lang
label
Apr 22, 2018
Centril
reviewed
Apr 22, 2018
| # Unresolved questions | ||
| [unresolved]: #unresolved-questions | ||
|
|
||
| * Should we support such an attribute at module-level? Crate-level? |
This comment has been minimized.
This comment has been minimized.
Centril
Apr 22, 2018
Contributor
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.
This comment has been minimized.
This comment has been minimized.
clarfon
Apr 26, 2018
Contributor
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.
This comment has been minimized.
This comment has been minimized.
Centril
May 24, 2018
Contributor
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 { .. }.
This comment has been minimized.
This comment has been minimized.
joshtriplett
Jun 25, 2018
Member
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.
Centril
reviewed
Apr 22, 2018
|
|
||
| ```rust | ||
| #[optimise(size)] | ||
| fn banana() { |
This comment was marked as resolved.
This comment was marked as resolved.
Centril
Apr 22, 2018
•
Contributor
This function is monomorphic. How do you deal with polymorphic functions?
This comment was marked as resolved.
This comment was marked as resolved.
nagisa
Apr 22, 2018
•
Author
Contributor
Where attributes are involved, monomorphisations behave as if they were monomorphic functions annotated with the same attributes as the generic function they were monomorphised from.
This is generally true, so I didn’t see too much reason to explicitly note this in RFC.
This comment was marked as resolved.
This comment was marked as resolved.
This comment has been minimized.
This comment has been minimized.
|
Two questions.
|
This comment has been minimized.
This comment has been minimized.
Interesting question. Precedent from
Should be an error. (With the possible exception of closure expressions.) |
This comment has been minimized.
This comment has been minimized.
|
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>
.
|
This comment has been minimized.
This comment has been minimized.
|
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. |
This comment has been minimized.
This comment has been minimized.
|
I insist on the American |
This comment has been minimized.
This comment has been minimized.
|
Most of the functions will be "cold", so I think it would make more sense to use |
This comment has been minimized.
This comment has been minimized.
|
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>
.
|
nikomatsakis
self-assigned this
Apr 26, 2018
This comment has been minimized.
This comment has been minimized.
|
@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. |
This comment has been minimized.
This comment has been minimized.
|
@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. =) |
This comment has been minimized.
This comment has been minimized.
rfcbot
commented
May 24, 2018
•
|
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. |
rfcbot
added
proposed-final-comment-period
disposition-merge
labels
May 24, 2018
This comment has been minimized.
This comment has been minimized.
|
It might be that this should be more of a @rust-lang/compiler RFC? In any case, cc compiler folks — take a look. |
This comment has been minimized.
This comment has been minimized.
|
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". |
This comment has been minimized.
This comment has been minimized.
|
@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. |
This comment has been minimized.
This comment has been minimized.
|
@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). |
This comment has been minimized.
This comment has been minimized.
|
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 |
rfcbot
added
proposed-final-comment-period
disposition-merge
labels
Jun 14, 2018
This comment has been minimized.
This comment has been minimized.
|
I thought about The approach would basically involve always compiling with what is now |
This comment has been minimized.
This comment has been minimized.
|
If we decide to go towards |
This comment has been minimized.
This comment has been minimized.
This seems like something that users might want to select (e.g., by specifying |
This comment has been minimized.
This comment has been minimized.
|
I like the idea of having At the risk of bikeshedding: rather than |
This comment has been minimized.
This comment has been minimized.
rfcbot
commented
Sep 24, 2018
|
|
rfcbot
added
final-comment-period
and removed
proposed-final-comment-period
labels
Sep 24, 2018
This comment has been minimized.
This comment has been minimized.
|
@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? |
nagisa
force-pushed the
nagisa:optimise-size
branch
from
86ffe97
to
1de6b6d
Sep 25, 2018
This comment has been minimized.
This comment has been minimized.
|
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. |
This comment has been minimized.
This comment has been minimized.
|
@nagisa That also works for me :) |
This comment has been minimized.
This comment has been minimized.
|
PS: If you want to care of the nits in #2412 (comment) I can deal with the merge procedure once the FCP is over. |
This comment has been minimized.
This comment has been minimized.
vi
commented
Sep 26, 2018
|
"Rendered" link is 404.
Would there be an auto-suggestion if mistyped with |
This comment has been minimized.
This comment has been minimized.
Fixed. :) |
This comment has been minimized.
This comment has been minimized.
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. |
This comment has been minimized.
This comment has been minimized.
|
@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? |
This comment has been minimized.
This comment has been minimized.
|
@Centril attributes have more subtlety compared to local variables. This is especially notable when syntax plugins can introduce arbitrary attribute names into a program. |
This comment has been minimized.
This comment has been minimized.
|
@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 |
This comment has been minimized.
This comment has been minimized.
|
@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 |
This comment has been minimized.
This comment has been minimized.
|
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.
|
This comment has been minimized.
This comment has been minimized.
jrpascucci
commented
Oct 4, 2018
|
@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. |
This comment has been minimized.
This comment has been minimized.
rfcbot
commented
Oct 4, 2018
|
The final comment period, with a disposition to merge, as per the review above, is now complete. |
rfcbot
added
finished-final-comment-period
and removed
final-comment-period
labels
Oct 4, 2018
This comment has been minimized.
This comment has been minimized.
|
@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, |
Centril
referenced this pull request
Oct 7, 2018
Open
Tracking issue for RFC 2412, "The optimize attribute" #54882
Centril
merged commit 4baa3fc
into
rust-lang:master
Oct 7, 2018
This comment has been minimized.
This comment has been minimized.
|
Huzzah! This RFC has been merged! Tracking issue: rust-lang/rust#54882 |
nagisa commentedApr 21, 2018
•
edited by Centril
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