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