Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/CUDApp/blob/main/docs/SM121_INSTRUCTION_SPEC.md # 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 Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `CUDApp / docs / SM121_INSTRUCTION_SPEC.md` 163 lines · 9927 bytes Raw Blame History Rendered 1 # Authoritative SM121 Instruction Specification Format 2 3 This document specifies the authoritative single instruction-field table format implemented in `om/encoder/bw_spec_sm121.om`, following Drake's three-layer architecture (Meaning -> Realization -> Encoding) described in the Omega/encoder direction. 4 5 ## Overview 6 7 In Blackwell (SM121 / GB10), instructions are represented as 128-bit words (four 32-bit little-endian words `w[0..3]`). 8 9 Instead of writing imperative bit-shifting procedures for encoding and separate pattern-matching tables for decoding, both operations are derived from **one authoritative specification table** in native `.om`. 10 11 The binary64 register forms (IR 68..78) are implemented in the seeded 12 `bw_enc_sm121.om` codegen encoder, not this older table's 0..62 catalog. 13 See [Binary64 BlackwellIR register forms](CODEGEN_F64.md) for their independently 14 derived encoding facts, simulator precision contract and chip runbook. 15 16 The specification defines: 17 1. **Instruction Form Identification**: 18 - `op`: Blackwell IR opcode (0..62). 19 - `var`: Variant selector when multiple forms share an opcode (e.g., `IMAD_RRR` vs `IMAD_RRU` vs `IMAD_RRI`). 20 - Fixed bits and masks for words `w[0..2]`. 21 - Default control word `w[3]`. 22 - Field count `nf` (number of parameterized fields in the instruction). 23 2. **Parameterized Fields**: 24 - `role`: Field semantic role (dest register, source register, predicate, immediate). 25 - `word`: Word index (0, 1, or 2) in the 128-bit instruction. 26 - `shift`: Bit position within the 32-bit word. 27 - `width`: Bit width of the field. 28 - `is_s`: Signedness (1 for signed two's complement, 0 for unsigned). 29 - `min_v`: Minimum valid value (bounds check). 30 - `max_v`: Maximum valid value (bounds check). 31 32 ## Field Roles 33 34 The following integer constants represent semantic field roles in `bw_spec_sm121.om`: 35 36 | Role ID | Constant | Meaning | Source in IR (`o[]`) | Bounds Check | 37 |---|---|---|---|---| 38 | 0 | `R_NONE` | Unused slot | N/A | None | 39 | 1 | `R_RD` | Destination GPR (0..255) | `o[1]` | `0 <= v <= 255` | 40 | 2 | `R_RA` | Source 1 GPR (0..255) | `o[2]` | `0 <= v <= 255` | 41 | 3 | `R_RB` | Source 2 GPR (0..255) | `o[3]` | `0 <= v <= 255` | 42 | 4 | `R_RC` | Source 3 GPR (0..255) | `o[4]` | `0 <= v <= 255` | 43 | 5 | `R_URD` | Destination Uniform Reg (0..31) | `o[1]` | `0 <= v <= 31` | 44 | 6 | `R_URA` | Source 1 Uniform Reg (0..31) | `o[2]` | `0 <= v <= 31` | 45 | 7 | `R_URB` | Source 2 Uniform Reg (0..31) | `o[5]` or `o[1]` | `0 <= v <= 31` | 46 | 8 | `R_PRED`| 4-bit Guard Predicate | `pred(o)` | `0 <= v <= 7` (or 8) | 47 | 9 | `R_IMM32`| 32-bit Immediate | `o[6]` | Full 32-bit range | 48 | 10 | `R_OFF24`| Signed 24-bit byte offset | `o[6]` | `-`8388608` <= v <= `8388607`` | 49 | 11 | `R_IMM16`| 16-bit Immediate | `o[6]` | `0 <= v <= 65535` | 50 | 12 | `R_BAR` | Barrier register | `o[11]` | Declared form bounds | 51 | 13 | `R_BSSY` | Relative instruction delta | `o[6]` | Declared bounds and exact representability of `(v - 1) * 16` | 52 | 14 | `R_BRA` | Branch delta | `o[6]` | Unsigned 32-bit IR representation | 53 | 15 | `R_LDC` | Constant-bank offset | `o[6]` | Declared bounds and exact alignment/field representation | 54 | 16 | `R_SHFL` | Shuffle immediate | `o[6]` | Raw value, no masking before validation | 55 | 17 | `R_LUT` | LOP3 LUT | `o[6]` | Raw value, no masking before validation | 56 | 18 | `R_S2R` | Special-register index | `o[6]` | Declared form bounds | 57 | 19 | `R_LDSW` | Shared-memory offset | `o[6]` | Declared form bounds and exact representation | 58 | 20 | `R_FSET` | FSETP comparison encoding | `o[6]` | Declared form bounds; zero selects the default encoding | 59 | 21 | `R_PRED3` | Predicate destination/source | `o[1]` or `o[4]` | Raw value, no masking before validation | 60 61 ## Specification Table Representation 62 63 The reusable module is function-mode Omega: no global pools and no `main`. 64 The owner allocates a zero-filled fixed-size `ulong 3001` workspace once per 65 encoding batch and passes it explicitly. Slots 0..2999 retain the authoritative 66 flat specification table; slot 3000 is the initialization marker, written only 67 after every table initializer completes. `sp_init` is idempotent for that owner. 68 `sp_enc` and `sp_dec` initialize on first use, including a cold decoder call. 69 70 The workspace is private process state, not an authenticated input format. Do not 71 accept it from a caller or serialized artifact, mutate it after initialization, 72 or race/share it across concurrent owners. Reuse it within one batch rather than 73 rebuilding the table for every instruction. Fixed-size array parameters occupy 74 one seeded argument slot. Initialization setters use at most six arguments. 75 76 Public signatures are `sp_init(state edit ulong 3001)`, 77 `sp_enc(state edit ulong 3001, o view ulong 16, w edit ulong 4) -> ulong`, and 78 `sp_dec(state edit ulong 3001, w view ulong 4, o edit ulong 16) -> ulong`. 79 Emit words only after `E_OK`: a failed encoder call may have already written 80 partial output. A native adapter must store these validated words directly, 81 including padding NOPs, rather than re-encode through the reference oracle. 82 83 Standalone assertions now live in `om/encoder/bw_spec_test.om`; concatenate that 84 driver after the reusable module for executable mode. No source stripping or 85 second encoder table is needed. The original audit source remains frozen at 86 CUDApp ``85c059cc15edf704c9833fe4832f18c052ee83b3`` (draft PR #160). 87 88 Each instruction form header occupies 10 slots: 89 - `+0`: `op` (Blackwell IR opcode) 90 - `+1`: `var` (variant selector) 91 - `+2`: `w0_fix` (fixed bits for word 0) 92 - `+3`: `w0_mask` (mask of fixed bits for word 0) 93 - `+4`: `w1_fix` (fixed bits for word 1) 94 - `+5`: `w1_mask` (mask of fixed bits for word 1) 95 - `+6`: `w2_fix` (fixed bits for word 2) 96 - `+7`: `w2_mask` (mask of fixed bits for word 2) 97 - `+8`: `w3_def` (default control word 3) 98 - `+9`: `nf` (number of parameterized fields, 0..5) 99 100 Following the header, each of the `nf` fields occupies 7 slots: 101 - `+0`: `role` 102 - `+1`: `word` 103 - `+2`: `shift` 104 - `+3`: `width` 105 - `+4`: `is_s` 106 - `+5`: `min_v` 107 - `+6`: `max_v` 108 109 Total slots per form is `10 + 5 * 7 = 45`. For 63 forms, the table uses `63 * 45 = 2835` slots. 110 111 ## Error Refusal Codes 112 113 The encoder refuses invalid input with explicit error codes instead of truncating or wrapping: 114 115 - `E_OK = 0`: Successful encoding or decoding. 116 - `E_UNSUP = 1`: Opcode or variant is not supported. 117 - `E_RANGE = 2`: Operand exceeds declared bounds or cannot be represented without losing semantic bits. 118 - `E_NOMAT = 3`: Instruction machine words do not match any known form's fixed bits and masks. 119 120 ## Dual Derivation 121 122 1. **`sp_enc state o w -> err`**: 123 - Matches `o` to an instruction form. 124 - Sets fixed word bits `w0_fix..w2_fix` and control word `w3`. 125 - Validates the raw semantic operand before inserting masked bits. 126 - Checks special-layout immediates by decoding their inserted representation and requiring exact equality. 127 2. **`sp_dec state w o -> err`**: 128 - Initializes its owner-private table automatically, including standalone cold calls. 129 - Searches `sp_tbl` for a form whose fixed bits match: `(w[k] & mask) == fix`. 130 - Extracts each field: `val = (w[word] >> shift) & mask`. 131 - Populates decoded IR array `o[]`. 132 3. **Host verification**: 133 - Compares every active semantic role after decoding, not just the opcode. Inactive IR packing slots are not operands. 134 - Canonicalizations are explicit: absent source operands for LOP3/SHF/IMAD.HI mean RZ; zero control selects the form default; zero FSETP immediate selects its default encoding. 135 - IMAD RRU has a GPR destination and a uniform second source. Corpus generation verifies that each intended form actually dispatches. 136 - Boundary fixtures select representable aligned immediates; separate audit regressions require refusal of raw lossy values. 137 - `sh tools/test_bw_spec_encoder.sh` checks reference bytes, active operands, cold decode, malformed streams and the operands printed by an independent decoder: Omega's own sm_121 disassembler (`SM121DIS`, `make test-om-spec-encoder`) by default, or NVIDIA's offline nvdisasm with `SPEC_ENCODER_ORACLE=nvdisasm` (`make test-om-spec-encoder-nvdisasm-oracle`). Producer failure, truncated records and read errors fail closed. 138 - All checks are host-only. Physical qualification of the changed encoder remains NOT_RUN until an authorized chip run. Frozen historical receipts are unchanged. 139 140 ## Function-mode host regression 141 142 `OMEGA_BRIDGE=/path/to/omega-bridge sh tools/test_bw_spec_encoder.sh` also 143 compiles the reusable module plus `bw_spec_native.om` as function bytes and 144 executes the generated bridge binding through the existing C oracle driver. 145 The semantic assertions remain in Omega. They cover cold decoder initialization, 146 private workspace isolation, warmed-table mutation without accidental reinit, 147 full RRU destination/source semantics, a deliberately corrupted expected operand 148 (return 1), and actual raw SHFL refusal (return `E_RANGE`, 2). Build, bridge, 149 compiler-pin, ELF-image and binding-status failures are not semantic mutant kills. 150 151 The exact compiler remains omega-mv ``e5b32567a7aff3ce594e3254a622a846a6cd190b``, 152 SHA-256 ``2c82480596bbdc50cda9626866209ef662d5834f278f839ff4ce0250c495029f``. 153 The unchanged bridge source `src/native.om` is pinned by SHA-256 154 ``c77a5922f91d53d4a0bbd4e7b1c4703f7098263abc843be2918db1ece7496782``. 155 This command proves a host function binding, not physical GPU qualification or 156 the downstream `bc_encode` consumer; that consumer requires its own native 157 runtime fixture at its exact integrated source pin. Physical qualification is 158 NOT_RUN. Historical audit and device receipts are not rewritten. 159 160 The native probe executes AArch64 function bytes, so it runs only with an 161 AArch64 C target. Elsewhere the script prints NOT_RUN for it and exits 3, 162 unless `BW_SPEC_NATIVE_PROBE_NOT_RUN_OK=1` accepts that row as NOT_RUN; 163 `make test-om-spec-encoder` sets it on non-AArch64 hosts only.