Skip to content

Commit a9ef67f

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 29a56a3 commit a9ef67f

File tree

7 files changed

+158
-131
lines changed

7 files changed

+158
-131
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

+7-8
Original file line numberDiff line numberDiff line change
@@ -1453,9 +1453,9 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
14531453
Some(pointee_align) => cmp::max(pointee_align, arg.layout.align.abi),
14541454
None => arg.layout.align.abi,
14551455
};
1456-
let scratch = PlaceRef::alloca_aligned(bx, arg.layout, required_align);
1457-
op.val.store(bx, scratch);
1458-
(scratch.val.llval, scratch.val.align, true)
1456+
let scratch = PlaceValue::alloca(bx, arg.layout.size, required_align);
1457+
op.val.store(bx, scratch.with_type(arg.layout));
1458+
(scratch.llval, scratch.align, true)
14591459
}
14601460
PassMode::Cast { .. } => {
14611461
let scratch = PlaceRef::alloca(bx, arg.layout);
@@ -1474,10 +1474,9 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
14741474
// For `foo(packed.large_field)`, and types with <4 byte alignment on x86,
14751475
// alignment requirements may be higher than the type's alignment, so copy
14761476
// to a higher-aligned alloca.
1477-
let scratch = PlaceRef::alloca_aligned(bx, arg.layout, required_align);
1478-
let op_place = PlaceRef { val: op_place_val, layout: op.layout };
1479-
bx.typed_place_copy(scratch, op_place);
1480-
(scratch.val.llval, scratch.val.align, true)
1477+
let scratch = PlaceValue::alloca(bx, arg.layout.size, required_align);
1478+
bx.typed_place_copy(scratch, op_place_val, op.layout);
1479+
(scratch.llval, scratch.align, true)
14811480
} else {
14821481
(op_place_val.llval, op_place_val.align, true)
14831482
}
@@ -1566,7 +1565,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
15661565
if place_val.llextra.is_some() {
15671566
bug!("closure arguments must be sized");
15681567
}
1569-
let tuple_ptr = PlaceRef { val: place_val, layout: tuple.layout };
1568+
let tuple_ptr = place_val.with_type(tuple.layout);
15701569
for i in 0..tuple.layout.fields.count() {
15711570
let field_ptr = tuple_ptr.project_field(bx, i);
15721571
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::builtin_deref`] to include the type of the place and
238+
/// assumes the place is aligned to the pointee's usual ABI alignment.
239+
///
240+
/// If you don't need the type, see [`OperandValue::pointer_parts`]
241+
/// or [`OperandValue::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

+56-41
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,15 @@ use rustc_middle::mir;
99
use rustc_middle::mir::tcx::PlaceTy;
1010
use rustc_middle::ty::layout::{HasTyCtxt, LayoutOf, TyAndLayout};
1111
use rustc_middle::ty::{self, Ty};
12-
use rustc_target::abi::{Align, FieldsShape, Int, Pointer, TagEncoding};
12+
use rustc_target::abi::{Align, FieldsShape, Int, Pointer, Size, TagEncoding};
1313
use rustc_target::abi::{VariantIdx, Variants};
1414

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,41 @@ 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+
/// Allocates a stack slot in the function for a value
42+
/// of the specified size and alignment.
43+
///
44+
/// The allocation itself is untyped.
45+
pub fn alloca<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx, Value = V>>(
46+
bx: &mut Bx,
47+
size: Size,
48+
align: Align,
49+
) -> PlaceValue<V> {
50+
let llval = bx.alloca(size, align);
51+
PlaceValue::new_sized(llval, align)
52+
}
53+
54+
/// Creates a `PlaceRef` to this location with the given type.
55+
pub fn with_type<'tcx>(self, layout: TyAndLayout<'tcx>) -> PlaceRef<'tcx, V> {
56+
debug_assert!(
57+
layout.is_unsized() || layout.abi.is_uninhabited() || self.llextra.is_none(),
58+
"Had pointer metadata {:?} for sized type {layout:?}",
59+
self.llextra,
60+
);
61+
PlaceRef { val: self, layout }
62+
}
63+
64+
/// Gets the pointer to this place as an [`OperandValue::Immediate`]
65+
/// or, for those needing metadata, an [`OperandValue::Pair`].
66+
///
67+
/// This is the inverse of [`OperandValue::deref`].
68+
pub fn address(self) -> OperandValue<V> {
69+
if let Some(llextra) = self.llextra {
70+
OperandValue::Pair(self.llval, llextra)
71+
} else {
72+
OperandValue::Immediate(self.llval)
73+
}
74+
}
3775
}
3876

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

