Skip to content

Commit efc02ee

Browse files
committed
Auto merge of rust-lang#132662 - RalfJung:const-panic-inlining, r=<try>
tweak attributes for const panic macro Let's do some random mutations of this macro to see if that can re-gain the perf lost in rust-lang#132542.
2 parents 4d215e2 + 5382586 commit efc02ee

File tree

5 files changed

+40
-23
lines changed

5 files changed

+40
-23
lines changed

library/core/src/char/methods.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1774,6 +1774,7 @@ const fn len_utf16(code: u32) -> usize {
17741774
#[cfg_attr(bootstrap, rustc_const_stable(feature = "const_char_encode_utf8", since = "1.83.0"))]
17751775
#[doc(hidden)]
17761776
#[inline]
1777+
#[rustc_allow_const_fn_unstable(const_eval_select)] // just used for panics
17771778
pub const fn encode_utf8_raw(code: u32, dst: &mut [u8]) -> &mut [u8] {
17781779
let len = len_utf8(code);
17791780
match (len, &mut *dst) {
@@ -1826,6 +1827,7 @@ pub const fn encode_utf8_raw(code: u32, dst: &mut [u8]) -> &mut [u8] {
18261827
)]
18271828
#[doc(hidden)]
18281829
#[inline]
1830+
#[rustc_allow_const_fn_unstable(const_eval_select)] // just used for panics
18291831
pub const fn encode_utf16_raw(mut code: u32, dst: &mut [u16]) -> &mut [u16] {
18301832
let len = len_utf16(code);
18311833
match (len, &mut *dst) {

library/core/src/intrinsics.rs

+23-2
Original file line numberDiff line numberDiff line change
@@ -2965,14 +2965,35 @@ pub(crate) macro const_eval_select {
29652965
$(#[$compiletime_attr:meta])* $compiletime:block
29662966
else
29672967
$(#[$runtime_attr:meta])* $runtime:block
2968+
) => {
2969+
// Use the `noinline` arm, after adding explicit `inline` attributes
2970+
$crate::intrinsics::const_eval_select!(
2971+
@capture { $($arg : $ty = $val),* } $(-> $ret)? :
2972+
#[noinline]
2973+
if const
2974+
#[inline] // prevent codegen on this function
2975+
$(#[$compiletime_attr])*
2976+
$compiletime
2977+
else
2978+
#[inline] // avoid the overhead of an extra fn call
2979+
$(#[$runtime_attr])*
2980+
$runtime
2981+
)
2982+
},
2983+
// With a leading #[noinline], we don't add inline attributes
2984+
(
2985+
@capture { $($arg:ident : $ty:ty = $val:expr),* $(,)? } $( -> $ret:ty )? :
2986+
#[noinline]
2987+
if const
2988+
$(#[$compiletime_attr:meta])* $compiletime:block
2989+
else
2990+
$(#[$runtime_attr:meta])* $runtime:block
29682991
) => {{
2969-
#[inline] // avoid the overhead of an extra fn call
29702992
$(#[$runtime_attr])*
29712993
fn runtime($($arg: $ty),*) $( -> $ret )? {
29722994
$runtime
29732995
}
29742996

2975-
#[inline] // prevent codegen on this function
29762997
$(#[$compiletime_attr])*
29772998
const fn compiletime($($arg: $ty),*) $( -> $ret )? {
29782999
// Don't warn if one of the arguments is unused.

library/core/src/num/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1457,6 +1457,7 @@ pub const fn can_not_overflow<T>(radix: u32, is_signed_ty: bool, digits: &[u8])
14571457
#[cfg_attr(feature = "panic_immediate_abort", inline)]
14581458
#[cold]
14591459
#[track_caller]
1460+
#[rustc_allow_const_fn_unstable(const_eval_select)] // just used for panics
14601461
const fn from_str_radix_panic(radix: u32) -> ! {
14611462
const_panic!(
14621463
"from_str_radix_int: must lie in the range `[2, 36]`",

library/core/src/panic.rs

+11-21
Original file line numberDiff line numberDiff line change
@@ -201,27 +201,17 @@ pub unsafe trait PanicPayload: crate::fmt::Display {
201201
#[unstable(feature = "panic_internals", issue = "none")]
202202
#[doc(hidden)]
203203
pub macro const_panic {
204-
($const_msg:literal, $runtime_msg:literal, $($arg:ident : $ty:ty = $val:expr),* $(,)?) => {{
205-
// Wrap call to `const_eval_select` in a function so that we can
206-
// add the `rustc_allow_const_fn_unstable`. This is okay to do
207-
// because both variants will panic, just with different messages.
208-
#[rustc_allow_const_fn_unstable(const_eval_select)]
209-
#[inline(always)]
210-
#[track_caller]
211-
#[cfg_attr(bootstrap, rustc_const_stable(feature = "const_panic", since = "CURRENT_RUSTC_VERSION"))]
212-
const fn do_panic($($arg: $ty),*) -> ! {
213-
$crate::intrinsics::const_eval_select!(
214-
@capture { $($arg: $ty),* } -> !:
215-
if const #[track_caller] {
216-
$crate::panic!($const_msg)
217-
} else #[track_caller] {
218-
$crate::panic!($runtime_msg)
219-
}
220-
)
221-
}
222-
223-
do_panic($($val),*)
224-
}},
204+
($const_msg:literal, $runtime_msg:literal, $($arg:ident : $ty:ty = $val:expr),* $(,)?) => {
205+
$crate::intrinsics::const_eval_select!(
206+
@capture { $($arg: $ty = $val),* } -> !:
207+
#[noinline]
208+
if const #[track_caller] {
209+
$crate::panic!($const_msg)
210+
} else #[track_caller] {
211+
$crate::panic!($runtime_msg)
212+
}
213+
)
214+
},
225215
// We support leaving away the `val` expressions for *all* arguments
226216
// (but not for *some* arguments, that's too tricky).
227217
($const_msg:literal, $runtime_msg:literal, $($arg:ident : $ty:ty),* $(,)?) => {

library/core/src/slice/index.rs

+3
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ where
3131
#[cfg_attr(not(feature = "panic_immediate_abort"), inline(never), cold)]
3232
#[cfg_attr(feature = "panic_immediate_abort", inline)]
3333
#[track_caller]
34+
#[rustc_allow_const_fn_unstable(const_eval_select)] // just used for panics
3435
const fn slice_start_index_len_fail(index: usize, len: usize) -> ! {
3536
const_panic!(
3637
"slice start index is out of range for slice",
@@ -43,6 +44,7 @@ const fn slice_start_index_len_fail(index: usize, len: usize) -> ! {
4344
#[cfg_attr(not(feature = "panic_immediate_abort"), inline(never), cold)]
4445
#[cfg_attr(feature = "panic_immediate_abort", inline)]
4546
#[track_caller]
47+
#[rustc_allow_const_fn_unstable(const_eval_select)] // just used for panics
4648
const fn slice_end_index_len_fail(index: usize, len: usize) -> ! {
4749
const_panic!(
4850
"slice end index is out of range for slice",
@@ -55,6 +57,7 @@ const fn slice_end_index_len_fail(index: usize, len: usize) -> ! {
5557
#[cfg_attr(not(feature = "panic_immediate_abort"), inline(never), cold)]
5658
#[cfg_attr(feature = "panic_immediate_abort", inline)]
5759
#[track_caller]
60+
#[rustc_allow_const_fn_unstable(const_eval_select)] // just used for panics
5861
const fn slice_index_order_fail(index: usize, end: usize) -> ! {
5962
const_panic!(
6063
"slice index start is larger than end",

0 commit comments

Comments
 (0)