brotensor/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
99 / 99
100.0%
Branches:
69 / 120
57.5%
Functions:
11 / 11
100.0%

src/kernels/mir_copy.cpp
Line Branch Exec Source
1 #include "mir_copy.h"
2 #include "mir_common.h"
3
4 namespace brotensor::kernels {
5
6 2 Function* build_fill_bytes_mir(Module& mod) {
7 4 Function* fn = mod.create_function(
8 2 "mir_fill_bytes", Type::void_type(), {Type::ptr(), Type::i32(), Type::i32()});
9 2 brass::codegen::KernelBuilder kb(mod, fn);
10
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 Ctx c(kb);
11
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 auto p = entry_params(c, fn);
12
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 Value* v = c.b.build_and(p[2], c.i32(0xff));
13
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
4 emit_vec_grid_stride(c, p[1], 1, kFillBlock, [&](Value* i) {
14 2 kb.store_u8(c.b.build_add(p[0], byte_offset(c, i, 1)), v);
15 2 });
16
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 c.b.build_ret_void();
17
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 return finalize_fn(fn);
18 2 }
19
20 7 Function* build_copy2d_mir(Module& mod, int esz, int lanes) {
21 14 Function* fn = mod.create_function(
22 7 "mir_copy2d", Type::void_type(),
23 7 {Type::ptr(), Type::ptr(), Type::i32(), Type::i32(), Type::i32(), Type::i32()});
24 7 brass::codegen::KernelBuilder kb(mod, fn);
25
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 Ctx c(kb);
26
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 auto p = entry_params(c, fn);
27
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 Value* gid_x = kb.global_tid_x();
28
3/6
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 7 times.
✗ Branch 5 not taken.
7 Value* stride_x = kb.mul(kb.nctaid_x(), c.i32(static_cast<int>(kCopyBlock)));
29
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 Value* n_vecs = vec_count(c, p[4], lanes);
30
31
4/8
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 7 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 7 times.
✗ Branch 7 not taken.
14 kb.for_range(kb.ctaid_y(), p[5], kb.nctaid_y(), [&](Value* r) {
32 7 Value* so = c.b.build_mul(r, p[2]);
33 7 Value* dof = c.b.build_mul(r, p[3]);
34
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
14 kb.for_range(gid_x, n_vecs, stride_x, [&](Value* kv) {
35 7 Value* col = col_elem_idx(c, kv, lanes);
36 7 Value* sp = ptr_at(c, p[0], c.b.build_add(so, col), esz);
37 7 Value* dp = ptr_at(c, p[1], c.b.build_add(dof, col), esz);
38
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 5 times.
7 if (lanes > 1) {
39 2 Value* vec = kb.vload(Type::i32x4(), sp);
40 2 kb.vstore(Type::i32x4(), dp, vec);
41
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 3 times.
7 } else if (esz == 4) {
42 2 kb.store_i32(dp, kb.load_i32(sp));
43
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 time.
5 } else if (esz == 2) {
44 2 kb.store_u16(dp, kb.load_u16(sp));
45 2 } else {
46 1 kb.store_u8(dp, kb.load_u8(sp));
47 }
48 7 });
49 7 });
50
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 c.b.build_ret_void();
51
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 return finalize_fn(fn);
52 7 }
53
54 4 Function* build_transpose_mir(Module& mod, int esz) {
55 8 Function* fn = mod.create_function(
56 4 "mir_transpose", Type::void_type(),
57 4 {Type::ptr(), Type::ptr(), Type::i32(), Type::i32(), Type::i32()});
58 4 brass::codegen::KernelBuilder kb(mod, fn);
59
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Ctx c(kb);
60
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 auto p = entry_params(c, fn);
61
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* tile = kb.shared_alloc_i32(32u * 33u);
62
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* tid = kb.tid_x();
63
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 Value* tx = c.b.build_and(tid, c.i32(31));
64
4/8
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 4 times.
✗ Branch 7 not taken.
4 Value* ty = c.b.build_and(c.b.build_lshr(tid, c.i32(5)), c.i32(7));
65 4 Value* rows = p[3];
66 4 Value* cols = p[4];
67
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* plane = c.b.build_mul(rows, cols);
68
4/8
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 4 times.
✗ Branch 7 not taken.
4 Value* tiles_c = c.b.build_lshr(c.b.build_add(cols, c.i32(31)), c.i32(5));
69
4/8
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 4 times.
✗ Branch 7 not taken.
4 Value* tiles_r = c.b.build_lshr(c.b.build_add(rows, c.i32(31)), c.i32(5));
70
71
4/8
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 4 times.
✗ Branch 7 not taken.
8 kb.for_range(kb.ctaid_z(), p[2], kb.nctaid_z(), [&](Value* b) {
72 4 Value* base = c.b.build_mul(b, plane);
73
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
8 kb.for_range(kb.ctaid_y(), tiles_r, kb.nctaid_y(), [&](Value* trow) {
74
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
8 kb.for_range(kb.ctaid_x(), tiles_c, kb.nctaid_x(), [&](Value* tcol) {
75 4 Value* r0 = c.b.build_shl(trow, c.i32(5));
76 4 Value* c0 = c.b.build_shl(tcol, c.i32(5));
77 4 Value* gc = c.b.build_add(c0, tx);
78
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 4 times.
20 for (int k = 0; k < 32; k += 8) {
79
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 4 times.
16 Value* tyk = (k == 0) ? ty : c.b.build_add(ty, c.i32(k));
80 16 Value* gr = c.b.build_add(r0, tyk);
81
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
32 kb.if_then(c.b.build_and(c.b.build_ult(gr, rows), c.b.build_ult(gc, cols)), [&] {
82 16 Value* idx = c.b.build_add(base, c.b.build_add(c.b.build_mul(gr, cols), gc));
83 16 Value* sp = ptr_at(c, p[0], idx, esz);
84
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 8 times.
16 Value* val = (esz == 4) ? kb.load_i32(sp) : kb.load_u16(sp);
85 16 Value* slot = c.b.build_add(c.b.build_mul(tyk, c.i32(33)), tx);
86 16 kb.shared_store_i32_indexed(tile, slot, val);
87 16 });
88 16 }
89 4 kb.sync();
90 4 Value* wr = c.b.build_add(r0, tx);
91 4 Value* tx33 = c.b.build_mul(tx, c.i32(33));
92
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 4 times.
20 for (int k = 0; k < 32; k += 8) {
93
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 4 times.
16 Value* tyk = (k == 0) ? ty : c.b.build_add(ty, c.i32(k));
94 16 Value* wc = c.b.build_add(c0, tyk);
95
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
32 kb.if_then(c.b.build_and(c.b.build_ult(wr, rows), c.b.build_ult(wc, cols)), [&] {
96 16 Value* val = kb.shared_load_i32_indexed(tile, c.b.build_add(tx33, tyk));
97 16 Value* idx = c.b.build_add(base, c.b.build_add(c.b.build_mul(wc, rows), wr));
98 16 Value* dp = ptr_at(c, p[1], idx, esz);
99
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 8 times.
16 if (esz == 4) kb.store_i32(dp, val);
100 8 else kb.store_u16(dp, val);
101 16 });
102 16 }
103 4 kb.sync();
104 4 });
105 4 });
106 4 });
107
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 c.b.build_ret_void();
108
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 return finalize_fn(fn);
109 4 }
110
111 } // namespace brotensor::kernels
112