Skip to content

Commit dcab06d

Browse files
committed
Unify Rvalue::Aggregate paths in cg_ssa
1 parent ee97564 commit dcab06d

File tree

4 files changed

+31
-23
lines changed

4 files changed

+31
-23
lines changed

Diff for: compiler/rustc_abi/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1255,7 +1255,7 @@ impl<FieldIdx: Idx> FieldsShape<FieldIdx> {
12551255

12561256
/// Gets source indices of the fields by increasing offsets.
12571257
#[inline]
1258-
pub fn index_by_increasing_offset(&self) -> impl Iterator<Item = usize> + '_ {
1258+
pub fn index_by_increasing_offset(&self) -> impl ExactSizeIterator<Item = usize> + '_ {
12591259
let mut inverse_small = [0u8; 64];
12601260
let mut inverse_big = IndexVec::new();
12611261
let use_small = self.count() <= inverse_small.len();

Diff for: compiler/rustc_codegen_ssa/src/mir/operand.rs

+13
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,19 @@ impl<V: CodegenObject> OperandValue<V> {
110110
let (llval, llextra) = self.pointer_parts();
111111
PlaceValue { llval, llextra, align }
112112
}
113+
114+
pub(crate) fn is_expected_variant_for_type<'tcx, Cx: LayoutTypeMethods<'tcx>>(
115+
&self,
116+
cx: &Cx,
117+
ty: TyAndLayout<'tcx>,
118+
) -> bool {
119+
match self {
120+
OperandValue::ZeroSized => ty.is_zst(),
121+
OperandValue::Immediate(_) => cx.is_backend_immediate(ty),
122+
OperandValue::Pair(_, _) => cx.is_backend_scalar_pair(ty),
123+
OperandValue::Ref(_) => cx.is_backend_ref(ty),
124+
}
125+
}
113126
}
114127

115128
/// An `OperandRef` is an "SSA" reference to a Rust value, along with

Diff for: compiler/rustc_codegen_ssa/src/mir/rvalue.rs

+16-21
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use rustc_span::{Span, DUMMY_SP};
1818
use rustc_target::abi::{self, FieldIdx, FIRST_VARIANT};
1919

2020
use arrayvec::ArrayVec;
21+
use either::Either;
2122

2223
impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
2324
#[instrument(level = "trace", skip(self, bx))]
@@ -696,35 +697,25 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
696697
OperandRef { val: OperandValue::Immediate(static_), layout }
697698
}
698699
mir::Rvalue::Use(ref operand) => self.codegen_operand(bx, operand),
699-
mir::Rvalue::Aggregate(box mir::AggregateKind::RawPtr(..), ref fields) => {
700-
let ty = rvalue.ty(self.mir, self.cx.tcx());
701-
let layout = self.cx.layout_of(self.monomorphize(ty));
702-
let [data, meta] = &*fields.raw else {
703-
bug!("RawPtr fields: {fields:?}");
704-
};
705-
let data = self.codegen_operand(bx, data);
706-
let meta = self.codegen_operand(bx, meta);
707-
match (data.val, meta.val) {
708-
(p @ OperandValue::Immediate(_), OperandValue::ZeroSized) => {
709-
OperandRef { val: p, layout }
710-
}
711-
(OperandValue::Immediate(p), OperandValue::Immediate(m)) => {
712-
OperandRef { val: OperandValue::Pair(p, m), layout }
713-
}
714-
_ => bug!("RawPtr operands {data:?} {meta:?}"),
715-
}
716-
}
717700
mir::Rvalue::Repeat(..) => bug!("{rvalue:?} in codegen_rvalue_operand"),
718-
mir::Rvalue::Aggregate(_, ref fields) => {
701+
mir::Rvalue::Aggregate(ref kind, ref fields) => {
719702
let ty = rvalue.ty(self.mir, self.cx.tcx());
720703
let ty = self.monomorphize(ty);
721704
let layout = self.cx.layout_of(ty);
722705

706+
let field_indices = if let mir::AggregateKind::RawPtr(..) = **kind {
707+
// `index_by_increasing_offset` gives an empty iterator for primitives
708+
Either::Left([0_usize, 1_usize].iter().copied())
709+
} else {
710+
Either::Right(layout.fields.index_by_increasing_offset())
711+
};
712+
debug_assert_eq!(field_indices.len(), fields.len());
713+
723714
// `rvalue_creates_operand` has arranged that we only get here if
724715
// we can build the aggregate immediate from the field immediates.
725716
let mut inputs = ArrayVec::<Bx::Value, 2>::new();
726717
let mut input_scalars = ArrayVec::<abi::Scalar, 2>::new();
727-
for field_idx in layout.fields.index_by_increasing_offset() {
718+
for field_idx in field_indices {
728719
let field_idx = FieldIdx::from_usize(field_idx);
729720
let op = self.codegen_operand(bx, &fields[field_idx]);
730721
let values = op.val.immediates_or_place().left_or_else(|p| {
@@ -748,6 +739,10 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
748739
);
749740

750741
let val = OperandValue::from_immediates(inputs);
742+
debug_assert!(
743+
val.is_expected_variant_for_type(self.cx, layout),
744+
"Made wrong variant {val:?} for type {layout:?}",
745+
);
751746
OperandRef { val, layout }
752747
}
753748
mir::Rvalue::ShallowInitBox(ref operand, content_ty) => {
@@ -792,7 +787,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
792787
debug_assert!(
793788
if bx.cx().type_has_metadata(ty) {
794789
matches!(val, OperandValue::Pair(..))
795-
} else {
790+
} else {
796791
matches!(val, OperandValue::Immediate(..))
797792
},
798793
"Address of place was unexpectedly {val:?} for pointee type {ty:?}",

Diff for: tests/codegen/mir-aggregate-no-alloca.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ pub fn make_cell_of_bool(b: bool) -> std::cell::Cell<bool> {
5555
std::cell::Cell::new(b)
5656
}
5757

58-
// CHECK-LABLE: { i8, i16 } @make_cell_of_bool_and_short(i1 noundef zeroext %b, i16 noundef %s)
58+
// CHECK-LABEL: { i8, i16 } @make_cell_of_bool_and_short(i1 noundef zeroext %b, i16 noundef %s)
5959
#[no_mangle]
6060
pub fn make_cell_of_bool_and_short(b: bool, s: u16) -> std::cell::Cell<(bool, u16)> {
6161
// CHECK-NOT: alloca

0 commit comments

Comments
 (0)