Skip to content

Commit 85f518e

Browse files
committed
Auto merge of rust-lang#138742 - taiki-e:riscv-vector, r=Amanieu
rustc_target: Add more RISC-V vector-related features and use zvl*b target features in vector ABI check Currently, we have only unstable `v` target feature, but RISC-V have more vector-related extensions. The first commit of this PR adds them to unstable `riscv_target_feature`. - `unaligned-vector-mem`: Has reasonably performant unaligned vector - [LLVM definition](https://github.com/llvm/llvm-project/blob/llvmorg-20.1.0/llvm/lib/Target/RISCV/RISCVFeatures.td#L1379) - Similar to currently unstable `unaligned-scalar-mem` target feature, but for vector instructions. - `zvfh`: Vector Extension for Half-Precision Floating-Point - [ISA Manual](https://github.com/riscv/riscv-isa-manual/blob/riscv-isa-release-2336fdc-2025-03-19/src/v-st-ext.adoc#zvfh-vector-extension-for-half-precision-floating-point) - [LLVM definition](https://github.com/llvm/llvm-project/blob/llvmorg-20.1.0/llvm/lib/Target/RISCV/RISCVFeatures.td#L668) - This implies `zvfhmin` and `zfhmin` - `zvfhmin`: Vector Extension for Minimal Half-Precision Floating-Point - [ISA Manual](https://github.com/riscv/riscv-isa-manual/blob/riscv-isa-release-2336fdc-2025-03-19/src/v-st-ext.adoc#zvfhmin-vector-extension-for-minimal-half-precision-floating-point) - [LLVM definition](https://github.com/llvm/llvm-project/blob/llvmorg-20.1.0/llvm/lib/Target/RISCV/RISCVFeatures.td#L662) - This implies `zve32f` - `zve32x`, `zve32f`, `zve64x`, `zve64f`, `zve64d`: Vector Extensions for Embedded Processors - [ISA Manual](https://github.com/riscv/riscv-isa-manual/blob/riscv-isa-release-2336fdc-2025-03-19/src/v-st-ext.adoc#zve-vector-extensions-for-embedded-processors) - [LLVM definitions](https://github.com/llvm/llvm-project/blob/llvmorg-20.1.0/llvm/lib/Target/RISCV/RISCVFeatures.td#L612-L641) - `zve32x` implies `zvl32b` - `zve32f` implies `zve32x` and `f` - `zve64x` implies `zve32x` and `zvl64b` - `zve64f` implies `zve32f` and `zve64x` - `zve64d` implies `zve64f` and `d` - `v` implies `zve64d` - `zvl*b`: Minimum Vector Length Standard Extensions - [ISA Manual](https://github.com/riscv/riscv-isa-manual/blob/riscv-isa-release-2336fdc-2025-03-19/src/v-st-ext.adoc#zvl-minimum-vector-length-standard-extensions) - [LLVM definitions](https://github.com/llvm/llvm-project/blob/llvmorg-20.1.0/llvm/lib/Target/RISCV/RISCVFeatures.td#L600-L610) - `zvl{N}b` implies `zvl{N>>1}b` - `v` implies `zvl128b` - Vector Cryptography and Bit-manipulation Extensions - [ISA Manual](https://github.com/riscv/riscv-isa-manual/blob/riscv-isa-release-2336fdc-2025-03-19/src/vector-crypto.adoc) - [LLVM definitions](https://github.com/llvm/llvm-project/blob/llvmorg-20.1.0/llvm/lib/Target/RISCV/RISCVFeatures.td#L679-L807) - `zvkb`: Vector Bit-manipulation used in Cryptography - This implies `zve32x` - `zvbb`: Vector basic bit-manipulation instructions - This implies `zvkb` - `zvbc`: Vector Carryless Multiplication - This implies `zve64x` - `zvkg`: Vector GCM instructions for Cryptography - This implies `zve32x` - `zvkned`: Vector AES Encryption & Decryption (Single Round) - This implies `zve32x` - `zvknha`: Vector SHA-2 (SHA-256 only)) - This implies `zve32x` - `zvknhb`: Vector SHA-2 (SHA-256 and SHA-512) - This implies `zve64x` - This is superset of `zvknha`, but doesn't imply that feature at least in LLVM - `zvksed`: SM4 Block Cipher Instructions - This implies `zve32x` - `zvksh`: SM3 Hash Function Instructions - This implies `zve32x` - `zvkt`: Vector Data-Independent Execution Latency - Similar to already stabilized scalar cryptography extension `zkt`. - `zvkn`: Shorthand for 'Zvkned', 'Zvknhb', 'Zvkb', and 'Zvkt' - Similar to already stabilized scalar cryptography extension `zkn`. - `zvknc`: Shorthand for 'Zvkn' and 'Zvbc' - `zvkng`: shorthand for 'Zvkn' and 'Zvkg' - `zvks`: shorthand for 'Zvksed', 'Zvksh', 'Zvkb', and 'Zvkt' - Similar to already stabilized scalar cryptography extension `zks`. - `zvksc`: shorthand for 'Zvks' and 'Zvbc' - `zvksg`: shorthand for 'Zvks' and 'Zvkg' Also, our vector ABI check wants `zvl*b` target features, the second commit of this PR updates vector ABI check to use them. https://github.com/rust-lang/rust/blob/4e2b096ed6c8a1400624a54f6c4fd0c0ce48a579/compiler/rustc_target/src/target_features.rs#L707-L708 --- r? `@Amanieu` `@rustbot` label +O-riscv +A-target-feature
2 parents 2196aff + 8a6f96a commit 85f518e

File tree

3 files changed

+91
-5
lines changed

3 files changed

+91
-5
lines changed

compiler/rustc_codegen_llvm/src/llvm_util.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,9 @@ pub(crate) fn to_llvm_features<'a>(sess: &Session, s: &'a str) -> Option<LLVMFea
274274
("arm", "fp16") => Some(LLVMFeature::new("fullfp16")),
275275
// In LLVM 18, `unaligned-scalar-mem` was merged with `unaligned-vector-mem` into a single
276276
// feature called `fast-unaligned-access`. In LLVM 19, it was split back out.
277-
("riscv32" | "riscv64", "unaligned-scalar-mem") if get_version().0 == 18 => {
277+
("riscv32" | "riscv64", "unaligned-scalar-mem" | "unaligned-vector-mem")
278+
if get_version().0 == 18 =>
279+
{
278280
Some(LLVMFeature::new("fast-unaligned-access"))
279281
}
280282
// Filter out features that are not supported by the current LLVM version

compiler/rustc_target/src/target_features.rs

+51-3
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,8 @@ static RISCV_FEATURES: &[(&str, Stability, ImpliedFeatures)] = &[
497497
("m", Stable, &[]),
498498
("relax", Unstable(sym::riscv_target_feature), &[]),
499499
("unaligned-scalar-mem", Unstable(sym::riscv_target_feature), &[]),
500-
("v", Unstable(sym::riscv_target_feature), &[]),
500+
("unaligned-vector-mem", Unstable(sym::riscv_target_feature), &[]),
501+
("v", Unstable(sym::riscv_target_feature), &["zvl128b", "zve64d"]),
501502
("za128rs", Unstable(sym::riscv_target_feature), &[]),
502503
("za64rs", Unstable(sym::riscv_target_feature), &[]),
503504
("zaamo", Unstable(sym::riscv_target_feature), &[]),
@@ -529,6 +530,41 @@ static RISCV_FEATURES: &[(&str, Stability, ImpliedFeatures)] = &[
529530
("zksed", Stable, &[]),
530531
("zksh", Stable, &[]),
531532
("zkt", Stable, &[]),
533+
("zvbb", Unstable(sym::riscv_target_feature), &["zvkb"]),
534+
("zvbc", Unstable(sym::riscv_target_feature), &["zve64x"]),
535+
("zve32f", Unstable(sym::riscv_target_feature), &["zve32x", "f"]),
536+
("zve32x", Unstable(sym::riscv_target_feature), &["zvl32b"]),
537+
("zve64d", Unstable(sym::riscv_target_feature), &["zve64f", "d"]),
538+
("zve64f", Unstable(sym::riscv_target_feature), &["zve32f", "zve64x"]),
539+
("zve64x", Unstable(sym::riscv_target_feature), &["zve32x", "zvl64b"]),
540+
("zvfh", Unstable(sym::riscv_target_feature), &["zvfhmin", "zfhmin"]),
541+
("zvfhmin", Unstable(sym::riscv_target_feature), &["zve32f"]),
542+
("zvkb", Unstable(sym::riscv_target_feature), &["zve32x"]),
543+
("zvkg", Unstable(sym::riscv_target_feature), &["zve32x"]),
544+
("zvkn", Unstable(sym::riscv_target_feature), &["zvkned", "zvknhb", "zvkb", "zvkt"]),
545+
("zvknc", Unstable(sym::riscv_target_feature), &["zvkn", "zvbc"]),
546+
("zvkned", Unstable(sym::riscv_target_feature), &["zve32x"]),
547+
("zvkng", Unstable(sym::riscv_target_feature), &["zvkn", "zvkg"]),
548+
("zvknha", Unstable(sym::riscv_target_feature), &["zve32x"]),
549+
("zvknhb", Unstable(sym::riscv_target_feature), &["zve64x"]),
550+
("zvks", Unstable(sym::riscv_target_feature), &["zvksed", "zvksh", "zvkb", "zvkt"]),
551+
("zvksc", Unstable(sym::riscv_target_feature), &["zvks", "zvbc"]),
552+
("zvksed", Unstable(sym::riscv_target_feature), &["zve32x"]),
553+
("zvksg", Unstable(sym::riscv_target_feature), &["zvks", "zvkg"]),
554+
("zvksh", Unstable(sym::riscv_target_feature), &["zve32x"]),
555+
("zvkt", Unstable(sym::riscv_target_feature), &[]),
556+
("zvl1024b", Unstable(sym::riscv_target_feature), &["zvl512b"]),
557+
("zvl128b", Unstable(sym::riscv_target_feature), &["zvl64b"]),
558+
("zvl16384b", Unstable(sym::riscv_target_feature), &["zvl8192b"]),
559+
("zvl2048b", Unstable(sym::riscv_target_feature), &["zvl1024b"]),
560+
("zvl256b", Unstable(sym::riscv_target_feature), &["zvl128b"]),
561+
("zvl32768b", Unstable(sym::riscv_target_feature), &["zvl16384b"]),
562+
("zvl32b", Unstable(sym::riscv_target_feature), &[]),
563+
("zvl4096b", Unstable(sym::riscv_target_feature), &["zvl2048b"]),
564+
("zvl512b", Unstable(sym::riscv_target_feature), &["zvl256b"]),
565+
("zvl64b", Unstable(sym::riscv_target_feature), &["zvl32b"]),
566+
("zvl65536b", Unstable(sym::riscv_target_feature), &["zvl32768b"]),
567+
("zvl8192b", Unstable(sym::riscv_target_feature), &["zvl4096b"]),
532568
// tidy-alphabetical-end
533569
];
534570

@@ -704,8 +740,20 @@ const ARM_FEATURES_FOR_CORRECT_VECTOR_ABI: &'static [(u64, &'static str)] = &[(1
704740
const POWERPC_FEATURES_FOR_CORRECT_VECTOR_ABI: &'static [(u64, &'static str)] = &[(128, "altivec")];
705741
const WASM_FEATURES_FOR_CORRECT_VECTOR_ABI: &'static [(u64, &'static str)] = &[(128, "simd128")];
706742
const S390X_FEATURES_FOR_CORRECT_VECTOR_ABI: &'static [(u64, &'static str)] = &[(128, "vector")];
707-
const RISCV_FEATURES_FOR_CORRECT_VECTOR_ABI: &'static [(u64, &'static str)] =
708-
&[/*(64, "zvl64b"), */ (128, "v")];
743+
const RISCV_FEATURES_FOR_CORRECT_VECTOR_ABI: &'static [(u64, &'static str)] = &[
744+
(32, "zvl32b"),
745+
(64, "zvl64b"),
746+
(128, "zvl128b"),
747+
(256, "zvl256b"),
748+
(512, "zvl512b"),
749+
(1024, "zvl1024b"),
750+
(2048, "zvl2048b"),
751+
(4096, "zvl4096b"),
752+
(8192, "zvl8192b"),
753+
(16384, "zvl16384b"),
754+
(32768, "zvl32768b"),
755+
(65536, "zvl65536b"),
756+
];
709757
// Always warn on SPARC, as the necessary target features cannot be enabled in Rust at the moment.
710758
const SPARC_FEATURES_FOR_CORRECT_VECTOR_ABI: &'static [(u64, &'static str)] = &[/*(64, "vis")*/];
711759

