Skip to content

core: add instrinsics for llvm.expect.i8-i64 #22883

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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
49 changes: 49 additions & 0 deletions src/libcore/intrinsics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,26 @@ extern "rust-intrinsic" {
/// own, or if it does not enable any significant optimizations.
pub fn assume(b: bool);

/// Inform the optimizer that `expected_val` is expected
/// (the most probable) value of val.
#[cfg(not(stage0))]
pub fn expect8(val: u8, expected_val: u8) -> u8;

/// Inform the optimizer that `expected_val` is expected
/// (the most probable) value of val.
#[cfg(not(stage0))]
pub fn expect16(val: u16, expected_val: u16) -> u16;

/// Inform the optimizer that `expected_val` is expected
/// (the most probable) value of val.
#[cfg(not(stage0))]
pub fn expect32(val: u32, expected_val: u32) -> u32;

/// Inform the optimizer that `expected_val` is expected
/// (the most probable) value of val.
#[cfg(not(stage0))]
pub fn expect64(val: u64, expected_val: u64) -> u64;

/// Execute a breakpoint trap, for inspection by a debugger.
pub fn breakpoint();

Expand Down Expand Up @@ -545,3 +565,32 @@ extern "rust-intrinsic" {
/// Performs checked `u64` multiplication.
pub fn u64_mul_with_overflow(x: u64, y: u64) -> (u64, bool);
}

/// Inform the optimizer that `expected_val` is expected
/// (the most probable) value of val.
#[cfg(stage0)]
pub unsafe fn expect8(val: u8, _expected_val: u8) -> u8 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I... expect... these aren't actually necessary to do the bootstrap?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't follow? Before i added cfg(stage0) versions, it complained during stage0 that these intrinsics weren't known by the compiler.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These functions aren't called during the bootstrap, so you can have the FFI declarations be #[cfg(not(stage0))] and just omit these altogether. (I.e. have core::intrinsics::expect* not exist in stage0 at all.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I see what you mean.

val
}

/// Inform the optimizer that `expected_val` is expected
/// (the most probable) value of val.
#[cfg(stage0)]
pub unsafe fn expect16(val: u16, _expected_val: u16) -> u16 {
val
}

/// Inform the optimizer that `expected_val` is expected
/// (the most probable) value of val.
#[cfg(stage0)]
pub unsafe fn expect32(val: u32, _expected_val: u32) -> u32 {
val
}

/// Inform the optimizer that `expected_val` is expected
/// (the most probable) value of val.
#[cfg(stage0)]
pub unsafe fn expect64(val: u64, _expected_val: u64) -> u64 {
val
}

4 changes: 4 additions & 0 deletions src/librustc_trans/trans/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -873,6 +873,10 @@ fn declare_intrinsic(ccx: &CrateContext, key: & &'static str) -> Option<ValueRef
ifn!("llvm.lifetime.end", fn(t_i64, i8p) -> void);

ifn!("llvm.expect.i1", fn(i1, i1) -> i1);
ifn!("llvm.expect.i8", fn(t_i8, t_i8) -> t_i8);
ifn!("llvm.expect.i16", fn(t_i16, t_i16) -> t_i16);
ifn!("llvm.expect.i32", fn(t_i32, t_i32) -> t_i32);
ifn!("llvm.expect.i64", fn(t_i64, t_i64) -> t_i64);
ifn!("llvm.assume", fn(i1) -> void);

// Some intrinsics were introduced in later versions of LLVM, but they have
Expand Down
4 changes: 4 additions & 0 deletions src/librustc_trans/trans/intrinsic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ pub fn get_simple_intrinsic(ccx: &CrateContext, item: &ast::ForeignItem) -> Opti
"bswap32" => "llvm.bswap.i32",
"bswap64" => "llvm.bswap.i64",
"assume" => "llvm.assume",
"expect8" => "llvm.assume.i8",
"expect16" => "llvm.assume.i16",
"expect32" => "llvm.assume.i32",
"expect64" => "llvm.assume.i64",
_ => return None
};
Some(ccx.get_intrinsic(&name))
Expand Down
5 changes: 5 additions & 0 deletions src/librustc_typeck/check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5434,6 +5434,11 @@ pub fn check_intrinsic_type(ccx: &CrateCtxt, it: &ast::ForeignItem) {

"assume" => (0, vec![tcx.types.bool], ty::mk_nil(tcx)),

"expect8" => (0, vec![tcx.types.u8, tcx.types.u8], tcx.types.u8),
"expect16" => (0, vec![tcx.types.u16, tcx.types.u16], tcx.types.u16),
"expect32" => (0, vec![tcx.types.u32, tcx.types.u32], tcx.types.u32),
"expect64" => (0, vec![tcx.types.u64, tcx.types.u64], tcx.types.u64),

ref other => {
span_err!(tcx.sess, it.span, E0093,
"unrecognized intrinsic function: `{}`", *other);
Expand Down
35 changes: 35 additions & 0 deletions src/test/run-pass/intrinsic-expect.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.


use std::intrinsics::expect8;

fn check8(x: u8) -> u8 {
let b = (x > 0) as u8;
if unsafe { expect8(b, 1) == 1 } {
10
} else {
5
}
}

fn main() {
let x = check8(1);
assert_eq!(x, 10);
let x = check8(2);
assert_eq!(x, 10);
let x = check8(5);
assert_eq!(x, 10);
let x = check8(10);
assert_eq!(x, 10);
let x = check8(-1);
assert_eq!(x, 5);

}