From 53df7f9a6363de2c63983d213bef12012a029c6b Mon Sep 17 00:00:00 2001 From: michael tesch Date: Wed, 21 Oct 2026 22:51:20 -0500 Subject: [PATCH 1/3] arm64: VORTEXM4 TRMM kernel stores four rows of ZA per move s2_c_store_alpha() (C = alpha ZA in the TRMM kernel, C += alpha ZA in the GEMM kernel for alpha other than 1 and -1) read ZA one slice at a time. It now reads four slices per MOVA, as s2_c_store(). This makes the TRMM kernel about 1.5x (n = 1024) to 2.4x (n = 256) faster. Part of #6147 --- kernel/arm64/sme2_gemm_tile.h | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/kernel/arm64/sme2_gemm_tile.h b/kernel/arm64/sme2_gemm_tile.h index 4200e5bc25..02db94afe3 100644 --- a/kernel/arm64/sme2_gemm_tile.h +++ b/kernel/arm64/sme2_gemm_tile.h @@ -508,15 +508,29 @@ S2_INL void s2_c_store(const int TM, const int TN, FLOAT *C, BLASLONG ldc, int m } } +/* alpha z, plus the C row at p (add). */ +S2_INL s2_v s2_alpha_v(s2_v z, FLOAT *p, svbool_t pg, FLOAT alpha, int add) S2_S { + return add ? S2_MLA(S2_LD1(pg, p), z, alpha) : S2_MUL(z, alpha); +} /* C = C + alpha ZA (add) or C = alpha ZA from a row of TN tiles: rows < mrows, columns < ncols. */ S2_INL void s2_c_store_alpha(const int TN, FLOAT *C, BLASLONG ldc, int mrows, int ncols, FLOAT alpha, int add) S2_S S2_ZA { - for (int s = 0; s < mrows; ++s) { - FLOAT *row = C + (BLASLONG)s * ldc; + int s = 0; + for (; s + 4 <= mrows; s += 4) { /* four rows per ZA move (MOVA vg4) */ + S2_UNROLL for (int Q = 0; Q < TN; ++Q) { + const svbool_t pg = S2_PW(Q * S2_VL, ncols); + const s2_v4 q = s2_rdh4(Q, s); + S2_UNROLL for (int u = 0; u < 4; ++u) { + FLOAT *p = C + (BLASLONG)(s + u) * ldc + Q * S2_VL; + S2_ST1(pg, p, s2_alpha_v(s2_get4(q, u), p, pg, alpha, add)); + } + } + } + for (; s < mrows; ++s) { S2_UNROLL for (int Q = 0; Q < TN; ++Q) { const svbool_t pg = S2_PW(Q * S2_VL, ncols); - const s2_v z = s2_rdh(Q, s); - S2_ST1(pg, row + Q * S2_VL, add ? S2_MLA(S2_LD1(pg, row + Q * S2_VL), z, alpha) : S2_MUL(z, alpha)); + FLOAT *p = C + (BLASLONG)s * ldc + Q * S2_VL; + S2_ST1(pg, p, s2_alpha_v(s2_rdh(Q, s), p, pg, alpha, add)); } } } From 9694c366474160d0d273297a02babad813063889 Mon Sep 17 00:00:00 2001 From: michael tesch Date: Wed, 21 Oct 2026 23:02:56 -0500 Subject: [PATCH 2/3] arm64: VORTEXM4 left TRMM kernel skips zero tiles at the diagonal In the 64-row block on the diagonal of a left TRMM, the A panel rows of tile Q are zero for VL * Q (lower: the last) steps. The kernel now leaves out the outer products of those tiles; about 4% (fp32) and 12% (fp64) faster at n = 256. Part of #6147 --- kernel/arm64/gemm_kernel_sme2.c | 46 ++++++++++++++++++++++++++++----- 1 file changed, 40 insertions(+), 6 deletions(-) diff --git a/kernel/arm64/gemm_kernel_sme2.c b/kernel/arm64/gemm_kernel_sme2.c index 9c060a0cd8..7ff6bc1d29 100644 --- a/kernel/arm64/gemm_kernel_sme2.c +++ b/kernel/arm64/gemm_kernel_sme2.c @@ -62,15 +62,49 @@ static inline void s2p_krange(BLASLONG i0, BLASLONG j0, int wm, int wn, BLASLONG #ifdef HAVE_SME2_GEMM #pragma clang attribute push(__attribute__((target("sme2"))), apply_to = function) -typedef void (*s2p_bfn)(BLASLONG, const FLOAT *, int, const FLOAT *, int, FLOAT *, BLASLONG, FLOAT, int) S2_S S2_ZA; +typedef void (*s2p_bfn)(BLASLONG, const FLOAT *, int, const FLOAT *, int, FLOAT *, BLASLONG, FLOAT, int, + BLASLONG) S2_S S2_ZA; + +#if defined(TRMMKERNEL) && defined(LEFT) +/* Steps [l0, l1) at the diagonal: tile Q only from step g + Q * VL on (TRANSA: before step g + (Q + 1) * VL). */ +S2_INL void s2p_mac_diag(const int TN, BLASLONG l0, BLASLONG l1, BLASLONG g, const FLOAT *a, int wm, const FLOAT *b, + int wn) S2_S S2_ZA { + const svbool_t pt = S2_PT(), pb = S2_PW(0, wn); + for (BLASLONG l = l0; l < l1; ++l) { + const s2_v bv = S2_LD1(pb, b + l * wn); + const s2_v4 a0 = s2_ld4n(a + l * wm, wm); + const s2_v4 a1 = TN > 4 ? s2_ld4n(a + l * wm + 4 * S2_VL, wm - 4 * S2_VL) : a0; + const BLASLONG q = (l - g) / S2_VL; +#ifndef TRANSA + S2_UNROLL for (int Q = 0; Q < TN; ++Q) if (Q <= q) s2_mopa(Q, pt, bv, s2_pick(a0, a1, Q)); +#else + S2_UNROLL for (int Q = 0; Q < TN; ++Q) if (Q >= q) s2_mopa(Q, pt, bv, s2_pick(a0, a1, Q)); +#endif + } +} +/* Left TRMM: the diagonal steps through s2p_mac_diag (g: the step of the diagonal element in row 0). */ +#ifndef TRANSA +#define S2P_MAC_BLK(TN, BF, SUB) \ + const BLASLONG t = s2_clip(g + (TN - 1) * S2_VL, kb); \ + s2p_mac_diag(TN, 0, t, g, a, wm, b, wn); \ + s2p_mac(TN, BF, SUB, kb - t, a + t * wm, wm, b + t * wn, wn); +#else +#define S2P_MAC_BLK(TN, BF, SUB) \ + const BLASLONG t = s2_clip(g + S2_VL, kb); \ + s2p_mac(TN, BF, SUB, t, a, wm, b, wn); \ + s2p_mac_diag(TN, t, kb, g, a, wm, b, wn); +#endif +#else +#define S2P_MAC_BLK(TN, BF, SUB) s2p_mac(TN, BF, SUB, kb, a, wm, b, wn); +#endif /* Block of wm rows and wn columns of C; cm 0: C += A B, 1: C -= A B, 2: C += alpha A B, 3: C = alpha A B. */ #define S2P_DEF(TN, BF, SUB) \ S2_NOINL void s2p_blk_##TN##_##BF##_##SUB(BLASLONG kb, const FLOAT *a, int wm, const FLOAT *b, int wn, FLOAT *C, \ - BLASLONG ldc, FLOAT alpha, int cm) S2_S S2_ZA { \ + BLASLONG ldc, FLOAT alpha, int cm, BLASLONG g) S2_S S2_ZA { \ if (cm < 2) s2_c_load(1, TN, C, ldc, wn, wm, S2_LOAD, (FLOAT)1); \ else svzero_za(); \ - s2p_mac(TN, BF, SUB, kb, a, wm, b, wn); \ + S2P_MAC_BLK(TN, BF, SUB) \ if (cm < 2) s2_c_store(1, TN, C, ldc, wn, wm); \ else s2_c_store_alpha(TN, C, ldc, wn, wm, alpha, cm == 2); \ } @@ -105,9 +139,9 @@ __arm_new("za") __arm_locally_streaming static void s2p_drive(BLASLONG m, BLASLO const int wm = m - i0 < S2_NR ? (int)(m - i0) : S2_NR; BLASLONG ks, kl; s2p_krange(i0, j0, wm, wn, k, offset, &ks, &kl); - s2p_blk[(wm + S2_VL - 1) / S2_VL - 1][wn == S2_VL][cm == 1](kl, sa + i0 * k + ks * wm, wm, - sb + j0 * k + ks * wn, wn, C + i0 + j0 * ldc, - ldc, alpha, cm); + s2p_blk[(wm + S2_VL - 1) / S2_VL - 1][wn == S2_VL][cm == 1](kl, sa + i0 * k + ks * wm, wm, sb + j0 * k + ks * wn, + wn, C + i0 + j0 * ldc, ldc, alpha, cm, + offset + i0 - ks); } j0 += wn; } From d3182e870ebb0b07766fd1822b290be2550a8590 Mon Sep 17 00:00:00 2001 From: michael tesch Date: Wed, 21 Oct 2026 23:08:40 -0500 Subject: [PATCH 3/3] arm64: VORTEXM4 TRMM panel copies in one streaming-mode call The triangle copies zeroed with memset and fixed the diagonal block with scalar stores on the core, between two streaming copies. The kernel then had to read these lines back from the core's L1. Now a copy of more than 512 elements is one streaming-mode call: the kept part with s2c_rows_s/s2c_cols_s, the zero part and the diagonal block with predicated streaming stores. Part of #6147 --- kernel/arm64/sme2_gemm_copy.h | 30 ++++++++++++++++ kernel/arm64/trmm_ncopy_sme2.c | 63 +++++++++++++++++++--------------- kernel/arm64/trmm_tcopy_sme2.c | 62 ++++++++++++++++++--------------- 3 files changed, 101 insertions(+), 54 deletions(-) diff --git a/kernel/arm64/sme2_gemm_copy.h b/kernel/arm64/sme2_gemm_copy.h index 50c741941d..49ce80ba0a 100644 --- a/kernel/arm64/sme2_gemm_copy.h +++ b/kernel/arm64/sme2_gemm_copy.h @@ -46,11 +46,13 @@ static inline BLASLONG s2c_clip(BLASLONG x, BLASLONG lo, BLASLONG hi) { return x #define S2C_WL(a, b) svwhilelt_b64_s64(a, b) #define S2C_LD svld1_f64 #define S2C_ST svst1_f64 +#define S2C_DUP svdup_n_f64 #else #define S2C_VL 16 #define S2C_WL(a, b) svwhilelt_b32_s64(a, b) #define S2C_LD svld1_f32 #define S2C_ST svst1_f32 +#define S2C_DUP svdup_n_f32 #endif static inline __attribute__((always_inline)) void s2c_rows_s(BLASLONG m, BLASLONG w, const FLOAT *a, BLASLONG lda, @@ -252,4 +254,32 @@ static inline void s2c_zero(BLASLONG m, BLASLONG w, FLOAT *b) { if (m > 0) memset(b, 0, (size_t)(m * w) * sizeof(FLOAT)); } +/* Rows i1..i2 of a diagonal block, diagonal in column d0 + i: zeros left (zl) or right of it, 1 there with unit. */ +static inline void s2c_tri(BLASLONG i1, BLASLONG i2, BLASLONG w, BLASLONG d0, int zl, int unit, FLOAT *b) { + for (BLASLONG i = i1; i < i2; ++i) { + const BLASLONG d = d0 + i; + FLOAT *bi = b + i * w; + for (BLASLONG c = zl ? 0 : d + 1; c < (zl ? d : w); ++c) bi[c] = ZERO; + if (unit) bi[d] = ONE; + } +} + +#ifdef HAVE_SME2_GEMM +static inline __attribute__((always_inline)) void s2c_zero_s(BLASLONG m, BLASLONG w, FLOAT *b) __arm_streaming { + for (BLASLONG k = 0; k < m * w; k += S2C_VL) S2C_ST(S2C_WL(k, m * w), b + k, S2C_DUP(0)); +} + +static inline __attribute__((always_inline)) void s2c_tri_s(BLASLONG i1, BLASLONG i2, BLASLONG w, BLASLONG d0, int zl, + int unit, FLOAT *b) __arm_streaming { + for (BLASLONG i = i1; i < i2; ++i) { + const BLASLONG d = d0 + i; + for (BLASLONG c = 0; c < w; c += S2C_VL) { + const svbool_t pw = S2C_WL(c, w), pd = S2C_WL(c, d), pd1 = S2C_WL(c, d + 1); + S2C_ST(zl ? pd : svbic_b_z(pw, pw, pd1), b + i * w + c, S2C_DUP(0)); + if (unit) S2C_ST(svbic_b_z(pw, pd1, pd), b + i * w + c, S2C_DUP(1)); + } + } +} +#endif + #endif diff --git a/kernel/arm64/trmm_ncopy_sme2.c b/kernel/arm64/trmm_ncopy_sme2.c index 791790113d..6ae3b8b66b 100644 --- a/kernel/arm64/trmm_ncopy_sme2.c +++ b/kernel/arm64/trmm_ncopy_sme2.c @@ -31,41 +31,50 @@ THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "sme2_gemm_copy.h" #ifndef LOWER -#define KEEP(r, c) ((r) < (c)) +#define ZL 1 #else -#define KEEP(r, c) ((r) > (c)) +#define ZL 0 #endif #ifdef UNIT -#define DIAG(v) ONE +#define UNITD 1 #else -#define DIAG(v) (v) +#define UNITD 0 #endif -int CNAME(BLASLONG m, BLASLONG n, FLOAT *a, BLASLONG lda, BLASLONG posX, BLASLONG posY, FLOAT *b) { - for (BLASLONG j0 = 0; j0 < n; j0 += S2C_W) { - const BLASLONG w = n - j0 < S2C_W ? n - j0 : S2C_W, lo = posY + j0; - const FLOAT *ap = a + posX + lo * lda; - const BLASLONG i1 = s2c_clip(lo - posX, 0, m), i2 = s2c_clip(lo + w - posX, i1, m); -#ifndef LOWER - s2c_cols(i1, w, ap, lda, b); - s2c_zero(m - i2, w, b + i2 * w); +#if ZL +#define PART(COPY, ZERO) \ + COPY(i2, w, ap, lda, b); \ + ZERO(m - i2, w, b + i2 * w) #else - s2c_zero(i1, w, b); - s2c_cols(m - i2, w, ap + i2, lda, b + i2 * w); +#define PART(COPY, ZERO) \ + ZERO(i1, w, b); \ + COPY(m - i1, w, ap + i1, lda, b + i1 * w) #endif - /* Diagonal block: a copy, then the zero side of the diagonal (column d of row i) and the unit diagonal. */ - s2c_cols(i2 - i1, w, ap + i1, lda, b + i1 * w); - for (BLASLONG i = i1; i < i2; ++i) { - const BLASLONG d = posX + i - lo; - FLOAT *bi = b + i * w; - if (KEEP(0, 1)) { - for (BLASLONG c = 0; c < d; ++c) bi[c] = ZERO; - } else { - for (BLASLONG c = d + 1; c < w; ++c) bi[c] = ZERO; - } - bi[d] = DIAG(bi[d]); - } - b += m * w; +/* Panels of w columns: the rows on the kept side of the diagonal copied, the others zeroed, then the diagonal block. */ +#define PANELS(COPY, ZERO, TRI) \ + for (BLASLONG j0 = 0, w; j0 < n; j0 += w, b += m * w) { \ + w = n - j0 < S2C_W ? n - j0 : S2C_W; \ + const BLASLONG lo = posY + j0, i1 = s2c_clip(lo - posX, 0, m), i2 = s2c_clip(lo + w - posX, i1, m); \ + const FLOAT *ap = a + posX + lo * lda; \ + PART(COPY, ZERO); \ + TRI(i1, i2, w, posX - lo, ZL, UNITD, b); \ } + +#ifdef HAVE_SME2_GEMM +__arm_new("za") __arm_locally_streaming static + __attribute__((noinline)) void panels_sme(BLASLONG m, BLASLONG n, FLOAT *a, BLASLONG lda, BLASLONG posX, + BLASLONG posY, FLOAT *b) { + PANELS(s2c_cols_s, s2c_zero_s, s2c_tri_s) +} +#endif + +int CNAME(BLASLONG m, BLASLONG n, FLOAT *a, BLASLONG lda, BLASLONG posX, BLASLONG posY, FLOAT *b) { +#ifdef HAVE_SME2_GEMM + if (m * n >= S2C_SME && s2_usable()) { + panels_sme(m, n, a, lda, posX, posY, b); + return 0; + } +#endif + PANELS(s2c_cols, s2c_zero, s2c_tri) return 0; } diff --git a/kernel/arm64/trmm_tcopy_sme2.c b/kernel/arm64/trmm_tcopy_sme2.c index 7eb014e922..cbf32fc45a 100644 --- a/kernel/arm64/trmm_tcopy_sme2.c +++ b/kernel/arm64/trmm_tcopy_sme2.c @@ -31,41 +31,49 @@ THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "sme2_gemm_copy.h" #ifndef LOWER -#define KEEP(r, c) ((r) > (c)) +#define ZL 0 #else -#define KEEP(r, c) ((r) < (c)) +#define ZL 1 #endif #ifdef UNIT -#define DIAG(v) ONE +#define UNITD 1 #else -#define DIAG(v) (v) +#define UNITD 0 #endif -int CNAME(BLASLONG m, BLASLONG n, FLOAT *a, BLASLONG lda, BLASLONG posX, BLASLONG posY, FLOAT *b) { - for (BLASLONG j0 = 0; j0 < n; j0 += S2C_W) { - const BLASLONG w = n - j0 < S2C_W ? n - j0 : S2C_W, lo = posY + j0; - const FLOAT *ap = a + lo + posX * lda; - const BLASLONG i1 = s2c_clip(lo - posX, 0, m), i2 = s2c_clip(lo + w - posX, i1, m); -#ifndef LOWER - s2c_zero(i1, w, b); - s2c_rows(m - i2, w, ap + i2 * lda, lda, b + i2 * w); +#if ZL +#define PART(COPY, ZERO) \ + COPY(i2, w, ap, lda, b); \ + ZERO(m - i2, w, b + i2 * w) #else - s2c_rows(i1, w, ap, lda, b); - s2c_zero(m - i2, w, b + i2 * w); +#define PART(COPY, ZERO) \ + ZERO(i1, w, b); \ + COPY(m - i1, w, ap + i1 * lda, lda, b + i1 * w) #endif - /* Diagonal block: a copy, then the zero side of the diagonal (column d of row i) and the unit diagonal. */ - s2c_rows(i2 - i1, w, ap + i1 * lda, lda, b + i1 * w); - for (BLASLONG i = i1; i < i2; ++i) { - const BLASLONG d = posX + i - lo; - FLOAT *bi = b + i * w; - if (KEEP(0, 1)) { - for (BLASLONG c = 0; c < d; ++c) bi[c] = ZERO; - } else { - for (BLASLONG c = d + 1; c < w; ++c) bi[c] = ZERO; - } - bi[d] = DIAG(bi[d]); - } - b += m * w; +/* Panels of w columns: the rows on the kept side of the diagonal copied, the others zeroed, then the diagonal block. */ +#define PANELS(COPY, ZERO, TRI) \ + for (BLASLONG j0 = 0, w; j0 < n; j0 += w, b += m * w) { \ + w = n - j0 < S2C_W ? n - j0 : S2C_W; \ + const BLASLONG lo = posY + j0, i1 = s2c_clip(lo - posX, 0, m), i2 = s2c_clip(lo + w - posX, i1, m); \ + const FLOAT *ap = a + lo + posX * lda; \ + PART(COPY, ZERO); \ + TRI(i1, i2, w, posX - lo, ZL, UNITD, b); \ } + +#ifdef HAVE_SME2_GEMM +__arm_locally_streaming static __attribute__((noinline)) void panels_sme(BLASLONG m, BLASLONG n, FLOAT *a, BLASLONG lda, + BLASLONG posX, BLASLONG posY, FLOAT *b) { + PANELS(s2c_rows_s, s2c_zero_s, s2c_tri_s) +} +#endif + +int CNAME(BLASLONG m, BLASLONG n, FLOAT *a, BLASLONG lda, BLASLONG posX, BLASLONG posY, FLOAT *b) { +#ifdef HAVE_SME2_GEMM + if (m * n >= S2C_SME && s2_usable()) { + panels_sme(m, n, a, lda, posX, posY, b); + return 0; + } +#endif + PANELS(s2c_rows, s2c_zero, s2c_tri) return 0; }