Commit 945245276 for llama.cpp

commit 94524527648367390024e2b48d29cd7b1dd417db
Author: Marin Radic <mradic07@gmail.com>
Date:   Tue Dec 1 16:24:37 2026 +0100

    ggml-et : fix multi-line L2 flush and replace L2-SCP polling with FCC 0 (#33313)

    FlushVA encodes numlines - 1 in 4 bits (max 16 lines), so the 32-line
    Q4_0 panel flush in mul_mat_Q4_0_matrix_engine silently dropped lines
    16..31. Add flush_to_l2_multi() in platform.h to chunk flushes by 16.

    L2-SCP also persists across kernel launches, so ready_ctr/consumed_ctr
    polling in mul_mat_Q4_0_matrix_engine and mul_mat_f16_matrix_engine
    raced at entry and could corrupt results or deadlock. Replace the
    L2-SCP counters with FCC 0 semaphores (et_sem_post/et_sem_wait) plus
    hart 1 exit credit drain (matching flash_attn_ext_f16_me.c), which
    avoids FLB 0 aliasing with uberkernel barriers and improves Q4_0
    4096x512x14336 throughput from 632.9 to 668.1 GFLOPS (+5.6%).

    Assisted-by: Antigravity

diff --git a/ggml/src/ggml-et/et-kernels/src/mul_mat_Q4_0_matrix_engine.c b/ggml/src/ggml-et/et-kernels/src/mul_mat_Q4_0_matrix_engine.c
index 28a103032..762760c72 100644
--- a/ggml/src/ggml-et/et-kernels/src/mul_mat_Q4_0_matrix_engine.c
+++ b/ggml/src/ggml-et/et-kernels/src/mul_mat_Q4_0_matrix_engine.c
@@ -25,32 +25,11 @@
 #define A_L1_START 0   // L1 SCP lines  0..15 for A (activations)
 #define B_L1_START 16  // L1 SCP lines 16..31 for B (dequantized weights)

-// L2 SCP layout per minion (double-buffered dequant panel + sync counters).
+// L2 SCP layout per minion (double-buffered dequant panel).
 // panel = BLOCK_K k-lines x TILE_M m (FP32) = 32 * 64 = 2048 bytes, in TenB
 // [k][m] order: panel[k*TILE_M + m].
-#define SCP_PANEL_SIZE   (BLOCK_K * TILE_M * (uint64_t) sizeof(float))  // 2048
-#define SCP_READY_OFF    (2 * SCP_PANEL_SIZE)                           // 4096
-#define SCP_CONSUMED_OFF (SCP_READY_OFF + 64)                           // 4160
-#define SCP_PER_MINION   (SCP_CONSUMED_OFF + 64)                        // 4224
-
-// Signal a counter value to the other hart via L2 SCP.
-static inline void __attribute__((always_inline)) scp_signal(volatile uint32_t * flag, uint32_t value) {
-    *flag = value;
-    FENCE;
-    evict_to_l2((const void *) flag, 1, 64);
-    WAIT_CACHEOPS;
-}
-
-// Wait for a counter in L2 SCP to reach the expected value.
-static inline void __attribute__((always_inline)) scp_wait(volatile uint32_t * flag, uint32_t expected) {
-    while (1) {
-        evict_to_l2((const void *) flag, 1, 64);
-        WAIT_CACHEOPS;
-        if (*flag >= expected) {
-            return;
-        }
-    }
-}
+#define SCP_PANEL_SIZE (BLOCK_K * TILE_M * (uint64_t) sizeof(float))  // 2048
+#define SCP_PER_MINION (2 * SCP_PANEL_SIZE)                           // 4096

 // Dequantize one 32-element Q4_0 block of TILE_M weight rows into the FP32
 // panel, written directly in TenB [k][m] order: panel[k*TILE_M + m].
@@ -192,22 +171,17 @@ int entry_point(struct ggml_et_binary_params * params, void * env) {
     const int64_t kb_start          = k_split * k_steps_per_split;   // first block
     const int64_t kb_end            = kb_start + k_steps_per_split;  // one past last

-    // L2 SCP pointers for this minion's double-buffered panels + sync.
+    // L2 SCP pointers for this minion's double-buffered panels.
     uint64_t scp_base     = local_minion * SCP_PER_MINION;
     float *  scp_panel[2] = {
         (float *) et_shire_l2scp_local(scp_base),
         (float *) et_shire_l2scp_local(scp_base + SCP_PANEL_SIZE),
     };
-    volatile uint32_t * ready_ctr    = (volatile uint32_t *) et_shire_l2scp_local(scp_base + SCP_READY_OFF);
-    volatile uint32_t * consumed_ctr = (volatile uint32_t *) et_shire_l2scp_local(scp_base + SCP_CONSUMED_OFF);

     // ================================================================
     // Hart 1: Q4_0 weight dequant producer
     // ================================================================
     if (is_hart1) {
-        scp_signal(ready_ctr, 0);
-        scp_signal(consumed_ctr, 0);
-
         uint32_t chunk_id = 0;

         for (int64_t tile = (int64_t) shire_id + local_tile_idx * NUM_COMPUTE_SHIRES; tile < base_tiles;
@@ -229,22 +203,28 @@ int entry_point(struct ggml_et_binary_params * params, void * env) {
             for (int64_t kb = kb_start; kb < kb_end; ++kb) {
                 int buf = chunk_id & 1;

-                // Back-pressure: wait for hart 0 to finish with this buffer.
+                // Back-pressure via FCC 0 (avoids FLB 0 aliasing with uberkernel barriers).
                 if (chunk_id >= 2) {
-                    scp_wait(consumed_ctr, chunk_id - 1);
+                    et_sem_wait(ET_BARRIER_MINION);
                 }

                 dequant_q4_0_panel(scp_panel[buf], src0_batch, mb, kb, nb1_0);

                 FENCE;
-                flush_to_l2(scp_panel[buf], BLOCK_K, 64);
+                flush_to_l2_multi(scp_panel[buf], BLOCK_K, 64);
                 WAIT_CACHEOPS;

+                et_sem_post(ET_BARRIER_MINION);
                 chunk_id++;
-                scp_signal(ready_ctr, chunk_id);
             }
         }

+        // Drain the up to 2 unconsumed FCC 0 credits from the wait-skip on chunks 0 and 1.
+        uint32_t drain = (chunk_id < 2) ? chunk_id : 2;
+        for (uint32_t d = 0; d < drain; ++d) {
+            et_sem_wait(ET_BARRIER_MINION);
+        }
+
         FENCE;
         return 0;
     }
@@ -261,11 +241,6 @@ int entry_point(struct ggml_et_binary_params * params, void * env) {
 #endif
     CLEAR_TENSOR_ERROR;

-    evict_to_l2((const void *) ready_ctr, 1, 64);
-    WAIT_CACHEOPS;
-    evict_to_l2((const void *) consumed_ctr, 1, 64);
-    WAIT_CACHEOPS;
-
     uint32_t chunk_id = 0;

     for (int64_t tile = (int64_t) shire_id + local_tile_idx * NUM_COMPUTE_SHIRES; tile < base_tiles;
@@ -307,8 +282,7 @@ int entry_point(struct ggml_et_binary_params * params, void * env) {
             int buf = chunk_id & 1;

             // Wait for hart 1 to finish dequantizing this block.
-            chunk_id++;
-            scp_wait(ready_ctr, chunk_id);
+            et_sem_wait(ET_BARRIER_MINION);

             // Two FMA passes over the 32-wide block (16 K-cols each).
             for (int half = 0; half < 2; ++half) {
@@ -337,7 +311,8 @@ int entry_point(struct ggml_et_binary_params * params, void * env) {
             }

             // Signal that this buffer is free for hart 1 to reuse.
-            scp_signal(consumed_ctr, chunk_id);
+            et_sem_post(ET_BARRIER_MINION);
+            chunk_id++;
         }

         // K-split ring reduce.
diff --git a/ggml/src/ggml-et/et-kernels/src/mul_mat_f16_matrix_engine.c b/ggml/src/ggml-et/et-kernels/src/mul_mat_f16_matrix_engine.c
index 2aab87ad5..25e6a27f9 100644
--- a/ggml/src/ggml-et/et-kernels/src/mul_mat_f16_matrix_engine.c
+++ b/ggml/src/ggml-et/et-kernels/src/mul_mat_f16_matrix_engine.c
@@ -10,7 +10,7 @@
 // Hart 0: tensor engine (load A, load B from SCP, FMA, reduce, store)
 // Hart 1: pack B into double-buffered L2 SCP panels, flush for tensor_load
 //
-// Sync: monotonic counters in L2 SCP with evict-based coherency.
+// Sync: FCC 0 semaphores (et_sem_post / et_sem_wait).
 // Double-buffered bpanel allows pack/FMA overlap.
 //
 #define NUM_COMPUTE_SHIRES 32
@@ -28,34 +28,11 @@

 typedef uint16_t et_fp16_t;

-// L2 SCP layout per minion (double-buffered bpanel + sync counters)
+// L2 SCP layout per minion (double-buffered bpanel)
 //   [0..1023]    bpanel buffer 0 (16 lines x 64 bytes)
 //   [1024..2047] bpanel buffer 1
-//   [2048..2111] ready counter   (hart1 -> hart0, own cache line)
-//   [2112..2175] consumed counter (hart0 -> hart1, own cache line)
-#define SCP_BPANEL_SIZE  (16 * 32 * sizeof(et_fp16_t))  // 1024 bytes
-#define SCP_READY_OFF    (2 * SCP_BPANEL_SIZE)          // 2048
-#define SCP_CONSUMED_OFF (SCP_READY_OFF + 64)           // 2112
-#define SCP_PER_MINION   (SCP_CONSUMED_OFF + 64)        // 2176
-
-// Signal a counter value to the other hart via L2 SCP.
-static inline void __attribute__((always_inline)) scp_signal(volatile uint32_t * flag, uint32_t value) {
-    *flag = value;
-    FENCE;
-    evict_to_l2((const void *) flag, 1, 64);
-    WAIT_CACHEOPS;
-}
-
-// Wait for a counter in L2 SCP to reach the expected value.
-static inline void __attribute__((always_inline)) scp_wait(volatile uint32_t * flag, uint32_t expected) {
-    while (1) {
-        evict_to_l2((const void *) flag, 1, 64);
-        WAIT_CACHEOPS;
-        if (*flag >= expected) {
-            return;
-        }
-    }
-}
+#define SCP_BPANEL_SIZE (16 * 32 * sizeof(et_fp16_t))  // 1024 bytes
+#define SCP_PER_MINION  (2 * SCP_BPANEL_SIZE)          // 2048 bytes

 /**
  * Build the interleaved B panel that TensorFMA16A32 expects (vectorized).
@@ -170,23 +147,17 @@ int entry_point(struct ggml_et_binary_params * params, void * env) {
     const int64_t k_start           = k_split * k_steps_per_split * TILE_K;
     const int64_t k_end             = k_start + k_steps_per_split * TILE_K;

-    // L2 SCP pointers for this minion's double-buffered panels + sync
+    // L2 SCP pointers for this minion's double-buffered panels
     uint64_t    scp_base  = local_minion * SCP_PER_MINION;
     et_fp16_t * scp_bp[2] = {
         (et_fp16_t *) et_shire_l2scp_local(scp_base),
         (et_fp16_t *) et_shire_l2scp_local(scp_base + SCP_BPANEL_SIZE),
     };
-    volatile uint32_t * ready_ctr    = (volatile uint32_t *) et_shire_l2scp_local(scp_base + SCP_READY_OFF);
-    volatile uint32_t * consumed_ctr = (volatile uint32_t *) et_shire_l2scp_local(scp_base + SCP_CONSUMED_OFF);

     // ================================================================
     // Hart 1: B-panel packer
     // ================================================================
     if (is_hart1) {
-        // Initialize sync counters
-        scp_signal(ready_ctr, 0);
-        scp_signal(consumed_ctr, 0);
-
         uint32_t chunk_id = 0;

         for (int64_t tile = (int64_t) shire_id + local_tile_idx * NUM_COMPUTE_SHIRES; tile < base_tiles;
@@ -208,9 +179,9 @@ int entry_point(struct ggml_et_binary_params * params, void * env) {
             for (int64_t kb = k_start; kb < k_end; kb += TILE_K) {
                 int buf = chunk_id & 1;

-                // Back-pressure: wait for hart 0 to finish with this buffer
+                // Back-pressure via FCC 0 (avoids FLB 0 aliasing with uberkernel barriers).
                 if (chunk_id >= 2) {
-                    scp_wait(consumed_ctr, chunk_id - 1);
+                    et_sem_wait(ET_BARRIER_MINION);
                 }

                 pack_b_interleaved(scp_bp[buf], src0_batch, mb, kb, nb1_0);
@@ -219,11 +190,17 @@ int entry_point(struct ggml_et_binary_params * params, void * env) {
                 flush_to_l2(scp_bp[buf], 16, 64);
                 WAIT_CACHEOPS;

+                et_sem_post(ET_BARRIER_MINION);
                 chunk_id++;
-                scp_signal(ready_ctr, chunk_id);
             }
         }

+        // Drain the up to 2 unconsumed FCC 0 credits from the wait-skip on chunks 0 and 1.
+        uint32_t drain = (chunk_id < 2) ? chunk_id : 2;
+        for (uint32_t d = 0; d < drain; ++d) {
+            et_sem_wait(ET_BARRIER_MINION);
+        }
+
         FENCE;
         return 0;
     }
@@ -240,12 +217,6 @@ int entry_point(struct ggml_et_binary_params * params, void * env) {
 #endif
     CLEAR_TENSOR_ERROR;

-    // Evict any stale L1D copies of sync counters
-    evict_to_l2((const void *) ready_ctr, 1, 64);
-    WAIT_CACHEOPS;
-    evict_to_l2((const void *) consumed_ctr, 1, 64);
-    WAIT_CACHEOPS;
-
     uint32_t chunk_id = 0;

     for (int64_t tile = (int64_t) shire_id + local_tile_idx * NUM_COMPUTE_SHIRES; tile < base_tiles;
@@ -282,8 +253,7 @@ int entry_point(struct ggml_et_binary_params * params, void * env) {
                         (uint64_t) nb1_1, 0);

             // Wait for hart 1 to finish packing this chunk
-            chunk_id++;
-            scp_wait(ready_ctr, chunk_id);
+            et_sem_wait(ET_BARRIER_MINION);

             // Load B from L2 SCP (hart 1 already flushed it)
             tensor_load(false, false, B_L1_START, TENSOR_LOAD_PLAIN, 0, (uint64_t) scp_bp[buf], 0, 15, 64, 1);
@@ -298,7 +268,8 @@ int entry_point(struct ggml_et_binary_params * params, void * env) {
             tensor_wait(TENSOR_FMA_WAIT);

             // Signal that this buffer is free for hart 1 to reuse
-            scp_signal(consumed_ctr, chunk_id);
+            et_sem_post(ET_BARRIER_MINION);
+            chunk_id++;
         }

         // K-split ring reduce
diff --git a/ggml/src/ggml-et/et-kernels/src/platform.h b/ggml/src/ggml-et/et-kernels/src/platform.h
index cbec4c98d..2ea9592a3 100644
--- a/ggml/src/ggml-et/et-kernels/src/platform.h
+++ b/ggml/src/ggml-et/et-kernels/src/platform.h
@@ -476,6 +476,23 @@ static inline void __attribute__((always_inline)) flush_to_l2(const void * addr,
         : "x31", "memory");
 }

+// Flush any number of cache lines to L2 in 16-line chunks (FlushVA hardware cap).
+static inline void __attribute__((always_inline)) flush_to_l2_multi(const void * addr,
+                                                                    uint64_t     nlines,
+                                                                    uint64_t     stride) {
+    const char *   p    = (const char *) addr;
+    const uint64_t step = stride & 0xFFFFFFFFFFC0ULL;
+
+    while (nlines > 16) {
+        flush_to_l2(p, 16, step);
+        p += 16 * step;
+        nlines -= 16;
+    }
+    if (nlines > 0) {
+        flush_to_l2(p, nlines, step);
+    }
+}
+
 // Evict nlines cache lines at stride apart starting at addr from L1 to L2.
 // Uses EvictVA (CSR 0x89F).  Unlike flush_to_l2, this guarantees the line is
 // NOT present in L1 after the operation - subsequent loads will miss and go