brotensor/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
-
-%
Branches:
-
-%
Functions:
-
-%

src/jit/spirv_emit.h
Line Branch Exec Source
1 #pragma once
2
3 // MIR emitters for the trace JIT's Vulkan backend.
4 //
5 // The same two kernel shapes the CUDA compiler emits as PTX text
6 // (ptx_emit.h), built here as brass MIR through KernelBuilder and lowered by
7 // brass's SPIR-V target (target::SpirvTarget::compile):
8 //
9 // elementwise — a grid-stride map over the trace's dominant shape, `vec`
10 // elements per thread so every full access moves 16 bytes (f32x4, or
11 // eight halves as an i32x4); (1, cols) rows and (1, 1) scalars are
12 // addressed by column, never materialised. BF16/FP16 are unpacked to FP32
13 // on load and rounded back on store.
14 //
15 // row-norm — RMSNorm / LayerNorm plus the elementwise chain around it, as
16 // row groups of `tpr` threads (plan::row_norm_geometry), subgroup
17 // shuffles plus one shared round for the reduction.
18 //
19 // Each emitter returns one kernel per entry: a vector entry (absent when the
20 // plan's `vec` is 1) and a scalar entry, with the same parameter list, so the
21 // launcher can pick at bind time from the pointers' alignment. Parameters are
22 // the plan's outputs then its inputs (all `ptr`), then for the elementwise
23 // kernel the slot count `n` (i32). Shapes, eps and the row geometry are baked
24 // in as constants: the trace signature already carries them.
25
26 #include "trace_dag.h"
27 #include "trace_plan.h"
28
29 #include <brass/target/spirv_target.hpp>
30
31 #include <cstdint>
32 #include <optional>
33
34 namespace brotensor::jit::spirv {
35
36 struct Kernels {
37 std::optional<brass::target::SpirvKernel> vec; // `lanes` = plan vec
38 brass::target::SpirvKernel scalar;
39 };
40
41 // Workgroup size of the elementwise kernel.
42 inline constexpr std::uint32_t kEwBlock = 256;
43
44 // Throws std::runtime_error for an op the emitters have no form for (callers
45 // check ops_supported() first).
46 Kernels emit_elementwise(const TraceDAG& dag, const plan::ElementwisePlan& plan);
47 Kernels emit_row_norm(const TraceDAG& dag, const plan::RowNormPlan& plan);
48
49 // True when every non-input node of `dag` is an op the elementwise chain can
50 // evaluate (the norms excepted: the row-norm emitter handles exactly one).
51 bool ops_supported(const TraceDAG& dag);
52
53 inline constexpr const char* kEntryVec = "trace_ew_vec";
54 inline constexpr const char* kEntryScalar = "trace_ew_scalar";
55 inline constexpr const char* kEntryRowNorm = "trace_row_norm_vec";
56 inline constexpr const char* kEntryRowNormScalar = "trace_row_norm_scalar";
57
58 } // namespace brotensor::jit::spirv
59