Skip to content

Commit 000ccd6

Browse files
committed
Auto merge of #148766 - cjgillot:mir-const-runtime-checks, r=RalfJung,saethlin
Replace Rvalue::NullaryOp by a variant in mir::Operand. Based on #148151 This PR fully removes the MIR `Rvalue::NullaryOp`. After #148151, it was only useful for runtime checks like `ub_checks`, `contract_checks` and `overflow_checks`. These are "runtime" checks, boolean constants that may only be `true` in codegen. It depends on a rustc flag passed to codegen, so we need to represent those flags cross-crate. This PR replaces those runtime checks by special variants in MIR `ConstValue`. This allows code that expects constants to manipulate those as such, even if we may not always be able to evaluate them to actual scalars.
2 parents acfd264 + c67b99f commit 000ccd6

File tree

65 files changed

+1095
-401
lines changed

Some content is hidden

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

65 files changed

+1095
-401
lines changed

compiler/rustc_borrowck/src/diagnostics/explain_borrow.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -764,6 +764,7 @@ impl<'tcx> MirBorrowckCtxt<'_, '_, 'tcx> {
764764
{
765765
// Just point to the function, to reduce the chance of overlapping spans.
766766
let function_span = match func {
767+
Operand::RuntimeChecks(_) => span,
767768
Operand::Constant(c) => c.span,
768769
Operand::Copy(place) | Operand::Move(place) => {
769770
if let Some(l) = place.as_local() {
@@ -809,6 +810,7 @@ impl<'tcx> MirBorrowckCtxt<'_, '_, 'tcx> {
809810
{
810811
// Just point to the function, to reduce the chance of overlapping spans.
811812
let function_span = match func {
813+
Operand::RuntimeChecks(_) => span,
812814
Operand::Constant(c) => c.span,
813815
Operand::Copy(place) | Operand::Move(place) => {
814816
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]);
@@ -2276,7 +2274,6 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
22762274
| Rvalue::Cast(..)
22772275
| Rvalue::ShallowInitBox(..)
22782276
| Rvalue::BinaryOp(..)
2279-
| Rvalue::NullaryOp(..)
22802277
| Rvalue::CopyForDeref(..)
22812278
| Rvalue::UnaryOp(..)
22822279
| Rvalue::Discriminant(..)

compiler/rustc_codegen_cranelift/src/base.rs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use rustc_data_structures::profiling::SelfProfilerRef;
1010
use rustc_index::IndexVec;
1111
use rustc_middle::ty::TypeVisitableExt;
1212
use rustc_middle::ty::adjustment::PointerCoercion;
13-
use rustc_middle::ty::layout::{FnAbiOf, HasTypingEnv};
13+
use rustc_middle::ty::layout::FnAbiOf;
1414
use rustc_middle::ty::print::with_no_trimmed_paths;
1515
use rustc_session::config::OutputFilenames;
1616
use rustc_span::Symbol;
@@ -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,11 @@ 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 val = checks.value(fx.tcx.sess);
1044+
let layout = fx.layout_of(fx.tcx.types.bool);
1045+
return CValue::const_val(fx, layout, val.into());
1046+
}
10531047
}
10541048
}
10551049

compiler/rustc_codegen_cranelift/src/constant.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,7 @@ pub(crate) fn mir_operand_get_const_val<'tcx>(
540540
operand: &Operand<'tcx>,
541541
) -> Option<ScalarInt> {
542542
match operand {
543+
Operand::RuntimeChecks(checks) => Some(checks.value(fx.tcx.sess).into()),
543544
Operand::Constant(const_) => eval_mir_constant(fx, const_).0.try_to_scalar_int(),
544545
// FIXME(rust-lang/rust#85105): Casts like `IMM8 as u32` result in the const being stored
545546
// 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
@@ -1056,6 +1056,17 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
10561056
OperandRef { move_annotation, ..self.codegen_consume(bx, place.as_ref()) }
10571057
}
10581058

1059+
mir::Operand::RuntimeChecks(checks) => {
1060+
let layout = bx.layout_of(bx.tcx().types.bool);
1061+
let BackendRepr::Scalar(scalar) = layout.backend_repr else {
1062+
bug!("from_const: invalid ByVal layout: {:#?}", layout);
1063+
};
1064+
let x = Scalar::from_bool(checks.value(bx.tcx().sess));
1065+
let llval = bx.scalar_to_backend(x, scalar, bx.immediate_backend_type(layout));
1066+
let val = OperandValue::Immediate(llval);
1067+
OperandRef { val, layout, move_annotation: None }
1068+
}
1069+
10591070
mir::Operand::Constant(ref constant) => {
10601071
let constant_ty = self.monomorphize(constant.ty());
10611072
// 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
@@ -619,21 +619,6 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
619619
}
620620
}
621621

622-
mir::Rvalue::NullaryOp(ref null_op) => {
623-
let val = match null_op {
624-
mir::NullOp::RuntimeChecks(kind) => {
625-
let val = kind.value(bx.tcx().sess);
626-
bx.cx().const_bool(val)
627-
}
628-
};
629-
let tcx = self.cx.tcx();
630-
OperandRef {
631-
val: OperandValue::Immediate(val),
632-
layout: self.cx.layout_of(null_op.ty(tcx)),
633-
move_annotation: None,
634-
}
635-
}
636-
637622
mir::Rvalue::ThreadLocalRef(def_id) => {
638623
assert!(bx.cx().tcx().is_static(def_id));
639624
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)