brotensor/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
378 / 487
77.6%
Branches:
352 / 840
41.9%
Functions:
21 / 25
84.0%

src/kernels/mir_gemm_mma.cpp
Line Branch Exec Source
1 #include "mir_gemm_mma.h"
2 #include "mir_common.h"
3 #include "mir_math.h"
4
5 #include <algorithm>
6 #include <cstdint>
7 #include <stdexcept>
8 #include <utility>
9 #include <vector>
10
11 namespace brotensor::kernels {
12
13 namespace {
14
15 constexpr std::uint32_t kMmaBk = 32u;
16 constexpr std::uint32_t kMmaStages = 3u;
17 constexpr std::uint32_t kL2GroupM = 8u;
18
19 28 Value* swz_bytes(Ctx& c, Value* row, Value* chunk) {
20 28 Value* row_off = c.b.build_shl(row, c.i32(6));
21 28 Value* swz = c.b.build_and(c.b.build_ashr(row, c.i32(1)), c.i32(3));
22 28 Value* ch_off = c.b.build_shl(c.b.build_xor(chunk, swz), c.i32(4));
23 28 return c.b.build_or(row_off, ch_off);
24 }
25
26 84 Value* ptr_2d_i64(Ctx& c, Value* base, Value* row, Value* ld, Value* col, int esz_shift) {
27 84 Value* row64 = c.b.build_zext_i64(row);
28 84 Value* ld64 = c.b.build_zext_i64(ld);
29 84 Value* col64 = c.b.build_zext_i64(col);
30 84 Value* elem = c.b.build_add(c.b.build_mul(row64, ld64), col64);
31 84 Value* bytes = c.b.build_shl(elem, c.kb.const_i64(esz_shift));
32 84 return c.b.build_add(base, bytes);
33 }
34
35 72 Value* pack_pair_i32(Ctx& c, Dtype dt, Value* v0, Value* v1) {
36
2/2
✓ Branch 0 taken 40 times.
✓ Branch 1 taken 32 times.
72 if (dt == Dtype::FP16) return c.kb.pack_f16x2(v0, v1);
37 32 Value* b0 = c.b.build_and(c.kb.f32_to_bf16(v0), c.i32(0xffff));
38 32 Value* b1 = c.b.build_shl(c.kb.f32_to_bf16(v1), c.i32(16));
39 32 return c.b.build_or(b0, b1);
40 72 }
41
42 12 std::pair<Value*, Value*> unpack_pair_f32(Ctx& c, Dtype dt, Value* packed) {
43 12 Value* lo_bits = c.b.build_and(packed, c.i32(0xffff));
44 12 Value* hi_bits = c.b.build_lshr(packed, c.i32(16));
45
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 4 times.
12 if (dt == Dtype::FP16) {
46 8 return {c.kb.f16_to_f32(lo_bits), c.kb.f16_to_f32(hi_bits)};
47 }
48 4 return {c.kb.bf16_to_f32(lo_bits), c.kb.bf16_to_f32(hi_bits)};
49 12 }
50
51 } // namespace
52
53 3 Function* build_gemm_mma_mir(Module& mod, const GemmMmaSpec& spec) {
54
3/4
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
3 if (spec.dt != Dtype::FP16 && spec.dt != Dtype::BF16) {
55
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ throw std::runtime_error("build_gemm_mma_mir: only FP16/BF16 supported");
56 }
57 3 const GemmMmaCfg& cfg = kGemmMmaCfgs[std::min<std::uint8_t>(spec.cfg, 3u)];
58 3 const int bm = static_cast<int>(cfg.bm);
59 3 const int bn = static_cast<int>(cfg.bn);
60 3 const int warps_n = static_cast<int>(cfg.warps_n);
61 3 const int threads = static_cast<int>(cfg.threads);
62 3 const int wm = bm / static_cast<int>(cfg.warps_m);
63 3 const int wn = bn / warps_n;
64 3 const int mt = wm / 16;
65 3 const int nt = wn / 8;
66 3 const int a_chunks = (bm * 4) / threads;
67 3 const int b_chunks = (bn * 4) / threads;
68 3 const int num_accs = mt * nt;
69 3 const int a_stage_bytes = bm * static_cast<int>(kMmaBk) * 2;
70 3 const int b_stage_bytes = bn * static_cast<int>(kMmaBk) * 2;
71
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 time.
3 const bool swiglu_inline = (spec.epi == 3u && !spec.has_partial);
72
73 30 std::vector<Type> param_types = {
74 15 Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(),
75 15 Type::i32(), Type::i32(), Type::i32(), Type::i32(), Type::i32()
76 };
77
2/6
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
3 if ((spec.strided || spec.f32_out) && (spec.has_partial || spec.epi >= 2u)) {
78
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ throw std::runtime_error("build_gemm_mma_mir: strided / f32_out take the store or accumulate epilogue");
79 }
80
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
3 if (spec.f32_out && spec.epi != 0u) {
81
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ throw std::runtime_error("build_gemm_mma_mir: f32_out takes the store epilogue");
82 }
83
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 const int ld_base = (spec.batched && !spec.has_partial) ? 13 : 10;
84
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
3 if (spec.batched && !spec.has_partial) {
85
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ param_types.push_back(Type::i32());
86
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ param_types.push_back(Type::i32());
87
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ param_types.push_back(Type::i32());
88 ✗ }
89
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (spec.strided) {
90
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ param_types.push_back(Type::i32());
91
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ param_types.push_back(Type::i32());
92
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ param_types.push_back(Type::i32());
93 ✗ }
94
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
3 Function* fn = mod.create_function(spec.f32_out ? "mir_gemm_mma_f32" : "mir_gemm_mma",
95 3 Type::void_type(), param_types);
96
97
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 brass::codegen::KernelBuilder kb(mod, fn);
98
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 Ctx c(kb);
99
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 auto p = entry_params(c, fn);
100 // p: A(0) W(1) C(2) bias(3) partial(4) M(5) N(6) K(7) kps(8) grid_m(9) [sA(10) sB(11) sC(12)]
101 3 Value* A_in = p[0];
102 3 Value* W_in = p[1];
103 3 Value* C_in = p[2];
104
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
3 if (spec.batched && !spec.has_partial) {
105
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Value* bz64 = c.b.build_zext_i64(kb.ctaid_z());
106 ✗ auto shift_batch = [&](Value* base, Value* stride32, int shift) {
107 ✗ Value* elems = c.b.build_mul(bz64, c.b.build_zext_i64(stride32));
108 ✗ return c.b.build_add(base, c.b.build_shl(elems, kb.const_i64(shift)));
109 };
110
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ A_in = shift_batch(p[0], p[10], 1);
111
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ W_in = shift_batch(p[1], p[11], 1);
112
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ C_in = shift_batch(p[2], p[12], spec.f32_out ? 2 : 1);
113 ✗ }
114 3 Value* M = p[5];
115 3 Value* N = p[6];
116 3 Value* K = p[7];
117 // Row pitches in elements: K for A and W, N for C unless strided.
118
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 Value* lda = spec.strided ? p[static_cast<std::size_t>(ld_base)] : K;
119
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 Value* ldw = spec.strided ? p[static_cast<std::size_t>(ld_base + 1)] : K;
120
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 Value* ldc = spec.strided ? p[static_cast<std::size_t>(ld_base + 2)] : N;
121 3 Value* kps = p[8];
122 3 Value* grid_m = p[9];
123
124
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 Value* smem = kb.shared_alloc_u16(kMmaStages * (cfg.bm + cfg.bn) * kMmaBk);
125 // `ptx_shared_alloc_*` produces a .shared state-space address (`mov.u64 %rd, smem_0`),
126 // already relative to .shared (not generic); truncating directly avoids `cvta.to.shared` underflow.
127
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 Value* sA_base = c.b.build_trunc_i32(smem);
128
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
3 Value* sB_base = c.b.build_add(sA_base, c.i32(static_cast<int>(kMmaStages) * a_stage_bytes));
129
130
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 Value* tid = kb.tid_x();
131
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
3 Value* lane = c.b.build_and(tid, c.i32(31));
132
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
3 Value* warp = c.b.build_lshr(tid, c.i32(5));
133
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
3 Value* w_m = c.b.build_udiv(warp, c.i32(warps_n));
134
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
3 Value* w_n = c.b.build_umod(warp, c.i32(warps_n));
135
136
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 Value* bx = kb.ctaid_x();
137
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 Value* by = kb.ctaid_y();
138 3 Value* tile_m = by;
139 3 Value* tile_n = bx;
140
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
3 if (spec.swizzle_l2 && !spec.has_partial) {
141
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Value* gn = kb.nctaid_x();
142
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Value* linear = c.b.build_add(c.b.build_mul(by, gn), bx);
143
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Value* per_slab = c.b.build_mul(c.i32(static_cast<int>(kL2GroupM)), gn);
144
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Value* slab = c.b.build_udiv(linear, per_slab);
145
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Value* in_slab = c.b.build_umod(linear, per_slab);
146
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Value* m_start = c.b.build_mul(slab, c.i32(static_cast<int>(kL2GroupM)));
147
0/6
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Value* slab_h = smin_i32(c, c.i32(static_cast<int>(kL2GroupM)), c.b.build_sub(grid_m, m_start));
148
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ tile_n = c.b.build_udiv(in_slab, slab_h);
149
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ tile_m = c.b.build_add(m_start, c.b.build_umod(in_slab, slab_h));
150 ✗ }
151
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
3 Value* m0 = c.b.build_mul(tile_m, c.i32(bm));
152
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
3 Value* n0 = c.b.build_mul(tile_n, c.i32(bn));
153
3/6
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 time.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
4 Value* N_half = (swiglu_inline || (spec.epi == 2 && !spec.has_partial))
154
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 ? c.b.build_lshr(N, c.i32(1))
155 : nullptr;
156 3 const int bn_half = bn / 2;
157
4/6
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 time.
✗ Branch 5 not taken.
3 Value* n0_half = swiglu_inline ? c.b.build_mul(tile_n, c.i32(bn_half)) : nullptr;
158
159
4/8
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 3 times.
✗ Branch 7 not taken.
3 Value* kt_all = c.b.build_lshr(c.b.build_add(K, c.i32(31)), c.i32(5));
160 3 Value* KT = kt_all;
161 3 Value* A_split = A_in;
162 3 Value* W_split = W_in;
163 3 Value* klim = K;
164
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (spec.has_partial) {
165
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Value* kt0 = c.b.build_mul(kb.ctaid_z(), kps);
166
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ KT = smin_i32(c, kps, c.b.build_sub(kt_all, kt0));
167
0/6
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Value* k0_bytes = c.b.build_shl(c.b.build_zext_i64(kt0), kb.const_i64(6));
168
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ A_split = c.b.build_add(A_in, k0_bytes);
169
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ W_split = c.b.build_add(W_in, k0_bytes);
170
0/6
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ klim = c.b.build_sub(K, c.b.build_shl(kt0, c.i32(5)));
171 ✗ }
172
173 // Precompute cp.async source/dest addresses (loop-invariant).
174 // Pack out-of-bounds row guards into one i32 bitmask (a_chunks + b_chunks <= 6)
175 // to keep live loop-carried predicates within SM89's 128-reg/thread budget.
176
4/8
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 3 times.
✗ Branch 7 not taken.
3 Value* lane_ch_k = c.b.build_shl(c.b.build_and(tid, c.i32(3)), c.i32(3));
177
3/4
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
3 Value* row_ok_mask = spec.aligned_mnk ? nullptr : c.i32(0);
178
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 std::vector<Value*> a_s_off(static_cast<std::size_t>(a_chunks));
179
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 std::vector<Value*> a_g_base(static_cast<std::size_t>(a_chunks));
180
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 6 times.
9 for (int i = 0; i < a_chunks; ++i) {
181 6 const auto iu = static_cast<std::size_t>(i);
182
4/6
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
6 Value* lin = (i == 0) ? tid : c.b.build_add(tid, c.i32(i * threads));
183
2/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
6 Value* row = c.b.build_lshr(lin, c.i32(2));
184
2/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
6 Value* ch = c.b.build_and(lin, c.i32(3));
185
2/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
6 a_s_off[iu] = c.b.build_add(sA_base, swz_bytes(c, row, ch));
186
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 Value* gr = c.b.build_add(m0, row);
187
2/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
6 Value* gk0 = c.b.build_shl(ch, c.i32(3));
188
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 a_g_base[iu] = ptr_2d_i64(c, A_split, gr, lda, gk0, 1);
189
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 4 times.
6 if (!spec.aligned_mnk) {
190
4/8
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2 times.
✗ Branch 7 not taken.
2 Value* bit = c.b.build_select(c.b.build_slt(gr, M), c.i32(1 << i), c.i32(0));
191
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 row_ok_mask = c.b.build_or(row_ok_mask, bit);
192 2 }
193 6 }
194
195
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 std::vector<Value*> b_s_off(static_cast<std::size_t>(b_chunks));
196
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 std::vector<Value*> b_g_base(static_cast<std::size_t>(b_chunks));
197
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 6 times.
9 for (int i = 0; i < b_chunks; ++i) {
198 6 const auto iu = static_cast<std::size_t>(i);
199
4/6
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
6 Value* lin = (i == 0) ? tid : c.b.build_add(tid, c.i32(i * threads));
200
2/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
6 Value* row = c.b.build_lshr(lin, c.i32(2));
201
2/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
6 Value* ch = c.b.build_and(lin, c.i32(3));
202
2/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
6 b_s_off[iu] = c.b.build_add(sB_base, swz_bytes(c, row, ch));
203
2/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
6 Value* gk0 = c.b.build_shl(ch, c.i32(3));
204 6 Value* gr = nullptr;
205 6 Value* valid_lim_cond = nullptr;
206
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 4 times.
6 if (swiglu_inline) {
207
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 Value* w_col = c.b.build_lshr(row, c.i32(5));
208
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 Value* in_w = c.b.build_and(row, c.i32(31));
209
4/8
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2 times.
✗ Branch 7 not taken.
2 Value* is_up = c.b.build_ne(c.b.build_and(in_w, c.i32(16)), c.i32(0));
210
5/10
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 2 times.
✗ Branch 9 not taken.
2 Value* loc_out = c.b.build_or(c.b.build_shl(w_col, c.i32(4)), c.b.build_and(in_w, c.i32(15)));
211
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 Value* out_col = c.b.build_add(n0_half, loc_out);
212
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 gr = c.b.build_select(is_up, c.b.build_add(N_half, out_col), out_col);
213
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 if (!spec.aligned_mnk) valid_lim_cond = c.b.build_slt(out_col, N_half);
214 2 } else {
215
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 gr = c.b.build_add(n0, row);
216
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
4 if (!spec.aligned_mnk) valid_lim_cond = c.b.build_slt(gr, N);
217 }
218
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 b_g_base[iu] = ptr_2d_i64(c, W_split, gr, ldw, gk0, 1);
219
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 4 times.
6 if (!spec.aligned_mnk) {
220
3/6
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
2 Value* bit = c.b.build_select(valid_lim_cond, c.i32(1 << (a_chunks + i)), c.i32(0));
221
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 row_ok_mask = c.b.build_or(row_ok_mask, bit);
222 2 }
223 6 }
224
225 // Precompute ldmatrix shared addresses for kk = 0; kk = 1 is addr ^ 32.
226
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
3 Value* a_lane_row = c.b.build_and(lane, c.i32(15));
227
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
3 Value* a_lane_ch = c.b.build_lshr(lane, c.i32(4));
228
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
3 Value* wm_base = c.b.build_mul(w_m, c.i32(wm));
229
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 std::vector<Value*> a_ld_off(static_cast<std::size_t>(mt));
230
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 10 times.
13 for (int i = 0; i < mt; ++i) {
231
3/6
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 10 times.
✗ Branch 5 not taken.
10 Value* row = c.b.build_add(c.b.build_add(wm_base, c.i32(i * 16)), a_lane_row);
232
2/4
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
10 a_ld_off[static_cast<std::size_t>(i)] = c.b.build_add(sA_base, swz_bytes(c, row, a_lane_ch));
233 10 }
234
235
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
3 Value* mi = c.b.build_lshr(lane, c.i32(3));
236
5/10
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 3 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 3 times.
✗ Branch 9 not taken.
6 Value* b_lane_row = c.b.build_add(c.b.build_shl(c.b.build_lshr(mi, c.i32(1)), c.i32(3)),
237
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
3 c.b.build_and(lane, c.i32(7)));
238
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
3 Value* b_lane_ch = c.b.build_and(mi, c.i32(1));
239
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
3 Value* wn_base = c.b.build_mul(w_n, c.i32(wn));
240
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 std::vector<Value*> b_ld_off(static_cast<std::size_t>(nt / 2));
241
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 6 times.
9 for (int j = 0; j < nt / 2; ++j) {
242
3/6
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 6 times.
✗ Branch 5 not taken.
6 Value* row = c.b.build_add(c.b.build_add(wn_base, c.i32(j * 16)), b_lane_row);
243
2/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
6 b_ld_off[static_cast<std::size_t>(j)] = c.b.build_add(sB_base, swz_bytes(c, row, b_lane_ch));
244 6 }
245
246 24 auto emit_load_stage_off = [&](Value* a_st_off, Value* b_st_off, Value* kt_idx) {
247 21 Value* kt_bytes = c.b.build_shl(c.b.build_zext_i64(kt_idx), kb.const_i64(6));
248 21 Value* k0 = c.b.build_shl(kt_idx, c.i32(5));
249 21 Value* gk = c.b.build_add(k0, lane_ch_k);
250
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 7 times.
21 Value* k_ok_mask = spec.aligned_mnk
251 ? nullptr
252 7 : c.b.build_select(c.b.build_slt(gk, klim), row_ok_mask, c.i32(0));
253
254
2/2
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 21 times.
63 for (int i = 0; i < a_chunks; ++i) {
255 42 const auto iu = static_cast<std::size_t>(i);
256
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 18 times.
42 Value* dst = a_st_off ? c.b.build_add(a_s_off[iu], a_st_off) : a_s_off[iu];
257 42 Value* src = c.b.build_add(a_g_base[iu], kt_bytes);
258
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 14 times.
42 if (spec.aligned_mnk) {
259 28 c.b.build_call("ptx_cp_async_cg_16", Type::void_type(), {dst, src});
260 28 } else {
261 14 Value* ok = c.b.build_ne(c.b.build_and(k_ok_mask, c.i32(1 << i)), c.i32(0));
262 14 Value* safe_src = c.b.build_select(ok, src, A_split);
263 14 Value* sz = c.b.build_select(ok, c.i32(16), c.i32(0));
264 14 c.b.build_call("ptx_cp_async_cg_16", Type::void_type(), {dst, safe_src, sz});
265 }
266 42 }
267
2/2
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 21 times.
63 for (int i = 0; i < b_chunks; ++i) {
268 42 const auto iu = static_cast<std::size_t>(i);
269
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 18 times.
42 Value* dst = b_st_off ? c.b.build_add(b_s_off[iu], b_st_off) : b_s_off[iu];
270 42 Value* src = c.b.build_add(b_g_base[iu], kt_bytes);
271
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 14 times.
42 if (spec.aligned_mnk) {
272 28 c.b.build_call("ptx_cp_async_cg_16", Type::void_type(), {dst, src});
273 28 } else {
274 28 Value* ok = c.b.build_ne(
275 14 c.b.build_and(k_ok_mask, c.i32(1 << (a_chunks + i))), c.i32(0));
276 14 Value* safe_src = c.b.build_select(ok, src, W_split);
277 14 Value* sz = c.b.build_select(ok, c.i32(16), c.i32(0));
278 14 c.b.build_call("ptx_cp_async_cg_16", Type::void_type(), {dst, safe_src, sz});
279 }
280 42 }
281 21 };
282 24 auto emit_load_stage = [&](int stage, Value* kt_idx) {
283 42 emit_load_stage_off(
284
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 9 times.
21 (stage == 0) ? nullptr : c.i32(stage * a_stage_bytes),
285
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 9 times.
21 (stage == 0) ? nullptr : c.i32(stage * b_stage_bytes),
286 21 kt_idx);
287 21 };
288
289
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 const bool use_fast_acc = (spec.fast_accum && spec.dt == Dtype::FP16);
290 3 const char* mma_sym = (spec.dt == Dtype::FP16) ? "ptx_mma_m16n8k16_f16_f32"
291 : "ptx_mma_m16n8k16_bf16_f32";
292
293 24 auto emit_compute_stage_off = [&](Value* a_st_off, Value* b_st_off, std::vector<Value*>& accs) {
294
2/2
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 21 times.
63 for (int kk = 0; kk < 2; ++kk) {
295 182 auto a_addr = [&](int i) -> Value* {
296 140 Value* base = a_ld_off[static_cast<std::size_t>(i)];
297
2/2
✓ Branch 0 taken 80 times.
✓ Branch 1 taken 60 times.
140 Value* p0 = a_st_off ? c.b.build_add(base, a_st_off) : base;
298
2/2
✓ Branch 0 taken 70 times.
✓ Branch 1 taken 70 times.
140 return (kk == 0) ? p0 : c.b.build_xor(p0, c.i32(32));
299 };
300 126 auto b_addr = [&](int j) -> Value* {
301 84 Value* base = b_ld_off[static_cast<std::size_t>(j)];
302
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 36 times.
84 Value* p0 = b_st_off ? c.b.build_add(base, b_st_off) : base;
303
2/2
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 42 times.
84 return (kk == 0) ? p0 : c.b.build_xor(p0, c.i32(32));
304 };
305
306 42 std::vector<Value*> b0(static_cast<std::size_t>(nt));
307
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 std::vector<Value*> b1(static_cast<std::size_t>(nt));
308
2/2
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 84 times.
126 for (int j = 0; j < nt / 2; ++j) {
309
3/6
✓ Branch 0 taken 84 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 84 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 84 times.
✗ Branch 5 not taken.
84 Value* r = c.b.build_call("ptx_ldmatrix_x4", Type::i32x4(), {b_addr(j)});
310 84 const auto j2 = static_cast<std::size_t>(2 * j);
311
1/2
✓ Branch 0 taken 84 times.
✗ Branch 1 not taken.
84 b0[j2] = c.b.build_vextract_lane(r, 0);
312
1/2
✓ Branch 0 taken 84 times.
✗ Branch 1 not taken.
84 b1[j2] = c.b.build_vextract_lane(r, 1);
313
1/2
✓ Branch 0 taken 84 times.
✗ Branch 1 not taken.
84 b0[j2 + 1u] = c.b.build_vextract_lane(r, 2);
314
1/2
✓ Branch 0 taken 84 times.
✗ Branch 1 not taken.
84 b1[j2 + 1u] = c.b.build_vextract_lane(r, 3);
315 84 }
316
317
3/6
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 42 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 42 times.
✗ Branch 5 not taken.
42 Value* af_cur = c.b.build_call("ptx_ldmatrix_x4", Type::i32x4(), {a_addr(0)});
318
2/2
✓ Branch 0 taken 140 times.
✓ Branch 1 taken 42 times.
182 for (int i = 0; i < mt; ++i) {
319
2/2
✓ Branch 0 taken 98 times.
✓ Branch 1 taken 42 times.
238 Value* af_next = (i + 1 < mt)
320
3/6
✓ Branch 0 taken 98 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 98 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 98 times.
✗ Branch 5 not taken.
98 ? c.b.build_call("ptx_ldmatrix_x4", Type::i32x4(), {a_addr(i + 1)})
321 : nullptr;
322
2/2
✓ Branch 0 taken 560 times.
✓ Branch 1 taken 140 times.
700 for (int j = 0; j < nt; ++j) {
323 560 const auto idx = static_cast<std::size_t>(i * nt + j);
324 560 const auto ju = static_cast<std::size_t>(j);
325
1/2
✓ Branch 0 taken 560 times.
✗ Branch 1 not taken.
560 if (use_fast_acc) {
326
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Value* zero32 = c.i32(0);
327
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Value* packed = c.b.build_call(
328
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ "ptx_mma_m16n8k16_f16_f16", Type::i64(),
329 ✗ {af_cur, b0[ju], b1[ju], zero32, zero32});
330
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Value* q0 = c.b.build_trunc_i32(packed);
331
0/6
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Value* q1 = c.b.build_trunc_i32(c.b.build_lshr(packed, c.kb.const_i64(32)));
332
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ auto [x0, x1] = unpack_pair_f32(c, Dtype::FP16, q0);
333
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ auto [x2, x3] = unpack_pair_f32(c, Dtype::FP16, q1);
334 ✗ Value* cur = accs[idx];
335
0/6
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ cur = c.b.build_vinsert_lane(cur, c.b.build_add(c.b.build_vextract_lane(cur, 0), x0), 0);
336
0/6
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ cur = c.b.build_vinsert_lane(cur, c.b.build_add(c.b.build_vextract_lane(cur, 1), x1), 1);
337
0/6
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ cur = c.b.build_vinsert_lane(cur, c.b.build_add(c.b.build_vextract_lane(cur, 2), x2), 2);
338
0/6
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ cur = c.b.build_vinsert_lane(cur, c.b.build_add(c.b.build_vextract_lane(cur, 3), x3), 3);
339 ✗ accs[idx] = cur;
340 ✗ } else {
341
1/2
✓ Branch 0 taken 560 times.
✗ Branch 1 not taken.
560 accs[idx] = c.b.build_call(
342
1/2
✓ Branch 0 taken 560 times.
✗ Branch 1 not taken.
560 mma_sym, Type::f32x4(),
343 560 {af_cur, b0[ju], b1[ju], accs[idx]});
344 }
345 560 }
346 140 af_cur = af_next;
347 140 }
348 42 }
349 21 };
350 24 auto emit_compute_stage = [&](int stage, std::vector<Value*>& accs) {
351 42 emit_compute_stage_off(
352
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 9 times.
21 (stage == 0) ? nullptr : c.i32(stage * a_stage_bytes),
353
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 9 times.
21 (stage == 0) ? nullptr : c.i32(stage * b_stage_bytes),
354 21 accs);
355 21 };
356
357 // Prologue: prefill stages 0 and 1.
358
4/8
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 3 times.
✗ Branch 7 not taken.
6 kb.if_then(c.b.build_slt(c.i32(0), KT), [&] { emit_load_stage(0, c.i32(0)); });
359
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
3 c.b.build_call("ptx_cp_async_commit", Type::void_type());
360
4/8
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 3 times.
✗ Branch 7 not taken.
6 kb.if_then(c.b.build_slt(c.i32(1), KT), [&] { emit_load_stage(1, c.i32(1)); });
361
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
3 c.b.build_call("ptx_cp_async_commit", Type::void_type());
362
363 3 std::vector<Value*> accs;
364
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (use_fast_acc) {
365 // Dynamic 3-stage ring avoids LICM hoisting of 3x-unrolled stage offsets,
366 // keeping live regs under sm_89's 128-reg/thread 2-CTA budget with 0 spills.
367 ✗ std::vector<Value*> init;
368
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ init.reserve(static_cast<std::size_t>(num_accs + 2));
369
0/6
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ for (int i = 0; i < num_accs; ++i) init.push_back(c.b.build_vzero(Type::f32x4()));
370
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ init.push_back(c.i32(0)); // sr
371
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ init.push_back(c.i32(2)); // sw
372
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ std::vector<Value*> st = kb.for_range_reduce_n(
373
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ c.i32(0), KT, c.i32(1), init,
374
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ [&](Value* kt, const std::vector<Value*>& cur) -> std::vector<Value*> {
375 ✗ std::vector<Value*> cur_accs(cur.begin(), cur.begin() + num_accs);
376 ✗ Value* sr = cur[static_cast<std::size_t>(num_accs)];
377 ✗ Value* sw = cur[static_cast<std::size_t>(num_accs + 1)];
378
0/6
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ c.b.build_call("ptx_cp_async_wait_group", Type::void_type(), {c.i32(1)});
379
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ kb.sync();
380
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Value* knext = c.b.build_add(kt, c.i32(2));
381
0/6
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ kb.if_then(c.b.build_slt(knext, KT), [&] {
382 ✗ emit_load_stage_off(
383 ✗ c.b.build_mul(sw, c.i32(a_stage_bytes)),
384 ✗ c.b.build_mul(sw, c.i32(b_stage_bytes)),
385 ✗ knext);
386 ✗ });
387
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ c.b.build_call("ptx_cp_async_commit", Type::void_type());
388
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ emit_compute_stage_off(
389
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ c.b.build_mul(sr, c.i32(a_stage_bytes)),
390
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ c.b.build_mul(sr, c.i32(b_stage_bytes)),
391 cur_accs);
392 ✗ auto inc_mod3 = [&](Value* v) {
393 ✗ return c.b.build_select(c.b.build_eq(v, c.i32(2)), c.i32(0), c.b.build_add(v, c.i32(1)));
394 };
395
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ cur_accs.push_back(inc_mod3(sr));
396
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ cur_accs.push_back(inc_mod3(sw));
397 ✗ return cur_accs;
398
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ });
399
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ accs.assign(st.begin(), st.begin() + num_accs);
400 ✗ } else {
401 // Initial accumulator fragments + loop-carried kt_rem counter.
402 3 std::vector<Value*> state_init;
403
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 state_init.reserve(static_cast<std::size_t>(num_accs + 1));
404
4/6
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 40 times.
✓ Branch 2 taken 40 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 40 times.
✗ Branch 5 not taken.
43 for (int i = 0; i < num_accs; ++i) state_init.push_back(c.b.build_vzero(Type::f32x4()));
405
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
3 state_init.push_back(c.i32(0));
406
407 // Hot unrolled 3-stage pipeline loop (step = 3, zero modulo or interior bounds branches).
408
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
3 Value* kt_limit = c.b.build_sub(KT, c.i32(4));
409
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 std::vector<Value*> state = kb.for_range_reduce_n(
410
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
3 c.i32(0), kt_limit, c.i32(3), state_init,
411
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
6 [&](Value* kt, const std::vector<Value*>& cur) -> std::vector<Value*> {
412 3 std::vector<Value*> cur_accs(cur.begin(), cur.begin() + num_accs);
413
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 9 times.
12 for (int s = 0; s < 3; ++s) {
414
3/6
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 9 times.
✗ Branch 5 not taken.
9 c.b.build_call("ptx_cp_async_wait_group", Type::void_type(), {c.i32(1)});
415
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 kb.sync();
416
3/6
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 9 times.
✗ Branch 5 not taken.
9 emit_load_stage((s + 2) % 3, c.b.build_add(kt, c.i32(s + 2)));
417
2/4
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
9 c.b.build_call("ptx_cp_async_commit", Type::void_type());
418
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 emit_compute_stage(s, cur_accs);
419 9 }
420
3/6
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
3 cur_accs.push_back(c.b.build_add(kt, c.i32(3)));
421 3 return cur_accs;
422
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 });
423
424 3 Value* kt_rem = state.back();
425
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 accs.assign(state.begin(), state.begin() + num_accs);
426
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 const std::vector<Type> acc_types(static_cast<std::size_t>(num_accs), Type::f32x4());
427
428 // 4-step tail (kt_rem % 3 == 0 by construction).
429
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 3 times.
15 for (int d = 0; d < 4; ++d) {
430 12 const int st_read = d % 3;
431 12 const int st_write = (d + 2) % 3;
432
4/6
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 9 times.
✗ Branch 5 not taken.
12 Value* kcur = (d == 0) ? kt_rem : c.b.build_add(kt_rem, c.i32(d));
433
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 accs = emit_if_else_values(
434
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 c, c.b.build_slt(kcur, KT), acc_types,
435 36 [&]() -> std::vector<Value*> {
436 24 std::vector<Value*> cur_accs = accs;
437
4/6
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 12 times.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 12 times.
✗ Branch 5 not taken.
24 c.b.build_call("ptx_cp_async_wait_group", Type::void_type(), {c.i32(1)});
438
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 kb.sync();
439
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 3 times.
12 if (d == 0 || d == 1) {
440
3/4
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
12 Value* knext = c.b.build_add(kcur, c.i32(2));
441
3/6
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 6 times.
✗ Branch 5 not taken.
12 kb.if_then(c.b.build_slt(knext, KT), [&] { emit_load_stage(st_write, knext); });
442 6 }
443
3/4
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
6 c.b.build_call("ptx_cp_async_commit", Type::void_type());
444
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 emit_compute_stage(st_read, cur_accs);
445 12 return cur_accs;
446
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
36 },
447 24 [&]() -> std::vector<Value*> { return accs; });
448 12 }
449 3 }
450
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
3 c.b.build_call("ptx_cp_async_wait_all", Type::void_type());
451
452 // Epilogue straight from m16n8k16 accumulators.
453
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
3 Value* g = c.b.build_lshr(lane, c.i32(2));
454
4/8
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 3 times.
✗ Branch 7 not taken.
3 Value* t2 = c.b.build_shl(c.b.build_and(lane, c.i32(3)), c.i32(1));
455 3 Value* p_base = nullptr;
456
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (spec.has_partial) {
457
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Value* z64 = c.b.build_zext_i64(kb.ctaid_z());
458
0/6
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Value* mn64 = c.b.build_mul(c.b.build_zext_i64(M), c.b.build_zext_i64(N));
459
0/8
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ p_base = c.b.build_add(p[4], c.b.build_shl(c.b.build_mul(z64, mn64), kb.const_i64(2)));
460 ✗ }
461
462
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 2 times.
3 if (swiglu_inline) {
463 1 const int wn_half = wn / 2;
464
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 Value* wn_half_base = c.b.build_mul(w_n, c.i32(wn_half));
465
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 time.
3 for (int j = 0; j < nt / 2; ++j) {
466
4/8
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2 times.
✗ Branch 7 not taken.
2 Value* col = c.b.build_add(c.b.build_add(n0_half, wn_half_base), c.b.build_add(c.i32(j * 8), t2));
467 4 auto emit_col = [&] {
468 2 Value* bg0 = nullptr;
469 2 Value* bg1 = nullptr;
470 2 Value* bu0 = nullptr;
471 2 Value* bu1 = nullptr;
472
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (spec.has_bias) {
473 2 Value* bgp = ptr_at(c, p[3], col, 2);
474 2 Value* bup = ptr_at(c, p[3], c.b.build_add(N_half, col), 2);
475 2 std::tie(bg0, bg1) = unpack_pair_f32(c, spec.dt, kb.load_i32(bgp));
476 2 std::tie(bu0, bu1) = unpack_pair_f32(c, spec.dt, kb.load_i32(bup));
477 2 }
478
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 4 times.
6 for (int i = 0; i < mt; ++i) {
479 4 Value* frag_g = accs[static_cast<std::size_t>(i * nt + j)];
480 4 Value* frag_u = accs[static_cast<std::size_t>(i * nt + j + nt / 2)];
481
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 4 times.
12 for (int h = 0; h < 2; ++h) {
482 16 Value* row = c.b.build_add(c.b.build_add(m0, wm_base),
483 8 c.b.build_add(c.i32(i * 16 + h * 8), g));
484 8 Value* g0 = c.b.build_vextract_lane(frag_g, 2 * h);
485 8 Value* g1 = c.b.build_vextract_lane(frag_g, 2 * h + 1);
486 8 Value* u0 = c.b.build_vextract_lane(frag_u, 2 * h);
487 8 Value* u1 = c.b.build_vextract_lane(frag_u, 2 * h + 1);
488 16 auto emit_store = [&] {
489
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (spec.has_bias) {
490 8 g0 = c.b.build_add(g0, bg0);
491 8 g1 = c.b.build_add(g1, bg1);
492 8 u0 = c.b.build_add(u0, bu0);
493 8 u1 = c.b.build_add(u1, bu1);
494 8 }
495 8 Value* y0 = c.b.build_mul(emit_gemm_act(c, 4, g0), u0);
496 8 Value* y1 = c.b.build_mul(emit_gemm_act(c, 4, g1), u1);
497 8 Value* dst_c = ptr_2d_i64(c, C_in, row, N_half, col, 1);
498 8 kb.store_i32(dst_c, pack_pair_i32(c, spec.dt, y0, y1));
499 8 };
500
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (spec.aligned_mnk) emit_store();
501
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 else kb.if_then(c.b.build_slt(row, M), emit_store);
502 8 }
503 4 }
504 2 };
505
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
2 if (spec.aligned_mnk) emit_col();
506
3/6
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
2 else kb.if_then(c.b.build_slt(col, N_half), emit_col);
507 2 }
508 1 } else {
509
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 2 times.
10 for (int j = 0; j < nt; ++j) {
510
4/8
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 8 times.
✗ Branch 7 not taken.
8 Value* col = c.b.build_add(c.b.build_add(n0, wn_base), c.b.build_add(c.i32(j * 8), t2));
511 16 auto emit_col = [&] {
512 8 Value* b0 = nullptr;
513 8 Value* b1 = nullptr;
514
2/4
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 8 times.
8 if (!spec.has_partial && spec.has_bias) {
515 8 Value* bp = ptr_at(c, p[3], col, 2);
516 8 std::tie(b0, b1) = unpack_pair_f32(c, spec.dt, kb.load_i32(bp));
517 8 }
518
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 32 times.
40 for (int i = 0; i < mt; ++i) {
519 32 Value* frag = accs[static_cast<std::size_t>(i * nt + j)];
520
2/2
✓ Branch 0 taken 64 times.
✓ Branch 1 taken 32 times.
96 for (int h = 0; h < 2; ++h) {
521 128 Value* row = c.b.build_add(c.b.build_add(m0, wm_base),
522 64 c.b.build_add(c.i32(i * 16 + h * 8), g));
523 64 Value* v0 = c.b.build_vextract_lane(frag, 2 * h);
524 64 Value* v1 = c.b.build_vextract_lane(frag, 2 * h + 1);
525 128 auto emit_store = [&] {
526
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 64 times.
64 if (spec.has_partial) {
527 ✗ Value* dst_p = ptr_2d_i64(c, p_base, row, N, col, 2);
528 ✗ kb.store_f32(dst_p, v0, 0);
529 ✗ kb.store_f32(dst_p, v1, 4);
530 ✗ } else {
531
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 64 times.
64 if (spec.has_bias) {
532 64 v0 = c.b.build_add(v0, b0);
533 64 v1 = c.b.build_add(v1, b1);
534 64 }
535
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 64 times.
64 if (spec.epi == 2) {
536 ✗ Value* gv = c.b.build_mul(v0, emit_gelu_exact(c, v1));
537 ✗ Value* col_h = c.b.build_lshr(col, c.i32(1));
538 ✗ Value* dst_g = ptr_2d_i64(c, C_in, row, N_half, col_h, 1);
539
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ store_lanes(c, spec.dt, 1, dst_g, {gv});
540 ✗ } else {
541
1/2
✓ Branch 0 taken 64 times.
✗ Branch 1 not taken.
64 if (spec.act != 0) {
542 ✗ v0 = emit_gemm_act(c, spec.act, v0);
543 ✗ v1 = emit_gemm_act(c, spec.act, v1);
544 ✗ }
545
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 64 times.
64 if (spec.f32_out) {
546 ✗ Value* dst_f = ptr_2d_i64(c, C_in, row, ldc, col, 2);
547 ✗ kb.store_f32(dst_f, v0, 0);
548 ✗ kb.store_f32(dst_f, v1, 4);
549 ✗ return;
550 }
551 64 Value* dst_c = ptr_2d_i64(c, C_in, row, ldc, col, 1);
552
1/2
✓ Branch 0 taken 64 times.
✗ Branch 1 not taken.
64 if (spec.epi == 1) {
553 ✗ auto [old0, old1] = unpack_pair_f32(c, spec.dt, kb.load_i32(dst_c));
554 ✗ v0 = c.b.build_add(v0, old0);
555 ✗ v1 = c.b.build_add(v1, old1);
556 ✗ }
557 64 kb.store_i32(dst_c, pack_pair_i32(c, spec.dt, v0, v1));
558 }
559 }
560 64 };
561
1/2
✓ Branch 0 taken 64 times.
✗ Branch 1 not taken.
64 if (spec.aligned_mnk) emit_store();
562
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ else kb.if_then(c.b.build_slt(row, M), emit_store);
563 64 }
564 32 }
565 8 };
566
2/4
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
8 if (spec.aligned_mnk) emit_col();
567
0/6
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ else kb.if_then(c.b.build_slt(col, N), emit_col);
568 8 }
569 }
570
571
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 c.b.build_ret_void();
572
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 fn->rebuild_cfg_predecessors();
573 3 return fn;
574 3 }
575
576 } // namespace brotensor::kernels
577