Skip to content

Commit 5e6c2b6

Browse files
committed
Auto merge of #125892 - workingjubilee:rollup-gytt1q7, r=workingjubilee
Rollup of 3 pull requests Successful merges: - #125311 (Make repr(packed) vectors work with SIMD intrinsics) - #125849 (Migrate `run-make/emit-named-files` to `rmake.rs`) - #125851 (Add some more specific checks to the MIR validator) r? `@ghost` `@rustbot` modify labels: rollup
2 parents 8bec878 + 30dc2ba commit 5e6c2b6

File tree

7 files changed

+222
-57
lines changed

7 files changed

+222
-57
lines changed

compiler/rustc_codegen_llvm/src/intrinsic.rs

+53-1
Original file line numberDiff line numberDiff line change
@@ -482,8 +482,60 @@ impl<'ll, 'tcx> IntrinsicCallMethods<'tcx> for Builder<'_, 'll, 'tcx> {
482482
}
483483

484484
_ if name.as_str().starts_with("simd_") => {
485+
// Unpack non-power-of-2 #[repr(packed, simd)] arguments.
486+
// This gives them the expected layout of a regular #[repr(simd)] vector.
487+
let mut loaded_args = Vec::new();
488+
for (ty, arg) in arg_tys.iter().zip(args) {
489+
loaded_args.push(
490+
// #[repr(packed, simd)] vectors are passed like arrays (as references,
491+
// with reduced alignment and no padding) rather than as immediates.
492+
// We can use a vector load to fix the layout and turn the argument
493+
// into an immediate.
494+
if ty.is_simd()
495+
&& let OperandValue::Ref(place) = arg.val
496+
{
497+
let (size, elem_ty) = ty.simd_size_and_type(self.tcx());
498+
let elem_ll_ty = match elem_ty.kind() {
499+
ty::Float(f) => self.type_float_from_ty(*f),
500+
ty::Int(i) => self.type_int_from_ty(*i),
501+
ty::Uint(u) => self.type_uint_from_ty(*u),
502+
ty::RawPtr(_, _) => self.type_ptr(),
503+
_ => unreachable!(),
504+
};
505+
let loaded =
506+
self.load_from_place(self.type_vector(elem_ll_ty, size), place);
507+
OperandRef::from_immediate_or_packed_pair(self, loaded, arg.layout)
508+
} else {
509+
*arg
510+
},
511+
);
512+
}
513+
514+
let llret_ty = if ret_ty.is_simd()
515+
&& let abi::Abi::Aggregate { .. } = self.layout_of(ret_ty).layout.abi
516+
{
517+
let (size, elem_ty) = ret_ty.simd_size_and_type(self.tcx());
518+
let elem_ll_ty = match elem_ty.kind() {
519+
ty::Float(f) => self.type_float_from_ty(*f),
520+
ty::Int(i) => self.type_int_from_ty(*i),
521+
ty::Uint(u) => self.type_uint_from_ty(*u),
522+
ty::RawPtr(_, _) => self.type_ptr(),
523+
_ => unreachable!(),
524+
};
525+
self.type_vector(elem_ll_ty, size)
526+
} else {
527+
llret_ty
528+
};
529+
485530
match generic_simd_intrinsic(
486-
self, name, callee_ty, fn_args, args, ret_ty, llret_ty, span,
531+
self,
532+
name,
533+
callee_ty,
534+
fn_args,
535+
&loaded_args,
536+
ret_ty,
537+
llret_ty,
538+
span,
487539
) {
488540
Ok(llval) => llval,
489541
Err(()) => return Ok(()),

compiler/rustc_mir_transform/src/validate.rs

+94-7
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use rustc_middle::mir::coverage::CoverageKind;
88
use rustc_middle::mir::interpret::Scalar;
99
use rustc_middle::mir::visit::{NonUseContext, PlaceContext, Visitor};
1010
use rustc_middle::mir::*;
11+
use rustc_middle::ty::adjustment::PointerCoercion;
1112
use rustc_middle::ty::{
1213
self, CoroutineArgsExt, InstanceDef, ParamEnv, Ty, TyCtxt, TypeVisitableExt, Variance,
1314
};
@@ -1134,9 +1135,76 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
11341135
// FIXME(dyn-star): make sure nothing needs to be done here.
11351136
}
11361137
// FIXME: Add Checks for these
1137-
CastKind::PointerWithExposedProvenance
1138-
| CastKind::PointerExposeProvenance
1139-
| CastKind::PointerCoercion(_) => {}
1138+
CastKind::PointerWithExposedProvenance | CastKind::PointerExposeProvenance => {}
1139+
CastKind::PointerCoercion(PointerCoercion::ReifyFnPointer) => {
1140+
// FIXME: check signature compatibility.
1141+
check_kinds!(
1142+
op_ty,
1143+
"CastKind::{kind:?} input must be a fn item, not {:?}",
1144+
ty::FnDef(..)
1145+
);
1146+
check_kinds!(
1147+
target_type,
1148+
"CastKind::{kind:?} output must be a fn pointer, not {:?}",
1149+
ty::FnPtr(..)
1150+
);
1151+
}
1152+
CastKind::PointerCoercion(PointerCoercion::UnsafeFnPointer) => {
1153+
// FIXME: check safety and signature compatibility.
1154+
check_kinds!(
1155+
op_ty,
1156+
"CastKind::{kind:?} input must be a fn pointer, not {:?}",
1157+
ty::FnPtr(..)
1158+
);
1159+
check_kinds!(
1160+
target_type,
1161+
"CastKind::{kind:?} output must be a fn pointer, not {:?}",
1162+
ty::FnPtr(..)
1163+
);
1164+
}
1165+
CastKind::PointerCoercion(PointerCoercion::ClosureFnPointer(..)) => {
1166+
// FIXME: check safety, captures, and signature compatibility.
1167+
check_kinds!(
1168+
op_ty,
1169+
"CastKind::{kind:?} input must be a closure, not {:?}",
1170+
ty::Closure(..)
1171+
);
1172+
check_kinds!(
1173+
target_type,
1174+
"CastKind::{kind:?} output must be a fn pointer, not {:?}",
1175+
ty::FnPtr(..)
1176+
);
1177+
}
1178+
CastKind::PointerCoercion(PointerCoercion::MutToConstPointer) => {
1179+
// FIXME: check same pointee?
1180+
check_kinds!(
1181+
op_ty,
1182+
"CastKind::{kind:?} input must be a raw mut pointer, not {:?}",
1183+
ty::RawPtr(_, Mutability::Mut)
1184+
);
1185+
check_kinds!(
1186+
target_type,
1187+
"CastKind::{kind:?} output must be a raw const pointer, not {:?}",
1188+
ty::RawPtr(_, Mutability::Not)
1189+
);
1190+
}
1191+
CastKind::PointerCoercion(PointerCoercion::ArrayToPointer) => {
1192+
// FIXME: Check pointee types
1193+
check_kinds!(
1194+
op_ty,
1195+
"CastKind::{kind:?} input must be a raw pointer, not {:?}",
1196+
ty::RawPtr(..)
1197+
);
1198+
check_kinds!(
1199+
target_type,
1200+
"CastKind::{kind:?} output must be a raw pointer, not {:?}",
1201+
ty::RawPtr(..)
1202+
);
1203+
}
1204+
CastKind::PointerCoercion(PointerCoercion::Unsize) => {
1205+
// This is used for all `CoerceUnsized` types,
1206+
// not just pointers/references, so is hard to check.
1207+
}
11401208
CastKind::IntToInt | CastKind::IntToFloat => {
11411209
let input_valid = op_ty.is_integral() || op_ty.is_char() || op_ty.is_bool();
11421210
let target_valid = target_type.is_numeric() || target_type.is_char();
@@ -1147,10 +1215,29 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
11471215
);
11481216
}
11491217
}
1150-
CastKind::FnPtrToPtr | CastKind::PtrToPtr => {
1151-
if !(op_ty.is_any_ptr() && target_type.is_unsafe_ptr()) {
1152-
self.fail(location, "Can't cast {op_ty} into 'Ptr'");
1153-
}
1218+
CastKind::FnPtrToPtr => {
1219+
check_kinds!(
1220+
op_ty,
1221+
"CastKind::{kind:?} input must be a fn pointer, not {:?}",
1222+
ty::FnPtr(..)
1223+
);
1224+
check_kinds!(
1225+
target_type,
1226+
"CastKind::{kind:?} output must be a raw pointer, not {:?}",
1227+
ty::RawPtr(..)
1228+
);
1229+
}
1230+
CastKind::PtrToPtr => {
1231+
check_kinds!(
1232+
op_ty,
1233+
"CastKind::{kind:?} input must be a raw pointer, not {:?}",
1234+
ty::RawPtr(..)
1235+
);
1236+
check_kinds!(
1237+
target_type,
1238+
"CastKind::{kind:?} output must be a raw pointer, not {:?}",
1239+
ty::RawPtr(..)
1240+
);
11541241
}
11551242
CastKind::FloatToFloat | CastKind::FloatToInt => {
11561243
if !op_ty.is_floating_point() || !target_type.is_numeric() {

src/tools/tidy/src/allowed_run_make_makefiles.txt

-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ run-make/dump-ice-to-disk/Makefile
3737
run-make/dump-mono-stats/Makefile
3838
run-make/duplicate-output-flavors/Makefile
3939
run-make/dylib-chain/Makefile
40-
run-make/emit-named-files/Makefile
4140
run-make/emit-path-unhashed/Makefile
4241
run-make/emit-shared-files/Makefile
4342
run-make/emit-stack-sizes/Makefile
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//@ compile-flags: -Cno-prepopulate-passes
2+
3+
#![crate_type = "lib"]
4+
#![feature(repr_simd, core_intrinsics)]
5+
// make sure that codegen emits correctly-aligned loads and stores for repr(packed, simd) types
6+
// the alignment of a load should be no less than T, and no more than the size of the vector type
7+
use std::intrinsics::simd as intrinsics;
8+
9+
#[derive(Copy, Clone)]
10+
#[repr(packed, simd)]
11+
struct f32x3([f32; 3]);
12+
13+
#[derive(Copy, Clone)]
14+
#[repr(packed, simd)]
15+
struct f32x4([f32; 4]);
16+
17+
// CHECK-LABEL: load_f32x3
18+
#[no_mangle]
19+
pub fn load_f32x3(floats: &f32x3) -> f32x3 {
20+
// FIXME: Is a memcpy really the best we can do?
21+
// CHECK: @llvm.memcpy.{{.*}}ptr align 4 {{.*}}ptr align 4
22+
*floats
23+
}
24+
25+
// CHECK-LABEL: load_f32x4
26+
#[no_mangle]
27+
pub fn load_f32x4(floats: &f32x4) -> f32x4 {
28+
// CHECK: load <4 x float>, ptr %{{[a-z0-9_]*}}, align {{4|8|16}}
29+
*floats
30+
}
31+
32+
// CHECK-LABEL: add_f32x3
33+
#[no_mangle]
34+
pub fn add_f32x3(x: f32x3, y: f32x3) -> f32x3 {
35+
// CHECK: load <3 x float>, ptr %{{[a-z0-9_]*}}, align 4
36+
unsafe { intrinsics::simd_add(x, y) }
37+
}
38+
39+
// CHECK-LABEL: add_f32x4
40+
#[no_mangle]
41+
pub fn add_f32x4(x: f32x4, y: f32x4) -> f32x4 {
42+
// CHECK: load <4 x float>, ptr %{{[a-z0-9_]*}}, align {{4|8|16}}
43+
unsafe { intrinsics::simd_add(x, y) }
44+
}

tests/run-make/emit-named-files/Makefile

-33
This file was deleted.
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
use std::fs::create_dir;
2+
use std::path::Path;
3+
4+
use run_make_support::{rustc, tmp_dir};
5+
6+
fn emit_and_check(out_dir: &Path, out_file: &str, format: &str) {
7+
let out_file = out_dir.join(out_file);
8+
rustc().input("foo.rs").emit(&format!("{format}={}", out_file.display())).run();
9+
assert!(out_file.is_file());
10+
}
11+
12+
fn main() {
13+
let out_dir = tmp_dir().join("emit");
14+
15+
create_dir(&out_dir).unwrap();
16+
17+
emit_and_check(&out_dir, "libfoo.s", "asm");
18+
emit_and_check(&out_dir, "libfoo.bc", "llvm-bc");
19+
emit_and_check(&out_dir, "libfoo.ll", "llvm-ir");
20+
emit_and_check(&out_dir, "libfoo.o", "obj");
21+
emit_and_check(&out_dir, "libfoo.rmeta", "metadata");
22+
emit_and_check(&out_dir, "libfoo.rlib", "link");
23+
emit_and_check(&out_dir, "libfoo.d", "dep-info");
24+
emit_and_check(&out_dir, "libfoo.mir", "mir");
25+
}

tests/ui/simd/repr_packed.rs

+6-15
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@
66
#[repr(simd, packed)]
77
struct Simd<T, const N: usize>([T; N]);
88

9-
#[repr(simd)]
10-
struct FullSimd<T, const N: usize>([T; N]);
11-
129
fn check_size_align<T, const N: usize>() {
1310
use std::mem;
1411
assert_eq!(mem::size_of::<Simd<T, N>>(), mem::size_of::<[T; N]>());
@@ -39,21 +36,15 @@ fn main() {
3936
check_ty::<f64>();
4037

4138
unsafe {
42-
// powers-of-two have no padding and work as usual
39+
// powers-of-two have no padding and have the same layout as #[repr(simd)]
4340
let x: Simd<f64, 4> =
4441
simd_add(Simd::<f64, 4>([0., 1., 2., 3.]), Simd::<f64, 4>([2., 2., 2., 2.]));
4542
assert_eq!(std::mem::transmute::<_, [f64; 4]>(x), [2., 3., 4., 5.]);
4643

47-
// non-powers-of-two have padding and need to be expanded to full vectors
48-
fn load<T, const N: usize>(v: Simd<T, N>) -> FullSimd<T, N> {
49-
unsafe {
50-
let mut tmp = core::mem::MaybeUninit::<FullSimd<T, N>>::uninit();
51-
std::ptr::copy_nonoverlapping(&v as *const _, tmp.as_mut_ptr().cast(), 1);
52-
tmp.assume_init()
53-
}
54-
}
55-
let x: FullSimd<f64, 3> =
56-
simd_add(load(Simd::<f64, 3>([0., 1., 2.])), load(Simd::<f64, 3>([2., 2., 2.])));
57-
assert_eq!(x.0, [2., 3., 4.]);
44+
// non-powers-of-two should have padding (which is removed by #[repr(packed)]),
45+
// but the intrinsic handles it
46+
let x: Simd<f64, 3> = simd_add(Simd::<f64, 3>([0., 1., 2.]), Simd::<f64, 3>([2., 2., 2.]));
47+
let arr: [f64; 3] = x.0;
48+
assert_eq!(arr, [2., 3., 4.]);
5849
}
5950
}

0 commit comments

Comments
 (0)