1
Fork 0

Add a new debug_assertions instrinsic (compiler)

And in clippy
This commit is contained in:
Ben Kimock 2024-02-07 10:26:00 -05:00
parent 55fabf35b1
commit 8836ac5758
18 changed files with 72 additions and 13 deletions

View file

@ -1984,6 +1984,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
ConstraintCategory::SizedBound, ConstraintCategory::SizedBound,
); );
} }
&Rvalue::NullaryOp(NullOp::DebugAssertions, _) => {}
Rvalue::ShallowInitBox(operand, ty) => { Rvalue::ShallowInitBox(operand, ty) => {
self.check_operand(operand, location); self.check_operand(operand, location);

View file

@ -767,6 +767,15 @@ fn codegen_stmt<'tcx>(
NullOp::OffsetOf(fields) => { NullOp::OffsetOf(fields) => {
layout.offset_of_subfield(fx, fields.iter()).bytes() layout.offset_of_subfield(fx, fields.iter()).bytes()
} }
NullOp::DebugAssertions => {
let val = fx.tcx.sess.opts.debug_assertions;
let val = CValue::by_val(
fx.bcx.ins().iconst(types::I8, i64::try_from(val).unwrap()),
fx.layout_of(fx.tcx.types.bool),
);
lval.write_cvalue(fx, val);
return;
}
}; };
let val = CValue::by_val( let val = CValue::by_val(
fx.bcx.ins().iconst(fx.pointer_type, i64::try_from(val).unwrap()), fx.bcx.ins().iconst(fx.pointer_type, i64::try_from(val).unwrap()),

View file

@ -672,17 +672,23 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
let val = match null_op { let val = match null_op {
mir::NullOp::SizeOf => { mir::NullOp::SizeOf => {
assert!(bx.cx().type_is_sized(ty)); assert!(bx.cx().type_is_sized(ty));
layout.size.bytes() let val = layout.size.bytes();
bx.cx().const_usize(val)
} }
mir::NullOp::AlignOf => { mir::NullOp::AlignOf => {
assert!(bx.cx().type_is_sized(ty)); assert!(bx.cx().type_is_sized(ty));
layout.align.abi.bytes() let val = layout.align.abi.bytes();
bx.cx().const_usize(val)
} }
mir::NullOp::OffsetOf(fields) => { mir::NullOp::OffsetOf(fields) => {
layout.offset_of_subfield(bx.cx(), fields.iter()).bytes() let val = layout.offset_of_subfield(bx.cx(), fields.iter()).bytes();
bx.cx().const_usize(val)
}
mir::NullOp::DebugAssertions => {
let val = bx.tcx().sess.opts.debug_assertions;
bx.cx().const_bool(val)
} }
}; };
let val = bx.cx().const_usize(val);
let tcx = self.cx.tcx(); let tcx = self.cx.tcx();
OperandRef { OperandRef {
val: OperandValue::Immediate(val), val: OperandValue::Immediate(val),

View file

@ -246,13 +246,25 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
); );
} }
let val = match null_op { let val = match null_op {
mir::NullOp::SizeOf => layout.size.bytes(), mir::NullOp::SizeOf => {
mir::NullOp::AlignOf => layout.align.abi.bytes(), let val = layout.size.bytes();
Scalar::from_target_usize(val, self)
}
mir::NullOp::AlignOf => {
let val = layout.align.abi.bytes();
Scalar::from_target_usize(val, self)
}
mir::NullOp::OffsetOf(fields) => { mir::NullOp::OffsetOf(fields) => {
layout.offset_of_subfield(self, fields.iter()).bytes() let val = layout.offset_of_subfield(self, fields.iter()).bytes();
Scalar::from_target_usize(val, self)
}
mir::NullOp::DebugAssertions => {
// The checks hidden behind this are always better done by the interpreter
// itself, because it knows the runtime state better.
Scalar::from_bool(false)
} }
}; };
self.write_scalar(Scalar::from_target_usize(val, self), &dest)?; self.write_scalar(val, &dest)?;
} }
ShallowInitBox(ref operand, _) => { ShallowInitBox(ref operand, _) => {

View file

@ -544,7 +544,10 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
Rvalue::Cast(_, _, _) => {} Rvalue::Cast(_, _, _) => {}
Rvalue::NullaryOp(NullOp::SizeOf | NullOp::AlignOf | NullOp::OffsetOf(_), _) => {} Rvalue::NullaryOp(
NullOp::SizeOf | NullOp::AlignOf | NullOp::OffsetOf(_) | NullOp::DebugAssertions,
_,
) => {}
Rvalue::ShallowInitBox(_, _) => {} Rvalue::ShallowInitBox(_, _) => {}
Rvalue::UnaryOp(_, operand) => { Rvalue::UnaryOp(_, operand) => {

View file

@ -1139,7 +1139,7 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
Rvalue::Repeat(_, _) Rvalue::Repeat(_, _)
| Rvalue::ThreadLocalRef(_) | Rvalue::ThreadLocalRef(_)
| Rvalue::AddressOf(_, _) | Rvalue::AddressOf(_, _)
| Rvalue::NullaryOp(NullOp::SizeOf | NullOp::AlignOf, _) | Rvalue::NullaryOp(NullOp::SizeOf | NullOp::AlignOf | NullOp::DebugAssertions, _)
| Rvalue::Discriminant(_) => {} | Rvalue::Discriminant(_) => {}
} }
self.super_rvalue(rvalue, location); self.super_rvalue(rvalue, location);

View file

@ -112,7 +112,8 @@ pub fn intrinsic_operation_unsafety(tcx: TyCtxt<'_>, intrinsic_id: DefId) -> hir
| sym::forget | sym::forget
| sym::black_box | sym::black_box
| sym::variant_count | sym::variant_count
| sym::ptr_mask => hir::Unsafety::Normal, | sym::ptr_mask
| sym::debug_assertions => hir::Unsafety::Normal,
_ => hir::Unsafety::Unsafe, _ => hir::Unsafety::Unsafe,
}; };
@ -461,6 +462,8 @@ pub fn check_intrinsic_type(tcx: TyCtxt<'_>, it: &hir::ForeignItem<'_>) {
(0, vec![Ty::new_imm_ptr(tcx, Ty::new_unit(tcx))], tcx.types.usize) (0, vec![Ty::new_imm_ptr(tcx, Ty::new_unit(tcx))], tcx.types.usize)
} }
sym::debug_assertions => (0, Vec::new(), tcx.types.bool),
other => { other => {
tcx.dcx().emit_err(UnrecognizedIntrinsicFunction { span: it.span, name: other }); tcx.dcx().emit_err(UnrecognizedIntrinsicFunction { span: it.span, name: other });
return; return;

View file

@ -907,6 +907,7 @@ impl<'tcx> Debug for Rvalue<'tcx> {
NullOp::SizeOf => write!(fmt, "SizeOf({t})"), NullOp::SizeOf => write!(fmt, "SizeOf({t})"),
NullOp::AlignOf => write!(fmt, "AlignOf({t})"), NullOp::AlignOf => write!(fmt, "AlignOf({t})"),
NullOp::OffsetOf(fields) => write!(fmt, "OffsetOf({t}, {fields:?})"), NullOp::OffsetOf(fields) => write!(fmt, "OffsetOf({t}, {fields:?})"),
NullOp::DebugAssertions => write!(fmt, "cfg!(debug_assertions)"),
} }
} }
ThreadLocalRef(did) => ty::tls::with(|tcx| { ThreadLocalRef(did) => ty::tls::with(|tcx| {

View file

@ -1361,6 +1361,8 @@ pub enum NullOp<'tcx> {
AlignOf, AlignOf,
/// Returns the offset of a field /// Returns the offset of a field
OffsetOf(&'tcx List<(VariantIdx, FieldIdx)>), OffsetOf(&'tcx List<(VariantIdx, FieldIdx)>),
/// cfg!(debug_assertions), but expanded in codegen
DebugAssertions,
} }
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]

View file

@ -194,6 +194,7 @@ impl<'tcx> Rvalue<'tcx> {
Rvalue::NullaryOp(NullOp::SizeOf | NullOp::AlignOf | NullOp::OffsetOf(..), _) => { Rvalue::NullaryOp(NullOp::SizeOf | NullOp::AlignOf | NullOp::OffsetOf(..), _) => {
tcx.types.usize tcx.types.usize
} }
Rvalue::NullaryOp(NullOp::DebugAssertions, _) => tcx.types.bool,
Rvalue::Aggregate(ref ak, ref ops) => match **ak { Rvalue::Aggregate(ref ak, ref ops) => match **ak {
AggregateKind::Array(ty) => Ty::new_array(tcx, ty, ops.len() as u64), AggregateKind::Array(ty) => Ty::new_array(tcx, ty, ops.len() as u64),
AggregateKind::Tuple => { AggregateKind::Tuple => {

View file

@ -429,7 +429,10 @@ impl<'b, 'a, 'tcx, F: Fn(Ty<'tcx>) -> bool> Gatherer<'b, 'a, 'tcx, F> {
| Rvalue::AddressOf(..) | Rvalue::AddressOf(..)
| Rvalue::Discriminant(..) | Rvalue::Discriminant(..)
| Rvalue::Len(..) | Rvalue::Len(..)
| Rvalue::NullaryOp(NullOp::SizeOf | NullOp::AlignOf | NullOp::OffsetOf(..), _) => {} | Rvalue::NullaryOp(
NullOp::SizeOf | NullOp::AlignOf | NullOp::OffsetOf(..) | NullOp::DebugAssertions,
_,
) => {}
} }
} }

View file

@ -639,6 +639,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
NullOp::OffsetOf(fields) => { NullOp::OffsetOf(fields) => {
op_layout.offset_of_subfield(self, fields.iter()).bytes() op_layout.offset_of_subfield(self, fields.iter()).bytes()
} }
NullOp::DebugAssertions => return None,
}; };
ImmTy::from_scalar(Scalar::from_target_usize(val, self), layout).into() ImmTy::from_scalar(Scalar::from_target_usize(val, self), layout).into()
} }

