Skip to content

Commit a6d0986

Browse files
committed
transpile: switch __builtin_arm_yield from spin_loop to __yield
`__yield` is the more direct, unstable equivalent of `__builtin_arm_yield`. Fixes 1st half of <#1263 (comment)>.
1 parent 31d9ccf commit a6d0986

File tree

12 files changed

+71
-54
lines changed

12 files changed

+71
-54
lines changed

c2rust-transpile/src/translator/builtins.rs

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -380,14 +380,9 @@ impl<'c> Translation<'c> {
380380
})
381381
}
382382

383-
"__builtin_ia32_pause" | "__builtin_arm_yield" => {
383+
"__builtin_ia32_pause" => {
384384
// `spin_loop()` is implemented as `_mm_pause()` (the `pause` instruction) on `x86`/`x86_64`,
385385
// but it's the safe and cross-platform version of it, so prefer it.
386-
// On `arm`, it's implemented as `yield`, although on `aarch64`,
387-
// it's implemented as `isb` instead as this is more efficient.
388-
// See <https://github.com/rust-lang/rust/commit/c064b6560b7ce0adeb9bbf5d7dcf12b1acb0c807>.
389-
// `core::arch::aarch64::__yield()` could be used instead,
390-
// but it's unstable (`#![feature(stdarch_arm_hints)]`), so it's not ideal.
391386
let spin_loop = mk().abs_path_expr(vec!["core", "hint", "spin_loop"]);
392387
let call = mk().call_expr(spin_loop, vec![]);
393388
self.convert_side_effects_expr(
@@ -397,6 +392,26 @@ impl<'c> Translation<'c> {
397392
)
398393
}
399394

395+
"__builtin_arm_yield" => {
396+
let fn_name = "__yield";
397+
self.use_feature("stdsimd");
398+
// TODO In Rust 1.88 (probably earlier), `#![feature(stdsimd)]`
399+
// has been removed and split into (at least):
400+
// `#![feature("stdarch_arm_hints")]` and
401+
// `#![cfg_attr(target_arch = "arm", feature(stdarch_arm_neon_intrinsics))]`.
402+
// self.use_feature("stdarch_arm_hints");
403+
// self.use_feature("stdarch_arm_neon_intrinsics"); // TODO need to add `cfg_attr` support.
404+
self.import_arch_function("arm", fn_name);
405+
self.import_arch_function("aarch64", fn_name);
406+
let ident = mk().ident_expr(fn_name);
407+
let call = mk().call_expr(ident, vec![]);
408+
self.convert_side_effects_expr(
409+
ctx,
410+
WithStmts::new_val(call),
411+
"Builtin is not supposed to be used",
412+
)
413+
}
414+
400415
// SIMD builtins:
401416
"__builtin_ia32_aeskeygenassist128" => {
402417
self.convert_simd_builtin(ctx, "_mm_aeskeygenassist_si128", args)

c2rust-transpile/src/translator/simd.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,13 @@ impl<'c> Translation<'c> {
147147
})
148148
}
149149

150+
/// Import a function from [`core::arch`] with a `#[cfg(target_arch = "{arch_name})]`.
151+
pub fn import_arch_function(&self, arch_name: &str, name: &str) {
152+
self.with_cur_file_item_store(|item_store| {
153+
add_arch_use(item_store, arch_name, name);
154+
});
155+
}
156+
150157
/// Determine if a particular function name is an SIMD primitive. If so an appropriate
151158
/// use statement is generated, `true` is returned, and no further processing will need to be done.
152159
pub fn import_simd_function(&self, name: &str) -> TranslationResult<bool> {

c2rust-transpile/tests/snapshots/arch-specific/dummy2.aarch64.rs

Lines changed: 0 additions & 9 deletions
This file was deleted.

c2rust-transpile/tests/snapshots/arch-specific/dummy2.c

Whitespace-only changes.

c2rust-transpile/tests/snapshots/arch-specific/dummy2.x86_64.rs

Lines changed: 0 additions & 9 deletions
This file was deleted.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#![allow(
2+
dead_code,
3+
mutable_transmutes,
4+
non_camel_case_types,
5+
non_snake_case,
6+
non_upper_case_globals,
7+
unused_assignments,
8+
unused_mut
9+
)]
10+
#![feature(stdsimd)]
11+
#[cfg(target_arch = "arm")]
12+
pub use core::arch::arm::__yield;
13+
#[cfg(target_arch = "aarch64")]
14+
pub use core::arch::aarch64::__yield;
15+
#[no_mangle]
16+
pub unsafe extern "C" fn spin() {
17+
__yield();
18+
}

c2rust-transpile/tests/snapshots/[email protected]

Lines changed: 0 additions & 14 deletions
This file was deleted.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
---
2+
source: c2rust-transpile/tests/snapshots.rs
3+
expression: cat tests/snapshots/arch-specific/spin.aarch64.rs
4+
input_file: c2rust-transpile/tests/snapshots/arch-specific/spin.c
5+
---
6+
#![allow(
7+
dead_code,
8+
mutable_transmutes,
9+
non_camel_case_types,
10+
non_snake_case,
11+
non_upper_case_globals,
12+
unused_assignments,
13+
unused_mut
14+
)]
15+
#![feature(stdsimd)]
16+
#[cfg(target_arch = "arm")]
17+
pub use core::arch::arm::__yield;
18+
#[cfg(target_arch = "aarch64")]
19+
pub use core::arch::aarch64::__yield;
20+
#[no_mangle]
21+
pub unsafe extern "C" fn spin() {
22+
__yield();
23+
}

0 commit comments

Comments
 (0)