Remove the explicit branch hint from const_panic

This commit is contained in:
Ben Kimock
2025-12-25 16:07:32 -05:00
parent 82dd3cb008
commit 315646a7a0
3 changed files with 22 additions and 31 deletions
+3 -25
View File
@@ -2402,8 +2402,7 @@ pub const fn const_eval_select<ARG: Tuple, F, G, RET>(
/// The `@capture` block declares which surrounding variables / expressions can be
/// used inside the `if const`.
/// Note that the two arms of this `if` really each become their own function, which is why the
/// macro supports setting attributes for those functions. The runtime function is always
/// marked as `#[inline]`.
/// macro supports setting attributes for those functions. Both functions are marked as `#[inline]`.
///
/// See [`const_eval_select()`] for the rules and requirements around that intrinsic.
pub(crate) macro const_eval_select {
@@ -2413,35 +2412,14 @@ pub const fn const_eval_select<ARG: Tuple, F, G, RET>(
$(#[$compiletime_attr:meta])* $compiletime:block
else
$(#[$runtime_attr:meta])* $runtime:block
) => {
// Use the `noinline` arm, after adding explicit `inline` attributes
$crate::intrinsics::const_eval_select!(
@capture$([$($binders)*])? { $($arg : $ty = $val),* } $(-> $ret)? :
#[noinline]
if const
#[inline] // prevent codegen on this function
$(#[$compiletime_attr])*
$compiletime
else
#[inline] // avoid the overhead of an extra fn call
$(#[$runtime_attr])*
$runtime
)
},
// With a leading #[noinline], we don't add inline attributes
(
@capture$([$($binders:tt)*])? { $($arg:ident : $ty:ty = $val:expr),* $(,)? } $( -> $ret:ty )? :
#[noinline]
if const
$(#[$compiletime_attr:meta])* $compiletime:block
else
$(#[$runtime_attr:meta])* $runtime:block
) => {{
#[inline]
$(#[$runtime_attr])*
fn runtime$(<$($binders)*>)?($($arg: $ty),*) $( -> $ret )? {
$runtime
}
#[inline]
$(#[$compiletime_attr])*
const fn compiletime$(<$($binders)*>)?($($arg: $ty),*) $( -> $ret )? {
// Don't warn if one of the arguments is unused.
+3 -4
View File
@@ -166,10 +166,9 @@ fn as_str(&mut self) -> Option<&str> {
const fn do_panic($($arg: $ty),*) -> ! {
$crate::intrinsics::const_eval_select!(
@capture { $($arg: $ty = $arg),* } -> !:
#[noinline]
if const #[track_caller] #[inline] { // Inline this, to prevent codegen
if const #[track_caller] {
$crate::panic!($const_msg)
} else #[track_caller] { // Do not inline this, it makes perf worse
} else #[track_caller] {
$crate::panic!($runtime_msg)
}
)
@@ -195,7 +194,7 @@ const fn do_panic($($arg: $ty),*) -> ! {
#[doc(hidden)]
pub macro const_assert {
($condition: expr, $const_msg:literal, $runtime_msg:literal, $($arg:tt)*) => {{
if !$crate::intrinsics::likely($condition) {
if !($condition) {
$crate::panic::const_panic!($const_msg, $runtime_msg, $($arg)*)
}
}}
+16 -2
View File
@@ -1,17 +1,31 @@
//@ compile-flags: -Copt-level=3
#![feature(panic_internals, const_eval_select, rustc_allow_const_fn_unstable, core_intrinsics)]
#![crate_type = "lib"]
// check that assert! and const_assert! emit branch weights
#[no_mangle]
pub fn test_assert(x: bool) {
assert!(x);
}
// check that assert! emits branch weights
// CHECK-LABEL: @test_assert(
// CHECK: br i1 %x, label %bb2, label %bb1, !prof ![[NUM:[0-9]+]]
// CHECK: bb1:
// CHECK: panic
// CHECK: bb2:
// CHECK: ret void
#[no_mangle]
pub fn test_const_assert(x: bool) {
core::panic::const_assert!(x, "", "",);
}
// CHECK-LABEL: @test_const_assert(
// CHECK: br i1 %x, label %bb2, label %bb1, !prof ![[NUM:[0-9]+]]
// CHECK: bb1:
// CHECK: panic
// CHECK: bb2:
// CHECK: ret void
// CHECK: ![[NUM]] = !{!"branch_weights", {{(!"expected", )?}}i32 2000, i32 1}