Skip to content

Commit 1c3409f

Browse files
committed
Introduce NullOp::AlignOf
1 parent b69fe57 commit 1c3409f

File tree

13 files changed

+54
-33
lines changed

13 files changed

+54
-33
lines changed

compiler/rustc_codegen_cranelift/src/base.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -726,15 +726,20 @@ fn codegen_stmt<'tcx>(
726726
let ptr = fx.bcx.inst_results(call)[0];
727727
lval.write_cvalue(fx, CValue::by_val(ptr, box_layout));
728728
}
729-
Rvalue::NullaryOp(NullOp::SizeOf, ty) => {
729+
Rvalue::NullaryOp(null_op, ty) => {
730730
assert!(
731731
lval.layout()
732732
.ty
733733
.is_sized(fx.tcx.at(stmt.source_info.span), ParamEnv::reveal_all())
734734
);
735-
let ty_size = fx.layout_of(fx.monomorphize(ty)).size.bytes();
735+
let layout = fx.layout_of(fx.monomorphize(ty));
736+
let val = match null_op {
737+
NullOp::SizeOf => layout.size.bytes(),
738+
NullOp::AlignOf => layout.align.abi.bytes(),
739+
NullOp::Box => unreachable!(),
740+
};
736741
let val =
737-
CValue::const_val(fx, fx.layout_of(fx.tcx.types.usize), ty_size.into());
742+
CValue::const_val(fx, fx.layout_of(fx.tcx.types.usize), val.into());
738743
lval.write_cvalue(fx, val);
739744
}
740745
Rvalue::Aggregate(ref kind, ref operands) => match kind.as_ref() {

compiler/rustc_codegen_cranelift/src/intrinsics/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -823,7 +823,7 @@ pub(crate) fn codegen_intrinsic_call<'tcx>(
823823
dest.write_cvalue(fx, val);
824824
};
825825

826-
pref_align_of | min_align_of | needs_drop | type_id | type_name | variant_count, () {
826+
pref_align_of | needs_drop | type_id | type_name | variant_count, () {
827827
let const_val =
828828
fx.tcx.const_eval_instance(ParamEnv::reveal_all(), instance, None).unwrap();
829829
let val = crate::constant::codegen_const_value(

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

+21-14
Original file line numberDiff line numberDiff line change
@@ -487,20 +487,6 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
487487
)
488488
}
489489

490-
mir::Rvalue::NullaryOp(mir::NullOp::SizeOf, ty) => {
491-
let ty = self.monomorphize(ty);
492-
assert!(bx.cx().type_is_sized(ty));
493-
let val = bx.cx().const_usize(bx.cx().layout_of(ty).size.bytes());
494-
let tcx = self.cx.tcx();
495-
(
496-
bx,
497-
OperandRef {
498-
val: OperandValue::Immediate(val),
499-
layout: self.cx.layout_of(tcx.types.usize),
500-
},
501-
)
502-
}
503-
504490
mir::Rvalue::NullaryOp(mir::NullOp::Box, content_ty) => {
505491
let content_ty = self.monomorphize(content_ty);
506492
let content_layout = bx.cx().layout_of(content_ty);
@@ -525,6 +511,27 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
525511
let operand = OperandRef { val: OperandValue::Immediate(val), layout: box_layout };
526512
(bx, operand)
527513
}
514+
515+
mir::Rvalue::NullaryOp(null_op, ty) => {
516+
let ty = self.monomorphize(ty);
517+
assert!(bx.cx().type_is_sized(ty));
518+
let layout = bx.cx().layout_of(ty);
519+
let val = match null_op {
520+
mir::NullOp::SizeOf => layout.size.bytes(),
521+
mir::NullOp::AlignOf => layout.align.abi.bytes(),
522+
mir::NullOp::Box => unreachable!(),
523+
};
524+
let val = bx.cx().const_usize(val);
525+
let tcx = self.cx.tcx();
526+
(
527+
bx,
528+
OperandRef {
529+
val: OperandValue::Immediate(val),
530+
layout: self.cx.layout_of(tcx.types.usize),
531+
},
532+
)
533+
}
534+
528535
mir::Rvalue::ThreadLocalRef(def_id) => {
529536
assert!(bx.cx().tcx().is_static(def_id));
530537
let static_ = bx.get_static(def_id);

compiler/rustc_const_eval/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_const_eval/src/interpret/step.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -270,18 +270,23 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
270270
M::box_alloc(self, &dest)?;
271271
}
272272

273-
NullaryOp(mir::NullOp::SizeOf, ty) => {
273+
NullaryOp(null_op, ty) => {
274274
let ty = self.subst_from_current_frame_and_normalize_erasing_regions(ty);
275275
let layout = self.layout_of(ty)?;
276276
if layout.is_unsized() {
277277
// FIXME: This should be a span_bug (#80742)
278278
self.tcx.sess.delay_span_bug(
279279
self.frame().current_span(),
280-
&format!("SizeOf nullary MIR operator called for unsized type {}", ty),
280+
&format!("Nullary MIR operator called for unsized type {}", ty),
281281
);
282282
throw_inval!(SizeOfUnsizedType(ty));
283283
}
284-
self.write_scalar(Scalar::from_machine_usize(layout.size.bytes(), self), &dest)?;
284+
let val = match null_op {
285+
mir::NullOp::SizeOf => layout.size.bytes(),
286+
mir::NullOp::AlignOf => layout.align.abi.bytes(),
287+
mir::NullOp::Box => unreachable!(),
288+
};
289+
self.write_scalar(Scalar::from_machine_usize(val, self), &dest)?;
285290
}
286291

287292
Cast(cast_kind, ref operand, cast_ty) => {

compiler/rustc_const_eval/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

654654
Rvalue::UnaryOp(_, ref operand) => {

compiler/rustc_const_eval/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::UnaryOp(op, operand) => {

compiler/rustc_middle/src/mir/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -2278,6 +2278,8 @@ impl BinOp {
22782278
pub enum NullOp {
22792279
/// Returns the size of a value of that type
22802280
SizeOf,
2281+
/// Returns the minimum alignment of a type
2282+
AlignOf,
22812283
/// Creates a new uninitialized box for a value of that type
22822284
Box,
22832285
}

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_dataflow/src/move_paths/builder.rs

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

compiler/rustc_mir_transform/src/lower_intrinsics.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,19 @@ impl<'tcx> MirPass<'tcx> for LowerIntrinsics {
9292
// since their semantics depend on the value of overflow-checks flag used
9393
// during codegen. Issue #35310.
9494
}
95-
sym::size_of => {
95+
sym::size_of | sym::min_align_of => {
9696
if let Some((destination, target)) = *destination {
9797
let tp_ty = substs.type_at(0);
98+
let null_op = match intrinsic_name {
99+
sym::size_of => NullOp::SizeOf,
100+
sym::min_align_of => NullOp::AlignOf,
101+
_ => bug!("unexpected intrinsic"),
102+
};
98103
block.statements.push(Statement {
99104
source_info: terminator.source_info,
100105
kind: StatementKind::Assign(Box::new((
101106
destination,
102-
Rvalue::NullaryOp(NullOp::SizeOf, tp_ty),
107+
Rvalue::NullaryOp(null_op, tp_ty),
103108
))),
104109
});
105110
terminator.kind = TerminatorKind::Goto { target };

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

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

0 commit comments

Comments
 (0)