5290
impl<'a, 'tcx, V: CodegenObject> PlaceRef<'tcx, V> {
5391
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 }
92+
PlaceRef::new_sized_aligned(llval, layout, layout.align.abi)
5793
}
5894

5995
pub fn new_sized_aligned(
@@ -62,27 +98,17 @@ impl<'a, 'tcx, V: CodegenObject> PlaceRef<'tcx, V> {
6298
align: Align,
6399
) -> PlaceRef<'tcx, V> {
64100
assert!(layout.is_sized());
65-
let val = PlaceValue::new_sized(llval, align);
66-
PlaceRef { val, layout }
101+
PlaceValue::new_sized(llval, align).with_type(layout)
67102
}
68103

69104
// FIXME(eddyb) pass something else for the name so no work is done
70105
// unless LLVM IR names are turned on (e.g. for `--emit=llvm-ir`).
71106
pub fn alloca<Bx: BuilderMethods<'a, 'tcx, Value = V>>(
72107
bx: &mut Bx,
73108
layout: TyAndLayout<'tcx>,
74-
) -> Self {
75-
Self::alloca_aligned(bx, layout, layout.align.abi)
76-
}
77-
78-
pub fn alloca_aligned<Bx: BuilderMethods<'a, 'tcx, Value = V>>(
79-
bx: &mut Bx,
80-
layout: TyAndLayout<'tcx>,
81-
align: Align,
82109
) -> Self {
83110
assert!(layout.is_sized(), "tried to statically allocate unsized place");
84-
let tmp = bx.alloca(layout.size, align);
85-
Self::new_sized_aligned(tmp, layout, align)
111+
PlaceValue::alloca(bx, layout.size, layout.align.abi).with_type(layout)
86112
}
87113

88114
/// Returns a place for an indirect reference to an unsized place.
@@ -131,18 +157,12 @@ impl<'a, 'tcx, V: CodegenObject> PlaceRef<'tcx, V> {
131157
} else {
132158
bx.inbounds_ptradd(self.val.llval, bx.const_usize(offset.bytes()))
133159
};
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-
}
160+
let val = PlaceValue {
161+
llval,
162+
llextra: if bx.cx().type_has_metadata(field.ty) { self.val.llextra } else { None },
163+
align: effective_field_align,
164+
};
165+
val.with_type(field)
146166
};
147167

148168
// Simple cases, which don't need DST adjustment:
@@ -197,7 +217,7 @@ impl<'a, 'tcx, V: CodegenObject> PlaceRef<'tcx, V> {
197217
let ptr = bx.inbounds_ptradd(self.val.llval, offset);
198218
let val =
199219
PlaceValue { llval: ptr, llextra: self.val.llextra, align: effective_field_align };
200-
PlaceRef { val, layout: field }
220+
val.with_type(field)
201221
}
202222

203223
/// Obtain the actual discriminant of a value.
@@ -386,18 +406,13 @@ impl<'a, 'tcx, V: CodegenObject> PlaceRef<'tcx, V> {
386406
layout.size
387407
};
388408

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-
}
409+
let llval = bx.inbounds_gep(
410+
bx.cx().backend_type(self.layout),
411+
self.val.llval,
412+
&[bx.cx().const_usize(0), llindex],
413+
);
414+
let align = self.val.align.restrict_for_offset(offset);
415+
PlaceValue::new_sized(llval, align).with_type(layout)
401416
}
402417

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

0 commit comments

Comments
 (0)