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 / native / blackwell / trace-emit.om
| 1 | note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 2 | note Device-side trace emitter for CUDApp #82, first slice. Handwritten seeded Omega. |
| 3 | note It emits the instruction words for one selected producer (thread 0 of the launched CTA) |
| 4 | note that records two bounded mark records around one synchronous region, and returns the |
| 5 | note input words unchanged when tracing is disabled. Enabled input is a prepared linear body |
| 6 | note fragment, not a complete kernel: the caller must initialise and preserve R0 = TID.X, |
| 7 | note launch exactly one CTA, and allow both markers to fall through. Branches, calls, returns |
| 8 | note and early exits are unsupported. EXIT instructions are explicitly refused, but the |
| 9 | note scanner is not a general control-flow verifier. Markers clobber P0, R2, R4:R13 and UR4:UR5; |
| 10 | note the caller must account for those live-register effects at both insertion points. |
| 11 | note |
| 12 | note Wire layout is PR #83 docs/device-trace/CONTRACT.md v1: 64-byte header, 64-byte records, |
| 13 | note kinds 1 mark / 2 begin / 3 end, clock domain 1 = raw device GLOBALTIMER low 32 bits with |
| 14 | note units UNKNOWN. The host owns header initialisation (magic, version, extent, capacity, |
| 15 | note generation, kernel, manifest, status) and finalises it after completion; this emitter |
| 16 | note writes records only. No 64-bit clock claim, no nanosecond claim. |
| 17 | note |
| 18 | note Register numbers are physical and fixed by this emitter: R0 tid, R2 stamp, R4:R5 trace |
| 19 | note base, R6 scratch, R8:R9 record address, R10:R11 generation, R12:R13 kernel; the global |
| 20 | note memory descriptor is loaded into UR4. Encodings are ported from the repository's own |
| 21 | note encoder, src/omega_blackwell_codegen.c encode_single_insn (S2R 0x7919, ISETP.GE 0x780c, |
| 22 | note BRA 0x947, LDC64 0xff007b82, LDCU64 0xff0077ac, MOV imm 0x7802, IMAD.WIDE 0x7825, |
| 23 | note STG.E 0x7986), and each emitted stream is decoded by nvdisasm 13.0 as an outside oracle. |
| 24 | note |
| 25 | note Record offsets use full-width base addition via the existing IMAD.WIDE.U32 form. |
| 26 | note The host must supply a valid nonwrapping allocated extent. The enabled path is refused |
| 27 | note unless gtchecks is exactly 1. This flag is a precondition, not evidence that arbitrary |
| 28 | note input words, launch geometry, memory visibility or hardware execution have been checked. |
| 29 | |
| 30 | const TRBAS ulong 1024 |
| 31 | const TRGEN ulong 1040 |
| 32 | const TRKRN ulong 1048 |
| 33 | const TRDSK ulong 856 |
| 34 | const HDRSZ ulong 64 |
| 35 | const RECSZ ulong 64 |
| 36 | const MAXCAP ulong 64 |
| 37 | const MAXEXT ulong 4160 |
| 38 | const SITE1 ulong 1 |
| 39 | const SITE2 ulong 2 |
| 40 | const KINDMK ulong 1 |
| 41 | const R_S2R ulong 31001 |
| 42 | const R_ISET ulong 30732 |
| 43 | const R_LDC ulong 4278221698 |
| 44 | const R_LDCU ulong 4278220716 |
| 45 | const R_MOVI ulong 30722 |
| 46 | const R_IMADW ulong 30757 |
| 47 | const R_STG ulong 31110 |
| 48 | const SR_TID ulong 33 |
| 49 | const SR_CLK ulong 82 |
| 50 | const R_BRA ulong 2375 |
| 51 | |
| 52 | global gbin uchar 4096 |
| 53 | global gbinlen ulong 1 |
| 54 | global gbout uchar 8192 |
| 55 | global gboutlen ulong 1 |
| 56 | global gterr ulong 1 |
| 57 | global gtcap ulong 1 |
| 58 | global gten ulong 1 |
| 59 | global gtchecks ulong 1 |
| 60 | |
| 61 | fn gtword v uint to void |
| 62 | for j ulong 4 |
| 63 | put gbout get gboutlen 0 uchar and shr v mul j 8 255 |
| 64 | put gboutlen 0 add get gboutlen 0 1 |
| 65 | end |
| 66 | end |
| 67 | |
| 68 | fn gtquad a uint b uint c uint d uint to void |
| 69 | gtword a |
| 70 | gtword b |
| 71 | gtword c |
| 72 | gtword d |
| 73 | end |
| 74 | |
| 75 | fn gts2r dst ulong sr ulong to void |
| 76 | gtquad uint or R_S2R shl dst 16 uint 0 uint shl sr 8 uint 926208 |
| 77 | end |
| 78 | |
| 79 | fn gtsel to void |
| 80 | note ISETP.GE P0, R0, 1: predicate true when tid >= 1, so only tid 0 records. |
| 81 | gtquad uint R_ISET uint 1 uint 66085488 uint 2087424 |
| 82 | end |
| 83 | |
| 84 | fn gtmov dst ulong imm ulong to void |
| 85 | gtquad uint or R_MOVI shl dst 16 uint imm uint 3840 uint 1040896 |
| 86 | end |
| 87 | |
| 88 | fn gtstg bse ulong val ulong off ulong to void |
| 89 | gtquad uint or R_STG shl bse 24 uint or val shl and off 65535 8 uint 202381572 uint 1040896 |
| 90 | end |
| 91 | |
| 92 | fn gtldc dst ulong off ulong to void |
| 93 | note LDC.64 Rd:Rd+1, c[0x0][off] |
| 94 | gtquad uint or R_LDC shl dst 16 uint and shl off 6 16776960 uint 2560 uint 926208 |
| 95 | end |
| 96 | |
| 97 | fn gtldu dst ulong off ulong to void |
| 98 | note LDCU.64 URd:URd+1, c[0x0][off] -- the global memory descriptor. |
| 99 | gtquad uint or R_LDCU shl dst 16 uint and shl off 5 16776960 uint 134220288 uint 945664 |
| 100 | end |
| 101 | |
| 102 | fn gtaddr off ulong to void |
| 103 | note MOV R6, off; IMAD.WIDE.U32 R8:R9, R6, 1, R4:R5 computes base + offset with carry. |
| 104 | note Existing encoding: src/omega_blackwell_codegen.c BW_IR_IMAD_WIDE; vector.om address pairs. |
| 105 | gtmov 6 off |
| 106 | gtquad uint or or R_IMADW shl ulong 8 16 shl ulong 6 24 uint 1 uint 126746628 uint 2083840 |
| 107 | end |
| 108 | |
| 109 | fn gtbrak to void |
| 110 | note BRA @P0 over the fixed 20-instruction mark block: delta 19, rel 18, predicate field 0. |
| 111 | gtquad uint or R_BRA shl ulong 18 18 uint 0 uint 58720256 uint 1042944 |
| 112 | end |
| 113 | |
| 114 | fn gtmark site ulong seq ulong to void |
| 115 | note One bounded mark record at 64*site (sites 1 and 2). Field offsets are contract v1. |
| 116 | gtsel |
| 117 | gtbrak |
| 118 | gtldu 4 TRDSK |
| 119 | gts2r 2 SR_CLK |
| 120 | gtldc 4 TRBAS |
| 121 | gtldc 10 TRGEN |
| 122 | gtldc 12 TRKRN |
| 123 | gtaddr mul site 64 |
| 124 | gtmov 6 site |
| 125 | gtstg 8 6 0 |
| 126 | gtmov 6 KINDMK |
| 127 | gtstg 8 6 4 |
| 128 | gtmov 6 seq |
| 129 | gtstg 8 6 8 |
| 130 | gtstg 8 2 12 |
| 131 | gtstg 8 10 16 |
| 132 | gtstg 8 11 20 |
| 133 | gtstg 8 12 24 |
| 134 | gtstg 8 13 28 |
| 135 | end |
| 136 | |
| 137 | fn gtinit cap ulong to bool |
| 138 | note Bound the extent before any word is emitted. Refusal codes: 1 capacity below two records, |
| 139 | note 2 extent above the contract maximum, 3 the input is not a whole number of instructions. |
| 140 | put gterr 0 0 |
| 141 | if gt cap MAXCAP |
| 142 | put gterr 0 2 |
| 143 | ret false |
| 144 | end |
| 145 | if lt cap 2 |
| 146 | put gterr 0 1 |
| 147 | ret false |
| 148 | end |
| 149 | if ne rem get gbinlen 0 16 0 |
| 150 | put gterr 0 3 |
| 151 | ret false |
| 152 | end |
| 153 | put gtcap 0 cap |
| 154 | ret true |
| 155 | end |
| 156 | |
| 157 | fn gtwrap on ulong to void |
| 158 | note Disabled: the output is the input byte for byte; no prologue, no store, no allocation. |
| 159 | note Enabled: mark(site 1), the caller's prepared body fragment, mark(site 2). |
| 160 | put gboutlen 0 0 |
| 161 | if eq on 0 |
| 162 | for i ulong get gbinlen 0 |
| 163 | put gbout i get gbin i |
| 164 | end |
| 165 | put gboutlen 0 get gbinlen 0 |
| 166 | ret |
| 167 | end |
| 168 | gtmark SITE1 0 |
| 169 | for i ulong get gbinlen 0 |
| 170 | put gbout get gboutlen 0 uchar get gbin i |
| 171 | put gboutlen 0 add get gboutlen 0 1 |
| 172 | end |
| 173 | gtmark SITE2 1 |
| 174 | end |
| 175 | |
| 176 | fn gtbody to bool |
| 177 | note Reject complete-kernel EXIT regardless of its predicate field (bits 12..15). |
| 178 | note This recognises the repository's EXIT encoding, not every control-flow instruction. |
| 179 | for i ulong div get gbinlen 0 16 |
| 180 | p = mul i 16 |
| 181 | lo = ulong get gbin p |
| 182 | hi = ulong and get gbin add p 1 15 |
| 183 | op = or lo shl hi 8 |
| 184 | if eq op 2381 |
| 185 | put gterr 0 5 |
| 186 | ret false |
| 187 | end |
| 188 | end |
| 189 | ret true |
| 190 | end |
| 191 | |
| 192 | fn gtewrap on ulong cap ulong to bool |
| 193 | note Checked entry point: refuse before emitting anything, then wrap. |
| 194 | if not gtinit cap |
| 195 | ret false |
| 196 | end |
| 197 | if and ne on 0 ne get gtchecks 0 1 |
| 198 | note Enabled needs the instruction and control-flow checks to have passed. |
| 199 | put gterr 0 4 |
| 200 | ret false |
| 201 | end |
| 202 | if ne on 0 |
| 203 | if not gtbody |
| 204 | ret false |
| 205 | end |
| 206 | end |
| 207 | gtwrap on |
| 208 | ret true |
| 209 | end |