CUDApp
CUDA++: AIEN’s GPU machine-code toolchain, runtime and driver client for NVIDIA GPUs without the CUDA toolkit. Hardware coverage and driver replacement limits are explicit.
Brought by drakestapleton
CUDApp / backend / amd / kfd / ring.om
| 1 | note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 2 | note A sequential, single-producer/single-consumer model, not an MMIO implementation or memory-model |
| 3 | note proof. Each command is one externally visible publication step. No AMD hardware is accessed. |
| 4 | note HSA hsa.h rocm-6.2.0: packet type INVALID=1, KERNEL_DISPATCH=2; doorbell carries ready packet ID. |
| 5 | note Host policy checked here: body bytes 4..63 -> header+setup (one 32-bit word) -> write index |
| 6 | note -> doorbell packet ID (new write index minus one). Consumer observes after each step; once |
| 7 | note the type is valid it must see every body byte. Completion consumes the published packet and |
| 8 | note invalidates its header before the producer reuses the slot. KFD 1.14 minimum ring 1024 bytes; |
| 9 | note 16 64-byte slots. The gate runs 40 launches, crossing the ring boundary twice. |
| 10 | note A real implementation needs release publication/acquire observation and device-coherent memory, |
| 11 | note as well as architecture-specific fences. This model does NOT establish those properties. |
| 12 | note usage: ring STEP... (1=body 2=header 3=write-index 4=doorbell 5=consume). |
| 13 | note Exit 0 complete sequences; 64 fewer than five steps; 3 bad order/data/index or incomplete sequence |
| 14 | note after the minimum argument check; 2 numeric input. |
| 15 | om2 |
| 16 | global ringbuf uchar 1024 |
| 17 | global rs ulong 4 |
| 18 | text raccept "RING ACCEPT body<header<write-index<doorbell<consume\n" |
| 19 | note rs: phase, write index, read index, doorbell packet ID. |
| 20 | fn bodyval id ulong k ulong to uchar |
| 21 | ret uchar and add id k 255 |
| 22 | end |
| 23 | fn inspect id ulong to void |
| 24 | slot = mul rem id 16 64 |
| 25 | if eq get ringbuf slot 2 |
| 26 | for k ulong 60 |
| 27 | p = add k 4 |
| 28 | if ne get ringbuf add slot p bodyval id p |
| 29 | exit 3 |
| 30 | end |
| 31 | end |
| 32 | end |
| 33 | end |
| 34 | fn step action ulong to void |
| 35 | id = get rs 2 |
| 36 | slot = mul rem id 16 64 |
| 37 | phase = get rs 0 |
| 38 | if eq action 1 |
| 39 | if ne phase 0 |
| 40 | exit 3 |
| 41 | end |
| 42 | if ne get ringbuf slot 1 |
| 43 | exit 3 |
| 44 | end |
| 45 | for k ulong 60 |
| 46 | p = add k 4 |
| 47 | put ringbuf add slot p bodyval id p |
| 48 | end |
| 49 | put rs 0 1 |
| 50 | end |
| 51 | if eq action 2 |
| 52 | if ne phase 1 |
| 53 | exit 3 |
| 54 | end |
| 55 | note Model the atomic 32-bit header+setup publication, not four observable byte stores. |
| 56 | put ringbuf slot 2 |
| 57 | put ringbuf add slot 1 21 |
| 58 | put ringbuf add slot 2 1 |
| 59 | put ringbuf add slot 3 0 |
| 60 | put rs 0 2 |
| 61 | end |
| 62 | if eq action 3 |
| 63 | if ne phase 2 |
| 64 | exit 3 |
| 65 | end |
| 66 | put rs 1 add id 1 |
| 67 | put rs 0 3 |
| 68 | end |
| 69 | if eq action 4 |
| 70 | if ne phase 3 |
| 71 | exit 3 |
| 72 | end |
| 73 | put rs 3 sub get rs 1 1 |
| 74 | put rs 0 4 |
| 75 | end |
| 76 | if eq action 5 |
| 77 | if ne phase 4 |
| 78 | exit 3 |
| 79 | end |
| 80 | if or ne get rs 1 add id 1 ne get rs 3 id |
| 81 | exit 3 |
| 82 | end |
| 83 | if or ne get ringbuf slot 2 ne get ringbuf add slot 1 21 |
| 84 | exit 3 |
| 85 | end |
| 86 | inspect id |
| 87 | put ringbuf slot 1 |
| 88 | put rs 2 add id 1 |
| 89 | put rs 0 0 |
| 90 | end |
| 91 | if or lt action 1 gt action 5 |
| 92 | exit 3 |
| 93 | end |
| 94 | inspect get rs 2 |
| 95 | end |
| 96 | fn main to int |
| 97 | if lt argcnt 6 |
| 98 | ret 64 |
| 99 | end |
| 100 | for i ulong 16 |
| 101 | put ringbuf mul i 64 1 |
| 102 | end |
| 103 | for i ulong sub ulong argcnt 1 |
| 104 | step argnum add i 1 |
| 105 | end |
| 106 | if ne get rs 0 0 |
| 107 | ret 3 |
| 108 | end |
| 109 | emit raccept |
| 110 | ret 0 |
| 111 | end |