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 / gpu / spirv / run.om

796 lines · 12529 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2note run.om: pure seeded-Omega SPIR-V compute binary executor (replaces Vulkan/lavapipe).
3note Decodes and executes SPIR-V 1.3 Vulkan Shader (GLCompute) and SPIR-V 1.0 OpenCL Kernel
4note binary modules directly in seeded Omega with exact IEEE binary32 arithmetic (0 ulp, no FMA).
5note
6note Usage:
7note run MODULE.spv N_WORK N_OUT OUT.bin IN0.bin [IN1.bin]
8note MODULE.spv SPIR-V binary file (little-endian 32-bit words, magic 0x07230203)
9note N_WORK number of work-items (gid = 0 .. N_WORK - 1)
10note N_OUT number of f32 elements in OUT.bin
11note OUT.bin output binary file (written as N_OUT little-endian binary32 values)
12note IN0.bin first input binary file (little-endian binary32 values)
13note IN1.bin optional second input binary file
14note
15note Exit: 0 ok; 2 usage; 3 I/O error; 4 invalid or unsupported SPIR-V module. Traps exit 100 + code.
16om2
17
18text tuse "usage: run MODULE.spv N_WORK N_OUT OUT.bin IN0.bin [IN1.bin]\n"
19text tio "run: I/O error or size limit exceeded\n"
20text tbad "run: invalid or unsupported SPIR-V module\n"
21text tocls "OpenCL.std"
22text tglsl "GLSL.std.450"
23
24const MAXBUF ulong 1048576
25const MAXIDS ulong 1024
26const MAXINST ulong 1024
27const POISON uint 4290838510
28
29global gspv uchar 65536
30global gw uint 16384
31global gio uchar 4194304
32global gbuf0 uint 1048576
33global gbuf1 uint 1048576
34global gbuf2 uint 1048576
35global gblen ulong 4
36global gval ulong 1024
37global gbind uint 1024
38global glabel uint 1024
39global giop uint 1024
40global gia1 uint 1024
41global gia2 uint 1024
42global gia3 uint 1024
43global gia4 uint 1024
44global gpath uchar 4096
45global garg uchar 64
46note gst:
47note 0 nwords, 1 id_bound, 2 exec_model (5 vk, 6 cl), 3 entry_fn,
48note 4 gid_var, 5 ext_id, 6 ext_kind (1 glsl, 2 ocl), 7 nin_files (1 or 2),
49note 8 n_work, 9 n_out, 10 n_inst, 11 n_ptr_params, 12 n_param_id, 13 in_entry,
50note 14 out_slot (1 if 1 input, 2 if 2 inputs), 15 ptr_f32_tid
51global gst ulong 16
52
53fn fail code int msg view uchar * to void
54n = write 2 msg 0 len msg
55exit code
56end
57
58fn parseu ai ulong to ulong
59n = argread ai garg 0 len garg
60if or le n 0 gt n 20
61fail 2 tuse
62end
63v = ulong 0
64for i ulong ulong n
65c = get garg i
66if or lt c 48 gt c 57
67fail 2 tuse
68end
69v = add mul v 10 ulong sub c 48
70end
71ret v
72end
73
74fn spvu32 i ulong to uint
75o = mul i 4
76v = uint 0
77for k ulong 4
78v = or v shl uint get gspv add o k mul uint k 8
79end
80ret v
81end
82
83fn giou32 i ulong to uint
84o = mul i 4
85v = uint 0
86for k ulong 4
87v = or v shl uint get gio add o k mul uint k 8
88end
89ret v
90end
91
92fn putgio32 i ulong v uint to void
93o = mul i 4
94for k ulong 4
95put gio add o k uchar and shr v mul uint k 8 255
96end
97end
98
99fn readspv ai ulong to void
100n = argread ai gpath 0 len gpath
101if le n 0
102fail 2 tuse
103end
104fd = fopen gpath 0 ulong n 0 0
105if lt fd 0
106fail 3 tio
107end
108sz = fseek int fd 0 2
109if or lt sz 20 gt sz 65536
110c = fclose int fd
111fail 4 tbad
112end
113if ne rem ulong sz 4 0
114c = fclose int fd
115fail 4 tbad
116end
117r = pread int fd gspv 0 ulong sz 0
118c = fclose int fd
119if ne r sz
120fail 3 tio
121end
122nw = div ulong sz 4
123put gst 0 nw
124for i ulong nw
125put gw i spvu32 i
126end
127end
128
129fn loadbuf ai ulong slot ulong to void
130n = argread ai gpath 0 len gpath
131if le n 0
132fail 2 tuse
133end
134fd = fopen gpath 0 ulong n 0 0
135if lt fd 0
136fail 3 tio
137end
138sz = fseek int fd 0 2
139if or lt sz 0 gt sz 4194304
140c = fclose int fd
141fail 3 tio
142end
143if ne rem ulong sz 4 0
144c = fclose int fd
145fail 3 tio
146end
147r = pread int fd gio 0 ulong sz 0
148c = fclose int fd
149if ne r sz
150fail 3 tio
151end
152nelem = div ulong sz 4
153put gblen slot nelem
154pos = ulong 0
155while lt pos nelem bound 260
156step = sub nelem pos
157if gt step 4096
158step = 4096
159end
160for l ulong step
161idx = add pos l
162v = giou32 idx
163if eq slot 0
164put gbuf0 idx v
165end
166if eq slot 1
167put gbuf1 idx v
168end
169end
170pos = add pos step
171end
172end
173
174fn saveout ai ulong nelem ulong to void
175n = argread ai gpath 0 len gpath
176if le n 0
177fail 2 tuse
178end
179pos = ulong 0
180while lt pos nelem bound 260
181step = sub nelem pos
182if gt step 4096
183step = 4096
184end
185for l ulong step
186idx = add pos l
187putgio32 idx get gbuf2 idx
188end
189pos = add pos step
190end
191nb = mul nelem 4
192fd = fopen gpath 0 ulong n 577 420
193if lt fd 0
194fail 3 tio
195end
196r = write int fd gio 0 nb
197c = fclose int fd
198if ne r long nb
199fail 3 tio
200end
201end
202
203fn initout nelem ulong to void
204put gblen get gst 14 nelem
205pos = ulong 0
206while lt pos nelem bound 260
207step = sub nelem pos
208if gt step 4096
209step = 4096
210end
211for l ulong step
212put gbuf2 add pos l POISON
213end
214pos = add pos step
215end
216end
217
218fn wbyte woff ulong i ulong to uchar
219w = get gw add woff div i 4
220sh = mul uint rem i 4 8
221ret uchar and shr w sh 255
222end
223
224fn stris woff ulong wc ulong t view uchar * to bool
225for i ulong len t
226if ge div i 4 wc
227ret false
228end
229if ne wbyte woff i get t i
230ret false
231end
232end
233if ge div len t 4 wc
234ret false
235end
236ret eq wbyte woff len t 0
237end
238
239fn chk_id id uint to void
240if ge ulong id MAXIDS
241fail 4 tbad
242end
243end
244
245fn addinst op uint a1 uint a2 uint a3 uint a4 uint to void
246k = get gst 10
247if ge k MAXINST
248fail 4 tbad
249end
250put giop k op
251put gia1 k a1
252put gia2 k a2
253put gia3 k a3
254put gia4 k a4
255put gst 10 add k 1
256end
257
258fn parse_meta op uint p ulong wc ulong to void
259if eq op 11
260if lt wc 3
261fail 4 tbad
262end
263eid = get gw add p 1
264chk_id eid
265if stris add p 2 sub wc 2 tglsl
266put gst 5 ulong eid
267put gst 6 1
268end
269if stris add p 2 sub wc 2 tocls
270put gst 5 ulong eid
271put gst 6 2
272end
273ret
274end
275if eq op 15
276if lt wc 4
277fail 4 tbad
278end
279put gst 2 ulong get gw add p 1
280put gst 3 ulong get gw add p 2
281ret
282end
283if eq op 71
284if lt wc 3
285fail 4 tbad
286end
287tid = get gw add p 1
288dec = get gw add p 2
289chk_id tid
290if and eq dec 11 ge wc 4
291if eq get gw add p 3 28
292put gst 4 ulong tid
293end
294end
295if and eq dec 33 ge wc 4
296slot = get gw add p 3
297if gt slot 2
298fail 4 tbad
299end
300put gbind ulong tid add slot 1
301end
302ret
303end
304if eq op 32
305if lt wc 4
306fail 4 tbad
307end
308tid = get gw add p 1
309sc = get gw add p 2
310if eq sc 5
311put gst 15 ulong tid
312end
313ret
314end
315if eq op 43
316if lt wc 4
317fail 4 tbad
318end
319rid = get gw add p 2
320chk_id rid
321v = ulong get gw add p 3
322if ge wc 5
323v = or v shl ulong get gw add p 4 32
324end
325put gval ulong rid v
326ret
327end
328end
329
330fn parse_body op uint p ulong wc ulong to void
331if eq op 55
332if lt wc 3
333fail 4 tbad
334end
335ty = get gw add p 1
336pid = get gw add p 2
337chk_id pid
338if eq ulong ty get gst 15
339ps = get gst 11
340if gt ps 2
341fail 4 tbad
342end
343put gbind ulong pid uint add ps 1
344put gst 11 add ps 1
345ret
346end
347put gst 12 ulong pid
348put gval ulong pid get gst 8
349ret
350end
351if eq op 248
352if lt wc 2
353fail 4 tbad
354end
355lid = get gw add p 1
356chk_id lid
357put glabel ulong lid uint add get gst 10 1
358ret
359end
360if eq op 247
361ret
362end
363if eq op 249
364if lt wc 2
365fail 4 tbad
366end
367addinst op get gw add p 1 0 0 0
368ret
369end
370if eq op 250
371if lt wc 4
372fail 4 tbad
373end
374addinst op get gw add p 1 get gw add p 2 get gw add p 3 0
375ret
376end
377if eq op 253
378addinst op 0 0 0 0
379ret
380end
381if eq op 61
382if lt wc 4
383fail 4 tbad
384end
385rid = get gw add p 2
386chk_id rid
387addinst op rid get gw add p 3 0 0
388ret
389end
390if eq op 62
391if lt wc 3
392fail 4 tbad
393end
394addinst op get gw add p 1 get gw add p 2 0 0
395ret
396end
397if eq op 81
398if lt wc 5
399fail 4 tbad
400end
401rid = get gw add p 2
402chk_id rid
403addinst op rid get gw add p 3 get gw add p 4 0
404ret
405end
406if eq op 68
407if lt wc 5
408fail 4 tbad
409end
410rid = get gw add p 2
411chk_id rid
412addinst op rid get gw add p 3 0 0
413ret
414end
415if eq op 113
416if lt wc 4
417fail 4 tbad
418end
419rid = get gw add p 2
420chk_id rid
421addinst op rid get gw add p 3 0 0
422ret
423end
424if eq op 65
425if lt wc 6
426fail 4 tbad
427end
428rid = get gw add p 2
429chk_id rid
430addinst op rid get gw add p 3 get gw add p 5 0
431ret
432end
433if eq op 70
434if lt wc 5
435fail 4 tbad
436end
437rid = get gw add p 2
438chk_id rid
439addinst op rid get gw add p 3 get gw add p 4 0
440ret
441end
442if eq op 12
443if lt wc 6
444fail 4 tbad
445end
446rid = get gw add p 2
447chk_id rid
448addinst op rid get gw add p 3 get gw add p 4 get gw add p 5
449ret
450end
451if eq op 169
452if lt wc 6
453fail 4 tbad
454end
455rid = get gw add p 2
456chk_id rid
457addinst op rid get gw add p 3 get gw add p 4 get gw add p 5
458ret
459end
460parse_body_bin op p wc
461end
462
463fn is_binop op uint to bool
464if eq op 128
465ret true
466end
467if eq op 129
468ret true
469end
470if eq op 131
471ret true
472end
473if eq op 132
474ret true
475end
476if eq op 133
477ret true
478end
479if eq op 134
480ret true
481end
482if eq op 136
483ret true
484end
485if eq op 137
486ret true
487end
488if eq op 176
489ret true
490end
491if eq op 186
492ret true
493end
494ret eq op 194
495end
496
497fn parse_body_bin op uint p ulong wc ulong to void
498if not is_binop op
499fail 4 tbad
500end
501if lt wc 5
502fail 4 tbad
503end
504rid = get gw add p 2
505chk_id rid
506addinst op rid get gw add p 3 get gw add p 4 0
507end
508
509fn decode to void
510nw = get gst 0
511if lt nw 5
512fail 4 tbad
513end
514if ne get gw 0 0x07230203
515fail 4 tbad
516end
517bnd = ulong get gw 3
518if or eq bnd 0 gt bnd MAXIDS
519fail 4 tbad
520end
521put gst 1 bnd
522p = ulong 5
523while lt p nw bound 16384
524hd = get gw p
525wc = ulong shr hd 16
526op = and hd 65535
527if or eq wc 0 gt add p wc nw
528fail 4 tbad
529end
530if eq op 54
531if lt wc 5
532fail 4 tbad
533end
534fid = ulong get gw add p 2
535if eq fid get gst 3
536put gst 13 1
537end
538end
539if eq op 56
540put gst 13 0
541end
542if and eq get gst 13 1 and ne op 54 ne op 56
543parse_body op p wc
544end
545if eq get gst 13 0
546parse_meta op p wc
547end
548p = add p wc
549end
550if eq get gst 10 0
551fail 4 tbad
552end
553end
554
555fn bufget slot ulong idx ulong to uint
556if ge idx get gblen slot
557fail 4 tbad
558end
559if eq slot 0
560ret get gbuf0 idx
561end
562if eq slot 1
563if eq get gst 14 1
564ret get gbuf2 idx
565end
566ret get gbuf1 idx
567end
568if eq slot 2
569ret get gbuf2 idx
570end
571fail 4 tbad
572ret 0
573end
574
575fn bufput slot ulong idx ulong v uint to void
576if ge idx get gblen slot
577fail 4 tbad
578end
579if eq slot get gst 14
580put gbuf2 idx v
581ret
582end
583fail 4 tbad
584end
585
586fn exec_alu op uint a1 ulong a2 ulong a3 ulong a4 ulong to void
587if eq op 128
588put gval a1 add get gval a2 get gval a3
589ret
590end
591if eq op 132
592put gval a1 mul get gval a2 get gval a3
593ret
594end
595if eq op 134
596put gval a1 div get gval a2 get gval a3
597ret
598end
599if eq op 137
600put gval a1 rem get gval a2 get gval a3
601ret
602end
603if eq op 194
604put gval a1 shr get gval a2 get gval a3
605ret
606end
607if eq op 176
608if lt get gval a2 get gval a3
609put gval a1 1
610ret
611end
612put gval a1 0
613ret
614end
615if eq op 129
616fa = bitcast f32 uint get gval a2
617fb = bitcast f32 uint get gval a3
618put gval a1 ulong bitcast uint add fa fb
619ret
620end
621if eq op 131
622fa = bitcast f32 uint get gval a2
623fb = bitcast f32 uint get gval a3
624put gval a1 ulong bitcast uint sub fa fb
625ret
626end
627if eq op 133
628fa = bitcast f32 uint get gval a2
629fb = bitcast f32 uint get gval a3
630put gval a1 ulong bitcast uint mul fa fb
631ret
632end
633if eq op 136
634fa = bitcast f32 uint get gval a2
635fb = bitcast f32 uint get gval a3
636put gval a1 ulong bitcast uint div fa fb
637ret
638end
639if eq op 186
640fa = bitcast f32 uint get gval a2
641fb = bitcast f32 uint get gval a3
642if gt fa fb
643put gval a1 1
644ret
645end
646put gval a1 0
647ret
648end
649if eq op 169
650if ne get gval a2 0
651put gval a1 get gval a3
652ret
653end
654put gval a1 get gval a4
655ret
656end
657if eq op 12
658if ne a2 get gst 5
659fail 4 tbad
660end
661ok = false
662if and eq get gst 6 1 eq a3 31
663ok = true
664end
665if and eq get gst 6 2 eq a3 61
666ok = true
667end
668if not ok
669fail 4 tbad
670end
671fa = bitcast f32 uint get gval a4
672put gval a1 ulong bitcast uint fsqrt fa
673ret
674end
675fail 4 tbad
676end
677
678fn exec_step op uint a1 ulong a2 ulong a3 ulong a4 ulong to void
679if eq op 61
680pv = get gval a2
681put gval a1 ulong bufget shr pv 32 and pv 4294967295
682ret
683end
684if eq op 62
685pv = get gval a1
686bufput shr pv 32 and pv 4294967295 uint get gval a2
687ret
688end
689if eq op 81
690if eq a3 0
691put gval a1 get gval a2
692else
693put gval a1 0
694end
695ret
696end
697if eq op 68
698b = get gbind a2
699if eq b 0
700fail 4 tbad
701end
702put gval a1 get gblen ulong sub b 1
703ret
704end
705if eq op 113
706put gval a1 and get gval a2 4294967295
707ret
708end
709if or eq op 65 eq op 70
710b = get gbind a2
711if eq b 0
712fail 4 tbad
713end
714put gval a1 or shl ulong sub b 1 32 and get gval a3 4294967295
715ret
716end
717exec_alu op a1 a2 a3 a4
718end
719
720fn jump_pc lbl ulong to ulong
721t = get glabel lbl
722if eq t 0
723fail 4 tbad
724end
725ret ulong sub t 1
726end
727
728fn run_one gid ulong to void
729ninst = get gst 10
730gvar = get gst 4
731pc = ulong 0
732while lt pc ninst bound 4096
733op = get giop pc
734a1 = ulong get gia1 pc
735a2 = ulong get gia2 pc
736if eq op 253
737ret
738end
739if eq op 249
740pc = jump_pc a1
741continue
742end
743if eq op 250
744if ne get gval a1 0
745pc = jump_pc a2
746else
747pc = jump_pc ulong get gia3 pc
748end
749continue
750end
751if and eq op 61 eq a2 gvar
752put gval a1 gid
753pc = add pc 1
754continue
755end
756exec_step op a1 a2 ulong get gia3 pc ulong get gia4 pc
757pc = add pc 1
758end
759end
760
761fn main to int
762ac = argcnt
763if and ne ac 6 ne ac 7
764fail 2 tuse
765end
766nin = ulong sub ac 5
767put gst 7 nin
768put gst 14 nin
769nw = parseu 2
770no = parseu 3
771if or gt nw MAXBUF gt no MAXBUF
772fail 3 tio
773end
774put gst 8 nw
775put gst 9 no
776readspv 1
777loadbuf 5 0
778if eq nin 2
779loadbuf 6 1
780end
781initout no
782decode
783pos = ulong 0
784while lt pos nw bound 260
785step = sub nw pos
786if gt step 4096
787step = 4096
788end
789for l ulong step
790run_one add pos l
791end
792pos = add pos step
793end
794saveout 4 no
795ret 0
796end