interplane.Get Omega
Read in your language

omega-bridge

Omega-first language interoperability: checked conversion backends and native language bindings

Brought by drakestapleton

branch main Branches Tags History

omega-bridge / tests / native / freestanding / aggregate.c

174 lines · 8553 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2 * Verification oracle only. Every allocator/IR/codec/math operation calls Omega.
3 * Static buffers back the actual fixed spans; no heap, TLS or device is needed. */
4#include <stdint.h>
5#include "omega_native.h"
6#define COUNT(a) (sizeof(a) / sizeof((a)[0]))
7static uint64_t results[16], registers[4096], fp[2], metadata[104], image[16777216];
8static uint64_t instructions[8192], physical[288];
9static uint8_t graph[6648836], identifier[32], name[64], text[16777216], code[16384];
10
11static int exercise(void) {
12 uint64_t ignored = 77;
13 union { double d; uint64_t u; } one = {.d = 1.0};
14 registers[0] = 1;
15 registers[2] = 128;
16 registers[18] = 1;
17 if (omega2_ra_run(&results[0], registers, COUNT(registers)) || results[0]) return 1;
18 results[1] = registers[3];
19 results[2] = registers[19];
20 if (results[2] < 2 || results[2] >= registers[2]) return 2;
21 if (omega2_ra_ir(&results[3], 0, 0, 1) || results[3]) return 3;
22 if (omega2_ra_live(&results[4], 0, 0, 0, 0, 0, 0) || results[4]) return 4;
23 fp[1] = 17;
24 if (omega2_omhost(&ignored, 0, one.u, 0, fp, COUNT(fp))) return 5;
25 results[5] = fp[0];
26 results[6] = fp[1];
27 /* WolframLanguageEvaluator: {32*16, Log[1]} -> {512, 0}. */
28 if (results[5] || results[6] != 17) return 6;
29 if (omega2_omgraph(&results[7], graph, COUNT(graph), identifier,
30 COUNT(identifier), 0, 0) || results[7]) return 7;
31 results[8] = graph[0];
32 if (results[8] != 1) return 8;
33 code[512] = 0xa5;
34 if (omega2_ampere_enc_sm86(&results[9], instructions, COUNT(instructions),
35 physical, COUNT(physical), 0, 1, code, COUNT(code))) return 9;
36 if (results[9] != 512 || code[results[9]] != 0xa5) return 10;
37 results[10] = code[0];
38 results[11] = code[results[9] - 1];
39 metadata[6] = 999; /* Unsupported target: source chk returns refusal 1, before output writes. */
40 image[0] = 17;
41 if (omega2_cubin_elf(&results[12], metadata, COUNT(metadata), name, COUNT(name),
42 text, COUNT(text), image, COUNT(image)) || results[12] != 1) return 11;
43 if (image[0] != 17) return 12;
44 results[14] = 99;
45 results[13] = omega2_cubin_elf(&results[14], metadata, COUNT(metadata), name,
46 COUNT(name), text, COUNT(text) - 1, image, COUNT(image));
47 if (results[13] != OMEGA_BAD_ARG || results[14] != 99 || image[0] != 17) return 13;
48#ifdef OMEGA_AGGREGATE_API_PLAN
49 /* WolframLanguageEvaluator:
50 * {Mod[128,128],Mod[129,128],Mod[7,2]} -> {0,1,1}.
51 * mm: mp=16 Ceiling[17/16], kp=16 Ceiling[19/16], np=8 Ceiling[9/8];
52 * {mp,kp,np,2 mp kp,Max[4 mp np,64 8 Ceiling[0/8]],2 kp np,19*9,16^^a5}
53 * -> {32,32,16,2048,2048,1024,171,165}. All sizes are bytes/counts as in mm_dims. */
54 uint64_t plan[16] = {17, 19, 9};
55 const uint64_t wanted[16] = {
56 17, 19, 9, 0, 0, 0, 0, 0, 32, 32, 16, 2048, 2048, 1024, 171, 165
57 };
58 for (unsigned i = 8; i < COUNT(plan); ++i) plan[i] = 0xa5;
59 if (omega2_api_policy(&ignored, 1, 1, 128, 0) || ignored != 0) return 14;
60 if (omega2_api_policy(&ignored, 1, 1, 129, 0) || ignored != 1) return 14;
61 if (omega2_api_policy(&ignored, 3, 7, 2, 64) || ignored != 1) return 14;
62 if (omega2_api_mm_plan(&ignored, 0, plan, COUNT(plan)) || ignored != 0) return 15;
63 for (unsigned i = 0; i < COUNT(plan); ++i)
64 if (plan[i] != wanted[i]) return 15;
65 ignored = 99;
66 if (omega2_api_mm_plan(&ignored, 0, plan, COUNT(plan) - 1) != OMEGA_BAD_ARG ||
67 ignored != 99) return 16;
68 for (unsigned i = 0; i < COUNT(plan); ++i)
69 if (plan[i] != wanted[i]) return 16;
70 /* WolframLanguageEvaluator: complete mode-0 vector -> {8,2,64,0,0,0,0,0,2,1,165,165,165,165,165,165}.
71 * Exact per-operator rounded queries in evidence/native-freestanding-f32/
72 * derive scale1.5=1074429868, h64/default/Newton=1043900987, h128=1040356334.
73 * These are integer-ABI output bits, never canonicalized or rewritten. */
74 uint64_t attention[16] = {8, 2, 64};
75 const uint64_t at_wanted[16] = {8, 2, 64, 0, 0, 0, 0, 0, 2, 1, 165, 165, 165, 165, 165, 165};
76 for (unsigned i = 8; i < COUNT(attention); ++i) attention[i] = 0xa5;
77 if (omega2_api_at_plan(&ignored, 0, attention, COUNT(attention)) || ignored != 0) return 17;
78 for (unsigned i = 0; i < COUNT(attention); ++i)
79 if (attention[i] != at_wanted[i]) return 17;
80 attention[0] = 7;
81 for (unsigned i = 8; i < COUNT(attention); ++i) attention[i] = 0xa5;
82 if (omega2_api_at_plan(&ignored, 0, attention, COUNT(attention)) || ignored != 1) return 18;
83 ignored = 99;
84 if (omega2_api_at_plan(&ignored, 0, attention, COUNT(attention) - 1) != OMEGA_BAD_ARG ||
85 ignored != 99) return 18;
86 for (unsigned i = 0; i < COUNT(attention); ++i) {
87 uint64_t want = i == 0 ? 7 : i == 1 ? 2 : i == 2 ? 64 : i < 8 ? 0 : 0xa5;
88 if (attention[i] != want) return 18;
89 }
90 for (unsigned i = 0; i < COUNT(attention); ++i) attention[i] = 0;
91 attention[0] = 1;
92 const uint64_t scales[] = {
93 0, UINT64_C(2147483648), UINT64_C(1065353216), UINT64_C(2137108966),
94 UINT64_C(2139095039), UINT64_C(2139095040), UINT64_C(3221225472),
95 UINT64_C(2147483647), UINT64_C(2143363909)
96 };
97 for (unsigned i = 0; i < COUNT(scales); ++i) {
98 attention[3] = scales[i];
99 if (omega2_api_at_plan(&ignored, 7, attention, COUNT(attention)) ||
100 ignored != (i < 4 ? 0u : 1u)) return 19;
101 for (unsigned j = 0; j < COUNT(attention); ++j) {
102 uint64_t want = j == 0 ? 1 : j == 3 ? scales[i] : 0;
103 if (attention[j] != want) return 19;
104 }
105 }
106 attention[0] = 64;
107 attention[1] = UINT64_C(1069547520); /* binary32 1.5, derived by Wolfram. */
108 if (omega2_api_at_plan(&ignored, 22, attention, COUNT(attention)) ||
109 ignored != 0 || attention[8] != UINT64_C(1074429868)) return 20;
110 attention[1] = 0;
111 if (omega2_api_at_plan(&ignored, 22, attention, COUNT(attention)) ||
112 ignored != 0 || attention[8] != UINT64_C(1043900987)) return 20;
113 attention[2] = 1;
114 if (omega2_api_at_plan(&ignored, 22, attention, COUNT(attention)) ||
115 ignored != 0 || attention[8] != UINT64_C(1043900987)) return 20;
116 if (omega2_api_at_plan(&ignored, 23, attention, COUNT(attention)) ||
117 ignored != 0 || attention[8] != UINT64_C(1043900987)) return 20;
118 attention[0] = 128;
119 if (omega2_api_at_plan(&ignored, 23, attention, COUNT(attention)) ||
120 ignored != 0 || attention[8] != UINT64_C(1040356334)) return 20;
121 for (unsigned i = 9; i < COUNT(attention); ++i)
122 if (attention[i] != 0) return 20;
123 results[15] = 1;
124#endif
125#ifdef OMEGA_AGGREGATE_WRONG
126 if (results[0] != 1) return 90;
127#endif
128 return 0;
129}
130
131#ifdef OMEGA_AGGREGATE_BARE
132#include <asm/unistd.h>
133#define STRING_INNER(x) #x
134#define STRING(x) STRING_INNER(x)
135static long linux_sys3(long number, long a, long b, long c) {
136#if defined(__aarch64__)
137 register long x0 __asm__("x0") = a;
138 register long x1 __asm__("x1") = b;
139 register long x2 __asm__("x2") = c;
140 register long x8 __asm__("x8") = number;
141 __asm__ volatile("svc #0" : "+r"(x0) : "r"(x1), "r"(x2), "r"(x8) : "memory", "cc");
142 return x0;
143#elif defined(__x86_64__)
144 long result;
145 __asm__ volatile("syscall" : "=a"(result) : "a"(number), "D"(a), "S"(b), "d"(c)
146 : "rcx", "r11", "memory", "cc");
147 return result;
148#else
149#error "aggregate test startup supports only AArch64 and x86-64 Linux"
150#endif
151}
152int omega_aggregate_main(void);
153int omega_aggregate_main(void) {
154 int status = exercise();
155 if (status) return status;
156 return linux_sys3(__NR_write, 1, (long)(uintptr_t)results, sizeof(results)) == (long)sizeof(results) ? 0 : 91;
157}
158/* The kernel's aligned initial stack enters a real ABI call, rather than a C _start
159 * with an invented return address. These are test-only Linux entry/exit syscalls. */
160#if defined(__aarch64__)
161__asm__(".text\n.global _start\n.type _start,%function\n_start:\n"
162 "bl omega_aggregate_main\nmov x8,#" STRING(__NR_exit) "\nsvc #0\nbrk #0\n");
163#else
164__asm__(".text\n.global _start\n.type _start,@function\n_start:\n"
165 "call omega_aggregate_main\nmov %rax,%rdi\nmov $" STRING(__NR_exit) ",%eax\nsyscall\nud2\n");
166#endif
167#else
168#include <stdio.h>
169int main(void) {
170 int status = exercise();
171 if (status) return status;
172 return fwrite(results, 1, sizeof(results), stdout) == sizeof(results) ? 0 : 91;
173}
174#endif