@@ -42,7 +42,112 @@ The *`no_builtins` [attribute]* may be applied at the crate level to disable
4242optimizing certain code patterns to invocations of library functions that are
4343assumed to exist.
4444
45+ ## The ` target_feature ` attribute
46+
47+ The * ` target_feature ` [ attribute] * may be applied to an [ unsafe function] to
48+ enable code generation of that function for specific platform architecture
49+ features. It uses the [ _ MetaListNameValueStr_ ] syntax with a single key of
50+ ` enable ` whose value is a string of comma-separated feature names to enable.
51+
52+ ``` rust,ignore
53+ #[target_feature(enable = "avx2")]
54+ unsafe fn foo_avx2() {}
55+ ```
56+
57+ Each [ target architecture] has a set of features that may be enabled. It is an
58+ error to specify a feature for a target architecture that the crate is not
59+ being compiled for.
60+
61+ It is [ undefined behavior] to call a function that is compiled with a feature
62+ that is not supported on the current platform the code is running on.
63+
64+ Functions marked with ` target_feature ` are not inlined into a context that
65+ does not support the given features. The ` #[inline(always)] ` attribute may not
66+ be used with a ` target_feature ` attribute.
67+
68+ ### Available features
69+
70+ The following is a list of the available feature names.
71+
72+ #### ` x86 ` or ` x86_64 `
73+
74+ Feature | Implicitly Enables | Description
75+ ------------|--------------------|-------------------
76+ ` aes ` | ` sse2 ` | [ AES] — Advanced Encryption Standard
77+ ` avx ` | ` sse4.2 ` | [ AVX] — Advanced Vector Extensions
78+ ` avx2 ` | ` avx ` | [ AVX2] — Advanced Vector Extensions 2
79+ ` bmi1 ` | | [ BMI1] — Bit Manipulation Instruction Sets
80+ ` bmi2 ` | | [ BMI2] — Bit Manipulation Instruction Sets 2
81+ ` fma ` | ` avx ` | [ FMA3] — Three-operand fused multiply-add
82+ ` fxsr ` | | [ ` fxsave ` ] and [ ` fxrstor ` ] — Save and restore x87 FPU, MMX Technology, and SSE State
83+ ` lzcnt ` | | [ ` lzcnt ` ] — Leading zeros count
84+ ` pclmulqdq ` | ` sse2 ` | [ ` pclmulqdq ` ] — Packed carry-less multiplication quadword
85+ ` popcnt ` | | [ ` popcnt ` ] — Count of bits set to 1
86+ ` rdrand ` | | [ ` rdrand ` ] — Read random number
87+ ` rdseed ` | | [ ` rdseed ` ] — Read random seed
88+ ` sha ` | ` sse2 ` | [ SHA] — Secure Hash Algorithm
89+ ` sse ` | | [ SSE] — Streaming <abbr title =" Single Instruction Multiple Data " >SIMD</abbr > Extensions
90+ ` sse2 ` | ` sse ` | [ SSE2] — Streaming SIMD Extensions 2
91+ ` sse3 ` | ` sse2 ` | [ SSE3] — Streaming SIMD Extensions 3
92+ ` sse4.1 ` | ` sse3 ` | [ SSE4.1] — Streaming SIMD Extensions 4.1
93+ ` sse4.2 ` | ` sse4.1 ` | [ SSE4.2] — Streaming SIMD Extensions 4.2
94+ ` ssse3 ` | ` sse3 ` | [ SSSE3] — Supplemental Streaming SIMD Extensions 3
95+ ` xsave ` | | [ ` xsave ` ] — Save processor extended states
96+ ` xsavec ` | | [ ` xsavec ` ] — Save processor extended states with compaction
97+ ` xsaveopt ` | | [ ` xsaveopt ` ] — Save processor extended states optimized
98+ ` xsaves ` | | [ ` xsaves ` ] — Save processor extended states supervisor
99+
100+ <!-- Keep links near each table to make it easier to move and update. -->
101+
102+ [ AES ] : https://en.wikipedia.org/wiki/AES_instruction_set
103+ [ AVX ] : https://en.wikipedia.org/wiki/Advanced_Vector_Extensions
104+ [ AVX2 ] : https://en.wikipedia.org/wiki/Advanced_Vector_Extensions#AVX2
105+ [ BMI1 ] : https://en.wikipedia.org/wiki/Bit_Manipulation_Instruction_Sets
106+ [ BMI2 ] : https://en.wikipedia.org/wiki/Bit_Manipulation_Instruction_Sets#BMI2
107+ [ FMA3 ] : https://en.wikipedia.org/wiki/FMA_instruction_set
108+ [ `fxsave` ] : https://www.felixcloutier.com/x86/fxsave
109+ [ `fxrstor` ] : https://www.felixcloutier.com/x86/fxrstor
110+ [ `lzcnt` ] : https://www.felixcloutier.com/x86/lzcnt
111+ [ `pclmulqdq` ] : https://www.felixcloutier.com/x86/pclmulqdq
112+ [ `popcnt` ] : https://www.felixcloutier.com/x86/popcnt
113+ [ `rdrand` ] : https://en.wikipedia.org/wiki/RdRand
114+ [ `rdseed` ] : https://en.wikipedia.org/wiki/RdRand
115+ [ SHA ] : https://en.wikipedia.org/wiki/Intel_SHA_extensions
116+ [ SSE ] : https://en.wikipedia.org/wiki/Streaming_SIMD_Extensions
117+ [ SSE2 ] : https://en.wikipedia.org/wiki/SSE2
118+ [ SSE3 ] : https://en.wikipedia.org/wiki/SSE3
119+ [ SSE4.1 ] : https://en.wikipedia.org/wiki/SSE4#SSE4.1
120+ [ SSE4.2 ] : https://en.wikipedia.org/wiki/SSE4#SSE4.2
121+ [ SSSE3 ] : https://en.wikipedia.org/wiki/SSSE3
122+ [ `xsave` ] : https://www.felixcloutier.com/x86/xsave
123+ [ `xsavec` ] : https://www.felixcloutier.com/x86/xsavec
124+ [ `xsaveopt` ] : https://www.felixcloutier.com/x86/xsaveopt
125+ [ `xsaves` ] : https://www.felixcloutier.com/x86/xsaves
126+
127+ ### Additional information
128+
129+ See the [ ` target_feature ` conditional compilation option] for selectively
130+ enabling or disabling compilation of code based on compile-time settings. Note
131+ that this option is not affected by the ` target_feature ` attribute, and is
132+ only driven by the features enabled for the entire crate.
133+
134+ See the [ ` is_x86_feature_detected ` ] macro in the standard library for runtime
135+ feature detection on the x86 platforms.
136+
137+ > Note: ` rustc ` has a default set of features enabled for each target and CPU.
138+ > The CPU may be chosen with the [ ` -C target-cpu ` ] flag. Individual features
139+ > may be enabled or disabled for an entire crate with the
140+ > [ ` -C target-feature ` ] flag.
141+
142+ [ _MetaListNameValueStr_ ] : attributes.html#meta-item-attribute-syntax
143+ [ `-C target-cpu` ] : ../rustc/codegen-options/index.html#target-cpu
144+ [ `-C target-feature` ] : ../rustc/codegen-options/index.html#target-feature
145+ [ `is_x86_feature_detected` ] : ../std/macro.is_x86_feature_detected.html
146+ [ `target_feature` conditional compilation option ] : conditional-compilation.html#target_feature
45147[ attribute ] : attributes.html
46148[ attributes ] : attributes.html
47149[ functions ] : items/functions.html
150+ [ target architecture ] : conditional-compilation.html#target_arch
48151[ trait ] : items/traits.html
152+ [ undefined behavior ] : behavior-considered-undefined.html
153+ [ unsafe function ] : unsafe-functions.html
0 commit comments