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 / target / sm121_pe.om

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