about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2021-06-05 06:17:17 +0000
committerbors <bors@rust-lang.org>2021-06-05 06:17:17 +0000
commit4e20754629c2ee51760d2d5ab3abb09d3e994e8a (patch)
treeb7548624e125ab9b5f2b0aaf41d7f5c4abd4a2b2
parent2c106885d5763250d3c5d5ca19b8491af816581d (diff)
parentd9a5df669c45204daa5e10f5221ff82026fef9e6 (diff)
downloadrust-4e20754629c2ee51760d2d5ab3abb09d3e994e8a.tar.gz
rust-4e20754629c2ee51760d2d5ab3abb09d3e994e8a.zip
Auto merge of #85919 - workingjubilee:simd-ptrs-are-valid, r=petrochenkov
Allow raw pointers in SIMD types

Closes #85915 by loosening the strictness in typechecking and adding a test to guarantee it passes.

This still might be too strict, as references currently do pass monomorphization, but my understanding is that they are not guaranteed to be "scalar" in the same way.
-rw-r--r--compiler/rustc_middle/src/ty/sty.rs5
-rw-r--r--compiler/rustc_typeck/src/check/check.rs15
-rw-r--r--src/test/ui/simd/issue-85915-simd-ptrs.rs67
-rw-r--r--src/test/ui/simd/simd-type-generic-monomorphisation-extern-nonnull-ptr.rs23
-rw-r--r--src/test/ui/simd/simd-type-generic-monomorphisation-wide-ptr.rs12
-rw-r--r--src/test/ui/simd/simd-type-generic-monomorphisation-wide-ptr.stderr4
-rw-r--r--src/test/ui/simd/simd-type-wide-ptr.rs12
-rw-r--r--src/test/ui/simd/simd-type-wide-ptr.stderr4
8 files changed, 134 insertions, 8 deletions
diff --git a/compiler/rustc_middle/src/ty/sty.rs b/compiler/rustc_middle/src/ty/sty.rs
index 012d9bd82c8..c1f71fbbfa4 100644
--- a/compiler/rustc_middle/src/ty/sty.rs
+++ b/compiler/rustc_middle/src/ty/sty.rs
@@ -1891,11 +1891,6 @@ impl<'tcx> TyS<'tcx> {
     }
 
     #[inline]
-    pub fn is_machine(&self) -> bool {
-        matches!(self.kind(), Int(..) | Uint(..) | Float(..))
-    }
-
-    #[inline]
     pub fn has_concrete_skeleton(&self) -> bool {
         !matches!(self.kind(), Param(_) | Infer(_) | Error(_))
     }
diff --git a/compiler/rustc_typeck/src/check/check.rs b/compiler/rustc_typeck/src/check/check.rs
index 92d7ea26003..166410731d2 100644
--- a/compiler/rustc_typeck/src/check/check.rs
+++ b/compiler/rustc_typeck/src/check/check.rs
@@ -1214,10 +1214,19 @@ pub fn check_simd(tcx: TyCtxt<'_>, sp: Span, def_id: LocalDefId) {
                 }
             }
 
