You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is a tracking issue for the unstable core_simd feature in the standard library. SIMD support is quite a thorny topic, but it's largely covered in rust-lang/rfcs#1199, being implemented in #27169, and @huonw will be creating an external crate for full-fledged SIMD support.
there is support for a lot of the intel x86/x86-64 intrinsics, but none of the AMD-specific ones
it also has pretty much all the ARM & AArch64 NEON ones
other targets have essentially no support
some of the fanciest intrinsics are missing, especially (IIRC) some of the ARM pointer ones
the cfg(target_feature = "...") detection is subpar, e.g. it doesn't detect features when an explicit -C target-cpu="..." is set, it doesn't handle disabling features like -C target-feature="-sse2", nor does it handle (AFAICT) for custom target specs
the cross-platform API is mostly there, for x86, ARM and AArch64
a small number of platform-intrinsics are exposed, but not a lot
the autogenerator (in etc) should be upgraded to handle emitting the actual wrappers as well as the raw extern blocks that it can currently do
this probably makes most sense after rewriting it into Rust, as a proper cargo binary checking in Cargo.lock and everything, using e.g. libs from crates.io (this is fine, as the binary is not part of the normal build process, it is only run when someone decides to)
I'm intending to work on the simd crate first, starting with the rewrite of the autogenerator, but I've currently got a thesis to work on.
@BurntSushi, @nikomatsakis, and I talked about this recently at the work week, and our thoughts are:
The original strategy of stabilizing the small pieces (intrinsics + #[repr(simd)]) in the compiler is probably the best way forward here. More experimentation can happen in the simd crate, but those are at least the bare bones for moving forward.
Specifically with intrinsics, we probably want to stabilize defining them in a different fashion, specifically:
That is, we'd probably stabilize a new #[simd_intrinsic] (ish) attribute whose name would closely follow the standard naming conventions (e.g. those in C compilers). The function would look like normal Rust and look like it recurses into itself but the compiler would understand that direct calls to the function are actually implemented inline, so this isn't actually infinite recursion.
The "structural typing" of the intrinsics is relatively ok. That is, the compiler can verify any definition is correct, even though each definition may be slightly different (e.g. any 32-bit x 4 value could show up perhaps). The bad part, however, is that not all intrinsics can be verified. For example the simd_lt intrinsic can have any number of SIMD types instantiated, but a pairing like String and Vec<u8> would be nonsensical. This may be difficult to solve in a "pure" fashion but may be worth stomaching to stabilize the SIMD intrinsics (which in general should never be called in favor of the simd crate itself)
Specifically with #[repr(simd)], we may want to remove support for tagging a generic structure. This does not appear to be used in the simd crate today and may not be necessary at all, in which case it's probably just complications that we don't want to have to think about today.
One possible route with the intrinsics would be a thought by @arielb1 where any illegal instantiation of type parameters just causes the intrinsic to codegen as a trap, which means that any instantiation is possible and some would just fail at runtime (which would need to be avoided anyway).
All of this was discussed hopefully with an eye to start the process of stabilization soon-ish, and then we can all get SIMD on stable Rust!
@alexcrichton Ahh, I ignored the multiple-definition option in my recent comment.
I think it's a great solution for integer and floating-point intrinsics, but I didn't consider stabilization of any intrinsic to be possible, hence why I tried to only think of options where libcore hosts all intrinsics.
I am still wary about stabilizing intrinsics, but #[simd_intrinsic] seems focused in scope, so I can see how that works. Although, would it be restricted to things that are definitely about SIMD?
There are various platform intrinsics that don't do anything with vectors, such as prefetch.
Other than that, this seems like a good move forward, without the complexities I was worried about.
@eddyb hm yeah I'm not sure if #[simd_intrinsic] is the best name, certainly up for debate! I would figure that all intrinsics would be defined through a similar mechanism, but the SIMD intrinsics were namespaced somehow so they're the only ones that we stabilize. I wouldn't want to stabilize, for example, intrinsics like prefetch (for now).
Oh interesting! I'd be ok punting on those for now in favor of just dealing with the SIMD pieces, but we can relatively easily reevaluate to do something different though.
So I had a really interesting conversation with @sunfishcode on the topic of SIMD, and in particular the design of SIMD in WASM. The high-level summary was two points:
The current breakdown (platform-specific intrinsics, a portable layer atop) is a good one.
Since we modeled the SIMD crate on the JS SIMD designs, which are now being incorporated into WASM, it will align well with the WASM design. Code that can be expressed in terms of the simd crate will thus also be a good candidate for compiling to WASM.
Some other interesting points that he raised:
SIMD has many audiences with diverse needs, and you can't necessarily accommodate them all very well with just one API:
codec authors want the raw intrinsics because they use them in clever and unexpected ways;
HPC people want higher-level abstractions but don't need access to every trick in the book;
high-performance demands also require raw intrinsics, because they don't mind investing the time to reshape the algorithm for each platform.
One way to support these diverse needs, which has been considered for WASM, is to offer the "union" of features across platforms, but offer a way to query which features are "fast" (the idea is that the "slow" features will be emulated). In Rust I would expect we may want similar things, though perhaps the "slow" paths would just trap? (It's probably a bug if you actually wind up executing one of them.)
On the topic of intrinsics, I feel overall pretty good about some kind of attribute that can be applied to a fn to indicate that the compiler should compile it via pure instructions. Such functions would have to have appropriate argument/return types (roughly like today). If the argument/return types are not generic, this seems very harmless to me, as we can check it purely at the definition site (as @alexcrichton noted).
However, I feel mildly less good about the generic versions, since these cannot be checked until trans time, which means we have to face two annoying choices:
issue traps or compilation errors when an intrinsic is used incorrectly;
introduce more machinery into the compiler like special SIMD traits.
However, it does seem that there is a third way out: we could remove all support for generic intrinsics, and instead have people define their own traits that map to these operations. For example, today the simd crate does something roughly like this:
traitSimd{// no longer an unsafe traittypeEqType;// we now include a method for the various simd operations we might want to do:fneq(x:&Self,y:&Self) -> Self::EqType;
...
}#[simd_intrinsic]fneq_u32x4(x:u32x4,y:u32x4) -> boolx4{...}implSimdforu32x4{#[inline(always)]fneq(x:&Self,y:&Self) -> Self::EqType{eq_u32x4(x, y)}}
I'm probably getting some of the details wrong (have to consult the crate for the precise names involved) but hopefully you get the idea. Basically, the compiler only supports monotype intrinsics, and the wrapper crate adds (using normal trait methods) any generic dispatch needed.
The function would look like normal Rust and look like it recurses into itself but the compiler would understand that direct calls to the function are actually implemented inline, so this isn't actually infinite recursion.
Is there a good reason for making the function recurse into itself? It seems like unnecessary repetition to me. Would a macro like intrinsic!(), similar to unreachable!(), be possible?
codec authors want the raw intrinsics because they use them in clever and unexpected ways;
HPC people want higher-level abstractions but don't need access to every trick in the book;
high-performance demands also require raw intrinsics, because they don't mind investing the time to reshape the algorithm for each platform.
I agree. This is one of the papercuts of the current state: most of the platform-specific intrinsics are there with their usual names, except for a few basic arithmetic operations, which are simd_add and such. I think it would be better to expose all of the raw platform intrinsics and build a higher-level cross-platform simd_add on top of that with #[cfg(target_feature)]. A crate like simd could build on top of that by providing fallback (e.g. two SSE adds if AVX is not available). It wouldn’t be generic, but does it need to be? I can’t think of a #[repr(simd)] type that is not just an n-tuple of the scalar type. And for the low-level intrinsics the types have little meaning anyway (e.g. _mm256_cmp_ps returns a vector of floats, but actually they are bitmasks).
Is there a good reason for making the function recurse into itself?
Maybe it's contrived, but casting the function to a function pointer would naturally give you a pointer to a function which contains the intrinsic operation.
except for a few basic arithmetic operations, which are simd_add and such
There's a very good reason for keeping those that way: they're basic LLVM operations (i.e. simd_add is just the integer/float add you get for + but with vector arguments) and LLVM can optimize them, unlike arbitrary intrinsics, which are function calls and get lowered in target codegen.
Activity
pnkfelix commentedon Oct 6, 2015
Note that #26403 may well be a blocker issue for 100% safe + composable SIMD
aturon commentedon Nov 5, 2015
This issue now tracks the
simd,simd_ffi, andrepr_simdfeatures.huonw commentedon Dec 11, 2015
Status update:
In the compiler:
cfg(target_feature = "...")detection is subpar, e.g. it doesn't detect features when an explicit-C target-cpu="..."is set, it doesn't handle disabling features like-C target-feature="-sse2", nor does it handle (AFAICT) for custom target specsIn https://github.com/huonw/simd:
etc) should be upgraded to handle emitting the actual wrappers as well as the rawexternblocks that it can currently doI'm intending to work on the simd crate first, starting with the rewrite of the autogenerator, but I've currently got a thesis to work on.
bstrie commentedon Mar 29, 2016
@huonw How's the thesis going? :) Any progress on this issue, to relay to those interested in SIMD stabilization?
alexcrichton commentedon Jun 17, 2016
@BurntSushi, @nikomatsakis, and I talked about this recently at the work week, and our thoughts are:
The original strategy of stabilizing the small pieces (intrinsics + #[repr(simd)]) in the compiler is probably the best way forward here. More experimentation can happen in the
simdcrate, but those are at least the bare bones for moving forward.Specifically with intrinsics, we probably want to stabilize defining them in a different fashion, specifically:
That is, we'd probably stabilize a new
#[simd_intrinsic](ish) attribute whose name would closely follow the standard naming conventions (e.g. those in C compilers). The function would look like normal Rust and look like it recurses into itself but the compiler would understand that direct calls to the function are actually implemented inline, so this isn't actually infinite recursion.The "structural typing" of the intrinsics is relatively ok. That is, the compiler can verify any definition is correct, even though each definition may be slightly different (e.g. any 32-bit x 4 value could show up perhaps). The bad part, however, is that not all intrinsics can be verified. For example the
simd_ltintrinsic can have any number of SIMD types instantiated, but a pairing likeStringandVec<u8>would be nonsensical. This may be difficult to solve in a "pure" fashion but may be worth stomaching to stabilize the SIMD intrinsics (which in general should never be called in favor of thesimdcrate itself)Specifically with
#[repr(simd)], we may want to remove support for tagging a generic structure. This does not appear to be used in thesimdcrate today and may not be necessary at all, in which case it's probably just complications that we don't want to have to think about today.One possible route with the intrinsics would be a thought by @arielb1 where any illegal instantiation of type parameters just causes the intrinsic to codegen as a trap, which means that any instantiation is possible and some would just fail at runtime (which would need to be avoided anyway).
All of this was discussed hopefully with an eye to start the process of stabilization soon-ish, and then we can all get SIMD on stable Rust!
cc @eddyb, you likely have many opinions as well!
eddyb commentedon Jun 17, 2016
@alexcrichton Ahh, I ignored the multiple-definition option in my recent comment.
I think it's a great solution for integer and floating-point intrinsics, but I didn't consider stabilization of any intrinsic to be possible, hence why I tried to only think of options where
libcorehosts all intrinsics.I am still wary about stabilizing intrinsics, but
#[simd_intrinsic]seems focused in scope, so I can see how that works. Although, would it be restricted to things that are definitely about SIMD?There are various platform intrinsics that don't do anything with vectors, such as
prefetch.Other than that, this seems like a good move forward, without the complexities I was worried about.
alexcrichton commentedon Jun 17, 2016
@eddyb hm yeah I'm not sure if
#[simd_intrinsic]is the best name, certainly up for debate! I would figure that all intrinsics would be defined through a similar mechanism, but the SIMD intrinsics were namespaced somehow so they're the only ones that we stabilize. I wouldn't want to stabilize, for example, intrinsics like prefetch (for now).BurntSushi commentedon Jun 17, 2016
There are other useful intrinsics like
crc32that are explicitly part of SSE 4.2 but aren't necessarily SIMD.alexcrichton commentedon Jun 17, 2016
Oh interesting! I'd be ok punting on those for now in favor of just dealing with the SIMD pieces, but we can relatively easily reevaluate to do something different though.
nikomatsakis commentedon Jun 17, 2016
So I had a really interesting conversation with @sunfishcode on the topic of SIMD, and in particular the design of SIMD in WASM. The high-level summary was two points:
Some other interesting points that he raised:
nikomatsakis commentedon Jun 17, 2016
On the topic of intrinsics, I feel overall pretty good about some kind of attribute that can be applied to a
fnto indicate that the compiler should compile it via pure instructions. Such functions would have to have appropriate argument/return types (roughly like today). If the argument/return types are not generic, this seems very harmless to me, as we can check it purely at the definition site (as @alexcrichton noted).However, I feel mildly less good about the generic versions, since these cannot be checked until trans time, which means we have to face two annoying choices:
However, it does seem that there is a third way out: we could remove all support for generic intrinsics, and instead have people define their own traits that map to these operations. For example, today the
simdcrate does something roughly like this:It seems like we could instead do:
I'm probably getting some of the details wrong (have to consult the crate for the precise names involved) but hopefully you get the idea. Basically, the compiler only supports monotype intrinsics, and the wrapper crate adds (using normal trait methods) any generic dispatch needed.
ruuda commentedon Jun 17, 2016
Is there a good reason for making the function recurse into itself? It seems like unnecessary repetition to me. Would a macro like
intrinsic!(), similar tounreachable!(), be possible?I agree. This is one of the papercuts of the current state: most of the platform-specific intrinsics are there with their usual names, except for a few basic arithmetic operations, which are
simd_addand such. I think it would be better to expose all of the raw platform intrinsics and build a higher-level cross-platformsimd_addon top of that with#[cfg(target_feature)]. A crate likesimdcould build on top of that by providing fallback (e.g. two SSE adds if AVX is not available). It wouldn’t be generic, but does it need to be? I can’t think of a#[repr(simd)]type that is not just an n-tuple of the scalar type. And for the low-level intrinsics the types have little meaning anyway (e.g._mm256_cmp_psreturns a vector of floats, but actually they are bitmasks).eddyb commentedon Jun 17, 2016
Maybe it's contrived, but casting the function to a function pointer would naturally give you a pointer to a function which contains the intrinsic operation.
There's a very good reason for keeping those that way: they're basic LLVM operations (i.e.
simd_addis just the integer/floataddyou get for+but with vector arguments) and LLVM can optimize them, unlike arbitrary intrinsics, which are function calls and get lowered in target codegen.60 remaining items
Amanieu commentedon Aug 4, 2023
Zbb doesn't need to be exposed as intrinsics, all of the functionality is already available as methods on plain integer types.
Crypto intrinsics are fine to add though, but please open a new tracking issue for them when you send a PR.
Auto merge of rust-lang#117372 - Amanieu:stdarch_update, r=Mark-Simul…
Rollup merge of rust-lang#117372 - Amanieu:stdarch_update, r=Mark-Sim…
Auto merge of rust-lang#117372 - Amanieu:stdarch_update, r=Mark-Simul…
Auto merge of rust-lang#117372 - Amanieu:stdarch_update, r=Mark-Simul…
Auto merge of rust-lang#117372 - Amanieu:stdarch_update, r=Mark-Simul…
Lokathor commentedon Feb 5, 2024
@workingjubilee bors has run rampant with power, this issue isn't done.
CryZe commentedon Feb 5, 2024
The commit explicitly says that this issue got split into various smaller issues. The question is rather where are they?
deps: update hashbrown
ChrisDenton commentedon Feb 8, 2024
Presumably: https://github.com/rust-lang/rust/issues?q=is%3Aissue+is%3Aopen++label%3AA-simd+label%3AC-tracking-issue
zachs18 commentedon Feb 8, 2024
Amanieu commentedon Feb 11, 2024
Here is the full set of new tracking issues for what
stdsimdwas previous tracking:Auto merge of rust-lang#117372 - Amanieu:stdarch_update, r=Mark-Simul…
sdk%feat!: bump MSRV to 1.89, add `portable` feature, define system r…