From b264f074491fc40587c76022ee592639c126370a Mon Sep 17 00:00:00 2001 From: Dayuxiaoshui <792179245@qq.com> Date: Fri, 19 Dec 2025 19:00:36 +0800 Subject: [PATCH 1/2] Add RISC-V RVV optimization support for zstd C library compilation This commit adds automatic RISC-V RVV (RISC-V Vector Extension) optimization support when building the zstd C library on RISC-V 64-bit platforms. Changes: - Detect riscv64 target architecture in build.rs - Automatically apply -march=rv64gcv flag (with fallback to -march=rv64gc) - Add -O3 optimization level for RISC-V builds - Use flag_if_supported_with_fallbacks to gracefully handle unsupported flags The optimization is automatically applied when compiling on riscv64 targets, with graceful fallback if the compiler doesn't support RVV extensions. Tested on: - RISC-V 64-bit platform (riscv64) - All tests pass (32 unit tests + 1 integration test + 4 doc tests) - Build successful with both debug and release profiles This enables better performance for zstd compression/decompression on RISC-V platforms that support vector extensions. Co-authored-by: gong-flying --- zstd-safe/zstd-sys/build.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/zstd-safe/zstd-sys/build.rs b/zstd-safe/zstd-sys/build.rs index b7f7e6b8..41df4eb9 100644 --- a/zstd-safe/zstd-sys/build.rs +++ b/zstd-safe/zstd-sys/build.rs @@ -172,6 +172,14 @@ fn compile_zstd() { .flag_if_supported("-ffunction-sections") .flag_if_supported("-fdata-sections") .flag_if_supported("-fmerge-all-constants"); + let target_arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap_or_default(); + if target_arch == "riscv64" { + flag_if_supported_with_fallbacks( + &mut config, + &["-march=rv64gcv", "-march=rv64gc"], + ); + config.flag_if_supported("-O3"); + } if cfg!(feature = "fat-lto") { config.flag_if_supported("-flto"); From d6a4758ee6a4a84877f20cdea96dd919424e45e9 Mon Sep 17 00:00:00 2001 From: Dayuxiaoshui <792179245@qq.com> Date: Fri, 26 Dec 2025 23:12:12 +0800 Subject: [PATCH 2/2] Make -O3 optimization conditional on release profile for RISC-V Update RISC-V RVV optimization to only apply -O3 flag in release builds, respecting the optimization level requested by the user. This addresses the review feedback to make optimization level conditional rather than always applied. Co-authored-by: gong-flying --- zstd-safe/zstd-sys/build.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/zstd-safe/zstd-sys/build.rs b/zstd-safe/zstd-sys/build.rs index 41df4eb9..19cc068c 100644 --- a/zstd-safe/zstd-sys/build.rs +++ b/zstd-safe/zstd-sys/build.rs @@ -178,7 +178,10 @@ fn compile_zstd() { &mut config, &["-march=rv64gcv", "-march=rv64gc"], ); - config.flag_if_supported("-O3"); + let profile = env::var("PROFILE").unwrap_or_default(); + if profile == "release" { + config.flag_if_supported("-O3"); + } } if cfg!(feature = "fat-lto") {