brotensor/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
256 / 302
84.8%
Branches:
258 / 532
48.5%
Functions:
17 / 18
94.4%

src/kernels/mir_gemm_mma_nn.cpp
Line Branch Exec Source
1 #include "mir_gemm_mma_nn.h"
2 #include "mir_gemm_mma.h"
3 #include "mir_common.h"
4
5 #include <algorithm>
6 #include <cstdint>
7 #include <stdexcept>
8 #include <vector>
9
10 namespace brotensor::kernels {
11
12 namespace {
13
14 constexpr std::uint32_t kNnBk = 32u;
15 constexpr std::uint32_t kNnStages = 3u;
16 constexpr std::uint32_t kNnL2GroupM = 8u;
17
18 // A tile has 32 FP16/BF16 cols -> 64 bytes/row -> 4 x 16B chunks per row.
19 6 Value* swz_a_bytes(Ctx& c, Value* row, Value* ch) {
20 6 Value* swz = c.b.build_and(c.b.build_ashr(row, c.i32(1)), c.i32(3));
21 12 return c.b.build_or(c.b.build_shl(row, c.i32(6)),
22 6 c.b.build_shl(c.b.build_xor(ch, swz), c.i32(4)));
23 }
24
25 // B tile has BN cols: bn==128 -> 256 bytes/row (16 chunks), bn==64 -> 128 bytes/row (8 chunks).
26 4 Value* swz_b_bytes(Ctx& c, Value* k_row, Value* n_ch, int bn_shift) {
27 4 Value* swz = c.b.build_and(k_row, c.i32(7));
28 8 return c.b.build_or(c.b.build_shl(k_row, c.i32(bn_shift)),
29 4 c.b.build_shl(c.b.build_xor(n_ch, swz), c.i32(4)));
30 }
31
32 36 Value* ptr_2d_i64(Ctx& c, Value* base, Value* row, Value* ld, Value* col) {
33 72 Value* elem = c.b.build_add(
34 36 c.b.build_mul(c.b.build_zext_i64(row), c.b.build_zext_i64(ld)),
35 36 c.b.build_zext_i64(col));
36 36 return c.b.build_add(base, c.b.build_shl(elem, c.kb.const_i64(1)));
37 }
38
39 32 Value* pack_pair_i32(Ctx& c, Dtype dt, Value* v0, Value* v1) {
40
1/2
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
32 if (dt == Dtype::FP16) return c.kb.pack_f16x2(v0, v1);
41 ✗ Value* b0 = c.b.build_and(c.kb.f32_to_bf16(v0), c.i32(0xffff));
42 ✗ Value* b1 = c.b.build_shl(c.kb.f32_to_bf16(v1), c.i32(16));
43 ✗ return c.b.build_or(b0, b1);
44 32 }
45
46 } // namespace
47
48 1 Function* build_gemm_mma_nn_mir(Module& mod, const GemmMmaNnSpec& spec) {
49
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
1 if (spec.dt != Dtype::FP16 && spec.dt != Dtype::BF16) {
50
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ throw std::runtime_error("build_gemm_mma_nn_mir: only FP16/BF16 supported");
51 }
52 1 const GemmMmaCfg& cfg = kGemmMmaCfgs[std::min<std::uint8_t>(spec.cfg, 3u)];
53 1 const int bm = static_cast<int>(cfg.bm);
54 1 const int bn = static_cast<int>(cfg.bn);
55 1 const int warps_n = static_cast<int>(cfg.warps_n);
56 1 const int threads = static_cast<int>(cfg.threads);
57 1 const int wm = bm / static_cast<int>(cfg.warps_m);
58 1 const int wn = bn / warps_n;
59 1 const int mt = wm / 16;
60 1 const int nt = wn / 8;
61 1 const int a_chunks = (bm * 4) / threads;
62 1 const int bn_ch = bn / 8;
63 1 const int bn_ch_shift = (bn == 128) ? 4 : 3;
64 1 const int bn_row_shift = (bn == 128) ? 8 : 7; // log2(bn * 2 bytes)
65 1 const int b_chunks = (static_cast<int>(kNnBk) * bn_ch) / threads;
66 1 const int num_accs = mt * nt;
67 1 const int a_stage_bytes = bm * static_cast<int>(kNnBk) * 2;
68 1 const int b_stage_bytes = static_cast<int>(kNnBk) * bn * 2;
69
70 5 std::vector<Type> param_types = {Type::ptr(), Type::ptr(), Type::ptr(),
71 4 Type::i32(), Type::i32(), Type::i32(), Type::i32()};
72 1 const int extra = (spec.batched ? 3 : 0) + (spec.strided ? 3 : 0);
73
1/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
1 for (int i = 0; i < extra; ++i) param_types.push_back(Type::i32());
74
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 Function* fn = mod.create_function(spec.f32_out ? "mir_gemm_mma_nn_f32" : "mir_gemm_mma_nn",
75 1 Type::void_type(), param_types);
76
77
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 brass::codegen::KernelBuilder kb(mod, fn);
78
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Ctx c(kb);
79
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 auto p = entry_params(c, fn);
80 // p: A(0) B(1) C(2) M(3) N(4) K(5) grid_m(6) [sA sB sC] [lda ldb ldc]
81 1 Value* A = p[0];
82 1 Value* B = p[1];
83 1 Value* C = p[2];
84
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
1 if (spec.batched) {
85
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());
86 ✗ auto shift_batch = [&](Value* base, Value* stride32, int shift) {
87 ✗ Value* elems = c.b.build_mul(bz64, c.b.build_zext_i64(stride32));
88 ✗ return c.b.build_add(base, c.b.build_shl(elems, kb.const_i64(shift)));
89 };
90
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ A = shift_batch(A, p[7], 1);
91
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ B = shift_batch(B, p[8], 1);
92
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ C = shift_batch(C, p[9], spec.f32_out ? 2 : 1);
93 ✗ }
94 1 Value* M = p[3];
95 1 Value* N = p[4];
96 1 Value* K = p[5];
97 1 Value* grid_m = p[6];
98 1 const std::size_t ld0 = spec.batched ? 10u : 7u;
99
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
1 Value* lda = spec.strided ? p[ld0] : K;
100
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
1 Value* ldb = spec.strided ? p[ld0 + 1] : N;
101
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
1 Value* ldc = spec.strided ? p[ld0 + 2] : N;
102
103
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Value* smem = kb.shared_alloc_u16(kNnStages * (cfg.bm + cfg.bn) * kNnBk);
104
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Value* sA_base = c.b.build_trunc_i32(smem);
105
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 Value* sB_base = c.b.build_add(sA_base, c.i32(static_cast<int>(kNnStages) * a_stage_bytes));
106
107
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Value* tid = kb.tid_x();
108
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 Value* lane = c.b.build_and(tid, c.i32(31));
109
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 Value* warp = c.b.build_lshr(tid, c.i32(5));
110
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 Value* w_m = c.b.build_udiv(warp, c.i32(warps_n));
111
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 Value* w_n = c.b.build_umod(warp, c.i32(warps_n));
112
113
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Value* bx = kb.ctaid_x();
114
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Value* by = kb.ctaid_y();
115 1 Value* tile_m = by;
116 1 Value* tile_n = bx;
117
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
1 if (spec.swizzle_l2) {
118
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Value* gn = kb.nctaid_x();
119
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);
120
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>(kNnL2GroupM)), gn);
121
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Value* slab = c.b.build_udiv(linear, per_slab);
122
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Value* in_slab = c.b.build_umod(linear, per_slab);
123
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>(kNnL2GroupM)));
124
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>(kNnL2GroupM)), c.b.build_sub(grid_m, m_start));
125
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ tile_n = c.b.build_udiv(in_slab, slab_h);
126
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));
127 ✗ }
128
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 Value* m0 = c.b.build_mul(tile_m, c.i32(bm));
129
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 Value* n0 = c.b.build_mul(tile_n, c.i32(bn));
130
4/8
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 time.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 time.
✗ Branch 7 not taken.
1 Value* KT = c.b.build_lshr(c.b.build_add(K, c.i32(31)), c.i32(5));
131
132 // Precompute cp.async source/dest addresses + single-i32 guard bitmask.
133
4/8
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 time.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 time.
✗ Branch 7 not taken.
1 Value* a_lane_k = c.b.build_shl(c.b.build_and(tid, c.i32(3)), c.i32(3));
134
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
1 Value* guard_mask = spec.aligned_mnk ? nullptr : c.i32(0);
135
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 std::vector<Value*> a_s_off(static_cast<std::size_t>(a_chunks));
136
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 std::vector<Value*> a_g_base(static_cast<std::size_t>(a_chunks));
137
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 2 times.
3 for (int i = 0; i < a_chunks; ++i) {
138 2 const auto iu = static_cast<std::size_t>(i);
139
4/6
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 time.
✗ Branch 5 not taken.
2 Value* lin = (i == 0) ? tid : c.b.build_add(tid, c.i32(i * threads));
140
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 Value* row = c.b.build_lshr(lin, c.i32(2));
141
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 Value* ch = c.b.build_and(lin, c.i32(3));
142
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 a_s_off[iu] = c.b.build_add(sA_base, swz_a_bytes(c, row, ch));
143
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 Value* gr = c.b.build_add(m0, row);
144
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 a_g_base[iu] = ptr_2d_i64(c, A, gr, lda, c.b.build_shl(ch, c.i32(3)));
145
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!spec.aligned_mnk) {
146
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.
✗ Value* bit = c.b.build_select(c.b.build_slt(gr, M), c.i32(1 << i), c.i32(0));
147
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ guard_mask = c.b.build_or(guard_mask, bit);
148 ✗ }
149 2 }
150
151
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 std::vector<Value*> b_s_off(static_cast<std::size_t>(b_chunks));
152
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 std::vector<Value*> b_g_base(static_cast<std::size_t>(b_chunks));
153
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 std::vector<Value*> b_k_row(static_cast<std::size_t>(b_chunks));
154
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 2 times.
3 for (int i = 0; i < b_chunks; ++i) {
155 2 const auto iu = static_cast<std::size_t>(i);
156
4/6
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 time.
✗ Branch 5 not taken.
2 Value* lin = (i == 0) ? tid : c.b.build_add(tid, c.i32(i * threads));
157
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 Value* k_row = c.b.build_lshr(lin, c.i32(bn_ch_shift));
158
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 Value* n_ch = c.b.build_and(lin, c.i32(bn_ch - 1));
159
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 b_s_off[iu] = c.b.build_add(sB_base, swz_b_bytes(c, k_row, n_ch, bn_row_shift));
160 2 b_k_row[iu] = k_row;
161
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* gcol = c.b.build_add(n0, c.b.build_shl(n_ch, c.i32(3)));
162
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 b_g_base[iu] = ptr_2d_i64(c, B, k_row, ldb, gcol);
163
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!spec.aligned_mnk) {
164
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.
✗ Value* bit = c.b.build_select(c.b.build_slt(gcol, N), c.i32(1 << (a_chunks + i)), c.i32(0));
165
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ guard_mask = c.b.build_or(guard_mask, bit);
166 ✗ }
167 2 }
168
169 // Precompute ldmatrix addresses for kk=0; kk=1 is xor 32 for A and +(16*bn*2) = xor (16<<bn_row_shift) for B.
170
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 Value* a_lane_row = c.b.build_and(lane, c.i32(15));
171
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 Value* a_lane_ch = c.b.build_lshr(lane, c.i32(4));
172
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 Value* wm_base = c.b.build_mul(w_m, c.i32(wm));
173
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 std::vector<Value*> a_ld_off(static_cast<std::size_t>(mt));
174
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 4 times.
5 for (int i = 0; i < mt; ++i) {
175
3/6
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
4 Value* row = c.b.build_add(c.b.build_add(wm_base, c.i32(i * 16)), a_lane_row);
176
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 a_ld_off[static_cast<std::size_t>(i)] = c.b.build_add(sA_base, swz_a_bytes(c, row, a_lane_ch));
177 4 }
178
179
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 Value* mi = c.b.build_lshr(lane, c.i32(3));
180
5/10
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 time.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 time.
✗ Branch 7 not taken.
✓ Branch 8 taken 1 time.
✗ Branch 9 not taken.
2 Value* bk_row0 = c.b.build_add(c.b.build_shl(c.b.build_and(mi, c.i32(1)), c.i32(3)),
181
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 c.b.build_and(lane, c.i32(7)));
182
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 Value* b_lane_nch = c.b.build_lshr(mi, c.i32(1));
183
4/8
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 time.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 time.
✗ Branch 7 not taken.
1 Value* wn_base_ch = c.b.build_lshr(c.b.build_mul(w_n, c.i32(wn)), c.i32(3));
184
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 std::vector<Value*> b_ld_off(static_cast<std::size_t>(nt / 2));
185
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 2 times.
3 for (int j = 0; j < nt / 2; ++j) {
186
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* n_ch = c.b.build_add(c.b.build_add(wn_base_ch, c.i32(j * 2)), b_lane_nch);
187
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 b_ld_off[static_cast<std::size_t>(j)] = c.b.build_add(
188
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 sB_base, swz_b_bytes(c, bk_row0, n_ch, bn_row_shift));
189 2 }
190 1 const int b_kk1_off = 16 << bn_row_shift; // +16 K-rows in byte offset (swz unchanged)
191
192
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Value* N64 = c.b.build_zext_i64(ldb);
193 8 auto emit_load_stage = [&](int stage, Value* kt_idx) {
194 7 Value* k0 = c.b.build_shl(kt_idx, c.i32(5));
195 7 Value* a_k_bytes = c.b.build_shl(c.b.build_zext_i64(kt_idx), kb.const_i64(6)); // 32 elems * 2B
196 7 Value* b_k_bytes = c.b.build_shl(c.b.build_mul(c.b.build_zext_i64(k0), N64), kb.const_i64(1));
197
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 Value* a_k_ok_mask = spec.aligned_mnk
198 ? nullptr
199 ✗ : c.b.build_select(c.b.build_slt(c.b.build_add(k0, a_lane_k), K), guard_mask, c.i32(0));
200 7 Value* a_st_off = c.i32(stage * a_stage_bytes);
201 7 Value* b_st_off = c.i32(stage * b_stage_bytes);
202
203
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 7 times.
21 for (int i = 0; i < a_chunks; ++i) {
204 14 const auto iu = static_cast<std::size_t>(i);
205
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 6 times.
14 Value* dst = (stage == 0) ? a_s_off[iu] : c.b.build_add(a_s_off[iu], a_st_off);
206 14 Value* src = c.b.build_add(a_g_base[iu], a_k_bytes);
207
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 if (spec.aligned_mnk) {
208 14 c.b.build_call("ptx_cp_async_cg_16", Type::void_type(), {dst, src});
209 14 } else {
210 ✗ Value* ok = c.b.build_ne(c.b.build_and(a_k_ok_mask, c.i32(1 << i)), c.i32(0));
211 ✗ c.b.build_call("ptx_cp_async_cg_16", Type::void_type(),
212 ✗ {dst, c.b.build_select(ok, src, A), c.b.build_select(ok, c.i32(16), c.i32(0))});
213 }
214 14 }
215
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 7 times.
21 for (int i = 0; i < b_chunks; ++i) {
216 14 const auto iu = static_cast<std::size_t>(i);
217
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 6 times.
14 Value* dst = (stage == 0) ? b_s_off[iu] : c.b.build_add(b_s_off[iu], b_st_off);
218 14 Value* src = c.b.build_add(b_g_base[iu], b_k_bytes);
219
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 if (spec.aligned_mnk) {
220 14 c.b.build_call("ptx_cp_async_cg_16", Type::void_type(), {dst, src});
221 14 } else {
222 ✗ Value* k_ok = c.b.build_slt(c.b.build_add(k0, b_k_row[iu]), K);
223 ✗ Value* n_ok = c.b.build_ne(c.b.build_and(guard_mask, c.i32(1 << (a_chunks + i))), c.i32(0));
224 ✗ Value* ok = c.b.build_and(k_ok, n_ok);
225 ✗ c.b.build_call("ptx_cp_async_cg_16", Type::void_type(),
226 ✗ {dst, c.b.build_select(ok, src, B), c.b.build_select(ok, c.i32(16), c.i32(0))});
227 }
228 14 }
229 7 };
230
231 1 const char* mma_sym = (spec.dt == Dtype::FP16) ? "ptx_mma_m16n8k16_f16_f32"
232 : "ptx_mma_m16n8k16_bf16_f32";
233
234 8 auto emit_compute_stage = [&](int stage, std::vector<Value*>& accs) {
235 7 Value* a_st_off = c.i32(stage * a_stage_bytes);
236 7 Value* b_st_off = c.i32(stage * b_stage_bytes);
237
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 7 times.
21 for (int kk = 0; kk < 2; ++kk) {
238 70 auto a_addr = [&](int i) -> Value* {
239 56 Value* base = a_ld_off[static_cast<std::size_t>(i)];
240
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 24 times.
56 Value* p0 = (stage == 0) ? base : c.b.build_add(base, a_st_off);
241
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 28 times.
56 return (kk == 0) ? p0 : c.b.build_xor(p0, c.i32(32));
242 };
243 42 auto b_addr = [&](int j) -> Value* {
244 28 Value* base = b_ld_off[static_cast<std::size_t>(j)];
245
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 12 times.
28 Value* p0 = (stage == 0) ? base : c.b.build_add(base, b_st_off);
246
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 14 times.
28 return (kk == 0) ? p0 : c.b.build_add(p0, c.i32(b_kk1_off));
247 };
248
249 14 std::vector<Value*> b0(static_cast<std::size_t>(nt));
250
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
14 std::vector<Value*> b1(static_cast<std::size_t>(nt));
251
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 28 times.
42 for (int j = 0; j < nt / 2; ++j) {
252
3/6
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 28 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 28 times.
✗ Branch 5 not taken.
28 Value* r = c.b.build_call("ptx_ldmatrix_x4_trans", Type::i32x4(), {b_addr(j)});
253 28 const auto j2 = static_cast<std::size_t>(2 * j);
254
1/2
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
28 b0[j2] = c.b.build_vextract_lane(r, 0);
255
1/2
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
28 b1[j2] = c.b.build_vextract_lane(r, 1);
256
1/2
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
28 b0[j2 + 1u] = c.b.build_vextract_lane(r, 2);
257
1/2
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
28 b1[j2 + 1u] = c.b.build_vextract_lane(r, 3);
258 28 }
259
260
3/6
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 14 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 14 times.
✗ Branch 5 not taken.
14 Value* af_cur = c.b.build_call("ptx_ldmatrix_x4", Type::i32x4(), {a_addr(0)});
261
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 56 times.
70 for (int i = 0; i < mt; ++i) {
262
2/2
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 14 times.
98 Value* af_next = (i + 1 < mt)
263
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 ? c.b.build_call("ptx_ldmatrix_x4", Type::i32x4(), {a_addr(i + 1)})
264 : nullptr;
265
2/2
✓ Branch 0 taken 224 times.
✓ Branch 1 taken 56 times.
280 for (int j = 0; j < nt; ++j) {
266 224 const auto idx = static_cast<std::size_t>(i * nt + j);
267
1/2
✓ Branch 0 taken 224 times.
✗ Branch 1 not taken.
224 accs[idx] = c.b.build_call(
268
1/2
✓ Branch 0 taken 224 times.
✗ Branch 1 not taken.
224 mma_sym, Type::f32x4(),
269 224 {af_cur, b0[static_cast<std::size_t>(j)], b1[static_cast<std::size_t>(j)], accs[idx]});
270 224 }
271 56 af_cur = af_next;
272 56 }
273 14 }
274 7 };
275
276
4/8
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 time.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 time.
✗ Branch 7 not taken.
2 kb.if_then(c.b.build_slt(c.i32(0), KT), [&] { emit_load_stage(0, c.i32(0)); });
277
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 c.b.build_call("ptx_cp_async_commit", Type::void_type());
278
4/8
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 time.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 time.
✗ Branch 7 not taken.
2 kb.if_then(c.b.build_slt(c.i32(1), KT), [&] { emit_load_stage(1, c.i32(1)); });
279
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 c.b.build_call("ptx_cp_async_commit", Type::void_type());
280
281 1 std::vector<Value*> state_init;
282
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 state_init.reserve(static_cast<std::size_t>(num_accs + 1));
283
4/6
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 16 times.
✓ Branch 2 taken 16 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 16 times.
✗ Branch 5 not taken.
17 for (int i = 0; i < num_accs; ++i) state_init.push_back(c.b.build_vzero(Type::f32x4()));
284
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 state_init.push_back(c.i32(0));
285
286
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 Value* kt_limit = c.b.build_sub(KT, c.i32(4));
287
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 std::vector<Value*> state = kb.for_range_reduce_n(
288
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 c.i32(0), kt_limit, c.i32(3), state_init,
289
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
2 [&](Value* kt, const std::vector<Value*>& cur) -> std::vector<Value*> {
290 1 std::vector<Value*> accs(cur.begin(), cur.begin() + num_accs);
291
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 3 times.
4 for (int s = 0; s < 3; ++s) {
292
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 c.b.build_call("ptx_cp_async_wait_group", Type::void_type(), {c.i32(1)});
293
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 kb.sync();
294
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 emit_load_stage((s + 2) % 3, c.b.build_add(kt, c.i32(s + 2)));
295
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());
296
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 emit_compute_stage(s, accs);
297 3 }
298
3/6
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 time.
✗ Branch 5 not taken.
1 accs.push_back(c.b.build_add(kt, c.i32(3)));
299 1 return accs;
300
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 });
301
302 1 Value* kt_rem = state.back();
303
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 std::vector<Value*> accs(state.begin(), state.begin() + num_accs);
304
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 const std::vector<Type> acc_types(static_cast<std::size_t>(num_accs), Type::f32x4());
305
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 4 times.
5 for (int d = 0; d < 4; ++d) {
306 4 const int st_read = d % 3;
307 4 const int st_write = (d + 2) % 3;
308
4/6
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
4 Value* kcur = (d == 0) ? kt_rem : c.b.build_add(kt_rem, c.i32(d));
309
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 accs = emit_if_else_values(
310
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 c, c.b.build_slt(kcur, KT), acc_types,
311 12 [&]() -> std::vector<Value*> {
312 8 std::vector<Value*> cur_accs = accs;
313
4/6
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
8 c.b.build_call("ptx_cp_async_wait_group", Type::void_type(), {c.i32(1)});
314
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 kb.sync();
315
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 time.
4 if (d == 0 || d == 1) {
316
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
4 Value* knext = c.b.build_add(kcur, c.i32(2));
317
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.
4 kb.if_then(c.b.build_slt(knext, KT), [&] { emit_load_stage(st_write, knext); });
318 2 }
319
3/4
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
2 c.b.build_call("ptx_cp_async_commit", Type::void_type());
320
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 emit_compute_stage(st_read, cur_accs);
321 4 return cur_accs;
322
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
12 },
323 8 [&]() -> std::vector<Value*> { return accs; });
324 4 }
325
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 c.b.build_call("ptx_cp_async_wait_all", Type::void_type());
326
327
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 Value* g = c.b.build_lshr(lane, c.i32(2));
328
4/8
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 time.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 time.
✗ Branch 7 not taken.
1 Value* t2 = c.b.build_shl(c.b.build_and(lane, c.i32(3)), c.i32(1));
329
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 Value* wn_base = c.b.build_mul(w_n, c.i32(wn));
330
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 4 times.
5 for (int j = 0; j < nt; ++j) {
331
4/8
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 4 times.
✗ Branch 7 not taken.
4 Value* col = c.b.build_add(c.b.build_add(n0, wn_base), c.b.build_add(c.i32(j * 8), t2));
332 8 auto emit_col = [&] {
333
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 4 times.
20 for (int i = 0; i < mt; ++i) {
334 16 Value* frag = accs[static_cast<std::size_t>(i * nt + j)];
335
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 16 times.
48 for (int h = 0; h < 2; ++h) {
336 64 Value* row = c.b.build_add(c.b.build_add(m0, wm_base),
337 32 c.b.build_add(c.i32(i * 16 + h * 8), g));
338 32 Value* v0 = c.b.build_vextract_lane(frag, 2 * h);
339 32 Value* v1 = c.b.build_vextract_lane(frag, 2 * h + 1);
340 64 auto emit_st = [&] {
341
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
32 if (spec.f32_out) {
342 ✗ Value* elem = c.b.build_add(
343 ✗ c.b.build_mul(c.b.build_zext_i64(row), c.b.build_zext_i64(ldc)),
344 ✗ c.b.build_zext_i64(col));
345 ✗ Value* dst = c.b.build_add(C, c.b.build_shl(elem, kb.const_i64(2)));
346 ✗ kb.store_f32(dst, v0, 0);
347 ✗ kb.store_f32(dst, v1, 4);
348 ✗ return;
349 }
350 32 kb.store_i32(ptr_2d_i64(c, C, row, ldc, col), pack_pair_i32(c, spec.dt, v0, v1));
351 32 };
352
1/2
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
32 if (spec.aligned_mnk) emit_st();
353
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ else kb.if_then(c.b.build_slt(row, M), emit_st);
354 32 }
355 16 }
356 4 };
357
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 if (spec.aligned_mnk) emit_col();
358
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);
359 4 }
360
361
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 c.b.build_ret_void();
362
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 fn->rebuild_cfg_predecessors();
363 1 return fn;
364 1 }
365
366 } // namespace brotensor::kernels
367