Skip to content

Commit b954ca1

Browse files
committed
riscv64: Support 128-bit atomics (Zacas extension)
1 parent 15afa46 commit b954ca1

File tree

15 files changed

+626
-15
lines changed

15 files changed

+626
-15
lines changed

.github/.cspell/project-dictionary.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ adde
33
alcgr
44
algr
55
allnoconfig
6+
amocas
67
aosp
78
aqrl
89
armasm
@@ -183,4 +184,5 @@ xsave
183184
xsub
184185
zaamo
185186
zabha
187+
zacas
186188
Zhaoxin

.github/workflows/ci.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,8 @@ jobs:
232232
target: riscv32gc-unknown-linux-gnu
233233
- rust: '1.59'
234234
target: riscv64gc-unknown-linux-gnu
235+
- rust: '1.73' # LLVM 17 (oldest version we can use experimental-zacas on this target)
236+
target: riscv64gc-unknown-linux-gnu
235237
- rust: stable
236238
target: riscv64gc-unknown-linux-gnu
237239
- rust: nightly
@@ -355,6 +357,13 @@ jobs:
355357
RUSTDOCFLAGS: ${{ env.RUSTDOCFLAGS }} -C target-cpu=pwr8
356358
RUSTFLAGS: ${{ env.RUSTFLAGS }} -C target-cpu=pwr8
357359
if: startsWith(matrix.target, 'powerpc64-')
360+
# riscv64 +experimental-zacas
361+
- run: tools/test.sh -vv --tests ${TARGET:-} ${BUILD_STD:-} ${RELEASE:-}
362+
env:
363+
RUSTDOCFLAGS: ${{ env.RUSTDOCFLAGS }} -C target-feature=+experimental-zacas
364+
RUSTFLAGS: ${{ env.RUSTFLAGS }} -C target-feature=+experimental-zacas
365+
# TODO: cranelift doesn't support cfg(target_feature): https://github.com/rust-lang/rustc_codegen_cranelift/issues/1400
366+
if: startsWith(matrix.target, 'riscv64') && !contains(matrix.flags, 'codegen-backend=cranelift')
358367
# s390x z196 (arch9)
359368
- run: tools/test.sh -vv --tests ${TARGET:-} ${BUILD_STD:-} ${RELEASE:-}
360369
env:

