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 / tests / checker2 / test_c2_blast.c

240 lines · 9529 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/* test_c2_blast: checker2's bit-blaster without any SAT solver (omega#446).
18 * A. Every function of every corpus unit that has a non-scalar register (slice, handle, arena,
19 * struct) is refused as BLAST-UNSUPPORTED "not a scalar", never approximated.
20 * B. Every program of the LRAT gate (tests/checker2/lrat, IR written by lrat.sh): the CNF is
21 * evaluated input by input with unit propagation alone (the encoding is full Tseitin, so fixing
22 * the argument bits fixes every gate). p* programs ("holds") must conflict on every input;
23 * n* programs ("trap") must have an input that satisfies it. Exhaustive up to 16 argument bits.
24 * This cross-checks the CNF against the expectation independently of c2sat and of the proof.
25 * Usage: test_c2_blast <corpus-ir-dir> <lrat-work-dir> */
26#include <dirent.h>
27#include <stdio.h>
28#include <stdlib.h>
29#include <string.h>
30
31#include "c2_blast.h"
32
33static int fails;
34
35static uint8_t *slurp(const char *path, size_t *n)
36{
37 FILE *f = fopen(path, "rb");
38 if (!f) return NULL;
39 fseek(f, 0, SEEK_END);
40 long len = ftell(f);
41 fseek(f, 0, SEEK_SET);
42 uint8_t *b = malloc((size_t)len + 1);
43 if (b && fread(b, 1, (size_t)len, f) != (size_t)len) { free(b); b = NULL; }
44 fclose(f);
45 *n = (size_t)len;
46 return b;
47}
48
49static int load(const char *path, C2Unit *u)
50{
51 size_t n;
52 uint8_t *b = slurp(path, &n);
53 C2Verdict v;
54 int rc = !b || c2_decode(b, n, u, &v) || c2_check_unit(u, &v);
55 free(b);
56 return rc ? -1 : 0;
57}
58
59/* ------------------------------------------------------------------ propagation evaluator */
60
61typedef struct {
62 const C2Cnf *c;
63 int **occ, *nocc; /* clauses containing literal l at occ[l + nvars] */
64 signed char *val;
65 int *trail, ntrail;
66} Ev;
67
68static void ev_init(Ev *e, const C2Cnf *c)
69{
70 int nv = c->nvars;
71 e->c = c;
72 e->occ = calloc((size_t)nv * 2 + 1, sizeof *e->occ);
73 e->nocc = calloc((size_t)nv * 2 + 1, sizeof *e->nocc);
74 int *cap = calloc((size_t)nv * 2 + 1, sizeof *cap);
75 e->val = calloc((size_t)nv + 1, 1);
76 e->trail = malloc(((size_t)nv + 1) * sizeof *e->trail);
77 for (int i = 0; i < c->nclauses; i++)
78 for (const int32_t *l = c->lits + c->start[i]; *l; l++) {
79 int s = *l + nv;
80 if (e->nocc[s] == cap[s]) { cap[s] = cap[s] ? cap[s] * 2 : 4; e->occ[s] = realloc(e->occ[s], (size_t)cap[s] * sizeof(int)); }
81 e->occ[s][e->nocc[s]++] = i;
82 }
83 free(cap);
84}
85
86static void ev_free(Ev *e)
87{
88 for (int i = 0; i < e->c->nvars * 2 + 1; i++) free(e->occ[i]);
89 free(e->occ);
90 free(e->nocc);
91 free(e->val);
92 free(e->trail);
93}
94
95static int lval(const Ev *e, int l) { int v = e->val[abs(l)]; return l > 0 ? v : -v; }
96static void set(Ev *e, int l) { e->val[abs(l)] = l > 0 ? 1 : -1; e->trail[e->ntrail++] = l; }
97
98/* 1 conflict, 0 every variable assigned without conflict, -1 propagation left variables open */
99static int ev_run(Ev *e, int inputs, unsigned long a)
100{
101 const C2Cnf *c = e->c;
102 while (e->ntrail) e->val[abs(e->trail[--e->ntrail])] = 0;
103 for (int i = 0; i < c->nclauses; i++) /* unit and empty input clauses */
104 if (c->lits[c->start[i]] == 0) return 1;
105 for (int i = 0; i < c->nclauses; i++) {
106 const int32_t *l = c->lits + c->start[i];
107 if (l[1] == 0) {
108 if (lval(e, l[0]) < 0) return 1;
109 if (!lval(e, l[0])) set(e, l[0]);
110 }
111 }
112 for (int k = 0; k < inputs; k++) {
113 int v = 2 + k, l = (a >> k) & 1 ? v : -v;
114 if (lval(e, l) < 0) return 1;
115 if (!lval(e, l)) set(e, l);
116 }
117 for (int q = 0; q < e->ntrail; q++) {
118 int f = -e->trail[q];
119 for (int k = 0; k < e->nocc[f + c->nvars]; k++) {
120 const int32_t *l = c->lits + c->start[e->occ[f + c->nvars][k]];
121 int open = 0, last = 0, sat = 0;
122 for (; *l; l++) {
123 int s = lval(e, *l);
124 if (s > 0) { sat = 1; break; }
125 if (!s) { open++; last = *l; }
126 }
127 if (sat) continue;
128 if (!open) return 1;
129 if (open == 1) set(e, last);
130 }
131 }
132 return e->ntrail == c->nvars ? 0 : -1;
133}
134
135/* ------------------------------------------------------------------ main */
136
137static int has_nonscalar(const C2Func *f)
138{
139 for (int r = 0; r < f->nvregs; r++)
140 if (!(f->vt[r].s >= S_BOOL && f->vt[r].s <= S_I64)) return 1;
141 return 0;
142}
143
144static int by_name(const void *a, const void *b) { return strcmp(*(char *const *)a, *(char *const *)b); }
145
146static int list(const char *dir, const char *suffix, char ***out)
147{
148 DIR *d = opendir(dir);
149 if (!d) return -1;
150 int n = 0, cap = 0;
151 char **v = NULL;
152 struct dirent *de;
153 while ((de = readdir(d))) {
154 size_t l = strlen(de->d_name), s = strlen(suffix);
155 if (l <= s || strcmp(de->d_name + l - s, suffix)) continue;
156 if (n == cap) { cap = cap ? cap * 2 : 64; v = realloc(v, (size_t)cap * sizeof *v); }
157 v[n] = malloc(strlen(dir) + l + 2);
158 sprintf(v[n++], "%s/%s", dir, de->d_name);
159 }
160 closedir(d);
161 qsort(v, (size_t)n, sizeof *v, by_name);
162 *out = v;
163 return n;
164}
165
166int main(int argc, char **argv)
167{
168 if (argc != 3) { fprintf(stderr, "usage: test_c2_blast <corpus-ir-dir> <lrat-work-dir>\n"); return 2; }
169 char **files;
170 int nf = list(argv[1], ".ir", &files), refused_nonscalar = 0, scalar_ok = 0, scalar_refused = 0;
171 if (nf <= 0) { printf("FAIL no corpus IR in %s\n", argv[1]); return 1; }
172 for (int i = 0; i < nf; i++) {
173 C2Unit u;
174 if (load(files[i], &u)) { printf("FAIL %s: not accepted\n", files[i]); fails++; free(files[i]); continue; }
175 for (int fi = 0; fi < u.nfuncs; fi++) {
176 C2Cnf cnf;
177 C2BlastInfo bi;
178 memset(&cnf, 0, sizeof cnf);
179 int rc = c2_blast_notrap(&u, fi, &cnf, &bi);
180 if (rc == 0) c2_cnf_free(&cnf);
181 if (has_nonscalar(&u.fn[fi])) {
182 if (rc == 0 || strcmp(bi.rule, "BLAST-UNSUPPORTED") || !strstr(bi.detail, "not a scalar")) {
183 printf("FAIL %s fn %d: non-scalar function not refused as such (rc=%d rule=%s detail=%s)\n",
184 files[i], fi, rc, rc ? bi.rule : "-", rc ? bi.detail : "-");
185 fails++;
186 } else refused_nonscalar++;
187 } else if (rc == 0) scalar_ok++;
188 else scalar_refused++;
189 }
190 c2_unit_free(&u);
191 free(files[i]);
192 }
193 free(files);
194 if (refused_nonscalar == 0) { printf("FAIL corpus has no non-scalar function to refuse\n"); fails++; }
195 printf("test_c2_blast: corpus %d units: %d scalar functions blasted, %d scalar refused (calls, division, memory), %d non-scalar refused\n",
196 nf, scalar_ok, scalar_refused, refused_nonscalar);
197
198 int np = list(argv[2], ".ir", &files), holds = 0, traps = 0, checked = 0;
199 if (np <= 0) { printf("FAIL no LRAT gate IR in %s\n", argv[2]); return 1; }
200 for (int i = 0; i < np; i++) {
201 const char *base = strrchr(files[i], '/') + 1;
202 int want_trap = base[0] == 'n';
203 if (base[0] != 'n' && base[0] != 'p') { free(files[i]); continue; }
204 C2Unit u;
205 C2Cnf cnf;
206 C2BlastInfo bi;
207 memset(&cnf, 0, sizeof cnf);
208 if (load(files[i], &u) || u.nfuncs != 1 || c2_blast_notrap(&u, 0, &cnf, &bi)) {
209 printf("FAIL %s: not blasted\n", base);
210 fails++;
211 free(files[i]);
212 continue;
213 }
214 Ev e;
215 memset(&e, 0, sizeof e);
216 ev_init(&e, &cnf);
217 int ok = 1;
218 unsigned long total = bi.inputs <= 16 ? 1UL << bi.inputs : 65536, sat = 0, open = 0;
219 unsigned long x = 99;
220 for (unsigned long a = 0; a < total; a++) {
221 unsigned long in = a;
222 if (bi.inputs > 16) { x = x * 6364136223846793005UL + 1442695040888963407UL; in = x >> 7; }
223 int r = ev_run(&e, bi.inputs, in);
224 if (r == 0) sat++;
225 if (r < 0) open++;
226 }
227 if (open) { printf("FAIL %s: %lu inputs left gates undetermined\n", base, open); ok = 0; }
228 if (want_trap && !sat) { printf("FAIL %s: expected a trapping input, none of %lu traps\n", base, total); ok = 0; }
229 if (!want_trap && sat) { printf("FAIL %s: %lu of %lu inputs trap but the program is expected to hold\n", base, sat, total); ok = 0; }
230 if (ok) { checked++; if (want_trap) traps++; else holds++; } else fails++;
231 ev_free(&e);
232 c2_cnf_free(&cnf);
233 c2_unit_free(&u);
234 free(files[i]);
235 }
236 free(files);
237 if (holds < 20) { printf("FAIL only %d holding programs checked (need 20)\n", holds); fails++; }
238 printf("test_c2_blast: %d gate programs evaluated input by input (%d hold, %d trap), %d failures\n", checked, holds, traps, fails);
239 return fails ? 1 : 0;
240}