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
35 changes: 25 additions & 10 deletions c2rust-transpile/src/translator/builtins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,16 +380,31 @@ impl<'c> Translation<'c> {
})
}

"__builtin_ia32_pause" | "__builtin_arm_yield" => {
// `spin_loop()` is implemented as `_mm_pause()` (the `pause` instruction) on `x86`/`x86_64`,
// but it's the safe and cross-platform version of it, so prefer it.
// On `arm`, it's implemented as `yield`, although on `aarch64`,
// it's implemented as `isb` instead as this is more efficient.
// See <https://github.com/rust-lang/rust/commit/c064b6560b7ce0adeb9bbf5d7dcf12b1acb0c807>.
// `core::arch::aarch64::__yield()` could be used instead,
// but it's unstable (`#![feature(stdarch_arm_hints)]`), so it's not ideal.
let spin_loop = mk().abs_path_expr(vec!["core", "hint", "spin_loop"]);
let call = mk().call_expr(spin_loop, vec![]);
"__builtin_ia32_pause" => {
let fn_name = "_mm_pause";
self.import_simd_function(fn_name)?;
let ident = mk().ident_expr(fn_name);
let call = mk().call_expr(ident, vec![]);
self.convert_side_effects_expr(
ctx,
WithStmts::new_val(call),
"Builtin is not supposed to be used",
)
}

"__builtin_arm_yield" => {
let fn_name = "__yield";
self.use_feature("stdsimd");
// TODO See #1298.
// In Rust 1.7, `#![feature(stdsimd)]` was removed and split into (at least):
// `#![feature("stdarch_arm_hints")]` and
// `#![cfg_attr(target_arch = "arm", feature(stdarch_arm_neon_intrinsics))]`.
// self.use_feature("stdarch_arm_hints");
// self.use_feature("stdarch_arm_neon_intrinsics"); // TODO need to add `cfg_attr` support.
self.import_arch_function("arm", fn_name);
self.import_arch_function("aarch64", fn_name);
let ident = mk().ident_expr(fn_name);
let call = mk().call_expr(ident, vec![]);
self.convert_side_effects_expr(
ctx,
WithStmts::new_val(call),
Expand Down
7 changes: 7 additions & 0 deletions c2rust-transpile/src/translator/simd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,13 @@ impl<'c> Translation<'c> {
})
}

/// Import a function from [`core::arch`] with a `#[cfg(target_arch = "{arch_name})]`.
pub fn import_arch_function(&self, arch_name: &str, name: &str) {
self.with_cur_file_item_store(|item_store| {
add_arch_use(item_store, arch_name, name);
});
}

/// Determine if a particular function name is an SIMD primitive. If so an appropriate
/// use statement is generated, `true` is returned, and no further processing will need to be done.
pub fn import_simd_function(&self, name: &str) -> TranslationResult<bool> {
Expand Down

This file was deleted.

Empty file.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@
unused_assignments,
unused_mut
)]
#![feature(stdsimd)]
#[cfg(target_arch = "arm")]
pub use core::arch::arm::__yield;
#[cfg(target_arch = "aarch64")]
pub use core::arch::aarch64::__yield;
#[no_mangle]
pub unsafe extern "C" fn spin() {
::core::hint::spin_loop();
__yield();
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
---
source: c2rust-transpile/tests/snapshots.rs
expression: cat tests/snapshots/spin.rs
input_file: c2rust-transpile/tests/snapshots/spin.c
---
#![allow(
dead_code,
mutable_transmutes,
Expand All @@ -12,7 +7,12 @@ input_file: c2rust-transpile/tests/snapshots/spin.c
unused_assignments,
unused_mut
)]
#![feature(stdsimd)]
#[cfg(target_arch = "x86")]
pub use core::arch::x86::_mm_pause;
#[cfg(target_arch = "x86_64")]
pub use core::arch::x86_64::_mm_pause;
#[no_mangle]
pub unsafe extern "C" fn spin() {
::core::hint::spin_loop();
_mm_pause();
}
14 changes: 0 additions & 14 deletions c2rust-transpile/tests/snapshots/[email protected]

This file was deleted.

23 changes: 23 additions & 0 deletions c2rust-transpile/tests/snapshots/[email protected]
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
source: c2rust-transpile/tests/snapshots.rs
expression: cat tests/snapshots/arch-specific/spin.aarch64.rs
input_file: c2rust-transpile/tests/snapshots/arch-specific/spin.c
---
#![allow(
dead_code,
mutable_transmutes,
non_camel_case_types,
non_snake_case,
non_upper_case_globals,
unused_assignments,
unused_mut
)]
#![feature(stdsimd)]
#[cfg(target_arch = "arm")]
pub use core::arch::arm::__yield;
#[cfg(target_arch = "aarch64")]
pub use core::arch::aarch64::__yield;
#[no_mangle]
pub unsafe extern "C" fn spin() {
__yield();
}
14 changes: 0 additions & 14 deletions c2rust-transpile/tests/snapshots/[email protected]

This file was deleted.

23 changes: 23 additions & 0 deletions c2rust-transpile/tests/snapshots/[email protected]
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
source: c2rust-transpile/tests/snapshots.rs
expression: cat tests/snapshots/arch-specific/spin.x86_64.rs
input_file: c2rust-transpile/tests/snapshots/arch-specific/spin.c
---
#![allow(
dead_code,
mutable_transmutes,
non_camel_case_types,
non_snake_case,
non_upper_case_globals,
unused_assignments,
unused_mut
)]
#![feature(stdsimd)]
#[cfg(target_arch = "x86")]
pub use core::arch::x86::_mm_pause;
#[cfg(target_arch = "x86_64")]
pub use core::arch::x86_64::_mm_pause;
#[no_mangle]
pub unsafe extern "C" fn spin() {
_mm_pause();
}