mirror of
https://github.com/official-stockfish/Stockfish.git
synced 2026-07-22 12:47:08 +00:00
[RfC] RISC-V port and universal binary
Performance on Spacemit K3, thanks @edolnx for testing master: Total time (ms) : 65200 Nodes searched : 3493826 Nodes/second : 53586 riscv-scalable-port: Total time (ms) : 15834 Nodes searched : 3493826 Nodes/second : 220653 Also thanks to @camel-cdr for guidance on RVV programming, and https://cloud-v.co for supplying an RVV instance to test with passed STC: LLR: 2.81 (-2.94,2.94) <0.00,2.00> Total: 1152 W: 527 L: 108 D: 517 Ptnml(0-2): 0, 17, 167, 348, 44 https://tests.stockfishchess.org/tests/view/6a39895b3036e45021aeb368 ## Summary We've had a `riscv64` target for a while, but haven't really optimized for it, in particular the vector extension (RVV). RVV, like SVE, is based on a scalable vector system where the vector length ranges from 128 to 65536. In practice implementations are between 128 and 2048, and 256 bits is quite common (e.g. the Spacemit K3 system above). Unfortunately this doesn't fit well into the rest of our code which assumes a fixed vector length, so what I've done is bypass the `VECTOR` ifdef (which now basically means "FIXED_LENGTH_VECTOR") and just have RVV-specific paths. The ability to explicitly control `vl` makes the code quite readable, in my opinion. We use LMUL>1 in most places to take advantage of multi-vector instructions. Generally the LMULs were chosen to best support a 256-bit vlen, which is very common, but by virtue of how the vlen control works, the code works with any vlen. In a couple places, i.e., `get_changed_pieces` and `AffineTransformSparseInput::propagate`, we have separate implementations depending on the vlen, because the optimal LMUL varies a lot between implementations. One little wrinkle is that `load_as` is compiled to a sequence of byte loads, because although unaligned loads are legal in RVA23, the spec says that they *may* be extremely slow (even though they usually aren't, in actual hw), so compilers are conservative. Thus I aligned the relevant buffers and made the semantics of `load_as` that the operand is aligned, by adding a runtime assertion. ### Universal binary Adding a universal binary is pretty easy and we can just cross-compile. There are two targets: baseline rv64gc and riscv64-rva23, which is actually a smaller subset of RVA23 that also works on some older processors that don't support the full thing. We use clang because GCC, until recently, has a nasty bug with LTO and RVV. Like the universal ARM and x86 builds, we check all the builds in CI. In this case we run bench with multiple vlens, 128 through 1024. In the meantime I deleted the existing broken and unused riscv64 tests. ### Follow-ups - Optimizations - zvdot4a8i path closes https://github.com/official-stockfish/Stockfish/pull/6920 No functional change
This commit is contained in:
committed by
Joost VandeVondele
parent
99489f57dd
commit
d5bbc6b67d
@@ -53,6 +53,26 @@ get_flags() {
|
||||
flags=$(normalize_ws "$flags")
|
||||
}
|
||||
|
||||
# Populate $flags from the RISC-V isa string; split the single-letter
|
||||
# base (e.g., rv64imafdcv), and keep multi-letter extensions.
|
||||
get_riscv_flags() {
|
||||
if [ -r "$cpuinfo_path" ]; then
|
||||
isa=$(awk -F: '/^isa[ \t]*:/{print $2; exit}' "$cpuinfo_path" 2>/dev/null)
|
||||
else
|
||||
isa=''
|
||||
fi
|
||||
isa=$(printf '%s\n' "$isa" | tr '[:upper:]' '[:lower:]')
|
||||
flags=$(printf '%s\n' "$isa" | awk '{
|
||||
gsub(/^[ \t]*rv(32|64)/, "");
|
||||
n = split($0, ext, "_");
|
||||
out = "";
|
||||
for (i = 1; i <= length(ext[1]); i++) out = out " " substr(ext[1], i, 1);
|
||||
for (i = 2; i <= n; i++) out = out " " ext[i];
|
||||
print out;
|
||||
}')
|
||||
flags=$(normalize_ws "$flags")
|
||||
}
|
||||
|
||||
# Populate $flags from sysctl on Darwin x86_64.
|
||||
get_sysctl_flags() {
|
||||
if [ -n "${GP_SYSCTL_FEATURES:-}" ]; then
|
||||
@@ -193,6 +213,16 @@ EOF
|
||||
)
|
||||
}
|
||||
|
||||
set_arch_riscv64() {
|
||||
get_riscv_flags
|
||||
true_arch=$(
|
||||
select_arch_from_table <<'EOF'
|
||||
riscv64-rva23|match_flags|v zba zbb zbs zicond
|
||||
riscv64|match_true|
|
||||
EOF
|
||||
)
|
||||
}
|
||||
|
||||
set_arch_x86_64() {
|
||||
true_arch=$(
|
||||
select_arch_from_table <<'EOF'
|
||||
@@ -324,7 +354,7 @@ case $uname_s in
|
||||
set_arch_loongarch64
|
||||
;;
|
||||
riscv64)
|
||||
true_arch='riscv64'
|
||||
set_arch_riscv64
|
||||
;;
|
||||
e2k*)
|
||||
true_arch='e2k'
|
||||
|
||||
Reference in New Issue
Block a user