Skip to content

Commit ee4f791

Browse files
committed
Auto merge of rust-lang#120594 - saethlin:delayed-debug-asserts, r=<try>
Toggle assert_unsafe_precondition in codegen instead of expansion r? `@ghost` rust-lang#120539 (comment)
2 parents f067fd6 + 3404820 commit ee4f791

File tree

54 files changed

+482
-602
lines changed

Some content is hidden

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

54 files changed

+482
-602
lines changed

compiler/rustc_borrowck/src/type_check/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1971,6 +1971,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
19711971
ConstraintCategory::SizedBound,
19721972
);
19731973
}
1974+
&Rvalue::NullaryOp(NullOp::DebugAssertions, _) => {}
19741975

19751976
Rvalue::ShallowInitBox(operand, ty) => {
19761977
self.check_operand(operand, location);

compiler/rustc_codegen_cranelift/src/base.rs

+9
Original file line numberDiff line numberDiff line change
@@ -767,6 +767,15 @@ fn codegen_stmt<'tcx>(
767767
NullOp::OffsetOf(fields) => {
768768
layout.offset_of_subfield(fx, fields.iter()).bytes()
769769
}
770+
NullOp::DebugAssertions => {
771+
let val = fx.tcx.sess.opts.debug_assertions;
772+
let val = CValue::by_val(
773+
fx.bcx.ins().iconst(types::I8, i64::try_from(val).unwrap()),
774+
fx.layout_of(fx.tcx.types.bool),
775+
);
776+
lval.write_cvalue(fx, val);
777+
return;
778+
}
770779
};
771780
let val = CValue::by_val(
772781
fx.bcx.ins().iconst(fx.pointer_type, i64::try_from(val).unwrap()),

compiler/rustc_codegen_ssa/src/mir/block.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -676,7 +676,6 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
676676
instance: Option<Instance<'tcx>>,
677677
source_info: mir::SourceInfo,
678678
target: Option<mir::BasicBlock>,
679-
unwind: mir::UnwindAction,
680679
mergeable_succ: bool,
681680
) -> Option<MergingSucc> {
682681
// Emit a panic or a no-op for `assert_*` intrinsics.
@@ -715,17 +714,19 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
715714
common::build_langcall(bx, Some(source_info.span), LangItem::PanicNounwind);
716715

717716
// Codegen the actual panic invoke/call.
718-
helper.do_call(
717+
let merging_succ = helper.do_call(
719718
self,
720719
bx,
721720
fn_abi,
722721
llfn,
723722
&[msg.0, msg.1],
724-
target.as_ref().map(|bb| (ReturnDest::Nothing, *bb)),
725-
unwind,
723+
None,
724+
mir::UnwindAction::Unreachable,
726725
&[],
727-
mergeable_succ,
728-
)
726+
false,
727+
);
728+
assert_eq!(merging_succ, MergingSucc::False);
729+
merging_succ
729730
} else {
730731
// a NOP
731732
let target = target.unwrap();
@@ -809,7 +810,6 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
809810
instance,
810811
source_info,
811812
target,
812-
unwind,
813813
mergeable_succ,
814814
) {
815815
return merging_succ;

compiler/rustc_codegen_ssa/src/mir/rvalue.rs

+10-4
Original file line numberDiff line numberDiff line change
@@ -672,17 +672,23 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
672672
let val = match null_op {
673673
mir::NullOp::SizeOf => {
674674
assert!(bx.cx().type_is_sized(ty));
675-
layout.size.bytes()
675+
let val = layout.size.bytes();
676+
bx.cx().const_usize(val)
676677
}
677678
mir::NullOp::AlignOf => {
678679
assert!(bx.cx().type_is_sized(ty));
679-
layout.align.abi.bytes()
680+
let val = layout.align.abi.bytes();
681+
bx.cx().const_usize(val)
680682
}
681683
mir::NullOp::OffsetOf(fields) => {
682-
layout.offset_of_subfield(bx.cx(), fields.iter()).bytes()
684+
let val = layout.offset_of_subfield(bx.cx(), fields.iter()).bytes();
685+
bx.cx().const_usize(val)
686+
}
687+
mir::NullOp::DebugAssertions => {
688+
let val = bx.tcx().sess.opts.debug_assertions;
689+
bx.cx().const_bool(val)
683690
}
684691
};
685-
let val = bx.cx().const_usize(val);
686692
let tcx = self.cx.tcx();
687693
OperandRef {
688694
val: OperandValue::Immediate(val),

compiler/rustc_codegen_ssa/src/mir/statement.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use rustc_middle::mir;
22
use rustc_middle::mir::NonDivergingIntrinsic;
3+
use rustc_session::config::OptLevel;
34

45
use super::FunctionCx;
56
use super::LocalRef;
@@ -67,8 +68,10 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
6768
self.codegen_coverage(bx, coverage, statement.source_info.scope);
6869
}
6970
mir::StatementKind::Intrinsic(box NonDivergingIntrinsic::Assume(ref op)) => {
70-
let op_val = self.codegen_operand(bx, op);
71-
bx.assume(op_val.immediate());
71+
if !matches!(bx.tcx().sess.opts.optimize, OptLevel::No | OptLevel::Less) {
72+
let op_val = self.codegen_operand(bx, op);
73+
bx.assume(op_val.immediate());
74+
}
7275
}
7376
mir::StatementKind::Intrinsic(box NonDivergingIntrinsic::CopyNonOverlapping(
7477
mir::CopyNonOverlapping { ref count, ref src, ref dst },

compiler/rustc_const_eval/src/const_eval/machine.rs

+1
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,7 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
536536
// (We know the value here in the machine of course, but this is the runtime of that code,
537537
// not the optimization stage.)
538538
sym::is_val_statically_known => ecx.write_scalar(Scalar::from_bool(false), dest)?,
539+
539540
_ => {
540541
throw_unsup_format!(
541542
"intrinsic `{intrinsic_name}` is not supported at compile-time"

compiler/rustc_const_eval/src/interpret/step.rs

+16-4
Original file line numberDiff line numberDiff line change
@@ -246,13 +246,25 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
246246
);
247247
}
248248
let val = match null_op {
249-
mir::NullOp::SizeOf => layout.size.bytes(),
250-
mir::NullOp::AlignOf => layout.align.abi.bytes(),
249+
mir::NullOp::SizeOf => {
250+
let val = layout.size.bytes();
251+
Scalar::from_target_usize(val, self)
252+
}
253+
mir::NullOp::AlignOf => {
254+
let val = layout.align.abi.bytes();
255+
Scalar::from_target_usize(val, self)
256+
}
251257
mir::NullOp::OffsetOf(fields) => {
252-
layout.offset_of_subfield(self, fields.iter()).bytes()
258+
let val = layout.offset_of_subfield(self, fields.iter()).bytes();
259+
Scalar::from_target_usize(val, self)
260+
}
261+
mir::NullOp::DebugAssertions => {
262+
// The checks hidden behind this are always better done by the interpreter
263+
// itself, because it knows the runtime state better.
264+
Scalar::from_bool(false)
253265
}
254266
};
255-
self.write_scalar(Scalar::from_target_usize(val, self), &dest)?;
267+
self.write_scalar(val, &dest)?;
256268
}
257269

258270
ShallowInitBox(ref operand, _) => {

compiler/rustc_const_eval/src/transform/check_consts/check.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,10 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
571571

572572
Rvalue::Cast(_, _, _) => {}
573573

574-
Rvalue::NullaryOp(NullOp::SizeOf | NullOp::AlignOf | NullOp::OffsetOf(_), _) => {}
574+
Rvalue::NullaryOp(
575+
NullOp::SizeOf | NullOp::AlignOf | NullOp::OffsetOf(_) | NullOp::DebugAssertions,
576+
_,
577+
) => {}
575578
Rvalue::ShallowInitBox(_, _) => {}
576579

577580
Rvalue::UnaryOp(_, operand) => {

compiler/rustc_const_eval/src/transform/validate.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1116,7 +1116,7 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
11161116
Rvalue::Repeat(_, _)
11171117
| Rvalue::ThreadLocalRef(_)
11181118
| Rvalue::AddressOf(_, _)
1119-
| Rvalue::NullaryOp(NullOp::SizeOf | NullOp::AlignOf, _)
1119+
| Rvalue::NullaryOp(NullOp::SizeOf | NullOp::AlignOf | NullOp::DebugAssertions, _)
11201120
| Rvalue::Discriminant(_) => {}
11211121
}
11221122
self.super_rvalue(rvalue, location);

compiler/rustc_hir_analysis/src/check/intrinsic.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,8 @@ pub fn intrinsic_operation_unsafety(tcx: TyCtxt<'_>, intrinsic_id: DefId) -> hir
112112
| sym::forget
113113
| sym::black_box
114114
| sym::variant_count
115-
| sym::ptr_mask => hir::Unsafety::Normal,
115+
| sym::ptr_mask
116+
| sym::debug_assertions => hir::Unsafety::Normal,
116117
_ => hir::Unsafety::Unsafe,
117118
};
118119

@@ -461,6 +462,8 @@ pub fn check_intrinsic_type(tcx: TyCtxt<'_>, it: &hir::ForeignItem<'_>) {
461462
(0, vec![Ty::new_imm_ptr(tcx, Ty::new_unit(tcx))], tcx.types.usize)
462463
}
463464

465+
sym::debug_assertions => (0, Vec::new(), tcx.types.bool),
466+
464467
other => {
465468
tcx.dcx().emit_err(UnrecognizedIntrinsicFunction { span: it.span, name: other });
466469
return;

compiler/rustc_middle/src/mir/pretty.rs

+1
Original file line numberDiff line numberDiff line change
@@ -907,6 +907,7 @@ impl<'tcx> Debug for Rvalue<'tcx> {
907907
NullOp::SizeOf => write!(fmt, "SizeOf({t})"),
908908
NullOp::AlignOf => write!(fmt, "AlignOf({t})"),
909909
NullOp::OffsetOf(fields) => write!(fmt, "OffsetOf({t}, {fields:?})"),
910+
NullOp::DebugAssertions => write!(fmt, "cfg!(debug_assertions)"),
910911
}
911912
}
912913
ThreadLocalRef(did) => ty::tls::with(|tcx| {

compiler/rustc_middle/src/mir/syntax.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1360,6 +1360,8 @@ pub enum NullOp<'tcx> {
13601360
AlignOf,
13611361
/// Returns the offset of a field
13621362
OffsetOf(&'tcx List<(VariantIdx, FieldIdx)>),
1363+
/// cfg!(debug_assertions), but expanded in codegen
1364+
DebugAssertions,
13631365
}
13641366

13651367
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]

compiler/rustc_middle/src/mir/tcx.rs

+1
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ impl<'tcx> Rvalue<'tcx> {
194194
Rvalue::NullaryOp(NullOp::SizeOf | NullOp::AlignOf | NullOp::OffsetOf(..), _) => {
195195
tcx.types.usize
196196
}
197+
Rvalue::NullaryOp(NullOp::DebugAssertions, _) => tcx.types.bool,
197198
Rvalue::Aggregate(ref ak, ref ops) => match **ak {
198199
AggregateKind::Array(ty) => Ty::new_array(tcx, ty, ops.len() as u64),
199200
AggregateKind::Tuple => {

compiler/rustc_mir_dataflow/src/move_paths/builder.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,10 @@ impl<'b, 'a, 'tcx, F: Fn(Ty<'tcx>) -> bool> Gatherer<'b, 'a, 'tcx, F> {
425425
| Rvalue::AddressOf(..)
426426
| Rvalue::Discriminant(..)
427427
| Rvalue::Len(..)
428-
| Rvalue::NullaryOp(NullOp::SizeOf | NullOp::AlignOf | NullOp::OffsetOf(..), _) => {}
428+
| Rvalue::NullaryOp(
429+
NullOp::SizeOf | NullOp::AlignOf | NullOp::OffsetOf(..) | NullOp::DebugAssertions,
430+
_,
431+
) => {}
429432
}
430433
}
431434

compiler/rustc_mir_transform/src/const_prop_lint.rs

+1
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
638638
NullOp::OffsetOf(fields) => {
639639
op_layout.offset_of_subfield(self, fields.iter()).bytes()
640640
}
641+
NullOp::DebugAssertions => return None,
641642
};
642643
ImmTy::from_scalar(Scalar::from_target_usize(val, self), layout).into()
643644
}

compiler/rustc_mir_transform/src/gvn.rs

+1
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,7 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
489489
NullOp::OffsetOf(fields) => {
490490
layout.offset_of_subfield(&self.ecx, fields.iter()).bytes()
491491
}
492+
NullOp::DebugAssertions => return None,
492493
};
493494
let usize_layout = self.ecx.layout_of(self.tcx.types.usize).unwrap();
494495
let imm = ImmTy::try_from_uint(val, usize_layout)?;

compiler/rustc_mir_transform/src/lower_intrinsics.rs

+11
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,17 @@ impl<'tcx> MirPass<'tcx> for LowerIntrinsics {
2121
sym::unreachable => {
2222
terminator.kind = TerminatorKind::Unreachable;
2323
}
24+
sym::debug_assertions => {
25+
let target = target.unwrap();
26+
block.statements.push(Statement {
27+
source_info: terminator.source_info,
28+
kind: StatementKind::Assign(Box::new((
29+
*destination,
30+
Rvalue::NullaryOp(NullOp::DebugAssertions, tcx.types.bool),
31+
))),
32+
});
33+
terminator.kind = TerminatorKind::Goto { target };
34+
}
2435
sym::forget => {
2536
if let Some(target) = *target {
2637
block.statements.push(Statement {

compiler/rustc_mir_transform/src/promote_consts.rs

+1
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,7 @@ impl<'tcx> Validator<'_, 'tcx> {
446446
NullOp::SizeOf => {}
447447
NullOp::AlignOf => {}
448448
NullOp::OffsetOf(_) => {}
449+
NullOp::DebugAssertions => {}
449450
},
450451

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

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

+1
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@ impl<'tcx> Stable<'tcx> for mir::NullOp<'tcx> {
257257
OffsetOf(indices) => stable_mir::mir::NullOp::OffsetOf(
258258
indices.iter().map(|idx| idx.stable(tables)).collect(),
259259
),
260+
DebugAssertions => stable_mir::mir::NullOp::DebugAssertions,
260261
}
261262
}
262263
}

compiler/stable_mir/src/mir/body.rs

+3
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,7 @@ impl Rvalue {
639639
Rvalue::NullaryOp(NullOp::SizeOf | NullOp::AlignOf | NullOp::OffsetOf(..), _) => {
640640
Ok(Ty::usize_ty())
641641
}
642+
Rvalue::NullaryOp(NullOp::DebugAssertions, _) => Ok(Ty::bool_ty()),
642643
Rvalue::Aggregate(ak, ops) => match *ak {
643644
AggregateKind::Array(ty) => Ty::try_new_array(ty, ops.len() as u64),
644645
AggregateKind::Tuple => Ok(Ty::new_tuple(
@@ -1005,6 +1006,8 @@ pub enum NullOp {
10051006
AlignOf,
10061007
/// Returns the offset of a field.
10071008
OffsetOf(Vec<(VariantIdx, FieldIdx)>),
1009+
/// cfg!(debug_assertions), but at codegen time
1010+
DebugAssertions,
10081011
}
10091012

10101013
impl Operand {

0 commit comments

Comments
 (0)