WGMMA: a block-major B tile can read K-major data left in the B buffer (rtlsim)

Open
#518 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

@youzi27 is already working on this.

Since Nov 27, 2026.

  • #519 by @youzi27 — open
  • #559 by @youzi27 — open

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
35/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Stale
Tech stack
cpp
Domain
backend

Research direction

Start with hw/rtl/tcu/VX_tcu_bbuf.sv, especially the fetch-slot address recording and block-major residency checks described around lines 236-247 and 425. Reproduce with tests/regression/sgemm_tcu_wg using the supplied ci/blackbox.sh command, then add a regression that mixes K-major and block-major B tiles. Done means rtlsim no longer reuses the stale K-major data and matches the reference results.

Written by the indexing model from the issue text.

Description

Hi there,

On master 2221ee286, a WGMMA whose B descriptor is block-major (stride 0) can compute with B data left in the shared B buffer by the previous WGMMA, when that one used a K-major B descriptor and its last B fetch was keyed on the bank row where the new block-major tile starts. rtlsim then uses the old tile's data for the B blocks of that row. SimX refetches and gets the right result.

To reproduce, let sgemm_tcu_wg stage B K-major on even K tiles and block-major on odd K tiles, the block-major tile one LMEM bank row short of a B tile past the K-major base (both descriptors are legal under smem_desc_check):

diff --git a/tests/regression/sgemm_tcu_wg/kernel.cpp b/tests/regression/sgemm_tcu_wg/kernel.cpp
--- a/tests/regression/sgemm_tcu_wg/kernel.cpp
+++ b/tests/regression/sgemm_tcu_wg/kernel.cpp
@@ -77,6 +77,9 @@ __kernel void kernel_main(kernel_arg_t* __UNIFORM__ arg) {
     //   Default (block-major):  B_smem[(k_blk*N_STEPS + n_blk)*BW + n_in*tcK + k_in]
     //   WGMMA_KMAJOR_B:         B_smem[c * ldb + b_off + r] — N-outer, K-inner.
     uint32_t b_size = ctx::tileK * ctx::xtileN;
+    // even K tiles: B K-major; odd K tiles: B block-major, one bank row short of a B tile further on
+    bool b_kmajor = ((k / ctx::tileK) & 1) == 0;
+    auto B_tile = b_kmajor ? B_smem : B_smem + b_size - (VX_CFG_NUM_THREADS * 4) / sizeof(ctx::input_t);
     for (uint32_t i = 0; i < b_size; i += num_threads) {
       uint32_t idx = i + tid;
       uint32_t r = idx / ctx::xtileN;
@@ -84,7 +87,8 @@ __kernel void kernel_main(kernel_arg_t* __UNIFORM__ arg) {
     #ifdef WGMMA_KMAJOR_B
       B_smem[c * ldb + b_off + r] = pB[(k + r) * N + (tile_col + c)];
     #else
-      B_smem[ctx::b_blockmajor_idx(r, c)] = pB[(k + r) * N + (tile_col + c)];
+      if (b_kmajor) B_tile[c * ctx::tileK + r] = pB[(k + r) * N + (tile_col + c)];
+      else          B_tile[ctx::b_blockmajor_idx(r, c)] = pB[(k + r) * N + (tile_col + c)];
     #endif
     }
 
@@ -101,7 +105,7 @@ __kernel void kernel_main(kernel_arg_t* __UNIFORM__ arg) {
   #ifdef WGMMA_KMAJOR_B
     auto desc_b = vt::vx_make_smem_desc(B_smem + b_off, ldb * sizeof(ctx::input_t));
   #else
-    auto desc_b = vt::vx_make_smem_desc(B_smem, 0); // block-major: stride field unused
+    auto desc_b = vt::vx_make_smem_desc(B_tile, b_kmajor ? ctx::tileK * sizeof(ctx::input_t) : 0);
   #endif
 
   #if defined(WGMMA_RS) && (WGMMA_NRC <= 16)
diff --git a/tests/regression/sgemm_tcu_wg/main.cpp b/tests/regression/sgemm_tcu_wg/main.cpp
--- a/tests/regression/sgemm_tcu_wg/main.cpp
+++ b/tests/regression/sgemm_tcu_wg/main.cpp
@@ -715,7 +715,7 @@ int main(int argc, char *argv[]) {
 #else
   uint32_t b_smem = wg_cfg::tileK * per_warp_N;
 #endif
-  uint32_t smem_size = (a_smem + b_smem) * sizeof(itype_t) + SMEM_PAD_BYTES;
+  uint32_t smem_size = (a_smem + 2 * b_smem) * sizeof(itype_t) + SMEM_PAD_BYTES;
   vx_event_h launch_ev = nullptr;
   {
     vx_launch_info_t li = {};
CONFIGS="-DVX_CFG_NUM_THREADS=8 -DVX_CFG_NUM_WARPS=8 -DVX_CFG_ISSUE_WIDTH=4 -DVX_CFG_EXT_TCU_ENABLE -DVX_CFG_TCU_WGMMA_ENABLE -DITYPE=fp16 -DOTYPE=fp32 -DWGMMA_NRC=8 -DWGMMA_SS -DVX_CFG_NUM_ALU_BLOCKS=1 -DVX_CFG_NUM_LSU_BLOCKS=1 -DVX_CFG_NUM_FPU_BLOCKS=1" ./ci/blackbox.sh --driver=rtlsim --app=sgemm_tcu_wg --args="-m 32 -n 8 -k 64"
*** error: [0] expected=13.822495, actual=14.551969
*** error: [1] expected=14.570732, actual=15.075275
*** error: [2] expected=15.396145, actual=14.823661
*** error: [3] expected=13.078876, actual=14.069447
...
PERF: instrs=9988, cycles=26071, IPC=0.383
Found 104 / 256 errors!
FAILED!

Every wrong output is in columns 0-3 of the tile, the two B blocks of the shared bank row (N steps 0 and 1). The same line passes with --driver=simx, and on rtlsim when the odd tiles keep the even tiles' base (same layouts, no shared bank row). It shows with one CTA per core (-n 8); with two co-resident CTAs the other CTA's WGMMA usually refills the buffer in between. At XLEN 64 the two addresses fall in different halves of a bank row and nothing goes wrong.

The cause is in hw/rtl/tcu/VX_tcu_bbuf.sv. Every fetch allocation records the block-major bank-row address of its (step_k, step_n) block as the slot address (slot_a_addr_r <= fetch_addr_a, :425), K-major ones included, but after a K-major fetch the slot holds that block's K-major rows. The block-major residency terms then match on address and sub-half only:

wire bank_row_resident_dense =
    slot_a_valid_r && !slot_is_sparse_r
    && (slot_a_addr_r == fetch_addr_dense)
    && (slot_a_sub_half_r == dense_sub_half);

(:236-239; bank_row_resident_sparse at :241-247 likewise), so a block-major request at that address takes the K-major slot as resident. SimX's B buffer key compares the layout first (bbuf_slot_t::same_row, sim/simx/tcu/tcu_unit.cpp:819-820) and refetches.

Requiring !slot_row_major_r in both block-major terms fixes it, and only changes timing where SimX already refetches. A PR follows.

Environment: master 2221ee286, ../configure --xlen=32 --tooldir=..., prebuilt toolchain v3.0.1 (VERSION pins v3.0.3), Verilator 5.052.

Thanks!

Dominant language
Verilog
Stars
2.4k
Forks
563
Avg merge
1d 7h
Merged PRs (30d)
71

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from vortexgpgpu/vortex

All issues in vortexgpgpu/vortex

Similar issues

More Backend & API Design issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.