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

511 lines · 7349 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2note val.om: pure seeded-Omega SPIR-V binary validator (replaces external spirv-val).
3note Checks header, framing, profile capabilities/memory-model/decorations (opencl2.2 vs vulkan1.1),
4note SSA result-ID uniqueness and bounds, OpEntryPoint target, and function/CFG block structure.
5note
6note Usage:
7note val [--target-env opencl2.2|vulkan1.1] FILE.spv
8note Exit: 0 valid; 1 invalid (diagnostic on stderr); 2 usage; 3 I/O error. Traps exit 100 + code.
9om2
10
11text tuse "usage: val [--target-env opencl2.2|vulkan1.1] FILE.spv\n"
12text tio "val: I/O error\n"
13text tenv "--target-env"
14text tocl "opencl2.2"
15text tvk "vulkan1.1"
16text te_hdr "val: bad SPIR-V header or magic\n"
17text te_frm "val: truncated or malformed instruction framing\n"
18text te_id "val: invalid or duplicate SSA id\n"
19text te_prof "val: profile mismatch (memory model, capability, execution mode, or decoration)\n"
20text te_cfg "val: invalid function or CFG block structure\n"
21text te_ep "val: missing or undefined entry point\n"
22
23const MAXIDS ulong 4096
24
25global gspv uchar 65536
26global gw uint 16384
27global gdef uchar 4096
28global gneed uchar 4096
29global gpath uchar 4096
30global garg uchar 32
31note gst:
32note 0 env (0 auto, 1 cl, 2 vk), 1 nwords, 2 id_bound, 3 caps_mask,
33note 4 mem_seen, 5 ep_seen, 6 ep_fn, 7 in_fn, 8 blk_state (0 params, 1 open, 2 term),
34note 9 expect_brc (1 after OpSelectionMerge), 10 fn_count, 11 file_arg_idx
35global gst ulong 16
36
37fn fail code int msg view uchar * to void
38n = write 2 msg 0 len msg
39exit code
40end
41
42fn argis i ulong t view uchar * to bool
43n = argread i garg 0 len garg
44if ne n long len t
45ret false
46end
47for k ulong len t
48if ne get garg k get t k
49ret false
50end
51end
52ret true
53end
54
55fn spvu32 i ulong to uint
56o = mul i 4
57v = uint 0
58for k ulong 4
59v = or v shl uint get gspv add o k mul uint k 8
60end
61ret v
62end
63
64fn readspv ai ulong to void
65n = argread ai gpath 0 len gpath
66if le n 0
67fail 2 tuse
68end
69fd = fopen gpath 0 ulong n 0 0
70if lt fd 0
71fail 3 tio
72end
73sz = fseek int fd 0 2
74if or lt sz 20 gt sz 65536
75c = fclose int fd
76fail 1 te_hdr
77end
78if ne rem ulong sz 4 0
79c = fclose int fd
80fail 1 te_hdr
81end
82r = pread int fd gspv 0 ulong sz 0
83c = fclose int fd
84if ne r sz
85fail 3 tio
86end
87nw = div ulong sz 4
88put gst 1 nw
89for i ulong nw
90put gw i spvu32 i
91end
92end
93
94fn chk_id id uint to void
95if or eq id 0 ge ulong id get gst 2
96fail 1 te_id
97end
98end
99
100fn def_id id uint kind uchar to void
101chk_id id
102if ne get gdef ulong id 0
103fail 1 te_id
104end
105put gdef ulong id kind
106end
107
108fn need_lbl id uint to void
109chk_id id
110put gneed ulong id 1
111end
112
113fn chk_header to void
114if ne get gw 0 0x07230203
115fail 1 te_hdr
116end
117ver = get gw 1
118if or lt ver 0x00010000 gt ver 0x00010600
119fail 1 te_hdr
120end
121if eq get gst 0 1
122if gt ver 0x00010400
123fail 1 te_hdr
124end
125end
126if eq get gst 0 2
127if gt ver 0x00010300
128fail 1 te_hdr
129end
130end
131bnd = ulong get gw 3
132if or eq bnd 0 gt bnd MAXIDS
133fail 1 te_hdr
134end
135if ne get gw 4 0
136fail 1 te_hdr
137end
138put gst 2 bnd
139end
140
141fn chk_cap cap uint to void
142m = get gst 3
143if eq cap 1
144put gst 3 or m 1
145end
146if eq cap 4
147put gst 3 or m 2
148end
149if eq cap 6
150put gst 3 or m 4
151end
152if eq cap 11
153put gst 3 or m 8
154end
155end
156
157fn chk_memmodel addr uint mem uint to void
158if ne get gst 4 0
159fail 1 te_prof
160end
161put gst 4 1
162env = get gst 0
163if eq env 0
164if and eq addr 2 eq mem 2
165put gst 0 1
166ret
167end
168if and eq addr 0 eq mem 1
169put gst 0 2
170ret
171end
172fail 1 te_prof
173end
174if eq env 1
175if or ne addr 2 ne mem 2
176fail 1 te_prof
177end
178ret
179end
180if or ne addr 0 ne mem 1
181fail 1 te_prof
182end
183end
184
185fn chk_ep em uint fnid uint to void
186chk_id fnid
187env = get gst 0
188if eq env 1
189if ne em 6
190fail 1 te_prof
191end
192end
193if eq env 2
194if ne em 5
195fail 1 te_prof
196end
197end
198put gst 5 1
199put gst 6 ulong fnid
200end
201
202fn chk_execmode mode uint to void
203if and eq mode 31 eq get gst 0 2
204fail 1 te_prof
205end
206end
207
208fn chk_decor dec uint to void
209if and eq dec 42 eq get gst 0 1
210fail 1 te_prof
211end
212end
213
214fn is_type_op op uint to bool
215if eq op 19
216ret true
217end
218if eq op 20
219ret true
220end
221if eq op 21
222ret true
223end
224if eq op 22
225ret true
226end
227if eq op 23
228ret true
229end
230if eq op 29
231ret true
232end
233if eq op 30
234ret true
235end
236if eq op 32
237ret true
238end
239ret eq op 33
240end
241
242fn chk_global_op op uint p ulong wc ulong to void
243if eq op 17
244if ne wc 2
245fail 1 te_frm
246end
247chk_cap get gw add p 1
248ret
249end
250if eq op 11
251if lt wc 3
252fail 1 te_frm
253end
254def_id get gw add p 1 4
255ret
256end
257if eq op 14
258if ne wc 3
259fail 1 te_frm
260end
261chk_memmodel get gw add p 1 get gw add p 2
262ret
263end
264if eq op 15
265if lt wc 4
266fail 1 te_frm
267end
268chk_ep get gw add p 1 get gw add p 2
269ret
270end
271if eq op 16
272if lt wc 3
273fail 1 te_frm
274end
275chk_id get gw add p 1
276chk_execmode get gw add p 2
277ret
278end
279if eq op 71
280if lt wc 3
281fail 1 te_frm
282end
283chk_id get gw add p 1
284chk_decor get gw add p 2
285ret
286end
287if eq op 72
288if lt wc 4
289fail 1 te_frm
290end
291chk_id get gw add p 1
292ret
293end
294if is_type_op op
295if lt wc 2
296fail 1 te_frm
297end
298def_id get gw add p 1 1
299ret
300end
301if eq op 43
302if lt wc 4
303fail 1 te_frm
304end
305chk_id get gw add p 1
306def_id get gw add p 2 2
307ret
308end
309if eq op 59
310if lt wc 4
311fail 1 te_frm
312end
313chk_id get gw add p 1
314def_id get gw add p 2 3
315ret
316end
317fail 1 te_frm
318end
319
320fn chk_fn_op op uint p ulong wc ulong to void
321if eq get gst 9 1
322if ne op 250
323fail 1 te_cfg
324end
325put gst 9 0
326end
327if eq op 55
328if or ne wc 3 ne get gst 8 0
329fail 1 te_cfg
330end
331chk_id get gw add p 1
332def_id get gw add p 2 6
333ret
334end
335if eq op 248
336if ne wc 2
337fail 1 te_frm
338end
339if eq get gst 8 1
340fail 1 te_cfg
341end
342def_id get gw add p 1 7
343put gst 8 1
344ret
345end
346if ne get gst 8 1
347fail 1 te_cfg
348end
349if eq op 247
350if lt wc 3
351fail 1 te_frm
352end
353need_lbl get gw add p 1
354put gst 9 1
355ret
356end
357if eq op 249
358if ne wc 2
359fail 1 te_frm
360end
361need_lbl get gw add p 1
362put gst 8 2
363ret
364end
365if eq op 250
366if lt wc 4
367fail 1 te_frm
368end
369chk_id get gw add p 1
370need_lbl get gw add p 2
371need_lbl get gw add p 3
372put gst 8 2
373ret
374end
375if eq op 253
376if ne wc 1
377fail 1 te_frm
378end
379put gst 8 2
380ret
381end
382if eq op 62
383if lt wc 3
384fail 1 te_frm
385end
386chk_id get gw add p 1
387chk_id get gw add p 2
388ret
389end
390if lt wc 4
391fail 1 te_frm
392end
393chk_id get gw add p 1
394def_id get gw add p 2 8
395end
396
397fn chk_labels to void
398bnd = get gst 2
399for i ulong bnd
400if eq get gneed i 1
401if ne get gdef i 7
402fail 1 te_cfg
403end
404put gneed i 0
405end
406end
407end
408
409fn validate to void
410chk_header
411nw = get gst 1
412p = ulong 5
413while lt p nw bound 16384
414hd = get gw p
415wc = ulong shr hd 16
416op = and hd 65535
417if or eq wc 0 gt add p wc nw
418fail 1 te_frm
419end
420if eq op 54
421if or ne get gst 7 0 ne wc 5
422fail 1 te_cfg
423end
424chk_id get gw add p 1
425def_id get gw add p 2 5
426chk_id get gw add p 4
427put gst 7 1
428put gst 8 0
429put gst 9 0
430put gst 10 add get gst 10 1
431p = add p wc
432continue
433end
434if eq op 56
435if ne get gst 7 1
436fail 1 te_cfg
437end
438if ne wc 1
439fail 1 te_cfg
440end
441if ne get gst 8 2
442fail 1 te_cfg
443end
444if ne get gst 9 0
445fail 1 te_cfg
446end
447chk_labels
448put gst 7 0
449p = add p wc
450continue
451end
452if eq get gst 7 1
453chk_fn_op op p wc
454else
455chk_global_op op p wc
456end
457p = add p wc
458end
459if ne get gst 7 0
460fail 1 te_cfg
461end
462if eq get gst 4 0
463fail 1 te_cfg
464end
465if eq get gst 5 0
466fail 1 te_cfg
467end
468if ne get gdef get gst 6 5
469fail 1 te_ep
470end
471caps = get gst 3
472if eq get gst 0 1
473if ne and caps 14 14
474fail 1 te_prof
475end
476end
477if eq get gst 0 2
478if eq and caps 1 0
479fail 1 te_prof
480end
481end
482end
483
484fn main to int
485ac = argcnt
486if eq ac 2
487put gst 0 0
488put gst 11 1
489else
490if eq ac 4
491if not argis 1 tenv
492fail 2 tuse
493end
494if argis 2 tocl
495put gst 0 1
496else
497if argis 2 tvk
498put gst 0 2
499else
500fail 2 tuse
501end
502end
503put gst 11 3
504else
505fail 2 tuse
506end
507end
508readspv get gst 11
509validate
510ret 0
511end