interplane.Get Omega
Read in your language

CUDApp

CUDA++: AIEN’s GPU machine-code toolchain, runtime and driver client for NVIDIA GPUs without the CUDA toolkit. Hardware coverage and driver replacement limits are explicit.

Brought by drakestapleton

branch main Branches Tags History

CUDApp / src / omega_core.c

478 lines · 17050 bytes Raw Blame History
1#ifndef CUDAPP_MIGRATION_PENDING_C_CORE
2#include "omega_core_om.inc"
3#else
4/* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5 * Copyright 2026 Drake Stapleton (AIEN / aien-dev)
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
20#include "omega_core.h"
21#include "omega_canonical.h"
22#include <stdlib.h>
23#include <string.h>
24#include <stdio.h>
25
26OmegaGraph* omega_graph_create(void) {
27 OmegaGraph *g = (OmegaGraph*)calloc(1, sizeof(OmegaGraph));
28 return g;
29}
30
31void omega_graph_destroy(OmegaGraph *g) {
32 if (g) free(g);
33}
34
35OmegaObject* omega_graph_add_object(OmegaGraph *g, SemanticKind kind) {
36 if (!g || g->object_count >= OMEGA_MAX_GRAPH_OBJECTS) return NULL;
37 OmegaObject *obj = &g->objects[g->object_count++];
38 memset(obj, 0, sizeof(OmegaObject));
39 obj->kind = kind;
40 return obj;
41}
42
43OmegaObject* omega_graph_find_object(OmegaGraph *g, const SemanticId *id) {
44 if (!g || !id) return NULL;
45 for (uint16_t i = 0; i < g->object_count; ++i) {
46 if (g->objects[i].has_id && omega_compare_semantic_id(&g->objects[i].id, id) == 0) {
47 return &g->objects[i];
48 }
49 }
50 return NULL;
51}
52
53const OmegaObject* omega_graph_find_object_const(const OmegaGraph *g, const SemanticId *id) {
54 if (!g || !id) return NULL;
55 for (uint16_t i = 0; i < g->object_count; ++i) {
56 if (g->objects[i].has_id && omega_compare_semantic_id(&g->objects[i].id, id) == 0) {
57 return &g->objects[i];
58 }
59 }
60 return NULL;
61}
62
63int omega_object_add_attribute(OmegaObject *obj, const char *key, const uint8_t *val, uint16_t val_len) {
64 if (!obj || !key || obj->attr_count >= OMEGA_MAX_ATTRIBUTES) return -1;
65 /* Fields are part of the semantic identity: refuse what does not fit. */
66 size_t key_len = 0;
67 while (key_len < OMEGA_MAX_KEY_LEN && key[key_len] != '\0') key_len++;
68 if (key_len >= OMEGA_MAX_KEY_LEN || val_len > OMEGA_MAX_VAL_LEN) return -1;
69 OmegaAttribute *attr = &obj->attributes[obj->attr_count++];
70 memcpy(attr->key, key, key_len);
71 attr->key[key_len] = '\0';
72 if (val && val_len > 0) {
73 memcpy(attr->value, val, val_len);
74 attr->val_len = val_len;
75 } else {
76 attr->val_len = 0;
77 }
78 return 0;
79}
80
81int omega_object_add_relation(OmegaObject *obj, RelationKind kind, const SemanticId *target) {
82 if (!obj || !target || obj->rel_count >= OMEGA_MAX_RELATIONS) return -1;
83 OmegaRelation *rel = &obj->relations[obj->rel_count++];
84 rel->kind = (uint16_t)kind;
85 memcpy(rel->target_id.bytes, target->bytes, OMEGA_ID_BYTES);
86 return 0;
87}
88
89int omega_object_add_constraint(OmegaObject *obj, ConstraintKind kind, const uint8_t *payload, uint16_t payload_len) {
90 if (!obj || obj->const_count >= OMEGA_MAX_CONSTRAINTS) return -1;
91 if (payload_len > sizeof(obj->constraints[0].payload)) return -1;
92 OmegaConstraint *c = &obj->constraints[obj->const_count++];
93 c->kind = (uint16_t)kind;
94 c->payload_len = payload_len;
95 if (payload && c->payload_len > 0) {
96 memcpy(c->payload, payload, c->payload_len);
97 }
98 return 0;
99}
100
101OmegaObject* omega_build_type_unit(OmegaGraph *g) {
102 OmegaObject *obj = omega_graph_add_object(g, KIND_TYPE);
103 if (!obj) return NULL;
104 TypePayload tp;
105 memset(&tp, 0, sizeof(tp));
106 tp.tag = TYPE_UNIT;
107 memcpy(obj->payload, &tp, sizeof(tp));
108 obj->payload_len = sizeof(tp);
109 omega_compute_semantic_id(obj);
110 return obj;
111}
112
113OmegaObject* omega_build_type_bool(OmegaGraph *g) {
114 OmegaObject *obj = omega_graph_add_object(g, KIND_TYPE);
115 if (!obj) return NULL;
116 TypePayload tp;
117 memset(&tp, 0, sizeof(tp));
118 tp.tag = TYPE_BOOL;
119 memcpy(obj->payload, &tp, sizeof(tp));
120 obj->payload_len = sizeof(tp);
121 omega_compute_semantic_id(obj);
122 return obj;
123}
124
125OmegaObject* omega_build_type_uint(OmegaGraph *g, uint16_t width) {
126 OmegaObject *obj = omega_graph_add_object(g, KIND_TYPE);
127 if (!obj) return NULL;
128 TypePayload tp;
129 memset(&tp, 0, sizeof(tp));
130 tp.tag = TYPE_UNSIGNED_INT;
131 tp.width = width;
132 memcpy(obj->payload, &tp, sizeof(tp));
133 obj->payload_len = sizeof(tp);
134 omega_compute_semantic_id(obj);
135 return obj;
136}
137
138OmegaObject* omega_build_type_signed_int(OmegaGraph *g, uint16_t width) {
139 OmegaObject *obj = omega_graph_add_object(g, KIND_TYPE);
140 if (!obj) return NULL;
141 TypePayload tp;
142 memset(&tp, 0, sizeof(tp));
143 tp.tag = TYPE_SIGNED_INT;
144 tp.width = width;
145 memcpy(obj->payload, &tp, sizeof(tp));
146 obj->payload_len = sizeof(tp);
147 omega_compute_semantic_id(obj);
148 return obj;
149}
150
151OmegaObject* omega_build_type_fp32(OmegaGraph *g) {
152 OmegaObject *obj = omega_graph_add_object(g, KIND_TYPE);
153 if (!obj) return NULL;
154 TypePayload tp;
155 memset(&tp, 0, sizeof(tp));
156 tp.tag = TYPE_FP32;
157 tp.width = 32;
158 memcpy(obj->payload, &tp, sizeof(tp));
159 obj->payload_len = sizeof(tp);
160 omega_compute_semantic_id(obj);
161 return obj;
162}
163
164OmegaObject* omega_build_type_bitvector(OmegaGraph *g, uint16_t width) {
165 OmegaObject *obj = omega_graph_add_object(g, KIND_TYPE);
166 if (!obj) return NULL;
167 TypePayload tp;
168 memset(&tp, 0, sizeof(tp));
169 tp.tag = TYPE_BITVECTOR;
170 tp.width = width;
171 memcpy(obj->payload, &tp, sizeof(tp));
172 obj->payload_len = sizeof(tp);
173 omega_compute_semantic_id(obj);
174 return obj;
175}
176
177OmegaObject* omega_build_type_byte(OmegaGraph *g) {
178 OmegaObject *obj = omega_graph_add_object(g, KIND_TYPE);
179 if (!obj) return NULL;
180 TypePayload tp;
181 memset(&tp, 0, sizeof(tp));
182 tp.tag = TYPE_BYTE;
183 tp.width = 8;
184 memcpy(obj->payload, &tp, sizeof(tp));
185 obj->payload_len = sizeof(tp);
186 omega_compute_semantic_id(obj);
187 return obj;
188}
189
190OmegaObject* omega_build_type_sequence(OmegaGraph *g, const SemanticId *elem_type, uint32_t len) {
191 OmegaObject *obj = omega_graph_add_object(g, KIND_TYPE);
192 if (!obj) return NULL;
193 TypePayload tp;
194 memset(&tp, 0, sizeof(tp));
195 tp.tag = TYPE_SEQUENCE;
196 tp.length = len;
197 if (elem_type) memcpy(tp.elem_type.bytes, elem_type->bytes, OMEGA_ID_BYTES);
198 memcpy(obj->payload, &tp, sizeof(tp));
199 obj->payload_len = sizeof(tp);
200 omega_compute_semantic_id(obj);
201 return obj;
202}
203
204OmegaObject* omega_build_type_cap_ref(OmegaGraph *g) {
205 OmegaObject *obj = omega_graph_add_object(g, KIND_TYPE);
206 if (!obj) return NULL;
207 TypePayload tp;
208 memset(&tp, 0, sizeof(tp));
209 tp.tag = TYPE_CAPABILITY_REF;
210 memcpy(obj->payload, &tp, sizeof(tp));
211 obj->payload_len = sizeof(tp);
212 omega_compute_semantic_id(obj);
213 return obj;
214}
215
216OmegaObject* omega_build_val_bool(OmegaGraph *g, const SemanticId *bool_type_id, bool val) {
217 OmegaObject *obj = omega_graph_add_object(g, KIND_VALUE);
218 if (!obj) return NULL;
219 ValuePayload vp;
220 memset(&vp, 0, sizeof(vp));
221 if (bool_type_id) memcpy(vp.type_id.bytes, bool_type_id->bytes, OMEGA_ID_BYTES);
222 vp.byte_len = 1;
223 vp.bytes[0] = val ? 1 : 0;
224 memcpy(obj->payload, &vp, sizeof(vp));
225 obj->payload_len = sizeof(vp);
226 omega_compute_semantic_id(obj);
227 return obj;
228}
229
230OmegaObject* omega_build_val_uint(OmegaGraph *g, const SemanticId *uint_type_id, uint16_t width, uint64_t val) {
231 OmegaObject *obj = omega_graph_add_object(g, KIND_VALUE);
232 if (!obj) return NULL;
233 ValuePayload vp;
234 memset(&vp, 0, sizeof(vp));
235 if (uint_type_id) memcpy(vp.type_id.bytes, uint_type_id->bytes, OMEGA_ID_BYTES);
236 uint16_t nbytes = (width + 7) / 8;
237 if (nbytes == 0) nbytes = 1;
238 if (nbytes > 64) nbytes = 64;
239 vp.byte_len = nbytes;
240 for (int i = 0; i < nbytes; ++i) {
241 int shift = (nbytes - 1 - i) * 8;
242 if (shift < 64) {
243 vp.bytes[i] = (uint8_t)((val >> shift) & 0xff);
244 } else {
245 vp.bytes[i] = 0;
246 }
247 }
248 memcpy(obj->payload, &vp, sizeof(vp));
249 obj->payload_len = sizeof(vp);
250 omega_compute_semantic_id(obj);
251 return obj;
252}
253
254OmegaObject* omega_build_param(OmegaGraph *g, const SemanticId *type_id, uint16_t index) {
255 OmegaObject *obj = omega_graph_add_object(g, KIND_VALUE);
256 if (!obj) return NULL;
257 uint8_t idx[2] = { (uint8_t)(index >> 8), (uint8_t)index };
258 if (omega_object_add_attribute(obj, "omega.param", idx, sizeof idx) != 0) return NULL;
259 ValuePayload vp;
260 memset(&vp, 0, sizeof(vp));
261 if (type_id) memcpy(vp.type_id.bytes, type_id->bytes, OMEGA_ID_BYTES);
262 vp.byte_len = 0;
263 memcpy(obj->payload, &vp, sizeof(vp));
264 obj->payload_len = sizeof(vp);
265 omega_compute_semantic_id(obj);
266 return obj;
267}
268
269OmegaObject* omega_build_op_binary(OmegaGraph *g, OpCode op, OverflowPolicy ov, const SemanticId *type_id) {
270 OmegaObject *obj = omega_graph_add_object(g, KIND_OPERATION);
271 if (!obj) return NULL;
272 OperationPayload opp;
273 memset(&opp, 0, sizeof(opp));
274 opp.opcode = op;
275 opp.overflow = ov;
276 if (type_id) {
277 memcpy(opp.type_id.bytes, type_id->bytes, OMEGA_ID_BYTES);
278 opp.arity = 2;
279 memcpy(opp.input_types[0].bytes, type_id->bytes, OMEGA_ID_BYTES);
280 memcpy(opp.input_types[1].bytes, type_id->bytes, OMEGA_ID_BYTES);
281 memcpy(opp.output_type.bytes, type_id->bytes, OMEGA_ID_BYTES);
282 }
283 memcpy(obj->payload, &opp, sizeof(opp));
284 obj->payload_len = sizeof(opp);
285 omega_compute_semantic_id(obj);
286 return obj;
287}
288
289OmegaObject* omega_build_apply(OmegaGraph *g, const SemanticId *op_id, const SemanticId *arg1, const SemanticId *arg2) {
290 OmegaObject *obj = omega_graph_add_object(g, KIND_OPERATION);
291 if (!obj) return NULL;
292 ApplyPayload app;
293 memset(&app, 0, sizeof(app));
294 if (op_id) memcpy(app.op_id.bytes, op_id->bytes, OMEGA_ID_BYTES);
295 app.operand_count = 2;
296 if (arg1) memcpy(app.operands[0].bytes, arg1->bytes, OMEGA_ID_BYTES);
297 if (arg2) memcpy(app.operands[1].bytes, arg2->bytes, OMEGA_ID_BYTES);
298 memcpy(obj->payload, &app, sizeof(app));
299 obj->payload_len = sizeof(app);
300 omega_compute_semantic_id(obj);
301 return obj;
302}
303
304/* ---- effect payload v2 (spec/effect-cap64-migration.md) ---- */
305
306static void eff_put16(uint8_t *p, uint16_t v) { p[0] = (uint8_t)(v >> 8); p[1] = (uint8_t)v; }
307static void eff_put32(uint8_t *p, uint32_t v) {
308 for (int i = 0; i < 4; i++) p[i] = (uint8_t)(v >> (24 - 8 * i));
309}
310static void eff_put64(uint8_t *p, uint64_t v) {
311 for (int i = 0; i < 8; i++) p[i] = (uint8_t)(v >> (56 - 8 * i));
312}
313static uint16_t eff_get16(const uint8_t *p) { return (uint16_t)(((uint16_t)p[0] << 8) | p[1]); }
314static uint32_t eff_get32(const uint8_t *p) {
315 uint32_t v = 0;
316 for (int i = 0; i < 4; i++) v = (v << 8) | p[i];
317 return v;
318}
319static uint64_t eff_get64(const uint8_t *p) {
320 uint64_t v = 0;
321 for (int i = 0; i < 8; i++) v = (v << 8) | p[i];
322 return v;
323}
324
325enum {
326 EFF_OFF_RESOURCE = 0x00, EFF_OFF_OPERATION = 0x02, EFF_OFF_SLOT = 0x04,
327 EFF_OFF_GENERATION = 0x08, EFF_OFF_CAP_REF = 0x10, EFF_OFF_PARAM_LEN = 0x30,
328 EFF_OFF_PARAMS = 0x32
329};
330_Static_assert(EFF_OFF_PARAMS + OMEGA_EFFECT_PARAM_MAX == OMEGA_EFFECT_PAYLOAD_LEN,
331 "effect payload v2 is 178 bytes");
332_Static_assert(OMEGA_EFFECT_PAYLOAD_LEN != OMEGA_EFFECT_LEGACY_V1_LEN, "v1 and v2 lengths differ");
333
334int omega_effect_payload_encode(const EffectPayload *eff, uint8_t out[OMEGA_EFFECT_PAYLOAD_LEN]) {
335 if (!eff || !out) return OMEGA_EFFECT_ERR_ARG;
336 eff_put16(out + EFF_OFF_RESOURCE, eff->resource_class);
337 eff_put16(out + EFF_OFF_OPERATION, eff->operation_code);
338 eff_put32(out + EFF_OFF_SLOT, eff->capability_slot);
339 eff_put64(out + EFF_OFF_GENERATION, eff->capability_generation);
340 memcpy(out + EFF_OFF_CAP_REF, eff->capability_ref.bytes, OMEGA_ID_BYTES);
341 eff_put16(out + EFF_OFF_PARAM_LEN, eff->param_len);
342 memcpy(out + EFF_OFF_PARAMS, eff->param_bytes, OMEGA_EFFECT_PARAM_MAX);
343 return OMEGA_EFFECT_OK;
344}
345
346int omega_effect_payload_decode(const uint8_t *bytes, size_t len, EffectPayload *out) {
347 if (out) memset(out, 0, sizeof *out);
348 if (!bytes || !out) return OMEGA_EFFECT_ERR_ARG;
349 if (len == OMEGA_EFFECT_LEGACY_V1_LEN) return OMEGA_EFFECT_ERR_LEGACY_V1;
350 if (len != OMEGA_EFFECT_PAYLOAD_LEN) return OMEGA_EFFECT_ERR_LENGTH;
351 EffectPayload e;
352 memset(&e, 0, sizeof e);
353 e.resource_class = eff_get16(bytes + EFF_OFF_RESOURCE);
354 e.operation_code = eff_get16(bytes + EFF_OFF_OPERATION);
355 e.capability_slot = eff_get32(bytes + EFF_OFF_SLOT);
356 e.capability_generation = eff_get64(bytes + EFF_OFF_GENERATION);
357 memcpy(e.capability_ref.bytes, bytes + EFF_OFF_CAP_REF, OMEGA_ID_BYTES);
358 e.param_len = eff_get16(bytes + EFF_OFF_PARAM_LEN);
359 if (e.resource_class == 0) return OMEGA_EFFECT_ERR_RESOURCE;
360 if (e.param_len > OMEGA_EFFECT_PARAM_MAX) return OMEGA_EFFECT_ERR_PARAM_LEN;
361 for (size_t i = e.param_len; i < OMEGA_EFFECT_PARAM_MAX; i++)
362 if (bytes[EFF_OFF_PARAMS + i] != 0) return OMEGA_EFFECT_ERR_PADDING;
363 memcpy(e.param_bytes, bytes + EFF_OFF_PARAMS, e.param_len);
364 *out = e;
365 return OMEGA_EFFECT_OK;
366}
367
368int omega_effect_write(OmegaObject *obj, const EffectPayload *eff) {
369 if (!obj || !eff || obj->kind != KIND_EFFECT) return OMEGA_EFFECT_ERR_ARG;
370 int rc = omega_effect_payload_encode(eff, obj->payload);
371 if (rc != OMEGA_EFFECT_OK) return rc;
372 obj->payload_len = OMEGA_EFFECT_PAYLOAD_LEN;
373 return OMEGA_EFFECT_OK;
374}
375
376int omega_effect_read(const OmegaObject *obj, EffectPayload *out) {
377 if (out) memset(out, 0, sizeof *out);
378 if (!obj || !out || obj->kind != KIND_EFFECT) return OMEGA_EFFECT_ERR_ARG;
379 if (obj->payload_len > OMEGA_MAX_PAYLOAD_LEN) return OMEGA_EFFECT_ERR_LENGTH;
380 return omega_effect_payload_decode(obj->payload, obj->payload_len, out);
381}
382
383const char *omega_effect_strerror(int code) {
384 switch (code) {
385 case OMEGA_EFFECT_OK: return "ok";
386 case OMEGA_EFFECT_ERR_ARG: return "not an effect object";
387 case OMEGA_EFFECT_ERR_LENGTH: return "effect payload length is not 178 bytes";
388 case OMEGA_EFFECT_ERR_LEGACY_V1: return "legacy v1 effect payload (32-bit generation) refused";
389 case OMEGA_EFFECT_ERR_RESOURCE: return "effect resource class is 0 (invalid resource)";
390 case OMEGA_EFFECT_ERR_PARAM_LEN: return "effect param_len exceeds 128";
391 case OMEGA_EFFECT_ERR_PADDING: return "effect parameter padding is not zero";
392 default: return "unknown effect error";
393 }
394}
395
396OmegaObject* omega_build_effect(OmegaGraph *g, uint16_t res_class, uint16_t op_code, uint32_t cap_slot, uint64_t cap_gen) {
397 OmegaObject *obj = omega_graph_add_object(g, KIND_EFFECT);
398 if (!obj) return NULL;
399 EffectPayload eff;
400 memset(&eff, 0, sizeof(eff));
401 eff.resource_class = res_class;
402 eff.operation_code = op_code;
403 eff.capability_slot = cap_slot;
404 eff.capability_generation = cap_gen;
405 omega_effect_write(obj, &eff);
406 omega_compute_semantic_id(obj);
407 return obj;
408}
409
410int omega_eval_pure_binary_uint(OpCode op, OverflowPolicy ov, uint16_t width, uint64_t a, uint64_t b, uint64_t *out_res) {
411 if (!out_res) return -1;
412 uint64_t mask = (width >= 64) ? 0xFFFFFFFFFFFFFFFFULL : ((1ULL << width) - 1ULL);
413 a &= mask;
414 b &= mask;
415
416 uint64_t res = 0;
417 bool overflowed = false;
418
419 switch (op) {
420 case OP_ADD: {
421 res = a + b;
422 if (res < a || (res & ~mask) != 0) {
423 overflowed = true;
424 }
425 break;
426 }
427 case OP_SUB: {
428 if (a < b) {
429 overflowed = true;
430 res = (a + (mask + 1)) - b;
431 } else {
432 res = a - b;
433 }
434 break;
435 }
436 case OP_MUL: {
437 __uint128_t full = (__uint128_t)a * (__uint128_t)b;
438 if ((full >> width) != 0) {
439 overflowed = true;
440 }
441 res = (uint64_t)(full & mask);
442 break;
443 }
444 case OP_DIV: {
445 if (b == 0) return -1; /* Division by zero failure */
446 res = a / b;
447 break;
448 }
449 case OP_EQUAL: {
450 *out_res = (a == b) ? 1 : 0;
451 return 0;
452 }
453 case OP_LESS_THAN: {
454 *out_res = (a < b) ? 1 : 0;
455 return 0;
456 }
457 case OP_AND: {
458 res = a & b;
459 break;
460 }
461 case OP_OR: {
462 res = a | b;
463 break;
464 }
465 default:
466 return -1;
467 }
468
469 if (overflowed) {
470 if (ov == OVERFLOW_FAIL_CLOSED) return -2;
471 if (ov == OVERFLOW_SATURATE) res = mask;
472 /* OVERFLOW_WRAP naturally applies mask */
473 }
474
475 *out_res = res & mask;
476 return 0;
477}
478#endif