Skip to content

Commit e475f40

Browse files
committed
Likely unlikely fix
1 parent 4010980 commit e475f40

File tree

1 file changed

+40
-8
lines changed

1 file changed

+40
-8
lines changed

core/src/intrinsics/mod.rs

+40-8
Original file line numberDiff line numberDiff line change
@@ -1465,6 +1465,22 @@ pub const unsafe fn assume(b: bool) {
14651465
}
14661466
}
14671467

1468+
/// Hints to the compiler that current code path is cold.
1469+
///
1470+
/// Note that, unlike most intrinsics, this is safe to call;
1471+
/// it does not require an `unsafe` block.
1472+
/// Therefore, implementations must not require the user to uphold
1473+
/// any safety invariants.
1474+
///
1475+
/// This intrinsic does not have a stable counterpart.
1476+
#[unstable(feature = "core_intrinsics", issue = "none")]
1477+
#[cfg_attr(not(bootstrap), rustc_intrinsic)]
1478+
#[cfg(not(bootstrap))]
1479+
#[rustc_nounwind]
1480+
#[miri::intrinsic_fallback_is_spec]
1481+
#[cold]
1482+
pub const fn cold_path() {}
1483+
14681484
/// Hints to the compiler that branch condition is likely to be true.
14691485
/// Returns the value passed to it.
14701486
///
@@ -1480,13 +1496,21 @@ pub const unsafe fn assume(b: bool) {
14801496
bootstrap,
14811497
rustc_const_stable(feature = "const_likely", since = "CURRENT_RUSTC_VERSION")
14821498
)]
1483-
#[cfg_attr(not(bootstrap), rustc_const_stable_intrinsic)]
14841499
#[unstable(feature = "core_intrinsics", issue = "none")]
1485-
#[rustc_intrinsic]
14861500
#[rustc_nounwind]
1487-
#[miri::intrinsic_fallback_is_spec]
1501+
#[inline(always)]
14881502
pub const fn likely(b: bool) -> bool {
1489-
b
1503+
#[cfg(bootstrap)]
1504+
{
1505+
b
1506+
}
1507+
#[cfg(not(bootstrap))]
1508+
if b {
1509+
true
1510+
} else {
1511+
cold_path();
1512+
false
1513+
}
14901514
}
14911515

14921516
/// Hints to the compiler that branch condition is likely to be false.
@@ -1504,13 +1528,21 @@ pub const fn likely(b: bool) -> bool {
15041528
bootstrap,
15051529
rustc_const_stable(feature = "const_likely", since = "CURRENT_RUSTC_VERSION")
15061530
)]
1507-
#[cfg_attr(not(bootstrap), rustc_const_stable_intrinsic)]
15081531
#[unstable(feature = "core_intrinsics", issue = "none")]
1509-
#[rustc_intrinsic]
15101532
#[rustc_nounwind]
1511-
#[miri::intrinsic_fallback_is_spec]
1533+
#[inline(always)]
15121534
pub const fn unlikely(b: bool) -> bool {
1513-
b
1535+
#[cfg(bootstrap)]
1536+
{
1537+
b
1538+
}
1539+
#[cfg(not(bootstrap))]
1540+
if b {
1541+
cold_path();
1542+
true
1543+
} else {
1544+
false
1545+
}
15141546
}
15151547

15161548
/// Returns either `true_val` or `false_val` depending on condition `b` with a

0 commit comments

Comments
 (0)