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

891 lines · 12969 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2note emit.om: seeded-Omega SPIR-V kernel builder for the portable backend (docs/ARCHITECTURE.md).
3note It writes one SPIR-V module to stdout for a fixed FP32 kernel and one environment:
4note cl SPIR-V 1.0, OpenCL Kernel, Physical64, CrossWorkgroup pointer arguments (format SPIRV_CL)
5note vk SPIR-V 1.3, Vulkan Shader GLCompute, Logical GLSL450, StorageBuffer bindings (format SPIRV_VK)
6note Why a kernel builder and not the IR route: omc IR mode refuses float words (OMEGESE 9.9 at
7note omega-mv 369dbc96), so seed/spirv/irspirv.om and vkspirv.om translate integer IR only. This file
8note is the FP32 builder until IR v7 (omega-mv#883) carries floats; it is not a compiler.
9note Opcode, enum and layout authority: registry.khronos.org/SPIR-V/specs/unified1/SPIRV.html
10note Every module is checked by spirv-val and by backend/targets.om before any run.
11note
12note Kernels (n = element count for vadd, row count for fsum16; gid = global invocation x):
13note vadd c[gid] = a[gid] + b[gid] for gid < n (hello_gpu's operation)
14note vmul c[gid] = a[gid] * b[gid] for gid < n
15note vsub c[gid] = a[gid] - b[gid] for gid < n
16note vmax c[gid] = a[gid] > b[gid] ? a[gid] : b[gid] for gid < n (OpFOrdGreaterThan, OpSelect)
17note fsum16 o[gid] = (((x[16g] + x[16g+1]) + x[16g+2]) ... + x[16g+15]) frozen left-to-right order
18note gemm16 one work-item per element of a 16x16 f32 tile, 4096 tiles (n = 1048576):
19note gid = tile * 256 + row * 16 + col
20note C = 0; for k in 0..15 left to right: C = C + (A[row,k] * B[k,col])
21note each * and + is its own op. No OpFma.
22note Vulkan decorates every mul and add with NoContraction (42). spirv-val --target-env opencl2.2
23note rejects that decoration (requires Capability Shader, which OpenCL forbids), so the OpenCL
24note module uses ExecutionMode ContractionOff (31) instead. spirv-val --target-env vulkan1.1
25note rejects ContractionOff (requires Capability Kernel, which Vulkan does not grant).
26note A and B are row-major 16x16 inside the tile (256 floats per tile, same length as C).
27note rmsnorm one work-item per row of 16 (train/openaccel-lab/workloads/rmsnorm/omega.om, frozen):
28note ss = (((x0*x0 + x1*x1) + x2*x2) ... + x15*x15)
29note yk = (xk * (1 / sqrt(ss * (1/16) + eps))) * wk eps = binary32(0.000001) = 0x358637BD
30note sqrt then div, not rsqrt and not fma. 1/16 is 0.0625 and is exact.
31note cl arguments: vadd, vmul, vsub, vmax, gemm16 (a, b, c, uint n), fsum16 (x, o, uint n),
32note rmsnorm (x, w, y, uint nrows). w is 16 floats, shared by every row.
33note vk bindings set 0: vadd, vmul, vsub, vmax, gemm16 a=0 b=1 c=2, fsum16 x=0 o=1, rmsnorm x=0 w=1 y=2;
34note n = OpArrayLength of the output buffer (rmsnorm divides that length by 16);
35note LocalSize 64 1 1.
36note
37note Usage: emit vadd|vmul|vsub|vmax|fsum16|rmsnorm|gemm16 cl|vk [mutant]
38note mutant plants a wrong kernel for negative controls: vadd uses OpFSub, vmul and vsub use OpFAdd,
39note vmax selects the other operand (min), fsum16, rmsnorm and gemm16 add right-to-left.
40note Exit: 0 module written; 2 usage; 3 stdout write failed. Traps exit 100 + code.
41om2
42
43text tuse "usage: emit vadd|vmul|vsub|vmax|fsum16|rmsnorm|gemm16 cl|vk [mutant]\n"
44text tio "emit: stdout write failed\n"
45text tvadd "vadd"
46text tfsum "fsum16"
47text tvmul "vmul"
48text tvsub "vsub"
49text tvmax "vmax"
50text trms "rmsnorm"
51text tgemm "gemm16"
52text tocls "OpenCL.std"
53text tglsl "GLSL.std.450"
54text tcl "cl"
55text tvk "vk"
56text tmut "mutant"
57
58const IVOID uint 1
59const IU32 uint 2
60const IU64 uint 3
61const IF32 uint 4
62const IBOOL uint 5
63const IV3 uint 6
64const IPIN uint 7
65const IPF uint 8
66const IFN uint 9
67const IGID uint 10
68const IMAIN uint 11
69const IRTA uint 12
70const IST uint 13
71const IPST uint 14
72const IB0 uint 15
73const IB1 uint 16
74const IB2 uint 17
75const IN uint 18
76const IK0 uint 20
77const IK16 uint 36
78const IEXT uint 19
79const IONE uint 37
80const IEPS uint 38
81const IINV uint 39
82note gemm16 ids: 256 and f32 0, then 16 mul results and 16 add results. Each is NoContraction.
83const I256 uint 74
84const IZERO uint 75
85const IMUL0 uint 80
86const IADD0 uint 96
87const IDYN uint 112
88
89global gw uint 8192
90global gob uchar 65536
91global garg uchar 16
92note gst: 0 word fill, 1 next id, 2 env (0 cl, 1 vk), 3 kernel (0 vadd, 1 fsum16, 2 vmul, 3 vsub, 4 vmax, 5 rmsnorm, 6 gemm16), 4 mutant
93global gst ulong 8
94
95fn usage to void
96n = write 2 tuse 0 len tuse
97exit 2
98end
99
100fn w v uint to void
101f = get gst 0
102put gw f v
103put gst 0 add f 1
104end
105
106fn op c uint n uint to void
107w or shl n 16 c
108end
109
110fn nid to uint
111i = get gst 1
112put gst 1 add i 1
113ret uint i
114end
115
116fn isvk to bool
117ret eq get gst 2 1
118end
119
120note Literal string: bytes little-endian in words, a zero byte always ends it, zero padded.
121fn strwc t view uchar * to uint
122ret uint add div len t 4 1
123end
124
125fn wstr t view uchar * to void
126nw = ulong strwc t
127for i ulong nw
128v = uint 0
129for k ulong 4
130j = add mul i 4 k
131if lt j len t
132v = or v shl uint get t j mul uint k 8
133end
134end
135w v
136end
137end
138
139fn argis i ulong t view uchar * to bool
140n = argread i garg 0 len garg
141if ne n long len t
142ret false
143end
144for k ulong len t
145if ne get garg k get t k
146ret false
147end
148end
149ret true
150end
151
152fn kname to void
153if eq get gst 3 0
154wstr tvadd
155ret
156end
157if eq get gst 3 2
158wstr tvmul
159ret
160end
161if eq get gst 3 3
162wstr tvsub
163ret
164end
165if eq get gst 3 4
166wstr tvmax
167ret
168end
169if eq get gst 3 5
170wstr trms
171ret
172end
173if eq get gst 3 6
174wstr tgemm
175ret
176end
177wstr tfsum
178end
179
180fn knamewc to uint
181if eq get gst 3 0
182ret strwc tvadd
183end
184if eq get gst 3 2
185ret strwc tvmul
186end
187if eq get gst 3 3
188ret strwc tvsub
189end
190if eq get gst 3 4
191ret strwc tvmax
192end
193if eq get gst 3 5
194ret strwc trms
195end
196if eq get gst 3 6
197ret strwc tgemm
198end
199ret strwc tfsum
200end
201
202fn nbuf to uint
203if eq get gst 3 1
204ret 2
205end
206ret 3
207end
208
209note Buffer i (0-based) as an id: cl parameters and vk variables share IB0..IB2.
210fn bufid i uint to uint
211ret add IB0 i
212end
213
214fn outbuf to uint
215ret bufid sub nbuf 1
216end
217
218fn extimp to void
219if ne get gst 3 5
220ret
221end
222if isvk
223op 11 add 2 strwc tglsl
224w IEXT
225wstr tglsl
226ret
227end
228op 11 add 2 strwc tocls
229w IEXT
230wstr tocls
231end
232
233fn header to void
234if isvk
235op 17 2
236w 1
237extimp
238op 14 3
239w 0
240w 1
241op 15 add 4 knamewc
242w 5
243w IMAIN
244kname
245w IGID
246op 16 6
247w IMAIN
248w 17
249w 64
250w 1
251w 1
252ret
253end
254op 17 2
255w 4
256op 17 2
257w 6
258op 17 2
259w 11
260extimp
261op 14 3
262w 2
263w 2
264op 15 add 4 knamewc
265w 6
266w IMAIN
267kname
268w IGID
269nocon
270end
271
272note Execution mode 31 is ContractionOff, OpenCL only. Vulkan rejects it (needs Kernel).
273note Applied to rmsnorm (5) and gemm16 (6) so OpenCL runtimes do not contract mul+add into FMA.
274fn nocon to void
275if isvk
276ret
277end
278if and ne get gst 3 5 ne get gst 3 6
279ret
280end
281op 16 3
282w IMAIN
283w 31
284end
285
286fn decor to void
287op 71 4
288w IGID
289w 11
290w 28
291if not isvk
292op 71 3
293w IGID
294w 22
295end
296if isvk
297op 71 4
298w IRTA
299w 6
300w 4
301op 72 5
302w IST
303w 0
304w 35
305w 0
306op 71 3
307w IST
308w 2
309for i uint nbuf
310op 71 4
311w bufid i
312w 34
313w 0
314op 71 4
315w bufid i
316w 33
317w i
318end
319end
320if ne get gst 3 6
321ret
322end
323if not isvk
324ret
325end
326note Decoration 42 is NoContraction. Vulkan only: OpenCL forbids it without Shader.
327for i uint 32
328op 71 3
329w add IMUL0 i
330w 42
331end
332end
333
334fn types to void
335op 19 2
336w IVOID
337op 21 4
338w IU32
339w 32
340w 0
341op 22 3
342w IF32
343w 32
344op 20 2
345w IBOOL
346if isvk
347op 23 4
348w IV3
349w IU32
350w 3
351op 32 4
352w IPIN
353w 1
354w IV3
355op 29 3
356w IRTA
357w IF32
358op 30 3
359w IST
360w IRTA
361op 32 4
362w IPST
363w 12
364w IST
365op 32 4
366w IPF
367w 12
368w IF32
369op 33 3
370w IFN
371w IVOID
372ret
373end
374op 21 4
375w IU64
376w 64
377w 0
378op 23 4
379w IV3
380w IU64
381w 3
382op 32 4
383w IPIN
384w 1
385w IV3
386op 32 4
387w IPF
388w 5
389w IF32
390op 33 add 4 nbuf
391w IFN
392w IVOID
393for i uint nbuf
394w IPF
395end
396w IU32
397end
398
399note Index constants k = 0..16 at ids IK0 + k: ulong for cl (two literal words), uint for vk.
400note rmsnorm also emits 1, eps and 1/16 at IONE, IEPS, IINV.
401fn consts to void
402for k uint 17
403if isvk
404op 43 4
405w IU32
406w add IK0 k
407w k
408end
409if not isvk
410op 43 5
411w IU64
412w add IK0 k
413w k
414w 0
415end
416end
417if eq get gst 3 6
418if isvk
419op 43 4
420w IU32
421w I256
422w 256
423end
424if not isvk
425op 43 5
426w IU64
427w I256
428w 256
429w 0
430end
431op 43 4
432w IF32
433w IZERO
434w 0
435ret
436end
437if ne get gst 3 5
438ret
439end
440op 43 4
441w IF32
442w IONE
443w 0x3f800000
444op 43 4
445w IF32
446w IEPS
447w 0x358637bd
448op 43 4
449w IF32
450w IINV
451w 0x3d800000
452end
453
454fn vars to void
455if isvk
456for i uint nbuf
457op 59 4
458w IPST
459w bufid i
460w 12
461end
462end
463op 59 4
464w IPIN
465w IGID
466w 1
467end
468
469fn idxty to uint
470if isvk
471ret IU32
472end
473ret IU64
474end
475
476fn eptr b uint x uint to uint
477p = nid
478if isvk
479op 65 6
480w IPF
481w p
482w b
483w IK0
484w x
485ret p
486end
487op 70 5
488w IPF
489w p
490w b
491w x
492ret p
493end
494
495fn ld p uint to uint
496r = nid
497if isvk
498op 61 4
499w IF32
500w r
501w p
502ret r
503end
504op 61 6
505w IF32
506w r
507w p
508w 2
509w 4
510ret r
511end
512
513fn st p uint v uint to void
514if isvk
515op 62 3
516w p
517w v
518ret
519end
520op 62 5
521w p
522w v
523w 2
524w 4
525end
526
527fn farith c uint a uint b uint to uint
528r = nid
529op c 5
530w IF32
531w r
532w a
533w b
534ret r
535end
536
537fn farithid c uint a uint b uint r uint to uint
538op c 5
539w IF32
540w r
541w a
542w b
543ret r
544end
545
546fn iarith c uint a uint b uint to uint
547r = nid
548op c 5
549w idxty
550w r
551w a
552w b
553ret r
554end
555
556note Elementwise binary kernels: vadd FAdd 129, vmul FMul 133, vsub FSub 131; mutants swap the opcode.
557fn binop to uint
558c = uint 129
559if eq get gst 3 2
560c = 133
561end
562if eq get gst 3 3
563c = 131
564end
565if eq get gst 4 1
566c = 129
567if eq get gst 3 0
568c = 131
569end
570end
571ret c
572end
573
574fn bvmax a uint b uint to uint
575c = nid
576op 186 5
577w IBOOL
578w c
579w a
580w b
581r = nid
582op 169 6
583w IF32
584w r
585w c
586if eq get gst 4 1
587w b
588w a
589ret r
590end
591w a
592w b
593ret r
594end
595
596fn bvadd x uint to void
597a = ld eptr IB0 x
598b = ld eptr IB1 x
599if eq get gst 3 4
600st eptr IB2 x bvmax a b
601ret
602end
603s = farith binop a b
604st eptr IB2 x s
605end
606
607fn fsel i uint to uint
608if eq get gst 4 1
609ret sub 15 i
610end
611ret i
612end
613
614fn fld bs uint k uint to uint
615x = bs
616if ne k 0
617x = iarith 128 bs add IK0 k
618end
619ret ld eptr IB0 x
620end
621
622fn sqrtv a uint to uint
623r = nid
624op 12 6
625w IF32
626w r
627w IEXT
628if isvk
629w 31
630end
631if not isvk
632w 61
633end
634w a
635ret r
636end
637
638fn fldat bs uint k uint to uint
639x = bs
640if ne k 0
641x = iarith 128 bs add IK0 k
642end
643ret ld eptr IB0 x
644end
645
646note rmsnorm: left-to-right sum of squares, then (x * (1 / sqrt(ss * 1/16 + eps))) * w.
647note The mutant reverses only the sum, the same way fsum16 does.
648fn brms x uint to void
649bs = iarith 132 x IK16
650z = fld bs fsel 0
651acc = farith 133 z z
652for i uint 15
653v = fld bs fsel add i 1
654acc = farith 129 acc farith 133 v v
655end
656acc = farith 133 acc IINV
657acc = farith 129 acc IEPS
658acc = farith 136 IONE sqrtv acc
659for i uint 16
660xv = fldat bs i
661p = farith 133 xv acc
662yv = farith 133 p ld eptr IB1 add IK0 i
663idx = bs
664if ne i 0
665idx = iarith 128 bs add IK0 i
666end
667st eptr IB2 idx yv
668end
669end
670
671fn bfsum x uint to void
672bs = iarith 132 x IK16
673acc = fld bs fsel 0
674for i uint 15
675v = fld bs fsel add i 1
676acc = farith 129 acc v
677end
678st eptr IB1 x acc
679end
680
681note gemm16: C = 0; for k left to right, C = C + (A[row,k] * B[k,col]).
682note Mul is OpFMul 133 and add is OpFAdd 129, each with its own result id (NoContraction).
683note The mutant walks k from 15 down to 0. No OpFma.
684fn gacc rowb uint j uint base uint s uint acc uint to uint
685q = fsel s
686q = add IK0 q
687a = iarith 128 rowb q
688a = iarith 128 base a
689b = iarith 132 q IK16
690b = iarith 128 b j
691b = iarith 128 base b
692a = ld eptr IB0 a
693b = ld eptr IB1 b
694q = farithid 133 a b add IMUL0 s
695ret farithid 129 acc q add IADD0 s
696end
697
698fn bgemm x uint to void
699p = iarith 134 x I256
700q = iarith 137 x I256
701i = iarith 134 q IK16
702j = iarith 137 q IK16
703rowb = iarith 132 i IK16
704base = iarith 132 p I256
705acc = IZERO
706for s uint 16
707acc = gacc rowb j base s acc
708end
709st eptr IB2 x acc
710end
711
712fn count x uint to uint
713n = nid
714if isvk
715op 68 5
716w IU32
717w n
718w outbuf
719w 0
720if ne get gst 3 5
721ret n
722end
723d = nid
724op 194 5
725w IU32
726w d
727w n
728w add IK0 4
729ret d
730end
731op 113 4
732w IU64
733w n
734w IN
735ret n
736end
737
738fn body to void
739op 54 5
740w IVOID
741w IMAIN
742w 0
743w IFN
744if not isvk
745for i uint nbuf
746op 55 3
747w IPF
748w bufid i
749end
750op 55 3
751w IU32
752w IN
753end
754op 248 2
755w nid
756v = nid
757op 61 4
758w IV3
759w v
760w IGID
761x = nid
762op 81 5
763w idxty
764w x
765w v
766w 0
767n = count x
768c = nid
769op 176 5
770w IBOOL
771w c
772w x
773w n
774tl = nid
775ml = nid
776op 247 3
777w ml
778w 0
779op 250 4
780w c
781w tl
782w ml
783op 248 2
784w tl
785k = get gst 3
786if eq k 1
787bfsum x
788end
789if eq k 5
790brms x
791end
792if eq k 6
793bgemm x
794end
795if ne k 1
796if ne k 5
797if ne k 6
798bvadd x
799end
800end
801end
802op 249 2
803w ml
804op 248 2
805w ml
806op 253 1
807op 56 1
808end
809
810fn ob o ulong v uint to void
811for k ulong 4
812put gob add o k uchar and shr v mul uint k 8 255
813end
814end
815
816fn flush to void
817nw = get gst 0
818ob 0 0x07230203
819if isvk
820ob 4 0x00010300
821end
822if not isvk
823ob 4 0x00010000
824end
825ob 8 0
826ob 12 uint get gst 1
827ob 16 0
828for i ulong nw
829ob add 20 mul i 4 get gw i
830end
831tot = add 20 mul nw 4
832r = write 1 gob 0 tot
833if ne r long tot
834n = write 2 tio 0 len tio
835exit 3
836end
837end
838
839fn main to int
840ac = argcnt
841if or lt ac 3 gt ac 4
842usage
843end
844put gst 3 9
845if argis 1 tvadd
846put gst 3 0
847end
848if argis 1 tfsum
849put gst 3 1
850end
851if argis 1 tvmul
852put gst 3 2
853end
854if argis 1 tvsub
855put gst 3 3
856end
857if argis 1 tvmax
858put gst 3 4
859end
860if argis 1 trms
861put gst 3 5
862end
863if argis 1 tgemm
864put gst 3 6
865end
866put gst 2 9
867if argis 2 tcl
868put gst 2 0
869end
870if argis 2 tvk
871put gst 2 1
872end
873if or eq get gst 3 9 eq get gst 2 9
874usage
875end
876if eq ac 4
877if not argis 3 tmut
878usage
879end
880put gst 4 1
881end
882put gst 1 ulong IDYN
883header
884decor
885types
886consts
887vars
888body
889flush
890ret 0
891end