Skip to content

Commit 6f9cfd6

Browse files
committed
Rework OperandRef::extract_field to stop calling to_immediate_scalar on things which are already immediates
That means it stops trying to truncate things that are already `i1`s.
1 parent 642a705 commit 6f9cfd6

File tree

9 files changed

+164
-87
lines changed

9 files changed

+164
-87
lines changed

compiler/rustc_codegen_gcc/src/builder.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -989,10 +989,14 @@ impl<'a, 'gcc, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'gcc, 'tcx> {
989989
OperandValue::Ref(place.val)
990990
} else if place.layout.is_gcc_immediate() {
991991
let load = self.load(place.layout.gcc_type(self), place.val.llval, place.val.align);
992-
if let abi::BackendRepr::Scalar(ref scalar) = place.layout.backend_repr {
993-
scalar_load_metadata(self, load, scalar);
994-
}
995-
OperandValue::Immediate(self.to_immediate(load, place.layout))
992+
OperandValue::Immediate(
993+
if let abi::BackendRepr::Scalar(ref scalar) = place.layout.backend_repr {
994+
scalar_load_metadata(self, load, scalar);
995+
self.to_immediate_scalar(load, *scalar)
996+
} else {
997+
load
998+
},
999+
)
9961000
} else if let abi::BackendRepr::ScalarPair(ref a, ref b) = place.layout.backend_repr {
9971001
let b_offset = a.size(self).align_to(b.align(self).abi);
9981002

compiler/rustc_codegen_gcc/src/intrinsic/mod.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use gccjit::FunctionType;
99
use gccjit::{ComparisonOp, Function, RValue, ToRValue, Type, UnaryOp};
1010
#[cfg(feature = "master")]
1111
use rustc_abi::ExternAbi;
12-
use rustc_abi::HasDataLayout;
12+
use rustc_abi::{BackendRepr, HasDataLayout};
1313
use rustc_codegen_ssa::MemFlags;
1414
use rustc_codegen_ssa::base::wants_msvc_seh;
1515
use rustc_codegen_ssa::common::IntPredicate;
@@ -181,14 +181,19 @@ impl<'a, 'gcc, 'tcx> IntrinsicCallBuilderMethods<'tcx> for Builder<'a, 'gcc, 'tc
181181
sym::volatile_load | sym::unaligned_volatile_load => {
182182
let tp_ty = fn_args.type_at(0);
183183
let ptr = args[0].immediate();
184+
let layout = self.layout_of(tp_ty);
184185
let load = if let PassMode::Cast { cast: ref ty, pad_i32: _ } = fn_abi.ret.mode {
185186
let gcc_ty = ty.gcc_type(self);
186187
self.volatile_load(gcc_ty, ptr)
187188
} else {
188-
self.volatile_load(self.layout_of(tp_ty).gcc_type(self), ptr)
189+
self.volatile_load(layout.gcc_type(self), ptr)
189190
};
190191
// TODO(antoyo): set alignment.
191-
self.to_immediate(load, self.layout_of(tp_ty))
192+
if let BackendRepr::Scalar(scalar) = layout.backend_repr {
193+
self.to_immediate_scalar(load, scalar)
194+
} else {
195+
load
196+
}
192197
}
193198
sym::volatile_store => {
194199
let dst = args[0].deref(self.cx());

compiler/rustc_codegen_llvm/src/builder.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -746,10 +746,12 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
746746
let load = self.load(llty, place.val.llval, place.val.align);
747747
if let abi::BackendRepr::Scalar(scalar) = place.layout.backend_repr {
748748
scalar_load_metadata(self, load, scalar, place.layout, Size::ZERO);
749+
self.to_immediate_scalar(load, scalar)
750+
} else {
751+
load
749752
}
750-
load
751753
});
752-
OperandValue::Immediate(self.to_immediate(llval, place.layout))
754+
OperandValue::Immediate(llval)
753755
} else if let abi::BackendRepr::ScalarPair(a, b) = place.layout.backend_repr {
754756
let b_offset = a.size(self).align_to(b.align(self).abi);
755757

@@ -943,6 +945,8 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
943945
}
944946

