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 / target / isel.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note seed/ng/target/isel.om: Shared cost-guided declarative ISA pattern selector + real machine-code emitters |
| 3 | note for AArch64, register-allocated x86-64, RV64GC, and SM121 (with compile-time SMEM/hazard verification). |
| 4 | |
| 5 | note Abstract ΩIR opcodes supported by the multi-ISA selector: |
| 6 | const OIR_ADD ulong 1 |
| 7 | const OIR_SUB ulong 2 |
| 8 | const OIR_MUL ulong 3 |
| 9 | const OIR_FMA ulong 4 |
| 10 | const OIR_SHA256 ulong 5 |
| 11 | const OIR_RET ulong 6 |
| 12 | |
| 13 | note Rule table (up to 64 rules): (isa, oirOp, featGuard, cost, encId) |
| 14 | const IS_MAX_RULES ulong 64 |
| 15 | global isCnt ulong 1 |
| 16 | global isIsa ulong 64 |
| 17 | global isOp ulong 64 |
| 18 | global isFeat ulong 64 |
| 19 | global isCost ulong 64 |
| 20 | global isEnc ulong 64 |
| 21 | |
| 22 | note Output byte buffer for emitted machine code (shared across all 4 ISAs) |
| 23 | global isBuf uchar 1024 |
| 24 | global isLen ulong 1 |
| 25 | |
| 26 | fn isBufReset to void |
| 27 | put isLen 0 0 |
| 28 | end |
| 29 | |
| 30 | fn isEmitU8 b ulong to void |
| 31 | p = get isLen 0 |
| 32 | put isBuf p uchar and b 255 |
| 33 | put isLen 0 add p 1 |
| 34 | end |
| 35 | |
| 36 | fn isEmitU32 w ulong to void |
| 37 | isEmitU8 w |
| 38 | isEmitU8 shr w 8 |
| 39 | isEmitU8 shr w 16 |
| 40 | isEmitU8 shr w 24 |
| 41 | end |
| 42 | |
| 43 | fn isEmitU64 w ulong to void |
| 44 | isEmitU32 w |
| 45 | isEmitU32 shr w 32 |
| 46 | end |
| 47 | |
| 48 | fn isAddRule isa ulong op ulong feat ulong cost ulong encId ulong to void |
| 49 | i = get isCnt 0 |
| 50 | put isIsa i isa |
| 51 | put isOp i op |
| 52 | put isFeat i feat |
| 53 | put isCost i cost |
| 54 | put isEnc i encId |
| 55 | put isCnt 0 add i 1 |
| 56 | end |
| 57 | |
| 58 | note Initialize declarative ISA rule tables across AArch64, x86-64, RV64GC, and SM121! |
| 59 | fn isInitRules to void |
| 60 | put isCnt 0 0 |
| 61 | note AArch64 rules (encId 100..119): |
| 62 | isAddRule ISA_A64 OIR_ADD 0 1 100 |
| 63 | isAddRule ISA_A64 OIR_SUB 0 1 101 |
| 64 | isAddRule ISA_A64 OIR_MUL 0 3 102 |
| 65 | isAddRule ISA_A64 OIR_FMA 0 6 103 |
| 66 | isAddRule ISA_A64 OIR_FMA FEAT_FMA 3 104 |
| 67 | isAddRule ISA_A64 OIR_SHA256 0 40 105 |
| 68 | isAddRule ISA_A64 OIR_SHA256 FEAT_SHA 4 106 |
| 69 | isAddRule ISA_A64 OIR_RET 0 1 107 |
| 70 | |
| 71 | note x86-64 rules (encId 200..219): |
| 72 | isAddRule ISA_X64 OIR_ADD 0 1 200 |
| 73 | isAddRule ISA_X64 OIR_SUB 0 1 201 |
| 74 | isAddRule ISA_X64 OIR_MUL 0 3 202 |
| 75 | isAddRule ISA_X64 OIR_FMA 0 7 203 |
| 76 | isAddRule ISA_X64 OIR_FMA FEAT_FMA 4 204 |
| 77 | isAddRule ISA_X64 OIR_SHA256 0 42 205 |
| 78 | isAddRule ISA_X64 OIR_SHA256 FEAT_SHA 5 206 |
| 79 | isAddRule ISA_X64 OIR_RET 0 1 207 |
| 80 | |
| 81 | note RV64GC rules (encId 300..319): |
| 82 | isAddRule ISA_RV64 OIR_ADD 0 1 300 |
| 83 | isAddRule ISA_RV64 OIR_SUB 0 1 301 |
| 84 | isAddRule ISA_RV64 OIR_MUL 0 3 302 |
| 85 | isAddRule ISA_RV64 OIR_FMA 0 7 303 |
| 86 | isAddRule ISA_RV64 OIR_FMA FEAT_FMA 4 304 |
| 87 | isAddRule ISA_RV64 OIR_SHA256 0 48 305 |
| 88 | isAddRule ISA_RV64 OIR_RET 0 1 307 |
| 89 | |
| 90 | note SM121 Blackwell GPU rules (encId 400..419): |
| 91 | isAddRule ISA_SM121 OIR_ADD 0 4 400 |
| 92 | isAddRule ISA_SM121 OIR_SUB 0 4 401 |
| 93 | isAddRule ISA_SM121 OIR_MUL 0 4 402 |
| 94 | isAddRule ISA_SM121 OIR_FMA FEAT_FMA 4 404 |
| 95 | isAddRule ISA_SM121 OIR_RET 0 4 407 |
| 96 | end |
| 97 | |
| 98 | note Cost-guided selector: picks lowest-cost legal rule for (TargetDesc tid, oirOp)! |
| 99 | fn isSelect tid ulong op ulong to ulong |
| 100 | isa = tdGet tid TDF_ISA |
| 101 | feat = tdGet tid TDF_FEAT |
| 102 | bestEnc = ulong 999999 |
| 103 | bestCost = ulong 999999 |
| 104 | n = get isCnt 0 |
| 105 | for i ulong n bound 64 |
| 106 | if and eq get isIsa i isa eq get isOp i op |
| 107 | req = get isFeat i |
| 108 | if eq and feat req req |
| 109 | c = get isCost i |
| 110 | if lt c bestCost |
| 111 | bestCost = c |
| 112 | bestEnc = get isEnc i |
| 113 | end |
| 114 | end |
| 115 | end |
| 116 | end |
| 117 | ret bestEnc |
| 118 | end |
| 119 | |
| 120 | note ---- Real Machine-Code Emitters (all 4 ISAs from the same (op, rd, rn, rm)!) ---- |
| 121 | |
| 122 | note 1. AArch64 encoder (Arm ARM DDI 0487): ADD/SUB/MUL/RET 32-bit LE words |
| 123 | fn isEmitA64 encId ulong rd ulong rn ulong rm ulong to bool |
| 124 | if eq encId 100 |
| 125 | note ADD Xd, Xn, Xm: 0x8b000000 | (rm<<16) | (rn<<5) | rd |
| 126 | w = or 2332033024 or shl rm 16 or shl rn 5 rd |
| 127 | isEmitU32 w |
| 128 | ret true |
| 129 | end |
| 130 | if eq encId 101 |
| 131 | note SUB Xd, Xn, Xm: 0xcb000000 | (rm<<16) | (rn<<5) | rd |
| 132 | w = or 3405774848 or shl rm 16 or shl rn 5 rd |
| 133 | isEmitU32 w |
| 134 | ret true |
| 135 | end |
| 136 | if eq encId 102 |
| 137 | note MUL Xd, Xn, Xm (MADD Xd, Xn, Xm, XZR): 0x9b007c00 | (rm<<16) | (rn<<5) | rd |
| 138 | w = or 2600500224 or shl rm 16 or shl rn 5 rd |
| 139 | isEmitU32 w |
| 140 | ret true |
| 141 | end |
| 142 | if eq encId 107 |
| 143 | note RET (X30): 0xd65f03c0 |
| 144 | isEmitU32 3596551104 |
| 145 | ret true |
| 146 | end |
| 147 | ret false |
| 148 | end |
| 149 | |
| 150 | note 2. Register-Allocated x86-64 encoder (real REX.W + opcode + ModRM, handling 2-addr move when rd != rn!) |
| 151 | note Logical reg mapping: 0=RAX(0), 1=RDI(7), 2=RSI(6), 3=RDX(2), 4=RCX(1), 5=R8(8), 6=R9(9) |
| 152 | fn isX64Phys r ulong to ulong |
| 153 | if eq r 0 |
| 154 | ret 0 |
| 155 | end |
| 156 | if eq r 1 |
| 157 | ret 7 |
| 158 | end |
| 159 | if eq r 2 |
| 160 | ret 6 |
| 161 | end |
| 162 | if eq r 3 |
| 163 | ret 2 |
| 164 | end |
| 165 | if eq r 4 |
| 166 | ret 1 |
| 167 | end |
| 168 | ret add r 3 |
| 169 | end |
| 170 | |
| 171 | fn isX64Rex rReg ulong rmReg ulong to ulong |
| 172 | rex = ulong 72 |
| 173 | if ge rReg 8 |
| 174 | rex = or rex 4 |
| 175 | end |
| 176 | if ge rmReg 8 |
| 177 | rex = or rex 1 |
| 178 | end |
| 179 | ret rex |
| 180 | end |
| 181 | |
| 182 | fn isX64ModRm rReg ulong rmReg ulong to ulong |
| 183 | ret or 192 or shl and rReg 7 3 and rmReg 7 |
| 184 | end |
| 185 | |
| 186 | fn isEmitX64Mov dst ulong src ulong to void |
| 187 | if eq dst src |
| 188 | ret |
| 189 | end |
| 190 | isEmitU8 isX64Rex src dst |
| 191 | isEmitU8 137 |
| 192 | isEmitU8 isX64ModRm src dst |
| 193 | end |
| 194 | |
| 195 | fn isEmitX64 encId ulong rd ulong rn ulong rm ulong to bool |
| 196 | pd = isX64Phys rd |
| 197 | pn = isX64Phys rn |
| 198 | pm = isX64Phys rm |
| 199 | if eq encId 200 |
| 200 | note 2-addr lowering: mov pd, pn (if needed); add pd, pm (REX.W 0x01 /r) |
| 201 | isEmitX64Mov pd pn |
| 202 | isEmitU8 isX64Rex pm pd |
| 203 | isEmitU8 1 |
| 204 | isEmitU8 isX64ModRm pm pd |
| 205 | ret true |
| 206 | end |
| 207 | if eq encId 201 |
| 208 | note sub pd, pm (REX.W 0x29 /r) |
| 209 | isEmitX64Mov pd pn |
| 210 | isEmitU8 isX64Rex pm pd |
| 211 | isEmitU8 41 |
| 212 | isEmitU8 isX64ModRm pm pd |
| 213 | ret true |
| 214 | end |
| 215 | if eq encId 202 |
| 216 | note imul pd, pm (REX.W 0x0F 0xAF /r -- note ModRM reg=pd, rm=pm) |
| 217 | isEmitX64Mov pd pn |
| 218 | isEmitU8 isX64Rex pd pm |
| 219 | isEmitU8 15 |
| 220 | isEmitU8 175 |
| 221 | isEmitU8 isX64ModRm pd pm |
| 222 | ret true |
| 223 | end |
| 224 | if eq encId 207 |
| 225 | note ret (0xC3) |
| 226 | isEmitU8 195 |
| 227 | ret true |
| 228 | end |
| 229 | ret false |
| 230 | end |
| 231 | |
| 232 | note 3. RV64GC encoder (RISC-V Unprivileged Spec: R-type ADD/SUB/MUL, I-type JALR x0, 0(x1)) |
| 233 | note Logical reg mapping: 0=a0(x10), 1=a1(x11), 2=a2(x12), 3=a3(x13) |
| 234 | fn isRvPhys r ulong to ulong |
| 235 | ret add 10 r |
| 236 | end |
| 237 | |
| 238 | fn isRvRtype f7 ulong pd ulong pn ulong pm ulong to ulong |
| 239 | w = or shl f7 25 shl pm 20 |
| 240 | w = or w shl pn 15 |
| 241 | w = or w shl pd 7 |
| 242 | ret or w 51 |
| 243 | end |
| 244 | |
| 245 | fn isEmitRv64 encId ulong rd ulong rn ulong rm ulong to bool |
| 246 | pd = isRvPhys rd |
| 247 | pn = isRvPhys rn |
| 248 | pm = isRvPhys rm |
| 249 | if eq encId 300 |
| 250 | isEmitU32 isRvRtype 0 pd pn pm |
| 251 | ret true |
| 252 | end |
| 253 | if eq encId 301 |
| 254 | isEmitU32 isRvRtype 32 pd pn pm |
| 255 | ret true |
| 256 | end |
| 257 | if eq encId 302 |
| 258 | isEmitU32 isRvRtype 1 pd pn pm |
| 259 | ret true |
| 260 | end |
| 261 | if eq encId 307 |
| 262 | note RET (JALR x0, 0(ra=x1)): 0x00008067 |
| 263 | isEmitU32 32871 |
| 264 | ret true |
| 265 | end |
| 266 | ret false |
| 267 | end |
| 268 | |
| 269 | note 4. SM121 Blackwell GPU 128-bit SASS word emitter (low 64b op + high 64b control/stall word) |
| 270 | fn isEmitSm121 encId ulong rd ulong rn ulong rm ulong to bool |
| 271 | if eq encId 400 |
| 272 | note SM121 IADD3 Rd, Rn, Rm, RZ + safe stall=6 control word (Antibody #14 floor!) |
| 273 | lo = or 2064 or shl rd 16 or shl rn 24 shl rm 32 |
| 274 | hi = ulong 6 |
| 275 | isEmitU64 lo |
| 276 | isEmitU64 hi |
| 277 | ret true |
| 278 | end |
| 279 | if eq encId 402 |
| 280 | note SM121 IMAD Rd, Rn, Rm, RZ + safe stall=6 control word |
| 281 | lo = or 2084 or shl rd 16 or shl rn 24 shl rm 32 |
| 282 | hi = ulong 6 |
| 283 | isEmitU64 lo |
| 284 | isEmitU64 hi |
| 285 | ret true |
| 286 | end |
| 287 | if eq encId 407 |
| 288 | note SM121 EXIT (0x000000000000794d) + stall=15 |
| 289 | isEmitU64 31053 |
| 290 | isEmitU64 15 |
| 291 | ret true |
| 292 | end |
| 293 | ret false |
| 294 | end |
| 295 | |
| 296 | note Universal dispatch: lower one ΩIR op for ANY TargetDesc tid! |
| 297 | fn isEmitOp tid ulong op ulong rd ulong rn ulong rm ulong to bool |
| 298 | enc = isSelect tid op |
| 299 | if eq enc 999999 |
| 300 | ret false |
| 301 | end |
| 302 | isa = tdGet tid TDF_ISA |
| 303 | if eq isa ISA_A64 |
| 304 | ret isEmitA64 enc rd rn rm |
| 305 | end |
| 306 | if eq isa ISA_X64 |
| 307 | ret isEmitX64 enc rd rn rm |
| 308 | end |
| 309 | if eq isa ISA_RV64 |
| 310 | ret isEmitRv64 enc rd rn rm |
| 311 | end |
| 312 | if eq isa ISA_SM121 |
| 313 | ret isEmitSm121 enc rd rn rm |
| 314 | end |
| 315 | ret false |
| 316 | end |
| 317 | |
| 318 | note Emit canonical 3-op ΩIR kernel `f(a, b, c) = (a + b) * c; ret` for ANY TargetDesc! |
| 319 | fn isCompileTestFn tid ulong to bool |
| 320 | isBufReset |
| 321 | if not isEmitOp tid OIR_ADD 0 0 1 |
| 322 | ret false |
| 323 | end |
| 324 | if not isEmitOp tid OIR_MUL 0 0 2 |
| 325 | ret false |
| 326 | end |
| 327 | ret isEmitOp tid OIR_RET 0 0 0 |
| 328 | end |