Building My Own SIMT GPGPU (7. Phase 5: Bundling Transactions with Memory Coalescing)

Previous article: https://msyksphinz-en.hatenablog.com/entry/2026/07/03/040000msyksphinz-en.hatenablog.com

Until now, the memory accesses of the lanes in a warp were sent to memory one at a time, in order. A load for 8 lanes took 8 accesses. This time we bundle them. We implement memory coalescing, which merges accesses to consecutive addresses into a single transaction.

Splitting the Memory Port into Instruction and Data

For coalescing, we first rebuild the memory interface. Until now, both instructions and data time-shared a single 32-bit port. We split it into two.

  • Instruction port: reads one instruction (1 word)
  • Data port: a vector. It has a per-lane address and write mask, and carries multiple lanes in one transaction

Making the data port a vector is the foundation of coalescing.

output logic             dmem_req;             // whether to access in this cycle
output logic             dmem_we;              // whether it is a write
output logic [31:0]      dmem_addr  [NLANE];   // per-lane address
output logic [NLANE-1:0] dmem_wmask;           // lanes participating in the current transaction
output logic [31:0]      dmem_wdata [NLANE];   // per-lane write data
input  logic [31:0]      dmem_rdata [NLANE];   // per-lane read data

Since dmem_addr and dmem_wdata are now per-lane arrays, the addresses and data of all active lanes can be put on the port at once in the same cycle. So we always wire each lane's effective address and write data to this port.

for (gd = 0; gd < NLANE; gd++) begin : DPORT
  assign dmem_addr[gd]  = eaddr[gd];   // effective address of lane g
  assign dmem_wdata[gd] = rv2[gd];     // store data of lane g
end

The addresses are always present. What decides which lanes are actually included in this cycle's transaction is the next signal, dmem_wmask.

Detecting Consecutive Accesses

Coalescing is possible when the lanes' accesses are consecutive. In an SIMT vector addition, lane g handles element i = tid, and the address is base + g*4. This is consecutive in 4-byte steps in lane order. So we check whether the addresses of all active lanes line up with a stride of 4 starting from eaddr[0].

logic coalesced;
always_comb begin
  coalesced = active[0];
  for (int gg = 1; gg < NLANE; gg++)
    if (active[gg] && (eaddr[gg] != eaddr[0] + 32'(gg) * 32'd4)) coalesced = 1'b0;
end
logic use_coalesce; assign use_coalesce = (COALESCE != 0) && coalesced;

If they are consecutive, all lanes are handled in a single transaction. If not, we fall back to per-lane accesses. We made this branch switchable with the COALESCE parameter.

How to Bundle into One Transaction

Whether to bundle is decided in S_EXEC, when a memory instruction arrives. If use_coalesce is asserted, we go to S_LSU_C, which handles all lanes in one cycle. Otherwise, we enter S_LSU, which goes through the lanes in turn.

end else if (is_mem) begin
  if      (use_coalesce)  wstate[cw] <= S_LSU_C;              // bundle
  else begin li[cw] <= '0; wstate[cw] <= S_LSU; end           // per lane
end

The substance of bundling comes down to the difference in whether dmem_req is asserted just once or many times, and what is put on dmem_wmask. There is one combinational logic block driving the port, and it behaves differently depending on the state.

logic [NLANE-1:0] onehot_li; assign onehot_li = (NLANE'(1) << c_li);
always_comb begin
  dmem_req = 1'b0; dmem_we = is_store; dmem_wmask = '0;
  if (wstate[cw] == S_LSU_C) begin
    dmem_req   = 1'b1;
    dmem_wmask = active;                          // all active lanes in "one" transaction
  end else if (wstate[cw] == S_LSU) begin
    dmem_req   = 1'b1;
    dmem_wmask = active[c_li] ? onehot_li : '0;   // set "only one" lane, c_li
  end
end

In S_LSU_C, the bits of all active lanes are set in dmem_wmask. As wired earlier, dmem_addr already carries the addresses of all lanes, so this single-cycle dmem_req becomes one transaction that carries all active lanes at once. In S_LSU, on the other hand, only the single bit for c_li is set in dmem_wmask, and c_li advances from 0 to NLANE-1. So there are as many transactions as there are lanes.

The cleanup on the core side also finishes in one cycle when bundled. S_LSU_C captures the load results of all active lanes at once from the returned vector dmem_rdata.

S_LSU_C: begin
  if (is_load)
    for (l = 0; l < NLANE; l++)
      if (active[l]) ld_data[cw][l] <= dmem_rdata[l];   // all lanes in 1 cycle
  wstate[cw] <= S_WB;
end

S_LSU (non-coalescing) does this one lane at a time, over NLANE cycles.

S_LSU: begin
  if (is_load && active[c_li]) ld_data[cw][c_li] <= dmem_rdata[c_li];
  if (c_li == LW'(NLANE-1)) wstate[cw] <= S_WB;
  else                      li[cw] <= c_li + LW'(1);
end

What the Memory Side Does in One Transaction

What "bundling into one transaction" actually means becomes clear when we look at the side that handles memory. In this project's simulation, the memory is held by the testbench's C++. One cycle in which dmem_req is asserted is counted as one transaction. And in that transaction, the words of all lanes whose dmem_wmask bit is set are moved.

if (d->dmem_req) txn_count()++;                 // a cycle with dmem_req asserted = 1 transaction

// Store: in this transaction, write the words of all lanes whose wmask bit is set
if (write)
  for (int g = 0; g < NLANE; ++g)
    if ((wmask >> g) & 1) mem[widx(d->dmem_addr[g])] = d->dmem_wdata[g];

// Load: likewise, read the words of the wmask lanes and return them as a vector
for (int g = 0; g < NLANE; ++g)
  if ((d->dmem_wmask >> g) & 1) d->dmem_rdata[g] = mem[widx(d->dmem_addr[g])];

In S_LSU_C, 8 bits of dmem_wmask are set, so 8 words move in this single sweep. In S_LSU, only one bit is set at a time, so it is split into 8 sweeps. This is what it means for 8 transactions to be bundled into one.

On real hardware, this one transaction corresponds to "one wide memory access" or "one cache line". This is why we checked for consecutiveness first. A single cache line carries a contiguous region at once, so lane accesses can be bundled into one transaction only when their addresses are consecutive (that is, fit in the same line). If they are scattered, they hit different lines, so they cannot be bundled and we have no choice but to fall back to per-lane accesses. dmem_wmask exists so that masked-off (inactive) lanes within that consecutive range can be treated as holes that are not written. Even when branch divergence leaves only some lanes active, just the active lanes can be handled in one transaction.

Results

With a vector addition at NLANE=8, we compared coalescing on and off. This kernel has 2 loads and 1 store, 3 memory instructions in total.

$ cd sim && make coalesce
--- COALESCE=1 (coalesce consecutive accesses) ---
(37 cycles, 3 mem txns)
result: PASS - RTL matches golden model
--- COALESCE=0 (per-lane) ---
(58 cycles, 24 mem txns)
result: PASS - RTL matches golden model

With coalescing enabled, memory transactions dropped to 3. That is one transaction per memory instruction. With it disabled, the count rises to 8 lanes × 3 instructions = 24 transactions. By bundling consecutive accesses, the number of transactions was reduced to one eighth.


This is an English translation of the original Japanese post: 自作SIMT GPGPUを作る (7. Phase 5: メモリコアレッシングでトランザクションを束ねる)