Enable RVV support in CMake and Bazel for RISC-V targets

Enable RISC-V Vector (RVV) by default when building natively on RISC-V
platforms (such as SpacemiT K1/K3) using CMake and Bazel:
- CMakeLists.txt: Detect riscv64 and riscv32, probe compiler support
  for -march=rv64gcv / -march=rv32gcv, and append architecture flags
  with auto-vectorization disabled (-fno-vectorize -fno-slp-vectorize).
  Support custom architecture flags via -DRISCV_COMPILER_FLAGS and
  provide -DUSE_RVV=OFF to disable.
- BUILD.bazel: Add RVV_COPTS for @platforms//cpu:riscv64 and
  @platforms//cpu:riscv32 with -fno-vectorize.
- docs/getting_started.md: Document native SBC builds, custom targets
  (SiFive X280, Coral 32-bit Zve32x), Bazel, and Blaze commands.

Test: cmake build on host and simulated riscv64; clang cross-compile tests
Bug: none
Change-Id: Iecb20b73b2934b6477e7e0c71b3539a8251e4e8b
TAG=agy
CONV=2f28dd8a-c892-4c29-8aed-606afd090398
Reviewed-on: https://chromium-review.googlesource.com/c/libyuv/libyuv/+/8349493
Reviewed-by: richard winterton <rrwinterton@gmail.com>
Commit-Queue: Frank Barchard <fbarchard@google.com>
This commit is contained in:
Frank Barchard 2026-09-03 13:20:08 -07:00 committed by libyuv-scoped@luci-project-accounts.iam.gserviceaccount.com
parent e58eeea369
commit af1aaca840
3 changed files with 106 additions and 16 deletions

View File

