Skip to content

rv64iv: 2vx12 sgemm micro-kernel for VLEN 256 - #965

Open
HiderX wants to merge 1 commit into
flame:masterfrom
HiderX:rviv-sgemm-2vx12
Open

HiderX wants to merge 1 commit into
flame:masterfrom
HiderX:rviv-sgemm-2vx12

Conversation

@HiderX

@HiderX HiderX commented Oct 26, 2026

Copy link
Copy Markdown

This PR adds a 2vx12 sgemm micro-kernel to rv64iv and uses it when VLEN is 256.

The kernel. bli_sgemm_rviv_2vx12 (assembly plus C wrapper) has MR = 2·v, one LMUL-2 vector group of A, and NR = 12.

  • 12 LMUL-2 accumulators hold the tile.
  • A is double-buffered.
  • Each k-step does one LMUL-2 load of A, 12 scalar B loads and 12 vfmacc.vf. Each loaded A element feeds 12
    FMAs, against 4 in 4vx4.
  • It uses only caller-saved registers and instructions common to RV32 and RV64, so the file also assembles for
    rv32iv, which shares the rviv kernel set.

Context. bli_cntx_init_rv64iv selects the kernel for sgemm when v == 8 (VLEN 256), with MC = 320 and KC = 512
(NC stays 3072). Every other datatype and every other VLEN keeps the existing 4vx4 kernels and block sizes.

Why VLEN 256 only. That is the VLEN where we measured it. At VLEN ≥ 1024, an MR = 2·v by NR = 12 tile gives
mr·nr·sizeof(dcomplex) above the default BLIS_STACK_BUF_MAX_SIZE (8192), and bli_gks aborts; #963 discusses the
same limit for the sifive configurations.

Measurements

The board is a SpacemiT K3: X100 cores, VLEN 256, 2.2 GHz.

Builds. All three are clang 21.1.8 builds, configured with ./configure --enable-cblas -t pthreads CC=clang rv64iv.

Harness. Row-major cblas_sgemm on square n ∈ {2048, 3072, 4096}, with 2 warm-ups and 10 timed reps per size, in 4
processes per configuration with the order alternating. Before timing, 256 sampled entries are checked against an
fp64 reference. The figure reported is the geometric mean over the three sizes of the per-size medians, in GFLOP/s.

1 thread (cpu 4) 8 threads (cpus 0–7, BLIS_NUM_THREADS=8, default ways)
master a8037cf (4vx4) 26.68 50.54
this PR (2vx12) 29.85 (+11.9 %) 134.52 (+166 %)
#955 (2vx8), for reference 26.96 96.56

Per size, against master:

n = 2048 n = 3072 n = 4096
1 thread +10.3 % +14.8 % +10.7 %
8 threads +163 % +173 % +163 %

The final commit (VLEN-256-only selection) was re-run against master at 8 threads: 134.65 against 50.50 GFLOP/s
(+167 %).

Testing

  • make checkblis-fast and make checkblas pass on the K3 for the final commit built with clang 21.1.8. They also
    pass for the first version of the patch (selected for VLEN ≥ 256, so the same code on this board) built with
    clang 21.1.8 and with gcc 15.2.0.
  • The fp64 reference check passes at n = 1, 7, 13, 33, 100, 333, 1000 and 1023, with 1 and 8 threads, and at every
    benchmark size.
  • The assembly file assembles for rv64gcv and rv32gcv with LLVM.

Relation to #955

#955 (draft) adds a 2vx8 sgemm and an 8x8 dgemm for VLEN ≥ 256, and it edits the same block of
bli_cntx_init_rv64iv.c. If #955 lands first I can rebase on it, or the 2vx12 sgemm could be folded into it. On the
same board, 2vx12 was 10.7 % (1 thread) and 39 % (8 threads) faster than 2vx8; see the table.

Disclosure: an AI coding agent (Anthropic Claude) prepared this patch, its validation runs and this description on
behalf of the account owner. All numbers above come from archived runs on the board.

Add bli_sgemm_rviv_2vx12 (MR = 2*v at LMUL 2, NR = 12) and select it for
sgemm when VLEN is 256 (v == 8). Twelve LMUL-2 accumulators hold the tile,
A is double-buffered, and each k-step issues 12 vfmacc.vf per vector load
of A (the 4vx4 kernel issues 4). MC = 320 (rounded to MR) and KC = 512.

The selection is limited to VLEN 256, where the kernel was measured: at
VLEN >= 1024 an MR = 2*v by NR = 12 tile would exceed the default
BLIS_STACK_BUF_MAX_SIZE. All other datatypes and VLENs keep 4vx4.

The assembly uses only caller-saved registers and RV32/RV64 common
instructions, so it also assembles for rv32iv.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant