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 / amd / kfd / launch / plan.om

611 lines · 14349 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2note OMPLAN01 trusted first-party dispatch plans; all file and relocation bounds preflight before KFD.
3note ps: planmap,fd,size,ncode,nfile,nbuf,ndisp,nprompt,ngen,tok,strOff,strLen,dataOff,dataLen,bufOff,promptOff,dispOff,codemeta,filemeta,dispmeta,packets,argbytes,ids,bufoffsets,VRAMbytes,GTTbytes.
4global ps ulong 26
5global planoptions ulong 1
6text planmode "plan"
7text planstep "OMEGA_STEP "
8text plantok " tok="
9text planus " us="
10text planready "OMEGA_PLAN ready\n"
11text plandone "OMEGA_PLAN complete\n"
12text planids "OMEGA_PLAN ids=["
13text plancomma ","
14text planend "]\n"
15text planrefusallabel "OMEGA refuse plan field="
16text preason0 "add-overflow\n"
17text preason1 "multiply-overflow\n"
18text preason2 "span-offset\n"
19text preason3 "span-size\n"
20text preason4 "word-width\n"
21text preason5 "padding\n"
22text preason6 "magic\n"
23text preason7 "code-count\n"
24text preason8 "count\n"
25text preason9 "token-index\n"
26text preason10 "argc\n"
27text preason11 "pool-layout\n"
28text preason12 "data-layout\n"
29text preason13 "name\n"
30text preason14 "buffer-size\n"
31text preason15 "buffer-flags\n"
32text preason16 "buffer-kind\n"
33text preason17 "zero-buffer\n"
34text preason18 "data-index\n"
35text preason19 "file-index\n"
36text preason20 "widen-size\n"
37text preason21 "duplicate-relocation\n"
38text preason22 "relocation-alignment\n"
39text preason23 "relocation-pointer\n"
40text preason24 "relocation-buffer\n"
41text preason25 "code-index\n"
42text preason26 "grid\n"
43text preason27 "workgroup\n"
44text preason28 "kernarg-size\n"
45text preason29 "relocation-count\n"
46text preason30 "ring-size\n"
47text preason31 "token-buffer\n"
48text preason32 "dispatch-end\n"
49fn planrefuse reason view uchar * to void
50r = write 1 planrefusallabel 0 len planrefusallabel
51r = write 1 reason 0 len reason
52exit 64
53end
54fn padd a ulong b ulong to ulong
55if or gt a 9223372036854775807 gt b sub ulong 9223372036854775807 a
56planrefuse preason0
57end
58ret add a b
59end
60fn pmul a ulong b ulong to ulong
61if ne b 0
62if gt a div ulong 9223372036854775807 b
63planrefuse preason1
64end
65end
66ret mul a b
67end
68fn pspan off ulong n ulong total ulong to void
69if gt off total
70planrefuse preason2
71end
72if gt n sub total off
73planrefuse preason3
74end
75end
76fn pw off ulong width ulong to ulong
77pspan off width get ps 2
78if gt width 8
79planrefuse preason4
80end
81b = new uchar 8 0
82r = checked mapread long get ps 0 off b 0 width
83v = ulong 0
84for i ulong width bound 8
85v = or v shl ulong get b i mul i 8
86end
87ret v
88end
89fn planpad off ulong limit ulong to ulong
90last = roundup off 8
91pspan off sub last off limit
92for i ulong sub last off bound 7
93if ne pw add off i 1 0
94planrefuse preason5
95end
96end
97ret last
98end
99fn pb i ulong byteoff ulong to ulong
100ret pw add add get ps 14 pmul i 48 byteoff 8
101end
102fn cm i ulong byteoff ulong to ulong
103ret mapget long get ps 17 add pmul i 96 byteoff 8
104end
105fn fm i ulong byteoff ulong to ulong
106ret mapget long get ps 18 add pmul i 16 byteoff 8
107end
108fn dm i ulong byteoff ulong to ulong
109ret mapget long get ps 19 add pmul i 32 byteoff 8
110end
111fn restoreel i ulong to void
112for j ulong 12
113put el j cm i mul j 8
114end
115end
116fn planhead to void
117fd = argfile 2 0
118put ps 1 ulong fd
119put ps 2 filesize fd
120pspan 0 96 get ps 2
121put ps 0 ulong checked mapnew fd 0 get ps 2 1 2
122if ne pw 0 8 0x31304e414c504d4f
123planrefuse preason6
124end
125for i ulong 7
126put ps add i 3 pw mul add i 1 8 8
127end
128if eq get ps 3 0
129planrefuse preason7
130end
131for i ulong 4
132if eq get ps add i 5 0
133planrefuse preason8
134end
135end
136if ge get ps 9 get ps 5
137planrefuse preason9
138end
139expected = padd 3 padd get ps 3 get ps 4
140put planoptions 0 0
141if ne ulong argcnt expected
142if ne ulong argcnt padd expected 1
143planrefuse preason10
144end
145put planoptions 0 argnum expected
146end
147for i ulong 4
148put ps add 10 i pw add sub get ps 2 32 mul i 8 8
149end
150if ne padd get ps 10 get ps 11 get ps 12
151planrefuse preason11
152end
153if ne padd get ps 12 get ps 13 sub get ps 2 32
154planrefuse preason12
155end
156put ps 14 padd 64 pmul get ps 3 16
157put ps 15 padd get ps 14 pmul get ps 5 48
158put ps 16 planpad padd get ps 15 pmul get ps 7 4 get ps 10
159pspan 64 sub get ps 16 64 get ps 10
160pspan get ps 16 pmul get ps 6 32 get ps 10
161steps = sub padd get ps 7 get ps 8 1
162n = pmul steps get ps 6
163put ps 17 ulong checked mapnew -1 0 pmul get ps 3 96 3 34
164put ps 18 ulong checked mapnew -1 0 pmul padd get ps 4 1 16 3 34
165put ps 19 ulong checked mapnew -1 0 pmul get ps 6 32 3 34
166put ps 22 ulong checked mapnew -1 0 pmul get ps 8 4 3 34
167end
168fn planname i ulong to ulong
169pos = add 64 mul i 16
170off = pw pos 8
171n = pw add pos 8 8
172pspan off n get ps 11
173if or eq n 0 gt n len pathbuf
174planrefuse preason13
175end
176r = checked mapread long get ps 0 add get ps 10 off pathbuf 0 n
177ret n
178end
179fn plansaveel i ulong to void
180for j ulong 12
181r = checked mapset long get ps 17 add mul i 96 mul j 8 8 get el j
182end
183end
184fn plancode i ulong to void
185fd = argfile add 3 i 0
186n = planname i
187parseopen fd n
188plansaveel i
189end
190fn plancodes to void
191for i ulong get ps 3 bound 9223372036854775807
192plancode i
193end
194end
195fn planfiles to void
196for i ulong get ps 4 bound 9223372036854775807
197fd = argfile add add 3 get ps 3 i 0
198r = checked mapset long get ps 18 mul i 16 8 ulong fd
199r = checked mapset long get ps 18 add mul i 16 8 8 filesize fd
200end
201end
202fn planbufcheck i ulong to void
203n = pb i 0
204kind = pb i 8
205idx = pb i 16
206off = pb i 24
207size = pb i 32
208flags = pb i 40
209if or eq n 0 gt n 9223372036854771712
210planrefuse preason14
211end
212if not or eq flags 1 eq flags 2
213planrefuse preason15
214end
215if gt kind 3
216planrefuse preason16
217end
218if eq kind 0
219if or ne idx 0 or ne off 0 ne size 0
220planrefuse preason17
221end
222else
223if eq kind 3
224if ne idx 0
225planrefuse preason18
226end
227pspan off size get ps 13
228else
229if ge idx get ps 4
230planrefuse preason19
231end
232pspan off size fm idx 8
233end
234end
235if eq kind 2
236if ne rem size 2 0
237planrefuse preason20
238end
239size = pmul size 2
240end
241pspan 0 size n
242end
243fn planduplicate pos ulong count ulong off ulong to void
244for k ulong count bound 9223372036854775807
245if eq off pw add pos mul k 16 4
246planrefuse preason21
247end
248end
249end
250fn planrelone p ulong raw ulong size ulong to void
251off = pw p 4
252if ne rem off 8 0
253planrefuse preason22
254end
255pspan off 8 size
256if ne pw add raw off 8 0
257planrefuse preason23
258end
259idx = pw add p 4 4
260if ge idx get ps 5
261planrefuse preason24
262end
263pspan pw add p 8 8 1 pb idx 0
264end
265fn planreloc pos ulong raw ulong size ulong count ulong to void
266for j ulong count bound 9223372036854775807
267planrelone add pos mul j 16 raw size
268planduplicate pos j pw add pos mul j 16 4
269end
270end
271fn planshape pos ulong to void
272pspan pos 32 get ps 10
273code = pw pos 4
274if ge code get ps 3
275planrefuse preason25
276end
277gx = pw add pos 4 4
278gy = pw add pos 8 4
279if or eq gx 0 or gt gx div ulong 4294967295 64 eq gy 0
280planrefuse preason26
281end
282if or ne pw add pos 12 4 1 or ne pw add pos 16 4 64 ne pw add pos 28 4 0
283planrefuse preason27
284end
285if ne pw add pos 20 4 cm code 64
286planrefuse preason28
287end
288end
289fn planstore i ulong pos ulong reloc ulong to void
290r = checked mapset long get ps 19 mul i 32 8 pos
291r = checked mapset long get ps 19 add mul i 32 8 8 reloc
292r = checked mapset long get ps 19 add mul i 32 16 8 get ps 21
293end
294fn plandispcheck pos ulong i ulong to ulong
295planshape pos
296size = pw add pos 20 4
297count = pw add pos 24 4
298if gt count div size 8
299planrefuse preason29
300end
301raw = add pos 32
302reloc = padd raw size
303last = padd reloc pmul count 16
304pspan pos sub last pos get ps 10
305planreloc reloc raw size count
306planstore i pos reloc
307put ps 21 padd get ps 21 roundup padd size 1 16
308ret planpad last get ps 10
309end
310fn planring to void
311note Keep the established 1024-byte AQL minimum, grow for the complete step.
312slots = ulong 16
313while lt slots padd get ps 6 1 bound 63
314slots = pmul slots 2
315end
316note CREATE_QUEUE ring_size is a 32-bit byte count; reject unrepresentable rings.
317if gt slots div ulong 4294967295 64
318planrefuse preason30
319end
320put launch 10 slots
321end
322fn plancheck to void
323planhead
324plancodes
325planfiles
326for i ulong get ps 5 bound 9223372036854775807
327planbufcheck i
328end
329if or lt pb get ps 9 0 4 ne pb get ps 9 40 2
330planrefuse preason31
331end
332pos = get ps 16
333for i ulong get ps 6 bound 9223372036854775807
334pos = plandispcheck pos i
335end
336if ne pos get ps 10
337planrefuse preason32
338end
339planlayout
340planring
341end
342fn bufoffset i ulong to ulong
343ret mapget long get ps 23 mul i 8 8
344end
345fn bufhandle i ulong to long
346ret lh add add 7 get ps 3 pb i 40
347end
348fn planlayout to void
349put ps 23 ulong checked mapnew -1 0 pmul get ps 5 8 3 34
350for i ulong get ps 5 bound 9223372036854775807
351slot = add 23 pb i 40
352r = checked mapset long get ps 23 mul i 8 8 get ps slot
353put ps slot padd get ps slot roundup pb i 0 4096
354end
355end
356fn planarenas to void
357for i ulong 2
358size = get ps add 24 i
359if ne size 0
360flags = ulong 0xa0000001
361if eq i 1
362flags = ulong 0xa6000002
363end
364h = allocate add add 8 get ps 3 i size flags
365end
366end
367end
368fn widenchunk h long off ulong n ulong to void
369for i ulong div n 2 bound 2048
370v = shl ulong get chunk mul i 2 16
371v = or v shl ulong get chunk add mul i 2 1 24
372r = checked mapset h add off mul i 4 4 v
373end
374end
375fn planwiden fd int off ulong h long dst ulong n ulong to void
376pos = ulong 0
377while lt pos n bound 9223372036854775807
378size = sub n pos
379if gt size len chunk
380size = len chunk
381end
382r = checked pread fd chunk 0 size long add off pos
383if or eq r 0 ne rem ulong r 2 0
384exit 65
385end
386widenchunk h add dst mul pos 2 ulong r
387pos = add pos ulong r
388end
389end
390fn planbufload i ulong to void
391h = bufhandle i
392base = bufoffset i
393kind = pb i 8
394used = pb i 32
395if eq kind 0
396used = ulong 0
397else
398if eq kind 3
399filecopy int get ps 1 add get ps 12 pb i 24 h base used
400else
401fd = int fm pb i 16 0
402if eq kind 2
403planwiden fd pb i 24 h base used
404used = mul used 2
405else
406filecopy fd pb i 24 h base used
407end
408end
409end
410zeromap h add base used sub pb i 0 used
411end
412fn planargs i ulong to void
413pos = dm i 0
414raw = add pos 32
415size = pw add pos 20 4
416argoff = dm i 16
417filecopy int get ps 1 raw lh 1 argoff size
418for j ulong pw add pos 24 4 bound 9223372036854775807
419p = add dm i 8 mul j 16
420off = pw p 4
421idx = pw add p 4 4
422value = padd mapaddr bufhandle idx padd bufoffset idx pw add p 8 8
423r = checked mapset lh 1 add argoff off 8 value
424end
425end
426fn plantemplate i ulong to void
427pos = dm i 0
428code = pw pos 4
429zerohdr
430hput 0 4 1
431hput 4 2 64
432hput 6 2 1
433hput 8 2 1
434hput 12 4 mul pw add pos 4 4 64
435hput 16 4 pw add pos 8 4
436hput 20 4 1
437note Descriptor metadata is group then private; the packet is private then group.
438hput 24 4 cm code 80
439hput 28 4 cm code 72
440hput 32 8 padd mapaddr lh add 8 code cm code 48
441hput 40 8 padd mapaddr lh 1 dm i 16
442if eq add i 1 get ps 6
443hput 56 8 mapaddr lh 7
444end
445r = checked mapwrite long get ps 20 mul i 64 hdr 0 64
446end
447fn planload to void
448n = padd 10 get ps 3
449put launch 0 ulong checked mapnew -1 0 pmul n 24 3 34
450put launch 1 sub n 8
451put launch 2 1
452put launch 3 get planoptions 0
453r = allocate 1 get ps 21 0xa0000001
454for i ulong get ps 3 bound 9223372036854775807
455restoreel i
456h = allocate add 8 i get el 7 0xe0000001
457loadelf h
458end
459planarenas
460for i ulong get ps 5 bound 9223372036854775807
461planbufload i
462end
463queueinit
464put ps 20 ulong checked mapnew -1 0 pmul get ps 6 64 3 34
465for i ulong get ps 6 bound 9223372036854775807
466planargs i
467plantemplate i
468end
469cpufence
470emit planready
471end
472fn plantimeout start long to void
473now = monons
474if lt now start
475exit 74
476end
477slowdispatch ulong sub now start
478if and ne get launch 3 0 gt ulong sub now start get launch 3
479stepbegin stwait 0
480steprow 110 false
481exit 75
482end
483end
484fn plancopy sent ulong to void
485pos = ulong 0
486size = pmul get ps 6 64
487ringbytes = pmul get launch 10 64
488while lt pos size bound 9223372036854775807
489slot = rem add mul rem sent get launch 10 64 pos ringbytes
490n = sub size pos
491if gt n len chunk
492n = len chunk
493end
494if gt n sub ringbytes slot
495n = sub ringbytes slot
496end
497r = checked mapread long get ps 20 pos chunk 0 n
498r = checked mapwrite lh 3 slot chunk 0 n
499pos = add pos n
500end
501end
502fn planpublish sent ulong start long to void
503while gt padd sub sent mapget lh 5 128 8 get ps 6 get launch 10 bound 9223372036854775807
504plantimeout start
505cpurelax
506end
507note Invalidate all owned slots, copy all bodies, then publish aligned headers.
508for i ulong get ps 6 bound 9223372036854775807
509r = checked mapset lh 3 mul rem add sent i get launch 10 64 4 1
510end
511cpufence
512plancopy sent
513cpufence
514for i ulong get ps 6 bound 9223372036854775807
515r = checked mapset lh 3 mul rem add sent i get launch 10 64 4 0x00021502
516end
517cpufence
518r = checked mapset lh 5 56 8 add sent get ps 6
519cpufence
520r = checked mapset long get launch 8 get launch 9 8 sub add sent get ps 6 1
521end
522fn recordtoken step ulong tok long to void
523if ge add step 1 get ps 7
524idx = sub add step 1 get ps 7
525r = checked mapset long get ps 22 mul idx 4 4 mapget tok bufoffset get ps 9 4
526end
527end
528fn printids to void
529emit planids
530for i ulong get ps 8 bound 9223372036854775807
531if ne i 0
532emit plancomma
533end
534printnum mapget long get ps 22 mul i 4 4
535end
536emit planend
537end
538fn planrun to void
539sent = ulong 0
540steps = sub add get ps 7 get ps 8 1
541tok = bufhandle get ps 9
542for step ulong steps bound 9223372036854775807
543if lt step get ps 7
544r = checked mapset tok bufoffset get ps 9 4 pw add get ps 15 mul step 4 4
545end
546r = checked mapset lh 7 8 8 1
547cpufence
548put dispatchalarm 0 0
549start = monons
550if lt start 0
551exit 74
552end
553planpublish sent start
554sent = add sent get ps 6
555while ne mapget lh 7 8 8 0 bound 9223372036854775807
556plantimeout start
557cpurelax
558end
559cpufence
560elapsed = sub monons start
561if lt elapsed 0
562exit 74
563end
564slowdispatch ulong elapsed
565recordtoken step tok
566emit planstep
567printnum step
568emit plantok
569printnum mapget tok bufoffset get ps 9 4
570emit planus
571printnum div ulong elapsed 1000
572emit newline
573end
574end
575fn planfree to void
576zerohdr
577hput 0 4 get launch 7
578r = checked kfdctl int get dev 0 3221768963 hdr 0 8
579r = checked mapdrop long get launch 8
580for i ulong add get launch 1 8 bound 9223372036854775807
581if ne mapget long get launch 0 mul i 24 8 0
582gpufree long get launch 0 mul i 24
583end
584end
585for i ulong get ps 3 bound 9223372036854775807
586r = checked fclose int cm i 0
587end
588for i ulong get ps 4 bound 9223372036854775807
589r = checked fclose int fm i 0
590end
591r = checked fclose int get dev 1
592r = checked fclose int get dev 0
593r = checked fclose int get ps 1
594r = checked mapdrop long get ps 0
595for i ulong 4
596r = checked mapdrop long get ps add 17 i
597end
598r = checked mapdrop long get launch 0
599r = checked mapdrop long get ps 23
600emit plandone
601printids
602r = checked mapdrop long get ps 22
603end
604fn planmain to int
605plancheck
606connectkfd
607planload
608planrun
609planfree
610ret 0
611end