Skip to content

Commit d746383

Browse files
committed
Make hint::cold_path #[cold] so that it works even if the MIR inliner can't inline it
1 parent 54333ff commit d746383

2 files changed

Lines changed: 21 additions & 0 deletions

File tree

library/core/src/hint.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -778,6 +778,9 @@ pub const fn unlikely(b: bool) -> bool {
778778
#[stable(feature = "cold_path", since = "1.95.0")]
779779
#[rustc_const_stable(feature = "cold_path", since = "1.95.0")]
780780
#[inline(always)]
781+
// Even if for some reason the cold_path intrinsic is not visible to codegen, the coldness will
782+
// ensure that branches this is in are still known to be cold.
783+
#[cold]
781784
pub const fn cold_path() {
782785
crate::intrinsics::cold_path()
783786
}

tests/codegen-llvm/hint/cold_path.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,22 @@ pub fn test2(x: i32) {
5050
// CHECK: path_a
5151
}
5252

53+
#[no_mangle]
54+
#[target_feature(enable = "sse2")]
55+
pub fn with_target_feature(x: bool) {
56+
if x {
57+
path_a();
58+
} else {
59+
cold_path();
60+
path_b();
61+
}
62+
63+
// CHECK-LABEL: @with_target_feature(
64+
// CHECK: br i1 %x, label %bb1, label %bb2, !prof ![[NUM]]
65+
// CHECK: bb2:
66+
// CHECK: path_b
67+
// CHECK: bb1:
68+
// CHECK: path_a
69+
}
70+
5371
// CHECK: ![[NUM]] = !{!"branch_weights", {{(!"expected", )?}}i32 2000, i32 1}

0 commit comments

Comments
 (0)