Skip to content

Commit 24bd8e0

Browse files
Auto merge of #146288 - pitaj:intrinsic-overflow_checks-trial, r=<try>
Intrinsic overflow checks perf trial
2 parents 360a3a4 + 47e50ab commit 24bd8e0

File tree

34 files changed

+301
-102
lines changed

34 files changed

+301
-102
lines changed

compiler/rustc_borrowck/src/type_check/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,8 +1056,7 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
10561056
ConstraintCategory::SizedBound,
10571057
);
10581058
}
1059-
&Rvalue::NullaryOp(NullOp::ContractChecks, _) => {}
1060-
&Rvalue::NullaryOp(NullOp::UbChecks, _) => {}
1059+
&Rvalue::NullaryOp(NullOp::RuntimeChecks(_), _) => {}
10611060

10621061
Rvalue::ShallowInitBox(_operand, ty) => {
10631062
let trait_ref =

compiler/rustc_codegen_cranelift/src/base.rs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -855,17 +855,8 @@ fn codegen_stmt<'tcx>(fx: &mut FunctionCx<'_, '_, 'tcx>, cur_block: Block, stmt:
855855
fields.iter(),
856856
)
857857
.bytes(),
858-
NullOp::UbChecks => {
859-
let val = fx.tcx.sess.ub_checks();
860-
let val = CValue::by_val(
861-
fx.bcx.ins().iconst(types::I8, i64::from(val)),
862-
fx.layout_of(fx.tcx.types.bool),
863-
);
864-
lval.write_cvalue(fx, val);
865-
return;
866-
}
867-
NullOp::ContractChecks => {
868-
let val = fx.tcx.sess.contract_checks();
858+
NullOp::RuntimeChecks(kind) => {
859+
let val = kind.value(fx.tcx.sess);
869860
let val = CValue::by_val(
870861
fx.bcx.ins().iconst(types::I8, i64::from(val)),
871862
fx.layout_of(fx.tcx.types.bool),

compiler/rustc_codegen_ssa/src/mir/rvalue.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -631,12 +631,8 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
631631
.bytes();
632632
bx.cx().const_usize(val)
633633
}
634-
mir::NullOp::UbChecks => {
635-
let val = bx.tcx().sess.ub_checks();
636-
bx.cx().const_bool(val)
637-
}
638-
mir::NullOp::ContractChecks => {
639-
let val = bx.tcx().sess.contract_checks();
634+
mir::NullOp::RuntimeChecks(kind) => {
635+
let val = kind.value(bx.tcx().sess);
640636
bx.cx().const_bool(val)
641637
}
642638
};

compiler/rustc_const_eval/src/check_consts/check.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -646,11 +646,7 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
646646
Rvalue::Cast(_, _, _) => {}
647647

648648
Rvalue::NullaryOp(
649-
NullOp::SizeOf
650-
| NullOp::AlignOf
651-
| NullOp::OffsetOf(_)
652-
| NullOp::UbChecks
653-
| NullOp::ContractChecks,
649+
NullOp::SizeOf | NullOp::AlignOf | NullOp::OffsetOf(_) | NullOp::RuntimeChecks(_),
654650
_,
655651
) => {}
656652
Rvalue::ShallowInitBox(_, _) => {}

compiler/rustc_const_eval/src/interpret/machine.rs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -298,11 +298,11 @@ pub trait Machine<'tcx>: Sized {
298298
interp_ok(())
299299
}
300300

301-
/// Determines the result of a `NullaryOp::UbChecks` invocation.
302-
fn ub_checks(_ecx: &InterpCx<'tcx, Self>) -> InterpResult<'tcx, bool>;
303-
304-
/// Determines the result of a `NullaryOp::ContractChecks` invocation.
305-
fn contract_checks(_ecx: &InterpCx<'tcx, Self>) -> InterpResult<'tcx, bool>;
301+
/// Determines the result of a `NullaryOp::RuntimeChecks` invocation.
302+
fn runtime_checks(
303+
_ecx: &InterpCx<'tcx, Self>,
304+
r: mir::RuntimeChecks,
305+
) -> InterpResult<'tcx, bool>;
306306

307307
/// Called when the interpreter encounters a `StatementKind::ConstEvalCounter` instruction.
308308
/// You can use this to detect long or endlessly running programs.
@@ -681,14 +681,10 @@ pub macro compile_time_machine(<$tcx: lifetime>) {
681681
}
682682

683683
#[inline(always)]
684-
fn ub_checks(_ecx: &InterpCx<$tcx, Self>) -> InterpResult<$tcx, bool> {
685-
// We can't look at `tcx.sess` here as that can differ across crates, which can lead to
686-
// unsound differences in evaluating the same constant at different instantiation sites.
687-
interp_ok(true)
688-
}
689-
690-
#[inline(always)]
691-
fn contract_checks(_ecx: &InterpCx<$tcx, Self>) -> InterpResult<$tcx, bool> {
684+
fn runtime_checks(
685+
_ecx: &InterpCx<$tcx, Self>,
686+
_r: mir::RuntimeChecks,
687+
) -> InterpResult<$tcx, bool> {
692688
// We can't look at `tcx.sess` here as that can differ across crates, which can lead to
693689
// unsound differences in evaluating the same constant at different instantiation sites.
694690
interp_ok(true)

compiler/rustc_const_eval/src/interpret/operator.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -536,8 +536,7 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
536536
self.tcx.offset_of_subfield(self.typing_env, layout, fields.iter()).bytes();
537537
ImmTy::from_uint(val, usize_layout())
538538
}
539-
UbChecks => ImmTy::from_bool(M::ub_checks(self)?, *self.tcx),
540-
ContractChecks => ImmTy::from_bool(M::contract_checks(self)?, *self.tcx),
539+
RuntimeChecks(r) => ImmTy::from_bool(M::runtime_checks(self, r)?, *self.tcx),
541540
})
542541
}
543542
}

