Skip to content

Commit 997d9d9

Browse files
authored
Merge pull request #979 from rivosinc/upstream-rvv
Add RISC-V Vector support.
2 parents be56a35 + dc9fbff commit 997d9d9

File tree

14 files changed

+2017
-7
lines changed

14 files changed

+2017
-7
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
set(CMAKE_SYSTEM_PROCESSOR riscv64)
2+
set(triple riscv64-linux-gnu)
3+
4+
include(${CMAKE_CURRENT_LIST_DIR}/clang.cmake)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
set(CMAKE_SYSTEM_PROCESSOR riscv64)
2+
set(triple riscv64-linux-gnu)
3+
4+
include(${CMAKE_CURRENT_LIST_DIR}/gcc.cmake)

.github/workflows/cross-rvv.yml

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
name: RISC-V RVV cross-compilation build
2+
on: [push, pull_request]
3+
concurrency:
4+
group: ${{ github.workflow }}-${{ github.job }}-${{ github.ref }}
5+
cancel-in-progress: true
6+
env:
7+
LLVM_VERSION: "17"
8+
GCC_VERSION: "12"
9+
jobs:
10+
build:
11+
runs-on: ubuntu-22.04
12+
name: 'RISC-V RVV${{ matrix.vector_bits }}'
13+
strategy:
14+
matrix:
15+
vector_bits:
16+
- 128
17+
- 256
18+
- 512
19+
steps:
20+
- name: Setup GCC
21+
run: |
22+
sudo apt-get -y -qq update
23+
sudo apt-get -y -qq --no-install-suggests --no-install-recommends install gcc-${GCC_VERSION}-riscv64-linux-gnu g++-${GCC_VERSION}-riscv64-linux-gnu
24+
sudo update-alternatives --install /usr/bin/riscv64-linux-gnu-gcc riscv64-linux-gnu-gcc /usr/bin/riscv64-linux-gnu-gcc-${GCC_VERSION} 20
25+
sudo update-alternatives --install /usr/bin/riscv64-linux-gnu-g++ riscv64-linux-gnu-g++ /usr/bin/riscv64-linux-gnu-g++-${GCC_VERSION} 20
26+
- name: Setup LLVM
27+
run: |
28+
# Install latest LLVM stable
29+
curl -o llvm.sh https://apt.llvm.org/llvm.sh
30+
chmod u+x llvm.sh
31+
sudo ./llvm.sh ${LLVM_VERSION}
32+
sudo ln -srf $(which clang-${LLVM_VERSION}) /usr/bin/clang
33+
sudo ln -srf $(which clang++-${LLVM_VERSION}) /usr/bin/clang++
34+
rm llvm.sh
35+
- name: Setup QEMU
36+
uses: docker/[email protected]
37+
with:
38+
platforms: riscv64
39+
- name: Setup Ninja
40+
run: |
41+
sudo apt-get -y -qq install ninja-build
42+
- name: Checkout xsimd
43+
uses: actions/checkout@v3
44+
- name: Setup
45+
run: >
46+
cmake -S . -B _build
47+
-GNinja
48+
-DBUILD_TESTS=ON
49+
-DDOWNLOAD_DOCTEST=ON
50+
-DCMAKE_BUILD_TYPE=Release
51+
-DTARGET_ARCH=generic
52+
-DCMAKE_C_FLAGS="-march=rv64gcv_zvl${{ matrix.vector_bits }}b_zba_zbb_zbs -mrvv-vector-bits=zvl"
53+
-DCMAKE_CXX_FLAGS="-march=rv64gcv_zvl${{ matrix.vector_bits }}b_zba_zbb_zbs -mrvv-vector-bits=zvl"
54+
-DCMAKE_TOOLCHAIN_FILE=${{ github.workspace }}/.github/toolchains/clang-riscv64-linux-gnu.cmake
55+
- name: Build
56+
run: cmake --build _build
57+
- name: Testing xsimd
58+
run: >
59+
QEMU_CPU="rv64,zba=true,zbb=true,zbs=true,v=true,vlen=${{ matrix.vector_bits }},elen=64,vext_spec=v1.0"
60+
QEMU_LD_PREFIX="/usr/riscv64-linux-gnu"
61+
./test/test_xsimd
62+
working-directory: ${{ github.workspace }}/_build

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ ${XSIMD_INCLUDE_DIR}/xsimd/arch/xsimd_generic.hpp
4242
${XSIMD_INCLUDE_DIR}/xsimd/arch/xsimd_isa.hpp
4343
${XSIMD_INCLUDE_DIR}/xsimd/arch/xsimd_neon.hpp
4444
${XSIMD_INCLUDE_DIR}/xsimd/arch/xsimd_neon64.hpp
45+
${XSIMD_INCLUDE_DIR}/xsimd/arch/xsimd_rvv.hpp
4546
${XSIMD_INCLUDE_DIR}/xsimd/arch/xsimd_scalar.hpp
4647
${XSIMD_INCLUDE_DIR}/xsimd/arch/xsimd_sse2.hpp
4748
${XSIMD_INCLUDE_DIR}/xsimd/arch/xsimd_sse3.hpp
@@ -69,6 +70,7 @@ ${XSIMD_INCLUDE_DIR}/xsimd/types/xsimd_fma3_sse_register.hpp
6970
${XSIMD_INCLUDE_DIR}/xsimd/types/xsimd_fma4_register.hpp
7071
${XSIMD_INCLUDE_DIR}/xsimd/types/xsimd_generic_arch.hpp
7172
${XSIMD_INCLUDE_DIR}/xsimd/types/xsimd_register.hpp
73+
${XSIMD_INCLUDE_DIR}/xsimd/types/xsimd_rvv_register.hpp
7274
${XSIMD_INCLUDE_DIR}/xsimd/types/xsimd_sse2_register.hpp
7375
${XSIMD_INCLUDE_DIR}/xsimd/types/xsimd_sse3_register.hpp
7476
${XSIMD_INCLUDE_DIR}/xsimd/types/xsimd_sse4_1_register.hpp

