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