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 / batch.om
| 1 | note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 2 | note batch.om: pure seeded-Omega Intel Xe3 (Panther Lake / Xe3Core) CCS batch buffer builder & checker. |
| 3 | note Bitfields verified against Intel compute-runtime shared/source/generated/xe3_core/hw_cmds_generated_xe3_core.inl: |
| 4 | note PIPELINE_SELECT (1 dw): 0x69040302 (PipelineSelection=GPGPU/Compute, Mask=0x3) |
| 5 | note STATE_BASE_ADDRESS (22 dw): 0x61010014 (InstructionBaseAddress DW10..11, InstructionBufferSize DW15) |
| 6 | note STATE_COMPUTE_MODE (3 dw): 0x61050001 (LargeGrfMode=0, UavCoherencyMode=1) |
| 7 | note CFE_STATE (6 dw): 0x72000004 (Pipeline=COMPUTE(2), Opcode=2, SubOp=0, MaxThreads DW3[31:16]) |
| 8 | note COMPUTE_WALKER (40 dw): 0x72080026 (Pipeline=COMPUTE(2), Opcode=2, SubOp=2, IDD DW19..26, PostSync DW27..31, InlineData DW32..39) |
| 9 | note PIPE_CONTROL (6 dw): 0x7a000a04 (UnTypedDataPortCacheFlush + DataportFlush + CS Stall + DC Flush + QWORD Write Immediate) |
| 10 | note MI_BATCH_BUFFER_END (1 dw): 0x05000000 |
| 11 | note |
| 12 | note Usage: |
| 13 | note batch emit KERNEL_VA INLINE_VA FENCE_VA FENCE_VAL GX GY GZ SIMD |
| 14 | note batch fence FENCE_VA FENCE_VAL |
| 15 | note batch check FILE.bin |
| 16 | note Exit: 0 ok; 1 invalid batch; 2 usage; 3 I/O error. |
| 17 | om2 |
| 18 | |
| 19 | text tuse "usage: batch emit KVA IVA FVA FVAL GX GY GZ SIMD | fence FVA FVAL | check FILE.bin\n" |
| 20 | text temit "emit" |
| 21 | text tfence "fence" |
| 22 | text tchk "check" |
| 23 | text tbad "batch: invalid Xe3 CCS batch buffer\n" |
| 24 | text tio "batch: I/O error\n" |
| 25 | |
| 26 | const OP_PIPE_SEL uint 0x69040302 |
| 27 | const OP_SBA uint 0x61010014 |
| 28 | const OP_SCM uint 0x61050001 |
| 29 | const OP_CFE uint 0x72000004 |
| 30 | const OP_WALKER uint 0x72080026 |
| 31 | const OP_PIPE_CTL uint 0x7a000a04 |
| 32 | const OP_BB_END uint 0x05000000 |
| 33 | |
| 34 | global gdw uint 256 |
| 35 | global gbuf uchar 1024 |
| 36 | global gpath uchar 4096 |
| 37 | global garg uchar 64 |
| 38 | global gst ulong 4 |
| 39 | |
| 40 | fn fail code int msg view uchar * to void |
| 41 | n = write 2 msg 0 len msg |
| 42 | exit code |
| 43 | end |
| 44 | |
| 45 | fn argis i ulong t view uchar * to bool |
| 46 | n = argread i garg 0 len garg |
| 47 | if ne n long len t |
| 48 | ret false |
| 49 | end |
| 50 | for k ulong len t |
| 51 | if ne get garg k get t k |
| 52 | ret false |
| 53 | end |
| 54 | end |
| 55 | ret true |
| 56 | end |
| 57 | |
| 58 | fn parseu ai ulong to ulong |
| 59 | n = argread ai garg 0 len garg |
| 60 | if or le n 0 gt n 20 |
| 61 | fail 2 tuse |
| 62 | end |
| 63 | v = ulong 0 |
| 64 | for i ulong ulong n |
| 65 | c = get garg i |
| 66 | if or lt c 48 gt c 57 |
| 67 | fail 2 tuse |
| 68 | end |
| 69 | v = add mul v 10 ulong sub c 48 |
| 70 | end |
| 71 | ret v |
| 72 | end |
| 73 | |
| 74 | fn dw v uint to void |
| 75 | n = get gst 0 |
| 76 | put gdw n v |
| 77 | put gst 0 add n 1 |
| 78 | end |
| 79 | |
| 80 | fn dw64 v ulong to void |
| 81 | dw uint and v 4294967295 |
| 82 | dw uint shr v 32 |
| 83 | end |
| 84 | |
| 85 | fn emit_sba kva ulong to void |
| 86 | note Xe3 STATE_BASE_ADDRESS (22 dwords): DW10..11 InstructionBaseAddress, DW15 InstructionBufferSize |
| 87 | dw OP_SBA |
| 88 | for i ulong 9 |
| 89 | dw 0 |
| 90 | end |
| 91 | dw64 or and kva 18446744073709547520 1 |
| 92 | for i ulong 3 |
| 93 | dw 0 |
| 94 | end |
| 95 | dw 0xfffff001 |
| 96 | for i ulong 6 |
| 97 | dw 0 |
| 98 | end |
| 99 | end |
| 100 | |
| 101 | fn emit_scm to void |
| 102 | note Xe3 STATE_COMPUTE_MODE (3 dwords): LargeGrfMode=0 (mask bit 31), UavCoherencyMode=1 (mask bit 22 + bit 6) |
| 103 | dw OP_SCM |
| 104 | dw 0x80000000 |
| 105 | dw 0x00400040 |
| 106 | end |
| 107 | |
| 108 | fn emit_cfe to void |
| 109 | note Xe3 CFE_STATE (6 dwords): MaximumNumberOfThreads = 1024 in DW3[31:16] |
| 110 | dw OP_CFE |
| 111 | dw 0 |
| 112 | dw 0 |
| 113 | dw 0x04000000 |
| 114 | dw 0 |
| 115 | dw 0 |
| 116 | end |
| 117 | |
| 118 | fn emit_walker kva ulong iva ulong gx uint gy uint gz uint simd uint to void |
| 119 | note Xe3 COMPUTE_WALKER (40 dwords, hw_cmds_generated_xe3_core.inl) |
| 120 | dw OP_WALKER |
| 121 | dw 0 |
| 122 | note DW2: IndirectDataLength = 64 |
| 123 | dw 64 |
| 124 | note DW3: IndirectDataStartAddress[31:6] (64B aligned) |
| 125 | dw uint and iva 4294967232 |
| 126 | note DW4: SimdSize[31:30] (1=SIMT16, 2=SIMT32) | GenerateLocalId(29) | EmitInlineParameter(25) |
| 127 | sbits = uint 0x62000000 |
| 128 | if eq simd 32 |
| 129 | sbits = 0xa2000000 |
| 130 | end |
| 131 | dw sbits |
| 132 | note DW5: ExecutionMask |
| 133 | dw 0xffffffff |
| 134 | note DW6: LocalXMaximum=63 (64 work-items/group) |
| 135 | dw 63 |
| 136 | note DW7..9: ThreadGroupId X, Y, Z dimensions |
| 137 | dw gx |
| 138 | dw gy |
| 139 | dw gz |
| 140 | note DW10..18: Starting X/Y/Z, PartitionId, PartitionSize, Preempt X/Y/Z, Reserved |
| 141 | for i ulong 9 |
| 142 | dw 0 |
| 143 | end |
| 144 | note DW19..26: INTERFACE_DESCRIPTOR_DATA (8 dwords) |
| 145 | note IDD DW0: KernelStartPointer[31:6] (64B aligned offset from InstructionBaseAddress) |
| 146 | dw uint and kva 4032 |
| 147 | dw 0 |
| 148 | dw 0 |
| 149 | dw 0 |
| 150 | dw 0 |
| 151 | note IDD DW5: NumberOfThreadsInGpgpuThreadGroup=4 (4*SIMD16=64), RoundingMode=0 (RTNE) |
| 152 | dw 4 |
| 153 | dw 0 |
| 154 | dw 0 |
| 155 | note DW27..31: POSTSYNC_DATA (5 dwords, no walker post-sync; PIPE_CONTROL signals fence) |
| 156 | for i ulong 5 |
| 157 | dw 0 |
| 158 | end |
| 159 | note DW32..39: InlineData[8] (32 bytes; inline_va in DW32..33) |
| 160 | dw64 iva |
| 161 | for i ulong 6 |
| 162 | dw 0 |
| 163 | end |
| 164 | end |
| 165 | |
| 166 | fn emit_pipe_ctl fva ulong fval ulong to void |
| 167 | note Xe3 PIPE_CONTROL (6 dwords): CS stall(20) | PostSync=WriteImmediate(14) | DcFlush(5) |
| 168 | dw OP_PIPE_CTL |
| 169 | dw 0x00104020 |
| 170 | dw64 fva |
| 171 | dw64 fval |
| 172 | end |
| 173 | |
| 174 | fn flush_dw to void |
| 175 | while ne rem get gst 0 16 0 bound 16 |
| 176 | dw 0 |
| 177 | end |
| 178 | ndw = get gst 0 |
| 179 | for i ulong ndw |
| 180 | v = get gdw i |
| 181 | o = mul i 4 |
| 182 | for k ulong 4 |
| 183 | put gbuf add o k uchar and shr v mul uint k 8 255 |
| 184 | end |
| 185 | end |
| 186 | nb = mul ndw 4 |
| 187 | r = write 1 gbuf 0 nb |
| 188 | if ne r long nb |
| 189 | fail 3 tio |
| 190 | end |
| 191 | end |
| 192 | |
| 193 | fn do_emit to void |
| 194 | if ne argcnt 10 |
| 195 | fail 2 tuse |
| 196 | end |
| 197 | kva = parseu 2 |
| 198 | iva = parseu 3 |
| 199 | fva = parseu 4 |
| 200 | fval = parseu 5 |
| 201 | gx = uint parseu 6 |
| 202 | gy = uint parseu 7 |
| 203 | gz = uint parseu 8 |
| 204 | simd = uint parseu 9 |
| 205 | if and ne simd 16 ne simd 32 |
| 206 | fail 2 tuse |
| 207 | end |
| 208 | dw OP_PIPE_SEL |
| 209 | emit_sba kva |
| 210 | emit_scm |
| 211 | emit_cfe |
| 212 | emit_walker kva iva gx gy gz simd |
| 213 | emit_pipe_ctl fva fval |
| 214 | dw OP_BB_END |
| 215 | flush_dw |
| 216 | end |
| 217 | |
| 218 | fn do_fence to void |
| 219 | if ne argcnt 4 |
| 220 | fail 2 tuse |
| 221 | end |
| 222 | fva = parseu 2 |
| 223 | fval = parseu 3 |
| 224 | emit_pipe_ctl fva fval |
| 225 | dw OP_BB_END |
| 226 | flush_dw |
| 227 | end |
| 228 | |
| 229 | fn gbu32 i ulong to uint |
| 230 | o = mul i 4 |
| 231 | v = uint 0 |
| 232 | for k ulong 4 |
| 233 | v = or v shl uint get gbuf add o k mul uint k 8 |
| 234 | end |
| 235 | ret v |
| 236 | end |
| 237 | |
| 238 | fn do_check to void |
| 239 | if ne argcnt 3 |
| 240 | fail 2 tuse |
| 241 | end |
| 242 | pn = argread 2 gpath 0 len gpath |
| 243 | if le pn 0 |
| 244 | fail 2 tuse |
| 245 | end |
| 246 | fd = fopen gpath 0 ulong pn 0 0 |
| 247 | if lt fd 0 |
| 248 | fail 3 tio |
| 249 | end |
| 250 | sz = fseek int fd 0 2 |
| 251 | if or lt sz 64 gt sz 1024 |
| 252 | c = fclose int fd |
| 253 | fail 1 tbad |
| 254 | end |
| 255 | if ne rem ulong sz 64 0 |
| 256 | c = fclose int fd |
| 257 | fail 1 tbad |
| 258 | end |
| 259 | r = pread int fd gbuf 0 ulong sz 0 |
| 260 | c = fclose int fd |
| 261 | if ne r sz |
| 262 | fail 3 tio |
| 263 | end |
| 264 | ndw = div ulong sz 4 |
| 265 | p = ulong 0 |
| 266 | seen_ctl = false |
| 267 | seen_end = false |
| 268 | while lt p ndw bound 256 |
| 269 | h = gbu32 p |
| 270 | if eq h 0 |
| 271 | p = add p 1 |
| 272 | continue |
| 273 | end |
| 274 | if eq h OP_BB_END |
| 275 | seen_end = true |
| 276 | break |
| 277 | end |
| 278 | if eq h OP_PIPE_SEL |
| 279 | p = add p 1 |
| 280 | continue |
| 281 | end |
| 282 | if eq h OP_SBA |
| 283 | p = add p 22 |
| 284 | continue |
| 285 | end |
| 286 | if eq h OP_SCM |
| 287 | p = add p 3 |
| 288 | continue |
| 289 | end |
| 290 | if eq h OP_CFE |
| 291 | p = add p 6 |
| 292 | continue |
| 293 | end |
| 294 | if eq h OP_WALKER |
| 295 | p = add p 40 |
| 296 | continue |
| 297 | end |
| 298 | if eq h OP_PIPE_CTL |
| 299 | seen_ctl = true |
| 300 | p = add p 6 |
| 301 | continue |
| 302 | end |
| 303 | fail 1 tbad |
| 304 | end |
| 305 | if or not seen_ctl not seen_end |
| 306 | fail 1 tbad |
| 307 | end |
| 308 | end |
| 309 | |
| 310 | fn main to int |
| 311 | if lt argcnt 3 |
| 312 | fail 2 tuse |
| 313 | end |
| 314 | if argis 1 temit |
| 315 | do_emit |
| 316 | ret 0 |
| 317 | end |
| 318 | if argis 1 tfence |
| 319 | do_fence |
| 320 | ret 0 |
| 321 | end |
| 322 | if argis 1 tchk |
| 323 | do_check |
| 324 | ret 0 |
| 325 | end |
| 326 | fail 2 tuse |
| 327 | ret 2 |
| 328 | end |