docs/Doxyfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ INPUT = ../include/xsimd/types/xsimd_api.hpp \
2121
../include/xsimd/types/xsimd_fma4_register.hpp \
2222
../include/xsimd/types/xsimd_neon64_register.hpp \
2323
../include/xsimd/types/xsimd_neon_register.hpp \
24+
../include/xsimd/types/xsimd_rvv_register.hpp \
2425
../include/xsimd/types/xsimd_sse2_register.hpp \
2526
../include/xsimd/types/xsimd_sse3_register.hpp \
2627
../include/xsimd/types/xsimd_sse4_1_register.hpp \

include/xsimd/arch/xsimd_isa.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@
8080
#include "./xsimd_sve.hpp"
8181
#endif
8282

83+
#if XSIMD_WITH_RVV
84+
#include "./xsimd_rvv.hpp"
85+
#endif
86+
8387
#if XSIMD_WITH_WASM
8488
#include "./xsimd_wasm.hpp"
8589
#endif

include/xsimd/arch/xsimd_rvv.hpp

Lines changed: 1485 additions & 0 deletions
Large diffs are not rendered by default.

include/xsimd/config/xsimd_arch.hpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,14 +192,17 @@ namespace xsimd
192192
};
193193
using all_x86_architectures = arch_list<avx512bw, avx512dq, avx512cd, avx512f, fma3<avx2>, avx2, fma3<avx>, avx, fma4, fma3<sse4_2>, sse4_2, sse4_1, /*sse4a,*/ ssse3, sse3, sse2>;
194194
using all_sve_architectures = arch_list<detail::sve<512>, detail::sve<256>, detail::sve<128>>;
195+
using all_rvv_architectures = arch_list<detail::rvv<512>, detail::rvv<256>, detail::rvv<128>>;
195196
using all_arm_architectures = typename detail::join<all_sve_architectures, arch_list<neon64, neon>>::type;
197+
using all_riscv_architectures = all_rvv_architectures;
196198
using all_wasm_architectures = arch_list<wasm>;
197-
using all_architectures = typename detail::join<all_arm_architectures, all_x86_architectures, all_wasm_architectures>::type;
199+
using all_architectures = typename detail::join<all_arm_architectures, all_x86_architectures, all_riscv_architectures, all_wasm_architectures>::type;
198200

199201
using supported_architectures = typename detail::supported<all_architectures>::type;
200202

201203
using x86_arch = typename detail::supported<all_x86_architectures>::type::best;
202204
using arm_arch = typename detail::supported<all_arm_architectures>::type::best;
205+
using riscv_arch = typename detail::supported<all_riscv_architectures>::type::best;
203206
using best_arch = typename supported_architectures::best;
204207

205208
#ifdef XSIMD_DEFAULT_ARCH

