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 / seed / ng / egraph / gate_egraph.om

621 lines · 12532 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note seed/ng/egraph/gate_egraph.om: Pure seeded OM-2 verification gate for seed/ng/egraph/ir_egraph.om.
3note Program-mode gate (no om2 line; concatenated with printf 'om2\n' + ir.om + ir_egraph.om + gate_egraph.om).
4note Exercises all 18 EGP3 rewrite rules across 32-bit and 64-bit widths + real ΩIR blocks,
5note verifies strict cost reduction under both SM121 and X925 cost parameters, checks negative controls,
6note self-validates the emitted EGP3 binary buffer, and optionally writes it to argv[1].
7
8global g_dig uchar 24
9global g_path uchar 1024
10global g_neg ulong 1
11global g_vrm ulong 1
12global g_verr ulong 1
13
14text g_msg_pass "NG-EGRAPH-GATE PASS certs="
15text g_msg_rules " rules_exercised="
16text g_msg_neg " neg_controls="
17text g_msg_bytes " egp3_bytes="
18text g_msg_nl "\n"
19text g_msg_fail "NG-EGRAPH-GATE FAIL check="
20
21fn g_wrt s view uchar * to void
22r = write 1 s 0 len s
23end
24
25fn g_num v ulong to void
26x = v
27p = ulong 24
28for i ulong 21
29p = sub p 1
30put g_dig p uchar add 48 rem x 10
31x = div x 10
32if eq x 0
33r = write 1 g_dig p sub 24 p
34ret
35end
36end
37end
38
39fn g_fail code ulong to int
40g_wrt g_msg_fail
41g_num code
42g_wrt g_msg_nl
43ret int code
44end
45
46fn g_popcount18 msk ulong to ulong
47c = ulong 0
48for r ulong 18
49if ne and msk shl ulong 1 add r 1 0
50c = add c 1
51end
52end
53ret c
54end
55
56note Direct algebraic cases 0..3 (compact locals to stay well within omc's register pool)
57fn g_build_case_lo cid ulong to ulong
58if eq cid 0
59note 32-bit: add(mul(r0, r1), add(r2, 0)) -> fma(r0, r1, r2) [Rules 1, 3]
60eg3_reset 32
61r0 = eg3_add_ssa 0
62r1 = eg3_add_ssa 1
63r2 = eg3_add_ssa 2
64t0 = eg3_add_bin EG3_OP_MUL r0 r1
65t1 = eg3_add_bin EG3_OP_ADD r2 eg3_add_const 0
66ret eg3_add_bin EG3_OP_ADD t0 t1
67end
68if eq cid 1
69note 32-bit: add(add(0, r2), mul(and(r0, r0), mul(r1, 1))) -> fma(r0, r1, r2) [Rules 2, 4, 5, 11]
70eg3_reset 32
71r0 = eg3_add_ssa 0
72r1 = eg3_add_ssa 1
73r2 = eg3_add_ssa 2
74t0 = eg3_add_bin EG3_OP_ADD eg3_add_const 0 r2
75t1 = eg3_add_bin EG3_OP_AND r0 r0
76t2 = eg3_add_bin EG3_OP_MUL r1 eg3_add_const 1
77t3 = eg3_add_bin EG3_OP_MUL t1 t2
78ret eg3_add_bin EG3_OP_ADD t0 t3
79end
80if eq cid 2
81note 32-bit: add(mul(8, r0), mul(0, r1)) -> shl(r0, 3) [Rules 6, 8, 18, 3]
82eg3_reset 32
83r0 = eg3_add_ssa 0
84r1 = eg3_add_ssa 1
85t0 = eg3_add_bin EG3_OP_MUL eg3_add_const 8 r0
86t1 = eg3_add_bin EG3_OP_MUL eg3_add_const 0 r1
87ret eg3_add_bin EG3_OP_ADD t0 t1
88end
89note 64-bit: fma(r0, 1<<33, 0) -> mul(r0, 1<<33) -> shl(r0, 33) [Rules 12, 18, val_hi!=0]
90eg3_reset 64
91r0 = eg3_add_ssa 0
92t0 = eg3_add_const shl ulong 1 33
93t1 = eg3_add_const 0
94ret eg3_add_fma r0 t0 t1
95end
96
97note Direct algebraic cases 4..7
98fn g_build_case_hi cid ulong to ulong
99if eq cid 4
100note 32-bit: add(add(r0, mul(r1, r2)), r3) -> add(r3, fma(r1, r2, r0)) [Rules 7, 9, 1, 2]
101eg3_reset 32
102r0 = eg3_add_ssa 0
103r1 = eg3_add_ssa 1
104r2 = eg3_add_ssa 2
105r3 = eg3_add_ssa 3
106t0 = eg3_add_bin EG3_OP_MUL r1 r2
107t1 = eg3_add_bin EG3_OP_ADD r0 t0
108ret eg3_add_bin EG3_OP_ADD t1 r3
109end
110if eq cid 5
111note 64-bit: mul(mul(r0, 8), 1) -> shl(r0, 3) [Rules 10, 5, 18]
112eg3_reset 64
113r0 = eg3_add_ssa 0
114t0 = eg3_add_bin EG3_OP_MUL r0 eg3_add_const 8
115ret eg3_add_bin EG3_OP_MUL t0 eg3_add_const 1
116end
117if eq cid 6
118note 32-bit: sub(add(sub(r0, 0), xor(r1, r1)), sub(r2, r2)) -> r0 [Rules 13, 14, 15, 3]
119eg3_reset 32
120r0 = eg3_add_ssa 0
121r1 = eg3_add_ssa 1
122r2 = eg3_add_ssa 2
123t0 = eg3_add_bin EG3_OP_SUB r0 eg3_add_const 0
124t1 = eg3_add_bin EG3_OP_XOR r1 r1
125t2 = eg3_add_bin EG3_OP_SUB r2 r2
126t3 = eg3_add_bin EG3_OP_ADD t0 t1
127ret eg3_add_bin EG3_OP_SUB t3 t2
128end
129note 64-bit: shl(or(and(r0, r0), xor(r1, r1)), sub(r2, r2)) -> r0 [Rules 11, 14, 15, 16, 17]
130eg3_reset 64
131r0 = eg3_add_ssa 0
132r1 = eg3_add_ssa 1
133r2 = eg3_add_ssa 2
134t0 = eg3_add_bin EG3_OP_AND r0 r0
135t1 = eg3_add_bin EG3_OP_XOR r1 r1
136t2 = eg3_add_bin EG3_OP_SUB r2 r2
137t3 = eg3_add_bin EG3_OP_OR t0 t1
138ret eg3_add_bin EG3_OP_SHL t3 t2
139end
140
141fn g_build_case cid ulong to ulong
142if lt cid 4
143ret g_build_case_lo cid
144end
145ret g_build_case_hi cid
146end
147
148note Verify strict cost reduction for case cid under BOTH SM121 and X925, and emit cert
149fn g_run_case cid ulong fnIdx ulong blkIdx ulong to int
150eg3_set_costs_x925
151rx = g_build_case cid
152eg3_compute_costs
153cb = eg3_get_cost rx
154eg3_saturate 2
155if ge eg3_get_cost rx cb
156ret g_fail add 10 cid
157end
158eg3_set_costs_sm121
159rs = g_build_case cid
160eg3_compute_costs
161cb = eg3_get_cost rs
162eg3_saturate 2
163if ge eg3_get_cost rs cb
164ret g_fail add 20 cid
165end
166if eq get eg3_trn 0 0
167ret g_fail add 30 cid
168end
169if not eg3_cert_append fnIdx blkIdx rs eg3_get_best rs
170ret g_fail add 40 cid
171end
172ret 0
173end
174
175note Emit token-bearing and convergent ops into 32-bit ΩIR block (returns latest token)
176fn g_emit_refused32 b uint p0 uint p1 uint p2 uint tok0 uint to uint
177o = ir_op b 70 0 0 3
178ir_set o 0 tok0
179ir_set o 1 p0
180ir_set o 2 p1
181tk = ir_otok o
182o = ir_op b 42 4 0 3
183ir_set o 0 tk
184ir_set o 1 p0
185ir_set o 2 p1
186o = ir_op b 43 0 0 4
187ir_set o 0 tk
188ir_set o 1 p0
189ir_set o 2 p1
190ir_set o 3 p2
191tk = ir_otok o
192o = ir_op b 110 4 0 1
193ir_set o 0 tk
194tk = ir_otok o
195o = ir_op b 44 0 0 1
196ir_set o 0 tk
197tk = ir_otok o
198o = ir_op b 45 4 0 2
199ir_set o 0 p0
200ir_set o 1 p1
201o = ir_op b 106 4 0 3
202ir_set o 0 p0
203ir_set o 1 p1
204ir_set o 2 p2
205ret tk
206end
207
208note Build real ΩIR block 0 (32-bit u32): mixes token-bearing + convergent ops with pure arithmetic
209fn g_build_ir_blk32 to uint
210ir_init
211f = ir_fnew 0 0 4
212p0 = ir_fpar f 4
213p1 = ir_fpar f 4
214p2 = ir_fpar f 4
215b = ir_blk f
216tk = g_emit_refused32 b p0 p1 p2 ir_ftok f
217c0 = ir_res ir_op b 1 4 0 0
218c8 = ir_res ir_op b 1 4 8 0
219o = ir_op b 12 4 0 2
220ir_set o 0 p0
221ir_set o 1 c8
222v1 = ir_res o
223o = ir_op b 12 4 0 2
224ir_set o 0 v1
225ir_set o 1 p1
226v1 = ir_res o
227o = ir_op b 10 4 0 2
228ir_set o 0 p2
229ir_set o 1 c0
230v2 = ir_res o
231o = ir_op b 10 4 0 2
232ir_set o 0 v1
233ir_set o 1 v2
234v1 = ir_res o
235o = ir_op b 63 0 0 2
236ir_set o 0 tk
237ir_set o 1 v1
238ret b
239end
240
241note Emit mmald(104), mmast(105), call(64) into 64-bit ΩIR block
242fn g_emit_refused64 b uint f uint p0 uint p1 uint p2 uint tok0 uint to uint
243o = ir_op b 104 5 0 5
244ir_set o 0 tok0
245ir_set o 1 p0
246ir_set o 2 p1
247ir_set o 3 p2
248ir_set o 4 p0
249o = ir_op b 105 0 0 6
250ir_set o 0 tok0
251ir_set o 1 p0
252ir_set o 2 p1
253ir_set o 3 p2
254ir_set o 4 p0
255ir_set o 5 p1
256tk = ir_otok o
257o = ir_op b 64 5 0 2
258ir_set o 0 tk
259ir_set o 1 f
260ret ir_otok o
261end
262
263note Build real ΩIR block 1 (64-bit u64): mixes mmald, mmast, call with pure u64 ops
264fn g_build_ir_blk64 to uint
265ir_init
266f = ir_fnew 0 0 5
267p0 = ir_fpar f 5
268p1 = ir_fpar f 5
269p2 = ir_fpar f 5
270b = ir_blk f
271tk = g_emit_refused64 b f p0 p1 p2 ir_ftok f
272c1 = ir_res ir_op b 1 5 1 0
273c16 = ir_res ir_op b 1 5 16 0
274o = ir_op b 12 5 0 2
275ir_set o 0 p0
276ir_set o 1 c1
277v1 = ir_res o
278o = ir_op b 12 5 0 2
279ir_set o 0 v1
280ir_set o 1 c16
281v1 = ir_res o
282o = ir_op b 11 5 0 2
283ir_set o 0 p1
284ir_set o 1 p1
285v2 = ir_res o
286o = ir_op b 17 5 0 2
287ir_set o 0 p2
288ir_set o 1 v2
289v2 = ir_res o
290o = ir_op b 10 5 0 2
291ir_set o 0 v1
292ir_set o 1 v2
293v1 = ir_res o
294o = ir_op b 63 0 0 2
295ir_set o 0 tk
296ir_set o 1 v1
297ret b
298end
299
300fn g_test_ir_blocks to int
301b32 = g_build_ir_blk32
302eg3_set_costs_x925
303if eq eg3_opt_ir_block 4 0 b32 32 false 0
304ret g_fail 50
305end
306eg3_set_costs_sm121
307if eq eg3_opt_ir_block 4 0 b32 32 true 0
308ret g_fail 51
309end
310if ne get eg3_ir_stat 0 5
311ret g_fail 52
312end
313if ne get eg3_ir_stat 1 3
314ret g_fail 53
315end
316if ne get eg3_ir_stat 11 1
317ret g_fail 54
318end
319put g_neg 0 add get g_neg 0 2
320
321b64 = g_build_ir_blk64
322eg3_set_costs_x925
323if eq eg3_opt_ir_block 5 0 b64 64 false 0
324ret g_fail 55
325end
326eg3_set_costs_sm121
327if eq eg3_opt_ir_block 5 0 b64 64 true 0
328ret g_fail 56
329end
330if ne get eg3_ir_stat 0 2
331ret g_fail 57
332end
333if ne get eg3_ir_stat 1 2
334ret g_fail 58
335end
336if ne get eg3_ir_stat 11 1
337ret g_fail 59
338end
339put g_neg 0 add get g_neg 0 2
340ret 0
341end
342
343note Negative controls part 1: non-power-of-two multiplies MUST NOT fire Rule 18
344fn g_neg_pow2 to int
345eg3_set_costs_sm121
346eg3_reset 32
347r0 = eg3_add_ssa 0
348m6 = eg3_add_bin EG3_OP_MUL r0 eg3_add_const 6
349eg3_compute_costs
350cb = eg3_get_cost m6
351eg3_saturate 2
352if ne and get eg3_rmask 0 shl ulong 1 18 0
353ret g_fail 60
354end
355if ne eg3_get_cost m6 cb
356ret g_fail 61
357end
358put g_neg 0 add get g_neg 0 1
359
360eg3_reset 64
361r0 = eg3_add_ssa 0
362mb = eg3_add_bin EG3_OP_MUL r0 eg3_add_const add shl ulong 1 33 1
363eg3_saturate 2
364if ne and get eg3_rmask 0 shl ulong 1 18 0
365ret g_fail 62
366end
367put g_neg 0 add get g_neg 0 1
368ret 0
369end
370
371note Negative controls part 2: token-bearing, convergent, and float opcodes strictly refused
372fn g_neg_opcodes to int
373if ne eg3_ir_opcode_check 42 4 EG3_REFUSE_TOKEN
374ret g_fail 63
375end
376if ne eg3_ir_opcode_check 43 4 EG3_REFUSE_TOKEN
377ret g_fail 64
378end
379if ne eg3_ir_opcode_check 64 4 EG3_REFUSE_TOKEN
380ret g_fail 65
381end
382if ne eg3_ir_opcode_check 70 4 EG3_REFUSE_TOKEN
383ret g_fail 66
384end
385if ne eg3_ir_opcode_check 110 4 EG3_REFUSE_TOKEN
386ret g_fail 67
387end
388put g_neg 0 add get g_neg 0 1
389
390if ne eg3_ir_opcode_check 44 4 EG3_REFUSE_CONVERGENT
391ret g_fail 68
392end
393if ne eg3_ir_opcode_check 45 4 EG3_REFUSE_CONVERGENT
394ret g_fail 69
395end
396if ne eg3_ir_opcode_check 104 4 EG3_REFUSE_CONVERGENT
397ret g_fail 70
398end
399if ne eg3_ir_opcode_check 105 4 EG3_REFUSE_CONVERGENT
400ret g_fail 71
401end
402if ne eg3_ir_opcode_check 106 4 EG3_REFUSE_CONVERGENT
403ret g_fail 72
404end
405put g_neg 0 add get g_neg 0 1
406
407if ne eg3_ir_opcode_check 10 13 EG3_REFUSE_TYPE
408ret g_fail 73
409end
410if ne eg3_ir_opcode_check 12 14 EG3_REFUSE_TYPE
411ret g_fail 74
412end
413put g_neg 0 add get g_neg 0 1
414ret 0
415end
416
417note Negative controls part 3: pure-token/convergent block yields 0 ops, plus FMA target-cost sensitivity
418fn g_neg_blk_cost to int
419ir_init
420f = ir_fnew 0 0 0
421p0 = ir_fpar f 4
422b = ir_blk f
423o = ir_op b 44 0 0 1
424ir_set o 0 ir_ftok f
425tk = ir_otok o
426o = ir_op b 45 4 0 2
427ir_set o 0 p0
428ir_set o 1 p0
429o = ir_op b 62 0 0 1
430ir_set o 0 tk
431d0 = eg3_opt_ir_block 9 0 b 32 true
432if or ne d0 0 ne get eg3_ir_stat 4 0
433ret g_fail 75
434end
435if ne get eg3_ir_stat 11 0
436ret g_fail 76
437end
438put g_neg 0 add get g_neg 0 1
439
440eg3_set_target_costs 0 2 8 0
441eg3_reset 32
442r0 = eg3_add_ssa 0
443r1 = eg3_add_ssa 1
444r2 = eg3_add_ssa 2
445m = eg3_add_bin EG3_OP_MUL r0 r1
446rt = eg3_add_bin EG3_OP_ADD m r2
447eg3_saturate 2
448if eq get eg3_op eg3_get_best rt EG3_OP_FMA
449ret g_fail 77
450end
451eg3_set_target_costs 0 2 8 1
452eg3_compute_costs
453if ne get eg3_op eg3_get_best rt EG3_OP_FMA
454ret g_fail 78
455end
456put g_neg 0 add get g_neg 0 1
457ret 0
458end
459
460fn g_verify_nodes p0 ulong nc ulong w ulong to ulong
461p = p0
462for i ulong nc bound 512
463if ne eg3_get_u32 add p 24 w
464put g_verr 0 88
465ret 0
466end
467p = add p 32
468end
469ret p
470end
471
472fn g_verify_trace p0 ulong nt ulong nc ulong to ulong
473p = p0
474for i ulong nt bound 512
475rid = eg3_get_u32 p
476if or lt rid 1 gt rid 18
477put g_verr 0 90
478ret 0
479end
480if or ge eg3_get_u32 add p 4 nc ge eg3_get_u32 add p 8 nc
481put g_verr 0 90
482ret 0
483end
484bit = shl ulong 1 rid
485put g_vrm 0 or get g_vrm 0 bit
486p = add p 12
487end
488ret p
489end
490
491note Verify one serialized EGP3 cert entry starting at offset p0; returns next offset or 0 on error
492fn g_verify_one_cert p0 ulong nb ulong to ulong
493if gt add p0 28 nb
494put g_verr 0 84
495ret 0
496end
497w = eg3_get_u32 add p0 8
498nc = eg3_get_u32 add p0 12
499rb = eg3_get_u32 add p0 16
500ra = eg3_get_u32 add p0 20
501nt = eg3_get_u32 add p0 24
502if and ne w 32 ne w 64
503put g_verr 0 85
504ret 0
505end
506if or eq nc 0 or ge rb nc or ge ra nc eq nt 0
507put g_verr 0 86
508ret 0
509end
510p = add p0 28
511if gt add p mul nc 32 nb
512put g_verr 0 87
513ret 0
514end
515p = g_verify_nodes p nc w
516if eq p 0
517ret 0
518end
519if gt add p mul nt 12 nb
520put g_verr 0 89
521ret 0
522end
523ret g_verify_trace p nt nc
524end
525
526fn g_verify_egp3_buf to int
527nb = get eg3_bpos 0
528if lt nb 8
529ret g_fail 80
530end
531if or ne get eg3_buf 0 69 ne get eg3_buf 1 71
532ret g_fail 81
533end
534if or ne get eg3_buf 2 80 ne get eg3_buf 3 51
535ret g_fail 82
536end
537ncerts = eg3_get_u32 4
538if ne ncerts get eg3_certs 0
539ret g_fail 83
540end
541p = ulong 8
542put g_vrm 0 0
543put g_verr 0 0
544for k ulong ncerts bound 64
545p = g_verify_one_cert p nb
546if eq p 0
547ret g_fail get g_verr 0
548end
549end
550if ne p nb
551ret g_fail 91
552end
553if ne g_popcount18 get g_vrm 0 18
554ret g_fail 92
555end
556ret 0
557end
558
559fn main to int
560put g_neg 0 0
561eg3_cert_init
562
563for cid ulong 8
564rc = g_run_case cid div cid 2 rem cid 2
565if ne rc 0
566ret rc
567end
568end
569
570rc = g_test_ir_blocks
571if ne rc 0
572ret rc
573end
574
575rc = g_neg_pow2
576if ne rc 0
577ret rc
578end
579
580rc = g_neg_opcodes
581if ne rc 0
582ret rc
583end
584
585rc = g_neg_blk_cost
586if ne rc 0
587ret rc
588end
589
590rc = g_verify_egp3_buf
591if ne rc 0
592ret rc
593end
594
595if ge argcnt 2
596plen = argread 1 g_path 0 1023
597if lt plen 1
598ret g_fail 95
599end
600fd = fopen g_path 0 ulong plen 577 420
601if lt fd 0
602ret g_fail 96
603end
604ok = eg3_write_fd int fd
605z = fclose int fd
606if not ok
607ret g_fail 97
608end
609end
610
611g_wrt g_msg_pass
612g_num get eg3_certs 0
613g_wrt g_msg_rules
614g_num g_popcount18 get eg3_all_rmask 0
615g_wrt g_msg_neg
616g_num get g_neg 0
617g_wrt g_msg_bytes
618g_num get eg3_bpos 0
619g_wrt g_msg_nl
620ret 0
621end