Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 3 additions & 25 deletions library/core/src/intrinsics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2402,8 +2402,7 @@ where
/// 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 {
Expand All @@ -2413,35 +2412,14 @@ pub(crate) macro const_eval_select {
$(#[$compiletime_attr:meta])* $compiletime:block
Comment thread
saethlin marked this conversation as resolved.
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.
Expand Down
7 changes: 3 additions & 4 deletions library/core/src/panic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,9 @@ pub macro const_panic {
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)
}
)
Expand All @@ -195,7 +194,7 @@ pub macro const_panic {
#[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)*)
}
}}
Expand Down
18 changes: 16 additions & 2 deletions tests/codegen-llvm/intrinsics/likely_assert.rs
Original file line number Diff line number Diff line change
@@ -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}
Loading