Skip to content

Commit d8a45ef

Browse files
committed
Refactoring after the PlaceValue addition
I added `PlaceValue` in 123775, but kept that one line-by-line simple because it touched so many places. This goes through to add more helpers & docs, and change some `PlaceRef` to `PlaceValue` where the type didn't need to be included. No behaviour changes.
1 parent fa0068b commit d8a45ef

File tree

7 files changed

+138
-115
lines changed

7 files changed

+138
-115
lines changed

compiler/rustc_codegen_ssa/src/base.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ pub fn coerce_unsized_into<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
282282
}
283283

284284
if src_f.layout.ty == dst_f.layout.ty {
285-
bx.typed_place_copy(dst_f, src_f);
285+
bx.typed_place_copy(dst_f.val, src_f.val, src_f.layout);
286286
} else {
287287
coerce_unsized_into(bx, src_f, dst_f);
288288
}

compiler/rustc_codegen_ssa/src/mir/block.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1472,8 +1472,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
14721472
// alignment requirements may be higher than the type's alignment, so copy
14731473
// to a higher-aligned alloca.
14741474
let scratch = PlaceRef::alloca_aligned(bx, arg.layout, required_align);
1475-
let op_place = PlaceRef { val: op_place_val, layout: op.layout };
1476-
bx.typed_place_copy(scratch, op_place);
1475+
bx.typed_place_copy(scratch.val, op_place_val, op.layout);
14771476
(scratch.val.llval, scratch.val.align, true)
14781477
} else {
14791478
(op_place_val.llval, op_place_val.align, true)
@@ -1563,7 +1562,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
15631562
if place_val.llextra.is_some() {
15641563
bug!("closure arguments must be sized");
15651564
}
1566-
let tuple_ptr = PlaceRef { val: place_val, layout: tuple.layout };
1565+
let tuple_ptr = place_val.with_type(tuple.layout);
15671566
for i in 0..tuple.layout.fields.count() {
15681567
let field_ptr = tuple_ptr.project_field(bx, i);
15691568
let field = bx.load_operand(field_ptr);

compiler/rustc_codegen_ssa/src/mir/intrinsic.rs

+7-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use super::operand::{OperandRef, OperandValue};
1+
use super::operand::OperandRef;
22
use super::place::PlaceRef;
33
use super::FunctionCx;
44
use crate::errors;
@@ -92,9 +92,10 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
9292
// into the (unoptimized) direct swapping implementation, so we disable it.
9393
|| bx.sess().target.arch == "spirv"
9494
{
95-
let x_place = PlaceRef::new_sized(args[0].immediate(), pointee_layout);
96-
let y_place = PlaceRef::new_sized(args[1].immediate(), pointee_layout);
97-
bx.typed_place_swap(x_place, y_place);
95+
let align = pointee_layout.align.abi;
96+
let x_place = args[0].val.deref(align);
97+
let y_place = args[1].val.deref(align);
98+
bx.typed_place_swap(x_place, y_place, pointee_layout);
9899
return Ok(());
99100
}
100101
}
@@ -112,15 +113,13 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
112113
sym::va_end => bx.va_end(args[0].immediate()),
113114
sym::size_of_val => {
114115
let tp_ty = fn_args.type_at(0);
115-
let meta =
116-
if let OperandValue::Pair(_, meta) = args[0].val { Some(meta) } else { None };
116+
let (_, meta) = args[0].val.pointer_parts();
117117
let (llsize, _) = size_of_val::size_and_align_of_dst(bx, tp_ty, meta);
118118
llsize
119119
}
120120
sym::min_align_of_val => {
121121
let tp_ty = fn_args.type_at(0);
122-
let meta =
123-
if let OperandValue::Pair(_, meta) = args[0].val { Some(meta) } else { None };
122+
let (_, meta) = args[0].val.pointer_parts();
124123
let (_, llalign) = size_of_val::size_and_align_of_dst(bx, tp_ty, meta);
125124
llalign
126125
}

compiler/rustc_codegen_ssa/src/mir/operand.rs

+37-10
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,32 @@ pub enum OperandValue<V> {
5757
ZeroSized,
5858
}
5959

60+
impl<V: CodegenObject> OperandValue<V> {
61+
/// Treat this value as a pointer and return the data pointer and
62+
/// optional metadata as backend values.
63+
///
64+
/// If you're making a place, use [`Self::deref`] instead.
65+
pub fn pointer_parts(self) -> (V, Option<V>) {
66+
match self {
67+
OperandValue::Immediate(llptr) => (llptr, None),
68+
OperandValue::Pair(llptr, llextra) => (llptr, Some(llextra)),
69+
_ => bug!("OperandValue cannot be a pointer: {self:?}"),
70+
}
71+
}
72+
73+
/// Treat this value as a pointer and return the place to which it points.
74+
///
75+
/// The pointer immediate doesn't inherently know its alignment,
76+
/// so you need to pass it in. If you want to get it from a type's ABI
77+
/// alignment, then maybe you want [`OperandRef::deref`] instead.
78+
///
79+
/// This is the inverse of [`PlaceValue::address`].
80+
pub fn deref(self, align: Align) -> PlaceValue<V> {
81+
let (llval, llextra) = self.pointer_parts();
82+
PlaceValue { llval, llextra, align }
83+
}
84+
}
85+
6086
/// An `OperandRef` is an "SSA" reference to a Rust value, along with
6187
/// its type.
6288
///
@@ -204,6 +230,15 @@ impl<'a, 'tcx, V: CodegenObject> OperandRef<'tcx, V> {
204230
}
205231
}
206232

