6
6
# Summary
7
7
[ summary ] : #summary
8
8
9
- This RFC introduces the ` #[optimize] ` attribute for controlling optimisation level on a per-item
9
+ This RFC introduces the ` #[optimize] ` attribute for controlling optimization level on a per-item
10
10
basis.
11
11
12
12
# Motivation
13
13
[ motivation ] : #motivation
14
14
15
- Currently, rustc has only a small number of optimisation options that apply globally to the
15
+ Currently, rustc has only a small number of optimization options that apply globally to the
16
16
crate. With LTO and RLIB-only crates these options become applicable to a whole-program, which
17
- reduces the ability to control optimisation even further.
17
+ reduces the ability to control optimization even further.
18
18
19
19
For applications such as embedded, it is critical, that they satisfy the size constraints. This
20
- means, that code must consciously pick one or the other optimisation level. Absence of a method to
21
- selectively optimise different parts of a program in different ways precludes users from utilising
20
+ means, that code must consciously pick one or the other optimization level. Absence of a method to
21
+ selectively optimize different parts of a program in different ways precludes users from utilising
22
22
the hardware they have to the greatest degree.
23
23
24
- With a C toolchain selective optimisation is fairly easy to achieve by compiling the relevant
24
+ With a C toolchain selective optimization is fairly easy to achieve by compiling the relevant
25
25
codegen units (objects) with different options. In Rust ecosystem, where the concept of such units
26
26
does not exist, an alternate solution is necessary.
27
27
28
- With the ` #[optimize] ` attribute it is possible to annotate the optimisation level of separate
29
- items, so that they are optimized differently from the global optimisation option.
28
+ With the ` #[optimize] ` attribute it is possible to annotate the optimization level of separate
29
+ items, so that they are optimized differently from the global optimization option.
30
30
31
31
# Guide-level explanation
32
32
[ guide-level-explanation ] : #guide-level-explanation
33
33
34
34
## ` #[optimize(size)] `
35
35
36
- Sometimes, optimisations are a trade-off between execution time and the code size. Some
37
- optimisations , such as loop unrolling increase code size many times on average (compared to
38
- original function size) for marginal performance benefits. In case such optimisation is not
36
+ Sometimes, optimizations are a trade-off between execution time and the code size. Some
37
+ optimizations , such as loop unrolling increase code size many times on average (compared to
38
+ original function size) for marginal performance benefits. In case such optimization is not
39
39
desirable…
40
40
41
41
``` rust
@@ -59,7 +59,7 @@ opt-level=z`.
59
59
60
60
## ` #[optimize(speed)] `
61
61
62
- Conversely, when one of the global optimisation options for code size is used (` -Copt-level=s ` or
62
+ Conversely, when one of the global optimization options for code size is used (` -Copt-level=s ` or
63
63
` -Copt-level=z ` ), profiling might reveal some functions that are unnecessarily “hot”. In that case,
64
64
those functions may be annotated with the ` #[optimize(speed)] ` to make the compiler make its best
65
65
effort to produce faster code.
@@ -72,17 +72,17 @@ fn banana() {
72
72
```
73
73
74
74
Much like with ` #[optimize(size)] ` , the ` speed ` counterpart is also a hint and will likely not
75
- yield the same results as using the global optimisation option for speed.
75
+ yield the same results as using the global optimization option for speed.
76
76
77
77
# Reference-level explanation
78
78
[ reference-level-explanation ] : #reference-level-explanation
79
79
80
- The ` #[optimize(size)] ` attribute applied to an item or expression will instruct the optimisation
81
- pipeline to avoid applying optimisations that could result in a size increase and machine code
80
+ The ` #[optimize(size)] ` attribute applied to an item or expression will instruct the optimization
81
+ pipeline to avoid applying optimizations that could result in a size increase and machine code
82
82
generator to generate code that’s smaller rather than faster.
83
83
84
- The ` #[optimize(speed)] ` attribute applied to an item or expression will instruct the optimisation
85
- pipeline to apply optimisations that are likely to yield performance wins and machine code
84
+ The ` #[optimize(speed)] ` attribute applied to an item or expression will instruct the optimization
85
+ pipeline to apply optimizations that are likely to yield performance wins and machine code
86
86
generator to generate code that’s faster rather than smaller.
87
87
88
88
The ` #[optimize] ` attributes are just a hint to the compiler and are not guaranteed to result in
@@ -99,8 +99,8 @@ function will propagate to other functions or closures defined within the body o
99
99
It is an error to specify multiple incompatible ` #[optimize] ` options to a single item or
100
100
expression at once. A more explicit ` #[optimize] ` attribute overrides a propagated attribute.
101
101
102
- ` #[optimize(speed)] ` is a no-op when a global optimisation for speed option is set (i.e. `-C
103
- opt-level=1-3` ). Similarly ` #[ optimize(size)] ` is a no-op when a global optimisation for size
102
+ ` #[optimize(speed)] ` is a no-op when a global optimization for speed option is set (i.e. `-C
103
+ opt-level=1-3` ). Similarly ` #[ optimize(size)] ` is a no-op when a global optimization for size
104
104
option is set (i.e. ` -C opt-level=s/z ` ). ` #[optimize] ` attributes are no-op when no optimizations
105
105
are done globally (i.e. ` -C opt-level=0 ` ). In all other cases the * exact* interaction of the
106
106
` #[optimize] ` attribute with the global optimization level is not specified and is left up to
@@ -119,7 +119,7 @@ For the LLVM backend, these attributes may be implemented in a following manner:
119
119
` #[optimize(size)] ` – explicit function attributes exist at LLVM level. Items with
120
120
` optimize(size) ` would simply apply the LLVM attributes to the functions.
121
121
122
- ` #[optimize(speed)] ` in conjunction with ` -C opt-level=s/z ` – use a global optimisation level of
122
+ ` #[optimize(speed)] ` in conjunction with ` -C opt-level=s/z ` – use a global optimization level of
123
123
` -C opt-level=2/3 ` and apply the equivalent LLVM function attribute (` optsize ` , ` minsize ` ) to all
124
124
items which do not have an ` #[optimize(speed)] ` attribute.
125
125
@@ -128,16 +128,16 @@ items which do not have an `#[optimize(speed)]` attribute.
128
128
129
129
* Not all of the alternative codegen backends may be able to express such a request, hence the
130
130
“this is a hint” note on the ` #[optimize] ` attribute.
131
- * As a fallback, this attribute may be implemented in terms of more specific optimisation hints
131
+ * As a fallback, this attribute may be implemented in terms of more specific optimization hints
132
132
(such as ` inline(never) ` , the future ` unroll(never) ` etc).
133
133
134
134
# Rationale and alternatives
135
135
[ alternatives ] : #alternatives
136
136
137
137
Proposed is a very semantic solution (describes the desired result, instead of behaviour) to the
138
- problem of needing to sometimes inhibit some of the trade-off optimisations such as loop unrolling.
138
+ problem of needing to sometimes inhibit some of the trade-off optimizations such as loop unrolling.
139
139
140
- Alternative, of course, would be to add attributes controlling such optimisations , such as
140
+ Alternative, of course, would be to add attributes controlling such optimizations , such as
141
141
` #[unroll(no)] ` on top of a loop statement. There’s already precedent for this in the ` #[inline] `
142
142
annotations.
143
143
@@ -148,15 +148,15 @@ attributes for people who know *why* the code is not satisfactory.
148
148
Furthermore, currently ` optimize ` is able to do more than any possible combination of targeted
149
149
attributes would be able to such as influencing the instruction selection or switch codegen
150
150
strategy (jump table, if chain, etc.) This makes the attribute useful even in presence of all the
151
- targeted optimisation knobs we might have in the future.
151
+ targeted optimization knobs we might have in the future.
152
152
153
153
# Prior art
154
154
[ prior-art ] : #prior-art
155
155
156
156
* LLVM: ` optsize ` , ` optnone ` , ` minsize ` function attributes (exposed in Clang in some way);
157
- * GCC: ` __attribute__((optimize)) ` function attribute which allows setting the optimisation level
157
+ * GCC: ` __attribute__((optimize)) ` function attribute which allows setting the optimization level
158
158
and using certain(?) ` -f ` flags for each function;
159
- * IAR: Optimisations have a check box for “No size constraints”, which allows compiler to go out of
159
+ * IAR: Optimizations have a check box for “No size constraints”, which allows compiler to go out of
160
160
its way to optimize without considering the size trade-off. Can only be applied on a
161
161
per-compilation-unit basis. Enabled by default, as is appropriate for a compiler targeting
162
162
embedded use-cases.
@@ -166,8 +166,8 @@ embedded use-cases.
166
166
167
167
* Should we also implement ` optimize(always) ` ? ` optimize(level=x) ` ?
168
168
* Left for future discussion, but should make sure such extension is possible.
169
- * Should there be any way to specify what global optimisation for speed level is used in
170
- conjunction with the optimisation for speed option (e.g. ` -Copt-level=s3 ` could be equivalent to
169
+ * Should there be any way to specify what global optimization for speed level is used in
170
+ conjunction with the optimization for speed option (e.g. ` -Copt-level=s3 ` could be equivalent to
171
171
` -Copt-level=3 ` and ` #[optimize(size)] ` on the crate item);
172
172
* This may matter for users of ` #[optimize(speed)] ` .
173
173
* Are the propagation and ` unused_attr ` approaches right?
0 commit comments