Skip to content

Add spin_loop_hint intrinsic #58870

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
11 changes: 2 additions & 9 deletions src/libcore/hint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,6 @@ pub unsafe fn unreachable_unchecked() -> ! {
#[inline]
#[unstable(feature = "renamed_spin_loop", issue = "55002")]
pub fn spin_loop() {
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
unsafe {
asm!("pause" ::: "memory" : "volatile");
}

#[cfg(target_arch = "aarch64")]
unsafe {
asm!("yield" ::: "memory" : "volatile");
}
#[cfg(not(stage0))]
intrinsics::spin_loop_hint();
}
3 changes: 3 additions & 0 deletions src/libcore/intrinsics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1308,6 +1308,9 @@ extern "rust-intrinsic" {
/// Emits a `!nontemporal` store according to LLVM (see their docs).
/// Probably will never become stable.
pub fn nontemporal_store<T>(ptr: *mut T, val: T);

#[cfg(not(stage0))]
pub fn spin_loop_hint();
}

mod real_intrinsics {
Expand Down
24 changes: 23 additions & 1 deletion src/librustc_codegen_llvm/intrinsic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use rustc::ty::{self, Ty};
use rustc::ty::layout::{self, LayoutOf, HasTyCtxt, Primitive};
use rustc_codegen_ssa::common::{IntPredicate, TypeKind};
use rustc::hir;
use syntax::ast::{self, FloatTy};
use syntax::ast::{self, FloatTy, AsmDialect};
use syntax::symbol::Symbol;

use rustc_codegen_ssa::traits::*;
Expand All @@ -28,6 +28,7 @@ use rustc::session::Session;
use syntax_pos::Span;

use std::cmp::Ordering;
use std::ffi::CStr;
use std::{iter, i128, u128};

fn get_simple_intrinsic(cx: &CodegenCx<'ll, '_>, name: &str) -> Option<&'ll Value> {
Expand Down Expand Up @@ -689,6 +690,27 @@ impl IntrinsicCallMethods<'tcx> for Builder<'a, 'll, 'tcx> {
return;
}

"spin_loop_hint" => {
Copy link
Member

Choose a reason for hiding this comment

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

This can (should) be implemented as a call to @llvm.x86.sse2.pause() on x86/x86_64.

Copy link
Member

Choose a reason for hiding this comment

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

(i.e. __mm_pause on x86 and an equivalent on Aarch still needs to be implemented(?))

let asm = match &*self.tcx().sess.target.target.arch {
"x86" | "x86_64" => b"pause\0",
"aarch64" => b"yield\0",
_ => return,
};
let r = self.inline_asm_call(
CStr::from_bytes_with_nul(asm).unwrap(),
CStr::from_bytes_with_nul(b"r,~{memory}\0").unwrap(),
&[],
self.type_void(),
true,
false,
AsmDialect::Att,
);
if r.is_none() {
bug!("broken spin_loop_hint");
}
return;
}

_ => bug!("unknown intrinsic '{}'", name),
};

Expand Down
5 changes: 4 additions & 1 deletion src/librustc_typeck/check/intrinsic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ pub fn intrisic_operation_unsafety(intrinsic: &str) -> hir::Unsafety {
"overflowing_add" | "overflowing_sub" | "overflowing_mul" |
"saturating_add" | "saturating_sub" |
"rotate_left" | "rotate_right" |
"ctpop" | "ctlz" | "cttz" | "bswap" | "bitreverse"
"ctpop" | "ctlz" | "cttz" | "bswap" | "bitreverse" |
"spin_loop_hint"
=> hir::Unsafety::Normal,
_ => hir::Unsafety::Unsafe,
}
Expand Down Expand Up @@ -379,6 +380,8 @@ pub fn check_intrinsic_type<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
(1, vec![ tcx.mk_mut_ptr(param(0)), param(0) ], tcx.mk_unit())
}

"spin_loop_hint" => (0, vec![], tcx.mk_unit()),

ref other => {
struct_span_err!(tcx.sess, it.span, E0093,
"unrecognized intrinsic function: `{}`",
Expand Down