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 / sm121_pe.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note seed/ng/target/sm121_pe.om: Runtime Shape Partial Evaluator + Proof-Checked SM121 Contract Emitter |
| 3 | note + Minimal 64-bit ELF64 container emitter (decoupled from ISA: EM_AARCH64=183, EM_X86_64=62, EM_RISCV=243). |
| 4 | |
| 5 | note Execution regimes derived automatically by partial evaluation on (M, K, N): |
| 6 | const PE_REGIME_GEMV_4R ulong 1 |
| 7 | const PE_REGIME_REG_TILE_M ulong 2 |
| 8 | const PE_REGIME_HMMA_16816 ulong 3 |
| 9 | |
| 10 | note Output of peSpecializeW4Gemm: [0]=regime, [1]=reqSmem, [2]=reqRegs, [3]=gridX, [4]=cacheSlot, [5]=status |
| 11 | global peOut ulong 8 |
| 12 | |
| 13 | note ELF64 output buffer (small static executable: 64B ehdr + 56B phdr + code) |
| 14 | global elfBuf uchar 4096 |
| 15 | global elfLen ulong 1 |
| 16 | |
| 17 | fn peMakeShapeSig M ulong K ulong N ulong to ulong |
| 18 | ret or shl M 32 or shl K 16 N |
| 19 | end |
| 20 | |
| 21 | note Antibody #14 verifier over emitted 128-bit SM121 SASS buffer: every non-EXIT insn must have stall >= 6! |
| 22 | fn peVerifySm121Antibodies to bool |
| 23 | n = get isLen 0 |
| 24 | if or eq n 0 ne rem n 16 0 |
| 25 | ret false |
| 26 | end |
| 27 | insns = div n 16 |
| 28 | for i ulong insns bound 64 |
| 29 | off = mul i 16 |
| 30 | hi = ulong get isBuf add off 8 |
| 31 | if lt hi 6 |
| 32 | ret false |
| 33 | end |
| 34 | end |
| 35 | ret true |
| 36 | end |
| 37 | |
| 38 | fn pePlanRegime M ulong K ulong N ulong to void |
| 39 | reg = PE_REGIME_GEMV_4R |
| 40 | reqSmem = mul K 4 |
| 41 | reqRegs = ulong 40 |
| 42 | reqMma = ulong 0 |
| 43 | reqFeat = ulong 0 |
| 44 | gx = div add N 31 32 |
| 45 | if and ge M 2 le M 8 |
| 46 | reg = PE_REGIME_REG_TILE_M |
| 47 | reqSmem = mul mul M K 4 |
| 48 | reqRegs = ulong 56 |
| 49 | gx = div add N 31 32 |
| 50 | end |
| 51 | if gt M 8 |
| 52 | reg = PE_REGIME_HMMA_16816 |
| 53 | reqSmem = ulong 32768 |
| 54 | reqRegs = ulong 64 |
| 55 | reqMma = MMA_16816_BF16 |
| 56 | reqFeat = FEAT_HMMA16816 |
| 57 | gx = div add N 63 64 |
| 58 | end |
| 59 | put peOut 0 reg |
| 60 | put peOut 1 reqSmem |
| 61 | put peOut 2 reqRegs |
| 62 | put peOut 3 gx |
| 63 | tdSetReq EXEC_SIMT_WARP reqSmem reqRegs 32 reqMma reqFeat |
| 64 | end |
| 65 | |
| 66 | note Runtime Shape Partial Evaluator for W4-B32 GEMM/GEMV on TargetDesc tid (e.g. TID_SM121): |
| 67 | fn peSpecializeW4Gemm semId ulong tid ulong M ulong K ulong N ulong to ulong |
| 68 | feat = tdGet tid TDF_FEAT |
| 69 | sig = peMakeShapeSig M K N |
| 70 | hit = tdCacheLookup semId tid feat sig |
| 71 | if ne hit 999999 |
| 72 | put peOut 4 hit |
| 73 | put peOut 5 TDK_OK |
| 74 | ret TDK_OK |
| 75 | end |
| 76 | pePlanRegime M K N |
| 77 | rc = tdCheckKernelContract tid |
| 78 | put peOut 4 999999 |
| 79 | put peOut 5 rc |
| 80 | if ne rc TDK_OK |
| 81 | ret rc |
| 82 | end |
| 83 | if not isCompileTestFn tid |
| 84 | put peOut 5 99 |
| 85 | ret 99 |
| 86 | end |
| 87 | if not peVerifySm121Antibodies |
| 88 | put peOut 5 98 |
| 89 | ret 98 |
| 90 | end |
| 91 | codePtr = add 65536 mul get peOut 0 4096 |
| 92 | slot = tdCacheInstall semId tid feat sig codePtr 7 |
| 93 | put peOut 4 slot |
| 94 | if eq slot 999999 |
| 95 | put peOut 5 97 |
| 96 | ret 97 |
| 97 | end |
| 98 | ret TDK_OK |
| 99 | end |
| 100 | |
| 101 | note ---- Orthogonal ELF64 Container Plugin (works identically for AArch64, x86-64, and RV64GC!) ---- |
| 102 | fn elfPutU8 off ulong v ulong to void |
| 103 | put elfBuf off uchar and v 255 |
| 104 | end |
| 105 | |
| 106 | fn elfPutU16 off ulong v ulong to void |
| 107 | elfPutU8 off v |
| 108 | elfPutU8 add off 1 shr v 8 |
| 109 | end |
| 110 | |
| 111 | fn elfPutU32 off ulong v ulong to void |
| 112 | elfPutU16 off v |
| 113 | elfPutU16 add off 2 shr v 16 |
| 114 | end |
| 115 | |
| 116 | fn elfPutU64 off ulong v ulong to void |
| 117 | elfPutU32 off v |
| 118 | elfPutU32 add off 4 shr v 32 |
| 119 | end |
| 120 | |
| 121 | fn elfMachineForIsa isa ulong to ulong |
| 122 | if eq isa ISA_A64 |
| 123 | ret 183 |
| 124 | end |
| 125 | if eq isa ISA_X64 |
| 126 | ret 62 |
| 127 | end |
| 128 | if eq isa ISA_RV64 |
| 129 | ret 243 |
| 130 | end |
| 131 | ret 0 |
| 132 | end |
| 133 | |
| 134 | note Emit a minimal static Linux ELF64 executable wrapping isBuf[0..isLen) at vaddr 0x400000 + 120! |
| 135 | fn elfEmitStatic tid ulong to ulong |
| 136 | em = elfMachineForIsa tdGet tid TDF_ISA |
| 137 | if eq em 0 |
| 138 | ret 0 |
| 139 | end |
| 140 | clen = get isLen 0 |
| 141 | tot = add 120 clen |
| 142 | for i ulong tot bound 4096 |
| 143 | put elfBuf i 0 |
| 144 | end |
| 145 | note ELF64 header (64 bytes) |
| 146 | elfPutU8 0 127 |
| 147 | elfPutU8 1 69 |
| 148 | elfPutU8 2 76 |
| 149 | elfPutU8 3 70 |
| 150 | elfPutU8 4 2 |
| 151 | elfPutU8 5 1 |
| 152 | elfPutU8 6 1 |
| 153 | elfPutU16 16 2 |
| 154 | elfPutU16 18 em |
| 155 | elfPutU32 20 1 |
| 156 | elfPutU64 24 4194424 |
| 157 | elfPutU64 32 64 |
| 158 | elfPutU16 52 64 |
| 159 | elfPutU16 54 56 |
| 160 | elfPutU16 56 1 |
| 161 | note PT_LOAD program header (56 bytes at offset 64): PF_R | PF_X (5) |
| 162 | elfPutU32 64 1 |
| 163 | elfPutU32 68 5 |
| 164 | elfPutU64 72 0 |
| 165 | elfPutU64 80 4194304 |
| 166 | elfPutU64 88 4194304 |
| 167 | elfPutU64 96 tot |
| 168 | elfPutU64 104 tot |
| 169 | elfPutU64 112 4096 |
| 170 | for i ulong clen bound 1024 |
| 171 | put elfBuf add 120 i get isBuf i |
| 172 | end |
| 173 | put elfLen 0 tot |
| 174 | ret tot |
| 175 | end |
| 176 | |
| 177 | note Emit tiny standalone exit(36) test binary for AArch64 and x86-64 using our register-allocated emitters! |
| 178 | note Computes (7 + 5) * 3 = 36 in register 0, then invokes Linux exit(reg0)! |
| 179 | fn elfBuildExecDemo tid ulong to ulong |
| 180 | isa = tdGet tid TDF_ISA |
| 181 | isBufReset |
| 182 | if eq isa ISA_A64 |
| 183 | note mov x0, #7 (0xd28000e0); mov x1, #5 (0xd28000a1); mov x2, #3 (0xd2800062) |
| 184 | isEmitU32 3531604192 |
| 185 | isEmitU32 3531604129 |
| 186 | isEmitU32 3531604066 |
| 187 | note Shared ISel: ADD x0,x0,x1; MUL x0,x0,x2 |
| 188 | ok = isEmitOp tid OIR_ADD 0 0 1 |
| 189 | ok = isEmitOp tid OIR_MUL 0 0 2 |
| 190 | note mov x8, #93 (SYS_exit=93: 0xd2800ba8); svc #0 (0xd4000001) |
| 191 | isEmitU32 3531606952 |
| 192 | isEmitU32 3556769793 |
| 193 | ret elfEmitStatic tid |
| 194 | end |
| 195 | if eq isa ISA_X64 |
| 196 | note mov eax, 7 (b8 07 00 00 00); mov edi, 5 (bf 05 00 00 00); mov esi, 3 (be 03 00 00 00) |
| 197 | isEmitU8 184 |
| 198 | isEmitU32 7 |
| 199 | isEmitU8 191 |
| 200 | isEmitU32 5 |
| 201 | isEmitU8 190 |
| 202 | isEmitU32 3 |
| 203 | note Shared ISel: add rax, rdi; imul rax, rsi |
| 204 | ok = isEmitOp tid OIR_ADD 0 0 1 |
| 205 | ok = isEmitOp tid OIR_MUL 0 0 2 |
| 206 | note mov rdi, rax (48 89 c7); mov eax, 60 (SYS_exit=60: b8 3c 00 00 00); syscall (0f 05) |
| 207 | isEmitU8 72 |
| 208 | isEmitU8 137 |
| 209 | isEmitU8 199 |
| 210 | isEmitU8 184 |
| 211 | isEmitU32 60 |
| 212 | isEmitU8 15 |
| 213 | isEmitU8 5 |
| 214 | ret elfEmitStatic tid |
| 215 | end |
| 216 | ret 0 |
| 217 | end |