Skip to content

is_riscv_feature_detected doesn't seem to actually detect anything at runtime #139139

Open
@ThomasHabets

Description

@ThomasHabets

I tried this code

fn check_rvv() {
    let enabled = cfg!(target_feature="v");
    let detected = std::arch::is_riscv_feature_detected!("v");
    println!("RVV enabled={enabled} detected={detected}");
}  

I expected to see this happen

Any way at all under hardware with and without RVV support, with and without -Ctarget-feature=+v, to be able to return something other than "enabled and detected", or "not enabled and not detected".

I.e. for this feature to be useful for:

  1. Saying: Hey, you have a CPU capable of vector instructions, but this binary is not, or
  2. Having a hand coded RVV function, built with #[target_feature(enable = "v")], that can be selected at runtime, while the rest of the Rust code does not assume +v.

I do realise that if I compile with +v (thus cfg!(target_feature = "v") returns true everywhere), then the binary will probably be broken on non-rvv hardware. E.g. println!() could use the very instructions that are not supported, so I can't even print an error.

But for the cases above:

  1. Should be safe, just tell user to rebuild.
  2. It's apparently not actually runtime detecting anything.

Instead, this happened

It seems that if -Ctarget-feature=+v is not enabled, then std::arch::is_riscv_feature_detected!("v") always returns false.

It's also curious that inside a #[target_feature(enable = "v")] function, cfg!(target_feature="v") doesn't return true, unless the whole program was built with +v. Is that working as intended?

It seems to me that this should not be the same as #138789, where the reason is that LLVM doesn't have the CPU in its database. Because if it were, then the example code should never show enabled=true detected=true, but it does.

Not sure if it helps at all, but the code base I'm working on is rvv-vroom. You can run the code related to this bug using cargo +nightly run.

Meta

rustc --version --verbose:

$ cargo +nightly version --verbose
cargo 1.87.0-nightly (6cf826701 2025-03-14)
release: 1.87.0-nightly
commit-hash: 6cf8267012570f63d6b86e85a2ae5627de52df9e
commit-date: 2025-03-14
host: riscv64gc-unknown-linux-gnu
libgit2: 1.9.0 (sys:0.20.0 vendored)
libcurl: 8.12.1-DEV (sys:0.4.80+curl-8.12.1 vendored ssl:OpenSSL/3.4.1)
ssl: OpenSSL 3.4.1 11 Feb 2025
os: Ubuntu 24.4.0 (noble) [64-bit]

Metadata

Metadata

Assignees

Labels

A-target-featureArea: Enabling/disabling target features like AVX, Neon, etc.C-bugCategory: This is a bug.O-riscvTarget: RISC-V architectureT-libsRelevant to the library team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions