interplane.Get Omega
Read in your language

cuda-crack

I broke CUDA: GB10 sm_121 machine code without the CUDA toolkit - method, encoder tables, receipts

Brought by drakestapleton

branch main Branches Tags History

cuda-crack / gen / isa.om

401 lines · 7626 bytes Raw Blame History
1note gen/isa.om: derive machine-readable sm_121 encoding tables from my Omega encoder by probing it.
2note Build: cat gen/bw_enc_sm121.om gen/isa.om | omc > isa-gen (omc reads one source from stdin, no imports).
3note Method: for every op 0..63 and every operand-presence/flag selector, encode one base instruction,
4note then flip one input bit at a time (register numbers, 32-bit immediate, barrier register) and diff
5note the 96 instruction bits (words 0..2; word 3 is the scheduling control word). Bits that never move
6note are the form's fixed bits (opcode and modifiers); bits that move belong to an operand field.
7note Output (decimal, space separated), one record per line:
8note F id op sel fix0 fix1 fix2 msk0 msk1 msk2 ctl
9note D id field m0 m1 m2 start width
10note sel bits: 0 dst 1 src1 2 src2 3 src3 4 ureg present, 5 uniform, 6 guard @P0, 7 guard negated.
11note field: 0 dst 1 src1 2 src2 3 src3 4 ureg 5 imm 6 barrier. start = bit (0..95) of input bit 0,
12note width = how many input bits map one-to-one onto consecutive output bits (0 when not linear).
13note op 255 is the NOP the encoder pads with.
14
15global obuf uchar 4194304
16global opos ulong 1
17global dg uchar 32
18global cw ulong 4
19global bw ulong 4
20global fmk ulong 21
21global pos ulong 32
22global fx ulong 65536
23global nf ulong 1
24note ga: the probe arguments op sel field bit (functions take at most six parameters)
25global ga ulong 4
26note cf: the candidate form op x0 x1 x2 m0 m1 m2
27global cf ulong 7
28
29fn pch c ulong to void
30p = get opos 0
31if lt p 4194304
32put obuf p uchar c
33put opos 0 add p 1
34end
35end
36
37fn pnum v ulong to void
38n = ulong 0
39x = v
40while gt x 0 bound 32
41q = div x 10
42put dg n uchar add 48 sub x mul q 10
43x = q
44n = add n 1
45end
46if eq n 0
47pch 48
48end
49k = n
50while gt k 0 bound 32
51k = sub k 1
52pch ulong get dg k
53end
54end
55
56fn psp v ulong to void
57pch 32
58pnum v
59end
60
61fn rdw code view uchar 16384 q ulong to ulong
62a = ulong get code q
63b = shl ulong get code add q 1 8
64c = shl ulong get code add q 2 16
65d = shl ulong get code add q 3 24
66ret or or a b or c d
67end
68
69note encode the one instruction in ins; 1 and words in cw, or 0 when refused
70fn encw ins view ulong 8192 phys view ulong 288 code edit uchar 16384 to ulong
71r = bw_enc_sm121 ins phys 1 0 edit code
72if eq r 0
73ret 0
74end
75for k ulong 4
76put cw k rdw code mul k 4
77end
78ret 1
79end
80
81fn setup ins edit ulong 8192 phys edit ulong 288 op ulong sel ulong to void
82put ins 0 op
83for s ulong 5
84vr = add s 1
85if eq s 4
86vr = 0
87end
88if ne and shr sel s 1 0
89put ins add s 1 vr
90else
91put ins add s 1 NEG
92end
93end
94put ins 6 3
95put ins 7 and shr sel 5 7
96for j ulong 5
97put phys j 0
98end
99put phys 256 0
100put phys 257 0
101end
102
103note set field f to its probe value for input bit b (f 7: no change, the base instruction)
104fn apply ins edit ulong 8192 phys edit ulong 288 f ulong b ulong to void
105v = shl 1 b
106if lt f 4
107put phys add f 1 v
108end
109if eq f 0
110put phys 257 v
111end
112if eq f 4
113put phys 256 v
114end
115if eq f 5
116put ins 6 xor 3 v
117end
118if eq f 6
119put ins 7 or get ins 7 shl v 8
120end
121end
122
123fn probe ins edit ulong 8192 phys edit ulong 288 code edit uchar 16384 to ulong
124setup edit ins edit phys get ga 0 get ga 1
125apply edit ins edit phys get ga 2 get ga 3
126ret encw ins phys edit code
127end
128
129note the bit index 0..31 of a one-bit word
130fn lg v ulong to ulong
131r = ulong 0
132for i ulong 32
133if eq shr v i 1
134r = i
135end
136end
137ret r
138end
139
140note position 0..95 when exactly one of the three words changed by one bit; 998 several; 999 none
141fn onepos d0 ulong d1 ulong d2 ulong to ulong
142n0 = ne d0 0
143n1 = ne d1 0
144n2 = ne d2 0
145if not or n0 or n1 n2
146ret 999
147end
148if and n0 and not n1 not n2
149if eq and d0 sub d0 1 0
150ret lg d0
151end
152end
153if and n1 and not n0 not n2
154if eq and d1 sub d1 1 0
155ret add 32 lg d1
156end
157end
158if and n2 and not n0 not n1
159if eq and d2 sub d2 1 0
160ret add 64 lg d2
161end
162end
163ret 998
164end
165
166note fold the last probe's diff against the base into field f's mask and bit b's position
167fn acc f ulong b ulong to void
168d0 = xor get cw 0 get bw 0
169d1 = xor get cw 1 get bw 1
170d2 = xor get cw 2 get bw 2
171m = mul f 3
172put fmk m or get fmk m d0
173put fmk add m 1 or get fmk add m 1 d1
174put fmk add m 2 or get fmk add m 2 d2
175put pos b onepos d0 d1 d2
176end
177
178note probe all nb input bits of field ga 2; masks into fmk, per-bit positions into pos
179fn field ins edit ulong 8192 phys edit ulong 288 code edit uchar 16384 nb ulong to void
180for b ulong nb
181put pos b 999
182put ga 3 b
183r = probe edit ins edit phys edit code
184if ne r 0
185acc get ga 2 b
186end
187end
188end
189
190note width of the run where input bit b lands on output bit start+b
191fn runw nb ulong to ulong
192s = get pos 0
193w = ulong 0
194if lt s 96
195w = 1
196ok = true
197for b ulong nb
198if and ok gt b 0
199if eq get pos b add s b
200w = add w 1
201else
202ok = false
203end
204end
205end
206end
207ret w
208end
209
210fn fmask k ulong to ulong
211m = ulong 0
212for f ulong 7
213m = or m get fmk add mul f 3 k
214end
215ret m
216end
217
218note 1 when a form with this op, fixed bits and mask is already recorded
219fn same j ulong to bool
220b = mul j 8
221ok = true
222for i ulong 7
223if ne get fx add b i get cf i
224ok = false
225end
226end
227ret ok
228end
229
230fn seen to bool
231hit = false
232for j ulong get nf 0
233if same j
234hit = true
235end
236end
237ret hit
238end
239
240fn record to ulong
241j = get nf 0
242b = mul j 8
243for i ulong 7
244put fx add b i get cf i
245end
246put nf 0 add j 1
247ret j
248end
249
250fn emitd id ulong f ulong w ulong to void
251m = mul f 3
252pch 68
253psp id
254psp f
255psp get fmk m
256psp get fmk add m 1
257psp get fmk add m 2
258s = get pos 0
259if eq w 0
260s = 0
261end
262psp s
263psp w
264pch 10
265end
266
267fn emitf id ulong op ulong sel ulong m0 ulong m1 ulong m2 ulong to void
268pch 70
269psp id
270psp op
271psp sel
272psp and get bw 0 xor m0 4294967295
273psp and get bw 1 xor m1 4294967295
274psp and get bw 2 xor m2 4294967295
275psp m0
276psp m1
277psp m2
278psp get bw 3
279pch 10
280end
281
282note the field record of f when the field moved any bit
283fn emitnz id ulong f ulong nb ulong to void
284m = mul f 3
285if ne or get fmk m or get fmk add m 1 get fmk add m 2 0
286emitd id f runw nb
287end
288end
289
290note probe every field of one (op, sel); the field records go out with the form record
291fn fields ins edit ulong 8192 phys edit ulong 288 code edit uchar 16384 id ulong emit bool to void
292sel = get ga 1
293for f ulong 7
294nb = ulong 8
295if eq f 5
296nb = 32
297end
298live = true
299if lt f 5
300live = ne and shr sel f 1 0
301end
302if live
303put ga 2 f
304field edit ins edit phys edit code nb
305if emit
306emitnz id f nb
307end
308end
309end
310end
311
312note the candidate form from the base words and the field masks
313fn mkcf to void
314put cf 0 get ga 0
315for k ulong 3
316m = fmask k
317put cf add k 1 and get bw k xor m 4294967295
318put cf add k 4 m
319end
320end
321
322note copy the base words and clear the field masks
323fn rebase to void
324for k ulong 4
325put bw k get cw k
326end
327clearm
328end
329
330fn clearm to void
331for i ulong 21
332put fmk i 0
333end
334end
335
336note record the candidate form and print its F record; its id
337fn newform to ulong
338id = record
339emitf id get ga 0 get ga 1 get cf 4 get cf 5 get cf 6
340clearm
341ret id
342end
343
344fn form ins edit ulong 8192 phys edit ulong 288 code edit uchar 16384 to void
345put ga 2 7
346put ga 3 0
347r = probe edit ins edit phys edit code
348if ne r 0
349rebase
350fields edit ins edit phys edit code 0 false
351mkcf
352if not seen
353id = newform
354fields edit ins edit phys edit code id true
355end
356end
357end
358
359fn flush to int
360d = ulong 0
361n = get opos 0
362while lt d n bound 1000000
363w = write 1 obuf d sub n d
364if le w 0
365ret 3
366end
367d = add d ulong w
368end
369ret 0
370end
371
372fn main to int
373ins = new ulong 8192 0
374phys = new ulong 288 0
375code = new uchar 16384 0
376for op ulong 64
377for sel ulong 256
378skip = and ne and sel 128 0 eq and sel 64 0
379if not skip
380put ga 0 op
381put ga 1 sel
382form edit ins edit phys edit code
383end
384end
385end
386r = bw_enc_sm121 ins phys 0 1 edit code
387if ne r 512
388ret 4
389end
390for k ulong 4
391put bw k rdw code mul k 4
392end
393put cf 0 255
394for k ulong 3
395put cf add k 1 get bw k
396put cf add k 4 0
397end
398id = record
399emitf id 255 0 0 0 0
400ret flush
401end