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

316 lines · 12132 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_machine.h"
18#include "omega_core.h"
19#include "sha256.h"
20#include "aarch64_decoder.h"
21#include <string.h>
22#include <stdio.h>
23
24void omega_machine_init(OmegaMachineGraph *mg, const char *name, uint8_t target_profile) {
25 if (!mg) return;
26 memset(mg, 0, sizeof(*mg));
27 if (name) {
28 snprintf(mg->name, sizeof(mg->name), "%s", name);
29 }
30 mg->target_profile = target_profile;
31}
32
33int omega_machine_compute_id(OmegaMachineGraph *mg) {
34 if (!mg) return -1;
35
36 uint8_t buffer[4096];
37 size_t pos = 0;
38
39 /* Canonical OMG0 wire header */
40 buffer[pos++] = 'O';
41 buffer[pos++] = 'M';
42 buffer[pos++] = 'G';
43 buffer[pos++] = '0';
44 buffer[pos++] = KIND_MACHINE; /* 0x07 */
45 buffer[pos++] = mg->target_profile;
46
47 /* Machine Name */
48 size_t name_len = strlen(mg->name);
49 buffer[pos++] = (uint8_t)name_len;
50 memcpy(&buffer[pos], mg->name, name_len);
51 pos += name_len;
52
53 /* Pipeline serialization */
54 uint32_t iw = mg->pipeline.issue_width;
55 buffer[pos++] = (uint8_t)((iw >> 24) & 0xFF);
56 buffer[pos++] = (uint8_t)((iw >> 16) & 0xFF);
57 buffer[pos++] = (uint8_t)((iw >> 8) & 0xFF);
58 buffer[pos++] = (uint8_t)(iw & 0xFF);
59
60 uint32_t mif = mg->pipeline.max_in_flight;
61 buffer[pos++] = (uint8_t)((mif >> 24) & 0xFF);
62 buffer[pos++] = (uint8_t)((mif >> 16) & 0xFF);
63 buffer[pos++] = (uint8_t)((mif >> 8) & 0xFF);
64 buffer[pos++] = (uint8_t)(mif & 0xFF);
65
66 buffer[pos++] = mg->pipeline.out_of_order ? 1 : 0;
67 buffer[pos++] = (uint8_t)mg->pipeline.unit_count;
68
69 for (size_t u = 0; u < mg->pipeline.unit_count && u < OMEGA_MACHINE_MAX_UNITS; ++u) {
70 const MachineComputeUnit *mcu = &mg->pipeline.units[u];
71 buffer[pos++] = (uint8_t)mcu->type;
72 buffer[pos++] = (uint8_t)mcu->count;
73 buffer[pos++] = (uint8_t)mcu->latency_cycles;
74 buffer[pos++] = (uint8_t)mcu->throughput_per_cycle;
75 }
76
77 /* Register File serialization */
78 uint32_t gpr_c = mg->registers.gpr_count;
79 uint32_t gpr_w = mg->registers.gpr_width_bits;
80 uint32_t vec_c = mg->registers.vector_count;
81 uint32_t vec_w = mg->registers.vector_width_bits;
82
83 buffer[pos++] = (uint8_t)gpr_c;
84 buffer[pos++] = (uint8_t)gpr_w;
85 buffer[pos++] = (uint8_t)vec_c;
86 buffer[pos++] = (uint8_t)vec_w;
87
88 /* Cache hierarchy serialization */
89 buffer[pos++] = (uint8_t)mg->cache_count;
90 for (size_t c = 0; c < mg->cache_count && c < OMEGA_MACHINE_MAX_CACHES; ++c) {
91 const MachineCacheLevel *mcl = &mg->caches[c];
92 buffer[pos++] = (uint8_t)mcl->level;
93 buffer[pos++] = mcl->is_instruction ? 1 : 0;
94 /* Size (8 bytes) */
95 for (int b = 7; b >= 0; --b) {
96 buffer[pos++] = (uint8_t)((mcl->size_bytes >> (b * 8)) & 0xFF);
97 }
98 buffer[pos++] = (uint8_t)mcl->line_size_bytes;
99 buffer[pos++] = (uint8_t)mcl->associativity;
100 buffer[pos++] = (uint8_t)mcl->latency_cycles;
101 }
102
103 /* DRAM layout serialization */
104 for (int b = 7; b >= 0; --b) {
105 buffer[pos++] = (uint8_t)((mg->dram_base >> (b * 8)) & 0xFF);
106 }
107 for (int b = 7; b >= 0; --b) {
108 buffer[pos++] = (uint8_t)((mg->dram_size >> (b * 8)) & 0xFF);
109 }
110
111 /* Physics Authority Seal */
112 buffer[pos++] = mg->is_physics_authorized ? 1 : 0;
113 memcpy(&buffer[pos], mg->physics_receipt_seal, 32);
114 pos += 32;
115
116 sha256_hash(buffer, pos, mg->machine_id.bytes);
117 return 0;
118}
119
120int omega_machine_build_dgx_spark(OmegaMachineGraph *mg) {
121 if (!mg) return -1;
122 omega_machine_init(mg, "NVIDIA_DGX_SPARK_GRACE_V2", AARCH64_PROFILE_V8A_BAREMETAL);
123
124 /* Neoverse V2: 4-wide dispatch pipeline */
125 mg->pipeline.issue_width = 4;
126 mg->pipeline.max_in_flight = 256;
127 mg->pipeline.out_of_order = true;
128 mg->pipeline.unit_count = 6;
129
130 mg->pipeline.units[0] = (MachineComputeUnit){ UNIT_ALU, 4, 1, 4 };
131 mg->pipeline.units[1] = (MachineComputeUnit){ UNIT_BRANCH, 2, 1, 2 };
132 mg->pipeline.units[2] = (MachineComputeUnit){ UNIT_MULTIPLIER, 2, 3, 2 };
133 mg->pipeline.units[3] = (MachineComputeUnit){ UNIT_DIVIDER, 1, 12, 1 };
134 mg->pipeline.units[4] = (MachineComputeUnit){ UNIT_LOAD_STORE, 2, 3, 2 };
135 mg->pipeline.units[5] = (MachineComputeUnit){ UNIT_VECTOR, 4, 2, 4 };
136
137 /* Register files */
138 mg->registers.gpr_count = 31;
139 mg->registers.gpr_width_bits = 64;
140 mg->registers.vector_count = 32;
141 mg->registers.vector_width_bits = 128;
142
143 /* Cache hierarchy */
144 mg->cache_count = 4;
145 mg->caches[0] = (MachineCacheLevel){ 1, true, 64ULL * 1024, 64, 4, 3 }; /* L1I: 64KB */
146 mg->caches[1] = (MachineCacheLevel){ 1, false, 64ULL * 1024, 64, 8, 4 }; /* L1D: 64KB */
147 mg->caches[2] = (MachineCacheLevel){ 2, false, 1024ULL * 1024, 64, 8, 10 }; /* L2: 1MB */
148 mg->caches[3] = (MachineCacheLevel){ 3, false, 114ULL * 1024 * 1024, 64, 16, 35 }; /* L3: 114MB */
149
150 /* Coherent DRAM */
151 mg->dram_base = 0x80000000ULL;
152 mg->dram_size = 128ULL * 1024 * 1024 * 1024; /* 128GB LPDDR5X */
153
154 mg->is_physics_authorized = true;
155 memset(mg->physics_receipt_seal, 0xA1, 32);
156
157 return omega_machine_compute_id(mg);
158}
159
160int omega_machine_build_qemu_virt(OmegaMachineGraph *mg) {
161 if (!mg) return -1;
162 omega_machine_init(mg, "QEMU_VIRT_AARCH64_GENERIC", AARCH64_PROFILE_V8A_BAREMETAL);
163
164 /* Generic QEMU: 2-wide dispatch pipeline */
165 mg->pipeline.issue_width = 2;
166 mg->pipeline.max_in_flight = 64;
167 mg->pipeline.out_of_order = false;
168 mg->pipeline.unit_count = 5;
169
170 mg->pipeline.units[0] = (MachineComputeUnit){ UNIT_ALU, 2, 1, 2 };
171 mg->pipeline.units[1] = (MachineComputeUnit){ UNIT_BRANCH, 1, 1, 1 };
172 mg->pipeline.units[2] = (MachineComputeUnit){ UNIT_MULTIPLIER, 1, 4, 1 };
173 mg->pipeline.units[3] = (MachineComputeUnit){ UNIT_LOAD_STORE, 1, 4, 1 };
174 mg->pipeline.units[4] = (MachineComputeUnit){ UNIT_VECTOR, 2, 3, 2 };
175
176 /* Register files */
177 mg->registers.gpr_count = 31;
178 mg->registers.gpr_width_bits = 64;
179 mg->registers.vector_count = 32;
180 mg->registers.vector_width_bits = 128;
181
182 /* Cache hierarchy */
183 mg->cache_count = 3;
184 mg->caches[0] = (MachineCacheLevel){ 1, true, 32ULL * 1024, 64, 2, 2 }; /* L1I: 32KB */
185 mg->caches[1] = (MachineCacheLevel){ 1, false, 32ULL * 1024, 64, 4, 3 }; /* L1D: 32KB */
186 mg->caches[2] = (MachineCacheLevel){ 2, false, 512ULL * 1024, 64, 8, 12 }; /* L2: 512KB */
187
188 /* DRAM */
189 mg->dram_base = 0x40000000ULL;
190 mg->dram_size = 1ULL * 1024 * 1024 * 1024; /* 1GB */
191
192 mg->is_physics_authorized = true;
193 memset(mg->physics_receipt_seal, 0xB2, 32);
194
195 return omega_machine_compute_id(mg);
196}
197
198int omega_machine_ingest_physics_descriptor(OmegaMachineGraph *mg, const PhysicsDescriptor *desc) {
199 if (!mg || !desc) return -1;
200 if (desc->magic != 0x4D414348) return -1; /* 'MACH' */
201 if (desc->issue_width == 0 || desc->gpr_count == 0) return -1;
202
203 omega_machine_init(mg, desc->cpu_name, (uint8_t)desc->target_profile);
204
205 mg->pipeline.issue_width = desc->issue_width;
206 mg->pipeline.max_in_flight = desc->issue_width * 32;
207 mg->pipeline.out_of_order = (desc->issue_width >= 4);
208 mg->pipeline.unit_count = 4;
209 mg->pipeline.units[0] = (MachineComputeUnit){ UNIT_ALU, desc->issue_width, 1, desc->issue_width };
210 mg->pipeline.units[1] = (MachineComputeUnit){ UNIT_BRANCH, 1, 1, 1 };
211 mg->pipeline.units[2] = (MachineComputeUnit){ UNIT_MULTIPLIER, 1, 3, 1 };
212 mg->pipeline.units[3] = (MachineComputeUnit){ UNIT_LOAD_STORE, 1, 3, 1 };
213
214 mg->registers.gpr_count = desc->gpr_count;
215 mg->registers.gpr_width_bits = 64;
216 mg->registers.vector_count = desc->vec_count;
217 mg->registers.vector_width_bits = 128;
218
219 mg->cache_count = 0;
220 if (desc->l1d_size > 0 && mg->cache_count < OMEGA_MACHINE_MAX_CACHES) {
221 mg->caches[mg->cache_count++] = (MachineCacheLevel){ 1, false, desc->l1d_size, 64, 4, 3 };
222 }
223 if (desc->l2_size > 0 && mg->cache_count < OMEGA_MACHINE_MAX_CACHES) {
224 mg->caches[mg->cache_count++] = (MachineCacheLevel){ 2, false, desc->l2_size, 64, 8, 10 };
225 }
226 if (desc->l3_size > 0 && mg->cache_count < OMEGA_MACHINE_MAX_CACHES) {
227 mg->caches[mg->cache_count++] = (MachineCacheLevel){ 3, false, desc->l3_size, 64, 16, 30 };
228 }
229
230 mg->dram_base = desc->dram_base;
231 mg->dram_size = desc->dram_size;
232
233 mg->is_physics_authorized = true;
234 memcpy(mg->physics_receipt_seal, desc->physics_seal, 32);
235
236 return omega_machine_compute_id(mg);
237}
238
239int omega_machine_validate_topology(const OmegaMachineGraph *mg, char *err_msg, size_t err_msg_len) {
240 if (!mg) return -1;
241
242 if (mg->pipeline.issue_width == 0) {
243 if (err_msg) snprintf(err_msg, err_msg_len, "Pipeline issue width is 0");
244 return -1;
245 }
246 if (mg->registers.gpr_count != 31) {
247 if (err_msg) snprintf(err_msg, err_msg_len, "Invalid GPR count: %u (expected 31)", mg->registers.gpr_count);
248 return -1;
249 }
250 if (mg->registers.gpr_width_bits != 64) {
251 if (err_msg) snprintf(err_msg, err_msg_len, "Invalid GPR width: %u (expected 64)", mg->registers.gpr_width_bits);
252 return -1;
253 }
254
255 /* Verify cache hierarchy monotonicity */
256 uint32_t prev_level = 0;
257 for (size_t c = 0; c < mg->cache_count; ++c) {
258 if (mg->caches[c].level < prev_level) {
259 if (err_msg) snprintf(err_msg, err_msg_len, "Cache hierarchy level inversion: L%u after L%u",
260 mg->caches[c].level, prev_level);
261 return -1;
262 }
263 if (mg->caches[c].line_size_bytes == 0 || (mg->caches[c].line_size_bytes & (mg->caches[c].line_size_bytes - 1)) != 0) {
264 if (err_msg) snprintf(err_msg, err_msg_len, "Invalid cache line size (non-power-of-two): %u",
265 mg->caches[c].line_size_bytes);
266 return -1;
267 }
268 prev_level = mg->caches[c].level;
269 }
270
271 return 0;
272}
273
274uint32_t omega_machine_estimate_latency(const OmegaMachineGraph *mg, const RealizationObject *real) {
275 if (!mg || !real || real->code_len == 0) return 0;
276
277 size_t insn_count = real->code_len / 4;
278 uint32_t total_cycles = 0;
279
280 uint32_t mul_latency = 3;
281 uint32_t alu_latency = 1;
282 for (size_t u = 0; u < mg->pipeline.unit_count; ++u) {
283 if (mg->pipeline.units[u].type == UNIT_MULTIPLIER) {
284 mul_latency = mg->pipeline.units[u].latency_cycles;
285 }
286 if (mg->pipeline.units[u].type == UNIT_ALU) {
287 alu_latency = mg->pipeline.units[u].latency_cycles;
288 }
289 }
290
291 for (size_t i = 0; i < insn_count; ++i) {
292 uint32_t word = ((uint32_t)real->code_bytes[i * 4 + 0]) |
293 (((uint32_t)real->code_bytes[i * 4 + 1]) << 8) |
294 (((uint32_t)real->code_bytes[i * 4 + 2]) << 16) |
295 (((uint32_t)real->code_bytes[i * 4 + 3]) << 24);
296
297 DecodedInsn dec;
298 if (aarch64_decode_instruction(word, &dec) == 0) {
299 if (dec.op == DECODED_MUL) {
300 total_cycles += mul_latency;
301 } else {
302 total_cycles += alu_latency;
303 }
304 } else {
305 total_cycles += alu_latency;
306 }
307 }
308
309 /* Pipeline issue folding */
310 if (mg->pipeline.issue_width > 1 && insn_count > 1) {
311 uint32_t folded = (uint32_t)((insn_count + mg->pipeline.issue_width - 1) / mg->pipeline.issue_width);
312 if (folded > total_cycles) total_cycles = folded;
313 }
314
315 return total_cycles;
316}