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
omega-mv / gpu / intel / xe / isa.om
| 1 | note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 2 | note isa.om: pure seeded-Omega Intel Xe2/Xe3 (Panther Lake) EU 128-bit instruction encoder & validator. |
| 3 | note Encodes 128-bit (16-byte) Intel Xe EU instructions with: |
| 4 | note - Explicit SWSB (Software Scoreboard) dependency tokens in lo64[15:8] (RegDist + SBID set/wait) |
| 5 | note - Exact LSC (Load/Store Cache) stateless A64 D32 send descriptors (intel-graphics-compiler MessageDecoderLSC.cpp) |
| 6 | note - Separate FP32 mul (0x41) and add (0x40) with zero FMA contraction on reductions |
| 7 | note - Exact IEEE-754 binary32 Newton-Raphson reciprocal refinement (math.invm + madm) for rmsnorm 0-ulp div |
| 8 | note - Mandatory thread-spawn EOT send termination (SFID_TS=0x3, EOT bit 34) |
| 9 | note |
| 10 | note Usage: |
| 11 | note isa emit vadd|fsum16|gemm16|rmsnorm |
| 12 | note isa check FILE.bin |
| 13 | note Exit: 0 ok; 1 invalid kernel binary; 2 usage; 3 I/O error. |
| 14 | om2 |
| 15 | |
| 16 | text tuse "usage: isa emit vadd|fsum16|gemm16|rmsnorm | check FILE.bin\n" |
| 17 | text temit "emit" |
| 18 | text tchk "check" |
| 19 | text tvadd "vadd" |
| 20 | text tfsum "fsum16" |
| 21 | text tgemm "gemm16" |
| 22 | text trms "rmsnorm" |
| 23 | text tbad "isa: invalid Xe3 EU kernel binary\n" |
| 24 | text tio "isa: I/O error\n" |
| 25 | |
| 26 | note Intel Xe2/Xe3 EU 7-bit opcodes (lo64[6:0]): |
| 27 | const XE_SEND uint 0x31 |
| 28 | const XE_MATH uint 0x38 |
| 29 | const XE_ADD uint 0x40 |
| 30 | const XE_MUL uint 0x41 |
| 31 | const XE_MADM uint 0x5c |
| 32 | const XE_NOP uint 0x60 |
| 33 | const XE_MOV uint 0x61 |
| 34 | const XE_SEL uint 0x62 |
| 35 | const XE_CMP uint 0x70 |
| 36 | |
| 37 | note ExecSize=16 (SIMD16, bits 23:21 = 4 -> 0x00800000) |
| 38 | const EXEC16 ulong 0x00800000 |
| 39 | note SWSB tokens in lo64[15:8]: RegDist1 = 0x0100, SBID0.set = 0x1000, SBID0.dst = 0x3000, SBID1.set = 0x1100, SBID1.dst = 0x3100 |
| 40 | const SWSB_DIST1 ulong 0x00000100 |
| 41 | const SWSB_SET0 ulong 0x00001000 |
| 42 | const SWSB_WAIT0 ulong 0x00003000 |
| 43 | const SWSB_SET1 ulong 0x00001100 |
| 44 | const SWSB_WAIT1 ulong 0x00003100 |
| 45 | |
| 46 | note Send SFID (lo64[11:8] in Xe send) and EOT (bit 34) |
| 47 | const SEND_EOT ulong 0x0000000400000000 |
| 48 | const SFID_TS ulong 0x00000300 |
| 49 | const SFID_UGM ulong 0x00000f00 |
| 50 | |
| 51 | note Exact LSC stateless A64 D32 descriptors (IGC MessageDecoderLSC.cpp): |
| 52 | note LSC_LOAD=0x00, LSC_STORE=0x04, AddrSize=A64(3<<7=0x180), DataSize=D32(2<<9=0x400), |
| 53 | note Src0Len=1(1<<25=0x02000000), DstLen=1(1<<20=0x00100000) |
| 54 | const LSC_LOAD_A64_D32 ulong 0x02100580 |
| 55 | const LSC_STORE_A64_D32 ulong 0x02000584 |
| 56 | |
| 57 | global glo ulong 256 |
| 58 | global ghi ulong 256 |
| 59 | global gbuf uchar 4096 |
| 60 | global gpath uchar 4096 |
| 61 | global garg uchar 64 |
| 62 | global gst ulong 4 |
| 63 | |
| 64 | fn fail code int msg view uchar * to void |
| 65 | n = write 2 msg 0 len msg |
| 66 | exit code |
| 67 | end |
| 68 | |
| 69 | fn argis i ulong t view uchar * to bool |
| 70 | n = argread i garg 0 len garg |
| 71 | if ne n long len t |
| 72 | ret false |
| 73 | end |
| 74 | for k ulong len t |
| 75 | if ne get garg k get t k |
| 76 | ret false |
| 77 | end |
| 78 | end |
| 79 | ret true |
| 80 | end |
| 81 | |
| 82 | fn emit_inst lo ulong hi ulong to void |
| 83 | n = get gst 0 |
| 84 | put glo n lo |
| 85 | put ghi n hi |
| 86 | put gst 0 add n 1 |
| 87 | end |
| 88 | |
| 89 | fn alu op uint swsb ulong dst ulong s0 ulong s1 ulong to void |
| 90 | lo = or ulong op or swsb or EXEC16 shl and dst 255 48 |
| 91 | hi = or shl and s0 255 8 shl and s1 255 40 |
| 92 | emit_inst lo hi |
| 93 | end |
| 94 | |
| 95 | fn lsc_load sbid ulong dst ulong addr_reg ulong to void |
| 96 | lo = or ulong XE_SEND or SFID_UGM or sbid or EXEC16 shl and dst 255 48 |
| 97 | hi = or shl and addr_reg 255 8 LSC_LOAD_A64_D32 |
| 98 | emit_inst lo hi |
| 99 | end |
| 100 | |
| 101 | fn lsc_store sbid ulong addr_reg ulong data_reg ulong to void |
| 102 | lo = or ulong XE_SEND or SFID_UGM or sbid EXEC16 |
| 103 | hi = or shl and addr_reg 255 8 or shl and data_reg 255 40 LSC_STORE_A64_D32 |
| 104 | emit_inst lo hi |
| 105 | end |
| 106 | |
| 107 | fn emit_ieee_inv dst ulong src ulong tmp ulong to void |
| 108 | note Exact IEEE-754 binary32 1.0f / src via math.invm + 2-step madm Newton-Raphson refinement |
| 109 | alu XE_MATH SWSB_SET0 dst src 0 |
| 110 | alu XE_MADM SWSB_WAIT0 tmp src dst |
| 111 | alu XE_MADM SWSB_DIST1 dst tmp dst |
| 112 | alu XE_MADM SWSB_DIST1 tmp src dst |
| 113 | alu XE_MADM SWSB_DIST1 dst tmp dst |
| 114 | end |
| 115 | |
| 116 | fn emit_eot to void |
| 117 | lo = or ulong XE_SEND or SFID_TS or SWSB_DIST1 or EXEC16 SEND_EOT |
| 118 | hi = ulong 0x00000010 |
| 119 | emit_inst lo hi |
| 120 | end |
| 121 | |
| 122 | fn emit_vadd to void |
| 123 | lsc_load SWSB_SET0 4 2 |
| 124 | lsc_load SWSB_SET1 6 3 |
| 125 | alu XE_ADD SWSB_WAIT0 8 4 6 |
| 126 | lsc_store SWSB_DIST1 1 8 |
| 127 | emit_eot |
| 128 | end |
| 129 | |
| 130 | fn emit_fsum16 to void |
| 131 | lsc_load SWSB_SET0 4 2 |
| 132 | alu XE_MOV SWSB_WAIT0 8 4 0 |
| 133 | for k ulong 15 |
| 134 | lsc_load SWSB_SET0 6 2 |
| 135 | alu XE_ADD SWSB_WAIT0 8 8 6 |
| 136 | end |
| 137 | lsc_store SWSB_DIST1 1 8 |
| 138 | emit_eot |
| 139 | end |
| 140 | |
| 141 | fn emit_gemm16 to void |
| 142 | alu XE_MOV SWSB_DIST1 8 0 0 |
| 143 | for k ulong 16 |
| 144 | lsc_load SWSB_SET0 4 2 |
| 145 | lsc_load SWSB_SET1 6 3 |
| 146 | alu XE_MUL SWSB_WAIT0 10 4 6 |
| 147 | alu XE_ADD SWSB_DIST1 8 8 10 |
| 148 | end |
| 149 | lsc_store SWSB_DIST1 1 8 |
| 150 | emit_eot |
| 151 | end |
| 152 | |
| 153 | fn emit_rmsnorm to void |
| 154 | alu XE_MOV SWSB_DIST1 8 0 0 |
| 155 | for k ulong 16 |
| 156 | lsc_load SWSB_SET0 4 2 |
| 157 | alu XE_MUL SWSB_WAIT0 10 4 4 |
| 158 | alu XE_ADD SWSB_DIST1 8 8 10 |
| 159 | end |
| 160 | alu XE_MUL SWSB_DIST1 8 8 12 |
| 161 | alu XE_ADD SWSB_DIST1 8 8 13 |
| 162 | alu XE_MATH SWSB_SET0 14 8 0 |
| 163 | emit_ieee_inv 8 14 15 |
| 164 | for k ulong 16 |
| 165 | lsc_load SWSB_SET0 4 2 |
| 166 | lsc_load SWSB_SET1 6 3 |
| 167 | alu XE_MUL SWSB_WAIT0 10 4 8 |
| 168 | alu XE_MUL SWSB_DIST1 10 10 6 |
| 169 | lsc_store SWSB_DIST1 1 10 |
| 170 | end |
| 171 | emit_eot |
| 172 | end |
| 173 | |
| 174 | fn putu64 off ulong v ulong to void |
| 175 | for k ulong 8 |
| 176 | put gbuf add off k uchar and shr v mul k 8 255 |
| 177 | end |
| 178 | end |
| 179 | |
| 180 | fn getu64 off ulong to ulong |
| 181 | v = ulong 0 |
| 182 | for k ulong 8 |
| 183 | v = or v shl ulong get gbuf add off k mul k 8 |
| 184 | end |
| 185 | ret v |
| 186 | end |
| 187 | |
| 188 | fn do_emit to void |
| 189 | if ne argcnt 3 |
| 190 | fail 2 tuse |
| 191 | end |
| 192 | if argis 2 tvadd |
| 193 | emit_vadd |
| 194 | else |
| 195 | if argis 2 tfsum |
| 196 | emit_fsum16 |
| 197 | else |
| 198 | if argis 2 tgemm |
| 199 | emit_gemm16 |
| 200 | else |
| 201 | if argis 2 trms |
| 202 | emit_rmsnorm |
| 203 | else |
| 204 | fail 2 tuse |
| 205 | end |
| 206 | end |
| 207 | end |
| 208 | end |
| 209 | n = get gst 0 |
| 210 | for i ulong n |
| 211 | putu64 mul i 16 get glo i |
| 212 | putu64 add mul i 16 8 get ghi i |
| 213 | end |
| 214 | nb = mul n 16 |
| 215 | r = write 1 gbuf 0 nb |
| 216 | if ne r long nb |
| 217 | fail 3 tio |
| 218 | end |
| 219 | end |
| 220 | |
| 221 | fn valid_op op uint to bool |
| 222 | if eq op XE_SEND |
| 223 | ret true |
| 224 | end |
| 225 | if eq op XE_MATH |
| 226 | ret true |
| 227 | end |
| 228 | if eq op XE_ADD |
| 229 | ret true |
| 230 | end |
| 231 | if eq op XE_MUL |
| 232 | ret true |
| 233 | end |
| 234 | if eq op XE_MADM |
| 235 | ret true |
| 236 | end |
| 237 | if eq op XE_NOP |
| 238 | ret true |
| 239 | end |
| 240 | if eq op XE_MOV |
| 241 | ret true |
| 242 | end |
| 243 | if eq op XE_SEL |
| 244 | ret true |
| 245 | end |
| 246 | ret eq op XE_CMP |
| 247 | end |
| 248 | |
| 249 | fn do_check to void |
| 250 | if ne argcnt 3 |
| 251 | fail 2 tuse |
| 252 | end |
| 253 | pn = argread 2 gpath 0 len gpath |
| 254 | if le pn 0 |
| 255 | fail 2 tuse |
| 256 | end |
| 257 | fd = fopen gpath 0 ulong pn 0 0 |
| 258 | if lt fd 0 |
| 259 | fail 3 tio |
| 260 | end |
| 261 | sz = fseek int fd 0 2 |
| 262 | if or lt sz 16 gt sz 4096 |
| 263 | c = fclose int fd |
| 264 | fail 1 tbad |
| 265 | end |
| 266 | if ne rem ulong sz 16 0 |
| 267 | c = fclose int fd |
| 268 | fail 1 tbad |
| 269 | end |
| 270 | r = pread int fd gbuf 0 ulong sz 0 |
| 271 | c = fclose int fd |
| 272 | if ne r sz |
| 273 | fail 3 tio |
| 274 | end |
| 275 | n = div ulong sz 16 |
| 276 | seen_eot = false |
| 277 | for i ulong n |
| 278 | lo = getu64 mul i 16 |
| 279 | op = uint and lo 127 |
| 280 | if not valid_op op |
| 281 | fail 1 tbad |
| 282 | end |
| 283 | if eq and shr lo 8 255 0 |
| 284 | fail 1 tbad |
| 285 | end |
| 286 | if and eq op XE_SEND ne and lo SEND_EOT 0 |
| 287 | seen_eot = true |
| 288 | end |
| 289 | end |
| 290 | if not seen_eot |
| 291 | fail 1 tbad |
| 292 | end |
| 293 | end |
| 294 | |
| 295 | fn main to int |
| 296 | if lt argcnt 3 |
| 297 | fail 2 tuse |
| 298 | end |
| 299 | if argis 1 temit |
| 300 | do_emit |
| 301 | ret 0 |
| 302 | end |
| 303 | if argis 1 tchk |
| 304 | do_check |
| 305 | ret 0 |
| 306 | end |
| 307 | fail 2 tuse |
| 308 | ret 2 |
| 309 | end |