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
3 changes: 3 additions & 0 deletions library/core/src/hint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -778,6 +778,9 @@ pub const fn unlikely(b: bool) -> bool {
#[stable(feature = "cold_path", since = "1.95.0")]
#[rustc_const_stable(feature = "cold_path", since = "1.95.0")]
#[inline(always)]
// Even if for some reason the cold_path intrinsic is not visible to codegen, the coldness will
// ensure that branches this is in are still known to be cold.
#[cold]
pub const fn cold_path() {
crate::intrinsics::cold_path()
}
Expand Down
56 changes: 56 additions & 0 deletions tests/codegen-llvm/hint/cold_path-target_feature.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
//@ compile-flags: -Copt-level=3
//@ only-x86_64
#![crate_type = "lib"]

// This test checks that hint::cold_path still works in #[target_feature] functions.

use std::hint::cold_path;

#[inline(never)]
#[no_mangle]
pub fn path_a() {
println!("path a");
}

#[inline(never)]
#[no_mangle]
pub fn path_b() {
println!("path b");
}

#[no_mangle]
pub fn test1(x: bool) {
if x {
path_a();
} else {
cold_path();
path_b();
}

// CHECK-LABEL: @test1(
// CHECK: br i1 %x, label %bb1, label %bb2, !prof ![[NUM:[0-9]+]]
// CHECK: bb2:
// CHECK: path_b
// CHECK: bb1:
// CHECK: path_a
}

#[no_mangle]
#[target_feature(enable = "sse2")]
pub fn with_target_feature(x: bool) {
if x {
path_a();
} else {
cold_path();
path_b();
}

// CHECK-LABEL: @with_target_feature(
// CHECK: br i1 %x, label %bb1, label %bb2, !prof ![[NUM]]
// CHECK: bb2:
// CHECK: path_b
// CHECK: bb1:
// CHECK: path_a
}

// CHECK: ![[NUM]] = !{!"branch_weights", {{(!"expected", )?}}i32 2000, i32 1}
Loading