Skip to content

Commit bc87a02

Browse files
authored
Merge pull request #545 from ehuss/target_feature
Document `target_feature` and `cfg_target_feature`.
2 parents be09d08 + 17c5a67 commit bc87a02

5 files changed

+139
-7
lines changed

src/attributes.md

+2
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ The following is an index of all built-in attributes.
216216
- [`inline`] — Hint to inline code.
217217
- [`cold`] — Hint that a function is unlikely to be called.
218218
- [`no_builtins`] — Disables use of certain built-in functions.
219+
- [`target_feature`] — Configure platform-specific code generation.
219220
- Documentation
220221
- `doc` — Specifies documentation. See [The Rustdoc Book] for more
221222
information. [Doc comments] are transformed into `doc` attributes.
@@ -282,6 +283,7 @@ The following is an index of all built-in attributes.
282283
[`recursion_limit`]: attributes/limits.html#the-recursion_limit-attribute
283284
[`repr`]: type-layout.html#representations
284285
[`should_panic`]: attributes/testing.html#the-should_panic-attribute
286+
[`target_feature`]: attributes/codegen.html#the-target_feature-attribute
285287
[`test`]: attributes/testing.html#the-test-attribute
286288
[`used`]: abi.html#the-used-attribute
287289
[`warn`]: attributes/diagnostics.html#lint-check-attributes

src/attributes/codegen.md

+105
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,112 @@ The *`no_builtins` [attribute]* may be applied at the crate level to disable
4242
optimizing certain code patterns to invocations of library functions that are
4343
assumed 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

src/behavior-considered-undefined.md

+8-5
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ code.
3838
* A discriminant in an `enum` not included in the type definition.
3939
* A value in a `char` which is a surrogate or above `char::MAX`.
4040
* Non-UTF-8 byte sequences in a `str`.
41+
* Executing code compiled with platform features that the current platform
42+
does not support (see [`target_feature`]).
4143

4244
> **Note**: Undefined behavior affects the entire program. For example, calling
4345
> a function in C that exhibits undefined behavior of C means your entire
@@ -48,9 +50,10 @@ code.
4850
[noalias]: http://llvm.org/docs/LangRef.html#noalias
4951
[pointer aliasing rules]: http://llvm.org/docs/LangRef.html#pointer-aliasing-rules
5052
[undef]: http://llvm.org/docs/LangRef.html#undefined-values
51-
[`offset`]: https://doc.rust-lang.org/std/primitive.pointer.html#method.offset
52-
[`std::ptr::copy_nonoverlapping_memory`]: https://doc.rust-lang.org/std/ptr/fn.copy_nonoverlapping.html
53-
[`UnsafeCell<U>`]: https://doc.rust-lang.org/std/cell/struct.UnsafeCell.html
54-
[`read_unaligned`]: https://doc.rust-lang.org/std/ptr/fn.read_unaligned.html
55-
[`write_unaligned`]: https://doc.rust-lang.org/std/ptr/fn.write_unaligned.html
53+
[`offset`]: ../std/primitive.pointer.html#method.offset
54+
[`std::ptr::copy_nonoverlapping_memory`]: ../std/ptr/fn.copy_nonoverlapping.html
55+
[`target_feature`]: attributes/codegen.html#the-target_feature-attribute
56+
[`UnsafeCell<U>`]: ../std/cell/struct.UnsafeCell.html
57+
[`read_unaligned`]: ../std/ptr/fn.read_unaligned.html
58+
[`write_unaligned`]: ../std/ptr/fn.write_unaligned.html
5659
[Rustonomicon]: ../nomicon/index.html

src/conditional-compilation.md

+21
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,25 @@ Example values:
9090
* `"arm"`
9191
* `"aarch64"`
9292

93+
### `target_feature`
94+
95+
Key-value option set for each platform feature available for the current
96+
compilation target.
97+
98+
Example values:
99+
100+
* `"avx"`
101+
* `"avx2"`
102+
* `"crt-static"`
103+
* `"rdrand"`
104+
* `"sse"`
105+
* `"sse2"`
106+
* `"sse4.1"`
107+
108+
See the [`target_feature` attribute] for more details on the available
109+
features. An additional feature of `crt-static` is available to the
110+
`target_feature` option to indicate that a [static C runtime] is available.
111+
93112
### `target_os`
94113

95114
Key-value option set once with the target's operating system. This value is
@@ -305,6 +324,8 @@ println!("I'm running on a {} machine!", machine_kind);
305324
[`cfg` macro]: #the-cfg-macro
306325
[`cfg_attr`]: #the-cfg_attr-attribute
307326
[`debug_assert!`]: ../std/macro.debug_assert.html
327+
[`target_feature` attribute]: attributes/codegen.html#the-target_feature-attribute
308328
[attribute]: attributes.html
309329
[attributes]: attributes.html
310330
[crate type]: linkage.html
331+
[static C runtime]: linkage.html#static-and-dynamic-c-runtimes

src/linkage.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ that it's linked as you would expect after the compiler succeeds.
168168
Crates may also learn about how the C runtime is being linked. Code on MSVC, for
169169
example, needs to be compiled differently (e.g. with `/MT` or `/MD`) depending
170170
on the runtime being linked. This is exported currently through the
171-
`target_feature` attribute (note this is a nightly feature):
171+
[`cfg` attribute `target_feature` option]:
172172

173173
```rust,ignore
174174
#[cfg(target_feature = "crt-static")]
@@ -210,5 +210,6 @@ a statically linked binary on MSVC you would execute:
210210
RUSTFLAGS='-C target-feature=+crt-static' cargo build --target x86_64-pc-windows-msvc
211211
```
212212

213+
[`cfg` attribute `target_feature` option]: conditional-compilation.html#target_feature
213214
[configuration option]: conditional-compilation.html
214-
[procedural macros]: procedural-macros.html
215+
[procedural macros]: procedural-macros.html

0 commit comments

Comments
 (0)