Skip to content

Commit bfa210e

Browse files
committed
Introduce NullOp::AlignOf
1 parent 50f13a3 commit bfa210e

File tree

11 files changed

+53
-10
lines changed

11 files changed

+53
-10
lines changed

compiler/rustc_codegen_ssa/src/mir/intrinsic.rs

-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,6 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
104104
}
105105
}
106106
sym::pref_align_of
107-
| sym::min_align_of
108107
| sym::needs_drop
109108
| sym::type_id
110109
| sym::type_name

compiler/rustc_codegen_ssa/src/mir/rvalue.rs

+14
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,20 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
501501
)
502502
}
503503

504+
mir::Rvalue::NullaryOp(mir::NullOp::AlignOf, ty) => {
505+
let ty = self.monomorphize(ty);
506+
assert!(bx.cx().type_is_sized(ty));
507+
let val = bx.cx().const_usize(bx.cx().layout_of(ty).align.abi.bytes());
508+
let tcx = self.cx.tcx();
509+
(
510+
bx,
511+
OperandRef {
512+
val: OperandValue::Immediate(val),
513+
layout: self.cx.layout_of(tcx.types.usize),
514+
},
515+
)
516+
}
517+
504518
mir::Rvalue::NullaryOp(mir::NullOp::Box, content_ty) => {
505519
let content_ty = self.monomorphize(content_ty);
506520
let content_layout = bx.cx().layout_of(content_ty);

compiler/rustc_middle/src/mir/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -2252,6 +2252,8 @@ impl BinOp {
22522252
pub enum NullOp {
22532253
/// Returns the size of a value of that type
22542254
SizeOf,
2255+
/// Returns the minimum alignment of a type
2256+
AlignOf,
22552257
/// Creates a new uninitialized box for a value of that type
22562258
Box,
22572259
}

compiler/rustc_middle/src/mir/tcx.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ impl<'tcx> Rvalue<'tcx> {
196196
Rvalue::UnaryOp(UnOp::Not | UnOp::Neg, ref operand) => operand.ty(local_decls, tcx),
197197
Rvalue::Discriminant(ref place) => place.ty(local_decls, tcx).ty.discriminant_ty(tcx),
198198
Rvalue::NullaryOp(NullOp::Box, t) => tcx.mk_box(t),
199-
Rvalue::NullaryOp(NullOp::SizeOf, _) => tcx.types.usize,
199+
Rvalue::NullaryOp(NullOp::SizeOf | NullOp::AlignOf, _) => tcx.types.usize,
200200
Rvalue::Aggregate(ref ak, ref ops) => match **ak {
201201
AggregateKind::Array(ty) => tcx.mk_array(ty, ops.len() as u64),
202202
AggregateKind::Tuple => tcx.mk_tup(ops.iter().map(|op| op.ty(local_decls, tcx))),

compiler/rustc_mir/src/dataflow/move_paths/builder.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ impl<'b, 'a, 'tcx> Gatherer<'b, 'a, 'tcx> {
343343
| Rvalue::AddressOf(..)
344344
| Rvalue::Discriminant(..)
345345
| Rvalue::Len(..)
346-
| Rvalue::NullaryOp(NullOp::SizeOf, _)
346+
| Rvalue::NullaryOp(NullOp::SizeOf | NullOp::AlignOf, _)
347347
| Rvalue::NullaryOp(NullOp::Box, _) => {
348348
// This returns an rvalue with uninitialized contents. We can't
349349
// move out of it here because it is an rvalue - assignments always

compiler/rustc_mir/src/interpret/intrinsics.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -160,17 +160,14 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
160160
self.write_scalar(Scalar::from_machine_usize(result, self), dest)?;
161161
}
162162

163-
sym::min_align_of
164-
| sym::pref_align_of
163+
sym::pref_align_of
165164
| sym::needs_drop
166165
| sym::type_id
167166
| sym::type_name
168167
| sym::variant_count => {
169168
let gid = GlobalId { instance, promoted: None };
170169
let ty = match intrinsic_name {
171-
sym::min_align_of | sym::pref_align_of | sym::variant_count => {
172-
self.tcx.types.usize
173-
}
170+
sym::pref_align_of | sym::variant_count => self.tcx.types.usize,
174171
sym::needs_drop => self.tcx.types.bool,
175172
sym::type_id => self.tcx.types.u64,
176173
sym::type_name => self.tcx.mk_static_str(),

compiler/rustc_mir/src/interpret/step.rs

+17
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,23 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
284284
self.write_scalar(Scalar::from_machine_usize(layout.size.bytes(), self), &dest)?;
285285
}
286286

287+
NullaryOp(mir::NullOp::AlignOf, ty) => {
288+
let ty = self.subst_from_current_frame_and_normalize_erasing_regions(ty);
289+
let layout = self.layout_of(ty)?;
290+
if layout.is_unsized() {
291+
// FIXME: This should be a span_bug (#80742)
292+
self.tcx.sess.delay_span_bug(
293+
self.frame().current_span(),
294+
&format!("SizeOf nullary MIR operator called for unsized type {}", ty),
295+
);
296+
throw_inval!(SizeOfUnsizedType(ty));
297+
}
298+
self.write_scalar(
299+
Scalar::from_machine_usize(layout.align.abi.bytes(), self),
300+
&dest,
301+
)?;
302+
}
303+
287304
InitBox(ref operand, _) => {
288305
let src = self.eval_operand(operand, None)?;
289306
let v = self.read_immediate(&src)?;

compiler/rustc_mir/src/transform/check_consts/check.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -648,7 +648,7 @@ impl Visitor<'tcx> for Checker<'mir, 'tcx> {
648648
}
649649
}
650650

651-
Rvalue::NullaryOp(NullOp::SizeOf, _) => {}
651+
Rvalue::NullaryOp(NullOp::SizeOf | NullOp::AlignOf, _) => {}
652652
Rvalue::NullaryOp(NullOp::Box, _) => self.check_op(ops::HeapAllocation),
653653
Rvalue::InitBox(_, _) => {}
654654

compiler/rustc_mir/src/transform/lower_intrinsics.rs

+13
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,19 @@ impl<'tcx> MirPass<'tcx> for LowerIntrinsics {
105105
terminator.kind = TerminatorKind::Goto { target };
106106
}
107107
}
108+
sym::min_align_of => {
109+
if let Some((destination, target)) = *destination {
110+
let tp_ty = substs.type_at(0);
111+
block.statements.push(Statement {
112+
source_info: terminator.source_info,
113+
kind: StatementKind::Assign(Box::new((
114+
destination,
115+
Rvalue::NullaryOp(NullOp::AlignOf, tp_ty),
116+
))),
117+
});
118+
terminator.kind = TerminatorKind::Goto { target };
119+
}
120+
}
108121
sym::discriminant_value => {
109122
if let (Some((destination, target)), Some(arg)) =
110123
(*destination, args[0].place())

compiler/rustc_mir/src/transform/promote_consts.rs

+1
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,7 @@ impl<'tcx> Validator<'_, 'tcx> {
520520
Rvalue::NullaryOp(op, _) => match op {
521521
NullOp::Box => return Err(Unpromotable),
522522
NullOp::SizeOf => {}
523+
NullOp::AlignOf => {}
523524
},
524525

525526
Rvalue::InitBox(_, _) => return Err(Unpromotable),

src/tools/clippy/clippy_utils/src/qualify_min_const_fn.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ fn check_rvalue(tcx: TyCtxt<'tcx>, body: &Body<'tcx>, def_id: DefId, rvalue: &Rv
194194
))
195195
}
196196
},
197-
Rvalue::NullaryOp(NullOp::SizeOf, _) => Ok(()),
197+
Rvalue::NullaryOp(NullOp::SizeOf | NullOp::AlignOf, _) => Ok(()),
198198
Rvalue::NullaryOp(NullOp::Box, _) => Err((span, "heap allocations are not allowed in const fn".into())),
199199
Rvalue::InitBox(_, _) => Ok(()),
200200
Rvalue::UnaryOp(_, operand) => {

0 commit comments

Comments
 (0)