Skip to content

Commit

Permalink
detect: Make macOS implementation available on other Apple targets
Browse files Browse the repository at this point in the history
  • Loading branch information
taiki-e committed Sep 7, 2024
1 parent babd04d commit d9968f0
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 15 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,10 @@ jobs:
os: windows-latest
- rust: nightly
target: aarch64-apple-darwin
os: macos-14 # AArch64
os: macos-latest # AArch64
- rust: nightly
target: aarch64-apple-ios-macabi
os: macos-latest # AArch64
- rust: '1.59' # LLVM 13
target: aarch64-unknown-linux-gnu
# - rust: '1.64' # LLVM 14
Expand Down
4 changes: 2 additions & 2 deletions src/imp/atomic128/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ Here is the table of targets that support run-time CPU feature detection and the
| aarch64 | android | getauxval | all | Enabled by default |
| aarch64 | freebsd | elf_aux_info | lse, lse2 | Enabled by default |
| aarch64 | netbsd | sysctl | all | Enabled by default |
| aarch64 | openbsd | sysctl | all | Enabled by default |
| aarch64 | macos | sysctl | all | Currently only used in tests because FEAT_LSE and FEAT_LSE2 are always available at compile-time. |
| aarch64 | openbsd | sysctlbyname | all | Enabled by default |
| aarch64 | macos/ios/tvos/watchos/visionos | sysctlbyname | all | Currently only used in tests because FEAT_LSE and FEAT_LSE2 are always available at compile-time. |
| aarch64 | windows | IsProcessorFeaturePresent | lse | Enabled by default |
| aarch64 | fuchsia | zx_system_get_features | lse | Enabled by default |
| powerpc64 | linux | getauxval | all | Disabled by default |
Expand Down
12 changes: 9 additions & 3 deletions src/imp/atomic128/aarch64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,15 @@ mod detect_aa64reg;
// mod detect_auxv;
#[cfg(test)]
#[cfg(not(portable_atomic_no_outline_atomics))]
#[cfg(target_os = "macos")]
#[path = "detect/aarch64_macos.rs"]
mod detect_macos;
#[cfg(any(
target_os = "macos",
target_os = "ios",
target_os = "tvos",
target_os = "watchos",
target_os = "visionos",
))]
#[path = "detect/aarch64_apple.rs"]
mod detect_apple;

#[cfg(not(portable_atomic_no_asm))]
use core::arch::asm;
Expand Down
2 changes: 1 addition & 1 deletion src/imp/atomic128/detect/aarch64_aa64reg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Supported platforms:
https://github.com/torvalds/linux/commit/77c97b4ee21290f5f083173d957843b615abbff2
- FreeBSD 12.0+ (emulate mrs instruction)
https://github.com/freebsd/freebsd-src/commit/398810619cb32abf349f8de23f29510b2ee0839b
- NetBSD 9.0+ (through sysctl)
- NetBSD 9.0+ (through sysctl/sysctlbyname)
https://github.com/NetBSD/src/commit/0e9d25528729f7fea53e78275d1bc5039dfe8ffb
- OpenBSD 7.1+ (through sysctl)
https://github.com/openbsd/src/commit/d335af936b9d7dd9cf655cae1ce19560c45de6c8
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: Apache-2.0 OR MIT

/*
Run-time CPU feature detection on AArch64 macOS by using sysctl.
Run-time CPU feature detection on AArch64 Apple targets by using sysctlbyname.
This module is currently only enabled on tests because AArch64 macOS always supports FEAT_LSE and FEAT_LSE2.
https://github.com/llvm/llvm-project/blob/llvmorg-18.1.2/llvm/include/llvm/TargetParser/AArch64TargetParser.h#L728
Expand All @@ -12,9 +12,10 @@ M4 is Armv9.2 and it doesn't support FEAT_LSE128/FEAT_LRCPC3.
Refs: https://developer.apple.com/documentation/kernel/1387446-sysctlbyname/determining_instruction_set_characteristics
Note that iOS doesn't support sysctl:
TODO: non-macOS targets doesn't always supports FEAT_LSE2, but sysctl on them on app stores is...?
- https://developer.apple.com/forums/thread/9440
- https://nabla-c0d3.github.io/blog/2015/06/16/ios9-security-privacy
- https://github.com/rust-lang/stdarch/pull/1636
*/

include!("common.rs");
Expand Down Expand Up @@ -136,7 +137,14 @@ mod tests {
clippy::no_effect_underscore_binding
)]
const _: fn() = || {
use test_helper::{libc, sys};
use test_helper::libc;
#[cfg(not(any(
target_os = "ios",
target_os = "tvos",
target_os = "watchos",
target_os = "visionos",
)))]
use test_helper::sys;
let mut _sysctlbyname: unsafe extern "C" fn(
*const ffi::c_char,
*mut ffi::c_void,
Expand All @@ -145,6 +153,14 @@ mod tests {
ffi::c_size_t,
) -> ffi::c_int = ffi::sysctlbyname;
_sysctlbyname = libc::sysctlbyname;
_sysctlbyname = sys::sysctlbyname;
#[cfg(not(any(
target_os = "ios",
target_os = "tvos",
target_os = "watchos",
target_os = "visionos",
)))] // TODO
{
_sysctlbyname = sys::sysctlbyname;
}
};
}
33 changes: 29 additions & 4 deletions src/imp/atomic128/detect/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,23 +148,48 @@ mod c_types {
// Hexagon https://lists.llvm.org/pipermail/llvm-dev/attachments/20190916/21516a52/attachment-0001.pdf
// AIX https://www.ibm.com/docs/en/xl-c-aix/13.1.2?topic=descriptions-qchars
// z/OS https://www.ibm.com/docs/en/zos/2.5.0?topic=specifiers-character-types
// (macOS is currently the only Apple target that uses this module, and Windows currently doesn't use this module)
#[cfg(not(target_os = "macos"))]
// (Windows currently doesn't use this module)
#[cfg(not(any(
target_os = "macos",
target_os = "ios",
target_os = "tvos",
target_os = "watchos",
target_os = "visionos",
)))]
pub(crate) type c_char = u8;
// c_char is i8 on all Apple targets
#[cfg(target_os = "macos")]
#[cfg(any(
target_os = "macos",
target_os = "ios",
target_os = "tvos",
target_os = "watchos",
target_os = "visionos",
))]
pub(crate) type c_char = i8;

// Static assertions for C type definitions.
#[cfg(test)]
const _: fn() = || {
use test_helper::{libc, sys};
use test_helper::libc;
#[cfg(not(any(
target_os = "ios",
target_os = "tvos",
target_os = "watchos",
target_os = "visionos",
)))]
use test_helper::sys;
let _: c_int = 0 as std::os::raw::c_int;
let _: c_uint = 0 as std::os::raw::c_uint;
let _: c_long = 0 as std::os::raw::c_long;
let _: c_ulong = 0 as std::os::raw::c_ulong;
let _: c_size_t = 0 as libc::size_t; // std::os::raw::c_size_t is unstable
let _: c_char = 0 as std::os::raw::c_char;
#[cfg(not(any(
target_os = "ios",
target_os = "tvos",
target_os = "watchos",
target_os = "visionos",
)))] // TODO
let _: c_char = 0 as sys::c_char;
};
}
Expand Down

0 comments on commit d9968f0

Please sign in to comment.