11# Code generation attributes
22
3+ r[ attributes.codegen]
4+
35The following [ attributes] are used for controlling code generation.
46
57## Optimization hints
68
9+ r[ attributes.codegen.hint]
10+
11+ r[ attributes.codegen.hint.cold-inline]
712The ` cold ` and ` inline ` [ attributes] give suggestions to generate code in a
813way that may be faster than what it would do without the hint. The attributes
914are only hints, and may be ignored.
1015
16+ r[ attributes.codegen.hint.usage]
1117Both attributes can be used on [ functions] . When applied to a function in a
1218[ trait] , they apply only to that function when used as a default function for
1319a trait implementation and not to all trait implementations. The attributes
1420have no effect on a trait function without a body.
1521
1622### The ` inline ` attribute
1723
24+ r[ attributes.codgen.inline]
25+
26+ r[ attributes.codegen.inline.general]
1827The * ` inline ` [ attribute] * suggests that a copy of the attributed function
1928should be placed in the caller, rather than generating code to call the
2029function where it is defined.
@@ -23,6 +32,7 @@ function where it is defined.
2332> internal heuristics. Incorrectly inlining functions can make the program
2433> slower, so this attribute should be used with care.
2534
35+ r[ attributes.codegen.inline.modes]
2636There are three ways to use the inline attribute:
2737
2838* ` #[inline] ` * suggests* performing an inline expansion.
@@ -36,17 +46,25 @@ There are three ways to use the inline attribute:
3646
3747### The ` cold ` attribute
3848
49+ r[ attribute.codegen.cold]
50+
3951The * ` cold ` [ attribute] * suggests that the attributed function is unlikely to
4052be called.
4153
4254## The ` no_builtins ` attribute
4355
56+ r[ attributes.codegen.no_builtins]
57+
4458The * ` no_builtins ` [ attribute] * may be applied at the crate level to disable
4559optimizing certain code patterns to invocations of library functions that are
4660assumed to exist.
4761
4862## The ` target_feature ` attribute
4963
64+ r[ attributes.codegen.target_feature]
65+
66+
67+ r[ attributes.codegen.target_feature.general]
5068The * ` target_feature ` [ attribute] * may be applied to a function to
5169enable code generation of that function for specific platform architecture
5270features. 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
5876unsafe fn foo_avx2 () {}
5977```
6078
79+ r[ attributes.codegen.target_features.arch]
6180Each [ target architecture] has a set of features that may be enabled. It is an
6281error to specify a feature for a target architecture that the crate is not
6382being compiled for.
6483
84+ r[ attributes.codegen.target_features.precondition]
6585It is [ undefined behavior] to call a function that is compiled with a feature
6686that is not supported on the current platform the code is running on, * except*
6787if the platform explicitly documents this to be safe.
6888
89+ r[ attributes.codegen.target_features.restriction]
6990Functions marked with ` target_feature ` are not inlined into a context that
7091does not support the given features. The ` #[inline(always)] ` attribute may not
7192be used with a ` target_feature ` attribute.
7293
7394### Available features
7495
96+ r[ attributes.codegen.target_features.availability]
97+
7598The following is a list of the available feature names.
7699
77100#### ` x86 ` or ` x86_64 `
78101
102+ r[ attributes.codegen.target_features.x86]
103+
79104Executing code with unsupported features is undefined behavior on this platform.
80105Hence this platform requires that ` #[target_feature] ` is only applied to [ ` unsafe `
81106functions] [ unsafe function ] .
@@ -143,6 +168,8 @@ Feature | Implicitly Enables | Description
143168
144169#### ` aarch64 `
145170
171+ r[ attributes.codegen.target_feature.aarch64]
172+
146173This platform requires that ` #[target_feature] ` is only applied to [ ` unsafe `
147174functions] [ unsafe function ] .
148175
@@ -206,6 +233,8 @@ Feature | Implicitly Enables | Feature Name
206233
207234#### ` riscv32 ` or ` riscv64 `
208235
236+ r[ attributes.codegen.target_feature.riscv]
237+
209238This platform requires that ` #[target_feature] ` is only applied to [ ` unsafe `
210239functions] [ unsafe function ] .
211240
@@ -266,6 +295,8 @@ Feature | Implicitly Enables | Description
266295
267296#### ` wasm32 ` or ` wasm64 `
268297
298+ r[ attributes.codegen.target_feature.wasm]
299+
269300` #[target_feature] ` may be used with both safe and
270301[ ` unsafe ` functions] [ unsafe function ] on Wasm platforms. It is impossible to
271302cause undefined behavior via the ` #[target_feature] ` attribute because
@@ -293,11 +324,15 @@ Feature | Implicitly Enables | Description
293324
294325### Additional information
295326
327+ r[ attributes.codegen.target_feature.info]
328+
329+ r[ attributes.codegen.target_feature.remark-cfg]
296330See the [ ` target_feature ` conditional compilation option] for selectively
297331enabling or disabling compilation of code based on compile-time settings. Note
298332that this option is not affected by the ` target_feature ` attribute, and is
299333only driven by the features enabled for the entire crate.
300334
335+ r[ attributes.codegen.target_feature.remark-rt]
301336See the [ ` is_x86_feature_detected ` ] or [ ` is_aarch64_feature_detected ` ] macros
302337in the standard library for runtime feature detection on these platforms.
303338
@@ -308,18 +343,25 @@ in the standard library for runtime feature detection on these platforms.
308343
309344## The ` track_caller ` attribute
310345
346+ r[ attributes.codegen.track_caller]
347+
348+ r[ attributes.codegen.track_caller.restriction]
311349The ` 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
314354default implementation with the attribute, then the attribute also applies to override implementations.
315355
356+ r[ attributes.codegen.track_caller.extern]
316357When applied to a function in an ` extern ` block the attribute must also be applied to any linked
317358implementations, otherwise undefined behavior results. When applied to a function which is made
318359available to an ` extern ` block, the declaration in the ` extern ` block must also have the attribute,
319360otherwise undefined behavior results.
320361
321362### Behavior
322363
364+ r[ attributes.codegen.track_caller.behaviour]
323365Applying the attribute to a function ` f ` allows code within ` f ` to get a hint of the [ ` Location ` ] of
324366the "topmost" tracked call that led to ` f ` 's invocation. At the point of observation, an
325367implementation 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.
399441
400442### Limitations
401443
444+ r[ attributes.codegen.track_caller.limits]
445+
446+ r[ attributes.codegent.track_caller.hint]
402447This information is a hint and implementations are not required to preserve it.
403448
449+ r[ attributes.codegen.track_caller.decay]
404450In particular, coercing a function with ` #[track_caller] ` to a function pointer creates a shim which
405451appears to observers to have been called at the attributed function's definition site, losing actual
406452caller 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.
431477
432478## The ` instruction_set ` attribute
433479
480+ r[ attributes.codegen.instruction_set]
481+
482+ r[ attributes.codegen.instruction_set.restriction]
434483The * ` 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]
435486This 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]
436489It uses the [ _ MetaListPath_ ] syntax, and a path comprised of the architecture family name and instruction set name.
437490
438491[ _MetaListPath_ ] : ../attributes.md#meta-item-attribute-syntax
439492
493+ r[ attributes.codegen.instruction_set.target-limits]
440494It is a compilation error to use the ` instruction_set ` attribute on a target that does not support it.
441495
442496### On ARM
443497
444- For the ` ARMv4T ` and ` ARMv5te ` architectures, the following are supported:
498+ r [ attributes.codegen.instruction_set.arm ]
445499
500+ For the ` ARMv4T ` and ` ARMv5te ` architectures, the following are supported:
446501* ` arm::a32 ` --- Generate the function as A32 "ARM" code.
447502* ` arm::t32 ` --- Generate the function as T32 "Thumb" code.
448503
0 commit comments