build.rs

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ fn main() {
4747

4848
if version.minor >= 80 {
4949
println!(
50-
r#"cargo:rustc-check-cfg=cfg(target_feature,values("zaamo","zabha","quadword-atomics","fast-serialization","load-store-on-cond","distinct-ops","miscellaneous-extensions-3"))"#
50+
r#"cargo:rustc-check-cfg=cfg(target_feature,values("zaamo","zabha","experimental-zacas","quadword-atomics","fast-serialization","load-store-on-cond","distinct-ops","miscellaneous-extensions-3"))"#
5151
);
5252

5353
// Custom cfgs set by build script. Not public API.
@@ -58,7 +58,7 @@ fn main() {
5858
// TODO: handle multi-line target_feature_fallback
5959
// grep -F 'target_feature_fallback("' build.rs | grep -Ev '^ *//' | sed -E 's/^.*target_feature_fallback\(//; s/",.*$/"/' | LC_ALL=C sort -u | tr '\n' ',' | sed -E 's/,$/\n/'
6060
println!(
61-
r#"cargo:rustc-check-cfg=cfg(portable_atomic_target_feature,values("cmpxchg16b","distinct-ops","fast-serialization","load-store-on-cond","lse","lse128","lse2","mclass","miscellaneous-extensions-3","quadword-atomics","rcpc3","v6","zaamo","zabha"))"#
61+
r#"cargo:rustc-check-cfg=cfg(portable_atomic_target_feature,values("cmpxchg16b","distinct-ops","experimental-zacas","fast-serialization","load-store-on-cond","lse","lse128","lse2","mclass","miscellaneous-extensions-3","quadword-atomics","rcpc3","v6","zaamo","zabha"))"#
6262
);
6363
}
6464

@@ -311,15 +311,22 @@ fn main() {
311311
}
312312
}
313313
"riscv32" | "riscv64" => {
314-
// As of rustc 1.80, target_feature "zaamo"/"zabha" is not available on rustc side:
314+
// As of rustc 1.80, target_feature "zaamo"/"zabha"/"zacas" is not available on rustc side:
315315
// https://github.com/rust-lang/rust/blob/1.80.0/compiler/rustc_target/src/target_features.rs#L273
316-
// zabha implies zaamo in GCC, but do not in LLVM (but enabling it without zaamo is not allowed).
317-
// https://github.com/llvm/llvm-project/blob/llvmorg-19.1.0-rc3/llvm/lib/TargetParser/RISCVISAInfo.cpp#L776-L778
318-
// https://github.com/gcc-mirror/gcc/blob/08693e29ec186fd7941d0b73d4d466388971fe2f/gcc/config/riscv/arch-canonicalize#L45
319-
if version.llvm >= 19 {
320-
// amo*.{b,h}
321-
// available since 19 https://github.com/llvm/llvm-project/commit/89f87c387627150d342722b79c78cea2311cddf7 / https://github.com/llvm/llvm-project/commit/6b7444964a8d028989beee554a1f5c61d16a1cac
322-
target_feature_fallback("zabha", false);
316+
// zacas and zabha imply zaamo in GCC, but do not in LLVM (but enabling them without zaamo is not allowed).
317+
// https://github.com/llvm/llvm-project/blob/llvmorg-19.1.0-rc3/llvm/lib/TargetParser/RISCVISAInfo.cpp#L772-L778
318+
// https://github.com/gcc-mirror/gcc/blob/08693e29ec186fd7941d0b73d4d466388971fe2f/gcc/config/riscv/arch-canonicalize#L45-L46
319+
if version.llvm >= 17 {
320+
// amocas.{w,d,q} (amocas.{b,h} if zabha is also available)
321+
// available as experimental since 17 https://github.com/llvm/llvm-project/commit/29f630a1ddcbb03caa31b5002f0cbc105ff3a869
322+
// attempted to make non-experimental in 19 https://github.com/llvm/llvm-project/commit/95aab69c109adf29e183090c25dc95c773215746
323+
// but reverted in https://github.com/llvm/llvm-project/commit/70e7d26e560173c8b9db4c75ab4a3004cd5f021a
324+
target_feature_fallback("experimental-zacas", false);
325+
if version.llvm >= 19 {
326+
// amo*.{b,h}
327+
// available since 19 https://github.com/llvm/llvm-project/commit/89f87c387627150d342722b79c78cea2311cddf7 / https://github.com/llvm/llvm-project/commit/6b7444964a8d028989beee554a1f5c61d16a1cac
328+
target_feature_fallback("zabha", false);
329+
}
323330
}
324331
// amo*.{w,d}
325332
target_feature_fallback("zaamo", false);

src/cfgs.rs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,35 @@ mod atomic_64_macros {
241241
),
242242
),
243243
),
244+
all(
245+
target_arch = "riscv64",
246+
not(portable_atomic_no_asm),
247+
any(
248+
target_feature = "experimental-zacas",
249+
portable_atomic_target_feature = "experimental-zacas",
250+
// TODO(riscv64)
251+
// all(
252+
// feature = "fallback",
253+
// not(portable_atomic_no_outline_atomics),
254+
// any(test, portable_atomic_outline_atomics), // TODO(riscv64): currently disabled by default
255+
// any(
256+
// all(
257+
// target_os = "linux",
258+
// any(
259+
// target_env = "gnu",
260+
// all(
261+
// any(target_env = "musl", target_env = "ohos"),
262+
// not(target_feature = "crt-static"),
263+
// ),
264+
// portable_atomic_outline_atomics,
265+
// ),
266+
// ),
267+
// target_os = "android",
268+
// ),
269+
// not(any(miri, portable_atomic_sanitize_thread)),
270+
// ),
271+
),
272+
),
244273
all(
245274
target_arch = "powerpc64",
246275
portable_atomic_unstable_asm_experimental_arch,
@@ -331,6 +360,35 @@ mod atomic_128_macros {
331360
),
332361
),
333362
),
363+
all(
364+
target_arch = "riscv64",
365+
not(portable_atomic_no_asm),
366+
any(
367+
target_feature = "experimental-zacas",
368+
portable_atomic_target_feature = "experimental-zacas",
369+
// TODO(riscv64)
370+
// all(
371+
// feature = "fallback",
372+
// not(portable_atomic_no_outline_atomics),
373+
// any(test, portable_atomic_outline_atomics), // TODO(riscv64): currently disabled by default
374+
// any(
375+
// all(
376+
// target_os = "linux",
377+
// any(
378+
// target_env = "gnu",
379+
// all(
380+
// any(target_env = "musl", target_env = "ohos"),
381+
// not(target_feature = "crt-static"),
382+
// ),
383+
// portable_atomic_outline_atomics,
384+
// ),
385+
// ),
386+
// target_os = "android",
387+
// ),
388+
// not(any(miri, portable_atomic_sanitize_thread)),
389+
// ),
390+
),
391+
),
334392
all(
335393
target_arch = "powerpc64",
336394
portable_atomic_unstable_asm_experimental_arch,

src/imp/atomic128/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Here is the table of targets that support 128-bit atomics and the instructions u
88
| ----------- | ---- | ----- | --- | --- | ---- |
99
| x86_64 | cmpxchg16b or vmovdqa | cmpxchg16b or vmovdqa | cmpxchg16b | cmpxchg16b | cmpxchg16b target feature required. vmovdqa requires Intel, AMD, or Zhaoxin CPU with AVX. <br> Both compile-time and run-time detection are supported for cmpxchg16b. vmovdqa is currently run-time detection only. <br> Requires rustc 1.59+ |
1010
| aarch64 | ldxp/stxp or casp or ldp/ldiapp | ldxp/stxp or casp or stp/stilp/swpp | ldxp/stxp or casp | ldxp/stxp or casp/swpp/ldclrp/ldsetp | casp requires lse target feature, ldp/stp requires lse2 target feature, ldiapp/stilp requires lse2 and rcpc3 target features, swpp/ldclrp/ldsetp requires lse128 target feature. <br> Both compile-time and run-time detection are supported. <br> Requires rustc 1.59+ |
11+
| riscv64 | amocas.q | amocas.q | amocas.q | amocas.q | Requires experimental-zacas target feature. Currently compile-time detection only due to LLVM marking it as experimental. <br> Requires 1.73+ (LLVM 17+) |
1112
| powerpc64 | lq | stq | lqarx/stqcx. | lqarx/stqcx. | Requires target-cpu pwr8+ (powerpc64le is pwr8 by default). Both compile-time and run-time detection are supported (run-time detection is currently disabled by default). <br> Requires nightly |
1213
| s390x | lpq | stpq | cdsg | cdsg | Requires nightly |
1314

@@ -17,7 +18,7 @@ See [aarch64.rs](aarch64.rs) module-level comments for more details on the instr
1718

1819
## Comparison with core::intrinsics::atomic_\* (core::sync::atomic::Atomic{I,U}128)
1920

20-
This directory has target-specific implementations with inline assembly ([aarch64.rs](aarch64.rs), [x86_64.rs](x86_64.rs), [powerpc64.rs](powerpc64.rs), [s390x.rs](s390x.rs)) and an implementation without inline assembly ([intrinsics.rs](intrinsics.rs)). The latter currently always needs nightly compilers and is only used for Miri and ThreadSanitizer, which do not support inline assembly.
21+
This directory has target-specific implementations with inline assembly ([aarch64.rs](aarch64.rs), [x86_64.rs](x86_64.rs), [powerpc64.rs](powerpc64.rs), [riscv64.rs](riscv64.rs), [s390x.rs](s390x.rs)) and an implementation without inline assembly ([intrinsics.rs](intrinsics.rs)). The latter currently always needs nightly compilers and is only used for Miri and ThreadSanitizer, which do not support inline assembly.
2122

2223
Implementations with inline assembly generate assemblies almost equivalent to the `core::intrinsics::atomic_*` (used in `core::sync::atomic::Atomic{I,U}128`) for many operations, but some operations may or may not generate more efficient code. For example:
2324

@@ -45,6 +46,7 @@ Here is the table of targets that support run-time CPU feature detection and the
4546
| aarch64 | macos | sysctl | all | Currently only used in tests because FEAT_LSE and FEAT_LSE2 are always available at compile-time. |
4647
| aarch64 | windows | IsProcessorFeaturePresent | lse | Enabled by default |
4748
| aarch64 | fuchsia | zx_system_get_features | lse | Enabled by default |
49+
| riscv64 | linux | riscv_hwprobe | all | Currently only used in tests due to LLVM marking zacas as experimental |
4850
| powerpc64 | linux | getauxval | all | Disabled by default |
4951
| powerpc64 | freebsd | elf_aux_info | all | Disabled by default |
5052
| powerpc64 | openbsd | elf_aux_info | all | Disabled by default |

src/imp/atomic128/detect/common.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,14 +109,20 @@ flags! {
109109
HAS_VMOVDQA_ATOMIC(2, has_vmovdqa_atomic, "vmovdqa-atomic", any(/* always false */)),
110110
}
111111

112+
#[cfg(target_arch = "riscv64")]
113+
flags! {
114+
// amocas.{w,d,q}
115+
HAS_ZACAS(1, has_zacas, "zacas", any(target_feature = "experimental-zacas", portable_atomic_target_feature = "experimental-zacas")),
116+
}
117+
112118
#[cfg(target_arch = "powerpc64")]
113119
flags! {
114120
// lqarx and stqcx.
115121
HAS_QUADWORD_ATOMICS(1, has_quadword_atomics, "quadword-atomics", any(target_feature = "quadword-atomics", portable_atomic_target_feature = "quadword-atomics")),
116122
}
117123

118124
// core::ffi::c_* (except c_void) requires Rust 1.64, libc will soon require Rust 1.47
119-
#[cfg(any(target_arch = "aarch64", target_arch = "powerpc64"))]
125+
#[cfg(any(target_arch = "aarch64", target_arch = "powerpc64", target_arch = "riscv64"))]
120126
#[cfg(not(windows))]
121127
#[allow(dead_code, non_camel_case_types)]
122128
mod c_types {
@@ -164,6 +170,9 @@ mod c_types {
164170
let _: c_long = 0 as std::os::raw::c_long;
165171
let _: c_ulong = 0 as std::os::raw::c_ulong;
166172
let _: c_size_t = 0 as libc::size_t; // std::os::raw::c_size_t is unstable
173+
#[cfg(not(
174+
all(target_arch = "riscv64", target_os = "android"), // TODO: https://github.com/rust-lang/rust/issues/129945
175+
))]
167176
let _: c_char = 0 as std::os::raw::c_char;
168177
let _: c_char = 0 as sys::c_char;
169178
};
@@ -310,6 +319,16 @@ mod tests_common {
310319
assert!(!detect().test(CpuInfo::HAS_VMOVDQA_ATOMIC));
311320
}
312321
}
322+
#[cfg(target_arch = "riscv64")]
323+
#[test]
324+
#[cfg_attr(portable_atomic_test_outline_atomics_detect_false, ignore)]
325+
fn test_detect() {
326+
if detect().has_zacas() {
327+
assert!(detect().test(CpuInfo::HAS_ZACAS));
328+
} else {
329+
assert!(!detect().test(CpuInfo::HAS_ZACAS));
330+
}
331+
}
313332
#[cfg(target_arch = "powerpc64")]
314333
#[test]
315334
#[cfg_attr(portable_atomic_test_outline_atomics_detect_false, ignore)]
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
// SPDX-License-Identifier: Apache-2.0 OR MIT
2+
3+
/*
4+
Run-time CPU feature detection on RISC-V Linux/Android by using riscv_hwprobe.
5+
6+
On RISC-V, detection using auxv only supports single-letter extensions.
7+
8+
Refs:
9+
- https://github.com/torvalds/linux/blob/v6.10/Documentation/arch/riscv/hwprobe.rst
10+
- https://github.com/golang/sys/commit/3283fc3f6160baf63bec24fbaa24e094e9ff6daf
11+
*/
12+
13+
include!("common.rs");
14+
15+
// core::ffi::c_* (except c_void) requires Rust 1.64, libc will soon require Rust 1.47
16+
#[allow(non_camel_case_types, non_upper_case_globals)]
17+
mod ffi {
18+
pub(crate) use super::c_types::c_long;
19+
20+
// https://github.com/torvalds/linux/blob/v6.10/arch/riscv/include/uapi/asm/hwprobe.h
21+
#[derive(Copy, Clone)]
22+
#[repr(C)]
23+
pub(crate) struct riscv_hwprobe {
24+
pub(crate) key: i64,
25+
pub(crate) value: u64,
26+
}
27+
28+
pub(crate) const __NR_riscv_hwprobe: c_long = 258;
29+
30+
// https://github.com/torvalds/linux/blob/v6.10/arch/riscv/include/uapi/asm/hwprobe.h
31+
pub(crate) const RISCV_HWPROBE_KEY_IMA_EXT_0: i64 = 4;
32+
// Linux 6.8+
33+
// https://github.com/torvalds/linux/commit/154a3706122978eeb34d8223d49285ed4f3c61fa
34+
pub(crate) const RISCV_HWPROBE_EXT_ZACAS: u64 = 1 << 34;
35+
36+
extern "C" {
37+
// https://man7.org/linux/man-pages/man2/syscall.2.html
38+
pub(crate) fn syscall(number: c_long, ...) -> c_long;
39+
}
40+
}
41+
42+
// syscall returns an unsupported error if riscv_hwprobe is not supported,
43+
// so we can safely use this function on older versions of Linux.
44+
fn riscv_hwprobe(out: &mut ffi::riscv_hwprobe, flags: usize) -> bool {
45+
// SAFETY: We've passed the valid pointer and length.
46+
unsafe {
47+
ffi::syscall(
48+
ffi::__NR_riscv_hwprobe,
49+
out as *mut ffi::riscv_hwprobe,
50+
1_usize,
51+
0_usize,
52+
0_usize,
53+
flags,
54+
0_usize,
55+
) == 0
56+
}
57+
}
58+
59+
#[cold]
60+
fn _detect(info: &mut CpuInfo) {
61+
let mut out = ffi::riscv_hwprobe { key: ffi::RISCV_HWPROBE_KEY_IMA_EXT_0, value: 0 };
62+
if riscv_hwprobe(&mut out, 0) {
63+
if out.key != -1 {
64+
let value = out.value;
65+
if value & ffi::RISCV_HWPROBE_EXT_ZACAS != 0 {
66+
info.set(CpuInfo::HAS_ZACAS);
67+
}
68+
}
69+
}
70+
}
71+
72+
#[allow(
73+
clippy::alloc_instead_of_core,
74+
clippy::std_instead_of_alloc,
75+
clippy::std_instead_of_core,
76+
clippy::undocumented_unsafe_blocks,
77+
clippy::wildcard_imports
78+
)]
79+
#[cfg(test)]
80+
mod tests {
81+
use super::*;
82+
83+
// Static assertions for FFI bindings.
84+
// This checks that FFI bindings defined in this crate, FFI bindings defined
85+
// in libc, and FFI bindings generated for the platform's latest header file
86+
// using bindgen have compatible signatures (or the same values if constants).
87+
// Since this is static assertion, we can detect problems with
88+
// `cargo check --tests --target <target>` run in CI (via TESTS=1 build.sh)
89+
// without actually running tests on these platforms.
90+
// See also tools/codegen/src/ffi.rs.
91+
// TODO(codegen): auto-generate this test
92+
#[allow(
93+
clippy::cast_possible_wrap,
94+
clippy::cast_sign_loss,
95+
clippy::no_effect_underscore_binding
96+
)]
97+
const _: fn() = || {
98+
use test_helper::sys;
99+
// TODO: syscall,riscv_hwprobe
100+
// static_assert!(ffi::__NR_riscv_hwprobe == libc::__NR_riscv_hwprobe); // libc doesn't have this
101+
static_assert!(ffi::__NR_riscv_hwprobe == sys::__NR_riscv_hwprobe as ffi::c_long);
102+
// static_assert!(ffi::RISCV_HWPROBE_KEY_IMA_EXT_0 == libc::RISCV_HWPROBE_KEY_IMA_EXT_0); // libc doesn't have this
103+
static_assert!(ffi::RISCV_HWPROBE_KEY_IMA_EXT_0 == sys::RISCV_HWPROBE_KEY_IMA_EXT_0 as i64);
104+
// static_assert!(ffi::RISCV_HWPROBE_EXT_ZACAS == libc::RISCV_HWPROBE_EXT_ZACAS); // libc doesn't have this
105+
static_assert!(ffi::RISCV_HWPROBE_EXT_ZACAS == sys::RISCV_HWPROBE_EXT_ZACAS);
106+
};
107+
}

src/imp/atomic128/macros.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,12 @@ macro_rules! atomic128 {
264264
};
265265
}
266266

267-
#[cfg(any(target_arch = "powerpc64", target_arch = "s390x", target_arch = "x86_64"))]
267+
#[cfg(any(
268+
target_arch = "powerpc64",
269+
target_arch = "riscv64",
270+
target_arch = "s390x",
271+
target_arch = "x86_64",
272+
))]
268273
#[allow(unused_macros)] // also used by intrinsics.rs
269274
macro_rules! atomic_rmw_by_atomic_update {
270275
() => {

0 commit comments

Comments
 (0)