945947
fn unchecked_utrunc(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
948+
debug_assert_ne!(self.val_ty(val), dest_ty);
949+
946950
let trunc = self.trunc(val, dest_ty);
947951
if llvm_util::get_version() >= (19, 0, 0) {
948952
unsafe {
@@ -955,6 +959,8 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
955959
}
956960

957961
fn unchecked_strunc(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
962+
debug_assert_ne!(self.val_ty(val), dest_ty);
963+
958964
let trunc = self.trunc(val, dest_ty);
959965
if llvm_util::get_version() >= (19, 0, 0) {
960966
unsafe {

compiler/rustc_codegen_ssa/src/mir/block.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1040,7 +1040,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
10401040
let (idx, _) = op.layout.non_1zst_field(bx).expect(
10411041
"not exactly one non-1-ZST field in a `DispatchFromDyn` type",
10421042
);
1043-
op = op.extract_field(bx, idx);
1043+
op = op.extract_field(self, bx, idx);
10441044
}
10451045

10461046
// Now that we have `*dyn Trait` or `&dyn Trait`, split it up into its
@@ -1072,7 +1072,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
10721072
let (idx, _) = op.layout.non_1zst_field(bx).expect(
10731073
"not exactly one non-1-ZST field in a `DispatchFromDyn` type",
10741074
);
1075-
op = op.extract_field(bx, idx);
1075+
op = op.extract_field(self, bx, idx);
10761076
}
10771077

10781078
// Make sure that we've actually unwrapped the rcvr down
@@ -1572,9 +1572,9 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
15721572
if scalar.is_bool() {
15731573
bx.range_metadata(llval, WrappingRange { start: 0, end: 1 });
15741574
}
1575+
// We store bools as `i8` so we need to truncate to `i1`.
1576+
llval = bx.to_immediate_scalar(llval, scalar);
15751577
}
1576-
// We store bools as `i8` so we need to truncate to `i1`.
1577-
llval = bx.to_immediate(llval, arg.layout);
15781578
}
15791579
}
15801580

@@ -1604,7 +1604,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
16041604
} else {
16051605
// If the tuple is immediate, the elements are as well.
16061606
for i in 0..tuple.layout.fields.count() {
1607-
let op = tuple.extract_field(bx, i);
1607+
let op = tuple.extract_field(self, bx, i);
16081608
self.codegen_argument(bx, op, llargs, &args[i]);
16091609
}
16101610
}

compiler/rustc_codegen_ssa/src/mir/operand.rs

+67-64
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1-
use std::assert_matches::assert_matches;
21
use std::fmt;
32

43
use arrayvec::ArrayVec;
54
use either::Either;
65
use rustc_abi as abi;
76
use rustc_abi::{Align, BackendRepr, Size};
8-
use rustc_middle::bug;
97
use rustc_middle::mir::interpret::{Pointer, Scalar, alloc_range};
108
use rustc_middle::mir::{self, ConstValue};
119
use rustc_middle::ty::Ty;
1210
use rustc_middle::ty::layout::{LayoutOf, TyAndLayout};
11+
use rustc_middle::{bug, span_bug};
1312
use tracing::debug;
1413

1514
use super::place::{PlaceRef, PlaceValue};
@@ -352,79 +351,83 @@ impl<'a, 'tcx, V: CodegenObject> OperandRef<'tcx, V> {
352351

