interplane.Get Omega
Read in your language

CUDApp

CUDA++: AIEN’s GPU machine-code toolchain, runtime and driver client for NVIDIA GPUs without the CUDA toolkit. Hardware coverage and driver replacement limits are explicit.

Brought by drakestapleton

branch main Branches Tags History

CUDApp / src / omega_gpu_cmdlist.c

987 lines · 57592 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */
2/*
3 * omega_gpu_cmdlist.c: many launches in one submission (see the header).
4 *
5 * The per-launch words reuse the session's chip-proven pieces (omega_gpu_session.c, read 2026-10-13): SETUP_WORDS,
6 * INVALIDATE_SHADER_CACHES_ALL, the QMD0/QMD1 builders, the C3 tail (first marker with RELEASE_WFI, L2_FLUSH_DIRTY
7 * mem-op, second marker on the uncached page) and the poll loop. What is new: banks and QMDs live in per-launch slots
8 * written by the CPU, WAIT_FOR_IDLE sits between launches, and the PCAS launch method.
9 */
10#include "omega_gpu_cmdlist.h"
11#include "omega_gpu_cmdlist_native.h"
12#include "omega_blackwell_codegen.h"
13#include "omega_blackwell_qmd.h"
14#include "omega_blackwell_submit.h"
15#include "omega_gpu_native_runtime.h"
16#include "cudapp_native.h"
17#include "omega_no_posix_session.h"
18#include <string.h>
19
20/* Command-list bookkeeping decisions are Omega (om/cmdlist/book.om). The C
21 * copies are the default-OFF differential oracle. */
22#if (defined(OMEGA_GPU_NO_POSIX) && CUDAPP_MIGRATION_PENDING_C_NO_POSIX_SESSION) || defined(CUDAPP_MIGRATION_PENDING_C_CMDLIST2) || defined(CUDAPP_MIGRATION_PENDING_CMDLIST2_DIFF)
23#define CMDLIST2_C_ORACLE 1
24#endif
25
26#ifdef CMDLIST2_C_ORACLE
27static uint64_t clbook_c(uint64_t op, uint64_t i0, uint64_t i1, uint64_t i2, uint64_t i3) {
28 if (op == 0) return (i0 == i1 && i2 == i3) ? 0u : 1u;
29 if (op == 1) return i0 == (uint32_t)(i1 + 1u) ? 1u : 0u;
30 return UINT64_MAX;
31}
32#endif
33
34static uint64_t clbook(uint64_t op, uint64_t i0, uint64_t i1, uint64_t i2, uint64_t i3) {
35#if (defined(OMEGA_GPU_NO_POSIX) && CUDAPP_MIGRATION_PENDING_C_NO_POSIX_SESSION) || defined(CUDAPP_MIGRATION_PENDING_C_CMDLIST2)
36 return clbook_c(op, i0, i1, i2, i3);
37#else
38 uint64_t r = 0, a[16] = {i0, i1, i2, i3}, o[4] = {0};
39 if (omega_book(&r, op, a, 16, o, 4) != 0) return UINT64_MAX;
40 return r;
41#endif
42}
43
44/* C4 streams (CUDApp#115): the method words of a submission (setup, wait_for ACQUIRE, barrier choice, stamps, PCAS or
45 * inline QMD headers, completion tail) and the submit_pair marker layout come from seeded Omega, om/streams/streams.om,
46 * through the generated binding src/cudapp_om_streams.c. This file still writes them into the ring and submits.
47 * The C generator below is migration-pending, the differential oracle only:
48 * CUDAPP_MIGRATION_PENDING_C_STREAMS the C generator alone;
49 * CUDAPP_MIGRATION_PENDING_STREAMS_DIFF both on every chunk, abort on any word difference
50 * (a SAME line per chunk to $CUDAPP_STREAMS_DIFF_LOG). */
51#if defined(CUDAPP_MIGRATION_PENDING_C_STREAMS) && defined(CUDAPP_MIGRATION_PENDING_STREAMS_DIFF)
52#error "CUDAPP_MIGRATION_PENDING_STREAMS_DIFF compares C against Omega: not with CUDAPP_MIGRATION_PENDING_C_STREAMS"
53#endif
54#if defined(CUDAPP_MIGRATION_PENDING_C_STREAMS) || defined(CUDAPP_MIGRATION_PENDING_STREAMS_DIFF)
55#define STREAMS_C_ORACLE 1
56#endif
57#ifdef CUDAPP_MIGRATION_PENDING_STREAMS_DIFF
58#include "omega_gpu_fmt.h"
59/* Diff builds only: the driver (tests/streams_diff_test.c) logs a SAME line, or prints a difference and aborts. This
60 * file stays free of hosted includes (tools/portability table). */
61void cudapp_streams_diff_note(int same, const char *line);
62#endif
63#define TAIL_WORDS 17u
64#define STAMP_WORDS 6u
65
66#define PAGE 0x1000ull
67#define RING_WORDS (0x10000u / 4u) /* the session ring: 64 KB */
68#define DEFAULT_TIMEOUT_MS 20000ull
69#ifdef STREAMS_C_ORACLE
70#define MTHD_WAIT_FOR_IDLE 0x0110u /* clcec0.h NVCEC0_WAIT_FOR_IDLE */
71#endif
72/* Completion markers live on the SESSION marker page (OmegaGpuSession.marker, allocated by the session with
73 * OMEGA_GPU_RIGHT_HOST_POLLED, GPU-uncached): the platform refuses poll_word on any buffer without that right
74 * (omega_gpu_platform_linux.c ref_poll_word, chip run cmdlist-r1 2026-10-13: "marker poll refused"), and the session
75 * exposes no polled allocation. The session uses bytes 0..0x1f of the page; this module uses 0x100..0x11f. */
76#define MARKER_OFF 0x100ull
77#ifdef STREAMS_C_ORACLE
78#define MTHD_SEND_PCAS_A 0x02b4u /* clcec0.h NVCEC0_SEND_PCAS_A, QMD_ADDRESS_SHIFTED8 */
79#define MTHD_SEND_SIGNALING_PCAS2_B 0x02c0u /* clcec0.h, PCAS_ACTION 3:0 */
80#define PCAS_ACTION_INVALIDATE_COPY_SCHEDULE 3u
81
82static inline uint32_t bw_mthd(uint32_t subc, uint32_t mthd, uint32_t count) {
83 return (2u << 28) | (count << 16) | (subc << 13) | (mthd >> 2);
84}
85/* Same words as omega_gpu_session.c / omega_blackwell_submit.c SETUP_WORDS (compute class setup). */
86static const uint32_t SETUP_WORDS[18] = {
87 0x20012061, 0x0000cec0, 0x20012092, 0x00000001, 0x200120a8, 0x0000000f, 0x2001255d, 0x00000003,
88 0x2001255e, 0x20000000, 0x2001255f, 0x000fffff, 0x20012557, 0x00000003, 0x20012558, 0x22000000,
89 0x20012559, 0x00000000,
90};
91#endif
92
93typedef struct {
94 OmegaGpuNativeCache *cache;
95 uint64_t state[16];
96 uint64_t code_va; /* successful add() intent, independently checked against actual QMD in .om */
97 uint32_t n_args; /* exact successful add() fact, including incumbent entries */
98 uint8_t intent_seal[32]; /* generated normalized bank/QMD binding at native add */
99} NativeRef;
100struct OmegaGpuCmdList {
101 uint32_t cap, n;
102 OmegaGpuMem banks, qmds, sems, scratch;
103 OmegaGpuMem stamps; /* (cap + 1) x 16 B: compute-class REPORT_SEMAPHORE four-word reports {u64 payload, u64 timestamp}
104 (mesa clcec0.h 0x0158..0x0168, nvk_query.h), slot 0 = submit start, slot i + 1 = launch i done */
105 uint32_t *words; /* host method stream */
106 uint32_t words_cap;
107 OmegaGpuMem nop_code; /* EXIT kernel used to force the session latch after a timeout */
108 uint32_t nop_gpr;
109 uint8_t *indep; /* per launch: 1 = shares no data with the launch before it (mark_independent): no barrier before it */
110 int stamped; /* the last submit had opts.stamps (the accessor refuses stale stamps from an earlier submit) */
111 uint64_t *wait_va; /* per launch: semaphore word of another list's launch to wait for before it (wait_for), 0 = none */
112 uint64_t pending_wait;/* wait_for recorded, taken by the next add */
113 uint32_t nwaits; /* launches with a wait (6 more words each) */
114 const OmegaGpuCmdList *wait_other; /* the one list this list waits on (it must be the other list of the pair) */
115 uint32_t wait_max; /* highest launch index of wait_other waited on */
116 uint32_t gen; /* omega_gpu_session_recoveries() at create: a list from before a recovery is stale (C5) */
117 uint64_t epoch; /* normal close/reopen also invalidates device handles */
118 uint64_t record_generation; /* advanced only by generated policy on safe reset */
119 NativeRef *native; /* per-launch exact code ownership, metadata only */
120 uint8_t *seal_cache; /* optional per-slot normalized byte memo; no ownership authority */
121 uint64_t *program; /* bounded bridge descriptor view, allocated only for program API */
122 uint64_t *barriers; /* generated hazard classes, one per launch */
123 int program_active; /* only while submit_program consumes the generated plan */
124};
125
126#define CMDLIST_DESCRIPTOR_WORDS 49u
127#define CMDLIST_PROGRAM_WORDS (OMEGA_GPU_CMDLIST_MAX_LAUNCHES * CMDLIST_DESCRIPTOR_WORDS)
128static uint64_t list_layout(uint64_t op, const uint64_t a[16], uint64_t out[16]) {
129 uint64_t r = UINT64_MAX;
130 if (omega_cmdlist_layout(&r, op, a, 16, out, 16) != OMEGA_OK) return UINT64_MAX;
131 return r;
132}
133static uint32_t list_room(const OmegaGpuCmdList *cl, const OmegaGpuCmdListOpts *o, uint32_t prefix) {
134#ifdef CUDAPP_MIGRATION_PENDING_CMDLIST
135 uint32_t per = (o->mode == OMEGA_GPU_CMDLIST_INLINE ? 202u : 8u) + (o->stamps ? 6u : 0u) + (cl->nwaits ? 6u : 0u);
136 if (cl->words_cap < 43u || prefix > cl->words_cap - 43u) return 0;
137 return (cl->words_cap - 43u - prefix) / per;
138#else
139 uint64_t a[16] = {cl->words_cap, prefix, o->mode, o->stamps ? 1u : 0u, cl->nwaits}, out[16];
140 uint64_t room = list_layout(1, a, out);
141 return room == UINT64_MAX ? 0 : (uint32_t)room;
142#endif
143}
144static int program_storage(OmegaGpuCmdList *cl) {
145 if (!cl->program) {
146 cl->program = omega_gpu_host_calloc(CMDLIST_PROGRAM_WORDS, sizeof *cl->program);
147 if (!cl->program) return -1;
148 for (uint32_t i = 0; i < cl->n; i++) cl->program[i * CMDLIST_DESCRIPTOR_WORDS] = 17;
149 }
150 if (!cl->barriers) cl->barriers = omega_gpu_host_calloc(OMEGA_GPU_CMDLIST_MAX_LAUNCHES, sizeof *cl->barriers);
151 return cl->barriers ? 0 : -1;
152}
153
154static uint64_t next_record_generation(uint64_t previous) {
155 uint64_t next = 0;
156 return omega_native_revision(&next, previous) == OMEGA_OK ? next : 0;
157}
158
159static uint32_t g_latched; /* 1 + omega_gpu_session_recoveries() at the latch; 0 never latched (C5: a recovery lifts it) */
160
161int omega_gpu_cmdlist_is_latched(void) { return clbook(1, __atomic_load_n(&g_latched, __ATOMIC_ACQUIRE), omega_gpu_session_recoveries(), 0, 0) ? 1 : 0; }
162uint32_t omega_gpu_cmdlist_count(const OmegaGpuCmdList *cl) { return cl ? cl->n : 0; }
163static int native_event(NativeRef *ref, unsigned event) {
164 if (!ref->cache) return 0;
165 uint64_t result = 0;
166 return omega_native_event(&result, ref->cache->pool, 528, ref->state, 16,
167 omega_gpu_session_epoch(), event) == OMEGA_OK && result == 0 ? 0 : -1;
168}
169static int native_events(OmegaGpuCmdList *cl, uint32_t first, uint32_t end, unsigned event) {
170 for (uint32_t i = first; i < end; i++)
171 if (native_event(&cl->native[i], event) != 0) return -1;
172 return 0;
173}
174static int native_begin(OmegaGpuCmdList *cl, uint32_t first, uint32_t end) {
175 uint32_t i;
176 for (i = first; i < end; i++) if (native_event(&cl->native[i], 1) != 0) break;
177 if (i == end) return 0;
178 (void)native_events(cl, first, i, 4); /* none of this chunk was submitted */
179 omega_gpu_session_set_error("cmdlist: native code lease refused before submit");
180 return -1;
181}
182void omega_gpu_cmdlist_reset(OmegaGpuCmdList *cl) {
183 if (!cl) return;
184 if (native_events(cl, 0, cl->n, 5) != 0) {
185 omega_gpu_session_set_error("cmdlist reset: uncertain native flight still owns list buffers"); return;
186 }
187 const uint64_t next = next_record_generation(cl->record_generation);
188 if (!next) {
189 omega_gpu_session_set_error("cmdlist reset: record generation exhausted"); return;
190 }
191 (void)native_events(cl, 0, cl->n, 3);
192 memset(cl->native, 0, (size_t)cl->cap * sizeof *cl->native);
193 cl->n = 0; cl->pending_wait = 0; cl->nwaits = 0; cl->wait_other = NULL; cl->wait_max = 0;
194 cl->record_generation = next;
195}
196/* C5: every handle of a list made before the latest session recovery belongs to the closed device */
197static int stale(const OmegaGpuCmdList *cl) {
198 if (clbook(0, cl->gen, omega_gpu_session_recoveries(), cl->epoch, omega_gpu_session_epoch()) == 0) return 0;
199 omega_gpu_session_set_error("cmdlist: made before a session recovery (its buffers are gone): create a new list");
200 return 1;
201}
202int omega_gpu_cmdlist_owner(const OmegaGpuCmdList *cl, OmegaGpuCmdListOwner *out) {
203 if (!cl || !out || stale(cl)) return -1;
204 out->banks = cl->banks; out->qmds = cl->qmds; out->semaphores = cl->sems;
205 out->scratch = cl->scratch; out->stamps = cl->stamps;
206 out->record_generation = cl->record_generation;
207 out->capacity = cl->cap; out->count = cl->n;
208 return 0;
209}
210int omega_gpu_cmdlist_entry(const OmegaGpuCmdList *cl, uint32_t index, OmegaGpuCmdListEntry *out) {
211 if (!cl || !out || index >= cl->n || stale(cl)) return -1;
212 out->bank = (const uint8_t *)cl->banks.cpu + (size_t)index * OMEGA_GPU_CMDLIST_BANK_BYTES;
213 out->qmd_slot = (const uint8_t *)cl->qmds.cpu + (size_t)index * OMEGA_GPU_CMDLIST_QMD_BYTES;
214 out->intent_seal = cl->native[index].intent_seal;
215 out->code_va = cl->native[index].code_va;
216 out->pending_wait = cl->wait_va[index]; out->record_generation = cl->record_generation;
217 out->ordinal = index; out->count = cl->n; out->n_args = cl->native[index].n_args;
218 out->independent = cl->indep[index];
219 return 0;
220}
221int omega_gpu_cmdlist_wait_for(OmegaGpuCmdList *cl, const OmegaGpuCmdList *other, uint32_t index) {
222 if (!cl || !other || cl == other || index >= other->n) { omega_gpu_session_set_error("cmdlist wait_for: another list and one of its launches"); return -1; }
223 if (stale(cl) || stale(other)) return -1;
224 if (cl->wait_other && cl->wait_other != other) { omega_gpu_session_set_error("cmdlist wait_for: a list waits on one other list only"); return -1; }
225 cl->wait_other = other;
226 if (index > cl->wait_max) cl->wait_max = index;
227 cl->pending_wait = other->sems.va + (uint64_t)index * 16u;
228 return 0;
229}
230int omega_gpu_cmdlist_mark_independent(OmegaGpuCmdList *cl) {
231 if (!cl || cl->n == 0) { omega_gpu_session_set_error("cmdlist mark_independent: no launch recorded"); return -1; }
232 cl->indep[cl->n - 1] = 1;
233 return 0;
234}
235
236static int build_nop(OmegaGpuCmdList *cl) {
237 BlackwellIRProgram *prog = omega_gpu_host_calloc(1, sizeof *prog);
238 if (!prog) return -1;
239 omega_bw_ir_init(prog);
240 BlackwellIRInsn ex; memset(&ex, 0, sizeof ex);
241 ex.op = BW_IR_EXIT; ex.dst_vreg = ex.src1_vreg = ex.src2_vreg = ex.src3_vreg = ex.ureg = -1;
242 ex.control = (5u << 9) | (7u << 14) | (7u << 17);
243 BlackwellIRInsn self = ex; self.op = BW_IR_BRA; self.control = 0x000fc000u;
244 int rc = (omega_bw_ir_append(prog, &ex) < 0 || omega_bw_ir_append(prog, &self) < 0 || omega_bw_regalloc_solve(prog) != 0) ? -1 : 0;
245 size_t need = rc == 0 ? omega_bw_program_code_bytes(prog) : 0;
246 if (need == 0 || omega_gpu_session_alloc_code(need, &cl->nop_code) != 0) { omega_gpu_host_free(prog); return -1; }
247 OmegaBlackwellKernel k;
248 rc = omega_blackwell_kernel_encode_into(prog, cl->nop_code.cpu, need, &k);
249 omega_gpu_host_free(prog);
250 if (rc != 0 || omega_gpu_session_sync_to_gpu(&cl->nop_code) != 0) return -1;
251 cl->nop_gpr = omega_bw_gpr_declared(k.gpr_count);
252 if (cl->nop_gpr < 64u) cl->nop_gpr = 64u;
253 return 0;
254}
255
256int omega_gpu_cmdlist_create(uint32_t cap, OmegaGpuCmdList **out) {
257 uint64_t shape[16] = {cap}, sizes[16];
258 if (!out || list_layout(0, shape, sizes) != 0) { omega_gpu_session_set_error("cmdlist create: arguments"); return -1; }
259 *out = NULL;
260 OmegaGpuCmdList *cl = omega_gpu_host_calloc(1, sizeof *cl);
261 if (!cl) return -1;
262 cl->cap = cap;
263 cl->record_generation = next_record_generation(0);
264 if (!cl->record_generation) { omega_gpu_host_free(cl); return -1; }
265 cl->words_cap = RING_WORDS;
266 cl->words = omega_gpu_host_alloc((size_t)cl->words_cap * 4u);
267 cl->indep = omega_gpu_host_calloc(cap, 1);
268 cl->wait_va = omega_gpu_host_calloc(cap, sizeof *cl->wait_va);
269 cl->native = omega_gpu_host_calloc(cap, sizeof *cl->native);
270 cl->seal_cache = omega_gpu_host_calloc(cap, 2048u);
271 int rc = cl->words && cl->indep && cl->wait_va && cl->native ? 0 : -1;
272 omega_gpu_session_lock();
273 if (rc == 0 && !omega_gpu_session_open()) rc = -1;
274 if (rc == 0 && (omega_gpu_session_alloc((size_t)sizes[0], &cl->banks) != 0 ||
275 omega_gpu_session_alloc((size_t)sizes[0], &cl->qmds) != 0 ||
276 omega_gpu_session_alloc((size_t)sizes[1], &cl->sems) != 0 ||
277 omega_gpu_session_alloc((size_t)sizes[2], &cl->stamps) != 0 ||
278 omega_gpu_session_alloc(0x4000, &cl->scratch) != 0))
279 rc = -1;
280 if (rc == 0 && build_nop(cl) != 0) { omega_gpu_session_set_error("cmdlist create: nop kernel"); rc = -1; }
281 if (rc == 0) {
282 /* Bind lifetime to the locked session that allocated these buffers. */
283 cl->gen = omega_gpu_session_recoveries();
284 cl->epoch = omega_gpu_session_epoch();
285 memset(cl->scratch.cpu, 0, 0x4000);
286 if (omega_gpu_session_sync_to_gpu(&cl->scratch) != 0) rc = -1;
287 }
288 omega_gpu_session_unlock();
289 if (rc != 0) { omega_gpu_cmdlist_free(cl); return -1; }
290 *out = cl;
291 return 0;
292}
293
294void omega_gpu_cmdlist_free(OmegaGpuCmdList *cl) {
295 if (!cl) return;
296 omega_gpu_session_lock();
297 if (cl->native) (void)native_events(cl, 0, cl->n, 3); /* timeout retains pool flight refs until device teardown */
298 OmegaGpuMem *m[] = { &cl->banks, &cl->qmds, &cl->sems, &cl->stamps, &cl->scratch, &cl->nop_code };
299 for (unsigned i = 0; i < sizeof m / sizeof m[0]; i++) if (m[i]->cpu) omega_gpu_session_free(m[i]);
300 omega_gpu_session_unlock();
301 omega_gpu_host_free(cl->words);
302 omega_gpu_host_free(cl->indep);
303 omega_gpu_host_free(cl->wait_va);
304 omega_gpu_host_free(cl->native);
305 omega_gpu_host_free(cl->seal_cache);
306 omega_gpu_host_free(cl->program);
307 omega_gpu_host_free(cl->barriers);
308 omega_gpu_host_free(cl);
309}
310
311#ifdef CUDAPP_MIGRATION_PENDING_CMDLIST
312static uint32_t round8(uint32_t v) { return (v + 7u) / 8u * 8u; }
313#endif
314
315static int add_recorded(OmegaGpuCmdList *cl, const OmegaGpuLaunch *L, NativeRef *pending) {
316 if (!cl || !L || !L->args) { omega_gpu_session_set_error("cmdlist add: arguments"); return -1; }
317 if (stale(cl)) return -1;
318#ifdef CUDAPP_MIGRATION_PENDING_CMDLIST
319 if (L->n_args == 0 || L->n_args > OMEGA_GPU_SESSION_MAX_ARGS || L->threads_x == 0 || L->grid_x == 0 || L->grid_y == 0 || L->code_va == 0) {
320 omega_gpu_session_set_error("cmdlist add: arguments"); return -1;
321 }
322 if (cl->n >= cl->cap) { omega_gpu_session_set_error("cmdlist add: list full"); return -1; }
323 const uint32_t i = cl->n;
324 const uint32_t ty = L->threads_y ? L->threads_y : 1;
325 const uint64_t bank_va = cl->banks.va + (uint64_t)i * OMEGA_GPU_CMDLIST_BANK_BYTES;
326 uint8_t *bank = (uint8_t *)cl->banks.cpu + (size_t)i * OMEGA_GPU_CMDLIST_BANK_BYTES;
327 uint32_t drv[OMEGA_BW_CBANK_DRIVER_WORDS];
328 omega_blackwell_build_cbank_driver_2d(drv, bank_va, L->threads_x, ty, L->grid_x, L->grid_y);
329 memcpy(bank, drv, sizeof drv);
330 memset(bank + sizeof drv, 0, 0x380 - sizeof drv);
331 memcpy(bank + 0x380, L->args, (size_t)L->n_args * 4u);
332
333 const uint64_t q1_va = cl->qmds.va + (uint64_t)i * OMEGA_GPU_CMDLIST_QMD_BYTES, q0_va = q1_va + 0x200;
334 const uint64_t sem_va = cl->sems.va + (uint64_t)i * 16u;
335 uint32_t gpr = L->gpr_count > 64u ? round8(L->gpr_count) : 64u;
336 OmegaBlackwellQmdConfig cfg = {
337 .code_va = L->code_va, .cbank_va = bank_va, .scratch_va = cl->scratch.va, .sem_va = sem_va,
338 .qmd0_va = q0_va, .qmd1_va = q1_va, .num_elements = (uint32_t)L->num_elements,
339 .threads_per_block = L->threads_x, .grid_width = L->grid_x * L->grid_y, .threads_x = L->threads_x, .threads_y = ty,
340 .grid_x = L->grid_x, .grid_y = L->grid_y, .gpr_count = gpr, .shared_bytes = L->shared_bytes,
341 };
342 uint32_t q1[OMEGA_BW_QMD_WORDS], q0[OMEGA_BW_QMD_WORDS];
343 if (omega_blackwell_build_qmd1(q1, &cfg) != 0 || omega_blackwell_verify_qmd_invariants(q1) != 0) { omega_gpu_session_set_error("cmdlist add: qmd"); return -1; }
344 omega_blackwell_build_qmd0(q0, q0_va, q1_va);
345 uint8_t *qs = (uint8_t *)cl->qmds.cpu + (size_t)i * OMEGA_GPU_CMDLIST_QMD_BYTES;
346 memcpy(qs, q1, sizeof q1);
347 memcpy(qs + 0x200, q0, sizeof q0);
348#else
349 uint64_t a[16] = {cl->cap, cl->n, L->n_args, L->threads_x, L->threads_y, L->grid_x, L->grid_y,
350 L->code_va, L->gpr_count, L->shared_bytes, cl->banks.va, cl->qmds.va, cl->sems.va, cl->scratch.va};
351 uint64_t args[OMEGA_GPU_SESSION_MAX_ARGS] = {0}, record[512], result = 1;
352 for (uint32_t j = 0; j < L->n_args && j < OMEGA_GPU_SESSION_MAX_ARGS; j++) args[j] = L->args[j];
353 if (omega_cmdlist_record(&result, a, 16, args, OMEGA_GPU_SESSION_MAX_ARGS, record, 512) != OMEGA_OK || result != 0) {
354 omega_gpu_session_set_error(result == 2 ? "cmdlist add: list full" : result == 3 ? "cmdlist add: qmd" : "cmdlist add: arguments");
355 return -1;
356 }
357 const uint32_t i = cl->n;
358 uint8_t *bank = (uint8_t *)cl->banks.cpu + (size_t)i * OMEGA_GPU_CMDLIST_BANK_BYTES;
359 uint8_t *qs = (uint8_t *)cl->qmds.cpu + (size_t)i * OMEGA_GPU_CMDLIST_QMD_BYTES;
360 for (uint32_t j = 0; j < 256; j++) {
361 ((uint32_t *)bank)[j] = (uint32_t)record[j];
362 ((uint32_t *)qs)[j] = (uint32_t)record[256 + j];
363 }
364#endif
365 if (pending) {
366 uint64_t result = 0;
367 uint32_t seal_rc = cl->seal_cache ?
368 omega_native_mcreseal(&result, bank, OMEGA_GPU_CMDLIST_BANK_BYTES,
369 qs, OMEGA_GPU_CMDLIST_QMD_BYTES, L->n_args,
370 cl->seal_cache + (size_t)i * 2048u, 2048u, pending->intent_seal, 32) :
371 omega_native_mcseal(&result, bank, OMEGA_GPU_CMDLIST_BANK_BYTES,
372 qs, OMEGA_GPU_CMDLIST_QMD_BYTES, L->n_args, pending->intent_seal, 32);
373 if (seal_rc != OMEGA_OK ||
374 result != 0) return -1;
375 pending->n_args = L->n_args;
376 pending->code_va = L->code_va;
377 cl->native[i] = *pending;
378 } else {
379 cl->native[i].n_args = L->n_args;
380 cl->native[i].code_va = L->code_va;
381 }
382 cl->indep[cl->n] = 0;
383 cl->wait_va[cl->n] = cl->pending_wait;
384 if (cl->pending_wait) cl->nwaits++;
385 cl->pending_wait = 0;
386 if (cl->program) cl->program[cl->n * CMDLIST_DESCRIPTOR_WORDS] = 17;
387 cl->n++;
388 return (int)i;
389}
390int omega_gpu_cmdlist_add(OmegaGpuCmdList *cl, const OmegaGpuLaunch *L) {
391 return add_recorded(cl, L, NULL);
392}
393
394int omega_gpu_cmdlist_add_accesses(OmegaGpuCmdList *cl, const OmegaGpuLaunch *L,
395 const OmegaGpuCmdListAccess *accesses, uint32_t count) {
396 if (!cl || !L || (count && !accesses) || count > OMEGA_GPU_CMDLIST_MAX_ACCESSES ||
397 cl->n >= cl->cap || stale(cl) || program_storage(cl) != 0) return -1;
398 uint64_t *p = cl->program + cl->n * CMDLIST_DESCRIPTOR_WORDS;
399 p[0] = count;
400 for (uint32_t j = 0; j < count; j++) {
401 p[1 + 3*j] = accesses[j].va; p[2 + 3*j] = accesses[j].bytes; p[3 + 3*j] = accesses[j].flags;
402 }
403 uint64_t r = 0;
404 if (omega_cmdlist_access(&r, cl->program, CMDLIST_PROGRAM_WORDS, cl->n) != OMEGA_OK || r != 1) {
405 omega_gpu_session_set_error("cmdlist add: invalid memory access set"); return -1;
406 }
407 int index = add_recorded(cl, L, NULL);
408 if (index >= 0) p[0] = count; /* add_recorded marks legacy entries unknown */
409 return index;
410}
411int omega_gpu_cmdlist_set_accesses(OmegaGpuCmdList *cl, uint32_t index,
412 const OmegaGpuCmdListAccess *accesses, uint32_t count) {
413 if (!cl || index >= cl->n || (count && !accesses) ||
414 count > OMEGA_GPU_CMDLIST_MAX_ACCESSES || stale(cl) || program_storage(cl) != 0) return -1;
415 uint64_t *p = cl->program + index * CMDLIST_DESCRIPTOR_WORDS, saved[CMDLIST_DESCRIPTOR_WORDS];
416 memcpy(saved, p, sizeof saved);
417 p[0] = count;
418 for (uint32_t j = 0; j < count; j++) {
419 p[1 + 3*j] = accesses[j].va; p[2 + 3*j] = accesses[j].bytes; p[3 + 3*j] = accesses[j].flags;
420 }
421 uint64_t r = 0;
422 if (omega_cmdlist_access(&r, cl->program, CMDLIST_PROGRAM_WORDS, index) != OMEGA_OK || r != 1) {
423 memcpy(p, saved, sizeof saved);
424 omega_gpu_session_set_error("cmdlist set: invalid memory access set"); return -1;
425 }
426 return 0;
427}
428
429
430int omega_gpu_cmdlist_plan_program(OmegaGpuCmdList *cl, const OmegaGpuCmdListOpts *opts,
431 OmegaGpuCmdListProgramPlan *plan) {
432 OmegaGpuCmdListOpts o = {0};
433 if (opts) o = *opts;
434 if (!cl || !plan || stale(cl) || program_storage(cl) != 0) return -1;
435 uint64_t a[16] = {cl->n, cl->cap, o.mode, o.stamps ? 1u : 0u, cl->words_cap,
436 omega_gpu_session_submit_prefix_words(), o.barrier, (uint64_t)o.skip_launch,
437 o.first_barrier_none ? 1u : 0u, cl->nwaits}, out[16], r = 1;
438 if (omega_cmdlist_plan(&r, a, 16, cl->program, CMDLIST_PROGRAM_WORDS, cl->barriers,
439 OMEGA_GPU_CMDLIST_MAX_LAUNCHES, out, 16) != OMEGA_OK || r != 0) {
440 omega_gpu_session_set_error("cmdlist program: invalid options/accesses or program does not fit one submission"); return -1;
441 }
442 plan->launches = cl->n; plan->words = (uint32_t)out[0];
443 plan->waits = (uint32_t)out[1]; plan->submissions = (uint32_t)out[2];
444 return 0;
445}
446
447int omega_gpu_cmdlist_add_native(OmegaGpuCmdList *cl, const OmegaGpuLaunch *L, const OmegaGpuNativeLease *lease) {
448 if (!cl || !lease || !lease->cache || cl->n >= cl->cap || stale(cl)) return -1;
449 NativeRef pending; memset(&pending, 0, sizeof pending);
450 pending.cache = lease->cache;
451 pending.state[0] = 1; pending.state[1] = lease->slot;
452 pending.state[2] = lease->revision; pending.state[3] = lease->epoch;
453 if (native_event(&pending, 0) != 0) return -1;
454 int index = add_recorded(cl, L, &pending);
455 if (index < 0) { (void)native_event(&pending, 3); return -1; }
456 return index;
457}
458
459#define WAIT_OK 0
460#define WAIT_TIMED_OUT 2
461#define WAIT_REFUSED 3
462
463static inline void cpu_relax(void) {
464#if defined(__aarch64__)
465 __asm__ volatile("yield" ::: "memory");
466#else
467 __asm__ volatile("" ::: "memory");
468#endif
469}
470
471static int wait_word(const OmegaGpuPlatform *p, const OmegaGpuMem *marker, uint64_t off, uint32_t want, uint64_t timeout_ms, uint32_t spin_us) {
472 off += MARKER_OFF;
473 volatile uint32_t *m = (volatile uint32_t *)((uint8_t *)marker->cpu + off);
474 if (spin_us) {
475 const uint64_t end = p->time_now_ns(p->ctx) + (uint64_t)spin_us * 1000u;
476 do { if ((int32_t)(*m - want) >= 0) return WAIT_OK; cpu_relax(); } while (p->time_now_ns(p->ctx) < end);
477 }
478 uint64_t waited = 0;
479 for (;;) {
480 uint32_t seen = 0;
481 int rc = p->poll_word(p->ctx, marker->handle, off, want, 1, &seen);
482 if (rc == OMEGA_GPU_PLAT_OK) return WAIT_OK;
483 if (rc != OMEGA_GPU_POLL_TIMED_OUT) return WAIT_REFUSED;
484 if (++waited >= timeout_ms) return WAIT_TIMED_OUT;
485 }
486}
487
488/* Uncertain completion: latch this module, then hand the session a 1 ms launch. The stuck list is unretired, so the
489 * platform refuses that submission REFUSED_BUSY before it writes the ring or rings a doorbell (caps.max_inflight 1,
490 * CudaAuditFable Q6), and the session latches the whole process (parks every buffer, refuses every API). The launch
491 * only writes session-owned cbank, QMD and marker bytes 0..0x1f, none of which the list uses. */
492static void force_latch(OmegaGpuCmdList *cl, const char *why) {
493 __atomic_store_n(&g_latched, omega_gpu_session_recoveries() + 1u, __ATOMIC_RELEASE);
494 uint32_t arg = 0;
495 OmegaGpuLaunch L = { .code_va = cl->nop_code.va, .gpr_count = cl->nop_gpr, .threads_x = 32, .threads_y = 1, .grid_x = 1, .grid_y = 1,
496 .num_elements = 1, .args = &arg, .n_args = 1, .timeout_ms = 1 };
497 (void)omega_gpu_session_launch(&L, NULL, NULL);
498 omega_gpu_session_note_module_latch(); /* C5: so omega_gpu_session_recover lifts it even when the check launch completed */
499 omega_gpu_session_set_error(why);
500}
501
502#ifdef STREAMS_C_ORACLE
503static size_t emit_tail(uint32_t *pb, size_t n, uint64_t marker_va) {
504 pb[n++] = bw_mthd(0, 0x005c, 5);
505 pb[n++] = (uint32_t)marker_va; pb[n++] = (uint32_t)(marker_va >> 32);
506 pb[n++] = OMEGA_BW_MARKER_COMPLETION_PAYLOAD; pb[n++] = 0; pb[n++] = 0x1 | (1u << 20);
507 pb[n++] = bw_mthd(0, 0x0028, 4); pb[n++] = 0; pb[n++] = 0; pb[n++] = 0; pb[n++] = (0x10u << 27);
508 pb[n++] = bw_mthd(0, 0x005c, 5);
509 pb[n++] = (uint32_t)(marker_va + 0x10); pb[n++] = (uint32_t)((marker_va + 0x10) >> 32);
510 pb[n++] = OMEGA_BW_MARKER2_PAYLOAD; pb[n++] = 0; pb[n++] = 0x1 | (1u << 20);
511 return n;
512}
513
514/* Compute-class four-word report {u64 payload = slot, u64 GPU timestamp} to stamps slot `slot` (mesa clcec0.h: PAYLOAD_LOWER 0x0158,
515 * PAYLOAD_UPPER 0x015c, ADDRESS_LOWER 0x0160, ADDRESS_UPPER 0x0164, EXECUTE 0x0168 = OPERATION_RELEASE 0 | STRUCTURE_SIZE_FOUR_WORDS 0).
516 * Ordered after the preceding WAIT_FOR_IDLE, like nvk_query_pool.c timestamp queries. */
517#define MTHD_REPORT_SEMAPHORE_PAYLOAD_LOWER 0x0158u
518/* INVALIDATE_SHADER_CACHES (NVCEC0 0x021c) fields, clc6c0.h:222-236 (same layout on Blackwell): INSTRUCTION 0:0, FLUSH_DATA 2:2, DATA 4:4, CONSTANT 12:12 */
519#define INVALIDATE_DATA (1u << 4)
520#define INVALIDATE_FLUSH_DATA (1u << 2)
521static size_t emit_stamp(uint32_t *pb, size_t n, uint64_t stamps_va, uint32_t slot) {
522 const uint64_t va = stamps_va + (uint64_t)slot * 16u;
523 pb[n++] = bw_mthd(1, MTHD_REPORT_SEMAPHORE_PAYLOAD_LOWER, 5);
524 pb[n++] = slot; pb[n++] = 0; pb[n++] = (uint32_t)va; pb[n++] = (uint32_t)(va >> 32); pb[n++] = 0;
525 return n;
526}
527
528static size_t emit_inline_qmd(uint32_t *pb, size_t n, uint64_t va, const uint32_t *words) {
529 pb[n++] = (98u << 16) | (1u << 13) | (0x0318 >> 2) | (2u << 28);
530 pb[n++] = (1u << 30) | (uint32_t)((va >> 40) & 0x1ff); pb[n++] = (uint32_t)(va >> 8);
531 memcpy(&pb[n], words, 96 * 4); return n + 96;
532}
533#endif
534
535/* Finalize ALL retained slots before observation, synchronization or any queue
536 * commit. INLINE consumes the same bytes, including after PCAS reuse. */
537static void prepare_qmd_mode(OmegaGpuCmdList *cl, const OmegaGpuCmdListOpts *op) {
538 for (uint32_t i = 0; i < cl->n; i++) {
539 uint32_t *q1 = (uint32_t *)((uint8_t *)cl->qmds.cpu + (size_t)i * OMEGA_GPU_CMDLIST_QMD_BYTES);
540 const uint64_t q1_va = cl->qmds.va + (uint64_t)i * OMEGA_GPU_CMDLIST_QMD_BYTES;
541#ifdef CUDAPP_MIGRATION_PENDING_CMDLIST
542 q1[10] = op->mode == OMEGA_GPU_CMDLIST_INLINE ? 0x00190000u : 0;
543 q1[12] = op->mode == OMEGA_GPU_CMDLIST_INLINE ? (uint32_t)((q1_va + 0x200) >> 8) : 0;
544#else
545 uint64_t a[16] = {op->mode, q1_va}, out[16];
546 (void)list_layout(2, a, out);
547 q1[10] = (uint32_t)out[0]; q1[12] = (uint32_t)out[1];
548#endif
549 }
550}
551#ifdef STREAMS_C_ORACLE
552/* One submission's method stream for launches [done,end), followed by completion. */
553static size_t emit_chunk_c(OmegaGpuCmdList *cl, const OmegaGpuCmdListOpts *op, uint32_t done, uint32_t end, uint64_t marker_va,
554 uint32_t *pb) {
555 const OmegaGpuCmdListOpts o = *op;
556 size_t n = 0;
557 memcpy(pb, SETUP_WORDS, sizeof SETUP_WORDS); n += 18;
558 for (uint32_t i = done; i < end; i++) {
559 if (cl->wait_va[i]) {
560 /* wait_for: host semaphore ACQUIRE (clc96f.h SEM_ADDR_LO 0x5c .. SEM_EXECUTE 0x6c, OPERATION_ACQUIRE 0, 32-bit
561 * payload; the omega-mv#580 ORDER_SEM words, chip-proven): this queue stops here until the other list's launch
562 * released DONE. Before the barrier, so the launch's cache invalidate comes after the wait. */
563 const uint64_t va = cl->wait_va[i];
564 pb[n++] = bw_mthd(0, 0x005c, 5); pb[n++] = (uint32_t)va; pb[n++] = (uint32_t)(va >> 32);
565 pb[n++] = OMEGA_BW_SEMAPHORE_INTERMEDIATE_DONE; pb[n++] = 0; pb[n++] = 0;
566 }
567 if (o.skip_launch && i == (uint32_t)(o.skip_launch - 1)) continue;
568 /* the first launch of a submission gets the full barrier (CPU-written QMDs, code and banks before it), unless
569 * opts.first_barrier_none (C4 streams measurement: a WAIT_FOR_IDLE may also wait for another queue's grids) */
570 const int indep = i != done && (cl->program_active ? cl->barriers[i] == 0 : cl->indep[i]);
571 /* a launch that waited (wait_for) always gets the full barrier: its invalidate must come after the wait */
572 const OmegaGpuCmdListBarrier b = cl->wait_va[i] ? OMEGA_GPU_CMDLIST_BARRIER_WFI
573 : i == done ? (i == 0 && o.first_barrier_none ? OMEGA_GPU_CMDLIST_BARRIER_NONE : OMEGA_GPU_CMDLIST_BARRIER_WFI)
574 : indep ? OMEGA_GPU_CMDLIST_BARRIER_NONE : o.barrier;
575 if (indep && o.stamps) n = emit_stamp(pb, n, cl->stamps.va, i); /* no WFI: slot i is the front-end time, launch i - 1 may still run */
576 if (b != OMEGA_GPU_CMDLIST_BARRIER_NONE) {
577 if (b != OMEGA_GPU_CMDLIST_BARRIER_FLUSH_DATA) { pb[n++] = bw_mthd(1, MTHD_WAIT_FOR_IDLE, 1); pb[n++] = 0; }
578 else { pb[n++] = bw_mthd(1, OMEGA_BW_MTHD_INVALIDATE_SHADER_CACHES, 1); pb[n++] = INVALIDATE_DATA | INVALIDATE_FLUSH_DATA; } /* implicit WFI (NVK) */
579 if (o.stamps) n = emit_stamp(pb, n, cl->stamps.va, i); /* slot i: launch i - 1 done (slot `done`: submit start) */
580 if (b == OMEGA_GPU_CMDLIST_BARRIER_WFI) { pb[n++] = bw_mthd(1, OMEGA_BW_MTHD_INVALIDATE_SHADER_CACHES, 1); pb[n++] = OMEGA_BW_INVALIDATE_SHADER_CACHES_ALL; }
581 else if (b == OMEGA_GPU_CMDLIST_BARRIER_WFI_DATA) { pb[n++] = bw_mthd(1, OMEGA_BW_MTHD_INVALIDATE_SHADER_CACHES, 1); pb[n++] = INVALIDATE_DATA; }
582 }
583 const uint64_t q1_va = cl->qmds.va + (uint64_t)i * OMEGA_GPU_CMDLIST_QMD_BYTES;
584 if (o.mode == OMEGA_GPU_CMDLIST_INLINE) {
585 const uint8_t *qs = (const uint8_t *)cl->qmds.cpu + (size_t)i * OMEGA_GPU_CMDLIST_QMD_BYTES;
586 n = emit_inline_qmd(pb, n, q1_va + 0x200, (const uint32_t *)(qs + 0x200));
587 n = emit_inline_qmd(pb, n, q1_va, (const uint32_t *)qs);
588 } else {
589 pb[n++] = bw_mthd(1, MTHD_SEND_PCAS_A, 1); pb[n++] = (uint32_t)(q1_va >> 8);
590 pb[n++] = bw_mthd(1, MTHD_SEND_SIGNALING_PCAS2_B, 1); pb[n++] = PCAS_ACTION_INVALIDATE_COPY_SCHEDULE;
591 }
592 }
593 if (o.stamps) {
594 pb[n++] = bw_mthd(1, MTHD_WAIT_FOR_IDLE, 1); pb[n++] = 0;
595 n = emit_stamp(pb, n, cl->stamps.va, end); /* slot end: the last launch done */
596 }
597 n = emit_tail(pb, n, marker_va);
598 return n;
599}
600#endif
601
602#ifndef CUDAPP_MIGRATION_PENDING_C_STREAMS
603/* One call of the Omega generator, om/streams/streams.om (the exported streams_words). The result is the word count
604 * (or the op's value); UINT64_MAX when the binding refused or the unit refused its inputs. */
605#define STREAMS_OM_IN 16u
606#define STREAMS_OM_OUT 32u
607enum { STREAMS_OP_SETUP, STREAMS_OP_LAUNCH, STREAMS_OP_INLINE, STREAMS_OP_FINISH, STREAMS_OP_CHANNELS, STREAMS_OP_PAIR };
608static uint64_t streams_om(uint64_t op, const uint64_t *in, uint64_t *out) {
609 uint64_t r = 0;
610 if (omega_streams_words(&r, op, in, STREAMS_OM_IN, out, STREAMS_OM_OUT) != 0) return UINT64_MAX;
611 return r;
612}
613/* Append the generator's first k ulongs as method words; 0 when k is not a word count the op can produce. */
614static int streams_put(uint32_t *pb, size_t *n, const uint64_t *w, uint64_t k, uint64_t max) {
615 if (k > max) return 0;
616 for (uint64_t j = 0; j < k; j++) pb[(*n)++] = (uint32_t)w[j];
617 return 1;
618}
619/* Launches [done,end) and completion, words from Omega; the inline QMD payload (96 words each) is copied here.
620 * Returns 0 when the generator failed: nothing is submitted. */
621static size_t emit_chunk_om(OmegaGpuCmdList *cl, const OmegaGpuCmdListOpts *op, uint32_t done, uint32_t end, uint64_t marker_va,
622 uint32_t *pb) {
623 uint64_t in[STREAMS_OM_IN] = {0}, out[STREAMS_OM_OUT];
624 size_t n = 0;
625 if (!streams_put(pb, &n, out, streams_om(STREAMS_OP_SETUP, in, out), 18)) return 0;
626 for (uint32_t i = done; i < end; i++) {
627 const uint64_t q1_va = cl->qmds.va + (uint64_t)i * OMEGA_GPU_CMDLIST_QMD_BYTES;
628 const uint64_t a[11] = { i, done, cl->wait_va[i],
629 cl->program_active ? (cl->barriers[i] == 0 ? 1u : 0u) : (cl->indep[i] ? 1u : 0u),
630 (uint64_t)op->skip_launch, op->first_barrier_none ? 1u : 0u,
631 (uint64_t)op->barrier, op->stamps ? 1u : 0u, cl->stamps.va, (uint64_t)op->mode, q1_va };
632 memset(in, 0, sizeof in); memcpy(in, a, sizeof a);
633 if (!streams_put(pb, &n, out, streams_om(STREAMS_OP_LAUNCH, in, out), 30)) return 0;
634 if (out[31] != 2) continue; /* 0 skipped, 1 PCAS words already emitted */
635 const uint8_t *qs = (const uint8_t *)cl->qmds.cpu + (size_t)i * OMEGA_GPU_CMDLIST_QMD_BYTES;
636 for (int h = 0; h < 2; h++) { /* the second-level QMD (q1 + 0x200) first, then q1 */
637 memset(in, 0, sizeof in); in[0] = h == 0 ? q1_va + 0x200 : q1_va;
638 if (!streams_put(pb, &n, out, streams_om(STREAMS_OP_INLINE, in, out), 3)) return 0;
639 memcpy(&pb[n], qs + (h == 0 ? 0x200 : 0), 96 * 4); n += 96;
640 }
641 }
642 memset(in, 0, sizeof in);
643 in[0] = op->stamps ? 1u : 0u; in[1] = cl->stamps.va; in[2] = end; in[3] = marker_va;
644 if (!streams_put(pb, &n, out, streams_om(STREAMS_OP_FINISH, in, out), 2 + STAMP_WORDS + TAIL_WORDS)) return 0;
645 return n;
646}
647#endif
648
649/* One submission's method stream for launches [done,end), followed by completion, in cl->words; 0 on generator failure. */
650static size_t emit_chunk(OmegaGpuCmdList *cl, const OmegaGpuCmdListOpts *op, uint32_t done, uint32_t end, uint64_t marker_va) {
651#ifdef CUDAPP_MIGRATION_PENDING_C_STREAMS
652 return emit_chunk_c(cl, op, done, end, marker_va, cl->words);
653#else
654 const size_t n = emit_chunk_om(cl, op, done, end, marker_va, cl->words);
655#ifdef CUDAPP_MIGRATION_PENDING_STREAMS_DIFF
656 char line[200];
657 uint32_t *ref = omega_gpu_host_calloc(cl->words_cap, 4u);
658 if (!ref) { cudapp_streams_diff_note(0, "streams diff: no memory for the C reference"); return 0; }
659 const size_t nc = emit_chunk_c(cl, op, done, end, marker_va, ref);
660#ifdef CUDAPP_STREAMS_DIFF_SELFTEST
661 if (n) cl->words[n - 1u] ^= 1u; /* red check: one flipped Omega word must abort */
662#endif
663 if (n != nc || memcmp(ref, cl->words, n * 4u) != 0) {
664 size_t j = 0;
665 while (j < n && j < nc && ref[j] == cl->words[j]) j++;
666 omega_gpu_fmt(line, sizeof line, "streams diff: launches [%u,%u) C %zu words, Omega %zu words, first difference at word %zu",
667 done, end, nc, n, j);
668 cudapp_streams_diff_note(0, line);
669 }
670 omega_gpu_host_free(ref);
671 omega_gpu_fmt(line, sizeof line, "SAME chunk launches=[%u,%u) mode=%d barrier=%d stamps=%d words=%zu", done, end,
672 (int)op->mode, (int)op->barrier, op->stamps ? 1 : 0, n);
673 cudapp_streams_diff_note(1, line);
674#endif
675 return n;
676#endif
677}
678
679#ifdef CUDAPP_MIGRATION_PENDING_STREAMS_DIFF
680/* Host differential sweep (CUDApp#115, diff builds only; the host and sim tiers never submit a cmdlist): every
681 * combination of mode, barrier, stamps, first_barrier_none, skip_launch, first launch, per-launch waits and
682 * independence marks over a 4-launch list goes through emit_chunk, which aborts on any C vs Omega difference.
683 * Then both pair markers. Returns the number of chunks compared. */
684static uint64_t pair_marker(uint64_t page_va, uint64_t k, uint32_t clr[2]);
685uint64_t omega_gpu_cmdlist_streams_diff_sweep(void);
686uint64_t omega_gpu_cmdlist_streams_diff_sweep(void) {
687 enum { L = 4 };
688 static uint32_t qmd_words[L * OMEGA_GPU_CMDLIST_QMD_BYTES / 4];
689 static uint32_t words[8192];
690 uint8_t indep[L]; uint64_t wait_va[L];
691 OmegaGpuCmdList cl; memset(&cl, 0, sizeof cl);
692 for (size_t j = 0; j < sizeof qmd_words / 4; j++) qmd_words[j] = (uint32_t)(j * 2654435761u);
693 cl.n = cl.cap = L; cl.words = words; cl.words_cap = sizeof words / 4; cl.indep = indep; cl.wait_va = wait_va;
694 cl.qmds.cpu = qmd_words; cl.qmds.va = 0x123456789000ull; cl.stamps.va = 0x2000abcd0000ull;
695 uint64_t chunks = 0;
696 for (unsigned wm = 0; wm < 16u; wm++) for (unsigned im = 0; im < 16u; im++)
697 for (int mode = 0; mode < 2; mode++) for (int bar = 0; bar < 5; bar++) for (int st = 0; st < 2; st++)
698 for (int fn = 0; fn < 2; fn++) for (int skip = 0; skip <= L; skip++) for (uint32_t done = 0; done < 2u; done++) {
699 for (unsigned i = 0; i < L; i++) { indep[i] = (im >> i) & 1u; wait_va[i] = (wm >> i) & 1u ? 0xfedc00001230ull + 16u * i : 0; }
700 OmegaGpuCmdListOpts o; memset(&o, 0, sizeof o);
701 o.mode = (OmegaGpuCmdListMode)mode; o.barrier = (OmegaGpuCmdListBarrier)bar; o.stamps = st;
702 o.first_barrier_none = fn; o.skip_launch = (uint32_t)skip;
703 if (!emit_chunk(&cl, &o, done, L, 0x7000beef0110ull)) cudapp_streams_diff_note(0, "streams diff: generator failed");
704 chunks++;
705 }
706 uint32_t clr[4];
707 if (pair_marker(0x7000beef0000ull, 0, clr) == UINT64_MAX || pair_marker(0x7000beef0000ull, 1, clr + 2) == UINT64_MAX)
708 cudapp_streams_diff_note(0, "streams diff: pair marker generator failed");
709 return chunks;
710}
711#endif
712
713static int submit_observed(OmegaGpuCmdList *cl, const OmegaGpuCmdListOpts *opts, OmegaGpuCmdListStats *st,
714 OmegaGpuCmdListObserver observer, void *context) {
715 OmegaGpuCmdListOpts o; memset(&o, 0, sizeof o);
716 if (opts) o = *opts;
717 if (st) { memset(st, 0, sizeof *st); st->first_missing = -1; }
718 const OmegaGpuPlatform *p = omega_gpu_session_platform();
719 if (!cl || cl->n == 0 || !p) { omega_gpu_session_set_error("cmdlist submit: empty list or no platform"); return -1; }
720 if (stale(cl)) return -1;
721 if (cl->nwaits) { omega_gpu_session_set_error("cmdlist submit: a list with waits (wait_for) runs only in a pair (submit_pair)"); return -1; }
722 if (omega_gpu_cmdlist_is_latched() || omega_gpu_session_is_blocked()) { omega_gpu_session_set_error("cmdlist submit: latched"); return -1; }
723 OmegaGpuSession *s = omega_gpu_session_open();
724 if (!s) return -1;
725 if (!s->marker.cpu || s->marker.size < MARKER_OFF + 0x20) { omega_gpu_session_set_error("cmdlist submit: session marker page"); return -1; }
726 const OmegaGpuMem *marker = &s->marker;
727 const uint64_t marker_va = marker->va + MARKER_OFF;
728 const uint64_t timeout = o.timeout_ms ? o.timeout_ms : DEFAULT_TIMEOUT_MS;
729 const uint32_t spin = o.spin_us ? o.spin_us : omega_gpu_session_spin_us();
730 prepare_qmd_mode(cl, &o);
731 if (observer) {
732 const OmegaGpuCmdListConsumed consumed = { .options = o, .timeout_ms = timeout, .spin_us = spin };
733 if (observer(cl, &consumed, context) != 0) {
734 omega_gpu_session_set_error("cmdlist submit: actual native closure refused before queue commit"); return -1;
735 }
736 }
737 const uint64_t th0 = p->time_now_ns(p->ctx);
738
739 /* semaphores INIT, markers 0, everything the CPU wrote made visible */
740 uint32_t *sem = cl->sems.cpu;
741 for (uint32_t i = 0; i < cl->n; i++) sem[4 * i] = OMEGA_BW_SEMAPHORE_INTERMEDIATE_INIT;
742 volatile uint32_t *mk = (volatile uint32_t *)((uint8_t *)marker->cpu + MARKER_OFF);
743 mk[0] = 0; mk[4] = 0;
744 if (p->sync_cpu_to_gpu(p->ctx, cl->banks.handle, 0, (uint64_t)cl->n * OMEGA_GPU_CMDLIST_BANK_BYTES) != OMEGA_GPU_PLAT_OK ||
745 p->sync_cpu_to_gpu(p->ctx, cl->qmds.handle, 0, (uint64_t)cl->n * OMEGA_GPU_CMDLIST_QMD_BYTES) != OMEGA_GPU_PLAT_OK ||
746 p->sync_cpu_to_gpu(p->ctx, cl->sems.handle, 0, (uint64_t)cl->n * 16u) != OMEGA_GPU_PLAT_OK ||
747 p->sync_cpu_to_gpu(p->ctx, marker->handle, MARKER_OFF, 0x20) != OMEGA_GPU_PLAT_OK) {
748 omega_gpu_session_set_error("cmdlist submit: sync_cpu_to_gpu"); return -1;
749 }
750
751 /* The published platform prefix is part of the generated ring budget. */
752 const uint32_t prefix = omega_gpu_session_submit_prefix_words();
753 const uint32_t room = cl->program_active ? cl->n : list_room(cl, &o, prefix);
754 if (!room) { omega_gpu_session_set_error("cmdlist submit: the platform submit prefix leaves no room for a launch"); return -1; }
755 uint64_t *stamp_cpu = cl->stamps.cpu;
756 cl->stamped = o.stamps ? 1 : 0;
757 if (o.stamps) {
758 memset(stamp_cpu, 0xff, (size_t)(cl->n + 1u) * 16u); /* poisoned: a slot the chip never wrote reads all-ones */
759 if (p->sync_cpu_to_gpu(p->ctx, cl->stamps.handle, 0, (uint64_t)(cl->n + 1u) * 16u) != OMEGA_GPU_PLAT_OK) {
760 omega_gpu_session_set_error("cmdlist submit: sync stamps"); return -1;
761 }
762 }
763 uint64_t elapsed = 0, host = p->time_now_ns(p->ctx) - th0;
764 uint32_t done = 0, submits = 0, words = 0;
765 while (done < cl->n) {
766 const uint64_t tb = p->time_now_ns(p->ctx);
767#ifdef CUDAPP_MIGRATION_PENDING_CMDLIST
768 const uint32_t end = (cl->n - done > room) ? done + room : cl->n;
769#else
770 uint64_t bounds[16] = {cl->n, done, room}, unused[16];
771 uint64_t planned_end = list_layout(3, bounds, unused);
772 if (planned_end == UINT64_MAX) return -1;
773 const uint32_t end = (uint32_t)planned_end;
774#endif
775 uint32_t *pb = cl->words;
776 size_t n = emit_chunk(cl, &o, done, end, marker_va);
777 if (!n) { omega_gpu_session_set_error("cmdlist submit: method word generator failed"); return -1; }
778 mk[0] = 0; mk[4] = 0;
779 (void)p->sync_cpu_to_gpu(p->ctx, marker->handle, MARKER_OFF, 0x20);
780 const uint64_t t0 = p->time_now_ns(p->ctx);
781 host += t0 - tb;
782 if (native_begin(cl, done, end) != 0) return -1;
783 const int qrc = p->queue_submit(p->ctx, s->queue, pb, (uint32_t)n);
784 if (qrc == OMEGA_GPU_PLAT_REFUSED_BUSY) { force_latch(cl, "cmdlist: an earlier submission is still in flight (latched)"); return -1; }
785 if (qrc != OMEGA_GPU_PLAT_OK) {
786 (void)native_events(cl, done, end, 4);
787 omega_gpu_session_set_error("cmdlist submit: queue_submit refused"); return -1; /* refused: nothing in flight */
788 }
789 int w = wait_word(p, marker, 0, OMEGA_BW_MARKER_COMPLETION_PAYLOAD, timeout, spin);
790 if (w == WAIT_OK) w = wait_word(p, marker, 0x10, OMEGA_BW_MARKER2_PAYLOAD, timeout, spin);
791 if (w != WAIT_OK) { force_latch(cl, w == WAIT_TIMED_OUT ? "cmdlist: marker wait timed out (latched)" : "cmdlist: marker poll refused (latched)"); return -1; }
792 (void)p->sync_gpu_to_cpu(p->ctx, marker->handle, MARKER_OFF, 0x20);
793 elapsed += p->time_now_ns(p->ctx) - t0;
794 (void)p->queue_retire(p->ctx, s->queue);
795 (void)native_events(cl, done, end, 2); /* both authoritative completion markers observed */
796 submits++; words += (uint32_t)n; done = end;
797 }
798 (void)p->sync_gpu_to_cpu(p->ctx, cl->sems.handle, 0, (uint64_t)cl->n * 16u);
799 if (o.stamps) (void)p->sync_gpu_to_cpu(p->ctx, cl->stamps.handle, 0, (uint64_t)(cl->n + 1u) * 16u);
800 uint32_t ok = 0; int32_t missing = -1;
801 for (uint32_t i = 0; i < cl->n; i++) {
802 if (sem[4 * i] == OMEGA_BW_SEMAPHORE_INTERMEDIATE_DONE) ok++;
803 else if (missing < 0) missing = (int32_t)i;
804 }
805 if (st) { st->launches = cl->n; st->submits = submits; st->words = words; st->elapsed_ns = elapsed; st->host_ns = host; st->sems_done = ok; st->first_missing = missing; }
806 if (missing >= 0) { omega_gpu_session_set_error("cmdlist: a launch semaphore is not DONE (launch did not run)"); return -1; }
807 return 0;
808}
809int omega_gpu_cmdlist_submit(OmegaGpuCmdList *cl, const OmegaGpuCmdListOpts *opts, OmegaGpuCmdListStats *st) {
810 return submit_observed(cl, opts, st, NULL, NULL);
811}
812int omega_gpu_cmdlist_submit_program(OmegaGpuCmdList *cl, const OmegaGpuCmdListOpts *opts, OmegaGpuCmdListStats *stats) {
813 OmegaGpuCmdListProgramPlan plan;
814 if (stats) { memset(stats, 0, sizeof *stats); stats->first_missing = -1; }
815 if (omega_gpu_cmdlist_plan_program(cl, opts, &plan) != 0) return -1;
816 if (!plan.launches) return 0;
817 cl->program_active = 1;
818 int rc = submit_observed(cl, opts, stats, NULL, NULL);
819 cl->program_active = 0;
820 return rc;
821}
822int omega_gpu_cmdlist_submit_observed(OmegaGpuCmdList *cl, const OmegaGpuCmdListOpts *opts, OmegaGpuCmdListStats *st,
823 OmegaGpuCmdListObserver observer, void *context) {
824 if (!observer) return -1;
825 return submit_observed(cl, opts, st, observer, context);
826}
827
828/* L136: list a on the session queue and list b on extra queue 1, both in flight before either is waited. Each list gets
829 * the same prefix as submit (semaphores INIT, banks / QMDs / semaphores synced) and the same words (emit_chunk); b's
830 * completion markers sit at MARKER_OFF + PAIR_B_MARKER on the same uncached page. */
831#define PAIR_B_MARKER 0x80ull
832/* Queue k's (0 or 1) completion marker VA on the marker page at page_va, and the two marker words (u32 index from
833 * MARKER_OFF) to clear before submitting; UINT64_MAX when the generator refused. */
834static uint64_t pair_marker(uint64_t page_va, uint64_t k, uint32_t clr[2]) {
835#ifdef CUDAPP_MIGRATION_PENDING_C_STREAMS
836 clr[0] = (uint32_t)(k * PAIR_B_MARKER / 4u); clr[1] = clr[0] + 4u;
837 return page_va + MARKER_OFF + k * PAIR_B_MARKER;
838#else
839 uint64_t in[STREAMS_OM_IN] = { page_va, k }, out[STREAMS_OM_OUT];
840 const uint64_t va = streams_om(STREAMS_OP_PAIR, in, out);
841 if (va == UINT64_MAX || out[0] > PAIR_B_MARKER / 4u || out[1] > PAIR_B_MARKER / 4u + 4u) return UINT64_MAX;
842 clr[0] = (uint32_t)out[0]; clr[1] = (uint32_t)out[1];
843#ifdef CUDAPP_MIGRATION_PENDING_STREAMS_DIFF
844 char line[120];
845 if (va != page_va + MARKER_OFF + k * PAIR_B_MARKER || clr[0] != k * PAIR_B_MARKER / 4u || clr[1] != clr[0] + 4u) {
846 omega_gpu_fmt(line, sizeof line, "streams diff: pair marker k=%llu Omega va 0x%llx clears %u %u", (unsigned long long)k,
847 (unsigned long long)va, clr[0], clr[1]);
848 cudapp_streams_diff_note(0, line);
849 }
850 omega_gpu_fmt(line, sizeof line, "SAME pair marker k=%llu", (unsigned long long)k);
851 cudapp_streams_diff_note(1, line);
852#endif
853 return va;
854#endif
855}
856static int pair_prep(const OmegaGpuPlatform *p, OmegaGpuCmdList *cl) {
857 uint32_t *sem = cl->sems.cpu;
858 for (uint32_t i = 0; i < cl->n; i++) sem[4 * i] = OMEGA_BW_SEMAPHORE_INTERMEDIATE_INIT;
859 cl->stamped = 0;
860 return p->sync_cpu_to_gpu(p->ctx, cl->banks.handle, 0, (uint64_t)cl->n * OMEGA_GPU_CMDLIST_BANK_BYTES) == OMEGA_GPU_PLAT_OK &&
861 p->sync_cpu_to_gpu(p->ctx, cl->sems.handle, 0, (uint64_t)cl->n * 16u) == OMEGA_GPU_PLAT_OK ? 0 : -1;
862}
863
864static int pair_check(const OmegaGpuPlatform *p, OmegaGpuCmdList *cl, uint32_t nwords, uint64_t elapsed, uint64_t host, OmegaGpuCmdListStats *st) {
865 (void)p->sync_gpu_to_cpu(p->ctx, cl->sems.handle, 0, (uint64_t)cl->n * 16u);
866 const uint32_t *sem = cl->sems.cpu;
867 uint32_t ok = 0; int32_t missing = -1;
868 for (uint32_t i = 0; i < cl->n; i++) {
869 if (sem[4 * i] == OMEGA_BW_SEMAPHORE_INTERMEDIATE_DONE) ok++;
870 else if (missing < 0) missing = (int32_t)i;
871 }
872 if (st) { st->launches = cl->n; st->submits = 1; st->words = nwords; st->elapsed_ns = elapsed; st->host_ns = host; st->sems_done = ok; st->first_missing = missing; }
873 if (missing >= 0) { omega_gpu_session_set_error("cmdlist pair: a launch semaphore is not DONE (launch did not run)"); return -1; }
874 return 0;
875}
876
877/* Uncertain completion of a pair: queue 1 may still run, so a nop on queue 0 proves nothing; latch the session directly. */
878static void pair_latch(const char *why) {
879 __atomic_store_n(&g_latched, omega_gpu_session_recoveries() + 1u, __ATOMIC_RELEASE);
880 omega_gpu_session_latch_uncertain(why);
881}
882
883int omega_gpu_cmdlist_submit_pair(OmegaGpuCmdList *a, OmegaGpuCmdList *b, const OmegaGpuCmdListOpts *opts, OmegaGpuCmdListStats *sa, OmegaGpuCmdListStats *sb) {
884 OmegaGpuCmdListOpts o; memset(&o, 0, sizeof o);
885 if (opts) o = *opts;
886 if (sa) { memset(sa, 0, sizeof *sa); sa->first_missing = -1; }
887 if (sb) { memset(sb, 0, sizeof *sb); sb->first_missing = -1; }
888 const OmegaGpuPlatform *p = omega_gpu_session_platform();
889 if (!a || !b || a == b || a->n == 0 || b->n == 0 || !p) { omega_gpu_session_set_error("cmdlist pair: two distinct non-empty lists and a platform"); return -1; }
890 if (stale(a) || stale(b)) return -1;
891 for (int k = 0; k < 2; k++) { /* a wait must point into the other list of this pair, at a launch it still has */
892 const OmegaGpuCmdList *me = k ? b : a, *peer = k ? a : b;
893 if (me->nwaits && (me->wait_other != peer || me->wait_max >= peer->n)) {
894 omega_gpu_session_set_error("cmdlist pair: a wait_for points outside the other list of this pair"); return -1;
895 }
896 }
897 if (o.mode != OMEGA_GPU_CMDLIST_PCAS || o.stamps || o.skip_launch) { omega_gpu_session_set_error("cmdlist pair: PCAS mode only, no stamps, no skip"); return -1; }
898 if (omega_gpu_cmdlist_is_latched() || omega_gpu_session_is_blocked()) { omega_gpu_session_set_error("cmdlist pair: latched"); return -1; }
899 OmegaGpuSession *s = omega_gpu_session_open();
900 if (!s) return -1;
901 if (!s->marker.cpu || s->marker.size < MARKER_OFF + PAIR_B_MARKER + 0x20) { omega_gpu_session_set_error("cmdlist pair: session marker page"); return -1; }
902 OmegaGpuQueueHandle q1;
903 if (omega_gpu_session_extra_queue(1, &q1) != 0) return -1; /* names the refusing stage */
904 const OmegaGpuMem *marker = &s->marker;
905 const uint64_t marker_va = marker->va + MARKER_OFF;
906 const uint64_t timeout = o.timeout_ms ? o.timeout_ms : DEFAULT_TIMEOUT_MS;
907 const uint32_t spin = o.spin_us ? o.spin_us : omega_gpu_session_spin_us();
908 const uint64_t th0 = p->time_now_ns(p->ctx);
909 /* one submission per list: both must fit their ring with the platform prefix (a q38 token is ~650 launches x 8 words) */
910 const uint32_t prefix = omega_gpu_session_submit_prefix_words();
911 const OmegaGpuCmdList *L[2] = { a, b };
912 for (int k = 0; k < 2; k++) {
913 if (list_room(L[k], &o, prefix) < L[k]->n) { omega_gpu_session_set_error("cmdlist pair: a list does not fit one submission"); return -1; }
914 }
915 prepare_qmd_mode(a, &o); prepare_qmd_mode(b, &o);
916 if (pair_prep(p, a) || pair_prep(p, b)) { omega_gpu_session_set_error("cmdlist pair: sync_cpu_to_gpu"); return -1; }
917 uint32_t clr[4];
918 const uint64_t va_a = pair_marker(marker->va, 0, clr), va_b = pair_marker(marker->va, 1, clr + 2);
919 if (va_a != marker_va || va_b == UINT64_MAX) { omega_gpu_session_set_error("cmdlist pair: marker layout generator failed"); return -1; }
920 const size_t na = emit_chunk(a, &o, 0, a->n, va_a), nb = emit_chunk(b, &o, 0, b->n, va_b);
921 if (!na || !nb) { omega_gpu_session_set_error("cmdlist pair: method word generator failed"); return -1; }
922 volatile uint32_t *mk = (volatile uint32_t *)((uint8_t *)marker->cpu + MARKER_OFF);
923 for (int k = 0; k < 4; k++) mk[clr[k]] = 0;
924 if (p->sync_cpu_to_gpu(p->ctx, a->qmds.handle, 0, (uint64_t)a->n * OMEGA_GPU_CMDLIST_QMD_BYTES) != OMEGA_GPU_PLAT_OK ||
925 p->sync_cpu_to_gpu(p->ctx, b->qmds.handle, 0, (uint64_t)b->n * OMEGA_GPU_CMDLIST_QMD_BYTES) != OMEGA_GPU_PLAT_OK ||
926 p->sync_cpu_to_gpu(p->ctx, marker->handle, MARKER_OFF, PAIR_B_MARKER + 0x20) != OMEGA_GPU_PLAT_OK) {
927 omega_gpu_session_set_error("cmdlist pair: sync qmd / marker"); return -1;
928 }
929 const uint64_t t0 = p->time_now_ns(p->ctx);
930 const uint64_t host = t0 - th0;
931 if (native_begin(a, 0, a->n) != 0) return -1;
932 if (native_begin(b, 0, b->n) != 0) { (void)native_events(a, 0, a->n, 4); return -1; }
933 const int aqrc = p->queue_submit(p->ctx, s->queue, a->words, (uint32_t)na);
934 if (aqrc != OMEGA_GPU_PLAT_OK) {
935 if (aqrc == OMEGA_GPU_PLAT_REFUSED_BUSY) {
936 pair_latch("cmdlist pair: uncertain earlier submission (latched)");
937 } else {
938 (void)native_events(a, 0, a->n, 4);
939 (void)native_events(b, 0, b->n, 4);
940 }
941 omega_gpu_session_set_error("cmdlist pair: queue_submit (queue 0) refused"); return -1;
942 }
943 const int bqrc = p->queue_submit(p->ctx, q1, b->words, (uint32_t)nb);
944 const int b_ok = bqrc == OMEGA_GPU_PLAT_OK;
945 if (!b_ok && bqrc != OMEGA_GPU_PLAT_REFUSED_BUSY) (void)native_events(b, 0, b->n, 4);
946 int w = wait_word(p, marker, 0, OMEGA_BW_MARKER_COMPLETION_PAYLOAD, timeout, spin);
947 if (w == WAIT_OK) w = wait_word(p, marker, 0x10, OMEGA_BW_MARKER2_PAYLOAD, timeout, spin);
948 if (w == WAIT_OK && b_ok) w = wait_word(p, marker, PAIR_B_MARKER, OMEGA_BW_MARKER_COMPLETION_PAYLOAD, timeout, spin);
949 if (w == WAIT_OK && b_ok) w = wait_word(p, marker, PAIR_B_MARKER + 0x10, OMEGA_BW_MARKER2_PAYLOAD, timeout, spin);
950 if (w != WAIT_OK) { pair_latch(w == WAIT_TIMED_OUT ? "cmdlist pair: marker wait timed out (latched)" : "cmdlist pair: marker poll refused (latched)"); return -1; }
951 (void)p->sync_gpu_to_cpu(p->ctx, marker->handle, MARKER_OFF, PAIR_B_MARKER + 0x20);
952 const uint64_t elapsed = p->time_now_ns(p->ctx) - t0;
953 (void)p->queue_retire(p->ctx, s->queue);
954 (void)native_events(a, 0, a->n, 2);
955 if (!b_ok) {
956 if (bqrc == OMEGA_GPU_PLAT_REFUSED_BUSY) pair_latch("cmdlist pair: queue 1 uncertain earlier flight (latched)");
957 omega_gpu_session_set_error("cmdlist pair: queue_submit (queue 1) refused"); return -1;
958 }
959 (void)p->queue_retire(p->ctx, q1);
960 (void)native_events(b, 0, b->n, 2);
961 const int ra = pair_check(p, a, (uint32_t)na, elapsed, host, sa), rb = pair_check(p, b, (uint32_t)nb, elapsed, host, sb);
962 return ra == 0 && rb == 0 ? 0 : -1;
963}
964
965int omega_gpu_cmdlist_stamps(const OmegaGpuCmdList *cl, uint64_t *out, uint32_t cap) {
966 if (!cl || !out || !cl->stamps.cpu) { omega_gpu_session_set_error("cmdlist stamps: arguments"); return -1; }
967 if (!cl->stamped) { omega_gpu_session_set_error("cmdlist stamps: the last submit had stamps off"); return -1; }
968 const uint32_t want = cl->n + 1u;
969 if (cap < want) { omega_gpu_session_set_error("cmdlist stamps: out too small"); return -1; }
970 const uint64_t *s = cl->stamps.cpu;
971 for (uint32_t i = 0; i < want; i++) {
972 if (s[2 * i] != (uint64_t)i || s[2 * i + 1] == UINT64_MAX) { omega_gpu_session_set_error("cmdlist stamps: a slot was not written (stamps off, or the launch did not run)"); return -1; }
973 out[i] = s[2 * i + 1];
974 }
975 return (int)want;
976}
977
978const char *omega_gpu_cmdlist_barrier_name(OmegaGpuCmdListBarrier b) {
979 switch (b) {
980 case OMEGA_GPU_CMDLIST_BARRIER_WFI: return "wfi+inval_all";
981 case OMEGA_GPU_CMDLIST_BARRIER_NONE: return "none";
982 case OMEGA_GPU_CMDLIST_BARRIER_WFI_ONLY: return "wfi_only";
983 case OMEGA_GPU_CMDLIST_BARRIER_WFI_DATA: return "wfi+inval_data";
984 case OMEGA_GPU_CMDLIST_BARRIER_FLUSH_DATA: return "flush_data";
985 }
986 return "?";
987}