Skip to content

Commit 95a8ac2

Browse files
authored
Unrolled build for #156863
Rollup merge of #156863 - saethlin:silly-cold-path, r=jackh726 Make hint::cold_path #[cold] so that it works even if the MIR inliner can't inline it This fixes #156859 because the branch weight metadata is actually keyed off seeing a `#[cold]` function call in an arm. We don't need the intrinsic, any `#[cold]` function will do. So if the hint wrapper itself is made `#[cold]`, we will still get the desired effect, and LLVM will clean up the call to an empty function when optimizations are enabled.
2 parents c58275e + 54569c6 commit 95a8ac2

2 files changed

Lines changed: 59 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
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
//@ compile-flags: -Copt-level=3
2+
//@ only-x86_64
3+
#![crate_type = "lib"]
4+
5+
// This test checks that hint::cold_path still works in #[target_feature] functions.
6+
7+
use std::hint::cold_path;
8+
9+
#[inline(never)]
10+
#[no_mangle]
11+
pub fn path_a() {
12+
println!("path a");
13+
}
14+
15+
#[inline(never)]
16+
#[no_mangle]
17+
pub fn path_b() {
18+
println!("path b");
19+
}
20+
21+
#[no_mangle]
22+
pub fn test1(x: bool) {
23+
if x {
24+
path_a();
25+
} else {
26+
cold_path();
27+
path_b();
28+
}
29+
30+
// CHECK-LABEL: @test1(
31+
// CHECK: br i1 %x, label %bb1, label %bb2, !prof ![[NUM:[0-9]+]]
32+
// CHECK: bb2:
33+
// CHECK: path_b
34+
// CHECK: bb1:
35+
// CHECK: path_a
36+
}
37+
38+
#[no_mangle]
39+
#[target_feature(enable = "sse2")]
40+
pub fn with_target_feature(x: bool) {
41+
if x {
42+
path_a();
43+
} else {
44+
cold_path();
45+
path_b();
46+
}
47+
48+
// CHECK-LABEL: @with_target_feature(
49+
// CHECK: br i1 %x, label %bb1, label %bb2, !prof ![[NUM]]
50+
// CHECK: bb2:
51+
// CHECK: path_b
52+
// CHECK: bb1:
53+
// CHECK: path_a
54+
}
55+
56+
// CHECK: ![[NUM]] = !{!"branch_weights", {{(!"expected", )?}}i32 2000, i32 1}

0 commit comments

Comments
 (0)