View file

@ -489,6 +489,7 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
NullOp::OffsetOf(fields) => { NullOp::OffsetOf(fields) => {
layout.offset_of_subfield(&self.ecx, fields.iter()).bytes() layout.offset_of_subfield(&self.ecx, fields.iter()).bytes()
} }
NullOp::DebugAssertions => return None,
}; };
let usize_layout = self.ecx.layout_of(self.tcx.types.usize).unwrap(); let usize_layout = self.ecx.layout_of(self.tcx.types.usize).unwrap();
let imm = ImmTy::try_from_uint(val, usize_layout)?; let imm = ImmTy::try_from_uint(val, usize_layout)?;

View file

@ -21,6 +21,17 @@ impl<'tcx> MirPass<'tcx> for LowerIntrinsics {
sym::unreachable => { sym::unreachable => {
terminator.kind = TerminatorKind::Unreachable; terminator.kind = TerminatorKind::Unreachable;
} }
sym::debug_assertions => {
let target = target.unwrap();
block.statements.push(Statement {
source_info: terminator.source_info,
kind: StatementKind::Assign(Box::new((
*destination,
Rvalue::NullaryOp(NullOp::DebugAssertions, tcx.types.bool),
))),
});
terminator.kind = TerminatorKind::Goto { target };
}
sym::forget => { sym::forget => {
if let Some(target) = *target { if let Some(target) = *target {
block.statements.push(Statement { block.statements.push(Statement {

View file

@ -446,6 +446,7 @@ impl<'tcx> Validator<'_, 'tcx> {
NullOp::SizeOf => {} NullOp::SizeOf => {}
NullOp::AlignOf => {} NullOp::AlignOf => {}
NullOp::OffsetOf(_) => {} NullOp::OffsetOf(_) => {}
NullOp::DebugAssertions => {}
}, },
Rvalue::ShallowInitBox(_, _) => return Err(Unpromotable), Rvalue::ShallowInitBox(_, _) => return Err(Unpromotable),

View file

@ -257,6 +257,7 @@ impl<'tcx> Stable<'tcx> for mir::NullOp<'tcx> {
OffsetOf(indices) => stable_mir::mir::NullOp::OffsetOf( OffsetOf(indices) => stable_mir::mir::NullOp::OffsetOf(
indices.iter().map(|idx| idx.stable(tables)).collect(), indices.iter().map(|idx| idx.stable(tables)).collect(),
), ),
DebugAssertions => stable_mir::mir::NullOp::DebugAssertions,
} }
} }
} }

View file

@ -639,6 +639,7 @@ impl Rvalue {
Rvalue::NullaryOp(NullOp::SizeOf | NullOp::AlignOf | NullOp::OffsetOf(..), _) => { Rvalue::NullaryOp(NullOp::SizeOf | NullOp::AlignOf | NullOp::OffsetOf(..), _) => {
Ok(Ty::usize_ty()) Ok(Ty::usize_ty())
} }
Rvalue::NullaryOp(NullOp::DebugAssertions, _) => Ok(Ty::bool_ty()),
Rvalue::Aggregate(ak, ops) => match *ak { Rvalue::Aggregate(ak, ops) => match *ak {
AggregateKind::Array(ty) => Ty::try_new_array(ty, ops.len() as u64), AggregateKind::Array(ty) => Ty::try_new_array(ty, ops.len() as u64),
AggregateKind::Tuple => Ok(Ty::new_tuple( AggregateKind::Tuple => Ok(Ty::new_tuple(
@ -1005,6 +1006,8 @@ pub enum NullOp {
AlignOf, AlignOf,
/// Returns the offset of a field. /// Returns the offset of a field.
OffsetOf(Vec<(VariantIdx, FieldIdx)>), OffsetOf(Vec<(VariantIdx, FieldIdx)>),
/// cfg!(debug_assertions), but at codegen time
DebugAssertions,
} }
impl Operand { impl Operand {

View file

@ -174,7 +174,7 @@ fn check_rvalue<'tcx>(
)) ))
} }
}, },
Rvalue::NullaryOp(NullOp::SizeOf | NullOp::AlignOf | NullOp::OffsetOf(_), _) | Rvalue::ShallowInitBox(_, _) => { Rvalue::NullaryOp(NullOp::SizeOf | NullOp::AlignOf | NullOp::OffsetOf(_) | NullOp::DebugAssertions, _) | Rvalue::ShallowInitBox(_, _) => {
Ok(()) Ok(())
}, },
Rvalue::UnaryOp(_, operand) => { Rvalue::UnaryOp(_, operand) => {