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_realize_synth.c

302 lines · 12944 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_realize_synth.h"
18#include "omega_core.h"
19#include "sha256.h"
20#include "aarch64_target.h"
21#include "aarch64_encoder.h"
22#include "aarch64_decoder.h"
23#include <string.h>
24#include <stdio.h>
25#include "omega_exec.h"
26#include "omega_canonical.h"
27#include "searchtrace/st_hook.h"
28
29void omega_realize_task_init(RealizationSynthesisTask *task,
30 const OmegaProgram *prog,
31 const OmegaMachineGraph *mg) {
32 if (!task) return;
33 memset(task, 0, sizeof(*task));
34 task->program = prog;
35 task->machine = mg;
36 task->optimize_latency = true;
37 task->max_unroll_factor = 1;
38}
39
40int omega_realize_compute_triple_id(const SemanticId *semantic_id,
41 const SemanticId *machine_id,
42 const RealizationObject *real,
43 SemanticId *out_id) {
44 if (!semantic_id || !machine_id || !real || !out_id) return -1;
45 if (real->code_len > sizeof(real->code_bytes)) return -1;
46
47 /* Header is buffered; code bytes are streamed after it, so every byte up
48 * to full capacity reaches the hash. */
49 uint8_t buffer[4 + 1 + 1 + 4 + 4 + OMEGA_ID_BYTES + OMEGA_ID_BYTES];
50 size_t pos = 0;
51
52 /* Canonical OMG0 wire header */
53 buffer[pos++] = 'O';
54 buffer[pos++] = 'M';
55 buffer[pos++] = 'G';
56 buffer[pos++] = '0';
57 buffer[pos++] = KIND_REALIZATION; /* 0x08 */
58 buffer[pos++] = real->target_profile;
59
60 /* Entry offset (4 bytes big-endian) */
61 buffer[pos++] = (uint8_t)((real->entry_offset >> 24) & 0xFF);
62 buffer[pos++] = (uint8_t)((real->entry_offset >> 16) & 0xFF);
63 buffer[pos++] = (uint8_t)((real->entry_offset >> 8) & 0xFF);
64 buffer[pos++] = (uint8_t)(real->entry_offset & 0xFF);
65
66 /* Code length (4 bytes big-endian) */
67 uint32_t clen = (uint32_t)real->code_len;
68 buffer[pos++] = (uint8_t)((clen >> 24) & 0xFF);
69 buffer[pos++] = (uint8_t)((clen >> 16) & 0xFF);
70 buffer[pos++] = (uint8_t)((clen >> 8) & 0xFF);
71 buffer[pos++] = (uint8_t)(clen & 0xFF);
72
73 /* Semantic ID (32 bytes) from G_S */
74 memcpy(&buffer[pos], semantic_id->bytes, OMEGA_ID_BYTES);
75 pos += OMEGA_ID_BYTES;
76
77 /* Machine ID (32 bytes) from G_M */
78 memcpy(&buffer[pos], machine_id->bytes, OMEGA_ID_BYTES);
79 pos += OMEGA_ID_BYTES;
80
81 /* Raw Machine Code Bytes */
82 sha256_ctx ctx;
83 sha256_init(&ctx);
84 sha256_update(&ctx, buffer, pos);
85 sha256_update(&ctx, real->code_bytes, real->code_len);
86 sha256_final(&ctx, out_id->bytes);
87 return 0;
88}
89
90/* ---- verification of a machine-bound realization against its program ------ */
91
92/* Deterministic xorshift64* seeded from the program id: same program, same inputs. */
93static uint64_t rng_next(uint64_t *s) {
94 uint64_t x = *s;
95 x ^= x >> 12; x ^= x << 25; x ^= x >> 27;
96 *s = x;
97 return x * 0x2545F4914F6CDD1Dull;
98}
99
100size_t omega_realize_differential_inputs(const OmegaProgram *prog, uint64_t *xs, size_t max) {
101 if (!prog || !xs || max < OMEGA_REALIZE_DIFF_INPUTS) return 0;
102 uint16_t w = prog->contract.input_width;
103 uint64_t mask = (w >= 64 || w == 0) ? ~0ull : ((1ull << w) - 1ull);
104 uint64_t top = (w >= 64 || w == 0) ? (1ull << 63) : (1ull << (w - 1));
105 const uint64_t fixed[] = { 0, 1, 2, 3, 5, 7, 10, 100, mask, mask - 1, mask - 2, top, top - 1, top + 1,
106 0x5555555555555555ull & mask, 0xAAAAAAAAAAAAAAAAull & mask,
107 0xFFFFull & mask, 0x10000ull & mask, 0xFFFFFFFFull & mask, 0x100000000ull & mask };
108 size_t n = 0;
109 for (size_t i = 0; i < sizeof fixed / sizeof fixed[0]; ++i) xs[n++] = fixed[i] & mask;
110 uint64_t s = 0x9E3779B97F4A7C15ull;
111 for (size_t i = 0; i < 8; ++i) s ^= (uint64_t)prog->program_id.bytes[i] << (8 * i);
112 if (!s) s = 1;
113 while (n < OMEGA_REALIZE_DIFF_INPUTS) xs[n++] = rng_next(&s) & mask;
114 return n;
115}
116
117int omega_realization_verify_program(const OmegaProgram *prog, const OmegaMachineGraph *mg,
118 const RealizationObject *real, uint32_t *out_checked,
119 char *why, size_t why_len) {
120 char tmp[256];
121 if (out_checked) *out_checked = 0;
122 if (!prog || !mg || !real) { if (why && why_len) snprintf(why, why_len, "bad arguments"); return -1; }
123 /* 1. semantic side: the program itself passes the V0 gate (id recomputed from the body) */
124 if (omega_program_realize_check(prog, NULL, tmp, sizeof tmp) != 0) {
125 if (why && why_len) snprintf(why, why_len, "program refused: %s", tmp);
126 return -1;
127 }
128 if (omega_compare_semantic_id(&real->semantic_id, &prog->program_id) != 0) {
129 if (why && why_len) snprintf(why, why_len, "SEMANTIC_ID of realization is not this program's id");
130 return -1;
131 }
132 /* 2. machine side: machine graph id is its own canonical id; realization bound to it */
133 OmegaMachineGraph mcopy = *mg;
134 if (omega_machine_compute_id(&mcopy) != 0 || omega_compare_semantic_id(&mcopy.machine_id, &mg->machine_id) != 0) {
135 if (why && why_len) snprintf(why, why_len, "machine graph id does not match its content");
136 return -1;
137 }
138 if (!real->has_machine_id || omega_compare_semantic_id(&real->machine_id, &mg->machine_id) != 0) {
139 if (why && why_len) snprintf(why, why_len, "MACHINE_ID of realization is not this machine's id");
140 return -1;
141 }
142 if (real->target_profile != mg->target_profile) {
143 if (why && why_len) snprintf(why, why_len, "realization profile differs from machine profile");
144 return -1;
145 }
146 /* 3. triple binding + structure (V0 recomputes the triple id over the code bytes) */
147 VerifyReport rep;
148 memset(&rep, 0, sizeof rep);
149 if (omega_verify_v0_structural(NULL, real, &rep) != 0 || !rep.passed) {
150 if (why && why_len) snprintf(why, why_len, "%.200s", rep.error_detail);
151 return -1;
152 }
153 /* 4. differential: native execution of the bytes == independent semantic evaluator */
154 uint64_t xs[OMEGA_REALIZE_DIFF_INPUTS], ys[OMEGA_REALIZE_DIFF_INPUTS];
155 size_t n = omega_realize_differential_inputs(prog, xs, OMEGA_REALIZE_DIFF_INPUTS);
156 if (n == 0 || omega_program_eval(prog, xs, n, ys) != 0) {
157 if (why && why_len) snprintf(why, why_len, "semantic evaluator refused the program");
158 return -1;
159 }
160 for (size_t i = 0; i < n; ++i) {
161 uint64_t got = 0;
162 int rc = omega_exec_native_f3(real, xs[i], 0, 0, &got);
163 if (rc == -2) {
164 if (why && why_len) snprintf(why, why_len, "not verifiable here: native differential needs an AArch64 host");
165 return -3;
166 }
167 if (rc != 0) {
168 if (why && why_len) snprintf(why, why_len, "native execution failed");
169 return -1;
170 }
171 if (got != ys[i]) {
172 if (why && why_len)
173 snprintf(why, why_len, "differential MISMATCH x=0x%llx: semantic=0x%llx native=0x%llx",
174 (unsigned long long)xs[i], (unsigned long long)ys[i], (unsigned long long)got);
175 return -1;
176 }
177 if (out_checked) (*out_checked)++;
178 }
179 if (why && why_len) why[0] = '\0';
180 return 0;
181}
182
183/* ---- G_S x G_M -> G_R: compile the actual program for the machine --------- */
184
185OmegaRealizeSchedule omega_realize_choose_schedule(const OmegaMachineGraph *mg) {
186 /* MachineGraph chooses the schedule only; both schedules compute the same function
187 * (checked by the differential). Wide issue + enough registers: preload constants. */
188 if (mg && mg->pipeline.issue_width >= 4 && mg->registers.gpr_count >= 16) return OMEGA_SCHED_PRELOAD;
189 return OMEGA_SCHED_SEQUENTIAL;
190}
191
192static int synthesize_realization_impl(const RealizationSynthesisTask *task,
193 RealizationSynthesisResult *result) {
194 if (!task || !task->program || !task->machine || !result) return -1;
195 memset(result, 0, sizeof(*result));
196
197 const OmegaProgram *prog = task->program;
198 const OmegaMachineGraph *mg = task->machine;
199
200 /* 1. canonical program -> verified semantic operation sequence (fail closed) */
201 uint16_t width = 0;
202 if (omega_program_realize_check(prog, &width, result->why, sizeof result->why) != 0) return -2;
203 if (mg->target_profile != AARCH64_PROFILE_V8A_BAREMETAL) {
204 snprintf(result->why, sizeof result->why, "machine profile 0x%02x is not the V0 AArch64 target",
205 (unsigned)mg->target_profile);
206 return -2;
207 }
208
209 /* 2. machine-aware planning (schedule choice only) */
210 result->schedule = omega_realize_choose_schedule(mg);
211
212 /* 3. target realization (AArch64 V0) through the one encoder */
213 RealizationObject *r = &result->realization;
214 size_t len = 0;
215 if (omega_program_emit_schedule(&prog->body, width, result->schedule, r->code_bytes, &len,
216 sizeof r->code_bytes) != 0) {
217 snprintf(result->why, sizeof result->why, "emitter refused the body (code buffer or encoding)");
218 return -2;
219 }
220 r->code_len = len;
221 r->semantic_id = prog->program_id;
222 r->machine_id = mg->machine_id;
223 r->has_machine_id = true;
224 r->target_profile = mg->target_profile;
225 r->entry_offset = 0;
226 result->code_bytes_len = (uint32_t)len;
227
228 /* 4. RealizationId = triple(SEMANTIC_ID, MACHINE_ID, code) */
229 omega_realize_compute_triple_id(&prog->program_id, &mg->machine_id, r, &result->realization_id);
230 r->realization_id = result->realization_id;
231 r->has_id = true;
232 result->estimated_cycles = omega_machine_estimate_latency(mg, r);
233
234 /* 5. verification: bindings + structure + differential vs the semantic evaluator */
235 int vrc = omega_realization_verify_program(prog, mg, r, &result->inputs_checked, result->why, sizeof result->why);
236 memset(&result->verify_report, 0, sizeof result->verify_report);
237 if (vrc != 0) {
238 result->verify_report.passed = false;
239 snprintf(result->verify_report.error_detail, sizeof result->verify_report.error_detail, "%.200s", result->why);
240 return vrc == -3 ? -3 : -1;
241 }
242 omega_verify_v2_properties(NULL, r, &result->verify_report);
243 result->solved = result->verify_report.passed;
244 return result->solved ? 0 : -1;
245}
246
247/* ---- M23 search-trace recorder hook (src/searchtrace/st_hook.h) ----------
248 * Off by default: with no hook installed omega_synthesize_realization is the
249 * unchanged body above plus one null test. The recorder sees the finished
250 * result after the body returns and cannot change it. */
251static _Thread_local StHookFn st_realize_hook;
252static _Thread_local void *st_realize_hook_ctx;
253
254int omega_realize_set_trace_hook(StHookFn fn, void *ctx) {
255 if (!fn || st_realize_hook) return -1;
256 st_realize_hook = fn;
257 st_realize_hook_ctx = ctx;
258 return 0;
259}
260
261int omega_realize_clear_trace_hook(void *ctx) {
262 if (!st_realize_hook || st_realize_hook_ctx != ctx) return -1;
263 st_realize_hook = NULL;
264 st_realize_hook_ctx = NULL;
265 return 0;
266}
267
268int omega_synthesize_realization(const RealizationSynthesisTask *task,
269 RealizationSynthesisResult *result) {
270 int rc = synthesize_realization_impl(task, result);
271 if (st_realize_hook && task && result) {
272 StEvent ev;
273 memset(&ev, 0, sizeof ev);
274 ev.kind = ST_EV_REALIZATION;
275 ev.u.realize.program = task->program;
276 ev.u.realize.machine = task->machine;
277 ev.u.realize.result = result;
278 ev.u.realize.rc = rc;
279 st_realize_hook(st_realize_hook_ctx, &ev);
280 }
281 return rc;
282}
283
284int omega_synthesize_for_dgx_spark(const OmegaProgram *prog, RealizationSynthesisResult *result) {
285 if (!prog || !result) return -1;
286 OmegaMachineGraph spark;
287 omega_machine_build_dgx_spark(&spark);
288
289 RealizationSynthesisTask task;
290 omega_realize_task_init(&task, prog, &spark);
291 return omega_synthesize_realization(&task, result);
292}
293
294int omega_synthesize_for_qemu_virt(const OmegaProgram *prog, RealizationSynthesisResult *result) {
295 if (!prog || !result) return -1;
296 OmegaMachineGraph qemu;
297 omega_machine_build_qemu_virt(&qemu);
298
299 RealizationSynthesisTask task;
300 omega_realize_task_init(&task, prog, &qemu);
301 return omega_synthesize_realization(&task, result);
302}