From a4cf5e9eca00527082fd58356e4adc8775aeee03 Mon Sep 17 00:00:00 2001 From: Chen Zhongyao Date: Mon, 14 Jul 2025 13:35:53 +0000 Subject: Add new Tier-3 target: riscv64a23-unknown-linux-gnu --- compiler/rustc_codegen_llvm/src/llvm_util.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'compiler/rustc_codegen_llvm/src') diff --git a/compiler/rustc_codegen_llvm/src/llvm_util.rs b/compiler/rustc_codegen_llvm/src/llvm_util.rs index 28d2100f478..a85a894e42c 100644 --- a/compiler/rustc_codegen_llvm/src/llvm_util.rs +++ b/compiler/rustc_codegen_llvm/src/llvm_util.rs @@ -278,7 +278,11 @@ pub(crate) fn to_llvm_features<'a>(sess: &Session, s: &'a str) -> Option None, + ("riscv32" | "riscv64", "zacas" | "rva23s64" | "rva23u64" | "sha" | "ssnpm" | "supm") + if get_version().0 < 20 => + { + None + } ( "s390x", "message-security-assist-extension12" -- cgit 1.4.1-3-g733a5 From 45ea228c42cfb6712be36a0b4b64b43b8651a54f Mon Sep 17 00:00:00 2001 From: Zhongyao Chen Date: Tue, 19 Aug 2025 10:33:54 +0800 Subject: completely remove rva23s64 --- compiler/rustc_codegen_llvm/src/llvm_util.rs | 6 +-- compiler/rustc_target/src/target_features.rs | 70 ---------------------------- tests/ui/check-cfg/target_feature.stderr | 14 ------ 3 files changed, 1 insertion(+), 89 deletions(-) (limited to 'compiler/rustc_codegen_llvm/src') diff --git a/compiler/rustc_codegen_llvm/src/llvm_util.rs b/compiler/rustc_codegen_llvm/src/llvm_util.rs index a85a894e42c..d81c4c329b3 100644 --- a/compiler/rustc_codegen_llvm/src/llvm_util.rs +++ b/compiler/rustc_codegen_llvm/src/llvm_util.rs @@ -278,11 +278,7 @@ pub(crate) fn to_llvm_features<'a>(sess: &Session, s: &'a str) -> Option - { - None - } + ("riscv32" | "riscv64", "zacas" | "rva23u64" | "supm") if get_version().0 < 20 => None, ( "s390x", "message-security-assist-extension12" diff --git a/compiler/rustc_target/src/target_features.rs b/compiler/rustc_target/src/target_features.rs index 434f6a840b6..0729c44bc91 100644 --- a/compiler/rustc_target/src/target_features.rs +++ b/compiler/rustc_target/src/target_features.rs @@ -597,62 +597,6 @@ static RISCV_FEATURES: &[(&str, Stability, ImpliedFeatures)] = &[ ), ("m", Stable, &[]), ("relax", Unstable(sym::riscv_target_feature), &[]), - ( - "rva23s64", - Unstable(sym::riscv_target_feature), - &[ - "m", - "a", - "f", - "d", - "c", - "b", - "v", - "zicsr", - "zicntr", - "zihpm", - "ziccif", - "ziccrse", - "ziccamoa", - "zicclsm", - "zic64b", - "za64rs", - "zihintpause", - "zba", - "zbb", - "zbs", - "zicbom", - "zicbop", - "zicboz", - "zfhmin", - "zkt", - "zvfhmin", - "zvbb", - "zvkt", - "zihintntl", - "zicond", - "zimop", - "zcmop", - "zcb", - "zfa", - "zawrs", - "svbare", - "svade", - "ssccptr", - "sstvecd", - "sstvala", - "sscounterenw", - "svpbmt", - "svinval", - "svnapot", - "sstc", - "sscofpmf", - "ssnpm", - "ssu64xl", - "sha", - "supm", - ], - ), ( "rva23u64", Unstable(sym::riscv_target_feature), @@ -695,21 +639,7 @@ static RISCV_FEATURES: &[(&str, Stability, ImpliedFeatures)] = &[ "supm", ], ), - ("sha", Unstable(sym::riscv_target_feature), &[]), - ("ssccptr", Unstable(sym::riscv_target_feature), &[]), - ("sscofpmf", Unstable(sym::riscv_target_feature), &[]), - ("sscounterenw", Unstable(sym::riscv_target_feature), &[]), - ("ssnpm", Unstable(sym::riscv_target_feature), &[]), - ("sstc", Unstable(sym::riscv_target_feature), &[]), - ("sstvala", Unstable(sym::riscv_target_feature), &[]), - ("sstvecd", Unstable(sym::riscv_target_feature), &[]), - ("ssu64xl", Unstable(sym::riscv_target_feature), &[]), ("supm", Unstable(sym::riscv_target_feature), &[]), - ("svade", Unstable(sym::riscv_target_feature), &[]), - ("svbare", Unstable(sym::riscv_target_feature), &[]), - ("svinval", Unstable(sym::riscv_target_feature), &[]), - ("svnapot", Unstable(sym::riscv_target_feature), &[]), - ("svpbmt", Unstable(sym::riscv_target_feature), &[]), ("unaligned-scalar-mem", Unstable(sym::riscv_target_feature), &[]), ("unaligned-vector-mem", Unstable(sym::riscv_target_feature), &[]), ("v", Unstable(sym::riscv_target_feature), &["zvl128b", "zve64d"]), diff --git a/tests/ui/check-cfg/target_feature.stderr b/tests/ui/check-cfg/target_feature.stderr index a484fb0e5f0..218755a01f7 100644 --- a/tests/ui/check-cfg/target_feature.stderr +++ b/tests/ui/check-cfg/target_feature.stderr @@ -241,7 +241,6 @@ LL | cfg!(target_feature = "_UNEXPECTED_VALUE"); `relax` `relaxed-simd` `rtm` -`rva23s64` `rva23u64` `sb` `scq` @@ -294,27 +293,17 @@ LL | cfg!(target_feature = "_UNEXPECTED_VALUE"); `soft-float` `spe` `ssbs` -`ssccptr` -`sscofpmf` -`sscounterenw` `sse` `sse2` `sse3` `sse4.1` `sse4.2` `sse4a` -`ssnpm` `ssse3` -`sstc` -`sstvala` -`sstvecd` -`ssu64xl` `ssve-fp8dot2` `ssve-fp8dot4` `ssve-fp8fma` `supm` -`svade` -`svbare` `sve` `sve-b16b16` `sve2` @@ -323,9 +312,6 @@ LL | cfg!(target_feature = "_UNEXPECTED_VALUE"); `sve2-sha3` `sve2-sm4` `sve2p1` -`svinval` -`svnapot` -`svpbmt` `tail-call` `tbm` `thumb-mode` -- cgit 1.4.1-3-g733a5 From fcff8f7f5a0d4add3c05f57de1b34291746c3c08 Mon Sep 17 00:00:00 2001 From: Zalathar Date: Tue, 26 Aug 2025 12:31:33 +1000 Subject: Assert that LLVM range-attribute values don't exceed 128 bits The underlying implementation of `LLVMCreateConstantRangeAttribute` assumes that each of `LowerWords` and `UpperWords` points to enough u64 values to define an integer of the specified bit-length, and will encounter UB if that is not the case. Our safe wrapper function always passes pointers to `[u64; 2]` arrays, regardless of the bit-length specified. That's fine in practice, because scalar primitives never exceed 128 bits, but it is technically a soundness hole in a safe function. We can close the soundness hole by explicitly asserting `size_bits <= 128`. This is effectively just a stricter version of the existing check that the value must be small enough to fit in `c_uint`. --- compiler/rustc_codegen_llvm/src/llvm/ffi.rs | 12 ++++++++--- compiler/rustc_codegen_llvm/src/llvm/mod.rs | 26 ++++++++++++++++-------- compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp | 3 +++ 3 files changed, 30 insertions(+), 11 deletions(-) (limited to 'compiler/rustc_codegen_llvm/src') diff --git a/compiler/rustc_codegen_llvm/src/llvm/ffi.rs b/compiler/rustc_codegen_llvm/src/llvm/ffi.rs index fa2802a891f..7c79cba2273 100644 --- a/compiler/rustc_codegen_llvm/src/llvm/ffi.rs +++ b/compiler/rustc_codegen_llvm/src/llvm/ffi.rs @@ -1929,11 +1929,17 @@ unsafe extern "C" { C: &Context, effects: MemoryEffects, ) -> &Attribute; + /// ## Safety + /// - Each of `LowerWords` and `UpperWords` must point to an array that is + /// long enough to fully define an integer of size `NumBits`, i.e. each + /// pointer must point to `NumBits.div_ceil(64)` elements or more. + /// - The implementation will make its own copy of the pointed-to `u64` + /// values, so the pointers only need to outlive this function call. pub(crate) fn LLVMRustCreateRangeAttribute( C: &Context, - num_bits: c_uint, - lower_words: *const u64, - upper_words: *const u64, + NumBits: c_uint, + LowerWords: *const u64, + UpperWords: *const u64, ) -> &Attribute; // Operations on functions diff --git a/compiler/rustc_codegen_llvm/src/llvm/mod.rs b/compiler/rustc_codegen_llvm/src/llvm/mod.rs index 6adabe53129..d6974e22c85 100644 --- a/compiler/rustc_codegen_llvm/src/llvm/mod.rs +++ b/compiler/rustc_codegen_llvm/src/llvm/mod.rs @@ -112,16 +112,26 @@ pub(crate) fn CreateAllocKindAttr(llcx: &Context, kind_arg: AllocKindFlags) -> & pub(crate) fn CreateRangeAttr(llcx: &Context, size: Size, range: WrappingRange) -> &Attribute { let lower = range.start; + // LLVM treats the upper bound as exclusive, but allows wrapping. let upper = range.end.wrapping_add(1); - let lower_words = [lower as u64, (lower >> 64) as u64]; - let upper_words = [upper as u64, (upper >> 64) as u64]; + + // Pass each `u128` endpoint value as a `[u64; 2]` array, least-significant part first. + let as_u64_array = |x: u128| [x as u64, (x >> 64) as u64]; + let lower_words: [u64; 2] = as_u64_array(lower); + let upper_words: [u64; 2] = as_u64_array(upper); + + // To ensure that LLVM doesn't try to read beyond the `[u64; 2]` arrays, + // we must explicitly check that `size_bits` does not exceed 128. + let size_bits = size.bits(); + assert!(size_bits <= 128); + // More robust assertions that are redundant with `size_bits <= 128` and + // should be optimized away. + assert!(size_bits.div_ceil(64) <= u64::try_from(lower_words.len()).unwrap()); + assert!(size_bits.div_ceil(64) <= u64::try_from(upper_words.len()).unwrap()); + let size_bits = c_uint::try_from(size_bits).unwrap(); + unsafe { - LLVMRustCreateRangeAttribute( - llcx, - size.bits().try_into().unwrap(), - lower_words.as_ptr(), - upper_words.as_ptr(), - ) + LLVMRustCreateRangeAttribute(llcx, size_bits, lower_words.as_ptr(), upper_words.as_ptr()) } } diff --git a/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp b/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp index e699e4b9c13..cce40da354d 100644 --- a/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp +++ b/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp @@ -488,6 +488,9 @@ extern "C" LLVMAttributeRef LLVMRustCreateRangeAttribute(LLVMContextRef C, unsigned NumBits, const uint64_t LowerWords[], const uint64_t UpperWords[]) { + // FIXME(Zalathar): There appears to be no stable guarantee that C++ + // `AttrKind` values correspond directly to the `unsigned KindID` values + // accepted by LLVM-C API functions, though in practice they currently do. return LLVMCreateConstantRangeAttribute(C, Attribute::Range, NumBits, LowerWords, UpperWords); } -- cgit 1.4.1-3-g733a5