brotensor/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
212 / 219
96.8%
Branches:
195 / 388
50.3%
Functions:
14 / 14
100.0%

src/kernels/mir_conv_igemm_f32.cpp
Line Branch Exec Source
1 #include "mir_conv_igemm_f32.h"
2
3 #include "mir_common.h"
4
5 #include <array>
6 #include <cstdint>
7 #include <string>
8 #include <vector>
9
10 namespace brotensor::kernels {
11
12 namespace {
13
14 constexpr int kBm = 128;
15 constexpr int kBn = 64;
16 constexpr int kBnp = kBn + 4; // padded W row
17 constexpr int kBk = 16;
18 constexpr int kAPix = 8; // A-tile pixels a thread loads
19 constexpr int kAcc = 32; // 8 pixels x 4 channels a thread
20
21 using jit::gpu_mir::bits_to_f32;
22
23 8 Value* make_f32x4(Ctx& c, Value* v0, Value* v1, Value* v2, Value* v3) {
24 8 Value* v = c.b.build_vzero(Type::f32x4());
25 8 v = c.b.build_vinsert_lane(v, v0, 0);
26 8 v = c.b.build_vinsert_lane(v, v1, 1);
27 8 v = c.b.build_vinsert_lane(v, v2, 2);
28 8 return c.b.build_vinsert_lane(v, v3, 3);
29 }
30
31 } // namespace
32
33 1 Function* build_conv2d_igemm_f32_mir(Module& mod, bool has_bias, bool vec_store, bool idx32) {
34 1 std::vector<Type> params(5, Type::ptr());
35
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 params.insert(params.end(), 20, Type::i32());
36
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 const std::string name = std::string("mir_conv2d_igemm_f32") + (idx32 ? "_i32" : "");
37
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Function* fn = mod.create_function(name, Type::void_type(), params);
38
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 brass::codegen::KernelBuilder kb(mod, fn);
39
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Ctx c(kb);
40
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 auto p = entry_params(c, fn);
41 // 0:X 1:Wt 2:bias 3:Y 4:part
42 // 5:Cin 6:H 7:W 8:Cout 9:kH 10:kW 11:sh 12:sw 13:ph 14:pw 15:dh 16:dw 17:Ho 18:Wo 19:M 20:K 21:KHW
43 // 22:HWo 23:kz (K a split, a multiple of 16) 24:n (N Cout HWo)
44 1 Value* X = p[0];
45 1 Value* Wt = p[1];
46 1 Value* bias = p[2];
47 1 Value* Cin = p[5];
48 1 Value* H = p[6];
49 1 Value* W = p[7];
50 1 Value* Cout = p[8];
51 1 Value* kW = p[10];
52 1 Value* sh = p[11];
53 1 Value* sw = p[12];
54 1 Value* ph = p[13];
55 1 Value* pw = p[14];
56 1 Value* dh = p[15];
57 1 Value* dw = p[16];
58 1 Value* Wo = p[18];
59 1 Value* M = p[19];
60 1 Value* KHW = p[21];
61 1 Value* HWo = p[22];
62
63 // Split-K: block z sums K in [z kz, min(K, (z+1) kz)). Split 0 writes Y
64 // with the bias; split z > 0 writes part + (z-1) n, which a fold adds in.
65
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Value* kz_id = kb.ctaid_z();
66
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 Value* split0 = c.b.build_eq(kz_id, c.i32(0));
67
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Value* k_begin = c.b.build_mul(kz_id, p[23]);
68
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.
2 Value* K = c.b.build_select(c.b.build_slt(c.b.build_add(k_begin, p[23]), p[20]),
69
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 c.b.build_add(k_begin, p[23]), p[20]); // this split's K end
70
6/12
✓ 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.
✓ Branch 10 taken 1 time.
✗ Branch 11 not taken.
2 Value* part_off = c.b.build_mul(c.b.build_zext_i64(c.b.build_select(split0, c.i32(0), c.b.build_sub(kz_id, c.i32(1)))),
71
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 c.b.build_zext_i64(p[24]));
72
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* Y = c.b.build_select(split0, p[3], c.b.build_add(p[4], c.b.build_shl(part_off, kb.const_i64(2))));
73
74
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Value* As = kb.shared_alloc_f32(2u * kBk * kBm); // [2][16][128]
75
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Value* Bs = kb.shared_alloc_f32(2u * kBk * kBnp); // [2][16][68]
76
77
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Value* tid = kb.tid_x();
78
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_x(), c.i32(kBm));
79
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_y(), c.i32(kBn));
80
81 // A loader: each thread owns one K row (a_k) of the tile and 8 pixels
82 // a_m + 16 q, so a load instruction reads 16 consecutive pixels a row
83 // and the K index decodes once a tile, not once an element. Per pixel,
84 // fixed for the whole K loop: the image's base offset and the window's
85 // top-left (ih0, iw0); a pixel past M gets ih0 = -2^30, which no tap
86 // brings into [0, H). idx32: X has < 2^31 elements, offsets stay 32-bit.
87
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 Value* a_m = c.b.build_and(tid, c.i32(15));
88
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 Value* a_k = c.b.build_lshr(tid, c.i32(4));
89
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Value* hw_in = c.b.build_mul(H, W);
90
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Value* chw_in = c.b.build_mul(Cin, hw_in);
91 1 std::array<Value*, kAPix> pix_base{}, pix_ih0{}, pix_iw0{};
92
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 8 times.
9 for (int q = 0; q < kAPix; ++q) {
93
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* m_glob = c.b.build_add(m0, c.b.build_add(a_m, c.i32(16 * q)));
94
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 Value* m_ok = c.b.build_slt(m_glob, M);
95
2/4
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
8 Value* safe_m = c.b.build_select(m_ok, m_glob, c.i32(0));
96
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 Value* n_img = c.b.build_udiv(safe_m, HWo);
97
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 Value* rem_p = c.b.build_umod(safe_m, HWo);
98
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 Value* oh = c.b.build_udiv(rem_p, Wo);
99
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 Value* ow = c.b.build_umod(rem_p, Wo);
100
2/4
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
8 Value* ih0 = c.b.build_sub(c.b.build_mul(oh, sh), ph);
101
2/4
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
8 Value* iw0 = c.b.build_sub(c.b.build_mul(ow, sw), pw);
102
2/4
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
8 pix_ih0[q] = c.b.build_select(m_ok, ih0, c.i32(-(1 << 30)));
103 8 pix_iw0[q] = iw0;
104 // n chw + ih0 W + iw0, signed (the window may start in the padding).
105
2/4
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
8 Value* win = c.b.build_add(c.b.build_mul(ih0, W), iw0);
106
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 pix_base[q] = idx32 ? c.b.build_add(c.b.build_mul(n_img, chw_in), win)
107
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.
✗ : c.b.build_add(c.b.build_mul(c.b.build_zext_i64(n_img), c.b.build_zext_i64(chw_in)),
108
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ c.b.build_sext_i64(win));
109 8 }
110
111 // W loader: each thread owns output channel b_n and 4 consecutive K (b_k + 0..3).
112
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 Value* b_n = c.b.build_lshr(tid, c.i32(2));
113
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* b_k = c.b.build_shl(c.b.build_and(tid, c.i32(3)), c.i32(2));
114
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Value* bn_glob = c.b.build_add(n0, b_n);
115
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Value* n_ok = c.b.build_slt(bn_glob, Cout);
116
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 Value* safe_bn = c.b.build_select(n_ok, bn_glob, c.i32(0));
117
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
2 Value* wrow = c.b.build_add(
118
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.
1 Wt, c.b.build_shl(c.b.build_mul(c.b.build_zext_i64(safe_bn), c.b.build_zext_i64(p[20])), kb.const_i64(2)));
119
120 3 auto load_tiles = [&](Value* k0) -> std::vector<Value*> {
121 2 std::vector<Value*> regs(kAPix + 4);
122
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 Value* k = c.b.build_add(k0, a_k);
123
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 Value* k_ok = c.b.build_slt(k, K);
124
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 Value* sk = c.b.build_select(k_ok, k, c.i32(0));
125
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 Value* ic = c.b.build_udiv(sk, KHW);
126
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 Value* r = c.b.build_umod(sk, KHW);
127
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 Value* kh = c.b.build_udiv(r, kW);
128
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 Value* kw = c.b.build_umod(r, kW);
129
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 Value* dkh = c.b.build_mul(kh, dh);
130
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 Value* dkw = c.b.build_mul(kw, dw);
131 // ic H W + dkh W + dkw: the tap's offset from a window's top-left.
132
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 Value* tap = c.b.build_add(c.b.build_mul(dkh, W), dkw);
133
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* koff = idx32 ? c.b.build_add(c.b.build_mul(ic, hw_in), tap)
134
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.
✗ : c.b.build_add(c.b.build_mul(c.b.build_zext_i64(ic), c.b.build_zext_i64(hw_in)),
135
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ c.b.build_sext_i64(tap));
136
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 2 times.
18 for (int q = 0; q < kAPix; ++q) {
137 // Unsigned compares fold ih >= 0 into ih < H.
138
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
16 Value* ih = c.b.build_add(pix_ih0[q], dkh);
139
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
16 Value* iw = c.b.build_add(pix_iw0[q], dkw);
140
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* in_ok = c.b.build_and(k_ok, c.b.build_and(c.b.build_ult(ih, H), c.b.build_ult(iw, W)));
141
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
16 Value* off = c.b.build_add(pix_base[q], koff);
142
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* idx = idx32 ? c.b.build_zext_i64(c.b.build_select(in_ok, off, c.i32(0)))
143
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ : c.b.build_select(in_ok, off, kb.const_i64(0));
144
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* loaded = kb.load_f32(c.b.build_add(X, c.b.build_shl(idx, kb.const_i64(2))));
145
2/4
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 16 times.
✗ Branch 3 not taken.
16 regs[static_cast<std::size_t>(q)] = c.b.build_select(in_ok, loaded, c.f32(0.0f));
146 16 }
147
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 8 times.
10 for (int e = 0; e < 4; ++e) {
148
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* kk = c.b.build_add(k0, c.b.build_add(b_k, c.i32(e)));
149
2/4
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
8 Value* ok = c.b.build_and(n_ok, c.b.build_slt(kk, K));
150
2/4
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
8 Value* skk = c.b.build_select(ok, kk, c.i32(0));
151 8 regs[static_cast<std::size_t>(kAPix + e)] =
152
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 c.b.build_select(ok, kb.load_f32(ptr_at(c, wrow, skk, 4)), c.f32(0.0f));
153 8 }
154 2 return regs;
155
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 };
156
157 3 auto store_tiles = [&](Value* buf, const std::vector<Value*>& regs) {
158 2 Value* a_buf = c.b.build_mul(buf, c.i32(kBk * kBm));
159 2 Value* b_buf = c.b.build_mul(buf, c.i32(kBk * kBnp));
160 2 Value* a_row = c.b.build_add(a_buf, c.b.build_add(c.b.build_mul(a_k, c.i32(kBm)), a_m));
161
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 2 times.
18 for (int q = 0; q < kAPix; ++q) {
162 16 kb.shared_store_f32_indexed(As, c.b.build_add(a_row, c.i32(16 * q)), regs[static_cast<std::size_t>(q)]);
163 16 }
164
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 2 times.
10 for (int e = 0; e < 4; ++e) {
165 8 Value* k = c.b.build_add(b_k, c.i32(e));
166 8 Value* sidx = c.b.build_add(b_buf, c.b.build_add(c.b.build_mul(k, c.i32(kBnp)), b_n));
167 8 kb.shared_store_f32_indexed(Bs, sidx, regs[static_cast<std::size_t>(kAPix + e)]);
168 8 }
169 2 };
170
171 // Prefill stage 0.
172
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 store_tiles(c.i32(0), load_tiles(k_begin));
173
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 kb.sync();
174
175 // Compute: thread (tm, tn) holds pixels {tm*4 + 0..3, 64 + tm*4 + 0..3}
176 // x channels tn*4 + 0..3; acc[i * 4 + j].
177
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 Value* tm = c.b.build_and(tid, c.i32(15));
178
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 Value* tn = c.b.build_lshr(tid, c.i32(4));
179 2113 auto acc_at = [](int i, int j) { return static_cast<std::size_t>(i * 4 + j); };
180
181 3 auto emit_fma_tile = [&](Value* buf, std::vector<Value*>& accs) {
182 2 Value* a_buf_v4 = c.b.build_mul(buf, c.i32(kBk * (kBm / 4)));
183 2 Value* b_buf_v4 = c.b.build_mul(buf, c.i32(kBk * (kBnp / 4)));
184
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 2 times.
34 for (int k = 0; k < kBk; ++k) {
185 64 Value* a0_v4 = kb.shared_load_i32x4_indexed(
186 32 As, c.b.build_add(a_buf_v4, c.b.build_add(c.i32(k * (kBm / 4)), tm)));
187 64 Value* a1_v4 = kb.shared_load_i32x4_indexed(
188 32 As, c.b.build_add(a_buf_v4, c.b.build_add(c.i32(k * (kBm / 4) + 16), tm)));
189 32 std::array<Value*, 8> av{};
190
2/2
✓ Branch 0 taken 128 times.
✓ Branch 1 taken 32 times.
160 for (std::uint32_t u = 0; u < 4; ++u) {
191 128 av[u] = bits_to_f32(c, c.b.build_vextract_lane(a0_v4, u));
192 128 av[4u + u] = bits_to_f32(c, c.b.build_vextract_lane(a1_v4, u));
193 128 }
194 64 Value* b_v4 = kb.shared_load_i32x4_indexed(
195 32 Bs, c.b.build_add(b_buf_v4, c.b.build_add(c.i32(k * (kBnp / 4)), tn)));
196 32 std::array<Value*, 4> bv{};
197
2/2
✓ Branch 0 taken 128 times.
✓ Branch 1 taken 32 times.
160 for (std::uint32_t u = 0; u < 4; ++u) bv[u] = bits_to_f32(c, c.b.build_vextract_lane(b_v4, u));
198
2/2
✓ Branch 0 taken 256 times.
✓ Branch 1 taken 32 times.
288 for (int i = 0; i < 8; ++i) {
199
2/2
✓ Branch 0 taken 1024 times.
✓ Branch 1 taken 256 times.
1280 for (int j = 0; j < 4; ++j) {
200 2048 accs[acc_at(i, j)] = c.b.build_fma_f32(av[static_cast<std::size_t>(i)],
201 1024 bv[static_cast<std::size_t>(j)], accs[acc_at(i, j)]);
202 1024 }
203 256 }
204 32 }
205 2 };
206
207
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 std::vector<Value*> init(kAcc + 1, c.f32(0.0f));
208
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 init[kAcc] = c.i32(0); // buf
209
210
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 Value* k_limit = c.b.build_sub(K, c.i32(kBk));
211
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 std::vector<Value*> state = kb.for_range_reduce_n(
212
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 k_begin, k_limit, c.i32(kBk), init,
213
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
2 [&](Value* k0, const std::vector<Value*>& cur) -> std::vector<Value*> {
214 1 std::vector<Value*> cur_accs(cur.begin(), cur.begin() + kAcc);
215 1 Value* buf = cur[kAcc];
216
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 Value* nxt_buf = c.b.build_xor(buf, c.i32(1));
217
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 auto nxt_regs = load_tiles(c.b.build_add(k0, c.i32(kBk)));
218
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 emit_fma_tile(buf, cur_accs);
219
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 store_tiles(nxt_buf, nxt_regs);
220
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 kb.sync();
221
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 cur_accs.push_back(nxt_buf);
222 1 return cur_accs;
223
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 });
224
225
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 std::vector<Value*> accs(state.begin(), state.begin() + kAcc);
226
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 emit_fma_tile(state[kAcc], accs);
227
228 // Epilogue scatter: Y[n, oc, sp] (+ bias[oc]).
229
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* cout_hwo = c.b.build_mul(c.b.build_zext_i64(Cout), c.b.build_zext_i64(HWo));
230
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Value* hwo64 = c.b.build_zext_i64(HWo);
231 {
232
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 4 times.
5 for (int j = 0; j < 4; ++j) {
233
5/10
✓ 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.
✓ Branch 8 taken 4 times.
✗ Branch 9 not taken.
4 Value* oc = c.b.build_add(n0, c.b.build_add(c.b.build_shl(tn, c.i32(2)), c.i32(j)));
234
3/6
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 4 times.
8 kb.if_then(c.b.build_slt(oc, Cout), [&] {
235
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* bv = has_bias ? c.b.build_select(split0, kb.load_f32(ptr_at(c, bias, oc, 4)), c.f32(0.0f))
236 ✗ : c.f32(0.0f);
237 4 Value* oc_off = c.b.build_mul(c.b.build_zext_i64(oc), hwo64);
238
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 4 times.
12 for (int hm = 0; hm < 2; ++hm) {
239 8 Value* mb = c.b.build_add(m0, c.b.build_add(c.i32(hm * 64), c.b.build_shl(tm, c.i32(2))));
240 72 auto acc_v = [&](int i) { return c.b.build_add(accs[acc_at(hm * 4 + i, j)], bv); };
241
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
16 kb.if_then(c.b.build_slt(mb, M), [&] {
242 16 auto emit_scalar = [&] {
243
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 8 times.
40 for (int i = 0; i < 4; ++i) {
244 32 Value* m_i = c.b.build_add(mb, c.i32(i));
245
1/2
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
64 kb.if_then(c.b.build_slt(m_i, M), [&] {
246 32 Value* ni = c.b.build_udiv(m_i, HWo);
247 32 Value* pi = c.b.build_umod(m_i, HWo);
248 64 Value* y_idx = c.b.build_add(
249 32 c.b.build_add(c.b.build_mul(c.b.build_zext_i64(ni), cout_hwo), oc_off),
250 32 c.b.build_zext_i64(pi));
251 32 kb.store_f32(c.b.build_add(Y, c.b.build_shl(y_idx, kb.const_i64(2))), acc_v(i));
252 32 });
253 32 }
254 8 };
255
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (vec_store) {
256
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 emit_if_else_values(
257 8 c, c.b.build_slt(c.b.build_add(mb, c.i32(3)), M), {},
258 16 [&]() -> std::vector<Value*> {
259 8 Value* n_b = c.b.build_udiv(mb, HWo);
260 8 Value* p_b = c.b.build_umod(mb, HWo);
261 16 Value* y_idx = c.b.build_add(
262 8 c.b.build_add(c.b.build_mul(c.b.build_zext_i64(n_b), cout_hwo), oc_off),
263 8 c.b.build_zext_i64(p_b));
264 8 Value* v4 = make_f32x4(c, acc_v(0), acc_v(1), acc_v(2), acc_v(3));
265 8 kb.vstore_f32x4(c.b.build_add(Y, c.b.build_shl(y_idx, kb.const_i64(2))), v4);
266 8 return {};
267 },
268 16 [&]() -> std::vector<Value*> {
269 8 emit_scalar();
270 8 return {};
271 });
272 8 } else {
273 ✗ emit_scalar();
274 }
275 8 });
276 8 }
277 4 });
278 4 }
279 }
280
281
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 c.b.build_ret_void();
282
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 fn->rebuild_cfg_predecessors();
283 1 return fn;
284 1 }
285
286 } // namespace brotensor::kernels
287