interplane.Get Omega
Read in your language

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

branch main Branches Tags History

CUDApp / src / omega_blackwell_encoder.c

134 lines · 6311 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */
2#include "omega_blackwell_encoder.h"
3#include "sha256.h"
4#include <string.h>
5#include "cudapp_native.h"
6#include "omega_bw_bind.h"
7
8/*
9 * Bit-for-bit verified sm_121 instruction sequence for unsigned 32-bit vector addition.
10 * Emits 32 instructions (512 bytes), aligned to 128 bytes.
11 */
12static const BlackwellInstruction VECADD_INSTRUCTIONS[OMEGA_BW_VECADD_INSN_COUNT] = {
13 /* 0000 */ { { 0xff017b82, 0x0000df00, 0x00000800, 0x000fe200 }, "LDC R1, c[0x0][0x37c]" },
14 /* 0010 */ { { 0x00097919, 0x00000000, 0x00002500, 0x000e2200 }, "S2R R9, SR_CTAID.X" },
15 /* 0020 */ { { 0xff0477ac, 0x00006c00, 0x08000800, 0x000e2200 }, "LDCU UR4, c[0x0][0x360]" },
16 /* 0030 */ { { 0x00007919, 0x00000000, 0x00002100, 0x000e2200 }, "S2R R0, SR_TID.X" },
17 /* 0040 */ { { 0xff0577ac, 0x00007300, 0x08000800, 0x000e6200 }, "LDCU UR5, c[0x0][0x398]" },
18 /* 0050 */ { { 0x09097c24, 0x00000004, 0x0f8e0200, 0x001fca00 }, "IMAD R9, R9, UR4, R0" },
19 /* 0060 */ { { 0x09007c0c, 0x00000005, 0x0bf06070, 0x002fda00 }, "ISETP.GE.U32.AND P0, PT, R9, UR5, PT" },
20 /* 0070 */ { { 0x0000094d, 0x00000000, 0x03800000, 0x000fea00 }, "@P0 EXIT" },
21 /* 0080 */ { { 0xff027b82, 0x0000e000, 0x00000a00, 0x000e2200 }, "LDC.64 R2, c[0x0][0x380]" },
22 /* 0090 */ { { 0xff0477ac, 0x00006b00, 0x08000a00, 0x000e6e00 }, "LDCU.64 UR4, c[0x0][0x358]" },
23 /* 00a0 */ { { 0xff047b82, 0x0000e200, 0x00000a00, 0x000eb000 }, "LDC.64 R4, c[0x0][0x388]" },
24 /* 00b0 */ { { 0xff067b82, 0x0000e400, 0x00000a00, 0x000ee200 }, "LDC.64 R6, c[0x0][0x390]" },
25 /* 00c0 */ { { 0x09027825, 0x00000004, 0x078e0002, 0x001fcc00 }, "IMAD.WIDE.U32 R2, R9, 0x4, R2" },
26 /* 00d0 */ { { 0x02027981, 0x00000004, 0x0c1e1900, 0x002f2200 }, "LDG.E R2, desc[UR4][R2.64]" },
27 /* 00e0 */ { { 0x09047825, 0x00000004, 0x078e0004, 0x004fcc00 }, "IMAD.WIDE.U32 R4, R9, 0x4, R4" },
28 /* 00f0 */ { { 0x04057981, 0x00000004, 0x0c1e1900, 0x000f2200 }, "LDG.E R5, desc[UR4][R4.64]" },
29 /* 0100 */ { { 0x09067825, 0x00000004, 0x078e0006, 0x008fe200 }, "IMAD.WIDE.U32 R6, R9, 0x4, R6" },
30 /* 0110 */ { { 0x02097210, 0x00000005, 0x07ffe0ff, 0x010fca00 }, "IADD3 R9, PT, PT, R2, R5, RZ" },
31 /* 0120 */ { { 0x06007986, 0x00000009, 0x0c101904, 0x000fe200 }, "STG.E desc[UR4][R6.64], R9" },
32 /* 0130 */ { { 0x0000794d, 0x00000000, 0x03800000, 0x000fea00 }, "EXIT" },
33 /* 0140 */ { { 0x00fc7947, 0xfffffffc, 0x0383ffff, 0x000fc000 }, "BRA (.L_x_0)" },
34 /* 0150 */ { { 0x00007918, 0x00000000, 0x00000000, 0x000fc000 }, "NOP" },
35 /* 0160 */ { { 0x00007918, 0x00000000, 0x00000000, 0x000fc000 }, "NOP" },
36 /* 0170 */ { { 0x00007918, 0x00000000, 0x00000000, 0x000fc000 }, "NOP" },
37 /* 0180 */ { { 0x00007918, 0x00000000, 0x00000000, 0x000fc000 }, "NOP" },
38 /* 0190 */ { { 0x00007918, 0x00000000, 0x00000000, 0x000fc000 }, "NOP" },
39 /* 01a0 */ { { 0x00007918, 0x00000000, 0x00000000, 0x000fc000 }, "NOP" },
40 /* 01b0 */ { { 0x00007918, 0x00000000, 0x00000000, 0x000fc000 }, "NOP" },
41 /* 01c0 */ { { 0x00007918, 0x00000000, 0x00000000, 0x000fc000 }, "NOP" },
42 /* 01d0 */ { { 0x00007918, 0x00000000, 0x00000000, 0x000fc000 }, "NOP" },
43 /* 01e0 */ { { 0x00007918, 0x00000000, 0x00000000, 0x000fc000 }, "NOP" },
44 /* 01f0 */ { { 0x00007918, 0x00000000, 0x00000000, 0x000fc000 }, "NOP" },
45};
46
47/* Both builds call cg_vecadd.om; C serialization is an explicit default-OFF oracle. */
48#ifdef OMEGA_GPU_NO_POSIX
49#include "omega_native.h"
50#define omega_cg_vec omega2_cg_vec
51#endif
52#if defined(CUDAPP_MIGRATION_PENDING_C_REGALLOC)
53int omega_blackwell_encode_vecadd(uint8_t *code_buf, size_t max_len, size_t *out_len) {
54 if (!code_buf || max_len < OMEGA_BW_VECADD_CODE_SIZE) return -1;
55
56 uint8_t *p = code_buf;
57 for (size_t i = 0; i < OMEGA_BW_VECADD_INSN_COUNT; i++) {
58 for (int w = 0; w < 4; w++) {
59 uint32_t val = VECADD_INSTRUCTIONS[i].w[w];
60 p[0] = (uint8_t)(val & 0xFF);
61 p[1] = (uint8_t)((val >> 8) & 0xFF);
62 p[2] = (uint8_t)((val >> 16) & 0xFF);
63 p[3] = (uint8_t)((val >> 24) & 0xFF);
64 p += 4;
65 }
66 }
67
68 if (out_len) *out_len = OMEGA_BW_VECADD_CODE_SIZE;
69 return 0;
70}
71#else
72int omega_blackwell_encode_vecadd(uint8_t *code_buf, size_t max_len, size_t *out_len) {
73 if (!code_buf || max_len < OMEGA_BW_VECADD_CODE_SIZE) return -1;
74 uint64_t result;
75 if (omega_cg_vec(&result, code_buf, OMEGA_BW_VECADD_CODE_SIZE) != 0 || result != 0)
76 return -1;
77 if (out_len) *out_len = OMEGA_BW_VECADD_CODE_SIZE;
78 return 0;
79}
80#endif
81
82int omega_blackwell_compute_code_digest(const uint8_t *code_buf, size_t len, uint8_t *out_digest) {
83#ifndef BW_BIND_C_PRIMARY
84 uint8_t w[256] = {0};
85 if (!code_buf || !out_digest || omega_bw_bind(7, len, 0, w) != 0) return -1;
86#else
87 if (!code_buf || len == 0 || !out_digest) return -1;
88#endif
89 sha256_ctx ctx;
90 sha256_init(&ctx);
91 sha256_update(&ctx, code_buf, len);
92 sha256_final(&ctx, out_digest);
93 return 0;
94}
95
96int omega_blackwell_verify_encoder_fixtures(void) {
97 uint8_t code_buf[OMEGA_BW_VECADD_CODE_SIZE];
98 size_t out_len = 0;
99 if (omega_blackwell_encode_vecadd(code_buf, sizeof(code_buf), &out_len) != 0) {
100 return -1;
101 }
102 if (out_len != OMEGA_BW_VECADD_CODE_SIZE) {
103 return -1;
104 }
105
106 uint8_t digest[32];
107 if (omega_blackwell_compute_code_digest(code_buf, out_len, digest) != 0) {
108 return -1;
109 }
110
111 static const char HEXDIGITS[] = "0123456789abcdef";
112 char hex[65];
113 for (int i = 0; i < 32; i++) { /* no sprintf: this file is part of the freestanding link target */
114 hex[i * 2] = HEXDIGITS[digest[i] >> 4];
115 hex[i * 2 + 1] = HEXDIGITS[digest[i] & 0x0f];
116 }
117 hex[64] = 0;
118
119 if (strcmp(hex, OMEGA_BW_VECADD_EXPECTED_SHA256) != 0) {
120 return -1;
121 }
122
123 /* Verify critical instruction patterns: */
124 /* 1. S2R R9, SR_CTAID.X */
125 if (VECADD_INSTRUCTIONS[1].w[0] != 0x00097919) return -1;
126 /* 2. S2R R0, SR_TID.X */
127 if (VECADD_INSTRUCTIONS[3].w[0] != 0x00007919) return -1;
128 /* 3. IADD3 R9, PT, PT, R2, R5, RZ */
129 if (VECADD_INSTRUCTIONS[17].w[0] != 0x02097210) return -1;
130 /* 4. STG.E desc[UR4][R6.64], R9 */
131 if (VECADD_INSTRUCTIONS[18].w[0] != 0x06007986) return -1;
132
133 return 0;
134}