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 / tests / test_omega_gpu_wait.c

319 lines · 14870 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/* Host test for src/omega_gpu_wait.c: fake memory, fake clock, no GPU.
18 * Every scenario runs against the real primitive (must behave) and against
19 * mutants (a naive wall-clock loop, a fixed wait that accepts overshoot, a
20 * sequence wait with an unsigned compare). Each mutant must be killed by the
21 * scenarios named for it, otherwise this test fails. */
22#include "omega_gpu_wait.h"
23
24#include <stdio.h>
25#include <string.h>
26
27typedef struct {
28 uint64_t t_ns;
29 uint64_t step_ns; /* clock advance per relax */
30 uint64_t relaxes;
31 uint64_t barriers;
32 uint64_t at[32];
33 volatile uint32_t *target[32];
34 uint32_t value[32];
35 int nsched;
36 uint32_t invalid_value; /* valid() rejects this value when nonzero */
37 int abort_at; /* abort() true once relaxes >= abort_at when nonzero */
38 uint64_t snaps; /* polls seen by the snapshot hook (it runs right after the reads) */
39 uint64_t last_snap_barriers;
40 uint64_t order_violations; /* polls whose reads were not preceded by a fresh barrier */
41} Fake;
42
43static uint64_t fk_now(void *c) { return ((Fake *)c)->t_ns; }
44static void fk_barrier(void *c) { ((Fake *)c)->barriers++; }
45static void fk_relax(void *c) {
46 Fake *f = c;
47 f->relaxes++;
48 f->t_ns += f->step_ns;
49 for (int i = 0; i < f->nsched; i++)
50 if (f->at[i] == f->relaxes) *f->target[i] = f->value[i];
51}
52static void fk_snapshot(void *c, uint32_t out[2]) {
53 Fake *f = c;
54 out[0] = out[1] = 0;
55 f->snaps++;
56 if (f->barriers <= f->last_snap_barriers) f->order_violations++;
57 f->last_snap_barriers = f->barriers;
58}
59static bool fk_valid(void *c, uint32_t v) { return !((Fake *)c)->invalid_value || v != ((Fake *)c)->invalid_value; }
60static bool fk_abort(void *c) { Fake *f = c; return f->abort_at && f->relaxes >= (uint64_t)f->abort_at; }
61
62typedef bool (*WaitFn)(volatile uint32_t *, uint32_t, omega_gpu_wait_report_t *, const omega_gpu_wait_cfg_t *);
63
64/* Mutants. mode 0: naive wall clock (single deadline, unsigned <, no progress
65 * tracking, no marker2, no barrier, no report). mode 1: exact for sequence too
66 * is not needed; mode "fixed_ge": signed >= for a fixed wait. mode "seq_unsigned":
67 * unsigned >= for a sequence wait. The last two keep everything else correct. */
68static bool naive_wait(volatile uint32_t *word, uint32_t want, omega_gpu_wait_report_t *rpt,
69 const omega_gpu_wait_cfg_t *cfg) {
70 uint64_t start = cfg->now_ns(cfg->hook_ctx);
71 uint64_t lim = 1000000ull * cfg->progress_timeout_ms;
72 if (rpt) memset(rpt, 0, sizeof(*rpt));
73 while (*word < want) {
74 if (cfg->now_ns(cfg->hook_ctx) - start >= lim) return false;
75 cfg->relax(cfg->hook_ctx);
76 }
77 return true;
78}
79static bool mut_loop(int cmp, volatile uint32_t *w, uint32_t want, omega_gpu_wait_report_t *rpt,
80 const omega_gpu_wait_cfg_t *cfg, omega_gpu_wait_kind_t kind) {
81 /* Correct kind-aware wait with one comparison replaced; delegate the rest
82 * to the real primitive by wrapping the watched value would be circular, so
83 * implement the minimal real loop here. */
84 uint64_t start = cfg->now_ns(cfg->hook_ctx), last = start;
85 uint64_t stall = 1000000ull * cfg->progress_timeout_ms;
86 uint32_t prev = *w;
87 if (rpt) { memset(rpt, 0, sizeof(*rpt)); rpt->wait_kind = kind; rpt->expected = want; }
88 for (;;) {
89 cfg->barrier(cfg->hook_ctx);
90 uint32_t m = *w, m2 = cfg->marker2 ? *cfg->marker2 : 0;
91 bool ok = cmp == 0 ? m == want : cmp == 1 ? (int32_t)(m - want) >= 0 : m >= want;
92 if (rpt) { rpt->last_observed = m; rpt->last_marker2 = m2; }
93 if (ok && (!cfg->marker2 || m2 == cfg->marker2_want)) {
94 cfg->barrier(cfg->hook_ctx);
95 if (rpt) rpt->result = OMEGA_GPU_WAIT_PASS;
96 return true;
97 }
98 uint64_t now = cfg->now_ns(cfg->hook_ctx);
99 if (m != prev) { prev = m; last = now; if (rpt) rpt->progress_count++; }
100 if (now - last >= stall) { if (rpt) rpt->result = OMEGA_GPU_WAIT_STALLED; return false; }
101 cfg->relax(cfg->hook_ctx);
102 }
103}
104static bool mut_fixed_ge(volatile uint32_t *w, uint32_t v, omega_gpu_wait_report_t *r, const omega_gpu_wait_cfg_t *c) {
105 return mut_loop(1, w, v, r, c, OMEGA_GPU_WAIT_FIXED);
106}
107static bool mut_seq_unsigned(volatile uint32_t *w, uint32_t v, omega_gpu_wait_report_t *r, const omega_gpu_wait_cfg_t *c) {
108 return mut_loop(2, w, v, r, c, OMEGA_GPU_WAIT_SEQUENCE);
109}
110
111static int g_fail, g_checks, g_quiet;
112#define CHECK(c, ...) do { g_checks++; if (!(c)) { g_fail++; if (!g_quiet) { printf(" FAIL: " __VA_ARGS__); printf("\n"); } } } while (0)
113
114static void setup(Fake *f, omega_gpu_wait_cfg_t *cfg) {
115 memset(f, 0, sizeof(*f));
116 f->step_ns = 1000000ull; /* 1 ms per poll */
117 memset(cfg, 0, sizeof(*cfg));
118 cfg->progress_timeout_ms = 100;
119 cfg->total_timeout_ms = 100000;
120 cfg->now_ns = fk_now;
121 cfg->barrier = fk_barrier;
122 cfg->relax = fk_relax;
123 cfg->valid = fk_valid;
124 cfg->abort = fk_abort;
125 cfg->hook_ctx = f;
126}
127static void sched(Fake *f, uint64_t at, volatile uint32_t *t, uint32_t v) {
128 f->at[f->nsched] = at; f->target[f->nsched] = t; f->value[f->nsched] = v; f->nsched++;
129}
130
131enum { S_NEVER, S_LATE, S_MARKER2, S_SEQ_WRAP, S_PROGRESS_RESETS, S_BARRIER, S_HARD_DEADLINE,
132 S_FIXED_OVERSHOOT, S_INVALID, S_ABORT, S_COUNT };
133static const char *names[S_COUNT] = {"never_written", "written_late", "marker2_gate", "seq_wrap32",
134 "progress_resets_idle", "barrier_each_read", "hard_deadline_with_progress",
135 "fixed_overshoot", "invalid_state", "abort"};
136
137/* Returns failed checks. fx and sq are the fixed and sequence implementations. */
138static int scenario(const char *name, WaitFn fx, WaitFn sq, int which) {
139 volatile uint32_t w = 0, w2 = 0;
140 Fake f;
141 omega_gpu_wait_cfg_t cfg;
142 omega_gpu_wait_report_t r;
143 int before = g_fail;
144 bool ok;
145 setup(&f, &cfg);
146
147 switch (which) {
148 case S_NEVER:
149 ok = sq(&w, 5, &r, &cfg);
150 CHECK(!ok, "%s: never-written marker must not pass", name);
151 CHECK(r.result == OMEGA_GPU_WAIT_STALLED, "%s: result STALLED (got %d)", name, r.result);
152 CHECK(r.last_observed == 0 && r.expected == 5 && r.wait_kind == OMEGA_GPU_WAIT_SEQUENCE,
153 "%s: report carries kind/expected/last-seen 0", name);
154 CHECK(r.elapsed_ns >= 100000000ull && r.progress_count == 0, "%s: elapsed %llu progress %llu", name,
155 (unsigned long long)r.elapsed_ns, (unsigned long long)r.progress_count);
156 break;
157 case S_LATE:
158 sched(&f, 60, &w, 7);
159 ok = fx(&w, 7, &r, &cfg);
160 CHECK(ok && r.result == OMEGA_GPU_WAIT_PASS && r.last_observed == 7, "%s: late fixed marker seen", name);
161 CHECK(r.progress_count == 1 && r.last_progress_ns > r.start_ns, "%s: progress recorded", name);
162 break;
163 case S_MARKER2:
164 cfg.marker2 = &w2;
165 cfg.marker2_want = 0x46464646u;
166 sched(&f, 10, &w, 3);
167 ok = fx(&w, 3, &r, &cfg);
168 CHECK(!ok, "%s: marker without marker2 must not pass", name);
169 CHECK(r.last_observed == 3 && r.last_marker2 == 0, "%s: report shows marker 3, marker2 0", name);
170 w = 0; w2 = 0; setup(&f, &cfg);
171 cfg.marker2 = &w2;
172 cfg.marker2_want = 0x46464646u;
173 sched(&f, 10, &w, 3);
174 sched(&f, 20, &w2, 0x46464646u);
175 ok = fx(&w, 3, &r, &cfg);
176 CHECK(ok && r.last_marker2 == 0x46464646u, "%s: both markers pass", name);
177 break;
178 case S_SEQ_WRAP: {
179 static const uint32_t tg[4] = {0xfffffffeu, 0xffffffffu, 0u, 1u};
180 for (int i = 0; i < 4; i++) {
181 w = tg[i] - 1u; /* one step behind, wrapping through 0 */
182 setup(&f, &cfg);
183 sched(&f, 10, &w, tg[i]);
184 ok = sq(&w, tg[i], &r, &cfg);
185 CHECK(ok, "%s: reaches target %08x from %08x", name, tg[i], tg[i] - 1u);
186 }
187 w = 3; setup(&f, &cfg);
188 ok = sq(&w, 0xffffffffu, &r, &cfg);
189 CHECK(ok, "%s: 3 is past 0xffffffff", name);
190 w = 0; setup(&f, &cfg);
191 ok = sq(&w, 0xfffffffeu, &r, &cfg);
192 CHECK(ok, "%s: 0 is past 0xfffffffe", name);
193 w = 0xfffffff0u; setup(&f, &cfg);
194 ok = sq(&w, 0xffffffffu, &r, &cfg);
195 CHECK(!ok, "%s: 0xfffffff0 is before 0xffffffff", name);
196 w = 0xfffffffeu; setup(&f, &cfg);
197 ok = sq(&w, 1, &r, &cfg);
198 CHECK(!ok, "%s: 0xfffffffe is before 1", name);
199 break;
200 }
201 case S_PROGRESS_RESETS:
202 for (int i = 0; i < 4; i++) sched(&f, 60u * (i + 1), &w, (uint32_t)(i + 1));
203 ok = sq(&w, 4, &r, &cfg); /* 240 ms total, 60 ms gaps, 100 ms stall bound */
204 CHECK(ok, "%s: steady progress must not stall", name);
205 CHECK(r.progress_count == 4, "%s: progress_count %llu", name, (unsigned long long)r.progress_count);
206 break;
207 case S_BARRIER:
208 sched(&f, 5, &w, 1);
209 cfg.snapshot = fk_snapshot; /* runs after the reads of each poll: marks where the read happened */
210 cfg.snapshot_ctx = &f;
211 ok = sq(&w, 1, &r, &cfg);
212 CHECK(ok && r.polls > 0 && f.barriers >= r.polls + 1, "%s: barriers %llu polls %llu", name,
213 (unsigned long long)f.barriers, (unsigned long long)r.polls);
214 CHECK(f.snaps == r.polls && f.order_violations == 0, "%s: a barrier must precede every read (%llu of %llu polls unordered)", name,
215 (unsigned long long)f.order_violations, (unsigned long long)f.snaps);
216 CHECK(f.barriers > f.last_snap_barriers, "%s: barrier after the PASS observation", name);
217 break;
218 case S_HARD_DEADLINE:
219 cfg.total_timeout_ms = 200;
220 for (int i = 0; i < 30; i++) sched(&f, (uint64_t)(20 * (i + 1)), &w, (uint32_t)(i + 1));
221 ok = sq(&w, 99, &r, &cfg); /* moves every 20 ms, never arrives */
222 CHECK(!ok && r.result == OMEGA_GPU_WAIT_TIMEOUT, "%s: hard deadline TIMEOUT (got %d)", name, r.result);
223 CHECK(r.progress_count > 3, "%s: progress was occurring (%llu)", name, (unsigned long long)r.progress_count);
224 break;
225 case S_FIXED_OVERSHOOT:
226 sched(&f, 10, &w, 5); /* jumps past the fixed value 3 */
227 ok = fx(&w, 3, &r, &cfg);
228 CHECK(!ok, "%s: fixed wait must not accept overshoot 5 for 3", name);
229 CHECK(r.result == OMEGA_GPU_WAIT_STALLED && r.last_observed == 5, "%s: stalled, saw 5", name);
230 w = 0; setup(&f, &cfg);
231 sched(&f, 10, &w, 5);
232 ok = sq(&w, 3, &r, &cfg);
233 CHECK(ok, "%s: sequence wait accepts 5 for target 3", name);
234 break;
235 case S_INVALID:
236 f.invalid_value = 9;
237 sched(&f, 10, &w, 9);
238 ok = sq(&w, 20, &r, &cfg);
239 CHECK(!ok && r.result == OMEGA_GPU_WAIT_INVALID_STATE && r.last_observed == 9,
240 "%s: invalid value reported (got %d)", name, r.result);
241 break;
242 case S_ABORT:
243 f.abort_at = 7;
244 ok = sq(&w, 20, &r, &cfg);
245 CHECK(!ok && r.result == OMEGA_GPU_WAIT_ABORTED, "%s: abort reported (got %d)", name, r.result);
246 break;
247 }
248 return g_fail - before;
249}
250
251/* CHIPWAIT-1c: QUEUE_WRAP marker2 pattern (src/omega_world_gates.c). Every entry of a
252 * batch but the last writes its dispatch_id to marker2; the last writes the sentinel
253 * 0x46464646. A sequence wait for the sentinel must not accept dispatch ids (even a
254 * high one), and must pass when the sentinel lands. */
255static int sentinel_batch(void) {
256 volatile uint32_t w2 = 0;
257 Fake f;
258 omega_gpu_wait_cfg_t cfg;
259 omega_gpu_wait_report_t r;
260 int before = g_fail;
261 setup(&f, &cfg);
262 for (uint32_t id = 1; id <= 15; id++) sched(&f, id, &w2, 3000u + id);
263 bool ok = omega_gpu_wait_sequence(&w2, 0x46464646u, &r, &cfg);
264 CHECK(!ok && r.result == OMEGA_GPU_WAIT_STALLED && r.last_observed == 3015u,
265 "sentinel: dispatch ids alone must not satisfy the sentinel wait (got %d, observed %u)", r.result, r.last_observed);
266 w2 = 0;
267 setup(&f, &cfg);
268 for (uint32_t id = 1; id <= 15; id++) sched(&f, id, &w2, 3000u + id);
269 sched(&f, 16, &w2, 0x46464646u);
270 ok = omega_gpu_wait_sequence(&w2, 0x46464646u, &r, &cfg);
271 CHECK(ok && r.result == OMEGA_GPU_WAIT_PASS && r.last_observed == 0x46464646u,
272 "sentinel: last entry's sentinel passes (got %d, observed %u)", r.result, r.last_observed);
273 return g_fail - before;
274}
275
276typedef struct {
277 const char *label;
278 WaitFn fx, sq;
279 unsigned must_kill; /* bitmask of scenarios that must kill this mutant */
280} Mutant;
281
282int main(void) {
283 for (int s = 0; s < S_COUNT; s++) {
284 int bad = scenario(names[s], omega_gpu_wait_fixed, omega_gpu_wait_sequence, s);
285 printf("real %-28s %s\n", names[s], bad ? "FAIL" : "ok");
286 }
287 { int bad = sentinel_batch(); printf("real %-28s %s\n", "queue_wrap_sentinel", bad ? "FAIL" : "ok"); }
288 int real_fail = g_fail;
289
290 const Mutant muts[] = {
291 {"naive_wall_clock", naive_wait, naive_wait, (1u << S_COUNT) - 1u},
292 {"fixed_accepts_overshoot", mut_fixed_ge, omega_gpu_wait_sequence, 1u << S_FIXED_OVERSHOOT},
293 {"seq_unsigned_compare", omega_gpu_wait_fixed, mut_seq_unsigned, 1u << S_SEQ_WRAP},
294 };
295 int saved = g_fail, kill_total = 0, kill_need = 0;
296 g_quiet = 1;
297 for (unsigned m = 0; m < sizeof(muts) / sizeof(muts[0]); m++) {
298 unsigned killed = 0;
299 for (int s = 0; s < S_COUNT; s++) {
300 if (scenario(names[s], muts[m].fx, muts[m].sq, s) > 0) killed |= 1u << s;
301 }
302 g_fail = saved;
303 g_quiet = 0;
304 for (int s = 0; s < S_COUNT; s++) {
305 if (!(muts[m].must_kill & (1u << s))) continue;
306 kill_need++;
307 g_checks++;
308 if (killed & (1u << s)) kill_total++;
309 else { g_fail++; printf(" FAIL: mutant %s survived scenario %s\n", muts[m].label, names[s]); }
310 }
311 printf("mutant %-26s killed mask %03x (required %03x)\n", muts[m].label, killed, muts[m].must_kill);
312 saved = g_fail;
313 g_quiet = 1;
314 }
315 g_quiet = 0;
316 printf("gpu_wait: checks=%d real_failures=%d required_kills=%d/%d\n", g_checks, real_fail, kill_total, kill_need);
317 printf("%s\n", g_fail ? "OMEGA_GPU_WAIT_HOST FAIL" : "OMEGA_GPU_WAIT_HOST PASS");
318 return g_fail ? 1 : 0;
319}