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.h

170 lines · 12489 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */
2#ifndef OMEGA_GPU_CMDLIST_H
3#define OMEGA_GPU_CMDLIST_H
4/*
5 * omega_gpu_cmdlist: many kernel launches in ONE submission (OWN-30B W-L2, decode launch collapse, 2026-10-13).
6 *
7 * Why: omega_gpu_session_launch submits one kernel per GPFIFO entry and polls two markers before it returns, so a
8 * Qwen3-30B decode token (289 launches) pays the submit + completion round trip 289 times. A command list records N
9 * launches, then submits them as one method stream with one completion (the C3 tail: marker, L2_FLUSH_DIRTY, second
10 * marker on the uncached page) at the end.
11 *
12 * What each launch gets (all buffers from omega_gpu_session_alloc, GPU-uncached; nvos.h:1117 read 2026-10-13):
13 * - its own constant bank 0 (1 KB: the 224 driver words patched for this bank, arguments at 0x380), CPU-written;
14 * - its own QMD slot (the session's QMD1 words with this launch's bank and semaphore), CPU-written;
15 * (completion markers: the session marker page at 0x100, the one page the platform lets the host poll)
16 * - its own release semaphore word, set to INTERMEDIATE_INIT (5) by the CPU and released to DONE (6) by the QMD when
17 * the grid completes. submit() checks every word: a launch that never ran is caught by name (index).
18 *
19 * Ordering between launches (the race this module must not create): Mesa NVK, the working open driver for this class,
20 * serialises dependent dispatches with WAIT_FOR_IDLE (NVCEC0 0x0110) or INVALIDATE_SHADER_CACHES ("This is also
21 * implicitly a WFI", nvk_cmd_buffer.c:633); without one, consecutive grids of one channel may overlap.
22 * Legacy submit precedes each launch by that barrier unless explicitly marked independent.
23 * submit_program derives the necessary drains from complete memory-effect sets instead;
24 * BARRIER_NONE remains only a legacy negative-control option.
25 *
26 * Launch methods (opts.mode):
27 * PCAS QMD in memory, SEND_PCAS_A (qmd >> 8) + SEND_SIGNALING_PCAS2_B INVALIDATE_COPY_SCHEDULE (clcec0.h 0x02b4 /
28 * 0x02c0; the NVK dispatch sequence, nvk_cmd_dispatch.c), no dependent QMD: 4 launch words plus
29 * 4 words at each required barrier. The program API refuses overflow rather than splitting.
30 * INLINE the session's own words per launch (QMD0 null grid + QMD1 inline), bank from memory: 198 launch words.
31 * Legacy submit splits a long list; submit_program requires the whole stream to fit.
32 *
33 * Threading: the caller holds omega_gpu_session_lock() around add / submit (one channel, as for every API).
34 * Uncertain completion: a marker timeout latches this module and then forces the session latch (the platform refuses
35 * a 1 ms session launch REFUSED_BUSY while the stuck list is unretired, before any ring write, and the session latches
36 * the process), so no API keeps launching on a channel that may still be busy.
37 */
38#include "omega_gpu_session.h"
39#include <stdint.h>
40
41#ifdef __cplusplus
42extern "C" {
43#endif
44
45#define OMEGA_GPU_CMDLIST_MAX_LAUNCHES 4096u
46#define OMEGA_GPU_CMDLIST_BANK_BYTES 0x400u
47#define OMEGA_GPU_CMDLIST_QMD_BYTES 0x400u /* QMD1 at +0, QMD0 (INLINE mode) at +0x200 */
48
49typedef enum { OMEGA_GPU_CMDLIST_PCAS = 0, OMEGA_GPU_CMDLIST_INLINE = 1 } OmegaGpuCmdListMode;
50/* Barrier before every launch after the first of a submission (the first always gets the full WFI + invalidate ALL):
51 * WFI WAIT_FOR_IDLE + INVALIDATE_SHADER_CACHES ALL (instruction, data, constant). The default since cut 1b.
52 * NONE nothing (TEST ONLY: negative control; consecutive grids may overlap).
53 * WFI_ONLY WAIT_FOR_IDLE, no cache invalidate (W-L2c launch-cost study: isolates the drain from the invalidate).
54 * WFI_DATA WAIT_FOR_IDLE + INVALIDATE_SHADER_CACHES {data} (keeps the instruction and constant caches warm).
55 * FLUSH_DATA INVALIDATE_SHADER_CACHES {data, flush_data} alone, which NVK uses for a storage-write to storage-read
56 * dependency between dispatches ("This is also implicitly a WFI", nvk_cmd_buffer.c:633). */
57typedef enum { OMEGA_GPU_CMDLIST_BARRIER_WFI = 0, OMEGA_GPU_CMDLIST_BARRIER_NONE = 1 /* TEST ONLY: negative control */,
58 OMEGA_GPU_CMDLIST_BARRIER_WFI_ONLY = 2, OMEGA_GPU_CMDLIST_BARRIER_WFI_DATA = 3, OMEGA_GPU_CMDLIST_BARRIER_FLUSH_DATA = 4 } OmegaGpuCmdListBarrier;
59const char *omega_gpu_cmdlist_barrier_name(OmegaGpuCmdListBarrier b);
60
61typedef struct {
62 OmegaGpuCmdListMode mode;
63 OmegaGpuCmdListBarrier barrier;
64 uint64_t timeout_ms; /* per submission; 0 = 20000 */
65 uint32_t spin_us; /* marker spin window before the platform poll; 0 = the session's process-wide setting */
66 int stamps; /* 1: a GPU timestamp report before the first launch and after every launch (after its WAIT_FOR_IDLE), read
67 back by omega_gpu_cmdlist_stamps. Costs 6 words + the report per launch. 0 = off. */
68 int skip_launch; /* TEST ONLY: >= 1 drops launch (skip_launch - 1) from the method stream (its slot stays
69 recorded), so the per-launch semaphore check must fail. 0 = off. */
70 int first_barrier_none; /* C4 streams: no WAIT_FOR_IDLE + invalidate before launch 0 (the CPU-written code, banks and QMDs
71 must not sit in a stale cache line: fresh slots or unchanged contents). A WAIT_FOR_IDLE may also
72 wait for grids of another queue of the group; the streams test measures both. 0 = the barrier. */
73} OmegaGpuCmdListOpts;
74
75typedef struct {
76 uint32_t launches; /* recorded launches submitted */
77 uint32_t submits; /* GPFIFO entries (1 in PCAS mode below the ring size) */
78 uint32_t words; /* method words over all submissions */
79 uint64_t elapsed_ns; /* first submit to last second marker */
80 uint64_t host_ns; /* building the method stream (CPU) */
81 uint32_t sems_done; /* launch semaphores found DONE */
82 int32_t first_missing; /* index of the first launch whose semaphore was not DONE, -1 if none */
83} OmegaGpuCmdListStats;
84
85/* Complete launch memory effects, expressed as half-open GPU VA intervals.
86 * Overlapping intervals alias even when their base addresses differ. Multiple
87 * mappings of the same storage must use one canonical interval namespace; if
88 * aliases cannot be normalized, record legacy unknown effects instead. READ|WRITE
89 * describes an in-place buffer. Code, immutable launch banks and QMD slots are
90 * admitted by the leading barrier, not user-data dependencies. */
91#define OMEGA_GPU_CMDLIST_MAX_ACCESSES 16u
92#define OMEGA_GPU_CMDLIST_ACCESS_READ 1u
93#define OMEGA_GPU_CMDLIST_ACCESS_WRITE 2u
94typedef struct {
95 uint64_t va, bytes;
96 uint32_t flags;
97} OmegaGpuCmdListAccess;
98typedef struct {
99 uint32_t launches, submissions, words, waits;
100} OmegaGpuCmdListProgramPlan;
101
102typedef struct OmegaGpuCmdList OmegaGpuCmdList;
103
104/* Takes the session lock itself. Allocates the slots for up to cap launches. 0 ok, -1 (session error names it). A list
105 * belongs to the device it was made on: after omega_gpu_session_recover, add / submit / wait_for refuse it (C5). */
106int omega_gpu_cmdlist_create(uint32_t cap, OmegaGpuCmdList **out);
107void omega_gpu_cmdlist_free(OmegaGpuCmdList *cl);
108/* Caller holds the lock. Empties the list (slots are rewritten by the next adds). */
109void omega_gpu_cmdlist_reset(OmegaGpuCmdList *cl);
110uint32_t omega_gpu_cmdlist_count(const OmegaGpuCmdList *cl);
111/* Caller holds the lock. Marks the most recently added launch as sharing NO data with the launch before it (neither reads
112 * what the other writes): submit emits no barrier before it, so the two grids may overlap on the chip (W-L2d: a weight
113 * prefetch beside the latency-bound attention). The launch after it gets the normal barrier, which waits for both.
114 * Ignored for the first launch of a submission. 0 ok, -1 (no launch recorded). */
115int omega_gpu_cmdlist_mark_independent(OmegaGpuCmdList *cl);
116/* Caller holds the lock. Writes the launch's bank and QMD into the next slot. The launch's code must stay resident
117 * until submit returns. Returns the launch index, or -1 (full, bad arguments, QMD refused). L->timeout_ms and
118 * L->spin_us are ignored (the list has one completion). */
119int omega_gpu_cmdlist_add(OmegaGpuCmdList *cl, const OmegaGpuLaunch *L);
120/* Same record bytes as add(), with a COMPLETE memory-effect set. n_accesses=0
121 * means no user-data accesses, not unknown. Zero sizes, wrapping intervals and
122 * invalid flags are refused. Legacy add() means unknown and stays conservative.
123 * Caller holds the session lock; accesses are copied, not retained. */
124int omega_gpu_cmdlist_add_accesses(OmegaGpuCmdList *cl, const OmegaGpuLaunch *L,
125 const OmegaGpuCmdListAccess *accesses, uint32_t n_accesses);
126/* Attach/replace the COMPLETE effects of an already recorded launch, including
127 * one recorded by a kernel helper. Rejection leaves its previous effects intact.
128 * Record bytes are unchanged. Same range validation and caller lock as above. */
129int omega_gpu_cmdlist_set_accesses(OmegaGpuCmdList *cl, uint32_t index,
130 const OmegaGpuCmdListAccess *accesses, uint32_t n_accesses);
131/* Plan/submit a whole generated program. The seeded planner compares every
132 * outstanding launch since the last drain and inserts a full WFI+invalidate on
133 * RAW, WAR or WAW. No inter-launch wait is emitted for independent accesses.
134 * The leading admission barrier and completion release remain. ONE submission
135 * or refusal before queue commit; no implicit splitting. Empty programs plan
136 * and submit as zero submissions. PCAS/INLINE and stamps are supported;
137 * barrier/skip/first_barrier_none experimental controls and cross-list waits
138 * are refused on this path. Ring capacity includes the platform prefix.
139 * plan_program does not open a device; caller holds the lock for both calls. */
140int omega_gpu_cmdlist_plan_program(OmegaGpuCmdList *cl, const OmegaGpuCmdListOpts *opts,
141 OmegaGpuCmdListProgramPlan *plan);
142int omega_gpu_cmdlist_submit_program(OmegaGpuCmdList *cl, const OmegaGpuCmdListOpts *opts,
143 OmegaGpuCmdListStats *stats);
144/* Caller holds the lock, device open, not latched. Submits every recorded launch in order, waits for the completion,
145 * checks every launch semaphore. 0 ok; -1 with the stage named (omega_gpu_session_last_error). */
146int omega_gpu_cmdlist_submit(OmegaGpuCmdList *cl, const OmegaGpuCmdListOpts *opts, OmegaGpuCmdListStats *st);
147/* L136 (roadblock R3): caller holds the lock. List a on the session queue and list b on extra queue 1
148 * (omega_gpu_session_extra_queue; needs AIEN_OMEGA_GPU_CHANNELS >= 2 at device open), both submitted before either is
149 * waited, so the two may run at the same time. The lists must share no written buffer. PCAS mode, no stamps, each list in
150 * one submission. Both stats get the pair wall time (first submit to the last marker). 0 ok; -1 with the stage named;
151 * a marker timeout latches the session. */
152int omega_gpu_cmdlist_submit_pair(OmegaGpuCmdList *a, OmegaGpuCmdList *b, const OmegaGpuCmdListOpts *opts, OmegaGpuCmdListStats *sa, OmegaGpuCmdListStats *sb);
153/* C4 events. Caller holds the lock. The next launch added to cl waits, on the chip, until launch `index` of list `other`
154 * (already added there) has completed: a host semaphore ACQUIRE on that launch's release semaphore (DONE), emitted
155 * before the launch's barrier, which is always the full WAIT_FOR_IDLE + invalidate (whatever the options say), so the
156 * launch reads what the other launch wrote. A list waits on one other list only, and a list with waits runs only in
157 * submit_pair with that list (submit refuses it; submit_pair refuses a wait outside the pair or past its launches).
158 * 0 ok, -1 (no such launch, a second other list, or a list made before a session recovery). */
159int omega_gpu_cmdlist_wait_for(OmegaGpuCmdList *cl, const OmegaGpuCmdList *other, uint32_t index);
160/* 1 once a timeout latched this module, until omega_gpu_session_recover (C5). */
161int omega_gpu_cmdlist_is_latched(void);
162
163/* After a submit with opts.stamps = 1: out[0] = GPU timestamp (ns, PTIMER) before launch 0, out[i + 1] = after launch i completed
164 * (taken after its WAIT_FOR_IDLE). Returns the count written (launches + 1) or -1 (stamps off, slot unwritten, cap too small). */
165int omega_gpu_cmdlist_stamps(const OmegaGpuCmdList *cl, uint64_t *out, uint32_t cap);
166
167#ifdef __cplusplus
168}
169#endif
170#endif