Skip to content

Commit

Permalink
aarch64: Allow enabling outline-atomics on linux-musl with static lin…
Browse files Browse the repository at this point in the history
…king enabled
  • Loading branch information
taiki-e committed Apr 21, 2023
1 parent 422a177 commit 31d0862
Show file tree
Hide file tree
Showing 12 changed files with 182 additions and 15 deletions.
10 changes: 9 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -175,13 +175,21 @@ jobs:
# and is not a public API.
RUSTDOCFLAGS: ${{ env.RUSTDOCFLAGS }} --cfg portable_atomic_test_outline_atomics_detect_false
RUSTFLAGS: ${{ env.RUSTFLAGS }} --cfg portable_atomic_test_outline_atomics_detect_false
if: (matrix.target == '' || startsWith(matrix.target, 'x86_64')) || startsWith(matrix.target, 'aarch64') || startsWith(matrix.target, 'armv5te') || matrix.target == 'arm-linux-androideabi'
# outline-atomics is disabled by default on aarch64 musl with static linking
if: (matrix.target == '' || startsWith(matrix.target, 'x86_64')) || startsWith(matrix.target, 'aarch64') && !contains(matrix.target, '-musl') || startsWith(matrix.target, 'armv5te') || matrix.target == 'arm-linux-androideabi'
# -crt-static
- run: tools/test.sh -vv $TARGET $DOCTEST_XCOMPILE $BUILD_STD $REPORT_TIME
env:
RUSTDOCFLAGS: ${{ env.RUSTDOCFLAGS }} -C target-feature=-crt-static
RUSTFLAGS: ${{ env.RUSTFLAGS }} -C target-feature=-crt-static
if: contains(matrix.target, '-musl')
# +crt-static + outline-atomics
# outline-atomics is disabled by default on aarch64 musl with static linking
- run: tools/test.sh -vv $TARGET $DOCTEST_XCOMPILE $BUILD_STD $REPORT_TIME
env:
RUSTDOCFLAGS: ${{ env.RUSTDOCFLAGS }} --cfg portable_atomic_outline_atomics
RUSTFLAGS: ${{ env.RUSTFLAGS }} --cfg portable_atomic_outline_atomics
if: startsWith(matrix.target, 'aarch64') && contains(matrix.target, '-musl')
# +cmpxchg16b
- run: tools/test.sh -vv $TARGET $DOCTEST_XCOMPILE $BUILD_STD $REPORT_TIME
env:
Expand Down
26 changes: 15 additions & 11 deletions src/imp/atomic128/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,23 @@ As 128-bit atomics-related APIs stabilize in the standard library, implementatio

[detect](detect) module has run-time feature detection implementations.

Run-time detections are enabled by default and can be disabled with `--cfg portable_atomic_no_outline_atomics`.

Here is the table of targets that support run-time feature detection and the instruction or API used:

| target_arch | target_os/target_env | instruction/API |
| ----------- | -------------------- | --------------- |
| x86_64 | all (except for sgx) | cpuid |
| aarch64 | linux/android | getauxval |
| aarch64 | freebsd | elf_aux_info |
| aarch64 | macos/openbsd | sysctl |
| aarch64 | windows | IsProcessorFeaturePresent |
| aarch64 | fuchsia | zx_system_get_features |
| target_arch | target_os/target_env | instruction/API | note |
| ----------- | -------------------- | --------------- | ---- |
| x86_64 | all (except for sgx) | cpuid | Enabled by default |
| aarch64 | linux | getauxval | Only enabled by default on `*-linux-gnu*`, and `*-linux-musl*"` (default is static linking)/`*-linux-ohos*` (default is dynamic linking) with dynamic linking enabled. |
| aarch64 | android | getauxval | Enabled by default |
| aarch64 | freebsd | elf_aux_info | Enabled by default |
| aarch64 | openbsd | sysctl | Enabled by default |
| aarch64 | macos | sysctl | Currently test-only because FEAT_LSE and FEAT_LSE2 are always available at compile-time. |
| aarch64 | windows | IsProcessorFeaturePresent | Enabled by default |
| aarch64 | fuchsia | zx_system_get_features | Enabled by default |

Run-time detection is enabled by default on most targets and can be disabled with `--cfg portable_atomic_no_outline_atomics`.

For targets such as `*-linux-musl*` with static linking enabled, where `getauxval` is not always available, so run-time detection is disabled by default and can be enabled by `--cfg portable_atomic_outline_atomics`. (When both cfg are enabled `*_no_*` cfg is preferred.)

For targets not included in the above table, run-time detections are disabled and work the same as when `--cfg portable_atomic_no_outline_atomics` is set.
For targets not included in the above table, run-time detection is always disabled and works the same as when `--cfg portable_atomic_no_outline_atomics` is set.

