interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / vendor / curve25519-dalek-derive / README.md

198 lines · 6847 bytes Raw Blame History Rendered
1# A more convenient `#[target_feature]` replacement
2
3To get good performance out of SIMD everything on the SIMD codepath must be inlined.
4With how SIMD is currently implemented in Rust one of two things have to be true for
5a function using SIMD to be inlinable: (and this includes the SIMD intrinsics themselves)
6
7 a) The whole program has to be compiled with the relevant `-C target-cpu` or `-C target-feature` flags.
8
9 b) SIMD support must be automatically detected at runtime, and every function on the SIMD codepath must be marked with `#[target_feature]`.
10
11Both have their downsides. Setting the `target-cpu` or `target-features` makes the resulting binary
12incompatible with older CPUs, while using `#[target_feature]` is incredibly inconvenient.
13
14This crate is meant to make `#[target_feature]` less painful to use.
15
16## Problems with `#[target_feature]`
17
18When we're not compiling with the relevant `target-cpu`/`target-feature` flags everything on
19the SIMD codepath must be marked with the `#[target_feature]` attribute. This is not a problem
20when all of your SIMD code is neatly encapsulated inside of a single function, but once you start
21to build out more elaborate abstractions it starts to become painful to use.
22
23 * It can only be used on `unsafe` functions, so everything on your SIMD codepath now has to be `unsafe`.
24
25 In theory this is nice - these functions require the relevant SIMD instructions to be present at runtime,
26 so calling them without checking is obviously unsafe! But in practice this is rarely what you want. When
27 you build an abstraction over SIMD code you usually want to assume that *internally* within your module
28 all of the necessary SIMD instructions are available, and you only want to check this at the boundaries
29 when you're first entering your module. You do *not* want to infect everything *inside* of the module with
30 `unsafe` since you've already checked this invariant at the module's API boundary.
31
32 * It cannot be used on non-`unsafe` trait methods.
33
34 If you're implementing a trait, say for example `std::ops::Add`, then you cannot mark the method `unsafe`
35 unless the original trait also has it marked as `unsafe`, and usually it doesn't.
36
37 * It makes it impossible to abstract over a given SIMD instruction set using a trait.
38
39 For example, let's assume you want to abstract over which SIMD instructions you use using a trait in the following way:
40
41 ```rust
42 trait Backend {
43 unsafe fn sum(input: &[u32]) -> u32;
44 }
45
46 struct AVX;
47 # #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
48 impl Backend for AVX {
49 #[target_feature(enable = "avx")]
50 unsafe fn sum(xs: &[u32]) -> u32 {
51 // ...
52 todo!();
53 }
54 }
55
56 struct AVX2;
57 # #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
58 impl Backend for AVX2 {
59 #[target_feature(enable = "avx2")]
60 unsafe fn sum(xs: &[u32]) -> u32 {
61 // ...
62 todo!();
63 }
64 }
65
66 // And now you want a have function which calls into that trait:
67 unsafe fn do_calculations<B>(xs: &[u32]) -> u32 where B: Backend {
68 let value = B::sum(xs);
69 // ...do some more calculations here...
70 value
71 }
72 ```
73
74 We have a problem here. This has to be marked with `#[target_feature]`, and that has to specify the concrete
75 feature flag for a given SIMD instruction set, but this function is generic so we can't do that!
76
77## How does this crate make it better?
78
79### You can now mark safe functions with `#[target_feature]`
80
81This crate exposes an `#[unsafe_target_feature]` macro which works just like `#[target_feature]` except
82it moves the `unsafe` from the function prototype into the macro name, and can be used on safe functions.
83
84```rust,compile_fail
85// ERROR: `#[target_feature(..)]` can only be applied to `unsafe` functions
86#[target_feature(enable = "avx2")]
87fn func() {}
88```
89
90```rust
91// It works, but must be `unsafe`
92# #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
93#[target_feature(enable = "avx2")]
94unsafe fn func() {}
95```
96
97```rust
98use curve25519_dalek_derive::unsafe_target_feature;
99
100// No `unsafe` on the function itself!
101# #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
102#[unsafe_target_feature("avx2")]
103fn func() {}
104```
105
106It can also be used to mark functions inside of impls:
107
108```rust,compile_fail
109struct S;
110
111impl core::ops::Add for S {
112 type Output = S;
113 // ERROR: method `add` has an incompatible type for trait
114 #[target_feature(enable = "avx2")]
115 unsafe fn add(self, rhs: S) -> S {
116 S
117 }
118}
119```
120
121```rust
122use curve25519_dalek_derive::unsafe_target_feature;
123
124struct S;
125
126# #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
127#[unsafe_target_feature("avx2")]
128impl core::ops::Add for S {
129 type Output = S;
130 // No `unsafe` on the function itself!
131 fn add(self, rhs: S) -> S {
132 S
133 }
134}
135
136```
137
138### You can generate specialized copies of a module for each target feature
139
140```rust
141use curve25519_dalek_derive::unsafe_target_feature_specialize;
142
143# #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
144#[unsafe_target_feature_specialize("sse2", "avx2", conditional("avx512ifma", nightly))]
145mod simd {
146 #[for_target_feature("sse2")]
147 pub const CONSTANT: u32 = 1;
148
149 #[for_target_feature("avx2")]
150 pub const CONSTANT: u32 = 2;
151
152 #[for_target_feature("avx512ifma")]
153 pub const CONSTANT: u32 = 3;
154
155 pub fn func() { /* ... */ }
156}
157
158# #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
159fn entry_point() {
160 #[cfg(nightly)]
161 if std::is_x86_feature_detected!("avx512ifma") {
162 return simd_avx512ifma::func();
163 }
164
165 if std::is_x86_feature_detected!("avx2") {
166 return simd_avx2::func();
167 }
168
169 if std::is_x86_feature_detected!("sse2") {
170 return simd_sse2::func();
171 }
172
173 unimplemented!();
174}
175```
176
177## How to use `#[unsafe_target_feature]`?
178
179 - Can be used on `fn`s, `impl`s and `mod`s.
180 - When used on a function will only apply to that function; it won't apply to any nested functions, traits, mods, etc.
181 - When used on an `impl` will only apply to all of the functions directly defined inside of that `impl`.
182 - When used on a `mod` will only apply to all of the `fn`s and `impl`s directly defined inside of that `mod`.
183 - Cannot be used on methods which use `self` or `Self`; instead use it on the `impl` in which the method is defined.
184
185## License
186
187Licensed under either of
188
189 * Apache License, Version 2.0, [LICENSE-APACHE](LICENSE-APACHE)
190 * MIT license ([LICENSE-MIT](LICENSE-MIT))
191
192at your option.
193
194### Contribution
195
196Unless you explicitly state otherwise, any contribution intentionally submitted
197for inclusion in the work by you, as defined in the Apache-2.0 license, shall be
198dual licensed as above, without any additional terms or conditions.