Files
stockfish/.github/workflows/universal_compilation.yml
anematodeandJoost VandeVondele d5bbc6b67d [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
2026-07-03 20:29:50 +02:00

319 lines
11 KiB
YAML

name: Universal Compilation
on:
workflow_call:
jobs:
UniversalLinux:
name: Linux
runs-on: ubuntu-22.04
defaults:
run:
shell: bash
steps:
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
with:
persist-credentials: false
fetch-depth: 0
- name: Install GCC 15
uses: egor-tensin/setup-gcc@a2861a8b8538f49cf2850980acccf6b05a1b2ae4 # v2.0
with:
version: 15
- name: Download SDE package
uses: petarpetrovt/setup-sde@31aa4a8e85e109bef00f1d838613fcc6ec421271 # v5.0
with:
environmentVariableName: SDE_DIR
sdeVersion: 9.58.0
- name: Build universal binary
run: make -j4 -C src ARCH=x86-64-universal profile-build RUN_PREFIX="$SDE_DIR/sde -future --"
- name: Strip binary
run: strip src/stockfish
- name: Rename binary
run: mv src/stockfish stockfish-linux-x86-64-universal
- name: Extract the bench number from the commit history
run: |
for hash in $(git rev-list -100 HEAD); do
benchref=$(git show -s $hash | tac | grep -m 1 -o -x '[[:space:]]*\b[Bb]ench[ :]\+[1-9][0-9]\{5,7\}\b[[:space:]]*' | sed 's/[^0-9]//g') && break || true
done
[[ -n "$benchref" ]] && echo "benchref=$benchref" >> $GITHUB_ENV && echo "From commit: $hash" && echo "Reference bench: $benchref" || echo "No bench found"
- name: Check arch selection under SDE
run: ./scripts/check_universal.sh ./stockfish-linux-x86-64-universal "$SDE_DIR/sde" "$benchref"
- name: Remove non-src files
run: git clean -fxd -- src
- name: Upload artifact for (pre)-release
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
with:
name: linux x86-64-universal
path: |
.
!.git
!.output
UniversalWindows:
name: Windows
runs-on: windows-2022
defaults:
run:
shell: msys2 {0}
steps:
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
with:
persist-credentials: false
fetch-depth: 0
- name: Setup msys and install required packages
uses: msys2/setup-msys2@e9898307ac31d1a803454791be09ab9973336e1c # v2.31.1
with:
msystem: mingw64
install: mingw-w64-x86_64-gcc make git zip
- name: Download SDE package
uses: petarpetrovt/setup-sde@31aa4a8e85e109bef00f1d838613fcc6ec421271 # v5.0
with:
environmentVariableName: SDE_DIR
sdeVersion: 9.58.0
- name: Build universal binary
run: |
SDE_PATH=$(cygpath "$SDE_DIR")
make -j4 -C src ARCH=x86-64-universal profile-build RUN_PREFIX="$SDE_PATH/sde.exe -future --"
- name: Strip binary
run: strip src/stockfish.exe
- name: Rename binary
run: mv src/stockfish.exe stockfish-windows-x86-64-universal.exe
- name: Extract the bench number from the commit history
run: |
for hash in $(git rev-list -100 HEAD); do
benchref=$(git show -s $hash | tac | grep -m 1 -o -x '[[:space:]]*\b[Bb]ench[ :]\+[1-9][0-9]\{5,7\}\b[[:space:]]*' | sed 's/[^0-9]//g') && break || true
done
[[ -n "$benchref" ]] && echo "benchref=$benchref" >> $GITHUB_ENV && echo "From commit: $hash" && echo "Reference bench: $benchref" || echo "No bench found"
- name: Check arch selection under SDE
run: ./scripts/check_universal.sh ./stockfish-windows-x86-64-universal.exe "$SDE_DIR/sde" "$benchref"
- name: Remove non-src files
run: git clean -fxd -- src
- name: Upload artifact for (pre)-release
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
with:
name: windows x86-64-universal
path: |
.
!.git
!.output
UniversalLinuxARM64:
name: Linux arm64
runs-on: ubuntu-22.04-arm
defaults:
run:
shell: bash
steps:
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
with:
persist-credentials: false
fetch-depth: 0
- name: Install GCC 15
uses: egor-tensin/setup-gcc@a2861a8b8538f49cf2850980acccf6b05a1b2ae4 # v2.0
with:
version: 15
- name: Install qemu-user
run: |
sudo apt-get update
sudo apt-get install -y qemu-user
- name: Build universal binary
run: make -j4 -C src ARCH=arm64-universal profile-build
- name: Strip binary
run: strip src/stockfish
- name: Rename binary
run: mv src/stockfish stockfish-linux-arm64-universal
- name: Extract the bench number from the commit history
run: |
for hash in $(git rev-list -100 HEAD); do
benchref=$(git show -s $hash | tac | grep -m 1 -o -x '[[:space:]]*\b[Bb]ench[ :]\+[1-9][0-9]\{5,7\}\b[[:space:]]*' | sed 's/[^0-9]//g') && break || true
done
[[ -n "$benchref" ]] && echo "benchref=$benchref" >> $GITHUB_ENV && echo "From commit: $hash" && echo "Reference bench: $benchref" || echo "No bench found"
- name: Check arch selection under qemu
run: ./scripts/check_universal_arm.sh ./stockfish-linux-arm64-universal "$benchref"
- name: Remove non-src files
run: git clean -fxd -- src
- name: Upload artifact for (pre)-release
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
with:
name: linux arm64-universal
path: |
.
!.git
!.output
UniversalWindowsARM64:
name: Windows arm64
runs-on: windows-11-arm
defaults:
run:
shell: msys2 {0}
steps:
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
with:
persist-credentials: false
fetch-depth: 0
- name: Setup msys and install required packages
uses: msys2/setup-msys2@e9898307ac31d1a803454791be09ab9973336e1c # v2.31.1
with:
msystem: clangarm64
install: mingw-w64-clang-aarch64-clang make git zip
- name: Build universal binary
run: make -j4 -C src ARCH=arm64-universal profile-build COMP=clang COMPCXX=clang++
- name: Strip binary
run: strip src/stockfish.exe
- name: Extract the bench number from the commit history
run: |
for hash in $(git rev-list -100 HEAD); do
benchref=$(git show -s $hash | tac | grep -m 1 -o -x '[[:space:]]*\b[Bb]ench[ :]\+[1-9][0-9]\{5,7\}\b[[:space:]]*' | sed 's/[^0-9]//g') && break || true
done
[[ -n "$benchref" ]] && echo "benchref=$benchref" >> $GITHUB_ENV && echo "From commit: $hash" && echo "Reference bench: $benchref" || echo "No bench found"
- name: Verify bench and compiler
working-directory: src
run: |
../tests/signature.sh $benchref
./stockfish compiler 2>&1 | grep -q DOTPROD || { echo "compiler output missing DOTPROD" >&2; exit 1; }
- name: Rename binary
run: mv src/stockfish.exe stockfish-windows-arm64-universal.exe
- name: Remove non-src files
run: git clean -fxd -- src
- name: Upload artifact for (pre)-release
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
with:
name: windows arm64-universal
path: |
.
!.git
!.output
UniversalMacOS:
name: macOS
runs-on: macos-15
defaults:
run:
shell: bash
steps:
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
with:
persist-credentials: false
fetch-depth: 0
# macos-15 is Apple silicon; Rosetta 2 is needed to exercise the x86-64 slice
- name: Install Rosetta 2
run: softwareupdate --install-rosetta --agree-to-license
- name: Build universal binary
run: make -j4 -C src macos-lipo COMP=clang
- name: Rename binary
run: mv src/stockfish stockfish-macos-universal
- name: Verify binary runs natively and under Rosetta
run: ./scripts/check_universal_macos.sh ./stockfish-macos-universal
- name: Remove non-src files
run: git clean -fxd -- src
- name: Upload artifact for (pre)-release
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
with:
name: macos universal
path: |
.
!.git
!.output
UniversalLinuxRISCV64:
name: Linux riscv64
runs-on: ubuntu-22.04
defaults:
run:
shell: bash
env:
QEMU_DEB: https://launchpad.net/ubuntu/+archive/primary/+files/qemu-user_9.2.1+ds-1ubuntu5_amd64.deb
steps:
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
with:
persist-credentials: false
fetch-depth: 0
- name: Install clang and the riscv cross runtime
run: |
wget -qO- https://apt.llvm.org/llvm-snapshot.gpg.key | sudo tee /etc/apt/trusted.gpg.d/apt.llvm.org.asc >/dev/null
echo "deb http://apt.llvm.org/jammy/ llvm-toolchain-jammy-20 main" | sudo tee /etc/apt/sources.list.d/llvm.list >/dev/null
sudo apt-get update
sudo apt-get install -y clang-20 lld-20 llvm-20 g++-riscv64-linux-gnu
echo "/usr/lib/llvm-20/bin" >> "$GITHUB_PATH"
- name: Install qemu
run: |
curl -sL -o /tmp/qemu.deb "$QEMU_DEB"
dpkg -x /tmp/qemu.deb /tmp/qemu
/tmp/qemu/usr/bin/qemu-riscv64 --version
echo "/tmp/qemu/usr/bin" >> "$GITHUB_PATH"
- name: Build universal binary
run: |
make -j4 -C src ARCH=riscv64-universal build \
COMP=clang COMPCXX=clang++ \
EXTRACXXFLAGS="--target=riscv64-linux-gnu" \
EXTRALDFLAGS="--target=riscv64-linux-gnu -fuse-ld=lld" \
OBJCOPY=llvm-objcopy
- name: Strip binary
run: llvm-strip src/stockfish
- name: Rename binary
run: mv src/stockfish stockfish-linux-riscv64-universal
- name: Extract the bench number from the commit history
run: |
for hash in $(git rev-list -100 HEAD); do
benchref=$(git show -s $hash | tac | grep -m 1 -o -x '[[:space:]]*\b[Bb]ench[ :]\+[1-9][0-9]\{5,7\}\b[[:space:]]*' | sed 's/[^0-9]//g') && break || true
done
[[ -n "$benchref" ]] && echo "benchref=$benchref" >> $GITHUB_ENV && echo "From commit: $hash" && echo "Reference bench: $benchref" || echo "No bench found"
- name: Check arch selection under qemu
run: |
QEMU=qemu-riscv64 QEMU_LD_PREFIX=/usr/riscv64-linux-gnu \
./scripts/check_universal_riscv.sh ./stockfish-linux-riscv64-universal "$benchref"
- name: Remove non-src files
run: git clean -fxd -- src
- name: Upload artifact for (pre)-release
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
with:
name: linux riscv64-universal
path: |
.
!.git
!.output