brotensor/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
571 / 609
93.8%
Branches:
488 / 948
51.5%
Functions:
24 / 31
77.4%

src/cpu/cpu_jit.cpp
Line Branch Exec Source
1 #include "cpu_jit.h"
2 #include <brotensor/detail/cpu/thread_pool.h>
3 #include <algorithm>
4 #include <cmath>
5 #include <cstdint>
6
7 namespace brotensor::detail::cpu::jit {
8 namespace ref {
9
10 1 inline void rms_norm(const float* X, const float* gamma, float eps, float* Y, int B, int D) {
11
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 time.
1 if (B <= 0 || D <= 0) return;
12 1 const float inv_D = 1.0f / static_cast<float>(D);
13 5 auto row_fn = [&](int b) {
14 4 const float* xr = X + static_cast<size_t>(b) * D;
15 4 float* yr = Y + static_cast<size_t>(b) * D;
16 4 float sum = 0.0f;
17
2/2
✓ Branch 0 taken 148 times.
✓ Branch 1 taken 4 times.
152 for (int i = 0; i < D; ++i) sum += xr[i] * xr[i];
18 4 float rrms = 1.0f / std::sqrt(sum * inv_D + eps);
19
2/2
✓ Branch 0 taken 148 times.
✓ Branch 1 taken 4 times.
152 for (int i = 0; i < D; ++i) yr[i] = xr[i] * gamma[i] * rrms;
20 4 };
21
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 if (B > 1 && static_cast<int64_t>(B) * D >= 262144) {
22
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ detail::cpu::parallel_for(static_cast<size_t>(B), [&](size_t bi) {
23 ✗ row_fn(static_cast<int>(bi));
24 ✗ });
25 ✗ } else {
26
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1 time.
5 for (int b = 0; b < B; ++b) row_fn(b);
27 }
28 1 }
29
30 371 inline void swiglu(const float* X, float* Y, int B, int D) {
31
2/4
✓ Branch 0 taken 371 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 371 times.
371 if (B <= 0 || D <= 0) return;
32 3990 auto row_fn = [&](int b) {
33 3619 const float* gate = X + static_cast<size_t>(b) * 2 * D;
34 3619 const float* up = gate + D;
35 3619 float* out = Y + static_cast<size_t>(b) * D;
36
2/2
✓ Branch 0 taken 8814369 times.
✓ Branch 1 taken 3619 times.
8817988 for (int i = 0; i < D; ++i) {
37 8814369 float g = gate[i];
38 8814369 float silu = g / (1.0f + std::exp(-g));
39 8814369 out[i] = silu * up[i];
40 8814369 }
41 3619 };
42
4/4
✓ Branch 0 taken 310 times.
✓ Branch 1 taken 61 times.
✓ Branch 2 taken 248 times.
✓ Branch 3 taken 62 times.
371 if (B > 1 && static_cast<int64_t>(B) * D >= 16384) {
43
1/2
✓ Branch 0 taken 62 times.
✗ Branch 1 not taken.
2378 detail::cpu::parallel_for(static_cast<size_t>(B), [&](size_t bi) {
44 2316 row_fn(static_cast<int>(bi));
45 2316 });
46 62 } else {
47
2/2
✓ Branch 0 taken 1303 times.
✓ Branch 1 taken 309 times.
1612 for (int b = 0; b < B; ++b) row_fn(b);
48 }
49 371 }
50
51 370 inline void modulate(const float* X, const float* scale, const float* shift, float* Y, int L, int D) {
52
2/4
✓ Branch 0 taken 370 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 370 times.
370 if (L <= 0 || D <= 0) return;
53 3595 auto row_fn = [&](int l) {
54 3225 const float* xr = X + static_cast<size_t>(l) * D;
55 3225 float* yr = Y + static_cast<size_t>(l) * D;
56
2/2
✓ Branch 0 taken 8466059 times.
✓ Branch 1 taken 3225 times.
8469284 for (int d = 0; d < D; ++d) {
57 8466059 yr[d] = xr[d] * (1.0f + scale[d]) + shift[d];
58 8466059 }
59 3225 };
60
3/4
✓ Branch 0 taken 309 times.
✓ Branch 1 taken 61 times.
✓ Branch 2 taken 309 times.
✗ Branch 3 not taken.
370 if (L > 1 && static_cast<int64_t>(L) * D >= 262144) {
61
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ detail::cpu::parallel_for(static_cast<size_t>(L), [&](size_t li) {
62 ✗ row_fn(static_cast<int>(li));
63 ✗ });
64 ✗ } else {
65
2/2
✓ Branch 0 taken 3225 times.
✓ Branch 1 taken 370 times.
3595 for (int l = 0; l < L; ++l) row_fn(l);
66 }
67 370 }
68
69 370 inline void broadcast_mul(const float* X, const float* v, float* Y, int L, int D) {
70
2/4
✓ Branch 0 taken 370 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 370 times.
370 if (L <= 0 || D <= 0) return;
71 3601 auto row_fn = [&](int l) {
72 3231 const float* xr = X + static_cast<size_t>(l) * D;
73 3231 float* yr = Y + static_cast<size_t>(l) * D;
74
2/2
✓ Branch 0 taken 8466317 times.
✓ Branch 1 taken 3231 times.
8469548 for (int d = 0; d < D; ++d) {
75 8466317 yr[d] = xr[d] * v[d];
76 8466317 }
77 3231 };
78
3/4
✓ Branch 0 taken 310 times.
✓ Branch 1 taken 60 times.
✓ Branch 2 taken 310 times.
✗ Branch 3 not taken.
370 if (L > 1 && static_cast<int64_t>(L) * D >= 262144) {
79
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ detail::cpu::parallel_for(static_cast<size_t>(L), [&](size_t li) {
80 ✗ row_fn(static_cast<int>(li));
81 ✗ });
82 ✗ } else {
83
2/2
✓ Branch 0 taken 3231 times.
✓ Branch 1 taken 370 times.
3601 for (int l = 0; l < L; ++l) row_fn(l);
84 }
85 370 }
86
87 1 inline void layernorm_forward_inference_batched(const float* X, const float* gamma, const float* beta,
88 float eps, float* Y, int R, int D) {
89
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 time.
1 if (R <= 0 || D <= 0) return;
90 1 const float inv_D = 1.0f / static_cast<float>(D);
91 5 auto row_fn = [&](int r) {
92 4 const float* xr = X + static_cast<size_t>(r) * D;
93 4 float* yr = Y + static_cast<size_t>(r) * D;
94 4 float sum = 0.0f;
95
2/2
✓ Branch 0 taken 148 times.
✓ Branch 1 taken 4 times.
152 for (int i = 0; i < D; ++i) sum += xr[i];
96 4 float mean = sum * inv_D;
97 4 float sumsq = 0.0f;
98
2/2
✓ Branch 0 taken 148 times.
✓ Branch 1 taken 4 times.
152 for (int i = 0; i < D; ++i) {
99 148 float diff = xr[i] - mean;
100 148 sumsq += diff * diff;
101 148 }
102 4 float var = sumsq * inv_D;
103 4 float rstd = 1.0f / std::sqrt(var + eps);
104
2/2
✓ Branch 0 taken 148 times.
✓ Branch 1 taken 4 times.
152 for (int i = 0; i < D; ++i) {
105
1/2
✓ Branch 0 taken 148 times.
✗ Branch 1 not taken.
148 float g = gamma ? gamma[i] : 1.0f;
106
1/2
✓ Branch 0 taken 148 times.
✗ Branch 1 not taken.
148 float b = beta ? beta[i] : 0.0f;
107 148 yr[i] = g * ((xr[i] - mean) * rstd) + b;
108 148 }
109 4 };
110
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 if (R > 1 && static_cast<int64_t>(R) * D >= 262144) {
111
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ detail::cpu::parallel_for(static_cast<size_t>(R), [&](size_t ri) {
112 ✗ row_fn(static_cast<int>(ri));
113 ✗ });
114 ✗ } else {
115
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1 time.
5 for (int r = 0; r < R; ++r) row_fn(r);
116 }
117 1 }
118
119 } // namespace ref
120 } // namespace brotensor::detail::cpu::jit
121
122 #if BROTENSOR_HAS_BRASS_JIT
123
124 #include <brass/codegen/kernel_jit.hpp>
125 #include <brass/codegen/ml_fusion.hpp>
126 #include <brass/mir/builder.hpp>
127 #include <brass/mir/module.hpp>
128 #include <brass/mir/function.hpp>
129 #include "../jit/kernel_disk_cache.h"
130 #include <atomic>
131 #include <cmath>
132 #include <mutex>
133 #include <vector>
134 #include <memory>
135 #include <algorithm>
136 #include <iostream>
137
138 namespace brotensor::detail::cpu::jit {
139
140 namespace {
141
142 using namespace brass;
143 using namespace brass::codegen;
144
145 struct JitKernels {
146 bool available = false;
147
148 using AdaLNModulateFn = void (*)(const float* x, const float* scale, const float* shift, float* out, uint64_t n);
149 AdaLNModulateFn adaln_fn = nullptr;
150
151 using BroadcastMulFn = void (*)(const float* x, const float* v, float* out, uint64_t n);
152 BroadcastMulFn broadcast_mul_fn = nullptr;
153
154 using RmsNormRowFn = void (*)(const float* x, const float* gamma, float* y, uint64_t d, float eps, float inv_d);
155 RmsNormRowFn rms_norm_row_fn = nullptr;
156
157 using LayerNormRowFn = void (*)(const float* x, const float* gamma, const float* beta, float* y, uint64_t d, float eps, float inv_d);
158 LayerNormRowFn layernorm_row_fn = nullptr;
159 };
160
161 // Horizontal sum of an 8-wide float vector using destination scratch buffer
162 36 static Value* reduce_vsum8(KernelBuilder& kb, Value* scratch, Value* vsum) {
163 36 kb.vstore_f32x8(scratch, vsum);
164 36 Value* s0 = kb.load_f32(scratch, 0); Value* s1 = kb.load_f32(scratch, 4);
165 36 Value* s2 = kb.load_f32(scratch, 8); Value* s3 = kb.load_f32(scratch, 12);
166 36 Value* s4 = kb.load_f32(scratch, 16); Value* s5 = kb.load_f32(scratch, 20);
167 36 Value* s6 = kb.load_f32(scratch, 24); Value* s7 = kb.load_f32(scratch, 28);
168 36 Value* sum0123 = kb.add(kb.add(s0, s1), kb.add(s2, s3));
169 36 Value* sum4567 = kb.add(kb.add(s4, s5), kb.add(s6, s7));
170 36 return kb.add(sum0123, sum4567);
171 }
172
173 // ── 1. Vectorized Broadcast Mul (AVX2 8-wide + scalar remainder) ────────────
174 12 static void populate_broadcast_mul(Module& mod) {
175 60 Function* fn = mod.create_function("broadcast_mul", Type::void_type(), {
176 48 Type::ptr(), Type::ptr(), Type::ptr(), Type::i64()
177 });
178 12 KernelBuilder kb(mod, fn);
179
180
2/4
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
12 BasicBlock* entry = kb.builder().append_block("entry");
181
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* x = kb.builder().add_block_param(entry, Type::ptr());
182
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* v = kb.builder().add_block_param(entry, Type::ptr());
183
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* out = kb.builder().add_block_param(entry, Type::ptr());
184
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* n = kb.builder().add_block_param(entry, Type::i64());
185
186
2/4
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
12 BasicBlock* v_head = kb.builder().create_block("v_head");
187
2/4
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
12 BasicBlock* v_body = kb.builder().create_block("v_body");
188
2/4
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
12 BasicBlock* s_head = kb.builder().create_block("s_head");
189
2/4
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
12 BasicBlock* s_body = kb.builder().create_block("s_body");
190
2/4
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
12 BasicBlock* exit = kb.builder().create_block("exit");
191
192 12 kb.position_at_end(entry);
193
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* zero_i64 = kb.builder().build_iconst_i64(0);
194
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* one_i64 = kb.builder().build_iconst_i64(1);
195
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* two_i64 = kb.builder().build_iconst_i64(2);
196
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* eight_i64 = kb.builder().build_iconst_i64(8);
197
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* mask_eight = kb.builder().build_iconst_i64(~int64_t(7));
198
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* vec_n = kb.builder().build_and(n, mask_eight);
199
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 kb.builder().build_br(v_head, {zero_i64});
200
201 // Vector loop: 8 elements per iteration
202
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 fn->append_block(v_head);
203
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* iv_v = kb.builder().add_block_param(v_head, Type::i64());
204 12 kb.position_at_end(v_head);
205
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* v_cond = kb.builder().build_slt(iv_v, vec_n);
206
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 kb.builder().build_br_if(v_cond, v_body, {}, s_head, {iv_v});
207
208
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 fn->append_block(v_body);
209 12 kb.position_at_end(v_body);
210
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* byte_off = kb.builder().build_shl(iv_v, two_i64);
211
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* px = kb.builder().build_add(x, byte_off);
212
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* pv = kb.builder().build_add(v, byte_off);
213
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* py = kb.builder().build_add(out, byte_off);
214
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* vx = kb.vload_f32x8(px);
215
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* vv = kb.vload_f32x8(pv);
216
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* vy = kb.vmul(vx, vv);
217
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 kb.vstore_f32x8(py, vy);
218
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* next_iv_v = kb.builder().build_add(iv_v, eight_i64);
219
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 kb.builder().build_br(v_head, {next_iv_v});
220
221 // Scalar remainder loop
222
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 fn->append_block(s_head);
223
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* iv_s = kb.builder().add_block_param(s_head, Type::i64());
224 12 kb.position_at_end(s_head);
225
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* s_cond = kb.builder().build_slt(iv_s, n);
226
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 kb.builder().build_br_if(s_cond, s_body, exit);
227
228
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 fn->append_block(s_body);
229 12 kb.position_at_end(s_body);
230
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* xi = kb.load_f32_indexed(x, iv_s, 4, 0);
231
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* vi = kb.load_f32_indexed(v, iv_s, 4, 0);
232
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* yi = kb.mul(xi, vi);
233
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 kb.store_f32_indexed(out, iv_s, yi, 4, 0);
234
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* next_iv_s = kb.builder().build_add(iv_s, one_i64);
235
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 kb.builder().build_br(s_head, {next_iv_s});
236
237
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 fn->append_block(exit);
238 12 kb.position_at_end(exit);
239
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 kb.builder().build_ret_void();
240 12 }
241
242 // ── 2. Vectorized AdaLN Modulate (AVX2 8-wide + FMA + scalar remainder) ─────
243 13 static void populate_adaln_modulate(Module& mod) {
244 78 Function* fn = mod.create_function("fused_adaln_modulate", Type::void_type(), {
245 65 Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(), Type::i64()
246 });
247 13 KernelBuilder kb(mod, fn);
248
249
2/4
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 13 times.
✗ Branch 3 not taken.
13 BasicBlock* entry = kb.builder().append_block("entry");
250
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
13 Value* x = kb.builder().add_block_param(entry, Type::ptr());
251
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
13 Value* scale = kb.builder().add_block_param(entry, Type::ptr());
252
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
13 Value* shift = kb.builder().add_block_param(entry, Type::ptr());
253
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
13 Value* out = kb.builder().add_block_param(entry, Type::ptr());
254
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
13 Value* n = kb.builder().add_block_param(entry, Type::i64());
255
256
2/4
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 13 times.
✗ Branch 3 not taken.
13 BasicBlock* v_head = kb.builder().create_block("v_head");
257
2/4
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 13 times.
✗ Branch 3 not taken.
13 BasicBlock* v_body = kb.builder().create_block("v_body");
258
2/4
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 13 times.
✗ Branch 3 not taken.
13 BasicBlock* s_head = kb.builder().create_block("s_head");
259
2/4
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 13 times.
✗ Branch 3 not taken.
13 BasicBlock* s_body = kb.builder().create_block("s_body");
260
2/4
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 13 times.
✗ Branch 3 not taken.
13 BasicBlock* exit = kb.builder().create_block("exit");
261
262 13 kb.position_at_end(entry);
263
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
13 Value* zero_i64 = kb.builder().build_iconst_i64(0);
264
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
13 Value* one_i64 = kb.builder().build_iconst_i64(1);
265
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
13 Value* two_i64 = kb.builder().build_iconst_i64(2);
266
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
13 Value* eight_i64 = kb.builder().build_iconst_i64(8);
267
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
13 Value* one_f32 = kb.builder().build_fconst_f32(1.0f);
268
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
13 Value* v_one = kb.vbroadcast(Type::f32x8(), one_f32);
269
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
13 Value* mask_eight = kb.builder().build_iconst_i64(~int64_t(7));
270
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
13 Value* vec_n = kb.builder().build_and(n, mask_eight);
271
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
13 kb.builder().build_br(v_head, {zero_i64});
272
273 // Vector loop: modulated = x * (1.0f + scale) + shift via AVX2 FMA
274
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
13 fn->append_block(v_head);
275
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
13 Value* iv_v = kb.builder().add_block_param(v_head, Type::i64());
276 13 kb.position_at_end(v_head);
277
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
13 Value* v_cond = kb.builder().build_slt(iv_v, vec_n);
278
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
13 kb.builder().build_br_if(v_cond, v_body, {}, s_head, {iv_v});
279
280
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
13 fn->append_block(v_body);
281 13 kb.position_at_end(v_body);
282
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
13 Value* byte_off = kb.builder().build_shl(iv_v, two_i64);
283
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
13 Value* px = kb.builder().build_add(x, byte_off);
284
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
13 Value* pscale = kb.builder().build_add(scale, byte_off);
285
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
13 Value* pshift = kb.builder().build_add(shift, byte_off);
286
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
13 Value* py = kb.builder().build_add(out, byte_off);
287
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
13 Value* vx = kb.vload_f32x8(px);
288
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
13 Value* vscale = kb.vload_f32x8(pscale);
289
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
13 Value* vshift = kb.vload_f32x8(pshift);
290
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
13 Value* v_1_plus_scale = kb.vadd(v_one, vscale);
291
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
13 Value* vy = kb.vfma(vx, v_1_plus_scale, vshift);
292
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
13 kb.vstore_f32x8(py, vy);
293
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
13 Value* next_iv_v = kb.builder().build_add(iv_v, eight_i64);
294
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
13 kb.builder().build_br(v_head, {next_iv_v});
295
296 // Scalar remainder loop
297
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
13 fn->append_block(s_head);
298
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
13 Value* iv_s = kb.builder().add_block_param(s_head, Type::i64());
299 13 kb.position_at_end(s_head);
300
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
13 Value* s_cond = kb.builder().build_slt(iv_s, n);
301
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
13 kb.builder().build_br_if(s_cond, s_body, exit);
302
303
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
13 fn->append_block(s_body);
304 13 kb.position_at_end(s_body);
305
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
13 Value* xi = kb.load_f32_indexed(x, iv_s, 4, 0);
306
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
13 Value* scale_i = kb.load_f32_indexed(scale, iv_s, 4, 0);
307
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
13 Value* shift_i = kb.load_f32_indexed(shift, iv_s, 4, 0);
308
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
13 Value* one_plus_scale = kb.add(one_f32, scale_i);
309
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
13 Value* yi = kb.builder().build_fma_f32(xi, one_plus_scale, shift_i);
310
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
13 kb.store_f32_indexed(out, iv_s, yi, 4, 0);
311
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
13 Value* next_iv_s = kb.builder().build_add(iv_s, one_i64);
312
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
13 kb.builder().build_br(s_head, {next_iv_s});
313
314
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
13 fn->append_block(exit);
315 13 kb.position_at_end(exit);
316
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
13 kb.builder().build_ret_void();
317 13 }
318
319 // ── 3. Vectorized RMSNorm Row (AVX2 FMA reduction + AVX2 affine scale) ─────
320 12 static void populate_rms_norm_row(Module& mod) {
321 84 Function* fn = mod.create_function("rms_norm_row", Type::void_type(), {
322 72 Type::ptr(), Type::ptr(), Type::ptr(), Type::i64(), Type::f32(), Type::f32()
323 });
324 12 KernelBuilder kb(mod, fn);
325
326
2/4
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
12 BasicBlock* entry = kb.builder().append_block("entry");
327
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* x = kb.builder().add_block_param(entry, Type::ptr());
328
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* gamma = kb.builder().add_block_param(entry, Type::ptr());
329
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* y = kb.builder().add_block_param(entry, Type::ptr());
330
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* d = kb.builder().add_block_param(entry, Type::i64());
331
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* eps = kb.builder().add_block_param(entry, Type::f32());
332
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* inv_d = kb.builder().add_block_param(entry, Type::f32());
333
334
2/4
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
12 BasicBlock* l1_vhead = kb.builder().create_block("l1_vhead");
335
2/4
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
12 BasicBlock* l1_vbody = kb.builder().create_block("l1_vbody");
336
2/4
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
12 BasicBlock* l1_vexit = kb.builder().create_block("l1_vexit");
337
2/4
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
12 BasicBlock* l1_shead = kb.builder().create_block("l1_shead");
338
2/4
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
12 BasicBlock* l1_sbody = kb.builder().create_block("l1_sbody");
339
2/4
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
12 BasicBlock* l1_exit = kb.builder().create_block("l1_exit");
340
341
2/4
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
12 BasicBlock* l2_vhead = kb.builder().create_block("l2_vhead");
342
2/4
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
12 BasicBlock* l2_vbody = kb.builder().create_block("l2_vbody");
343
2/4
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
12 BasicBlock* l2_shead = kb.builder().create_block("l2_shead");
344
2/4
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
12 BasicBlock* l2_sbody = kb.builder().create_block("l2_sbody");
345
2/4
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
12 BasicBlock* l2_exit = kb.builder().create_block("l2_exit");
346
347 12 kb.position_at_end(entry);
348
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* zero_i64 = kb.builder().build_iconst_i64(0);
349
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* one_i64 = kb.builder().build_iconst_i64(1);
350
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* two_i64 = kb.builder().build_iconst_i64(2);
351
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* eight_i64 = kb.builder().build_iconst_i64(8);
352
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* zero_f32 = kb.builder().build_fconst_f32(0.0f);
353
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* vzero = kb.vzero(Type::f32x8());
354
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* mask_eight = kb.builder().build_iconst_i64(~int64_t(7));
355
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* vec_d = kb.builder().build_and(d, mask_eight);
356
357
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* has_vec = kb.builder().build_sge(d, eight_i64);
358
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 kb.builder().build_br_if(has_vec, l1_vhead, {zero_i64, vzero}, l1_shead, {zero_i64, zero_f32});
359
360 // Pass 1: Vector loop accumulating sum_sq via vfma
361
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 fn->append_block(l1_vhead);
362
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* iv1_v = kb.builder().add_block_param(l1_vhead, Type::i64());
363
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* vsum = kb.builder().add_block_param(l1_vhead, Type::f32x8());
364 12 kb.position_at_end(l1_vhead);
365
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* cond1_v = kb.builder().build_slt(iv1_v, vec_d);
366
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 kb.builder().build_br_if(cond1_v, l1_vbody, {}, l1_vexit, {iv1_v, vsum});
367
368
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 fn->append_block(l1_vbody);
369 12 kb.position_at_end(l1_vbody);
370
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* byte_off1 = kb.builder().build_shl(iv1_v, two_i64);
371
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* px1 = kb.builder().build_add(x, byte_off1);
372
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* vx1 = kb.vload_f32x8(px1);
373
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* next_vsum = kb.vfma(vx1, vx1, vsum);
374
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* next_iv1_v = kb.builder().build_add(iv1_v, eight_i64);
375
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 kb.builder().build_br(l1_vhead, {next_iv1_v, next_vsum});
376
377 // Pass 1: Vector exit -> horizontal vector reduction via y scratch buffer
378
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 fn->append_block(l1_vexit);
379
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* exit_iv1 = kb.builder().add_block_param(l1_vexit, Type::i64());
380
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* exit_vsum = kb.builder().add_block_param(l1_vexit, Type::f32x8());
381 12 kb.position_at_end(l1_vexit);
382
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* init_scalar_sum = reduce_vsum8(kb, y, exit_vsum);
383
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 kb.builder().build_br(l1_shead, {exit_iv1, init_scalar_sum});
384
385 // Pass 1: Scalar remainder loop
386
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 fn->append_block(l1_shead);
387
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* iv1_s = kb.builder().add_block_param(l1_shead, Type::i64());
388
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* sum_sq = kb.builder().add_block_param(l1_shead, Type::f32());
389 12 kb.position_at_end(l1_shead);
390
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* cond1_s = kb.builder().build_slt(iv1_s, d);
391
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 kb.builder().build_br_if(cond1_s, l1_sbody, {}, l1_exit, {sum_sq});
392
393
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 fn->append_block(l1_sbody);
394 12 kb.position_at_end(l1_sbody);
395
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* xi = kb.load_f32_indexed(x, iv1_s, 4, 0);
396
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* xi_sq = kb.mul(xi, xi);
397
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* next_sum = kb.add(sum_sq, xi_sq);
398
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* next_iv1_s = kb.builder().build_add(iv1_s, one_i64);
399
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 kb.builder().build_br(l1_shead, {next_iv1_s, next_sum});
400
401 // Pass 1 exit -> compute rrms = 1.0f / sqrt(mean_sq + eps)
402
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 fn->append_block(l1_exit);
403
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* final_sum = kb.builder().add_block_param(l1_exit, Type::f32());
404 12 kb.position_at_end(l1_exit);
405
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* mean_sq = kb.mul(final_sum, inv_d);
406
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* denom = kb.add(mean_sq, eps);
407
3/6
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 12 times.
✗ Branch 5 not taken.
12 Value* rrms = kb.div(kb.builder().build_fconst_f32(1.0f), kb.builder().build_sqrt_f32(denom));
408
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* vrrms = kb.vbroadcast(Type::f32x8(), rrms);
409
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 kb.builder().build_br(l2_vhead, {zero_i64, rrms, vrrms});
410
411 // Pass 2: Vector loop y[i] = x[i] * gamma[i] * rrms
412
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 fn->append_block(l2_vhead);
413
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* iv2_v = kb.builder().add_block_param(l2_vhead, Type::i64());
414
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* rrms_val = kb.builder().add_block_param(l2_vhead, Type::f32());
415
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* vrrms_val = kb.builder().add_block_param(l2_vhead, Type::f32x8());
416 12 kb.position_at_end(l2_vhead);
417
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* cond2_v = kb.builder().build_slt(iv2_v, vec_d);
418
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 kb.builder().build_br_if(cond2_v, l2_vbody, {}, l2_shead, {iv2_v, rrms_val});
419
420
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 fn->append_block(l2_vbody);
421 12 kb.position_at_end(l2_vbody);
422
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* byte_off2 = kb.builder().build_shl(iv2_v, two_i64);
423
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* px2 = kb.builder().build_add(x, byte_off2);
424
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* pg2 = kb.builder().build_add(gamma, byte_off2);
425
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* py2 = kb.builder().build_add(y, byte_off2);
426
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* vx2 = kb.vload_f32x8(px2);
427
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* vg2 = kb.vload_f32x8(pg2);
428
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* vxg = kb.vmul(vx2, vg2);
429
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* vy = kb.vmul(vxg, vrrms_val);
430
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 kb.vstore_f32x8(py2, vy);
431
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* next_iv2_v = kb.builder().build_add(iv2_v, eight_i64);
432
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 kb.builder().build_br(l2_vhead, {next_iv2_v, rrms_val, vrrms_val});
433
434 // Pass 2: Scalar remainder loop
435
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 fn->append_block(l2_shead);
436
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* iv2_s = kb.builder().add_block_param(l2_shead, Type::i64());
437
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* rrms_scalar = kb.builder().add_block_param(l2_shead, Type::f32());
438 12 kb.position_at_end(l2_shead);
439
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* cond2_s = kb.builder().build_slt(iv2_s, d);
440
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 kb.builder().build_br_if(cond2_s, l2_sbody, l2_exit);
441
442
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 fn->append_block(l2_sbody);
443 12 kb.position_at_end(l2_sbody);
444
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* x2 = kb.load_f32_indexed(x, iv2_s, 4, 0);
445
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* g2 = kb.load_f32_indexed(gamma, iv2_s, 4, 0);
446
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* xg2 = kb.mul(x2, g2);
447
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* out_val = kb.mul(xg2, rrms_scalar);
448
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 kb.store_f32_indexed(y, iv2_s, out_val, 4, 0);
449
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* next_iv2_s = kb.builder().build_add(iv2_s, one_i64);
450
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 kb.builder().build_br(l2_shead, {next_iv2_s, rrms_scalar});
451
452
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 fn->append_block(l2_exit);
453 12 kb.position_at_end(l2_exit);
454
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 kb.builder().build_ret_void();
455 12 }
456
457 // ── 4. Vectorized LayerNorm Row (AVX2 mean + var + affine) ──────────────────
458 12 static void populate_layernorm_row(Module& mod) {
459 96 Function* fn = mod.create_function("layernorm_row", Type::void_type(), {
460 84 Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(), Type::i64(), Type::f32(), Type::f32()
461 });
462 12 KernelBuilder kb(mod, fn);
463
464
2/4
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
12 BasicBlock* entry = kb.builder().append_block("entry");
465
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* x = kb.builder().add_block_param(entry, Type::ptr());
466
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* gamma = kb.builder().add_block_param(entry, Type::ptr());
467
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* beta = kb.builder().add_block_param(entry, Type::ptr());
468
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* y = kb.builder().add_block_param(entry, Type::ptr());
469
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* d = kb.builder().add_block_param(entry, Type::i64());
470
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* eps = kb.builder().add_block_param(entry, Type::f32());
471
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* inv_d = kb.builder().add_block_param(entry, Type::f32());
472
473 // Pass 1 blocks (mean)
474
2/4
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
12 BasicBlock* l1_vhead = kb.builder().create_block("ln_l1_vhead");
475
2/4
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
12 BasicBlock* l1_vbody = kb.builder().create_block("ln_l1_vbody");
476
2/4
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
12 BasicBlock* l1_vexit = kb.builder().create_block("ln_l1_vexit");
477
2/4
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
12 BasicBlock* l1_shead = kb.builder().create_block("ln_l1_shead");
478
2/4
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
12 BasicBlock* l1_sbody = kb.builder().create_block("ln_l1_sbody");
479
2/4
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
12 BasicBlock* l1_exit = kb.builder().create_block("ln_l1_exit");
480
481 // Pass 2 blocks (variance)
482
2/4
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
12 BasicBlock* l2_vhead = kb.builder().create_block("ln_l2_vhead");
483
2/4
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
12 BasicBlock* l2_vbody = kb.builder().create_block("ln_l2_vbody");
484
2/4
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
12 BasicBlock* l2_vexit = kb.builder().create_block("ln_l2_vexit");
485
2/4
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
12 BasicBlock* l2_shead = kb.builder().create_block("ln_l2_shead");
486
2/4
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
12 BasicBlock* l2_sbody = kb.builder().create_block("ln_l2_sbody");
487
2/4
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
12 BasicBlock* l2_exit = kb.builder().create_block("ln_l2_exit");
488
489 // Pass 3 blocks (affine)
490
2/4
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
12 BasicBlock* l3_vhead = kb.builder().create_block("ln_l3_vhead");
491
2/4
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
12 BasicBlock* l3_vbody = kb.builder().create_block("ln_l3_vbody");
492
2/4
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
12 BasicBlock* l3_shead = kb.builder().create_block("ln_l3_shead");
493
2/4
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
12 BasicBlock* l3_sbody = kb.builder().create_block("ln_l3_sbody");
494
2/4
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
12 BasicBlock* l3_exit = kb.builder().create_block("ln_l3_exit");
495
496 12 kb.position_at_end(entry);
497
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* zero_i64 = kb.builder().build_iconst_i64(0);
498
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* one_i64 = kb.builder().build_iconst_i64(1);
499
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* two_i64 = kb.builder().build_iconst_i64(2);
500
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* eight_i64 = kb.builder().build_iconst_i64(8);
501
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* zero_f32 = kb.builder().build_fconst_f32(0.0f);
502
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* vzero = kb.vzero(Type::f32x8());
503
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* mask_eight = kb.builder().build_iconst_i64(~int64_t(7));
504
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* vec_d = kb.builder().build_and(d, mask_eight);
505
506
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* has_vec = kb.builder().build_sge(d, eight_i64);
507
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 kb.builder().build_br_if(has_vec, l1_vhead, {zero_i64, vzero}, l1_shead, {zero_i64, zero_f32});
508
509 // Pass 1: Vector sum
510
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 fn->append_block(l1_vhead);
511
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* iv1_v = kb.builder().add_block_param(l1_vhead, Type::i64());
512
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* vsum1 = kb.builder().add_block_param(l1_vhead, Type::f32x8());
513 12 kb.position_at_end(l1_vhead);
514
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* cond1_v = kb.builder().build_slt(iv1_v, vec_d);
515
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 kb.builder().build_br_if(cond1_v, l1_vbody, {}, l1_vexit, {iv1_v, vsum1});
516
517
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 fn->append_block(l1_vbody);
518 12 kb.position_at_end(l1_vbody);
519
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* byte_off1 = kb.builder().build_shl(iv1_v, two_i64);
520
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* px1 = kb.builder().build_add(x, byte_off1);
521
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* vx1 = kb.vload_f32x8(px1);
522
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* next_vsum1 = kb.vadd(vsum1, vx1);
523
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* next_iv1_v = kb.builder().build_add(iv1_v, eight_i64);
524
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 kb.builder().build_br(l1_vhead, {next_iv1_v, next_vsum1});
525
526 // Pass 1: Vector exit -> horizontal sum
527
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 fn->append_block(l1_vexit);
528
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* exit_iv1 = kb.builder().add_block_param(l1_vexit, Type::i64());
529
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* exit_vsum1 = kb.builder().add_block_param(l1_vexit, Type::f32x8());
530 12 kb.position_at_end(l1_vexit);
531
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* init_sum1 = reduce_vsum8(kb, y, exit_vsum1);
532
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 kb.builder().build_br(l1_shead, {exit_iv1, init_sum1});
533
534 // Pass 1: Scalar remainder
535
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 fn->append_block(l1_shead);
536
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* iv1_s = kb.builder().add_block_param(l1_shead, Type::i64());
537
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* sum1 = kb.builder().add_block_param(l1_shead, Type::f32());
538 12 kb.position_at_end(l1_shead);
539
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* cond1_s = kb.builder().build_slt(iv1_s, d);
540
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 kb.builder().build_br_if(cond1_s, l1_sbody, {}, l1_exit, {sum1});
541
542
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 fn->append_block(l1_sbody);
543 12 kb.position_at_end(l1_sbody);
544
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* xi1 = kb.load_f32_indexed(x, iv1_s, 4, 0);
545
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* next_sum1 = kb.add(sum1, xi1);
546
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* next_iv1_s = kb.builder().build_add(iv1_s, one_i64);
547
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 kb.builder().build_br(l1_shead, {next_iv1_s, next_sum1});
548
549 // Pass 1: Mean computed -> enter Pass 2
550
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 fn->append_block(l1_exit);
551
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* final_sum1 = kb.builder().add_block_param(l1_exit, Type::f32());
552 12 kb.position_at_end(l1_exit);
553
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* mean = kb.mul(final_sum1, inv_d);
554
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* vmean = kb.vbroadcast(Type::f32x8(), mean);
555
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 kb.builder().build_br_if(has_vec, l2_vhead, {zero_i64, vzero, mean, vmean}, l2_shead, {zero_i64, zero_f32, mean});
556
557 // Pass 2: Vector sum of squared deviations
558
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 fn->append_block(l2_vhead);
559
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* iv2_v = kb.builder().add_block_param(l2_vhead, Type::i64());
560
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* vsum2 = kb.builder().add_block_param(l2_vhead, Type::f32x8());
561
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* m2_scalar = kb.builder().add_block_param(l2_vhead, Type::f32());
562
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* vm2 = kb.builder().add_block_param(l2_vhead, Type::f32x8());
563 12 kb.position_at_end(l2_vhead);
564
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* cond2_v = kb.builder().build_slt(iv2_v, vec_d);
565
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 kb.builder().build_br_if(cond2_v, l2_vbody, {}, l2_vexit, {iv2_v, vsum2, m2_scalar});
566
567
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 fn->append_block(l2_vbody);
568 12 kb.position_at_end(l2_vbody);
569
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* byte_off2 = kb.builder().build_shl(iv2_v, two_i64);
570
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* px2 = kb.builder().build_add(x, byte_off2);
571
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* vx2 = kb.vload_f32x8(px2);
572
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* vdev = kb.vsub(vx2, vm2);
573
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* next_vsum2 = kb.vfma(vdev, vdev, vsum2);
574
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* next_iv2_v = kb.builder().build_add(iv2_v, eight_i64);
575
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 kb.builder().build_br(l2_vhead, {next_iv2_v, next_vsum2, m2_scalar, vm2});
576
577 // Pass 2: Vector exit -> horizontal sum
578
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 fn->append_block(l2_vexit);
579
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* exit_iv2 = kb.builder().add_block_param(l2_vexit, Type::i64());
580
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* exit_vsum2 = kb.builder().add_block_param(l2_vexit, Type::f32x8());
581
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* m2_out = kb.builder().add_block_param(l2_vexit, Type::f32());
582 12 kb.position_at_end(l2_vexit);
583
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* init_sumsq = reduce_vsum8(kb, y, exit_vsum2);
584
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 kb.builder().build_br(l2_shead, {exit_iv2, init_sumsq, m2_out});
585
586 // Pass 2: Scalar remainder
587
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 fn->append_block(l2_shead);
588
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* iv2_s = kb.builder().add_block_param(l2_shead, Type::i64());
589
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* sumsq = kb.builder().add_block_param(l2_shead, Type::f32());
590
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* m2_s = kb.builder().add_block_param(l2_shead, Type::f32());
591 12 kb.position_at_end(l2_shead);
592
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* cond2_s = kb.builder().build_slt(iv2_s, d);
593
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 kb.builder().build_br_if(cond2_s, l2_sbody, {}, l2_exit, {sumsq, m2_s});
594
595
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 fn->append_block(l2_sbody);
596 12 kb.position_at_end(l2_sbody);
597
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* xi2 = kb.load_f32_indexed(x, iv2_s, 4, 0);
598
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* diff2 = kb.sub(xi2, m2_s);
599
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* diff_sq2 = kb.mul(diff2, diff2);
600
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* next_sumsq = kb.add(sumsq, diff_sq2);
601
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* next_iv2_s = kb.builder().build_add(iv2_s, one_i64);
602
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 kb.builder().build_br(l2_shead, {next_iv2_s, next_sumsq, m2_s});
603
604 // Pass 2 exit -> compute rstd
605
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 fn->append_block(l2_exit);
606
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* final_sumsq = kb.builder().add_block_param(l2_exit, Type::f32());
607
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* final_mean = kb.builder().add_block_param(l2_exit, Type::f32());
608 12 kb.position_at_end(l2_exit);
609
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* var = kb.mul(final_sumsq, inv_d);
610
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* var_eps = kb.add(var, eps);
611
3/6
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 12 times.
✗ Branch 5 not taken.
12 Value* rstd = kb.div(kb.builder().build_fconst_f32(1.0f), kb.builder().build_sqrt_f32(var_eps));
612
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* vmean3 = kb.vbroadcast(Type::f32x8(), final_mean);
613
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* vrstd3 = kb.vbroadcast(Type::f32x8(), rstd);
614
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 kb.builder().build_br(l3_vhead, {zero_i64, final_mean, rstd, vmean3, vrstd3});
615
616 // Pass 3: Vector affine y[i] = gamma[i] * ((x[i] - mean) * rstd) + beta[i]
617
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 fn->append_block(l3_vhead);
618
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* iv3_v = kb.builder().add_block_param(l3_vhead, Type::i64());
619
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* m3_s = kb.builder().add_block_param(l3_vhead, Type::f32());
620
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* r3_s = kb.builder().add_block_param(l3_vhead, Type::f32());
621
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* vm3 = kb.builder().add_block_param(l3_vhead, Type::f32x8());
622
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* vr3 = kb.builder().add_block_param(l3_vhead, Type::f32x8());
623 12 kb.position_at_end(l3_vhead);
624
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* cond3_v = kb.builder().build_slt(iv3_v, vec_d);
625
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 kb.builder().build_br_if(cond3_v, l3_vbody, {}, l3_shead, {iv3_v, m3_s, r3_s});
626
627
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 fn->append_block(l3_vbody);
628 12 kb.position_at_end(l3_vbody);
629
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* byte_off3 = kb.builder().build_shl(iv3_v, two_i64);
630
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* px3 = kb.builder().build_add(x, byte_off3);
631
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* pg3 = kb.builder().build_add(gamma, byte_off3);
632
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* pb3 = kb.builder().build_add(beta, byte_off3);
633
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* py3 = kb.builder().build_add(y, byte_off3);
634
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* vx3 = kb.vload_f32x8(px3);
635
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* vg3 = kb.vload_f32x8(pg3);
636
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* vb3 = kb.vload_f32x8(pb3);
637
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* vdiff3 = kb.vsub(vx3, vm3);
638
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* vxhat3 = kb.vmul(vdiff3, vr3);
639
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* vy3 = kb.vfma(vg3, vxhat3, vb3);
640
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 kb.vstore_f32x8(py3, vy3);
641
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* next_iv3_v = kb.builder().build_add(iv3_v, eight_i64);
642
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 kb.builder().build_br(l3_vhead, {next_iv3_v, m3_s, r3_s, vm3, vr3});
643
644 // Pass 3: Scalar remainder
645
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 fn->append_block(l3_shead);
646
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* iv3_s = kb.builder().add_block_param(l3_shead, Type::i64());
647
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* m3_sc = kb.builder().add_block_param(l3_shead, Type::f32());
648
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* r3_sc = kb.builder().add_block_param(l3_shead, Type::f32());
649 12 kb.position_at_end(l3_shead);
650
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* cond3_s = kb.builder().build_slt(iv3_s, d);
651
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 kb.builder().build_br_if(cond3_s, l3_sbody, l3_exit);
652
653
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 fn->append_block(l3_sbody);
654 12 kb.position_at_end(l3_sbody);
655
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* x3 = kb.load_f32_indexed(x, iv3_s, 4, 0);
656
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* g3 = kb.load_f32_indexed(gamma, iv3_s, 4, 0);
657
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* b3 = kb.load_f32_indexed(beta, iv3_s, 4, 0);
658
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* diff3 = kb.sub(x3, m3_sc);
659
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* xhat3 = kb.mul(diff3, r3_sc);
660
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* y3 = kb.builder().build_fma_f32(g3, xhat3, b3);
661
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 kb.store_f32_indexed(y, iv3_s, y3, 4, 0);
662
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 Value* next_iv3_s = kb.builder().build_add(iv3_s, one_i64);
663
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 kb.builder().build_br(l3_shead, {next_iv3_s, m3_sc, r3_sc});
664
665
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 fn->append_block(l3_exit);
666 12 kb.position_at_end(l3_exit);
667
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 kb.builder().build_ret_void();
668 12 }
669
670 // ── Kernel Initialization & AOT Disk Cache Management ───────────────────────
671 static std::mutex s_core_jit_mu;
672 static std::atomic<bool> s_core_jit_ready{false};
673 static JitKernels s_core_kernels;
674
675 3690 static const JitKernels& get_kernels() {
676
2/2
✓ Branch 0 taken 3675 times.
✓ Branch 1 taken 15 times.
3690 if (s_core_jit_ready.load(std::memory_order_acquire)) {
677 3675 return s_core_kernels;
678 }
679 15 std::lock_guard<std::mutex> lk(s_core_jit_mu);
680
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
15 if (!s_core_jit_ready.load(std::memory_order_relaxed)) {
681 try {
682
1/2
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
15 auto& cache = brotensor::jit::KernelDiskCache::instance();
683 15 brotensor::jit::KernelDiskCache::CpuCompileOptions opts;
684
685
1/2
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
30 s_core_kernels.adaln_fn = cache.compile_or_load_cpu_fn<JitKernels::AdaLNModulateFn>(
686
2/4
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 15 times.
✗ Branch 3 not taken.
15 "cpu_core_adaln_modulate_v1", "fused_adaln_modulate", populate_adaln_modulate, opts);
687
1/2
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
30 s_core_kernels.broadcast_mul_fn = cache.compile_or_load_cpu_fn<JitKernels::BroadcastMulFn>(
688
2/4
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 15 times.
✗ Branch 3 not taken.
15 "cpu_core_broadcast_mul_v1", "broadcast_mul", populate_broadcast_mul, opts);
689
1/2
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
30 s_core_kernels.rms_norm_row_fn = cache.compile_or_load_cpu_fn<JitKernels::RmsNormRowFn>(
690
2/4
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 15 times.
✗ Branch 3 not taken.
15 "cpu_core_rms_norm_row_v1", "rms_norm_row", populate_rms_norm_row, opts);
691
1/2
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
30 s_core_kernels.layernorm_row_fn = cache.compile_or_load_cpu_fn<JitKernels::LayerNormRowFn>(
692
2/4
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 15 times.
✗ Branch 3 not taken.
15 "cpu_core_layernorm_row_v1", "layernorm_row", populate_layernorm_row, opts);
693
694
1/2
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
30 s_core_kernels.available = (s_core_kernels.adaln_fn != nullptr &&
695
1/2
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
15 s_core_kernels.broadcast_mul_fn != nullptr &&
696
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
15 s_core_kernels.rms_norm_row_fn != nullptr &&
697 15 s_core_kernels.layernorm_row_fn != nullptr);
698
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
15 } catch (const std::exception& e) {
699
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.
✗ std::cerr << "brotensor: Brass JIT initialization failed: " << e.what() << "\n";
700 ✗ s_core_kernels.available = false;
701 ✗ }
702 15 s_core_jit_ready.store(true, std::memory_order_release);
703 15 }
704 15 return s_core_kernels;
705 3690 }
706
707 } // namespace
708
709 1865 bool is_jit_available() {
710 1865 return get_kernels().available;
711 }
712
713 ✗ void prewarm_cpu_jit_kernels() {
714 ✗ (void)get_kernels();
715 ✗ prewarm_fused_jit_kernels();
716 ✗ prewarm_quant_jit_kernels();
717 ✗ }
718
719 6 void reset_cpu_jit_caches_for_testing() {
720 {
721 6 std::lock_guard<std::mutex> lk(s_core_jit_mu);
722 6 s_core_jit_ready.store(false, std::memory_order_relaxed);
723 6 s_core_kernels = JitKernels{};
724 6 }
725 6 reset_fused_jit_caches_for_testing();
726 6 reset_quant_jit_caches_for_testing();
727 6 brotensor::jit::KernelDiskCache::instance().clear_loaded_libraries();
728 6 }
729
730 381 void rms_norm_forward(const float* X, const float* gamma, float eps, float* Y, int B, int D) {
731 381 const auto& k = get_kernels();
732
7/12
✓ Branch 0 taken 381 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 381 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 380 times.
✓ Branch 5 taken 1 time.
✓ Branch 6 taken 380 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 380 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✓ Branch 11 taken 380 times.
381 if (!k.available || k.rms_norm_row_fn == nullptr || X == Y || gamma == nullptr || B <= 0 || D <= 0) {
733 1 ref::rms_norm(X, gamma, eps, Y, B, D);
734 1 return;
735 }
736
737 380 const float inv_D = 1.0f / static_cast<float>(D);
738
739 // Single-thread vectorized loop executes in sub-microsecond latency.
740 // Multi-threaded pool dispatch only when workload amortizes thread wake-up overhead.
741
3/4
✓ Branch 0 taken 318 times.
✓ Branch 1 taken 62 times.
✓ Branch 2 taken 318 times.
✗ Branch 3 not taken.
380 if (B > 1 && static_cast<int64_t>(B) * D >= 262144) {
742
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ detail::cpu::parallel_for(static_cast<std::size_t>(B), [&](std::size_t bi) {
743 ✗ const int b = static_cast<int>(bi);
744 ✗ const float* xr = X + static_cast<std::size_t>(b) * D;
745 ✗ float* yr = Y + static_cast<std::size_t>(b) * D;
746 ✗ k.rms_norm_row_fn(xr, gamma, yr, static_cast<uint64_t>(D), eps, inv_D);
747 ✗ });
748 ✗ } else {
749
2/2
✓ Branch 0 taken 3269 times.
✓ Branch 1 taken 380 times.
3649 for (int b = 0; b < B; ++b) {
750 3269 const float* xr = X + static_cast<std::size_t>(b) * D;
751 3269 float* yr = Y + static_cast<std::size_t>(b) * D;
752 3269 k.rms_norm_row_fn(xr, gamma, yr, static_cast<uint64_t>(D), eps, inv_D);
753 3269 }
754 }
755 381 }
756
757 // SwiGLU has no JIT kernel. brass's MlFusionCompiler::compile_swiglu evaluates
758 // exp(-g) as a degree-5 Taylor polynomial of -g/16 raised to the 16th power;
759 // the polynomial turns negative once -g/16 passes about -3.5, so every gate
760 // above ~55 came out with silu(g) ~ 0 instead of g, and ViT-H SwiGLU FFNs
761 // (DINOv3) put gates well past that. A JIT kernel calling the exact expf per
762 // element measured ~4x slower than this loop (MSVC vectorizes std::exp), so
763 // the row-parallel reference is the kernel.
764 371 void swiglu_forward(const float* X, float* Y, int B, int D) {
765 371 (void)get_kernels();
766 371 ref::swiglu(X, Y, B, D);
767 371 }
768
769 // modulate and broadcast_mul are one load-op-store stream per element, which
770 // the compiler already vectorizes: on AArch64 clang's NEON loop is unrolled
771 // and runs these about 2x faster than the JIT's f32x8 (a q-register pair per
772 // iteration, not unrolled), so there the row-parallel reference is the kernel.
773 // The norms keep the JIT everywhere: their reductions are where it wins.
774 #if defined(__aarch64__) || defined(_M_ARM64)
775 constexpr bool kJitStreamingKernels = false;
776 #else
777 constexpr bool kJitStreamingKernels = true;
778 #endif
779
780 370 void modulate(const float* X, const float* scale, const float* shift, float* Y, int L, int D) {
781 370 const auto& k = get_kernels();
782 if (!kJitStreamingKernels || !k.available || k.adaln_fn == nullptr) {
783 370 ref::modulate(X, scale, shift, Y, L, D);
784 370 return;
785 }
786
787 if (L > 1 && static_cast<int64_t>(L) * D >= 262144) {
788 detail::cpu::parallel_for(static_cast<std::size_t>(L), [&](std::size_t li) {
789 const int l = static_cast<int>(li);
790 const float* xr = X + static_cast<std::size_t>(l) * D;
791 float* yr = Y + static_cast<std::size_t>(l) * D;
792 k.adaln_fn(xr, scale, shift, yr, static_cast<uint64_t>(D));
793 });
794 } else {
795 for (int l = 0; l < L; ++l) {
796 const float* xr = X + static_cast<std::size_t>(l) * D;
797 float* yr = Y + static_cast<std::size_t>(l) * D;
798 k.adaln_fn(xr, scale, shift, yr, static_cast<uint64_t>(D));
799 }
800 }
801 }
802
803 370 void broadcast_mul(const float* X, const float* v, float* Y, int L, int D) {
804 370 const auto& k = get_kernels();
805 if (!kJitStreamingKernels || !k.available || k.broadcast_mul_fn == nullptr) {
806 370 ref::broadcast_mul(X, v, Y, L, D);
807 370 return;
808 }
809
810 if (L > 1 && static_cast<int64_t>(L) * D >= 262144) {
811 detail::cpu::parallel_for(static_cast<std::size_t>(L), [&](std::size_t li) {
812 const int l = static_cast<int>(li);
813 const float* xr = X + static_cast<std::size_t>(l) * D;
814 float* yr = Y + static_cast<std::size_t>(l) * D;
815 k.broadcast_mul_fn(xr, v, yr, static_cast<uint64_t>(D));
816 });
817 } else {
818 for (int l = 0; l < L; ++l) {
819 const float* xr = X + static_cast<std::size_t>(l) * D;
820 float* yr = Y + static_cast<std::size_t>(l) * D;
821 k.broadcast_mul_fn(xr, v, yr, static_cast<uint64_t>(D));
822 }
823 }
824 }
825
826 333 void layernorm_forward_inference_batched(const float* X, const float* gamma, const float* beta, float eps, float* Y, int R, int D) {
827 333 const auto& k = get_kernels();
828
8/14
✓ Branch 0 taken 333 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 333 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 332 times.
✓ Branch 5 taken 1 time.
✓ Branch 6 taken 332 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 332 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 332 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✓ Branch 13 taken 332 times.
333 if (!k.available || k.layernorm_row_fn == nullptr || X == Y || gamma == nullptr || beta == nullptr || R <= 0 || D <= 0) {
829 1 ref::layernorm_forward_inference_batched(X, gamma, beta, eps, Y, R, D);
830 1 return;
831 }
832
833 332 const float inv_D = 1.0f / static_cast<float>(D);
834
835
3/4
✓ Branch 0 taken 254 times.
✓ Branch 1 taken 78 times.
✓ Branch 2 taken 254 times.
✗ Branch 3 not taken.
332 if (R > 1 && static_cast<int64_t>(R) * D >= 262144) {
836
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ detail::cpu::parallel_for(static_cast<std::size_t>(R), [&](std::size_t ri) {
837 ✗ const int r = static_cast<int>(ri);
838 ✗ const float* xr = X + static_cast<std::size_t>(r) * D;
839 ✗ float* yr = Y + static_cast<std::size_t>(r) * D;
840 ✗ k.layernorm_row_fn(xr, gamma, beta, yr, static_cast<uint64_t>(D), eps, inv_D);
841 ✗ });
842 ✗ } else {
843
2/2
✓ Branch 0 taken 3514 times.
✓ Branch 1 taken 332 times.
3846 for (int r = 0; r < R; ++r) {
844 3514 const float* xr = X + static_cast<std::size_t>(r) * D;
845 3514 float* yr = Y + static_cast<std::size_t>(r) * D;
846 3514 k.layernorm_row_fn(xr, gamma, beta, yr, static_cast<uint64_t>(D), eps, inv_D);
847 3514 }
848 }
849 333 }
850
851 } // namespace brotensor::detail::cpu::jit
852
853 #else // !BROTENSOR_HAS_BRASS_JIT
854
855 namespace brotensor::detail::cpu::jit {
856
857 bool is_jit_available() { return false; }
858 void prewarm_cpu_jit_kernels() {}
859 void reset_cpu_jit_caches_for_testing() {}
860 void rms_norm_forward(const float* X, const float* gamma, float eps, float* Y, int B, int D) {
861 ref::rms_norm(X, gamma, eps, Y, B, D);
862 }
863 void swiglu_forward(const float* X, float* Y, int B, int D) {
864 (void)get_kernels();
865 ref::swiglu(X, Y, B, D);
866 }
867 void modulate(const float* X, const float* scale, const float* shift, float* Y, int L, int D) {
868 ref::modulate(X, scale, shift, Y, L, D);
869 }
870 void broadcast_mul(const float* X, const float* v, float* Y, int L, int D) {
871 ref::broadcast_mul(X, v, Y, L, D);
872 }
873 void layernorm_forward_inference_batched(const float* X, const float* gamma, const float* beta, float eps, float* Y, int R, int D) {
874 ref::layernorm_forward_inference_batched(X, gamma, beta, eps, Y, R, D);
875 }
876
877 } // namespace brotensor::detail::cpu::jit
878
879 #endif // BROTENSOR_HAS_BRASS_JIT
880