forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of rust-lang#120370 - x17jiri:likely_unlikely_fix, r=saethlin
Likely unlikely fix RFC 1131 ( rust-lang#26179 ) added likely/unlikely intrinsics, but they have been broken for a while: rust-lang#96276 , rust-lang#96275 , rust-lang#88767 . This PR tries to fix them. Changes: - added a new `cold_path()` intrinsic - `likely()` and `unlikely()` changed to regular functions implemented using `cold_path()`
- Loading branch information
Showing
22 changed files
with
256 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
The loop took around 1250ms | ||
The loop took around 1350ms | ||
(It's fine for this number to change when you `--bless` this test.) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
//@ compile-flags: -O | ||
#![crate_type = "lib"] | ||
#![feature(core_intrinsics)] | ||
|
||
use std::intrinsics::cold_path; | ||
|
||
#[no_mangle] | ||
pub fn test_cold_path(x: bool) { | ||
cold_path(); | ||
} | ||
|
||
// CHECK-LABEL: @test_cold_path( | ||
// CHECK-NOT: cold_path |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,35 @@ | ||
//@ compile-flags: -C no-prepopulate-passes -Copt-level=1 | ||
|
||
//@ compile-flags: -O | ||
#![crate_type = "lib"] | ||
#![feature(core_intrinsics)] | ||
|
||
use std::intrinsics::{likely, unlikely}; | ||
use std::intrinsics::likely; | ||
|
||
#[inline(never)] | ||
#[no_mangle] | ||
pub fn check_likely(x: i32, y: i32) -> Option<i32> { | ||
unsafe { | ||
// CHECK: call i1 @llvm.expect.i1(i1 %{{.*}}, i1 true) | ||
if likely(x == y) { None } else { Some(x + y) } | ||
} | ||
pub fn path_a() { | ||
println!("path a"); | ||
} | ||
|
||
#[inline(never)] | ||
#[no_mangle] | ||
pub fn path_b() { | ||
println!("path b"); | ||
} | ||
|
||
#[no_mangle] | ||
pub fn check_unlikely(x: i32, y: i32) -> Option<i32> { | ||
unsafe { | ||
// CHECK: call i1 @llvm.expect.i1(i1 %{{.*}}, i1 false) | ||
if unlikely(x == y) { None } else { Some(x + y) } | ||
pub fn test_likely(x: bool) { | ||
if likely(x) { | ||
path_a(); | ||
} else { | ||
path_b(); | ||
} | ||
} | ||
|
||
// CHECK-LABEL: @test_likely( | ||
// CHECK: br i1 %x, label %bb2, label %bb3, !prof ![[NUM:[0-9]+]] | ||
// CHECK: bb3: | ||
// CHECK-NOT: cold_path | ||
// CHECK: path_b | ||
// CHECK: bb2: | ||
// CHECK: path_a | ||
// CHECK: ![[NUM]] = !{!"branch_weights", {{(!"expected", )?}}i32 2000, i32 1} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
//@ compile-flags: -O | ||
#![crate_type = "lib"] | ||
|
||
#[no_mangle] | ||
pub fn test_assert(x: bool) { | ||
assert!(x); | ||
} | ||
|
||
// check that assert! emits branch weights | ||
|
||
// CHECK-LABEL: @test_assert( | ||
// CHECK: br i1 %x, label %bb2, label %bb1, !prof ![[NUM:[0-9]+]] | ||
// CHECK: bb1: | ||
// CHECK: panic | ||
// CHECK: bb2: | ||
// CHECK: ret void | ||
// CHECK: ![[NUM]] = !{!"branch_weights", {{(!"expected", )?}}i32 2000, i32 1} |
Oops, something went wrong.