include/xsimd/config/xsimd_config.hpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,19 @@
285285
#define XSIMD_SVE_BITS 0
286286
#endif
287287

288+
/**
289+
* @ingroup xsimd_config_macro
290+
*
291+
* Set to 1 if RVV is available and bit width is pre-set at compile-time, to 0 otherwise.
292+
*/
293+
#if defined(__riscv_vector) && defined(__riscv_v_fixed_vlen) && __riscv_v_fixed_vlen > 0
294+
#define XSIMD_WITH_RVV 1
295+
#define XSIMD_RVV_BITS __riscv_v_fixed_vlen
296+
#else
297+
#define XSIMD_WITH_RVV 0
298+
#define XSIMD_RVV_BITS 0
299+
#endif
300+
288301
/**
289302
* @ingroup xsimd_config_macro
290303
*
@@ -354,7 +367,7 @@
354367

355368
#endif
356369

357-
#if !XSIMD_WITH_SSE2 && !XSIMD_WITH_SSE3 && !XSIMD_WITH_SSSE3 && !XSIMD_WITH_SSE4_1 && !XSIMD_WITH_SSE4_2 && !XSIMD_WITH_AVX && !XSIMD_WITH_AVX2 && !XSIMD_WITH_FMA3_SSE && !XSIMD_WITH_FMA4 && !XSIMD_WITH_FMA3_AVX && !XSIMD_WITH_FMA3_AVX2 && !XSIMD_WITH_AVX512F && !XSIMD_WITH_AVX512CD && !XSIMD_WITH_AVX512DQ && !XSIMD_WITH_AVX512BW && !XSIMD_WITH_NEON && !XSIMD_WITH_NEON64 && !XSIMD_WITH_SVE && !XSIMD_WITH_WASM
370+
#if !XSIMD_WITH_SSE2 && !XSIMD_WITH_SSE3 && !XSIMD_WITH_SSSE3 && !XSIMD_WITH_SSE4_1 && !XSIMD_WITH_SSE4_2 && !XSIMD_WITH_AVX && !XSIMD_WITH_AVX2 && !XSIMD_WITH_FMA3_SSE && !XSIMD_WITH_FMA4 && !XSIMD_WITH_FMA3_AVX && !XSIMD_WITH_FMA3_AVX2 && !XSIMD_WITH_AVX512F && !XSIMD_WITH_AVX512CD && !XSIMD_WITH_AVX512DQ && !XSIMD_WITH_AVX512BW && !XSIMD_WITH_NEON && !XSIMD_WITH_NEON64 && !XSIMD_WITH_SVE && !XSIMD_WITH_RVV && !XSIMD_WITH_WASM
358371
#endif
359372

360373
#endif

include/xsimd/config/xsimd_cpuid.hpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
#include <algorithm>
1616
#include <cstring>
1717

18-
#if defined(__linux__) && (defined(__ARM_NEON) || defined(_M_ARM))
18+
#if defined(__linux__) && (defined(__ARM_NEON) || defined(_M_ARM) || defined(__riscv_vector))
1919
#include <asm/hwcap.h>
2020
#include <sys/auxv.h>
2121
#endif
@@ -53,6 +53,7 @@ namespace xsimd
5353
unsigned neon : 1;
5454
unsigned neon64 : 1;
5555
unsigned sve : 1;
56+
unsigned rvv : 1;
5657

5758
// version number of the best arch available
5859
unsigned best;
@@ -85,6 +86,18 @@ namespace xsimd
8586
#endif
8687
best = sve::version() * sve;
8788

89+
#elif defined(__riscv_vector) && defined(__riscv_v_fixed_vlen) && __riscv_v_fixed_vlen > 0
90+
91+
#if defined(__linux__) && (!defined(__ANDROID_API__) || __ANDROID_API__ >= 18)
92+
#ifndef HWCAP_V
93+
#define HWCAP_V (1 << ('V' - 'A'))
94+
#endif
95+
rvv = bool(getauxval(AT_HWCAP) & HWCAP_V);
96+
#else
97+
rvv = 0;
98+
#endif
99+
100+
best = ::xsimd::rvv::version() * rvv;
88101
#elif defined(__x86_64__) || defined(__i386__) || defined(_M_AMD64) || defined(_M_IX86)
89102
auto get_cpuid = [](int reg[4], int func_id) noexcept
90103
{

0 commit comments

Comments
 (0)