Skip to content

Commit 803f841

Browse files
committed
add overflow_checks intrinsic
1 parent 176e545 commit 803f841

File tree

25 files changed

+133
-26
lines changed

25 files changed

+133
-26
lines changed

compiler/rustc_borrowck/src/type_check/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1955,7 +1955,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
19551955
ConstraintCategory::SizedBound,
19561956
);
19571957
}
1958-
&Rvalue::NullaryOp(NullOp::UbChecks, _) => {}
1958+
&Rvalue::NullaryOp(NullOp::RuntimeChecks(_), _) => {}
19591959

19601960
Rvalue::ShallowInitBox(operand, ty) => {
19611961
self.check_operand(operand, location);

compiler/rustc_codegen_cranelift/src/base.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -839,8 +839,8 @@ fn codegen_stmt<'tcx>(
839839
.tcx
840840
.offset_of_subfield(ParamEnv::reveal_all(), layout, fields.iter())
841841
.bytes(),
842-
NullOp::UbChecks => {
843-
let val = fx.tcx.sess.ub_checks();
842+
NullOp::RuntimeChecks(kind) => {
843+
let val = kind.value(fx.tcx.sess);
844844
let val = CValue::by_val(
845845
fx.bcx.ins().iconst(types::I8, i64::try_from(val).unwrap()),
846846
fx.layout_of(fx.tcx.types.bool),

compiler/rustc_codegen_ssa/src/mir/rvalue.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -706,8 +706,8 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
706706
.bytes();
707707
bx.cx().const_usize(val)
708708
}
709-
mir::NullOp::UbChecks => {
710-
let val = bx.tcx().sess.ub_checks();
709+
mir::NullOp::RuntimeChecks(kind) => {
710+
let val = kind.value(bx.tcx().sess);
711711
bx.cx().const_bool(val)
712712
}
713713
};

compiler/rustc_const_eval/src/check_consts/check.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,7 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
567567
Rvalue::Cast(_, _, _) => {}
568568

569569
Rvalue::NullaryOp(
570-
NullOp::SizeOf | NullOp::AlignOf | NullOp::OffsetOf(_) | NullOp::UbChecks,
570+
NullOp::SizeOf | NullOp::AlignOf | NullOp::OffsetOf(_) | NullOp::RuntimeChecks(_),
571571
_,
572572
) => {}
573573
Rvalue::ShallowInitBox(_, _) => {}

compiler/rustc_const_eval/src/interpret/step.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,9 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
257257
.bytes();
258258
Scalar::from_target_usize(val, self)
259259
}
260-
mir::NullOp::UbChecks => Scalar::from_bool(self.tcx.sess.ub_checks()),
260+
mir::NullOp::RuntimeChecks(kind) => {
261+
Scalar::from_bool(kind.value(self.tcx.sess))
262+
}
261263
};
262264
self.write_scalar(val, &dest)?;
263265
}

compiler/rustc_hir_analysis/src/check/intrinsic.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ pub fn intrinsic_operation_unsafety(tcx: TyCtxt<'_>, intrinsic_id: LocalDefId) -
140140
| sym::aggregate_raw_ptr
141141
| sym::ptr_metadata
142142
| sym::ub_checks
143+
| sym::overflow_checks
143144
| sym::fadd_algebraic
144145
| sym::fsub_algebraic
145146
| sym::fmul_algebraic
@@ -592,7 +593,7 @@ pub fn check_intrinsic_type(
592593
sym::aggregate_raw_ptr => (3, 0, vec![param(1), param(2)], param(0)),
593594
sym::ptr_metadata => (2, 0, vec![Ty::new_imm_ptr(tcx, param(0))], param(1)),
594595

595-
sym::ub_checks => (0, 0, Vec::new(), tcx.types.bool),
596+
sym::ub_checks | sym::overflow_checks => (0, 0, Vec::new(), tcx.types.bool),
596597

597598
sym::simd_eq
598599
| sym::simd_ne

compiler/rustc_middle/src/mir/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -745,7 +745,9 @@ impl<'tcx> Body<'tcx> {
745745
}
746746

747747
match rvalue {
748-
Rvalue::NullaryOp(NullOp::UbChecks, _) => Some((tcx.sess.ub_checks() as u128, targets)),
748+
Rvalue::NullaryOp(NullOp::RuntimeChecks(kind), _) => {
749+
Some((kind.value(tcx.sess) as u128, targets))
750+
}
749751
Rvalue::Use(Operand::Constant(constant)) => {
750752
let bits = eval_mono_const(constant)?;
751753
Some((bits, targets))

compiler/rustc_middle/src/mir/pretty.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1004,7 +1004,10 @@ impl<'tcx> Debug for Rvalue<'tcx> {
10041004
NullOp::SizeOf => write!(fmt, "SizeOf({t})"),
10051005
NullOp::AlignOf => write!(fmt, "AlignOf({t})"),
10061006
NullOp::OffsetOf(fields) => write!(fmt, "OffsetOf({t}, {fields:?})"),
1007-
NullOp::UbChecks => write!(fmt, "UbChecks()"),
1007+
NullOp::RuntimeChecks(RuntimeChecks::UbChecks) => write!(fmt, "UbChecks()"),
1008+
NullOp::RuntimeChecks(RuntimeChecks::OverflowChecks) => {
1009+
write!(fmt, "OverflowChecks()")
1010+
}
10081011
}
10091012
}
10101013
ThreadLocalRef(did) => ty::tls::with(|tcx| {

compiler/rustc_middle/src/mir/syntax.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1460,9 +1460,27 @@ pub enum NullOp<'tcx> {
14601460
AlignOf,
14611461
/// Returns the offset of a field
14621462
OffsetOf(&'tcx List<(VariantIdx, FieldIdx)>),
1463+
/// Returns whether we should perform some checking at runtime.
1464+
RuntimeChecks(RuntimeChecks),
1465+
}
1466+
1467+
#[derive(Copy, Clone, Debug, PartialEq, Eq, TyEncodable, TyDecodable, Hash, HashStable)]
1468+
pub enum RuntimeChecks {
14631469
/// Returns whether we should perform some UB-checking at runtime.
14641470
/// See the `ub_checks` intrinsic docs for details.
14651471
UbChecks,
1472+
/// Returns whether we should perform some overflow-checking at runtime.
1473+
/// See the `overflow_checks` intrinsic docs for details.
1474+
OverflowChecks,
1475+
}
1476+
1477+
impl RuntimeChecks {
1478+
pub fn value(self, sess: &rustc_session::Session) -> bool {
1479+
match self {
1480+
Self::UbChecks => sess.ub_checks(),
1481+
Self::OverflowChecks => sess.overflow_checks(),
1482+
}
1483+
}
14661484
}
14671485

14681486
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]

compiler/rustc_middle/src/mir/tcx.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ impl<'tcx> Rvalue<'tcx> {
189189
Rvalue::NullaryOp(NullOp::SizeOf | NullOp::AlignOf | NullOp::OffsetOf(..), _) => {
190190
tcx.types.usize
191191
}
192-
Rvalue::NullaryOp(NullOp::UbChecks, _) => tcx.types.bool,
192+
Rvalue::NullaryOp(NullOp::RuntimeChecks(_), _) => tcx.types.bool,
193193
Rvalue::Aggregate(ref ak, ref ops) => match **ak {
194194
AggregateKind::Array(ty) => Ty::new_array(tcx, ty, ops.len() as u64),
195195
AggregateKind::Tuple => {

0 commit comments

Comments
 (0)