brotensor/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
39 / 39
100.0%
Branches:
12 / 14
85.7%
Functions:
7 / 7
100.0%

src/jit/gpu_mir_emit_detail.h
Line Branch Exec Source
1 #pragma once
2
3 // Shared MIR-building helpers for the trace JIT's Vulkan emitters
4 // (spirv_emit_elementwise.cpp, spirv_emit_rownorm.cpp). Everything here is a
5 // few lines of KernelBuilder MIR; nothing knows SPIR-V syntax.
6 //
7 // Values are FP32 scalars, one per lane: a 16-byte load is split into lanes
8 // with vextract_lane (or word shifts for packed halves), the op chain runs
9 // per lane, and a store packs the lanes back. The driver's compiler (ACO on
10 // RADV) keeps the vector loads and stores 128-bit.
11 //
12 // BF16 needs the bits of an FP32 value and an FP32 value from bits: MIR's
13 // bitcast.i32 / bitcast.f32 (brass docs/mir_reference.md), one OpBitcast each.
14
15 #include "trace_plan.h"
16
17 #include <brass/codegen/kernel_jit.hpp>
18 #include <brass/mir/builder.hpp>
19 #include <brass/mir/function.hpp>
20 #include <brass/mir/module.hpp>
21 #include <brass/verify/gpu_kernel_prover.hpp>
22
23 #include <string>
24 #include <unordered_map>
25 #include <vector>
26
27 namespace brotensor::jit::gpu_mir {
28
29 using brass::Builder;
30 using brass::Function;
31 using brass::Type;
32 using brass::Value;
33 using brass::codegen::KernelBuilder;
34
35 using ValueMap = std::unordered_map<int, std::vector<Value*>>;
36
37 struct Ctx {
38 KernelBuilder& kb;
39 Builder& b;
40
41 3216 explicit Ctx(KernelBuilder& k) : kb(k), b(k.builder()) {}
42
43 234771 Value* i32(int v) { return kb.const_i32(v); }
44 51915 Value* f32(float v) { return kb.const_f32(v); }
45 };
46
47 // Creates `fn`'s entry block with one block parameter per kernel parameter.
48 std::vector<Value*> entry_params(Ctx& c, Function* fn);
49
50 // Bit reinterpretation (bitcast.f32 / bitcast.i32).
51 Value* bits_to_f32(Ctx& c, Value* bits_i32);
52 Value* f32_to_bits(Ctx& c, Value* x);
53
54 // `elem` (i32 element index) as an i64 byte offset for `esz`-byte elements.
55 Value* byte_offset(Ctx& c, Value* elem, int esz);
56
57 // Loads `lanes` consecutive elements of `dt` at `addr` as FP32 values: one
58 // access (f32, f32x4, u16, i64 = four halves, i32x4 = eight halves).
59 std::vector<Value*> load_lanes(Ctx& c, Dtype dt, int lanes, Value* addr);
60 void store_lanes(Ctx& c, Dtype dt, int lanes, Value* addr, const std::vector<Value*>& vals);
61
62 // One op on one lane. Throws for an op with no elementwise form.
63 Value* lane_op(Ctx& c, const TraceNode& n, const std::vector<Value*>& in);
64 bool has_lane_form(TraceOpKind op);
65
66 // Evaluates every node in `order` (default: the whole DAG in creation order,
67 // which is topological) that has no value yet, `lanes` wide.
68 ValueMap eval_chain(Ctx& c, const TraceDAG& dag, int lanes, ValueMap vals,
69 const std::vector<int>* order = nullptr);
70
71 // Runs the brass MIR optimization pipeline (constant folding, dominator CSE,
72 // GVN, FMA fusion, SMT guard simplification, MIR superoptimizer, and scalar
73 // cleanup) in place on `fn` while preserving structured-CFG invariants.
74 void optimize_function(Function& fn, const brass::verify::GpuKernelContract* contract = nullptr);
75
76 inline constexpr std::uint32_t kEwBlock = 256;
77
78 bool ops_supported(const TraceDAG& dag);
79 brass::Function* build_elementwise_fn(brass::Module& mod, const TraceDAG& dag,
80 const plan::ElementwisePlan& plan, int lanes,
81 const char* name, bool optimize = true);
82 brass::Function* build_row_norm_fn(brass::Module& mod, const TraceDAG& dag,
83 const plan::RowNormPlan& plan, int lanes,
84 const char* name, bool optimize = true);
85
86 36992 inline int log2_exact(int v) {
87 36992 int l = 0;
88
2/2
✓ Branch 0 taken 49264 times.
✓ Branch 1 taken 36992 times.
86256 while ((1 << l) < v) ++l;
89 36992 return l;
90 }
91
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 123 times.
123 inline bool is_pow2(int v) { return v > 0 && (v & (v - 1)) == 0; }
92
93 // Unsigned 32-bit Granlund-Montgomery magic-multiplier parameters for exact
94 // division by a runtime-constant divisor `d >= 1`:
95 // xp = x >> pre_shift
96 // if (!add) q = mul_hi_u32(xp, mul) >> post_shift
97 // else q = (((xp - mul_hi_u32(xp, mul)) >> 1) + mul_hi_u32(xp, mul)) >> (post_shift - 1)
98 44 struct MagicU32 {
99 44 std::uint32_t mul = 1;
100 44 std::uint32_t pre_shift = 0;
101 44 std::uint32_t post_shift = 0;
102 44 bool add = false;
103 };
104
105 44 inline MagicU32 magic_u32(std::uint32_t d) {
106 44 MagicU32 m;
107
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 44 times.
44 if (d <= 1u) return m;
108
2/2
✓ Branch 0 taken 162 times.
✓ Branch 1 taken 44 times.
206 while ((d & 1u) == 0u) {
109 162 d >>= 1u;
110 162 ++m.pre_shift;
111 }
112
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 40 times.
44 if (d == 1u) return m;
113 40 std::uint32_t l = 0;
114
2/2
✓ Branch 0 taken 189 times.
✓ Branch 1 taken 40 times.
229 while ((1ULL << l) < d) ++l;
115 40 const std::uint64_t p_lo = 1ULL << (32 + l - 1);
116 40 const std::uint64_t m_lo = (p_lo + d - 1ULL) / d;
117 40 const std::uint64_t e_lo = m_lo * d - p_lo;
118 40 const std::uint64_t max_v = 0xFFFFFFFFULL >> m.pre_shift;
119 40 const std::uint64_t nc = (max_v / d) * d - 1ULL;
120
2/2
✓ Branch 0 taken 37 times.
✓ Branch 1 taken 3 times.
40 if (nc * e_lo < p_lo) {
121 37 m.mul = static_cast<std::uint32_t>(m_lo);
122 37 m.post_shift = l - 1u;
123 37 m.add = false;
124 37 } else {
125 3 const std::uint64_t p_hi = 1ULL << (32 + l);
126 3 const std::uint64_t m_hi = (p_hi + d - 1ULL) / d;
127 3 m.mul = static_cast<std::uint32_t>(m_hi - (1ULL << 32));
128 3 m.post_shift = l;
129 3 m.add = true;
130 }
131 40 return m;
132 44 }
133
134 // Fast unsigned 32-bit division and quotient+remainder by compile-time constant
135 // `d >= 1` using bitwise shifts/masks for powers of two and 64-bit magic
136 // multiply-shift otherwise (zero OpUDiv / OpUMod instructions).
137 Value* udiv_const(Ctx& c, Value* x, int d);
138 void udiv_urem_const(Ctx& c, Value* x, int d, Value*& q, Value*& r);
139
140 } // namespace brotensor::jit::gpu_mir
141