interplane.Get Omega
Read in your language

CUDApp

CUDA++: AIEN’s GPU machine-code toolchain, runtime and driver client for NVIDIA GPUs without the CUDA toolkit. Hardware coverage and driver replacement limits are explicit.

Brought by drakestapleton

branch main Branches Tags History

CUDApp / backend / spirv / emit.om

576 lines · 7160 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 fsum16 o[gid] = (((x[16g] + x[16g+1]) + x[16g+2]) ... + x[16g+15]) frozen left-to-right order
15note cl arguments: vadd (a, b, c, uint n), fsum16 (x, o, uint n).
16note vk bindings set 0: vadd a=0 b=1 c=2, fsum16 x=0 o=1; n = OpArrayLength of the output buffer;
17note LocalSize 64 1 1.
18note
19note Usage: emit vadd|fsum16 cl|vk [mutant]
20note mutant plants a wrong kernel for negative controls: vadd uses OpFSub, fsum16 adds right-to-left.
21note Exit: 0 module written; 2 usage; 3 stdout write failed. Traps exit 100 + code.
22om2
23
24text tuse "usage: emit vadd|fsum16 cl|vk [mutant]\n"
25text tio "emit: stdout write failed\n"
26text tvadd "vadd"
27text tfsum "fsum16"
28text tcl "cl"
29text tvk "vk"
30text tmut "mutant"
31
32const IVOID uint 1
33const IU32 uint 2
34const IU64 uint 3
35const IF32 uint 4
36const IBOOL uint 5
37const IV3 uint 6
38const IPIN uint 7
39const IPF uint 8
40const IFN uint 9
41const IGID uint 10
42const IMAIN uint 11
43const IRTA uint 12
44const IST uint 13
45const IPST uint 14
46const IB0 uint 15
47const IB1 uint 16
48const IB2 uint 17
49const IN uint 18
50const IK0 uint 20
51const IK16 uint 36
52const IDYN uint 40
53
54global gw uint 4096
55global gob uchar 16384
56global garg uchar 16
57note gst: 0 word fill, 1 next id, 2 env (0 cl, 1 vk), 3 kernel (0 vadd, 1 fsum16), 4 mutant
58global gst ulong 8
59
60fn usage to void
61n = write 2 tuse 0 len tuse
62exit 2
63end
64
65fn w v uint to void
66f = get gst 0
67put gw f v
68put gst 0 add f 1
69end
70
71fn op c uint n uint to void
72w or shl n 16 c
73end
74
75fn nid to uint
76i = get gst 1
77put gst 1 add i 1
78ret uint i
79end
80
81fn isvk to bool
82ret eq get gst 2 1
83end
84
85note Literal string: bytes little-endian in words, a zero byte always ends it, zero padded.
86fn strwc t view uchar * to uint
87ret uint add div len t 4 1
88end
89
90fn wstr t view uchar * to void
91nw = ulong strwc t
92for i ulong nw
93v = uint 0
94for k ulong 4
95j = add mul i 4 k
96if lt j len t
97v = or v shl uint get t j mul uint k 8
98end
99end
100w v
101end
102end
103
104fn argis i ulong t view uchar * to bool
105n = argread i garg 0 len garg
106if ne n long len t
107ret false
108end
109for k ulong len t
110if ne get garg k get t k
111ret false
112end
113end
114ret true
115end
116
117fn kname to void
118if eq get gst 3 0
119wstr tvadd
120ret
121end
122wstr tfsum
123end
124
125fn knamewc to uint
126if eq get gst 3 0
127ret strwc tvadd
128end
129ret strwc tfsum
130end
131
132fn nbuf to uint
133if eq get gst 3 0
134ret 3
135end
136ret 2
137end
138
139note Buffer i (0-based) as an id: cl parameters and vk variables share IB0..IB2.
140fn bufid i uint to uint
141ret add IB0 i
142end
143
144fn outbuf to uint
145ret bufid sub nbuf 1
146end
147
148fn header to void
149if isvk
150op 17 2
151w 1
152op 14 3
153w 0
154w 1
155op 15 add 4 knamewc
156w 5
157w IMAIN
158kname
159w IGID
160op 16 6
161w IMAIN
162w 17
163w 64
164w 1
165w 1
166ret
167end
168op 17 2
169w 4
170op 17 2
171w 6
172op 17 2
173w 11
174op 14 3
175w 2
176w 2
177op 15 add 4 knamewc
178w 6
179w IMAIN
180kname
181w IGID
182end
183
184fn decor to void
185op 71 4
186w IGID
187w 11
188w 28
189if not isvk
190op 71 3
191w IGID
192w 22
193ret
194end
195op 71 4
196w IRTA
197w 6
198w 4
199op 72 5
200w IST
201w 0
202w 35
203w 0
204op 71 3
205w IST
206w 2
207for i uint nbuf
208op 71 4
209w bufid i
210w 34
211w 0
212op 71 4
213w bufid i
214w 33
215w i
216end
217end
218
219fn types to void
220op 19 2
221w IVOID
222op 21 4
223w IU32
224w 32
225w 0
226op 22 3
227w IF32
228w 32
229op 20 2
230w IBOOL
231if isvk
232op 23 4
233w IV3
234w IU32
235w 3
236op 32 4
237w IPIN
238w 1
239w IV3
240op 29 3
241w IRTA
242w IF32
243op 30 3
244w IST
245w IRTA
246op 32 4
247w IPST
248w 12
249w IST
250op 32 4
251w IPF
252w 12
253w IF32
254op 33 3
255w IFN
256w IVOID
257ret
258end
259op 21 4
260w IU64
261w 64
262w 0
263op 23 4
264w IV3
265w IU64
266w 3
267op 32 4
268w IPIN
269w 1
270w IV3
271op 32 4
272w IPF
273w 5
274w IF32
275op 33 add 4 nbuf
276w IFN
277w IVOID
278for i uint nbuf
279w IPF
280end
281w IU32
282end
283
284note Index constants k = 0..16 at ids IK0 + k: ulong for cl (two literal words), uint for vk.
285fn consts to void
286for k uint 17
287if isvk
288op 43 4
289w IU32
290w add IK0 k
291w k
292end
293if not isvk
294op 43 5
295w IU64
296w add IK0 k
297w k
298w 0
299end
300end
301end
302
303fn vars to void
304if isvk
305for i uint nbuf
306op 59 4
307w IPST
308w bufid i
309w 12
310end
311end
312op 59 4
313w IPIN
314w IGID
315w 1
316end
317
318fn idxty to uint
319if isvk
320ret IU32
321end
322ret IU64
323end
324
325fn eptr b uint x uint to uint
326p = nid
327if isvk
328op 65 6
329w IPF
330w p
331w b
332w IK0
333w x
334ret p
335end
336op 70 5
337w IPF
338w p
339w b
340w x
341ret p
342end
343
344fn ld p uint to uint
345r = nid
346if isvk
347op 61 4
348w IF32
349w r
350w p
351ret r
352end
353op 61 6
354w IF32
355w r
356w p
357w 2
358w 4
359ret r
360end
361
362fn st p uint v uint to void
363if isvk
364op 62 3
365w p
366w v
367ret
368end
369op 62 5
370w p
371w v
372w 2
373w 4
374end
375
376fn farith c uint a uint b uint to uint
377r = nid
378op c 5
379w IF32
380w r
381w a
382w b
383ret r
384end
385
386fn iarith c uint a uint b uint to uint
387r = nid
388op c 5
389w idxty
390w r
391w a
392w b
393ret r
394end
395
396fn bvadd x uint to void
397a = ld eptr IB0 x
398b = ld eptr IB1 x
399c = uint 129
400if eq get gst 4 1
401c = 131
402end
403s = farith c a b
404st eptr IB2 x s
405end
406
407fn fsel i uint to uint
408if eq get gst 4 1
409ret sub 15 i
410end
411ret i
412end
413
414fn fld bs uint k uint to uint
415x = bs
416if ne k 0
417x = iarith 128 bs add IK0 k
418end
419ret ld eptr IB0 x
420end
421
422fn bfsum x uint to void
423bs = iarith 132 x IK16
424acc = fld bs fsel 0
425for i uint 15
426v = fld bs fsel add i 1
427acc = farith 129 acc v
428end
429st eptr IB1 x acc
430end
431
432fn count x uint to uint
433n = nid
434if isvk
435op 68 5
436w IU32
437w n
438w outbuf
439w 0
440ret n
441end
442op 113 4
443w IU64
444w n
445w IN
446ret n
447end
448
449fn body to void
450op 54 5
451w IVOID
452w IMAIN
453w 0
454w IFN
455if not isvk
456for i uint nbuf
457op 55 3
458w IPF
459w bufid i
460end
461op 55 3
462w IU32
463w IN
464end
465op 248 2
466w nid
467v = nid
468op 61 4
469w IV3
470w v
471w IGID
472x = nid
473op 81 5
474w idxty
475w x
476w v
477w 0
478n = count x
479c = nid
480op 176 5
481w IBOOL
482w c
483w x
484w n
485tl = nid
486ml = nid
487op 247 3
488w ml
489w 0
490op 250 4
491w c
492w tl
493w ml
494op 248 2
495w tl
496if eq get gst 3 0
497bvadd x
498end
499if eq get gst 3 1
500bfsum x
501end
502op 249 2
503w ml
504op 248 2
505w ml
506op 253 1
507op 56 1
508end
509
510fn ob o ulong v uint to void
511for k ulong 4
512put gob add o k uchar and shr v mul uint k 8 255
513end
514end
515
516fn flush to void
517nw = get gst 0
518ob 0 0x07230203
519if isvk
520ob 4 0x00010300
521end
522if not isvk
523ob 4 0x00010000
524end
525ob 8 0
526ob 12 uint get gst 1
527ob 16 0
528for i ulong nw
529ob add 20 mul i 4 get gw i
530end
531tot = add 20 mul nw 4
532r = write 1 gob 0 tot
533if ne r long tot
534n = write 2 tio 0 len tio
535exit 3
536end
537end
538
539fn main to int
540ac = argcnt
541if or lt ac 3 gt ac 4
542usage
543end
544put gst 3 9
545if argis 1 tvadd
546put gst 3 0
547end
548if argis 1 tfsum
549put gst 3 1
550end
551put gst 2 9
552if argis 2 tcl
553put gst 2 0
554end
555if argis 2 tvk
556put gst 2 1
557end
558if or eq get gst 3 9 eq get gst 2 9
559usage
560end
561if eq ac 4
562if not argis 3 tmut
563usage
564end
565put gst 4 1
566end
567put gst 1 ulong IDYN
568header
569decor
570types
571consts
572vars
573body
574flush
575ret 0
576end