Skip to content

Commit 0fdbf3c

Browse files
committed
More basic codegen cleanup
1 parent 1769ee1 commit 0fdbf3c

7 files changed

+45
-63
lines changed

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;

library/core/src/intrinsics.rs

+23-26
Original file line numberDiff line numberDiff line change
@@ -2614,29 +2614,30 @@ pub const unsafe fn is_val_statically_known<T: Copy>(_arg: T) -> bool {
26142614
macro_rules! assert_unsafe_precondition {
26152615
($name:expr, $([$($tt:tt)*])?($($i:ident:$ty:ty),*$(,)?) => $e:expr $(,)?) => {
26162616
{
2617+
// allow non_snake_case to allow capturing const generics
2618+
#[allow(non_snake_case)]
2619+
#[inline(always)]
2620+
fn runtime$(<$($tt)*>)?($($i:$ty),*) {
2621+
#[cfg(miri)]
2622+
return;
2623+
if !$e {
2624+
// don't unwind to reduce impact on code size
2625+
::core::panicking::panic_nounwind(
2626+
concat!("unsafe precondition(s) violated: ", $name)
2627+
);
2628+
}
2629+
}
2630+
#[allow(non_snake_case)]
2631+
#[inline]
2632+
const fn comptime$(<$($tt)*>)?($(_:$ty),*) {}
2633+
26172634
#[cfg(bootstrap)]
2618-
let should_check = cfg!(debug_assertions);
2635+
if cfg!(debug_assertions) {
2636+
::core::intrinsics::const_eval_select(($($i,)*), comptime, runtime);
2637+
}
26192638

2620-
// Turn assertions off in Miri, but otherwise check in codegen
26212639
#[cfg(not(bootstrap))]
2622-
let should_check = !cfg!(miri) && ::core::intrinsics::debug_assertions();
2623-
2624-
if should_check {
2625-
// allow non_snake_case to allow capturing const generics
2626-
#[allow(non_snake_case)]
2627-
#[inline(always)]
2628-
fn runtime$(<$($tt)*>)?($($i:$ty),*) {
2629-
if !$e {
2630-
// don't unwind to reduce impact on code size
2631-
::core::panicking::panic_nounwind(
2632-
concat!("unsafe precondition(s) violated: ", $name)
2633-
);
2634-
}
2635-
}
2636-
#[allow(non_snake_case)]
2637-
#[inline]
2638-
const fn comptime$(<$($tt)*>)?($(_:$ty),*) {}
2639-
2640+
if ::core::intrinsics::debug_assertions() {
26402641
::core::intrinsics::const_eval_select(($($i,)*), comptime, runtime);
26412642
}
26422643
}
@@ -2648,22 +2649,18 @@ pub(crate) use assert_unsafe_precondition;
26482649
/// `align_of::<T>()`.
26492650
#[inline]
26502651
pub(crate) fn is_aligned_and_not_null<T>(ptr: *const T) -> bool {
2651-
let mask = const { crate::mem::align_of::<T>() - 1 };
2652-
let is_aligned = (ptr.addr() & mask) == 0;
2653-
let not_null = ptr.addr() != 0;
2654-
is_aligned && not_null
2652+
((ptr.addr() & const { crate::mem::align_of::<T>() - 1 }) == 0) & (ptr.addr() != 0)
26552653
}
26562654

26572655
/*
26582656
/// Checks whether an allocation of `len` instances of `T` exceeds
26592657
/// the maximum allowed allocation size.
26602658
#[inline]
26612659
pub(crate) fn is_valid_allocation_size<T>(len: usize) -> bool {
2662-
let max_len = const {
2660+
len <= const {
26632661
let size = crate::mem::size_of::<T>();
26642662
if size == 0 { usize::MAX } else { isize::MAX as usize / size }
26652663
};
2666-
len <= max_len
26672664
}
26682665
*/
26692666

tests/mir-opt/inline/unwrap_unchecked.unwrap_unchecked.Inline.panic-abort.diff

+3-6
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,10 @@
1414
+ }
1515
+ scope 3 {
1616
+ scope 5 (inlined unreachable_unchecked) {
17+
+ let mut _5: bool;
1718
+ scope 6 {
18-
+ let _5: bool;
19-
+ scope 7 {
20-
+ debug should_check => _5;
21-
+ scope 8 (inlined unreachable_unchecked::runtime) {
22-
+ let _6: !;
23-
+ }
19+
+ scope 7 (inlined unreachable_unchecked::runtime) {
20+
+ let _6: !;
2421
+ }
2522
+ }
2623
+ }

tests/mir-opt/inline/unwrap_unchecked.unwrap_unchecked.Inline.panic-unwind.diff

+3-6
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,10 @@
1414
+ }
1515
+ scope 3 {
1616
+ scope 5 (inlined unreachable_unchecked) {
17+
+ let mut _5: bool;
1718
+ scope 6 {
18-
+ let _5: bool;
19-
+ scope 7 {
20-
+ debug should_check => _5;
21-
+ scope 8 (inlined unreachable_unchecked::runtime) {
22-
+ let _6: !;
23-
+ }
19+
+ scope 7 (inlined unreachable_unchecked::runtime) {
20+
+ let _6: !;
2421
+ }
2522
+ }
2623
+ }

tests/mir-opt/inline/unwrap_unchecked.unwrap_unchecked.PreCodegen.after.panic-abort.mir

+3-6
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,10 @@ fn unwrap_unchecked(_1: Option<T>) -> T {
1212
}
1313
scope 3 {
1414
scope 5 (inlined unreachable_unchecked) {
15+
let mut _3: bool;
1516
scope 6 {
16-
let _3: bool;
17-
scope 7 {
18-
debug should_check => _3;
19-
scope 8 (inlined unreachable_unchecked::runtime) {
20-
let _4: !;
21-
}
17+
scope 7 (inlined unreachable_unchecked::runtime) {
18+
let _4: !;
2219
}
2320
}
2421
}

tests/mir-opt/inline/unwrap_unchecked.unwrap_unchecked.PreCodegen.after.panic-unwind.mir

+3-6
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,10 @@ fn unwrap_unchecked(_1: Option<T>) -> T {
1212
}
1313
scope 3 {
1414
scope 5 (inlined unreachable_unchecked) {
15+
let mut _3: bool;
1516
scope 6 {
16-
let _3: bool;
17-
scope 7 {
18-
debug should_check => _3;
19-
scope 8 (inlined unreachable_unchecked::runtime) {
20-
let _4: !;
21-
}
17+
scope 7 (inlined unreachable_unchecked::runtime) {
18+
let _4: !;
2219
}
2320
}
2421
}

tests/mir-opt/pre-codegen/duplicate_switch_targets.ub_if_b.PreCodegen.after.mir

+3-6
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,10 @@ fn ub_if_b(_1: Thing) -> Thing {
55
let mut _0: Thing;
66
let mut _2: isize;
77
scope 1 (inlined unreachable_unchecked) {
8+
let mut _3: bool;
89
scope 2 {
9-
let _3: bool;
10-
scope 3 {
11-
debug should_check => _3;
12-
scope 4 (inlined unreachable_unchecked::runtime) {
13-
let _4: !;
14-
}
10+
scope 3 (inlined unreachable_unchecked::runtime) {
11+
let _4: !;
1512
}
1613
}
1714
}

0 commit comments

Comments
 (0)