1
1
# Code generation attributes
2
2
3
+ r[ attributes.codegen]
4
+
3
5
The following [ attributes] are used for controlling code generation.
4
6
5
7
## Optimization hints
6
8
9
+ r[ attributes.codegen.hint]
10
+
11
+ r[ attributes.codegen.hint.cold-inline]
7
12
The ` cold ` and ` inline ` [ attributes] give suggestions to generate code in a
8
13
way that may be faster than what it would do without the hint. The attributes
9
14
are only hints, and may be ignored.
10
15
16
+ r[ attributes.codegen.hint.usage]
11
17
Both attributes can be used on [ functions] . When applied to a function in a
12
18
[ trait] , they apply only to that function when used as a default function for
13
19
a trait implementation and not to all trait implementations. The attributes
14
20
have no effect on a trait function without a body.
15
21
16
22
### The ` inline ` attribute
17
23
24
+ r[ attributes.codgen.inline]
25
+
26
+ r[ attributes.codegen.inline.general]
18
27
The * ` inline ` [ attribute] * suggests that a copy of the attributed function
19
28
should be placed in the caller, rather than generating code to call the
20
29
function where it is defined.
@@ -23,6 +32,7 @@ function where it is defined.
23
32
> internal heuristics. Incorrectly inlining functions can make the program
24
33
> slower, so this attribute should be used with care.
25
34
35
+ r[ attributes.codegen.inline.modes]
26
36
There are three ways to use the inline attribute:
27
37
28
38
* ` #[inline] ` * suggests* performing an inline expansion.
@@ -36,17 +46,25 @@ There are three ways to use the inline attribute:
36
46
37
47
### The ` cold ` attribute
38
48
49
+ r[ attribute.codegen.cold]
50
+
39
51
The * ` cold ` [ attribute] * suggests that the attributed function is unlikely to
40
52
be called.
41
53
42
54
## The ` no_builtins ` attribute
43
55
56
+ r[ attributes.codegen.no_builtins]
57
+
44
58
The * ` no_builtins ` [ attribute] * may be applied at the crate level to disable
45
59
optimizing certain code patterns to invocations of library functions that are
46
60
assumed to exist.
47
61
48
62
## The ` target_feature ` attribute
49
63
64
+ r[ attributes.codegen.target_feature]
65
+
66
+
67
+ r[ attributes.codegen.target_feature.general]
50
68
The * ` target_feature ` [ attribute] * may be applied to a function to
51
69
enable code generation of that function for specific platform architecture
52
70
features. It uses the [ _ MetaListNameValueStr_ ] syntax with a single key of
@@ -58,24 +76,31 @@ features. It uses the [_MetaListNameValueStr_] syntax with a single key of
58
76
unsafe fn foo_avx2 () {}
59
77
```
60
78
79
+ r[ attributes.codegen.target_features.arch]
61
80
Each [ target architecture] has a set of features that may be enabled. It is an
62
81
error to specify a feature for a target architecture that the crate is not
63
82
being compiled for.
64
83
84
+ r[ attributes.codegen.target_features.precondition]
65
85
It is [ undefined behavior] to call a function that is compiled with a feature
66
86
that is not supported on the current platform the code is running on, * except*
67
87
if the platform explicitly documents this to be safe.
68
88
89
+ r[ attributes.codegen.target_features.restriction]
69
90
Functions marked with ` target_feature ` are not inlined into a context that
70
91
does not support the given features. The ` #[inline(always)] ` attribute may not
71
92
be used with a ` target_feature ` attribute.
72
93
73
94
### Available features
74
95
96
+ r[ attributes.codegen.target_features.availability]
97
+
75
98
The following is a list of the available feature names.
76
99
77
100
#### ` x86 ` or ` x86_64 `
78
101
102
+ r[ attributes.codegen.target_features.x86]
103
+
79
104
Executing code with unsupported features is undefined behavior on this platform.
80
105
Hence this platform requires that ` #[target_feature] ` is only applied to [ ` unsafe `
81
106
functions] [ unsafe function ] .
@@ -143,6 +168,8 @@ Feature | Implicitly Enables | Description
143
168
144
169
#### ` aarch64 `
145
170
171
+ r[ attributes.codegen.target_feature.aarch64]
172
+
146
173
This platform requires that ` #[target_feature] ` is only applied to [ ` unsafe `
147
174
functions] [ unsafe function ] .
148
175
@@ -206,6 +233,8 @@ Feature | Implicitly Enables | Feature Name
206
233
207
234
#### ` riscv32 ` or ` riscv64 `
208
235
236
+ r[ attributes.codegen.target_feature.riscv]
237
+
209
238
This platform requires that ` #[target_feature] ` is only applied to [ ` unsafe `
210
239
functions] [ unsafe function ] .
211
240
@@ -266,6 +295,8 @@ Feature | Implicitly Enables | Description
266
295
267
296
#### ` wasm32 ` or ` wasm64 `
268
297
298
+ r[ attributes.codegen.target_feature.wasm]
299
+
269
300
` #[target_feature] ` may be used with both safe and
270
301
[ ` unsafe ` functions] [ unsafe function ] on Wasm platforms. It is impossible to
271
302
cause undefined behavior via the ` #[target_feature] ` attribute because
@@ -293,11 +324,15 @@ Feature | Implicitly Enables | Description
293
324
294
325
### Additional information
295
326
327
+ r[ attributes.codegen.target_feature.info]
328
+
329
+ r[ attributes.codegen.target_feature.remark-cfg]
296
330
See the [ ` target_feature ` conditional compilation option] for selectively
297
331
enabling or disabling compilation of code based on compile-time settings. Note
298
332
that this option is not affected by the ` target_feature ` attribute, and is
299
333
only driven by the features enabled for the entire crate.
300
334
335
+ r[ attributes.codegen.target_feature.remark-rt]
301
336
See the [ ` is_x86_feature_detected ` ] or [ ` is_aarch64_feature_detected ` ] macros
302
337
in the standard library for runtime feature detection on these platforms.
303
338
@@ -308,18 +343,25 @@ in the standard library for runtime feature detection on these platforms.
308
343
309
344
## The ` track_caller ` attribute
310
345
346
+ r[ attributes.codegen.track_caller]
347
+
348
+ r[ attributes.codegen.track_caller.restriction]
311
349
The ` track_caller ` attribute may be applied to any function with [ ` "Rust" ` ABI] [ rust-abi ]
312
- with the exception of the entry point ` fn main ` . When applied to functions and methods in
313
- trait declarations, the attribute applies to all implementations. If the trait provides a
350
+ with the exception of the entry point ` fn main ` .
351
+
352
+ r[ attributes.codegen.track_caller.traits]
353
+ When applied to functions and methods in trait declarations, the attribute applies to all implementations. If the trait provides a
314
354
default implementation with the attribute, then the attribute also applies to override implementations.
315
355
356
+ r[ attributes.codegen.track_caller.extern]
316
357
When applied to a function in an ` extern ` block the attribute must also be applied to any linked
317
358
implementations, otherwise undefined behavior results. When applied to a function which is made
318
359
available to an ` extern ` block, the declaration in the ` extern ` block must also have the attribute,
319
360
otherwise undefined behavior results.
320
361
321
362
### Behavior
322
363
364
+ r[ attributes.codegen.track_caller.behaviour]
323
365
Applying the attribute to a function ` f ` allows code within ` f ` to get a hint of the [ ` Location ` ] of
324
366
the "topmost" tracked call that led to ` f ` 's invocation. At the point of observation, an
325
367
implementation behaves as if it walks up the stack from ` f ` 's frame to find the nearest frame of an
@@ -399,8 +441,12 @@ And so on.
399
441
400
442
### Limitations
401
443
444
+ r[ attributes.codegen.track_caller.limits]
445
+
446
+ r[ attributes.codegent.track_caller.hint]
402
447
This information is a hint and implementations are not required to preserve it.
403
448
449
+ r[ attributes.codegen.track_caller.decay]
404
450
In particular, coercing a function with ` #[track_caller] ` to a function pointer creates a shim which
405
451
appears to observers to have been called at the attributed function's definition site, losing actual
406
452
caller information across virtual calls. A common example of this coercion is the creation of a
@@ -431,18 +477,27 @@ trait object whose methods are attributed.
431
477
432
478
## The ` instruction_set ` attribute
433
479
480
+ r[ attributes.codegen.instruction_set]
481
+
482
+ r[ attributes.codegen.instruction_set.restriction]
434
483
The * ` instruction_set ` [ attribute] * may be applied to a function to control which instruction set the function will be generated for.
484
+
485
+ r[ attributes.codegen.instruction_set.behaviour]
435
486
This allows mixing more than one instruction set in a single program on CPU architectures that support it.
487
+
488
+ r[ attributes.codegen.instruction_set.syntax]
436
489
It uses the [ _ MetaListPath_ ] syntax, and a path comprised of the architecture family name and instruction set name.
437
490
438
491
[ _MetaListPath_ ] : ../attributes.md#meta-item-attribute-syntax
439
492
493
+ r[ attributes.codegen.instruction_set.target-limits]
440
494
It is a compilation error to use the ` instruction_set ` attribute on a target that does not support it.
441
495
442
496
### On ARM
443
497
444
- For the ` ARMv4T ` and ` ARMv5te ` architectures, the following are supported:
498
+ r [ attributes.codegen.instruction_set.arm ]
445
499
500
+ For the ` ARMv4T ` and ` ARMv5te ` architectures, the following are supported:
446
501
* ` arm::a32 ` --- Generate the function as A32 "ARM" code.
447
502
* ` arm::t32 ` --- Generate the function as T32 "Thumb" code.
448
503
0 commit comments