rust/tests/codegen-llvm/hint/cold_path.rs
Trevor Gross 2e365985be Stabilize core::hint::cold_path
`cold_path` has been around unstably for a while and is a rather useful
tool to have. It does what it is supposed to and there are no known
remaining issues, so stabilize it here (including const).

Newly stable API:

    // in core::hint
    pub const fn cold_path();

I have opted to exclude `likely` and `unlikely` for now since they have
had some concerns about ease of use that `cold_path` doesn't suffer
from. `cold_path` is also significantly more flexible; in addition to
working with boolean `if` conditions, it can be used in `match` arms,
`if let`, closures, and other control flow blocks. `likely` and
`unlikely` are also possible to implement in user code via `cold_path`,
if desired.
2026-01-26 13:43:06 -06:00

53 lines
956 B
Rust

//@ compile-flags: -Copt-level=3
#![crate_type = "lib"]
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]
pub fn test2(x: i32) {
match x > 0 {
true => path_a(),
false => {
cold_path();
path_b()
}
}
// CHECK-LABEL: @test2(
// CHECK: br i1 %_2, label %bb2, label %bb1, !prof ![[NUM]]
// CHECK: bb1:
// CHECK: path_b
// CHECK: bb2:
// CHECK: path_a
}
// CHECK: ![[NUM]] = !{!"branch_weights", {{(!"expected", )?}}i32 2000, i32 1}