353352
pub(crate) fn extract_field<Bx: BuilderMethods<'a, 'tcx, Value = V>>(
354353
&self,
354+
fx: &mut FunctionCx<'a, 'tcx, Bx>,
355355
bx: &mut Bx,
356356
i: usize,
357357
) -> Self {
358358
let field = self.layout.field(bx.cx(), i);
359359
let offset = self.layout.fields.offset(i);
360360

361-
let mut val = match (self.val, self.layout.backend_repr) {
362-
// If the field is ZST, it has no data.
363-
_ if field.is_zst() => OperandValue::ZeroSized,
364-
365-
// Newtype of a scalar, scalar pair or vector.
366-
(OperandValue::Immediate(_) | OperandValue::Pair(..), _)
367-
if field.size == self.layout.size =>
368-
{
369-
assert_eq!(offset.bytes(), 0);
370-
self.val
361+
let val = if field.is_zst() {
362+
OperandValue::ZeroSized
363+
} else if field.size == self.layout.size {
364+
assert_eq!(offset.bytes(), 0);
365+
if let Some(field_val) = fx.codegen_transmute_operand(bx, *self, field) {
366+
field_val
367+
} else {
368+
// we have to go through memory for things like
369+
// Newtype vector of array, e.g. #[repr(simd)] struct S([i32; 4]);
370+
let place = PlaceRef::alloca(bx, field);
371+
self.val.store(bx, place.val.with_type(self.layout));
372+
bx.load_operand(place).val
371373
}
372-
373-
// Extract a scalar component from a pair.
374-
(OperandValue::Pair(a_llval, b_llval), BackendRepr::ScalarPair(a, b)) => {
375-
if offset.bytes() == 0 {
376-
assert_eq!(field.size, a.size(bx.cx()));
377-
OperandValue::Immediate(a_llval)
378-
} else {
379-
assert_eq!(offset, a.size(bx.cx()).align_to(b.align(bx.cx()).abi));
380-
assert_eq!(field.size, b.size(bx.cx()));
381-
OperandValue::Immediate(b_llval)
374+
} else {
375+
let (in_scalar, imm) = match (self.val, self.layout.backend_repr) {
376+
// Extract a scalar component from a pair.
377+
(OperandValue::Pair(a_llval, b_llval), BackendRepr::ScalarPair(a, b)) => {
378+
if offset.bytes() == 0 {
379+
assert_eq!(field.size, a.size(bx.cx()));
380+
(Some(a), a_llval)
381+
} else {
382+
assert_eq!(offset, a.size(bx.cx()).align_to(b.align(bx.cx()).abi));
383+
assert_eq!(field.size, b.size(bx.cx()));
384+
(Some(b), b_llval)
385+
}
382386
}
383-
}
384387

385-
// `#[repr(simd)]` types are also immediate.
386-
(OperandValue::Immediate(llval), BackendRepr::Vector { .. }) => {
387-
OperandValue::Immediate(bx.extract_element(llval, bx.cx().const_usize(i as u64)))
388-
}
388+
// `#[repr(simd)]` types are also immediate.
389+
(OperandValue::Immediate(llval), BackendRepr::Vector { .. }) => {
390+
(None, bx.extract_element(llval, bx.cx().const_usize(i as u64)))
391+
}
389392

390-
_ => bug!("OperandRef::extract_field({:?}): not applicable", self),
393+
_ => {
394+
span_bug!(fx.mir.span, "OperandRef::extract_field({:?}): not applicable", self)
395+
}
396+
};
397+
OperandValue::Immediate(match field.backend_repr {
398+
BackendRepr::Vector { .. } => imm,
399+
BackendRepr::Scalar(out_scalar) => {
400+
let Some(in_scalar) = in_scalar else {
401+
span_bug!(
402+
fx.mir.span,
403+
"OperandRef::extract_field({:?}): missing input scalar for output scalar",
404+
self
405+
)
406+
};
407+
if in_scalar != out_scalar {
408+
// If the backend and backend_immediate types might differ,
409+
// flip back to the backend type then to the new immediate.
410+
// This avoids nop truncations, but still handles things like
411+
// Bools in union fields needs to be truncated.
412+
let backend = bx.from_immediate(imm);
413+
bx.to_immediate_scalar(backend, out_scalar)
414+
} else {
415+
imm
416+
}
417+
}
418+
BackendRepr::Memory { sized: true } => {
419+
span_bug!(
420+
fx.mir.span,
421+
"Projecting into a simd type with padding doesn't work; \
422+
See <https://github.com/rust-lang/rust/issues/137108>",
423+
);
424+
}
425+
BackendRepr::Uninhabited
426+
| BackendRepr::ScalarPair(_, _)
427+
| BackendRepr::Memory { sized: false } => bug!(),
428+
})
391429
};
392430

393-
match (&mut val, field.backend_repr) {
394-
(OperandValue::ZeroSized, _) => {}
395-
(
396-
OperandValue::Immediate(llval),
397-
BackendRepr::Scalar(_) | BackendRepr::ScalarPair(..) | BackendRepr::Vector { .. },
398-
) => {
399-
// Bools in union fields needs to be truncated.
400-
*llval = bx.to_immediate(*llval, field);
401-
}
402-
(OperandValue::Pair(a, b), BackendRepr::ScalarPair(a_abi, b_abi)) => {
403-
// Bools in union fields needs to be truncated.
404-
*a = bx.to_immediate_scalar(*a, a_abi);
405-
*b = bx.to_immediate_scalar(*b, b_abi);
406-
}
407-
// Newtype vector of array, e.g. #[repr(simd)] struct S([i32; 4]);
408-
(OperandValue::Immediate(llval), BackendRepr::Memory { sized: true }) => {
409-
assert_matches!(self.layout.backend_repr, BackendRepr::Vector { .. });
410-
411-
let llfield_ty = bx.cx().backend_type(field);
412-
413-
// Can't bitcast an aggregate, so round trip through memory.
414-
let llptr = bx.alloca(field.size, field.align.abi);
415-
bx.store(*llval, llptr, field.align.abi);
416-
*llval = bx.load(llfield_ty, llptr, field.align.abi);
417-
}
418-
(
419-
OperandValue::Immediate(_),
420-
BackendRepr::Uninhabited | BackendRepr::Memory { sized: false },
421-
) => {
422-
bug!()
423-
}
424-
(OperandValue::Pair(..), _) => bug!(),
425-
(OperandValue::Ref(..), _) => bug!(),
426-
}
427-
428431
OperandRef { val, layout: field }
429432
}
430433
}
@@ -587,7 +590,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
587590
"Bad PlaceRef: destructing pointers should use cast/PtrMetadata, \
588591
but tried to access field {f:?} of pointer {o:?}",
589592
);
590-
o = o.extract_field(bx, f.index());
593+
o = o.extract_field(self, bx, f.index());
591594
}
592595
mir::ProjectionElem::Index(_)
593596
| mir::ProjectionElem::ConstantIndex { .. } => {

compiler/rustc_codegen_ssa/src/mir/rvalue.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
231231
///
232232
/// Returns `None` for cases that can't work in that framework, such as for
233233
/// `Immediate`->`Ref` that needs an `alloc` to get the location.
234-
fn codegen_transmute_operand(
234+
pub(crate) fn codegen_transmute_operand(
235235
&mut self,
236236
bx: &mut Bx,
237237
operand: OperandRef<'tcx, Bx::Value>,
@@ -260,6 +260,8 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
260260
OperandValue::Ref(source_place_val) => {
261261
assert_eq!(source_place_val.llextra, None);
262262
assert_matches!(operand_kind, OperandValueKind::Ref);
263+
// The existing alignment is part of `source_place_val`,
264+
// so that alignment will be used, not `cast`'s.
263265
Some(bx.load_operand(source_place_val.with_type(cast)).val)
264266
}
265267
OperandValue::ZeroSized => {

compiler/rustc_codegen_ssa/src/traits/builder.rs

+1-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::assert_matches::assert_matches;
22
use std::ops::Deref;
33

4-
use rustc_abi::{Align, BackendRepr, Scalar, Size, WrappingRange};
4+
use rustc_abi::{Align, Scalar, Size, WrappingRange};
55
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrs;
66
use rustc_middle::ty::layout::{FnAbiOf, LayoutOf, TyAndLayout};
77
use rustc_middle::ty::{Instance, Ty};
@@ -223,13 +223,6 @@ pub trait BuilderMethods<'a, 'tcx>:
223223
) -> (Self::Value, Self::Value);
224224

225225
fn from_immediate(&mut self, val: Self::Value) -> Self::Value;
226-
fn to_immediate(&mut self, val: Self::Value, layout: TyAndLayout<'_>) -> Self::Value {
227-
if let BackendRepr::Scalar(scalar) = layout.backend_repr {
228-
self.to_immediate_scalar(val, scalar)
229-
} else {
230-
val
231-
}
232-
}
233226
fn to_immediate_scalar(&mut self, val: Self::Value, scalar: Scalar) -> Self::Value;
234227

235228
fn alloca(&mut self, size: Size, align: Align) -> Self::Value;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//@compile-flags: -Copt-level=3
2+
3+
#![crate_type = "lib"]
4+
#![feature(repr_simd, core_intrinsics)]
5+
6+
#[allow(non_camel_case_types)]
7+
#[derive(Clone, Copy)]
8+
#[repr(simd)]
9+
struct i32x4([i32; 4]);
10+
11+
#[inline(always)]
12+
fn to_array4(a: i32x4) -> [i32; 4] {
13+
a.0
14+
}
15+
16+
// CHECK-LABEL: simd_add_self_then_return_array(
17+
// CHECK-SAME: ptr{{.+}}sret{{.+}}%[[RET:.+]],
18+
// CHECK-SAME: ptr{{.+}}%a)
19+
#[no_mangle]
20+
pub fn simd_add_self_then_return_array(a: &i32x4) -> [i32; 4] {
21+
// It would be nice to just ban `.0` into simd types,
22+
// but until we do this has to keep working.
23+
// See also <https://github.com/rust-lang/rust/issues/105439>
24+
25+
// CHECK: %[[T1:.+]] = load <4 x i32>, ptr %a
26+
// CHECK: %[[T2:.+]] = shl <4 x i32> %[[T1]], {{splat \(i32 1\)|<i32 1, i32 1, i32 1, i32 1>}}
27+
// CHECK: store <4 x i32> %[[T2]], ptr %[[RET]]
28+
let a = *a;
29+
let b = unsafe { core::intrinsics::simd::simd_add(a, a) };
30+
to_array4(b)
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//@ known-bug: #137108
2+
//@compile-flags: -Copt-level=3
3+
4+
// If you fix this, put it in the corresponding codegen test,
5+
// not in a UI test like the readme says.
6+
7+
#![crate_type = "lib"]
8+
9+
#![feature(repr_simd, core_intrinsics)]
10+
11+
#[allow(non_camel_case_types)]
12+
#[derive(Clone, Copy)]
13+
#[repr(simd)]
14+
struct i32x3([i32; 3]);
15+
16+
const _: () = { assert!(size_of::<i32x3>() == 16) };
17+
18+
#[inline(always)]
19+
fn to_array3(a: i32x3) -> [i32; 3] {
20+
a.0
21+
}
22+
23+
// CHECK-LABEL: simd_add_self_then_return_array_packed(
24+
// CHECK-SAME: ptr{{.+}}sret{{.+}}%[[RET:.+]],
25+
// CHECK-SAME: ptr{{.+}}%a)
26+
#[no_mangle]
27+
pub fn simd_add_self_then_return_array_packed(a: i32x3) -> [i32; 3] {
28+
// CHECK: %[[T1:.+]] = load <3 x i32>, ptr %a
29+
// CHECK: %[[T2:.+]] = shl <3 x i32> %[[T1]], <i32 1, i32 1, i32 1>
30+
// CHECK: store <3 x i32> %[[T2]], ptr %[[RET]]
31+
let b = unsafe { core::intrinsics::simd::simd_add(a, a) };
32+
to_array3(b)
33+
}

0 commit comments

Comments
 (0)