brotensor/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
72 / 76
94.7%
Branches:
70 / 106
66.0%
Functions:
4 / 4
100.0%

src/jit/gpu_mir_emit_elementwise.cpp
Line Branch Exec Source
1 // Elementwise trace kernel as brass MIR (spirv_emit.h): a grid-stride loop
2 // over `n` slots of `lanes` elements; every operand addressed by its
3 // broadcast kind, every buffer at its own dtype.
4
5 #include "gpu_mir_emit_detail.h"
6 #if BRASS_WITH_SPIRV
7 #include "trace_contract.h"
8 #endif
9
10 #include <string>
11 #include <unordered_map>
12 #include <vector>
13
14 namespace brotensor::jit::gpu_mir {
15
16 65 brass::Function* build_elementwise_fn(brass::Module& mod, const TraceDAG& dag,
17 const plan::ElementwisePlan& plan, int lanes,
18 const char* name, bool optimize) {
19 65 const std::size_t n_out = plan.outputs.size();
20 65 const std::size_t n_in = plan.inputs.size();
21 65 std::vector<Type> params(n_out + n_in, Type::ptr());
22
1/2
✓ Branch 0 taken 65 times.
✗ Branch 1 not taken.
65 params.push_back(Type::i32());
23
2/4
✓ Branch 0 taken 65 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 65 times.
✗ Branch 3 not taken.
65 Function* fn = mod.create_function(name, Type::void_type(), brass::Span<const Type>(params));
24
25
1/2
✓ Branch 0 taken 65 times.
✗ Branch 1 not taken.
65 KernelBuilder kb(mod, fn);
26
1/2
✓ Branch 0 taken 65 times.
✗ Branch 1 not taken.
65 Ctx c(kb);
27 65 Builder& b = c.b;
28
1/2
✓ Branch 0 taken 65 times.
✗ Branch 1 not taken.
65 const std::vector<Value*> p = entry_params(c, fn);
29 65 Value* n = p.back();
30
31
1/2
✓ Branch 0 taken 65 times.
✗ Branch 1 not taken.
65 Value* i0 = kb.global_tid_x();
32
3/6
✓ Branch 0 taken 65 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 65 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 65 times.
✗ Branch 5 not taken.
65 Value* stride = b.build_mul(kb.ntid_x(), kb.nctaid_x());
33
34
2/4
✓ Branch 0 taken 65 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 65 times.
✗ Branch 3 not taken.
130 kb.for_range(i0, n, stride, [&](Value* i) {
35 // Element index of this thread's first lane, its column when some
36 // operand broadcasts across rows, and its row when some operand
37 // broadcasts across columns.
38
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 32 times.
65 Value* base = lanes > 1 ? b.build_shl(i, c.i32(log2_exact(lanes))) : i;
39 65 Value* col = nullptr;
40 65 Value* row = nullptr;
41
4/4
✓ Branch 0 taken 35 times.
✓ Branch 1 taken 30 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 32 times.
65 if (plan.any_row_bcast && plan.any_col_bcast) {
42 32 udiv_urem_const(c, base, plan.cols, row, col);
43
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 30 times.
65 } else if (plan.any_row_bcast) {
44
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (is_pow2(plan.cols)) {
45 3 col = b.build_and(base, c.i32(plan.cols - 1));
46 3 } else {
47 ✗ Value* q = nullptr;
48 ✗ udiv_urem_const(c, base, plan.cols, q, col);
49 }
50
1/2
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
33 } else if (plan.any_col_bcast) {
51 ✗ row = udiv_const(c, base, plan.cols);
52 ✗ }
53 // Byte offsets, one per (broadcast kind, element size) actually used.
54 65 std::unordered_map<int, Value*> full_off, row_off, col_off;
55 410 auto offset_for = [&](BroadcastKind bk, Dtype dt) -> Value* {
56 345 const int esz = plan::elem_bytes(dt);
57
2/2
✓ Branch 0 taken 47 times.
✓ Branch 1 taken 298 times.
643 auto& tbl = (bk == BroadcastKind::Row) ? row_off
58
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 250 times.
298 : (bk == BroadcastKind::Col) ? col_off : full_off;
59 345 auto it = tbl.find(esz);
60
2/2
✓ Branch 0 taken 213 times.
✓ Branch 1 taken 132 times.
345 if (it != tbl.end()) return it->second;
61
2/2
✓ Branch 0 taken 35 times.
✓ Branch 1 taken 97 times.
229 Value* idx = (bk == BroadcastKind::Row) ? col
62
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 65 times.
97 : (bk == BroadcastKind::Col) ? row : base;
63 132 Value* off = byte_offset(c, idx, esz);
64 132 tbl[esz] = off;
65 132 return off;
66 345 };
67
68 65 ValueMap vals;
69
2/2
✓ Branch 0 taken 236 times.
✓ Branch 1 taken 65 times.
301 for (std::size_t k = 0; k < n_in; ++k) {
70 236 const plan::BufferSpec& s = plan.inputs[k];
71 236 Value* ptr = p[n_out + k];
72
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 232 times.
236 if (s.bcast == BroadcastKind::Scalar) {
73 // One load feeds every lane.
74
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* v = load_lanes(c, s.dtype, 1, ptr)[0];
75
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
4 vals[s.node_id] = std::vector<Value*>(static_cast<std::size_t>(lanes), v);
76
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 184 times.
236 } else if (s.bcast == BroadcastKind::Col) {
77 // All lanes of this thread sit in the same row (build_plan
78 // requires cols % lanes == 0 when any_col_bcast is true).
79
3/6
✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 48 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 48 times.
✗ Branch 5 not taken.
48 Value* v = load_lanes(c, s.dtype, 1, b.build_add(ptr, offset_for(s.bcast, s.dtype)))[0];
80
2/4
✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 48 times.
✗ Branch 3 not taken.
48 vals[s.node_id] = std::vector<Value*>(static_cast<std::size_t>(lanes), v);
81 48 } else {
82
4/8
✓ Branch 0 taken 184 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 184 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 184 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 184 times.
✗ Branch 7 not taken.
184 vals[s.node_id] = load_lanes(c, s.dtype, lanes, b.build_add(ptr, offset_for(s.bcast, s.dtype)));
83 }
84 236 }
85
86
1/2
✓ Branch 0 taken 65 times.
✗ Branch 1 not taken.
65 vals = eval_chain(c, dag, lanes, std::move(vals));
87
88
2/2
✓ Branch 0 taken 113 times.
✓ Branch 1 taken 65 times.
178 for (std::size_t m = 0; m < n_out; ++m) {
89 113 const plan::BufferSpec& s = plan.outputs[m];
90
2/4
✓ Branch 0 taken 113 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 113 times.
✗ Branch 3 not taken.
113 Value* addr = b.build_add(p[m], offset_for(BroadcastKind::Full, s.dtype));
91
2/4
✓ Branch 0 taken 113 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 113 times.
✗ Branch 3 not taken.
113 store_lanes(c, s.dtype, lanes, addr, vals.at(s.node_id));
92 113 }
93 65 });
94
1/2
✓ Branch 0 taken 65 times.
✗ Branch 1 not taken.
65 b.build_ret_void();
95
2/2
✓ Branch 0 taken 64 times.
✓ Branch 1 taken 1 time.
65 if (optimize) {
96 #if BRASS_WITH_SPIRV
97
1/2
✓ Branch 0 taken 64 times.
✗ Branch 1 not taken.
64 const brass::verify::GpuKernelContract contract = spirv::build_elementwise_contract(plan, lanes);
98
1/2
✓ Branch 0 taken 64 times.
✗ Branch 1 not taken.
64 optimize_function(*fn, &contract);
99 #else
100 optimize_function(*fn);
101 #endif
102 64 }
103 65 return fn;
104 65 }
105
106 7 bool ops_supported(const TraceDAG& dag) {
107
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 39 times.
46 for (const auto& n : dag.nodes()) {
108
5/6
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 21 times.
✓ Branch 2 taken 15 times.
✓ Branch 3 taken 3 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 15 times.
39 if (n.op == TraceOpKind::Input || n.op == TraceOpKind::RMSNorm ||
109 15 n.op == TraceOpKind::LayerNorm) {
110 24 continue;
111 }
112
1/2
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
15 if (!has_lane_form(n.op)) return false;
113 }
114 7 return true;
115 7 }
116
117 } // namespace brotensor::jit::gpu_mir
118