233+
/// Asserts that this operand is a pointer (or reference) and returns
234+
/// the place to which it points. (This requires no code to be emitted
235+
/// as we represent places using the pointer to the place.)
236+
///
237+
/// This uses [`Ty::deref`] to include the type of the place and assumes
238+
/// the place is aligned to the pointee's usual ABI alignment.
239+
///
240+
/// If you don't need the type, see [`PlaceValue::pointer_parts`]
241+
/// or [`PlaceValue::deref`].
207242
pub fn deref<Cx: LayoutTypeMethods<'tcx>>(self, cx: &Cx) -> PlaceRef<'tcx, V> {
208243
if self.layout.ty.is_box() {
209244
// Derefer should have removed all Box derefs
@@ -217,15 +252,8 @@ impl<'a, 'tcx, V: CodegenObject> OperandRef<'tcx, V> {
217252
.unwrap_or_else(|| bug!("deref of non-pointer {:?}", self))
218253
.ty;
219254

220-
let (llptr, llextra) = match self.val {
221-
OperandValue::Immediate(llptr) => (llptr, None),
222-
OperandValue::Pair(llptr, llextra) => (llptr, Some(llextra)),
223-
OperandValue::Ref(..) => bug!("Deref of by-Ref operand {:?}", self),
224-
OperandValue::ZeroSized => bug!("Deref of ZST operand {:?}", self),
225-
};
226255
let layout = cx.layout_of(projected_ty);
227-
let val = PlaceValue { llval: llptr, llextra, align: layout.align.abi };
228-
PlaceRef { val, layout }
256+
self.val.deref(layout.align.abi).with_type(layout)
229257
}
230258

231259
/// If this operand is a `Pair`, we return an aggregate with the two values.
@@ -418,8 +446,7 @@ impl<'a, 'tcx, V: CodegenObject> OperandValue<V> {
418446
if val.llextra.is_some() {
419447
bug!("cannot directly store unsized values");
420448
}
421-
let source_place = PlaceRef { val, layout: dest.layout };
422-
bx.typed_place_copy_with_flags(dest, source_place, flags);
449+
bx.typed_place_copy_with_flags(dest.val, val, dest.layout, flags);
423450
}
424451
OperandValue::Immediate(s) => {
425452
let val = bx.from_immediate(s);

compiler/rustc_codegen_ssa/src/mir/place.rs

+41-30
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ use rustc_target::abi::{VariantIdx, Variants};
1515
/// The location and extra runtime properties of the place.
1616
///
1717
/// Typically found in a [`PlaceRef`] or an [`OperandValue::Ref`].
18+
///
19+
/// As a location in memory, this has no specific type. If you want to
20+
/// load or store it using a typed operation, use [`Self::with_type`].
1821
#[derive(Copy, Clone, Debug)]
1922
pub struct PlaceValue<V> {
2023
/// A pointer to the contents of the place.
@@ -34,6 +37,28 @@ impl<V: CodegenObject> PlaceValue<V> {
3437
pub fn new_sized(llval: V, align: Align) -> PlaceValue<V> {
3538
PlaceValue { llval, llextra: None, align }
3639
}
40+
41+
/// Creates a `PlaceRef` to this location with the given type.
42+
pub fn with_type<'tcx>(self, layout: TyAndLayout<'tcx>) -> PlaceRef<'tcx, V> {
43+
debug_assert!(
44+
layout.is_unsized() || layout.abi.is_uninhabited() || self.llextra.is_none(),
45+
"Had pointer metadata {:?} for sized type {layout:?}",
46+
self.llextra,
47+
);
48+
PlaceRef { val: self, layout }
49+
}
50+
51+
/// Gets the pointer to this place as an [`OperandValue::Immediate`]
52+
/// or, for those needing metadata, an [`OperandValue::Pair`].
53+
///
54+
/// This is the inverse of [`OperandValue::deref`].
55+
pub fn address(self) -> OperandValue<V> {
56+
if let Some(llextra) = self.llextra {
57+
OperandValue::Pair(self.llval, llextra)
58+
} else {
59+
OperandValue::Immediate(self.llval)
60+
}
61+
}
3762
}
3863

3964
#[derive(Copy, Clone, Debug)]
@@ -51,9 +76,7 @@ pub struct PlaceRef<'tcx, V> {
5176

5277
impl<'a, 'tcx, V: CodegenObject> PlaceRef<'tcx, V> {
5378
pub fn new_sized(llval: V, layout: TyAndLayout<'tcx>) -> PlaceRef<'tcx, V> {
54-
assert!(layout.is_sized());
55-
let val = PlaceValue::new_sized(llval, layout.align.abi);
56-
PlaceRef { val, layout }
79+
PlaceRef::new_sized_aligned(llval, layout, layout.align.abi)
5780
}
5881

5982
pub fn new_sized_aligned(
@@ -62,8 +85,7 @@ impl<'a, 'tcx, V: CodegenObject> PlaceRef<'tcx, V> {
6285
align: Align,
6386
) -> PlaceRef<'tcx, V> {
6487
assert!(layout.is_sized());
65-
let val = PlaceValue::new_sized(llval, align);
66-
PlaceRef { val, layout }
88+
PlaceValue::new_sized(llval, align).with_type(layout)
6789
}
6890

6991
// FIXME(eddyb) pass something else for the name so no work is done
@@ -131,18 +153,12 @@ impl<'a, 'tcx, V: CodegenObject> PlaceRef<'tcx, V> {
131153
} else {
132154
bx.inbounds_ptradd(self.val.llval, bx.const_usize(offset.bytes()))
133155
};
134-
PlaceRef {
135-
val: PlaceValue {
136-
llval,
137-
llextra: if bx.cx().type_has_metadata(field.ty) {
138-
self.val.llextra
139-
} else {
140-
None
141-
},
142-
align: effective_field_align,
143-
},
144-
layout: field,
145-
}
156+
let val = PlaceValue {
157+
llval,
158+
llextra: if bx.cx().type_has_metadata(field.ty) { self.val.llextra } else { None },
159+
align: effective_field_align,
160+
};
161+
val.with_type(field)
146162
};
147163

