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 / tests / visor / test_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 | /* Lane 5: realization lab tests. Prints "PASS n/n". */ |
| 18 | #include "visor_realization.h" |
| 19 | #include "visor_machine.h" |
| 20 | #include "visor.h" |
| 21 | #include "omega_core.h" |
| 22 | #include "aarch64_encoder.h" |
| 23 | |
| 24 | #include <stdint.h> |
| 25 | #include <stdio.h> |
| 26 | #include <string.h> |
| 27 | |
| 28 | static int g_pass, g_total; |
| 29 | #define CHECK(cond, msg) do { g_total++; if (cond) g_pass++; else fprintf(stderr, "FAIL: %s (%s:%d)\n", msg, __FILE__, __LINE__); } while (0) |
| 30 | |
| 31 | static char b1[16384], b2[16384]; |
| 32 | static VisorRealizationView alt1[4], alt2[4], altv[4]; |
| 33 | static VisorRealizationEntry alte[4]; |
| 34 | |
| 35 | static SemanticId make_apply(OmegaGraph *g, OpCode op, OverflowPolicy ov, uint16_t width, SemanticId *op_out) { |
| 36 | OmegaObject *t = omega_build_type_uint(g, width); |
| 37 | OmegaObject *a = omega_build_val_uint(g, &t->id, width, 7); |
| 38 | OmegaObject *b = omega_build_val_uint(g, &t->id, width, 11); |
| 39 | OmegaObject *o = omega_build_op_binary(g, op, ov, &t->id); |
| 40 | OmegaObject *ap = omega_build_apply(g, &o->id, &a->id, &b->id); |
| 41 | if (op_out) *op_out = o->id; |
| 42 | return ap->id; |
| 43 | } |
| 44 | |
| 45 | int main(void) { |
| 46 | OmegaMachineGraph spark, qemu; |
| 47 | omega_machine_build_dgx_spark(&spark); |
| 48 | omega_machine_build_qemu_virt(&qemu); |
| 49 | OmegaGraph *g = omega_graph_create(); |
| 50 | CHECK(g != NULL, "graph"); |
| 51 | |
| 52 | /* 7 + 11 apply */ |
| 53 | SemanticId add_op; |
| 54 | SemanticId add_ap = make_apply(g, OP_ADD, OVERFLOW_WRAP, 64, &add_op); |
| 55 | VisorRealizationEntry e; |
| 56 | VisorRealizationView v; |
| 57 | CHECK(visor_realize_apply(g, &add_ap, &spark, &e, &v) == 0, "realize 7+11 apply"); |
| 58 | CHECK(e.verdict_known && e.compatible, "direct apply entry verdict compatible"); |
| 59 | CHECK(memcmp(&e.subject_id, &add_ap, sizeof(SemanticId)) == 0, "subject = apply id"); |
| 60 | CHECK(memcmp(&e.real.semantic_id, &add_op, sizeof(SemanticId)) == 0, "realized = op id"); |
| 61 | CHECK(strcmp(v.subject_id, v.realized_id) != 0, "subject and realized both shown, distinct"); |
| 62 | CHECK(e.has_machine_id && e.has_estimate && strcmp(e.target_name, VISOR_TARGET_AARCH64) == 0, "entry machine/target"); |
| 63 | CHECK(v.disasm_count == 2 && strstr(v.disasm[0], "add x0, x0, x1") && strstr(v.disasm[1], "ret"), "disasm"); |
| 64 | uint64_t args[3] = { 7, 11, 0 }, r = 0; |
| 65 | CHECK(visor_realization_run_pure(&e, args, 2, &r) == 0 && r == 18, "run_pure(7,11)==18"); |
| 66 | args[0] = UINT64_MAX; args[1] = 1; |
| 67 | CHECK(visor_realization_run_pure(&e, args, 2, &r) == 0 && r == 0, "(2^64-1)+1 wraps to 0"); |
| 68 | CHECK(visor_realization_run_pure(&e, args, 4, &r) == -3, "argc>3 refused"); |
| 69 | CHECK(v.compatible && v.runnable, "compatible + runnable"); |
| 70 | |
| 71 | /* fail closed */ |
| 72 | SemanticId div_ap = make_apply(g, OP_DIV, OVERFLOW_WRAP, 64, NULL); |
| 73 | VisorRealizationEntry ebad; |
| 74 | VisorRealizationView vbad; |
| 75 | CHECK(visor_realize_apply(g, &div_ap, &spark, &ebad, &vbad) == -2 && strstr(vbad.why, "DIV"), "DIV unsupported"); |
| 76 | CHECK(visor_realization_format_text(&vbad, b1, sizeof(b1)) > 0 && visor_realization_why(&vbad, b2, sizeof(b2)) > 0 && |
| 77 | strstr(b1, "not realized") && !vbad.cost.measured.present, "refused view formats safely"); |
| 78 | SemanticId sat_ap = make_apply(g, OP_ADD, OVERFLOW_SATURATE, 64, NULL); |
| 79 | CHECK(visor_realize_apply(g, &sat_ap, &spark, &ebad, &vbad) == -2 && strstr(vbad.why, "overflow"), "SATURATE refused"); |
| 80 | SemanticId u32_ap = make_apply(g, OP_ADD, OVERFLOW_WRAP, 32, NULL); |
| 81 | CHECK(visor_realize_apply(g, &u32_ap, &spark, &ebad, &vbad) == -2 && strstr(vbad.why, "width 32"), "u32 refused"); |
| 82 | SemanticId junk; |
| 83 | memset(&junk, 0x5A, sizeof(junk)); |
| 84 | CHECK(visor_realize_apply(g, &junk, &spark, &ebad, &vbad) == -2 && vbad.why[0], "unknown id refused"); |
| 85 | OmegaObject *t64 = omega_build_type_uint(g, 64); |
| 86 | CHECK(visor_realize_apply(g, &t64->id, &spark, &ebad, &vbad) == -2, "TYPE object refused"); |
| 87 | |
| 88 | /* cost: four separate slots */ |
| 89 | VisorCostView c; |
| 90 | CHECK(visor_realization_cost(&e, &spark, &c) == 0, "cost"); |
| 91 | CHECK(c.predicted.present && c.predicted.cls == VISOR_COST_PREDICTED && c.predicted.insn_count == 2, "predicted present"); |
| 92 | CHECK(c.estimated.present && c.estimated.cls == VISOR_COST_ESTIMATED && strstr(c.estimated.source, "omega_machine_estimate_latency"), |
| 93 | "estimated present from machine model"); |
| 94 | CHECK(c.measured.present == false && c.measured.cls == VISOR_COST_MEASURED, "measured ABSENT"); |
| 95 | CHECK(c.qualified.present == false && c.qualified.cls == VISOR_COST_QUALIFIED, "qualified ABSENT"); |
| 96 | CHECK(strcmp(c.predicted.source, c.estimated.source) != 0 && strcmp(c.predicted.cls_name, "predicted") == 0 && |
| 97 | strcmp(c.estimated.cls_name, "estimated") == 0, "classes never conflated"); |
| 98 | CHECK(c.measured.cycles == 0 && c.qualified.cycles == 0, "absent slots carry no numbers"); |
| 99 | CHECK(visor_cost_format_json(&c, b1, sizeof(b1)) > 0 && strstr(b1, "\"measured\":{\"present\":false"), "cost json"); |
| 100 | CHECK(visor_cost_format_text(&c, b1, sizeof(b1)) > 0 && strstr(b1, "measured ABSENT") && strstr(b1, "qualified ABSENT"), |
| 101 | "cost text"); |
| 102 | |
| 103 | /* program realize + run */ |
| 104 | OmegaProgram p; |
| 105 | CHECK(omega_program_build_unary_op(&p, "add5", OP_ADD, 5) == 0, "build add5"); |
| 106 | VisorRealizationEntry ep; |
| 107 | VisorRealizationView vp; |
| 108 | CHECK(visor_realize_program(&p, &spark, &ep, &vp) == 0 && ep.subject_is_program, "realize program"); |
| 109 | CHECK(ep.verdict_known && ep.compatible && ep.incompatible_reason[0] == '\0', "direct program entry verdict compatible"); |
| 110 | args[0] = 10; |
| 111 | CHECK(visor_realization_run_pure(&ep, args, 1, &r) == 0 && r == 15, "program run 10+5==15"); |
| 112 | OmegaProgram unreal; |
| 113 | omega_program_init(&unreal, "empty"); |
| 114 | CHECK(visor_realize_program(&unreal, &spark, &ep, &vp) == -2 && vp.why[0], "unrealized program refused"); |
| 115 | |
| 116 | /* alternatives: deterministic, labelled, synth checked against direct */ |
| 117 | size_t n1 = 0, n2 = 0; |
| 118 | CHECK(visor_realization_alternatives(&p, &spark, alt1, 4, &n1) == 0 && n1 == 4, "alternatives count"); |
| 119 | CHECK(visor_realization_alternatives(&p, &spark, alt2, 4, &n2) == 0 && n2 == 4, "alternatives again"); |
| 120 | bool same = true; |
| 121 | for (size_t i = 0; i < 4; ++i) { |
| 122 | visor_realization_format_text(&alt1[i], b1, sizeof(b1)); |
| 123 | visor_realization_format_text(&alt2[i], b2, sizeof(b2)); |
| 124 | if (strcmp(b1, b2) != 0) same = false; |
| 125 | } |
| 126 | CHECK(same, "alternatives deterministic"); |
| 127 | CHECK(strcmp(alt1[0].label, "direct@dgx-spark") == 0 && strcmp(alt1[1].label, "direct@qemu-virt") == 0 && |
| 128 | strcmp(alt1[2].label, "synth@dgx-spark") == 0 && strcmp(alt1[3].label, "synth@qemu-virt") == 0, "labels/order"); |
| 129 | CHECK(alt1[0].compatible && alt1[1].compatible, "direct alternatives compatible"); |
| 130 | /* synth = program-driven realization (program body -> machine schedule -> verified bytes) */ |
| 131 | CHECK(alt1[2].compatible && alt1[2].runnable && strstr(alt1[2].why, "matches direct") && |
| 132 | strstr(alt1[2].why, "compiled from the program body"), "synth for add5 compiled from the program and agrees"); |
| 133 | CHECK(alt1[3].compatible && alt1[3].runnable, "synth for add5 on qemu-virt compatible"); |
| 134 | size_t ne = 0; |
| 135 | CHECK(visor_realization_alternatives_ex(&p, &spark, alte, altv, 4, &ne) == 0 && ne == 4, "alternatives_ex count"); |
| 136 | CHECK(alte[0].verdict_known && alte[0].compatible && alte[1].verdict_known && alte[1].compatible, |
| 137 | "direct alternative entries compatible"); |
| 138 | CHECK(alte[2].verdict_known && alte[2].compatible && alte[2].has_machine_id, "synth entry compatible, machine-bound"); |
| 139 | args[0] = 10; |
| 140 | CHECK(visor_realization_run_pure(&alte[2], args, 1, &r) == 0 && r == 15, "synth entry runs 10+5==15"); |
| 141 | /* the stored entry carries a verdict: an entry marked incompatible stays so, run refused */ |
| 142 | VisorRealizationEntry bad = alte[2]; |
| 143 | bad.verdict_known = true; bad.compatible = false; |
| 144 | snprintf(bad.incompatible_reason, sizeof(bad.incompatible_reason), "marked incompatible by test"); |
| 145 | VisorRealizationView rv; |
| 146 | CHECK(visor_realization_view(&bad, &spark, &rv) == 0 && !rv.compatible && !rv.runnable && |
| 147 | strstr(rv.why, "INCOMPATIBLE") && strstr(rv.why, bad.incompatible_reason), |
| 148 | "re-viewed incompatible entry: not compatible, not runnable, reason in why"); |
| 149 | CHECK(visor_realization_run_pure(&bad, args, 1, &r) == -3, "incompatible entry run refused"); |
| 150 | CHECK(visor_realization_view(&alte[0], &spark, &rv) == 0 && rv.compatible && !strstr(rv.why, "INCOMPATIBLE"), |
| 151 | "re-viewed direct entry unchanged"); |
| 152 | CHECK(visor_realization_run_pure(&alte[0], args, 1, &r) == 0 && r == 15, "direct alternative entry runs 10+5==15"); |
| 153 | VisorRealizationEntry unk = alte[0]; |
| 154 | unk.verdict_known = false; unk.compatible = false; |
| 155 | CHECK(visor_realization_run_pure(&unk, args, 1, &r) == 0 && r == 15, "verdict not evaluated treated as compatible"); |
| 156 | /* 3x-2 program: synth agrees */ |
| 157 | OmegaProgram m3, s2, aff; |
| 158 | omega_program_build_unary_op(&m3, "mul3", OP_MUL, 3); |
| 159 | omega_program_build_unary_op(&s2, "sub2", OP_SUB, 2); |
| 160 | char err[128]; |
| 161 | CHECK(omega_program_compose(&m3, &s2, &aff, err, sizeof(err)) == 0, "compose 3x-2"); |
| 162 | CHECK(visor_realization_alternatives(&aff, &spark, alt2, 4, &n2) == 0 && n2 == 4, "alternatives 3x-2"); |
| 163 | CHECK(alt2[2].compatible && strstr(alt2[2].why, "matches direct"), "synth for 3x-2 matches direct"); |
| 164 | CHECK(alt2[2].insn_count == 5 && alt2[2].cost.estimated.present && !alt2[2].cost.measured.present, "synth cost view"); |
| 165 | size_t n3 = 0; |
| 166 | CHECK(visor_realization_alternatives(&aff, &spark, alt2, 2, &n3) == 0 && n3 == 2, "max respected"); |
| 167 | |
| 168 | /* compare / why */ |
| 169 | CHECK(visor_realization_compare(&alt1[0], &alt1[1], b1, sizeof(b1)) > 0 && strstr(b1, "profile") && |
| 170 | strstr(b1, "omega_machine_estimate_latency") && strstr(b1, "code identical"), "compare"); |
| 171 | CHECK(strstr(b1, "measured A=ABSENT B=ABSENT"), "compare never compares absent measured"); |
| 172 | CHECK(visor_realization_why(&v, b1, sizeof(b1)) > 0 && strstr(b1, "profile") && strstr(b1, "cost sources") && |
| 173 | strstr(b1, "ABSENT"), "why"); |
| 174 | |
| 175 | /* format json/text */ |
| 176 | CHECK(visor_realization_format_json(&v, b1, sizeof(b1)) > 0 && b1[0] == '{' && strstr(b1, "\"disasm\":["), "json"); |
| 177 | CHECK(visor_realization_format_text(&v, b1, 64) == -1, "text truncation fails closed"); |
| 178 | |
| 179 | /* Blackwell entries: run refused */ |
| 180 | VisorRealizationEntry bw = e; |
| 181 | snprintf(bw.target_name, sizeof(bw.target_name), "%s", VISOR_TARGET_BLACKWELL); |
| 182 | CHECK(visor_realization_run_pure(&bw, args, 2, &r) == -3, "blackwell target run refused"); |
| 183 | VisorRealizationView vbw; |
| 184 | CHECK(visor_realization_view(&bw, &spark, &vbw) == 0 && !vbw.runnable && !vbw.compatible, "blackwell view not runnable"); |
| 185 | VisorRealizationEntry bw2 = e; |
| 186 | CHECK(visor_machine_blackwell_id(&bw2.machine_id) == 0, "blackwell id"); |
| 187 | bw2.has_machine_id = true; |
| 188 | CHECK(visor_realization_run_pure(&bw2, args, 2, &r) == -3, "blackwell machine id run refused"); |
| 189 | |
| 190 | /* non-pure code refused */ |
| 191 | VisorRealizationEntry br = e; |
| 192 | size_t pos = 0; |
| 193 | aarch64_emit_b(br.real.code_bytes, &pos, sizeof(br.real.code_bytes), 0); |
| 194 | aarch64_emit_ret(br.real.code_bytes, &pos, sizeof(br.real.code_bytes)); |
| 195 | br.real.code_len = pos; |
| 196 | CHECK(visor_realization_run_pure(&br, args, 2, &r) == -3, "branch code refused"); |
| 197 | VisorRealizationEntry bp = e; |
| 198 | bp.real.target_profile = 0x02; |
| 199 | CHECK(visor_realization_run_pure(&bp, args, 2, &r) == -3, "non-v8a profile refused"); |
| 200 | VisorRealizationEntry nr = e; |
| 201 | nr.real.code_len = 4; /* ADD without RET */ |
| 202 | CHECK(visor_realization_run_pure(&nr, args, 2, &r) == -3, "missing RET refused"); |
| 203 | |
| 204 | visor_realization_format_text(&v, b1, sizeof(b1)); |
| 205 | fputs(b1, stdout); |
| 206 | omega_program_destroy(&p); |
| 207 | omega_graph_destroy(g); |
| 208 | printf("%s %d/%d\n", g_pass == g_total ? "PASS" : "FAIL", g_pass, g_total); |
| 209 | return g_pass == g_total ? 0 : 1; |
| 210 | } |