+            // Check that we use types valid for use in the lanes of a SIMD "vector register"
+            // These are scalar types which directly match a "machine" type
+            // Yes: Integers, floats, "thin" pointers
+            // No: char, "fat" pointers, compound types
             match e.kind() {
-                ty::Param(_) => { /* struct<T>(T, T, T, T) is ok */ }
-                _ if e.is_machine() => { /* struct(u8, u8, u8, u8) is ok */ }
-                ty::Array(ty, _c) if ty.is_machine() => { /* struct([f32; 4]) */ }
+                ty::Param(_) => (), // pass struct<T>(T, T, T, T) through, let monomorphization catch errors
+                ty::Int(_) | ty::Uint(_) | ty::Float(_) | ty::RawPtr(_) => (), // struct(u8, u8, u8, u8) is ok
+                ty::Array(t, _clen)
+                    if matches!(
+                        t.kind(),
+                        ty::Int(_) | ty::Uint(_) | ty::Float(_) | ty::RawPtr(_)
+                    ) =>
+                { /* struct([f32; 4]) is ok */ }
                 _ => {
                     struct_span_err!(
                         tcx.sess,
diff --git a/src/test/ui/simd/issue-85915-simd-ptrs.rs b/src/test/ui/simd/issue-85915-simd-ptrs.rs
new file mode 100644
index 00000000000..6fe415545f8
--- /dev/null
+++ b/src/test/ui/simd/issue-85915-simd-ptrs.rs
@@ -0,0 +1,67 @@
+// run-pass
+// ignore-emscripten
+
+// Short form of the generic gather/scatter tests,
+// verifying simd([*const T; N]) and simd([*mut T; N]) pass typeck and work.
+#![feature(repr_simd, platform_intrinsics)]
+#![allow(non_camel_case_types)]
+
+#[repr(simd)]
+#[derive(Copy, Clone, PartialEq, Debug)]
+struct cptrx4<T>([*const T; 4]);
+
+#[repr(simd)]
+#[derive(Copy, Clone, PartialEq, Debug)]
+struct mptrx4<T>([*mut T; 4]);
+
+#[repr(simd)]
+#[derive(Copy, Clone, PartialEq, Debug)]
+struct f32x4([f32; 4]);
+
+#[repr(simd)]
+#[derive(Copy, Clone, PartialEq, Debug)]
+struct i32x4([i32; 4]);
+
+extern "platform-intrinsic" {
+    fn simd_gather<T, U, V>(x: T, y: U, z: V) -> T;
+    fn simd_scatter<T, U, V>(x: T, y: U, z: V) -> ();
+}
+
+fn main() {
+    let mut x = [0_f32, 1., 2., 3., 4., 5., 6., 7.];
+
+    let default = f32x4([-3_f32, -3., -3., -3.]);
+    let s_strided = f32x4([0_f32, 2., -3., 6.]);
+    let mask = i32x4([-1_i32, -1, 0, -1]);
+
+    // reading from *const
+    unsafe {
+        let pointer = &x as *const f32;
+        let pointers =  cptrx4([
+            pointer.offset(0) as *const f32,
+            pointer.offset(2),
+            pointer.offset(4),
+            pointer.offset(6)
+        ]);
+
+        let r_strided = simd_gather(default, pointers, mask);
+
+        assert_eq!(r_strided, s_strided);
+    }
+
+    // writing to *mut
+    unsafe {
+        let pointer = &mut x as *mut f32;
+        let pointers = mptrx4([
+            pointer.offset(0) as *mut f32,
+            pointer.offset(2),
+            pointer.offset(4),
+            pointer.offset(6)
+        ]);
+
+        let values = f32x4([42_f32, 43_f32, 44_f32, 45_f32]);
+        simd_scatter(values, pointers, mask);
+
+        assert_eq!(x, [42., 1., 43., 3., 4., 5., 45., 7.]);
+    }
+}
diff --git a/src/test/ui/simd/simd-type-generic-monomorphisation-extern-nonnull-ptr.rs b/src/test/ui/simd/simd-type-generic-monomorphisation-extern-nonnull-ptr.rs
new file mode 100644
index 00000000000..ae321c974b9
--- /dev/null
+++ b/src/test/ui/simd/simd-type-generic-monomorphisation-extern-nonnull-ptr.rs
@@ -0,0 +1,23 @@
+// run-pass
+// ignore-emscripten
+
+#![feature(extern_types)]
+#![feature(repr_simd)]
+
+use std::ptr::NonNull;
+
+extern {
+    type Extern;
+}
+
+#[repr(simd)]
+struct S<T>(T);
+
+#[inline(never)]
+fn identity<T>(v: T) -> T {
+    v
+}
+
+fn main() {
+    let _v: S<[Option<NonNull<Extern>>; 4]> = identity(S([None; 4]));
+}
diff --git a/src/test/ui/simd/simd-type-generic-monomorphisation-wide-ptr.rs b/src/test/ui/simd/simd-type-generic-monomorphisation-wide-ptr.rs
new file mode 100644
index 00000000000..3e02b08ce5d
--- /dev/null
+++ b/src/test/ui/simd/simd-type-generic-monomorphisation-wide-ptr.rs
@@ -0,0 +1,12 @@
+// build-fail
+
+#![feature(repr_simd)]
+
+// error-pattern:monomorphising SIMD type `S<[*mut [u8]; 4]>` with a non-primitive-scalar (integer/float/pointer) element type `*mut [u8]`
+
+#[repr(simd)]
+struct S<T>(T);
+
+fn main() {
+    let _v: Option<S<[*mut [u8]; 4]>> = None;
+}
diff --git a/src/test/ui/simd/simd-type-generic-monomorphisation-wide-ptr.stderr b/src/test/ui/simd/simd-type-generic-monomorphisation-wide-ptr.stderr
new file mode 100644
index 00000000000..3888e7a0f38
--- /dev/null
+++ b/src/test/ui/simd/simd-type-generic-monomorphisation-wide-ptr.stderr
@@ -0,0 +1,4 @@
+error: monomorphising SIMD type `S<[*mut [u8]; 4]>` with a non-primitive-scalar (integer/float/pointer) element type `*mut [u8]`
+
+error: aborting due to previous error
+
diff --git a/src/test/ui/simd/simd-type-wide-ptr.rs b/src/test/ui/simd/simd-type-wide-ptr.rs
new file mode 100644
index 00000000000..88f62a07ea0
--- /dev/null
+++ b/src/test/ui/simd/simd-type-wide-ptr.rs
@@ -0,0 +1,12 @@
+// build-fail
+
+#![feature(repr_simd)]
+
+// error-pattern:monomorphising SIMD type `S` with a non-primitive-scalar (integer/float/pointer) element type `*mut [u8]`
+
+#[repr(simd)]
+struct S([*mut [u8]; 4]);
+
+fn main() {
+    let _v: Option<S> = None;
+}
diff --git a/src/test/ui/simd/simd-type-wide-ptr.stderr b/src/test/ui/simd/simd-type-wide-ptr.stderr
new file mode 100644
index 00000000000..51d3c005072
--- /dev/null
+++ b/src/test/ui/simd/simd-type-wide-ptr.stderr
@@ -0,0 +1,4 @@
+error: monomorphising SIMD type `S` with a non-primitive-scalar (integer/float/pointer) element type `*mut [u8]`
+
+error: aborting due to previous error
+