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 | 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 | 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 | 38 | if (d <= 1) return x; | |
| 56 | 38 | Builder& b = c.b; | |
| 57 | 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 | 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 | 27 | if (!m.add) { | |
| 62 | 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 | ✗ | 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 | 38 | if (d <= 1) { | |
| 74 | ✗ | r = c.i32(0); | |
| 75 | 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 | 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 | 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 | 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 | 16357 | out.reserve(static_cast<std::size_t>(lanes)); | |
| 119 | 16357 | if (dt == Dtype::FP32) { | |
| 120 | 4881 | if (lanes == 1) { | |
| 121 | 3550 | out.push_back(b.build_load(Type::f32(), addr, 0)); | |
| 122 | 4881 | } else if (lanes == 4) { | |
| 123 | 1331 | Value* v = c.kb.vload_f32x4(addr); | |
| 124 | 6655 | for (uint32_t j = 0; j < 4; ++j) out.push_back(b.build_vextract_lane(v, j)); | |
| 125 | 1331 | } else { | |
| 126 | ✗ | throw std::logic_error("brotensor::jit: FP32 load of " + std::to_string(lanes) + " lanes"); | |
| 127 | } | ||
| 128 | 4881 | return out; | |
| 129 | } | ||
| 130 | 11476 | if (lanes == 1) { | |
| 131 | 9518 | Value* h = c.kb.load_u16(addr); | |
| 132 | 9518 | if (dt == Dtype::FP16) { | |
| 133 | 7654 | out.push_back(c.kb.f16_to_f32(h)); | |
| 134 | 7654 | } else { | |
| 135 | 1864 | out.push_back(bits_to_f32(c, b.build_shl(h, c.i32(16)))); | |
| 136 | } | ||
| 137 | 9518 | return out; | |
| 138 | } | ||
| 139 | 1958 | if (lanes == 4) { | |
| 140 | 32 | Value* w = c.kb.load_i64(addr); | |
| 141 | 32 | unpack_word(c, dt, b.build_trunc_i32(w), out); | |
| 142 | 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 | 1926 | if (lanes == 8) { | |
| 146 | 1926 | Value* v = c.kb.vload(Type::i32x4(), addr); | |
| 147 | 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 | ✗ | 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 | 9198 | if (dt == Dtype::FP32) { | |
| 156 | 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 | 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 | 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 | 579 | for (int j = 0; j < lanes; j += 2) { | |
| 171 | 454 | Value* lo = pack_half(c, dt, vals[static_cast<std::size_t>(j)]); | |
| 172 | 454 | Value* hi = pack_half(c, dt, vals[static_cast<std::size_t>(j + 1)]); | |
| 173 | 454 | words.push_back(b.build_or(lo, b.build_shl(hi, c.i32(16)))); | |
| 174 | 454 | } | |
| 175 | 125 | if (lanes == 4) { | |
| 176 | 46 | Value* w = b.build_or(b.build_zext_i64(words[0]), | |
| 177 | 23 | b.build_shl(b.build_zext_i64(words[1]), c.kb.const_i64(32))); | |
| 178 | 23 | c.kb.store_i64(addr, w); | |
| 179 | 23 | return; | |
| 180 | } | ||
| 181 | 102 | Value* v = b.build_vzero(Type::i32x4()); | |
| 182 | 510 | for (uint32_t j = 0; j < 4; ++j) v = b.build_vinsert_lane(v, words[j], j); | |
| 183 | 102 | c.kb.vstore(Type::i32x4(), addr, v); | |
| 184 | 9198 | } | |
| 185 | |||
| 186 | 15 | bool has_lane_form(TraceOpKind op) { | |
| 187 | 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 | 7420 | if (in.size() < k) { | |
| 205 | ✗ | throw std::runtime_error(std::string("brotensor::jit: ") + op_kind_name(n.op) + | |
| 206 | " is missing an operand"); | ||
| 207 | } | ||
| 208 | 7420 | }; | |
| 209 | 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 | 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 | ✗ | if (n.dtype == Dtype::FP16) return c.kb.f16_to_f32(c.kb.f32_to_f16(in[0])); | |
| 234 | ✗ | 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 | ✗ | 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 | 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 | 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 | 218 | if (order) { | |
| 284 | 148 | ids = *order; | |
| 285 | 148 | } else { | |
| 286 | 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 | 3536 | for (const auto& n : dag.nodes()) { | |
| 292 | 7338 | for (int src : n.inputs) ++use_count[src]; | |
| 293 | } | ||
| 294 | 218 | std::unordered_set<int> in_chain(ids.begin(), ids.end()); | |
| 295 | 218 | std::unordered_set<int> fused_mul; | |
| 296 | 2936 | for (int id : ids) { | |
| 297 | 2718 | const TraceNode& n = dag.node(id); | |
| 298 | 2718 | if (n.op != TraceOpKind::Add || n.inputs.size() < 2) continue; | |
| 299 | 880 | for (int k = 0; k < 2; ++k) { | |
| 300 | 621 | const int src = n.inputs[static_cast<std::size_t>(k)]; | |
| 301 | 621 | if (!in_chain.count(src) || vals.count(src)) continue; | |
| 302 | 517 | const TraceNode& m = dag.node(src); | |
| 303 | 517 | if (m.op == TraceOpKind::Mul && !m.is_live_at_end && use_count[src] == 1 && | |
| 304 | 79 | m.inputs.size() >= 2) { | |
| 305 | 79 | fused_mul.insert(src); | |
| 306 | 79 | break; | |
| 307 | } | ||
| 308 | 438 | } | |
| 309 | } | ||
| 310 | |||
| 311 | 218 | std::vector<int> evaluated; | |
| 312 | 218 | evaluated.reserve(ids.size()); | |
| 313 | |||
| 314 | 2936 | for (int id : ids) { | |
| 315 | 2718 | const TraceNode& n = dag.node(id); | |
| 316 | 2718 | if (n.op == TraceOpKind::Input || vals.count(n.id)) continue; | |
| 317 | 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 | 2388 | if (n.op == TraceOpKind::Cast && !n.inputs.empty()) { | |
| 322 | 4 | const int src = n.inputs[0]; | |
| 323 | 4 | if (n.dtype == Dtype::FP32 || dag.node(src).dtype == n.dtype) { | |
| 324 | 4 | vals[n.id] = vals.at(src); | |
| 325 | 4 | continue; | |
| 326 | } | ||
| 327 | ✗ | } | |
| 328 | 2384 | if (n.op == TraceOpKind::Copy && !n.inputs.empty()) { | |
| 329 | 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 | 137989 | for (int prev_id : evaluated) { | |
| 336 | 135838 | if (same_node_expr(n, dag.node(prev_id))) { | |
| 337 | 130 | cse_match = prev_id; | |
| 338 | 130 | break; | |
| 339 | } | ||
| 340 | } | ||
| 341 | 2281 | if (cse_match >= 0) { | |
| 342 | 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 | 2151 | if (n.op == TraceOpKind::Add && n.inputs.size() >= 2) { | |
| 349 | 334 | if (fused_mul.count(n.inputs[0])) mul_input_idx = 0; | |
| 350 | 279 | else if (fused_mul.count(n.inputs[1])) mul_input_idx = 1; | |
| 351 | 334 | } | |
| 352 | |||
| 353 | 2151 | std::vector<Value*> dst(static_cast<std::size_t>(lanes)); | |
| 354 | 2151 | if (mul_input_idx >= 0) { | |
| 355 | 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 | 79 | const auto& va = vals.at(m.inputs[0]); | |
| 358 | 79 | const auto& vb = vals.at(m.inputs[1]); | |
| 359 | 79 | const auto& vd = vals.at(addend_id); | |
| 360 | 296 | for (int j = 0; j < lanes; ++j) { | |
| 361 | 217 | const std::size_t js = static_cast<std::size_t>(j); | |
| 362 | 217 | if (j > 0 && va[js] == va[0] && vb[js] == vb[0] && vd[js] == vd[0]) { | |
| 363 | ✗ | dst[js] = dst[0]; | |
| 364 | ✗ | } else { | |
| 365 | 217 | dst[js] = c.b.build_fma_f32(va[js], vb[js], vd[js]); | |
| 366 | } | ||
| 367 | 217 | } | |
| 368 | 79 | vals[n.id] = std::move(dst); | |
| 369 | 79 | evaluated.push_back(n.id); | |
| 370 | 79 | continue; | |
| 371 | } | ||
| 372 | |||
| 373 | 2072 | std::vector<Value*> in0; | |
| 374 | 2072 | in0.reserve(n.inputs.size()); | |
| 375 | 7276 | for (int j = 0; j < lanes; ++j) { | |
| 376 | 5204 | std::vector<Value*> in; | |
| 377 | 5204 | in.reserve(n.inputs.size()); | |
| 378 | 5204 | bool same_as_lane0 = (j > 0); | |
| 379 | 12792 | for (std::size_t k = 0; k < n.inputs.size(); ++k) { | |
| 380 | 7588 | const int src = n.inputs[k]; | |
| 381 | 7588 | auto it = vals.find(src); | |
| 382 | 7588 | if (it == vals.end()) { | |
| 383 | ✗ | throw std::runtime_error("brotensor::jit: trace node " + std::to_string(n.id) + | |
| 384 | " reads an operand that was never materialised"); | ||
| 385 | } | ||
| 386 | 7588 | Value* v = it->second[static_cast<std::size_t>(j)]; | |
| 387 | 7588 | in.push_back(v); | |
| 388 | 7588 | if (j > 0 && v != in0[k]) same_as_lane0 = false; | |
| 389 | 7588 | } | |
| 390 | 5204 | if (j == 0) in0 = in; | |
| 391 | 5204 | if (same_as_lane0) { | |
| 392 | 12 | dst[static_cast<std::size_t>(j)] = dst[0]; | |
| 393 | 12 | } else { | |
| 394 | 5192 | dst[static_cast<std::size_t>(j)] = lane_op(c, n, in); | |
| 395 | } | ||
| 396 | 5204 | } | |
| 397 | 2072 | vals[n.id] = std::move(dst); | |
| 398 | 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 | 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 |