Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit ec08263

Browse files
committedNov 6, 2024·
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 116fc31 + 1d38a1d commit ec08263

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed
 

‎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/panic.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,8 @@ pub macro const_panic {
211211
#[cfg_attr(bootstrap, rustc_const_stable(feature = "const_panic", since = "CURRENT_RUSTC_VERSION"))]
212212
const fn do_panic($($arg: $ty),*) -> ! {
213213
$crate::intrinsics::const_eval_select!(
214-
@capture { $($arg: $ty),* } -> !:
214+
@capture { $($arg: $ty = $arg),* } -> !:
215+
#[noinline]
215216
if const #[track_caller] {
216217
$crate::panic!($const_msg)
217218
} else #[track_caller] {

0 commit comments

Comments
 (0)
Please sign in to comment.