brotensor/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
262 / 286
91.6%
Branches:
234 / 422
55.5%
Functions:
19 / 19
100.0%

src/jit/gpu_mir_emit_common.cpp
Line Branch Exec Source
1 // Shared MIR helpers for GPU trace emitters (SPIR-V, PTX, MSL): typed lane loads
2 // and stores, BF16/FP16 packing, the per-lane op table. Notes: gpu_mir_emit_detail.h.
3
4 #include "gpu_mir_emit_detail.h"
5
6 #include <brass/mir/fma_opt.hpp>
7 #include <brass/mir/gpu_opt.hpp>
8
9 #include <cstdint>
10 #include <cstring>
11 #include <stdexcept>
12 #include <unordered_set>
13
14 namespace brotensor::jit::gpu_mir {
15
16 namespace {
17
18 constexpr float kLog2e = 1.4426950408889634f;
19
20 // 1 / (1 + 2^(-x log2 e)), with the exponent clamped so ex2 stays finite: the
21 // module does not declare SignedZeroInfNanPreserve, so an infinity in the
22 // middle of the expression is undefined rather than "rounds to 0".
23 189 Value* sigmoid(Ctx& c, Value* x) {
24 189 Value* t = c.kb.fmin(c.kb.fmax(c.b.build_mul(x, c.f32(-kLog2e)), c.f32(-88.0f)), c.f32(88.0f));
25 189 Value* e = c.kb.ex2_approx(t);
26 189 return c.kb.rcp_approx(c.b.build_add(e, c.f32(1.0f)));
27 }
28
29 // tanh(x) = sign(x) (1 - e) / (1 + e), e = 2^(-2|x| log2 e) in (0, 1]: no
30 // overflow anywhere, and no cancellation worse than the absolute error of e.
31 175 Value* tanh_(Ctx& c, Value* x) {
32 175 return c.kb.tanh_approx(x);
33 }
34
35 } // namespace
36
37 1608 std::vector<Value*> entry_params(Ctx& c, Function* fn) {
38 1608 brass::BasicBlock* entry = c.b.append_block("entry");
39 1608 c.b.position_at_end(entry);
40 1608 std::vector<Value*> out;
41
4/6
✓ Branch 0 taken 1608 times.
✓ Branch 1 taken 19369 times.
✓ Branch 2 taken 19369 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 19369 times.
✗ Branch 5 not taken.
20977 for (Type t : fn->param_types()) out.push_back(c.b.add_block_param(entry, t));
42 1608 return out;
43 1608 }
44
45 12113 Value* bits_to_f32(Ctx& c, Value* bits) { return c.b.build_bitcast_f32_i32(bits); }
46
47 615 Value* f32_to_bits(Ctx& c, Value* x) { return c.b.build_bitcast_i32_f32(x); }
48
49 36174 Value* byte_offset(Ctx& c, Value* elem, int esz) {
50 36174 Value* e = c.b.build_zext_i64(elem);
51
2/2
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 36149 times.
36174 return esz == 1 ? e : c.b.build_shl(e, c.kb.const_i64(log2_exact(esz)));
52 }
53
54 38 Value* udiv_const(Ctx& c, Value* x, int d) {
55
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38 times.
38 if (d <= 1) return x;
56 38 Builder& b = c.b;
57
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 27 times.
38 if (is_pow2(d)) return b.build_lshr(x, c.i32(log2_exact(d)));
58 27 const MagicU32 m = magic_u32(static_cast<std::uint32_t>(d));
59
1/2
✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
27 Value* xp = m.pre_shift ? b.build_lshr(x, c.i32(static_cast<int>(m.pre_shift))) : x;
60 27 Value* mh = c.kb.mul_hi_u32(xp, c.i32(static_cast<int32_t>(m.mul)));
61
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 if (!m.add) {
62
1/2
✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
27 return m.post_shift ? b.build_lshr(mh, c.i32(static_cast<int>(m.post_shift))) : mh;
63 }
64 ✗ Value* diff = b.build_sub(xp, mh);
65 ✗ Value* half = b.build_lshr(diff, c.i32(1));
66 ✗ Value* sum = b.build_add(half, mh);
67
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ return m.post_shift > 1 ? b.build_lshr(sum, c.i32(static_cast<int>(m.post_shift - 1))) : sum;
68 38 }
69
70 38 void udiv_urem_const(Ctx& c, Value* x, int d, Value*& q, Value*& r) {
71 38 Builder& b = c.b;
72 38 q = udiv_const(c, x, d);
73
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38 times.
38 if (d <= 1) {
74 ✗ r = c.i32(0);
75
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 27 times.
38 } else if (is_pow2(d)) {
76 11 r = b.build_and(x, c.i32(d - 1));
77 11 } else {
78 27 r = b.build_sub(x, b.build_mul(q, c.i32(d)));
79 }
80 38 }
81
82 namespace {
83
84 12 bool is_exact_pow2_f32(float f) {
85 12 std::uint32_t bits = 0;
86 12 std::memcpy(&bits, &f, sizeof(float));
87 12 const std::uint32_t exp = (bits >> 23) & 0xFFu;
88 12 const std::uint32_t mant = bits & 0x007FFFFFu;
89
2/4
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 12 times.
12 return mant == 0u && exp > 1u && exp < 253u;
90 }
91
92 // The two halves of a packed word as FP32.
93 7768 void unpack_word(Ctx& c, Dtype dt, Value* w, std::vector<Value*>& out) {
94 7768 Builder& b = c.b;
95
2/2
✓ Branch 0 taken 4006 times.
✓ Branch 1 taken 3762 times.
7768 if (dt == Dtype::FP16) {
96 // UnpackHalf2x16(x).x reads the low 16 bits only.
97 4006 out.push_back(c.kb.f16_to_f32(w));
98 4006 out.push_back(c.kb.f16_to_f32(b.build_lshr(w, c.i32(16))));
99 4006 } else {
100 3762 out.push_back(bits_to_f32(c, b.build_shl(w, c.i32(16))));
101 3762 out.push_back(bits_to_f32(c, b.build_and(w, c.i32(static_cast<int>(0xFFFF0000u)))));
102 }
103 7768 }
104
105 // One FP32 value as the 16 bits of `dt` (in the low half of an i32, zeros
106 // above). BF16 rounds to nearest even and keeps NaNs quiet — bit-identical to
107 // f32_to_bf16 in shaders/common.glsl and to brotensor::fp32_to_bf16_bits.
108 7865 Value* pack_half(Ctx& c, Dtype dt, Value* x) {
109
2/2
✓ Branch 0 taken 5292 times.
✓ Branch 1 taken 2573 times.
7865 if (dt == Dtype::FP16) return c.kb.f32_to_f16(x);
110 2573 return c.kb.f32_to_bf16(x);
111 7865 }
112
113 } // namespace
114
115 16357 std::vector<Value*> load_lanes(Ctx& c, Dtype dt, int lanes, Value* addr) {
116 16357 Builder& b = c.b;
117 16357 std::vector<Value*> out;
118
1/2
✓ Branch 0 taken 16357 times.
✗ Branch 1 not taken.
16357 out.reserve(static_cast<std::size_t>(lanes));
119
2/2
✓ Branch 0 taken 11476 times.
✓ Branch 1 taken 4881 times.
16357 if (dt == Dtype::FP32) {
120
2/2
✓ Branch 0 taken 3550 times.
✓ Branch 1 taken 1331 times.
4881 if (lanes == 1) {
121
2/4
✓ Branch 0 taken 3550 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3550 times.
✗ Branch 3 not taken.
3550 out.push_back(b.build_load(Type::f32(), addr, 0));
122
1/2
✓ Branch 0 taken 1331 times.
✗ Branch 1 not taken.
4881 } else if (lanes == 4) {
123
1/2
✓ Branch 0 taken 1331 times.
✗ Branch 1 not taken.
1331 Value* v = c.kb.vload_f32x4(addr);
124
4/6
✓ Branch 0 taken 1331 times.
✓ Branch 1 taken 5324 times.
✓ Branch 2 taken 5324 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 5324 times.
✗ Branch 5 not taken.
6655 for (uint32_t j = 0; j < 4; ++j) out.push_back(b.build_vextract_lane(v, j));
125 1331 } else {
126
0/12
✗ 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.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ throw std::logic_error("brotensor::jit: FP32 load of " + std::to_string(lanes) + " lanes");
127 }
128 4881 return out;
129 }
130
2/2
✓ Branch 0 taken 9518 times.
✓ Branch 1 taken 1958 times.
11476 if (lanes == 1) {
131
1/2
✓ Branch 0 taken 9518 times.
✗ Branch 1 not taken.
9518 Value* h = c.kb.load_u16(addr);
132
2/2
✓ Branch 0 taken 7654 times.
✓ Branch 1 taken 1864 times.
9518 if (dt == Dtype::FP16) {
133
2/4
✓ Branch 0 taken 7654 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7654 times.
✗ Branch 3 not taken.
7654 out.push_back(c.kb.f16_to_f32(h));
134 7654 } else {
135
4/8
✓ Branch 0 taken 1864 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1864 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1864 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1864 times.
✗ Branch 7 not taken.
1864 out.push_back(bits_to_f32(c, b.build_shl(h, c.i32(16))));
136 }
137 9518 return out;
138 }
139
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 1926 times.
1958 if (lanes == 4) {
140
1/2
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
32 Value* w = c.kb.load_i64(addr);
141
2/4
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 32 times.
✗ Branch 3 not taken.
32 unpack_word(c, dt, b.build_trunc_i32(w), out);
142
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 unpack_word(c, dt, b.build_trunc_i32(b.build_lshr(w, c.kb.const_i64(32))), out);
143 32 return out;
144 }
145
1/2
✓ Branch 0 taken 1926 times.
✗ Branch 1 not taken.
1926 if (lanes == 8) {
146
1/2
✓ Branch 0 taken 1926 times.
✗ Branch 1 not taken.
1926 Value* v = c.kb.vload(Type::i32x4(), addr);
147
4/6
✓ Branch 0 taken 1926 times.
✓ Branch 1 taken 7704 times.
✓ Branch 2 taken 7704 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 7704 times.
✗ Branch 5 not taken.
9630 for (uint32_t j = 0; j < 4; ++j) unpack_word(c, dt, b.build_vextract_lane(v, j), out);
148 1926 return out;
149 }
150
0/10
✗ 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.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ throw std::logic_error("brotensor::jit: 16-bit load of " + std::to_string(lanes) + " lanes");
151 16357 }
152
153 9198 void store_lanes(Ctx& c, Dtype dt, int lanes, Value* addr, const std::vector<Value*>& vals) {
154 9198 Builder& b = c.b;
155
2/2
✓ Branch 0 taken 7082 times.
✓ Branch 1 taken 2116 times.
9198 if (dt == Dtype::FP32) {
156
2/2
✓ Branch 0 taken 1979 times.
✓ Branch 1 taken 137 times.
2116 if (lanes == 1) {
157 1979 b.build_store(Type::f32(), addr, 0, vals[0]);
158 1979 } else {
159 137 Value* v = b.build_vzero(Type::f32x4());
160
2/2
✓ Branch 0 taken 548 times.
✓ Branch 1 taken 137 times.
685 for (uint32_t j = 0; j < 4; ++j) v = b.build_vinsert_lane(v, vals[j], j);
161 137 c.kb.vstore_f32x4(addr, v);
162 }
163 2116 return;
164 }
165
2/2
✓ Branch 0 taken 6957 times.
✓ Branch 1 taken 125 times.
7082 if (lanes == 1) {
166 6957 c.kb.store_u16(addr, pack_half(c, dt, vals[0]));
167 6957 return;
168 }
169 125 std::vector<Value*> words;
170
2/2
✓ Branch 0 taken 454 times.
✓ Branch 1 taken 125 times.
579 for (int j = 0; j < lanes; j += 2) {
171
1/2
✓ Branch 0 taken 454 times.
✗ Branch 1 not taken.
454 Value* lo = pack_half(c, dt, vals[static_cast<std::size_t>(j)]);
172
1/2
✓ Branch 0 taken 454 times.
✗ Branch 1 not taken.
454 Value* hi = pack_half(c, dt, vals[static_cast<std::size_t>(j + 1)]);
173
4/8
✓ Branch 0 taken 454 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 454 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 454 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 454 times.
✗ Branch 7 not taken.
454 words.push_back(b.build_or(lo, b.build_shl(hi, c.i32(16))));
174 454 }
175
2/2
✓ Branch 0 taken 23 times.
✓ Branch 1 taken 102 times.
125 if (lanes == 4) {
176
2/4
✓ Branch 0 taken 23 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 23 times.
✗ Branch 3 not taken.
46 Value* w = b.build_or(b.build_zext_i64(words[0]),
177
3/6
✓ Branch 0 taken 23 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 23 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 23 times.
✗ Branch 5 not taken.
23 b.build_shl(b.build_zext_i64(words[1]), c.kb.const_i64(32)));
178
1/2
✓ Branch 0 taken 23 times.
✗ Branch 1 not taken.
23 c.kb.store_i64(addr, w);
179 23 return;
180 }
181
1/2
✓ Branch 0 taken 102 times.
✗ Branch 1 not taken.
102 Value* v = b.build_vzero(Type::i32x4());
182
3/4
✓ Branch 0 taken 102 times.
✓ Branch 1 taken 408 times.
✓ Branch 2 taken 408 times.
✗ Branch 3 not taken.
510 for (uint32_t j = 0; j < 4; ++j) v = b.build_vinsert_lane(v, words[j], j);
183
1/2
✓ Branch 0 taken 102 times.
✗ Branch 1 not taken.
102 c.kb.vstore(Type::i32x4(), addr, v);
184 9198 }
185
186 15 bool has_lane_form(TraceOpKind op) {
187
1/2
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
15 switch (op) {
188 case TraceOpKind::Add: case TraceOpKind::Sub: case TraceOpKind::Mul: case TraceOpKind::Div:
189 case TraceOpKind::AddScalar: case TraceOpKind::SubScalar: case TraceOpKind::MulScalar:
190 case TraceOpKind::DivScalar: case TraceOpKind::ScalarSub: case TraceOpKind::ScalarDiv:
191 case TraceOpKind::FMA: case TraceOpKind::SiLU: case TraceOpKind::GELU: case TraceOpKind::ReLU:
192 case TraceOpKind::Tanh: case TraceOpKind::Sigmoid: case TraceOpKind::Modulate:
193 case TraceOpKind::Copy: case TraceOpKind::Where: case TraceOpKind::Clamp:
194 case TraceOpKind::Cast:
195 15 return true;
196 default:
197 ✗ return false;
198 }
199 15 }
200
201 7420 Value* lane_op(Ctx& c, const TraceNode& n, const std::vector<Value*>& in) {
202 7420 Builder& b = c.b;
203 14840 auto need = [&](std::size_t k) {
204
1/2
✓ Branch 0 taken 7420 times.
✗ Branch 1 not taken.
7420 if (in.size() < k) {
205
0/14
✗ 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.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ throw std::runtime_error(std::string("brotensor::jit: ") + op_kind_name(n.op) +
206 " is missing an operand");
207 }
208 7420 };
209
17/22
✓ Branch 0 taken 868 times.
✓ Branch 1 taken 58 times.
✓ Branch 2 taken 1048 times.
✓ Branch 3 taken 672 times.
✓ Branch 4 taken 1246 times.
✓ Branch 5 taken 84 times.
✓ Branch 6 taken 1087 times.
✓ Branch 7 taken 270 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✓ Branch 10 taken 1196 times.
✓ Branch 11 taken 75 times.
✓ Branch 12 taken 364 times.
✓ Branch 13 taken 60 times.
✓ Branch 14 taken 129 times.
✓ Branch 15 taken 170 times.
✓ Branch 16 taken 76 times.
✓ Branch 17 taken 5 times.
✓ Branch 18 taken 12 times.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
7420 switch (n.op) {
210 868 case TraceOpKind::Add: need(2); return b.build_add(in[0], in[1]);
211 58 case TraceOpKind::Sub: need(2); return b.build_sub(in[0], in[1]);
212 1048 case TraceOpKind::Mul: need(2); return b.build_mul(in[0], in[1]);
213 672 case TraceOpKind::Div: need(2); return b.build_sdiv(in[0], in[1]);
214 1246 case TraceOpKind::AddScalar: need(1); return b.build_add(in[0], c.f32(n.scalar));
215 84 case TraceOpKind::SubScalar: need(1); return b.build_sub(in[0], c.f32(n.scalar));
216 1087 case TraceOpKind::MulScalar: need(1); return b.build_mul(in[0], c.f32(n.scalar));
217 case TraceOpKind::DivScalar:
218 12 need(1);
219
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 if (is_exact_pow2_f32(n.scalar)) return b.build_mul(in[0], c.f32(1.0f / n.scalar));
220 ✗ return b.build_sdiv(in[0], c.f32(n.scalar));
221 270 case TraceOpKind::ScalarSub: need(1); return b.build_sub(c.f32(n.scalar), in[0]);
222 ✗ case TraceOpKind::ScalarDiv: need(1); return b.build_sdiv(c.f32(n.scalar), in[0]);
223 ✗ case TraceOpKind::FMA: need(3); return b.build_fma_f32(in[0], in[1], in[2]);
224 1196 case TraceOpKind::ReLU: need(1); return c.kb.fmax(in[0], c.f32(0.0f));
225 75 case TraceOpKind::Clamp: need(1); return c.kb.fmin(c.kb.fmax(in[0], c.f32(n.scalar)), c.f32(n.scalar2));
226 case TraceOpKind::Where: {
227 364 need(3);
228 364 Value* bits = b.build_and(f32_to_bits(c, in[0]), c.i32(0x7FFFFFFF));
229 364 return b.build_select(b.build_ne(bits, c.i32(0)), in[1], in[2]);
230 }
231 case TraceOpKind::Cast: {
232 ✗ need(1);
233
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (n.dtype == Dtype::FP16) return c.kb.f16_to_f32(c.kb.f32_to_f16(in[0]));
234
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (n.dtype == Dtype::BF16) {
235 ✗ return bits_to_f32(c, b.build_shl(pack_half(c, Dtype::BF16, in[0]), c.i32(16)));
236 }
237 ✗ return in[0];
238 }
239 60 case TraceOpKind::Sigmoid: need(1); return sigmoid(c, in[0]);
240 129 case TraceOpKind::SiLU: need(1); return b.build_mul(in[0], sigmoid(c, in[0]));
241 170 case TraceOpKind::Tanh: need(1); return tanh_(c, in[0]);
242 76 case TraceOpKind::Copy: need(1); return in[0];
243 case TraceOpKind::GELU: {
244 // 0.5 x (1 + tanh(sqrt(2/pi) (x + 0.044715 x^3))) — the tanh
245 // approximation nn.GELU(approximate="tanh") uses, as the PTX form.
246 5 need(1);
247 5 Value* x = in[0];
248 5 Value* xc = c.kb.fmin(c.kb.fmax(x, c.f32(-20.0f)), c.f32(20.0f));
249 5 Value* x2 = b.build_mul(xc, xc);
250 5 Value* inner = b.build_fma_f32(x2, c.f32(0.044715f), c.f32(1.0f));
251 5 Value* arg = b.build_mul(b.build_mul(inner, xc), c.f32(0.7978845608f));
252 5 Value* t1 = b.build_add(tanh_(c, arg), c.f32(1.0f));
253 5 return b.build_mul(b.build_mul(x, c.f32(0.5f)), t1);
254 }
255 case TraceOpKind::Modulate:
256 // x * (1 + scale) + shift; scale/shift are normally (1, D) rows
257 // the loader has already resolved to this lane's column.
258 ✗ need(3);
259 ✗ return b.build_fma_f32(in[0], b.build_add(in[1], c.f32(1.0f)), in[2]);
260 default:
261
0/14
✗ 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.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ throw std::runtime_error(std::string("brotensor::jit: op '") + op_kind_name(n.op) +
262 "' has no elementwise SPIR-V form");
263 }
264 7420 }
265
266 namespace {
267
268 135838 bool same_node_expr(const TraceNode& a, const TraceNode& b) {
269
5/6
✓ Branch 0 taken 19116 times.
✓ Branch 1 taken 116722 times.
✓ Branch 2 taken 19116 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 18926 times.
✓ Branch 5 taken 190 times.
135838 if (a.op != b.op || a.dtype != b.dtype || a.inputs != b.inputs) return false;
270 190 std::uint32_t as1 = 0, bs1 = 0, as2 = 0, bs2 = 0;
271 190 std::memcpy(&as1, &a.scalar, sizeof(float));
272 190 std::memcpy(&bs1, &b.scalar, sizeof(float));
273 190 std::memcpy(&as2, &a.scalar2, sizeof(float));
274 190 std::memcpy(&bs2, &b.scalar2, sizeof(float));
275
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 130 times.
190 return as1 == bs1 && as2 == bs2;
276 135838 }
277
278 } // namespace
279
280 218 ValueMap eval_chain(Ctx& c, const TraceDAG& dag, int lanes, ValueMap vals,
281 const std::vector<int>* order) {
282 218 std::vector<int> ids;
283
2/2
✓ Branch 0 taken 148 times.
✓ Branch 1 taken 70 times.
218 if (order) {
284
1/2
✓ Branch 0 taken 148 times.
✗ Branch 1 not taken.
148 ids = *order;
285 148 } else {
286
3/4
✓ Branch 0 taken 70 times.
✓ Branch 1 taken 2684 times.
✓ Branch 2 taken 2684 times.
✗ Branch 3 not taken.
2754 for (const auto& n : dag.nodes()) ids.push_back(n.id);
287 }
288
289 // Identify single-use Mul nodes that feed directly into an Add in this chain.
290 218 std::unordered_map<int, int> use_count;
291
2/2
✓ Branch 0 taken 218 times.
✓ Branch 1 taken 3318 times.
3536 for (const auto& n : dag.nodes()) {
292
3/4
✓ Branch 0 taken 3318 times.
✓ Branch 1 taken 4020 times.
✓ Branch 2 taken 4020 times.
✗ Branch 3 not taken.
7338 for (int src : n.inputs) ++use_count[src];
293 }
294
1/2
✓ Branch 0 taken 218 times.
✗ Branch 1 not taken.
218 std::unordered_set<int> in_chain(ids.begin(), ids.end());
295 218 std::unordered_set<int> fused_mul;
296
2/2
✓ Branch 0 taken 2718 times.
✓ Branch 1 taken 218 times.
2936 for (int id : ids) {
297
1/2
✓ Branch 0 taken 2718 times.
✗ Branch 1 not taken.
2718 const TraceNode& n = dag.node(id);
298
3/4
✓ Branch 0 taken 338 times.
✓ Branch 1 taken 2380 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 338 times.
2718 if (n.op != TraceOpKind::Add || n.inputs.size() < 2) continue;
299
2/2
✓ Branch 0 taken 259 times.
✓ Branch 1 taken 621 times.
880 for (int k = 0; k < 2; ++k) {
300 621 const int src = n.inputs[static_cast<std::size_t>(k)];
301
6/8
✓ Branch 0 taken 621 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 561 times.
✓ Branch 3 taken 60 times.
✓ Branch 4 taken 561 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 517 times.
✓ Branch 7 taken 44 times.
621 if (!in_chain.count(src) || vals.count(src)) continue;
302
1/2
✓ Branch 0 taken 517 times.
✗ Branch 1 not taken.
517 const TraceNode& m = dag.node(src);
303
7/10
✓ Branch 0 taken 81 times.
✓ Branch 1 taken 436 times.
✓ Branch 2 taken 81 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 81 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 79 times.
✓ Branch 7 taken 2 times.
✓ Branch 8 taken 79 times.
✗ Branch 9 not taken.
517 if (m.op == TraceOpKind::Mul && !m.is_live_at_end && use_count[src] == 1 &&
304 79 m.inputs.size() >= 2) {
305
1/2
✓ Branch 0 taken 79 times.
✗ Branch 1 not taken.
79 fused_mul.insert(src);
306 79 break;
307 }
308 438 }
309 }
310
311 218 std::vector<int> evaluated;
312
1/2
✓ Branch 0 taken 218 times.
✗ Branch 1 not taken.
218 evaluated.reserve(ids.size());
313
314
2/2
✓ Branch 0 taken 2718 times.
✓ Branch 1 taken 218 times.
2936 for (int id : ids) {
315
1/2
✓ Branch 0 taken 2718 times.
✗ Branch 1 not taken.
2718 const TraceNode& n = dag.node(id);
316
4/6
✓ Branch 0 taken 2467 times.
✓ Branch 1 taken 251 times.
✓ Branch 2 taken 2467 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2467 times.
✗ Branch 5 not taken.
2718 if (n.op == TraceOpKind::Input || vals.count(n.id)) continue;
317
3/4
✓ Branch 0 taken 2467 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 79 times.
✓ Branch 3 taken 2388 times.
2467 if (fused_mul.count(n.id)) continue;
318
319 // Elide redundant Cast when input node already carries the target dtype
320 // or when casting to FP32 (since registers are already FP32).
321
3/4
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2384 times.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
2388 if (n.op == TraceOpKind::Cast && !n.inputs.empty()) {
322 4 const int src = n.inputs[0];
323
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
4 if (n.dtype == Dtype::FP32 || dag.node(src).dtype == n.dtype) {
324
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 vals[n.id] = vals.at(src);
325 4 continue;
326 }
327 ✗ }
328
3/4
✓ Branch 0 taken 103 times.
✓ Branch 1 taken 2281 times.
✓ Branch 2 taken 103 times.
✗ Branch 3 not taken.
2384 if (n.op == TraceOpKind::Copy && !n.inputs.empty()) {
329
3/6
✓ Branch 0 taken 103 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 103 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 103 times.
✗ Branch 5 not taken.
103 vals[n.id] = vals.at(n.inputs[0]);
330 103 continue;
331 }
332
333 // DAG-level CSE across identical expressions in the same chain.
334 2281 int cse_match = -1;
335
2/2
✓ Branch 0 taken 2151 times.
✓ Branch 1 taken 135838 times.
137989 for (int prev_id : evaluated) {
336
4/6
✓ Branch 0 taken 135838 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 135838 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 135708 times.
✓ Branch 5 taken 130 times.
135838 if (same_node_expr(n, dag.node(prev_id))) {
337 130 cse_match = prev_id;
338 130 break;
339 }
340 }
341
2/2
✓ Branch 0 taken 130 times.
✓ Branch 1 taken 2151 times.
2281 if (cse_match >= 0) {
342
3/6
✓ Branch 0 taken 130 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 130 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 130 times.
✗ Branch 5 not taken.
130 vals[n.id] = vals.at(cse_match);
343 130 continue;
344 }
345
346 // Fused Add(Mul(a, b), d) or Add(d, Mul(a, b)) -> FMA(a, b, d).
347 2151 int mul_input_idx = -1;
348
3/4
✓ Branch 0 taken 334 times.
✓ Branch 1 taken 1817 times.
✓ Branch 2 taken 334 times.
✗ Branch 3 not taken.
2151 if (n.op == TraceOpKind::Add && n.inputs.size() >= 2) {
349
3/4
✓ Branch 0 taken 334 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 55 times.
✓ Branch 3 taken 279 times.
334 if (fused_mul.count(n.inputs[0])) mul_input_idx = 0;
350
3/4
✓ Branch 0 taken 279 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 24 times.
✓ Branch 3 taken 255 times.
279 else if (fused_mul.count(n.inputs[1])) mul_input_idx = 1;
351 334 }
352
353
1/2
✓ Branch 0 taken 2151 times.
✗ Branch 1 not taken.
2151 std::vector<Value*> dst(static_cast<std::size_t>(lanes));
354
2/2
✓ Branch 0 taken 79 times.
✓ Branch 1 taken 2072 times.
2151 if (mul_input_idx >= 0) {
355
1/2
✓ Branch 0 taken 79 times.
✗ Branch 1 not taken.
79 const TraceNode& m = dag.node(n.inputs[static_cast<std::size_t>(mul_input_idx)]);
356 79 const int addend_id = n.inputs[static_cast<std::size_t>(1 - mul_input_idx)];
357
1/2
✓ Branch 0 taken 79 times.
✗ Branch 1 not taken.
79 const auto& va = vals.at(m.inputs[0]);
358
1/2
✓ Branch 0 taken 79 times.
✗ Branch 1 not taken.
79 const auto& vb = vals.at(m.inputs[1]);
359
1/2
✓ Branch 0 taken 79 times.
✗ Branch 1 not taken.
79 const auto& vd = vals.at(addend_id);
360
2/2
✓ Branch 0 taken 79 times.
✓ Branch 1 taken 217 times.
296 for (int j = 0; j < lanes; ++j) {
361 217 const std::size_t js = static_cast<std::size_t>(j);
362
3/8
✓ Branch 0 taken 138 times.
✓ Branch 1 taken 79 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 138 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
217 if (j > 0 && va[js] == va[0] && vb[js] == vb[0] && vd[js] == vd[0]) {
363 ✗ dst[js] = dst[0];
364 ✗ } else {
365
1/2
✓ Branch 0 taken 217 times.
✗ Branch 1 not taken.
217 dst[js] = c.b.build_fma_f32(va[js], vb[js], vd[js]);
366 }
367 217 }
368
1/2
✓ Branch 0 taken 79 times.
✗ Branch 1 not taken.
79 vals[n.id] = std::move(dst);
369
1/2
✓ Branch 0 taken 79 times.
✗ Branch 1 not taken.
79 evaluated.push_back(n.id);
370 79 continue;
371 }
372
373 2072 std::vector<Value*> in0;
374
1/2
✓ Branch 0 taken 2072 times.
✗ Branch 1 not taken.
2072 in0.reserve(n.inputs.size());
375
2/2
✓ Branch 0 taken 2072 times.
✓ Branch 1 taken 5204 times.
7276 for (int j = 0; j < lanes; ++j) {
376 5204 std::vector<Value*> in;
377
1/2
✓ Branch 0 taken 5204 times.
✗ Branch 1 not taken.
5204 in.reserve(n.inputs.size());
378 5204 bool same_as_lane0 = (j > 0);
379
2/2
✓ Branch 0 taken 7588 times.
✓ Branch 1 taken 5204 times.
12792 for (std::size_t k = 0; k < n.inputs.size(); ++k) {
380 7588 const int src = n.inputs[k];
381
1/2
✓ Branch 0 taken 7588 times.
✗ Branch 1 not taken.
7588 auto it = vals.find(src);
382
2/4
✓ Branch 0 taken 7588 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7588 times.
✗ Branch 3 not taken.
7588 if (it == vals.end()) {
383
0/10
✗ 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.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ throw std::runtime_error("brotensor::jit: trace node " + std::to_string(n.id) +
384 " reads an operand that was never materialised");
385 }
386
1/2
✓ Branch 0 taken 7588 times.
✗ Branch 1 not taken.
7588 Value* v = it->second[static_cast<std::size_t>(j)];
387
1/2
✓ Branch 0 taken 7588 times.
✗ Branch 1 not taken.
7588 in.push_back(v);
388
4/4
✓ Branch 0 taken 4557 times.
✓ Branch 1 taken 3031 times.
✓ Branch 2 taken 4491 times.
✓ Branch 3 taken 66 times.
7588 if (j > 0 && v != in0[k]) same_as_lane0 = false;
389 7588 }
390
3/4
✓ Branch 0 taken 2072 times.
✓ Branch 1 taken 3132 times.
✓ Branch 2 taken 2072 times.
✗ Branch 3 not taken.
5204 if (j == 0) in0 = in;
391
2/2
✓ Branch 0 taken 5192 times.
✓ Branch 1 taken 12 times.
5204 if (same_as_lane0) {
392 12 dst[static_cast<std::size_t>(j)] = dst[0];
393 12 } else {
394
1/2
✓ Branch 0 taken 5192 times.
✗ Branch 1 not taken.
5192 dst[static_cast<std::size_t>(j)] = lane_op(c, n, in);
395 }
396 5204 }
397
1/2
✓ Branch 0 taken 2072 times.
✗ Branch 1 not taken.
2072 vals[n.id] = std::move(dst);
398
1/2
✓ Branch 0 taken 2072 times.
✗ Branch 1 not taken.
2072 evaluated.push_back(n.id);
399 2151 }
400 218 return vals;
401 218 }
402
403 134 void optimize_function(Function& fn, const brass::verify::GpuKernelContract* contract) {
404
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 134 times.
134 if (fn.parent()) {
405 134 brass::GpuOptOptions gpu_opts;
406 134 gpu_opts.opt_level = 2;
407 134 gpu_opts.enable_gpu_superopt = true;
408 134 gpu_opts.enable_small_loop_unroll = true;
409 134 gpu_opts.enable_load_coalesce = true;
410 134 gpu_opts.enable_proof_guided_opt = true;
411 134 gpu_opts.contract = contract;
412 134 brass::optimize_gpu_function(fn, *fn.parent(), gpu_opts);
413 134 }
414 134 }
415
416 } // namespace brotensor::jit::gpu_mir
417