Skip to content

std_detect: Use riscv_hwprobe on RISC-V Linux/Android #1762

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 4 additions & 1 deletion crates/std_detect/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,14 @@ crate from working on applications in which `std` is not available.
[`cupid`](https://crates.io/crates/cupid) crate.

* Linux/Android:
* `arm{32, 64}`, `mips{32,64}{,el}`, `powerpc{32,64}{,le}`, `riscv{32,64}`, `loongarch64`, `s390x`:
* `arm{32, 64}`, `mips{32,64}{,el}`, `powerpc{32,64}{,le}`, `loongarch64`, `s390x`:
`std_detect` supports these on Linux by querying ELF auxiliary vectors (using `getauxval`
when available), and if that fails, by querying `/proc/cpuinfo`.
* `arm64`: partial support for doing run-time feature detection by directly
querying `mrs` is implemented for Linux >= 4.11, but not enabled by default.
* `riscv{32,64}`:
`std_detect` supports these on Linux by querying `riscv_hwprobe`, and
by querying ELF auxiliary vectors (using `getauxval` when available).

* FreeBSD:
* `arm32`, `powerpc64`: `std_detect` supports these on FreeBSD by querying ELF
Expand Down
83 changes: 83 additions & 0 deletions crates/std_detect/src/detect/arch/riscv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@ features! {

@FEATURE: #[stable(feature = "riscv_ratified", since = "1.78.0")] a: "a";
/// "A" Extension for Atomic Instructions
@FEATURE: #[unstable(feature = "stdarch_riscv_feature_detection", issue = "111192")] zacas: "zacas";
/// Atomic Compare-And-Swap Instructions
@FEATURE: #[unstable(feature = "stdarch_riscv_feature_detection", issue = "111192")] zawrs: "zawrs";
/// Wait on Reservation Set
@FEATURE: #[unstable(feature = "stdarch_riscv_feature_detection", issue = "111192")] zam: "zam";
without cfg check: true;
/// "Zam" Extension for Misaligned Atomics
Expand Down Expand Up @@ -194,6 +198,85 @@ features! {

@FEATURE: #[unstable(feature = "stdarch_riscv_feature_detection", issue = "111192")] v: "v";
/// "V" Extension for Vector Operations
@FEATURE: #[unstable(feature = "stdarch_riscv_feature_detection", issue = "111192")] zvfh: "zvfh";
without cfg check: true; // FIXME: added in https://github.com/rust-lang/rust/pull/138742
/// Vector Extension for Half-Precision Floating-Point
@FEATURE: #[unstable(feature = "stdarch_riscv_feature_detection", issue = "111192")] zvfhmin: "zvfhmin";
without cfg check: true; // FIXME: added in https://github.com/rust-lang/rust/pull/138742
/// Vector Extension for Minimal Half-Precision Floating-Point
@FEATURE: #[unstable(feature = "stdarch_riscv_feature_detection", issue = "111192")] zve32x: "zve32x";
without cfg check: true; // FIXME: added in https://github.com/rust-lang/rust/pull/138742
/// Vector Extensions for Embedded Processors
@FEATURE: #[unstable(feature = "stdarch_riscv_feature_detection", issue = "111192")] zve32f: "zve32f";
without cfg check: true; // FIXME: added in https://github.com/rust-lang/rust/pull/138742
/// Vector Extensions for Embedded Processors
@FEATURE: #[unstable(feature = "stdarch_riscv_feature_detection", issue = "111192")] zve64x: "zve64x";
without cfg check: true; // FIXME: added in https://github.com/rust-lang/rust/pull/138742
/// Vector Extensions for Embedded Processors
@FEATURE: #[unstable(feature = "stdarch_riscv_feature_detection", issue = "111192")] zve64f: "zve64f";
without cfg check: true; // FIXME: added in https://github.com/rust-lang/rust/pull/138742
/// Vector Extensions for Embedded Processors
@FEATURE: #[unstable(feature = "stdarch_riscv_feature_detection", issue = "111192")] zve64d: "zve64d";
without cfg check: true; // FIXME: added in https://github.com/rust-lang/rust/pull/138742
/// Vector Extensions for Embedded Processors

// FIXME: we can implement this by getting the current vlen
// zvl*b: Minimum Vector Length Standard Extensions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Despite that I can allow this comment alone (so that you can keep up with this PR), I disagree (actually) adding all Zvl*b extensions (in total of 12; * being a power of two between 32 and 65536 inclusive) for being too redundant (e.g. 256 implies 128, 128 implies 64 and so on) and either:

  • Unlikely to be referred by real-world programs (due to the software-side nature of RVV) or
  • Likely that (for specialized programs) just knowing the vector length is not enough (it is said that implementing RVV to the hardware is harder than implementing regular SIMD instructions and some reports suggested that the actual performance boost heavily depends on the actual vector engine implementation) so that adding Zvl*b alone will not help them much.

I acknowledge that there's still a need to retrieve the vector register size and I think an intrinsic (which returns the vector register size) would be a better solution.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It indeed makes more sense to have it as a core::arch intrinsic, since nothing OS-specific is needed to get the VLEN, and the actual length information can be obtained in a single call.

Removed that code comment.


@FEATURE: #[unstable(feature = "stdarch_riscv_feature_detection", issue = "111192")] zvkb: "zvkb";
without cfg check: true; // FIXME: added in https://github.com/rust-lang/rust/pull/138742
/// Vector Bit-manipulation used in Cryptography
@FEATURE: #[unstable(feature = "stdarch_riscv_feature_detection", issue = "111192")] zvbb: "zvbb";
without cfg check: true; // FIXME: added in https://github.com/rust-lang/rust/pull/138742
/// Vector basic bit-manipulation instructions
@FEATURE: #[unstable(feature = "stdarch_riscv_feature_detection", issue = "111192")] zvbc: "zvbc";
without cfg check: true; // FIXME: added in https://github.com/rust-lang/rust/pull/138742
/// Vector Carryless Multiplication
@FEATURE: #[unstable(feature = "stdarch_riscv_feature_detection", issue = "111192")] zvkg: "zvkg";
without cfg check: true; // FIXME: added in https://github.com/rust-lang/rust/pull/138742
/// Vector GCM instructions for Cryptography
@FEATURE: #[unstable(feature = "stdarch_riscv_feature_detection", issue = "111192")] zvkned: "zvkned";
without cfg check: true; // FIXME: added in https://github.com/rust-lang/rust/pull/138742
/// Vector AES Encryption & Decryption (Single Round)
@FEATURE: #[unstable(feature = "stdarch_riscv_feature_detection", issue = "111192")] zvknha: "zvknha";
without cfg check: true; // FIXME: added in https://github.com/rust-lang/rust/pull/138742
/// Vector SHA-2 (SHA-256 only))
@FEATURE: #[unstable(feature = "stdarch_riscv_feature_detection", issue = "111192")] zvknhb: "zvknhb";
without cfg check: true; // FIXME: added in https://github.com/rust-lang/rust/pull/138742
/// Vector SHA-2 (SHA-256 and SHA-512)
@FEATURE: #[unstable(feature = "stdarch_riscv_feature_detection", issue = "111192")] zvksed: "zvksed";
without cfg check: true; // FIXME: added in https://github.com/rust-lang/rust/pull/138742
/// SM4 Block Cipher Instructions
@FEATURE: #[unstable(feature = "stdarch_riscv_feature_detection", issue = "111192")] zvksh: "zvksh";
without cfg check: true; // FIXME: added in https://github.com/rust-lang/rust/pull/138742
/// SM3 Hash Function Instructions
@FEATURE: #[unstable(feature = "stdarch_riscv_feature_detection", issue = "111192")] zvkn: "zvkn";
without cfg check: true; // FIXME: added in https://github.com/rust-lang/rust/pull/138742
/// Shorthand for 'Zvkned', 'Zvknhb', 'Zvkb', and 'Zvkt'
@FEATURE: #[unstable(feature = "stdarch_riscv_feature_detection", issue = "111192")] zvknc: "zvknc";
without cfg check: true; // FIXME: added in https://github.com/rust-lang/rust/pull/138742
/// Shorthand for 'Zvkn' and 'Zvbc'
@FEATURE: #[unstable(feature = "stdarch_riscv_feature_detection", issue = "111192")] zvkng: "zvkng";
without cfg check: true; // FIXME: added in https://github.com/rust-lang/rust/pull/138742
/// Shorthand for 'Zvkn' and 'Zvkg'
@FEATURE: #[unstable(feature = "stdarch_riscv_feature_detection", issue = "111192")] zvks: "zvks";
without cfg check: true; // FIXME: added in https://github.com/rust-lang/rust/pull/138742
/// Shorthand for 'Zvksed', 'Zvksh', 'Zvkb', and 'Zvkt'
@FEATURE: #[unstable(feature = "stdarch_riscv_feature_detection", issue = "111192")] zvksc: "zvksc";
without cfg check: true; // FIXME: added in https://github.com/rust-lang/rust/pull/138742
/// Shorthand for 'Zvks' and 'Zvbc'
@FEATURE: #[unstable(feature = "stdarch_riscv_feature_detection", issue = "111192")] zvksg: "zvksg";
without cfg check: true; // FIXME: added in https://github.com/rust-lang/rust/pull/138742
/// Shorthand for 'Zvks' and 'Zvkg'
@FEATURE: #[unstable(feature = "stdarch_riscv_feature_detection", issue = "111192")] zvkt: "zvkt";
without cfg check: true; // FIXME: added in https://github.com/rust-lang/rust/pull/138742
/// Vector Data-Independent Execution Latency

@FEATURE: #[unstable(feature = "stdarch_riscv_feature_detection", issue = "111192")] unaligned_scalar_mem: "unaligned-scalar-mem";
/// Has reasonably performant unaligned scalar
@FEATURE: #[unstable(feature = "stdarch_riscv_feature_detection", issue = "111192")] unaligned_vector_mem: "unaligned-vector-mem";
without cfg check: true; // FIXME: added in https://github.com/rust-lang/rust/pull/138742
/// Has reasonably performant unaligned vector

@FEATURE: #[unstable(feature = "stdarch_riscv_feature_detection", issue = "111192")] svnapot: "svnapot";
without cfg check: true;
Expand Down
Loading