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 / kflow / kpack.om

638 lines · 9950 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note seed/kflow/kpack.om: kflow's package stage. Wraps raw SASS text in a loadable CUDA cubin (ELF64,
3note EM_CUDA 190), in seeded Omega (program mode, Linux AArch64). The layout is a port of CUDA++'s
4note omega_cubin_pack (aien-dev/CUDApp src/omega_cubin.c at 853e814d): the same ten sections, the same
5note .nv.info attributes, the same five program headers, made per architecture:
6note sm_121: e_flags 0x6007902, cuinfo note arch 121, with CUDA++'s 36-byte .nv.compat
7note sm_80: e_flags 0x6005004, cuinfo note arch 80, no .nv.compat (ptxas 13.0 writes none for sm_80)
8note
9note Run: kpack ARCH NAME BIN OUT REGS SHARED P...
10note ARCH 80 or 121; NAME the kernel symbol ([A-Za-z_][A-Za-z0-9_]*, at most 63 bytes); BIN raw SASS
11note (a nonzero multiple of 16 bytes, at most 64 KiB); OUT the cubin to write; REGS registers per thread
12note (at most 255); SHARED reserved shared bytes (multiple of 4); then one word per parameter in order:
13note Select ARCH 121-tk for a driver-loadable CUDA 13 image with linked toolkit metadata.
14note p8 a pointer, or 1 2 4 8 a value of that many bytes. Parameters are laid out naturally aligned from
15note constant bank offset 0x380 (CUDA's ABI), and the constant bank ends at the end of the last one.
16note Exit 0 written, 1 refused arguments or input, 2 write failed. The packer does not decode SASS: the
17note register count is the caller's claim, and EIATTR_EXIT_INSTR_OFFSETS is left out as in CUDA++.
18om2
19text msg "kpack: usage: kpack ARCH NAME BIN OUT REGS SHARED P...\nkpack: bad argument\nkpack: bin unreadable, empty, too large or not a multiple of 16 bytes\nkpack: cannot write output\nkpack: wrote bytes=\n sections=\n cbank=\n"
20text sn ".shstrtab|.strtab|.symtab|.text.|.nv.constant0.|.nv.info|.nv.compat|.nv.info.|.nv.shared.reserved.0|.note.nv.cuinfo|.note.nv.tkinfo|"
21text tkname "121-tk"
22text producer "Omega kpack"
23text target "sm_121a"
24global ob uchar 196608
25global tx uchar 65536
26global ab uchar 256
27global fnb uchar 64
28global dig uchar 32
29global po ulong 32
30global ps ulong 32
31global pp ulong 32
32global so ulong 12
33global sz ulong 12
34global nm ulong 12
35global sv ulong 16
36note sv: 0 arch, 1 cursor, 2 name length, 3 parameter count, 4 text bytes, 5 cbank size, 6 regs,
37note 7 shared bytes, 8 section count (with the null one)
38fn say id ulong to void
39k = ulong 1
40s = ulong 0
41for i ulong len msg
42if eq get msg i 10
43if eq k id
44e = i
45if lt id 5
46e = add i 1
47end
48r = write 1 msg s sub e s
49ret
50end
51k = add k 1
52s = add i 1
53end
54end
55exit 99
56end
57fn num v ulong to void
58x = v
59n = ulong 0
60while ge x 10 bound 64
61put dig n uchar add 48 rem x 10
62x = div x 10
63n = add n 1
64end
65put dig n uchar add 48 x
66n = add n 1
67for i ulong n
68r = write 1 dig sub sub n i 1 1
69end
70end
71note ---- little-endian stores at an absolute offset, and at the cursor ----
72fn b1 o ulong v ulong to void
73put ob o uchar and v 255
74end
75fn b2 o ulong v ulong to void
76b1 o v
77b1 add o 1 shr v 8
78end
79fn b4 o ulong v ulong to void
80b2 o v
81b2 add o 2 shr v 16
82end
83fn b8 o ulong v ulong to void
84b4 o v
85b4 add o 4 shr v 32
86end
87fn cur to ulong
88ret get sv 1
89end
90fn adv n ulong to void
91put sv 1 add get sv 1 n
92end
93fn e1 v ulong to void
94b1 cur v
95adv 1
96end
97fn e4 v ulong to void
98b4 cur v
99adv 4
100end
101fn alignc a ulong to void
102while ne rem cur a 0 bound 256
103adv 1
104end
105end
106note ---- .nv.info attributes: format 4 (id, length, payload), 3 (id, u16), 2 (id, 0) ----
107fn at4 id ulong n ulong to void
108e1 4
109e1 id
110e1 and n 255
111e1 shr n 8
112end
113fn at3 id ulong v ulong to void
114e1 3
115e1 id
116e1 and v 255
117e1 shr v 8
118end
119fn ename to void
120for i ulong get sv 2
121e1 ulong get fnb i
122end
123end
124note section-name number k (0..9) at the cursor: the prefix, the kernel name for 3 4 7, then 0
125fn sname k ulong to void
126j = ulong 0
127for i ulong len sn
128c = get sn i
129if eq c 124
130if eq j k
131if or or eq k 3 eq k 4 eq k 7
132ename
133end
134e1 0
135ret
136end
137j = add j 1
138else
139if eq j k
140e1 ulong c
141end
142end
143end
144end
145fn hascompat to bool
146ret eq get sv 0 121
147end
148fn shstr to void
149e1 0
150for k ulong add 10 get sv 9
151if or ne k 6 hascompat
152put nm k sub cur get so 0
153sname k
154end
155end
156end
157fn symtab to void
158b = cur
159for i ulong 96
160e1 0
161end
162b1 add b 28 3
163b2 add b 30 4
164b4 add b 48 1
165b1 add b 52 0x12
166b1 add b 53 0x10
167b2 add b 54 4
168b8 add b 64 get sv 4
169b1 add b 76 3
170b2 add b 78 5
171end
172fn ginfo to void
173at4 0x2f 8
174e4 2
175e4 get sv 6
176at4 0x11 8
177e4 2
178e4 0
179at4 0x12 8
180e4 2
181e4 0
182end
183fn compat to void
184e4 0x00000902
185e4 0x00020202
186e4 0x00050502
187e4 0x01010703
188e4 0x00000302
189e4 0x00010602
190e4 0x00080b04
191e4 0
192e4 0
193end
194fn finfo to void
195at4 0x37 4
196e4 130
197np = get sv 3
198for i ulong np
199at4 0x17 12
200e4 0
201e4 or i shl sub get po i 0x380 16
202t = ulong 0x0011f000
203if ne get sv 9 0
204t = or 0x0001f000 shl get ps i 18
205end
206if ne get pp i 0
207t = ulong 0x0021f500
208end
209e4 t
210end
211e = sub get sv 5 0x380
212at3 0x19 e
213at4 0x0a 8
214e4 3
215e4 or 0x380 shl e 16
216at3 0x50 0
217at3 0x1b get sv 6
218at3 0x5f 0x0101
219e1 2
220e1 0x4a
221e1 0
222e1 0
223at4 0x36 4
224e4 0
225end
226note one section body, by section-name number k
227fn body k ulong to void
228if eq k 0
229shstr
230end
231if eq k 1
232e1 0
233ename
234e1 0
235end
236if eq k 2
237symtab
238end
239if eq k 3
240for j ulong get sv 4
241e1 ulong get tx j
242end
243end
244if eq k 4
245for j ulong get sv 5
246e1 0
247end
248end
249if eq k 5
250ginfo
251end
252if eq k 6
253compat
254end
255if eq k 7
256finfo
257end
258if eq k 8
259adv get sv 7
260end
261if eq k 9
262noteb
263end
264if eq k 10
265toolnote
266end
267end
268fn toolnote to void
269note ELF toolkit descriptor v2 with producer, ABI release, build and target string offsets.
270e4 12
271e4 64
272e4 2000
273e4 0x4449564e
274e4 0x43204149
275e4 0x0070726f
276e4 2
277e4 0
278e4 1
279e4 13
280e4 18
281e4 25
282e1 0
283for i ulong len producer
284e1 ulong get producer i
285end
286e1 0
287e4 0x302e3331
288e1 0
289e4 0x64656573
290e1 101
291e1 100
292e1 0
293for i ulong len target
294e1 ulong get target i
295end
296e1 0
297for i ulong 7
298e1 0
299end
300end
301fn noteb to void
302e4 12
303e4 8
304e4 0x3e8
305e4 0x4449564e
306e4 0x43204149
307e4 0x0070726f
308e1 2
309e1 0
310e1 and get sv 0 255
311e1 0
312e4 130
313end
314note section attributes for name number k: type, flags, link, info, align, entsize
315fn sty k ulong to ulong
316if eq k 2
317ret 2
318end
319if or eq k 3 eq k 4
320ret 1
321end
322if or eq k 5 eq k 7
323ret 0x70000000
324end
325if eq k 6
326ret 0x70000086
327end
328if eq k 8
329ret 8
330end
331if ge k 9
332ret 7
333end
334ret 3
335end
336fn sfl k ulong to ulong
337if eq k 3
338ret 6
339end
340if eq k 4
341ret 0x42
342end
343if eq k 7
344ret 0x40
345end
346if eq k 8
347ret 3
348end
349if eq k 10
350ret 0x1000000
351end
352if eq k 9
353ret 0x1000040
354end
355ret 0
356end
357fn sal k ulong to ulong
358if eq k 2
359ret 8
360end
361if eq k 3
362ret 128
363end
364if eq k 4
365ret 16
366end
367if or or eq k 5 eq k 6 or eq k 7 ge k 9
368ret 4
369end
370ret 1
371end
372fn slk k ulong to ulong
373if and eq k 9 ne get sv 9 0
374ret 11
375end
376if eq k 2
377ret 2
378end
379if or or eq k 3 eq k 4 or eq k 5 eq k 7
380ret 3
381end
382ret 0
383end
384fn sin k ulong to ulong
385if eq k 2
386ret 2
387end
388if eq k 3
389ret 2
390end
391if or eq k 4 eq k 7
392ret 4
393end
394if eq k 9
395ret 2
396end
397ret 0
398end
399fn shdr p ulong k ulong to void
400b4 p get nm k
401b4 add p 4 sty k
402b8 add p 8 sfl k
403b8 add p 24 get so k
404b8 add p 32 get sz k
405b4 add p 40 slk k
406b4 add p 44 sin k
407b8 add p 48 sal k
408if eq k 2
409b8 add p 56 24
410end
411end
412fn phdr p ulong t ulong f ulong o ulong fs ulong ms ulong to void
413b4 p t
414b4 add p 4 f
415b8 add p 8 o
416b8 add p 32 fs
417b8 add p 40 ms
418b8 add p 48 8
419end
420note ---- arguments ----
421fn argdec i ulong to ulong
422r = argread i ab 0 20
423if lt r 1
424ret 0xffffffffffffffff
425end
426v = ulong 0
427for k ulong ulong r
428c = get ab k
429if or lt c 48 gt c 57
430ret 0xffffffffffffffff
431end
432v = add mul v 10 ulong sub c 48
433end
434ret v
435end
436fn namearg to bool
437r = argread 2 fnb 0 64
438if or lt r 1 gt r 63
439ret false
440end
441n = ulong r
442for k ulong n
443c = get fnb k
444d = and ge c 48 le c 57
445a = or and ge c 65 le c 90 and ge c 97 le c 122
446if not or or a eq c 95 and d gt k 0
447ret false
448end
449end
450put sv 2 n
451ret true
452end
453fn params to bool
454np = sub ulong argcnt 7
455if gt np 32
456ret false
457end
458e = ulong 0x380
459for i ulong np
460r = argread add i 7 ab 0 4
461p = ulong 0
462s = ulong 0
463if and eq r 2 eq get ab 0 112
464if eq get ab 1 56
465p = ulong 1
466s = ulong 8
467end
468end
469if eq r 1
470s = ulong sub get ab 0 48
471end
472if not or or eq s 1 eq s 2 or eq s 4 eq s 8
473ret false
474end
475while ne rem e s 0 bound 8
476e = add e 1
477end
478put po i e
479put ps i s
480put pp i p
481e = add e s
482end
483put sv 3 np
484put sv 5 e
485ret true
486end
487fn loadbin to bool
488r = argread 3 ab 0 255
489if lt r 1
490ret false
491end
492fd = fopen ab 0 ulong r 524288 0
493if lt fd 0
494ret false
495end
496n = ulong 0
497for k ulong 4097
498x = read int fd tx n sub len tx n
499if le x 0
500break
501end
502n = add n ulong x
503if eq n len tx
504break
505end
506end
507y = read int fd ab 0 1
508c = fclose int fd
509put sv 4 n
510ret and and gt n 0 eq rem n 16 0 eq y 0
511end
512fn place to void
513put sv 1 64
514i = ulong 1
515for k ulong add 10 get sv 9
516if or ne k 6 hascompat
517alignc sal k
518put so k cur
519body k
520put sz k sub cur get so k
521i = add i 1
522end
523end
524put sv 8 i
525end
526fn headers to ulong
527alignc 8
528sh = cur
529n = get sv 8
530i = ulong 1
531for k ulong add 10 get sv 9
532if or ne k 6 hascompat
533shdr add sh mul i 64 k
534i = add i 1
535end
536end
537ph = add sh mul n 64
538phdr ph 6 4 ph 280 280
539phdr add ph 56 1 4 ph 280 280
540phdr add ph 112 1 5 get so 3 get sz 3 get sz 3
541phdr add ph 168 1 6 get so 8 0 get sz 8
542phdr add ph 224 1 4 get so 4 get sz 4 get sz 4
543b4 0 0x464c457f
544b1 4 2
545b1 5 1
546b1 6 1
547b1 7 0x41
548b1 8 8
549b2 16 2
550b2 18 190
551b4 20 1
552b8 32 ph
553b8 40 sh
554f = ulong 0x6005004
555if hascompat
556f = ulong 0x6007902
557end
558b4 48 f
559b2 52 64
560b2 54 56
561b2 56 5
562b2 58 64
563b2 60 n
564b2 62 1
565ret add ph 280
566end
567fn architecture to ulong
568a = argdec 1
569r = argread 1 ab 0 20
570if eq r long len tkname
571match = true
572for j ulong len tkname
573if ne get ab j get tkname j
574match = false
575end
576end
577if match
578a = ulong 121
579put sv 9 1
580end
581end
582ret a
583end
584fn main to int
585if lt argcnt 7
586say 1
587ret 1
588end
589a = architecture
590g = argdec 5
591h = argdec 6
592if or or and ne a 80 ne a 121 gt g 255 or gt h 0xfffff ne rem h 4 0
593say 2
594ret 1
595end
596if not namearg
597say 2
598ret 1
599end
600if not params
601say 2
602ret 1
603end
604if not loadbin
605say 3
606ret 1
607end
608put sv 0 a
609put sv 6 g
610put sv 7 h
611place
612t = headers
613r = argread 4 ab 0 255
614if lt r 1
615say 4
616ret 2
617end
618fd = fopen ab 0 ulong r 524865 420
619if lt fd 0
620say 4
621ret 2
622end
623w = write int fd ob 0 t
624c = fclose int fd
625if or ne w long t ne c 0
626say 4
627ret 2
628end
629say 5
630num t
631say 6
632num get sv 8
633say 7
634num get sv 5
635put dig 0 10
636r = write 1 dig 0 1
637ret 0
638end