interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / src / omega_blackwell_codegen.c

2023 lines · 102455 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0
2 * Copyright 2026 Drake Stapleton (AIEN / aien-dev)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "omega_blackwell_codegen.h"
18#include "omega_blackwell_submit.h"
19#include "omega_native.h"
20#include "sha256.h"
21#include "omega_path_check.h"
22#include <string.h>
23/* native v1 P07 cut A.2 (aienos#286, docs/numeric/OMEGA_GPU_NATIVE_BOUNDARY.md): with OMEGA_GPU_NO_POSIX this file
24 * builds -ffreestanding. Code generation then runs only through the allocation-free entry points
25 * (omega_blackwell_kernel_encode_into: the caller supplies the code buffer and its capacity); the allocating
26 * generators, their diagnostics and the printing variation self-test exist only in the hosted build. */
27#ifndef OMEGA_GPU_NO_POSIX
28#include <fcntl.h>
29#include <stdio.h>
30#include <stdlib.h>
31#include <unistd.h>
32#define BW_DIAG(...) fprintf(stderr, __VA_ARGS__)
33#else
34#define BW_DIAG(...) ((void)0)
35#endif
36
37void omega_bw_ir_init(BlackwellIRProgram *prog) {
38 if (!prog) return;
39 memset(prog, 0, sizeof(*prog));
40}
41
42int omega_bw_ir_alloc_vreg(BlackwellIRProgram *prog) {
43 if (!prog || prog->regalloc.num_vregs >= BW_MAX_VREGS) return -1;
44 int v = prog->regalloc.num_vregs++;
45 prog->regalloc.intervals[v].vreg = v;
46 prog->regalloc.intervals[v].first_def = -1;
47 prog->regalloc.intervals[v].last_use = -1;
48 prog->regalloc.intervals[v].phys_reg = -1;
49 prog->regalloc.intervals[v].bundle_size = 1;
50 prog->regalloc.intervals[v].is_pair = false;
51 prog->regalloc.intervals[v].is_quad = false;
52 prog->regalloc.intervals[v].active = false;
53 prog->regalloc.vreg_to_phys[v] = -1;
54 return v;
55}
56
57int omega_bw_ir_alloc_vreg64(BlackwellIRProgram *prog) {
58 if (!prog || prog->regalloc.num_vregs >= BW_MAX_VREGS) return -1;
59 int v = prog->regalloc.num_vregs++;
60 prog->regalloc.intervals[v].vreg = v;
61 prog->regalloc.intervals[v].first_def = -1;
62 prog->regalloc.intervals[v].last_use = -1;
63 prog->regalloc.intervals[v].phys_reg = -1;
64 prog->regalloc.intervals[v].bundle_size = 2;
65 prog->regalloc.intervals[v].is_pair = true;
66 prog->regalloc.intervals[v].is_quad = false;
67 prog->regalloc.intervals[v].active = false;
68 prog->regalloc.vreg_to_phys[v] = -1;
69 return v;
70}
71
72int omega_bw_ir_alloc_vreg128(BlackwellIRProgram *prog) {
73 if (!prog || prog->regalloc.num_vregs >= BW_MAX_VREGS) return -1;
74 int v = prog->regalloc.num_vregs++;
75 prog->regalloc.intervals[v].vreg = v;
76 prog->regalloc.intervals[v].first_def = -1;
77 prog->regalloc.intervals[v].last_use = -1;
78 prog->regalloc.intervals[v].phys_reg = -1;
79 prog->regalloc.intervals[v].bundle_size = 4;
80 prog->regalloc.intervals[v].is_pair = false;
81 prog->regalloc.intervals[v].is_quad = true;
82 prog->regalloc.intervals[v].active = false;
83 prog->regalloc.vreg_to_phys[v] = -1;
84 return v;
85}
86
87int omega_bw_ir_alloc_uvreg(BlackwellIRProgram *prog) {
88 if (!prog || prog->regalloc.num_uvregs >= BW_MAX_UVREGS) return -1;
89 int uv = prog->regalloc.num_uvregs++;
90 prog->regalloc.uintervals[uv].vreg = uv;
91 prog->regalloc.uintervals[uv].first_def = -1;
92 prog->regalloc.uintervals[uv].last_use = -1;
93 prog->regalloc.uintervals[uv].phys_reg = -1;
94 prog->regalloc.uintervals[uv].is_pair = false;
95 prog->regalloc.uintervals[uv].active = false;
96 prog->regalloc.uvreg_to_phys[uv] = -1;
97 return uv;
98}
99
100int omega_bw_ir_alloc_uvreg64(BlackwellIRProgram *prog) {
101 if (!prog || prog->regalloc.num_uvregs >= BW_MAX_UVREGS) return -1;
102 int uv = prog->regalloc.num_uvregs++;
103 prog->regalloc.uintervals[uv].vreg = uv;
104 prog->regalloc.uintervals[uv].first_def = -1;
105 prog->regalloc.uintervals[uv].last_use = -1;
106 prog->regalloc.uintervals[uv].phys_reg = -1;
107 prog->regalloc.uintervals[uv].is_pair = true;
108 prog->regalloc.uintervals[uv].active = false;
109 prog->regalloc.uvreg_to_phys[uv] = -1;
110 return uv;
111}
112
113int omega_bw_ir_append(BlackwellIRProgram *prog, const BlackwellIRInsn *insn) {
114 if (!prog || !insn || prog->count >= BW_MAX_IR_INSNS) return -1;
115 int idx = (int)prog->count++;
116 prog->insns[idx] = *insn;
117
118 /* Update live-interval def/use tracking */
119 if (insn->dst_vreg >= 0 && insn->dst_vreg < prog->regalloc.num_vregs) {
120 if (!insn->is_uniform) {
121 OmegaLiveInterval *iv = &prog->regalloc.intervals[insn->dst_vreg];
122 if (iv->first_def < 0) iv->first_def = idx;
123 if (idx > iv->last_use) iv->last_use = idx;
124 }
125 }
126 if (insn->src1_vreg >= 0 && insn->src1_vreg < prog->regalloc.num_vregs) {
127 OmegaLiveInterval *iv = &prog->regalloc.intervals[insn->src1_vreg];
128 if (iv->first_def < 0) iv->first_def = idx;
129 if (idx > iv->last_use) iv->last_use = idx;
130 }
131 if (insn->src2_vreg >= 0 && insn->src2_vreg < prog->regalloc.num_vregs) {
132 OmegaLiveInterval *iv = &prog->regalloc.intervals[insn->src2_vreg];
133 if (iv->first_def < 0) iv->first_def = idx;
134 if (idx > iv->last_use) iv->last_use = idx;
135 }
136 if (insn->src3_vreg >= 0 && insn->src3_vreg < prog->regalloc.num_vregs) {
137 OmegaLiveInterval *iv = &prog->regalloc.intervals[insn->src3_vreg];
138 if (iv->first_def < 0) iv->first_def = idx;
139 if (idx > iv->last_use) iv->last_use = idx;
140 }
141
142 /* Uniform register def/use tracking */
143 if (insn->is_uniform && insn->dst_vreg >= 0 && insn->dst_vreg < prog->regalloc.num_uvregs) {
144 OmegaLiveInterval *uiv = &prog->regalloc.uintervals[insn->dst_vreg];
145 if (uiv->first_def < 0) uiv->first_def = idx;
146 if (idx > uiv->last_use) uiv->last_use = idx;
147 }
148 if (insn->ureg >= 0 && insn->ureg < prog->regalloc.num_uvregs) {
149 OmegaLiveInterval *uiv = &prog->regalloc.uintervals[insn->ureg];
150 if (uiv->first_def < 0) uiv->first_def = idx;
151 if (idx > uiv->last_use) uiv->last_use = idx;
152 }
153
154 return idx;
155}
156
157int omega_bw_regalloc_solve(BlackwellIRProgram *prog) {
158 if (!prog) return -1;
159 OmegaRegAlloc *ra = &prog->regalloc;
160 if (ra->num_vregs < 0 || ra->num_vregs > BW_MAX_VREGS) return -1;
161 if (ra->num_uvregs < 0 || ra->num_uvregs > BW_MAX_UVREGS) return -1;
162
163 uint64_t cfg[4] = { (uint64_t)ra->num_vregs, (uint64_t)ra->num_uvregs, (uint64_t)ra->gpr_limit, 0 };
164 int64_t vinfo[BW_MAX_VREGS * 3];
165 uint64_t uinfo[BW_MAX_UVREGS];
166 int64_t res[290];
167 memset(vinfo, 0, sizeof(vinfo));
168 memset(uinfo, 0, sizeof(uinfo));
169 memset(res, 0, sizeof(res));
170
171 for (int v = 0; v < ra->num_vregs; v++) {
172 const OmegaLiveInterval *iv = &ra->intervals[v];
173 uint32_t bsize = iv->bundle_size ? iv->bundle_size : (iv->is_pair ? 2u : (iv->is_quad ? 4u : 1u));
174 vinfo[v * 3 + 0] = (int64_t)iv->first_def;
175 vinfo[v * 3 + 1] = (int64_t)iv->last_use;
176 vinfo[v * 3 + 2] = (int64_t)bsize;
177 }
178 for (int uv = 0; uv < ra->num_uvregs; uv++) {
179 uinfo[uv] = ra->uintervals[uv].is_pair ? 2u : 1u;
180 }
181
182 uint64_t rc = 1;
183 if (omega2_bw_regalloc_solve(&rc, cfg, 4, vinfo, BW_MAX_VREGS * 3, uinfo, BW_MAX_UVREGS, res, 290) != OMEGA_OK ||
184 rc != 0) {
185 return -1;
186 }
187
188 for (int v = 0; v < ra->num_vregs; v++) {
189 ra->intervals[v].phys_reg = (int)res[v];
190 ra->vreg_to_phys[v] = (int)res[v];
191 }
192 for (int uv = 0; uv < ra->num_uvregs; uv++) {
193 ra->uintervals[uv].phys_reg = (int)res[256 + uv];
194 ra->uvreg_to_phys[uv] = (int)res[256 + uv];
195 }
196 ra->peak_gpr_usage = (uint32_t)res[288];
197 ra->peak_ugpr_usage = (uint32_t)res[289];
198 return 0;
199}
200
201/* ---- sm_121 instruction encoder (aien-dev/CUDApp#79) ---------------------------------------------------
202 * The hosted build encodes through om/encoder/bw_enc_sm121.om: seeded Omega, compiled by the pinned omc and
203 * linked through the omega-bridge native binding src/omega_om_units.c (generated by om/units.sh, never edited
204 * by hand). The C encoder below is migration-pending: it is compiled only
205 * - in the freestanding build (OMEGA_GPU_NO_POSIX), because the bridge binding needs pthread and mmap and the
206 * freestanding binding is still a proposal (successor: the same .om unit behind a caller-stack binding);
207 * - with CUDAPP_MIGRATION_PENDING_C_ENCODER, the differential oracle build (make CUDAPP_ENCODER=c);
208 * - with CUDAPP_MIGRATION_PENDING_ENCODER_DIFF, which encodes every instruction and program both ways, aborts on
209 * any byte difference and logs each agreement to $CUDAPP_ENCODER_DIFF_LOG (make CUDAPP_ENCODER=diff).
210 * Successor of the C body: bw_enc_sm121.om; it is deleted once the freestanding binding lands. */
211#if defined(OMEGA_GPU_NO_POSIX) || defined(CUDAPP_MIGRATION_PENDING_C_ENCODER)
212#define BW_ENC_C_PRIMARY 1
213#endif
214#if defined(BW_ENC_C_PRIMARY) && defined(CUDAPP_MIGRATION_PENDING_ENCODER_DIFF)
215#error "CUDAPP_MIGRATION_PENDING_ENCODER_DIFF needs the .om encoder: not with OMEGA_GPU_NO_POSIX or the C encoder"
216#endif
217#if defined(BW_ENC_C_PRIMARY) || defined(CUDAPP_MIGRATION_PENDING_ENCODER_DIFF)
218static int bw_c_encode_insn(const BlackwellIRInsn *insn, const OmegaRegAlloc *ra, uint32_t w[4]) {
219 memset(w, 0, 16);
220
221 int dst = (insn->dst_vreg >= 0) ? (ra->vreg_to_phys[insn->dst_vreg] + insn->dst_subreg) : 0;
222 int src1 = (insn->src1_vreg >= 0) ? (ra->vreg_to_phys[insn->src1_vreg] + insn->src1_subreg) : 0;
223 int src2 = (insn->src2_vreg >= 0) ? (ra->vreg_to_phys[insn->src2_vreg] + insn->src2_subreg) : 0;
224 int src3 = (insn->src3_vreg >= 0) ? (ra->vreg_to_phys[insn->src3_vreg] + insn->src3_subreg) : 0xff; /* Default to RZ (0xff) */
225 int ureg = (insn->ureg >= 0) ? ra->uvreg_to_phys[insn->ureg] : 0;
226
227 if (insn->is_uniform && insn->dst_vreg >= 0) {
228 dst = ra->uvreg_to_phys[insn->dst_vreg];
229 }
230
231 switch (insn->op) {
232 case BW_IR_NOP:
233 w[0] = 0x00007918;
234 w[1] = 0x00000000;
235 w[2] = 0x00000000;
236 w[3] = insn->control ? insn->control : 0x000fc000; /* control: a timed pause (fragment matmul) */
237 break;
238
239 case BW_IR_MOV_RZ:
240 /* MOV Rd, RZ */
241 w[0] = 0x7202 | ((uint32_t)(dst & 0xff) << 16);
242 w[1] = 0x000000ff;
243 w[2] = 0x00000f00;
244 w[3] = insn->control ? insn->control : 0x000fe200;
245 break;
246
247 case BW_IR_MOV_IMM:
248 /* MOV Rd, imm32 */
249 w[0] = 0x7802 | ((uint32_t)(dst & 0xff) << 16);
250 w[1] = insn->imm;
251 w[2] = 0x00000f00;
252 w[3] = insn->control ? insn->control : 0x000fe200;
253 break;
254
255 case BW_IR_S2R:
256 /* S2R Rd, SR */
257 w[0] = 0x7919 | ((uint32_t)(dst & 0xff) << 16);
258 w[1] = 0x0;
259 w[2] = (insn->imm & 0xff) << 8;
260 w[3] = insn->control ? insn->control : 0x000e2200;
261 break;
262
263 case BW_IR_LDC:
264 /* LDC Rd, c[0x0][imm] */
265 w[0] = 0xff007b82 | ((uint32_t)(dst & 0xff) << 16);
266 w[1] = (insn->imm << 6) & 0x00ffff00;
267 w[2] = 0x00000800;
268 w[3] = insn->control ? insn->control : 0x000fe200;
269 break;
270
271 case BW_IR_LDC64:
272 /* LDC.64 Rd:Rd+1, c[0x0][imm] */
273 w[0] = 0xff007b82 | ((uint32_t)(dst & 0xff) << 16);
274 w[1] = (insn->imm << 6) & 0x00ffff00;
275 w[2] = 0x00000a00;
276 w[3] = insn->control ? insn->control : 0x000e2200;
277 break;
278
279 case BW_IR_LDCU:
280 /* LDCU URd, c[0x0][imm] */
281 w[0] = 0xff0077ac | ((uint32_t)(dst & 0xff) << 16);
282 w[1] = (insn->imm << 5) & 0x00ffff00;
283 w[2] = 0x08000800;
284 w[3] = insn->control ? insn->control : 0x000e2200;
285 break;
286
287 case BW_IR_LDCU64:
288 /* LDCU.64 URd:URd+1, c[0x0][imm] */
289 w[0] = 0xff0077ac | ((uint32_t)(dst & 0xff) << 16);
290 w[1] = (insn->imm << 5) & 0x00ffff00;
291 w[2] = 0x08000a00;
292 w[3] = insn->control ? insn->control : 0x000e6e00;
293 break;
294
295 case BW_IR_IMAD:
296 if (insn->src2_vreg >= 0) {
297 /* Register-register: IMAD Rd, Ra, Rb, Rc (0x7224) */
298 w[0] = 0x7224 | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24);
299 w[1] = (uint32_t)(src2 & 0xff);
300 w[2] = 0x078e0200 | (uint32_t)(src3 & 0xff);
301 w[3] = insn->control ? insn->control : 0x001fca00;
302 } else if (insn->is_uniform && insn->ureg >= 0) {
303 /* Uniform register multiplier: IMAD Rd, Ra, URb, Rc (0x7c24) */
304 w[0] = 0x7c24 | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24);
305 w[1] = (uint32_t)(ureg & 0xff);
306 w[2] = 0x0f8e0200 | (uint32_t)(src3 & 0xff);
307 w[3] = insn->control ? insn->control : 0x001fca00;
308 } else {
309 /* Immediate multiplier: IMAD Rd, Ra, imm, Rc (0x7824) */
310 w[0] = 0x7824 | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24);
311 w[1] = insn->imm;
312 w[2] = 0x078e0200 | (uint32_t)(src3 & 0xff);
313 w[3] = insn->control ? insn->control : 0x001fca00;
314 }
315 break;
316
317 case BW_IR_IMAD_WIDE:
318 /* IMAD.WIDE.U32 Rd:Rd+1, Ra, imm, Rc:Rc+1 */
319 w[0] = 0x7825 | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24);
320 w[1] = insn->imm;
321 w[2] = 0x078e0000 | (uint32_t)(src3 & 0xff);
322 w[3] = insn->control ? insn->control : 0x001fcc00;
323 break;
324
325 case BW_IR_ISETP_GE:
326 if (insn->is_uniform && insn->ureg >= 0) {
327 w[0] = 0x7c0c | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24);
328 w[1] = (uint32_t)(ureg & 0xff);
329 w[2] = 0x0bf06070;
330 w[3] = insn->control ? insn->control : 0x002fda00;
331 } else {
332 w[0] = 0x780c | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24);
333 w[1] = insn->imm;
334 w[2] = 0x03f06270;
335 w[3] = insn->control ? insn->control : 0x001fda00;
336 }
337 break;
338
339 case BW_IR_LDG_E: {
340 /* LDG.E Rd, desc[URd][Ra.64+imm]: the 32-bit size (4) of the LDG.E.64 / LDG.E.128 words below; imm as there
341 * (signed 24-bit byte offset at bits 40..63, w[1] bits 8..31), unchanged words when imm is 0. */
342 int32_t off = (int32_t)insn->imm;
343 if (off < -(1 << 23) || off >= (1 << 23)) return -1;
344 w[0] = 0x7981 | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24);
345 w[1] = (uint32_t)(ureg & 0xff) | (((uint32_t)off & 0xffffffU) << 8);
346 w[2] = 0x0c1e1900;
347 w[3] = insn->control ? insn->control : 0x000f2200;
348 break;
349 }
350
351 case BW_IR_STG_E: {
352 /* STG.E desc[URd][Ra.64+imm], Rb. imm = signed 24-bit byte offset at bits 40..63 (w[1] bits
353 * 8..31), as for LDG; words from nvcc 13.0.88 -arch=sm_121 (omega_blackwell_verify_codegen_fixtures_fragmm,
354 * 2026-10-12: STG.E desc[UR4][R4.64+0x4], R9 = 0x0000040904007986 0x000fe8000c101904). */
355 int32_t off = (int32_t)insn->imm;
356 if (off < -(1 << 23) || off >= (1 << 23)) return -1;
357 w[0] = 0x7986 | ((uint32_t)(src1 & 0xff) << 24);
358 w[1] = (uint32_t)(src2 & 0xff) | (((uint32_t)off & 0xffffffU) << 8);
359 w[2] = 0x0c101900 | (uint32_t)(ureg & 0xff);
360 w[3] = insn->control ? insn->control : 0x000fe200;
361 break;
362 }
363
364 case BW_IR_STG_EF:
365 /* STG.E.MMIO.GPU. Uncached store, meant to be visible without a fence. */
366 w[0] = 0x7986 | ((uint32_t)(src1 & 0xff) << 24);
367 w[1] = (uint32_t)(src2 & 0xff);
368 w[2] = 0x0c111900 | (uint32_t)(ureg & 0xff);
369 w[3] = insn->control ? insn->control : 0x000fe200;
370 break;
371
372 case BW_IR_IADD3:
373 /* IADD3 Rd, PT, PT, Ra, Rb, Rc */
374 w[0] = 0x7210 | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24);
375 w[1] = (uint32_t)(src2 & 0xff);
376 w[2] = 0x07ffe0ff;
377 w[3] = insn->control ? insn->control : 0x010fca00; /* omega-mv#545: the emitter's waits, not a fixed SB4 wait */
378 break;
379
380 case BW_IR_HMMA_F16:
381 case BW_IR_HMMA_BF16: {
382 /* HMMA.16816.F32[.BF16] Rd, Ra, Rb, Rc */
383 w[0] = 0x723c | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24);
384 w[1] = (uint32_t)(src2 & 0xff);
385 uint32_t prec_bit = (insn->op == BW_IR_HMMA_BF16) ? 0x00040000 : 0x00000000;
386 w[2] = (uint32_t)(src3 & 0xff) | (0x18 << 8) | prec_bit;
387 w[3] = insn->control ? insn->control : 0x000fe200;
388 break;
389 }
390
391 case BW_IR_SHF_R:
392 /* SHF.R.U32.HI Rd, RZ, imm, Ra */
393 w[0] = 0x7819U | ((uint32_t)(dst & 0xff) << 16) | (0xffU << 24);
394 w[1] = (uint32_t)insn->imm;
395 w[2] = 0x00011600 | (uint32_t)(src1 & 0xff);
396 w[3] = insn->control ? insn->control : 0x001fca00;
397 break;
398
399 case BW_IR_LOP3_AND:
400 /* LOP3.LUT Rd, Ra, imm, RZ, 0xc0, !PT */
401 w[0] = 0x7812U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24);
402 w[1] = (uint32_t)insn->imm;
403 w[2] = 0x078ec0ff;
404 w[3] = insn->control ? insn->control : 0x001fca00;
405 break;
406
407 case BW_IR_LDG_E_64:
408 case BW_IR_LDG_E_128: {
409 /* LDG.E.64 / LDG.E.128 Rd, desc[URd][Ra.64+imm]. imm = signed 24-bit byte offset at
410 * bits 40..63 (w[1] bits 8..31); size field in w[2] bits 9..11 (32 = 4, 64 = 5, 128 = 6).
411 * Words from nvcc 13.0.88 -arch=sm_121 (omega_blackwell_verify_codegen_fixtures_fragmm). */
412 int32_t off = (int32_t)insn->imm;
413 if (off < -(1 << 23) || off >= (1 << 23)) return -1;
414 w[0] = 0x7981U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24);
415 w[1] = (uint32_t)(ureg & 0xff) | (((uint32_t)off & 0xffffffU) << 8);
416 w[2] = insn->op == BW_IR_LDG_E_64 ? 0x0c1e1b00U : 0x0c1e1d00U;
417 w[3] = insn->control ? insn->control : 0x000f2200;
418 break;
419 }
420
421 case BW_IR_LDG_E_U16:
422 /* LDG.E.U16 Rd, desc[URd][Ra.64] */
423 w[0] = 0x7981U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24);
424 w[1] = (uint32_t)(ureg & 0xff);
425 w[2] = 0x0c1e1500;
426 w[3] = insn->control ? insn->control : 0x000f2200;
427 break;
428
429 case BW_IR_LDG_STRONG_SYS:
430 /* LDG.E.STRONG.SYS. Same shape as LDG.E; w[2] carries the
431 * system-scope bits (0x0c1e1900 -> 0x0c1f5900). */
432 w[0] = 0x7981 | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24);
433 w[1] = (uint32_t)(ureg & 0xff);
434 w[2] = 0x0c1f5900;
435 w[3] = insn->control ? insn->control : 0x000f2200;
436 break;
437
438 case BW_IR_LDG_MMIO:
439 /* LDG.E.MMIO.GPU. Do not keep a stale cached copy.
440 * 0x0c1f1900. 0x0c0e1900 is LDG.E.EF, the EF mutant, not this opcode. */
441 w[0] = 0x7981 | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24);
442 w[1] = (uint32_t)(ureg & 0xff);
443 w[2] = 0x0c1f1900;
444 w[3] = insn->control ? insn->control : 0x000f2200;
445 break;
446
447 case BW_IR_STG_STRONG_SYS:
448 w[0] = 0x7986 | ((uint32_t)(src1 & 0xff) << 24);
449 if (insn->predicate_p0) {
450 uint32_t pred = insn->predicate_not ? 0x8u : 0x0u;
451 w[0] = (w[0] & ~0xF000u) | (pred << 12);
452 }
453 w[1] = (uint32_t)(src2 & 0xff);
454 w[2] = 0x0c115900 | (uint32_t)(ureg & 0xff);
455 w[3] = insn->control ? insn->control : 0x000fe200;
456 break;
457
458 case BW_IR_MEMBAR_ALL_SYS:
459 w[0] = 0x00007992;
460 w[1] = 0x00000000;
461 w[2] = 0x0000b000;
462 w[3] = insn->control ? insn->control : 0x000fec00;
463 break;
464
465 case BW_IR_MEMBAR_SC_SYS:
466 w[0] = 0x00007992;
467 w[1] = 0x00000000;
468 w[2] = 0x00003000;
469 w[3] = insn->control ? insn->control : 0x000fec00;
470 break;
471
472 case BW_IR_CCTL_IVALL:
473 w[0] = 0xff00798f;
474 w[1] = 0x00000000;
475 w[2] = 0x02000000;
476 w[3] = insn->control ? insn->control : 0x000fe800;
477 break;
478
479 case BW_IR_ATOMG_ADD_STRONG_SYS:
480 w[0] = 0x79a8 | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24);
481 w[1] = (uint32_t)(src2 & 0xff) | 0x80000000u;
482 w[2] = 0x081f5100 | (uint32_t)(ureg & 0xff);
483 w[3] = insn->control ? insn->control : 0x00321e00;
484 break;
485
486 case BW_IR_ATOMG_EXCH_STRONG_SYS:
487 w[0] = 0x79a8 | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24);
488 w[1] = (uint32_t)(src2 & 0xff) | 0x80000000u;
489 w[2] = 0x0c1f5100 | (uint32_t)(ureg & 0xff);
490 w[3] = insn->control ? insn->control : 0x00321e00;
491 break;
492
493 case BW_IR_ATOMG_ADD_F32_STRONG_SYS:
494 case BW_IR_ATOMG_ADD_F32_STRONG_GPU:
495 /* ATOMG.E.ADD.F32.FTZ.RN.STRONG.{SYS,GPU} PT, Rd|RZ, desc[UR][Ra.64], Rb (omega_blackwell_verify_codegen_fixtures_atomf32).
496 * Float opcode 0x9a3; w2 bit 26 selects the desc[] form, type 9 (w2 bits 9..12) is F32.FTZ.RN, order (w2 bits
497 * 13..16) 0xa = SYS, 0x7 = GPU. No destination vreg encodes RZ (the generic default 0 would clobber R0). */
498 w[0] = 0x79a3 | ((uint32_t)(insn->dst_vreg < 0 ? 0xff : (dst & 0xff)) << 16) | ((uint32_t)(src1 & 0xff) << 24);
499 w[1] = (uint32_t)(src2 & 0xff) | 0x80000000u;
500 w[2] = (insn->op == BW_IR_ATOMG_ADD_F32_STRONG_GPU ? 0x0c1ef300u : 0x0c1f5300u) | (uint32_t)(ureg & 0xff);
501 w[3] = insn->control ? insn->control : 0x00321e00;
502 break;
503
504 case BW_IR_ISETP_GE_U32:
505 /* ISETP.GE.U32.AND P0, PT, Ra, Rb, PT. Checked with nvdisasm -b SM121. */
506 w[0] = 0x720c | ((uint32_t)(src1 & 0xff) << 24);
507 w[1] = (uint32_t)(src2 & 0xff);
508 w[2] = 0x03f06070;
509 w[3] = insn->control ? insn->control : 0x001fda00;
510 break;
511
512 case BW_IR_LOP3_XOR:
513 /* LOP3.LUT Rd, Ra, Rb, RZ, 0x3c, !PT. Checked with nvdisasm -b SM121. */
514 w[0] = 0x7212 | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24);
515 w[1] = (uint32_t)(src2 & 0xff);
516 w[2] = 0x078e3cff;
517 w[3] = insn->control ? insn->control : 0x001fca00;
518 break;
519
520 case BW_IR_FADD:
521 /* FADD Rd, Ra, Rb */
522 w[0] = 0x7221U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24);
523 w[1] = (uint32_t)(src2 & 0xff);
524 w[2] = 0x00000000;
525 w[3] = insn->control ? insn->control : 0x004fc400;
526 break;
527
528 case BW_IR_FSUB:
529 /* FSUB Rd, Ra, Rb (FADD Rd, Ra, -Rb) */
530 w[0] = 0x7221U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24);
531 w[1] = (uint32_t)(src2 & 0xff) | 0x80000000U;
532 w[2] = 0x00000000;
533 w[3] = insn->control ? insn->control : 0x004fc400;
534 break;
535
536 case BW_IR_FMUL:
537 /* FMUL Rd, Ra, Rb */
538 w[0] = 0x7220U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24);
539 w[1] = (uint32_t)(src2 & 0xff);
540 w[2] = 0x00400000;
541 w[3] = insn->control ? insn->control : 0x004fc400;
542 break;
543
544 case BW_IR_FFMA:
545 /* FFMA Rd, Ra, Rb, Rc */
546 w[0] = 0x7223U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24);
547 w[1] = (uint32_t)(src2 & 0xff);
548 w[2] = (uint32_t)(src3 & 0xff);
549 w[3] = insn->control ? insn->control : 0x004fc400;
550 break;
551
552 case BW_IR_FSETP:
553 /* FSETP.cond.AND P0, PT, Ra, Rb, PT */
554 w[0] = 0x720bU | ((uint32_t)(dst & 0x7) << 16) | ((uint32_t)(src1 & 0xff) << 24);
555 w[1] = (uint32_t)(src2 & 0xff);
556 w[2] = insn->imm ? insn->imm : 0x03f06000; /* Default GE condition */
557 w[3] = insn->control ? insn->control : 0x004fc400;
558 break;
559
560 case BW_IR_FSEL:
561 /* FSEL Rd, Ra, Rb, P0 */
562 w[0] = 0x7208U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24);
563 w[1] = (uint32_t)(src2 & 0xff);
564 w[2] = (uint32_t)(src3 & 0x7);
565 w[3] = insn->control ? insn->control : 0x000fca00;
566 break;
567
568 case BW_IR_FMNMX_MIN:
569 /* FMNMX Rd, Ra, Rb, PT (minimum) */
570 w[0] = 0x7209U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24);
571 w[1] = (uint32_t)(src2 & 0xff);
572 w[2] = 0x03800000;
573 w[3] = insn->control ? insn->control : 0x004fc400;
574 break;
575
576 case BW_IR_FMNMX_MAX:
577 /* FMNMX Rd, Ra, Rb, !PT (maximum) */
578 w[0] = 0x7209U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24);
579 w[1] = (uint32_t)(src2 & 0xff);
580 w[2] = 0x07800000;
581 w[3] = insn->control ? insn->control : 0x004fc400;
582 break;
583
584 case BW_IR_I2FP:
585 /* I2FP.F32.S32 Rd, Ra */
586 w[0] = 0x7245U | ((uint32_t)(dst & 0xff) << 16);
587 w[1] = (uint32_t)(src1 & 0xff);
588 w[2] = 0x00201400;
589 w[3] = insn->control ? insn->control : 0x004fe200;
590 break;
591
592 case BW_IR_F2I:
593 /* F2I.TRUNC.NTZ Rd, Ra */
594 w[0] = 0x7305U | ((uint32_t)(dst & 0xff) << 16);
595 w[1] = (uint32_t)(src1 & 0xff);
596 w[2] = 0x0020f100;
597 w[3] = insn->control ? insn->control : 0x004e2200;
598 break;
599
600 case BW_IR_MUFU_RCP:
601 /* MUFU.RCP Rd, Ra */
602 w[0] = 0x7308U | ((uint32_t)(dst & 0xff) << 16);
603 w[1] = (uint32_t)(src1 & 0xff);
604 w[2] = 0x00001000;
605 w[3] = insn->control ? insn->control : 0x000e2400;
606 break;
607
608 case BW_IR_MUFU_LG2:
609 /* MUFU.LG2 Rd, Ra (nvdisasm 13.0 -b SM121 decodes sub-op 0x0c00 as MUFU.LG2) */
610 w[0] = 0x7308U | ((uint32_t)(dst & 0xff) << 16);
611 w[1] = (uint32_t)(src1 & 0xff);
612 w[2] = 0x00000c00;
613 w[3] = insn->control ? insn->control : 0x000e2400;
614 break;
615
616 case BW_IR_MUFU_RSQ:
617 /* MUFU.RSQ Rd, Ra */
618 w[0] = 0x7308U | ((uint32_t)(dst & 0xff) << 16);
619 w[1] = (uint32_t)(src1 & 0xff);
620 w[2] = 0x00001400;
621 w[3] = insn->control ? insn->control : 0x000e2400;
622 break;
623
624 case BW_IR_SHFL_DOWN:
625 /* SHFL.DOWN PT, Rd, Ra, offset, 0x1f */
626 w[0] = 0x7f89U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24);
627 w[1] = 0x08001f00U | (((uint32_t)insn->imm & 0x1f) << 21);
628 w[2] = 0x000e0000;
629 w[3] = insn->control ? insn->control : 0x000e2400;
630 break;
631
632 case BW_IR_SHFL_BFLY:
633 /* SHFL.BFLY PT, Rd, Ra, lane, 0x1f: SHFL.DOWN with bit 58 (w[1] bit 26) set
634 * (omega_blackwell_verify_codegen_fixtures_bfly_f2fp) */
635 w[0] = 0x7f89U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24);
636 w[1] = 0x0c001f00U | (((uint32_t)insn->imm & 0x1f) << 21);
637 w[2] = 0x000e0000;
638 w[3] = insn->control ? insn->control : 0x000e2400;
639 break;
640
641 case BW_IR_F2FP_BF16_PACK:
642 /* F2FP.BF16.F32.PACK_AB Rd, Ra, Rb (omega_blackwell_verify_codegen_fixtures_bfly_f2fp). The default control
643 * is FMNMX's: the kernels emitted this as a patched FMNMX_MAX before R5 and their bytes must not move. */
644 w[0] = 0x723eU | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24);
645 w[1] = (uint32_t)(src2 & 0xff);
646 w[2] = 0x000010ff;
647 w[3] = insn->control ? insn->control : 0x004fc400;
648 break;
649
650 case BW_IR_LDS:
651 /* LDS Rd, [Ra] */
652 w[0] = 0x7984U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24);
653 w[1] = 0x00000000;
654 w[2] = (uint32_t)(insn->imm & 0xffff);
655 w[3] = insn->control ? insn->control : 0x000fe200;
656 break;
657
658 case BW_IR_STS:
659 /* STS [Ra], Rb */
660 w[0] = 0x7388U | ((uint32_t)(src1 & 0xff) << 24);
661 w[1] = (uint32_t)(src2 & 0xff);
662 w[2] = (uint32_t)(insn->imm & 0xffff);
663 w[3] = insn->control ? insn->control : 0x000fe200;
664 break;
665
666 case BW_IR_EXIT:
667 w[0] = insn->predicate_p0 ? 0x0000094d : 0x0000794d;
668 w[1] = 0x00000000;
669 w[2] = 0x03800000;
670 w[3] = insn->control ? insn->control : 0x000fea00;
671 break;
672
673 case BW_IR_BRA: {
674 /* sm_121 BRA. Distance is (target - this) - 1, split across the
675 * low six bits at instruction bits 18..23 and the rest at bit 34.
676 * Predicate bits 12..15: 7 = always, 0 = @P0, 8 = @!P0.
677 * A zero delta with no predicate keeps the self-branch word the
678 * existing kernels already emit after EXIT. */
679 int32_t delta = (int32_t)insn->imm;
680 int32_t rel = delta - 1;
681 uint32_t pred_field = 0x7u;
682 if (insn->predicate_p0)
683 pred_field = insn->predicate_not ? 0x8u : 0x0u;
684 w[0] = 0x947u | (pred_field << 12) | (((uint32_t)rel & 0x3fu) << 18);
685 w[1] = (uint32_t)(rel >> 6) << 2;
686 w[2] = (rel < 0) ? 0x0383ffffu : 0x03800000u;
687 if (insn->control)
688 w[3] = insn->control;
689 else if (delta == 0 && !insn->predicate_p0)
690 w[3] = 0x000fc000u;
691 else
692 w[3] = 0x000fea00u;
693 break;
694 }
695
696 case BW_IR_BSSY: {
697 /* BSSY.RECONVERGENT Bn, target (omega #308). nvcc 13.0.88 -arch=sm_121:
698 * [0070] BSSY.RECONVERGENT B0, 0x140 ; 0x000000c000007945 0x000fe20003800200
699 * opcode 0x945 (NAK sm70_encode.rs:4165), barrier id at bits 16..19 (set_bar_dst
700 * 16..20), guard predicate at bits 12..15 like BRA, relative byte offset
701 * target - (this + 16) at bit 32 (NAK: field 34.. in 4-byte units), the cond
702 * predicate PT at bits 87..89 (0x0380 << 16 in w[2]) and bit 73 (0x200 in w[2]),
703 * which the disassembler prints as .RECONVERGENT; nvcc sets it on every BSSY/BSYNC
704 * for sm_121 and Mesa NAK does not. We follow nvcc. The target is the instruction
705 * after the matching BSYNC, so the delta is at least 2; smaller is refused. */
706 int32_t delta = (int32_t)insn->imm;
707 if (delta < 2 || insn->bar_reg >= BW_RECONV_MAX_BAR) return -1;
708 uint32_t pred_field = 0x7u;
709 if (insn->predicate_p0)
710 pred_field = insn->predicate_not ? 0x8u : 0x0u;
711 w[0] = 0x945u | (pred_field << 12) | ((uint32_t)insn->bar_reg << 16);
712 w[1] = (uint32_t)(delta - 1) * 16u;
713 w[2] = 0x03800200u;
714 w[3] = insn->control ? insn->control : 0x000fe200u;
715 break;
716 }
717
718 case BW_IR_BSYNC: {
719 /* BSYNC.RECONVERGENT Bn: [0130] BSYNC.RECONVERGENT B0 ; 0x0000000000007941 0x000fea0003800200
720 * opcode 0x941 (NAK sm70_encode.rs:4179), barrier id bits 16..19, cond PT, bit 73. */
721 if (insn->bar_reg >= BW_RECONV_MAX_BAR) return -1;
722 uint32_t pred_field = 0x7u;
723 if (insn->predicate_p0)
724 pred_field = insn->predicate_not ? 0x8u : 0x0u;
725 w[0] = 0x941u | (pred_field << 12) | ((uint32_t)insn->bar_reg << 16);
726 w[1] = 0;
727 w[2] = 0x03800200u;
728 w[3] = insn->control ? insn->control : 0x000fea00u;
729 break;
730 }
731
732 case BW_IR_MUFU_EX2:
733 /* MUFU.EX2 Rd, Ra. nvdisasm 13.0 -b SM121: 0x7308 with sub-op 0x0800 decodes
734 * "MUFU.EX2 R2, R4" (RCP is 0x1000, RSQ 0x1400 above). */
735 w[0] = 0x7308U | ((uint32_t)(dst & 0xff) << 16);
736 w[1] = (uint32_t)(src1 & 0xff);
737 w[2] = 0x00000800;
738 w[3] = insn->control ? insn->control : 0x000e2400;
739 break;
740
741 case BW_IR_BAR_SYNC:
742 /* BAR.SYNC.DEFER_BLOCKING 0x0: the exact words src/omega_numeric.c:885 runs on
743 * the chip (nvdisasm: "BAR.SYNC.DEFER_BLOCKING 0x0"). No registers. */
744 w[0] = 0x00007b1dU;
745 w[1] = 0x00000000;
746 w[2] = 0x00010000;
747 w[3] = insn->control ? insn->control : 0x000fec00;
748 break;
749
750 case BW_IR_LDS128: {
751 /* LDS.128 Rd..Rd+3, [Ra+imm]. nvcc 13.0.88 -arch=sm_121 words (fixtures below):
752 * LDS.128 R4, [R20] ; 0x0000000014047984 0x000fe80000000c00
753 * LDS.128 R8, [R20+0x40] ; 0x0000400014087984 0x000e280000000c00
754 * imm = byte offset at w[1] bits 8..31; size field 6 (128) at w[2] bits 9..11. */
755 if (insn->imm >= (1u << 23)) return -1;
756 w[0] = 0x7984U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24);
757 w[1] = (insn->imm & 0xffffffU) << 8;
758 w[2] = 0x00000c00U;
759 w[3] = insn->control ? insn->control : 0x000fe800;
760 break;
761 }
762
763 case BW_IR_LDS32:
764 /* LDS Rd, [Ra+URZ] (32-bit), the src/omega_numeric.c:886 form. */
765 w[0] = 0x7984U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24);
766 w[1] = 0x000000ff;
767 w[2] = 0x08000800;
768 w[3] = insn->control ? insn->control : 0x000e2800;
769 break;
770
771 case BW_IR_STS32:
772 /* STS [Ra+URZ], Rb (32-bit), the src/omega_numeric.c:884 form. */
773 w[0] = 0x7988U | ((uint32_t)(src1 & 0xff) << 24);
774 w[1] = (uint32_t)(src2 & 0xff);
775 w[2] = 0x080008ffU;
776 w[3] = insn->control ? insn->control : 0x000fe200;
777 break;
778
779 /* Prime race cut (2026-10-05). An absent operand is RZ for these three ops. */
780 case BW_IR_LOP3_LUT: {
781 /* LOP3.LUT Rd, Ra, Rb, Rc, imm8, !PT. LOP3_XOR above is this form with Rc = RZ, LUT 0x3c. */
782 uint32_t a = (insn->src1_vreg >= 0) ? (uint32_t)(src1 & 0xff) : 0xffU;
783 uint32_t b = (insn->src2_vreg >= 0) ? (uint32_t)(src2 & 0xff) : 0xffU;
784 w[0] = 0x7212U | ((uint32_t)(dst & 0xff) << 16) | (a << 24);
785 w[1] = b;
786 w[2] = 0x078e0000U | ((insn->imm & 0xffU) << 8) | (uint32_t)(src3 & 0xff);
787 w[3] = insn->control ? insn->control : 0x001fca00;
788 break;
789 }
790
791 case BW_IR_SHF_L_U32: {
792 /* SHF.L.U32 Rd, Ra, Rb, Rc: register-amount form (0x7219) of SHF_R's 0x7819, with the
793 * .R (w[2] bit 12) and .HI (bit 16) flags clear. Rc is the funnel high word (RZ). */
794 uint32_t a = (insn->src1_vreg >= 0) ? (uint32_t)(src1 & 0xff) : 0xffU;
795 uint32_t b = (insn->src2_vreg >= 0) ? (uint32_t)(src2 & 0xff) : 0xffU;
796 w[0] = 0x7219U | ((uint32_t)(dst & 0xff) << 16) | (a << 24);
797 w[1] = b;
798 w[2] = 0x00000600U | (uint32_t)(src3 & 0xff);
799 w[3] = insn->control ? insn->control : 0x001fca00;
800 break;
801 }
802
803 case BW_IR_IMAD_HI_U32: {
804 /* IMAD.HI.U32 Rd, Ra, Rb, Rc: opcode 0x227 with the IMAD.WIDE.U32 modifier word. */
805 uint32_t a = (insn->src1_vreg >= 0) ? (uint32_t)(src1 & 0xff) : 0xffU;
806 uint32_t b = (insn->src2_vreg >= 0) ? (uint32_t)(src2 & 0xff) : 0xffU;
807 w[0] = 0x7227U | ((uint32_t)(dst & 0xff) << 16) | (a << 24);
808 w[1] = b;
809 w[2] = 0x078e0000U | (uint32_t)(src3 & 0xff);
810 w[3] = insn->control ? insn->control : 0x001fca00;
811 break;
812 }
813
814 default:
815 return -1;
816 }
817 return 0;
818}
819#endif /* C encoder, migration-pending */
820
821#ifdef BW_ENC_C_PRIMARY
822static int encode_single_insn(const BlackwellIRInsn *insn, const OmegaRegAlloc *ra, uint32_t w[4]) {
823 return bw_c_encode_insn(insn, ra, w);
824}
825#else
826#include "omega_native.h"
827/* The .om unit reads the IR as 8 ulongs per instruction and the allocation as one 288-entry table; these are the
828 * layouts its source documents (om/encoder/bw_enc_sm121.om), so the enum values and sizes are pinned here. */
829#define BW_OM_INS_LEN 8192u /* BW_MAX_IR_INSNS * 8 */
830#define BW_OM_PHYS_LEN 288u /* BW_MAX_VREGS + BW_MAX_UVREGS */
831#define BW_OM_CODE_LEN 16384u /* BW_MAX_IR_INSNS * 16 */
832_Static_assert(BW_MAX_IR_INSNS == 1024 && BW_MAX_VREGS == 256 && BW_MAX_UVREGS == 32, "bw_enc_sm121.om array sizes");
833_Static_assert(BW_IR_NOP == 0 && BW_IR_MOV_RZ == 1 && BW_IR_MOV_IMM == 2 && BW_IR_S2R == 3 && BW_IR_LDC == 4 &&
834 BW_IR_LDC64 == 5 && BW_IR_LDCU == 6 && BW_IR_LDCU64 == 7 && BW_IR_IMAD == 8 && BW_IR_IMAD_WIDE == 9 &&
835 BW_IR_ISETP_GE == 10 && BW_IR_LDG_E == 11 && BW_IR_STG_E == 12 && BW_IR_STG_EF == 13 &&
836 BW_IR_IADD3 == 14 && BW_IR_HMMA_F16 == 15 && BW_IR_HMMA_BF16 == 16 && BW_IR_SHF_R == 17 &&
837 BW_IR_LOP3_AND == 18 && BW_IR_LDG_E_U16 == 19 && BW_IR_LDG_STRONG_SYS == 20 && BW_IR_LDG_MMIO == 21 &&
838 BW_IR_STG_STRONG_SYS == 22 && BW_IR_MEMBAR_ALL_SYS == 23 && BW_IR_MEMBAR_SC_SYS == 24 &&
839 BW_IR_CCTL_IVALL == 25 && BW_IR_ATOMG_ADD_STRONG_SYS == 26 && BW_IR_ATOMG_EXCH_STRONG_SYS == 27 &&
840 BW_IR_ISETP_GE_U32 == 28 && BW_IR_LOP3_XOR == 29, "bw_enc_sm121.om opcode numbers 0-29");
841_Static_assert(BW_IR_FADD == 30 && BW_IR_FSUB == 31 && BW_IR_FMUL == 32 && BW_IR_FFMA == 33 && BW_IR_FSETP == 34 &&
842 BW_IR_FSEL == 35 && BW_IR_FMNMX_MIN == 36 && BW_IR_FMNMX_MAX == 37 && BW_IR_I2FP == 38 &&
843 BW_IR_F2I == 39 && BW_IR_MUFU_RCP == 40 && BW_IR_MUFU_RSQ == 41 && BW_IR_SHFL_DOWN == 42 &&
844 BW_IR_LDS == 43 && BW_IR_STS == 44 && BW_IR_EXIT == 45 && BW_IR_BRA == 46 && BW_IR_MUFU_EX2 == 47 &&
845 BW_IR_BAR_SYNC == 48 && BW_IR_LDS32 == 49 && BW_IR_STS32 == 50 && BW_IR_LOP3_LUT == 51 &&
846 BW_IR_SHF_L_U32 == 52 && BW_IR_IMAD_HI_U32 == 53 && BW_IR_BSSY == 54 && BW_IR_BSYNC == 55 &&
847 BW_IR_LDG_E_64 == 56 && BW_IR_LDG_E_128 == 57 && BW_IR_LDS128 == 58 && BW_IR_MUFU_LG2 == 59,
848 "bw_enc_sm121.om opcode numbers 30-59");
849_Static_assert(BW_IR_SHFL_BFLY == 60 && BW_IR_F2FP_BF16_PACK == 61 && BW_IR_ATOMG_ADD_F32_STRONG_SYS == 62 &&
850 BW_IR_ATOMG_ADD_F32_STRONG_GPU == 63, "bw_enc_sm121.om opcode numbers 60-63");
851
852/* Encodes n instructions through the .om unit into code (BW_OM_CODE_LEN bytes). pad != 0 is program mode (NOP
853 * padding to a multiple of 8, at least 32). Returns the bytes written; 0 = refused. */
854static size_t bw_om_encode(const BlackwellIRInsn *insns, size_t n, const OmegaRegAlloc *ra, int pad, uint8_t *code) {
855 if (n > BW_MAX_IR_INSNS) return 0;
856 uint64_t ins[BW_OM_INS_LEN], phys[BW_OM_PHYS_LEN];
857 for (size_t i = 0; i < n; i++) {
858 const BlackwellIRInsn *in = &insns[i];
859 uint64_t *r = &ins[i * 8];
860 r[0] = (uint64_t)(int64_t)(int)in->op;
861 r[1] = (uint64_t)(int64_t)in->dst_vreg;
862 r[2] = (uint64_t)(int64_t)in->src1_vreg;
863 r[3] = (uint64_t)(int64_t)in->src2_vreg;
864 r[4] = (uint64_t)(int64_t)in->src3_vreg;
865 r[5] = (uint64_t)(int64_t)in->ureg;
866 r[6] = (uint64_t)in->imm | (uint64_t)in->control << 32;
867 r[7] = (uint64_t)in->is_uniform | (uint64_t)in->predicate_p0 << 1 | (uint64_t)in->predicate_not << 2 |
868 (uint64_t)in->bar_reg << 8 | (uint64_t)in->dst_subreg << 16 | (uint64_t)in->src1_subreg << 24 |
869 (uint64_t)in->src2_subreg << 32 | (uint64_t)in->src3_subreg << 40;
870 }
871 for (size_t i = n * 8; i < BW_OM_INS_LEN; i++) ins[i] = 0;
872 for (int i = 0; i < BW_MAX_VREGS; i++) phys[i] = (uint64_t)(int64_t)ra->vreg_to_phys[i];
873 for (int i = 0; i < BW_MAX_UVREGS; i++) phys[BW_MAX_VREGS + i] = (uint64_t)(int64_t)ra->uvreg_to_phys[i];
874 uint64_t bytes = 0;
875 if (omega_bw_enc_sm121(&bytes, ins, BW_OM_INS_LEN, phys, BW_OM_PHYS_LEN, (uint64_t)n, pad ? 1u : 0u, code,
876 BW_OM_CODE_LEN) != OMEGA_OK)
877 return 0;
878 return (size_t)bytes;
879}
880
881#ifdef CUDAPP_MIGRATION_PENDING_ENCODER_DIFF
882/* One line per agreement, appended; any disagreement aborts the process after naming it. */
883static void bw_enc_diff_note(const char *what, size_t insns, const uint8_t *code, size_t len, int agree) {
884 char hex[17] = "-";
885 if (len) {
886 uint8_t d[SHA256_DIGEST_SIZE]; sha256_hash(code, len, d);
887 for (int i = 0; i < 8; i++) snprintf(hex + 2 * i, 3, "%02x", (unsigned)d[i]);
888 }
889 const char *path = getenv("CUDAPP_ENCODER_DIFF_LOG");
890 if (path && path[0]) {
891 int fd = open(path, O_WRONLY | O_CREAT | O_APPEND | O_CLOEXEC, 0600);
892 if (fd >= 0) {
893 char line[160];
894 int k = snprintf(line, sizeof line, "%s insns=%zu bytes=%zu sha16=%s %s\n", what, insns, len, hex,
895 agree ? "SAME" : "DIFFER");
896 if (k > 0 && write(fd, line, (size_t)k) != k) BW_DIAG("encoder diff: short log write\n");
897 close(fd);
898 }
899 }
900 if (!agree) {
901 BW_DIAG("ENCODER DIFF: .om and C encoders disagree (%s, %zu insns)\n", what, insns);
902 abort();
903 }
904}
905#endif
906
907static int encode_single_insn(const BlackwellIRInsn *insn, const OmegaRegAlloc *ra, uint32_t w[4]) {
908 uint8_t code[BW_OM_CODE_LEN];
909 int rc = bw_om_encode(insn, 1, ra, 0, code) == 16 ? 0 : -1;
910 memset(w, 0, 16);
911 if (rc == 0)
912 for (int k = 0; k < 4; k++)
913 w[k] = (uint32_t)code[4 * k] | (uint32_t)code[4 * k + 1] << 8 | (uint32_t)code[4 * k + 2] << 16 |
914 (uint32_t)code[4 * k + 3] << 24;
915#ifdef CUDAPP_MIGRATION_PENDING_ENCODER_DIFF
916 uint32_t c[4];
917 int crc = bw_c_encode_insn(insn, ra, c);
918 bw_enc_diff_note("insn", 1, rc == 0 ? code : NULL, rc == 0 ? 16 : 0,
919 crc == rc && (rc != 0 || memcmp(c, w, 16) == 0));
920#endif
921 return rc;
922}
923#endif
924
925/* Prime race cut (2026-10-05): golden words for LOP3_LUT, SHF_L_U32 and IMAD_HI_U32.
926 * Each expected (w0, w1, w2) is the low 96 bits of an instruction nvcc 13.0.88 emitted
927 * for sm_121 (nvcc -arch=sm_121 -cubin, cuobjdump -sass; source and dump in
928 * bench/prime_race/tests/encoding_oracle.md). Control words (w3) are scheduler output and
929 * not compared. nvcc is an offline oracle only; nothing here links CUDA. 0 = all good. */
930int omega_blackwell_verify_codegen_fixtures_intops(void) {
931 OmegaRegAlloc ra;
932 memset(&ra, 0, sizeof(ra));
933 static const int phys[] = { 0, 2, 6, 7, 9, 11, 13 };
934 for (int i = 0; i < (int)(sizeof phys / sizeof phys[0]); i++) ra.vreg_to_phys[i] = phys[i];
935 enum { R0 = 0, R2, R6, R7, R9, R11, R13, RZ = -1 };
936 static const struct {
937 BlackwellIROpcode op; int d, a, b, c; uint32_t imm; uint32_t w0, w1, w2; const char *text;
938 } fx[] = {
939 { BW_IR_LOP3_LUT, R9, R7, R0, RZ, 0xfc, 0x07097212U, 0x00000000U, 0x078efcffU, "LOP3.LUT R9, R7, R0, RZ, 0xfc, !PT" },
940 { BW_IR_LOP3_LUT, R11, R6, R7, R0, 0x78, 0x060b7212U, 0x00000007U, 0x078e7800U, "LOP3.LUT R11, R6, R7, R0, 0x78, !PT" },
941 { BW_IR_LOP3_LUT, R13, R6, R7, R0, 0x10, 0x060d7212U, 0x00000007U, 0x078e1000U, "LOP3.LUT R13, R6, R7, R0, 0x10, !PT" },
942 { BW_IR_LOP3_LUT, R7, RZ, R9, RZ, 0x33, 0xff077212U, 0x00000009U, 0x078e33ffU, "LOP3.LUT R7, RZ, R9, RZ, 0x33, !PT" },
943 { BW_IR_SHF_L_U32, R7, R9, R2, RZ, 0, 0x09077219U, 0x00000002U, 0x000006ffU, "SHF.L.U32 R7, R9, R2, RZ" },
944 { BW_IR_SHF_L_U32, R7, R0, R7, RZ, 0, 0x00077219U, 0x00000007U, 0x000006ffU, "SHF.L.U32 R7, R0, R7, RZ" },
945 { BW_IR_IMAD_HI_U32, R9, R9, R0, R6, 0, 0x09097227U, 0x00000000U, 0x078e0006U, "IMAD.HI.U32 R9, R9, R0, R6" },
946 { BW_IR_IMAD_HI_U32, R7, R0, R7, RZ, 0, 0x00077227U, 0x00000007U, 0x078e00ffU, "IMAD.HI.U32 R7, R0, R7, RZ" },
947 };
948 for (int i = 0; i < (int)(sizeof fx / sizeof fx[0]); i++) {
949 BlackwellIRInsn in;
950 memset(&in, 0, sizeof in);
951 in.op = fx[i].op; in.dst_vreg = fx[i].d; in.src1_vreg = fx[i].a; in.src2_vreg = fx[i].b;
952 in.src3_vreg = fx[i].c; in.ureg = -1; in.imm = fx[i].imm;
953 uint32_t w[4];
954 if (encode_single_insn(&in, &ra, w) != 0) return -(2 * i + 1);
955 if (w[0] != fx[i].w0 || w[1] != fx[i].w1 || w[2] != fx[i].w2) return -(2 * i + 2);
956 }
957 return 0;
958}
959
960int omega_blackwell_encode_one(const BlackwellIRInsn *insn, uint32_t w[4]) {
961 if (!insn || !w) return -1;
962 OmegaRegAlloc ra;
963 memset(&ra, 0, sizeof ra);
964 for (int i = 0; i < BW_MAX_VREGS; i++) ra.vreg_to_phys[i] = i;
965 for (int i = 0; i < BW_MAX_UVREGS; i++) ra.uvreg_to_phys[i] = i;
966 return encode_single_insn(insn, &ra, w);
967}
968
969/* omega #328: golden words for the fragment-major matmul. Oracle: nvcc 13.0.88 -arch=sm_121
970 * -cubin -O3, cuobjdump -sass, on a kernel with uint2 / uint4 loads at immediate offsets and a
971 * dependent mma.m16n8k16 bf16 chain (source and dump sealed in
972 * ~/workspace/investigations/2026-10-07-qwen3-decode-matmul/oracle/frag_plain.{cu,sass}):
973 * LDG.E.64 R2, desc[UR8][R30.64] ; 0x000000081e027981 0x000ea8000c1e1b00
974 * LDG.E.64 R28, desc[UR8][R30.64+0x100] ; 0x000100081e1c7981 0x000ee8000c1e1b00
975 * LDG.E.128 R8, desc[UR8][R32.64] ; 0x0000000820087981 0x000ea8000c1e1d00
976 * LDG.E.128 R20, desc[UR8][R32.64+0x1000] ; 0x0010000820147981 0x000e9e000c1e1d00
977 * HMMA.16816.F32.BF16 R8, R12, R6, R8 ; 0x000000060c08723c 0x010fe20000041808
978 * NOP ; 0x0000000000007918 0x000fdc0000000000
979 * The dependent HMMA chain nvcc emits is HMMA (stall 15) then NOP (stall 14): 29 cycles from
980 * one HMMA to the next that reads its accumulator. w3 is compared for the NOP only (it is the
981 * whole point of that instruction). 0 = all good. */
982int omega_blackwell_verify_codegen_fixtures_fragmm(void) {
983 static const struct {
984 BlackwellIROpcode op; int d, a, b, c, u; uint32_t imm, control; uint32_t w0, w1, w2, w3; const char *text;
985 } fx[] = {
986 { BW_IR_LDG_E_64, 2, 30, -1, -1, 8, 0, 0, 0x1e027981U, 0x00000008U, 0x0c1e1b00U, 0, "LDG.E.64 R2, desc[UR8][R30.64]" },
987 { BW_IR_LDG_E_64, 28, 30, -1, -1, 8, 0x100, 0, 0x1e1c7981U, 0x00010008U, 0x0c1e1b00U, 0, "LDG.E.64 R28, desc[UR8][R30.64+0x100]" },
988 { BW_IR_LDG_E_128, 8, 32, -1, -1, 8, 0, 0, 0x20087981U, 0x00000008U, 0x0c1e1d00U, 0, "LDG.E.128 R8, desc[UR8][R32.64]" },
989 { BW_IR_LDG_E_128, 20, 32, -1, -1, 8, 0x1000, 0, 0x20147981U, 0x00100008U, 0x0c1e1d00U, 0, "LDG.E.128 R20, desc[UR8][R32.64+0x1000]" },
990 { BW_IR_HMMA_BF16, 8, 12, 6, 8, -1, 0, 0, 0x0c08723cU, 0x00000006U, 0x00041808U, 0, "HMMA.16816.F32.BF16 R8, R12, R6, R8" },
991 { BW_IR_NOP, -1, -1, -1, -1, -1, 0, 0x000fdc00U, 0x00007918U, 0, 0, 0x000fdc00U, "NOP (stall 14)" },
992 /* LDS.128 with an immediate offset (attention batched loads, 2026-10-12): nvcc 13.0.88 -arch=sm_121 on
993 * float4 shared reads at register + constant offsets (fixture source in the omega speed/attention-batched-loads PR) */
994 { BW_IR_LDS128, 4, 20, -1, -1, -1, 0, 0, 0x14047984U, 0x00000000U, 0x00000c00U, 0, "LDS.128 R4, [R20]" },
995 { BW_IR_LDS128, 8, 20, -1, -1, -1, 0x40, 0, 0x14087984U, 0x00004000U, 0x00000c00U, 0, "LDS.128 R8, [R20+0x40]" },
996 { BW_IR_LDS128, 12, 20, -1, -1, -1, 0x100, 0, 0x140c7984U, 0x00010000U, 0x00000c00U, 0, "LDS.128 R12, [R20+0x100]" },
997 /* STG.E with an immediate offset (blocked prefill matmul, 2026-10-12): nvcc 13.0.88 -arch=sm_121 on float stores
998 * at register + constant offsets (~/.claude/jobs/ade3c837/tmp/stg-oracle/stg.cu); the second word is the
999 * offset-free STG word with UR4 */
1000 { BW_IR_STG_E, -1, 4, 8, -1, 4, 0, 0, 0x04007986U, 0x00000008U, 0x0c101904U, 0, "STG.E desc[UR4][R4.64], R8" },
1001 { BW_IR_STG_E, -1, 4, 9, -1, 4, 0x4, 0, 0x04007986U, 0x00000409U, 0x0c101904U, 0, "STG.E desc[UR4][R4.64+0x4], R9" },
1002 { BW_IR_STG_E, -1, 4, 10, -1, 4, 0x4000, 0, 0x04007986U, 0x0040000aU, 0x0c101904U, 0, "STG.E desc[UR4][R4.64+0x4000], R10" },
1003 { BW_IR_STG_E, -1, 6, 11, -1, 4, 0xfff00000u, 0, 0x06007986U, 0xf000000bU, 0x0c101904U, 0, "STG.E desc[UR4][R6.64+-0x100000], R11" },
1004 };
1005 for (int i = 0; i < (int)(sizeof fx / sizeof fx[0]); i++) {
1006 BlackwellIRInsn in;
1007 memset(&in, 0, sizeof in);
1008 in.op = fx[i].op; in.dst_vreg = fx[i].d; in.src1_vreg = fx[i].a; in.src2_vreg = fx[i].b;
1009 in.src3_vreg = fx[i].c; in.ureg = fx[i].u; in.imm = fx[i].imm; in.control = fx[i].control;
1010 uint32_t w[4];
1011 if (omega_blackwell_encode_one(&in, w) != 0) return -(2 * i + 1);
1012 if (w[0] != fx[i].w0 || w[1] != fx[i].w1 || w[2] != fx[i].w2 || (fx[i].w3 && w[3] != fx[i].w3)) return -(2 * i + 2);
1013 }
1014 /* offsets outside the signed 24-bit field are refused, not wrapped */
1015 BlackwellIRInsn big;
1016 memset(&big, 0, sizeof big);
1017 big.op = BW_IR_LDG_E_64; big.dst_vreg = 2; big.src1_vreg = 30; big.src2_vreg = -1; big.src3_vreg = -1; big.ureg = 8;
1018 big.imm = 1u << 23;
1019 uint32_t w[4];
1020 if (omega_blackwell_encode_one(&big, w) == 0) return -100;
1021 big.imm = (uint32_t)-(1 << 23);
1022 if (omega_blackwell_encode_one(&big, w) != 0) return -101;
1023 return 0;
1024}
1025
1026/* omega #308: BSSY / BSYNC golden words. Oracle: nvcc 13.0.88 -arch=sm_121 -cubin -O3 on the
1027 * kernels in docs/gpu-reconvergence-308.md, cuobjdump -sass (low 64-bit word, then high; the
1028 * top 23 bits of the high word are the control word and are not compared here):
1029 * k_loop [0070] BSSY.RECONVERGENT B0, 0x140 ; 0x000000c000007945 0x000fe20003800200 (delta 13)
1030 * k_nested [0070] BSSY.RECONVERGENT B0, 0x300 ; 0x0000028000007945 0x000fe20003800200 (delta 41)
1031 * k_break [0070] BSSY.RECONVERGENT B0, 0x110 ; 0x0000009000007945 0x000fe20003800200 (delta 10)
1032 * k_loop [0130] BSYNC.RECONVERGENT B0 ; 0x0000000000007941 0x000fea0003800200
1033 * Barrier ids other than B0 and the predicated forms are decoded by tools/reconv_nvdisasm_check.sh
1034 * (nvdisasm -b SM121 text), not by this table. */
1035int omega_blackwell_verify_codegen_fixtures_reconv(void) {
1036 static const struct { BlackwellIROpcode op; int delta; int bar; uint32_t w0, w1, w2; } fx[] = {
1037 { BW_IR_BSSY, 13, 0, 0x00007945U, 0x000000c0U, 0x03800200U },
1038 { BW_IR_BSSY, 41, 0, 0x00007945U, 0x00000280U, 0x03800200U },
1039 { BW_IR_BSSY, 10, 0, 0x00007945U, 0x00000090U, 0x03800200U },
1040 { BW_IR_BSYNC, 0, 0, 0x00007941U, 0x00000000U, 0x03800200U },
1041 };
1042 for (int i = 0; i < (int)(sizeof fx / sizeof fx[0]); i++) {
1043 BlackwellIRInsn in;
1044 memset(&in, 0, sizeof in);
1045 in.op = fx[i].op; in.dst_vreg = -1; in.src1_vreg = -1; in.src2_vreg = -1; in.src3_vreg = -1; in.ureg = -1;
1046 in.imm = (uint32_t)fx[i].delta; in.bar_reg = (uint8_t)fx[i].bar;
1047 uint32_t w[4];
1048 if (omega_blackwell_encode_one(&in, w) != 0) return -(2 * i + 1);
1049 if (w[0] != fx[i].w0 || w[1] != fx[i].w1 || w[2] != fx[i].w2) return -(2 * i + 2);
1050 /* control word defaults mirror nvcc's: BSSY 0x000fe200, BSYNC 0x000fea00 */
1051 if (w[3] != (fx[i].op == BW_IR_BSSY ? 0x000fe200U : 0x000fea00U)) return -(2 * i + 2);
1052 }
1053 /* refusals: a BSSY must name a later instruction (delta >= 2), barrier ids stop at B15 */
1054 BlackwellIRInsn bad; uint32_t w[4];
1055 memset(&bad, 0, sizeof bad); bad.op = BW_IR_BSSY; bad.dst_vreg = bad.src1_vreg = bad.src2_vreg = bad.src3_vreg = bad.ureg = -1;
1056 bad.imm = 1; bad.bar_reg = 0;
1057 if (omega_blackwell_encode_one(&bad, w) == 0) return -101;
1058 bad.imm = (uint32_t)-3;
1059 if (omega_blackwell_encode_one(&bad, w) == 0) return -102;
1060 bad.imm = 5; bad.bar_reg = 16;
1061 if (omega_blackwell_encode_one(&bad, w) == 0) return -103;
1062 bad.op = BW_IR_BSYNC; bad.imm = 0; bad.bar_reg = 16;
1063 if (omega_blackwell_encode_one(&bad, w) == 0) return -104;
1064 bad.bar_reg = 15;
1065 if (omega_blackwell_encode_one(&bad, w) != 0 || w[0] != (0x00007941U | (15U << 16))) return -105;
1066 return 0;
1067}
1068
1069
1070/* Training R5 (2026-10-15): golden words for SHFL.BFLY and F2FP.BF16.F32.PACK_AB, the two instructions the prefill
1071 * kernels emitted as placeholders patched after encoding until R5. Oracle: nvcc 13.0.88 -arch=sm_121, cuobjdump -sass,
1072 * sealed in evidence/OWN30B-T3-TC-PEAK-BW-5faa0f70-20261013T2236Z/fa_ops.{cu,sass} (low 64-bit word, then high; the top
1073 * 23 bits of the high word are the control word and are not compared):
1074 * SHFL.BFLY PT, R5, R0, 0x1, 0x1f ; 0x0c201f0000057f89 0x004e2800000e0000
1075 * SHFL.BFLY PT, R7, R8, 0x1, 0x1f ; 0x0c201f0008077f89 0x008e6200000e0000
1076 * SHFL.BFLY PT, R4, R9, 0x2, 0x1f ; 0x0c401f0009047f89 0x000e2800000e0000
1077 * SHFL.BFLY PT, R6, R11, 0x2, 0x1f ; 0x0c401f000b067f89 0x000e6200000e0000
1078 * F2FP.BF16.F32.PACK_AB R9, R3, R0 ; 0x000000000309723e 0x000fe200000010ff
1079 * F2FP.BF16.F32.PACK_AB R13, R13, R10 ; 0x0000000a0d0d723e 0x000fe400000010ff
1080 * SHFL.BFLY is SHFL.DOWN (oracle words in omega_numeric.c, lanes 16 .. 1) with bit 58 set (Mesa NAK mode field 58..59: Idx 0,
1081 * Up 1, Down 2, Bfly 3; same lane field 53..57); the lane check below covers every lane 0 .. 31 against that rule. 0 = all good. */
1082int omega_blackwell_verify_codegen_fixtures_bfly_f2fp(void) {
1083 static const struct {
1084 BlackwellIROpcode op; int d, a, b; uint32_t imm; uint32_t w0, w1, w2; const char *text;
1085 } fx[] = {
1086 { BW_IR_SHFL_BFLY, 5, 0, -1, 1, 0x00057f89U, 0x0c201f00U, 0x000e0000U, "SHFL.BFLY PT, R5, R0, 0x1, 0x1f" },
1087 { BW_IR_SHFL_BFLY, 7, 8, -1, 1, 0x08077f89U, 0x0c201f00U, 0x000e0000U, "SHFL.BFLY PT, R7, R8, 0x1, 0x1f" },
1088 { BW_IR_SHFL_BFLY, 4, 9, -1, 2, 0x09047f89U, 0x0c401f00U, 0x000e0000U, "SHFL.BFLY PT, R4, R9, 0x2, 0x1f" },
1089 { BW_IR_SHFL_BFLY, 6, 11, -1, 2, 0x0b067f89U, 0x0c401f00U, 0x000e0000U, "SHFL.BFLY PT, R6, R11, 0x2, 0x1f" },
1090 { BW_IR_F2FP_BF16_PACK, 9, 3, 0, 0, 0x0309723eU, 0x00000000U, 0x000010ffU, "F2FP.BF16.F32.PACK_AB R9, R3, R0" },
1091 { BW_IR_F2FP_BF16_PACK, 13, 13, 10, 0, 0x0d0d723eU, 0x0000000aU, 0x000010ffU, "F2FP.BF16.F32.PACK_AB R13, R13, R10" },
1092 };
1093 uint32_t w[4];
1094 for (int i = 0; i < (int)(sizeof fx / sizeof fx[0]); i++) {
1095 BlackwellIRInsn in;
1096 memset(&in, 0, sizeof in);
1097 in.op = fx[i].op; in.dst_vreg = fx[i].d; in.src1_vreg = fx[i].a; in.src2_vreg = fx[i].b;
1098 in.src3_vreg = -1; in.ureg = -1; in.imm = fx[i].imm;
1099 if (omega_blackwell_encode_one(&in, w) != 0) return -(2 * i + 1);
1100 if (w[0] != fx[i].w0 || w[1] != fx[i].w1 || w[2] != fx[i].w2) return -(2 * i + 2);
1101 }
1102 for (uint32_t lane = 0; lane < 32; lane++) {
1103 BlackwellIRInsn in; uint32_t d[4];
1104 memset(&in, 0, sizeof in);
1105 in.op = BW_IR_SHFL_DOWN; in.dst_vreg = 9; in.src1_vreg = 2; in.src2_vreg = in.src3_vreg = in.ureg = -1; in.imm = lane;
1106 if (omega_blackwell_encode_one(&in, d) != 0) return -100;
1107 in.op = BW_IR_SHFL_BFLY;
1108 if (omega_blackwell_encode_one(&in, w) != 0) return -101;
1109 if (w[0] != d[0] || w[1] != (d[1] | 0x04000000U) || w[2] != d[2] || w[3] != d[3]) return -102;
1110 }
1111 return 0;
1112}
1113
1114/* Training R5 part 2: f32 global atomic add. Starting map from Mesa NAK sm70_encode.rs (a08a0b9, sm >= 90 float ATOMG;
1115 * Mesa ranges are half-open): opcode 0x9a3, op 87..90 (Add 0, Min 2, Max 4), type 73..76 (F32 = 9, which is .FTZ.RN;
1116 * 12 decodes as .RN without FTZ), order 77..80 (Strong System 0xa, Strong GPU 0x7), pred-dst 81..83 = PT, eviction
1117 * 84..86 = 1, bit 63 = Ra.64, bit 91 = UR form. Then every word below was decoded offline with nvdisasm -b SM121
1118 * (CUDA 13.0) to the text shown. Where Mesa and the decoder disagree the decoder (and the chip probe) win: this word
1119 * sets bit 90 (w2 bit 26), which falls inside Mesa's op field, yet it decodes as .ADD with desc[UR][R.64]; without it
1120 * the same word decodes as [R.64+UR]. (Bit 88 decodes as .MIN, so the op field is there.)
1121 * 0x80000005020479a3 0x00321e000c1f5306 ATOMG.E.ADD.F32.FTZ.RN.STRONG.SYS PT, R4, desc[UR6][R2.64], R5 ;
1122 * 0x800000070a0979a3 0x00321e000c1f5304 ATOMG.E.ADD.F32.FTZ.RN.STRONG.SYS PT, R9, desc[UR4][R10.64], R7 ;
1123 * 0x8000000502ff79a3 0x00321e000c1f5306 ATOMG.E.ADD.F32.FTZ.RN.STRONG.SYS PT, RZ, desc[UR6][R2.64], R5 ;
1124 * 0x80000005020479a3 0x00321e000c1ef306 ATOMG.E.ADD.F32.FTZ.RN.STRONG.GPU PT, R4, desc[UR6][R2.64], R5 ;
1125 * 0x800000070a0979a3 0x00321e000c1ef304 ATOMG.E.ADD.F32.FTZ.RN.STRONG.GPU PT, R9, desc[UR4][R10.64], R7 ;
1126 * 0x8000000502ff79a3 0x00321e000c1ef306 ATOMG.E.ADD.F32.FTZ.RN.STRONG.GPU PT, RZ, desc[UR6][R2.64], R5 ;
1127 * Rows with d = -1 (no destination vreg) must encode RZ, not R0. Chip proof: tests/gpu_atomf32_probe_test.c. 0 = all good. */
1128int omega_blackwell_verify_codegen_fixtures_atomf32(void) {
1129 static const struct { BlackwellIROpcode op; int d, a, b, u; uint32_t w0, w1, w2; } fx[] = {
1130 { BW_IR_ATOMG_ADD_F32_STRONG_SYS, 4, 2, 5, 6, 0x020479a3U, 0x80000005U, 0x0c1f5306U },
1131 { BW_IR_ATOMG_ADD_F32_STRONG_SYS, 9, 10, 7, 4, 0x0a0979a3U, 0x80000007U, 0x0c1f5304U },
1132 { BW_IR_ATOMG_ADD_F32_STRONG_SYS, 255, 2, 5, 6, 0x02ff79a3U, 0x80000005U, 0x0c1f5306U },
1133 { BW_IR_ATOMG_ADD_F32_STRONG_SYS, -1, 2, 5, 6, 0x02ff79a3U, 0x80000005U, 0x0c1f5306U },
1134 { BW_IR_ATOMG_ADD_F32_STRONG_GPU, 4, 2, 5, 6, 0x020479a3U, 0x80000005U, 0x0c1ef306U },
1135 { BW_IR_ATOMG_ADD_F32_STRONG_GPU, 9, 10, 7, 4, 0x0a0979a3U, 0x80000007U, 0x0c1ef304U },
1136 { BW_IR_ATOMG_ADD_F32_STRONG_GPU, -1, 2, 5, 6, 0x02ff79a3U, 0x80000005U, 0x0c1ef306U },
1137 };
1138 uint32_t w[4];
1139 for (int i = 0; i < (int)(sizeof fx / sizeof fx[0]); i++) {
1140 BlackwellIRInsn in;
1141 memset(&in, 0, sizeof in);
1142 in.op = fx[i].op; in.dst_vreg = fx[i].d; in.src1_vreg = fx[i].a; in.src2_vreg = fx[i].b;
1143 in.src3_vreg = -1; in.ureg = fx[i].u;
1144 if (omega_blackwell_encode_one(&in, w) != 0) return -(2 * i + 1);
1145 if (w[0] != fx[i].w0 || w[1] != fx[i].w1 || w[2] != fx[i].w2 || w[3] != 0x00321e00U) return -(2 * i + 2);
1146 }
1147 return 0;
1148}
1149/* FB-1 cut 4: word-level fixtures for the four additive ops (checked against the
1150 * nvdisasm decodes recorded in docs/numeric/FB1_CUT4_ELEMENTWISE.md). 0 = all good. */
1151int omega_blackwell_verify_codegen_fixtures_fb1cut4(void) {
1152 OmegaRegAlloc ra;
1153 memset(&ra, 0, sizeof(ra));
1154 ra.vreg_to_phys[0] = 2; /* R2 */
1155 ra.vreg_to_phys[1] = 4; /* R4 */
1156 ra.vreg_to_phys[2] = 9; /* R9 */
1157 ra.vreg_to_phys[3] = 10; /* R10 */
1158 ra.vreg_to_phys[4] = 8; /* R8 */
1159 uint32_t w[4];
1160
1161 /* MUFU.EX2 R2, R4 */
1162 BlackwellIRInsn ex2 = { .op = BW_IR_MUFU_EX2, .dst_vreg = 0, .src1_vreg = 1 };
1163 if (encode_single_insn(&ex2, &ra, w) != 0) return -1;
1164 if (w[0] != 0x00027308U || w[1] != 4 || w[2] != 0x00000800U) return -2;
1165 /* BAR.SYNC.DEFER_BLOCKING 0x0 */
1166 BlackwellIRInsn bar = { .op = BW_IR_BAR_SYNC };
1167 if (encode_single_insn(&bar, &ra, w) != 0) return -3;
1168 if (w[0] != 0x00007b1dU || w[1] != 0 || w[2] != 0x00010000U || w[3] != 0x000fec00U) return -4;
1169 /* LDS R9, [R10+URZ] */
1170 BlackwellIRInsn lds = { .op = BW_IR_LDS32, .dst_vreg = 2, .src1_vreg = 3 };
1171 if (encode_single_insn(&lds, &ra, w) != 0) return -5;
1172 if (w[0] != 0x0a097984U || w[1] != 0xff || w[2] != 0x08000800U) return -6;
1173 /* STS [R8+URZ], R2 */
1174 BlackwellIRInsn sts = { .op = BW_IR_STS32, .src1_vreg = 4, .src2_vreg = 0 };
1175 if (encode_single_insn(&sts, &ra, w) != 0) return -7;
1176 if (w[0] != 0x08007988U || w[1] != 2 || w[2] != 0x080008ffU) return -8;
1177 return 0;
1178}
1179
1180/* Kernel capture (C6 of docs/OMEGA.md, tools/sm121dis): with OMEGA_KERNEL_DUMP_DIR set, every encoded kernel is written
1181 * there as <first 16 hex of its SHA-256>.bin, once per distinct body, so the disassembler can be checked against nvdisasm on
1182 * every kernel the library builds. Unset (the normal case) it does nothing; the freestanding build has no files. */
1183void omega_kernel_dump(const uint8_t *code, size_t len) {
1184#ifndef OMEGA_GPU_NO_POSIX
1185 const char *env = getenv("OMEGA_KERNEL_DUMP_DIR");
1186 if (!env || !env[0] || !code || !len) return;
1187 /* Only the checked copy (src/omega_path_check.h) is opened; a refused directory is a diagnostic, which
1188 * tools/sm121dis/corpus.sh fails on, never a silent skip. */
1189 char dir[4000];
1190 if (omega_path_copy(dir, sizeof dir, env) != 0) {
1191 BW_DIAG("omega_kernel_dump: OMEGA_KERNEL_DUMP_DIR refused (src/omega_path_check.h)\n");
1192 return;
1193 }
1194 uint8_t d[SHA256_DIGEST_SIZE]; sha256_hash(code, len, d);
1195 char path[4096];
1196 int n = snprintf(path, sizeof path, "%s/%02x%02x%02x%02x%02x%02x%02x%02x.bin",
1197 dir, (unsigned)d[0], (unsigned)d[1], (unsigned)d[2], (unsigned)d[3],
1198 (unsigned)d[4], (unsigned)d[5], (unsigned)d[6], (unsigned)d[7]);
1199 if (n < 0 || (size_t)n >= sizeof path) return;
1200 /* Private, exclusive creation: an existing body or symlink is left alone. */
1201 int fd = open(path, O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC, 0600);
1202 if (fd < 0) return;
1203 FILE *f = fdopen(fd, "wb");
1204 if (!f) { close(fd); unlink(path); BW_DIAG("omega_kernel_dump: write failed, removed %s\n", path); return; }
1205 /* A body cut short (a full disk) must not stay under its full-content name: it would be compared as a smaller kernel
1206 * and, created O_EXCL, never rewritten. tools/sm121dis/corpus.sh fails on these diagnostics. */
1207 int bad = fwrite(code, 1, len, f) != len;
1208 if (fclose(f) != 0) bad = 1;
1209 if (bad) { unlink(path); BW_DIAG("omega_kernel_dump: write failed, removed %s\n", path); }
1210#else
1211 (void)code; (void)len;
1212#endif
1213}
1214
1215#ifdef BW_ENC_C_PRIMARY
1216static int bw_encode_program_bytes(const BlackwellIRProgram *prog, uint8_t *code_buf, size_t req_bytes) {
1217 size_t padded_insns = req_bytes / 16;
1218 uint8_t *p = code_buf;
1219 for (size_t i = 0; i < padded_insns; i++) {
1220 uint32_t w[4];
1221 if (i < prog->count) {
1222 if (encode_single_insn(&prog->insns[i], &prog->regalloc, w) != 0) {
1223 return -1;
1224 }
1225 } else {
1226 /* Pad with NOP */
1227 w[0] = 0x00007918;
1228 w[1] = 0x00000000;
1229 w[2] = 0x00000000;
1230 w[3] = 0x000fc000;
1231 }
1232 for (int k = 0; k < 4; k++) {
1233 p[4 * k] = (uint8_t)(w[k] & 0xff);
1234 p[4 * k + 1] = (uint8_t)((w[k] >> 8) & 0xff);
1235 p[4 * k + 2] = (uint8_t)((w[k] >> 16) & 0xff);
1236 p[4 * k + 3] = (uint8_t)((w[k] >> 24) & 0xff);
1237 }
1238 p += 16;
1239 }
1240 return 0;
1241}
1242#else
1243static int bw_encode_program_bytes(const BlackwellIRProgram *prog, uint8_t *code_buf, size_t req_bytes) {
1244 uint8_t code[BW_OM_CODE_LEN];
1245 size_t got = bw_om_encode(prog->insns, prog->count, &prog->regalloc, 1, code);
1246 int rc = (got == req_bytes && got != 0) ? 0 : -1;
1247#ifdef CUDAPP_MIGRATION_PENDING_ENCODER_DIFF
1248 /* The C oracle encodes the same program, instruction by instruction, NOP-padded the same way. */
1249#ifdef CUDAPP_ENCODER_DIFF_SELFTEST
1250 if (rc == 0) code[0] ^= 1; /* red check of the comparison itself: one flipped bit must abort */
1251#endif
1252 uint8_t ref[BW_OM_CODE_LEN];
1253 int crc = 0;
1254 for (size_t i = 0; i < req_bytes / 16 && crc == 0; i++) {
1255 uint32_t w[4] = { 0x00007918, 0, 0, 0x000fc000 };
1256 if (i < prog->count) crc = bw_c_encode_insn(&prog->insns[i], &prog->regalloc, w);
1257 for (int k = 0; k < 16; k++) ref[16 * i + (size_t)k] = (uint8_t)(w[k / 4] >> (8 * (k % 4)));
1258 }
1259 bw_enc_diff_note("program", prog->count, rc == 0 ? code : NULL, rc == 0 ? req_bytes : 0,
1260 crc == rc && (rc != 0 || memcmp(code, ref, req_bytes) == 0));
1261#endif
1262 if (rc == 0) memcpy(code_buf, code, req_bytes);
1263 return rc;
1264}
1265#endif
1266
1267int omega_bw_encode_program(const BlackwellIRProgram *prog, uint8_t *code_buf, size_t max_len, size_t *out_len) {
1268 if (!prog || !code_buf || !out_len) return -1;
1269
1270 size_t padded_insns = (prog->count + 7) & ~7ULL;
1271 if (padded_insns < 32) padded_insns = 32;
1272
1273 size_t req_bytes = padded_insns * 16;
1274 if (max_len < req_bytes) return -1;
1275
1276 if (bw_encode_program_bytes(prog, code_buf, req_bytes) != 0) return -1;
1277
1278 omega_kernel_dump(code_buf, req_bytes);
1279 *out_len = req_bytes;
1280 return 0;
1281}
1282
1283static int bw_ir_unpack_raw(const uint64_t ins[8192], const uint64_t meta[512], BlackwellIRProgram *prog);
1284
1285int omega_blackwell_codegen_matmul_i32_prog(const OmegaMatMulSpec *spec, BlackwellIRProgram *prog) {
1286 if (!spec || !prog) return -1;
1287 uint64_t sp[12] = { spec->m, spec->k, spec->n, 0 };
1288 uint64_t ins[8192], meta[512];
1289 memset(ins, 0, sizeof(ins));
1290 memset(meta, 0, sizeof(meta));
1291 uint64_t rc = 1;
1292 if (omega2_cg_matmul_i32(&rc, sp, 12, ins, 8192, meta, 512) != OMEGA_OK || rc != 0) {
1293 omega_bw_ir_init(prog);
1294 return -1;
1295 }
1296 if (bw_ir_unpack_raw(ins, meta, prog) != 0) return -1;
1297 if (omega_bw_regalloc_solve(prog) != 0) {
1298 omega_bw_ir_init(prog);
1299 return -1;
1300 }
1301 return 0;
1302}
1303
1304/* ---- native v1 P07 cut A.2: allocation-free kernel encoding ---------------------------------
1305 * The one place a solved IR program becomes a kernel. Hosted and freestanding builds both encode
1306 * through it, so the two produce the same bytes for the same program (make test-codegen-freestanding
1307 * compares them). */
1308size_t omega_bw_program_code_bytes(const BlackwellIRProgram *prog) {
1309 if (!prog) return 0;
1310 size_t padded_insns = (prog->count + 7) & ~(size_t)7;
1311 if (padded_insns < 32) padded_insns = 32;
1312 return padded_insns * 16;
1313}
1314
1315int omega_blackwell_kernel_encode_into(const BlackwellIRProgram *prog, uint8_t *code_buf, size_t code_cap,
1316 OmegaBlackwellKernel *kernel) {
1317 if (!kernel) return -1;
1318 memset(kernel, 0, sizeof *kernel);
1319 if (!prog || !code_buf) return -1;
1320 size_t need = omega_bw_program_code_bytes(prog);
1321 if (code_cap < need) return OMEGA_BW_CODE_BUFFER_TOO_SMALL; /* refused before any byte is written */
1322 size_t emitted_size = 0;
1323 if (omega_bw_encode_program(prog, code_buf, code_cap, &emitted_size) != 0) return -1;
1324 kernel->code = code_buf;
1325 kernel->code_size = emitted_size;
1326 kernel->insn_count = emitted_size / 16;
1327 kernel->gpr_count = prog->regalloc.peak_gpr_usage;
1328 kernel->uniform_gpr_count = prog->regalloc.peak_ugpr_usage;
1329 sha256_hash(kernel->code, kernel->code_size, kernel->code_digest);
1330 return 0;
1331}
1332
1333#ifndef OMEGA_GPU_NO_POSIX
1334/* Hosted only: the allocating generators keep their names, caps and behavior. Each builds the program,
1335 * mallocs max_bytes (the cap each generator always had) and encodes through omega_blackwell_kernel_encode_into;
1336 * omega_blackwell_kernel_free releases the buffer. */
1337static int bw_kernel_alloc_encode(const BlackwellIRProgram *prog, size_t max_bytes, OmegaBlackwellKernel *kernel) {
1338 uint8_t *buf = (uint8_t *)malloc(max_bytes);
1339 if (!buf) { kernel->code = NULL; return -1; }
1340 if (omega_blackwell_kernel_encode_into(prog, buf, max_bytes, kernel) != 0) {
1341 free(buf);
1342 kernel->code = NULL;
1343 return -1;
1344 }
1345 return 0;
1346}
1347
1348int omega_blackwell_codegen_matmul_i32(const OmegaMatMulSpec *spec, OmegaBlackwellKernel *kernel) {
1349 if (!spec || !kernel) return -1;
1350 BlackwellIRProgram prog;
1351 if (omega_blackwell_codegen_matmul_i32_prog(spec, &prog) != 0) return -1;
1352 /* 13. the code buffer is exactly the padded program (the i32 generator never had a fixed cap) */
1353 return bw_kernel_alloc_encode(&prog, omega_bw_program_code_bytes(&prog), kernel);
1354}
1355#endif
1356
1357static int bw_om_check_reg_local(int64_t r64, uint8_t sub, uint64_t limit, const uint64_t *widths) {
1358 if (r64 == -1) return sub == 0 ? 0 : -1;
1359 if (r64 < 0 || (uint64_t)r64 >= limit) return -1;
1360 if (sub >= widths[(uint64_t)r64]) return -1;
1361 return 0;
1362}
1363
1364static int bw_ir_unpack_raw(const uint64_t ins[8192], const uint64_t meta[512], BlackwellIRProgram *prog) {
1365 if (!prog) return -1;
1366 omega_bw_ir_init(prog);
1367 if (!ins || !meta || meta[5] != 0) return -1;
1368 const uint64_t n = meta[0], nv = meta[1], nu = meta[2];
1369 if (n == 0 || n > BW_MAX_IR_INSNS || nv > BW_MAX_VREGS || nu > BW_MAX_UVREGS) return -1;
1370 for (uint64_t v = 0; v < nv; v++) {
1371 uint64_t w = meta[64u + v];
1372 if (w != 1u && w != 2u && w != 4u) goto fail;
1373 int r = (w == 4u) ? omega_bw_ir_alloc_vreg128(prog) : (w == 2u) ? omega_bw_ir_alloc_vreg64(prog) : omega_bw_ir_alloc_vreg(prog);
1374 if (r != (int)v) goto fail;
1375 }
1376 for (uint64_t u = 0; u < nu; u++) {
1377 uint64_t w = meta[352u + u];
1378 if (w != 1u && w != 2u) goto fail;
1379 int r = (w == 2u) ? omega_bw_ir_alloc_uvreg64(prog) : omega_bw_ir_alloc_uvreg(prog);
1380 if (r != (int)u) goto fail;
1381 }
1382 for (uint64_t i = 0; i < n; i++) {
1383 const uint64_t *r = &ins[i * 8u];
1384 int64_t op64 = (int64_t)r[0];
1385 if (op64 < 0 || op64 > (int64_t)BW_IR_ATOMG_ADD_F32_STRONG_GPU) goto fail;
1386 if ((r[7] >> 48) != 0) goto fail;
1387 bool is_u = (r[7] & 1u) != 0;
1388 uint8_t dsub = (uint8_t)((r[7] >> 16) & 0xffu);
1389 uint8_t s1sub = (uint8_t)((r[7] >> 24) & 0xffu);
1390 uint8_t s2sub = (uint8_t)((r[7] >> 32) & 0xffu);
1391 uint8_t s3sub = (uint8_t)((r[7] >> 40) & 0xffu);
1392 uint8_t bar = (uint8_t)((r[7] >> 8) & 0xffu);
1393 if (bar >= BW_RECONV_MAX_BAR) goto fail;
1394 const uint64_t *wtab = is_u ? &meta[352] : &meta[64];
1395 uint64_t lim = is_u ? nu : nv;
1396 if (bw_om_check_reg_local((int64_t)r[1], dsub, lim, wtab) != 0) goto fail;
1397 if (bw_om_check_reg_local((int64_t)r[2], s1sub, lim, wtab) != 0) goto fail;
1398 if (bw_om_check_reg_local((int64_t)r[3], s2sub, lim, wtab) != 0) goto fail;
1399 if (bw_om_check_reg_local((int64_t)r[4], s3sub, lim, wtab) != 0) goto fail;
1400 if (bw_om_check_reg_local((int64_t)r[5], 0, nu, &meta[352]) != 0) goto fail;
1401
1402 BlackwellIRInsn in;
1403 memset(&in, 0, sizeof in);
1404 in.op = (BlackwellIROpcode)(int)op64;
1405 in.dst_vreg = (int)(int64_t)r[1];
1406 in.src1_vreg = (int)(int64_t)r[2];
1407 in.src2_vreg = (int)(int64_t)r[3];
1408 in.src3_vreg = (int)(int64_t)r[4];
1409 in.ureg = (int)(int64_t)r[5];
1410 in.imm = (uint32_t)(r[6] & 0xffffffffu);
1411 in.control = (uint32_t)(r[6] >> 32);
1412 in.is_uniform = is_u;
1413 in.predicate_p0 = ((r[7] >> 1) & 1u) != 0;
1414 in.predicate_not = ((r[7] >> 2) & 1u) != 0;
1415 in.bar_reg = bar;
1416 in.dst_subreg = dsub;
1417 in.src1_subreg = s1sub;
1418 in.src2_subreg = s2sub;
1419 in.src3_subreg = s3sub;
1420 if (omega_bw_ir_append(prog, &in) < 0) goto fail;
1421 }
1422 return 0;
1423fail:
1424 omega_bw_ir_init(prog);
1425 return -1;
1426}
1427
1428static void bw_keep_alive(BlackwellIRProgram *prog, int from_idx, int to_idx, int last_idx) {
1429 /* every vreg first defined in [from_idx, to_idx) stays live until last_idx */
1430 for (int v = 0; v < prog->regalloc.num_vregs; v++) {
1431 OmegaLiveInterval *iv = &prog->regalloc.intervals[v];
1432 if (iv->first_def >= from_idx && iv->first_def < to_idx && iv->last_use < last_idx)
1433 iv->last_use = last_idx;
1434 }
1435}
1436
1437static void bw_live_until(BlackwellIRProgram *prog, int vreg, int last_idx) {
1438 if (vreg >= 0 && vreg < prog->regalloc.num_vregs && prog->regalloc.intervals[vreg].last_use < last_idx)
1439 prog->regalloc.intervals[vreg].last_use = last_idx;
1440}
1441
1442int omega_blackwell_codegen_matmul_tensor_prog(const OmegaMatMulSpec *spec, BlackwellIRProgram *prog) {
1443 if (!spec || !prog) return -1;
1444 uint64_t sp[12] = { spec->m, spec->k, spec->n, (uint64_t)spec->precision, 0 };
1445 uint64_t ins[8192], meta[512];
1446 memset(ins, 0, sizeof(ins));
1447 memset(meta, 0, sizeof(meta));
1448 uint64_t rc = 1;
1449 if (omega2_cg_matmul_tensor(&rc, sp, 12, ins, 8192, meta, 512) != OMEGA_OK || rc != 0) {
1450 omega_bw_ir_init(prog);
1451 return -1;
1452 }
1453 if (bw_ir_unpack_raw(ins, meta, prog) != 0) return -1;
1454 if (omega_bw_regalloc_solve(prog) != 0) {
1455 omega_bw_ir_init(prog);
1456 return -1;
1457 }
1458 return 0;
1459}
1460
1461#ifndef OMEGA_GPU_NO_POSIX
1462int omega_blackwell_codegen_matmul_tensor(const OmegaMatMulSpec *spec, OmegaBlackwellKernel *kernel) {
1463 if (!spec || !kernel) return -1;
1464 BlackwellIRProgram prog;
1465 if (omega_blackwell_codegen_matmul_tensor_prog(spec, &prog) != 0) {
1466 return -1;
1467 }
1468
1469 return bw_kernel_alloc_encode(&prog, OMEGA_BW_CODE_CAP_SMALL, kernel);
1470}
1471#endif
1472
1473int omega_blackwell_codegen_matmul_tensor_loop_prog(const OmegaMatMulSpec *spec, uint32_t grid_x,
1474 int mutant, BlackwellIRProgram *prog) {
1475 if (!spec || !prog) return -1;
1476 uint64_t sp[12] = { spec->m, spec->k, spec->n, (uint64_t)spec->precision, grid_x, (uint64_t)(mutant != 0), 0 };
1477 uint64_t ins[8192], meta[512];
1478 memset(ins, 0, sizeof(ins));
1479 memset(meta, 0, sizeof(meta));
1480 uint64_t rc = 1;
1481 if (omega2_cg_matmul_tensor_loop(&rc, sp, 12, ins, 8192, meta, 512) != OMEGA_OK || rc != 0) {
1482 omega_bw_ir_init(prog);
1483 return -1;
1484 }
1485 if (bw_ir_unpack_raw(ins, meta, prog) != 0) return -1;
1486 const int L_col = (int)meta[480], L_k = (int)meta[481], L_k_end = (int)meta[482], L_col_end = (int)meta[483];
1487 const int v_rd = (int)meta[484];
1488 bw_keep_alive(prog, 0, L_col, L_col_end);
1489 bw_keep_alive(prog, L_col, L_k, L_k_end);
1490 bw_keep_alive(prog, L_k, L_k_end, L_k_end);
1491 bw_live_until(prog, v_rd, L_col_end);
1492 if (omega_bw_regalloc_solve(prog) != 0) {
1493 omega_bw_ir_init(prog);
1494 return -1;
1495 }
1496 return 0;
1497}
1498
1499#ifndef OMEGA_GPU_NO_POSIX
1500int omega_blackwell_codegen_matmul_tensor_loop(const OmegaMatMulSpec *spec, uint32_t grid_x,
1501 int mutant, OmegaBlackwellKernel *kernel) {
1502 if (!spec || !kernel) return -1;
1503 BlackwellIRProgram prog;
1504 if (omega_blackwell_codegen_matmul_tensor_loop_prog(spec, grid_x, mutant, &prog) != 0) return -1;
1505 return bw_kernel_alloc_encode(&prog, OMEGA_BW_CODE_CAP_SMALL, kernel);
1506}
1507#endif
1508
1509int omega_blackwell_codegen_matmul_frag_prog(const OmegaMatMulSpec *spec, uint32_t grid_x, uint32_t unroll,
1510 int mutant, BlackwellIRProgram *prog) {
1511 if (!spec || !prog) return -1;
1512 uint64_t sp[12] = { spec->m, spec->k, spec->n, (uint64_t)spec->precision, grid_x, unroll, (uint64_t)(mutant != 0), 0 };
1513 uint64_t ins[8192], meta[512];
1514 memset(ins, 0, sizeof(ins));
1515 memset(meta, 0, sizeof(meta));
1516 uint64_t rc = 1;
1517 if (omega2_cg_matmul_frag(&rc, sp, 12, ins, 8192, meta, 512) != OMEGA_OK || rc != 0) {
1518 omega_bw_ir_init(prog);
1519 return -1;
1520 }
1521 if (bw_ir_unpack_raw(ins, meta, prog) != 0) return -1;
1522 const int L_k = (int)meta[481], L_k_end = (int)meta[482], L_col_end = (int)meta[483];
1523 for (int i = 484; i <= 490; i++) bw_live_until(prog, (int)meta[i], L_col_end);
1524 for (int i = 491; i <= 493; i++) bw_live_until(prog, (int)meta[i], L_k_end);
1525 bw_keep_alive(prog, L_k, L_k_end, L_k_end);
1526 if (omega_bw_regalloc_solve(prog) != 0) {
1527 omega_bw_ir_init(prog);
1528 return -1;
1529 }
1530 return 0;
1531}
1532
1533#ifndef OMEGA_GPU_NO_POSIX
1534int omega_blackwell_codegen_matmul_frag(const OmegaMatMulSpec *spec, uint32_t grid_x, uint32_t unroll,
1535 int mutant, OmegaBlackwellKernel *kernel) {
1536 if (!spec || !kernel) return -1;
1537 BlackwellIRProgram prog;
1538 if (omega_blackwell_codegen_matmul_frag_prog(spec, grid_x, unroll, mutant, &prog) != 0) return -1;
1539 return bw_kernel_alloc_encode(&prog, OMEGA_BW_CODE_CAP_SMALL, kernel);
1540}
1541#endif
1542
1543int omega_blackwell_codegen_matmul_frag_block_prog(const OmegaMatMulSpec *spec, uint32_t grid_x, uint32_t unroll,
1544 uint32_t rt, uint32_t ct, int mutant, BlackwellIRProgram *prog) {
1545 if (!spec || !prog) return -1;
1546 uint64_t sp[12] = { spec->m, spec->k, spec->n, (uint64_t)spec->precision, grid_x, unroll, rt, ct, (uint64_t)(mutant != 0), 0 };
1547 uint64_t ins[8192], meta[512];
1548 memset(ins, 0, sizeof(ins));
1549 memset(meta, 0, sizeof(meta));
1550 uint64_t rc = 1;
1551 if (omega2_cg_matmul_frag_block(&rc, sp, 12, ins, 8192, meta, 512) != OMEGA_OK || rc != 0) {
1552 omega_bw_ir_init(prog);
1553 return -1;
1554 }
1555 if (bw_ir_unpack_raw(ins, meta, prog) != 0) return -1;
1556 const int L_k = (int)meta[481], L_k_end = (int)meta[482], L_col_end = (int)meta[483];
1557 for (int i = 484; i <= 489; i++) bw_live_until(prog, (int)meta[i], L_col_end);
1558 for (uint32_t t = 0; t < rt * ct; t++) bw_live_until(prog, (int)meta[408 + t], L_col_end);
1559 for (int i = 490; i <= 492; i++) bw_live_until(prog, (int)meta[i], L_k_end);
1560 bw_keep_alive(prog, L_k, L_k_end, L_k_end);
1561 if (omega_bw_regalloc_solve(prog) != 0) {
1562 omega_bw_ir_init(prog);
1563 return -1;
1564 }
1565 return 0;
1566}
1567
1568#ifndef OMEGA_GPU_NO_POSIX
1569int omega_blackwell_codegen_matmul_frag_block(const OmegaMatMulSpec *spec, uint32_t grid_x, uint32_t unroll,
1570 uint32_t rt, uint32_t ct, int mutant, OmegaBlackwellKernel *kernel) {
1571 if (!spec || !kernel) return -1;
1572 BlackwellIRProgram prog;
1573 if (omega_blackwell_codegen_matmul_frag_block_prog(spec, grid_x, unroll, rt, ct, mutant, &prog) != 0) return -1;
1574 return bw_kernel_alloc_encode(&prog, OMEGA_BW_CODE_CAP_MAX, kernel);
1575}
1576#endif
1577
1578void omega_matmul_frag_block_shape(uint32_t mt, uint32_t nt, uint32_t *rt, uint32_t *ct) {
1579 uint64_t res[4] = { 1, 1, 1, 1 }, rc = 1;
1580 omega2_cg_matmul_frag_shape(&rc, mt, nt, 16, res, 4);
1581 *rt = (uint32_t)res[0];
1582 *ct = (uint32_t)res[1];
1583}
1584
1585uint32_t omega_matmul_frag_block_unroll(uint32_t kp, uint32_t rt, uint32_t ct) {
1586 const uint32_t ks = kp / 16u;
1587 return (ks % 2u == 0 && 2u * (rt + ct) <= 8u) ? 2u : 1u;
1588}
1589
1590uint32_t omega_matmul_frag_unroll(uint32_t kp) {
1591 uint64_t res[4] = { 1, 1, 1, 1 }, rc = 1;
1592 omega2_cg_matmul_frag_shape(&rc, 1, 1, kp, res, 4);
1593 return (uint32_t)res[3];
1594}
1595
1596/* A block (mt, ks) holds rows 16mt..16mt+15 and columns 16ks..16ks+15 as 256 bf16 at element
1597 * (mt*KS + ks)*256; inside the block, element (rr, cc) sits at lane*8 + e with lane = (rr%8)*4 + (cc%8)/2
1598 * and e = (cc/8)*4 + (rr/8)*2 + cc%2 (the HMMA.16816 A fragment order, header comment). The 256-entry
1599 * table makes the pack a tile loop with no per-element division (2026-10-12: the per-element form with
1600 * six divisions and a conversion call per element was ~2.5 ms per MB of activations at m = 128). */
1601static uint16_t g_frag_a_tab[256];
1602static void frag_a_tab_init(void) {
1603 if (g_frag_a_tab[255]) return;
1604 for (uint32_t rr = 0; rr < 16; rr++)
1605 for (uint32_t cc = 0; cc < 16; cc++)
1606 g_frag_a_tab[rr * 16 + cc] = (uint16_t)((((rr & 7u) * 4u + ((cc & 7u) >> 1)) * 8u) + (cc >> 3) * 4u + (rr >> 3) * 2u + (cc & 1u));
1607}
1608/* bit for bit what the compiler's (__bf16)f cast gives (gpu_matmul_api_test checks it against
1609 * omega_fp32_to_bf16 on ties, denormals, inf and NaN payloads): round to nearest even, a NaN keeps
1610 * its top payload bits and is made quiet. gcc 13 on aarch64 emits the cast as a call to the
1611 * soft-float routine __truncsfbf2 per element, which was most of the packing cost. */
1612static inline uint16_t frag_f32_to_bf16(float f) {
1613 uint32_t u;
1614 memcpy(&u, &f, 4);
1615 if ((u & 0x7f800000u) == 0x7f800000u) return (uint16_t)((u >> 16) | ((u & 0x007fffffu) ? 0x0040u : 0u));
1616 return (uint16_t)((u + 0x7fffu + ((u >> 16) & 1u)) >> 16);
1617}
1618#define FRAG_PACK_A_BODY(LOAD) \
1619 do { \
1620 const uint32_t KS = kp / 16u; \
1621 frag_a_tab_init(); \
1622 memset(dst, 0, (size_t)mp * kp * sizeof *dst); \
1623 for (uint32_t mt = 0; mt < (m + 15u) / 16u; mt++) { \
1624 const uint32_t r1 = m - mt * 16u < 16u ? m - mt * 16u : 16u; \
1625 for (uint32_t ks = 0; ks < (k + 15u) / 16u; ks++) { \
1626 const uint32_t c1 = k - ks * 16u < 16u ? k - ks * 16u : 16u; \
1627 uint16_t *blk = dst + ((size_t)mt * KS + ks) * 256u; \
1628 for (uint32_t rr = 0; rr < r1; rr++) { \
1629 const uint16_t *tab = g_frag_a_tab + rr * 16u; \
1630 const size_t src = (size_t)(mt * 16u + rr) * k + ks * 16u; \
1631 for (uint32_t cc = 0; cc < c1; cc++) blk[tab[cc]] = LOAD(a[src + cc]); \
1632 } \
1633 } \
1634 } \
1635 } while (0)
1636#define FRAG_ID(x) (x)
1637void omega_matmul_frag_pack_a(const uint16_t *a, uint32_t m, uint32_t k, uint32_t mp, uint32_t kp, uint16_t *dst) {
1638 FRAG_PACK_A_BODY(FRAG_ID);
1639}
1640void omega_matmul_frag_pack_a_f32(const float *a, uint32_t m, uint32_t k, uint32_t mp, uint32_t kp, uint16_t *dst) {
1641 FRAG_PACK_A_BODY(frag_f32_to_bf16);
1642}
1643
1644/* Row 0 only, every column of every K block (zeros past k): for a one-row call whose buffer already
1645 * holds zeros in rows 1..15 of every block from an earlier full pack of the same width. */
1646#define FRAG_PACK_A_ROW0_BODY(LOAD) \
1647 do { \
1648 const uint32_t KS = kp / 16u; \
1649 frag_a_tab_init(); \
1650 const uint16_t *tab = g_frag_a_tab; \
1651 for (uint32_t ks = 0; ks < KS; ks++) { \
1652 uint16_t *blk = dst + (size_t)ks * 256u; \
1653 const uint32_t c0 = ks * 16u, c1 = k - c0 < 16u ? k - c0 : 16u; \
1654 for (uint32_t cc = 0; cc < c1; cc++) blk[tab[cc]] = LOAD(a[c0 + cc]); \
1655 for (uint32_t cc = c1; cc < 16u; cc++) blk[tab[cc]] = 0; \
1656 } \
1657 } while (0)
1658void omega_matmul_frag_pack_a_row0(const uint16_t *a, uint32_t k, uint32_t kp, uint16_t *dst) {
1659 FRAG_PACK_A_ROW0_BODY(FRAG_ID);
1660}
1661size_t omega_matmul_result_sentinels(float *cd, uint32_t m, uint32_t n, uint32_t np, float poison, int set) {
1662 /* one word per 16 x 8 fragment tile that holds a real output word: the tile's last real row and column */
1663 size_t count = 0;
1664 if (m == 0 || n == 0) return 0;
1665 const uint32_t mt = (m + 15u) / 16u, nt = (n + 7u) / 8u;
1666 for (uint32_t ti = 0; ti < mt; ti++) {
1667 uint32_t r = ti * 16u + 15u; if (r >= m) r = m - 1u;
1668 float *row = cd + (size_t)r * np;
1669 for (uint32_t tj = 0; tj < nt; tj++) {
1670 uint32_t c = tj * 8u + 7u; if (c >= n) c = n - 1u;
1671 if (set) { row[c] = poison; count++; }
1672 else if (row[c] == poison) count++;
1673 }
1674 }
1675 return count;
1676}
1677
1678void omega_matmul_frag_pack_a_row0_f32(const float *a, uint32_t k, uint32_t kp, uint16_t *dst) {
1679 FRAG_PACK_A_ROW0_BODY(frag_f32_to_bf16);
1680}
1681
1682void omega_matmul_frag_pack_b(const uint16_t *b, uint32_t k, uint32_t n, uint32_t kp,
1683 uint32_t nt0, uint32_t nt1, uint16_t *dst) {
1684 const uint32_t KS = kp / 16u;
1685 memset(dst, 0, (size_t)(nt1 - nt0) * KS * 128u * sizeof *dst);
1686 for (uint32_t row = 0; row < k; row++) {
1687 const uint32_t ks = row / 16u, rr = row % 16u, khi = rr / 8u, tig = (rr % 8u) / 2u, lo = rr % 2u;
1688 const uint16_t *src = &b[(size_t)row * n];
1689 for (uint32_t nt = nt0; nt < nt1; nt++) {
1690 const uint32_t c0 = nt * 8u;
1691 for (uint32_t group = 0; group < 8u && c0 + group < n; group++) {
1692 const size_t lane = group * 4u + tig;
1693 dst[(((size_t)(nt - nt0) * KS + ks) * 32u + lane) * 4u + khi * 2u + lo] = src[c0 + group];
1694 }
1695 }
1696 }
1697}
1698
1699int omega_blackwell_verify_codegen_fixtures(void) {
1700 OmegaRegAlloc ra;
1701 memset(&ra, 0, sizeof(ra));
1702 ra.vreg_to_phys[0] = 2;
1703 ra.vreg_to_phys[1] = 4;
1704 ra.vreg_to_phys[2] = 6;
1705 ra.vreg_to_phys[3] = 7;
1706 ra.uvreg_to_phys[0] = 4;
1707
1708 uint32_t w[4];
1709
1710 /* 1. MOV R7, RZ */
1711 BlackwellIRInsn insn_mov_rz = { .op = BW_IR_MOV_RZ, .dst_vreg = 3 };
1712 if (encode_single_insn(&insn_mov_rz, &ra, w) != 0) return -1;
1713 if (w[0] != (0x7202 | (7 << 16)) || w[1] != 0xff || w[2] != 0x00000f00) return -2;
1714
1715 /* 2. MOV R2, 1 */
1716 BlackwellIRInsn insn_mov_imm = { .op = BW_IR_MOV_IMM, .dst_vreg = 0, .imm = 1 };
1717 if (encode_single_insn(&insn_mov_imm, &ra, w) != 0) return -3;
1718 if (w[0] != (0x7802 | (2 << 16)) || w[1] != 1 || w[2] != 0x00000f00) return -4;
1719
1720 /* 3. Register-Register IMAD: IMAD R7, R2, R6, R7 */
1721 BlackwellIRInsn insn_imad_rr = { .op = BW_IR_IMAD, .dst_vreg = 3, .src1_vreg = 0, .src2_vreg = 2, .src3_vreg = 3 };
1722 if (encode_single_insn(&insn_imad_rr, &ra, w) != 0) return -5;
1723 if (w[0] != (0x7224 | (7 << 16) | (2 << 24)) || w[1] != 6 || w[2] != (0x078e0200 | 7)) return -6;
1724
1725 /* 4. Immediate IMAD: IMAD R6, R2, 16, RZ */
1726 BlackwellIRInsn insn_imad_imm = { .op = BW_IR_IMAD, .dst_vreg = 2, .src1_vreg = 0, .src2_vreg = -1, .imm = 16, .src3_vreg = -1 };
1727 if (encode_single_insn(&insn_imad_imm, &ra, w) != 0) return -7;
1728 if (w[0] != (0x7824 | (6 << 16) | (2 << 24)) || w[1] != 16 || w[2] != 0x078e02ff) return -8;
1729
1730 /* 5. IMAD.WIDE: IMAD.WIDE R4, R2, 4, R4 */
1731 BlackwellIRInsn insn_imad_wide = { .op = BW_IR_IMAD_WIDE, .dst_vreg = 1, .src1_vreg = 0, .imm = 4, .src3_vreg = 1 };
1732 if (encode_single_insn(&insn_imad_wide, &ra, w) != 0) return -9;
1733 if (w[0] != (0x7825 | (4 << 16) | (2 << 24)) || w[1] != 4 || w[2] != (0x078e0000 | 4)) return -10;
1734
1735 /* 6. LDC.64 R4, c[0x0][0x380] */
1736 BlackwellIRInsn insn_ldc64 = { .op = BW_IR_LDC64, .dst_vreg = 1, .imm = 0x380 };
1737 if (encode_single_insn(&insn_ldc64, &ra, w) != 0) return -11;
1738 if (w[0] != (0xff007b82 | (4 << 16)) || w[1] != ((0x380 << 6) & 0x00ffff00) || w[2] != 0x00000a00) return -12;
1739
1740 /* 7. LDCU.64 UR4, c[0x0][0x358] */
1741 BlackwellIRInsn insn_ldcu64 = { .op = BW_IR_LDCU64, .dst_vreg = 0, .imm = 0x358, .is_uniform = true };
1742 if (encode_single_insn(&insn_ldcu64, &ra, w) != 0) return -13;
1743 if (w[0] != (0xff0077ac | (4 << 16)) || w[1] != ((0x358 << 5) & 0x00ffff00) || w[2] != 0x08000a00) return -14;
1744
1745 /* 8. LDG.E R2, desc[UR4][R4.64] */
1746 BlackwellIRInsn insn_ldg = { .op = BW_IR_LDG_E, .dst_vreg = 0, .src1_vreg = 1, .ureg = 0 };
1747 if (encode_single_insn(&insn_ldg, &ra, w) != 0) return -15;
1748 if (w[0] != (0x7981 | (2 << 16) | (4 << 24)) || w[1] != 4 || w[2] != 0x0c1e1900) return -16;
1749
1750 /* 9. STG.E desc[UR4][R4.64], R7 */
1751 BlackwellIRInsn insn_stg = { .op = BW_IR_STG_E, .src1_vreg = 1, .src2_vreg = 3, .ureg = 0 };
1752 if (encode_single_insn(&insn_stg, &ra, w) != 0) return -17;
1753 if (w[0] != (0x7986 | (4 << 24)) || w[1] != 7 || w[2] != (0x0c101900 | 4)) return -18;
1754
1755 /* 10. S2R R2, SR_CTAID.X */
1756 BlackwellIRInsn insn_s2r = { .op = BW_IR_S2R, .dst_vreg = 0, .imm = BW_SR_CTAID_X };
1757 if (encode_single_insn(&insn_s2r, &ra, w) != 0) return -19;
1758 if (w[0] != (0x7919 | (2 << 16)) || w[2] != (BW_SR_CTAID_X << 8)) return -20;
1759
1760 /* 11. HMMA.16816.F32 R4, R4, R2, RZ */
1761 BlackwellIRInsn insn_hmma_f16 = { .op = BW_IR_HMMA_F16, .dst_vreg = 1, .src1_vreg = 1, .src2_vreg = 0, .src3_vreg = -1 };
1762 if (encode_single_insn(&insn_hmma_f16, &ra, w) != 0) return -21;
1763 if (w[0] != 0x0404723c || w[1] != 0x00000002 || w[2] != 0x000018ff || w[3] != 0x000fe200) return -22;
1764
1765 /* 12. HMMA.16816.F32.BF16 R4, R4, R2, RZ */
1766 BlackwellIRInsn insn_hmma_bf16 = { .op = BW_IR_HMMA_BF16, .dst_vreg = 1, .src1_vreg = 1, .src2_vreg = 0, .src3_vreg = -1 };
1767 uint32_t w_bf16[4];
1768 if (encode_single_insn(&insn_hmma_bf16, &ra, w_bf16) != 0) return -23;
1769 if (w_bf16[0] != 0x0404723c || w_bf16[1] != 0x00000002 || w_bf16[2] != 0x000418ff || w_bf16[3] != 0x000fe200) return -24;
1770
1771 /* 13. Differential Invariant: w[2] bit 18 toggles strictly between FP16 and BF16 */
1772 if ((w[2] ^ w_bf16[2]) != 0x00040000) return -25;
1773 if (w[0] != w_bf16[0] || w[1] != w_bf16[1] || w[3] != w_bf16[3]) return -26;
1774
1775 /* 14. SHF.R.U32.HI R4, RZ, 2, R2 */
1776 BlackwellIRInsn insn_shf = { .op = BW_IR_SHF_R, .dst_vreg = 1, .src1_vreg = 0, .imm = 2 };
1777 if (encode_single_insn(&insn_shf, &ra, w) != 0) return -27;
1778 if (w[0] != 0xff047819 || w[1] != 2 || w[2] != 0x00011602 || w[3] != 0x001fca00) return -28;
1779
1780 /* 15. LOP3.LUT R4, R2, 3, RZ, 0xc0, !PT */
1781 BlackwellIRInsn insn_lop3 = { .op = BW_IR_LOP3_AND, .dst_vreg = 1, .src1_vreg = 0, .imm = 3 };
1782 if (encode_single_insn(&insn_lop3, &ra, w) != 0) return -29;
1783 if (w[0] != 0x02047812 || w[1] != 3 || w[2] != 0x078ec0ff || w[3] != 0x001fca00) return -30;
1784
1785 /* 16. LDG.E.U16 R2, desc[UR4][R4.64] */
1786 BlackwellIRInsn insn_ldg16 = { .op = BW_IR_LDG_E_U16, .dst_vreg = 0, .src1_vreg = 1, .ureg = 0 };
1787 if (encode_single_insn(&insn_ldg16, &ra, w) != 0) return -31;
1788 if (w[0] != 0x04027981 || w[1] != 4 || w[2] != 0x0c1e1500 || w[3] != 0x000f2200) return -32;
1789
1790 /* A zero-delta branch stays the self-branch the matmul kernels already emit. */
1791 BlackwellIRInsn insn_bra0 = { .op = BW_IR_BRA };
1792 if (encode_single_insn(&insn_bra0, &ra, w) != 0) return -33;
1793 if (w[0] != 0x00fc7947 || w[1] != 0xfffffffc || w[2] != 0x0383ffff || w[3] != 0x000fc000) return -34;
1794
1795 BlackwellIRInsn insn_ge = { .op = BW_IR_ISETP_GE_U32, .src1_vreg = 1, .src2_vreg = 2 };
1796 if (encode_single_insn(&insn_ge, &ra, w) != 0) return -35;
1797 if (w[0] != (0x720c | (4 << 24)) || w[1] != 6 || w[2] != 0x03f06070) return -36;
1798
1799 BlackwellIRInsn insn_xor = { .op = BW_IR_LOP3_XOR, .dst_vreg = 1, .src1_vreg = 0, .src2_vreg = 2 };
1800 if (encode_single_insn(&insn_xor, &ra, w) != 0) return -37;
1801 if (w[0] != (0x7212 | (4 << 16) | (2 << 24)) || w[1] != 6 || w[2] != 0x078e3cff) return -38;
1802
1803 BlackwellIRInsn insn_ldg_ss = { .op = BW_IR_LDG_STRONG_SYS, .dst_vreg = 0, .src1_vreg = 1, .ureg = 0 };
1804 if (encode_single_insn(&insn_ldg_ss, &ra, w) != 0) return -39;
1805 if (w[0] != (0x7981 | (2 << 16) | (4 << 24)) || w[1] != 4 || w[2] != 0x0c1f5900) return -40;
1806 /* 17. FADD R7, R2, R4 */
1807 BlackwellIRInsn insn_fadd = { .op = BW_IR_FADD, .dst_vreg = 3, .src1_vreg = 0, .src2_vreg = 1 };
1808 if (encode_single_insn(&insn_fadd, &ra, w) != 0) return -133;
1809 if (w[0] != 0x02077221 || w[1] != 4 || w[2] != 0 || w[3] != 0x004fc400) return -134;
1810
1811 /* 18. FSUB R7, R2, R4 (negated src2) */
1812 BlackwellIRInsn insn_fsub = { .op = BW_IR_FSUB, .dst_vreg = 3, .src1_vreg = 0, .src2_vreg = 1 };
1813 if (encode_single_insn(&insn_fsub, &ra, w) != 0) return -135;
1814 if (w[0] != 0x02077221 || w[1] != 0x80000004 || w[2] != 0 || w[3] != 0x004fc400) return -136;
1815
1816 /* 19. FMUL R7, R2, R4 */
1817 BlackwellIRInsn insn_fmul = { .op = BW_IR_FMUL, .dst_vreg = 3, .src1_vreg = 0, .src2_vreg = 1 };
1818 if (encode_single_insn(&insn_fmul, &ra, w) != 0) return -137;
1819 if (w[0] != 0x02077220 || w[1] != 4 || w[2] != 0x00400000 || w[3] != 0x004fc400) return -138;
1820
1821 /* 20. FFMA R7, R2, R4, R6 */
1822 BlackwellIRInsn insn_ffma = { .op = BW_IR_FFMA, .dst_vreg = 3, .src1_vreg = 0, .src2_vreg = 1, .src3_vreg = 2 };
1823 if (encode_single_insn(&insn_ffma, &ra, w) != 0) return -139;
1824 if (w[0] != 0x02077223 || w[1] != 4 || w[2] != 6 || w[3] != 0x004fc400) return -140;
1825
1826 /* 21. FMNMX_MIN R7, R2, R4 */
1827 BlackwellIRInsn insn_min = { .op = BW_IR_FMNMX_MIN, .dst_vreg = 3, .src1_vreg = 0, .src2_vreg = 1 };
1828 if (encode_single_insn(&insn_min, &ra, w) != 0) return -141;
1829 if (w[0] != 0x02077209 || w[1] != 4 || w[2] != 0x03800000 || w[3] != 0x004fc400) return -142;
1830
1831 /* 22. FMNMX_MAX R7, R2, R4 */
1832 BlackwellIRInsn insn_max = { .op = BW_IR_FMNMX_MAX, .dst_vreg = 3, .src1_vreg = 0, .src2_vreg = 1 };
1833 if (encode_single_insn(&insn_max, &ra, w) != 0) return -143;
1834 if (w[0] != 0x02077209 || w[1] != 4 || w[2] != 0x07800000 || w[3] != 0x004fc400) return -144;
1835
1836 /* 23. I2FP R7, R2 */
1837 BlackwellIRInsn insn_i2f = { .op = BW_IR_I2FP, .dst_vreg = 3, .src1_vreg = 0 };
1838 if (encode_single_insn(&insn_i2f, &ra, w) != 0) return -145;
1839 if (w[0] != 0x00077245 || w[1] != 2 || w[2] != 0x00201400 || w[3] != 0x004fe200) return -146;
1840
1841 /* 24. F2I R7, R2 */
1842 BlackwellIRInsn insn_f2i = { .op = BW_IR_F2I, .dst_vreg = 3, .src1_vreg = 0 };
1843 if (encode_single_insn(&insn_f2i, &ra, w) != 0) return -147;
1844 if (w[0] != 0x00077305 || w[1] != 2 || w[2] != 0x0020f100 || w[3] != 0x004e2200) return -148;
1845
1846 /* 25. MUFU_RCP R7, R2 */
1847 BlackwellIRInsn insn_mufu_rcp = { .op = BW_IR_MUFU_RCP, .dst_vreg = 3, .src1_vreg = 0 };
1848 if (encode_single_insn(&insn_mufu_rcp, &ra, w) != 0) return -149;
1849 if (w[0] != 0x00077308 || w[1] != 2 || w[2] != 0x00001000 || w[3] != 0x000e2400) return -150;
1850
1851 /* 26. MUFU_RSQ R7, R2 */
1852 BlackwellIRInsn insn_mufu_rsq = { .op = BW_IR_MUFU_RSQ, .dst_vreg = 3, .src1_vreg = 0 };
1853 if (encode_single_insn(&insn_mufu_rsq, &ra, w) != 0) return -151;
1854 if (w[0] != 0x00077308 || w[1] != 2 || w[2] != 0x00001400 || w[3] != 0x000e2400) return -152;
1855
1856 /* 27. SHFL_DOWN R7, R2, 16 */
1857 BlackwellIRInsn insn_shfl = { .op = BW_IR_SHFL_DOWN, .dst_vreg = 3, .src1_vreg = 0, .imm = 16 };
1858 if (encode_single_insn(&insn_shfl, &ra, w) != 0) return -153;
1859 if (w[0] != 0x02077f89 || w[1] != 0x0a001f00 || w[2] != 0x000e0000 || w[3] != 0x000e2400) return -154;
1860
1861 /* 28. LDS R7, [R2] */
1862 BlackwellIRInsn insn_lds = { .op = BW_IR_LDS, .dst_vreg = 3, .src1_vreg = 0, .imm = 0 };
1863 if (encode_single_insn(&insn_lds, &ra, w) != 0) return -155;
1864 if (w[0] != 0x02077984 || w[1] != 0 || w[2] != 0 || w[3] != 0x000fe200) return -156;
1865
1866 /* 29. STS [R2], R4 */
1867 BlackwellIRInsn insn_sts = { .op = BW_IR_STS, .src1_vreg = 0, .src2_vreg = 1, .imm = 0 };
1868 if (encode_single_insn(&insn_sts, &ra, w) != 0) return -157;
1869 if (w[0] != 0x02007388 || w[1] != 4 || w[2] != 0 || w[3] != 0x000fe200) return -158;
1870
1871 return 0;
1872}
1873
1874int omega_blackwell_test_regalloc_bounds(void) {
1875 BlackwellIRProgram prog;
1876 omega_bw_ir_init(&prog);
1877
1878 int v0 = omega_bw_ir_alloc_vreg64(&prog);
1879 int v1 = omega_bw_ir_alloc_vreg(&prog);
1880 int v2 = omega_bw_ir_alloc_vreg64(&prog);
1881 int v3 = omega_bw_ir_alloc_vreg128(&prog); /* Test 128-bit quad allocation */
1882
1883 BlackwellIRInsn i0 = { .op = BW_IR_LDC64, .dst_vreg = v0, .imm = 0x380 };
1884 omega_bw_ir_append(&prog, &i0);
1885 BlackwellIRInsn i1 = { .op = BW_IR_MOV_IMM, .dst_vreg = v1, .imm = 10 };
1886 omega_bw_ir_append(&prog, &i1);
1887 BlackwellIRInsn i2 = { .op = BW_IR_LDC64, .dst_vreg = v2, .imm = 0x388 };
1888 omega_bw_ir_append(&prog, &i2);
1889 BlackwellIRInsn i3 = { .op = BW_IR_HMMA_F16, .dst_vreg = v3, .src1_vreg = v3, .src2_vreg = v0, .src3_vreg = -1 };
1890 omega_bw_ir_append(&prog, &i3);
1891 /* i4 uses v1, v2, v3 simultaneously so all intervals are concurrently active */
1892 int v_sink = omega_bw_ir_alloc_vreg(&prog);
1893 BlackwellIRInsn i4 = { .op = BW_IR_IADD3, .dst_vreg = v_sink, .src1_vreg = v1, .src2_vreg = v2, .src3_vreg = v3 };
1894 omega_bw_ir_append(&prog, &i4);
1895
1896 if (omega_bw_regalloc_solve(&prog) != 0) return -1;
1897
1898 int phys_v0 = prog.regalloc.vreg_to_phys[v0];
1899 int phys_v1 = prog.regalloc.vreg_to_phys[v1];
1900 int phys_v2 = prog.regalloc.vreg_to_phys[v2];
1901 int phys_v3 = prog.regalloc.vreg_to_phys[v3];
1902
1903 if (phys_v0 % 2 != 0) return -2;
1904 if (phys_v2 % 2 != 0) return -3;
1905 if (phys_v3 % 4 != 0) return -4; /* 4-alignment for 128-bit quad */
1906 if (phys_v1 == phys_v0 || phys_v1 == phys_v0 + 1) return -5;
1907 if (phys_v2 == phys_v0 || phys_v2 == phys_v0 + 1) return -6;
1908 for (int k = 0; k < 4; k++) {
1909 if (phys_v3 + k == phys_v0 || phys_v3 + k == phys_v0 + 1) return -7;
1910 if (phys_v3 + k == phys_v2 || phys_v3 + k == phys_v2 + 1) return -8;
1911 if (phys_v3 + k == phys_v1) return -9;
1912 }
1913
1914 /* Test bounds refusal: intentionally attempt to allocate more registers than hardware capacity */
1915 BlackwellIRProgram overflow_prog;
1916 omega_bw_ir_init(&overflow_prog);
1917 for (int i = 0; i < BW_PHYS_GPR_MAX + 5; i++) {
1918 int v = omega_bw_ir_alloc_vreg(&overflow_prog);
1919 if (v < 0) break;
1920 BlackwellIRInsn insn = { .op = BW_IR_MOV_IMM, .dst_vreg = v, .imm = (uint32_t)i };
1921 omega_bw_ir_append(&overflow_prog, &insn);
1922 }
1923 for (size_t i = 0; i < overflow_prog.count; i++) {
1924 for (int v = 0; v < overflow_prog.regalloc.num_vregs; v++) {
1925 overflow_prog.regalloc.intervals[v].last_use = (int)overflow_prog.count - 1;
1926 }
1927 }
1928 if (omega_bw_regalloc_solve(&overflow_prog) == 0) {
1929 return -6;
1930 }
1931
1932 return 0;
1933}
1934
1935#ifndef OMEGA_GPU_NO_POSIX /* prints: hosted only (the freestanding build has no stdio) */
1936int omega_blackwell_test_codegen_variation(void) {
1937 OmegaMatMulSpec spec1, spec2;
1938 if (omega_matmul_spec_init(&spec1, 16, 16, 16, OMEGA_MATMUL_PRECISION_INT32) != 0) {
1939 printf("FAIL: spec1 init failed\n");
1940 return -1;
1941 }
1942 if (omega_matmul_spec_init(&spec2, 32, 16, 64, OMEGA_MATMUL_PRECISION_INT32) != 0) {
1943 printf("FAIL: spec2 init failed\n");
1944 return -1;
1945 }
1946
1947 OmegaBlackwellKernel k1, k2;
1948 memset(&k1, 0, sizeof(k1));
1949 memset(&k2, 0, sizeof(k2));
1950
1951 if (omega_blackwell_codegen_matmul(&spec1, &k1) != 0) {
1952 printf("FAIL: k1 codegen failed\n");
1953 return -1;
1954 }
1955 if (omega_blackwell_codegen_matmul(&spec2, &k2) != 0) {
1956 printf("FAIL: k2 codegen failed\n");
1957 omega_blackwell_kernel_free(&k1);
1958 return -1;
1959 }
1960
1961 OmegaBlackwellRealizationIdentity id1, id2;
1962 omega_blackwell_bind_matmul_realization(&spec1, &k1, &id1);
1963 omega_blackwell_bind_matmul_realization(&spec2, &k2, &id2);
1964
1965 printf("================================================================================\n");
1966 printf(" OMEGA BLACKWELL CODEGEN VARIATION DEMONSTRATION (GATE 7 STAGE-1)\n");
1967 printf("================================================================================\n");
1968 printf("Config 1: 16x16x16 INT32\n");
1969 printf(" Instructions: %zu (%zu bytes, %u GPRs, %u UGPRs)\n", k1.insn_count, k1.code_size, k1.gpr_count, k1.uniform_gpr_count);
1970 printf(" Spec ID: ");
1971 for (int i = 0; i < 16; i++) printf("%02x", spec1.spec_id[i]);
1972 printf("...\n");
1973 printf(" Code Digest: ");
1974 for (int i = 0; i < 16; i++) printf("%02x", k1.code_digest[i]);
1975 printf("...\n");
1976 printf(" Realization: ");
1977 for (int i = 0; i < 16; i++) printf("%02x", id1.realization_id[i]);
1978 printf("...\n\n");
1979
1980 printf("Config 2: 32x16x64 INT32\n");
1981 printf(" Instructions: %zu (%zu bytes, %u GPRs, %u UGPRs)\n", k2.insn_count, k2.code_size, k2.gpr_count, k2.uniform_gpr_count);
1982 printf(" Spec ID: ");
1983 for (int i = 0; i < 16; i++) printf("%02x", spec2.spec_id[i]);
1984 printf("...\n");
1985 printf(" Code Digest: ");
1986 for (int i = 0; i < 16; i++) printf("%02x", k2.code_digest[i]);
1987 printf("...\n");
1988 printf(" Realization: ");
1989 for (int i = 0; i < 16; i++) printf("%02x", id2.realization_id[i]);
1990 printf("...\n");
1991 printf("================================================================================\n");
1992
1993 /* Validation checks */
1994 if (memcmp(spec1.spec_id, spec2.spec_id, 32) == 0) {
1995 printf("FAIL: spec IDs match (expected distinct)\n");
1996 return -2;
1997 }
1998 if (memcmp(k1.code_digest, k2.code_digest, 32) == 0) {
1999 printf("FAIL: code digests match (expected distinct)\n");
2000 return -3;
2001 }
2002 if (memcmp(id1.realization_id, id2.realization_id, 32) == 0) {
2003 printf("FAIL: realization IDs match (expected distinct)\n");
2004 return -4;
2005 }
2006
2007 printf("PASS: Dynamic codegen variation verified (distinct specifications, code digests, and realization IDs).\n");
2008
2009 omega_blackwell_kernel_free(&k1);
2010 omega_blackwell_kernel_free(&k2);
2011 return 0;
2012}
2013#endif
2014
2015/* Declared registers: peak + 2 rounded up to 8 (see omega_blackwell_codegen.h for the chip evidence). */
2016uint32_t omega_bw_gpr_declared(uint32_t peak) { return ((peak + 2u + 7u) / 8u) * 8u; }
2017int omega_bw_gpr_declared_ok(uint32_t declared, uint32_t peak) { return declared % 8u == 0u && declared >= peak + 2u; }
2018
2019int omega_bw_ir_set_gpr_limit(BlackwellIRProgram *prog, uint32_t limit) {
2020 if (!prog || limit <= BW_PHYS_GPR_MAX || limit > BW_PHYS_GPR_HW_LIMIT) return -1;
2021 prog->regalloc.gpr_limit = limit;
2022 return 0;
2023}