Skip to content

Commit f642b25

Browse files
Auto merge of #148766 - cjgillot:mir-const-runtime-checks, r=<try>
Replace Rvalue::NullaryOp by a variant in mir::Operand.
2 parents 7c2c3c0 + 06de909 commit f642b25

File tree

61 files changed

+246
-351
lines changed

Some content is hidden

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

61 files changed

+246
-351
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 & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1559,10 +1559,6 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, '_, 'tcx> {
15591559
self.consume_operand(location, (operand2, span), state);
15601560
}
15611561

1562-
Rvalue::NullaryOp(_op) => {
1563-
// nullary ops take no dynamic input; no borrowck effect.
1564-
}
1565-
15661562
Rvalue::Aggregate(aggregate_kind, operands) => {
15671563
// We need to report back the list of mutable upvars that were
15681564
// moved into the closure and subsequently used by the closure,
@@ -1699,7 +1695,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, '_, 'tcx> {
16991695
_ => propagate_closure_used_mut_place(self, place),
17001696
}
17011697
}
1702-
Operand::Constant(..) => {}
1698+
Operand::Constant(..) | Operand::RuntimeChecks(_) => {}
17031699
}
17041700
}
17051701

@@ -1750,7 +1746,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, '_, 'tcx> {
17501746
state,
17511747
);
17521748
}
1753-
Operand::Constant(_) => {}
1749+
Operand::Constant(_) | Operand::RuntimeChecks(_) => {}
17541750
}
17551751
}
17561752

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

Lines changed: 1 addition & 3 deletions
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

@@ -314,8 +314,6 @@ impl<'a, 'tcx> LoanInvalidationsGenerator<'a, 'tcx> {
314314
self.consume_operand(location, operand2);
315315
}
316316

317-
Rvalue::NullaryOp(_op) => {}
318-
319317
Rvalue::Aggregate(_, operands) => {
320318
for operand in operands {
321319
self.consume_operand(location, operand);

compiler/rustc_borrowck/src/type_check/mod.rs

Lines changed: 1 addition & 4 deletions
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
}
@@ -1046,8 +1046,6 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
10461046
}
10471047
}
10481048

1049-
&Rvalue::NullaryOp(NullOp::RuntimeChecks(_)) => {}
1050-
10511049
Rvalue::ShallowInitBox(_operand, ty) => {
10521050
let trait_ref =
10531051
ty::TraitRef::new(tcx, tcx.require_lang_item(LangItem::Sized, span), [*ty]);
@@ -2201,7 +2199,6 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
22012199
| Rvalue::Cast(..)
22022200
| Rvalue::ShallowInitBox(..)
22032201
| Rvalue::BinaryOp(..)
2204-
| Rvalue::NullaryOp(..)
22052202
| Rvalue::CopyForDeref(..)
22062203
| Rvalue::UnaryOp(..)
22072204
| Rvalue::Discriminant(..)

compiler/rustc_codegen_cranelift/src/base.rs

Lines changed: 8 additions & 13 deletions
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;
13-
use rustc_middle::ty::layout::{FnAbiOf, HasTypingEnv};
12+
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

@@ -853,17 +853,6 @@ fn codegen_stmt<'tcx>(fx: &mut FunctionCx<'_, '_, 'tcx>, cur_block: Block, stmt:
853853
fx.bcx.ins().nop();
854854
}
855855
}
856-
Rvalue::NullaryOp(ref null_op) => {
857-
assert!(lval.layout().ty.is_sized(fx.tcx, fx.typing_env()));
858-
let val = match null_op {
859-
NullOp::RuntimeChecks(kind) => kind.value(fx.tcx.sess),
860-
};
861-
let val = CValue::by_val(
862-
fx.bcx.ins().iconst(types::I8, i64::from(val)),
863-
fx.layout_of(fx.tcx.types.bool),
864-
);
865-
lval.write_cvalue(fx, val);
866-
}
867856
Rvalue::Aggregate(ref kind, ref operands)
868857
if matches!(**kind, AggregateKind::RawPtr(..)) =>
869858
{
@@ -1050,6 +1039,12 @@ pub(crate) fn codegen_operand<'tcx>(
10501039
cplace.to_cvalue(fx)
10511040
}
10521041
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+
}
10531048
}
10541049
}
10551050

compiler/rustc_codegen_cranelift/src/constant.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,10 @@ pub(crate) fn mir_operand_get_const_val<'tcx>(
540540
operand: &Operand<'tcx>,
541541
) -> Option<ScalarInt> {
542542
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+
}
543547
Operand::Constant(const_) => eval_mir_constant(fx, const_).0.try_to_scalar_int(),
544548
// FIXME(rust-lang/rust#85105): Casts like `IMM8 as u32` result in the const being stored
545549
// 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 & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1052,6 +1052,17 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
10521052
OperandRef { move_annotation, ..self.codegen_consume(bx, place.as_ref()) }
10531053
}
10541054

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+
10551066
mir::Operand::Constant(ref constant) => {
10561067
let constant_ty = self.monomorphize(constant.ty());
10571068
// Most SIMD vector constants should be passed as immediates.

compiler/rustc_codegen_ssa/src/mir/rvalue.rs

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -613,21 +613,6 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
613613
}
614614
}
615615

616-
mir::Rvalue::NullaryOp(ref null_op) => {
617-
let val = match null_op {
618-
mir::NullOp::RuntimeChecks(kind) => {
619-
let val = kind.value(bx.tcx().sess);
620-
bx.cx().const_bool(val)
621-
}
622-
};
623-
let tcx = self.cx.tcx();
624-
OperandRef {
625-
val: OperandValue::Immediate(val),
626-
layout: self.cx.layout_of(null_op.ty(tcx)),
627-
move_annotation: None,
628-
}
629-
}
630-
631616
mir::Rvalue::ThreadLocalRef(def_id) => {
632617
assert!(bx.cx().tcx().is_static(def_id));
633618
let layout = bx.layout_of(bx.cx().tcx().static_ptr_ty(def_id, bx.typing_env()));

compiler/rustc_const_eval/src/check_consts/check.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -645,7 +645,6 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
645645

646646
Rvalue::Cast(_, _, _) => {}
647647

648-
Rvalue::NullaryOp(NullOp::RuntimeChecks(_)) => {}
649648
Rvalue::ShallowInitBox(_, _) => {}
650649

651650
Rvalue::UnaryOp(op, operand) => {

compiler/rustc_const_eval/src/check_consts/qualifs.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -230,9 +230,7 @@ where
230230
F: FnMut(Local) -> bool,
231231
{
232232
match rvalue {
233-
Rvalue::ThreadLocalRef(_) | Rvalue::NullaryOp(..) => {
234-
Q::in_any_value_of_ty(cx, rvalue.ty(cx.body, cx.tcx))
235-
}
233+
Rvalue::ThreadLocalRef(_) => Q::in_any_value_of_ty(cx, rvalue.ty(cx.body, cx.tcx)),
236234

237235
Rvalue::Discriminant(place) => in_place::<Q, _>(cx, in_local, place.as_ref()),
238236

@@ -340,6 +338,7 @@ where
340338
Operand::Copy(place) | Operand::Move(place) => {
341339
return in_place::<Q, _>(cx, in_local, place.as_ref());
342340
}
341+
Operand::RuntimeChecks(_) => return Q::in_any_value_of_ty(cx, cx.tcx.types.bool),
343342

344343
Operand::Constant(c) => c,
345344
};

0 commit comments

Comments
 (0)