compiler/rustc_hir_analysis/src/check/intrinsic.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ fn intrinsic_operation_unsafety(tcx: TyCtxt<'_>, intrinsic_id: LocalDefId) -> hi
163163
| sym::minnumf128
164164
| sym::mul_with_overflow
165165
| sym::needs_drop
166+
| sym::overflow_checks
166167
| sym::powf16
167168
| sym::powf32
168169
| sym::powf64
@@ -643,7 +644,7 @@ pub(crate) fn check_intrinsic_type(
643644
sym::aggregate_raw_ptr => (3, 0, vec![param(1), param(2)], param(0)),
644645
sym::ptr_metadata => (2, 0, vec![Ty::new_imm_ptr(tcx, param(0))], param(1)),
645646

646-
sym::ub_checks => (0, 0, Vec::new(), tcx.types.bool),
647+
sym::ub_checks | sym::overflow_checks => (0, 0, Vec::new(), tcx.types.bool),
647648

648649
sym::box_new => (1, 0, vec![param(0)], Ty::new_box(tcx, param(0))),
649650

compiler/rustc_middle/src/mir/mod.rs

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

650650
match rvalue {
651-
Rvalue::NullaryOp(NullOp::UbChecks, _) => Some((tcx.sess.ub_checks() as u128, targets)),
651+
Rvalue::NullaryOp(NullOp::RuntimeChecks(kind), _) => {
652+
Some((kind.value(tcx.sess) as u128, targets))
653+
}
652654
Rvalue::Use(Operand::Constant(constant)) => {
653655
let bits = eval_mono_const(constant)?;
654656
Some((bits, targets))

compiler/rustc_middle/src/mir/pretty.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1095,8 +1095,13 @@ impl<'tcx> Debug for Rvalue<'tcx> {
10951095
NullOp::SizeOf => write!(fmt, "SizeOf({t})"),
10961096
NullOp::AlignOf => write!(fmt, "AlignOf({t})"),
10971097
NullOp::OffsetOf(fields) => write!(fmt, "OffsetOf({t}, {fields:?})"),
1098-
NullOp::UbChecks => write!(fmt, "UbChecks()"),
1099-
NullOp::ContractChecks => write!(fmt, "ContractChecks()"),
1098+
NullOp::RuntimeChecks(RuntimeChecks::UbChecks) => write!(fmt, "UbChecks()"),
1099+
NullOp::RuntimeChecks(RuntimeChecks::ContractChecks) => {
1100+
write!(fmt, "ContractChecks()")
1101+
}
1102+
NullOp::RuntimeChecks(RuntimeChecks::OverflowChecks) => {
1103+
write!(fmt, "OverflowChecks()")
1104+
}
11001105
}
11011106
}
11021107
ThreadLocalRef(did) => ty::tls::with(|tcx| {

compiler/rustc_middle/src/mir/statement.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -785,8 +785,7 @@ impl<'tcx> Rvalue<'tcx> {
785785
Rvalue::NullaryOp(NullOp::SizeOf | NullOp::AlignOf | NullOp::OffsetOf(..), _) => {
786786
tcx.types.usize
787787
}
788-
Rvalue::NullaryOp(NullOp::ContractChecks, _)
789-
| Rvalue::NullaryOp(NullOp::UbChecks, _) => tcx.types.bool,
788+
Rvalue::NullaryOp(NullOp::RuntimeChecks(_), _) => tcx.types.bool,
790789
Rvalue::Aggregate(ref ak, ref ops) => match **ak {
791790
AggregateKind::Array(ty) => Ty::new_array(tcx, ty, ops.len() as u64),
792791
AggregateKind::Tuple => {
@@ -854,7 +853,7 @@ impl<'tcx> NullOp<'tcx> {
854853
pub fn ty(&self, tcx: TyCtxt<'tcx>) -> Ty<'tcx> {
855854
match self {
856855
NullOp::SizeOf | NullOp::AlignOf | NullOp::OffsetOf(_) => tcx.types.usize,
857-
NullOp::UbChecks | NullOp::ContractChecks => tcx.types.bool,
856+
NullOp::RuntimeChecks(_) => tcx.types.bool,
858857
}
859858
}
860859
}

0 commit comments

Comments
 (0)