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 / ir_egraph.om

1144 lines · 22098 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note seed/ng/egraph/ir_egraph.om: Verified Equality-Saturation E-Graph Optimizer for ΩIR (pure OM-2).
3note Library unit (no om2 header, no fn main; all symbols eg3_* / EG3_*).
4note Supports both 32-bit and 64-bit wrapping arithmetic, all 18 verified EGP3 rewrite rules,
5note TargetDesc-parameterized extraction (addCost, mulCost, fmaCost, shlCost, bitCost),
6note strict refusal of token-bearing and convergent GPU ops, ΩIR block extraction bridge
7note (eg3_opt_ir_block), and full EGP3 binary structural proof certificate emission.
8
9note EGP3 node opcodes:
10note 1=CONST 2=SSA_VAL 3=ADD 4=SUB 5=MUL 6=FMA 7=SHL 8=SHR 9=AND 10=OR 11=XOR
11const EG3_OP_CONST ulong 1
12const EG3_OP_SSA ulong 2
13const EG3_OP_ADD ulong 3
14const EG3_OP_SUB ulong 4
15const EG3_OP_MUL ulong 5
16const EG3_OP_FMA ulong 6
17const EG3_OP_SHL ulong 7
18const EG3_OP_SHR ulong 8
19const EG3_OP_AND ulong 9
20const EG3_OP_OR ulong 10
21const EG3_OP_XOR ulong 11
22
23note Capacities and sentinels:
24const EG3_MAX_NODES ulong 512
25const EG3_SAT_CAP ulong 320
26const EG3_MAX_TRACE ulong 512
27const EG3_BUF_CAP ulong 131072
28const EG3_INF_COST ulong 999999
29const EG3_NONE ulong 999999
30const EG3_MASK32 ulong 4294967295
31
32note ΩIR extraction refusal codes:
33const EG3_ALLOW ulong 0
34const EG3_REFUSE_TOKEN ulong 1
35const EG3_REFUSE_CONVERGENT ulong 2
36const EG3_REFUSE_TYPE ulong 3
37const EG3_REFUSE_UNSUP ulong 4
38
39note Working e-graph tables (per expression / block):
40global eg3_par ulong 512
41global eg3_op ulong 512
42global eg3_a ulong 512
43global eg3_b ulong 512
44global eg3_c ulong 512
45global eg3_vlo ulong 512
46global eg3_vhi ulong 512
47global eg3_w ulong 512
48global eg3_fl ulong 512
49global eg3_cnt ulong 1
50global eg3_width ulong 1
51
52note Cost model parameters [0]=add [1]=mul [2]=fma [3]=shl [4]=bit and extraction arrays:
53global eg3_cparam ulong 8
54global eg3_cost ulong 512
55global eg3_dcost ulong 512
56global eg3_best ulong 512
57
58note Proof witness trace (rule_id, lhs_node, rhs_node) and rule-coverage bitmasks:
59global eg3_tr_rule ulong 512
60global eg3_tr_lhs ulong 512
61global eg3_tr_rhs ulong 512
62global eg3_trn ulong 1
63global eg3_rmask ulong 1
64global eg3_all_rmask ulong 1
65
66note EGP3 serialized binary certificate stream:
67global eg3_buf uchar 131072
68global eg3_bpos ulong 1
69global eg3_certs ulong 1
70
71note ΩIR block extraction mapping and telemetry:
72note eg3_ir_stat: [0]=ref_tok [1]=ref_conv [2]=ref_type [3]=ref_unsup [4]=extracted
73note [5]=cost_before [6]=cost_after [7]=root_before [8]=root_after
74note [9]=trace_cnt [10]=width [11]=cert_emitted
75global eg3_ir_val uint 256
76global eg3_ir_nod ulong 256
77global eg3_ir_vn ulong 1
78global eg3_ir_sn ulong 1
79global eg3_ir_stat ulong 16
80
81note ---- Cost Model Configuration ----------------------------------------------------------
82
83fn eg3_set_costs addC ulong mulC ulong fmaC ulong shlC ulong bitC ulong to void
84put eg3_cparam 0 addC
85put eg3_cparam 1 mulC
86put eg3_cparam 2 fmaC
87put eg3_cparam 3 shlC
88put eg3_cparam 4 bitC
89end
90
91note Derive (addCost, mulCost, fmaCost, shlCost, bitCost) from TargetDesc fields
92note (TDF_EXEC, TDF_MULLAT, TDF_DIVLAT, TDF_FEAT).
93fn eg3_set_target_costs execM ulong mulLat ulong divLat ulong feat ulong to void
94addC = ulong 1
95mulC = mulLat
96if eq mulC 0
97mulC = ulong 2
98end
99if eq execM 1
100addC = ulong 2
101if lt mulC 3
102mulC = ulong 3
103end
104end
105fmaC = add addC mulC
106fmaC = add fmaC 1
107if ne and feat 1 0
108if eq execM 1
109fmaC = ulong 2
110else
111fmaC = mulC
112end
113end
114eg3_set_costs addC mulC fmaC 1 1
115end
116
117fn eg3_set_costs_sm121 to void
118eg3_set_costs 2 3 2 1 1
119end
120
121fn eg3_set_costs_x925 to void
122eg3_set_costs 1 2 2 1 1
123end
124
125fn eg3_set_costs_a725 to void
126eg3_set_costs 1 3 3 1 1
127end
128
129note ---- Core E-Graph Primitives -----------------------------------------------------------
130
131fn eg3_reset widthBits ulong to void
132w = widthBits
133if ne w 64
134w = ulong 32
135end
136put eg3_width 0 w
137put eg3_cnt 0 0
138put eg3_trn 0 0
139put eg3_rmask 0 0
140end
141
142fn eg3_mask_val v ulong w ulong to ulong
143if eq w 32
144ret and v EG3_MASK32
145end
146ret v
147end
148
149fn eg3_node_val id ulong to ulong
150lo = get eg3_vlo id
151hi = get eg3_vhi id
152ret or lo shl hi 32
153end
154
155fn eg3_find x ulong to ulong
156c = x
157for i ulong 64
158p = get eg3_par c
159if eq p c
160ret c
161end
162c = p
163end
164ret c
165end
166
167fn eg3_add_raw op ulong a ulong b ulong c ulong val ulong to ulong
168id = get eg3_cnt 0
169if ge id EG3_MAX_NODES
170ret EG3_NONE
171end
172w = get eg3_width 0
173mv = val
174if eq op EG3_OP_CONST
175mv = eg3_mask_val val w
176end
177put eg3_cnt 0 add id 1
178put eg3_par id id
179put eg3_op id op
180put eg3_a id a
181put eg3_b id b
182put eg3_c id c
183put eg3_vlo id and mv EG3_MASK32
184put eg3_vhi id shr mv 32
185put eg3_w id w
186put eg3_fl id 0
187ret id
188end
189
190fn eg3_add_const val ulong to ulong
191w = get eg3_width 0
192mv = eg3_mask_val val w
193lo = and mv EG3_MASK32
194hi = shr mv 32
195n = get eg3_cnt 0
196for i ulong n bound 512
197if eq get eg3_op i EG3_OP_CONST
198if eq get eg3_vlo i lo
199if eq get eg3_vhi i hi
200if eq get eg3_w i w
201ret i
202end
203end
204end
205end
206end
207ret eg3_add_raw EG3_OP_CONST 0 0 0 mv
208end
209
210fn eg3_add_ssa sid ulong to ulong
211w = get eg3_width 0
212lo = and sid EG3_MASK32
213n = get eg3_cnt 0
214for i ulong n bound 512
215if eq get eg3_op i EG3_OP_SSA
216if eq get eg3_vlo i lo
217if eq get eg3_w i w
218ret i
219end
220end
221end
222end
223ret eg3_add_raw EG3_OP_SSA 0 0 0 lo
224end
225
226fn eg3_add_bin op ulong a ulong b ulong to ulong
227ret eg3_add_raw op a b 0 0
228end
229
230fn eg3_add_fma a ulong b ulong c ulong to ulong
231ret eg3_add_raw EG3_OP_FMA a b c 0
232end
233
234fn eg3_intern_bin op ulong a ulong b ulong to ulong
235if or ge a EG3_MAX_NODES ge b EG3_MAX_NODES
236ret EG3_NONE
237end
238ca = eg3_find a
239cb = eg3_find b
240w = get eg3_width 0
241n = get eg3_cnt 0
242for i ulong n bound 512
243if eq get eg3_op i op
244if eq get eg3_w i w
245if eq eg3_find get eg3_a i ca
246if eq eg3_find get eg3_b i cb
247ret i
248end
249end
250end
251end
252end
253ret eg3_add_raw op a b 0 0
254end
255
256fn eg3_intern_fma a ulong b ulong c ulong to ulong
257if or ge a EG3_MAX_NODES or ge b EG3_MAX_NODES ge c EG3_MAX_NODES
258ret EG3_NONE
259end
260ca = eg3_find a
261cb = eg3_find b
262cc = eg3_find c
263w = get eg3_width 0
264n = get eg3_cnt 0
265for i ulong n bound 512
266if eq get eg3_op i EG3_OP_FMA
267if eq get eg3_w i w
268if eq eg3_find get eg3_a i ca
269if eq eg3_find get eg3_b i cb
270if eq eg3_find get eg3_c i cc
271ret i
272end
273end
274end
275end
276end
277end
278ret eg3_add_raw EG3_OP_FMA a b c 0
279end
280
281fn eg3_union u ulong v ulong rule ulong to bool
282n = get eg3_cnt 0
283if or ge u n ge v n
284ret false
285end
286ru = eg3_find u
287rv = eg3_find v
288if eq ru rv
289ret false
290end
291t = get eg3_trn 0
292if ge t EG3_MAX_TRACE
293ret false
294end
295put eg3_par rv ru
296put eg3_tr_rule t rule
297put eg3_tr_lhs t u
298put eg3_tr_rhs t v
299put eg3_trn 0 add t 1
300if and ge rule 1 le rule 18
301put eg3_rmask 0 or get eg3_rmask 0 shl ulong 1 rule
302end
303ret true
304end
305
306note ---- Target-Parameterized Cost Model & Extraction --------------------------------------
307
308fn eg3_op_cost op ulong to ulong
309if or eq op EG3_OP_CONST eq op EG3_OP_SSA
310ret 0
311end
312if or eq op EG3_OP_ADD eq op EG3_OP_SUB
313ret get eg3_cparam 0
314end
315if eq op EG3_OP_MUL
316ret get eg3_cparam 1
317end
318if eq op EG3_OP_FMA
319ret get eg3_cparam 2
320end
321if or eq op EG3_OP_SHL eq op EG3_OP_SHR
322ret get eg3_cparam 3
323end
324ret get eg3_cparam 4
325end
326
327fn eg3_node_ec_cost id ulong to ulong
328op = get eg3_op id
329base = eg3_op_cost op
330if or eq op EG3_OP_CONST eq op EG3_OP_SSA
331ret 0
332end
333ca = get eg3_cost eg3_find get eg3_a id
334cb = get eg3_cost eg3_find get eg3_b id
335if or ge ca EG3_INF_COST ge cb EG3_INF_COST
336ret EG3_INF_COST
337end
338tot = add base add ca cb
339if eq op EG3_OP_FMA
340cc = get eg3_cost eg3_find get eg3_c id
341if ge cc EG3_INF_COST
342ret EG3_INF_COST
343end
344tot = add tot cc
345end
346ret tot
347end
348
349fn eg3_compute_dcosts to void
350n = get eg3_cnt 0
351for i ulong n bound 512
352op = get eg3_op i
353if or eq op EG3_OP_CONST eq op EG3_OP_SSA
354put eg3_dcost i 0
355else
356base = eg3_op_cost op
357da = get eg3_dcost get eg3_a i
358db = get eg3_dcost get eg3_b i
359tot = add base add da db
360if eq op EG3_OP_FMA
361tot = add tot get eg3_dcost get eg3_c i
362end
363put eg3_dcost i tot
364end
365end
366end
367
368fn eg3_compute_costs to void
369n = get eg3_cnt 0
370eg3_compute_dcosts
371for i ulong n bound 512
372put eg3_cost i EG3_INF_COST
373put eg3_best i i
374end
375for iter ulong 16
376for i ulong n bound 512
377c = eg3_node_ec_cost i
378r = eg3_find i
379cur = get eg3_cost r
380if lt c cur
381put eg3_cost r c
382put eg3_best r i
383else
384if and eq c cur lt c EG3_INF_COST
385b = get eg3_best r
386if lt get eg3_dcost i get eg3_dcost b
387put eg3_best r i
388end
389end
390end
391end
392end
393end
394
395fn eg3_get_cost x ulong to ulong
396ret get eg3_cost eg3_find x
397end
398
399fn eg3_get_best x ulong to ulong
400ret get eg3_best eg3_find x
401end
402
403fn eg3_best_of x ulong to ulong
404r = eg3_find x
405if lt get eg3_cost r EG3_INF_COST
406ret get eg3_best r
407end
408ret x
409end
410
411note ---- Constant, Power-of-Two, and E-Class Matching Helpers ------------------------------
412
413fn eg3_find_const id ulong want ulong to ulong
414c = eg3_find id
415w = get eg3_width 0
416mw = eg3_mask_val want w
417lo = and mw EG3_MASK32
418hi = shr mw 32
419n = get eg3_cnt 0
420for i ulong n bound 512
421if eq get eg3_op i EG3_OP_CONST
422if eq eg3_find i c
423if eq get eg3_vlo i lo
424if eq get eg3_vhi i hi
425ret i
426end
427end
428end
429end
430end
431ret EG3_NONE
432end
433
434fn eg3_has_const id ulong want ulong to bool
435ret ne eg3_find_const id want EG3_NONE
436end
437
438note Return k if masked v == (1 << k) with 0 <= k < widthBits, else EG3_NONE.
439fn eg3_pow2_exp v ulong w ulong to ulong
440mv = eg3_mask_val v w
441if eq mv 0
442ret EG3_NONE
443end
444if ne and mv sub mv 1 0
445ret EG3_NONE
446end
447for k ulong w bound 64
448if eq shl ulong 1 k mv
449ret k
450end
451end
452ret EG3_NONE
453end
454
455note Find a power-of-two constant 1 << k (1 <= k < widthBits) in e-class of id; else EG3_NONE.
456fn eg3_find_pow2_ec id ulong to ulong
457c = eg3_find id
458w = get eg3_width 0
459n = get eg3_cnt 0
460for i ulong n bound 512
461if eq get eg3_op i EG3_OP_CONST
462if eq eg3_find i c
463k = eg3_pow2_exp eg3_node_val i w
464if and ge k 1 lt k w
465ret k
466end
467end
468end
469end
470ret EG3_NONE
471end
472
473fn eg3_ec_has_bin ec ulong op ulong ca ulong cb ulong to bool
474n = get eg3_cnt 0
475for i ulong n bound 512
476if eq get eg3_op i op
477if eq eg3_find i ec
478if eq eg3_find get eg3_a i ca
479if eq eg3_find get eg3_b i cb
480ret true
481end
482end
483end
484end
485end
486ret false
487end
488
489fn eg3_ec_has_fma ec ulong ca ulong cb ulong cc ulong to bool
490n = get eg3_cnt 0
491for i ulong n bound 512
492if eq get eg3_op i EG3_OP_FMA
493if eq eg3_find i ec
494if eq eg3_find get eg3_a i ca
495if eq eg3_find get eg3_b i cb
496if eq eg3_find get eg3_c i cc
497ret true
498end
499end
500end
501end
502end
503end
504ret false
505end
506
507fn eg3_ec_has_assoc ec ulong op ulong cx ulong cy ulong cz ulong to bool
508n = get eg3_cnt 0
509for i ulong n bound 512
510if eq get eg3_op i op
511if eq eg3_find i ec
512if eq eg3_find get eg3_a i cx
513if eg3_ec_has_bin eg3_find get eg3_b i op cy cz
514ret true
515end
516end
517end
518end
519end
520ret false
521end
522
523note ---- All 18 Verified Equality-Saturation Rewrite Rules ---------------------------------
524
525note Phase A1: identity, absorption, and self-cancellation rules (3,4,5,6,11,13,14,15,16,17)
526fn eg3_apply_a1_node i ulong to bool
527op = get eg3_op i
528a = get eg3_a i
529b = get eg3_b i
530ch = false
531if eq op EG3_OP_ADD
532if eg3_has_const b 0
533if eg3_union i a 3
534ch = true
535end
536end
537if eg3_has_const a 0
538if eg3_union i b 4
539ch = true
540end
541end
542ret ch
543end
544if eq op EG3_OP_MUL
545if eg3_has_const b 1
546if eg3_union i a 5
547ch = true
548end
549end
550z = eg3_find_const b 0
551if ne z EG3_NONE
552if eg3_union i z 6
553ch = true
554end
555end
556ret ch
557end
558if eq op EG3_OP_AND
559if eq eg3_find a eg3_find b
560if eg3_union i a 11
561ch = true
562end
563end
564ret ch
565end
566if eq op EG3_OP_SUB
567if eg3_has_const b 0
568if eg3_union i a 13
569ch = true
570end
571end
572if eq eg3_find a eg3_find b
573z = eg3_add_const 0
574if eg3_union i z 14
575ch = true
576end
577end
578ret ch
579end
580if eq op EG3_OP_XOR
581if eq eg3_find a eg3_find b
582z = eg3_add_const 0
583if eg3_union i z 15
584ch = true
585end
586end
587ret ch
588end
589if eq op EG3_OP_OR
590if eg3_has_const b 0
591if eg3_union i a 16
592ch = true
593end
594end
595ret ch
596end
597if eq op EG3_OP_SHL
598if eg3_has_const b 0
599if eg3_union i a 17
600ch = true
601end
602end
603ret ch
604end
605ret false
606end
607
608fn eg3_apply_phase_a1 to bool
609ch = false
610n = get eg3_cnt 0
611for i ulong n bound 512
612if eg3_apply_a1_node i
613ch = true
614end
615end
616ret ch
617end
618
619note Rule 1: add(mul(a,b), c) -> fma(a,b,c)
620fn eg3_try_fma_l i ulong n ulong to bool
621ca = eg3_find get eg3_a i
622cnode = eg3_best_of get eg3_b i
623cc = eg3_find cnode
624ec = eg3_find i
625for j ulong n bound 512
626if eq get eg3_op j EG3_OP_MUL
627if eq eg3_find j ca
628ja = eg3_best_of get eg3_a j
629jb = eg3_best_of get eg3_b j
630if eg3_ec_has_fma ec eg3_find ja eg3_find jb cc
631ret false
632end
633nf = eg3_intern_fma ja jb cnode
634ret eg3_union i nf 1
635end
636end
637end
638ret false
639end
640
641note Rule 2: add(c, mul(a,b)) -> fma(a,b,c)
642fn eg3_try_fma_r i ulong n ulong to bool
643cb = eg3_find get eg3_b i
644cnode = eg3_best_of get eg3_a i
645cc = eg3_find cnode
646ec = eg3_find i
647for j ulong n bound 512
648if eq get eg3_op j EG3_OP_MUL
649if eq eg3_find j cb
650ja = eg3_best_of get eg3_a j
651jb = eg3_best_of get eg3_b j
652if eg3_ec_has_fma ec eg3_find ja eg3_find jb cc
653ret false
654end
655nf = eg3_intern_fma ja jb cnode
656ret eg3_union i nf 2
657end
658end
659end
660ret false
661end
662
663note Rule 12: fma(a,b,0) -> mul(a,b)
664fn eg3_try_fma_zero i ulong to bool
665if not eg3_has_const get eg3_c i 0
666ret false
667end
668a = eg3_best_of get eg3_a i
669b = eg3_best_of get eg3_b i
670if eg3_ec_has_bin eg3_find i EG3_OP_MUL eg3_find a eg3_find b
671ret false
672end
673nm = eg3_intern_bin EG3_OP_MUL a b
674ret eg3_union i nm 12
675end
676
677note Rule 18: mul(x, 1<<k) -> shl(x, k) for 1 <= k < width_bits
678fn eg3_try_mul_shl i ulong to bool
679k = eg3_find_pow2_ec get eg3_b i
680if eq k EG3_NONE
681ret false
682end
683x = eg3_best_of get eg3_a i
684ck = eg3_add_const k
685if eq ck EG3_NONE
686ret false
687end
688if eg3_ec_has_bin eg3_find i EG3_OP_SHL eg3_find x eg3_find ck
689ret false
690end
691ns = eg3_intern_bin EG3_OP_SHL x ck
692ret eg3_union i ns 18
693end
694
695note Phase A2a: FMA-zero unfusion and power-of-two shift strength reduction (12, 18)
696fn eg3_apply_phase_a2_sr to bool
697ch = false
698n = get eg3_cnt 0
699for i ulong n bound 512
700op = get eg3_op i
701if eq op EG3_OP_FMA
702if eg3_try_fma_zero i
703ch = true
704end
705end
706if eq op EG3_OP_MUL
707if eg3_try_mul_shl i
708ch = true
709end
710end
711end
712ret ch
713end
714
715note Phase A2b: FMA fusion (1, 2) using already-reduced best children
716fn eg3_apply_phase_a2_fma to bool
717ch = false
718n = get eg3_cnt 0
719for i ulong n bound 512
720if eq get eg3_op i EG3_OP_ADD
721if eg3_try_fma_l i n
722ch = true
723end
724if eg3_try_fma_r i n
725ch = true
726end
727end
728end
729ret ch
730end
731
732note Rule 7 (add_comm) and Rule 8 (mul_comm)
733fn eg3_try_comm i ulong op ulong rule ulong to bool
734if ge get eg3_cnt 0 EG3_SAT_CAP
735ret false
736end
737a = eg3_best_of get eg3_a i
738b = eg3_best_of get eg3_b i
739ca = eg3_find a
740cb = eg3_find b
741if eq ca cb
742ret false
743end
744if eg3_ec_has_bin eg3_find i op cb ca
745ret false
746end
747nv = eg3_intern_bin op b a
748ret eg3_union i nv rule
749end
750
751fn eg3_assoc_emit i ulong op ulong rule ulong x ulong y ulong z ulong to bool
752k = eg3_intern_bin op y z
753v = eg3_intern_bin op x k
754ret eg3_union i v rule
755end
756
757note Rule 9 (add_assoc) and Rule 10 (mul_assoc)
758fn eg3_try_assoc i ulong op ulong rule ulong n ulong to bool
759if ge add get eg3_cnt 0 2 EG3_SAT_CAP
760ret false
761end
762ca = eg3_find get eg3_a i
763z = eg3_best_of get eg3_b i
764for j ulong n bound 512
765if and ne j i eq get eg3_op j op
766if eq eg3_find j ca
767x = eg3_best_of get eg3_a j
768y = eg3_best_of get eg3_b j
769if not eg3_ec_has_assoc eg3_find i op eg3_find x eg3_find y eg3_find z
770if eg3_assoc_emit i op rule x y z
771ret true
772end
773end
774end
775end
776end
777ret false
778end
779
780note Phase B: commutativity and associativity exploration (7,8,9,10)
781fn eg3_apply_phase_b to bool
782ch = false
783n = get eg3_cnt 0
784for i ulong n bound 512
785if ge get eg3_cnt 0 EG3_SAT_CAP
786ret ch
787end
788op = get eg3_op i
789if eq op EG3_OP_ADD
790if eg3_try_comm i EG3_OP_ADD 7
791ch = true
792end
793if eg3_try_assoc i EG3_OP_ADD 9 n
794ch = true
795end
796end
797if eq op EG3_OP_MUL
798if eg3_try_comm i EG3_OP_MUL 8
799ch = true
800end
801if eg3_try_assoc i EG3_OP_MUL 10 n
802ch = true
803end
804end
805end
806ret ch
807end
808
809fn eg3_saturate_inner to bool
810any = false
811for r ulong 6
812c1 = eg3_apply_phase_a1
813eg3_compute_costs
814c2 = eg3_apply_phase_a2_sr
815eg3_compute_costs
816c3 = eg3_apply_phase_a2_fma
817eg3_compute_costs
818if or c1 or c2 c3
819any = true
820else
821break
822end
823end
824ret any
825end
826
827fn eg3_saturate maxOuter ulong to void
828eg3_compute_costs
829z = eg3_saturate_inner
830for outer ulong maxOuter bound 16
831cb = eg3_apply_phase_b
832eg3_compute_costs
833if not cb
834break
835end
836ca = eg3_saturate_inner
837if not ca
838break
839end
840end
841z2 = eg3_saturate_inner
842end
843
844note ---- EGP3 Binary Structural Proof Certificate Serializer -------------------------------
845
846fn eg3_put_u32 off ulong v ulong to void
847put eg3_buf off uchar and v 255
848put eg3_buf add off 1 uchar and shr v 8 255
849put eg3_buf add off 2 uchar and shr v 16 255
850put eg3_buf add off 3 uchar and shr v 24 255
851end
852
853fn eg3_get_u32 off ulong to ulong
854b0 = ulong get eg3_buf off
855b1 = shl ulong get eg3_buf add off 1 8
856b2 = shl ulong get eg3_buf add off 2 16
857b3 = shl ulong get eg3_buf add off 3 24
858ret or or b0 b1 or b2 b3
859end
860
861fn eg3_cert_init to void
862put eg3_buf 0 69
863put eg3_buf 1 71
864put eg3_buf 2 80
865put eg3_buf 3 51
866eg3_put_u32 4 0
867put eg3_bpos 0 8
868put eg3_certs 0 0
869put eg3_all_rmask 0 0
870end
871
872fn eg3_cert_append fnIdx ulong blkIdx ulong rootBefore ulong rootAfter ulong to bool
873nc = get eg3_cnt 0
874nt = get eg3_trn 0
875p = get eg3_bpos 0
876need = add 28 add mul nc 32 mul nt 12
877if gt add p need EG3_BUF_CAP
878ret false
879end
880eg3_put_u32 p fnIdx
881eg3_put_u32 add p 4 blkIdx
882eg3_put_u32 add p 8 get eg3_width 0
883eg3_put_u32 add p 12 nc
884eg3_put_u32 add p 16 rootBefore
885eg3_put_u32 add p 20 rootAfter
886eg3_put_u32 add p 24 nt
887p = add p 28
888for i ulong nc bound 512
889eg3_put_u32 p get eg3_op i
890eg3_put_u32 add p 4 get eg3_a i
891eg3_put_u32 add p 8 get eg3_b i
892eg3_put_u32 add p 12 get eg3_c i
893eg3_put_u32 add p 16 get eg3_vlo i
894eg3_put_u32 add p 20 get eg3_vhi i
895eg3_put_u32 add p 24 get eg3_w i
896eg3_put_u32 add p 28 get eg3_fl i
897p = add p 32
898end
899for i ulong nt bound 512
900eg3_put_u32 p get eg3_tr_rule i
901eg3_put_u32 add p 4 get eg3_tr_lhs i
902eg3_put_u32 add p 8 get eg3_tr_rhs i
903p = add p 12
904end
905put eg3_bpos 0 p
906cc = add get eg3_certs 0 1
907put eg3_certs 0 cc
908eg3_put_u32 4 cc
909put eg3_all_rmask 0 or get eg3_all_rmask 0 get eg3_rmask 0
910ret true
911end
912
913fn eg3_write_fd fd int to bool
914n = get eg3_bpos 0
915w = write fd eg3_buf 0 n
916ret eq w long n
917end
918
919note ---- ΩIR Bridge (reads seed/ng/src/ir.om tables, strictly refuses token/convergent) ---
920
921fn eg3_ir_ty_width ty uint to ulong
922if or eq ty 4 eq ty 8
923ret 32
924end
925if or eq ty 5 eq ty 9
926ret 64
927end
928ret 0
929end
930
931note Strict eligibility filter for ΩIR ops:
932note - Refuses convergent GPU ops (sync=44, shfl=45, mmald=104, mmast=105, mma=106)
933note - Refuses all token-bearing ops (i_rtk != 0 or ir_htok != 0: load, store, chk.*, call, host, terminators)
934note - Refuses non-32/64 integer types (e.g. f32, f64, tok, void, vectors)
935note - Accepts only pure class-P wrapping arithmetic/bitwise ops
936fn eg3_ir_opcode_check op uint ty uint to ulong
937ir_rinit
938if or eq op 44 or eq op 45 or eq op 104 or eq op 105 eq op 106
939ret EG3_REFUSE_CONVERGENT
940end
941if or ne get i_rtk op 0 ne ir_htok op 0
942ret EG3_REFUSE_TOKEN
943end
944if eq eg3_ir_ty_width ty 0
945ret EG3_REFUSE_TYPE
946end
947if eq op 1
948ret EG3_ALLOW
949end
950if or eq op 10 or eq op 11 eq op 12
951ret EG3_ALLOW
952end
953if or eq op 16 or eq op 17 or eq op 18 eq op 20
954ret EG3_ALLOW
955end
956if eq op 21
957if or eq ty 4 eq ty 5
958ret EG3_ALLOW
959end
960ret EG3_REFUSE_TYPE
961end
962if eq op 46
963ret EG3_ALLOW
964end
965ret EG3_REFUSE_UNSUP
966end
967
968fn eg3_ir_op_check o uint to ulong
969ret eg3_ir_opcode_check uint get i_oop o get i_oty o
970end
971
972fn eg3_ir_to_eg_op op uint to ulong
973if eq op 1
974ret EG3_OP_CONST
975end
976if eq op 10
977ret EG3_OP_ADD
978end
979if eq op 11
980ret EG3_OP_SUB
981end
982if eq op 12
983ret EG3_OP_MUL
984end
985if eq op 46
986ret EG3_OP_FMA
987end
988if eq op 20
989ret EG3_OP_SHL
990end
991if eq op 21
992ret EG3_OP_SHR
993end
994if eq op 16
995ret EG3_OP_AND
996end
997if eq op 17
998ret EG3_OP_OR
999end
1000if eq op 18
1001ret EG3_OP_XOR
1002end
1003ret 0
1004end
1005
1006fn eg3_ir_val_to_node v uint to ulong
1007n = get eg3_ir_vn 0
1008for i ulong n bound 256
1009if eq get eg3_ir_val i v
1010ret get eg3_ir_nod i
1011end
1012end
1013sid = get eg3_ir_sn 0
1014put eg3_ir_sn 0 add sid 1
1015nid = eg3_add_ssa sid
1016if lt n 256
1017put eg3_ir_val n v
1018put eg3_ir_nod n nid
1019put eg3_ir_vn 0 add n 1
1020end
1021ret nid
1022end
1023
1024fn eg3_ir_bind_val v uint nid ulong to void
1025if eq v 0
1026ret
1027end
1028n = get eg3_ir_vn 0
1029if lt n 256
1030put eg3_ir_val n v
1031put eg3_ir_nod n nid
1032put eg3_ir_vn 0 add n 1
1033end
1034end
1035
1036fn eg3_ir_import_op o uint to ulong
1037op = uint get i_oop o
1038if eq op 1
1039nid = eg3_add_const get i_oim o
1040eg3_ir_bind_val get i_ors o nid
1041ret nid
1042end
1043eop = eg3_ir_to_eg_op op
1044if eq op 46
1045na = eg3_ir_val_to_node ir_arg o 0
1046nb = eg3_ir_val_to_node ir_arg o 1
1047nc = eg3_ir_val_to_node ir_arg o 2
1048nid = eg3_add_fma na nb nc
1049eg3_ir_bind_val get i_ors o nid
1050ret nid
1051end
1052na = eg3_ir_val_to_node ir_arg o 0
1053nb = eg3_ir_val_to_node ir_arg o 1
1054nid = eg3_add_bin eop na nb
1055eg3_ir_bind_val get i_ors o nid
1056ret nid
1057end
1058
1059fn eg3_ir_bump_ref chk ulong to void
1060if eq chk EG3_REFUSE_TOKEN
1061put eg3_ir_stat 0 add get eg3_ir_stat 0 1
1062ret
1063end
1064if eq chk EG3_REFUSE_CONVERGENT
1065put eg3_ir_stat 1 add get eg3_ir_stat 1 1
1066ret
1067end
1068if eq chk EG3_REFUSE_TYPE
1069put eg3_ir_stat 2 add get eg3_ir_stat 2 1
1070ret
1071end
1072put eg3_ir_stat 3 add get eg3_ir_stat 3 1
1073end
1074
1075fn eg3_ir_extract_blk b uint widthHint ulong to ulong
1076w = widthHint
1077if ne w 0
1078eg3_reset w
1079end
1080lastRoot = EG3_NONE
1081lastAny = EG3_NONE
1082o = get i_bo1 b
1083while ne o 0 bound 4096
1084chk = eg3_ir_op_check o
1085if eq chk EG3_ALLOW
1086ow = eg3_ir_ty_width get i_oty o
1087if eq w 0
1088w = ow
1089eg3_reset w
1090end
1091if eq ow w
1092nid = eg3_ir_import_op o
1093if ne nid EG3_NONE
1094put eg3_ir_stat 4 add get eg3_ir_stat 4 1
1095lastAny = nid
1096if ne get i_oop o 1
1097lastRoot = nid
1098end
1099end
1100end
1101else
1102eg3_ir_bump_ref chk
1103end
1104o = get i_onx o
1105end
1106if eq lastRoot EG3_NONE
1107ret lastAny
1108end
1109ret lastRoot
1110end
1111
1112note Extract pure class-P arithmetic/bitwise ops from ΩIR block b, saturate via e-graph,
1113note record EGP3 cert (when emitCert is true and trace/reduction holds), and return cost delta.
1114fn eg3_opt_ir_block fnIdx ulong blkIdx ulong b uint widthHint ulong emitCert bool to ulong
1115for i ulong 16
1116put eg3_ir_stat i 0
1117end
1118put eg3_ir_vn 0 0
1119put eg3_ir_sn 0 0
1120lastRoot = eg3_ir_extract_blk b widthHint
1121if eq lastRoot EG3_NONE
1122ret 0
1123end
1124put eg3_ir_stat 10 get eg3_width 0
1125eg3_compute_costs
1126cbef = eg3_get_cost lastRoot
1127eg3_saturate 2
1128caft = eg3_get_cost lastRoot
1129raft = eg3_get_best lastRoot
1130put eg3_ir_stat 5 cbef
1131put eg3_ir_stat 6 caft
1132put eg3_ir_stat 7 lastRoot
1133put eg3_ir_stat 8 raft
1134put eg3_ir_stat 9 get eg3_trn 0
1135if and emitCert and gt get eg3_trn 0 0 lt caft cbef
1136if eg3_cert_append fnIdx blkIdx lastRoot raft
1137put eg3_ir_stat 11 1
1138end
1139end
1140if gt cbef caft
1141ret sub cbef caft
1142end
1143ret 0
1144end