@ -56,7 +56,21 @@ PLATFORM_COPTS = select({
"-DGOOGLE_COMMANDLINEFLAGS_FULL_API",
]
ALL_COPTS = PLATFORM_COPTS
RVV_COPTS = select({
"@platforms//cpu:riscv64": [
"-march=rv64gcv",
"-fno-vectorize",
"-fno-slp-vectorize",
],
"@platforms//cpu:riscv32": [
"-march=rv32gcv",
"-fno-vectorize",
"-fno-slp-vectorize",
],
"//conditions:default": [],
})
ALL_COPTS = PLATFORM_COPTS + RVV_COPTS
ALL_NEON_COPTS = NEON_COPTS + PLATFORM_COPTS

View File

@ -139,6 +139,41 @@ int main(void) { return 0; }
add_definitions(-DLIBYUV_DISABLE_SME)
endif()
endif()
if(arch_lowercase MATCHES "^riscv")
option(USE_RVV "Enable RISC-V Vector" ON)
if(USE_RVV)
if(NOT RISCV_COMPILER_FLAGS)
if(CMAKE_SIZEOF_VOID_P EQUAL 8 OR arch_lowercase STREQUAL "riscv64")
set(RISCV_COMPILER_FLAGS "-march=rv64gcv")
else()
set(RISCV_COMPILER_FLAGS "-march=rv32gcv")
endif()
endif()
include(CheckCSourceCompiles)
set(OLD_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS})
set(OLD_CMAKE_TRY_COMPILE_TARGET_TYPE ${CMAKE_TRY_COMPILE_TARGET_TYPE})
set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} ${RISCV_COMPILER_FLAGS}")
set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
check_c_source_compiles("
int main(void) { return 0; }
" CAN_COMPILE_RVV)
set(CMAKE_REQUIRED_FLAGS ${OLD_CMAKE_REQUIRED_FLAGS})
set(CMAKE_TRY_COMPILE_TARGET_TYPE ${OLD_CMAKE_TRY_COMPILE_TARGET_TYPE})
if(CAN_COMPILE_RVV)
if(NOT CMAKE_C_FLAGS MATCHES "-march=" AND NOT CMAKE_C_FLAGS MATCHES "-mcpu=")
set(CMAKE_C_FLAGS "${RISCV_COMPILER_FLAGS} -fno-vectorize -fno-slp-vectorize ${CMAKE_C_FLAGS}")
set(CMAKE_CXX_FLAGS "${RISCV_COMPILER_FLAGS} -fno-vectorize -fno-slp-vectorize ${CMAKE_CXX_FLAGS}")
endif()
else()
add_definitions(-DLIBYUV_DISABLE_RVV)
endif()
else()
add_definitions(-DLIBYUV_DISABLE_RVV)
endif()
endif()
endif()
if(LOONGARCH64)

View File

@ -224,7 +224,61 @@ Install cmake: http://www.cmake.org/
## Building RISC-V target with cmake
### Prerequisite: build risc-v clang toolchain and qemu
### Native build on RISC-V SBC (e.g. SpacemiT K1/K3)
On a 64-bit RISC-V system, CMake automatically detects `riscv64` and enables
RVV with `-march=rv64gcv`:
cmake -B out/Release -DCMAKE_BUILD_TYPE=Release -DUNIT_TEST=ON
cmake --build out/Release
To build for 32-bit RISC-V targets:
cmake -B out/Release -DCMAKE_BUILD_TYPE=Release -DRISCV_COMPILER_FLAGS="-march=rv32gcv"
cmake --build out/Release
#### Customized Compiler Flags
Customized compiler flags can be passed with `-DRISCV_COMPILER_FLAGS="xxx"`.
When assigned, the specified flags are used instead of the default `-march=rv64gcv`:
Targeting SiFive X280:
cmake -B out/Release -DUNIT_TEST=ON \
-DCMAKE_BUILD_TYPE=Release \
-DRISCV_COMPILER_FLAGS="-mcpu=sifive-x280"
Targeting Coral (32-bit without floating point, Zve32x):
cmake -B out/Release -DUNIT_TEST=ON \
-DCMAKE_BUILD_TYPE=Release \
-DRISCV_COMPILER_FLAGS="-march=rv32imac_zve32x"
To disable RVV:
cmake -B out/Release -DCMAKE_BUILD_TYPE=Release -DUSE_RVV=OFF
### Building RISC-V with Bazel and Blaze
To build with Bazel for 64-bit or 32-bit RISC-V targets:
bazel test --platforms=@platforms//cpu:riscv64 //...
bazel test --platforms=@platforms//cpu:riscv32 //...
For custom targets such as SiFive X280 or Coral:
bazel test --platforms=@platforms//cpu:riscv64 --copt=-mcpu=sifive-x280 //...
bazel test --platforms=@platforms//cpu:riscv32 --copt=-march=rv32imac_zve32x //...
In google3 using Blaze:
blaze test --config=mpu64 //third_party/libyuv:libyuv_test
blaze run --config=mpu64 //third_party/libyuv:cpuid
blaze run --config=mpu64 //third_party/libyuv:yuvconvert
### Cross-compile for RISC-V target using QEMU
#### Prerequisite: build risc-v clang toolchain and qemu
If you don't have prebuilt clang and riscv64 qemu, run the script to download source and build them.
@ -232,7 +286,7 @@ If you don't have prebuilt clang and riscv64 qemu, run the script to download so
After running script, clang & qemu are built in `build-toolchain-qemu/riscv-clang/` & `build-toolchain-qemu/riscv-qemu/`.
### Cross-compile for RISC-V target
#### Cross-compile for RISC-V target
cmake -B out/Release/ -DUNIT_TEST=ON \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_TOOLCHAIN_FILE="./riscv_script/riscv-clang.cmake" \
@ -240,19 +294,6 @@ After running script, clang & qemu are built in `build-toolchain-qemu/riscv-clan
-DUSE_RVV=ON .
cmake --build out/Release/
#### Customized Compiler Flags
Customized compiler flags are supported by `-DRISCV_COMPILER_FLAGS="xxx"`.
If `-DRISCV_COMPILER_FLAGS="xxx"` is manually assigned, other compile flags(e.g disable -march=xxx) will not be appended.
Example:
cmake -B out/Release/ -DUNIT_TEST=ON \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_TOOLCHAIN_FILE="./riscv_script/riscv-clang.cmake" \
-DRISCV_COMPILER_FLAGS="-mcpu=sifive-x280" \
.
### Run on QEMU
#### Run libyuv_unittest on QEMU