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

+1-1
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

+2-2
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

+2-2
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

+1-1
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

+3-1
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

+2-1
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

+3-1
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

+4-1
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

+18
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

+1-1
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 => {

compiler/rustc_middle/src/mir/traversal.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -286,9 +286,7 @@ pub fn reverse_postorder<'a, 'tcx>(
286286
/// reachable.
287287
///
288288
/// Such a traversal is mostly useful because it lets us skip lowering the `false` side
289-
/// of `if <T as Trait>::CONST`, as well as [`NullOp::UbChecks`].
290-
///
291-
/// [`NullOp::UbChecks`]: rustc_middle::mir::NullOp::UbChecks
289+
/// of `if <T as Trait>::CONST`, as well as [`NullOp::RuntimeChecks`].
292290
pub fn mono_reachable<'a, 'tcx>(
293291
body: &'a Body<'tcx>,
294292
tcx: TyCtxt<'tcx>,

compiler/rustc_mir_dataflow/src/move_paths/builder.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ impl<'b, 'a, 'tcx, F: Fn(Ty<'tcx>) -> bool> Gatherer<'b, 'a, 'tcx, F> {
436436
| Rvalue::Discriminant(..)
437437
| Rvalue::Len(..)
438438
| Rvalue::NullaryOp(
439-
NullOp::SizeOf | NullOp::AlignOf | NullOp::OffsetOf(..) | NullOp::UbChecks,
439+
NullOp::SizeOf | NullOp::AlignOf | NullOp::OffsetOf(..) | NullOp::RuntimeChecks(_),
440440
_,
441441
) => {}
442442
}

compiler/rustc_mir_transform/src/cost_checker.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ impl<'tcx> Visitor<'tcx> for CostChecker<'_, 'tcx> {
9393

9494
fn visit_rvalue(&mut self, rvalue: &Rvalue<'tcx>, _location: Location) {
9595
match rvalue {
96-
Rvalue::NullaryOp(NullOp::UbChecks, ..)
96+
// FIXME: Should we do the same for `OverflowChecks`?
97+
Rvalue::NullaryOp(NullOp::RuntimeChecks(RuntimeChecks::UbChecks), ..)
9798
if !self
9899
.tcx
99100
.sess

compiler/rustc_mir_transform/src/gvn.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,7 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
528528
.tcx
529529
.offset_of_subfield(self.ecx.param_env(), layout, fields.iter())
530530
.bytes(),
531-
NullOp::UbChecks => return None,
531+
NullOp::RuntimeChecks(_) => return None,
532532
};
533533
let usize_layout = self.ecx.layout_of(self.tcx.types.usize).unwrap();
534534
let imm = ImmTy::from_uint(val, usize_layout);

compiler/rustc_mir_transform/src/instsimplify.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,8 @@ impl<'tcx> InstSimplifyContext<'tcx, '_> {
200200
}
201201

202202
fn simplify_ub_check(&self, source_info: &SourceInfo, rvalue: &mut Rvalue<'tcx>) {
203-
if let Rvalue::NullaryOp(NullOp::UbChecks, _) = *rvalue {
203+
// FIXME: Should we do the same for overflow checks?
204+
if let Rvalue::NullaryOp(NullOp::RuntimeChecks(RuntimeChecks::UbChecks), _) = *rvalue {
204205
let const_ = Const::from_bool(self.tcx, self.tcx.sess.ub_checks());
205206
let constant = ConstOperand { span: source_info.span, const_, user_ty: None };
206207
*rvalue = Rvalue::Use(Operand::Constant(Box::new(constant)));

compiler/rustc_mir_transform/src/known_panics_lint.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
627627
.tcx
628628
.offset_of_subfield(self.param_env, op_layout, fields.iter())
629629
.bytes(),
630-
NullOp::UbChecks => return None,
630+
NullOp::RuntimeChecks(_) => return None,
631631
};
632632
ImmTy::from_scalar(Scalar::from_target_usize(val, self), layout).into()
633633
}

compiler/rustc_mir_transform/src/lower_intrinsics.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,18 @@ impl<'tcx> MirPass<'tcx> for LowerIntrinsics {
2323
sym::unreachable => {
2424
terminator.kind = TerminatorKind::Unreachable;
2525
}
26-
sym::ub_checks => {
26+
sym::ub_checks | sym::overflow_checks => {
27+
let op = match intrinsic.name {
28+
sym::ub_checks => RuntimeChecks::UbChecks,
29+
sym::overflow_checks => RuntimeChecks::OverflowChecks,
30+
_ => unreachable!(),
31+
};
2732
let target = target.unwrap();
2833
block.statements.push(Statement {
2934
source_info: terminator.source_info,
3035
kind: StatementKind::Assign(Box::new((
3136
*destination,
32-
Rvalue::NullaryOp(NullOp::UbChecks, tcx.types.bool),
37+
Rvalue::NullaryOp(NullOp::RuntimeChecks(op), tcx.types.bool),
3338
))),
3439
});
3540
terminator.kind = TerminatorKind::Goto { target };

compiler/rustc_mir_transform/src/promote_consts.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ impl<'tcx> Validator<'_, 'tcx> {
448448
NullOp::SizeOf => {}
449449
NullOp::AlignOf => {}
450450
NullOp::OffsetOf(_) => {}
451-
NullOp::UbChecks => {}
451+
NullOp::RuntimeChecks(_) => {}
452452
},
453453

454454
Rvalue::ShallowInitBox(_, _) => return Err(Unpromotable),

compiler/rustc_mir_transform/src/validate.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1346,7 +1346,7 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
13461346
Rvalue::Repeat(_, _)
13471347
| Rvalue::ThreadLocalRef(_)
13481348
| Rvalue::AddressOf(_, _)
1349-
| Rvalue::NullaryOp(NullOp::SizeOf | NullOp::AlignOf | NullOp::UbChecks, _)
1349+
| Rvalue::NullaryOp(NullOp::SizeOf | NullOp::AlignOf | NullOp::RuntimeChecks(_), _)
13501350
| Rvalue::Discriminant(_) => {}
13511351
}
13521352
self.super_rvalue(rvalue, location);

compiler/rustc_smir/src/rustc_smir/convert/mir.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -267,13 +267,17 @@ impl<'tcx> Stable<'tcx> for mir::NullOp<'tcx> {
267267
type T = stable_mir::mir::NullOp;
268268
fn stable(&self, tables: &mut Tables<'_>) -> Self::T {
269269
use rustc_middle::mir::NullOp::*;
270+
use rustc_middle::mir::RuntimeChecks::*;
270271
match self {
271272
SizeOf => stable_mir::mir::NullOp::SizeOf,
272273
AlignOf => stable_mir::mir::NullOp::AlignOf,
273274
OffsetOf(indices) => stable_mir::mir::NullOp::OffsetOf(
274275
indices.iter().map(|idx| idx.stable(tables)).collect(),
275276
),
276-
UbChecks => stable_mir::mir::NullOp::UbChecks,
277+
RuntimeChecks(kind) => stable_mir::mir::NullOp::RuntimeChecks(match kind {
278+
UbChecks => stable_mir::mir::RuntimeChecks::UbChecks,
279+
OverflowChecks => stable_mir::mir::RuntimeChecks::OverflowChecks,
280+
}),
277281
}
278282
}
279283
}

compiler/stable_mir/src/mir/body.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -606,7 +606,7 @@ impl Rvalue {
606606
Rvalue::NullaryOp(NullOp::SizeOf | NullOp::AlignOf | NullOp::OffsetOf(..), _) => {
607607
Ok(Ty::usize_ty())
608608
}
609-
Rvalue::NullaryOp(NullOp::UbChecks, _) => Ok(Ty::bool_ty()),
609+
Rvalue::NullaryOp(NullOp::RuntimeChecks(_), _) => Ok(Ty::bool_ty()),
610610
Rvalue::Aggregate(ak, ops) => match *ak {
611611
AggregateKind::Array(ty) => Ty::try_new_array(ty, ops.len() as u64),
612612
AggregateKind::Tuple => Ok(Ty::new_tuple(
@@ -978,8 +978,16 @@ pub enum NullOp {
978978
AlignOf,
979979
/// Returns the offset of a field.
980980
OffsetOf(Vec<(VariantIdx, FieldIdx)>),
981+
/// Codegen conditions for runtime checks.
982+
RuntimeChecks(RuntimeChecks),
983+
}
984+
985+
#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
986+
pub enum RuntimeChecks {
981987
/// cfg!(ub_checks), but at codegen time
982988
UbChecks,
989+
/// cfg!(overflow_checks), but at codegen time
990+
OverflowChecks,
983991
}
984992

985993
impl Operand {

library/core/src/intrinsics.rs

+21
Original file line numberDiff line numberDiff line change
@@ -2671,6 +2671,27 @@ pub const fn ub_checks() -> bool {
26712671
cfg!(debug_assertions)
26722672
}
26732673

2674+
/// Returns whether we should perform some overflow-checking at runtime. This eventually evaluates to
2675+
/// `cfg!(overflow_checks)`, but behaves different from `cfg!` when mixing crates built with different
2676+
/// flags: if the crate has UB checks enabled or carries the `#[rustc_preserve_overflow_checks]`
2677+
/// attribute, evaluation is delayed until monomorphization (or until the call gets inlined into
2678+
/// a crate that does not delay evaluation further); otherwise it can happen any time.
2679+
///
2680+
/// The common case here is a user program built with overflow_checks linked against the distributed
2681+
/// sysroot which is built without overflow_checks but with `#[rustc_preserve_overflow_checks]`.
2682+
/// For code that gets monomorphized in the user crate (i.e., generic functions and functions with
2683+
/// `#[inline]`), gating assertions on `overflow_checks()` rather than `cfg!(overflow_checks)` means that
2684+
/// assertions are enabled whenever the *user crate* has UB checks enabled. However if the
2685+
/// user has overflow checks disabled, the checks will still get optimized out.
2686+
#[cfg(not(bootstrap))]
2687+
#[rustc_const_unstable(feature = "const_overflow_checks", issue = "none")]
2688+
#[unstable(feature = "core_intrinsics", issue = "none")]
2689+
#[inline(always)]
2690+
#[rustc_intrinsic]
2691+
pub const fn overflow_checks() -> bool {
2692+
cfg!(debug_assertions)
2693+
}
2694+
26742695
/// Allocates a block of memory at compile time.
26752696
/// At runtime, just returns a null pointer.
26762697
///

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ fn check_rvalue<'tcx>(
177177
))
178178
}
179179
},
180-
Rvalue::NullaryOp(NullOp::SizeOf | NullOp::AlignOf | NullOp::OffsetOf(_) | NullOp::UbChecks, _)
180+
Rvalue::NullaryOp(NullOp::SizeOf | NullOp::AlignOf | NullOp::OffsetOf(_) | NullOp::RuntimeChecks(_), _)
181181
| Rvalue::ShallowInitBox(_, _) => Ok(()),
182182
Rvalue::UnaryOp(_, operand) => {
183183
let ty = operand.ty(body, tcx);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//@ compile-flags: -Cdebug-assertions=yes
2+
3+
#![crate_type = "lib"]
4+
#![feature(strict_overflow_ops)]
5+
#![feature(rustc_attrs)]
6+
#![feature(core_intrinsics)]
7+
8+
/// Emulates the default behavior of `+` using
9+
/// `intrinsics::overflow_checks()`.
10+
#[inline]
11+
#[rustc_inherit_overflow_checks]
12+
pub fn add(a: u8, b: u8) -> u8 {
13+
if core::intrinsics::overflow_checks() { a.strict_add(b) } else { a.wrapping_add(b) }
14+
}

tests/codegen/overflow-checks.rs

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// With -Coverflow-checks=yes (enabled by default by -Cdebug-assertions=yes) we will produce a
2+
// runtime check that panics when an operation would result in integer overflow.
3+
//
4+
// This test ensures that such a runtime check is *not* emitted when debug-assertions are enabled,
5+
// but overflow-checks are explicitly disabled. It also ensures that even if a dependency is
6+
// compiled with overflow checks, `intrinsics::overflow_checks()` will be treated with the
7+
// overflow-checks setting of the current crate (when `#[rustc_inherit_overflow_checks]`) is used.
8+
9+
//@ aux-build:overflow_checks_add.rs
10+
//@ revisions: DEBUG NOCHECKS
11+
//@ [DEBUG] compile-flags:
12+
//@ [NOCHECKS] compile-flags: -Coverflow-checks=no
13+
//@ compile-flags: -O -Cdebug-assertions=yes
14+
15+
#![crate_type = "lib"]
16+
17+
extern crate overflow_checks_add;
18+
19+
// CHECK-LABEL: @add(
20+
#[no_mangle]
21+
pub unsafe fn add(a: u8, b: u8) -> u8 {
22+
// CHECK: i8 noundef %a, i8 noundef %b
23+
// DEBUG: @llvm.uadd.with.overflow.i8
24+
// DEBUG: call core::num::overflow_panic::add
25+
// DEBUG: unreachable
26+
// NOCHECKS: %0 = add i8 %b, %a
27+
// NOCHECKS: ret i8 %0
28+
overflow_checks_add::add(a, b)
29+
}

0 commit comments

Comments
 (0)