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

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