148164
// Simple cases, which don't need DST adjustment:
@@ -197,7 +213,7 @@ impl<'a, 'tcx, V: CodegenObject> PlaceRef<'tcx, V> {
197213
let ptr = bx.inbounds_ptradd(self.val.llval, offset);
198214
let val =
199215
PlaceValue { llval: ptr, llextra: self.val.llextra, align: effective_field_align };
200-
PlaceRef { val, layout: field }
216+
val.with_type(field)
201217
}
202218

203219
/// Obtain the actual discriminant of a value.
@@ -386,18 +402,13 @@ impl<'a, 'tcx, V: CodegenObject> PlaceRef<'tcx, V> {
386402
layout.size
387403
};
388404

389-
PlaceRef {
390-
val: PlaceValue {
391-
llval: bx.inbounds_gep(
392-
bx.cx().backend_type(self.layout),
393-
self.val.llval,
394-
&[bx.cx().const_usize(0), llindex],
395-
),
396-
llextra: None,
397-
align: self.val.align.restrict_for_offset(offset),
398-
},
399-
layout,
400-
}
405+
let llval = bx.inbounds_gep(
406+
bx.cx().backend_type(self.layout),
407+
self.val.llval,
408+
&[bx.cx().const_usize(0), llindex],
409+
);
410+
let align = self.val.align.restrict_for_offset(offset);
411+
PlaceValue::new_sized(llval, align).with_type(layout)
401412
}
402413