tests/ui/check-cfg/target_feature.stderr

+37-1
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ LL | cfg!(target_feature = "_UNEXPECTED_VALUE");
245245
`trustzone`
246246
`ual`
247247
`unaligned-scalar-mem`
248+
`unaligned-vector-mem`
248249
`v`
249250
`v5te`
250251
`v6`
@@ -325,7 +326,42 @@ LL | cfg!(target_feature = "_UNEXPECTED_VALUE");
325326
`zkr`
326327
`zks`
327328
`zksed`
328-
`zksh`, and `zkt`
329+
`zksh`
330+
`zkt`
331+
`zvbb`
332+
`zvbc`
333+
`zve32f`
334+
`zve32x`
335+
`zve64d`
336+
`zve64f`
337+
`zve64x`
338+
`zvfh`
339+
`zvfhmin`
340+
`zvkb`
341+
`zvkg`
342+
`zvkn`
343+
`zvknc`
344+
`zvkned`
345+
`zvkng`
346+
`zvknha`
347+
`zvknhb`
348+
`zvks`
349+
`zvksc`
350+
`zvksed`
351+
`zvksg`
352+
`zvksh`
353+
`zvkt`
354+
`zvl1024b`
355+
`zvl128b`
356+
`zvl16384b`
357+
`zvl2048b`
358+
`zvl256b`
359+
`zvl32768b`
360+
`zvl32b`
361+
`zvl4096b`
362+
`zvl512b`
363+
`zvl64b`
364+
`zvl65536b`, and `zvl8192b`
329365
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration
330366
= note: `#[warn(unexpected_cfgs)]` on by default
331367

0 commit comments

Comments
 (0)