Skip to content

Commit e3c425b

Browse files
committed
Auto merge of rust-lang#132662 - RalfJung:const-panic-inlining, r=tgross35
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 75609d6 + 0820004 commit e3c425b

File tree

4 files changed

+34
-10
lines changed

4 files changed

+34
-10
lines changed

core/src/intrinsics/mod.rs

+23-2
Original file line numberDiff line numberDiff line change
@@ -2989,14 +2989,35 @@ pub(crate) macro const_eval_select {
29892989
$(#[$compiletime_attr:meta])* $compiletime:block
29902990
else
29912991
$(#[$runtime_attr:meta])* $runtime:block
2992+
) => {
2993+
// Use the `noinline` arm, after adding explicit `inline` attributes
2994+
$crate::intrinsics::const_eval_select!(
2995+
@capture { $($arg : $ty = $val),* } $(-> $ret)? :
2996+
#[noinline]
2997+
if const
2998+
#[inline] // prevent codegen on this function
2999+
$(#[$compiletime_attr])*
3000+
$compiletime
3001+
else
3002+
#[inline] // avoid the overhead of an extra fn call
3003+
$(#[$runtime_attr])*
3004+
$runtime
3005+
)
3006+
},
3007+
// With a leading #[noinline], we don't add inline attributes
3008+
(
3009+
@capture { $($arg:ident : $ty:ty = $val:expr),* $(,)? } $( -> $ret:ty )? :
3010+
#[noinline]
3011+
if const
3012+
$(#[$compiletime_attr:meta])* $compiletime:block
3013+
else
3014+
$(#[$runtime_attr:meta])* $runtime:block
29923015
) => {{
2993-
#[inline] // avoid the overhead of an extra fn call
29943016
$(#[$runtime_attr])*
29953017
fn runtime($($arg: $ty),*) $( -> $ret )? {
29963018
$runtime
29973019
}
29983020

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

core/src/num/f128.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1268,8 +1268,9 @@ impl f128 {
12681268
min <= max,
12691269
"min > max, or either was NaN",
12701270
"min > max, or either was NaN. min = {min:?}, max = {max:?}",
1271-
min: f128,
1272-
max: f128,
1271+
// FIXME(f16_f128): Passed by-ref to avoid codegen crashes
1272+
min: &f128 = &min,
1273+
max: &f128 = &max,
12731274
);
12741275

12751276
if self < min {

core/src/num/f16.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1243,8 +1243,9 @@ impl f16 {
12431243
min <= max,
12441244
"min > max, or either was NaN",
12451245
"min > max, or either was NaN. min = {min:?}, max = {max:?}",
1246-
min: f16,
1247-
max: f16,
1246+
// FIXME(f16_f128): Passed by-ref to avoid codegen crashes
1247+
min: &f16 = &min,
1248+
max: &f16 = &max,
12481249
);
12491250

12501251
if self < min {

core/src/panic.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -206,15 +206,16 @@ pub macro const_panic {
206206
// add the `rustc_allow_const_fn_unstable`. This is okay to do
207207
// because both variants will panic, just with different messages.
208208
#[rustc_allow_const_fn_unstable(const_eval_select)]
209-
#[inline(always)]
209+
#[inline(always)] // inline the wrapper
210210
#[track_caller]
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),* } -> !:
215-
if const #[track_caller] {
214+
@capture { $($arg: $ty = $arg),* } -> !:
215+
#[noinline]
216+
if const #[track_caller] #[inline] { // Inline this, to prevent codegen
216217
$crate::panic!($const_msg)
217-
} else #[track_caller] {
218+
} else #[track_caller] { // Do not inline this, it makes perf worse
218219
$crate::panic!($runtime_msg)
219220
}
220221
)

0 commit comments

Comments
 (0)