See [detect/auxv.rs](detect/auxv.rs) module-level comments for more details on Linux/Android/FreeBSD.
4 changes: 4 additions & 0 deletions src/imp/atomic128/aarch64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ include!("macros.rs");
any(
target_env = "gnu",
all(any(target_env = "musl", target_env = "ohos"), not(target_feature = "crt-static")),
portable_atomic_outline_atomics,
),
),
target_os = "android",
Expand Down Expand Up @@ -121,6 +122,7 @@ macro_rules! debug_assert_lse {
any(target_env = "musl", target_env = "ohos"),
not(target_feature = "crt-static"),
),
portable_atomic_outline_atomics,
),
),
target_os = "android",
Expand Down Expand Up @@ -420,6 +422,7 @@ unsafe fn atomic_compare_exchange(
any(target_env = "musl", target_env = "ohos"),
not(target_feature = "crt-static"),
),
portable_atomic_outline_atomics,
),
),
target_os = "android",
Expand All @@ -444,6 +447,7 @@ unsafe fn atomic_compare_exchange(
any(target_env = "musl", target_env = "ohos"),
not(target_feature = "crt-static"),
),
portable_atomic_outline_atomics,
),
),
target_os = "android",
Expand Down
2 changes: 2 additions & 0 deletions src/imp/atomic128/detect/aarch64_auxv.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// Run-time feature detection on aarch64 Linux/Android/FreeBSD by parsing ELF auxiliary vectors.
//
// See auxv.rs for more.

#![cfg_attr(
any(
Expand Down
4 changes: 2 additions & 2 deletions src/imp/atomic128/detect/auxv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
// but even in the version containing that patch, [there is report](https://github.com/rust-lang/rust/issues/89626#issuecomment-1242636038)
// of the same error.)
//
// On other Linux targets, we cannot assume that getauxval is always available, so we don't support
// outline-atomics for now.
// On other Linux targets, we cannot assume that getauxval is always available, so we don't enable
// outline-atomics by default (can be enabled by `--cfg portable_atomic_outline_atomics`).
//
// - On musl with static linking. See the above for more.
// Also, in this case, dlsym(getauxval) always returns null.
Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,9 @@ compile_error!(
#[cfg(portable_atomic_no_outline_atomics)]
#[cfg(not(any(target_arch = "aarch64", target_arch = "arm", target_arch = "x86_64")))]
compile_error!("cfg(portable_atomic_no_outline_atomics) does not compatible with this target");
#[cfg(portable_atomic_outline_atomics)]
#[cfg(not(any(target_arch = "aarch64")))]
compile_error!("cfg(portable_atomic_outline_atomics) does not compatible with this target");
#[cfg(portable_atomic_disable_fiq)]
#[cfg(not(all(
target_arch = "arm",
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions tests/helper/src/gen/sys/aarch64_linux_uclibc/mod.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions tests/helper/src/gen/sys/mod.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 16 additions & 1 deletion tools/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ known_cfgs=(
portable_atomic_s_mode
portable_atomic_disable_fiq
portable_atomic_no_outline_atomics
portable_atomic_outline_atomics

# Not public APIs
portable_atomic_test_outline_atomics_detect_false
Expand Down Expand Up @@ -434,7 +435,7 @@ build() {
fi
RUSTFLAGS="${target_rustflags}" \
x_cargo "${args[@]}" "$@"
# Check no-outline-atomics
# Check {,no-}outline-atomics
case "${target}" in
# portable_atomic_no_outline_atomics only affects x86_64, aarch64, and arm.
x86_64* | aarch64* | arm*)
Expand All @@ -443,13 +444,27 @@ build() {
x_cargo "${args[@]}" "$@"
;;
esac
case "${target}" in
# portable_atomic_outline_atomics only affects aarch64 Linux.
aarch64*-linux-*)
CARGO_TARGET_DIR="${target_dir}/outline-atomics" \
RUSTFLAGS="${target_rustflags} --cfg portable_atomic_outline_atomics" \
x_cargo "${args[@]}" "$@"
CARGO_TARGET_DIR="${target_dir}/outline-atomics-no-outline-atomics" \
RUSTFLAGS="${target_rustflags} --cfg portable_atomic_outline_atomics --cfg portable_atomic_no_outline_atomics" \
x_cargo "${args[@]}" "$@"
;;
esac
# Check target features
case "${target}" in
mips*-linux-musl*) ;; # -crt-static by default
*-linux-musl*)
CARGO_TARGET_DIR="${target_dir}/no-crt-static" \
RUSTFLAGS="${target_rustflags} -C target_feature=-crt-static" \
x_cargo "${args[@]}" "$@"
CARGO_TARGET_DIR="${target_dir}/no-crt-static-no-outline-atomics" \
RUSTFLAGS="${target_rustflags} -C target_feature=-crt-static --cfg portable_atomic_no_outline_atomics" \
x_cargo "${args[@]}" "$@"
;;
esac
case "${target}" in
Expand Down
1 change: 1 addition & 0 deletions tools/codegen/src/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ static TARGETS: &[Target] = &[
"aarch64-unknown-linux-gnu_ilp32",
"aarch64_be-unknown-linux-gnu_ilp32",
"aarch64-unknown-linux-musl",
"aarch64-unknown-linux-uclibc",
"aarch64-linux-android",
],
headers: &[
Expand Down

0 comments on commit 31d0862

Please sign in to comment.