-
Notifications
You must be signed in to change notification settings - Fork 12.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
simd_shuffle, simd_insert, simd_extract should use const generics #85229
Comments
Would this enable using |
I had not thought about this... maybe? Do const generic support "dependent types" like this one? fn simd_shuffle<const N: usize, const IDX: [u32; N]>(...) |
I think the full const generics feature does support that, but there's still the open question on how we know whether the type is StructuralEq. For this specific case it is easy, because the array length does not influence that question, but we still need to figure out the details here |
cc @Amanieu -- is this is a case that should use the new attribute you added or have been adding? |
Not really. |
In that case... how about we change the const generic argument to be a slice? Right now this #![feature(const_generics)]
#![allow(incomplete_features)]
fn foo<const X: &'static [usize]>() {
}
fn main() {
foo::<{&[1, 2, 3, 4]}>();
} ICEs with
but it seems totally straight forward to fix |
FWIW, there also currently seems to be no way to use any generic parameters in a const generic expression, even when indirecting via associated consts. Until that is resolved, using const generics for #![feature(const_generics, const_evaluatable_checked)]
pub fn print_const<const N: usize>() {
println!("{}", N);
}
struct Const<T>(T);
impl<T> Const<T> {
const N: usize = 5;
}
pub fn print_thing<T>() {
print_const::<{Const::<T>::N}>()
} This needs extra |
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
…gjubilee Prototype using const generic for simd_shuffle IDX array cc rust-lang#85229 r? `@workingjubilee` on the design TLDR: there is now a `fn simd_shuffle_generic<T, U, const IDX: &'static [u32]>(x: T, y: T) -> U;` intrinsic that allows replacing ```rust simd_shuffle(a, b, const { stuff }) ``` with ```rust simd_shuffle_generic::<_, _, {&stuff}>(a, b) ``` which makes the compiler implementations much simpler, if we manage to at some point eliminate `simd_shuffle`. There are some issues with this today though (can't do math without bubbling it up in the generic arguments). With this change, we can start porting the simple cases and get better data on the others.
Prototype using const generic for simd_shuffle IDX array cc rust-lang/rust#85229 r? `@workingjubilee` on the design TLDR: there is now a `fn simd_shuffle_generic<T, U, const IDX: &'static [u32]>(x: T, y: T) -> U;` intrinsic that allows replacing ```rust simd_shuffle(a, b, const { stuff }) ``` with ```rust simd_shuffle_generic::<_, _, {&stuff}>(a, b) ``` which makes the compiler implementations much simpler, if we manage to at some point eliminate `simd_shuffle`. There are some issues with this today though (can't do math without bubbling it up in the generic arguments). With this change, we can start porting the simple cases and get better data on the others.
Prototype using const generic for simd_shuffle IDX array cc rust-lang/rust#85229 r? `@workingjubilee` on the design TLDR: there is now a `fn simd_shuffle_generic<T, U, const IDX: &'static [u32]>(x: T, y: T) -> U;` intrinsic that allows replacing ```rust simd_shuffle(a, b, const { stuff }) ``` with ```rust simd_shuffle_generic::<_, _, {&stuff}>(a, b) ``` which makes the compiler implementations much simpler, if we manage to at some point eliminate `simd_shuffle`. There are some issues with this today though (can't do math without bubbling it up in the generic arguments). With this change, we can start porting the simple cases and get better data on the others.
A const-generic version of simd_shuffle now exists. However, using it requires generic_const_exprs. From talking with @lcnr, this feature is still pretty unstable, so unfortunately stdarch cannot currently use simd_shuffle_generic. It also needs adt_const_params since the array is passed as a slice. That's more mature though some thorny open questions make it unfit for stabilization (but I don't think those are a blocker for use in stdarch). |
My understanding is that the main complexity with generic_const_exprs is "what happens if const-eval fails". So would it make sense to try and carve out a min_generic_const_exprs of guaranteed-not-to-fail expressions? That would contain e.g. constructing an array and |
There are also quite a few issues wrt to generic consts in where-clauses and some other less fundamental implementation challenges. While it hasn't been updated recently as there hasn't been any focussed work on gce, here's a list of the known issues https://github.com/rust-lang/project-const-generics/issues?q=is%3Aissue+is%3Aopen+label%3AA-generic-exprs+label%3AC-design-docs+ |
Prototype using const generic for simd_shuffle IDX array cc rust-lang/rust#85229 r? `@workingjubilee` on the design TLDR: there is now a `fn simd_shuffle_generic<T, U, const IDX: &'static [u32]>(x: T, y: T) -> U;` intrinsic that allows replacing ```rust simd_shuffle(a, b, const { stuff }) ``` with ```rust simd_shuffle_generic::<_, _, {&stuff}>(a, b) ``` which makes the compiler implementations much simpler, if we manage to at some point eliminate `simd_shuffle`. There are some issues with this today though (can't do math without bubbling it up in the generic arguments). With this change, we can start porting the simple cases and get better data on the others.
Prototype using const generic for simd_shuffle IDX array cc rust-lang/rust#85229 r? `@workingjubilee` on the design TLDR: there is now a `fn simd_shuffle_generic<T, U, const IDX: &'static [u32]>(x: T, y: T) -> U;` intrinsic that allows replacing ```rust simd_shuffle(a, b, const { stuff }) ``` with ```rust simd_shuffle_generic::<_, _, {&stuff}>(a, b) ``` which makes the compiler implementations much simpler, if we manage to at some point eliminate `simd_shuffle`. There are some issues with this today though (can't do math without bubbling it up in the generic arguments). With this change, we can start porting the simple cases and get better data on the others.
simd_shuffle
requires its last parameter, the array defining the shuffling, to be a constant:rust/compiler/rustc_codegen_llvm/src/intrinsic.rs
Line 888 in 487e273
Currently (after #85110), this is enforced by an ad-hoc hack in
compiler/rustc_mir/src/transform/lower_intrinsics.rs
.But I think it'd be better to instead change the type of
simd_shuffle
to make the shuffle array a const generic parameter. I am not doing this yet since const generic arrays require enabling anincomplete_feature
which is not great, but once const generic arrays are sufficiently stable we could use them as a more principled way to ensure thatsimd_shuffle
is called in the right way.Update (2024-02-17): A const-generic version of simd_shuffle now exists, but using it in stdarch would require generic_const_exprs and that's too experimental for such uses.
Cc @rust-lang/project-const-generics
The text was updated successfully, but these errors were encountered: