brotensor/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
361 / 431
83.8%
Branches:
333 / 768
43.4%
Functions:
24 / 27
88.9%

src/kernels/mir_conv_mma.cpp
Line Branch Exec Source
1 #include "mir_conv_mma.h"
2
3 #include "mir_common.h"
4
5 #include <algorithm>
6 #include <array>
7 #include <cstdint>
8 #include <stdexcept>
9 #include <utility>
10 #include <vector>
11
12 namespace brotensor::kernels {
13
14 namespace {
15
16 constexpr std::uint32_t kConvMmaBk = 32u;
17 constexpr std::uint32_t kConvMmaStages = 2u;
18
19 struct ConvShape { int kh, kw, ph, pw, sh, sw; };
20 constexpr ConvShape kConvShapes[7] = {
21 {3, 3, 1, 1, 1, 1}, // 0: 3x3 p1 s1
22 {1, 1, 0, 0, 1, 1}, // 1: 1x1 p0 s1
23 {3, 3, 1, 1, 2, 2}, // 2: 3x3 p1 s2
24 {3, 3, 0, 0, 1, 1}, // 3: 3x3 p0 s1
25 {7, 7, 3, 3, 1, 1}, // 4: 7x7 p3 s1
26 {7, 7, 0, 0, 1, 1}, // 5: 7x7 p0 s1
27 {5, 5, 2, 2, 1, 1}, // 6: 5x5 p2 s1
28 };
29
30 6 Value* swz_bytes(Ctx& c, Value* row, Value* chunk) {
31 6 Value* row_off = c.b.build_shl(row, c.i32(6));
32 6 Value* swz = c.b.build_and(c.b.build_ashr(row, c.i32(1)), c.i32(3));
33 6 return c.b.build_or(row_off, c.b.build_shl(c.b.build_xor(chunk, swz), c.i32(4)));
34 }
35
36 4 Value* swz_chunk(Ctx& c, Value* row, Value* chunk) {
37 4 Value* row_off = c.b.build_shl(row, c.i32(2));
38 4 Value* swz = c.b.build_and(c.b.build_ashr(row, c.i32(1)), c.i32(3));
39 4 return c.b.build_or(row_off, c.b.build_xor(chunk, swz));
40 }
41
42 4 Value* make_i32x4(Ctx& c, Value* w0, Value* w1, Value* w2, Value* w3) {
43 4 Value* v = c.b.build_vzero(Type::i32x4());
44 4 v = c.b.build_vinsert_lane(v, w0, 0);
45 4 v = c.b.build_vinsert_lane(v, w1, 1);
46 4 v = c.b.build_vinsert_lane(v, w2, 2);
47 4 return c.b.build_vinsert_lane(v, w3, 3);
48 }
49
50 32 Value* pack_u16_pair(Ctx& c, Value* lo, Value* hi) {
51 32 return c.b.build_or(c.b.build_and(lo, c.i32(0xffff)), c.b.build_shl(hi, c.i32(16)));
52 }
53
54 2 Value* ptr_2d_i64(Ctx& c, Value* base, Value* row, Value* ld, Value* col) {
55 4 Value* elem = c.b.build_add(
56 2 c.b.build_mul(c.b.build_zext_i64(row), c.b.build_zext_i64(ld)),
57 2 c.b.build_zext_i64(col));
58 2 return c.b.build_add(base, c.b.build_shl(elem, c.kb.const_i64(1)));
59 }
60
61 } // namespace
62
63 1 Function* build_conv2d_mma_mir(Module& mod, const Conv2dMmaSpec& spec) {
64
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) {
65
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ throw std::runtime_error("build_conv2d_mma_mir: only FP16/BF16 supported");
66 }
67
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 if (spec.shape_id >= 7u) {
68
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ throw std::runtime_error("build_conv2d_mma_mir: invalid shape_id");
69 }
70 1 const ConvShape& sh = kConvShapes[spec.shape_id];
71 1 const int khw_sz = sh.kh * sh.kw;
72 1 const Conv2dMmaCfg& cfg = kConv2dMmaCfgs[std::min<std::uint8_t>(spec.cfg, 1u)];
73 1 const int bm = static_cast<int>(cfg.bm);
74 1 const int bn = static_cast<int>(cfg.bn);
75 1 const int warps_n = static_cast<int>(cfg.warps_n);
76 1 const int threads = static_cast<int>(cfg.threads);
77 1 const int wm = bm / static_cast<int>(cfg.warps_m);
78 1 const int wn = bn / warps_n;
79 1 const int mt = wm / 16; // 2
80 1 const int nt = wn / 8; // 4
81 1 const int num_accs = mt * nt;
82 1 const int a_rows_thr = 2; // each warp of 32 lanes covers 2 rows x 8 cols per stage
83 1 const int b_chunks = (bn * 4) / threads;
84 1 const int a_stage_bytes = bm * static_cast<int>(kConvMmaBk) * 2;
85 1 const int b_stage_bytes = bn * static_cast<int>(kConvMmaBk) * 2;
86 1 const int a_stage_chunks = bm * 4;
87 1 const int sB_chunk_base = static_cast<int>(kConvMmaStages) * a_stage_chunks;
88 1 const int b_stage_chunks = bn * 4;
89
90 1 const int io = spec.split_k ? 1 : 0; // the int params sit after `part`
91 1 std::vector<Type> param_types(static_cast<std::size_t>(4 + io), Type::ptr());
92
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 param_types.insert(param_types.end(), static_cast<std::size_t>(10 + io), Type::i32());
93
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.split_k ? "mir_conv2d_mma_splitk" : "mir_conv2d_mma",
94 1 Type::void_type(), param_types);
95
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 brass::codegen::KernelBuilder kb(mod, fn);
96
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Ctx c(kb);
97
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 auto p = entry_params(c, fn);
98 // p: X(0) Wt(1) bias(2) Y(3) [part] | N C_in H W C_out H_out W_out M_tot HW_out K_tot [kts]
99 9 auto ip = [&](int i) { return p[static_cast<std::size_t>(i + io)]; };
100 1 Value* X = p[0];
101 1 Value* Wt = p[1];
102 1 Value* bias = p[2];
103 1 Value* Y = p[3];
104
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Value* C_in = ip(5);
105
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Value* H = ip(6);
106
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Value* W = ip(7);
107
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Value* C_out = ip(8);
108
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Value* W_out = ip(10);
109
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Value* M_tot = ip(11);
110
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Value* HW_out = ip(12);
111
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Value* K_tot = ip(13);
112
113
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Value* smem = kb.shared_alloc_u16(kConvMmaStages * (cfg.bm + cfg.bn) * kConvMmaBk);
114
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Value* sA_base = c.b.build_trunc_i32(smem);
115
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>(kConvMmaStages) * a_stage_bytes));
116
117
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Value* tid = kb.tid_x();
118
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));
119
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));
120
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));
121
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));
122
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 Value* m0 = c.b.build_mul(kb.ctaid_y(), c.i32(bm));
123
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 Value* n0 = c.b.build_mul(kb.ctaid_x(), c.i32(bn));
124
125
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Value* HW_in = c.b.build_mul(H, W);
126
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Value* CHW_in = c.b.build_mul(C_in, HW_in);
127 // K tiles [kt0, kt0 + KT): all of them, or split z's kts of them.
128
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_tot, c.i32(31)), c.i32(5));
129
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Value* kt0 = c.i32(0);
130
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
1 if (spec.split_k) {
131
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.
✗ kt0 = c.b.build_mul(kb.ctaid_z(), ip(14));
132
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.
✗ KT = smin_i32(c, ip(14), c.b.build_sub(KT, kt0));
133 ✗ }
134
135 // Coalesced NCHW A-tile layout:
136 // Every warp reads 2 rows (32 consecutive spatial pixels) at a warp-uniform ch in 0..3.
137
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 Value* a_ch = c.b.build_and(warp, c.i32(3));
138
2/8
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
1 Value* a_w_sub = (spec.cfg == 0u) ? c.i32(0) : c.b.build_lshr(warp, c.i32(2));
139
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Value* a_base_row = (spec.cfg == 0u)
140 1 ? lane
141
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_add(c.b.build_shl(a_w_sub, c.i32(6)), lane);
142
143 struct ARowInfo {
144 Value* chunk_off;
145 Value* m_ok;
146 Value* is_int;
147 Value* x_n;
148 Value* ih0;
149 Value* iw0;
150 Value* x_fast;
151 };
152 1 std::array<ARowInfo, 2> ar{};
153
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 2 times.
3 for (int r = 0; r < a_rows_thr; ++r) {
154
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* row = (r == 0) ? a_base_row : c.b.build_add(a_base_row, c.i32(32));
155
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 Value* m_g = c.b.build_add(m0, row);
156
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 Value* m_ok = c.b.build_slt(m_g, M_tot);
157
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 Value* safe_m = c.b.build_select(m_ok, m_g, c.i32(0));
158
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 Value* n_b = c.b.build_udiv(safe_m, HW_out);
159
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 Value* sp = c.b.build_umod(safe_m, HW_out);
160
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 Value* oh = c.b.build_udiv(sp, W_out);
161
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 Value* ow = c.b.build_umod(sp, W_out);
162
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
4 Value* ih0 = (sh.kh == 1 && sh.sh == 1 && sh.ph == 0)
163 ✗ ? oh
164
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 : c.b.build_sub(c.b.build_mul(oh, c.i32(sh.sh)), c.i32(sh.ph));
165
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
4 Value* iw0 = (sh.kw == 1 && sh.sw == 1 && sh.pw == 0)
166 ✗ ? ow
167
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 : c.b.build_sub(c.b.build_mul(ow, c.i32(sh.sw)), c.i32(sh.pw));
168
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 Value* in_h_ok = c.b.build_and(c.b.build_sge(ih0, c.i32(0)),
169
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 c.b.build_sle(c.b.build_add(ih0, c.i32(sh.kh)), H));
170
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 Value* in_w_ok = c.b.build_and(c.b.build_sge(iw0, c.i32(0)),
171
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 c.b.build_sle(c.b.build_add(iw0, c.i32(sh.kw)), W));
172
1/8
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
4 Value* is_int = (sh.kh == 1 && sh.kw == 1 && sh.ph == 0 && sh.pw == 0)
173 ✗ ? m_ok
174
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 : c.b.build_and(m_ok, c.b.build_and(in_h_ok, in_w_ok));
175
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
4 Value* x_n = c.b.build_add(X, c.b.build_shl(
176
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 c.b.build_mul(c.b.build_zext_i64(n_b), c.b.build_zext_i64(CHW_in)), kb.const_i64(1)));
177
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 Value* hw_off = c.b.build_add(c.b.build_mul(ih0, W), iw0);
178
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 Value* safe_hw = c.b.build_select(is_int, hw_off, c.i32(0));
179
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* x_fast = c.b.build_add(x_n, c.b.build_shl(c.b.build_zext_i64(safe_hw), kb.const_i64(1)));
180
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 ar[static_cast<std::size_t>(r)] = {swz_chunk(c, row, a_ch), m_ok, is_int, x_n, ih0, iw0, x_fast};
181 2 }
182
183 // Precompute B descriptors (cp.async when aligned_k8, guarded u16 loads otherwise).
184
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* lane_ch_k = c.b.build_shl(c.b.build_and(tid, c.i32(3)), c.i32(3));
185
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));
186
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 std::vector<Value*> b_chunk_off(static_cast<std::size_t>(b_chunks));
187
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));
188
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 std::vector<Value*> b_row_ok(static_cast<std::size_t>(b_chunks));
189
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 2 times.
3 for (int i = 0; i < b_chunks; ++i) {
190 2 const auto iu = static_cast<std::size_t>(i);
191
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));
192
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));
193
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));
194
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_bytes(c, row, ch));
195
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 b_chunk_off[iu] = c.b.build_add(c.i32(sB_chunk_base), swz_chunk(c, row, ch));
196
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 Value* oc = c.b.build_add(n0, row);
197
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 b_row_ok[iu] = c.b.build_slt(oc, C_out);
198
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 b_g_base[iu] = ptr_2d_i64(c, Wt, oc, K_tot, c.b.build_shl(ch, c.i32(3)));
199 2 }
200
201 // Precompute ldmatrix shared addresses (kk = 0; kk = 1 is addr ^ 32).
202
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));
203
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));
204
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));
205
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 std::vector<Value*> a_ld_off(static_cast<std::size_t>(mt));
206
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 2 times.
3 for (int i = 0; i < mt; ++i) {
207
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* row = c.b.build_add(c.b.build_add(wm_base, c.i32(i * 16)), a_lane_row);
208
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 a_ld_off[static_cast<std::size_t>(i)] = c.b.build_add(sA_base, swz_bytes(c, row, a_lane_ch));
209 2 }
210
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));
211
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* b_lane_row = c.b.build_add(c.b.build_shl(c.b.build_lshr(mi, c.i32(1)), c.i32(3)),
212
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)));
213
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 Value* b_lane_ch = c.b.build_and(mi, c.i32(1));
214
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));
215
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));
216
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 2 times.
3 for (int j = 0; j < nt / 2; ++j) {
217
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* row = c.b.build_add(c.b.build_add(wn_base, c.i32(j * 16)), b_lane_row);
218
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 b_ld_off[static_cast<std::size_t>(j)] = c.b.build_add(sB_base, swz_bytes(c, row, b_lane_ch));
219 2 }
220
221 3 auto emit_load_B_cp = [&](Value* st_idx, Value* kt_idx) {
222 2 Value* b_st_off = c.b.build_mul(st_idx, c.i32(b_stage_bytes));
223 2 Value* kt_bytes = c.b.build_shl(c.b.build_zext_i64(kt_idx), kb.const_i64(6));
224 2 Value* k_ok = c.b.build_slt(c.b.build_add(c.b.build_shl(kt_idx, c.i32(5)), lane_ch_k), K_tot);
225
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
6 for (int i = 0; i < b_chunks; ++i) {
226 4 const auto iu = static_cast<std::size_t>(i);
227 4 Value* dst = c.b.build_add(b_s_off[iu], b_st_off);
228 4 Value* src = c.b.build_add(b_g_base[iu], kt_bytes);
229 4 Value* ok = c.b.build_and(b_row_ok[iu], k_ok);
230 4 Value* safe_src = c.b.build_select(ok, src, Wt);
231 4 Value* sz = c.b.build_select(ok, c.i32(16), c.i32(0));
232 4 c.b.build_call("ptx_cp_async_cg_16", Type::void_type(), {dst, safe_src, sz});
233 4 }
234 2 c.b.build_call("ptx_cp_async_commit", Type::void_type());
235 2 };
236
237 1 auto emit_fetch_B_u16 = [&](Value* kt_idx) -> std::vector<Value*> {
238 ✗ std::vector<Value*> packed(static_cast<std::size_t>(b_chunks * 4));
239
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* kt_bytes = c.b.build_shl(c.b.build_zext_i64(kt_idx), kb.const_i64(6));
240
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* gk0 = c.b.build_add(c.b.build_shl(kt_idx, c.i32(5)), lane_ch_k);
241
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ for (int i = 0; i < b_chunks; ++i) {
242 ✗ const auto iu = static_cast<std::size_t>(i);
243
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Value* base = c.b.build_add(b_g_base[iu], kt_bytes);
244
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ for (int pr = 0; pr < 4; ++pr) {
245 ✗ Value* u0 = nullptr;
246 ✗ Value* u1 = nullptr;
247
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ for (int q = 0; q < 2; ++q) {
248 ✗ const int e = 2 * pr + q;
249
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* ok = c.b.build_and(b_row_ok[iu], c.b.build_slt(c.b.build_add(gk0, c.i32(e)), K_tot));
250
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Value* src = c.b.build_add(base, kb.const_i64(e * 2));
251
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Value* v = c.b.build_select(
252
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.
✗ ok, kb.load_u16(c.b.build_select(ok, src, Wt)), c.i32(0));
253
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (q == 0) u0 = v;
254 ✗ else u1 = v;
255 ✗ }
256
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ packed[iu * 4u + static_cast<std::size_t>(pr)] = pack_u16_pair(c, u0, u1);
257 ✗ }
258 ✗ }
259 ✗ return packed;
260
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ };
261
262 3 auto emit_fetch_A_u16 = [&](Value* kt_idx) -> std::vector<Value*> {
263 // Precompute warp-uniform k offsets across q = 0..7 once for both rows.
264 2 Value* gk0 = c.b.build_add(c.b.build_shl(kt_idx, c.i32(5)), c.b.build_shl(a_ch, c.i32(3)));
265 2 std::array<Value*, 8> ic{}, kh{}, kw{}, k_off{}, k_ok{};
266
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 2 times.
18 for (int q = 0; q < 8; ++q) {
267 16 Value* gk = c.b.build_add(gk0, c.i32(q));
268 16 k_ok[q] = c.b.build_slt(gk, K_tot);
269 16 Value* safe_k = c.b.build_select(k_ok[q], gk, c.i32(0));
270
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 if (spec.shape_id == 1u) {
271 ✗ ic[q] = safe_k;
272 ✗ k_off[q] = c.b.build_mul(ic[q], HW_in);
273 ✗ } else {
274 16 ic[q] = c.b.build_udiv(safe_k, c.i32(khw_sz));
275 16 Value* rem = c.b.build_umod(safe_k, c.i32(khw_sz));
276 16 kh[q] = c.b.build_udiv(rem, c.i32(sh.kw));
277 16 kw[q] = c.b.build_umod(rem, c.i32(sh.kw));
278 32 k_off[q] = c.b.build_add(
279 16 c.b.build_mul(ic[q], HW_in),
280 16 c.b.build_add(c.b.build_mul(kh[q], W), kw[q]));
281 }
282 16 }
283 2 Value* all_k_ok = k_ok[7];
284 2 std::vector<Value*> out(8);
285
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
6 for (int r = 0; r < a_rows_thr; ++r) {
286 4 const ARowInfo& a = ar[static_cast<std::size_t>(r)];
287
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* fast_ok = c.b.build_and(a.is_int, all_k_ok);
288
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 auto vals = emit_if_else_values(
289
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 c, fast_ok, std::vector<Type>(4, Type::i32()),
290 8 [&]() -> std::vector<Value*> {
291 4 std::vector<Value*> res(4);
292
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 16 times.
20 for (int pr = 0; pr < 4; ++pr) {
293
4/8
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 16 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 16 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 16 times.
✗ Branch 7 not taken.
16 Value* p0 = c.b.build_add(a.x_fast, c.b.build_shl(c.b.build_zext_i64(k_off[2 * pr]), kb.const_i64(1)));
294
4/8
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 16 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 16 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 16 times.
✗ Branch 7 not taken.
16 Value* p1 = c.b.build_add(a.x_fast, c.b.build_shl(c.b.build_zext_i64(k_off[2 * pr + 1]), kb.const_i64(1)));
295
3/6
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 16 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 16 times.
✗ Branch 5 not taken.
16 res[static_cast<std::size_t>(pr)] = pack_u16_pair(c, kb.load_u16(p0), kb.load_u16(p1));
296 16 }
297 4 return res;
298
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 },
299 8 [&]() -> std::vector<Value*> {
300
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 return emit_if_else_values(
301 4 c, a.m_ok, std::vector<Type>(4, Type::i32()),
302 8 [&]() -> std::vector<Value*> {
303 4 std::vector<Value*> res(4);
304
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 4 times.
20 for (int pr = 0; pr < 4; ++pr) {
305 Value* u[2];
306
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 32 times.
48 for (int q0 = 0; q0 < 2; ++q0) {
307 32 const int q = 2 * pr + q0;
308
1/2
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
32 if (spec.shape_id == 1u) {
309
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Value* p0 = c.b.build_add(
310
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.
✗ a.x_fast, c.b.build_shl(c.b.build_zext_i64(k_off[q]), kb.const_i64(1)));
311
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ u[q0] = c.b.build_select(
312
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.
✗ k_ok[q], kb.load_u16(c.b.build_select(k_ok[q], p0, X)), c.i32(0));
313 ✗ } else {
314
1/2
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
32 Value* ih = c.b.build_add(a.ih0, kh[q]);
315
1/2
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
32 Value* iw = c.b.build_add(a.iw0, kw[q]);
316
1/2
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
64 Value* ok = c.b.build_and(
317 32 k_ok[q],
318
1/2
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
64 c.b.build_and(
319
4/8
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 32 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 32 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 32 times.
✗ Branch 7 not taken.
32 c.b.build_and(c.b.build_sge(ih, c.i32(0)), c.b.build_slt(ih, H)),
320
4/8
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 32 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 32 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 32 times.
✗ Branch 7 not taken.
32 c.b.build_and(c.b.build_sge(iw, c.i32(0)), c.b.build_slt(iw, W))));
321
1/2
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
64 Value* xoff = c.b.build_add(
322
1/2
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
32 c.b.build_mul(ic[q], HW_in),
323
2/4
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 32 times.
✗ Branch 3 not taken.
32 c.b.build_add(c.b.build_mul(ih, W), iw));
324
2/4
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 32 times.
✗ Branch 3 not taken.
32 Value* safe_off = c.b.build_select(ok, xoff, c.i32(0));
325
1/2
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
64 u[q0] = c.b.build_select(
326
3/6
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 32 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 32 times.
✗ Branch 5 not taken.
32 ok, kb.load_u16(ptr_at(c, a.x_n, safe_off, 2)), c.i32(0));
327 }
328 32 }
329
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
16 res[static_cast<std::size_t>(pr)] = pack_u16_pair(c, u[0], u[1]);
330 16 }
331 4 return res;
332
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 },
333 8 [&]() -> std::vector<Value*> {
334 4 return {c.i32(0), c.i32(0), c.i32(0), c.i32(0)};
335 });
336 ✗ });
337
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 4 times.
20 for (int pr = 0; pr < 4; ++pr) {
338 16 out[static_cast<std::size_t>(r * 4 + pr)] = vals[static_cast<std::size_t>(pr)];
339 16 }
340 4 }
341 2 return out;
342
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 };
343
344 3 auto emit_store_A = [&](Value* st_idx, const std::vector<Value*>& a_words) {
345 2 Value* a_ch_off = c.b.build_mul(st_idx, c.i32(a_stage_chunks));
346
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
6 for (int r = 0; r < a_rows_thr; ++r) {
347 4 const std::size_t o = static_cast<std::size_t>(r * 4);
348 4 Value* v4 = make_i32x4(c, a_words[o], a_words[o + 1], a_words[o + 2], a_words[o + 3]);
349 8 kb.shared_store_i32x4_indexed(
350 4 smem, c.b.build_add(ar[static_cast<std::size_t>(r)].chunk_off, a_ch_off), v4);
351 4 }
352 2 };
353
354 1 auto emit_store_B_u16 = [&](Value* st_idx, const std::vector<Value*>& b_words) {
355 ✗ Value* b_ch_off = c.b.build_mul(st_idx, c.i32(b_stage_chunks));
356
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ for (int i = 0; i < b_chunks; ++i) {
357 ✗ const std::size_t o = static_cast<std::size_t>(i * 4);
358 ✗ Value* v4 = make_i32x4(c, b_words[o], b_words[o + 1], b_words[o + 2], b_words[o + 3]);
359 ✗ kb.shared_store_i32x4_indexed(
360 ✗ smem, c.b.build_add(b_chunk_off[static_cast<std::size_t>(i)], b_ch_off), v4);
361 ✗ }
362 ✗ };
363
364 1 const char* mma_sym = (spec.dt == Dtype::FP16) ? "ptx_mma_m16n8k16_f16_f32"
365 : "ptx_mma_m16n8k16_bf16_f32";
366 3 auto emit_mma = [&](Value* st_idx, std::vector<Value*>& accs) {
367 2 Value* a_st_off = c.b.build_mul(st_idx, c.i32(a_stage_bytes));
368 2 Value* b_st_off = c.b.build_mul(st_idx, c.i32(b_stage_bytes));
369
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
6 for (int kk = 0; kk < 2; ++kk) {
370 12 auto a_addr = [&](int i) -> Value* {
371 8 Value* p0 = c.b.build_add(a_ld_off[static_cast<std::size_t>(i)], a_st_off);
372
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
8 return (kk == 0) ? p0 : c.b.build_xor(p0, c.i32(32));
373 };
374 12 auto b_addr = [&](int j) -> Value* {
375 8 Value* p0 = c.b.build_add(b_ld_off[static_cast<std::size_t>(j)], b_st_off);
376
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
8 return (kk == 0) ? p0 : c.b.build_xor(p0, c.i32(32));
377 };
378 4 std::vector<Value*> b0(static_cast<std::size_t>(nt));
379
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 std::vector<Value*> b1(static_cast<std::size_t>(nt));
380
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 8 times.
12 for (int j = 0; j < nt / 2; ++j) {
381
3/6
✓ 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.
8 Value* r = c.b.build_call("ptx_ldmatrix_x4", Type::i32x4(), {b_addr(j)});
382 8 const auto j2 = static_cast<std::size_t>(2 * j);
383
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 b0[j2] = c.b.build_vextract_lane(r, 0);
384
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 b1[j2] = c.b.build_vextract_lane(r, 1);
385
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 b0[j2 + 1u] = c.b.build_vextract_lane(r, 2);
386
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 b1[j2 + 1u] = c.b.build_vextract_lane(r, 3);
387 8 }
388
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* af_cur = c.b.build_call("ptx_ldmatrix_x4", Type::i32x4(), {a_addr(0)});
389
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 8 times.
12 for (int i = 0; i < mt; ++i) {
390
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
12 Value* af_next = (i + 1 < mt)
391
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 ? c.b.build_call("ptx_ldmatrix_x4", Type::i32x4(), {a_addr(i + 1)})
392 : nullptr;
393
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 8 times.
40 for (int j = 0; j < nt; ++j) {
394 32 const auto idx = static_cast<std::size_t>(i * nt + j);
395 32 const auto ju = static_cast<std::size_t>(j);
396
2/4
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 32 times.
✗ Branch 3 not taken.
32 accs[idx] = c.b.build_call(mma_sym, Type::f32x4(), {af_cur, b0[ju], b1[ju], accs[idx]});
397 32 }
398 8 af_cur = af_next;
399 8 }
400 4 }
401 2 };
402
403
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), [&] {
404
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
1 if (spec.aligned_k8) emit_load_B_cp(c.i32(0), kt0);
405 1 auto a0 = emit_fetch_A_u16(kt0);
406
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 emit_store_A(c.i32(0), a0);
407
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
1 if (!spec.aligned_k8) {
408
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ auto b0 = emit_fetch_B_u16(kt0);
409
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ emit_store_B_u16(c.i32(0), b0);
410 ✗ } else {
411
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 c.b.build_call("ptx_cp_async_wait_group", Type::void_type(), {c.i32(0)});
412 }
413
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 kb.sync();
414 1 });
415
416 1 std::vector<Value*> init;
417
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 init.reserve(static_cast<std::size_t>(num_accs + 1));
418
4/6
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
9 for (int i = 0; i < num_accs; ++i) init.push_back(c.b.build_vzero(Type::f32x4()));
419
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 init.push_back(c.i32(0));
420
421
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 Value* kt_lim = c.b.build_sub(KT, c.i32(1));
422
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 std::vector<Value*> state = kb.for_range_reduce_n(
423
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_lim, c.i32(1), init,
424
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
2 [&](Value* kt, const std::vector<Value*>& cur) -> std::vector<Value*> {
425 1 std::vector<Value*> cur_accs(cur.begin(), cur.begin() + num_accs);
426 1 Value* cur_st = cur.back();
427
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 Value* nxt_st = c.b.build_xor(cur_st, c.i32(1));
428
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 Value* knext = c.b.build_add(c.b.build_add(kt0, kt), c.i32(1));
429
430
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 if (spec.aligned_k8) emit_load_B_cp(nxt_st, knext);
431
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 auto a_next = emit_fetch_A_u16(knext);
432 1 std::vector<Value*> b_next;
433
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
1 if (!spec.aligned_k8) b_next = emit_fetch_B_u16(knext);
434
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 emit_mma(cur_st, cur_accs);
435
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 emit_store_A(nxt_st, a_next);
436
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
1 if (!spec.aligned_k8) {
437
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ emit_store_B_u16(nxt_st, b_next);
438 ✗ } else {
439
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 c.b.build_call("ptx_cp_async_wait_group", Type::void_type(), {c.i32(0)});
440 }
441
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 kb.sync();
442
443
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 cur_accs.push_back(nxt_st);
444 1 return cur_accs;
445
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 });
446
447
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 std::vector<Type> tail_types(static_cast<std::size_t>(num_accs), Type::f32x4());
448
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 std::vector<Value*> init_accs(state.begin(), state.begin() + num_accs);
449 1 Value* tail_st = state.back();
450
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 std::vector<Value*> accs = emit_if_else_values(
451
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 c, c.b.build_sgt(KT, c.i32(0)), tail_types,
452 2 [&]() -> std::vector<Value*> {
453 1 std::vector<Value*> a = init_accs;
454
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 emit_mma(tail_st, a);
455 1 return a;
456
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 },
457 2 [&]() -> std::vector<Value*> { return init_accs; });
458
459 // Direct register epilogue: Y[(n * C_out + oc) * HW_out + sp].
460
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));
461
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));
462
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Value* CHW_out = c.b.build_mul(C_out, HW_out);
463 // Split-K: split 0 stores Y (+ bias); split z > 0 stores FP32 into
464 // part[(z - 1) * M_tot * C_out + same index], folded into Y afterwards.
465 1 Value* first_split = nullptr;
466 1 Value* later_split = nullptr;
467 1 Value* part_base = nullptr;
468
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
1 if (spec.split_k) {
469
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Value* z = kb.ctaid_z();
470
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ first_split = c.b.build_eq(z, c.i32(0));
471
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ later_split = c.b.build_ne(z, c.i32(0));
472
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* zm1 = c.b.build_select(first_split, c.i32(0), c.b.build_sub(z, c.i32(1)));
473
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* n_out64 = c.b.build_mul(c.b.build_zext_i64(M_tot), c.b.build_zext_i64(C_out));
474
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.
✗ part_base = c.b.build_add(p[4], c.b.build_shl(c.b.build_mul(c.b.build_zext_i64(zm1), n_out64),
475
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ kb.const_i64(2)));
476 ✗ }
477
478
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 2 times.
3 for (int i = 0; i < mt; ++i) {
479
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
6 for (int h = 0; h < 2; ++h) {
480
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* m_g = c.b.build_add(c.b.build_add(m0, wm_base), c.b.build_add(c.i32(i * 16 + h * 8), g));
481
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.
8 kb.if_then(c.b.build_slt(m_g, M_tot), [&] {
482 4 Value* n_b = c.b.build_udiv(m_g, HW_out);
483 4 Value* sp = c.b.build_umod(m_g, HW_out);
484 8 Value* y_base = c.b.build_add(
485 8 Y, c.b.build_shl(
486 8 c.b.build_add(c.b.build_mul(c.b.build_zext_i64(n_b), c.b.build_zext_i64(CHW_out)),
487 4 c.b.build_zext_i64(sp)),
488 4 kb.const_i64(1)));
489
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 4 times.
20 for (int j = 0; j < nt; ++j) {
490 16 Value* frag = accs[static_cast<std::size_t>(i * nt + j)];
491
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 32 times.
48 for (int c_off = 0; c_off < 2; ++c_off) {
492 64 Value* oc = c.b.build_add(
493 32 c.b.build_add(n0, wn_base), c.b.build_add(c.i32(j * 8 + c_off), t2));
494
1/2
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
64 kb.if_then(c.b.build_slt(oc, C_out), [&] {
495 32 Value* v = c.b.build_vextract_lane(frag, 2 * h + c_off);
496 32 Value* oc_off = c.b.build_mul(c.b.build_zext_i64(oc), c.b.build_zext_i64(HW_out));
497 64 auto store_y = [&] {
498 32 Value* vy = v;
499
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
32 if (spec.has_bias) {
500 32 vy = c.b.build_add(vy, load_elem_dt(c, spec.dt, bias, oc));
501 32 }
502 32 Value* dst = c.b.build_add(y_base, c.b.build_shl(oc_off, kb.const_i64(1)));
503
1/2
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
32 store_lanes(c, spec.dt, 1, dst, {vy});
504 32 };
505
1/2
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
32 if (!spec.split_k) {
506 32 store_y();
507 32 return;
508 }
509
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ kb.if_then(first_split, store_y);
510
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ kb.if_then(later_split, [&] {
511 ✗ Value* idx = c.b.build_add(
512 ✗ c.b.build_add(c.b.build_mul(c.b.build_zext_i64(n_b), c.b.build_zext_i64(CHW_out)),
513 ✗ c.b.build_zext_i64(sp)),
514 ✗ oc_off);
515 ✗ kb.store_f32(c.b.build_add(part_base, c.b.build_shl(idx, kb.const_i64(2))), v, 0);
516 ✗ });
517 32 });
518 32 }
519 16 }
520 4 });
521 4 }
522 2 }
523
524
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 c.b.build_ret_void();
525
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 fn->rebuild_cfg_predecessors();
526 1 return fn;
527 1 }
528
529 } // namespace brotensor::kernels
530