403414
pub fn project_downcast<Bx: BuilderMethods<'a, 'tcx, Value = V>>(

compiler/rustc_codegen_ssa/src/mir/rvalue.rs

+14-38
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
7272
if val.llextra.is_some() {
7373
bug!("unsized coercion on an unsized rvalue");
7474
}
75-
let source = PlaceRef { val, layout: operand.layout };
76-
base::coerce_unsized_into(bx, source, dest);
75+
base::coerce_unsized_into(bx, val.with_type(operand.layout), dest);
7776
}
7877
OperandValue::ZeroSized => {
7978
bug!("unsized coercion on a ZST rvalue");
@@ -182,10 +181,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
182181
OperandValue::Immediate(..) | OperandValue::Pair(..) => {
183182
// When we have immediate(s), the alignment of the source is irrelevant,
184183
// so we can store them using the destination's alignment.
185-
src.val.store(
186-
bx,
187-
PlaceRef::new_sized_aligned(dst.val.llval, src.layout, dst.val.align),
188-
);
184+
src.val.store(bx, dst.val.with_type(src.layout));
189185
}
190186
}
191187
}
@@ -223,8 +219,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
223219
OperandValue::Ref(source_place_val) => {
224220
debug_assert_eq!(source_place_val.llextra, None);
225221
debug_assert!(matches!(operand_kind, OperandValueKind::Ref));
226-
let fake_place = PlaceRef { val: source_place_val, layout: cast };
227-
Some(bx.load_operand(fake_place).val)
222+
Some(bx.load_operand(source_place_val.with_type(cast)).val)
228223
}
229224
OperandValue::ZeroSized => {
230225
let OperandValueKind::ZeroSized = operand_kind else {
@@ -452,23 +447,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
452447
}
453448
mir::CastKind::PointerCoercion(PointerCoercion::Unsize) => {
454449
assert!(bx.cx().is_backend_scalar_pair(cast));
455-
let (lldata, llextra) = match operand.val {
456-
OperandValue::Pair(lldata, llextra) => {
457-
// unsize from a fat pointer -- this is a
458-
// "trait-object-to-supertrait" coercion.
459-
(lldata, Some(llextra))
460-
}
461-
OperandValue::Immediate(lldata) => {
462-
// "standard" unsize
463-
(lldata, None)
464-
}
465-
OperandValue::Ref(..) => {
466-
bug!("by-ref operand {:?} in `codegen_rvalue_operand`", operand);
467-
}
468-
OperandValue::ZeroSized => {
469-
bug!("zero-sized operand {:?} in `codegen_rvalue_operand`", operand);
470-
}
471-
};
450+
let (lldata, llextra) = operand.val.pointer_parts();
472451
let (lldata, llextra) =
473452
base::unsize_ptr(bx, lldata, operand.layout.ty, cast.ty, llextra);
474453
OperandValue::Pair(lldata, llextra)
@@ -489,12 +468,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
489468
}
490469
}
491470
mir::CastKind::DynStar => {
492-
let (lldata, llextra) = match operand.val {
493-
OperandValue::Ref(..) => todo!(),
494-
OperandValue::Immediate(v) => (v, None),
495-
OperandValue::Pair(v, l) => (v, Some(l)),
496-
OperandValue::ZeroSized => bug!("ZST -- which is not PointerLike -- in DynStar"),
497-
};
471+
let (lldata, llextra) = operand.val.pointer_parts();
498472
let (lldata, llextra) =
499473
base::cast_to_dyn_star(bx, lldata, operand.layout, cast.ty, llextra);
500474
OperandValue::Pair(lldata, llextra)
@@ -762,16 +736,18 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
762736
mk_ptr_ty: impl FnOnce(TyCtxt<'tcx>, Ty<'tcx>) -> Ty<'tcx>,
763737
) -> OperandRef<'tcx, Bx::Value> {
764738
let cg_place = self.codegen_place(bx, place.as_ref());
739+
let val = cg_place.val.address();
765740

766741
let ty = cg_place.layout.ty;
742+
debug_assert!(
743+
if bx.cx().type_has_metadata(ty) {
744+
matches!(val, OperandValue::Pair(..))
745+
} else {
746+
matches!(val, OperandValue::Immediate(..))
747+
},
748+
"Address of place was unexpectedly {val:?} for pointee type {ty:?}",
749+
);
767750

768-
// Note: places are indirect, so storing the `llval` into the
769-
// destination effectively creates a reference.
770-
let val = if !bx.cx().type_has_metadata(ty) {
771-
OperandValue::Immediate(cg_place.val.llval)
772-
} else {
773-
OperandValue::Pair(cg_place.val.llval, cg_place.val.llextra.unwrap())
774-
};
775751
OperandRef { val, layout: self.cx.layout_of(mk_ptr_ty(self.cx.tcx(), ty)) }
776752
}
777753

0 commit comments

Comments
 (0)