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 / src / omega_gpu_code_alloc.h
| 1 | /* SPDX-License-Identifier: Apache-2.0 |
| 2 | * Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /* Size of a GPU code buffer. Header only: no device, no physics headers. |
| 18 | * |
| 19 | * The SM instruction cache prefetches past the last instruction of a kernel, so the code buffer must |
| 20 | * stay mapped for 2 KB after the code. Source (NVIDIA guidance as recorded by Mesa NVK, |
| 21 | * src/nouveau/vulkan/nvk_device.c, shader heap set-up): "the I-cache pre-fetches and NVIDIA has |
| 22 | * informed us overallocating shaders BOs by 2K is sufficient". A page-rounded buffer alone leaves |
| 23 | * whatever happens to be left in the last page (2560 B of code: 1536 B; 2688 B: 1408 B). |
| 24 | * |
| 25 | * omega#323 (2026-10-07): GB10 Xid 31, GPCCLIENT_GCC FAULT_PTE read on a page boundary while the |
| 26 | * head_dim 128 attention kernel (2688 B) ran; that this tail is the cause is INFERRED until the |
| 27 | * padded build turns the fault off on the chip. Every code upload sizes its buffer here so no site |
| 28 | * can drop the tail (tests/test_omega_gpu_code_alloc.c also scans src/ for page-rounded code sizes). |
| 29 | */ |
| 30 | #ifndef OMEGA_GPU_CODE_ALLOC_H |
| 31 | #define OMEGA_GPU_CODE_ALLOC_H |
| 32 | |
| 33 | #include <stddef.h> |
| 34 | #include <stdint.h> |
| 35 | |
| 36 | #define OMEGA_GPU_CODE_PREFETCH_TAIL 2048u |
| 37 | #define OMEGA_GPU_CODE_PAGE 0x1000u |
| 38 | |
| 39 | /* Bytes to allocate for code_size bytes of code: code + 2 KB, rounded up to a page, at least one page. |
| 40 | * 0 when the sum would overflow (callers refuse the upload). */ |
| 41 | static inline size_t omega_gpu_code_alloc_bytes(size_t code_size) { |
| 42 | #ifdef OMEGA_GPU_CODE_ALLOC_MUTANT_NO_TAIL |
| 43 | const size_t tail = 0; /* mutant: the page-rounded size every site used before omega#323 */ |
| 44 | #else |
| 45 | const size_t tail = OMEGA_GPU_CODE_PREFETCH_TAIL; |
| 46 | #endif |
| 47 | if (code_size > SIZE_MAX - tail - (OMEGA_GPU_CODE_PAGE - 1u)) return 0; |
| 48 | size_t b = (code_size + tail + (OMEGA_GPU_CODE_PAGE - 1u)) & ~(size_t)(OMEGA_GPU_CODE_PAGE - 1u); |
| 49 | return b < OMEGA_GPU_CODE_PAGE ? OMEGA_GPU_CODE_PAGE : b; |
| 50 | } |
| 51 | |
| 52 | #endif |