-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
Prototype using const generic for simd_shuffle IDX array #115933
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,7 @@ use rustc_codegen_ssa::mir::place::PlaceRef; | |
use rustc_codegen_ssa::traits::*; | ||
use rustc_hir as hir; | ||
use rustc_middle::ty::layout::{FnAbiOf, HasTyCtxt, LayoutOf}; | ||
use rustc_middle::ty::{self, Ty}; | ||
use rustc_middle::ty::{self, GenericArgsRef, Ty}; | ||
use rustc_middle::{bug, span_bug}; | ||
use rustc_span::{sym, symbol::kw, Span, Symbol}; | ||
use rustc_target::abi::{self, Align, HasDataLayout, Primitive}; | ||
|
@@ -376,7 +376,9 @@ impl<'ll, 'tcx> IntrinsicCallMethods<'tcx> for Builder<'_, 'll, 'tcx> { | |
} | ||
|
||
_ if name.as_str().starts_with("simd_") => { | ||
match generic_simd_intrinsic(self, name, callee_ty, args, ret_ty, llret_ty, span) { | ||
match generic_simd_intrinsic( | ||
self, name, callee_ty, fn_args, args, ret_ty, llret_ty, span, | ||
) { | ||
Ok(llval) => llval, | ||
Err(()) => return, | ||
} | ||
|
@@ -911,6 +913,7 @@ fn generic_simd_intrinsic<'ll, 'tcx>( | |
bx: &mut Builder<'_, 'll, 'tcx>, | ||
name: Symbol, | ||
callee_ty: Ty<'tcx>, | ||
fn_args: GenericArgsRef<'tcx>, | ||
args: &[OperandRef<'tcx, &'ll Value>], | ||
ret_ty: Ty<'tcx>, | ||
llret_ty: &'ll Type, | ||
|
@@ -1030,6 +1033,56 @@ fn generic_simd_intrinsic<'ll, 'tcx>( | |
)); | ||
} | ||
|
||
if name == sym::simd_shuffle_generic { | ||
let idx = fn_args[2] | ||
.expect_const() | ||
.eval(tcx, ty::ParamEnv::reveal_all(), Some(span)) | ||
.unwrap() | ||
.unwrap_branch(); | ||
let n = idx.len() as u64; | ||
|
||
require_simd!(ret_ty, InvalidMonomorphization::SimdReturn { span, name, ty: ret_ty }); | ||
let (out_len, out_ty) = ret_ty.simd_size_and_type(bx.tcx()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it's copy paste from the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are 8 occurences now (there were 7) of this same code. We should probably deduplicate something here and reconsider the names, but not in this PR ^^ |
||
require!( | ||
out_len == n, | ||
InvalidMonomorphization::ReturnLength { span, name, in_len: n, ret_ty, out_len } | ||
); | ||
require!( | ||
in_elem == out_ty, | ||
InvalidMonomorphization::ReturnElement { span, name, in_elem, in_ty, ret_ty, out_ty } | ||
); | ||
|
||
let total_len = in_len * 2; | ||
|
||
let indices: Option<Vec<_>> = idx | ||
.iter() | ||
.enumerate() | ||
.map(|(arg_idx, val)| { | ||
let idx = val.unwrap_leaf().try_to_i32().unwrap(); | ||
if idx >= i32::try_from(total_len).unwrap() { | ||
bx.sess().emit_err(InvalidMonomorphization::ShuffleIndexOutOfBounds { | ||
span, | ||
name, | ||
arg_idx: arg_idx as u64, | ||
total_len: total_len.into(), | ||
}); | ||
None | ||
} else { | ||
Some(bx.const_i32(idx)) | ||
} | ||
}) | ||
.collect(); | ||
let Some(indices) = indices else { | ||
return Ok(bx.const_null(llret_ty)); | ||
}; | ||
|
||
return Ok(bx.shuffle_vector( | ||
args[0].immediate(), | ||
args[1].immediate(), | ||
bx.const_vector(&indices), | ||
)); | ||
} | ||
|
||
if name == sym::simd_shuffle { | ||
// Make sure this is actually an array, since typeck only checks the length-suffixed | ||
// version of this intrinsic. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
// build-fail | ||
|
||
#![feature(repr_simd, platform_intrinsics, rustc_attrs)] | ||
#![feature(repr_simd, platform_intrinsics, rustc_attrs, adt_const_params)] | ||
#![allow(incomplete_features)] | ||
|
||
#[repr(simd)] | ||
#[derive(Copy, Clone)] | ||
|
@@ -35,6 +36,7 @@ extern "platform-intrinsic" { | |
fn simd_extract<T, E>(x: T, idx: u32) -> E; | ||
|
||
fn simd_shuffle<T, I, U>(x: T, y: T, idx: I) -> U; | ||
fn simd_shuffle_generic<T, U, const IDX: &'static [u32]>(x: T, y: T) -> U; | ||
} | ||
|
||
fn main() { | ||
|
@@ -71,5 +73,29 @@ fn main() { | |
//~^ ERROR expected return type of length 4, found `i32x8` with length 8 | ||
simd_shuffle::<_, _, i32x2>(x, x, IDX8); | ||
//~^ ERROR expected return type of length 8, found `i32x2` with length 2 | ||
|
||
const I2: &[u32] = &[0; 2]; | ||
simd_shuffle_generic::<i32, i32, I2>(0, 0); | ||
//~^ ERROR expected SIMD input type, found non-SIMD `i32` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It should really say "scalar" in this case, but... probably not a real concern. |
||
const I4: &[u32] = &[0; 4]; | ||
simd_shuffle_generic::<i32, i32, I4>(0, 0); | ||
//~^ ERROR expected SIMD input type, found non-SIMD `i32` | ||
const I8: &[u32] = &[0; 8]; | ||
simd_shuffle_generic::<i32, i32, I8>(0, 0); | ||
//~^ ERROR expected SIMD input type, found non-SIMD `i32` | ||
|
||
simd_shuffle_generic::<_, f32x2, I2>(x, x); | ||
//~^ ERROR element type `i32` (element of input `i32x4`), found `f32x2` with element type `f32` | ||
simd_shuffle_generic::<_, f32x4, I4>(x, x); | ||
//~^ ERROR element type `i32` (element of input `i32x4`), found `f32x4` with element type `f32` | ||
simd_shuffle_generic::<_, f32x8, I8>(x, x); | ||
//~^ ERROR element type `i32` (element of input `i32x4`), found `f32x8` with element type `f32` | ||
|
||
simd_shuffle_generic::<_, i32x8, I2>(x, x); | ||
//~^ ERROR expected return type of length 2, found `i32x8` with length 8 | ||
simd_shuffle_generic::<_, i32x8, I4>(x, x); | ||
//~^ ERROR expected return type of length 4, found `i32x8` with length 8 | ||
simd_shuffle_generic::<_, i32x2, I8>(x, x); | ||
//~^ ERROR expected return type of length 8, found `i32x2` with length 2 | ||
Comment on lines
+98
to
+99
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems wrong, it shouldn't error? Given:
the shufflevector instruction produces a vector of type T but size M, and M > N is legitimate (especially in the case of "unifying two vectors where There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oh, I see. apologies, yes, that's correct. Hmm that's... annoying, but anything more fancy is probably too painful to implement at this level of the type system. |
||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should have used u32. Will fix in https://github.com/bjorn3/rustc_codegen_cranelift