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
omega-mv / src / visor / visor_realization.c
| 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 | /* |
| 18 | * visor_realization.c -- Omega Visor V1, lane 5: realization lab. |
| 19 | * See visor_realization.h. Reuses existing realizers only; never invents code, |
| 20 | * ids or costs. No physics headers. Fixed storage. Fails closed. |
| 21 | */ |
| 22 | #include "visor_realization.h" |
| 23 | #include "visor_machine.h" |
| 24 | #include "omega_core.h" |
| 25 | #include "omega_realize.h" |
| 26 | #include "omega_realize_synth.h" |
| 27 | #include "omega_exec.h" |
| 28 | #include "aarch64_decoder.h" |
| 29 | #include "aarch64_target.h" |
| 30 | |
| 31 | #include <inttypes.h> |
| 32 | #include <stdarg.h> |
| 33 | #include <stdio.h> |
| 34 | #include <string.h> |
| 35 | |
| 36 | /* ---- small helpers ------------------------------------------------------- */ |
| 37 | |
| 38 | typedef struct { char *buf; size_t n, pos; bool trunc; } Out; |
| 39 | |
| 40 | static void outf(Out *o, const char *fmt, ...) __attribute__((format(printf, 2, 3))); |
| 41 | static void outf(Out *o, const char *fmt, ...) { |
| 42 | if (o->trunc) return; |
| 43 | va_list ap; |
| 44 | va_start(ap, fmt); |
| 45 | int w = vsnprintf(o->buf + o->pos, o->n - o->pos, fmt, ap); |
| 46 | va_end(ap); |
| 47 | if (w < 0 || (size_t)w >= o->n - o->pos) { o->trunc = true; o->buf[o->n - 1] = '\0'; return; } |
| 48 | o->pos += (size_t)w; |
| 49 | } |
| 50 | |
| 51 | static void json_str(Out *o, const char *s) { |
| 52 | outf(o, "\""); |
| 53 | for (; s && *s; ++s) { |
| 54 | unsigned char c = (unsigned char)*s; |
| 55 | if (c == '"' || c == '\\') outf(o, "\\%c", c); |
| 56 | else if (c < 0x20) outf(o, "\\u%04x", c); |
| 57 | else outf(o, "%c", c); |
| 58 | } |
| 59 | outf(o, "\""); |
| 60 | } |
| 61 | |
| 62 | static bool id_is_zero(const SemanticId *id) { |
| 63 | for (size_t i = 0; i < OMEGA_ID_BYTES; ++i) if (id->bytes[i]) return false; |
| 64 | return true; |
| 65 | } |
| 66 | |
| 67 | static bool id_eq(const SemanticId *a, const SemanticId *b) { |
| 68 | return memcmp(a->bytes, b->bytes, OMEGA_ID_BYTES) == 0; |
| 69 | } |
| 70 | |
| 71 | static const char *opcode_name(OpCode op) { |
| 72 | switch (op) { |
| 73 | case OP_ADD: return "ADD"; |
| 74 | case OP_SUB: return "SUB"; |
| 75 | case OP_MUL: return "MUL"; |
| 76 | case OP_DIV: return "DIV"; |
| 77 | case OP_AND: return "AND"; |
| 78 | case OP_OR: return "OR"; |
| 79 | case OP_NOT: return "NOT"; |
| 80 | case OP_EQUAL: return "EQUAL"; |
| 81 | case OP_LESS_THAN: return "LESS_THAN"; |
| 82 | case OP_IDENTITY: return "IDENTITY"; |
| 83 | case OP_CONSTANT: return "CONSTANT"; |
| 84 | case OP_SELECT: return "SELECT"; |
| 85 | case OP_CONCAT: return "CONCAT"; |
| 86 | case OP_SLICE: return "SLICE"; |
| 87 | case OP_COMPILE: return "COMPILE"; |
| 88 | case OP_CONVERT: return "CONVERT"; |
| 89 | default: return "INVALID"; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | static const char *cost_cls_name(VisorCostClass c) { |
| 94 | switch (c) { |
| 95 | case VISOR_COST_PREDICTED: return "predicted"; |
| 96 | case VISOR_COST_ESTIMATED: return "estimated"; |
| 97 | case VISOR_COST_MEASURED: return "measured"; |
| 98 | case VISOR_COST_QUALIFIED: return "qualified"; |
| 99 | } |
| 100 | return "unknown"; |
| 101 | } |
| 102 | |
| 103 | /* Short target name for a canonical machine, else "" */ |
| 104 | static void machine_target_name(const OmegaMachineGraph *mg, char out[32]) { |
| 105 | OmegaMachineGraph c; |
| 106 | out[0] = '\0'; |
| 107 | if (!mg) { snprintf(out, 32, "%s", VISOR_TARGET_AARCH64); return; } |
| 108 | if (omega_machine_build_dgx_spark(&c) == 0 && id_eq(&c.machine_id, &mg->machine_id)) { |
| 109 | snprintf(out, 32, "%s", VISOR_TARGET_DGX_SPARK); return; |
| 110 | } |
| 111 | if (omega_machine_build_qemu_virt(&c) == 0 && id_eq(&c.machine_id, &mg->machine_id)) { |
| 112 | snprintf(out, 32, "%s", VISOR_TARGET_QEMU_VIRT); return; |
| 113 | } |
| 114 | snprintf(out, 32, "%s", VISOR_TARGET_AARCH64); |
| 115 | } |
| 116 | |
| 117 | /* ---- disassembly --------------------------------------------------------- */ |
| 118 | |
| 119 | static const char *reg(bool sf, uint8_t r, char buf[8]) { |
| 120 | if (r == 31) snprintf(buf, 8, "%s", sf ? "xzr" : "wzr"); |
| 121 | else snprintf(buf, 8, "%c%u", sf ? 'x' : 'w', r); |
| 122 | return buf; |
| 123 | } |
| 124 | |
| 125 | size_t visor_disasm(const uint8_t *code, size_t len, char lines[][48], size_t max) { |
| 126 | if (!code || !lines) return 0; |
| 127 | size_t count = 0; |
| 128 | for (size_t off = 0; off + 4 <= len && count < max; off += 4) { |
| 129 | uint32_t w = (uint32_t)code[off] | ((uint32_t)code[off + 1] << 8) | |
| 130 | ((uint32_t)code[off + 2] << 16) | ((uint32_t)code[off + 3] << 24); |
| 131 | DecodedInsn d; |
| 132 | char a[8], b[8], c[8]; |
| 133 | char *L = lines[count++]; |
| 134 | if (aarch64_decode_instruction(w, &d) != 0) { snprintf(L, 48, "%08x .word 0x%08x", w, w); continue; } |
| 135 | switch (d.op) { |
| 136 | case DECODED_ADD: case DECODED_SUB: case DECODED_MUL: case DECODED_AND: |
| 137 | case DECODED_ORR: case DECODED_EOR: { |
| 138 | const char *m = d.op == DECODED_ADD ? "add" : d.op == DECODED_SUB ? "sub" : |
| 139 | d.op == DECODED_MUL ? "mul" : d.op == DECODED_AND ? "and" : |
| 140 | d.op == DECODED_ORR ? "orr" : "eor"; |
| 141 | uint32_t imm6 = (w >> 10) & 0x3F, sh = (w >> 22) & 3; |
| 142 | if (d.op != DECODED_MUL && imm6) |
| 143 | snprintf(L, 48, "%08x %s %s, %s, %s, %s #%u", w, m, reg(d.sf, d.rd, a), reg(d.sf, d.rn, b), |
| 144 | reg(d.sf, d.rm, c), sh == 0 ? "lsl" : sh == 1 ? "lsr" : sh == 2 ? "asr" : "ror", imm6); |
| 145 | else |
| 146 | snprintf(L, 48, "%08x %s %s, %s, %s", w, m, reg(d.sf, d.rd, a), reg(d.sf, d.rn, b), |
| 147 | reg(d.sf, d.rm, c)); |
| 148 | break; |
| 149 | } |
| 150 | case DECODED_MOVZ: case DECODED_MOVK: { |
| 151 | uint32_t hw = (w >> 21) & 3; |
| 152 | if (hw) |
| 153 | snprintf(L, 48, "%08x %s %s, #0x%x, lsl #%u", w, d.op == DECODED_MOVZ ? "movz" : "movk", |
| 154 | reg(d.sf, d.rd, a), d.imm16, hw * 16); |
| 155 | else |
| 156 | snprintf(L, 48, "%08x %s %s, #0x%x", w, d.op == DECODED_MOVZ ? "movz" : "movk", |
| 157 | reg(d.sf, d.rd, a), d.imm16); |
| 158 | break; |
| 159 | } |
| 160 | case DECODED_RET: snprintf(L, 48, "%08x ret", w); break; |
| 161 | case DECODED_B: snprintf(L, 48, "%08x b imm26=0x%x", w, (unsigned)d.branch_imm); break; |
| 162 | case DECODED_B_COND: snprintf(L, 48, "%08x b.cond %u imm19=0x%x", w, (unsigned)d.cond, (unsigned)d.branch_imm); break; |
| 163 | case DECODED_CBZ: snprintf(L, 48, "%08x cbz %s imm19=0x%x", w, reg(d.sf, d.rd, a), (unsigned)d.branch_imm); break; |
| 164 | case DECODED_CBNZ: snprintf(L, 48, "%08x cbnz %s imm19=0x%x", w, reg(d.sf, d.rd, a), (unsigned)d.branch_imm); break; |
| 165 | case DECODED_ADR: snprintf(L, 48, "%08x adr", w); break; |
| 166 | case DECODED_LDR: snprintf(L, 48, "%08x ldr", w); break; |
| 167 | case DECODED_STR: snprintf(L, 48, "%08x str", w); break; |
| 168 | case DECODED_LDRB: snprintf(L, 48, "%08x ldrb", w); break; |
| 169 | case DECODED_STRB: snprintf(L, 48, "%08x strb", w); break; |
| 170 | case DECODED_SUBS: snprintf(L, 48, "%08x subs", w); break; |
| 171 | default: snprintf(L, 48, "%08x .word 0x%08x", w, w); break; |
| 172 | } |
| 173 | } |
| 174 | return count; |
| 175 | } |
| 176 | |
| 177 | /* ---- run gate ------------------------------------------------------------ */ |
| 178 | |
| 179 | static bool target_is_aarch64(const char *t) { |
| 180 | return strcmp(t, VISOR_TARGET_AARCH64) == 0 || strcmp(t, VISOR_TARGET_DGX_SPARK) == 0 || |
| 181 | strcmp(t, VISOR_TARGET_QEMU_VIRT) == 0; |
| 182 | } |
| 183 | |
| 184 | /* 0 = runnable; -3 = refused with reason. */ |
| 185 | static int run_check(const VisorRealizationEntry *e, char *why, size_t n) { |
| 186 | if (!e) { snprintf(why, n, "no realization"); return -3; } |
| 187 | SemanticId bw; |
| 188 | if (strcmp(e->target_name, VISOR_TARGET_BLACKWELL) == 0 || |
| 189 | (e->has_machine_id && visor_machine_blackwell_id(&bw) == 0 && id_eq(&bw, &e->machine_id))) { |
| 190 | snprintf(why, n, "Blackwell target: requires GPU submit authority; not linked in Visor V1"); |
| 191 | return -3; |
| 192 | } |
| 193 | if (!target_is_aarch64(e->target_name)) { |
| 194 | snprintf(why, n, "target '%.31s' is not an AArch64 target", e->target_name); |
| 195 | return -3; |
| 196 | } |
| 197 | if (e->real.target_profile != AARCH64_PROFILE_V8A_BAREMETAL) { |
| 198 | snprintf(why, n, "profile 0x%02x is not AArch64 v8a (0x01)", e->real.target_profile); |
| 199 | return -3; |
| 200 | } |
| 201 | size_t len = e->real.code_len; |
| 202 | if (len == 0 || len % 4 || len > AARCH64_MAX_CODE_BYTES || e->real.entry_offset != 0) { |
| 203 | snprintf(why, n, "code length %zu / entry offset %u not runnable", len, e->real.entry_offset); |
| 204 | return -3; |
| 205 | } |
| 206 | char err[96] = {0}; |
| 207 | if (aarch64_validate_code_buffer(e->real.code_bytes, len, err, sizeof(err)) != 0) { |
| 208 | snprintf(why, n, "code validation failed: %.80s", err); |
| 209 | return -3; |
| 210 | } |
| 211 | for (size_t off = 0; off < len; off += 4) { |
| 212 | const uint8_t *p = &e->real.code_bytes[off]; |
| 213 | uint32_t w = (uint32_t)p[0] | ((uint32_t)p[1] << 8) | ((uint32_t)p[2] << 16) | ((uint32_t)p[3] << 24); |
| 214 | DecodedInsn d; |
| 215 | if (aarch64_decode_instruction(w, &d) != 0) { |
| 216 | snprintf(why, n, "undecodable instruction 0x%08x at +%zu", w, off); |
| 217 | return -3; |
| 218 | } |
| 219 | bool last = off + 4 == len; |
| 220 | switch (d.op) { |
| 221 | case DECODED_ADD: case DECODED_SUB: case DECODED_MUL: case DECODED_AND: |
| 222 | case DECODED_ORR: case DECODED_EOR: case DECODED_MOVZ: case DECODED_MOVK: |
| 223 | if (last) { snprintf(why, n, "code does not end in RET"); return -3; } |
| 224 | break; |
| 225 | case DECODED_RET: |
| 226 | if (!last) { snprintf(why, n, "RET before end of code at +%zu", off); return -3; } |
| 227 | break; |
| 228 | default: |
| 229 | snprintf(why, n, "non-pure instruction 0x%08x at +%zu (branch/load/store not allowed)", w, off); |
| 230 | return -3; |
| 231 | } |
| 232 | } |
| 233 | #if !defined(__aarch64__) |
| 234 | snprintf(why, n, "host is not aarch64"); |
| 235 | return -3; |
| 236 | #else |
| 237 | snprintf(why, n, "pure AArch64 register-only code ending in RET; executes natively on host"); |
| 238 | return 0; |
| 239 | #endif |
| 240 | } |
| 241 | |
| 242 | int visor_realization_run_pure(const VisorRealizationEntry *e, const uint64_t *args, size_t argc, uint64_t *out) { |
| 243 | if (!e || !out || (argc > 0 && !args)) return -1; |
| 244 | if (argc > 3) return -3; |
| 245 | if (e->verdict_known && !e->compatible) return -3; |
| 246 | char why[160]; |
| 247 | if (run_check(e, why, sizeof(why)) != 0) return -3; |
| 248 | uint64_t a = argc > 0 ? args[0] : 0, b = argc > 1 ? args[1] : 0, c = argc > 2 ? args[2] : 0; |
| 249 | return omega_exec_native_f3(&e->real, a, b, c, out) == 0 ? 0 : -1; |
| 250 | } |
| 251 | |
| 252 | /* ---- cost ---------------------------------------------------------------- */ |
| 253 | |
| 254 | static void cost_slot(VisorCostItem *it, VisorCostClass cls) { |
| 255 | memset(it, 0, sizeof(*it)); |
| 256 | it->cls = cls; |
| 257 | it->cls_name = cost_cls_name(cls); |
| 258 | } |
| 259 | |
| 260 | int visor_realization_cost(const VisorRealizationEntry *e, const OmegaMachineGraph *mg, VisorCostView *out) { |
| 261 | if (!e || !out) return -1; |
| 262 | memset(out, 0, sizeof(*out)); |
| 263 | cost_slot(&out->predicted, VISOR_COST_PREDICTED); |
| 264 | cost_slot(&out->estimated, VISOR_COST_ESTIMATED); |
| 265 | cost_slot(&out->measured, VISOR_COST_MEASURED); |
| 266 | cost_slot(&out->qualified, VISOR_COST_QUALIFIED); |
| 267 | uint32_t insns = (uint32_t)(e->real.code_len / 4); |
| 268 | uint32_t bytes = (uint32_t)e->real.code_len; |
| 269 | |
| 270 | if (insns > 0) { |
| 271 | out->predicted.present = true; |
| 272 | out->predicted.cycles = insns; |
| 273 | out->predicted.insn_count = insns; |
| 274 | out->predicted.code_bytes = bytes; |
| 275 | snprintf(out->predicted.source, sizeof(out->predicted.source), |
| 276 | "static: code_len/4 = %u instructions x 1 cycle; no machine model", insns); |
| 277 | } else { |
| 278 | snprintf(out->predicted.source, sizeof(out->predicted.source), "absent: empty code"); |
| 279 | } |
| 280 | |
| 281 | if (mg && insns > 0) { |
| 282 | out->estimated.present = true; |
| 283 | out->estimated.cycles = omega_machine_estimate_latency(mg, &e->real); |
| 284 | out->estimated.insn_count = insns; |
| 285 | out->estimated.code_bytes = bytes; |
| 286 | snprintf(out->estimated.source, sizeof(out->estimated.source), |
| 287 | "omega_machine_estimate_latency(%.63s) (assumed profile, not measured)", mg->name); |
| 288 | } else if (e->has_estimate) { |
| 289 | out->estimated.present = true; |
| 290 | out->estimated.cycles = e->estimated_cycles; |
| 291 | out->estimated.insn_count = insns; |
| 292 | out->estimated.code_bytes = bytes; |
| 293 | snprintf(out->estimated.source, sizeof(out->estimated.source), |
| 294 | "machine-model estimate recorded when realized (assumed profile, not measured)"); |
| 295 | } else { |
| 296 | snprintf(out->estimated.source, sizeof(out->estimated.source), "absent: no machine model"); |
| 297 | } |
| 298 | |
| 299 | snprintf(out->measured.source, sizeof(out->measured.source), |
| 300 | "absent: Visor V1 ingests no execution receipts"); |
| 301 | snprintf(out->qualified.source, sizeof(out->qualified.source), |
| 302 | "absent: no qualified gate result for this realization in V1"); |
| 303 | return 0; |
| 304 | } |
| 305 | |
| 306 | /* ---- view ---------------------------------------------------------------- */ |
| 307 | |
| 308 | int visor_realization_view(const VisorRealizationEntry *e, const OmegaMachineGraph *mg, VisorRealizationView *out) { |
| 309 | if (!e || !out) return -1; |
| 310 | memset(out, 0, sizeof(*out)); |
| 311 | if (e->real.has_id) visor_format_id(&e->real.realization_id, out->realization_id); |
| 312 | else snprintf(out->realization_id, sizeof(out->realization_id), "none"); |
| 313 | visor_format_id(&e->subject_id, out->subject_id); |
| 314 | if (id_is_zero(&e->real.semantic_id)) |
| 315 | snprintf(out->realized_id, sizeof(out->realized_id), "none"); |
| 316 | else visor_format_id(&e->real.semantic_id, out->realized_id); |
| 317 | if (e->has_machine_id) visor_format_id(&e->machine_id, out->machine_id); |
| 318 | else snprintf(out->machine_id, sizeof(out->machine_id), "none"); |
| 319 | if (e->real.has_machine_id) visor_format_id(&e->real.machine_id, out->bound_machine_id); |
| 320 | else snprintf(out->bound_machine_id, sizeof(out->bound_machine_id), "none"); |
| 321 | snprintf(out->machine_name, sizeof(out->machine_name), "%s", mg ? mg->name : "none"); |
| 322 | snprintf(out->target, sizeof(out->target), "%s", e->target_name); |
| 323 | out->profile = e->real.target_profile; |
| 324 | out->code_len = (uint32_t)e->real.code_len; |
| 325 | out->insn_count = (uint32_t)(e->real.code_len / 4); |
| 326 | out->disasm_count = visor_disasm(e->real.code_bytes, e->real.code_len, out->disasm, VISOR_DISASM_MAX); |
| 327 | |
| 328 | bool profile_ok = mg ? (e->real.target_profile == mg->target_profile) : false; |
| 329 | const char *bind = "realizer bound no machine"; |
| 330 | bool bind_ok = true; |
| 331 | if (e->real.has_machine_id) { |
| 332 | bind_ok = mg && id_eq(&e->real.machine_id, &mg->machine_id); |
| 333 | bind = bind_ok ? "bound machine == model" : "bound machine != model"; |
| 334 | } |
| 335 | bool not_bw = strcmp(e->target_name, VISOR_TARGET_BLACKWELL) != 0; |
| 336 | bool verdict_ok = !e->verdict_known || e->compatible; |
| 337 | out->compatible = profile_ok && bind_ok && not_bw && verdict_ok; |
| 338 | char runwhy[160]; |
| 339 | out->runnable = verdict_ok && run_check(e, runwhy, sizeof(runwhy)) == 0; |
| 340 | char vw[192] = ""; |
| 341 | if (!verdict_ok) |
| 342 | snprintf(vw, sizeof(vw), "; realizer verdict INCOMPATIBLE: %.127s", e->incompatible_reason); |
| 343 | if (mg) |
| 344 | snprintf(out->why, sizeof(out->why), "profile 0x%02x vs machine 0x%02x %s; %s; run %s%s", |
| 345 | e->real.target_profile, mg->target_profile, profile_ok ? "match" : "MISMATCH", |
| 346 | bind, out->runnable ? "allowed" : "refused", vw); |
| 347 | else |
| 348 | snprintf(out->why, sizeof(out->why), "profile 0x%02x; no machine model (not compatible); %s; run %s%s", |
| 349 | e->real.target_profile, bind, out->runnable ? "allowed" : "refused", vw); |
| 350 | return visor_realization_cost(e, mg, &out->cost); |
| 351 | } |
| 352 | |
| 353 | /* ---- realize ------------------------------------------------------------- */ |
| 354 | |
| 355 | static void entry_base(VisorRealizationEntry *e, const OmegaMachineGraph *mg) { |
| 356 | if (mg) { e->machine_id = mg->machine_id; e->has_machine_id = true; } |
| 357 | if (mg && e->real.code_len > 0) { |
| 358 | e->estimated_cycles = omega_machine_estimate_latency(mg, &e->real); |
| 359 | e->has_estimate = true; |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | static int fail_view(VisorRealizationView *v, const char *why) { |
| 364 | if (v) { |
| 365 | memset(v, 0, sizeof(*v)); |
| 366 | snprintf(v->why, sizeof(v->why), "%s", why); |
| 367 | snprintf(v->realization_id, sizeof(v->realization_id), "none"); |
| 368 | snprintf(v->subject_id, sizeof(v->subject_id), "none"); |
| 369 | snprintf(v->realized_id, sizeof(v->realized_id), "none"); |
| 370 | snprintf(v->machine_id, sizeof(v->machine_id), "none"); |
| 371 | snprintf(v->bound_machine_id, sizeof(v->bound_machine_id), "none"); |
| 372 | snprintf(v->machine_name, sizeof(v->machine_name), "none"); |
| 373 | snprintf(v->target, sizeof(v->target), "none"); |
| 374 | VisorCostItem *it[4] = { &v->cost.predicted, &v->cost.estimated, &v->cost.measured, &v->cost.qualified }; |
| 375 | for (int i = 0; i < 4; ++i) { |
| 376 | cost_slot(it[i], (VisorCostClass)i); |
| 377 | snprintf(it[i]->source, sizeof(it[i]->source), "absent: not realized"); |
| 378 | } |
| 379 | } |
| 380 | return -2; |
| 381 | } |
| 382 | |
| 383 | int visor_realize_apply(const OmegaGraph *g, const SemanticId *apply_id, const OmegaMachineGraph *mg, |
| 384 | VisorRealizationEntry *out_entry, VisorRealizationView *out_view) { |
| 385 | if (!g || !apply_id || !out_entry || !out_view) return -1; |
| 386 | memset(out_entry, 0, sizeof(*out_entry)); |
| 387 | char why[256]; |
| 388 | const OmegaObject *obj = omega_graph_find_object_const(g, apply_id); |
| 389 | if (!obj) return fail_view(out_view, "unknown id: not in graph"); |
| 390 | if (obj->kind != KIND_OPERATION) |
| 391 | return fail_view(out_view, "not an APPLY or OPERATION object; only pure binary u64 applies realize in V1"); |
| 392 | |
| 393 | SemanticId op_id; |
| 394 | if (obj->payload_len == sizeof(ApplyPayload)) { |
| 395 | ApplyPayload ap; |
| 396 | memcpy(&ap, obj->payload, sizeof(ap)); |
| 397 | if (ap.operand_count != 2) { |
| 398 | snprintf(why, sizeof(why), "apply has %u operands; pure binary realizer needs 2", ap.operand_count); |
| 399 | return fail_view(out_view, why); |
| 400 | } |
| 401 | op_id = ap.op_id; |
| 402 | } else if (obj->payload_len >= sizeof(OperationPayload)) { |
| 403 | op_id = *apply_id; /* an OPERATION itself */ |
| 404 | } else { |
| 405 | return fail_view(out_view, "OPERATION-kind object with unrecognised payload size"); |
| 406 | } |
| 407 | |
| 408 | const OmegaObject *op = omega_graph_find_object_const(g, &op_id); |
| 409 | if (!op || op->kind != KIND_OPERATION || op->payload_len < sizeof(OperationPayload)) |
| 410 | return fail_view(out_view, "apply references an operation that is not in the graph"); |
| 411 | OperationPayload opp; |
| 412 | memcpy(&opp, op->payload, sizeof(opp)); |
| 413 | switch (opp.opcode) { |
| 414 | case OP_ADD: case OP_SUB: case OP_MUL: case OP_AND: case OP_OR: break; |
| 415 | default: |
| 416 | snprintf(why, sizeof(why), "opcode %s not supported by omega_realize_pure_binary (ADD SUB MUL AND OR only)", |
| 417 | opcode_name(opp.opcode)); |
| 418 | return fail_view(out_view, why); |
| 419 | } |
| 420 | const OmegaObject *ty = omega_graph_find_object_const(g, &opp.type_id); |
| 421 | if (!ty || ty->kind != KIND_TYPE || ty->payload_len < sizeof(TypePayload)) |
| 422 | return fail_view(out_view, "operation type not found in graph"); |
| 423 | TypePayload tp; |
| 424 | memcpy(&tp, ty->payload, sizeof(tp)); |
| 425 | if (tp.tag != TYPE_UNSIGNED_INT || tp.width != 64) { |
| 426 | snprintf(why, sizeof(why), "type tag 0x%02x width %u unsupported: realizer emits 64-bit unsigned code only", |
| 427 | (unsigned)tp.tag, tp.width); |
| 428 | return fail_view(out_view, why); |
| 429 | } |
| 430 | if ((opp.opcode == OP_ADD || opp.opcode == OP_SUB || opp.opcode == OP_MUL) && |
| 431 | opp.overflow != OVERFLOW_WRAP && opp.overflow != OVERFLOW_DEFAULT) { |
| 432 | snprintf(why, sizeof(why), "overflow policy 0x%02x unsupported: realized code wraps mod 2^64", |
| 433 | (unsigned)opp.overflow); |
| 434 | return fail_view(out_view, why); |
| 435 | } |
| 436 | |
| 437 | if (omega_realize_pure_binary(g, &op_id, &out_entry->real) != 0) |
| 438 | return fail_view(out_view, "omega_realize_pure_binary failed"); |
| 439 | out_entry->subject_id = *apply_id; |
| 440 | out_entry->subject_is_program = false; |
| 441 | out_entry->program_index = -1; |
| 442 | snprintf(out_entry->target_name, sizeof(out_entry->target_name), "%s", VISOR_TARGET_AARCH64); |
| 443 | out_entry->verdict_known = true; |
| 444 | out_entry->compatible = true; |
| 445 | entry_base(out_entry, mg); |
| 446 | return visor_realization_view(out_entry, mg, out_view); |
| 447 | } |
| 448 | |
| 449 | int visor_realize_program(const OmegaProgram *p, const OmegaMachineGraph *mg, |
| 450 | VisorRealizationEntry *out_entry, VisorRealizationView *out_view) { |
| 451 | if (!p || !out_entry || !out_view) return -1; |
| 452 | memset(out_entry, 0, sizeof(*out_entry)); |
| 453 | /* Always compile from the canonical body (omega_program_realize); never trust code the |
| 454 | * program happens to carry. The V0 gate's reason is shown when it refuses. */ |
| 455 | OmegaProgram c = *p; |
| 456 | c.graph = NULL; |
| 457 | char why[192]; |
| 458 | if (omega_program_realize_ex(&c, why, sizeof why) != 0) { |
| 459 | char msg[240]; |
| 460 | snprintf(msg, sizeof msg, "omega_program_realize refused: %s", why[0] ? why : "unknown"); |
| 461 | return fail_view(out_view, msg); |
| 462 | } |
| 463 | out_entry->real = c.realization; |
| 464 | out_entry->subject_id = p->program_id; |
| 465 | out_entry->subject_is_program = true; |
| 466 | out_entry->program_index = -1; |
| 467 | snprintf(out_entry->target_name, sizeof(out_entry->target_name), "%s", VISOR_TARGET_AARCH64); |
| 468 | out_entry->verdict_known = true; |
| 469 | out_entry->compatible = true; |
| 470 | entry_base(out_entry, mg); |
| 471 | return visor_realization_view(out_entry, mg, out_view); |
| 472 | } |
| 473 | |
| 474 | /* ---- alternatives -------------------------------------------------------- */ |
| 475 | |
| 476 | static const uint64_t k_diff_inputs[] = { 0, 1, 2, 5, 10, 50, 100, 0xFFFFFFFFULL }; |
| 477 | |
| 478 | static void set_verdict(VisorRealizationEntry *e, bool compatible, const char *reason) { |
| 479 | e->verdict_known = true; |
| 480 | e->compatible = compatible; |
| 481 | snprintf(e->incompatible_reason, sizeof(e->incompatible_reason), "%s", compatible ? "" : reason); |
| 482 | } |
| 483 | |
| 484 | static void synth_alt(const OmegaProgram *p, const OmegaMachineGraph *m, const char *tname, |
| 485 | VisorRealizationEntry *e, VisorRealizationView *v) { |
| 486 | memset(e, 0, sizeof(*e)); |
| 487 | e->subject_id = p->program_id; |
| 488 | e->subject_is_program = true; |
| 489 | e->program_index = -1; |
| 490 | snprintf(e->target_name, sizeof(e->target_name), "%s", tname); |
| 491 | #if defined(__aarch64__) |
| 492 | /* Program-driven realization for machine m: program body -> schedule chosen from m -> bytes -> |
| 493 | * triple id -> verification (native == semantic evaluator). */ |
| 494 | RealizationSynthesisTask task; |
| 495 | RealizationSynthesisResult res; |
| 496 | omega_realize_task_init(&task, p, m); |
| 497 | task.optimize_latency = true; |
| 498 | int rc = omega_synthesize_realization(&task, &res); |
| 499 | if (res.realization.code_len == 0) { |
| 500 | char r[sizeof(e->incompatible_reason)]; |
| 501 | snprintf(r, sizeof(r), "realization refused (rc=%d): %.96s", rc, res.why); |
| 502 | set_verdict(e, false, r); |
| 503 | visor_realization_view(e, m, v); |
| 504 | v->compatible = false; |
| 505 | v->runnable = false; |
| 506 | snprintf(v->why, sizeof(v->why), "omega_synthesize_realization refused (rc=%d): %.160s", rc, res.why); |
| 507 | return; |
| 508 | } |
| 509 | e->real = res.realization; |
| 510 | e->machine_id = m->machine_id; |
| 511 | e->has_machine_id = true; |
| 512 | e->estimated_cycles = res.estimated_cycles; |
| 513 | e->has_estimate = true; |
| 514 | /* cross-check vs the canonical (direct) realization of the same program */ |
| 515 | char diff[160]; |
| 516 | OmegaProgram c = *p; |
| 517 | c.graph = NULL; |
| 518 | bool match = omega_program_realize(&c) == 0; |
| 519 | snprintf(diff, sizeof(diff), "program has no direct realization to compare"); |
| 520 | if (match) { |
| 521 | snprintf(diff, sizeof(diff), "matches direct on %zu inputs", |
| 522 | sizeof(k_diff_inputs) / sizeof(k_diff_inputs[0])); |
| 523 | for (size_t i = 0; i < sizeof(k_diff_inputs) / sizeof(k_diff_inputs[0]); ++i) { |
| 524 | uint64_t yd = 0, ys = 0, x = k_diff_inputs[i]; |
| 525 | if (omega_exec_native_f3(&c.realization, x, 0, 0, &yd) != 0 || |
| 526 | omega_exec_native_f3(&e->real, x, 0, 0, &ys) != 0 || yd != ys) { |
| 527 | match = false; |
| 528 | snprintf(diff, sizeof(diff), "MISMATCH x=%" PRIu64 ": direct=%" PRIu64 " synth=%" PRIu64, x, yd, ys); |
| 529 | break; |
| 530 | } |
| 531 | } |
| 532 | } |
| 533 | if (rc != 0 || !res.solved) match = false; |
| 534 | if (match) { |
| 535 | set_verdict(e, true, ""); |
| 536 | } else { |
| 537 | char r[sizeof(e->incompatible_reason)]; |
| 538 | snprintf(r, sizeof(r), "synth rc=%d solved=%d; %.40s; %.60s", rc, res.solved ? 1 : 0, diff, res.why); |
| 539 | set_verdict(e, false, r); |
| 540 | } |
| 541 | visor_realization_view(e, m, v); |
| 542 | v->compatible = v->compatible && match; |
| 543 | v->runnable = v->runnable && match; |
| 544 | snprintf(v->why, sizeof(v->why), |
| 545 | "compiled from the program body, %s schedule for this machine; native == semantic evaluator on %u inputs; %.60s; synth rc=%d", |
| 546 | res.schedule == OMEGA_SCHED_PRELOAD ? "preload" : "sequential", res.inputs_checked, diff, rc); |
| 547 | #else |
| 548 | set_verdict(e, false, "synthesis not attempted: host is not aarch64"); |
| 549 | visor_realization_view(e, m, v); |
| 550 | v->compatible = false; |
| 551 | v->runnable = false; |
| 552 | snprintf(v->why, sizeof(v->why), "synthesis not attempted: its V1 check executes AArch64 code; host is not aarch64"); |
| 553 | #endif |
| 554 | } |
| 555 | |
| 556 | int visor_realization_alternatives_ex(const OmegaProgram *p, const OmegaMachineGraph *mg, |
| 557 | VisorRealizationEntry *entries, VisorRealizationView *out, |
| 558 | size_t max, size_t *count) { |
| 559 | if (!p || !mg || !out || !count) return -1; |
| 560 | *count = 0; |
| 561 | static OmegaMachineGraph other; /* fixed storage; single-threaded Visor */ |
| 562 | OmegaMachineGraph spark; |
| 563 | if (omega_machine_build_dgx_spark(&spark) != 0) return -1; |
| 564 | if (id_eq(&spark.machine_id, &mg->machine_id)) { |
| 565 | if (omega_machine_build_qemu_virt(&other) != 0) return -1; |
| 566 | } else { |
| 567 | other = spark; |
| 568 | } |
| 569 | const OmegaMachineGraph *ms[2] = { mg, &other }; |
| 570 | char tn[2][32]; |
| 571 | machine_target_name(mg, tn[0]); |
| 572 | machine_target_name(&other, tn[1]); |
| 573 | |
| 574 | VisorRealizationEntry tmp; |
| 575 | for (size_t k = 0; k < 4 && *count < max; ++k) { |
| 576 | VisorRealizationEntry *e = entries ? &entries[*count] : &tmp; |
| 577 | VisorRealizationView *v = &out[*count]; |
| 578 | const OmegaMachineGraph *m = ms[k % 2]; |
| 579 | const char *t = tn[k % 2]; |
| 580 | if (k < 2) { |
| 581 | if (visor_realize_program(p, m, e, v) != 0) { |
| 582 | set_verdict(e, false, "direct realization failed"); |
| 583 | v->compatible = false; |
| 584 | v->runnable = false; |
| 585 | } |
| 586 | snprintf(e->target_name, sizeof(e->target_name), "%s", VISOR_TARGET_AARCH64); |
| 587 | snprintf(v->label, sizeof(v->label), "direct@%s", t); |
| 588 | } else { |
| 589 | synth_alt(p, m, t, e, v); |
| 590 | snprintf(v->label, sizeof(v->label), "synth@%s", t); |
| 591 | } |
| 592 | (*count)++; |
| 593 | } |
| 594 | return 0; |
| 595 | } |
| 596 | |
| 597 | int visor_realization_alternatives(const OmegaProgram *p, const OmegaMachineGraph *mg, |
| 598 | VisorRealizationView *out, size_t max, size_t *count) { |
| 599 | return visor_realization_alternatives_ex(p, mg, NULL, out, max, count); |
| 600 | } |
| 601 | |
| 602 | /* ---- text ---------------------------------------------------------------- */ |
| 603 | |
| 604 | static void cost_item_text(Out *o, const VisorCostItem *it) { |
| 605 | if (it->present) |
| 606 | outf(o, " %-9s cycles=%" PRIu64 " insns=%u bytes=%u [%s]\n", it->cls_name, it->cycles, |
| 607 | it->insn_count, it->code_bytes, it->source); |
| 608 | else |
| 609 | outf(o, " %-9s ABSENT [%s]\n", it->cls_name, it->source); |
| 610 | } |
| 611 | |
| 612 | int visor_cost_format_text(const VisorCostView *c, char *out, size_t n) { |
| 613 | if (!c || !out || n == 0) return -1; |
| 614 | Out o = { out, n, 0, false }; |
| 615 | out[0] = '\0'; |
| 616 | outf(&o, "cost (four separate classes; never merged)\n"); |
| 617 | cost_item_text(&o, &c->predicted); |
| 618 | cost_item_text(&o, &c->estimated); |
| 619 | cost_item_text(&o, &c->measured); |
| 620 | cost_item_text(&o, &c->qualified); |
| 621 | return o.trunc ? -1 : (int)o.pos; |
| 622 | } |
| 623 | |
| 624 | static void cost_item_json(Out *o, const char *key, const VisorCostItem *it) { |
| 625 | outf(o, "\"%s\":{\"present\":%s", key, it->present ? "true" : "false"); |
| 626 | if (it->present) |
| 627 | outf(o, ",\"cycles\":%" PRIu64 ",\"insn_count\":%u,\"code_bytes\":%u", it->cycles, it->insn_count, |
| 628 | it->code_bytes); |
| 629 | outf(o, ",\"source\":"); |
| 630 | json_str(o, it->source); |
| 631 | outf(o, "}"); |
| 632 | } |
| 633 | |
| 634 | int visor_cost_format_json(const VisorCostView *c, char *out, size_t n) { |
| 635 | if (!c || !out || n == 0) return -1; |
| 636 | Out o = { out, n, 0, false }; |
| 637 | out[0] = '\0'; |
| 638 | outf(&o, "{"); |
| 639 | cost_item_json(&o, "predicted", &c->predicted); outf(&o, ","); |
| 640 | cost_item_json(&o, "estimated", &c->estimated); outf(&o, ","); |
| 641 | cost_item_json(&o, "measured", &c->measured); outf(&o, ","); |
| 642 | cost_item_json(&o, "qualified", &c->qualified); |
| 643 | outf(&o, "}"); |
| 644 | return o.trunc ? -1 : (int)o.pos; |
| 645 | } |
| 646 | |
| 647 | int visor_realization_why(const VisorRealizationView *v, char *out, size_t n) { |
| 648 | if (!v || !out || n == 0) return -1; |
| 649 | Out o = { out, n, 0, false }; |
| 650 | out[0] = '\0'; |
| 651 | outf(&o, "why %s%s\n", v->label[0] ? v->label : "realization", v->label[0] ? "" : ""); |
| 652 | outf(&o, " compatibility: %s\n", v->compatible ? "compatible" : "NOT compatible"); |
| 653 | outf(&o, " profile: realization 0x%02x, machine %s; V1 realizers and both canonical profiles use 0x01," |
| 654 | " so a match is expected, not a hardware check\n", v->profile, v->machine_name); |
| 655 | outf(&o, " machine binding: realizer bound %s; model %s\n", v->bound_machine_id, v->machine_id); |
| 656 | outf(&o, " target: %s; run %s\n", v->target, v->runnable ? "allowed (pure AArch64, native)" : "refused"); |
| 657 | outf(&o, " constraints: %s\n", v->why); |
| 658 | outf(&o, " cost sources:\n"); |
| 659 | const VisorCostItem *it[4] = { &v->cost.predicted, &v->cost.estimated, &v->cost.measured, &v->cost.qualified }; |
| 660 | for (size_t i = 0; i < 4; ++i) |
| 661 | outf(&o, " %-9s %s: %s\n", it[i]->cls_name, it[i]->present ? "present" : "ABSENT", it[i]->source); |
| 662 | return o.trunc ? -1 : (int)o.pos; |
| 663 | } |
| 664 | |
| 665 | int visor_realization_format_text(const VisorRealizationView *v, char *out, size_t n) { |
| 666 | if (!v || !out || n == 0) return -1; |
| 667 | Out o = { out, n, 0, false }; |
| 668 | out[0] = '\0'; |
| 669 | if (v->label[0]) outf(&o, "realization %s\n", v->label); |
| 670 | else outf(&o, "realization\n"); |
| 671 | outf(&o, " id %s\n", v->realization_id); |
| 672 | outf(&o, " subject %s\n", v->subject_id); |
| 673 | outf(&o, " realized %s\n", v->realized_id); |
| 674 | outf(&o, " machine %s (%s)\n", v->machine_id, v->machine_name); |
| 675 | outf(&o, " bound %s\n", v->bound_machine_id); |
| 676 | outf(&o, " target %s profile=0x%02x\n", v->target, v->profile); |
| 677 | outf(&o, " code %u bytes, %u instructions\n", v->code_len, v->insn_count); |
| 678 | for (size_t i = 0; i < v->disasm_count; ++i) outf(&o, " %s\n", v->disasm[i]); |
| 679 | outf(&o, " compatible %s\n", v->compatible ? "yes" : "no"); |
| 680 | outf(&o, " runnable %s\n", v->runnable ? "yes" : "no"); |
| 681 | outf(&o, " why %s\n", v->why); |
| 682 | if (!o.trunc) { |
| 683 | int w = visor_cost_format_text(&v->cost, out + o.pos, n - o.pos); |
| 684 | if (w < 0) o.trunc = true; else o.pos += (size_t)w; |
| 685 | } |
| 686 | return o.trunc ? -1 : (int)o.pos; |
| 687 | } |
| 688 | |
| 689 | int visor_realization_format_json(const VisorRealizationView *v, char *out, size_t n) { |
| 690 | if (!v || !out || n == 0) return -1; |
| 691 | Out o = { out, n, 0, false }; |
| 692 | out[0] = '\0'; |
| 693 | outf(&o, "{\"label\":"); json_str(&o, v->label); |
| 694 | outf(&o, ",\"realization_id\":"); json_str(&o, v->realization_id); |
| 695 | outf(&o, ",\"subject_id\":"); json_str(&o, v->subject_id); |
| 696 | outf(&o, ",\"realized_id\":"); json_str(&o, v->realized_id); |
| 697 | outf(&o, ",\"machine_id\":"); json_str(&o, v->machine_id); |
| 698 | outf(&o, ",\"machine_name\":"); json_str(&o, v->machine_name); |
| 699 | outf(&o, ",\"bound_machine_id\":"); json_str(&o, v->bound_machine_id); |
| 700 | outf(&o, ",\"target\":"); json_str(&o, v->target); |
| 701 | outf(&o, ",\"profile\":%u,\"code_len\":%u,\"insn_count\":%u,\"disasm\":[", v->profile, v->code_len, v->insn_count); |
| 702 | for (size_t i = 0; i < v->disasm_count; ++i) { if (i) outf(&o, ","); json_str(&o, v->disasm[i]); } |
| 703 | outf(&o, "],\"compatible\":%s,\"runnable\":%s,\"why\":", v->compatible ? "true" : "false", |
| 704 | v->runnable ? "true" : "false"); |
| 705 | json_str(&o, v->why); |
| 706 | outf(&o, ",\"cost\":"); |
| 707 | if (!o.trunc) { |
| 708 | int w = visor_cost_format_json(&v->cost, out + o.pos, n - o.pos); |
| 709 | if (w < 0) o.trunc = true; else o.pos += (size_t)w; |
| 710 | } |
| 711 | outf(&o, "}"); |
| 712 | return o.trunc ? -1 : (int)o.pos; |
| 713 | } |
| 714 | |
| 715 | static void cmp_cost(Out *o, const char *name, const VisorCostItem *a, const VisorCostItem *b) { |
| 716 | if (a->present && b->present) |
| 717 | outf(o, " %-9s A=%" PRIu64 " B=%" PRIu64 " (%s) [A: %s] [B: %s]\n", name, a->cycles, b->cycles, |
| 718 | a->cycles == b->cycles ? "equal" : (a->cycles < b->cycles ? "A lower" : "B lower"), a->source, b->source); |
| 719 | else |
| 720 | outf(o, " %-9s A=%s B=%s (not compared)\n", name, a->present ? "present" : "ABSENT", |
| 721 | b->present ? "present" : "ABSENT"); |
| 722 | } |
| 723 | |
| 724 | int visor_realization_compare(const VisorRealizationView *a, const VisorRealizationView *b, char *out, size_t n) { |
| 725 | if (!a || !b || !out || n == 0) return -1; |
| 726 | Out o = { out, n, 0, false }; |
| 727 | out[0] = '\0'; |
| 728 | outf(&o, "compare A=%s B=%s\n", a->label[0] ? a->label : a->realization_id, |
| 729 | b->label[0] ? b->label : b->realization_id); |
| 730 | outf(&o, " realization_id %s\n", strcmp(a->realization_id, b->realization_id) == 0 ? "same" : "different"); |
| 731 | bool same_code = a->code_len == b->code_len && a->disasm_count == b->disasm_count; |
| 732 | for (size_t i = 0; same_code && i < a->disasm_count; ++i) |
| 733 | if (strcmp(a->disasm[i], b->disasm[i]) != 0) same_code = false; |
| 734 | outf(&o, " code %s (A %u insns, B %u insns)\n", same_code ? "identical" : "different", |
| 735 | a->insn_count, b->insn_count); |
| 736 | outf(&o, " profile A=0x%02x (%s on %s) B=0x%02x (%s on %s)\n", a->profile, a->target, a->machine_name, |
| 737 | b->profile, b->target, b->machine_name); |
| 738 | outf(&o, " compatible A=%s B=%s; runnable A=%s B=%s\n", a->compatible ? "yes" : "no", |
| 739 | b->compatible ? "yes" : "no", a->runnable ? "yes" : "no", b->runnable ? "yes" : "no"); |
| 740 | outf(&o, " cost (per class; classes never mixed)\n"); |
| 741 | cmp_cost(&o, "predicted", &a->cost.predicted, &b->cost.predicted); |
| 742 | cmp_cost(&o, "estimated", &a->cost.estimated, &b->cost.estimated); |
| 743 | cmp_cost(&o, "measured", &a->cost.measured, &b->cost.measured); |
| 744 | cmp_cost(&o, "qualified", &a->cost.qualified, &b->cost.qualified); |
| 745 | return o.trunc ? -1 : (int)o.pos; |
| 746 | } |