Skip to content

Commit 4183df4

Browse files
committed
Introduce Operand::RuntimeChecks.
1 parent d4b2f18 commit 4183df4

File tree

45 files changed

+167
-148
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+167
-148
lines changed

compiler/rustc_borrowck/src/diagnostics/explain_borrow.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -763,6 +763,7 @@ impl<'tcx> MirBorrowckCtxt<'_, '_, 'tcx> {
763763
{
764764
// Just point to the function, to reduce the chance of overlapping spans.
765765
let function_span = match func {
766+
Operand::RuntimeChecks(_) => span,
766767
Operand::Constant(c) => c.span,
767768
Operand::Copy(place) | Operand::Move(place) => {
768769
if let Some(l) = place.as_local() {
@@ -808,6 +809,7 @@ impl<'tcx> MirBorrowckCtxt<'_, '_, 'tcx> {
808809
{
809810
// Just point to the function, to reduce the chance of overlapping spans.
810811
let function_span = match func {
812+
Operand::RuntimeChecks(_) => span,
811813
Operand::Constant(c) => c.span,
812814
Operand::Copy(place) | Operand::Move(place) => {
813815
if let Some(l) = place.as_local() {

compiler/rustc_borrowck/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1695,7 +1695,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, '_, 'tcx> {
16951695
_ => propagate_closure_used_mut_place(self, place),
16961696
}
16971697
}
1698-
Operand::Constant(..) => {}
1698+
Operand::Constant(..) | Operand::RuntimeChecks(_) => {}
16991699
}
17001700
}
17011701

@@ -1746,7 +1746,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, '_, 'tcx> {
17461746
state,
17471747
);
17481748
}
1749-
Operand::Constant(_) => {}
1749+
Operand::Constant(_) | Operand::RuntimeChecks(_) => {}
17501750
}
17511751
}
17521752

compiler/rustc_borrowck/src/polonius/legacy/loan_invalidations.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ impl<'a, 'tcx> LoanInvalidationsGenerator<'a, 'tcx> {
247247
LocalMutationIsAllowed::Yes,
248248
);
249249
}
250-
Operand::Constant(_) => {}
250+
Operand::Constant(_) | Operand::RuntimeChecks(_) => {}
251251
}
252252
}
253253

compiler/rustc_borrowck/src/type_check/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1023,7 +1023,7 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
10231023
// element, so we require the `Copy` trait.
10241024
if len.try_to_target_usize(tcx).is_none_or(|len| len > 1) {
10251025
match operand {
1026-
Operand::Copy(..) | Operand::Constant(..) => {
1026+
Operand::Copy(..) | Operand::Constant(..) | Operand::RuntimeChecks(_) => {
10271027
// These are always okay: direct use of a const, or a value that can
10281028
// evidently be copied.
10291029
}

compiler/rustc_codegen_cranelift/src/base.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ use rustc_ast::InlineAsmOptions;
88
use rustc_codegen_ssa::base::is_call_from_compiler_builtins_to_upstream_monomorphization;
99
use rustc_data_structures::profiling::SelfProfilerRef;
1010
use rustc_index::IndexVec;
11-
use rustc_middle::ty::TypeVisitableExt;
1211
use rustc_middle::ty::adjustment::PointerCoercion;
1312
use rustc_middle::ty::layout::FnAbiOf;
1413
use rustc_middle::ty::print::with_no_trimmed_paths;
14+
use rustc_middle::ty::{ScalarInt, TypeVisitableExt};
1515
use rustc_session::config::OutputFilenames;
1616
use rustc_span::Symbol;
1717

@@ -1039,6 +1039,12 @@ pub(crate) fn codegen_operand<'tcx>(
10391039
cplace.to_cvalue(fx)
10401040
}
10411041
Operand::Constant(const_) => crate::constant::codegen_constant_operand(fx, const_),
1042+
Operand::RuntimeChecks(checks) => {
1043+
let int = checks.value(fx.tcx.sess);
1044+
let int = ScalarInt::try_from_uint(int, Size::from_bits(1)).unwrap();
1045+
let layout = fx.layout_of(fx.tcx.types.bool);
1046+
return CValue::const_val(fx, layout, int);
1047+
}
10421048
}
10431049
}
10441050

compiler/rustc_codegen_cranelift/src/constant.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -215,11 +215,6 @@ pub(crate) fn codegen_const_value<'tcx>(
215215
CValue::by_val(val, layout)
216216
}
217217
},
218-
ConstValue::RuntimeChecks(checks) => {
219-
let int = checks.value(fx.tcx.sess);
220-
let int = ScalarInt::try_from_uint(int, Size::from_bits(1)).unwrap();
221-
return CValue::const_val(fx, layout, int);
222-
}
223218
ConstValue::Indirect { alloc_id, offset } => CValue::by_ref(
224219
Pointer::new(pointer_for_allocation(fx, alloc_id))
225220
.offset_i64(fx, i64::try_from(offset.bytes()).unwrap()),
@@ -545,6 +540,10 @@ pub(crate) fn mir_operand_get_const_val<'tcx>(
545540
operand: &Operand<'tcx>,
546541
) -> Option<ScalarInt> {
547542
match operand {
543+
Operand::RuntimeChecks(checks) => {
544+
let int = checks.value(fx.tcx.sess);
545+
ScalarInt::try_from_uint(int, Size::from_bits(1))
546+
}
548547
Operand::Constant(const_) => eval_mir_constant(fx, const_).0.try_to_scalar_int(),
549548
// FIXME(rust-lang/rust#85105): Casts like `IMM8 as u32` result in the const being stored
550549
// inside a temporary before being passed to the intrinsic requiring the const argument.

compiler/rustc_codegen_ssa/src/mir/operand.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -165,14 +165,6 @@ impl<'a, 'tcx, V: CodegenObject> OperandRef<'tcx, V> {
165165
let llval = bx.scalar_to_backend(x, scalar, bx.immediate_backend_type(layout));
166166
OperandValue::Immediate(llval)
167167
}
168-
mir::ConstValue::RuntimeChecks(checks) => {
169-
let BackendRepr::Scalar(scalar) = layout.backend_repr else {
170-
bug!("from_const: invalid ByVal layout: {:#?}", layout);
171-
};
172-
let x = Scalar::from_bool(checks.value(bx.tcx().sess));
173-
let llval = bx.scalar_to_backend(x, scalar, bx.immediate_backend_type(layout));
174-
OperandValue::Immediate(llval)
175-
}
176168
ConstValue::ZeroSized => return OperandRef::zero_sized(layout),
177169
ConstValue::Slice { alloc_id, meta } => {
178170
let BackendRepr::ScalarPair(a_scalar, _) = layout.backend_repr else {
@@ -1060,6 +1052,17 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
10601052
OperandRef { move_annotation, ..self.codegen_consume(bx, place.as_ref()) }
10611053
}
10621054

1055+
mir::Operand::RuntimeChecks(checks) => {
1056+
let layout = bx.layout_of(bx.tcx().types.bool);
1057+
let BackendRepr::Scalar(scalar) = layout.backend_repr else {
1058+
bug!("from_const: invalid ByVal layout: {:#?}", layout);
1059+
};
1060+
let x = Scalar::from_bool(checks.value(bx.tcx().sess));
1061+
let llval = bx.scalar_to_backend(x, scalar, bx.immediate_backend_type(layout));
1062+
let val = OperandValue::Immediate(llval);
1063+
OperandRef { val, layout, move_annotation: None }
1064+
}
1065+
10631066
mir::Operand::Constant(ref constant) => {
10641067
let constant_ty = self.monomorphize(constant.ty());
10651068
// Most SIMD vector constants should be passed as immediates.

compiler/rustc_const_eval/src/check_consts/qualifs.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,7 @@ where
338338
Operand::Copy(place) | Operand::Move(place) => {
339339
return in_place::<Q, _>(cx, in_local, place.as_ref());
340340
}
341+
Operand::RuntimeChecks(_) => return Q::in_any_value_of_ty(cx, cx.tcx.types.bool),
341342

342343
Operand::Constant(c) => c,
343344
};

compiler/rustc_const_eval/src/const_eval/dummy_machine.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,13 @@ impl<'tcx> interpret::Machine<'tcx> for DummyMachine {
122122
unimplemented!()
123123
}
124124

125+
#[inline(always)]
126+
fn runtime_checks(_ecx: &InterpCx<'tcx, Self>, r: RuntimeChecks) -> InterpResult<'tcx, bool> {
127+
// We can't look at `tcx.sess` here as that can differ across crates, which can lead to
128+
// unsound differences in evaluating the same constant at different instantiation sites.
129+
panic!("compiletime machine evaluated {r:?}")
130+
}
131+
125132
fn binary_ptr_op(
126133
ecx: &InterpCx<'tcx, Self>,
127134
bin_op: BinOp,

compiler/rustc_const_eval/src/const_eval/machine.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,16 @@ impl<'tcx> interpret::Machine<'tcx> for CompileTimeMachine<'tcx> {
642642
Err(ConstEvalErrKind::AssertFailure(err)).into()
643643
}
644644

645+
#[inline(always)]
646+
fn runtime_checks(
647+
_ecx: &InterpCx<'tcx, Self>,
648+
_r: mir::RuntimeChecks,
649+
) -> InterpResult<'tcx, bool> {
650+
// We can't look at `tcx.sess` here as that can differ across crates, which can lead to
651+
// unsound differences in evaluating the same constant at different instantiation sites.
652+
interp_ok(true)
653+
}
654+
645655
fn binary_ptr_op(
646656
_ecx: &InterpCx<'tcx, Self>,
647657
_bin_op: mir::BinOp,

0 commit comments

Comments
 (0)