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/omega-mv/patches/83780a44cdea270e # 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 Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers ## Omega consumes CUDA++ closed `83780a44cdea270e` #618 · opened 2026-10-22 04:52 UTC · closed 2026-10-30 02:45 UTC Imported from GitHub aien-dev/omega-mv#618, by drakestapleton Base `a8ffd2b912c84197d08f963b05f9c7d8b30a3f8a` · Head `67c70741c8cec5c33e28a099d7c83bbd4f3ab839` drakestapleton wrote on 2026-10-22 04:52 UTC Drake 2026-10-22: this branch to the front. Host and simulator receipts are on the branch. Chip runs are not in this merge. ### Fetch it A git bundle of the pull request's head, 7461 bytes. ``` curl -fsSL https://interplane.io/drakestapleton/omega-mv/patches/83780a44cdea270e.bundle -o 83780a44cdea270e.bundle git fetch 83780a44cdea270e.bundle refs/pull/618/head ``` Raw bundle · Diff ### Provenance This proposal is imported from GitHub; it is not signed by its author. Its import statement and source record: ``` github-import 1 aien-dev/omega-mv 618 67c70741c8cec5c33e28a099d7c83bbd4f3ab839 ``` ``` {"kind":"github-import","owner":"aien-dev","repo":"omega-mv","number":618,"url":"https://github.com/aien-dev/omega-mv/pull/618","export_sha256":"7319147b350ad908bf0b731068ec3be7b8dee848041dc862f71455e8e4680140","imported_utc":"2026-11-18T16:35:48Z"} ``` ### Changes ``` diff --git a/.gitignore b/.gitignore index fd0ce970e..086a01172 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,157 @@ build/qual-runs/ /omega tests/test_omega_numeric .crumb.local + +# CUDA++ files, linked in by tools/cudapp-link.sh (see cudapp.files) +/src/omega_blackwell_codegen_attn.c +/src/omega_blackwell_codegen_attn.h +/src/omega_blackwell_codegen_bf16.c +/src/omega_blackwell_codegen_bf16.h +/src/omega_blackwell_codegen.c +/src/omega_blackwell_codegen_dec.c +/src/omega_blackwell_codegen_dec.h +/src/omega_blackwell_codegen_gemm.c +/src/omega_blackwell_codegen_gemm.h +/src/omega_blackwell_codegen.h +/src/omega_blackwell_codegen_moe2.c +/src/omega_blackwell_codegen_moe2.h +/src/omega_blackwell_codegen_moe.c +/src/omega_blackwell_codegen_moe.h +/src/omega_blackwell_codegen_pattn.c +/src/omega_blackwell_codegen_pattn.h +/src/omega_blackwell_codegen_pf.c +/src/omega_blackwell_codegen_pf.h +/src/omega_blackwell_codegen_pfhost.c +/src/omega_blackwell_codegen_pfhost.h +/src/omega_blackwell_codegen_qg.c +/src/omega_blackwell_codegen_qg.h +/src/omega_blackwell_codegen_qmma.c +/src/omega_blackwell_codegen_qmma.h +/src/omega_blackwell_codegen_router.c +/src/omega_blackwell_codegen_router.h +/src/omega_blackwell_codegen_tc2.c +/src/omega_blackwell_codegen_tc2.h +/src/omega_blackwell_codegen_tc.c +/src/omega_blackwell_codegen_tc.h +/src/omega_blackwell_codegen_tern.c +/src/omega_blackwell_codegen_tern.h +/src/omega_blackwell_encoder.c +/src/omega_blackwell_encoder.h +/src/omega_blackwell_engine.c +/src/omega_blackwell_engine.h +/src/omega_blackwell_matmul.c +/src/omega_blackwell_matmul.h +/src/omega_blackwell_qmd.c +/src/omega_blackwell_qmd.h +/src/omega_blackwell_realize.c +/src/omega_blackwell_realize.h +/src/omega_blackwell_submit.c +/src/omega_blackwell_submit.h +/src/omega_bw_em.h +/src/omega_bw_em_q.h +/src/omega_bw_reconv.h +/src/omega_gpu_attention_api.c +/src/omega_gpu_attention_api.h +/src/omega_gpu_bf16_gemv_api.c +/src/omega_gpu_bf16_gemv_api.h +/src/omega_gpu_cmdlist.c +/src/omega_gpu_cmdlist.h +/src/omega_gpu_code_alloc.h +/src/omega_gpu_decode_attn_api.c +/src/omega_gpu_decode_attn_api.h +/src/omega_gpu_decode_graph.c +/src/omega_gpu_decode_graph.h +/src/omega_gpu_elementwise_api.c +/src/omega_gpu_elementwise_api.h +/src/omega_gpu_engine.c +/src/omega_gpu_engine.h +/src/omega_gpu_fmt.c +/src/omega_gpu_fmt.h +/src/omega_gpu_fp8_api.c +/src/omega_gpu_fp8_api.h +/src/omega_gpu_fp8gemm_api.c +/src/omega_gpu_fp8gemm_api.h +/src/omega_gpu_fp8_pack.c +/src/omega_gpu_hostmath.c +/src/omega_gpu_hostmath.h +/src/omega_gpu_matmul_api.c +/src/omega_gpu_matmul_api.h +/src/omega_gpu_mem_cached.c +/src/omega_gpu_mem_cached.h +/src/omega_gpu_moe_api.c +/src/omega_gpu_moe_api.h +/src/omega_gpu_pfgemm.c +/src/omega_gpu_pfhost.c +/src/omega_gpu_pfhost.h +/src/omega_gpu_platform.c +/src/omega_gpu_platform.h +/src/omega_gpu_platform_linux.c +/src/omega_gpu_platform_linux.h +/src/omega_gpu_prefill_attn_api.c +/src/omega_gpu_prefill_attn_api.h +/src/omega_gpu_prefill.c +/src/omega_gpu_prefill.h +/src/omega_gpu_router_api.c +/src/omega_gpu_router_api.h +/src/omega_gpu_serving.c +/src/omega_gpu_serving.h +/src/omega_gpu_session.c +/src/omega_gpu_session.h +/src/omega_gpu_tcgemm_api.c +/src/omega_gpu_tcgemm_api.h +/src/omega_gpu_tern_api.c +/src/omega_gpu_tern_api.h +/src/omega_gpu_wait.c +/src/omega_gpu_wait.h +/tests/bw_hmma_model.h +/tests/bw_qmma_model.h +/tests/bw_warp_sim.h +/tests/bw_warp_sim_k.h +/tests/bw_warp_sim_pa.h +/tests/bw_warp_sim_tc.h +/tests/fake_m16_native.c +/tests/fake_m16_native.h +/tests/gpu_attention_test.c +/tests/gpu_bf16_gemv_test.c +/tests/gpu_cmdlist_test.c +/tests/gpu_decode_attn_test.c +/tests/gpu_decode_graph_test.c +/tests/gpu_elementwise_test.c +/tests/gpu_fp8_api_test.c +/tests/gpu_fp8gemm_test.c +/tests/gpu_gpr_boundary_test.c +/tests/gpu_l2_reuse_test.c +/tests/gpu_l2_stale_test.c +/tests/gpu_launch_cost_test.c +/tests/gpu_lmhead_fp8_study.c +/tests/gpu_matmul_api_test.c +/tests/gpu_matmul_r4_receipt.c +/tests/gpu_moe_window_test.c +/tests/gpu_pfgemm_test.c +/tests/gpu_pfhost_test.c +/tests/gpu_pf_mem_test.c +/tests/gpu_prefill_attn_test.c +/tests/gpu_qgemm_test.c +/tests/gpu_qmma_probe_test.c +/tests/gpu_reconv_test.c +/tests/gpu_router_test.c +/tests/gpu_serving_alloc_count_test.c +/tests/gpu_session_probe.c +/tests/gpu_tcgemm_test.c +/tests/gpu_tc_peak_test.c +/tests/gpu_tc_probe_test.c +/tests/test_omega_blackwell_engine.c +/tests/test_omega_codegen_freestanding.c +/tests/test_omega_gpu_code_alloc.c +/tests/test_omega_gpu_engine.c +/tests/test_omega_gpu_engine_compile.c +/tests/test_omega_gpu_engine_gb10.c +/tests/test_omega_gpu_platform.c +/tests/test_omega_gpu_platform_linux.c +/tests/test_omega_gpu_session_platform.c +/tests/test_omega_gpu_wait.c +/mk/omega-gpu-engine.mk +/mk/omega-gpu-code-alloc.mk +/mk/omega-gpu-wait.mk +/tools/gpu_engine_mutations.sh +/tools/gpu_engine_backend_mutations.sh diff --git a/Makefile b/Makefile index 3551fea3f..8c5856001 100644 --- a/Makefile +++ b/Makefile @@ -2,6 +2,15 @@ CC ?= gcc .DEFAULT_GOAL := all PHYSICS_DIR ?= ../physics OUT_DIR ?= build +# CUDA++ (aien-dev/CUDAplusplus, MIT) holds the GPU toolchain, runtime and libraries since 2026-10-21. +# They are linked in from CUDAPP_DIR at cudapp.lock before anything else is read (tools/cudapp-link.sh). +# Default: a CUDA++ checkout next to omega if there is one, else build/cudapp (cloned at cudapp.lock on first use). +CUDAPP_DIR ?= $(if $(wildcard ../CUDAplusplus/src),../CUDAplusplus,build/cudapp) +GPU_PROD_TARGET ?= build/omegatool +CUDAPP_LINK := $(shell tools/cudapp-link.sh "$(CUDAPP_DIR)" >&2 || echo FAIL) +ifneq ($(CUDAPP_LINK),) +$(error CUDA++ files could not be linked from CUDAPP_DIR=$(CUDAPP_DIR); see the cudapp-link message above) +endif -include mk/*.mk PHYSICS_LOCK_CHECK ?= 1 @@ -75,7 +84,7 @@ LEARNER = $(OUT_DIR)/crumbline-learner .PHONY: all clean check-physics-lock crumbline-learner test-crumbline test-m19 test test-m5 test-m6 test-m7 test-m8 test-m9 test-m10 test-m11 test-m12 test-m13 test-m14 test-m15 test-m17 test-r3 test-i11-wake-merge test-action-graph test-state-projection test-capability-query test-capability-graph test-skillroute-compose test-semantic-comm test-cognitive-routing test-sem-incremental test-branch-reuse test-jspace-prod test-plan-reuse test-cortex -all: check-physics-lock $(TARGET) +all: check-physics-lock check-cudapp-shared $(TARGET) check-physics-lock: @if [ "$(PHYSICS_LOCK_CHECK)" != "0" ]; then \ @@ -2604,3 +2613,11 @@ $(GPU_LAUNCH_COST_TEST): tests/gpu_launch_cost_test.c src/omega_gpu_fp8_api.h sr GPU_L2_REUSE_TEST = $(OUT_DIR)/gpu_l2_reuse_test $(GPU_L2_REUSE_TEST): tests/gpu_l2_reuse_test.c src/omega_gpu_fp8_api.h src/omega_gpu_cmdlist.h $(OUT_DIR)/libomega_gpu.a $(CC) $(CFLAGS) -ffp-contract=off -o $@ tests/gpu_l2_reuse_test.c $(OUT_DIR)/libomega_gpu.a -lpthread -lm + +# The shared base (CUDA++ tools/extract/shared.txt): files both repositories need. Omega owns them and +# builds only its own copies; the pinned CUDA++ must hold byte-identical copies, or this fails. +CUDAPP_SHARED = src/sha256.c src/sha256.h src/omega_types.h src/omega_vector.c src/omega_vector.h \ + src/omega_core.c src/omega_core.h src/omega_canonical.c src/omega_canonical.h tools/mutation_runner.sh +.PHONY: check-cudapp-shared +check-cudapp-shared: + @for f in $(CUDAPP_SHARED); do cmp -s "$$f" "$(CUDAPP_DIR)/$$f" || { echo "error: $$f differs from CUDAPP_DIR=$(CUDAPP_DIR)/$$f (shared base drift: change both, then move cudapp.lock)" >&2; exit 1; }; done diff --git a/README.md b/README.md index 87f91fbf1..a3b25993e 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,8 @@ The live status is owned by [aien-architecture](https://github.com/aien-spark/ai ## How this fits with the other repositories +**CUDA++** ([aien-dev/CUDAplusplus](https://github.com/aien-dev/CUDAplusplus), MIT) is the GPU half of the Omega runtime: the sm_121 toolchain, the GPU runtime, the GPU libraries and the driver client, split out of this repository with their history on 2026-10-21 so anyone can use them. Omega builds them at the commit in `cudapp.lock`: `make` links each file listed in `cudapp.files` from a CUDA++ checkout (`../CUDAplusplus` if it exists, else `build/cudapp`, cloned on first use) by `tools/cudapp-link.sh`, so every rule and script here works unchanged. Change those files in CUDA++, then move `cudapp.lock`. Nine shared files (`CUDAPP_SHARED` in the Makefile) live in both repositories; `make check-cudapp-shared` fails if they differ. + [aienos](https://github.com/aien-spark/aienos) is the kernel Omega is meant to run on. [physics](https://github.com/aien-spark/physics) provides FORGE machine realization and the GB10 native path (pinned in `physics.lock`). [aien-architecture](https://github.com/aien-spark/aien-architecture) owns status and sequencing. ARGUS-0/1 (a defensive plane) is implemented in aienos (`native/argus`, pinned by `argus.lock`); Omega only carries the opt-in `RX_ARGUS` hooks (`src/runtime/rx_argus.c`) and receipts in `evidence/ARGUS/`. Physics Zero exists here as a host benchmark apparatus and learner (`src/physics0/`, `tests/physics0/`, `mk/physics0-learner.mk`, `docs/physics0/`, receipts in `evidence/physics0/`), with its specifications in the physics repository. DIRAC-0 is a specification in aien-architecture plus an oracle source in `research/dirac-oracle/` that has never been built or run. The Evolution Arena is a plan only. ## Standing rules @@ -35,7 +37,7 @@ Language rule: Rust is scaffolding, Omega is the destination, and C or assembly ## Build and verify -Needs gcc and make on an AArch64 Linux host: CI runs the host suites on `ubuntu-24.04-arm`, and `test-realize` compiles with `-march=armv8.6-a+dotprod+i8mm+sve`. Host suites need no GPU and no physics checkout: +Needs gcc and make on an AArch64 Linux host: CI runs the host suites on `ubuntu-24.04-arm`, and `test-realize` compiles with `-march=armv8.6-a+dotprod+i8mm+sve`. Host suites need no GPU and no physics checkout (several compile CUDA++ files, which `make` links in first; see above): ```bash make PHYSICS_DIR=/nonexistent PHYSICS_LOCK_CHECK=0 test-compiler-quick # compiler, quick suite diff --git a/cudapp.files b/cudapp.files new file mode 100644 index 000000000..ea4bc04bf --- /dev/null +++ b/cudapp.files @@ -0,0 +1,154 @@ +# Files that live in CUDA++ (aien-dev/CUDAplusplus, MIT) since 2026-10-21. tools/cudapp-link.sh links each +# one from the pinned CUDA++ checkout (CUDAPP_DIR at cudapp.lock) into this tree; .gitignore lists them. +src/omega_blackwell_codegen_attn.c +src/omega_blackwell_codegen_attn.h +src/omega_blackwell_codegen_bf16.c +src/omega_blackwell_codegen_bf16.h +src/omega_blackwell_codegen.c +src/omega_blackwell_codegen_dec.c +src/omega_blackwell_codegen_dec.h +src/omega_blackwell_codegen_gemm.c +src/omega_blackwell_codegen_gemm.h +src/omega_blackwell_codegen.h +src/omega_blackwell_codegen_moe2.c +src/omega_blackwell_codegen_moe2.h +src/omega_blackwell_codegen_moe.c +src/omega_blackwell_codegen_moe.h +src/omega_blackwell_codegen_pattn.c +src/omega_blackwell_codegen_pattn.h +src/omega_blackwell_codegen_pf.c +src/omega_blackwell_codegen_pf.h +src/omega_blackwell_codegen_pfhost.c +src/omega_blackwell_codegen_pfhost.h +src/omega_blackwell_codegen_qg.c +src/omega_blackwell_codegen_qg.h +src/omega_blackwell_codegen_qmma.c +src/omega_blackwell_codegen_qmma.h +src/omega_blackwell_codegen_router.c +src/omega_blackwell_codegen_router.h +src/omega_blackwell_codegen_tc2.c +src/omega_blackwell_codegen_tc2.h +src/omega_blackwell_codegen_tc.c +src/omega_blackwell_codegen_tc.h +src/omega_blackwell_codegen_tern.c +src/omega_blackwell_codegen_tern.h +src/omega_blackwell_encoder.c +src/omega_blackwell_encoder.h +src/omega_blackwell_engine.c +src/omega_blackwell_engine.h +src/omega_blackwell_matmul.c +src/omega_blackwell_matmul.h +src/omega_blackwell_qmd.c +src/omega_blackwell_qmd.h +src/omega_blackwell_realize.c +src/omega_blackwell_realize.h +src/omega_blackwell_submit.c +src/omega_blackwell_submit.h +src/omega_bw_em.h +src/omega_bw_em_q.h +src/omega_bw_reconv.h +src/omega_gpu_attention_api.c +src/omega_gpu_attention_api.h +src/omega_gpu_bf16_gemv_api.c +src/omega_gpu_bf16_gemv_api.h +src/omega_gpu_cmdlist.c +src/omega_gpu_cmdlist.h +src/omega_gpu_code_alloc.h +src/omega_gpu_decode_attn_api.c +src/omega_gpu_decode_attn_api.h +src/omega_gpu_decode_graph.c +src/omega_gpu_decode_graph.h +src/omega_gpu_elementwise_api.c +src/omega_gpu_elementwise_api.h +src/omega_gpu_engine.c +src/omega_gpu_engine.h +src/omega_gpu_fmt.c +src/omega_gpu_fmt.h +src/omega_gpu_fp8_api.c +src/omega_gpu_fp8_api.h +src/omega_gpu_fp8gemm_api.c +src/omega_gpu_fp8gemm_api.h +src/omega_gpu_fp8_pack.c +src/omega_gpu_hostmath.c +src/omega_gpu_hostmath.h +src/omega_gpu_matmul_api.c +src/omega_gpu_matmul_api.h +src/omega_gpu_mem_cached.c +src/omega_gpu_mem_cached.h +src/omega_gpu_moe_api.c +src/omega_gpu_moe_api.h +src/omega_gpu_pfgemm.c +src/omega_gpu_pfhost.c +src/omega_gpu_pfhost.h +src/omega_gpu_platform.c +src/omega_gpu_platform.h +src/omega_gpu_platform_linux.c +src/omega_gpu_platform_linux.h +src/omega_gpu_prefill_attn_api.c +src/omega_gpu_prefill_attn_api.h +src/omega_gpu_prefill.c +src/omega_gpu_prefill.h +src/omega_gpu_router_api.c +src/omega_gpu_router_api.h +src/omega_gpu_serving.c +src/omega_gpu_serving.h +src/omega_gpu_session.c +src/omega_gpu_session.h +src/omega_gpu_tcgemm_api.c +src/omega_gpu_tcgemm_api.h +src/omega_gpu_tern_api.c +src/omega_gpu_tern_api.h +src/omega_gpu_wait.c +src/omega_gpu_wait.h +tests/bw_hmma_model.h +tests/bw_qmma_model.h +tests/bw_warp_sim.h +tests/bw_warp_sim_k.h +tests/bw_warp_sim_pa.h +tests/bw_warp_sim_tc.h +tests/fake_m16_native.c +tests/fake_m16_native.h +tests/gpu_attention_test.c +tests/gpu_bf16_gemv_test.c +tests/gpu_cmdlist_test.c +tests/gpu_decode_attn_test.c +tests/gpu_decode_graph_test.c +tests/gpu_elementwise_test.c +tests/gpu_fp8_api_test.c +tests/gpu_fp8gemm_test.c +tests/gpu_gpr_boundary_test.c +tests/gpu_l2_reuse_test.c +tests/gpu_l2_stale_test.c +tests/gpu_launch_cost_test.c +tests/gpu_lmhead_fp8_study.c +tests/gpu_matmul_api_test.c +tests/gpu_matmul_r4_receipt.c +tests/gpu_moe_window_test.c +tests/gpu_pfgemm_test.c +tests/gpu_pfhost_test.c +tests/gpu_pf_mem_test.c +tests/gpu_prefill_attn_test.c +tests/gpu_qgemm_test.c +tests/gpu_qmma_probe_test.c +tests/gpu_reconv_test.c +tests/gpu_router_test.c +tests/gpu_serving_alloc_count_test.c +tests/gpu_session_probe.c +tests/gpu_tcgemm_test.c +tests/gpu_tc_peak_test.c +tests/gpu_tc_probe_test.c +tests/test_omega_blackwell_engine.c +tests/test_omega_codegen_freestanding.c +tests/test_omega_gpu_code_alloc.c +tests/test_omega_gpu_engine.c +tests/test_omega_gpu_engine_compile.c +tests/test_omega_gpu_engine_gb10.c +tests/test_omega_gpu_platform.c +tests/test_omega_gpu_platform_linux.c +tests/test_omega_gpu_session_platform.c +tests/test_omega_gpu_wait.c +mk/omega-gpu-engine.mk +mk/omega-gpu-code-alloc.mk +mk/omega-gpu-wait.mk +tools/gpu_engine_mutations.sh +tools/gpu_engine_backend_mutations.sh diff --git a/cudapp.lock b/cudapp.lock new file mode 100644 index 000000000..64641c86c --- /dev/null +++ b/cudapp.lock @@ -0,0 +1 @@ +8f0b645dbc859f1b63868cec77880621aa70b42d diff --git a/mk/omega-gpu-code-alloc.mk b/mk/omega-gpu-code-alloc.mk deleted file mode 100644 index 3b217c32d..000000000 --- a/mk/omega-gpu-code-alloc.mk +++ /dev/null @@ -1,18 +0,0 @@ -# GPU code buffer sizing (src/omega_gpu_code_alloc.h): the 2 KB instruction prefetch tail after every -# kernel (omega#323). Host only: no device, no physics headers. Three parts: the size rule, a NO_TAIL -# mutant that must fail it, and a source scan that refuses the page-only code sizing idioms the six -# variable-size upload sites and five fixed-size sites used before. -CODE_ALLOC_SCAN_SRC := src/*.c src/runtime/*.c -CODE_ALLOC_SCAN_RE := code_size \+ 0x[fF]{3}|session_alloc\([^,]*code_size|alloc[a-z_]*\([^,]*, *(0x[0-9a-fA-F]+|OMEGA_DS_MAX_CODE_BYTES), *&[a-z_>.-]*code_mem\) -.PHONY: test-gpu-code-alloc -build/test_omega_gpu_code_alloc: tests/test_omega_gpu_code_alloc.c src/omega_gpu_code_alloc.h - @mkdir -p build - gcc -std=gnu11 -O2 -Wall -Wextra -Werror -Isrc -o $@ tests/test_omega_gpu_code_alloc.c -test-gpu-code-alloc: build/test_omega_gpu_code_alloc - ./build/test_omega_gpu_code_alloc - @gcc -std=gnu11 -O2 -Wall -Wextra -Werror -DOMEGA_GPU_CODE_ALLOC_MUTANT_NO_TAIL -Isrc -o build/test_omega_gpu_code_alloc_no_tail tests/test_omega_gpu_code_alloc.c - @if ./build/test_omega_gpu_code_alloc_no_tail >build/test_omega_gpu_code_alloc_no_tail.log 2>&1; then echo "MUTANT SURVIVED: NO_TAIL"; exit 1; fi - @echo "NO_TAIL mutant: killed ($$(grep -c '^FAIL:' build/test_omega_gpu_code_alloc_no_tail.log) failing checks)" - @if grep -nE '$(CODE_ALLOC_SCAN_RE)' $(CODE_ALLOC_SCAN_SRC); then echo "code buffer sized without the prefetch tail (use omega_gpu_code_alloc_bytes)"; exit 1; fi - @echo "source scan: no page-only code sizing" - @echo "OMEGA_GPU_CODE_ALLOC_GATE PASS" diff --git a/mk/omega-gpu-engine.mk b/mk/omega-gpu-engine.mk deleted file mode 100644 index db63683e4..000000000 --- a/mk/omega-gpu-engine.mk +++ /dev/null @@ -1,231 +0,0 @@ -# Omega GPU Engine (docs/numeric/OMEGA_GPU_ENGINE.md). Picked up by -# `-include mk/*.mk`. Host only: opens no device; needs no physics headers except the two A3b1 targets, not -# part of `all` or `test`. -# test-gpu-engine-compile header + core compile, enum names, refusals, no-op backend (A2/A3a) -# test-gpu-engine the engine core against a fake backend (every state and failure) -# test-gpu-engine-backend-compile syntax-checks src/omega_blackwell_engine.c (A3b1): -c only, no link, no device -# test-gpu-engine-prod-build production build of build/omegatool at the physics pin: links only, opens no device -# test-gpu-engine-mutants removes each protection from src/omega_gpu_engine.c in a scratch -# copy; the host test must fail each one (exit nonzero on a survivor) -# test-gpu-engine-sim (A3b2) the REAL engine core + Blackwell backend + vector wrapper against a simulated driver -# (tests/fake_m16_native.c); host only, no device, no physics C sources -# test-gpu-engine-backend-mutants (A3b2) one mutant per protection in the backend and the vector wrapper -# test-gpu-engine-gb10-compile (A3b2) compile-only check of tests/test_omega_gpu_engine_gb10.c (Phase B chip test, never run here) -# gpu-engine-freestanding (native v1 P07 cut A) the portable GPU files built -ffreestanding with -# OMEGA_GPU_NO_POSIX: no Linux header, no errno, no clock, no libc beyond mem*/str* -# test-codegen-freestanding (cut A.2) codegen hosted vs freestanding byte-identical, bounded code buffers -# test-gpu-engine-freestanding the engine contract test and the platform contract test linked against -# those freestanding objects (host runner, opens no device) -.PHONY: test-gpu-engine-compile test-gpu-engine test-gpu-engine-mutants test-gpu-engine-backend-compile test-gpu-engine-prod-build test-gpu-engine-sim test-gpu-engine-backend-mutants test-gpu-engine-gb10-compile gpu-engine-freestanding test-gpu-engine-freestanding -build/test_omega_gpu_engine_compile: tests/test_omega_gpu_engine_compile.c src/omega_gpu_engine.c src/omega_gpu_engine.h - @mkdir -p build - gcc -std=gnu11 -O2 -Wall -Wextra -Werror -Isrc -DOMEGA_NUMERIC_CPU_ONLY -o $@ tests/test_omega_gpu_engine_compile.c src/omega_gpu_engine.c -test-gpu-engine-compile: build/test_omega_gpu_engine_compile - ./build/test_omega_gpu_engine_compile - -build/test_omega_gpu_engine: tests/test_omega_gpu_engine.c src/omega_gpu_engine.c src/omega_gpu_engine.h - @mkdir -p build - gcc -std=gnu11 -O2 -Wall -Wextra -Werror -Isrc -DOMEGA_NUMERIC_CPU_ONLY -o $@ tests/test_omega_gpu_engine.c src/omega_gpu_engine.c -test-gpu-engine: build/test_omega_gpu_engine - ./build/test_omega_gpu_engine - -test-gpu-engine-mutants: - sh tools/gpu_engine_mutations.sh - -# A3b1: the Blackwell backend includes physics headers (m16_native.h, nvrm.h), so it -# uses the same physics -I paths as the top-level Makefile CFLAGS (Makefile lines 9-14). -GPU_ENGINE_PHYS_INC = -I$(PHYSICS_DIR)/m16 -I$(PHYSICS_DIR)/nvrm \ - -I$(PHYSICS_DIR)/third_party/nvidia-open-580.173.02/src/common/sdk/nvidia/inc \ - -I$(PHYSICS_DIR)/third_party/nvidia-open-580.173.02/kernel-open/common/inc \ - -I$(PHYSICS_DIR)/third_party/nvidia-open-580.173.02/kernel-open/nvidia-uvm \ - -I$(PHYSICS_DIR)/third_party/nvidia-open-580.173.02/src/nvidia/arch/nvalloc/unix/include - -build/omega_blackwell_engine_syntax.o: src/omega_blackwell_engine.c src/omega_blackwell_engine.h src/omega_gpu_engine.h - @mkdir -p build - gcc -std=gnu11 -O2 -Wall -Wextra -Werror -D_GNU_SOURCE -Isrc $(GPU_ENGINE_PHYS_INC) -c -o $@ src/omega_blackwell_engine.c -test-gpu-engine-backend-compile: build/omega_blackwell_engine_syntax.o - -# Links only, opens no device. Uses the real m16_native and nvrm sources at the physics pin. -test-gpu-engine-prod-build: - $(MAKE) build/omegatool - -# A3b2: simulated-driver host test. Links the sources of the Makefile RX_COMPOSE_GPU_SRCS list (plus the -# three core files the vector builder needs) with tests/fake_m16_native.c in place of the physics -# m16_native.c and nvrm.c. Opens no device. -GPU_ENGINE_SIM_SRCS = src/omega_blackwell_submit.c src/omega_blackwell_engine.c src/omega_gpu_engine.c \ - src/omega_blackwell_matmul.c src/omega_blackwell_codegen.c src/omega_blackwell_encoder.c \ - src/omega_blackwell_qmd.c src/omega_blackwell_realize.c src/omega_vector.c src/omega_validate.c \ - src/omega_core.c src/omega_canonical.c src/sha256.c - -build/test_omega_blackwell_engine: tests/test_omega_blackwell_engine.c tests/fake_m16_native.c tests/fake_m16_native.h \ - $(GPU_ENGINE_SIM_SRCS) src/omega_gpu_engine.h src/omega_blackwell_engine.h - @mkdir -p build - gcc -std=gnu11 -O2 -Wall -Wextra -Werror -D_GNU_SOURCE -pthread -ffunction-sections -fdata-sections \ - -Isrc -Itests $(GPU_ENGINE_PHYS_INC) -o $@ tests/test_omega_blackwell_engine.c tests/fake_m16_native.c \ - $(GPU_ENGINE_SIM_SRCS) -Wl,--gc-sections -lm -test-gpu-engine-sim: build/test_omega_blackwell_engine - ./build/test_omega_blackwell_engine - -# One mutant per backend and vector-wrapper protection; the sim test must fail each one. -test-gpu-engine-backend-mutants: - PHYSICS_DIR="$(PHYSICS_DIR)" sh tools/gpu_engine_backend_mutations.sh - -# Phase B chip test, compile only here. Never run in Phase A. -build/test_omega_gpu_engine_gb10.o: tests/test_omega_gpu_engine_gb10.c src/omega_gpu_engine.h src/omega_blackwell_engine.h src/omega_blackwell_submit.h - @mkdir -p build - gcc -std=gnu11 -O2 -Wall -Wextra -Werror -ffp-contract=off -fno-fast-math -pthread -Isrc $(GPU_ENGINE_PHYS_INC) -c -o $@ tests/test_omega_gpu_engine_gb10.c -test-gpu-engine-gb10-compile: build/test_omega_gpu_engine_gb10.o - -# Measurement (sc#277): driver allocations and frees made by the REAL session + matmul + attention -# APIs while serving a Qwen3-4B-shaped sequence, against the simulated driver. Host only, no device. -# Prints MEASURE lines; asserts only bookkeeping. Links no physics C sources, only its headers. -GPU_SERVE_ALLOC_SRCS = src/omega_gpu_session.c src/omega_gpu_platform.c src/omega_gpu_platform_linux.c src/omega_gpu_fmt.c src/omega_gpu_matmul_api.c src/omega_gpu_attention_api.c src/omega_gpu_serving.c \ - src/omega_gpu_wait.c src/omega_blackwell_codegen.c src/omega_blackwell_encoder.c src/omega_blackwell_qmd.c \ - src/omega_blackwell_matmul.c src/sha256.c -build/gpu_serving_alloc_count_test: tests/gpu_serving_alloc_count_test.c tests/fake_m16_native.c tests/fake_m16_native.h $(GPU_SERVE_ALLOC_SRCS) - @mkdir -p build - gcc -std=gnu11 -O2 -Wall -Wextra -Werror -D_GNU_SOURCE -pthread -ffp-contract=off -Isrc -Itests $(GPU_ENGINE_PHYS_INC) \ - -o $@ tests/gpu_serving_alloc_count_test.c tests/fake_m16_native.c $(GPU_SERVE_ALLOC_SRCS) -lm -.PHONY: test-gpu-serving-alloc -test-gpu-serving-alloc: build/gpu_serving_alloc_count_test - ./build/gpu_serving_alloc_count_test plain - ./build/gpu_serving_alloc_count_test reserve - ./build/gpu_serving_alloc_count_test bounds - ./build/gpu_serving_alloc_count_test bounds-block - ./build/gpu_serving_alloc_count_test rollback - ./build/gpu_serving_alloc_count_test sweep - ./build/gpu_serving_alloc_count_test pin - ./build/gpu_serving_alloc_count_test seal-bare - ./build/gpu_serving_alloc_count_test update - @if ./build/gpu_serving_alloc_count_test sweep-skip >build/gpu_serving_alloc_count_sweep_skip.log 2>&1; then echo "RED RUN PASSED: zero allocations over the row sweep do not need prepare + seal"; exit 1; fi - @if [ "$$(grep -c "^FAIL" build/gpu_serving_alloc_count_sweep_skip.log)" -lt 1 ]; then echo "sweep-skip exited nonzero without a FAIL check (crash, not the red assertion)"; exit 1; fi - @grep -E "^MEASURE sweep" build/gpu_serving_alloc_count_sweep_skip.log | sed "s/^/sweep-skip (control): /" - @if ./build/gpu_serving_alloc_count_test reserve-skip >build/gpu_serving_alloc_count_skip.log 2>&1; then echo "RED RUN PASSED: the zero-allocation assertion does not need the reservation call"; exit 1; fi - @if [ "$$(grep -c "^FAIL" build/gpu_serving_alloc_count_skip.log)" -lt 1 ]; then echo "reserve-skip exited nonzero without a FAIL check (crash, not the red assertion)"; exit 1; fi - @echo "reserve-skip (no reservation call): fails as required ($$(grep -c "^FAIL" build/gpu_serving_alloc_count_skip.log) failing checks)" - -# native v1 P07 cut A (aienos#286 cut A, aien-architecture#190): the freestanding link target. -# Each file below is compiled with no hosted headers available (-ffreestanding, -fno-builtin, -# OMEGA_GPU_NO_POSIX) and the partial link of all of them may leave only the mem*/str* symbols -# undefined. Any other undefined symbol (errno, clock_gettime, printf, malloc, pthread_*, nvrm_*, -# m16_*) fails the target. This proves the first-compute PATH links without Linux headers or -# runtime symbols; it proves nothing about device ownership (NOT_RUN until cut B runs on the Spark). -GPU_FS_DIR = build/gpu_fs -GPU_FS_CFLAGS = -std=gnu11 -O2 -Wall -Wextra -Werror -ffreestanding -fno-builtin -fno-stack-protector \ - -U_FORTIFY_SOURCE -DOMEGA_GPU_NO_POSIX -DOMEGA_NUMERIC_CPU_ONLY -Isrc -# P07 cut C (session part): the persistent session, the matmul and attention APIs, the serving reservation and the -# bounded wait join the link. Their machine comes from an installed OmegaGpuPlatform; the Linux reference -# (src/omega_gpu_platform_linux.c) is deliberately NOT in this list. Still outside it: the elementwise API, the -# numeric launchers, the vector executor, the resident runtime (docs/numeric/OMEGA_GPU_NATIVE_BOUNDARY.md). -GPU_FS_SRCS = src/omega_gpu_engine.c src/omega_gpu_platform.c src/omega_blackwell_encoder.c src/omega_blackwell_codegen.c \ - src/omega_blackwell_matmul.c src/omega_blackwell_qmd.c src/omega_blackwell_realize.c src/sha256.c \ - src/omega_gpu_session.c src/omega_gpu_fmt.c src/omega_gpu_matmul_api.c src/omega_gpu_attention_api.c \ - src/omega_gpu_serving.c src/omega_gpu_wait.c -GPU_FS_HDRS = src/omega_gpu_engine.h src/omega_gpu_platform.h src/omega_blackwell_codegen.h src/omega_blackwell_matmul.h \ - src/omega_gpu_session.h src/omega_gpu_fmt.h src/omega_gpu_matmul_api.h src/omega_gpu_attention_api.h \ - src/omega_gpu_serving.h src/omega_gpu_wait.h -GPU_FS_OBJS = $(patsubst src/%.c,$(GPU_FS_DIR)/%.o,$(GPU_FS_SRCS)) -GPU_FS_LINK = $(GPU_FS_DIR)/omega_gpu_freestanding.o -GPU_FS_ALLOWED = memcpy|memmove|memset|memcmp|strlen|strcmp - -$(GPU_FS_DIR)/%.o: src/%.c $(GPU_FS_HDRS) - @mkdir -p $(GPU_FS_DIR) - gcc $(GPU_FS_CFLAGS) -c -o $@ $< - -$(GPU_FS_LINK): $(GPU_FS_OBJS) - gcc -r -nostdlib -o $@ $(GPU_FS_OBJS) - @bad=$$(nm -u $@ | awk '{print $$NF}' | grep -Ev '^(_GLOBAL_OFFSET_TABLE_|($(GPU_FS_ALLOWED)))$$' || true); \ - if [ -n "$$bad" ]; then echo "freestanding GPU link still needs: $$bad"; rm -f $@; exit 1; fi - @echo "freestanding GPU link ($(words $(GPU_FS_SRCS)) files): only $$(nm -u $@ | awk '{print $$NF}' | grep -Ev '^_GLOBAL_OFFSET_TABLE_$$' | sort -u | tr '\n' ' ')undefined" -gpu-engine-freestanding: $(GPU_FS_LINK) - -build/test_omega_gpu_engine_freestanding: tests/test_omega_gpu_engine.c $(GPU_FS_DIR)/omega_gpu_engine.o src/omega_gpu_engine.h - @mkdir -p build - gcc -std=gnu11 -O2 -Wall -Wextra -Werror -Isrc -DOMEGA_NUMERIC_CPU_ONLY -DOMEGA_GPU_NO_POSIX \ - -o $@ tests/test_omega_gpu_engine.c $(GPU_FS_DIR)/omega_gpu_engine.o -build/test_omega_gpu_platform: tests/test_omega_gpu_platform.c $(GPU_FS_DIR)/omega_gpu_platform.o src/omega_gpu_platform.h - @mkdir -p build - gcc -std=gnu11 -O2 -Wall -Wextra -Werror -Isrc -o $@ tests/test_omega_gpu_platform.c $(GPU_FS_DIR)/omega_gpu_platform.o -test-gpu-engine-freestanding: $(GPU_FS_LINK) build/test_omega_gpu_engine_freestanding build/test_omega_gpu_platform - ./build/test_omega_gpu_engine_freestanding - ./build/test_omega_gpu_platform - -# native v1 P07 cut A.2 (aienos#286): code generation joins the freestanding link above with a caller-owned -# code buffer (omega_blackwell_kernel_encode_into, no allocator). The same test source is built hosted (the -# allocating generators) and freestanding (the *_prog generators into a caller buffer); their instruction -# stream dumps of every fixture must be byte-identical (cmp). The freestanding run then checks the bounded -# buffer contract, the golden-word fixture verifiers on the freestanding object and the bf16 rounding path on -# all 2^32 inputs. The allowlist control links a malloc caller into the freestanding object and requires the -# nm -u check to refuse it (a check that can never fail proves nothing). -.PHONY: test-codegen-freestanding test-gpu-fs-allowlist-control -CODEGEN_FS_TEST_DEPS = tests/test_omega_codegen_freestanding.c src/omega_blackwell_codegen.h src/omega_blackwell_matmul.h -build/test_codegen_hosted: $(CODEGEN_FS_TEST_DEPS) src/omega_blackwell_codegen.c src/omega_blackwell_matmul.c src/sha256.c - @mkdir -p build - gcc -std=gnu11 -O2 -Wall -Wextra -Werror -Isrc -o $@ tests/test_omega_codegen_freestanding.c \ - src/omega_blackwell_codegen.c src/omega_blackwell_matmul.c src/sha256.c -build/test_codegen_freestanding: $(CODEGEN_FS_TEST_DEPS) $(GPU_FS_LINK) - @mkdir -p build - gcc -std=gnu11 -O2 -Wall -Wextra -Werror -Isrc -DOMEGA_GPU_NO_POSIX -o $@ tests/test_omega_codegen_freestanding.c \ - $(GPU_FS_DIR)/omega_blackwell_codegen.o $(GPU_FS_DIR)/omega_blackwell_matmul.o $(GPU_FS_DIR)/sha256.o -test-codegen-freestanding: build/test_codegen_hosted build/test_codegen_freestanding test-gpu-fs-allowlist-control - ./build/test_codegen_hosted --dump > build/codegen_dump_hosted.txt - ./build/test_codegen_freestanding --dump > build/codegen_dump_freestanding.txt - cmp build/codegen_dump_hosted.txt build/codegen_dump_freestanding.txt - @echo "codegen dumps byte-identical: $$(grep -c '^fixture ' build/codegen_dump_hosted.txt) fixtures, $$(grep -v -E '^(fixture|digest) ' build/codegen_dump_hosted.txt | tr -d '\n' | wc -c | awk '{print $$1/2}') code bytes" - ./build/test_codegen_freestanding - -test-gpu-fs-allowlist-control: $(GPU_FS_LINK) - @mkdir -p $(GPU_FS_DIR)/control - printf 'void *malloc(unsigned long);\nvoid *omega_gpu_fs_control(void) { return malloc(16); }\n' > $(GPU_FS_DIR)/control/canary.c - gcc $(GPU_FS_CFLAGS) -c -o $(GPU_FS_DIR)/control/canary.o $(GPU_FS_DIR)/control/canary.c - gcc -r -nostdlib -o $(GPU_FS_DIR)/control/with_canary.o $(GPU_FS_OBJS) $(GPU_FS_DIR)/control/canary.o - @bad=$$(nm -u $(GPU_FS_DIR)/control/with_canary.o | awk '{print $$NF}' | grep -Ev '^(_GLOBAL_OFFSET_TABLE_|($(GPU_FS_ALLOWED)))$$' || true); \ - if [ "$$bad" != "malloc" ]; then echo "allowlist control: expected exactly malloc refused, got '$$bad'"; exit 1; fi; \ - echo "allowlist control: a malloc caller is refused by the nm -u check (as required)" - -# native v1 P07 cut C, session part (aienos#286): the portable session over OmegaGpuPlatform. -# - test_omega_gpu_session_platform links the FREESTANDING partial-link object and drives it through a fake -# platform: install refusals, no default platform, typed GPU-uncached buffers, polling only, cancel before and -# after submit, timeout (latch + park, nothing released), stale handle after close/reopen, reservation refusal. -# - test_omega_gpu_platform_linux drives the Linux REFERENCE platform (reference only) on the simulated driver: -# same driver calls in the same order as before cut C, its refusals, and a marker that never lands. -# - the mutants prove the session test can fail: each one breaks one rule in a copy of the session and the test -# must report FAIL for it. -.PHONY: test-gpu-session-platform test-gpu-session-mutants -build/test_omega_gpu_session_platform: tests/test_omega_gpu_session_platform.c $(GPU_FS_LINK) - @mkdir -p build - gcc -std=gnu11 -O2 -Wall -Wextra -Werror -Isrc -DOMEGA_GPU_NO_POSIX -o $@ tests/test_omega_gpu_session_platform.c $(GPU_FS_LINK) -GPU_LINUX_REF_SRCS = src/omega_gpu_session.c src/omega_gpu_platform.c src/omega_gpu_platform_linux.c src/omega_gpu_fmt.c src/omega_blackwell_qmd.c -# omega #459: the command list over the reference (near-full ring with the #457 L2 invalidate prefix pending) needs the -# cached allocation, the command list and the codegen behind its nop kernel. -GPU_LINUX_REF_CMDLIST_SRCS = src/omega_gpu_mem_cached.c src/omega_gpu_cmdlist.c src/omega_blackwell_codegen.c src/omega_blackwell_encoder.c \ - src/omega_blackwell_matmul.c src/omega_blackwell_realize.c src/sha256.c -build/test_omega_gpu_platform_linux: tests/test_omega_gpu_platform_linux.c tests/fake_m16_native.c tests/fake_m16_native.h $(GPU_LINUX_REF_SRCS) $(GPU_FS_HDRS) src/omega_gpu_platform_linux.h \ - src/omega_gpu_cmdlist.h src/omega_gpu_mem_cached.h $(GPU_LINUX_REF_CMDLIST_SRCS) - @mkdir -p build - gcc -std=gnu11 -O2 -Wall -Wextra -Werror -D_GNU_SOURCE -pthread -Isrc -Itests $(GPU_ENGINE_PHYS_INC) \ - -o $@ tests/test_omega_gpu_platform_linux.c tests/fake_m16_native.c $(GPU_LINUX_REF_SRCS) $(GPU_LINUX_REF_CMDLIST_SRCS) -lm -test-gpu-session-platform: build/test_omega_gpu_session_platform build/test_omega_gpu_platform_linux test-gpu-session-mutants - ./build/test_omega_gpu_session_platform - ./build/test_omega_gpu_platform_linux - -GPU_SESSION_MUT_DIR = build/gpu_session_mut -test-gpu-session-mutants: $(GPU_FS_LINK) - @mkdir -p $(GPU_SESSION_MUT_DIR) - @set -e; killed=0; \ - smut() { name=$$1; script=$$2; \ - sed -e "$$script" src/omega_gpu_session.c > $(GPU_SESSION_MUT_DIR)/$$name.c; \ - if cmp -s src/omega_gpu_session.c $(GPU_SESSION_MUT_DIR)/$$name.c; then echo "mutant $$name: pattern not found"; exit 1; fi; \ - gcc $(GPU_FS_CFLAGS) -c -o $(GPU_SESSION_MUT_DIR)/$$name.o $(GPU_SESSION_MUT_DIR)/$$name.c; \ - objs=$$(echo $(GPU_FS_OBJS) | tr ' ' '\n' | grep -v '/omega_gpu_session.o$$' | tr '\n' ' '); \ - gcc -std=gnu11 -O2 -Wall -Wextra -Werror -Isrc -DOMEGA_GPU_NO_POSIX -o $(GPU_SESSION_MUT_DIR)/$$name \ - tests/test_omega_gpu_session_platform.c $$objs $(GPU_SESSION_MUT_DIR)/$$name.o; \ - if ./$(GPU_SESSION_MUT_DIR)/$$name > $(GPU_SESSION_MUT_DIR)/$$name.log 2>&1; then echo "mutant $$name SURVIVED"; exit 1; fi; \ - echo "mutant $$name killed: $$(grep -c '^FAIL' $(GPU_SESSION_MUT_DIR)/$$name.log) case(s) failed"; killed=$$((killed+1)); }; \ - smut release-when-latched 's|if (LOAD(g_blocked)) {$$|if (0) {|'; \ - smut ignore-cancel 's|if (LOAD(g_cancel)) return WAIT_CANCELLED;|;|g'; \ - smut no-latch-on-timeout 's|latch(w == WAIT_TIMED_OUT|omega_gpu_session_set_error(w == WAIT_TIMED_OUT|'; \ - smut stale-not-counted 's|else { BUMP(g_stats.stale_refusals, 1u); fail_plat("dma_free", rc); }|else {}|'; \ - smut marker-not-polled 's|RIGHTS_MARKER, PAGE|RIGHTS_DATA, PAGE|'; \ - smut close-while-latched 's|if (g_open \&\& !LOAD(g_blocked) \&\& p) {|if (g_open \&\& p) {|; s|if (!LOAD(g_blocked)) { /\* a hook|if (1) { /* a hook|'; \ - echo "test-gpu-session-mutants: $$killed of 6 mutants killed" diff --git a/mk/omega-gpu-wait.mk b/mk/omega-gpu-wait.mk deleted file mode 100644 index 42e5ed25b..000000000 --- a/mk/omega-gpu-wait.mk +++ /dev/null @@ -1,25 +0,0 @@ -# Shared GPU completion wait primitive (src/omega_gpu_wait.c): host unit test with -# fake memory and a fake clock, plus a naive wall-clock mutant that must be -# caught. Picked up by `-include mk/*.mk`. Host only: opens no device, needs no -# physics headers, takes no chip lock. -.PHONY: test-gpu-wait -build/test_omega_gpu_wait: tests/test_omega_gpu_wait.c src/omega_gpu_wait.c src/omega_gpu_wait.h - @mkdir -p build - gcc -std=gnu11 -O2 -Wall -Wextra -Werror -D_GNU_SOURCE -Isrc -o $@ tests/test_omega_gpu_wait.c src/omega_gpu_wait.c -test-gpu-wait: build/test_omega_gpu_wait - ./build/test_omega_gpu_wait - -# Real-code mutants: src/omega_gpu_wait.c carries one-line compile-flag mutants. -# Each flagged build of the real primitive must FAIL the suite; the unflagged -# build must PASS. A mutant that does not compile is an error, not a kill. -GPU_WAIT_MUTANTS := OMEGA_GPU_WAIT_MUTANT_STALL_NO_RESET OMEGA_GPU_WAIT_MUTANT_PROGRESS_EXTENDS_HARD OMEGA_GPU_WAIT_MUTANT_NO_READ_BARRIER -.PHONY: test-gpu-wait-mutants -test-gpu-wait-mutants: build/test_omega_gpu_wait - @./build/test_omega_gpu_wait >/dev/null || { echo "unflagged build FAILED"; exit 1; } - @echo "unflagged: PASS" - @set -e; for m in $(GPU_WAIT_MUTANTS); do \ - gcc -std=gnu11 -O2 -Wall -Wextra -Werror -D_GNU_SOURCE -D$$m -Isrc -o build/test_omega_gpu_wait_$$m tests/test_omega_gpu_wait.c src/omega_gpu_wait.c; \ - if ./build/test_omega_gpu_wait_$$m >build/test_omega_gpu_wait_$$m.log 2>&1; then echo "MUTANT SURVIVED: $$m"; exit 1; fi; \ - echo "$$m: killed ($$(grep -c 'FAIL' build/test_omega_gpu_wait_$$m.log) failing checks)"; \ - done - @echo "OMEGA_GPU_WAIT_MUTANTS PASS" diff --git a/src/omega_blackwell_codegen.c b/src/omega_blackwell_codegen.c deleted file mode 100644 index b09fa1e54..000000000 --- a/src/omega_blackwell_codegen.c +++ /dev/null @@ -1,2539 +0,0 @@ -#include "omega_blackwell_codegen.h" -#include "omega_blackwell_submit.h" -#include "sha256.h" -#include -/* native v1 P07 cut A.2 (aienos#286, docs/numeric/OMEGA_GPU_NATIVE_BOUNDARY.md): with OMEGA_GPU_NO_POSIX this file - * builds -ffreestanding. Code generation then runs only through the allocation-free entry points - * (omega_blackwell_kernel_encode_into: the caller supplies the code buffer and its capacity); the allocating - * generators, their diagnostics and the printing variation self-test exist only in the hosted build. */ -#ifndef OMEGA_GPU_NO_POSIX -#include -#include -#define BW_DIAG(...) fprintf(stderr, __VA_ARGS__) -#else -#define BW_DIAG(...) ((void)0) -#endif - -void omega_bw_ir_init(BlackwellIRProgram *prog) { - if (!prog) return; - memset(prog, 0, sizeof(*prog)); -} - -int omega_bw_ir_alloc_vreg(BlackwellIRProgram *prog) { - if (!prog || prog->regalloc.num_vregs >= BW_MAX_VREGS) return -1; - int v = prog->regalloc.num_vregs++; - prog->regalloc.intervals[v].vreg = v; - prog->regalloc.intervals[v].first_def = -1; - prog->regalloc.intervals[v].last_use = -1; - prog->regalloc.intervals[v].phys_reg = -1; - prog->regalloc.intervals[v].bundle_size = 1; - prog->regalloc.intervals[v].is_pair = false; - prog->regalloc.intervals[v].is_quad = false; - prog->regalloc.intervals[v].active = false; - prog->regalloc.vreg_to_phys[v] = -1; - return v; -} - -int omega_bw_ir_alloc_vreg64(BlackwellIRProgram *prog) { - if (!prog || prog->regalloc.num_vregs >= BW_MAX_VREGS) return -1; - int v = prog->regalloc.num_vregs++; - prog->regalloc.intervals[v].vreg = v; - prog->regalloc.intervals[v].first_def = -1; - prog->regalloc.intervals[v].last_use = -1; - prog->regalloc.intervals[v].phys_reg = -1; - prog->regalloc.intervals[v].bundle_size = 2; - prog->regalloc.intervals[v].is_pair = true; - prog->regalloc.intervals[v].is_quad = false; - prog->regalloc.intervals[v].active = false; - prog->regalloc.vreg_to_phys[v] = -1; - return v; -} - -int omega_bw_ir_alloc_vreg128(BlackwellIRProgram *prog) { - if (!prog || prog->regalloc.num_vregs >= BW_MAX_VREGS) return -1; - int v = prog->regalloc.num_vregs++; - prog->regalloc.intervals[v].vreg = v; - prog->regalloc.intervals[v].first_def = -1; - prog->regalloc.intervals[v].last_use = -1; - prog->regalloc.intervals[v].phys_reg = -1; - prog->regalloc.intervals[v].bundle_size = 4; - prog->regalloc.intervals[v].is_pair = false; - prog->regalloc.intervals[v].is_quad = true; - prog->regalloc.intervals[v].active = false; - prog->regalloc.vreg_to_phys[v] = -1; - return v; -} - -int omega_bw_ir_alloc_uvreg(BlackwellIRProgram *prog) { - if (!prog || prog->regalloc.num_uvregs >= BW_MAX_UVREGS) return -1; - int uv = prog->regalloc.num_uvregs++; - prog->regalloc.uintervals[uv].vreg = uv; - prog->regalloc.uintervals[uv].first_def = -1; - prog->regalloc.uintervals[uv].last_use = -1; - prog->regalloc.uintervals[uv].phys_reg = -1; - prog->regalloc.uintervals[uv].is_pair = false; - prog->regalloc.uintervals[uv].active = false; - prog->regalloc.uvreg_to_phys[uv] = -1; - return uv; -} - -int omega_bw_ir_alloc_uvreg64(BlackwellIRProgram *prog) { - if (!prog || prog->regalloc.num_uvregs >= BW_MAX_UVREGS) return -1; - int uv = prog->regalloc.num_uvregs++; - prog->regalloc.uintervals[uv].vreg = uv; - prog->regalloc.uintervals[uv].first_def = -1; - prog->regalloc.uintervals[uv].last_use = -1; - prog->regalloc.uintervals[uv].phys_reg = -1; - prog->regalloc.uintervals[uv].is_pair = true; - prog->regalloc.uintervals[uv].active = false; - prog->regalloc.uvreg_to_phys[uv] = -1; - return uv; -} - -int omega_bw_ir_append(BlackwellIRProgram *prog, const BlackwellIRInsn *insn) { - if (!prog || !insn || prog->count >= BW_MAX_IR_INSNS) return -1; - int idx = (int)prog->count++; - prog->insns[idx] = *insn; - - /* Update live-interval def/use tracking */ - if (insn->dst_vreg >= 0 && insn->dst_vreg < prog->regalloc.num_vregs) { - if (!insn->is_uniform) { - OmegaLiveInterval *iv = &prog->regalloc.intervals[insn->dst_vreg]; - if (iv->first_def < 0) iv->first_def = idx; - if (idx > iv->last_use) iv->last_use = idx; - } - } - if (insn->src1_vreg >= 0 && insn->src1_vreg < prog->regalloc.num_vregs) { - OmegaLiveInterval *iv = &prog->regalloc.intervals[insn->src1_vreg]; - if (iv->first_def < 0) iv->first_def = idx; - if (idx > iv->last_use) iv->last_use = idx; - } - if (insn->src2_vreg >= 0 && insn->src2_vreg < prog->regalloc.num_vregs) { - OmegaLiveInterval *iv = &prog->regalloc.intervals[insn->src2_vreg]; - if (iv->first_def < 0) iv->first_def = idx; - if (idx > iv->last_use) iv->last_use = idx; - } - if (insn->src3_vreg >= 0 && insn->src3_vreg < prog->regalloc.num_vregs) { - OmegaLiveInterval *iv = &prog->regalloc.intervals[insn->src3_vreg]; - if (iv->first_def < 0) iv->first_def = idx; - if (idx > iv->last_use) iv->last_use = idx; - } - - /* Uniform register def/use tracking */ - if (insn->is_uniform && insn->dst_vreg >= 0 && insn->dst_vreg < prog->regalloc.num_uvregs) { - OmegaLiveInterval *uiv = &prog->regalloc.uintervals[insn->dst_vreg]; - if (uiv->first_def < 0) uiv->first_def = idx; - if (idx > uiv->last_use) uiv->last_use = idx; - } - if (insn->ureg >= 0 && insn->ureg < prog->regalloc.num_uvregs) { - OmegaLiveInterval *uiv = &prog->regalloc.uintervals[insn->ureg]; - if (uiv->first_def < 0) uiv->first_def = idx; - if (idx > uiv->last_use) uiv->last_use = idx; - } - - return idx; -} - -int omega_bw_regalloc_solve(BlackwellIRProgram *prog) { - if (!prog) return -1; - OmegaRegAlloc *ra = &prog->regalloc; - - /* - * Bounded deterministic linear-scan register allocator. - * Computes genuine live intervals across virtual registers and assigns - * available physical GPRs (R2..R127) and UGPRs (UR4..UR31). - * Enforces even-alignment and dual-register reservation for 64-bit pairs. - */ - bool gpr_busy[BW_PHYS_GPR_MAX]; - memset(gpr_busy, 0, sizeof(gpr_busy)); - - int max_phys_used = BW_PHYS_GPR_START; - - /* Sort virtual register indices chronologically by first_def (Poletto & Sarkar Linear Scan) */ - int order[BW_MAX_VREGS]; - int count = 0; - for (int v = 0; v < ra->num_vregs; v++) { - if (ra->intervals[v].first_def >= 0) { - order[count++] = v; - } else { - ra->intervals[v].phys_reg = BW_PHYS_GPR_START; - ra->vreg_to_phys[v] = ra->intervals[v].phys_reg; - } - } - for (int i = 0; i < count - 1; i++) { - for (int j = i + 1; j < count; j++) { - if (ra->intervals[order[i]].first_def > ra->intervals[order[j]].first_def) { - int tmp = order[i]; - order[i] = order[j]; - order[j] = tmp; - } - } - } - - for (int idx = 0; idx < count; idx++) { - int v = order[idx]; - OmegaLiveInterval *iv = &ra->intervals[v]; - - /* Free physical registers whose virtual live intervals have ended */ - for (int p = 0; p < ra->num_vregs; p++) { - if (ra->intervals[p].active && ra->intervals[p].last_use < iv->first_def) { - int freed = ra->intervals[p].phys_reg; - if (freed >= 0 && freed < BW_PHYS_GPR_MAX) { - uint32_t bsize = ra->intervals[p].bundle_size ? ra->intervals[p].bundle_size : (ra->intervals[p].is_pair ? 2 : (ra->intervals[p].is_quad ? 4 : 1)); - for (uint32_t k = 0; k < bsize && (freed + (int)k) < BW_PHYS_GPR_MAX; k++) { - gpr_busy[freed + k] = false; - } - } - ra->intervals[p].active = false; - } - } - - /* Find lowest available physical register */ - int assigned = -1; - uint32_t bsize = iv->bundle_size ? iv->bundle_size : (iv->is_pair ? 2 : (iv->is_quad ? 4 : 1)); - if (bsize == 4) { - /* 128-bit quad requires 4-alignment and 4 contiguous free registers */ - int start = (BW_PHYS_GPR_START + 3) & ~3; - for (int p = start; p <= BW_PHYS_GPR_MAX - 4; p += 4) { - if (!gpr_busy[p] && !gpr_busy[p + 1] && !gpr_busy[p + 2] && !gpr_busy[p + 3]) { - assigned = p; - for (int k = 0; k < 4; k++) gpr_busy[p + k] = true; - break; - } - } - } else if (bsize == 2) { - /* 64-bit pair requires even alignment and contiguous free registers */ - int start = (BW_PHYS_GPR_START + 1) & ~1; - for (int p = start; p <= BW_PHYS_GPR_MAX - 2; p += 2) { - if (!gpr_busy[p] && !gpr_busy[p + 1]) { - assigned = p; - gpr_busy[p] = true; - gpr_busy[p + 1] = true; - break; - } - } - } else { - for (int p = BW_PHYS_GPR_START; p < BW_PHYS_GPR_MAX; p++) { - if (!gpr_busy[p]) { - assigned = p; - gpr_busy[p] = true; - break; - } - } - } - - if (assigned < 0) { - /* Hardware register capacity exceeded: fail closed loudly */ - return -1; - } - - iv->phys_reg = assigned; - iv->active = true; - ra->vreg_to_phys[v] = assigned; - - int top = assigned + (int)bsize - 1; - if (top > max_phys_used) { - max_phys_used = top; - } - } - ra->peak_gpr_usage = (uint32_t)(max_phys_used + 1); - - /* Allocate uniform registers */ - int cur_u = BW_PHYS_UGPR_START; - for (int uv = 0; uv < ra->num_uvregs; uv++) { - OmegaLiveInterval *uiv = &ra->uintervals[uv]; - if (uiv->is_pair) { - if (cur_u % 2 != 0) cur_u++; - if (cur_u + 1 >= BW_PHYS_UGPR_MAX) return -1; - uiv->phys_reg = cur_u; - ra->uvreg_to_phys[uv] = cur_u; - cur_u += 2; - } else { - if (cur_u >= BW_PHYS_UGPR_MAX) return -1; - uiv->phys_reg = cur_u; - ra->uvreg_to_phys[uv] = cur_u++; - } - } - ra->peak_ugpr_usage = (uint32_t)cur_u; - - return 0; -} - -static int encode_single_insn(const BlackwellIRInsn *insn, const OmegaRegAlloc *ra, uint32_t w[4]) { - memset(w, 0, 16); - - int dst = (insn->dst_vreg >= 0) ? (ra->vreg_to_phys[insn->dst_vreg] + insn->dst_subreg) : 0; - int src1 = (insn->src1_vreg >= 0) ? (ra->vreg_to_phys[insn->src1_vreg] + insn->src1_subreg) : 0; - int src2 = (insn->src2_vreg >= 0) ? (ra->vreg_to_phys[insn->src2_vreg] + insn->src2_subreg) : 0; - int src3 = (insn->src3_vreg >= 0) ? (ra->vreg_to_phys[insn->src3_vreg] + insn->src3_subreg) : 0xff; /* Default to RZ (0xff) */ - int ureg = (insn->ureg >= 0) ? ra->uvreg_to_phys[insn->ureg] : 0; - - if (insn->is_uniform && insn->dst_vreg >= 0) { - dst = ra->uvreg_to_phys[insn->dst_vreg]; - } - - switch (insn->op) { - case BW_IR_NOP: - w[0] = 0x00007918; - w[1] = 0x00000000; - w[2] = 0x00000000; - w[3] = insn->control ? insn->control : 0x000fc000; /* control: a timed pause (fragment matmul) */ - break; - - case BW_IR_MOV_RZ: - /* MOV Rd, RZ */ - w[0] = 0x7202 | ((uint32_t)(dst & 0xff) << 16); - w[1] = 0x000000ff; - w[2] = 0x00000f00; - w[3] = insn->control ? insn->control : 0x000fe200; - break; - - case BW_IR_MOV_IMM: - /* MOV Rd, imm32 */ - w[0] = 0x7802 | ((uint32_t)(dst & 0xff) << 16); - w[1] = insn->imm; - w[2] = 0x00000f00; - w[3] = insn->control ? insn->control : 0x000fe200; - break; - - case BW_IR_S2R: - /* S2R Rd, SR */ - w[0] = 0x7919 | ((uint32_t)(dst & 0xff) << 16); - w[1] = 0x0; - w[2] = (insn->imm & 0xff) << 8; - w[3] = insn->control ? insn->control : 0x000e2200; - break; - - case BW_IR_LDC: - /* LDC Rd, c[0x0][imm] */ - w[0] = 0xff007b82 | ((uint32_t)(dst & 0xff) << 16); - w[1] = (insn->imm << 6) & 0x00ffff00; - w[2] = 0x00000800; - w[3] = insn->control ? insn->control : 0x000fe200; - break; - - case BW_IR_LDC64: - /* LDC.64 Rd:Rd+1, c[0x0][imm] */ - w[0] = 0xff007b82 | ((uint32_t)(dst & 0xff) << 16); - w[1] = (insn->imm << 6) & 0x00ffff00; - w[2] = 0x00000a00; - w[3] = insn->control ? insn->control : 0x000e2200; - break; - - case BW_IR_LDCU: - /* LDCU URd, c[0x0][imm] */ - w[0] = 0xff0077ac | ((uint32_t)(dst & 0xff) << 16); - w[1] = (insn->imm << 5) & 0x00ffff00; - w[2] = 0x08000800; - w[3] = insn->control ? insn->control : 0x000e2200; - break; - - case BW_IR_LDCU64: - /* LDCU.64 URd:URd+1, c[0x0][imm] */ - w[0] = 0xff0077ac | ((uint32_t)(dst & 0xff) << 16); - w[1] = (insn->imm << 5) & 0x00ffff00; - w[2] = 0x08000a00; - w[3] = insn->control ? insn->control : 0x000e6e00; - break; - - case BW_IR_IMAD: - if (insn->src2_vreg >= 0) { - /* Register-register: IMAD Rd, Ra, Rb, Rc (0x7224) */ - w[0] = 0x7224 | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); - w[1] = (uint32_t)(src2 & 0xff); - w[2] = 0x078e0200 | (uint32_t)(src3 & 0xff); - w[3] = insn->control ? insn->control : 0x001fca00; - } else if (insn->is_uniform && insn->ureg >= 0) { - /* Uniform register multiplier: IMAD Rd, Ra, URb, Rc (0x7c24) */ - w[0] = 0x7c24 | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); - w[1] = (uint32_t)(ureg & 0xff); - w[2] = 0x0f8e0200 | (uint32_t)(src3 & 0xff); - w[3] = insn->control ? insn->control : 0x001fca00; - } else { - /* Immediate multiplier: IMAD Rd, Ra, imm, Rc (0x7824) */ - w[0] = 0x7824 | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); - w[1] = insn->imm; - w[2] = 0x078e0200 | (uint32_t)(src3 & 0xff); - w[3] = insn->control ? insn->control : 0x001fca00; - } - break; - - case BW_IR_IMAD_WIDE: - /* IMAD.WIDE.U32 Rd:Rd+1, Ra, imm, Rc:Rc+1 */ - w[0] = 0x7825 | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); - w[1] = insn->imm; - w[2] = 0x078e0000 | (uint32_t)(src3 & 0xff); - w[3] = insn->control ? insn->control : 0x001fcc00; - break; - - case BW_IR_ISETP_GE: - if (insn->is_uniform && insn->ureg >= 0) { - w[0] = 0x7c0c | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); - w[1] = (uint32_t)(ureg & 0xff); - w[2] = 0x0bf06070; - w[3] = 0x002fda00; - } else { - w[0] = 0x780c | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); - w[1] = insn->imm; - w[2] = 0x03f06270; - w[3] = 0x001fda00; - } - break; - - case BW_IR_LDG_E: - /* LDG.E Rd, desc[URd][Ra.64] */ - w[0] = 0x7981 | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); - w[1] = (uint32_t)(ureg & 0xff); - w[2] = 0x0c1e1900; - w[3] = insn->control ? insn->control : 0x000f2200; - break; - - case BW_IR_STG_E: { - /* STG.E desc[URd][Ra.64+imm], Rb. imm = signed 24-bit byte offset at bits 40..63 (w[1] bits - * 8..31), as for LDG; words from nvcc 13.0.88 -arch=sm_121 (omega_blackwell_verify_codegen_fixtures_fragmm, - * 2026-10-12: STG.E desc[UR4][R4.64+0x4], R9 = 0x0000040904007986 0x000fe8000c101904). */ - int32_t off = (int32_t)insn->imm; - if (off < -(1 << 23) || off >= (1 << 23)) return -1; - w[0] = 0x7986 | ((uint32_t)(src1 & 0xff) << 24); - w[1] = (uint32_t)(src2 & 0xff) | (((uint32_t)off & 0xffffffU) << 8); - w[2] = 0x0c101900 | (uint32_t)(ureg & 0xff); - w[3] = insn->control ? insn->control : 0x000fe200; - break; - } - - case BW_IR_STG_EF: - /* STG.E.MMIO.GPU. Uncached store, meant to be visible without a fence. */ - w[0] = 0x7986 | ((uint32_t)(src1 & 0xff) << 24); - w[1] = (uint32_t)(src2 & 0xff); - w[2] = 0x0c111900 | (uint32_t)(ureg & 0xff); - w[3] = insn->control ? insn->control : 0x000fe200; - break; - - case BW_IR_IADD3: - /* IADD3 Rd, PT, PT, Ra, Rb, Rc */ - w[0] = 0x7210 | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); - w[1] = (uint32_t)(src2 & 0xff); - w[2] = 0x07ffe0ff; - w[3] = 0x010fca00; - break; - - case BW_IR_HMMA_F16: - case BW_IR_HMMA_BF16: { - /* HMMA.16816.F32[.BF16] Rd, Ra, Rb, Rc */ - w[0] = 0x723c | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); - w[1] = (uint32_t)(src2 & 0xff); - uint32_t prec_bit = (insn->op == BW_IR_HMMA_BF16) ? 0x00040000 : 0x00000000; - w[2] = (uint32_t)(src3 & 0xff) | (0x18 << 8) | prec_bit; - w[3] = insn->control ? insn->control : 0x000fe200; - break; - } - - case BW_IR_SHF_R: - /* SHF.R.U32.HI Rd, RZ, imm, Ra */ - w[0] = 0x7819U | ((uint32_t)(dst & 0xff) << 16) | (0xffU << 24); - w[1] = (uint32_t)insn->imm; - w[2] = 0x00011600 | (uint32_t)(src1 & 0xff); - w[3] = insn->control ? insn->control : 0x001fca00; - break; - - case BW_IR_LOP3_AND: - /* LOP3.LUT Rd, Ra, imm, RZ, 0xc0, !PT */ - w[0] = 0x7812U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); - w[1] = (uint32_t)insn->imm; - w[2] = 0x078ec0ff; - w[3] = insn->control ? insn->control : 0x001fca00; - break; - - case BW_IR_LDG_E_64: - case BW_IR_LDG_E_128: { - /* LDG.E.64 / LDG.E.128 Rd, desc[URd][Ra.64+imm]. imm = signed 24-bit byte offset at - * bits 40..63 (w[1] bits 8..31); size field in w[2] bits 9..11 (32 = 4, 64 = 5, 128 = 6). - * Words from nvcc 13.0.88 -arch=sm_121 (omega_blackwell_verify_codegen_fixtures_fragmm). */ - int32_t off = (int32_t)insn->imm; - if (off < -(1 << 23) || off >= (1 << 23)) return -1; - w[0] = 0x7981U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); - w[1] = (uint32_t)(ureg & 0xff) | (((uint32_t)off & 0xffffffU) << 8); - w[2] = insn->op == BW_IR_LDG_E_64 ? 0x0c1e1b00U : 0x0c1e1d00U; - w[3] = insn->control ? insn->control : 0x000f2200; - break; - } - - case BW_IR_LDG_E_U16: - /* LDG.E.U16 Rd, desc[URd][Ra.64] */ - w[0] = 0x7981U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); - w[1] = (uint32_t)(ureg & 0xff); - w[2] = 0x0c1e1500; - w[3] = insn->control ? insn->control : 0x000f2200; - break; - - case BW_IR_LDG_STRONG_SYS: - /* LDG.E.STRONG.SYS. Same shape as LDG.E; w[2] carries the - * system-scope bits (0x0c1e1900 -> 0x0c1f5900). */ - w[0] = 0x7981 | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); - w[1] = (uint32_t)(ureg & 0xff); - w[2] = 0x0c1f5900; - w[3] = insn->control ? insn->control : 0x000f2200; - break; - - case BW_IR_LDG_MMIO: - /* LDG.E.EF. Drop the cached line, then load. */ - w[0] = 0x7981 | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); - w[1] = (uint32_t)(ureg & 0xff); - w[2] = 0x0c0e1900; - w[3] = insn->control ? insn->control : 0x000f2200; - break; - - case BW_IR_STG_STRONG_SYS: - w[0] = 0x7986 | ((uint32_t)(src1 & 0xff) << 24); - if (insn->predicate_p0) { - uint32_t pred = insn->predicate_not ? 0x8u : 0x0u; - w[0] = (w[0] & ~0xF000u) | (pred << 12); - } - w[1] = (uint32_t)(src2 & 0xff); - w[2] = 0x0c115900 | (uint32_t)(ureg & 0xff); - w[3] = insn->control ? insn->control : 0x000fe200; - break; - - case BW_IR_MEMBAR_ALL_SYS: - w[0] = 0x00007992; - w[1] = 0x00000000; - w[2] = 0x0000b000; - w[3] = insn->control ? insn->control : 0x000fec00; - break; - - case BW_IR_MEMBAR_SC_SYS: - w[0] = 0x00007992; - w[1] = 0x00000000; - w[2] = 0x00003000; - w[3] = insn->control ? insn->control : 0x000fec00; - break; - - case BW_IR_CCTL_IVALL: - w[0] = 0xff00798f; - w[1] = 0x00000000; - w[2] = 0x02000000; - w[3] = insn->control ? insn->control : 0x000fe800; - break; - - case BW_IR_ATOMG_ADD_STRONG_SYS: - w[0] = 0x79a8 | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); - w[1] = (uint32_t)(src2 & 0xff) | 0x80000000u; - w[2] = 0x081f5100 | (uint32_t)(ureg & 0xff); - w[3] = insn->control ? insn->control : 0x00321e00; - break; - - case BW_IR_ATOMG_EXCH_STRONG_SYS: - w[0] = 0x79a8 | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); - w[1] = (uint32_t)(src2 & 0xff) | 0x80000000u; - w[2] = 0x0c1f5100 | (uint32_t)(ureg & 0xff); - w[3] = insn->control ? insn->control : 0x00321e00; - break; - - case BW_IR_ISETP_GE_U32: - /* ISETP.GE.U32.AND P0, PT, Ra, Rb, PT. Checked with nvdisasm -b SM121. */ - w[0] = 0x720c | ((uint32_t)(src1 & 0xff) << 24); - w[1] = (uint32_t)(src2 & 0xff); - w[2] = 0x03f06070; - w[3] = insn->control ? insn->control : 0x001fda00; - break; - - case BW_IR_LOP3_XOR: - /* LOP3.LUT Rd, Ra, Rb, RZ, 0x3c, !PT. Checked with nvdisasm -b SM121. */ - w[0] = 0x7212 | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); - w[1] = (uint32_t)(src2 & 0xff); - w[2] = 0x078e3cff; - w[3] = insn->control ? insn->control : 0x001fca00; - break; - - case BW_IR_FADD: - /* FADD Rd, Ra, Rb */ - w[0] = 0x7221U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); - w[1] = (uint32_t)(src2 & 0xff); - w[2] = 0x00000000; - w[3] = insn->control ? insn->control : 0x004fc400; - break; - - case BW_IR_FSUB: - /* FSUB Rd, Ra, Rb (FADD Rd, Ra, -Rb) */ - w[0] = 0x7221U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); - w[1] = (uint32_t)(src2 & 0xff) | 0x80000000U; - w[2] = 0x00000000; - w[3] = insn->control ? insn->control : 0x004fc400; - break; - - case BW_IR_FMUL: - /* FMUL Rd, Ra, Rb */ - w[0] = 0x7220U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); - w[1] = (uint32_t)(src2 & 0xff); - w[2] = 0x00400000; - w[3] = insn->control ? insn->control : 0x004fc400; - break; - - case BW_IR_FFMA: - /* FFMA Rd, Ra, Rb, Rc */ - w[0] = 0x7223U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); - w[1] = (uint32_t)(src2 & 0xff); - w[2] = (uint32_t)(src3 & 0xff); - w[3] = insn->control ? insn->control : 0x004fc400; - break; - - case BW_IR_FSETP: - /* FSETP.cond.AND P0, PT, Ra, Rb, PT */ - w[0] = 0x720bU | ((uint32_t)(dst & 0x7) << 16) | ((uint32_t)(src1 & 0xff) << 24); - w[1] = (uint32_t)(src2 & 0xff); - w[2] = insn->imm ? insn->imm : 0x03f06000; /* Default GE condition */ - w[3] = insn->control ? insn->control : 0x004fc400; - break; - - case BW_IR_FSEL: - /* FSEL Rd, Ra, Rb, P0 */ - w[0] = 0x7208U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); - w[1] = (uint32_t)(src2 & 0xff); - w[2] = (uint32_t)(src3 & 0x7); - w[3] = insn->control ? insn->control : 0x000fca00; - break; - - case BW_IR_FMNMX_MIN: - /* FMNMX Rd, Ra, Rb, PT (minimum) */ - w[0] = 0x7209U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); - w[1] = (uint32_t)(src2 & 0xff); - w[2] = 0x03800000; - w[3] = insn->control ? insn->control : 0x004fc400; - break; - - case BW_IR_FMNMX_MAX: - /* FMNMX Rd, Ra, Rb, !PT (maximum) */ - w[0] = 0x7209U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); - w[1] = (uint32_t)(src2 & 0xff); - w[2] = 0x07800000; - w[3] = insn->control ? insn->control : 0x004fc400; - break; - - case BW_IR_I2FP: - /* I2FP.F32.S32 Rd, Ra */ - w[0] = 0x7245U | ((uint32_t)(dst & 0xff) << 16); - w[1] = (uint32_t)(src1 & 0xff); - w[2] = 0x00201400; - w[3] = insn->control ? insn->control : 0x004fe200; - break; - - case BW_IR_F2I: - /* F2I.TRUNC.NTZ Rd, Ra */ - w[0] = 0x7305U | ((uint32_t)(dst & 0xff) << 16); - w[1] = (uint32_t)(src1 & 0xff); - w[2] = 0x0020f100; - w[3] = insn->control ? insn->control : 0x004e2200; - break; - - case BW_IR_MUFU_RCP: - /* MUFU.RCP Rd, Ra */ - w[0] = 0x7308U | ((uint32_t)(dst & 0xff) << 16); - w[1] = (uint32_t)(src1 & 0xff); - w[2] = 0x00001000; - w[3] = insn->control ? insn->control : 0x000e2400; - break; - - case BW_IR_MUFU_RSQ: - /* MUFU.RSQ Rd, Ra */ - w[0] = 0x7308U | ((uint32_t)(dst & 0xff) << 16); - w[1] = (uint32_t)(src1 & 0xff); - w[2] = 0x00001400; - w[3] = insn->control ? insn->control : 0x000e2400; - break; - - case BW_IR_SHFL_DOWN: - /* SHFL.DOWN PT, Rd, Ra, offset, 0x1f */ - w[0] = 0x7f89U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); - w[1] = 0x08001f00U | (((uint32_t)insn->imm & 0x1f) << 21); - w[2] = 0x000e0000; - w[3] = insn->control ? insn->control : 0x000e2400; - break; - - case BW_IR_LDS: - /* LDS Rd, [Ra] */ - w[0] = 0x7984U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); - w[1] = 0x00000000; - w[2] = (uint32_t)(insn->imm & 0xffff); - w[3] = insn->control ? insn->control : 0x000fe200; - break; - - case BW_IR_STS: - /* STS [Ra], Rb */ - w[0] = 0x7388U | ((uint32_t)(src1 & 0xff) << 24); - w[1] = (uint32_t)(src2 & 0xff); - w[2] = (uint32_t)(insn->imm & 0xffff); - w[3] = insn->control ? insn->control : 0x000fe200; - break; - - case BW_IR_EXIT: - w[0] = insn->predicate_p0 ? 0x0000094d : 0x0000794d; - w[1] = 0x00000000; - w[2] = 0x03800000; - w[3] = 0x000fea00; - break; - - case BW_IR_BRA: { - /* sm_121 BRA. Distance is (target - this) - 1, split across the - * low six bits at instruction bits 18..23 and the rest at bit 34. - * Predicate bits 12..15: 7 = always, 0 = @P0, 8 = @!P0. - * A zero delta with no predicate keeps the self-branch word the - * existing kernels already emit after EXIT. */ - int32_t delta = (int32_t)insn->imm; - int32_t rel = delta - 1; - uint32_t pred_field = 0x7u; - if (insn->predicate_p0) - pred_field = insn->predicate_not ? 0x8u : 0x0u; - w[0] = 0x947u | (pred_field << 12) | (((uint32_t)rel & 0x3fu) << 18); - w[1] = (uint32_t)(rel >> 6) << 2; - w[2] = (rel < 0) ? 0x0383ffffu : 0x03800000u; - if (insn->control) - w[3] = insn->control; - else if (delta == 0 && !insn->predicate_p0) - w[3] = 0x000fc000u; - else - w[3] = 0x000fea00u; - break; - } - - case BW_IR_BSSY: { - /* BSSY.RECONVERGENT Bn, target (omega #308). nvcc 13.0.88 -arch=sm_121: - * [0070] BSSY.RECONVERGENT B0, 0x140 ; 0x000000c000007945 0x000fe20003800200 - * opcode 0x945 (NAK sm70_encode.rs:4165), barrier id at bits 16..19 (set_bar_dst - * 16..20), guard predicate at bits 12..15 like BRA, relative byte offset - * target - (this + 16) at bit 32 (NAK: field 34.. in 4-byte units), the cond - * predicate PT at bits 87..89 (0x0380 << 16 in w[2]) and bit 73 (0x200 in w[2]), - * which the disassembler prints as .RECONVERGENT; nvcc sets it on every BSSY/BSYNC - * for sm_121 and Mesa NAK does not. We follow nvcc. The target is the instruction - * after the matching BSYNC, so the delta is at least 2; smaller is refused. */ - int32_t delta = (int32_t)insn->imm; - if (delta < 2 || insn->bar_reg >= BW_RECONV_MAX_BAR) return -1; - uint32_t pred_field = 0x7u; - if (insn->predicate_p0) - pred_field = insn->predicate_not ? 0x8u : 0x0u; - w[0] = 0x945u | (pred_field << 12) | ((uint32_t)insn->bar_reg << 16); - w[1] = (uint32_t)(delta - 1) * 16u; - w[2] = 0x03800200u; - w[3] = insn->control ? insn->control : 0x000fe200u; - break; - } - - case BW_IR_BSYNC: { - /* BSYNC.RECONVERGENT Bn: [0130] BSYNC.RECONVERGENT B0 ; 0x0000000000007941 0x000fea0003800200 - * opcode 0x941 (NAK sm70_encode.rs:4179), barrier id bits 16..19, cond PT, bit 73. */ - if (insn->bar_reg >= BW_RECONV_MAX_BAR) return -1; - uint32_t pred_field = 0x7u; - if (insn->predicate_p0) - pred_field = insn->predicate_not ? 0x8u : 0x0u; - w[0] = 0x941u | (pred_field << 12) | ((uint32_t)insn->bar_reg << 16); - w[1] = 0; - w[2] = 0x03800200u; - w[3] = insn->control ? insn->control : 0x000fea00u; - break; - } - - case BW_IR_MUFU_EX2: - /* MUFU.EX2 Rd, Ra. nvdisasm 13.0 -b SM121: 0x7308 with sub-op 0x0800 decodes - * "MUFU.EX2 R2, R4" (RCP is 0x1000, RSQ 0x1400 above). */ - w[0] = 0x7308U | ((uint32_t)(dst & 0xff) << 16); - w[1] = (uint32_t)(src1 & 0xff); - w[2] = 0x00000800; - w[3] = insn->control ? insn->control : 0x000e2400; - break; - - case BW_IR_BAR_SYNC: - /* BAR.SYNC.DEFER_BLOCKING 0x0: the exact words src/omega_numeric.c:885 runs on - * the chip (nvdisasm: "BAR.SYNC.DEFER_BLOCKING 0x0"). No registers. */ - w[0] = 0x00007b1dU; - w[1] = 0x00000000; - w[2] = 0x00010000; - w[3] = insn->control ? insn->control : 0x000fec00; - break; - - case BW_IR_LDS128: { - /* LDS.128 Rd..Rd+3, [Ra+imm]. nvcc 13.0.88 -arch=sm_121 words (fixtures below): - * LDS.128 R4, [R20] ; 0x0000000014047984 0x000fe80000000c00 - * LDS.128 R8, [R20+0x40] ; 0x0000400014087984 0x000e280000000c00 - * imm = byte offset at w[1] bits 8..31; size field 6 (128) at w[2] bits 9..11. */ - if (insn->imm >= (1u << 23)) return -1; - w[0] = 0x7984U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); - w[1] = (insn->imm & 0xffffffU) << 8; - w[2] = 0x00000c00U; - w[3] = insn->control ? insn->control : 0x000fe800; - break; - } - - case BW_IR_LDS32: - /* LDS Rd, [Ra+URZ] (32-bit), the src/omega_numeric.c:886 form. */ - w[0] = 0x7984U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); - w[1] = 0x000000ff; - w[2] = 0x08000800; - w[3] = insn->control ? insn->control : 0x000e2800; - break; - - case BW_IR_STS32: - /* STS [Ra+URZ], Rb (32-bit), the src/omega_numeric.c:884 form. */ - w[0] = 0x7988U | ((uint32_t)(src1 & 0xff) << 24); - w[1] = (uint32_t)(src2 & 0xff); - w[2] = 0x080008ffU; - w[3] = insn->control ? insn->control : 0x000fe200; - break; - - /* Prime race cut (2026-10-05). An absent operand is RZ for these three ops. */ - case BW_IR_LOP3_LUT: { - /* LOP3.LUT Rd, Ra, Rb, Rc, imm8, !PT. LOP3_XOR above is this form with Rc = RZ, LUT 0x3c. */ - uint32_t a = (insn->src1_vreg >= 0) ? (uint32_t)(src1 & 0xff) : 0xffU; - uint32_t b = (insn->src2_vreg >= 0) ? (uint32_t)(src2 & 0xff) : 0xffU; - w[0] = 0x7212U | ((uint32_t)(dst & 0xff) << 16) | (a << 24); - w[1] = b; - w[2] = 0x078e0000U | ((insn->imm & 0xffU) << 8) | (uint32_t)(src3 & 0xff); - w[3] = insn->control ? insn->control : 0x001fca00; - break; - } - - case BW_IR_SHF_L_U32: { - /* SHF.L.U32 Rd, Ra, Rb, Rc: register-amount form (0x7219) of SHF_R's 0x7819, with the - * .R (w[2] bit 12) and .HI (bit 16) flags clear. Rc is the funnel high word (RZ). */ - uint32_t a = (insn->src1_vreg >= 0) ? (uint32_t)(src1 & 0xff) : 0xffU; - uint32_t b = (insn->src2_vreg >= 0) ? (uint32_t)(src2 & 0xff) : 0xffU; - w[0] = 0x7219U | ((uint32_t)(dst & 0xff) << 16) | (a << 24); - w[1] = b; - w[2] = 0x00000600U | (uint32_t)(src3 & 0xff); - w[3] = insn->control ? insn->control : 0x001fca00; - break; - } - - case BW_IR_IMAD_HI_U32: { - /* IMAD.HI.U32 Rd, Ra, Rb, Rc: opcode 0x227 with the IMAD.WIDE.U32 modifier word. */ - uint32_t a = (insn->src1_vreg >= 0) ? (uint32_t)(src1 & 0xff) : 0xffU; - uint32_t b = (insn->src2_vreg >= 0) ? (uint32_t)(src2 & 0xff) : 0xffU; - w[0] = 0x7227U | ((uint32_t)(dst & 0xff) << 16) | (a << 24); - w[1] = b; - w[2] = 0x078e0000U | (uint32_t)(src3 & 0xff); - w[3] = insn->control ? insn->control : 0x001fca00; - break; - } - - default: - return -1; - } - return 0; -} - -/* Prime race cut (2026-10-05): golden words for LOP3_LUT, SHF_L_U32 and IMAD_HI_U32. - * Each expected (w0, w1, w2) is the low 96 bits of an instruction nvcc 13.0.88 emitted - * for sm_121 (nvcc -arch=sm_121 -cubin, cuobjdump -sass; source and dump in - * bench/prime_race/tests/encoding_oracle.md). Control words (w3) are scheduler output and - * not compared. nvcc is an offline oracle only; nothing here links CUDA. 0 = all good. */ -int omega_blackwell_verify_codegen_fixtures_intops(void) { - OmegaRegAlloc ra; - memset(&ra, 0, sizeof(ra)); - static const int phys[] = { 0, 2, 6, 7, 9, 11, 13 }; - for (int i = 0; i < (int)(sizeof phys / sizeof phys[0]); i++) ra.vreg_to_phys[i] = phys[i]; - enum { R0 = 0, R2, R6, R7, R9, R11, R13, RZ = -1 }; - static const struct { - BlackwellIROpcode op; int d, a, b, c; uint32_t imm; uint32_t w0, w1, w2; const char *text; - } fx[] = { - { BW_IR_LOP3_LUT, R9, R7, R0, RZ, 0xfc, 0x07097212U, 0x00000000U, 0x078efcffU, "LOP3.LUT R9, R7, R0, RZ, 0xfc, !PT" }, - { BW_IR_LOP3_LUT, R11, R6, R7, R0, 0x78, 0x060b7212U, 0x00000007U, 0x078e7800U, "LOP3.LUT R11, R6, R7, R0, 0x78, !PT" }, - { BW_IR_LOP3_LUT, R13, R6, R7, R0, 0x10, 0x060d7212U, 0x00000007U, 0x078e1000U, "LOP3.LUT R13, R6, R7, R0, 0x10, !PT" }, - { BW_IR_LOP3_LUT, R7, RZ, R9, RZ, 0x33, 0xff077212U, 0x00000009U, 0x078e33ffU, "LOP3.LUT R7, RZ, R9, RZ, 0x33, !PT" }, - { BW_IR_SHF_L_U32, R7, R9, R2, RZ, 0, 0x09077219U, 0x00000002U, 0x000006ffU, "SHF.L.U32 R7, R9, R2, RZ" }, - { BW_IR_SHF_L_U32, R7, R0, R7, RZ, 0, 0x00077219U, 0x00000007U, 0x000006ffU, "SHF.L.U32 R7, R0, R7, RZ" }, - { BW_IR_IMAD_HI_U32, R9, R9, R0, R6, 0, 0x09097227U, 0x00000000U, 0x078e0006U, "IMAD.HI.U32 R9, R9, R0, R6" }, - { BW_IR_IMAD_HI_U32, R7, R0, R7, RZ, 0, 0x00077227U, 0x00000007U, 0x078e00ffU, "IMAD.HI.U32 R7, R0, R7, RZ" }, - }; - for (int i = 0; i < (int)(sizeof fx / sizeof fx[0]); i++) { - BlackwellIRInsn in; - memset(&in, 0, sizeof in); - in.op = fx[i].op; in.dst_vreg = fx[i].d; in.src1_vreg = fx[i].a; in.src2_vreg = fx[i].b; - in.src3_vreg = fx[i].c; in.ureg = -1; in.imm = fx[i].imm; - uint32_t w[4]; - if (encode_single_insn(&in, &ra, w) != 0) return -(2 * i + 1); - if (w[0] != fx[i].w0 || w[1] != fx[i].w1 || w[2] != fx[i].w2) return -(2 * i + 2); - } - return 0; -} - -int omega_blackwell_encode_one(const BlackwellIRInsn *insn, uint32_t w[4]) { - if (!insn || !w) return -1; - OmegaRegAlloc ra; - memset(&ra, 0, sizeof ra); - for (int i = 0; i < BW_MAX_VREGS; i++) ra.vreg_to_phys[i] = i; - for (int i = 0; i < BW_MAX_UVREGS; i++) ra.uvreg_to_phys[i] = i; - return encode_single_insn(insn, &ra, w); -} - -/* omega #328: golden words for the fragment-major matmul. Oracle: nvcc 13.0.88 -arch=sm_121 - * -cubin -O3, cuobjdump -sass, on a kernel with uint2 / uint4 loads at immediate offsets and a - * dependent mma.m16n8k16 bf16 chain (source and dump sealed in - * ~/workspace/investigations/2026-10-07-qwen3-decode-matmul/oracle/frag_plain.{cu,sass}): - * LDG.E.64 R2, desc[UR8][R30.64] ; 0x000000081e027981 0x000ea8000c1e1b00 - * LDG.E.64 R28, desc[UR8][R30.64+0x100] ; 0x000100081e1c7981 0x000ee8000c1e1b00 - * LDG.E.128 R8, desc[UR8][R32.64] ; 0x0000000820087981 0x000ea8000c1e1d00 - * LDG.E.128 R20, desc[UR8][R32.64+0x1000] ; 0x0010000820147981 0x000e9e000c1e1d00 - * HMMA.16816.F32.BF16 R8, R12, R6, R8 ; 0x000000060c08723c 0x010fe20000041808 - * NOP ; 0x0000000000007918 0x000fdc0000000000 - * The dependent HMMA chain nvcc emits is HMMA (stall 15) then NOP (stall 14): 29 cycles from - * one HMMA to the next that reads its accumulator. w3 is compared for the NOP only (it is the - * whole point of that instruction). 0 = all good. */ -int omega_blackwell_verify_codegen_fixtures_fragmm(void) { - static const struct { - BlackwellIROpcode op; int d, a, b, c, u; uint32_t imm, control; uint32_t w0, w1, w2, w3; const char *text; - } fx[] = { - { BW_IR_LDG_E_64, 2, 30, -1, -1, 8, 0, 0, 0x1e027981U, 0x00000008U, 0x0c1e1b00U, 0, "LDG.E.64 R2, desc[UR8][R30.64]" }, - { BW_IR_LDG_E_64, 28, 30, -1, -1, 8, 0x100, 0, 0x1e1c7981U, 0x00010008U, 0x0c1e1b00U, 0, "LDG.E.64 R28, desc[UR8][R30.64+0x100]" }, - { BW_IR_LDG_E_128, 8, 32, -1, -1, 8, 0, 0, 0x20087981U, 0x00000008U, 0x0c1e1d00U, 0, "LDG.E.128 R8, desc[UR8][R32.64]" }, - { BW_IR_LDG_E_128, 20, 32, -1, -1, 8, 0x1000, 0, 0x20147981U, 0x00100008U, 0x0c1e1d00U, 0, "LDG.E.128 R20, desc[UR8][R32.64+0x1000]" }, - { BW_IR_HMMA_BF16, 8, 12, 6, 8, -1, 0, 0, 0x0c08723cU, 0x00000006U, 0x00041808U, 0, "HMMA.16816.F32.BF16 R8, R12, R6, R8" }, - { BW_IR_NOP, -1, -1, -1, -1, -1, 0, 0x000fdc00U, 0x00007918U, 0, 0, 0x000fdc00U, "NOP (stall 14)" }, - /* LDS.128 with an immediate offset (attention batched loads, 2026-10-12): nvcc 13.0.88 -arch=sm_121 on - * float4 shared reads at register + constant offsets (fixture source in the omega speed/attention-batched-loads PR) */ - { BW_IR_LDS128, 4, 20, -1, -1, -1, 0, 0, 0x14047984U, 0x00000000U, 0x00000c00U, 0, "LDS.128 R4, [R20]" }, - { BW_IR_LDS128, 8, 20, -1, -1, -1, 0x40, 0, 0x14087984U, 0x00004000U, 0x00000c00U, 0, "LDS.128 R8, [R20+0x40]" }, - { BW_IR_LDS128, 12, 20, -1, -1, -1, 0x100, 0, 0x140c7984U, 0x00010000U, 0x00000c00U, 0, "LDS.128 R12, [R20+0x100]" }, - /* STG.E with an immediate offset (blocked prefill matmul, 2026-10-12): nvcc 13.0.88 -arch=sm_121 on float stores - * at register + constant offsets (~/.claude/jobs/ade3c837/tmp/stg-oracle/stg.cu); the second word is the - * offset-free STG word with UR4 */ - { BW_IR_STG_E, -1, 4, 8, -1, 4, 0, 0, 0x04007986U, 0x00000008U, 0x0c101904U, 0, "STG.E desc[UR4][R4.64], R8" }, - { BW_IR_STG_E, -1, 4, 9, -1, 4, 0x4, 0, 0x04007986U, 0x00000409U, 0x0c101904U, 0, "STG.E desc[UR4][R4.64+0x4], R9" }, - { BW_IR_STG_E, -1, 4, 10, -1, 4, 0x4000, 0, 0x04007986U, 0x0040000aU, 0x0c101904U, 0, "STG.E desc[UR4][R4.64+0x4000], R10" }, - { BW_IR_STG_E, -1, 6, 11, -1, 4, 0xfff00000u, 0, 0x06007986U, 0xf000000bU, 0x0c101904U, 0, "STG.E desc[UR4][R6.64+-0x100000], R11" }, - }; - for (int i = 0; i < (int)(sizeof fx / sizeof fx[0]); i++) { - BlackwellIRInsn in; - memset(&in, 0, sizeof in); - in.op = fx[i].op; in.dst_vreg = fx[i].d; in.src1_vreg = fx[i].a; in.src2_vreg = fx[i].b; - in.src3_vreg = fx[i].c; in.ureg = fx[i].u; in.imm = fx[i].imm; in.control = fx[i].control; - uint32_t w[4]; - if (omega_blackwell_encode_one(&in, w) != 0) return -(2 * i + 1); - if (w[0] != fx[i].w0 || w[1] != fx[i].w1 || w[2] != fx[i].w2 || (fx[i].w3 && w[3] != fx[i].w3)) return -(2 * i + 2); - } - /* offsets outside the signed 24-bit field are refused, not wrapped */ - BlackwellIRInsn big; - memset(&big, 0, sizeof big); - big.op = BW_IR_LDG_E_64; big.dst_vreg = 2; big.src1_vreg = 30; big.src2_vreg = -1; big.src3_vreg = -1; big.ureg = 8; - big.imm = 1u << 23; - uint32_t w[4]; - if (omega_blackwell_encode_one(&big, w) == 0) return -100; - big.imm = (uint32_t)-(1 << 23); - if (omega_blackwell_encode_one(&big, w) != 0) return -101; - return 0; -} - -/* omega #308: BSSY / BSYNC golden words. Oracle: nvcc 13.0.88 -arch=sm_121 -cubin -O3 on the - * kernels in docs/gpu-reconvergence-308.md, cuobjdump -sass (low 64-bit word, then high; the - * top 23 bits of the high word are the control word and are not compared here): - * k_loop [0070] BSSY.RECONVERGENT B0, 0x140 ; 0x000000c000007945 0x000fe20003800200 (delta 13) - * k_nested [0070] BSSY.RECONVERGENT B0, 0x300 ; 0x0000028000007945 0x000fe20003800200 (delta 41) - * k_break [0070] BSSY.RECONVERGENT B0, 0x110 ; 0x0000009000007945 0x000fe20003800200 (delta 10) - * k_loop [0130] BSYNC.RECONVERGENT B0 ; 0x0000000000007941 0x000fea0003800200 - * Barrier ids other than B0 and the predicated forms are decoded by tools/reconv_nvdisasm_check.sh - * (nvdisasm -b SM121 text), not by this table. */ -int omega_blackwell_verify_codegen_fixtures_reconv(void) { - static const struct { BlackwellIROpcode op; int delta; int bar; uint32_t w0, w1, w2; } fx[] = { - { BW_IR_BSSY, 13, 0, 0x00007945U, 0x000000c0U, 0x03800200U }, - { BW_IR_BSSY, 41, 0, 0x00007945U, 0x00000280U, 0x03800200U }, - { BW_IR_BSSY, 10, 0, 0x00007945U, 0x00000090U, 0x03800200U }, - { BW_IR_BSYNC, 0, 0, 0x00007941U, 0x00000000U, 0x03800200U }, - }; - for (int i = 0; i < (int)(sizeof fx / sizeof fx[0]); i++) { - BlackwellIRInsn in; - memset(&in, 0, sizeof in); - in.op = fx[i].op; in.dst_vreg = -1; in.src1_vreg = -1; in.src2_vreg = -1; in.src3_vreg = -1; in.ureg = -1; - in.imm = (uint32_t)fx[i].delta; in.bar_reg = (uint8_t)fx[i].bar; - uint32_t w[4]; - if (omega_blackwell_encode_one(&in, w) != 0) return -(2 * i + 1); - if (w[0] != fx[i].w0 || w[1] != fx[i].w1 || w[2] != fx[i].w2) return -(2 * i + 2); - /* control word defaults mirror nvcc's: BSSY 0x000fe200, BSYNC 0x000fea00 */ - if (w[3] != (fx[i].op == BW_IR_BSSY ? 0x000fe200U : 0x000fea00U)) return -(2 * i + 2); - } - /* refusals: a BSSY must name a later instruction (delta >= 2), barrier ids stop at B15 */ - BlackwellIRInsn bad; uint32_t w[4]; - memset(&bad, 0, sizeof bad); bad.op = BW_IR_BSSY; bad.dst_vreg = bad.src1_vreg = bad.src2_vreg = bad.src3_vreg = bad.ureg = -1; - bad.imm = 1; bad.bar_reg = 0; - if (omega_blackwell_encode_one(&bad, w) == 0) return -101; - bad.imm = (uint32_t)-3; - if (omega_blackwell_encode_one(&bad, w) == 0) return -102; - bad.imm = 5; bad.bar_reg = 16; - if (omega_blackwell_encode_one(&bad, w) == 0) return -103; - bad.op = BW_IR_BSYNC; bad.imm = 0; bad.bar_reg = 16; - if (omega_blackwell_encode_one(&bad, w) == 0) return -104; - bad.bar_reg = 15; - if (omega_blackwell_encode_one(&bad, w) != 0 || w[0] != (0x00007941U | (15U << 16))) return -105; - return 0; -} - -/* FB-1 cut 4: word-level fixtures for the four additive ops (checked against the - * nvdisasm decodes recorded in docs/numeric/FB1_CUT4_ELEMENTWISE.md). 0 = all good. */ -int omega_blackwell_verify_codegen_fixtures_fb1cut4(void) { - OmegaRegAlloc ra; - memset(&ra, 0, sizeof(ra)); - ra.vreg_to_phys[0] = 2; /* R2 */ - ra.vreg_to_phys[1] = 4; /* R4 */ - ra.vreg_to_phys[2] = 9; /* R9 */ - ra.vreg_to_phys[3] = 10; /* R10 */ - ra.vreg_to_phys[4] = 8; /* R8 */ - uint32_t w[4]; - - /* MUFU.EX2 R2, R4 */ - BlackwellIRInsn ex2 = { .op = BW_IR_MUFU_EX2, .dst_vreg = 0, .src1_vreg = 1 }; - if (encode_single_insn(&ex2, &ra, w) != 0) return -1; - if (w[0] != 0x00027308U || w[1] != 4 || w[2] != 0x00000800U) return -2; - /* BAR.SYNC.DEFER_BLOCKING 0x0 */ - BlackwellIRInsn bar = { .op = BW_IR_BAR_SYNC }; - if (encode_single_insn(&bar, &ra, w) != 0) return -3; - if (w[0] != 0x00007b1dU || w[1] != 0 || w[2] != 0x00010000U || w[3] != 0x000fec00U) return -4; - /* LDS R9, [R10+URZ] */ - BlackwellIRInsn lds = { .op = BW_IR_LDS32, .dst_vreg = 2, .src1_vreg = 3 }; - if (encode_single_insn(&lds, &ra, w) != 0) return -5; - if (w[0] != 0x0a097984U || w[1] != 0xff || w[2] != 0x08000800U) return -6; - /* STS [R8+URZ], R2 */ - BlackwellIRInsn sts = { .op = BW_IR_STS32, .src1_vreg = 4, .src2_vreg = 0 }; - if (encode_single_insn(&sts, &ra, w) != 0) return -7; - if (w[0] != 0x08007988U || w[1] != 2 || w[2] != 0x080008ffU) return -8; - return 0; -} - -int omega_bw_encode_program(const BlackwellIRProgram *prog, uint8_t *code_buf, size_t max_len, size_t *out_len) { - if (!prog || !code_buf || !out_len) return -1; - - size_t padded_insns = (prog->count + 7) & ~7ULL; - if (padded_insns < 32) padded_insns = 32; - - size_t req_bytes = padded_insns * 16; - if (max_len < req_bytes) return -1; - - uint8_t *p = code_buf; - for (size_t i = 0; i < padded_insns; i++) { - uint32_t w[4]; - if (i < prog->count) { - if (encode_single_insn(&prog->insns[i], &prog->regalloc, w) != 0) { - return -1; - } - } else { - /* Pad with NOP */ - w[0] = 0x00007918; - w[1] = 0x00000000; - w[2] = 0x00000000; - w[3] = 0x000fc000; - } - - p[0] = (uint8_t)(w[0] & 0xff); - p[1] = (uint8_t)((w[0] >> 8) & 0xff); - p[2] = (uint8_t)((w[0] >> 16) & 0xff); - p[3] = (uint8_t)((w[0] >> 24) & 0xff); - - p[4] = (uint8_t)(w[1] & 0xff); - p[5] = (uint8_t)((w[1] >> 8) & 0xff); - p[6] = (uint8_t)((w[1] >> 16) & 0xff); - p[7] = (uint8_t)((w[1] >> 24) & 0xff); - - p[8] = (uint8_t)(w[2] & 0xff); - p[9] = (uint8_t)((w[2] >> 8) & 0xff); - p[10] = (uint8_t)((w[2] >> 16) & 0xff); - p[11] = (uint8_t)((w[2] >> 24) & 0xff); - - p[12] = (uint8_t)(w[3] & 0xff); - p[13] = (uint8_t)((w[3] >> 8) & 0xff); - p[14] = (uint8_t)((w[3] >> 16) & 0xff); - p[15] = (uint8_t)((w[3] >> 24) & 0xff); - - p += 16; - } - - *out_len = req_bytes; - return 0; -} - -int omega_blackwell_codegen_matmul_i32_prog(const OmegaMatMulSpec *spec, BlackwellIRProgram *prog) { - if (!spec || !prog) return -1; - if (spec->m == 0 || spec->k == 0 || spec->n == 0) return -1; - - /* Fail closed loudly if K exceeds bounded unroll limit */ - if (spec->k > 32) { - BW_DIAG("ERROR: MatMul integer codegen exceeds bounded unroll limit (K=%u > 32)\n", spec->k); - return -1; - } - - omega_bw_ir_init(prog); - - /* 1. Allocate virtual registers */ - /* 64-bit base pointers */ - int v_ptr_a = omega_bw_ir_alloc_vreg64(prog); - int v_ptr_b = omega_bw_ir_alloc_vreg64(prog); - int v_ptr_c = omega_bw_ir_alloc_vreg64(prog); - int uv_desc = omega_bw_ir_alloc_uvreg64(prog); - - /* 32-bit CTA / Thread indexing */ - int v_ctaid_x = omega_bw_ir_alloc_vreg(prog); - int v_ctaid_y = omega_bw_ir_alloc_vreg(prog); - int v_tid_x = omega_bw_ir_alloc_vreg(prog); - int v_tid_y = omega_bw_ir_alloc_vreg(prog); - - /* 2D Coordinates & loop invariants */ - int v_col = omega_bw_ir_alloc_vreg(prog); - int v_row = omega_bw_ir_alloc_vreg(prog); - int v_row_k = omega_bw_ir_alloc_vreg(prog); - int v_const_1 = omega_bw_ir_alloc_vreg(prog); - int v_acc = omega_bw_ir_alloc_vreg(prog); - - /* Working registers for memory address and operands */ - int v_idx_a = omega_bw_ir_alloc_vreg(prog); - int v_off_a = omega_bw_ir_alloc_vreg64(prog); - int v_val_a = omega_bw_ir_alloc_vreg(prog); - - int v_idx_b = omega_bw_ir_alloc_vreg(prog); - int v_off_b = omega_bw_ir_alloc_vreg64(prog); - int v_val_b = omega_bw_ir_alloc_vreg(prog); - - int v_idx_c = omega_bw_ir_alloc_vreg(prog); - int v_off_c = omega_bw_ir_alloc_vreg64(prog); - - if (v_off_c < 0) return -1; - - /* 2. Load 64-bit global memory descriptor into uniform register pair */ - /* LDCU.64 uv_desc, c[0x0][0x358] */ - BlackwellIRInsn insn_desc = { .op = BW_IR_LDCU64, .dst_vreg = uv_desc, .imm = 0x358, .is_uniform = true }; - omega_bw_ir_append(prog, &insn_desc); - - /* 3. Load 64-bit base pointers from Constant Bank 0 */ - /* LDC.64 v_ptr_a, c[0x0][0x380] */ - BlackwellIRInsn insn_ptra = { .op = BW_IR_LDC64, .dst_vreg = v_ptr_a, .imm = 0x380 }; - omega_bw_ir_append(prog, &insn_ptra); - - /* LDC.64 v_ptr_b, c[0x0][0x388] */ - BlackwellIRInsn insn_ptrb = { .op = BW_IR_LDC64, .dst_vreg = v_ptr_b, .imm = 0x388 }; - omega_bw_ir_append(prog, &insn_ptrb); - - /* LDC.64 v_ptr_c, c[0x0][0x390] */ - BlackwellIRInsn insn_ptrc = { .op = BW_IR_LDC64, .dst_vreg = v_ptr_c, .imm = 0x390 }; - omega_bw_ir_append(prog, &insn_ptrc); - - /* 4. Query 2D CTA and Thread coordinates */ - /* S2R v_ctaid_x, SR_CTAID.X */ - BlackwellIRInsn insn_ctax = { .op = BW_IR_S2R, .dst_vreg = v_ctaid_x, .imm = BW_SR_CTAID_X }; - omega_bw_ir_append(prog, &insn_ctax); - - /* S2R v_ctaid_y, SR_CTAID.Y */ - BlackwellIRInsn insn_ctay = { .op = BW_IR_S2R, .dst_vreg = v_ctaid_y, .imm = BW_SR_CTAID_Y }; - omega_bw_ir_append(prog, &insn_ctay); - - /* S2R v_tid_x, SR_TID.X */ - BlackwellIRInsn insn_tidx = { .op = BW_IR_S2R, .dst_vreg = v_tid_x, .imm = BW_SR_TID_X }; - omega_bw_ir_append(prog, &insn_tidx); - - /* S2R v_tid_y, SR_TID.Y */ - BlackwellIRInsn insn_tidy = { .op = BW_IR_S2R, .dst_vreg = v_tid_y, .imm = BW_SR_TID_Y }; - omega_bw_ir_append(prog, &insn_tidy); - - /* 5. Compute 2D (row, col) coordinates */ - /* Standard 16x16 tile threads: col = ctaid_x * 16 + tid_x */ - BlackwellIRInsn insn_col = { .op = BW_IR_IMAD, .dst_vreg = v_col, .src1_vreg = v_ctaid_x, .src2_vreg = -1, .imm = 16, .src3_vreg = v_tid_x }; - omega_bw_ir_append(prog, &insn_col); - - /* row = ctaid_y * 16 + tid_y */ - BlackwellIRInsn insn_row = { .op = BW_IR_IMAD, .dst_vreg = v_row, .src1_vreg = v_ctaid_y, .src2_vreg = -1, .imm = 16, .src3_vreg = v_tid_y }; - omega_bw_ir_append(prog, &insn_row); - - /* 6. Precompute row base index: v_row_k = v_row * K + RZ */ - BlackwellIRInsn insn_row_k = { .op = BW_IR_IMAD, .dst_vreg = v_row_k, .src1_vreg = v_row, .src2_vreg = -1, .imm = spec->k, .src3_vreg = -1 }; - omega_bw_ir_append(prog, &insn_row_k); - - /* 7. Initialize v_const_1 = 1 for immediate linear indexing */ - BlackwellIRInsn insn_c1 = { .op = BW_IR_MOV_IMM, .dst_vreg = v_const_1, .imm = 1 }; - omega_bw_ir_append(prog, &insn_c1); - - /* 8. Initialize accumulator to zero: MOV v_acc, RZ */ - BlackwellIRInsn insn_init_acc = { .op = BW_IR_MOV_RZ, .dst_vreg = v_acc }; - omega_bw_ir_append(prog, &insn_init_acc); - - /* 9. Synthesize inner accumulation loop across K iterations */ - for (uint32_t step = 0; step < spec->k; step++) { - /* A[row, step]: idx_a = v_row_k + step = v_const_1 * step + v_row_k */ - BlackwellIRInsn insn_idxa = { .op = BW_IR_IMAD, .dst_vreg = v_idx_a, .src1_vreg = v_const_1, .src2_vreg = -1, .imm = step, .src3_vreg = v_row_k }; - omega_bw_ir_append(prog, &insn_idxa); - - /* off_a = v_ptr_a + idx_a * 4 */ - BlackwellIRInsn insn_offa = { .op = BW_IR_IMAD_WIDE, .dst_vreg = v_off_a, .src1_vreg = v_idx_a, .src2_vreg = -1, .imm = 4, .src3_vreg = v_ptr_a }; - omega_bw_ir_append(prog, &insn_offa); - - /* Load A[row, step]: LDG.E v_val_a, desc[uv_desc][v_off_a.64] */ - BlackwellIRInsn insn_lda = { .op = BW_IR_LDG_E, .dst_vreg = v_val_a, .src1_vreg = v_off_a, .src2_vreg = -1, .src3_vreg = -1, .ureg = uv_desc, .control = 0x001ea800 }; - omega_bw_ir_append(prog, &insn_lda); - - /* B[step, col]: idx_b = step * N + col = v_const_1 * (step * N) + v_col */ - uint32_t step_n = step * spec->n; - BlackwellIRInsn insn_idxb = { .op = BW_IR_IMAD, .dst_vreg = v_idx_b, .src1_vreg = v_const_1, .src2_vreg = -1, .imm = step_n, .src3_vreg = v_col }; - omega_bw_ir_append(prog, &insn_idxb); - - /* off_b = v_ptr_b + idx_b * 4 */ - BlackwellIRInsn insn_offb = { .op = BW_IR_IMAD_WIDE, .dst_vreg = v_off_b, .src1_vreg = v_idx_b, .src2_vreg = -1, .imm = 4, .src3_vreg = v_ptr_b }; - omega_bw_ir_append(prog, &insn_offb); - - /* Load B[step, col]: LDG.E v_val_b, desc[uv_desc][v_off_b.64] */ - BlackwellIRInsn insn_ldb = { .op = BW_IR_LDG_E, .dst_vreg = v_val_b, .src1_vreg = v_off_b, .src2_vreg = -1, .src3_vreg = -1, .ureg = uv_desc, .control = 0x002ea800 }; - omega_bw_ir_append(prog, &insn_ldb); - - /* Multiply and accumulate: IMAD v_acc, v_val_a, v_val_b, v_acc */ - BlackwellIRInsn insn_acc = { .op = BW_IR_IMAD, .dst_vreg = v_acc, .src1_vreg = v_val_a, .src2_vreg = v_val_b, .src3_vreg = v_acc, .control = 0x004fca00 }; - omega_bw_ir_append(prog, &insn_acc); - } - - /* 10. Compute output index: idx_c = row * N + col */ - BlackwellIRInsn insn_idxc = { .op = BW_IR_IMAD, .dst_vreg = v_idx_c, .src1_vreg = v_row, .src2_vreg = -1, .imm = spec->n, .src3_vreg = v_col }; - omega_bw_ir_append(prog, &insn_idxc); - - /* off_c = v_ptr_c + idx_c * 4 */ - BlackwellIRInsn insn_offc = { .op = BW_IR_IMAD_WIDE, .dst_vreg = v_off_c, .src1_vreg = v_idx_c, .src2_vreg = -1, .imm = 4, .src3_vreg = v_ptr_c }; - omega_bw_ir_append(prog, &insn_offc); - - /* Store C[row, col]: STG.E desc[uv_desc][v_off_c.64], v_acc */ - BlackwellIRInsn insn_stc = { .op = BW_IR_STG_E, .src1_vreg = v_off_c, .src2_vreg = v_acc, .src3_vreg = -1, .ureg = uv_desc }; - omega_bw_ir_append(prog, &insn_stc); - - /* 11. Kernel termination */ - BlackwellIRInsn insn_exit = { .op = BW_IR_EXIT, .predicate_p0 = false }; - omega_bw_ir_append(prog, &insn_exit); - - BlackwellIRInsn insn_bra = { .op = BW_IR_BRA }; - omega_bw_ir_append(prog, &insn_bra); - - /* 12. Solve bounded deterministic register allocation */ - if (omega_bw_regalloc_solve(prog) != 0) { - return -1; - } - return 0; -} - -/* ---- native v1 P07 cut A.2: allocation-free kernel encoding --------------------------------- - * The one place a solved IR program becomes a kernel. Hosted and freestanding builds both encode - * through it, so the two produce the same bytes for the same program (make test-codegen-freestanding - * compares them). */ -size_t omega_bw_program_code_bytes(const BlackwellIRProgram *prog) { - if (!prog) return 0; - size_t padded_insns = (prog->count + 7) & ~(size_t)7; - if (padded_insns < 32) padded_insns = 32; - return padded_insns * 16; -} - -int omega_blackwell_kernel_encode_into(const BlackwellIRProgram *prog, uint8_t *code_buf, size_t code_cap, - OmegaBlackwellKernel *kernel) { - if (!kernel) return -1; - memset(kernel, 0, sizeof *kernel); - if (!prog || !code_buf) return -1; - size_t need = omega_bw_program_code_bytes(prog); - if (code_cap < need) return OMEGA_BW_CODE_BUFFER_TOO_SMALL; /* refused before any byte is written */ - size_t emitted_size = 0; - if (omega_bw_encode_program(prog, code_buf, code_cap, &emitted_size) != 0) return -1; - kernel->code = code_buf; - kernel->code_size = emitted_size; - kernel->insn_count = emitted_size / 16; - kernel->gpr_count = prog->regalloc.peak_gpr_usage; - kernel->uniform_gpr_count = prog->regalloc.peak_ugpr_usage; - sha256_hash(kernel->code, kernel->code_size, kernel->code_digest); - return 0; -} - -#ifndef OMEGA_GPU_NO_POSIX -/* Hosted only: the allocating generators keep their names, caps and behavior. Each builds the program, - * mallocs max_bytes (the cap each generator always had) and encodes through omega_blackwell_kernel_encode_into; - * omega_blackwell_kernel_free releases the buffer. */ -static int bw_kernel_alloc_encode(const BlackwellIRProgram *prog, size_t max_bytes, OmegaBlackwellKernel *kernel) { - uint8_t *buf = (uint8_t *)malloc(max_bytes); - if (!buf) { kernel->code = NULL; return -1; } - if (omega_blackwell_kernel_encode_into(prog, buf, max_bytes, kernel) != 0) { - free(buf); - kernel->code = NULL; - return -1; - } - return 0; -} - -int omega_blackwell_codegen_matmul_i32(const OmegaMatMulSpec *spec, OmegaBlackwellKernel *kernel) { - if (!spec || !kernel) return -1; - BlackwellIRProgram prog; - if (omega_blackwell_codegen_matmul_i32_prog(spec, &prog) != 0) return -1; - /* 13. the code buffer is exactly the padded program (the i32 generator never had a fixed cap) */ - return bw_kernel_alloc_encode(&prog, omega_bw_program_code_bytes(&prog), kernel); -} -#endif - -int omega_blackwell_codegen_matmul_tensor_prog(const OmegaMatMulSpec *spec, BlackwellIRProgram *prog) { - if (!spec || !prog) return -1; - if (spec->m == 0 || spec->k == 0 || spec->n == 0) return -1; - if (spec->precision != OMEGA_MATMUL_PRECISION_FP16 && spec->precision != OMEGA_MATMUL_PRECISION_BF16) return -1; - if (spec->m % 16 != 0 || spec->n % 8 != 0 || spec->k != 16) { - BW_DIAG("ERROR: MatMul tensor codegen requires M multiple of 16, N multiple of 8, K=16 (got %ux%ux%u)\n", - spec->m, spec->k, spec->n); - return -1; - } - - omega_bw_ir_init(prog); - - /* 1. Allocate virtual registers */ - /* 64-bit base pointers */ - int v_ptr_a = omega_bw_ir_alloc_vreg64(prog); - int v_ptr_b = omega_bw_ir_alloc_vreg64(prog); - int v_ptr_c = omega_bw_ir_alloc_vreg64(prog); - int uv_desc = omega_bw_ir_alloc_uvreg64(prog); - - /* 32-bit CTA / Thread indexing */ - int v_tid = omega_bw_ir_alloc_vreg(prog); - int v_ctaid_x = omega_bw_ir_alloc_vreg(prog); - int v_ctaid_y = omega_bw_ir_alloc_vreg(prog); - - /* Tile and thread coordinates */ - int v_group_id = omega_bw_ir_alloc_vreg(prog); - int v_tid_in_grp = omega_bw_ir_alloc_vreg(prog); - int v_row_base = omega_bw_ir_alloc_vreg(prog); - int v_col_base = omega_bw_ir_alloc_vreg(prog); - int v_r0 = omega_bw_ir_alloc_vreg(prog); - int v_r1 = omega_bw_ir_alloc_vreg(prog); - int v_col_b = omega_bw_ir_alloc_vreg(prog); - int v_tid_col2 = omega_bw_ir_alloc_vreg(prog); - int v_const_1 = omega_bw_ir_alloc_vreg(prog); - - /* Bundles for HMMA */ - int v_ra = omega_bw_ir_alloc_vreg128(prog); /* Quad: Ra[0]..Ra[3] */ - int v_rb = omega_bw_ir_alloc_vreg64(prog); /* Pair: Rb[0]..Rb[1] */ - int v_rd = omega_bw_ir_alloc_vreg128(prog); /* Quad: Rd[0]..Rd[3] */ - - /* Scratch registers for indexing and loads */ - int v_idx = omega_bw_ir_alloc_vreg(prog); - int v_off = omega_bw_ir_alloc_vreg64(prog); - int v_b_tmp0 = omega_bw_ir_alloc_vreg(prog); - int v_b_tmp1 = omega_bw_ir_alloc_vreg(prog); - - if (v_b_tmp1 < 0) return -1; - - /* 2. Load 64-bit global memory descriptor into uniform register pair */ - /* LDCU.64 uv_desc, c[0x0][0x358] */ - BlackwellIRInsn insn_desc = { .op = BW_IR_LDCU64, .dst_vreg = uv_desc, .imm = 0x358, .is_uniform = true }; - omega_bw_ir_append(prog, &insn_desc); - - /* 3. Load 64-bit base pointers from Constant Bank 0 */ - /* LDC.64 v_ptr_a, c[0x0][0x380] */ - BlackwellIRInsn insn_ptra = { .op = BW_IR_LDC64, .dst_vreg = v_ptr_a, .imm = 0x380 }; - omega_bw_ir_append(prog, &insn_ptra); - - /* LDC.64 v_ptr_b, c[0x0][0x388] */ - BlackwellIRInsn insn_ptrb = { .op = BW_IR_LDC64, .dst_vreg = v_ptr_b, .imm = 0x388 }; - omega_bw_ir_append(prog, &insn_ptrb); - - /* LDC.64 v_ptr_c, c[0x0][0x390] */ - BlackwellIRInsn insn_ptrc = { .op = BW_IR_LDC64, .dst_vreg = v_ptr_c, .imm = 0x390 }; - omega_bw_ir_append(prog, &insn_ptrc); - - /* 4. Query Special Registers */ - /* S2R v_tid, SR_TID.X */ - BlackwellIRInsn insn_tidx = { .op = BW_IR_S2R, .dst_vreg = v_tid, .imm = BW_SR_TID_X }; - omega_bw_ir_append(prog, &insn_tidx); - - /* S2R v_ctaid_x, SR_CTAID.X */ - BlackwellIRInsn insn_cta_x = { .op = BW_IR_S2R, .dst_vreg = v_ctaid_x, .imm = BW_SR_CTAID_X }; - omega_bw_ir_append(prog, &insn_cta_x); - - /* S2R v_ctaid_y, SR_CTAID.Y */ - BlackwellIRInsn insn_cta_y = { .op = BW_IR_S2R, .dst_vreg = v_ctaid_y, .imm = BW_SR_CTAID_Y }; - omega_bw_ir_append(prog, &insn_cta_y); - - /* 5. Set up constant 1 */ - BlackwellIRInsn insn_c1 = { .op = BW_IR_MOV_IMM, .dst_vreg = v_const_1, .imm = 1 }; - omega_bw_ir_append(prog, &insn_c1); - - /* 6. Extract warp-level tile and thread coordinates */ - /* group_id = tid >> 2 */ - BlackwellIRInsn insn_grp = { .op = BW_IR_SHF_R, .dst_vreg = v_group_id, .src1_vreg = v_tid, .imm = 2 }; - omega_bw_ir_append(prog, &insn_grp); - - /* tid_in_grp = tid & 3 */ - BlackwellIRInsn insn_tig = { .op = BW_IR_LOP3_AND, .dst_vreg = v_tid_in_grp, .src1_vreg = v_tid, .imm = 3 }; - omega_bw_ir_append(prog, &insn_tig); - - /* row_base = ctaid_y * 16 */ - BlackwellIRInsn insn_rbase = { .op = BW_IR_IMAD, .dst_vreg = v_row_base, .src1_vreg = v_ctaid_y, .src2_vreg = -1, .imm = 16, .src3_vreg = -1 }; - omega_bw_ir_append(prog, &insn_rbase); - - /* col_base = ctaid_x * 8 */ - BlackwellIRInsn insn_cbase = { .op = BW_IR_IMAD, .dst_vreg = v_col_base, .src1_vreg = v_ctaid_x, .src2_vreg = -1, .imm = 8, .src3_vreg = -1 }; - omega_bw_ir_append(prog, &insn_cbase); - - /* r0 = row_base + group_id */ - BlackwellIRInsn insn_r0 = { .op = BW_IR_IADD3, .dst_vreg = v_r0, .src1_vreg = v_row_base, .src2_vreg = v_group_id, .src3_vreg = -1 }; - omega_bw_ir_append(prog, &insn_r0); - - /* r1 = r0 + 8 */ - BlackwellIRInsn insn_r1 = { .op = BW_IR_IMAD, .dst_vreg = v_r1, .src1_vreg = v_const_1, .src2_vreg = -1, .imm = 8, .src3_vreg = v_r0 }; - omega_bw_ir_append(prog, &insn_r1); - - /* col_b = col_base + group_id */ - BlackwellIRInsn insn_colb = { .op = BW_IR_IADD3, .dst_vreg = v_col_b, .src1_vreg = v_col_base, .src2_vreg = v_group_id, .src3_vreg = -1 }; - omega_bw_ir_append(prog, &insn_colb); - - /* tid_col2 = tid_in_grp * 2 */ - BlackwellIRInsn insn_tcol2 = { .op = BW_IR_IMAD, .dst_vreg = v_tid_col2, .src1_vreg = v_tid_in_grp, .src2_vreg = -1, .imm = 2, .src3_vreg = -1 }; - omega_bw_ir_append(prog, &insn_tcol2); - - /* 7. Load A fragments (16x16 row-major, 2 bytes/element, row stride 32 bytes) */ - /* Pair 0: a0, a1 at r0 * 32 + tid_col2 * 2 */ - BlackwellIRInsn insn_a0_idx1 = { .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_r0, .src2_vreg = -1, .imm = 32, .src3_vreg = -1 }; - omega_bw_ir_append(prog, &insn_a0_idx1); - BlackwellIRInsn insn_a0_idx2 = { .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_tid_col2, .src2_vreg = -1, .imm = 2, .src3_vreg = v_idx }; - omega_bw_ir_append(prog, &insn_a0_idx2); - BlackwellIRInsn insn_a0_off = { .op = BW_IR_IMAD_WIDE, .dst_vreg = v_off, .src1_vreg = v_idx, .src2_vreg = -1, .imm = 1, .src3_vreg = v_ptr_a }; - omega_bw_ir_append(prog, &insn_a0_off); - BlackwellIRInsn insn_lda0 = { .op = BW_IR_LDG_E, .dst_vreg = v_ra, .dst_subreg = 0, .src1_vreg = v_off, .src2_vreg = -1, .src3_vreg = -1, .ureg = uv_desc, .control = 0x001ea800 }; - omega_bw_ir_append(prog, &insn_lda0); - - /* Pair 1: a2, a3 at r1 * 32 + tid_col2 * 2 */ - BlackwellIRInsn insn_a1_idx1 = { .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_r1, .src2_vreg = -1, .imm = 32, .src3_vreg = -1 }; - omega_bw_ir_append(prog, &insn_a1_idx1); - BlackwellIRInsn insn_a1_idx2 = { .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_tid_col2, .src2_vreg = -1, .imm = 2, .src3_vreg = v_idx }; - omega_bw_ir_append(prog, &insn_a1_idx2); - BlackwellIRInsn insn_a1_off = { .op = BW_IR_IMAD_WIDE, .dst_vreg = v_off, .src1_vreg = v_idx, .src2_vreg = -1, .imm = 1, .src3_vreg = v_ptr_a }; - omega_bw_ir_append(prog, &insn_a1_off); - BlackwellIRInsn insn_lda1 = { .op = BW_IR_LDG_E, .dst_vreg = v_ra, .dst_subreg = 1, .src1_vreg = v_off, .src2_vreg = -1, .src3_vreg = -1, .ureg = uv_desc, .control = 0x001ea800 }; - omega_bw_ir_append(prog, &insn_lda1); - - /* Pair 2: a4, a5 at r0 * 32 + tid_col2 * 2 + 16 */ - BlackwellIRInsn insn_a2_idx1 = { .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_r0, .src2_vreg = -1, .imm = 32, .src3_vreg = -1 }; - omega_bw_ir_append(prog, &insn_a2_idx1); - BlackwellIRInsn insn_a2_idx2 = { .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_tid_col2, .src2_vreg = -1, .imm = 2, .src3_vreg = v_idx }; - omega_bw_ir_append(prog, &insn_a2_idx2); - BlackwellIRInsn insn_a2_idx3 = { .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_const_1, .src2_vreg = -1, .imm = 16, .src3_vreg = v_idx }; - omega_bw_ir_append(prog, &insn_a2_idx3); - BlackwellIRInsn insn_a2_off = { .op = BW_IR_IMAD_WIDE, .dst_vreg = v_off, .src1_vreg = v_idx, .src2_vreg = -1, .imm = 1, .src3_vreg = v_ptr_a }; - omega_bw_ir_append(prog, &insn_a2_off); - BlackwellIRInsn insn_lda2 = { .op = BW_IR_LDG_E, .dst_vreg = v_ra, .dst_subreg = 2, .src1_vreg = v_off, .src2_vreg = -1, .src3_vreg = -1, .ureg = uv_desc, .control = 0x001ea800 }; - omega_bw_ir_append(prog, &insn_lda2); - - /* Pair 3: a6, a7 at r1 * 32 + tid_col2 * 2 + 16 */ - BlackwellIRInsn insn_a3_idx1 = { .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_r1, .src2_vreg = -1, .imm = 32, .src3_vreg = -1 }; - omega_bw_ir_append(prog, &insn_a3_idx1); - BlackwellIRInsn insn_a3_idx2 = { .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_tid_col2, .src2_vreg = -1, .imm = 2, .src3_vreg = v_idx }; - omega_bw_ir_append(prog, &insn_a3_idx2); - BlackwellIRInsn insn_a3_idx3 = { .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_const_1, .src2_vreg = -1, .imm = 16, .src3_vreg = v_idx }; - omega_bw_ir_append(prog, &insn_a3_idx3); - BlackwellIRInsn insn_a3_off = { .op = BW_IR_IMAD_WIDE, .dst_vreg = v_off, .src1_vreg = v_idx, .src2_vreg = -1, .imm = 1, .src3_vreg = v_ptr_a }; - omega_bw_ir_append(prog, &insn_a3_off); - BlackwellIRInsn insn_lda3 = { .op = BW_IR_LDG_E, .dst_vreg = v_ra, .dst_subreg = 3, .src1_vreg = v_off, .src2_vreg = -1, .src3_vreg = -1, .ureg = uv_desc, .control = 0x001ea800 }; - omega_bw_ir_append(prog, &insn_lda3); - - /* 8. Load and pack B fragments (16xN row-major, 2 bytes/element) */ - /* b0 at tid_col2 * N + col_b */ - BlackwellIRInsn insn_b0_idx = { .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_tid_col2, .src2_vreg = -1, .imm = spec->n, .src3_vreg = v_col_b }; - omega_bw_ir_append(prog, &insn_b0_idx); - BlackwellIRInsn insn_b0_off = { .op = BW_IR_IMAD_WIDE, .dst_vreg = v_off, .src1_vreg = v_idx, .src2_vreg = -1, .imm = 2, .src3_vreg = v_ptr_b }; - omega_bw_ir_append(prog, &insn_b0_off); - BlackwellIRInsn insn_ldb0 = { .op = BW_IR_LDG_E_U16, .dst_vreg = v_b_tmp0, .src1_vreg = v_off, .src2_vreg = -1, .src3_vreg = -1, .ureg = uv_desc, .control = 0x002ea800 }; - omega_bw_ir_append(prog, &insn_ldb0); - - /* b1 at (tid_col2 + 1) * N + col_b = b0_idx + N */ - BlackwellIRInsn insn_b1_idx = { .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_const_1, .src2_vreg = -1, .imm = spec->n, .src3_vreg = v_idx }; - omega_bw_ir_append(prog, &insn_b1_idx); - BlackwellIRInsn insn_b1_off = { .op = BW_IR_IMAD_WIDE, .dst_vreg = v_off, .src1_vreg = v_idx, .src2_vreg = -1, .imm = 2, .src3_vreg = v_ptr_b }; - omega_bw_ir_append(prog, &insn_b1_off); - BlackwellIRInsn insn_ldb1 = { .op = BW_IR_LDG_E_U16, .dst_vreg = v_b_tmp1, .src1_vreg = v_off, .src2_vreg = -1, .src3_vreg = -1, .ureg = uv_desc, .control = 0x002ea800 }; - omega_bw_ir_append(prog, &insn_ldb1); - - /* Pack Rb[0] = (b1 << 16) | b0 (wait on barrier 2) */ - BlackwellIRInsn insn_pack_b0 = { .op = BW_IR_IMAD, .dst_vreg = v_rb, .dst_subreg = 0, .src1_vreg = v_b_tmp1, .src2_vreg = -1, .imm = 0x10000, .src3_vreg = v_b_tmp0, .control = 0x004fca00 }; - omega_bw_ir_append(prog, &insn_pack_b0); - - /* b2 at (tid_col2 + 8) * N + col_b */ - BlackwellIRInsn insn_b2_idx1 = { .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_tid_col2, .src2_vreg = -1, .imm = spec->n, .src3_vreg = v_col_b }; - omega_bw_ir_append(prog, &insn_b2_idx1); - BlackwellIRInsn insn_b2_idx2 = { .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_const_1, .src2_vreg = -1, .imm = 8 * spec->n, .src3_vreg = v_idx }; - omega_bw_ir_append(prog, &insn_b2_idx2); - BlackwellIRInsn insn_b2_off = { .op = BW_IR_IMAD_WIDE, .dst_vreg = v_off, .src1_vreg = v_idx, .src2_vreg = -1, .imm = 2, .src3_vreg = v_ptr_b }; - omega_bw_ir_append(prog, &insn_b2_off); - BlackwellIRInsn insn_ldb2 = { .op = BW_IR_LDG_E_U16, .dst_vreg = v_b_tmp0, .src1_vreg = v_off, .src2_vreg = -1, .src3_vreg = -1, .ureg = uv_desc, .control = 0x002ea800 }; - omega_bw_ir_append(prog, &insn_ldb2); - - /* b3 at (tid_col2 + 9) * N + col_b = b2_idx + N */ - BlackwellIRInsn insn_b3_idx = { .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_const_1, .src2_vreg = -1, .imm = spec->n, .src3_vreg = v_idx }; - omega_bw_ir_append(prog, &insn_b3_idx); - BlackwellIRInsn insn_b3_off = { .op = BW_IR_IMAD_WIDE, .dst_vreg = v_off, .src1_vreg = v_idx, .src2_vreg = -1, .imm = 2, .src3_vreg = v_ptr_b }; - omega_bw_ir_append(prog, &insn_b3_off); - BlackwellIRInsn insn_ldb3 = { .op = BW_IR_LDG_E_U16, .dst_vreg = v_b_tmp1, .src1_vreg = v_off, .src2_vreg = -1, .src3_vreg = -1, .ureg = uv_desc, .control = 0x002ea800 }; - omega_bw_ir_append(prog, &insn_ldb3); - - /* Pack Rb[1] = (b3 << 16) | b2 (wait on barrier 2) */ - BlackwellIRInsn insn_pack_b1 = { .op = BW_IR_IMAD, .dst_vreg = v_rb, .dst_subreg = 1, .src1_vreg = v_b_tmp1, .src2_vreg = -1, .imm = 0x10000, .src3_vreg = v_b_tmp0, .control = 0x004fca00 }; - omega_bw_ir_append(prog, &insn_pack_b1); - - /* 9. Execute HMMA Tensor Core instruction (wait on barrier 1 for A loads) */ - BlackwellIROpcode mma_op = (spec->precision == OMEGA_MATMUL_PRECISION_BF16) ? BW_IR_HMMA_BF16 : BW_IR_HMMA_F16; - BlackwellIRInsn insn_hmma = { - .op = mma_op, - .dst_vreg = v_rd, - .dst_subreg = 0, - .src1_vreg = v_ra, - .src1_subreg = 0, - .src2_vreg = v_rb, - .src2_subreg = 0, - .src3_vreg = -1, /* RZ accumulator input */ - .control = 0x002fca00 /* Wait on barrier 1 */ - }; - omega_bw_ir_append(prog, &insn_hmma); - - /* 10. Store C output fragments (16xN row-major, 4 bytes/float element) */ - /* d0: row r0, col col_base + tid_col2 + 0 */ - BlackwellIRInsn insn_d0_idx1 = { .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_r0, .src2_vreg = -1, .imm = spec->n, .src3_vreg = v_col_base }; - omega_bw_ir_append(prog, &insn_d0_idx1); - BlackwellIRInsn insn_d0_idx2 = { .op = BW_IR_IADD3, .dst_vreg = v_idx, .src1_vreg = v_idx, .src2_vreg = v_tid_col2, .src3_vreg = -1 }; - omega_bw_ir_append(prog, &insn_d0_idx2); - BlackwellIRInsn insn_d0_off = { .op = BW_IR_IMAD_WIDE, .dst_vreg = v_off, .src1_vreg = v_idx, .src2_vreg = -1, .imm = 4, .src3_vreg = v_ptr_c }; - omega_bw_ir_append(prog, &insn_d0_off); - BlackwellIRInsn insn_std0 = { .op = BW_IR_STG_E, .src1_vreg = v_off, .src2_vreg = v_rd, .src2_subreg = 0, .ureg = uv_desc, .control = 0x000fe200 }; - omega_bw_ir_append(prog, &insn_std0); - - /* d1: row r0, col col_base + tid_col2 + 1 */ - BlackwellIRInsn insn_d1_idx = { .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_const_1, .src2_vreg = -1, .imm = 1, .src3_vreg = v_idx }; - omega_bw_ir_append(prog, &insn_d1_idx); - BlackwellIRInsn insn_d1_off = { .op = BW_IR_IMAD_WIDE, .dst_vreg = v_off, .src1_vreg = v_idx, .src2_vreg = -1, .imm = 4, .src3_vreg = v_ptr_c }; - omega_bw_ir_append(prog, &insn_d1_off); - BlackwellIRInsn insn_std1 = { .op = BW_IR_STG_E, .src1_vreg = v_off, .src2_vreg = v_rd, .src2_subreg = 1, .ureg = uv_desc, .control = 0x000fe200 }; - omega_bw_ir_append(prog, &insn_std1); - - /* d2: row r1, col col_base + tid_col2 + 0 */ - BlackwellIRInsn insn_d2_idx1 = { .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_r1, .src2_vreg = -1, .imm = spec->n, .src3_vreg = v_col_base }; - omega_bw_ir_append(prog, &insn_d2_idx1); - BlackwellIRInsn insn_d2_idx2 = { .op = BW_IR_IADD3, .dst_vreg = v_idx, .src1_vreg = v_idx, .src2_vreg = v_tid_col2, .src3_vreg = -1 }; - omega_bw_ir_append(prog, &insn_d2_idx2); - BlackwellIRInsn insn_d2_off = { .op = BW_IR_IMAD_WIDE, .dst_vreg = v_off, .src1_vreg = v_idx, .src2_vreg = -1, .imm = 4, .src3_vreg = v_ptr_c }; - omega_bw_ir_append(prog, &insn_d2_off); - BlackwellIRInsn insn_std2 = { .op = BW_IR_STG_E, .src1_vreg = v_off, .src2_vreg = v_rd, .src2_subreg = 2, .ureg = uv_desc, .control = 0x000fe200 }; - omega_bw_ir_append(prog, &insn_std2); - - /* d3: row r1, col col_base + tid_col2 + 1 */ - BlackwellIRInsn insn_d3_idx = { .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_const_1, .src2_vreg = -1, .imm = 1, .src3_vreg = v_idx }; - omega_bw_ir_append(prog, &insn_d3_idx); - BlackwellIRInsn insn_d3_off = { .op = BW_IR_IMAD_WIDE, .dst_vreg = v_off, .src1_vreg = v_idx, .src2_vreg = -1, .imm = 4, .src3_vreg = v_ptr_c }; - omega_bw_ir_append(prog, &insn_d3_off); - BlackwellIRInsn insn_std3 = { .op = BW_IR_STG_E, .src1_vreg = v_off, .src2_vreg = v_rd, .src2_subreg = 3, .ureg = uv_desc, .control = 0x000fe200 }; - omega_bw_ir_append(prog, &insn_std3); - - /* 11. Program Exit */ - BlackwellIRInsn insn_exit = { .op = BW_IR_EXIT }; - omega_bw_ir_append(prog, &insn_exit); - BlackwellIRInsn insn_bra = { .op = BW_IR_BRA }; - omega_bw_ir_append(prog, &insn_bra); - - /* 12. Run bounded register allocation */ - if (omega_bw_regalloc_solve(prog) != 0) { - return -1; - } - return 0; -} - -#ifndef OMEGA_GPU_NO_POSIX -int omega_blackwell_codegen_matmul_tensor(const OmegaMatMulSpec *spec, OmegaBlackwellKernel *kernel) { - if (!spec || !kernel) return -1; - BlackwellIRProgram prog; - if (omega_blackwell_codegen_matmul_tensor_prog(spec, &prog) != 0) { - return -1; - } - - return bw_kernel_alloc_encode(&prog, OMEGA_BW_CODE_CAP_SMALL, kernel); -} -#endif - -/* ---- FB-1 cut 1b: looped tensor matmul ------------------------------------------ - * - * Control words (bundle bits 105..125, decoded from the chip-proven kernel and from - * the vendor compiler's SASS for an equivalent HMMA loop on this chip, 2026-10-04): - * stall = bits 9..12 of w[3], yield = 13, write barrier = 14..16, read barrier = - * 17..19, wait mask = 20..25. 7 in a barrier field means "none". - * LDG (A) 0x001ea800 stall 4, yield, sets SB2 - * LDG.U16 (B) 0x002ea800 stall 4, yield, sets SB2 - * pack IMAD 0x004fca00 stall 5, waits SB2 (every outstanding load has landed) - * HMMA 0x002fca00 stall 5 (fixed latency: the vendor compiler sets no - * write barrier on HMMA either; consumers wait by stall count) - * STG 0x0007e200 stall 1, yield, read barrier SB3 (the store may read its - * data/address registers late; the next writer waits SB3) - * wait SB3 mask bit 23 (0x00800000) on the first instruction that overwrites a - * register a store reads: the C pointer advance. - * Loop-carried registers are kept live to the end of their loop (keep_alive) because - * the allocator is a straight-line linear scan and knows nothing about back edges. - */ -static void bw_keep_alive(BlackwellIRProgram *prog, int from_idx, int to_idx, int last_idx) { - /* every vreg first defined in [from_idx, to_idx) stays live until last_idx */ - for (int v = 0; v < prog->regalloc.num_vregs; v++) { - OmegaLiveInterval *iv = &prog->regalloc.intervals[v]; - if (iv->first_def >= from_idx && iv->first_def < to_idx && iv->last_use < last_idx) - iv->last_use = last_idx; - } -} - -#define BW_APPEND(prog, ...) do { BlackwellIRInsn _i = { __VA_ARGS__ }; if (omega_bw_ir_append((prog), &_i) < 0) return -1; } while (0) - -int omega_blackwell_codegen_matmul_tensor_loop_prog(const OmegaMatMulSpec *spec, uint32_t grid_x, - int mutant, BlackwellIRProgram *prog) { - if (!spec || !prog) return -1; - if (spec->m == 0 || spec->k == 0 || spec->n == 0) return -1; - if (spec->precision != OMEGA_MATMUL_PRECISION_FP16 && spec->precision != OMEGA_MATMUL_PRECISION_BF16) return -1; - if (spec->m % 16 != 0 || spec->n % 8 != 0 || spec->k % 16 != 0) { - BW_DIAG("ERROR: looped MatMul tensor codegen requires M%%16, N%%8, K%%16 (got %ux%ux%u)\n", - spec->m, spec->k, spec->n); - return -1; - } - const uint32_t K = spec->k, N = spec->n, NT = N / 8, KSTEPS = K / 16; - if (grid_x == 0 || grid_x > NT) return -1; - /* every immediate below must fit 32 bits: 32 * N (B row-block stride in bytes) is the largest */ - if ((uint64_t)N * 32u > 0xffffffffull || (uint64_t)grid_x * 32u > 0xffffffffull) return -1; - if (mutant && KSTEPS < 2) return -1; - - omega_bw_ir_init(prog); - - int v_ptr_a = omega_bw_ir_alloc_vreg64(prog), v_ptr_b = omega_bw_ir_alloc_vreg64(prog), v_ptr_c = omega_bw_ir_alloc_vreg64(prog); - int uv_desc = omega_bw_ir_alloc_uvreg64(prog); - int v_tid = omega_bw_ir_alloc_vreg(prog), v_ctaid_x = omega_bw_ir_alloc_vreg(prog), v_ctaid_y = omega_bw_ir_alloc_vreg(prog); - int v_group = omega_bw_ir_alloc_vreg(prog), v_tig = omega_bw_ir_alloc_vreg(prog), v_tcol2 = omega_bw_ir_alloc_vreg(prog); - int v_r0 = omega_bw_ir_alloc_vreg(prog), v_r1 = omega_bw_ir_alloc_vreg(prog), v_c1 = omega_bw_ir_alloc_vreg(prog); - int v_idx = omega_bw_ir_alloc_vreg(prog), v_col = omega_bw_ir_alloc_vreg(prog), v_cnt = omega_bw_ir_alloc_vreg(prog); - int v_pa0 = omega_bw_ir_alloc_vreg64(prog), v_pa1 = omega_bw_ir_alloc_vreg64(prog), v_pbcol = omega_bw_ir_alloc_vreg64(prog); - int v_pc0 = omega_bw_ir_alloc_vreg64(prog), v_pc1 = omega_bw_ir_alloc_vreg64(prog); - int v_pa0c = omega_bw_ir_alloc_vreg64(prog), v_pa1c = omega_bw_ir_alloc_vreg64(prog), v_pbc = omega_bw_ir_alloc_vreg64(prog); - int v_ta2 = omega_bw_ir_alloc_vreg64(prog), v_ta3 = omega_bw_ir_alloc_vreg64(prog); - int v_tb1 = omega_bw_ir_alloc_vreg64(prog), v_tb2 = omega_bw_ir_alloc_vreg64(prog), v_tb3 = omega_bw_ir_alloc_vreg64(prog); - int v_tc1 = omega_bw_ir_alloc_vreg64(prog), v_tc3 = omega_bw_ir_alloc_vreg64(prog); - int v_ra = omega_bw_ir_alloc_vreg128(prog), v_rb = omega_bw_ir_alloc_vreg64(prog), v_rd = omega_bw_ir_alloc_vreg128(prog); - int v_b0 = omega_bw_ir_alloc_vreg(prog), v_b1 = omega_bw_ir_alloc_vreg(prog); - if (v_b1 < 0 || uv_desc < 0) return -1; - - const uint32_t CTRL_LDA = 0x001ea800u, CTRL_LDB = 0x002ea800u, CTRL_PACK = 0x004fca00u; - const uint32_t CTRL_HMMA = 0x002fca00u, CTRL_STG_RDBAR3 = 0x0007e200u, CTRL_IMADW_WAIT3 = 0x009fcc00u; - const uint32_t CTRL_MOVRZ_WAIT3 = 0x008fe200u; - - /* prologue: descriptor, pointers, ids */ - BW_APPEND(prog, .op = BW_IR_LDCU64, .dst_vreg = uv_desc, .imm = 0x358, .is_uniform = true); - BW_APPEND(prog, .op = BW_IR_LDC64, .dst_vreg = v_ptr_a, .imm = 0x380); - BW_APPEND(prog, .op = BW_IR_LDC64, .dst_vreg = v_ptr_b, .imm = 0x388); - BW_APPEND(prog, .op = BW_IR_LDC64, .dst_vreg = v_ptr_c, .imm = 0x390); - BW_APPEND(prog, .op = BW_IR_S2R, .dst_vreg = v_tid, .imm = BW_SR_TID_X); - BW_APPEND(prog, .op = BW_IR_S2R, .dst_vreg = v_ctaid_x, .imm = BW_SR_CTAID_X); - BW_APPEND(prog, .op = BW_IR_S2R, .dst_vreg = v_ctaid_y, .imm = BW_SR_CTAID_Y); - BW_APPEND(prog, .op = BW_IR_MOV_IMM, .dst_vreg = v_c1, .imm = 1); - BW_APPEND(prog, .op = BW_IR_SHF_R, .dst_vreg = v_group, .src1_vreg = v_tid, .imm = 2); - BW_APPEND(prog, .op = BW_IR_LOP3_AND, .dst_vreg = v_tig, .src1_vreg = v_tid, .imm = 3); - BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_tcol2, .src1_vreg = v_tig, .src2_vreg = -1, .imm = 2, .src3_vreg = -1); - BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_r0, .src1_vreg = v_ctaid_y, .src2_vreg = -1, .imm = 16, .src3_vreg = v_group); - BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_r1, .src1_vreg = v_c1, .src2_vreg = -1, .imm = 8, .src3_vreg = v_r0); - /* pa0 = A + (r0*K + tcol2)*2 ; pa1 likewise with r1 (A row-major, K bf16 per row) */ - BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_r0, .src2_vreg = -1, .imm = K, .src3_vreg = v_tcol2); - BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pa0, .src1_vreg = v_idx, .src2_vreg = -1, .imm = 2, .src3_vreg = v_ptr_a); - BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_r1, .src2_vreg = -1, .imm = K, .src3_vreg = v_tcol2); - BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pa1, .src1_vreg = v_idx, .src2_vreg = -1, .imm = 2, .src3_vreg = v_ptr_a); - /* col = ctaid_x (first column tile of this CTA) */ - BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_col, .src1_vreg = v_ctaid_x, .src2_vreg = -1, .imm = 1, .src3_vreg = -1); - /* pbcol = B + (tcol2*N + col*8 + group)*2 */ - BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_tcol2, .src2_vreg = -1, .imm = N, .src3_vreg = v_group); - BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_col, .src2_vreg = -1, .imm = 8, .src3_vreg = v_idx); - BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pbcol, .src1_vreg = v_idx, .src2_vreg = -1, .imm = 2, .src3_vreg = v_ptr_b); - /* pc0 = C + (r0*N + col*8 + tcol2)*4 ; pc1 with r1 */ - BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_r0, .src2_vreg = -1, .imm = N, .src3_vreg = v_tcol2); - BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_col, .src2_vreg = -1, .imm = 8, .src3_vreg = v_idx); - BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pc0, .src1_vreg = v_idx, .src2_vreg = -1, .imm = 4, .src3_vreg = v_ptr_c); - BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_r1, .src2_vreg = -1, .imm = N, .src3_vreg = v_tcol2); - BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_col, .src2_vreg = -1, .imm = 8, .src3_vreg = v_idx); - BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pc1, .src1_vreg = v_idx, .src2_vreg = -1, .imm = 4, .src3_vreg = v_ptr_c); - - /* ---- column-tile loop ---- */ - const int L_col = (int)prog->count; - /* accumulator = 0 (the first write waits SB3: the previous tile's stores may still read it) */ - BW_APPEND(prog, .op = BW_IR_MOV_RZ, .dst_vreg = v_rd, .dst_subreg = 0, .control = CTRL_MOVRZ_WAIT3); - BW_APPEND(prog, .op = BW_IR_MOV_RZ, .dst_vreg = v_rd, .dst_subreg = 1); - BW_APPEND(prog, .op = BW_IR_MOV_RZ, .dst_vreg = v_rd, .dst_subreg = 2); - BW_APPEND(prog, .op = BW_IR_MOV_RZ, .dst_vreg = v_rd, .dst_subreg = 3); - /* K cursors start at the row/column bases (IMAD.WIDE with imm 0 is a 64-bit copy) */ - BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pa0c, .src1_vreg = v_c1, .src2_vreg = -1, .imm = 0, .src3_vreg = v_pa0); - BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pa1c, .src1_vreg = v_c1, .src2_vreg = -1, .imm = 0, .src3_vreg = v_pa1); - BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pbc, .src1_vreg = v_c1, .src2_vreg = -1, .imm = 0, .src3_vreg = v_pbcol); - BW_APPEND(prog, .op = BW_IR_MOV_IMM, .dst_vreg = v_cnt, .imm = mutant ? KSTEPS - 1 : KSTEPS); - - /* ---- K loop: one 16x16 A fragment and one 16x8 B fragment per step ---- */ - const int L_k = (int)prog->count; - BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_ta2, .src1_vreg = v_c1, .src2_vreg = -1, .imm = 16, .src3_vreg = v_pa0c); - BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_ta3, .src1_vreg = v_c1, .src2_vreg = -1, .imm = 16, .src3_vreg = v_pa1c); - BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_tb1, .src1_vreg = v_c1, .src2_vreg = -1, .imm = 2u * N, .src3_vreg = v_pbc); - BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_tb2, .src1_vreg = v_c1, .src2_vreg = -1, .imm = 16u * N, .src3_vreg = v_pbc); - BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_tb3, .src1_vreg = v_c1, .src2_vreg = -1, .imm = 18u * N, .src3_vreg = v_pbc); - /* A: a0,a1 at (r0, kk+tcol2) ; a2,a3 at (r1, ...) ; a4,a5 at (r0, +8) ; a6,a7 at (r1, +8) */ - BW_APPEND(prog, .op = BW_IR_LDG_E, .dst_vreg = v_ra, .dst_subreg = 0, .src1_vreg = v_pa0c, .src2_vreg = -1, .src3_vreg = -1, .ureg = uv_desc, .control = CTRL_LDA); - BW_APPEND(prog, .op = BW_IR_LDG_E, .dst_vreg = v_ra, .dst_subreg = 1, .src1_vreg = v_pa1c, .src2_vreg = -1, .src3_vreg = -1, .ureg = uv_desc, .control = CTRL_LDA); - BW_APPEND(prog, .op = BW_IR_LDG_E, .dst_vreg = v_ra, .dst_subreg = 2, .src1_vreg = v_ta2, .src2_vreg = -1, .src3_vreg = -1, .ureg = uv_desc, .control = CTRL_LDA); - BW_APPEND(prog, .op = BW_IR_LDG_E, .dst_vreg = v_ra, .dst_subreg = 3, .src1_vreg = v_ta3, .src2_vreg = -1, .src3_vreg = -1, .ureg = uv_desc, .control = CTRL_LDA); - /* B: rows kk+tcol2, +1, +8, +9 at column col*8+group */ - BW_APPEND(prog, .op = BW_IR_LDG_E_U16, .dst_vreg = v_b0, .src1_vreg = v_pbc, .src2_vreg = -1, .src3_vreg = -1, .ureg = uv_desc, .control = CTRL_LDB); - BW_APPEND(prog, .op = BW_IR_LDG_E_U16, .dst_vreg = v_b1, .src1_vreg = v_tb1, .src2_vreg = -1, .src3_vreg = -1, .ureg = uv_desc, .control = CTRL_LDB); - BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_rb, .dst_subreg = 0, .src1_vreg = v_b1, .src2_vreg = -1, .imm = 0x10000, .src3_vreg = v_b0, .control = CTRL_PACK); - BW_APPEND(prog, .op = BW_IR_LDG_E_U16, .dst_vreg = v_b0, .src1_vreg = v_tb2, .src2_vreg = -1, .src3_vreg = -1, .ureg = uv_desc, .control = CTRL_LDB); - BW_APPEND(prog, .op = BW_IR_LDG_E_U16, .dst_vreg = v_b1, .src1_vreg = v_tb3, .src2_vreg = -1, .src3_vreg = -1, .ureg = uv_desc, .control = CTRL_LDB); - BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_rb, .dst_subreg = 1, .src1_vreg = v_b1, .src2_vreg = -1, .imm = 0x10000, .src3_vreg = v_b0, .control = CTRL_PACK); - /* rd += ra * rb (accumulate in place) */ - BW_APPEND(prog, .op = (spec->precision == OMEGA_MATMUL_PRECISION_BF16) ? BW_IR_HMMA_BF16 : BW_IR_HMMA_F16, - .dst_vreg = v_rd, .dst_subreg = 0, .src1_vreg = v_ra, .src1_subreg = 0, .src2_vreg = v_rb, .src2_subreg = 0, - .src3_vreg = v_rd, .src3_subreg = 0, .control = CTRL_HMMA); - /* advance K: A by 16 bf16 (32 bytes), B by 16 rows (32 N bytes); every load above has landed (packs waited SB2) */ - BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pa0c, .src1_vreg = v_c1, .src2_vreg = -1, .imm = 32, .src3_vreg = v_pa0c); - BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pa1c, .src1_vreg = v_c1, .src2_vreg = -1, .imm = 32, .src3_vreg = v_pa1c); - BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pbc, .src1_vreg = v_c1, .src2_vreg = -1, .imm = 32u * N, .src3_vreg = v_pbc); - BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_cnt, .src1_vreg = v_c1, .src2_vreg = -1, .imm = 0xffffffffu, .src3_vreg = v_cnt); - BW_APPEND(prog, .op = BW_IR_ISETP_GE, .dst_vreg = -1, .src1_vreg = v_cnt, .src2_vreg = -1, .imm = 1, .src3_vreg = -1); - { - int here = (int)prog->count; - BW_APPEND(prog, .op = BW_IR_BRA, .imm = (uint32_t)(int32_t)(L_k - here), .predicate_p0 = true); - } - const int L_k_end = (int)prog->count - 1; - - /* ---- store the 16x8 tile: d0 (r0, c), d1 (r0, c+1), d2 (r1, c), d3 (r1, c+1) ---- */ - BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_tc1, .src1_vreg = v_c1, .src2_vreg = -1, .imm = 4, .src3_vreg = v_pc0); - BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_tc3, .src1_vreg = v_c1, .src2_vreg = -1, .imm = 4, .src3_vreg = v_pc1); - BW_APPEND(prog, .op = BW_IR_STG_E, .src1_vreg = v_pc0, .src2_vreg = v_rd, .src2_subreg = 0, .src3_vreg = -1, .ureg = uv_desc, .control = CTRL_STG_RDBAR3); - BW_APPEND(prog, .op = BW_IR_STG_E, .src1_vreg = v_tc1, .src2_vreg = v_rd, .src2_subreg = 1, .src3_vreg = -1, .ureg = uv_desc, .control = CTRL_STG_RDBAR3); - BW_APPEND(prog, .op = BW_IR_STG_E, .src1_vreg = v_pc1, .src2_vreg = v_rd, .src2_subreg = 2, .src3_vreg = -1, .ureg = uv_desc, .control = CTRL_STG_RDBAR3); - BW_APPEND(prog, .op = BW_IR_STG_E, .src1_vreg = v_tc3, .src2_vreg = v_rd, .src2_subreg = 3, .src3_vreg = -1, .ureg = uv_desc, .control = CTRL_STG_RDBAR3); - /* next column tile of this CTA: col += grid_x ; B by grid_x*8 bf16 ; C by grid_x*8 f32 (waits SB3 first) */ - BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_col, .src1_vreg = v_c1, .src2_vreg = -1, .imm = grid_x, .src3_vreg = v_col); - BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pc0, .src1_vreg = v_c1, .src2_vreg = -1, .imm = grid_x * 32u, .src3_vreg = v_pc0, .control = CTRL_IMADW_WAIT3); - BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pc1, .src1_vreg = v_c1, .src2_vreg = -1, .imm = grid_x * 32u, .src3_vreg = v_pc1); - BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pbcol, .src1_vreg = v_c1, .src2_vreg = -1, .imm = grid_x * 16u, .src3_vreg = v_pbcol); - BW_APPEND(prog, .op = BW_IR_ISETP_GE, .dst_vreg = -1, .src1_vreg = v_col, .src2_vreg = -1, .imm = NT, .src3_vreg = -1); - { - int here = (int)prog->count; - BW_APPEND(prog, .op = BW_IR_BRA, .imm = (uint32_t)(int32_t)(L_col - here), .predicate_p0 = true, .predicate_not = true); - } - const int L_col_end = (int)prog->count - 1; - BW_APPEND(prog, .op = BW_IR_EXIT); - BW_APPEND(prog, .op = BW_IR_BRA); - - /* loop-carried values live to the end of their loop */ - bw_keep_alive(prog, 0, L_col, L_col_end); - bw_keep_alive(prog, L_col, L_k, L_k_end); - /* K-loop temporaries too: a load's address pair must never be recycled by a - * later instruction of the same iteration (the load may read it late). */ - bw_keep_alive(prog, L_k, L_k_end, L_k_end); - /* the accumulator is read by the stores after the K loop */ - if (prog->regalloc.intervals[v_rd].last_use < L_col_end) prog->regalloc.intervals[v_rd].last_use = L_col_end; - - return omega_bw_regalloc_solve(prog); -} - -#ifndef OMEGA_GPU_NO_POSIX -int omega_blackwell_codegen_matmul_tensor_loop(const OmegaMatMulSpec *spec, uint32_t grid_x, - int mutant, OmegaBlackwellKernel *kernel) { - if (!spec || !kernel) return -1; - BlackwellIRProgram prog; - if (omega_blackwell_codegen_matmul_tensor_loop_prog(spec, grid_x, mutant, &prog) != 0) return -1; - return bw_kernel_alloc_encode(&prog, OMEGA_BW_CODE_CAP_SMALL, kernel); -} -#endif - -/* ---- omega #328: fragment-major tensor matmul ---------------------------------------- - * - * The looped kernel above reads B as four scattered 2-byte loads per K step and waits for - * them before the next step (pack waits SB2), so each warp has one 256-byte step in flight; - * at m = 1 on GB10 that streams the weights at about 28 GB/s (omega #328 measurement). - * Here A and B arrive in the order HMMA.16816 consumes them (omega_matmul_frag_pack_a / _b): - * A block (mt, ks): 512 bytes, lane L at L*16: a0..a7, one LDG.E.128 per step; - * B block (nt, ks): 256 bytes, lane L at L*8: b0..b3, one LDG.E.64 per step; - * consecutive K steps of one tile are adjacent, so a warp streams its column tile. Each loop - * pass issues `unroll` steps of loads at immediate offsets (A on SB2, B on SB4), then runs the HMMA - * chain: every HMMA waits SB2 and SB4 (stall 15) and a NOP (stall 14) follows it, the 29 cycles nvcc - * puts between dependent HMMA.16816 (omega_blackwell_verify_codegen_fixtures_fragmm). - * The cursors advance only after the chain, when every load has landed (the HMMAs waited - * SB2 and SB4), as in the looped kernel. Stores, the column-tile loop and the C layout (row-major - * f32) are the looped kernel's. */ -/* A loads set SB2 and B loads SB4 (nvcc spreads loads over several scoreboards too); at the - * product unroll of 4 that is 4 loads per scoreboard per pass. The scoreboard depth is not - * documented to us; splitting did not make unroll 8 complete (omega_matmul_frag_unroll). */ -#define BW_FRAG_CTRL_LDG_A 0x000ea800u /* stall 4, yield, sets SB2 (nvcc word) */ -#define BW_FRAG_CTRL_LDG_B 0x000f2800u /* stall 4, yield, sets SB4 (nvcc word) */ -#define BW_FRAG_CTRL_HMMA 0x014fde00u /* stall 15, waits SB2 and SB4 (nvcc: dependent HMMA chain) */ -#define BW_FRAG_CTRL_NOP 0x000fdc00u /* stall 14 (nvcc, between dependent HMMAs) */ - -static void bw_live_until(BlackwellIRProgram *prog, int vreg, int last_idx) { - if (prog->regalloc.intervals[vreg].last_use < last_idx) prog->regalloc.intervals[vreg].last_use = last_idx; -} - -int omega_blackwell_codegen_matmul_frag_prog(const OmegaMatMulSpec *spec, uint32_t grid_x, uint32_t unroll, - int mutant, BlackwellIRProgram *prog) { - if (!spec || !prog) return -1; - if (spec->m == 0 || spec->k == 0 || spec->n == 0) return -1; - if (spec->precision != OMEGA_MATMUL_PRECISION_FP16 && spec->precision != OMEGA_MATMUL_PRECISION_BF16) return -1; - if (spec->m % 16 != 0 || spec->n % 8 != 0 || spec->k % 16 != 0) return -1; - if (unroll != 1 && unroll != 2 && unroll != 4 && unroll != 8) return -1; - const uint32_t K = spec->k, N = spec->n, NT = N / 8, KS = K / 16; - if (KS % unroll != 0) return -1; - const uint32_t ITERS = KS / unroll; - if (grid_x == 0 || grid_x > NT) return -1; - if (mutant && ITERS < 2) return -1; - /* every index and immediate below must fit 32 bits (rows up to OMEGA_BW_MATMUL_MAX_M) */ - if ((uint64_t)NT * KS * 32u > 0xffffffffull || (uint64_t)(OMEGA_BW_MATMUL_MAX_M / 16u) * KS * 32u > 0xffffffffull || - (uint64_t)grid_x * KS * 256u > 0xffffffffull || (uint64_t)N * 32u > 0xffffffffull || - (uint64_t)OMEGA_BW_MATMUL_MAX_M * N > 0xffffffffull) - return -1; - - omega_bw_ir_init(prog); - int v_ptr_a = omega_bw_ir_alloc_vreg64(prog), v_ptr_b = omega_bw_ir_alloc_vreg64(prog), v_ptr_c = omega_bw_ir_alloc_vreg64(prog); - int uv_desc = omega_bw_ir_alloc_uvreg64(prog); - int v_tid = omega_bw_ir_alloc_vreg(prog), v_ctaid_x = omega_bw_ir_alloc_vreg(prog), v_ctaid_y = omega_bw_ir_alloc_vreg(prog); - int v_group = omega_bw_ir_alloc_vreg(prog), v_tig = omega_bw_ir_alloc_vreg(prog), v_tcol2 = omega_bw_ir_alloc_vreg(prog); - int v_r0 = omega_bw_ir_alloc_vreg(prog), v_r1 = omega_bw_ir_alloc_vreg(prog), v_c1 = omega_bw_ir_alloc_vreg(prog); - int v_idx = omega_bw_ir_alloc_vreg(prog), v_col = omega_bw_ir_alloc_vreg(prog), v_cnt = omega_bw_ir_alloc_vreg(prog); - int v_pa = omega_bw_ir_alloc_vreg64(prog), v_pbcol = omega_bw_ir_alloc_vreg64(prog); - int v_pc0 = omega_bw_ir_alloc_vreg64(prog), v_pc1 = omega_bw_ir_alloc_vreg64(prog); - int v_pac = omega_bw_ir_alloc_vreg64(prog), v_pbc = omega_bw_ir_alloc_vreg64(prog); - int v_tc1 = omega_bw_ir_alloc_vreg64(prog), v_tc3 = omega_bw_ir_alloc_vreg64(prog); - int v_rd = omega_bw_ir_alloc_vreg128(prog); - int v_ra[8], v_rb[8]; - for (uint32_t u = 0; u < unroll; u++) { v_ra[u] = omega_bw_ir_alloc_vreg128(prog); v_rb[u] = omega_bw_ir_alloc_vreg64(prog); } - if (v_rb[unroll - 1] < 0 || v_ra[unroll - 1] < 0 || uv_desc < 0) return -1; - - const uint32_t CTRL_STG_RDBAR3 = 0x0007e200u, CTRL_IMADW_WAIT3 = 0x009fcc00u, CTRL_MOVRZ_WAIT3 = 0x008fe200u; - - /* prologue: descriptor, pointers, ids (as the looped kernel) */ - BW_APPEND(prog, .op = BW_IR_LDCU64, .dst_vreg = uv_desc, .imm = 0x358, .is_uniform = true); - BW_APPEND(prog, .op = BW_IR_LDC64, .dst_vreg = v_ptr_a, .imm = 0x380); - BW_APPEND(prog, .op = BW_IR_LDC64, .dst_vreg = v_ptr_b, .imm = 0x388); - BW_APPEND(prog, .op = BW_IR_LDC64, .dst_vreg = v_ptr_c, .imm = 0x390); - BW_APPEND(prog, .op = BW_IR_S2R, .dst_vreg = v_tid, .imm = BW_SR_TID_X); - BW_APPEND(prog, .op = BW_IR_S2R, .dst_vreg = v_ctaid_x, .imm = BW_SR_CTAID_X); - BW_APPEND(prog, .op = BW_IR_S2R, .dst_vreg = v_ctaid_y, .imm = BW_SR_CTAID_Y); - BW_APPEND(prog, .op = BW_IR_MOV_IMM, .dst_vreg = v_c1, .imm = 1); - BW_APPEND(prog, .op = BW_IR_SHF_R, .dst_vreg = v_group, .src1_vreg = v_tid, .imm = 2); - BW_APPEND(prog, .op = BW_IR_LOP3_AND, .dst_vreg = v_tig, .src1_vreg = v_tid, .imm = 3); - BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_tcol2, .src1_vreg = v_tig, .src2_vreg = -1, .imm = 2, .src3_vreg = -1); - BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_r0, .src1_vreg = v_ctaid_y, .src2_vreg = -1, .imm = 16, .src3_vreg = v_group); - BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_r1, .src1_vreg = v_c1, .src2_vreg = -1, .imm = 8, .src3_vreg = v_r0); - /* pa = A + (ctaid_y * KS * 32 + tid) * 16: this CTA's row tile, this lane's 16 bytes of step 0 */ - BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_ctaid_y, .src2_vreg = -1, .imm = KS * 32u, .src3_vreg = v_tid); - BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pa, .src1_vreg = v_idx, .src2_vreg = -1, .imm = 16, .src3_vreg = v_ptr_a); - BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_col, .src1_vreg = v_ctaid_x, .src2_vreg = -1, .imm = 1, .src3_vreg = -1); - /* pbcol = B + (col * KS * 32 + tid) * 8: column tile col, this lane's 8 bytes of step 0 */ - BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_col, .src2_vreg = -1, .imm = KS * 32u, .src3_vreg = v_tid); - BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pbcol, .src1_vreg = v_idx, .src2_vreg = -1, .imm = 8, .src3_vreg = v_ptr_b); - /* pc0 = C + (r0*N + col*8 + tcol2)*4 ; pc1 with r1 */ - BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_r0, .src2_vreg = -1, .imm = N, .src3_vreg = v_tcol2); - BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_col, .src2_vreg = -1, .imm = 8, .src3_vreg = v_idx); - BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pc0, .src1_vreg = v_idx, .src2_vreg = -1, .imm = 4, .src3_vreg = v_ptr_c); - BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_r1, .src2_vreg = -1, .imm = N, .src3_vreg = v_tcol2); - BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_col, .src2_vreg = -1, .imm = 8, .src3_vreg = v_idx); - BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pc1, .src1_vreg = v_idx, .src2_vreg = -1, .imm = 4, .src3_vreg = v_ptr_c); - - /* ---- column-tile loop ---- */ - const int L_col = (int)prog->count; - BW_APPEND(prog, .op = BW_IR_MOV_RZ, .dst_vreg = v_rd, .dst_subreg = 0, .control = CTRL_MOVRZ_WAIT3); - BW_APPEND(prog, .op = BW_IR_MOV_RZ, .dst_vreg = v_rd, .dst_subreg = 1); - BW_APPEND(prog, .op = BW_IR_MOV_RZ, .dst_vreg = v_rd, .dst_subreg = 2); - BW_APPEND(prog, .op = BW_IR_MOV_RZ, .dst_vreg = v_rd, .dst_subreg = 3); - BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pac, .src1_vreg = v_c1, .src2_vreg = -1, .imm = 0, .src3_vreg = v_pa); - BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pbc, .src1_vreg = v_c1, .src2_vreg = -1, .imm = 0, .src3_vreg = v_pbcol); - BW_APPEND(prog, .op = BW_IR_MOV_IMM, .dst_vreg = v_cnt, .imm = mutant ? ITERS - 1 : ITERS); - - /* ---- K loop: `unroll` steps of loads, then the HMMA chain ---- */ - const int L_k = (int)prog->count; - for (uint32_t u = 0; u < unroll; u++) { - BW_APPEND(prog, .op = BW_IR_LDG_E_128, .dst_vreg = v_ra[u], .src1_vreg = v_pac, .src2_vreg = -1, .src3_vreg = -1, - .ureg = uv_desc, .imm = u * 512u, .control = BW_FRAG_CTRL_LDG_A); - BW_APPEND(prog, .op = BW_IR_LDG_E_64, .dst_vreg = v_rb[u], .src1_vreg = v_pbc, .src2_vreg = -1, .src3_vreg = -1, - .ureg = uv_desc, .imm = u * 256u, .control = BW_FRAG_CTRL_LDG_B); - } - const BlackwellIROpcode hmma = (spec->precision == OMEGA_MATMUL_PRECISION_BF16) ? BW_IR_HMMA_BF16 : BW_IR_HMMA_F16; - for (uint32_t u = 0; u < unroll; u++) { - BW_APPEND(prog, .op = hmma, .dst_vreg = v_rd, .src1_vreg = v_ra[u], .src2_vreg = v_rb[u], .src3_vreg = v_rd, - .control = BW_FRAG_CTRL_HMMA); - BW_APPEND(prog, .op = BW_IR_NOP, .dst_vreg = -1, .src1_vreg = -1, .src2_vreg = -1, .src3_vreg = -1, .ureg = -1, - .control = BW_FRAG_CTRL_NOP); - } - BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pac, .src1_vreg = v_c1, .src2_vreg = -1, .imm = unroll * 512u, .src3_vreg = v_pac); - BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pbc, .src1_vreg = v_c1, .src2_vreg = -1, .imm = unroll * 256u, .src3_vreg = v_pbc); - BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_cnt, .src1_vreg = v_c1, .src2_vreg = -1, .imm = 0xffffffffu, .src3_vreg = v_cnt); - BW_APPEND(prog, .op = BW_IR_ISETP_GE, .dst_vreg = -1, .src1_vreg = v_cnt, .src2_vreg = -1, .imm = 1, .src3_vreg = -1); - { - int here = (int)prog->count; - BW_APPEND(prog, .op = BW_IR_BRA, .imm = (uint32_t)(int32_t)(L_k - here), .predicate_p0 = true); - } - const int L_k_end = (int)prog->count - 1; - - /* ---- store the 16x8 tile (as the looped kernel) ---- */ - BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_tc1, .src1_vreg = v_c1, .src2_vreg = -1, .imm = 4, .src3_vreg = v_pc0); - BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_tc3, .src1_vreg = v_c1, .src2_vreg = -1, .imm = 4, .src3_vreg = v_pc1); - BW_APPEND(prog, .op = BW_IR_STG_E, .src1_vreg = v_pc0, .src2_vreg = v_rd, .src2_subreg = 0, .src3_vreg = -1, .ureg = uv_desc, .control = CTRL_STG_RDBAR3); - BW_APPEND(prog, .op = BW_IR_STG_E, .src1_vreg = v_tc1, .src2_vreg = v_rd, .src2_subreg = 1, .src3_vreg = -1, .ureg = uv_desc, .control = CTRL_STG_RDBAR3); - BW_APPEND(prog, .op = BW_IR_STG_E, .src1_vreg = v_pc1, .src2_vreg = v_rd, .src2_subreg = 2, .src3_vreg = -1, .ureg = uv_desc, .control = CTRL_STG_RDBAR3); - BW_APPEND(prog, .op = BW_IR_STG_E, .src1_vreg = v_tc3, .src2_vreg = v_rd, .src2_subreg = 3, .src3_vreg = -1, .ureg = uv_desc, .control = CTRL_STG_RDBAR3); - /* next column tile: col += grid_x ; C by grid_x*8 f32 (waits SB3 first) ; B by grid_x tiles of KS*256 bytes */ - BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_col, .src1_vreg = v_c1, .src2_vreg = -1, .imm = grid_x, .src3_vreg = v_col); - BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pc0, .src1_vreg = v_c1, .src2_vreg = -1, .imm = grid_x * 32u, .src3_vreg = v_pc0, .control = CTRL_IMADW_WAIT3); - BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pc1, .src1_vreg = v_c1, .src2_vreg = -1, .imm = grid_x * 32u, .src3_vreg = v_pc1); - BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pbcol, .src1_vreg = v_c1, .src2_vreg = -1, .imm = grid_x * KS * 256u, .src3_vreg = v_pbcol); - BW_APPEND(prog, .op = BW_IR_ISETP_GE, .dst_vreg = -1, .src1_vreg = v_col, .src2_vreg = -1, .imm = NT, .src3_vreg = -1); - { - int here = (int)prog->count; - BW_APPEND(prog, .op = BW_IR_BRA, .imm = (uint32_t)(int32_t)(L_col - here), .predicate_p0 = true, .predicate_not = true); - } - const int L_col_end = (int)prog->count - 1; - BW_APPEND(prog, .op = BW_IR_EXIT); - BW_APPEND(prog, .op = BW_IR_BRA); - - /* Loop-carried values live to the end of their loop (the allocator knows no back edges). - * Only these are read in a later pass than the one that wrote them; prologue temporaries - * are dead before the first loop and may be reused. K-loop temporaries (fragments) are - * kept to the end of the pass, as in the looped kernel. */ - const int col_carried[] = { v_c1, v_col, v_pa, v_pbcol, v_pc0, v_pc1, v_rd }; - for (size_t i = 0; i < sizeof col_carried / sizeof col_carried[0]; i++) bw_live_until(prog, col_carried[i], L_col_end); - const int k_carried[] = { v_pac, v_pbc, v_cnt }; - for (size_t i = 0; i < sizeof k_carried / sizeof k_carried[0]; i++) bw_live_until(prog, k_carried[i], L_k_end); - bw_keep_alive(prog, L_k, L_k_end, L_k_end); - return omega_bw_regalloc_solve(prog); -} - -#ifndef OMEGA_GPU_NO_POSIX -int omega_blackwell_codegen_matmul_frag(const OmegaMatMulSpec *spec, uint32_t grid_x, uint32_t unroll, - int mutant, OmegaBlackwellKernel *kernel) { - if (!spec || !kernel) return -1; - BlackwellIRProgram prog; - if (omega_blackwell_codegen_matmul_frag_prog(spec, grid_x, unroll, mutant, &prog) != 0) return -1; - return bw_kernel_alloc_encode(&prog, OMEGA_BW_CODE_CAP_SMALL, kernel); -} -#endif - -/* Capped at 4: unroll 8 (72 registers, 16 loads per pass) never completed on the GB10, with all - * loads on SB2 and again with A on SB2 and B on SB4 (2026-10-07 07:50Z and 07:54Z, omega #328), - * while unroll 4 (48 registers) ran every Qwen3 decode shape correctly. Cause UNKNOWN; the next - * experiment is the unroll-4 kernel launched with 72 declared registers (one variable). */ -/* ---- 2026-10-12: row/column-blocked fragment kernel (prefill rows) ------------------------ - * - * The kernel above gives one warp one 16x8 output tile: every B fragment is streamed once per row - * tile and every A fragment once per column tile, so at m = 128 on GB10 the three Qwen3-8B prefill - * shapes move about 4.7 GB per call at the DRAM rate (128x4096x24576: 20.9 ms, 2026-10-12 baseline). - * Here one warp owns RT row tiles x CT column tiles (RT*CT accumulators) over all of K: per K step - * it loads RT A fragments and CT B fragments and runs RT*CT HMMAs, so each loaded fragment feeds - * RT (B) or CT (A) tensor ops. Same packing, grid rules and C layout as the kernel above: the A - * fragments of row tile i sit at +i*KS*512 bytes, the B fragments of column tile j at +j*KS*256, - * both immediate offsets of the same cursors; the C tile (i, j) is at +i*16*N*4 + j*32 bytes from - * the two row pointers (STG.E immediate offsets, fixtures in omega_blackwell_verify_codegen_fixtures_fragmm). - * Loads per pass stay at or under 8 (unroll*(RT+CT)), the count the proven unroll-4 kernel issues; - * unroll 8 of that kernel (16 loads per pass) never completed on the GB10 and the cause is UNKNOWN, - * so this kernel does not go past that envelope. The HMMA chain keeps the dependent-chain control - * words (stall 15 then NOP stall 14) although the RT*CT accumulators are independent: a longer stall - * is never wrong, and the loads dominate the pass. CTA = one warp, as above; a grid of - * (grid_x, row blocks); column blocks advance by grid_x per pass of the column loop. */ -/* The C store offset of the last tile (rt-1, ct-1) is a signed 24-bit STG immediate; for wide N (lm_head, - * 151936 columns) rt > 1 does not fit. Shared by the generator below and omega_matmul_frag_block_shape. */ -static int frag_block_store_fits(uint32_t rt, uint32_t ct, uint32_t n) { - return (uint64_t)(rt - 1) * 16u * n * 4u + (uint64_t)(ct - 1) * 32u + 4u < (1ull << 23); -} - -int omega_blackwell_codegen_matmul_frag_block_prog(const OmegaMatMulSpec *spec, uint32_t grid_x, uint32_t unroll, - uint32_t rt, uint32_t ct, int mutant, BlackwellIRProgram *prog) { - if (!spec || !prog) return -1; - if (spec->m == 0 || spec->k == 0 || spec->n == 0) return -1; - if (spec->precision != OMEGA_MATMUL_PRECISION_FP16 && spec->precision != OMEGA_MATMUL_PRECISION_BF16) return -1; - if (spec->m % 16 != 0 || spec->n % 8 != 0 || spec->k % 16 != 0) return -1; - if (rt != 1 && rt != 2 && rt != 4 && rt != 8) return -1; - if (ct != 1 && ct != 2 && ct != 4) return -1; - if (rt * ct > 16 || rt * ct < 2) return -1; /* 1x1 is the kernel above */ - if (unroll != 1 && unroll != 2) return -1; - if (unroll * (rt + ct) > 8) return -1; - const uint32_t K = spec->k, N = spec->n, NT = N / 8, KS = K / 16; - if (KS % unroll != 0 || NT % ct != 0) return -1; - const uint32_t ITERS = KS / unroll, NB = NT / ct; - if (grid_x == 0 || grid_x > NB) return -1; - if (mutant && ITERS < 2) return -1; - /* every index and immediate below must fit: 32 bits for the index arithmetic, signed 24 bits for - * the load/store offsets (rows up to OMEGA_BW_MATMUL_MAX_M) */ - if ((uint64_t)NT * KS * 32u > 0xffffffffull || (uint64_t)(OMEGA_BW_MATMUL_MAX_M / 16u) * KS * 32u > 0xffffffffull || - (uint64_t)grid_x * ct * KS * 256u > 0xffffffffull || (uint64_t)N * 32u > 0xffffffffull || - (uint64_t)OMEGA_BW_MATMUL_MAX_M * N > 0xffffffffull) - return -1; - if ((uint64_t)(rt - 1) * KS * 512u + (unroll - 1) * 512u >= (1ull << 23) || - (uint64_t)(ct - 1) * KS * 256u + (unroll - 1) * 256u >= (1ull << 23) || - !frag_block_store_fits(rt, ct, N)) - return -1; - - omega_bw_ir_init(prog); - int v_ptr_a = omega_bw_ir_alloc_vreg64(prog), v_ptr_b = omega_bw_ir_alloc_vreg64(prog), v_ptr_c = omega_bw_ir_alloc_vreg64(prog); - int uv_desc = omega_bw_ir_alloc_uvreg64(prog); - int v_tid = omega_bw_ir_alloc_vreg(prog), v_ctaid_x = omega_bw_ir_alloc_vreg(prog), v_ctaid_y = omega_bw_ir_alloc_vreg(prog); - int v_group = omega_bw_ir_alloc_vreg(prog), v_tig = omega_bw_ir_alloc_vreg(prog), v_tcol2 = omega_bw_ir_alloc_vreg(prog); - int v_r0 = omega_bw_ir_alloc_vreg(prog), v_r1 = omega_bw_ir_alloc_vreg(prog), v_c1 = omega_bw_ir_alloc_vreg(prog); - int v_idx = omega_bw_ir_alloc_vreg(prog), v_col = omega_bw_ir_alloc_vreg(prog), v_cnt = omega_bw_ir_alloc_vreg(prog); - int v_pa = omega_bw_ir_alloc_vreg64(prog), v_pbcol = omega_bw_ir_alloc_vreg64(prog); - int v_pc0 = omega_bw_ir_alloc_vreg64(prog), v_pc1 = omega_bw_ir_alloc_vreg64(prog); - int v_pac = omega_bw_ir_alloc_vreg64(prog), v_pbc = omega_bw_ir_alloc_vreg64(prog); - int v_rd[16], v_ra[2][8], v_rb[2][4]; - for (uint32_t t = 0; t < rt * ct; t++) v_rd[t] = omega_bw_ir_alloc_vreg128(prog); - for (uint32_t u = 0; u < unroll; u++) { - for (uint32_t i = 0; i < rt; i++) v_ra[u][i] = omega_bw_ir_alloc_vreg128(prog); - for (uint32_t j = 0; j < ct; j++) v_rb[u][j] = omega_bw_ir_alloc_vreg64(prog); - } - if (v_rb[unroll - 1][ct - 1] < 0 || v_ra[unroll - 1][rt - 1] < 0 || v_rd[rt * ct - 1] < 0 || uv_desc < 0) return -1; - - const uint32_t CTRL_STG_RDBAR3 = 0x0007e200u, CTRL_IMADW_WAIT3 = 0x009fcc00u, CTRL_MOVRZ_WAIT3 = 0x008fe200u; - - /* prologue: descriptor, pointers, ids (as the kernel above; a CTA's row block is rt row tiles) */ - BW_APPEND(prog, .op = BW_IR_LDCU64, .dst_vreg = uv_desc, .imm = 0x358, .is_uniform = true); - BW_APPEND(prog, .op = BW_IR_LDC64, .dst_vreg = v_ptr_a, .imm = 0x380); - BW_APPEND(prog, .op = BW_IR_LDC64, .dst_vreg = v_ptr_b, .imm = 0x388); - BW_APPEND(prog, .op = BW_IR_LDC64, .dst_vreg = v_ptr_c, .imm = 0x390); - BW_APPEND(prog, .op = BW_IR_S2R, .dst_vreg = v_tid, .imm = BW_SR_TID_X); - BW_APPEND(prog, .op = BW_IR_S2R, .dst_vreg = v_ctaid_x, .imm = BW_SR_CTAID_X); - BW_APPEND(prog, .op = BW_IR_S2R, .dst_vreg = v_ctaid_y, .imm = BW_SR_CTAID_Y); - BW_APPEND(prog, .op = BW_IR_MOV_IMM, .dst_vreg = v_c1, .imm = 1); - BW_APPEND(prog, .op = BW_IR_SHF_R, .dst_vreg = v_group, .src1_vreg = v_tid, .imm = 2); - BW_APPEND(prog, .op = BW_IR_LOP3_AND, .dst_vreg = v_tig, .src1_vreg = v_tid, .imm = 3); - BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_tcol2, .src1_vreg = v_tig, .src2_vreg = -1, .imm = 2, .src3_vreg = -1); - /* r0 = ctaid_y * 16 * rt + group: row of this lane in row tile 0 of the block; r1 = r0 + 8 */ - BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_r0, .src1_vreg = v_ctaid_y, .src2_vreg = -1, .imm = 16u * rt, .src3_vreg = v_group); - BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_r1, .src1_vreg = v_c1, .src2_vreg = -1, .imm = 8, .src3_vreg = v_r0); - /* pa = A + (ctaid_y * rt * KS * 32 + tid) * 16: row tile 0 of this block, this lane's 16 bytes of step 0 */ - BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_ctaid_y, .src2_vreg = -1, .imm = rt * KS * 32u, .src3_vreg = v_tid); - BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pa, .src1_vreg = v_idx, .src2_vreg = -1, .imm = 16, .src3_vreg = v_ptr_a); - /* col = ctaid_x * ct: first column tile of this CTA's first column block */ - BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_col, .src1_vreg = v_ctaid_x, .src2_vreg = -1, .imm = ct, .src3_vreg = -1); - /* pbcol = B + (col * KS * 32 + tid) * 8: column tile col, this lane's 8 bytes of step 0 */ - BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_col, .src2_vreg = -1, .imm = KS * 32u, .src3_vreg = v_tid); - BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pbcol, .src1_vreg = v_idx, .src2_vreg = -1, .imm = 8, .src3_vreg = v_ptr_b); - /* pc0 = C + (r0*N + col*8 + tcol2)*4 ; pc1 with r1 */ - BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_r0, .src2_vreg = -1, .imm = N, .src3_vreg = v_tcol2); - BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_col, .src2_vreg = -1, .imm = 8, .src3_vreg = v_idx); - BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pc0, .src1_vreg = v_idx, .src2_vreg = -1, .imm = 4, .src3_vreg = v_ptr_c); - BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_r1, .src2_vreg = -1, .imm = N, .src3_vreg = v_tcol2); - BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_col, .src2_vreg = -1, .imm = 8, .src3_vreg = v_idx); - BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pc1, .src1_vreg = v_idx, .src2_vreg = -1, .imm = 4, .src3_vreg = v_ptr_c); - - /* ---- column-block loop: zero the rt*ct accumulators (the first MOV waits SB3: the previous - * block's stores have read them), rewind the K cursors ---- */ - const int L_col = (int)prog->count; - for (uint32_t t = 0; t < rt * ct; t++) - for (uint32_t s = 0; s < 4; s++) - BW_APPEND(prog, .op = BW_IR_MOV_RZ, .dst_vreg = v_rd[t], .dst_subreg = (int)s, - .control = (t == 0 && s == 0) ? CTRL_MOVRZ_WAIT3 : 0); - BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pac, .src1_vreg = v_c1, .src2_vreg = -1, .imm = 0, .src3_vreg = v_pa); - BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pbc, .src1_vreg = v_c1, .src2_vreg = -1, .imm = 0, .src3_vreg = v_pbcol); - BW_APPEND(prog, .op = BW_IR_MOV_IMM, .dst_vreg = v_cnt, .imm = mutant ? ITERS - 1 : ITERS); - - /* ---- K loop: `unroll` steps of (rt A + ct B) loads, then the rt*ct*unroll HMMAs ---- */ - const int L_k = (int)prog->count; - for (uint32_t u = 0; u < unroll; u++) { - for (uint32_t i = 0; i < rt; i++) - BW_APPEND(prog, .op = BW_IR_LDG_E_128, .dst_vreg = v_ra[u][i], .src1_vreg = v_pac, .src2_vreg = -1, .src3_vreg = -1, - .ureg = uv_desc, .imm = u * 512u + i * KS * 512u, .control = BW_FRAG_CTRL_LDG_A); - for (uint32_t j = 0; j < ct; j++) - BW_APPEND(prog, .op = BW_IR_LDG_E_64, .dst_vreg = v_rb[u][j], .src1_vreg = v_pbc, .src2_vreg = -1, .src3_vreg = -1, - .ureg = uv_desc, .imm = u * 256u + j * KS * 256u, .control = BW_FRAG_CTRL_LDG_B); - } - const BlackwellIROpcode hmma = (spec->precision == OMEGA_MATMUL_PRECISION_BF16) ? BW_IR_HMMA_BF16 : BW_IR_HMMA_F16; - for (uint32_t u = 0; u < unroll; u++) - for (uint32_t i = 0; i < rt; i++) - for (uint32_t j = 0; j < ct; j++) { - const int rd = v_rd[i * ct + j]; - BW_APPEND(prog, .op = hmma, .dst_vreg = rd, .src1_vreg = v_ra[u][i], .src2_vreg = v_rb[u][j], .src3_vreg = rd, - .control = BW_FRAG_CTRL_HMMA); - BW_APPEND(prog, .op = BW_IR_NOP, .dst_vreg = -1, .src1_vreg = -1, .src2_vreg = -1, .src3_vreg = -1, .ureg = -1, - .control = BW_FRAG_CTRL_NOP); - } - BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pac, .src1_vreg = v_c1, .src2_vreg = -1, .imm = unroll * 512u, .src3_vreg = v_pac); - BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pbc, .src1_vreg = v_c1, .src2_vreg = -1, .imm = unroll * 256u, .src3_vreg = v_pbc); - BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_cnt, .src1_vreg = v_c1, .src2_vreg = -1, .imm = 0xffffffffu, .src3_vreg = v_cnt); - BW_APPEND(prog, .op = BW_IR_ISETP_GE, .dst_vreg = -1, .src1_vreg = v_cnt, .src2_vreg = -1, .imm = 1, .src3_vreg = -1); - { - int here = (int)prog->count; - BW_APPEND(prog, .op = BW_IR_BRA, .imm = (uint32_t)(int32_t)(L_k - here), .predicate_p0 = true); - } - const int L_k_end = (int)prog->count - 1; - - /* ---- store the rt*ct tiles: tile (i, j) at +i*16*N*4 + j*32 bytes from pc0 (rows r0) and pc1 (rows r1) ---- */ - for (uint32_t i = 0; i < rt; i++) - for (uint32_t j = 0; j < ct; j++) { - const int rd = v_rd[i * ct + j]; - const uint32_t off = i * 16u * N * 4u + j * 32u; - BW_APPEND(prog, .op = BW_IR_STG_E, .src1_vreg = v_pc0, .src2_vreg = rd, .src2_subreg = 0, .src3_vreg = -1, .ureg = uv_desc, .imm = off, .control = CTRL_STG_RDBAR3); - BW_APPEND(prog, .op = BW_IR_STG_E, .src1_vreg = v_pc0, .src2_vreg = rd, .src2_subreg = 1, .src3_vreg = -1, .ureg = uv_desc, .imm = off + 4u, .control = CTRL_STG_RDBAR3); - BW_APPEND(prog, .op = BW_IR_STG_E, .src1_vreg = v_pc1, .src2_vreg = rd, .src2_subreg = 2, .src3_vreg = -1, .ureg = uv_desc, .imm = off, .control = CTRL_STG_RDBAR3); - BW_APPEND(prog, .op = BW_IR_STG_E, .src1_vreg = v_pc1, .src2_vreg = rd, .src2_subreg = 3, .src3_vreg = -1, .ureg = uv_desc, .imm = off + 4u, .control = CTRL_STG_RDBAR3); - } - /* next column block: col += grid_x*ct ; C by grid_x*ct*8 f32 (waits SB3 first) ; B by grid_x*ct tiles of KS*256 bytes */ - BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_col, .src1_vreg = v_c1, .src2_vreg = -1, .imm = grid_x * ct, .src3_vreg = v_col); - BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pc0, .src1_vreg = v_c1, .src2_vreg = -1, .imm = grid_x * ct * 32u, .src3_vreg = v_pc0, .control = CTRL_IMADW_WAIT3); - BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pc1, .src1_vreg = v_c1, .src2_vreg = -1, .imm = grid_x * ct * 32u, .src3_vreg = v_pc1); - BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pbcol, .src1_vreg = v_c1, .src2_vreg = -1, .imm = grid_x * ct * KS * 256u, .src3_vreg = v_pbcol); - BW_APPEND(prog, .op = BW_IR_ISETP_GE, .dst_vreg = -1, .src1_vreg = v_col, .src2_vreg = -1, .imm = NT, .src3_vreg = -1); - { - int here = (int)prog->count; - BW_APPEND(prog, .op = BW_IR_BRA, .imm = (uint32_t)(int32_t)(L_col - here), .predicate_p0 = true, .predicate_not = true); - } - const int L_col_end = (int)prog->count - 1; - BW_APPEND(prog, .op = BW_IR_EXIT); - BW_APPEND(prog, .op = BW_IR_BRA); - - /* loop-carried values live to the end of their loop (as the kernel above) */ - const int col_carried[] = { v_c1, v_col, v_pa, v_pbcol, v_pc0, v_pc1 }; - for (size_t i = 0; i < sizeof col_carried / sizeof col_carried[0]; i++) bw_live_until(prog, col_carried[i], L_col_end); - for (uint32_t t = 0; t < rt * ct; t++) bw_live_until(prog, v_rd[t], L_col_end); - const int k_carried[] = { v_pac, v_pbc, v_cnt }; - for (size_t i = 0; i < sizeof k_carried / sizeof k_carried[0]; i++) bw_live_until(prog, k_carried[i], L_k_end); - bw_keep_alive(prog, L_k, L_k_end, L_k_end); - return omega_bw_regalloc_solve(prog); -} - -#ifndef OMEGA_GPU_NO_POSIX -int omega_blackwell_codegen_matmul_frag_block(const OmegaMatMulSpec *spec, uint32_t grid_x, uint32_t unroll, - uint32_t rt, uint32_t ct, int mutant, OmegaBlackwellKernel *kernel) { - if (!spec || !kernel) return -1; - BlackwellIRProgram prog; - if (omega_blackwell_codegen_matmul_frag_block_prog(spec, grid_x, unroll, rt, ct, mutant, &prog) != 0) return -1; - return bw_kernel_alloc_encode(&prog, OMEGA_BW_CODE_CAP_MAX, kernel); -} -#endif - -/* The row and column tiles one warp owns in the blocked kernel: the largest of 4, 2, 1 dividing the - * row tiles (rt), and for rt > 1 the largest of 4, 2, 1 dividing the column tiles (ct); rt = ct = 1 - * is the single-tile kernel (decode rows). An rt whose last C tile's store offset does not fit the - * 24-bit immediate (frag_block_store_fits: wide N such as lm_head) steps down, to 1 at the end. */ -void omega_matmul_frag_block_shape(uint32_t mt, uint32_t nt, uint32_t *rt, uint32_t *ct) { - uint32_t r = mt % 4u == 0 ? 4u : mt % 2u == 0 ? 2u : 1u, c = 1u; - for (; r > 1; r /= 2u) { - c = nt % 4u == 0 ? 4u : nt % 2u == 0 ? 2u : 1u; - if (frag_block_store_fits(r, c, nt * 8u)) break; - } - *rt = r; - *ct = r > 1 ? c : 1u; -} - -/* loads per pass at or under 8 (see the kernel comment): unroll 2 only when 2*(rt+ct) fits and KS is even */ -uint32_t omega_matmul_frag_block_unroll(uint32_t kp, uint32_t rt, uint32_t ct) { - const uint32_t ks = kp / 16u; - return (ks % 2u == 0 && 2u * (rt + ct) <= 8u) ? 2u : 1u; -} - -uint32_t omega_matmul_frag_unroll(uint32_t kp) { - const uint32_t ks = kp / 16u; - return ks % 4u == 0 ? 4u : ks % 2u == 0 ? 2u : 1u; -} - -/* A block (mt, ks) holds rows 16mt..16mt+15 and columns 16ks..16ks+15 as 256 bf16 at element - * (mt*KS + ks)*256; inside the block, element (rr, cc) sits at lane*8 + e with lane = (rr%8)*4 + (cc%8)/2 - * and e = (cc/8)*4 + (rr/8)*2 + cc%2 (the HMMA.16816 A fragment order, header comment). The 256-entry - * table makes the pack a tile loop with no per-element division (2026-10-12: the per-element form with - * six divisions and a conversion call per element was ~2.5 ms per MB of activations at m = 128). */ -static uint16_t g_frag_a_tab[256]; -static void frag_a_tab_init(void) { - if (g_frag_a_tab[255]) return; - for (uint32_t rr = 0; rr < 16; rr++) - for (uint32_t cc = 0; cc < 16; cc++) - g_frag_a_tab[rr * 16 + cc] = (uint16_t)((((rr & 7u) * 4u + ((cc & 7u) >> 1)) * 8u) + (cc >> 3) * 4u + (rr >> 3) * 2u + (cc & 1u)); -} -/* bit for bit what the compiler's (__bf16)f cast gives (gpu_matmul_api_test checks it against - * omega_fp32_to_bf16 on ties, denormals, inf and NaN payloads): round to nearest even, a NaN keeps - * its top payload bits and is made quiet. gcc 13 on aarch64 emits the cast as a call to the - * soft-float routine __truncsfbf2 per element, which was most of the packing cost. */ -static inline uint16_t frag_f32_to_bf16(float f) { - uint32_t u; - memcpy(&u, &f, 4); - if ((u & 0x7f800000u) == 0x7f800000u) return (uint16_t)((u >> 16) | ((u & 0x007fffffu) ? 0x0040u : 0u)); - return (uint16_t)((u + 0x7fffu + ((u >> 16) & 1u)) >> 16); -} -#define FRAG_PACK_A_BODY(LOAD) \ - do { \ - const uint32_t KS = kp / 16u; \ - frag_a_tab_init(); \ - memset(dst, 0, (size_t)mp * kp * sizeof *dst); \ - for (uint32_t mt = 0; mt < (m + 15u) / 16u; mt++) { \ - const uint32_t r1 = m - mt * 16u < 16u ? m - mt * 16u : 16u; \ - for (uint32_t ks = 0; ks < (k + 15u) / 16u; ks++) { \ - const uint32_t c1 = k - ks * 16u < 16u ? k - ks * 16u : 16u; \ - uint16_t *blk = dst + ((size_t)mt * KS + ks) * 256u; \ - for (uint32_t rr = 0; rr < r1; rr++) { \ - const uint16_t *tab = g_frag_a_tab + rr * 16u; \ - const size_t src = (size_t)(mt * 16u + rr) * k + ks * 16u; \ - for (uint32_t cc = 0; cc < c1; cc++) blk[tab[cc]] = LOAD(a[src + cc]); \ - } \ - } \ - } \ - } while (0) -#define FRAG_ID(x) (x) -void omega_matmul_frag_pack_a(const uint16_t *a, uint32_t m, uint32_t k, uint32_t mp, uint32_t kp, uint16_t *dst) { - FRAG_PACK_A_BODY(FRAG_ID); -} -void omega_matmul_frag_pack_a_f32(const float *a, uint32_t m, uint32_t k, uint32_t mp, uint32_t kp, uint16_t *dst) { - FRAG_PACK_A_BODY(frag_f32_to_bf16); -} - -/* Row 0 only, every column of every K block (zeros past k): for a one-row call whose buffer already - * holds zeros in rows 1..15 of every block from an earlier full pack of the same width. */ -#define FRAG_PACK_A_ROW0_BODY(LOAD) \ - do { \ - const uint32_t KS = kp / 16u; \ - frag_a_tab_init(); \ - const uint16_t *tab = g_frag_a_tab; \ - for (uint32_t ks = 0; ks < KS; ks++) { \ - uint16_t *blk = dst + (size_t)ks * 256u; \ - const uint32_t c0 = ks * 16u, c1 = k - c0 < 16u ? k - c0 : 16u; \ - for (uint32_t cc = 0; cc < c1; cc++) blk[tab[cc]] = LOAD(a[c0 + cc]); \ - for (uint32_t cc = c1; cc < 16u; cc++) blk[tab[cc]] = 0; \ - } \ - } while (0) -void omega_matmul_frag_pack_a_row0(const uint16_t *a, uint32_t k, uint32_t kp, uint16_t *dst) { - FRAG_PACK_A_ROW0_BODY(FRAG_ID); -} -size_t omega_matmul_result_sentinels(float *cd, uint32_t m, uint32_t n, uint32_t np, float poison, int set) { - /* one word per 16 x 8 fragment tile that holds a real output word: the tile's last real row and column */ - size_t count = 0; - if (m == 0 || n == 0) return 0; - const uint32_t mt = (m + 15u) / 16u, nt = (n + 7u) / 8u; - for (uint32_t ti = 0; ti < mt; ti++) { - uint32_t r = ti * 16u + 15u; if (r >= m) r = m - 1u; - float *row = cd + (size_t)r * np; - for (uint32_t tj = 0; tj < nt; tj++) { - uint32_t c = tj * 8u + 7u; if (c >= n) c = n - 1u; - if (set) { row[c] = poison; count++; } - else if (row[c] == poison) count++; - } - } - return count; -} - -void omega_matmul_frag_pack_a_row0_f32(const float *a, uint32_t k, uint32_t kp, uint16_t *dst) { - FRAG_PACK_A_ROW0_BODY(frag_f32_to_bf16); -} - -void omega_matmul_frag_pack_b(const uint16_t *b, uint32_t k, uint32_t n, uint32_t kp, - uint32_t nt0, uint32_t nt1, uint16_t *dst) { - const uint32_t KS = kp / 16u; - memset(dst, 0, (size_t)(nt1 - nt0) * KS * 128u * sizeof *dst); - for (uint32_t row = 0; row < k; row++) { - const uint32_t ks = row / 16u, rr = row % 16u, khi = rr / 8u, tig = (rr % 8u) / 2u, lo = rr % 2u; - const uint16_t *src = &b[(size_t)row * n]; - for (uint32_t nt = nt0; nt < nt1; nt++) { - const uint32_t c0 = nt * 8u; - for (uint32_t group = 0; group < 8u && c0 + group < n; group++) { - const size_t lane = group * 4u + tig; - dst[(((size_t)(nt - nt0) * KS + ks) * 32u + lane) * 4u + khi * 2u + lo] = src[c0 + group]; - } - } - } -} - -int omega_blackwell_verify_codegen_fixtures(void) { - OmegaRegAlloc ra; - memset(&ra, 0, sizeof(ra)); - ra.vreg_to_phys[0] = 2; - ra.vreg_to_phys[1] = 4; - ra.vreg_to_phys[2] = 6; - ra.vreg_to_phys[3] = 7; - ra.uvreg_to_phys[0] = 4; - - uint32_t w[4]; - - /* 1. MOV R7, RZ */ - BlackwellIRInsn insn_mov_rz = { .op = BW_IR_MOV_RZ, .dst_vreg = 3 }; - if (encode_single_insn(&insn_mov_rz, &ra, w) != 0) return -1; - if (w[0] != (0x7202 | (7 << 16)) || w[1] != 0xff || w[2] != 0x00000f00) return -2; - - /* 2. MOV R2, 1 */ - BlackwellIRInsn insn_mov_imm = { .op = BW_IR_MOV_IMM, .dst_vreg = 0, .imm = 1 }; - if (encode_single_insn(&insn_mov_imm, &ra, w) != 0) return -3; - if (w[0] != (0x7802 | (2 << 16)) || w[1] != 1 || w[2] != 0x00000f00) return -4; - - /* 3. Register-Register IMAD: IMAD R7, R2, R6, R7 */ - BlackwellIRInsn insn_imad_rr = { .op = BW_IR_IMAD, .dst_vreg = 3, .src1_vreg = 0, .src2_vreg = 2, .src3_vreg = 3 }; - if (encode_single_insn(&insn_imad_rr, &ra, w) != 0) return -5; - if (w[0] != (0x7224 | (7 << 16) | (2 << 24)) || w[1] != 6 || w[2] != (0x078e0200 | 7)) return -6; - - /* 4. Immediate IMAD: IMAD R6, R2, 16, RZ */ - BlackwellIRInsn insn_imad_imm = { .op = BW_IR_IMAD, .dst_vreg = 2, .src1_vreg = 0, .src2_vreg = -1, .imm = 16, .src3_vreg = -1 }; - if (encode_single_insn(&insn_imad_imm, &ra, w) != 0) return -7; - if (w[0] != (0x7824 | (6 << 16) | (2 << 24)) || w[1] != 16 || w[2] != 0x078e02ff) return -8; - - /* 5. IMAD.WIDE: IMAD.WIDE R4, R2, 4, R4 */ - BlackwellIRInsn insn_imad_wide = { .op = BW_IR_IMAD_WIDE, .dst_vreg = 1, .src1_vreg = 0, .imm = 4, .src3_vreg = 1 }; - if (encode_single_insn(&insn_imad_wide, &ra, w) != 0) return -9; - if (w[0] != (0x7825 | (4 << 16) | (2 << 24)) || w[1] != 4 || w[2] != (0x078e0000 | 4)) return -10; - - /* 6. LDC.64 R4, c[0x0][0x380] */ - BlackwellIRInsn insn_ldc64 = { .op = BW_IR_LDC64, .dst_vreg = 1, .imm = 0x380 }; - if (encode_single_insn(&insn_ldc64, &ra, w) != 0) return -11; - if (w[0] != (0xff007b82 | (4 << 16)) || w[1] != ((0x380 << 6) & 0x00ffff00) || w[2] != 0x00000a00) return -12; - - /* 7. LDCU.64 UR4, c[0x0][0x358] */ - BlackwellIRInsn insn_ldcu64 = { .op = BW_IR_LDCU64, .dst_vreg = 0, .imm = 0x358, .is_uniform = true }; - if (encode_single_insn(&insn_ldcu64, &ra, w) != 0) return -13; - if (w[0] != (0xff0077ac | (4 << 16)) || w[1] != ((0x358 << 5) & 0x00ffff00) || w[2] != 0x08000a00) return -14; - - /* 8. LDG.E R2, desc[UR4][R4.64] */ - BlackwellIRInsn insn_ldg = { .op = BW_IR_LDG_E, .dst_vreg = 0, .src1_vreg = 1, .ureg = 0 }; - if (encode_single_insn(&insn_ldg, &ra, w) != 0) return -15; - if (w[0] != (0x7981 | (2 << 16) | (4 << 24)) || w[1] != 4 || w[2] != 0x0c1e1900) return -16; - - /* 9. STG.E desc[UR4][R4.64], R7 */ - BlackwellIRInsn insn_stg = { .op = BW_IR_STG_E, .src1_vreg = 1, .src2_vreg = 3, .ureg = 0 }; - if (encode_single_insn(&insn_stg, &ra, w) != 0) return -17; - if (w[0] != (0x7986 | (4 << 24)) || w[1] != 7 || w[2] != (0x0c101900 | 4)) return -18; - - /* 10. S2R R2, SR_CTAID.X */ - BlackwellIRInsn insn_s2r = { .op = BW_IR_S2R, .dst_vreg = 0, .imm = BW_SR_CTAID_X }; - if (encode_single_insn(&insn_s2r, &ra, w) != 0) return -19; - if (w[0] != (0x7919 | (2 << 16)) || w[2] != (BW_SR_CTAID_X << 8)) return -20; - - /* 11. HMMA.16816.F32 R4, R4, R2, RZ */ - BlackwellIRInsn insn_hmma_f16 = { .op = BW_IR_HMMA_F16, .dst_vreg = 1, .src1_vreg = 1, .src2_vreg = 0, .src3_vreg = -1 }; - if (encode_single_insn(&insn_hmma_f16, &ra, w) != 0) return -21; - if (w[0] != 0x0404723c || w[1] != 0x00000002 || w[2] != 0x000018ff || w[3] != 0x000fe200) return -22; - - /* 12. HMMA.16816.F32.BF16 R4, R4, R2, RZ */ - BlackwellIRInsn insn_hmma_bf16 = { .op = BW_IR_HMMA_BF16, .dst_vreg = 1, .src1_vreg = 1, .src2_vreg = 0, .src3_vreg = -1 }; - uint32_t w_bf16[4]; - if (encode_single_insn(&insn_hmma_bf16, &ra, w_bf16) != 0) return -23; - if (w_bf16[0] != 0x0404723c || w_bf16[1] != 0x00000002 || w_bf16[2] != 0x000418ff || w_bf16[3] != 0x000fe200) return -24; - - /* 13. Differential Invariant: w[2] bit 18 toggles strictly between FP16 and BF16 */ - if ((w[2] ^ w_bf16[2]) != 0x00040000) return -25; - if (w[0] != w_bf16[0] || w[1] != w_bf16[1] || w[3] != w_bf16[3]) return -26; - - /* 14. SHF.R.U32.HI R4, RZ, 2, R2 */ - BlackwellIRInsn insn_shf = { .op = BW_IR_SHF_R, .dst_vreg = 1, .src1_vreg = 0, .imm = 2 }; - if (encode_single_insn(&insn_shf, &ra, w) != 0) return -27; - if (w[0] != 0xff047819 || w[1] != 2 || w[2] != 0x00011602 || w[3] != 0x001fca00) return -28; - - /* 15. LOP3.LUT R4, R2, 3, RZ, 0xc0, !PT */ - BlackwellIRInsn insn_lop3 = { .op = BW_IR_LOP3_AND, .dst_vreg = 1, .src1_vreg = 0, .imm = 3 }; - if (encode_single_insn(&insn_lop3, &ra, w) != 0) return -29; - if (w[0] != 0x02047812 || w[1] != 3 || w[2] != 0x078ec0ff || w[3] != 0x001fca00) return -30; - - /* 16. LDG.E.U16 R2, desc[UR4][R4.64] */ - BlackwellIRInsn insn_ldg16 = { .op = BW_IR_LDG_E_U16, .dst_vreg = 0, .src1_vreg = 1, .ureg = 0 }; - if (encode_single_insn(&insn_ldg16, &ra, w) != 0) return -31; - if (w[0] != 0x04027981 || w[1] != 4 || w[2] != 0x0c1e1500 || w[3] != 0x000f2200) return -32; - - /* A zero-delta branch stays the self-branch the matmul kernels already emit. */ - BlackwellIRInsn insn_bra0 = { .op = BW_IR_BRA }; - if (encode_single_insn(&insn_bra0, &ra, w) != 0) return -33; - if (w[0] != 0x00fc7947 || w[1] != 0xfffffffc || w[2] != 0x0383ffff || w[3] != 0x000fc000) return -34; - - BlackwellIRInsn insn_ge = { .op = BW_IR_ISETP_GE_U32, .src1_vreg = 1, .src2_vreg = 2 }; - if (encode_single_insn(&insn_ge, &ra, w) != 0) return -35; - if (w[0] != (0x720c | (4 << 24)) || w[1] != 6 || w[2] != 0x03f06070) return -36; - - BlackwellIRInsn insn_xor = { .op = BW_IR_LOP3_XOR, .dst_vreg = 1, .src1_vreg = 0, .src2_vreg = 2 }; - if (encode_single_insn(&insn_xor, &ra, w) != 0) return -37; - if (w[0] != (0x7212 | (4 << 16) | (2 << 24)) || w[1] != 6 || w[2] != 0x078e3cff) return -38; - - BlackwellIRInsn insn_ldg_ss = { .op = BW_IR_LDG_STRONG_SYS, .dst_vreg = 0, .src1_vreg = 1, .ureg = 0 }; - if (encode_single_insn(&insn_ldg_ss, &ra, w) != 0) return -39; - if (w[0] != (0x7981 | (2 << 16) | (4 << 24)) || w[1] != 4 || w[2] != 0x0c1f5900) return -40; - /* 17. FADD R7, R2, R4 */ - BlackwellIRInsn insn_fadd = { .op = BW_IR_FADD, .dst_vreg = 3, .src1_vreg = 0, .src2_vreg = 1 }; - if (encode_single_insn(&insn_fadd, &ra, w) != 0) return -133; - if (w[0] != 0x02077221 || w[1] != 4 || w[2] != 0 || w[3] != 0x004fc400) return -134; - - /* 18. FSUB R7, R2, R4 (negated src2) */ - BlackwellIRInsn insn_fsub = { .op = BW_IR_FSUB, .dst_vreg = 3, .src1_vreg = 0, .src2_vreg = 1 }; - if (encode_single_insn(&insn_fsub, &ra, w) != 0) return -135; - if (w[0] != 0x02077221 || w[1] != 0x80000004 || w[2] != 0 || w[3] != 0x004fc400) return -136; - - /* 19. FMUL R7, R2, R4 */ - BlackwellIRInsn insn_fmul = { .op = BW_IR_FMUL, .dst_vreg = 3, .src1_vreg = 0, .src2_vreg = 1 }; - if (encode_single_insn(&insn_fmul, &ra, w) != 0) return -137; - if (w[0] != 0x02077220 || w[1] != 4 || w[2] != 0x00400000 || w[3] != 0x004fc400) return -138; - - /* 20. FFMA R7, R2, R4, R6 */ - BlackwellIRInsn insn_ffma = { .op = BW_IR_FFMA, .dst_vreg = 3, .src1_vreg = 0, .src2_vreg = 1, .src3_vreg = 2 }; - if (encode_single_insn(&insn_ffma, &ra, w) != 0) return -139; - if (w[0] != 0x02077223 || w[1] != 4 || w[2] != 6 || w[3] != 0x004fc400) return -140; - - /* 21. FMNMX_MIN R7, R2, R4 */ - BlackwellIRInsn insn_min = { .op = BW_IR_FMNMX_MIN, .dst_vreg = 3, .src1_vreg = 0, .src2_vreg = 1 }; - if (encode_single_insn(&insn_min, &ra, w) != 0) return -141; - if (w[0] != 0x02077209 || w[1] != 4 || w[2] != 0x03800000 || w[3] != 0x004fc400) return -142; - - /* 22. FMNMX_MAX R7, R2, R4 */ - BlackwellIRInsn insn_max = { .op = BW_IR_FMNMX_MAX, .dst_vreg = 3, .src1_vreg = 0, .src2_vreg = 1 }; - if (encode_single_insn(&insn_max, &ra, w) != 0) return -143; - if (w[0] != 0x02077209 || w[1] != 4 || w[2] != 0x07800000 || w[3] != 0x004fc400) return -144; - - /* 23. I2FP R7, R2 */ - BlackwellIRInsn insn_i2f = { .op = BW_IR_I2FP, .dst_vreg = 3, .src1_vreg = 0 }; - if (encode_single_insn(&insn_i2f, &ra, w) != 0) return -145; - if (w[0] != 0x00077245 || w[1] != 2 || w[2] != 0x00201400 || w[3] != 0x004fe200) return -146; - - /* 24. F2I R7, R2 */ - BlackwellIRInsn insn_f2i = { .op = BW_IR_F2I, .dst_vreg = 3, .src1_vreg = 0 }; - if (encode_single_insn(&insn_f2i, &ra, w) != 0) return -147; - if (w[0] != 0x00077305 || w[1] != 2 || w[2] != 0x0020f100 || w[3] != 0x004e2200) return -148; - - /* 25. MUFU_RCP R7, R2 */ - BlackwellIRInsn insn_mufu_rcp = { .op = BW_IR_MUFU_RCP, .dst_vreg = 3, .src1_vreg = 0 }; - if (encode_single_insn(&insn_mufu_rcp, &ra, w) != 0) return -149; - if (w[0] != 0x00077308 || w[1] != 2 || w[2] != 0x00001000 || w[3] != 0x000e2400) return -150; - - /* 26. MUFU_RSQ R7, R2 */ - BlackwellIRInsn insn_mufu_rsq = { .op = BW_IR_MUFU_RSQ, .dst_vreg = 3, .src1_vreg = 0 }; - if (encode_single_insn(&insn_mufu_rsq, &ra, w) != 0) return -151; - if (w[0] != 0x00077308 || w[1] != 2 || w[2] != 0x00001400 || w[3] != 0x000e2400) return -152; - - /* 27. SHFL_DOWN R7, R2, 16 */ - BlackwellIRInsn insn_shfl = { .op = BW_IR_SHFL_DOWN, .dst_vreg = 3, .src1_vreg = 0, .imm = 16 }; - if (encode_single_insn(&insn_shfl, &ra, w) != 0) return -153; - if (w[0] != 0x02077f89 || w[1] != 0x0a001f00 || w[2] != 0x000e0000 || w[3] != 0x000e2400) return -154; - - /* 28. LDS R7, [R2] */ - BlackwellIRInsn insn_lds = { .op = BW_IR_LDS, .dst_vreg = 3, .src1_vreg = 0, .imm = 0 }; - if (encode_single_insn(&insn_lds, &ra, w) != 0) return -155; - if (w[0] != 0x02077984 || w[1] != 0 || w[2] != 0 || w[3] != 0x000fe200) return -156; - - /* 29. STS [R2], R4 */ - BlackwellIRInsn insn_sts = { .op = BW_IR_STS, .src1_vreg = 0, .src2_vreg = 1, .imm = 0 }; - if (encode_single_insn(&insn_sts, &ra, w) != 0) return -157; - if (w[0] != 0x02007388 || w[1] != 4 || w[2] != 0 || w[3] != 0x000fe200) return -158; - - return 0; -} - -int omega_blackwell_test_regalloc_bounds(void) { - BlackwellIRProgram prog; - omega_bw_ir_init(&prog); - - int v0 = omega_bw_ir_alloc_vreg64(&prog); - int v1 = omega_bw_ir_alloc_vreg(&prog); - int v2 = omega_bw_ir_alloc_vreg64(&prog); - int v3 = omega_bw_ir_alloc_vreg128(&prog); /* Test 128-bit quad allocation */ - - BlackwellIRInsn i0 = { .op = BW_IR_LDC64, .dst_vreg = v0, .imm = 0x380 }; - omega_bw_ir_append(&prog, &i0); - BlackwellIRInsn i1 = { .op = BW_IR_MOV_IMM, .dst_vreg = v1, .imm = 10 }; - omega_bw_ir_append(&prog, &i1); - BlackwellIRInsn i2 = { .op = BW_IR_LDC64, .dst_vreg = v2, .imm = 0x388 }; - omega_bw_ir_append(&prog, &i2); - BlackwellIRInsn i3 = { .op = BW_IR_HMMA_F16, .dst_vreg = v3, .src1_vreg = v3, .src2_vreg = v0, .src3_vreg = -1 }; - omega_bw_ir_append(&prog, &i3); - /* i4 uses v1, v2, v3 simultaneously so all intervals are concurrently active */ - int v_sink = omega_bw_ir_alloc_vreg(&prog); - BlackwellIRInsn i4 = { .op = BW_IR_IADD3, .dst_vreg = v_sink, .src1_vreg = v1, .src2_vreg = v2, .src3_vreg = v3 }; - omega_bw_ir_append(&prog, &i4); - - if (omega_bw_regalloc_solve(&prog) != 0) return -1; - - int phys_v0 = prog.regalloc.vreg_to_phys[v0]; - int phys_v1 = prog.regalloc.vreg_to_phys[v1]; - int phys_v2 = prog.regalloc.vreg_to_phys[v2]; - int phys_v3 = prog.regalloc.vreg_to_phys[v3]; - - if (phys_v0 % 2 != 0) return -2; - if (phys_v2 % 2 != 0) return -3; - if (phys_v3 % 4 != 0) return -4; /* 4-alignment for 128-bit quad */ - if (phys_v1 == phys_v0 || phys_v1 == phys_v0 + 1) return -5; - if (phys_v2 == phys_v0 || phys_v2 == phys_v0 + 1) return -6; - for (int k = 0; k < 4; k++) { - if (phys_v3 + k == phys_v0 || phys_v3 + k == phys_v0 + 1) return -7; - if (phys_v3 + k == phys_v2 || phys_v3 + k == phys_v2 + 1) return -8; - if (phys_v3 + k == phys_v1) return -9; - } - - /* Test bounds refusal: intentionally attempt to allocate more registers than hardware capacity */ - BlackwellIRProgram overflow_prog; - omega_bw_ir_init(&overflow_prog); - for (int i = 0; i < BW_PHYS_GPR_MAX + 5; i++) { - int v = omega_bw_ir_alloc_vreg(&overflow_prog); - if (v < 0) break; - BlackwellIRInsn insn = { .op = BW_IR_MOV_IMM, .dst_vreg = v, .imm = (uint32_t)i }; - omega_bw_ir_append(&overflow_prog, &insn); - } - for (size_t i = 0; i < overflow_prog.count; i++) { - for (int v = 0; v < overflow_prog.regalloc.num_vregs; v++) { - overflow_prog.regalloc.intervals[v].last_use = (int)overflow_prog.count - 1; - } - } - if (omega_bw_regalloc_solve(&overflow_prog) == 0) { - return -6; - } - - return 0; -} - -#ifndef OMEGA_GPU_NO_POSIX /* prints: hosted only (the freestanding build has no stdio) */ -int omega_blackwell_test_codegen_variation(void) { - OmegaMatMulSpec spec1, spec2; - if (omega_matmul_spec_init(&spec1, 16, 16, 16, OMEGA_MATMUL_PRECISION_INT32) != 0) { - printf("FAIL: spec1 init failed\n"); - return -1; - } - if (omega_matmul_spec_init(&spec2, 32, 16, 64, OMEGA_MATMUL_PRECISION_INT32) != 0) { - printf("FAIL: spec2 init failed\n"); - return -1; - } - - OmegaBlackwellKernel k1, k2; - memset(&k1, 0, sizeof(k1)); - memset(&k2, 0, sizeof(k2)); - - if (omega_blackwell_codegen_matmul(&spec1, &k1) != 0) { - printf("FAIL: k1 codegen failed\n"); - return -1; - } - if (omega_blackwell_codegen_matmul(&spec2, &k2) != 0) { - printf("FAIL: k2 codegen failed\n"); - omega_blackwell_kernel_free(&k1); - return -1; - } - - OmegaBlackwellRealizationIdentity id1, id2; - omega_blackwell_bind_matmul_realization(&spec1, &k1, &id1); - omega_blackwell_bind_matmul_realization(&spec2, &k2, &id2); - - printf("================================================================================\n"); - printf(" OMEGA BLACKWELL CODEGEN VARIATION DEMONSTRATION (GATE 7 STAGE-1)\n"); - printf("================================================================================\n"); - printf("Config 1: 16x16x16 INT32\n"); - printf(" Instructions: %zu (%zu bytes, %u GPRs, %u UGPRs)\n", k1.insn_count, k1.code_size, k1.gpr_count, k1.uniform_gpr_count); - printf(" Spec ID: "); - for (int i = 0; i < 16; i++) printf("%02x", spec1.spec_id[i]); - printf("...\n"); - printf(" Code Digest: "); - for (int i = 0; i < 16; i++) printf("%02x", k1.code_digest[i]); - printf("...\n"); - printf(" Realization: "); - for (int i = 0; i < 16; i++) printf("%02x", id1.realization_id[i]); - printf("...\n\n"); - - printf("Config 2: 32x16x64 INT32\n"); - printf(" Instructions: %zu (%zu bytes, %u GPRs, %u UGPRs)\n", k2.insn_count, k2.code_size, k2.gpr_count, k2.uniform_gpr_count); - printf(" Spec ID: "); - for (int i = 0; i < 16; i++) printf("%02x", spec2.spec_id[i]); - printf("...\n"); - printf(" Code Digest: "); - for (int i = 0; i < 16; i++) printf("%02x", k2.code_digest[i]); - printf("...\n"); - printf(" Realization: "); - for (int i = 0; i < 16; i++) printf("%02x", id2.realization_id[i]); - printf("...\n"); - printf("================================================================================\n"); - - /* Validation checks */ - if (memcmp(spec1.spec_id, spec2.spec_id, 32) == 0) { - printf("FAIL: spec IDs match (expected distinct)\n"); - return -2; - } - if (memcmp(k1.code_digest, k2.code_digest, 32) == 0) { - printf("FAIL: code digests match (expected distinct)\n"); - return -3; - } - if (memcmp(id1.realization_id, id2.realization_id, 32) == 0) { - printf("FAIL: realization IDs match (expected distinct)\n"); - return -4; - } - - printf("PASS: Dynamic codegen variation verified (distinct specifications, code digests, and realization IDs).\n"); - - omega_blackwell_kernel_free(&k1); - omega_blackwell_kernel_free(&k2); - return 0; -} -#endif - -/* Declared registers: peak + 2 rounded up to 8 (see omega_blackwell_codegen.h for the chip evidence). */ -uint32_t omega_bw_gpr_declared(uint32_t peak) { return ((peak + 2u + 7u) / 8u) * 8u; } -int omega_bw_gpr_declared_ok(uint32_t declared, uint32_t peak) { return declared % 8u == 0u && declared >= peak + 2u; } diff --git a/src/omega_blackwell_codegen.h b/src/omega_blackwell_codegen.h deleted file mode 100644 index cc36e237d..000000000 --- a/src/omega_blackwell_codegen.h +++ /dev/null @@ -1,333 +0,0 @@ -#ifndef OMEGA_BLACKWELL_CODEGEN_H -#define OMEGA_BLACKWELL_CODEGEN_H - -#include -#include -#include -#include "omega_blackwell_matmul.h" - -#define BW_MAX_IR_INSNS 1024 /* cut K: the four-head hd 128 attention kernel is ~700 instructions */ -#define BW_MAX_VREGS 256 -#define BW_MAX_UVREGS 32 -#define BW_PHYS_GPR_START 2 /* Reserve R0, R1 */ -#define BW_PHYS_GPR_MAX 128 -#define BW_PHYS_UGPR_START 4 /* Reserve UR0-UR3 */ -#define BW_PHYS_UGPR_MAX 32 - -/* Minimal Blackwell IR Opcodes for Stage 1 & Stage 2 */ -typedef enum { - BW_IR_NOP = 0, - BW_IR_MOV_RZ, /* MOV Rd, RZ (zero initialization) */ - BW_IR_MOV_IMM, /* MOV Rd, imm32 */ - BW_IR_S2R, /* S2R Rd, SR */ - BW_IR_LDC, /* LDC Rd, c[bank][offset] */ - BW_IR_LDC64, /* LDC.64 Rd:Rd+1, c[bank][offset] */ - BW_IR_LDCU, /* LDCU URd, c[bank][offset] */ - BW_IR_LDCU64, /* LDCU.64 URd:URd+1, c[bank][offset] */ - BW_IR_IMAD, /* IMAD Rd, Ra, Rb/imm, Rc (multiply-add) */ - BW_IR_IMAD_WIDE, /* IMAD.WIDE.U32 Rd:Rd+1, Ra, imm, Rc:Rc+1 */ - BW_IR_ISETP_GE, /* ISETP.GE P0, PT, Ra, Rb/imm, PT */ - BW_IR_LDG_E, /* LDG.E Rd, desc[URd][Ra.64] */ - BW_IR_STG_E, /* STG.E desc[URd][Ra.64], Rb */ - BW_IR_STG_EF, /* STG.E.EF: evict the line so the other side can see it */ - BW_IR_IADD3, /* IADD3 Rd, PT, PT, Ra, Rb, Rc */ - BW_IR_HMMA_F16, /* HMMA.16816.F32 Rd, Ra, Rb, Rc (FP16 input, FP32 accumulator) */ - BW_IR_HMMA_BF16, /* HMMA.16816.F32.BF16 Rd, Ra, Rb, Rc (BF16 input, FP32 accumulator) */ - BW_IR_SHF_R, /* SHF.R.U32.HI Rd, RZ, imm, Ra */ - BW_IR_LOP3_AND, /* LOP3.LUT Rd, Ra, imm, RZ, 0xc0, !PT (Bitwise AND) */ - BW_IR_LDG_E_U16, /* LDG.E.U16 Rd, desc[URd][Ra.64] */ - /* System-scope ordering. Encodings checked against the sm_121 disassembler. - * A host build of these opcodes is not a graphics-chip result. */ - BW_IR_LDG_STRONG_SYS, - BW_IR_LDG_MMIO, /* LDG.E.MMIO.GPU: do not keep a stale cached copy */ - BW_IR_STG_STRONG_SYS, - BW_IR_MEMBAR_ALL_SYS, - BW_IR_MEMBAR_SC_SYS, - BW_IR_CCTL_IVALL, - BW_IR_ATOMG_ADD_STRONG_SYS, - BW_IR_ATOMG_EXCH_STRONG_SYS, - BW_IR_ISETP_GE_U32, /* ISETP.GE.U32.AND P0, PT, Ra, Rb, PT */ - BW_IR_LOP3_XOR, /* LOP3.LUT Rd, Ra, Rb, RZ, 0x3c, !PT */ - BW_IR_FADD, /* FADD Rd, Ra, Rb */ - BW_IR_FSUB, /* FSUB Rd, Ra, Rb (FADD Rd, Ra, -Rb) */ - BW_IR_FMUL, /* FMUL Rd, Ra, Rb */ - BW_IR_FFMA, /* FFMA Rd, Ra, Rb, Rc (Rd = Ra * Rb + Rc) */ - BW_IR_FSETP, /* FSETP.cond.AND P0, PT, Ra, Rb, PT */ - BW_IR_FSEL, /* FSEL Rd, Ra, Rb, P0 */ - BW_IR_FMNMX_MIN, /* FMNMX Rd, Ra, Rb, PT (minimum) */ - BW_IR_FMNMX_MAX, /* FMNMX Rd, Ra, Rb, !PT (maximum) */ - BW_IR_I2FP, /* I2FP.F32.S32 Rd, Ra */ - BW_IR_F2I, /* F2I.TRUNC.NTZ Rd, Ra */ - BW_IR_MUFU_RCP, /* MUFU.RCP Rd, Ra */ - BW_IR_MUFU_RSQ, /* MUFU.RSQ Rd, Ra */ - BW_IR_SHFL_DOWN, /* SHFL.DOWN PT, Rd, Ra, offset, 0x1f */ - BW_IR_LDS, /* LDS Rd, [Ra] */ - BW_IR_STS, /* STS [Ra], Rb */ - BW_IR_EXIT, /* EXIT */ - BW_IR_BRA, /* BRA. imm = signed instruction delta; predicate_p0 / predicate_not select @P0 or @!P0 */ - /* FB-1 cut 4 (2026-10-04), additive. Encodings decoded with nvdisasm 13.0 -b SM121 - * (tools/divsqrt_nvdisasm_check.sh method); the BAR/LDS/STS words are the ones - * src/omega_numeric.c:884-886 runs on the chip (LDS_STS op, 148/148 specs PASS). */ - BW_IR_MUFU_EX2, /* MUFU.EX2 Rd, Ra (2^x, approximate: PTX ex2.approx.f32 bound 2^-22.5 rel) */ - BW_IR_BAR_SYNC, /* BAR.SYNC.DEFER_BLOCKING 0x0 (CTA barrier 0; the QMD declares 1 barrier) */ - BW_IR_LDS32, /* LDS Rd, [Ra+URZ] 32-bit shared load (BW_IR_LDS encodes LDS.U8: byte) */ - BW_IR_STS32, /* STS [Ra+URZ], Rb 32-bit shared store (BW_IR_STS encodes STS.U8: byte) */ - /* Prime race cut (2026-10-05), additive, general integer ops. Words derived from the - * register forms of LOP3_XOR / SHF_R / IMAD and matched word for word against - * nvcc 13.0.88 -arch=sm_121 -cubin + cuobjdump -sass (offline oracle only; see - * omega_blackwell_verify_codegen_fixtures_intops). For these three ops an operand - * vreg of -1 encodes RZ (not R0). */ - BW_IR_LOP3_LUT, /* LOP3.LUT Rd, Ra, Rb, Rc, imm8, !PT: Rd = LUT(Ra, Rb, Rc). imm & 0xff is the - * truth table over Ra = 0xf0, Rb = 0xcc, Rc = 0xaa (a|b = 0xfc, ~(a|b) = 0x03) */ - BW_IR_SHF_L_U32, /* SHF.L.U32 Rd, Ra, Rb, RZ: Rd = Ra << Rb, register amount (the form nvcc emits - * for PTX shl.b32). Amounts >= 32 are NOT chip-verified: callers keep Rb < 32 */ - BW_IR_IMAD_HI_U32, /* IMAD.HI.U32 Rd, Ra, Rb, Rc: Rd = hi32(Ra * Rb) + Rc mod 2^32 (nvcc emits this - * word for __umulhi(a, b) + c) */ - /* Structured warp reconvergence (omega #308, 2026-10-05), additive. Words matched against - * nvcc 13.0.88 -arch=sm_121 output (BSSY.RECONVERGENT / BSYNC.RECONVERGENT; the oracle - * kernels are listed in docs/gpu-reconvergence-308.md) and the Mesa NAK encoder - * (sm70_encode.rs OpBSSy 0x945 / OpBSync 0x941, barrier register at bits 16..19). - * bar_reg selects B0..B15. BSSY arms the barrier for the lanes executing it and names - * the join point; BSYNC waits for every armed lane that has not exited, then the warp - * continues as one. imm of BSSY = signed instruction delta to the join point (the - * instruction after the BSYNC), like BW_IR_BRA; the encoder refuses deltas < 1. */ - BW_IR_BSSY, - BW_IR_BSYNC, - /* Fragment-major matmul (omega #328, 2026-10-07), additive. Words matched against nvcc 13.0.88 - * -arch=sm_121 (omega_blackwell_verify_codegen_fixtures_fragmm). imm = signed byte offset, - * -2^23 .. 2^23-1; dst is a register pair (64) or an aligned quad (128). */ - BW_IR_LDG_E_64, /* LDG.E.64 Rd:Rd+1, desc[URd][Ra.64+imm] */ - BW_IR_LDG_E_128, /* LDG.E.128 Rd..Rd+3, desc[URd][Ra.64+imm] */ - /* Batched attention loads (2026-10-12). Words matched against nvcc 13.0.88 -arch=sm_121 - * (omega_blackwell_verify_codegen_fixtures_fragmm). imm = unsigned byte offset < 2^23; dst an aligned quad. */ - BW_IR_LDS128 /* LDS.128 Rd..Rd+3, [Ra+imm] */ -} BlackwellIROpcode; - -/* Barrier (reconvergence) registers: the sm_121 BSSY/BSYNC words carry a 4-bit barrier id - * (NAK sm70_encode.rs set_bar_dst(16..20)), so B0..B15 are encodable. How many the chip - * provides is not documented to us; chip tests nest to BW_RECONV_CHIP_TESTED_DEPTH. */ -#define BW_RECONV_MAX_BAR 16 - -/* Special Register Identifiers */ -typedef enum { - BW_SR_TID_X = 0x21, - BW_SR_TID_Y = 0x22, - BW_SR_CTAID_X = 0x25, - BW_SR_CTAID_Y = 0x26 -} BlackwellSR; - -/* IR Instruction Node */ -typedef struct { - BlackwellIROpcode op; - int dst_vreg; /* Virtual destination register (-1 if none) */ - int src1_vreg; /* Virtual source 1 register (-1 if none) */ - int src2_vreg; /* Virtual source 2 register (-1 if none / immediate mode) */ - int src3_vreg; /* Virtual source 3 register (-1 if none / RZ) */ - int ureg; /* Uniform register (-1 if none) */ - uint32_t imm; /* Immediate value / constant bank offset / SR code. - * For BW_IR_BRA: signed instruction delta (target minus - * this instruction). 0 branches to itself. */ - uint32_t control; /* Bundle control word */ - bool is_uniform; /* True if targets/uses uniform registers */ - bool predicate_p0; /* True if predicated on P0 */ - bool predicate_not; /* With predicate_p0, encode @!P0 */ - uint8_t dst_subreg; /* Subregister offset (0..3) within register bundle */ - uint8_t src1_subreg; - uint8_t src2_subreg; - uint8_t src3_subreg; - uint8_t bar_reg; /* BW_IR_BSSY / BW_IR_BSYNC: barrier register B0..B15 */ -} BlackwellIRInsn; - -/* Live Interval for Bounded Linear Register Allocation */ -typedef struct { - int vreg; - int first_def; - int last_use; - int phys_reg; - uint32_t bundle_size; /* 1: scalar (32-bit), 2: pair (64-bit), 4: quad (128-bit) */ - bool is_pair; /* True if 64-bit register pair (requires even-aligned R_2k) */ - bool is_quad; /* True if 128-bit register quad (requires 4-aligned R_4k) */ - bool active; -} OmegaLiveInterval; - -/* Register Allocator State */ -typedef struct { - int num_vregs; - int num_uvregs; - OmegaLiveInterval intervals[BW_MAX_VREGS]; - OmegaLiveInterval uintervals[BW_MAX_UVREGS]; - int vreg_to_phys[BW_MAX_VREGS]; - int uvreg_to_phys[BW_MAX_UVREGS]; - uint32_t peak_gpr_usage; - uint32_t peak_ugpr_usage; -} OmegaRegAlloc; - -/* IR Program */ -typedef struct { - BlackwellIRInsn insns[BW_MAX_IR_INSNS]; - size_t count; - OmegaRegAlloc regalloc; -} BlackwellIRProgram; - -/* Initializes IR program */ -void omega_bw_ir_init(BlackwellIRProgram *prog); - -/* Appends an instruction node to IR program */ -int omega_bw_ir_append(BlackwellIRProgram *prog, const BlackwellIRInsn *insn); - -/* Allocates a new 32-bit virtual general-purpose register */ -int omega_bw_ir_alloc_vreg(BlackwellIRProgram *prog); - -/* Allocates a new 64-bit aligned virtual register pair (for 64-bit memory addresses or HMMA B matrix) */ -int omega_bw_ir_alloc_vreg64(BlackwellIRProgram *prog); - -/* Allocates a new 128-bit aligned virtual register quad (for HMMA accumulator and A matrix) */ -int omega_bw_ir_alloc_vreg128(BlackwellIRProgram *prog); - -/* Allocates a new 32-bit virtual uniform register */ -int omega_bw_ir_alloc_uvreg(BlackwellIRProgram *prog); - -/* Allocates a new 64-bit aligned virtual uniform register pair */ -int omega_bw_ir_alloc_uvreg64(BlackwellIRProgram *prog); - -/* Solves bounded deterministic live-interval register allocation */ -int omega_bw_regalloc_solve(BlackwellIRProgram *prog); - -/* Encodes scheduled IR program into 128-bit sm_121 machine code */ -int omega_bw_encode_program(const BlackwellIRProgram *prog, uint8_t *code_buf, size_t max_len, size_t *out_len); - -/* ---- native v1 P07 cut A.2: allocation-free code generation (aienos#286) ---------------------------- - * The freestanding build (OMEGA_GPU_NO_POSIX, make gpu-engine-freestanding) has no allocator: the caller - * supplies the code buffer and its capacity. Build the program with one of the *_prog generators, then - * encode it with omega_blackwell_kernel_encode_into. The hosted allocating generators below encode through - * the same function, so both builds emit the same bytes (make test-codegen-freestanding). - * - * omega_bw_program_code_bytes: the exact bytes the encoder writes for prog (padded to 8 instructions, - * at least 32), 0 for NULL. - * omega_blackwell_kernel_encode_into: zeroes *kernel, then encodes prog into code_buf. Refuses with - * OMEGA_BW_CODE_BUFFER_TOO_SMALL, writing no byte of code_buf, when code_cap is below - * omega_bw_program_code_bytes(prog); -1 for NULL arguments or an instruction the encoder rejects. On - * success kernel->code points INTO the caller's buffer: the kernel owns nothing, the caller keeps - * code_buf alive as long as the kernel is used and never passes such a kernel to - * omega_blackwell_kernel_free (which exists only in the hosted build). */ -#define OMEGA_BW_CODE_BUFFER_TOO_SMALL (-2) -#define OMEGA_BW_CODE_CAP_SMALL 4096u /* cap of the tensor, tensor_loop and frag generators */ -#define OMEGA_BW_CODE_CAP_MAX ((size_t)BW_MAX_IR_INSNS * 16u) /* cap of frag_block; no program encodes past it */ -size_t omega_bw_program_code_bytes(const BlackwellIRProgram *prog); -int omega_blackwell_kernel_encode_into(const BlackwellIRProgram *prog, uint8_t *code_buf, size_t code_cap, - OmegaBlackwellKernel *kernel); -/* The integer generator's program (K <= 32); the allocating omega_blackwell_codegen_matmul_i32 encodes it. */ -int omega_blackwell_codegen_matmul_i32_prog(const OmegaMatMulSpec *spec, BlackwellIRProgram *prog); - -/* The allocating generators (omega_blackwell_codegen_matmul_i32, _tensor, _tensor_loop, _frag, _frag_block), - * omega_blackwell_codegen_matmul, omega_blackwell_kernel_free and omega_blackwell_test_codegen_variation exist - * only in the hosted build. */ - -/* Top-level dynamic code generator for integer matrix multiplication */ -int omega_blackwell_codegen_matmul_i32(const OmegaMatMulSpec *spec, OmegaBlackwellKernel *kernel); - -/* Dynamic sm_121 Tensor Core code generator (FP16 & BF16 via HMMA.16816) */ -int omega_blackwell_codegen_matmul_tensor_prog(const OmegaMatMulSpec *spec, BlackwellIRProgram *prog); -int omega_blackwell_codegen_matmul_tensor(const OmegaMatMulSpec *spec, OmegaBlackwellKernel *kernel); - -/* FB-1 cut 1b: looped Tensor Core matmul. One warp per CTA. CTA (x, y) owns row - * tile y (16 rows) and column tiles x, x + grid_x, x + 2 grid_x, ... (8 columns - * each); the whole K dimension is accumulated in the HMMA accumulator on the - * chip (no host K slicing). Requires M % 16 == 0, N % 8 == 0, K % 16 == 0 and - * 1 <= grid_x <= N / 8. The kernel does not depend on M (rows come from - * CTAID.Y), so one kernel serves every row count of a (K, N, grid_x) triple. - * Uses only instructions already present in the chip-proven single-tile kernel - * plus the predicated backward BRA and ISETP.GE this IR already encodes. - * mutant != 0 drops the last K step (test oracle: the wrong kernel must be - * caught by the parity test; never set in production). */ -int omega_blackwell_codegen_matmul_tensor_loop_prog(const OmegaMatMulSpec *spec, uint32_t grid_x, - int mutant, BlackwellIRProgram *prog); -int omega_blackwell_codegen_matmul_tensor_loop(const OmegaMatMulSpec *spec, uint32_t grid_x, - int mutant, OmegaBlackwellKernel *kernel); - -/* omega #328: fragment-major tensor matmul. Same grid, C layout (row-major f32) and argument - * words as the looped kernel; A and B are in fragment order instead of row-major: - * A (mp x kp, from omega_matmul_frag_pack_a): block (mt, ks) of 256 bf16 at ((mt*KS + ks)*32 - * + lane)*8, lane = group*4 + tig, holding A[r][c], A[r][c+1], A[r+8][c], A[r+8][c+1], - * A[r][c+8], A[r][c+9], A[r+8][c+8], A[r+8][c+9] with r = 16mt + group, c = 16ks + 2tig; - * B (kp x np, from omega_matmul_frag_pack_b): block (nt, ks) of 128 bf16 at ((nt*KS + ks)*32 - * + lane)*4 holding B[k][j], B[k+1][j], B[k+8][j], B[k+9][j] with k = 16ks + 2tig, - * j = 8nt + group; KS = kp / 16, entries outside the real matrix are zero. - * One LDG.E.128 (A) and one LDG.E.64 (B) per K step; `unroll` (1, 2, 4 or 8, dividing KS) - * steps of loads are issued before the HMMA chain. mutant != 0 drops the last pass (test - * oracle only). Each tile occupies 16*kp (A) and 8*kp (B) elements, so a row-tile offset into - * A is the same in bytes as for row-major A. */ -int omega_blackwell_codegen_matmul_frag_prog(const OmegaMatMulSpec *spec, uint32_t grid_x, uint32_t unroll, - int mutant, BlackwellIRProgram *prog); -int omega_blackwell_codegen_matmul_frag(const OmegaMatMulSpec *spec, uint32_t grid_x, uint32_t unroll, - int mutant, OmegaBlackwellKernel *kernel); -/* The largest of 4, 2, 1 that divides kp / 16 (unroll 8 is generated for experiments only: it - * did not complete on the GB10, see the definition). */ -uint32_t omega_matmul_frag_unroll(uint32_t kp); -/* 2026-10-12: blocked fragment kernel for prefill rows. One warp owns rt row tiles x ct column tiles - * (rt in 1, 2, 4, 8; ct in 1, 2, 4; 2 <= rt*ct <= 16; unroll 1 or 2 with unroll*(rt+ct) <= 8; nt % ct == 0; - * grid_x <= nt/ct). Same packing, argument words and C layout as the single-tile kernel; a CTA's - * row block is rt row tiles (grid_y = row tiles / rt). mutant != 0 drops the last K pass. */ -int omega_blackwell_codegen_matmul_frag_block_prog(const OmegaMatMulSpec *spec, uint32_t grid_x, uint32_t unroll, - uint32_t rt, uint32_t ct, int mutant, BlackwellIRProgram *prog); -int omega_blackwell_codegen_matmul_frag_block(const OmegaMatMulSpec *spec, uint32_t grid_x, uint32_t unroll, - uint32_t rt, uint32_t ct, int mutant, OmegaBlackwellKernel *kernel); -/* rt, ct for mt row tiles and nt column tiles (1, 1 = the single-tile kernel), and the unroll the API uses */ -void omega_matmul_frag_block_shape(uint32_t mt, uint32_t nt, uint32_t *rt, uint32_t *ct); -uint32_t omega_matmul_frag_block_unroll(uint32_t kp, uint32_t rt, uint32_t ct); - -/* Registers to declare in the QMD for a kernel whose allocator peak (OmegaLiveInterval count, regalloc.peak_gpr_usage) - * is `peak`: peak + 2, rounded up to a multiple of 8 (the session raises anything below 64 to 64). - * Chip evidence (OWN-30B, 2026-10-13): the fp8 grouped GEMM (lane 8, 8 rows) with peak 103 declared 104 (= peak + 1 - * rounded up, the earlier rule) hung the chip in 7 of 7 runs; the same program declared 112 ran every gate. Earlier - * evidence: peak 64 declared 64 faulted (Xid 13 Out Of Range Register). Declared >= peak + 2 has run everywhere else - * (router peak 46 declared 48). The exact minimum is UNKNOWN (declarations are multiples of 8); the rule is INFERRED. */ -uint32_t omega_bw_gpr_declared(uint32_t peak); -/* 1 iff `declared` is a legal declaration for `peak`: a multiple of 8 and >= peak + 2. Launchers refuse otherwise. */ -int omega_bw_gpr_declared_ok(uint32_t declared, uint32_t peak); -/* a: m x k row-major. dst: mp x kp elements, fully written (zero padding). */ -void omega_matmul_frag_pack_a(const uint16_t *a, uint32_t m, uint32_t k, uint32_t mp, uint32_t kp, uint16_t *dst); -/* the same from f32 rows, converted to bf16 as omega_fp32_to_bf16 does (one pass, no staging copy) */ -void omega_matmul_frag_pack_a_f32(const float *a, uint32_t m, uint32_t k, uint32_t mp, uint32_t kp, uint16_t *dst); -/* Row 0 of a one-row call into a buffer whose rows 1..15 already hold zeros for this kp (every column, - * zeros past k); the caller tracks that the zeros are there (omega_gpu_matmul_api.c, m = 1 path). */ -void omega_matmul_frag_pack_a_row0(const uint16_t *a, uint32_t k, uint32_t kp, uint16_t *dst); -void omega_matmul_frag_pack_a_row0_f32(const float *a, uint32_t k, uint32_t kp, uint16_t *dst); -/* Result sentinels (host side of the poison check): one word per 16 x 8 fragment tile that holds a real - * output word, at the tile's last real row and column, in the row-major m x np result. set != 0 writes - * poison into every sentinel and returns their count; set == 0 returns how many still hold poison. The - * kernel's epilogue stores a fragment tile as one straight-line run of STG.E words, so a tile whose - * sentinel survived was never stored. */ -size_t omega_matmul_result_sentinels(float *cd, uint32_t m, uint32_t n, uint32_t np, float poison, int set); -/* b: k x n row-major. dst: the column tiles [nt0, nt1), (nt1 - nt0) * kp * 8 elements, fully - * written; it is the part of the packed tensor starting at element nt0 * kp * 8. */ -void omega_matmul_frag_pack_b(const uint16_t *b, uint32_t k, uint32_t n, uint32_t kp, - uint32_t nt0, uint32_t nt1, uint16_t *dst); - -/* Unit test for instruction encoding bitfield fixtures (Gate 3) */ -int omega_blackwell_verify_codegen_fixtures(void); - -/* FB-1 cut 4: fixtures for MUFU_EX2, BAR_SYNC, LDS32, STS32 (0 = pass) */ -int omega_blackwell_verify_codegen_fixtures_fb1cut4(void); - -/* Prime race cut: golden words for LOP3_LUT, SHF_L_U32, IMAD_HI_U32 (0 = pass) */ -int omega_blackwell_verify_codegen_fixtures_intops(void); - -/* omega #328: golden words for LDG.E.64 / LDG.E.128 with offsets and the timed NOP (0 = pass) */ -int omega_blackwell_verify_codegen_fixtures_fragmm(void); - -/* omega #308: golden words for BSSY / BSYNC (nvcc 13.0.88 sm_121 oracle), plus the - * encoder's refusals (negative delta, barrier id > 15). 0 = pass. */ -int omega_blackwell_verify_codegen_fixtures_reconv(void); - -/* omega #308: encodes one instruction with a given physical register map of - * identity (vreg n -> Rn) so offline tools can disassemble single words. 0 = ok. */ -int omega_blackwell_encode_one(const BlackwellIRInsn *insn, uint32_t w[4]); - -/* Unit test for bounded register allocation live intervals and bounds enforcement (Gate 4) */ -int omega_blackwell_test_regalloc_bounds(void); - -/* Stage-1 dynamic codegen variation demonstration between 16x16x16 and 32x16x64 (Gate 7) */ -int omega_blackwell_test_codegen_variation(void); - -#endif /* OMEGA_BLACKWELL_CODEGEN_H */ diff --git a/src/omega_blackwell_codegen_attn.c b/src/omega_blackwell_codegen_attn.c deleted file mode 100644 index 37a67d8df..000000000 --- a/src/omega_blackwell_codegen_attn.c +++ /dev/null @@ -1,549 +0,0 @@ -/* - * omega_blackwell_codegen_attn.c: the one-launch decode attention kernel (see the header). Emitter rules follow - * omega_blackwell_codegen_moe.c (control words, read barrier on every load, loop live ranges). - */ -#include "omega_blackwell_codegen_attn.h" -#include - -#define CW(stall, wbar, rbar, wait) \ - (((uint32_t)(stall) << 9) | ((uint32_t)(wbar) << 14) | ((uint32_t)(rbar) << 17) | ((uint32_t)(wait) << 20)) -#define CW_YIELD 0x2000u -#define SB_NONE 7u -#define SB_GPR 0u -#define SB_RD 1u -#define SB_UGPR 2u -#define NOREG (-1) -#define MAX_LOOPS 4 - -enum { K_FIXED, K_PRED, K_VAR, K_VAR_MORE, K_UVAR, K_STORE, K_SYNC, K_EXIT, K_BRA, K_BSSY, K_BSYNC }; - -typedef struct { - BlackwellIRProgram *p; - uint32_t pending; - int err; - int open_start[MAX_LOOPS]; - int depth; -} Em; - -static void emit(Em *e, BlackwellIRInsn in, int kind) { - uint32_t wait = e->pending; - switch (kind) { - case K_FIXED: in.control = CW(6, SB_NONE, SB_NONE, wait); e->pending = 0; break; - case K_PRED: in.control = CW(14, SB_NONE, SB_NONE, wait); e->pending = 0; break; - case K_VAR: in.control = CW(4, SB_GPR, SB_RD, wait) | CW_YIELD; e->pending = (1u << SB_GPR) | (1u << SB_RD); break; - case K_VAR_MORE: in.control = CW(4, SB_GPR, SB_RD, (e->pending & (1u << SB_RD))) | CW_YIELD; e->pending |= (1u << SB_GPR) | (1u << SB_RD); break; - case K_UVAR: in.control = CW(4, SB_UGPR, SB_NONE, wait) | CW_YIELD; e->pending = 1u << SB_UGPR; break; - case K_STORE: in.control = CW(4, SB_NONE, SB_RD, wait) | CW_YIELD; e->pending = 1u << SB_RD; break; - case K_SYNC: in.control = CW(6, SB_NONE, SB_NONE, wait); e->pending = 0; break; - case K_EXIT: in.control = CW(5, SB_NONE, SB_NONE, wait); e->pending = in.predicate_p0 ? wait : 0; break; - case K_BRA: in.control = CW(5, SB_NONE, SB_NONE, wait); e->pending = in.predicate_p0 ? wait : 0; break; - case K_BSSY: in.control = CW(1, SB_NONE, SB_NONE, wait) | CW_YIELD; e->pending = 0; break; - case K_BSYNC: in.control = CW(5, SB_NONE, SB_NONE, wait) | CW_YIELD; e->pending = 0; break; - default: e->err = -1; return; - } - if (omega_bw_ir_append(e->p, &in) < 0) e->err = -1; -} - -static BlackwellIRInsn I0(BlackwellIROpcode op) { - BlackwellIRInsn i; memset(&i, 0, sizeof i); - i.op = op; i.dst_vreg = NOREG; i.src1_vreg = NOREG; i.src2_vreg = NOREG; i.src3_vreg = NOREG; i.ureg = NOREG; - return i; -} -static int V(Em *e) { int v = omega_bw_ir_alloc_vreg(e->p); if (v < 0) e->err = -1; return v; } -static int V64(Em *e) { int v = omega_bw_ir_alloc_vreg64(e->p); if (v < 0) e->err = -1; return v; } -static int V128(Em *e) { int v = omega_bw_ir_alloc_vreg128(e->p); if (v < 0) e->err = -1; return v; } -static int UV64(Em *e) { int v = omega_bw_ir_alloc_uvreg64(e->p); if (v < 0) e->err = -1; return v; } -static int here(const Em *e) { return (int)e->p->count; } - -/* operand with subregister */ -typedef struct { int v; unsigned s; } Op; -static Op R(int v) { Op o = { v, 0 }; return o; } -static Op S(int v, unsigned s) { Op o = { v, s }; return o; } - -static void ldcu64(Em *e, int ud, uint32_t off) { BlackwellIRInsn i = I0(BW_IR_LDCU64); i.dst_vreg = ud; i.imm = off; i.is_uniform = true; emit(e, i, K_UVAR); } -static void ldc64(Em *e, int d, uint32_t off) { BlackwellIRInsn i = I0(BW_IR_LDC64); i.dst_vreg = d; i.imm = off; emit(e, i, K_VAR); } -static void ldc32(Em *e, int d, uint32_t off) { BlackwellIRInsn i = I0(BW_IR_LDC); i.dst_vreg = d; i.imm = off; emit(e, i, K_VAR); } -static void s2r(Em *e, int d, uint32_t sr) { BlackwellIRInsn i = I0(BW_IR_S2R); i.dst_vreg = d; i.imm = sr; emit(e, i, K_VAR); } -static void movi(Em *e, int d, uint32_t imm) { BlackwellIRInsn i = I0(BW_IR_MOV_IMM); i.dst_vreg = d; i.imm = imm; emit(e, i, K_FIXED); } -static void movrz(Em *e, Op d) { BlackwellIRInsn i = I0(BW_IR_MOV_RZ); i.dst_vreg = d.v; i.dst_subreg = (uint8_t)d.s; emit(e, i, K_FIXED); } -/* d = a * imm + c */ -static void imadi(Em *e, Op d, Op a, uint32_t imm, int c) { BlackwellIRInsn i = I0(BW_IR_IMAD); i.dst_vreg = d.v; i.dst_subreg = (uint8_t)d.s; i.src1_vreg = a.v; i.src1_subreg = (uint8_t)a.s; i.imm = imm; i.src3_vreg = c; emit(e, i, K_FIXED); } -/* d = a * b + c (registers) */ -static void imadr(Em *e, int d, int a, int b, int c) { BlackwellIRInsn i = I0(BW_IR_IMAD); i.dst_vreg = d; i.src1_vreg = a; i.src2_vreg = b; i.src3_vreg = c; emit(e, i, K_FIXED); } -/* d64 = a * imm + c64 */ -static void imadw(Em *e, int d64, int a, uint32_t imm, int c64) { BlackwellIRInsn i = I0(BW_IR_IMAD_WIDE); i.dst_vreg = d64; i.src1_vreg = a; i.imm = imm; i.src3_vreg = c64; emit(e, i, K_FIXED); } -static void mov(Em *e, Op d, Op a) { imadi(e, d, a, 1u, NOREG); } -static void shrs(Em *e, int d, Op a, uint32_t imm) { BlackwellIRInsn i = I0(BW_IR_SHF_R); i.dst_vreg = d; i.src1_vreg = a.v; i.src1_subreg = (uint8_t)a.s; i.imm = imm; emit(e, i, K_FIXED); } -static void shr(Em *e, int d, int a, uint32_t imm) { shrs(e, d, R(a), imm); } -static void andi(Em *e, Op d, Op a, uint32_t imm) { BlackwellIRInsn i = I0(BW_IR_LOP3_AND); i.dst_vreg = d.v; i.dst_subreg = (uint8_t)d.s; i.src1_vreg = a.v; i.src1_subreg = (uint8_t)a.s; i.imm = imm; emit(e, i, K_FIXED); } -static void xorr(Em *e, Op d, Op a, Op b) { BlackwellIRInsn i = I0(BW_IR_LOP3_XOR); i.dst_vreg = d.v; i.dst_subreg = (uint8_t)d.s; i.src1_vreg = a.v; i.src1_subreg = (uint8_t)a.s; i.src2_vreg = b.v; i.src2_subreg = (uint8_t)b.s; emit(e, i, K_FIXED); } -static void iadd(Em *e, int d, int a, int b) { BlackwellIRInsn i = I0(BW_IR_IADD3); i.dst_vreg = d; i.src1_vreg = a; i.src2_vreg = b; emit(e, i, K_FIXED); } -static void ldg_wide(Em *e, BlackwellIROpcode op, int d, int addr64, uint32_t imm, int udesc, int first) { - BlackwellIRInsn i = I0(op); i.dst_vreg = d; i.src1_vreg = addr64; i.imm = imm; i.ureg = udesc; emit(e, i, first ? K_VAR : K_VAR_MORE); -} -static void ldg32(Em *e, BlackwellIROpcode op, int d, int addr64, int udesc, int first) { BlackwellIRInsn i = I0(op); i.dst_vreg = d; i.src1_vreg = addr64; i.ureg = udesc; emit(e, i, first ? K_VAR : K_VAR_MORE); } -static void stg(Em *e, BlackwellIROpcode op, int addr64, Op val, int udesc) { BlackwellIRInsn i = I0(op); i.src1_vreg = addr64; i.src2_vreg = val.v; i.src2_subreg = (uint8_t)val.s; i.ureg = udesc; emit(e, i, K_STORE); } -static void atom_add(Em *e, int d, int addr64, int val, int udesc) { BlackwellIRInsn i = I0(BW_IR_ATOMG_ADD_STRONG_SYS); i.dst_vreg = d; i.src1_vreg = addr64; i.src2_vreg = val; i.ureg = udesc; emit(e, i, K_VAR); } -static void membar(Em *e) { emit(e, I0(BW_IR_MEMBAR_ALL_SYS), K_FIXED); } -static void lds128(Em *e, int d128, int addr, uint32_t imm, int first) { BlackwellIRInsn i = I0(BW_IR_LDS128); i.dst_vreg = d128; i.src1_vreg = addr; i.imm = imm; emit(e, i, first ? K_VAR : K_VAR_MORE); } -static void lds32(Em *e, Op d, int addr, int first) { BlackwellIRInsn i = I0(BW_IR_LDS32); i.dst_vreg = d.v; i.dst_subreg = (uint8_t)d.s; i.src1_vreg = addr; emit(e, i, first ? K_VAR : K_VAR_MORE); } -static void sts32(Em *e, int addr, Op val) { BlackwellIRInsn i = I0(BW_IR_STS32); i.src1_vreg = addr; i.src2_vreg = val.v; i.src2_subreg = (uint8_t)val.s; emit(e, i, K_STORE); } -static void bar_sync(Em *e) { emit(e, I0(BW_IR_BAR_SYNC), K_SYNC); } -static void fop(Em *e, BlackwellIROpcode op, Op d, Op a, Op b, Op c) { - BlackwellIRInsn i = I0(op); i.dst_vreg = d.v; i.dst_subreg = (uint8_t)d.s; i.src1_vreg = a.v; i.src1_subreg = (uint8_t)a.s; - i.src2_vreg = b.v; i.src2_subreg = (uint8_t)b.s; i.src3_vreg = c.v; i.src3_subreg = (uint8_t)c.s; emit(e, i, K_FIXED); -} -static void fadd(Em *e, Op d, Op a, Op b) { fop(e, BW_IR_FADD, d, a, b, R(NOREG)); } -static void fsub(Em *e, Op d, Op a, Op b) { fop(e, BW_IR_FSUB, d, a, b, R(NOREG)); } -static void fmul(Em *e, Op d, Op a, Op b) { fop(e, BW_IR_FMUL, d, a, b, R(NOREG)); } -static void ffma(Em *e, Op d, Op a, Op b, Op c) { fop(e, BW_IR_FFMA, d, a, b, c); } -static void fmaxx(Em *e, Op d, Op a, Op b) { fop(e, BW_IR_FMNMX_MAX, d, a, b, R(NOREG)); } -static void mufu(Em *e, BlackwellIROpcode op, Op d, Op a) { BlackwellIRInsn i = I0(op); i.dst_vreg = d.v; i.dst_subreg = (uint8_t)d.s; i.src1_vreg = a.v; i.src1_subreg = (uint8_t)a.s; emit(e, i, K_VAR); } -static void shfl_down(Em *e, int d, int a, uint32_t off) { BlackwellIRInsn i = I0(BW_IR_SHFL_DOWN); i.dst_vreg = d; i.src1_vreg = a; i.imm = off; emit(e, i, K_VAR); } -static void isetp_ge_u32(Em *e, int a, int b) { BlackwellIRInsn i = I0(BW_IR_ISETP_GE_U32); i.src1_vreg = a; i.src2_vreg = b; emit(e, i, K_PRED); } -static void bra(Em *e, int target) { BlackwellIRInsn i = I0(BW_IR_BRA); i.imm = (uint32_t)(target - here(e)); emit(e, i, K_BRA); } -static int bra_if_p0_fwd(Em *e) { int at = here(e); BlackwellIRInsn i = I0(BW_IR_BRA); i.predicate_p0 = true; i.imm = 1; emit(e, i, K_BRA); return at; } -static void patch_bra(Em *e, int at, int target) { if (!e->err) e->p->insns[at].imm = (uint32_t)(target - at); } -static int bssy_fwd(Em *e) { int at = here(e); BlackwellIRInsn i = I0(BW_IR_BSSY); i.bar_reg = 0; i.imm = 2; emit(e, i, K_BSSY); return at; } -static void bsync(Em *e) { BlackwellIRInsn i = I0(BW_IR_BSYNC); i.bar_reg = 0; emit(e, i, K_BSYNC); } -static void exit_k(Em *e) { emit(e, I0(BW_IR_EXIT), K_EXIT); } -static void tail(Em *e) { - exit_k(e); - BlackwellIRInsn self = I0(BW_IR_BRA); - self.control = 0x000fc000u; - if (omega_bw_ir_append(e->p, &self) < 0) e->err = -1; -} -static void loop_begin(Em *e) { if (e->depth >= MAX_LOOPS) { e->err = -1; return; } e->open_start[e->depth++] = here(e); } -static void loop_end(Em *e) { - if (e->depth <= 0) { e->err = -1; return; } - int start = e->open_start[--e->depth], end = here(e) - 1; - OmegaRegAlloc *ra = &e->p->regalloc; - for (int v = 0; v < ra->num_vregs; v++) { - OmegaLiveInterval *iv = &ra->intervals[v]; - if (iv->first_def >= 0 && iv->first_def < start && iv->last_use >= start && iv->last_use < end) iv->last_use = end; - } -} - -/* A region only some lanes of a warp execute: BSSY ... @P0 BRA join ... BSYNC. P0 true = skip. */ -typedef struct { int bssy, bra; } Region; -static Region region_skip_if_p0(Em *e) { Region r; r.bssy = bssy_fwd(e); r.bra = bra_if_p0_fwd(e); return r; } -static void region_end(Em *e, Region r) { int at = here(e); bsync(e); patch_bra(e, r.bra, at); patch_bra(e, r.bssy, at + 1); } - -/* bf16 round-to-nearest-even of f32 x into the low 16 bits of d: r = x + 0x7fff + ((x >> 16) & 1); d = r >> 16 */ -static void bf16_of(Em *e, int d, Op x, int c7fff, int t) { - shrs(e, t, x, 16); - andi(e, R(t), R(t), 1u); - iadd(e, t, t, c7fff); - fop(e, BW_IR_IADD3, R(d), x, R(t), R(NOREG)); /* IADD3 d = x + t */ - shr(e, d, d, 16); -} - -static const uint32_t NEG_BIG = 0xF149F2CAu; /* -1e30f */ - -int omega_blackwell_codegen_decode_attn_prog(const OmegaDecodeAttnSpec *spec, BlackwellIRProgram *prog) { - if (!spec || !prog || spec->depth < 1 || spec->depth > OMEGA_DATTN_MAX_DEPTH || spec->mutant < 0 || spec->mutant > 4) return -1; - omega_bw_ir_init(prog); - Em em; memset(&em, 0, sizeof em); em.p = prog; Em *e = &em; - const uint32_t D = spec->depth; - const int MUT = spec->mutant; - - /* ---- arguments and identities ---- */ - int udesc = UV64(e); - int pqkv = V64(e), pkc = V64(e), pvc = V64(e), prope = V64(e), pqn = V64(e), pkn = V64(e); - int pos = V(e), per = V(e), splits = V(e), kvs = V(e), eps = V(e), qks = V(e); - /* pout, ppart, pcnt, oscale are loaded from the constant bank in the epilogue (register pressure: the chip allocator has 128) */ - int tid = V(e), cx = V(e), cy = V(e), warp = V(e), lane = V(e), one = V(e), zero = V(e), c7fff = V(e), c16 = V(e); - if (e->err) return -1; - ldcu64(e, udesc, 0x358); - ldc64(e, pqkv, OMEGA_DATTN_ARG_QKV); ldc64(e, pkc, OMEGA_DATTN_ARG_KC); ldc64(e, pvc, OMEGA_DATTN_ARG_VC); - ldc64(e, prope, OMEGA_DATTN_ARG_ROPE); ldc64(e, pqn, OMEGA_DATTN_ARG_QN); ldc64(e, pkn, OMEGA_DATTN_ARG_KN); - ldc32(e, pos, OMEGA_DATTN_ARG_POS); ldc32(e, per, OMEGA_DATTN_ARG_PER_SPLIT); ldc32(e, splits, OMEGA_DATTN_ARG_SPLITS); - ldc32(e, kvs, OMEGA_DATTN_ARG_KV_STRIDE); ldc32(e, eps, OMEGA_DATTN_ARG_EPS); - ldc32(e, qks, OMEGA_DATTN_ARG_QK_SCALE); - s2r(e, tid, BW_SR_TID_X); s2r(e, cx, BW_SR_CTAID_X); s2r(e, cy, BW_SR_CTAID_Y); - movi(e, one, 1); movrz(e, R(zero)); movi(e, c7fff, 0x7fffu); movi(e, c16, 16); - shr(e, warp, tid, 5); - andi(e, R(lane), R(tid), 31u); - - /* range of this CTA: base = cx * per; end = min(base + per, pos + 1) */ - int base = V(e), bend = V(e), ctx = V(e), t1 = V(e), t2 = V(e), end = V(e), endm1 = V(e), negendm1 = V(e), splitsm1 = V(e), cyk = V(e); - imadr(e, base, cx, per, NOREG); - imadi(e, R(bend), R(base), 1u, per); - imadi(e, R(ctx), R(pos), 1u, one); - imadi(e, R(t1), R(ctx), 0xffffffffu, bend); /* t1 = bend - ctx */ - imadi(e, R(t2), R(t1), 0xffffffffu, NOREG); /* t2 = ctx - bend */ - shr(e, t2, t2, 31); /* 1 when bend > ctx */ - imadr(e, t2, t2, t1, NOREG); /* t2 = over * (bend - ctx) */ - imadi(e, R(end), R(t2), 0xffffffffu, bend); /* end = bend - t2 */ - imadi(e, R(endm1), R(one), 0xffffffffu, end); - imadi(e, R(negendm1), R(endm1), 0xffffffffu, NOREG); - imadi(e, R(splitsm1), R(one), 0xffffffffu, splits); - imadr(e, cyk, cy, kvs, NOREG); /* byte offset of this KV head's plane */ - - /* ---- prologue: q' for the 8 heads of the group (warp = head) ---- */ - int q4 = V128(e), w4 = V128(e), pt4 = V128(e), cos4 = V128(e), sin4 = V128(e), k4 = V128(e), v4 = V128(e); - int sq = V(e), ts = V(e), rs = V(e), a0 = V(e), a1 = V(e), a2 = V(e), a3 = V(e), ad64 = V64(e), rope64 = V64(e), sgn = V(e), pl = V(e); - if (e->err) return -1; - imadi(e, R(t1), R(cy), 8u, warp); /* q head index */ - imadi(e, R(t1), R(t1), 512u, NOREG); - imadi(e, R(t1), R(lane), 16u, t1); - imadw(e, ad64, t1, 1u, pqkv); - ldg_wide(e, BW_IR_LDG_E_128, q4, ad64, 0, udesc, 1); - imadw(e, ad64, lane, 16u, pqn); - ldg_wide(e, BW_IR_LDG_E_128, w4, ad64, 0, udesc, 0); - /* rope row for this lane: cos/sin index = lane & 15 (dims 4i..4i+3 for lanes < 16, dims 64 + 4(i-16) above) */ - andi(e, R(t2), R(lane), 15u); - imadi(e, R(t2), R(t2), 16u, NOREG); - imadi(e, R(t2), R(pos), OMEGA_DATTN_ROPE_ROW_BYTES, t2); - imadw(e, rope64, t2, 1u, prope); - ldg_wide(e, BW_IR_LDG_E_128, cos4, rope64, 0, udesc, 0); - ldg_wide(e, BW_IR_LDG_E_128, sin4, rope64, 256, udesc, 0); - /* sign of the sin term: lanes < 16 take q[d] * cos - q[d+64] * sin, lanes >= 16 take q[d] * cos + q[d-64] * sin */ - imadi(e, R(sgn), R(one), 0xfffffff0u, lane); /* lane - 16 */ - shr(e, sgn, sgn, 31); - imadi(e, R(sgn), R(sgn), 0x80000000u, NOREG); - for (unsigned i = 0; i < 4; i++) xorr(e, S(sin4, i), S(sin4, i), R(sgn)); - xorr(e, R(pl), R(lane), R(c16)); /* partner lane */ - - /* RMSNorm of the quad's head: sum of squares over the warp, broadcast through shared memory */ - fmul(e, R(sq), S(q4, 0), S(q4, 0)); - for (unsigned i = 1; i < 4; i++) ffma(e, R(sq), S(q4, i), S(q4, i), R(sq)); - for (uint32_t o = 16; o >= 1; o /= 2) { shfl_down(e, ts, sq, o); fadd(e, R(sq), R(sq), R(ts)); } - imadi(e, R(a0), R(warp), 4u, NOREG); imadi(e, R(a0), R(one), OMEGA_DATTN_SM_L, a0); - isetp_ge_u32(e, lane, one); - { Region r = region_skip_if_p0(e); sts32(e, a0, R(sq)); region_end(e, r); } - bar_sync(e); - lds32(e, R(sq), a0, 1); - movi(e, ts, 0x3c000000u); /* 1/128 */ - ffma(e, R(rs), R(sq), R(ts), R(eps)); - mufu(e, BW_IR_MUFU_RSQ, R(rs), R(rs)); - for (unsigned i = 0; i < 4; i++) { fmul(e, S(q4, i), S(q4, i), R(rs)); fmul(e, S(q4, i), S(q4, i), S(w4, i)); } - /* stage, read the partner half, rotate */ - imadi(e, R(a0), R(warp), 512u, NOREG); imadi(e, R(a0), R(lane), 16u, a0); /* SM_Q + warp*512 + lane*16 (SM_Q = 0) */ - imadi(e, R(a1), R(one), 4u, a0); imadi(e, R(a2), R(one), 8u, a0); imadi(e, R(a3), R(one), 12u, a0); - sts32(e, a0, S(q4, 0)); sts32(e, a1, S(q4, 1)); sts32(e, a2, S(q4, 2)); sts32(e, a3, S(q4, 3)); - bar_sync(e); - imadi(e, R(t2), R(warp), 512u, NOREG); imadi(e, R(t2), R(pl), 16u, t2); - lds128(e, pt4, t2, 0, 1); - if (MUT != 1) for (unsigned i = 0; i < 4; i++) { fmul(e, R(ts), S(q4, i), S(cos4, i)); ffma(e, S(q4, i), S(pt4, i), S(sin4, i), R(ts)); } - for (unsigned i = 0; i < 4; i++) fmul(e, S(q4, i), S(q4, i), R(qks)); - bar_sync(e); /* every partner read done before q' overwrites the staging */ - sts32(e, a0, S(q4, 0)); sts32(e, a1, S(q4, 1)); sts32(e, a2, S(q4, 2)); sts32(e, a3, S(q4, 3)); - - /* ---- the new token's K (norm + rope) and V, bf16 into the cache by the CTA that owns `pos` ---- */ - imadi(e, R(t1), R(cy), 512u, NOREG); imadi(e, R(t1), R(lane), 16u, t1); - imadi(e, R(t1), R(one), 32u * 128u * 4u, t1); /* k block of qkv */ - imadw(e, ad64, t1, 1u, pqkv); - ldg_wide(e, BW_IR_LDG_E_128, k4, ad64, 0, udesc, 1); - ldg_wide(e, BW_IR_LDG_E_128, v4, ad64, 4u * 128u * 4u, udesc, 0); /* v block follows the 4 k heads */ - imadw(e, ad64, lane, 16u, pkn); - ldg_wide(e, BW_IR_LDG_E_128, w4, ad64, 0, udesc, 0); - fmul(e, R(sq), S(k4, 0), S(k4, 0)); - for (unsigned i = 1; i < 4; i++) ffma(e, R(sq), S(k4, i), S(k4, i), R(sq)); - for (uint32_t o = 16; o >= 1; o /= 2) { shfl_down(e, ts, sq, o); fadd(e, R(sq), R(sq), R(ts)); } - imadi(e, R(a1), R(warp), 4u, NOREG); imadi(e, R(a1), R(one), OMEGA_DATTN_SM_L, a1); - isetp_ge_u32(e, lane, one); - { Region r = region_skip_if_p0(e); sts32(e, a1, R(sq)); region_end(e, r); } - bar_sync(e); - lds32(e, R(sq), a1, 1); - movi(e, ts, 0x3c000000u); - ffma(e, R(rs), R(sq), R(ts), R(eps)); - mufu(e, BW_IR_MUFU_RSQ, R(rs), R(rs)); - for (unsigned i = 0; i < 4; i++) { fmul(e, S(k4, i), S(k4, i), R(rs)); fmul(e, S(k4, i), S(k4, i), S(w4, i)); } - imadi(e, R(a1), R(lane), 16u, NOREG); imadi(e, R(a1), R(one), OMEGA_DATTN_SM_K, a1); - imadi(e, R(a2), R(one), 4u, a1); imadi(e, R(a3), R(one), 8u, a1); imadi(e, R(t2), R(one), 12u, a1); - sts32(e, a1, S(k4, 0)); sts32(e, a2, S(k4, 1)); sts32(e, a3, S(k4, 2)); sts32(e, t2, S(k4, 3)); /* every warp writes the same words */ - bar_sync(e); - imadi(e, R(t2), R(pl), 16u, NOREG); imadi(e, R(t2), R(one), OMEGA_DATTN_SM_K, t2); - lds128(e, pt4, t2, 0, 1); - if (MUT != 1) for (unsigned i = 0; i < 4; i++) { fmul(e, R(ts), S(k4, i), S(cos4, i)); ffma(e, S(k4, i), S(pt4, i), S(sin4, i), R(ts)); } - /* bf16 words: k4 -> (a2, a3), v4 -> (sq, ts) */ - { - int h0 = V(e), h1 = V(e), tb = V(e); - bf16_of(e, h0, S(k4, 0), c7fff, tb); bf16_of(e, h1, S(k4, 1), c7fff, tb); imadi(e, R(a2), R(h1), 65536u, h0); - bf16_of(e, h0, S(k4, 2), c7fff, tb); bf16_of(e, h1, S(k4, 3), c7fff, tb); imadi(e, R(a3), R(h1), 65536u, h0); - bf16_of(e, h0, S(v4, 0), c7fff, tb); bf16_of(e, h1, S(v4, 1), c7fff, tb); imadi(e, R(sq), R(h1), 65536u, h0); - bf16_of(e, h0, S(v4, 2), c7fff, tb); bf16_of(e, h1, S(v4, 3), c7fff, tb); imadi(e, R(ts), R(h1), 65536u, h0); - } - /* cache row address of position pos for this lane */ - imadi(e, R(t1), R(pos), OMEGA_DATTN_ROW_BYTES, cyk); imadi(e, R(t1), R(lane), 8u, t1); /* 4 bf16 = 8 bytes per lane */ - /* key = (splits - 1 - cx) + warp: zero only for warp 0 of the last split */ - imadi(e, R(t2), R(cx), 0xffffffffu, splitsm1); - if (MUT != 3) { - int ka = V64(e), kb = V64(e), key = V(e); - imadi(e, R(key), R(t2), 1u, warp); - isetp_ge_u32(e, key, one); - int b1 = bra_if_p0_fwd(e); - imadw(e, ka, t1, 1u, pkc); imadw(e, kb, one, 4u, ka); - stg(e, BW_IR_STG_E, ka, R(a2), udesc); stg(e, BW_IR_STG_E, kb, R(a3), udesc); - patch_bra(e, b1, here(e)); - xorr(e, R(key), R(warp), R(one)); imadi(e, R(key), R(t2), 1u, key); - isetp_ge_u32(e, key, one); - int b2 = bra_if_p0_fwd(e); - imadw(e, ka, t1, 1u, pvc); imadw(e, kb, one, 4u, ka); - stg(e, BW_IR_STG_E, ka, R(sq), udesc); stg(e, BW_IR_STG_E, kb, R(ts), udesc); - patch_bra(e, b2, here(e)); - } - membar(e); - bar_sync(e); - - /* ---- running state ---- */ - int acc[8], m[8], l[8], sc[8], dl[8], wd[8], mn = V(e); - for (unsigned h = 0; h < 8; h++) { acc[h] = V128(e); m[h] = V(e); l[h] = V(e); sc[h] = V(e); dl[h] = V(e); wd[h] = V(e); } - int kq[OMEGA_DATTN_MAX_DEPTH], vq[OMEGA_DATTN_MAX_DEPTH]; - for (uint32_t d = 0; d < D; d++) { kq[d] = V128(e); vq[d] = V64(e); } - int qa4 = V128(e), qb4 = V128(e); - int cb = V(e), tpos = V(e), over = V(e), krow = V64(e), kpf = V64(e), qoff = V(e), j = V(e), sa = V(e), smS = V(e); - int n = V(e), vrow = V64(e), vpf = V64(e), pa = V(e), i = V(e), dsum = V(e), lane8 = V(e), negwarp = V(e), ccnt = V(e); - if (e->err) return -1; - for (unsigned h = 0; h < 8; h++) { for (unsigned q = 0; q < 4; q++) movrz(e, S(acc[h], q)); movi(e, m[h], NEG_BIG); movrz(e, R(l[h])); } - movi(e, smS, OMEGA_DATTN_SM_S); - imadi(e, R(lane8), R(lane), 8u, NOREG); - if (MUT == 2) imadi(e, R(lane8), R(one), OMEGA_DATTN_ROW_BYTES, lane8); - imadi(e, R(negwarp), R(warp), 0xffffffffu, NOREG); - movi(e, ccnt, 16); - mov(e, R(cb), R(base)); - - /* ---- chunk loop ---- */ - const int L_chunk = here(e); - loop_begin(e); - isetp_ge_u32(e, cb, end); - const int bra_done = bra_if_p0_fwd(e); - /* score pass: thread = position cb + tid, clamped to end - 1 (clamped lanes compute a score nobody reads) */ - imadi(e, R(tpos), R(cb), 1u, tid); - imadi(e, R(over), R(tpos), 0xffffffffu, endm1); /* endm1 - tpos */ - shr(e, over, over, 31); /* 1 when tpos > endm1 */ - imadi(e, R(t1), R(tpos), 1u, negendm1); /* tpos - endm1 */ - imadr(e, t1, over, t1, NOREG); - imadi(e, R(t1), R(t1), 0xffffffffu, tpos); /* clamped position */ - imadi(e, R(t1), R(t1), OMEGA_DATTN_ROW_BYTES, cyk); - imadw(e, krow, t1, 1u, pkc); - for (unsigned h = 0; h < 8; h++) movrz(e, R(sc[h])); - for (uint32_t d = 0; d < D; d++) ldg_wide(e, BW_IR_LDG_E_128, kq[d], krow, d * 16u, udesc, d == 0); - imadw(e, kpf, one, D * 16u, krow); - movrz(e, R(qoff)); movrz(e, R(j)); - const int L_q = here(e); - loop_begin(e); - isetp_ge_u32(e, j, ccnt); - const int bra_qend = bra_if_p0_fwd(e); - for (unsigned w = 0; w < 4; w++) { imadi(e, R(wd[2 * w]), S(kq[0], w), 65536u, NOREG); andi(e, R(wd[2 * w + 1]), S(kq[0], w), 0xffff0000u); } - for (unsigned h = 0; h < 8; h++) { - lds128(e, qa4, qoff, h * 512u, 1); lds128(e, qb4, qoff, h * 512u + 16u, 0); - for (unsigned q = 0; q < 4; q++) ffma(e, R(sc[h]), R(wd[q]), S(qa4, q), R(sc[h])); - for (unsigned q = 0; q < 4; q++) ffma(e, R(sc[h]), R(wd[4 + q]), S(qb4, q), R(sc[h])); - } - for (uint32_t d = 0; d + 1 < D; d++) for (unsigned w = 0; w < 4; w++) mov(e, S(kq[d], w), S(kq[d + 1], w)); - ldg_wide(e, BW_IR_LDG_E_128, kq[D - 1], kpf, 0, udesc, 1); - imadw(e, kpf, one, 16u, kpf); - imadi(e, R(qoff), R(one), 32u, qoff); - imadi(e, R(j), R(one), 1u, j); - bra(e, L_q); - loop_end(e); - patch_bra(e, bra_qend, here(e)); - imadi(e, R(sa), R(tid), 32u, smS); - for (unsigned h = 0; h < 8; h++) { imadi(e, R(t1), R(one), h * 4u, sa); sts32(e, t1, R(sc[h])); } - bar_sync(e); - /* V pass: this warp's positions cb + warp + 8 i, i < n = max(0, ceil((min(end, cb + 256) - cb - warp) / 8)) */ - imadi(e, R(t1), R(one), OMEGA_DATTN_CHUNK, cb); /* cb + 256 */ - imadi(e, R(t2), R(t1), 0xffffffffu, end); /* end - (cb + 256) */ - shr(e, over, t2, 31); /* 1 when the chunk is cut short by end */ - imadr(e, t2, over, t2, t1); /* cend = min(end, cb + 256) */ - imadi(e, R(t1), R(cb), 0xffffffffu, t2); /* cend - cb */ - imadi(e, R(t1), R(t1), 1u, negwarp); /* x = end - cb - warp */ - shr(e, over, t1, 31); - imadi(e, R(n), R(one), 7u, t1); - shr(e, n, n, 3); - imadr(e, t2, over, n, NOREG); - imadi(e, R(n), R(t2), 0xffffffffu, n); - imadi(e, R(t1), R(cb), 1u, warp); - imadi(e, R(t1), R(t1), OMEGA_DATTN_ROW_BYTES, lane8); - imadi(e, R(t1), R(t1), 1u, cyk); - imadw(e, vrow, t1, 1u, pvc); - for (uint32_t d = 0; d < D; d++) ldg_wide(e, BW_IR_LDG_E_64, vq[d], vrow, d * 8u * OMEGA_DATTN_ROW_BYTES, udesc, d == 0); - imadw(e, vpf, one, D * 8u * OMEGA_DATTN_ROW_BYTES, vrow); - imadi(e, R(pa), R(warp), 32u, smS); - movrz(e, R(i)); - const int L_v = here(e); - loop_begin(e); - isetp_ge_u32(e, i, n); - const int bra_vend = bra_if_p0_fwd(e); - imadi(e, R(wd[0]), S(vq[0], 0), 65536u, NOREG); andi(e, R(wd[1]), S(vq[0], 0), 0xffff0000u); - imadi(e, R(wd[2]), S(vq[0], 1), 65536u, NOREG); andi(e, R(wd[3]), S(vq[0], 1), 0xffff0000u); - lds128(e, qa4, pa, 0, 1); lds128(e, qb4, pa, 16, 0); - for (unsigned h = 0; h < 8; h++) { fmaxx(e, R(mn), R(m[h]), h < 4 ? S(qa4, h) : S(qb4, h - 4)); fsub(e, R(dl[h]), R(m[h]), R(mn)); mov(e, R(m[h]), R(mn)); } /* dl <= 0; exactly +0 when the max stayed */ - fadd(e, R(dsum), R(dl[0]), R(dl[1])); - for (unsigned h = 2; h < 8; h++) fadd(e, R(dsum), R(dsum), R(dl[h])); - isetp_ge_u32(e, one, dsum); /* 1 >= bits(dsum): no head's max moved (dsum is +0); a negative dsum has the sign bit */ - const int bra_nores = bra_if_p0_fwd(e); - for (unsigned h = 0; h < 8; h++) mufu(e, BW_IR_MUFU_EX2, R(dl[h]), R(dl[h])); - for (unsigned h = 0; h < 8; h++) { for (unsigned q = 0; q < 4; q++) fmul(e, S(acc[h], q), S(acc[h], q), R(dl[h])); fmul(e, R(l[h]), R(l[h]), R(dl[h])); } - patch_bra(e, bra_nores, here(e)); - for (unsigned h = 0; h < 8; h++) { fsub(e, R(dl[h]), h < 4 ? S(qa4, h) : S(qb4, h - 4), R(m[h])); mufu(e, BW_IR_MUFU_EX2, R(dl[h]), R(dl[h])); } - for (unsigned h = 0; h < 8; h++) { for (unsigned q = 0; q < 4; q++) ffma(e, S(acc[h], q), R(dl[h]), R(wd[q]), S(acc[h], q)); fadd(e, R(l[h]), R(l[h]), R(dl[h])); } - for (uint32_t d = 0; d + 1 < D; d++) for (unsigned w = 0; w < 2; w++) mov(e, S(vq[d], w), S(vq[d + 1], w)); - ldg_wide(e, BW_IR_LDG_E_64, vq[D - 1], vpf, 0, udesc, 1); - imadw(e, vpf, one, 8u * OMEGA_DATTN_ROW_BYTES, vpf); - imadi(e, R(pa), R(one), 8u * 32u, pa); - imadi(e, R(i), R(one), 1u, i); - bra(e, L_v); - loop_end(e); - patch_bra(e, bra_vend, here(e)); - bar_sync(e); /* the next score pass overwrites s[][] */ - imadi(e, R(cb), R(one), OMEGA_DATTN_CHUNK, cb); - bra(e, L_chunk); - loop_end(e); - patch_bra(e, bra_done, here(e)); - - /* ---- epilogue: combine the warps, write the partial ---- */ - int ep[8], ea[8], eb[8]; - for (unsigned h = 0; h < 8; h++) { ep[h] = V(e); ea[h] = V(e); eb[h] = V(e); } - int pout = V64(e), ppart = V64(e), pcnt = V64(e), oscale = V(e); - if (e->err) return -1; - ldc64(e, pout, OMEGA_DATTN_ARG_OUT); ldc64(e, ppart, OMEGA_DATTN_ARG_PART); ldc64(e, pcnt, OMEGA_DATTN_ARG_CNT); ldc32(e, oscale, OMEGA_DATTN_ARG_OUT_SCALE); - /* per-warp maxes to shared: lane 0 of warp w writes m[h] at SM_M + w*32 + h*4 */ - imadi(e, R(a0), R(warp), 32u, NOREG); imadi(e, R(a0), R(one), OMEGA_DATTN_SM_M, a0); - isetp_ge_u32(e, lane, one); - { Region r = region_skip_if_p0(e); for (unsigned h = 0; h < 8; h++) { imadi(e, R(t1), R(one), h * 4u, a0); sts32(e, t1, R(m[h])); } region_end(e, r); } - bar_sync(e); - /* lanes 0..7 of every warp: M[lane] = max over the 8 warps; lanes of warp 0 write it at SM_M + 256 + lane*4 */ - movi(e, ccnt, 8); - isetp_ge_u32(e, lane, ccnt); - { - Region r = region_skip_if_p0(e); - imadi(e, R(a1), R(lane), 4u, NOREG); imadi(e, R(a1), R(one), OMEGA_DATTN_SM_M, a1); - for (unsigned w = 0; w < 8; w++) { imadi(e, R(t1), R(one), w * 32u, a1); lds32(e, R(ep[w]), t1, w == 0); } - for (unsigned w = 1; w < 8; w++) fmaxx(e, R(ep[0]), R(ep[0]), R(ep[w])); - imadi(e, R(t1), R(one), 256u, a1); - sts32(e, t1, R(ep[0])); - region_end(e, r); - } - bar_sync(e); - movi(e, t1, OMEGA_DATTN_SM_M + 256u); - lds128(e, qa4, t1, 0, 1); lds128(e, qb4, t1, 16, 0); - for (unsigned h = 0; h < 8; h++) { fsub(e, R(ea[h]), R(m[h]), h < 4 ? S(qa4, h) : S(qb4, h - 4)); mufu(e, BW_IR_MUFU_EX2, R(ea[h]), R(ea[h])); } - for (unsigned h = 0; h < 8; h++) { for (unsigned q = 0; q < 4; q++) fmul(e, S(acc[h], q), S(acc[h], q), R(ea[h])); fmul(e, R(l[h]), R(l[h]), R(ea[h])); } - /* per-warp l to shared, then the partial's m and l by threads 0..7 */ - imadi(e, R(a0), R(warp), 32u, NOREG); imadi(e, R(a0), R(one), OMEGA_DATTN_SM_L, a0); - isetp_ge_u32(e, lane, one); - { Region r = region_skip_if_p0(e); for (unsigned h = 0; h < 8; h++) { imadi(e, R(t1), R(one), h * 4u, a0); sts32(e, t1, R(l[h])); } region_end(e, r); } - bar_sync(e); - int pp = V64(e), pa64 = V64(e), pb64 = V64(e); - imadr(e, t2, cy, splits, cx); - imadi(e, R(t2), R(t2), OMEGA_DATTN_PARTIAL_WORDS * 4u, NOREG); - imadw(e, pp, t2, 1u, ppart); - isetp_ge_u32(e, tid, ccnt); /* tid >= 8 skips */ - { - Region r = region_skip_if_p0(e); - imadi(e, R(a1), R(tid), 4u, NOREG); imadi(e, R(a1), R(one), OMEGA_DATTN_SM_L, a1); - for (unsigned w = 0; w < 8; w++) { imadi(e, R(t1), R(one), w * 32u, a1); lds32(e, R(ep[w]), t1, w == 0); } - for (unsigned w = 1; w < 8; w++) fadd(e, R(ep[0]), R(ep[0]), R(ep[w])); - imadi(e, R(t1), R(tid), 4u, NOREG); imadi(e, R(t1), R(one), OMEGA_DATTN_SM_M + 256u, t1); - lds32(e, R(ep[1]), t1, 1); - imadw(e, pa64, tid, 4u, pp); - stg(e, BW_IR_STG_E, pa64, R(ep[1]), udesc); /* m[h] */ - imadw(e, pb64, one, 32u, pa64); - stg(e, BW_IR_STG_E, pb64, R(ep[0]), udesc); /* l[h] */ - region_end(e, r); - } - /* acc: 4 rounds of 2 heads through the s[][] region: r[w][h'][128]; the 8 store and 8 load addresses computed once */ - imadi(e, R(a0), R(warp), 1024u, NOREG); imadi(e, R(a0), R(lane), 16u, a0); imadi(e, R(a0), R(one), OMEGA_DATTN_SM_S, a0); - imadi(e, R(a1), R(tid), 4u, NOREG); imadi(e, R(a1), R(one), OMEGA_DATTN_SM_S, a1); - for (unsigned q = 0; q < 8; q++) imadi(e, R(eb[q]), R(one), (q / 4u) * 512u + (q % 4u) * 4u, a0); - for (unsigned w = 0; w < 8; w++) imadi(e, R(ea[w]), R(one), w * 1024u, a1); - imadw(e, pa64, tid, 4u, pp); - for (unsigned rd = 0; rd < 4; rd++) { - for (unsigned hh = 0; hh < 2; hh++) for (unsigned q = 0; q < 4; q++) sts32(e, eb[hh * 4 + q], S(acc[2 * rd + hh], q)); - bar_sync(e); - for (unsigned w = 0; w < 8; w++) lds32(e, R(ep[w]), ea[w], w == 0); - for (unsigned w = 1; w < 8; w++) fadd(e, R(ep[0]), R(ep[0]), R(ep[w])); - imadw(e, pb64, one, 64u + rd * 1024u, pa64); - stg(e, BW_IR_STG_E, pb64, R(ep[0]), udesc); - bar_sync(e); - } - /* publish, count, the last CTA combines */ - membar(e); - bar_sync(e); - movi(e, t1, OMEGA_DATTN_SM_FLAG); - isetp_ge_u32(e, tid, one); - { - Region r = region_skip_if_p0(e); - imadw(e, pb64, cy, 4u, pcnt); - atom_add(e, ep[0], pb64, one, udesc); - sts32(e, t1, R(ep[0])); - region_end(e, r); - } - bar_sync(e); - lds32(e, R(ep[0]), t1, 1); - isetp_ge_u32(e, ep[0], splitsm1); - const int bra_last = bra_if_p0_fwd(e); - exit_k(e); - patch_bra(e, bra_last, here(e)); - /* combine: thread tid, outputs o = tid + 256 r (head o >> 7, dim o & 127), r = 0..3 */ - int ho = V(e), oidx = V(e), ma = V64(e), la = V64(e), aa = V64(e), num = V(e), den = V(e), mx = V(e), s_i = V(e), rr = V(e); - if (e->err) return -1; - shr(e, ho, tid, 7); - mov(e, R(oidx), R(tid)); - movrz(e, R(rr)); - movi(e, ccnt, 4); - imadr(e, t2, cy, splits, NOREG); - imadi(e, R(t2), R(t2), OMEGA_DATTN_PARTIAL_WORDS * 4u, NOREG); /* byte offset of partials[cy][0] */ - const int L_r = here(e); - loop_begin(e); - isetp_ge_u32(e, rr, ccnt); - const int bra_rend = bra_if_p0_fwd(e); - imadi(e, R(t1), R(ho), 4u, t2); imadw(e, ma, t1, 1u, ppart); - imadw(e, la, one, 32u, ma); - imadi(e, R(t1), R(oidx), 4u, t2); imadw(e, aa, t1, 1u, ppart); imadw(e, aa, one, 64u, aa); - movrz(e, R(num)); movrz(e, R(den)); movi(e, mx, NEG_BIG); movrz(e, R(s_i)); - const int L_s = here(e); - loop_begin(e); - isetp_ge_u32(e, s_i, splits); - const int bra_send = bra_if_p0_fwd(e); - ldg32(e, BW_IR_LDG_STRONG_SYS, ep[0], ma, udesc, 1); - ldg32(e, BW_IR_LDG_STRONG_SYS, ep[1], la, udesc, 0); - ldg32(e, BW_IR_LDG_STRONG_SYS, ep[2], aa, udesc, 0); - if (MUT != 4) { - fmaxx(e, R(ep[3]), R(mx), R(ep[0])); - fsub(e, R(ep[4]), R(mx), R(ep[3])); mufu(e, BW_IR_MUFU_EX2, R(ep[4]), R(ep[4])); - fsub(e, R(ep[5]), R(ep[0]), R(ep[3])); mufu(e, BW_IR_MUFU_EX2, R(ep[5]), R(ep[5])); - mov(e, R(mx), R(ep[3])); - fmul(e, R(num), R(num), R(ep[4])); fmul(e, R(den), R(den), R(ep[4])); - } else { - movi(e, ep[5], 0x3f800000u); - } - ffma(e, R(num), R(ep[5]), R(ep[2]), R(num)); - ffma(e, R(den), R(ep[5]), R(ep[1]), R(den)); - imadw(e, ma, one, OMEGA_DATTN_PARTIAL_WORDS * 4u, ma); - imadw(e, la, one, OMEGA_DATTN_PARTIAL_WORDS * 4u, la); - imadw(e, aa, one, OMEGA_DATTN_PARTIAL_WORDS * 4u, aa); - imadi(e, R(s_i), R(one), 1u, s_i); - bra(e, L_s); - loop_end(e); - patch_bra(e, bra_send, here(e)); - mufu(e, BW_IR_MUFU_RCP, R(den), R(den)); - fmul(e, R(num), R(num), R(den)); - fmul(e, R(num), R(num), R(oscale)); - imadi(e, R(t1), R(cy), 1024u, oidx); - imadw(e, pb64, t1, 4u, pout); - stg(e, BW_IR_STG_E, pb64, R(num), udesc); - imadi(e, R(ho), R(one), 2u, ho); - imadi(e, R(oidx), R(one), 256u, oidx); - imadi(e, R(rr), R(one), 1u, rr); - bra(e, L_r); - loop_end(e); - patch_bra(e, bra_rend, here(e)); - /* thread 0 resets the counter for the next launch (every other CTA of this head has already added) */ - isetp_ge_u32(e, tid, one); - { Region r = region_skip_if_p0(e); imadw(e, pb64, cy, 4u, pcnt); stg(e, BW_IR_STG_STRONG_SYS, pb64, R(zero), udesc); region_end(e, r); } - tail(e); - if (e->err) return -1; - return omega_bw_regalloc_solve(prog); -} - -int omega_decode_attn_prog_stats(const OmegaDecodeAttnSpec *spec, uint32_t *insns, uint32_t *gprs) { - static BlackwellIRProgram prog; - if (omega_blackwell_codegen_decode_attn_prog(spec, &prog) != 0) return -1; - if (insns) *insns = (uint32_t)prog.count; - if (gprs) *gprs = prog.regalloc.peak_gpr_usage; - return 0; -} diff --git a/src/omega_blackwell_codegen_attn.h b/src/omega_blackwell_codegen_attn.h deleted file mode 100644 index 102a3f2e5..000000000 --- a/src/omega_blackwell_codegen_attn.h +++ /dev/null @@ -1,90 +0,0 @@ -#ifndef OMEGA_BLACKWELL_CODEGEN_ATTN_H -#define OMEGA_BLACKWELL_CODEGEN_ATTN_H -/* - * omega_blackwell_codegen_attn: decode attention for Qwen3-30B-A3B in ONE launch per layer (OWN-30B cut 8, - * 2026-10-13). Grouped-query attention with 8 query heads per KV head, head_dim 128, per-head RMSNorm on q - * and k, rotary embedding from a host-built cos/sin table, the new token's K/V appended to a GPU-resident - * bf16 cache inside the kernel, softmax over the whole context, output ready for o_proj (scaled by - * `out_scale`, so it can be the x' row of the FP8 GEMV without a host round trip). - * - * Grid: (splits, kv_heads). CTA (cx, cy) handles KV head cy and the positions [cx*per_split, min(+per_split, - * pos+1)) of the context; the host picks splits = ceil((pos+1)/per_split), so no CTA is empty. 8 warps = 256 - * threads per CTA (fixed: warp h = query head h of the group in the prologue, lane = position in the score pass). - * - * Per CTA: - * prologue warp h: q[cy*8+h] (f32 quad per lane) -> RMSNorm(q_norm, eps) -> RoPE(table[pos]) -> * qk_scale, - * staged in shared memory as q'[h][128]. Every warp also computes k_new = RoPE(RMSNorm(k[cy])) - * and v_new; the CTA whose range holds `pos` (the last split) writes them as bf16 into the cache at - * [cy][pos] (warp 0 K, warp 1 V), then MEMBAR + BAR.SYNC, so its own stream reads them back. - * chunks of 256 positions: score pass (thread = position: the bf16 K row streamed in 16 quads with a - * `depth`-deep prefetch, 8 dot products against q' from shared memory, scores to shared s[t][8]); - * V pass (warp w: positions w, w+8, ...; lane = 4 dims; online softmax per warp: running max, - * alpha = EX2(m_old - m_new) applied only when the max moved (warp-uniform branch), p = EX2(s - m), - * acc += p * v, l += p). - * epilogue warps combine (m, l, acc) through shared memory and write the CTA partial - * {m[8], l[8], acc[8][128]} to partials[cy][cx]; then one ATOMG.ADD on counters[cy]; the last CTA - * of the KV head combines the partials with the usual max rescale and writes out[(cy*8+h)*128+d] - * * out_scale, and resets the counter. - * - * Numbers (f32 on the chip, double in the host reference, tolerances in tests/gpu_decode_attn_test.c): - * scores in log2 units: q' already carries (1/sqrt(128)) * log2(e), so p = EX2(s - m) = exp((q.k)/sqrt(d) - M). - * bf16 K/V are widened by a 16-bit shift; the new K/V are rounded to bf16 round-to-nearest-even before storing - * (the reference rounds the same way, so the attention of the new token sees the stored value). - * - * The host simulator (tests/bw_warp_sim_k.h) runs this kernel at 8 warps; a simulator pass is never a chip result. - */ -#include "omega_blackwell_codegen.h" -#include - -#define OMEGA_DATTN_HEAD_DIM 128u -#define OMEGA_DATTN_Q_PER_KV 8u -#define OMEGA_DATTN_WARPS 8u -#define OMEGA_DATTN_CHUNK 256u /* positions per score pass = threads per CTA */ -#define OMEGA_DATTN_ROW_BYTES 256u /* one bf16 K or V row */ -#define OMEGA_DATTN_ROPE_ROW_BYTES 512u /* cos[64] then sin[64], f32 */ -#define OMEGA_DATTN_PARTIAL_WORDS 1040u /* m[8], l[8], acc[8][128] */ -#define OMEGA_DATTN_MAX_DEPTH 4u -/* bytes past the last row of a K or V plane the prefetch may touch (clamped reads of the last rows) */ -#define OMEGA_DATTN_PLANE_SLACK (OMEGA_DATTN_MAX_DEPTH * 8u * OMEGA_DATTN_ROW_BYTES + 64u) - -/* constant bank: every address at 0x380 + 8n, then the scalars */ -#define OMEGA_DATTN_ARG_QKV 0x380u /* f32 [32*128 q | 4*128 k | 4*128 v] */ -#define OMEGA_DATTN_ARG_KC 0x388u /* bf16 K cache base: [kv_head][max_pos][128], kv_stride bytes per head */ -#define OMEGA_DATTN_ARG_VC 0x390u /* bf16 V cache base, same layout */ -#define OMEGA_DATTN_ARG_ROPE 0x398u /* f32 [max_pos][cos 64 | sin 64] */ -#define OMEGA_DATTN_ARG_QN 0x3a0u /* f32 [128] q_norm weight */ -#define OMEGA_DATTN_ARG_KN 0x3a8u /* f32 [128] k_norm weight */ -#define OMEGA_DATTN_ARG_OUT 0x3b0u /* f32 [32][128] */ -#define OMEGA_DATTN_ARG_PART 0x3b8u /* f32 [kv_heads][splits][1040] */ -#define OMEGA_DATTN_ARG_CNT 0x3c0u /* u32 [kv_heads], zero before the launch */ -#define OMEGA_DATTN_ARG_POS 0x3c8u /* u32 position of the new token; context = pos + 1 */ -#define OMEGA_DATTN_ARG_PER_SPLIT 0x3ccu /* u32 positions per CTA (multiple of 256) */ -#define OMEGA_DATTN_ARG_SPLITS 0x3d0u /* u32 = grid_x */ -#define OMEGA_DATTN_ARG_KV_STRIDE 0x3d4u /* u32 bytes per KV head plane */ -#define OMEGA_DATTN_ARG_EPS 0x3d8u /* f32 rms eps */ -#define OMEGA_DATTN_ARG_OUT_SCALE 0x3dcu /* f32 multiplier on the output (2^64 for the FP8 GEMV x' row, 1 otherwise) */ -#define OMEGA_DATTN_ARG_QK_SCALE 0x3e0u /* f32 (1/sqrt(128)) * log2(e) */ -#define OMEGA_DATTN_ARGS_WORDS 25u - -/* shared memory map (bytes) */ -#define OMEGA_DATTN_SM_Q 0u /* q'[8][128] f32: 4096 */ -#define OMEGA_DATTN_SM_S 4096u /* s[256][8] f32: 8192; reused as the reduce buffer r[8 warps][256] */ -#define OMEGA_DATTN_SM_M 12288u /* m[8 warps][8] then the head maxes M[8]: 256 + 32 */ -#define OMEGA_DATTN_SM_L 12576u /* l[8 warps][8]: 256 */ -#define OMEGA_DATTN_SM_FLAG 12832u /* the atomic's old value: 16 */ -#define OMEGA_DATTN_SM_K 12864u /* k_new[128] f32 staging: 512 */ -#define OMEGA_DATTN_SHARED_BYTES 13376u - -typedef struct { - uint32_t depth; /* prefetch depth of the K and V streams, 1..4 */ - int mutant; /* TEST ONLY. 0 none; 1 no RoPE on q and k; 2 V read one position late; 3 the new K/V are not - * written to the cache; 4 the cross-split combine ignores the max (differs only when splits > 1). */ -} OmegaDecodeAttnSpec; - -/* Builds the IR program (register-allocated) into *prog. 0 ok, -1 refused (depth 0 or > 4, mutant outside 0..4, - * or the program outgrew BW_MAX_IR_INSNS / BW_MAX_VREGS). */ -int omega_blackwell_codegen_decode_attn_prog(const OmegaDecodeAttnSpec *spec, BlackwellIRProgram *prog); -/* Instruction and peak-register counts of the program (host, no device). */ -int omega_decode_attn_prog_stats(const OmegaDecodeAttnSpec *spec, uint32_t *insns, uint32_t *gprs); - -#endif diff --git a/src/omega_blackwell_codegen_bf16.c b/src/omega_blackwell_codegen_bf16.c deleted file mode 100644 index 08125e6c6..000000000 --- a/src/omega_blackwell_codegen_bf16.c +++ /dev/null @@ -1,262 +0,0 @@ -/* omega_blackwell_codegen_bf16.c: bf16 streaming GEMV kernel (the lm_head), OWN-30B 2026-10-13. See the header. - * The emitter helpers below are copies of omega_blackwell_codegen_moe.c's (same chip-learned scheduling rules: every - * instruction waits on everything still pending; variable-latency results go through scoreboard 0, stores set the read - * barrier 1, uniform loads scoreboard 2; fixed-latency producers stall 6; ISETP stalls 14 before a predicated branch; - * loads of one iteration are issued back to back so a warp has all of them in flight at once). */ -#include "omega_blackwell_codegen_bf16.h" -#ifndef OMEGA_GPU_NO_POSIX -#include -#endif -#include - -#define CW(stall, wbar, rbar, wait) \ - (((uint32_t)(stall) << 9) | ((uint32_t)(wbar) << 14) | ((uint32_t)(rbar) << 17) | ((uint32_t)(wait) << 20)) -#define CW_YIELD 0x2000u -#define SB_NONE 7u -#define SB_GPR 0u -#define SB_RD 1u -#define SB_UGPR 2u -#define NOREG (-1) -#define MAX_LOOPS 4 - -enum { K_FIXED, K_PRED, K_VAR, K_VAR_MORE, K_UVAR, K_STORE, K_SYNC, K_EXIT, K_BRA, K_BSSY, K_BSYNC }; - -typedef struct { - BlackwellIRProgram *p; - uint32_t pending; - int err; - int open_start[MAX_LOOPS]; - int depth; -} Em; - -static void emit(Em *e, BlackwellIRInsn in, int kind) { - uint32_t wait = e->pending; - switch (kind) { - case K_FIXED: in.control = CW(6, SB_NONE, SB_NONE, wait); e->pending = 0; break; - case K_PRED: in.control = CW(14, SB_NONE, SB_NONE, wait); e->pending = 0; break; - /* Loads read their address operands late (the read barrier SB_RD): a later instruction must not overwrite an - * in-flight load's address register. Every load sets SB_RD and the next load of a batch waits on it (cheap: - * the operand read, not the data). Found on the chip: the deep k=2048 kernel hung when the allocator reused a - * dead scale address register as the next load's destination (evidence OWN30B-FP8-GEMV-c4006391). */ - case K_VAR: in.control = CW(4, SB_GPR, SB_RD, wait) | CW_YIELD; e->pending = (1u << SB_GPR) | (1u << SB_RD); break; - /* a further load of the same group: waits on nothing, joins the counting scoreboard */ - case K_VAR_MORE: in.control = CW(4, SB_GPR, SB_RD, (e->pending & (1u << SB_RD))) | CW_YIELD; e->pending |= (1u << SB_GPR) | (1u << SB_RD); break; - case K_UVAR: in.control = CW(4, SB_UGPR, SB_NONE, wait) | CW_YIELD; e->pending = 1u << SB_UGPR; break; - case K_STORE: in.control = CW(4, SB_NONE, SB_RD, wait) | CW_YIELD; e->pending = 1u << SB_RD; break; - case K_SYNC: in.control = CW(6, SB_NONE, SB_NONE, wait); e->pending = 0; break; /* BAR.SYNC after the STS: the attention kernel's chip-proven pattern */ - case K_EXIT: in.control = CW(5, SB_NONE, SB_NONE, wait); e->pending = in.predicate_p0 ? wait : 0; break; - case K_BRA: in.control = CW(5, SB_NONE, SB_NONE, wait); e->pending = in.predicate_p0 ? wait : 0; break; - case K_BSSY: in.control = CW(1, SB_NONE, SB_NONE, wait) | CW_YIELD; e->pending = 0; break; - case K_BSYNC: in.control = CW(5, SB_NONE, SB_NONE, wait) | CW_YIELD; e->pending = 0; break; - default: e->err = -1; return; - } - if (omega_bw_ir_append(e->p, &in) < 0) e->err = -1; -} - -static BlackwellIRInsn I0(BlackwellIROpcode op) { - BlackwellIRInsn in; memset(&in, 0, sizeof in); - in.op = op; in.dst_vreg = NOREG; in.src1_vreg = NOREG; in.src2_vreg = NOREG; in.src3_vreg = NOREG; in.ureg = NOREG; - return in; -} -static int V(Em *e) { int v = omega_bw_ir_alloc_vreg(e->p); if (v < 0) e->err = -1; return v; } -static int V64(Em *e) { int v = omega_bw_ir_alloc_vreg64(e->p); if (v < 0) e->err = -1; return v; } -static int V128(Em *e) { int v = omega_bw_ir_alloc_vreg128(e->p); if (v < 0) e->err = -1; return v; } -static int UV64(Em *e) { int v = omega_bw_ir_alloc_uvreg64(e->p); if (v < 0) e->err = -1; return v; } -static int here(const Em *e) { return (int)e->p->count; } - -static void ldcu64(Em *e, int ud, uint32_t off) { BlackwellIRInsn i = I0(BW_IR_LDCU64); i.dst_vreg = ud; i.imm = off; i.is_uniform = true; emit(e, i, K_UVAR); } -static void ldc64(Em *e, int d, uint32_t off) { BlackwellIRInsn i = I0(BW_IR_LDC64); i.dst_vreg = d; i.imm = off; emit(e, i, K_VAR); } -static void ldc32(Em *e, int d, uint32_t off) { BlackwellIRInsn i = I0(BW_IR_LDC); i.dst_vreg = d; i.imm = off; emit(e, i, K_VAR); } -static void s2r(Em *e, int d, uint32_t sr) { BlackwellIRInsn i = I0(BW_IR_S2R); i.dst_vreg = d; i.imm = sr; emit(e, i, K_VAR); } -static void movi(Em *e, int d, uint32_t imm) { BlackwellIRInsn i = I0(BW_IR_MOV_IMM); i.dst_vreg = d; i.imm = imm; emit(e, i, K_FIXED); } -static void movrz(Em *e, int d) { BlackwellIRInsn i = I0(BW_IR_MOV_RZ); i.dst_vreg = d; emit(e, i, K_FIXED); } -/* d = a * imm + c (c NOREG = RZ); a may be a sub-register of a quad */ -static void imadi(Em *e, int d, int a, unsigned asub, uint32_t imm, int c) { BlackwellIRInsn i = I0(BW_IR_IMAD); i.dst_vreg = d; i.src1_vreg = a; i.src1_subreg = (uint8_t)asub; i.imm = imm; i.src3_vreg = c; emit(e, i, K_FIXED); } - -/* d64 = a * imm + c64 */ -static void imadw(Em *e, int d64, int a, uint32_t imm, int c64) { BlackwellIRInsn i = I0(BW_IR_IMAD_WIDE); i.dst_vreg = d64; i.src1_vreg = a; i.imm = imm; i.src3_vreg = c64; emit(e, i, K_FIXED); } -static void shr(Em *e, int d, int a, unsigned asub, uint32_t imm) { BlackwellIRInsn i = I0(BW_IR_SHF_R); i.dst_vreg = d; i.src1_vreg = a; i.src1_subreg = (uint8_t)asub; i.imm = imm; emit(e, i, K_FIXED); } -static void andi(Em *e, int d, int a, unsigned asub, uint32_t imm) { BlackwellIRInsn i = I0(BW_IR_LOP3_AND); i.dst_vreg = d; i.src1_vreg = a; i.src1_subreg = (uint8_t)asub; i.imm = imm; emit(e, i, K_FIXED); } -static void ldg_wide(Em *e, BlackwellIROpcode op, int d, int addr64, uint32_t imm, int udesc, int first) { - BlackwellIRInsn i = I0(op); i.dst_vreg = d; i.src1_vreg = addr64; i.imm = imm; i.ureg = udesc; emit(e, i, first ? K_VAR : K_VAR_MORE); -} -static void ldg32(Em *e, int d, int addr64, int udesc, int first) { BlackwellIRInsn i = I0(BW_IR_LDG_E); i.dst_vreg = d; i.src1_vreg = addr64; i.ureg = udesc; emit(e, i, first ? K_VAR : K_VAR_MORE); } -static void stg(Em *e, int addr64, uint32_t imm, int val, int udesc) { BlackwellIRInsn i = I0(BW_IR_STG_E); i.src1_vreg = addr64; i.src2_vreg = val; i.imm = imm; i.ureg = udesc; emit(e, i, K_STORE); } -static void lds128(Em *e, int d128, int addr, uint32_t imm, int first) { BlackwellIRInsn i = I0(BW_IR_LDS128); i.dst_vreg = d128; i.src1_vreg = addr; i.imm = imm; emit(e, i, first ? K_VAR : K_VAR_MORE); } -static void sts32(Em *e, int addr, int val) { BlackwellIRInsn i = I0(BW_IR_STS32); i.src1_vreg = addr; i.src2_vreg = val; emit(e, i, K_STORE); } -static void bar_sync(Em *e) { emit(e, I0(BW_IR_BAR_SYNC), K_SYNC); } -static void fadd(Em *e, int d, int a, int b) { BlackwellIRInsn i = I0(BW_IR_FADD); i.dst_vreg = d; i.src1_vreg = a; i.src2_vreg = b; emit(e, i, K_FIXED); } -/* d = a * b[bsub] + c */ -static void ffma(Em *e, int d, int a, int b, unsigned bsub, int c) { BlackwellIRInsn i = I0(BW_IR_FFMA); i.dst_vreg = d; i.src1_vreg = a; i.src2_vreg = b; i.src2_subreg = (uint8_t)bsub; i.src3_vreg = c; emit(e, i, K_FIXED); } -static void shfl_down(Em *e, int d, int a, uint32_t off) { BlackwellIRInsn i = I0(BW_IR_SHFL_DOWN); i.dst_vreg = d; i.src1_vreg = a; i.imm = off; emit(e, i, K_VAR); } -static void isetp_ge_u32(Em *e, int a, int b) { BlackwellIRInsn i = I0(BW_IR_ISETP_GE_U32); i.src1_vreg = a; i.src2_vreg = b; emit(e, i, K_PRED); } -static void bra_if_p0(Em *e, int target) { BlackwellIRInsn i = I0(BW_IR_BRA); i.predicate_p0 = true; i.imm = (uint32_t)(target - here(e)); emit(e, i, K_BRA); } -static void bra(Em *e, int target) { BlackwellIRInsn i = I0(BW_IR_BRA); i.imm = (uint32_t)(target - here(e)); emit(e, i, K_BRA); } -/* a forward branch whose target is patched when known: returns the instruction index */ -static int bra_if_p0_fwd(Em *e) { int at = here(e); BlackwellIRInsn i = I0(BW_IR_BRA); i.predicate_p0 = true; i.imm = 1; emit(e, i, K_BRA); return at; } -static void patch_bra(Em *e, int at, int target) { if (!e->err) e->p->insns[at].imm = (uint32_t)(target - at); } -static int bssy_fwd(Em *e) { int at = here(e); BlackwellIRInsn i = I0(BW_IR_BSSY); i.bar_reg = 0; i.imm = 2; emit(e, i, K_BSSY); return at; } -static void bsync(Em *e) { BlackwellIRInsn i = I0(BW_IR_BSYNC); i.bar_reg = 0; emit(e, i, K_BSYNC); } -static void tail(Em *e) { - emit(e, I0(BW_IR_EXIT), K_EXIT); - BlackwellIRInsn self = I0(BW_IR_BRA); - self.control = 0x000fc000u; - if (omega_bw_ir_append(e->p, &self) < 0) e->err = -1; -} - -/* Loops: a value defined before the loop and read inside it stays live until the back-branch - * (the linear-scan allocator knows no back edges; same rule as the elementwise emitter). */ -static void loop_begin(Em *e) { if (e->depth >= MAX_LOOPS) { e->err = -1; return; } e->open_start[e->depth++] = here(e); } -static void loop_end(Em *e) { - if (e->depth <= 0) { e->err = -1; return; } - int start = e->open_start[--e->depth], end = here(e) - 1; - OmegaRegAlloc *ra = &e->p->regalloc; - for (int v = 0; v < ra->num_vregs; v++) { - OmegaLiveInterval *iv = &ra->intervals[v]; - if (iv->first_def >= 0 && iv->first_def < start && iv->last_use >= start && iv->last_use < end) iv->last_use = end; - } -} - - -static int spec_ok(const OmegaBf16GemvSpec *s) { - if (!s) return 0; - if (s->rows != 1 && s->rows != 2 && s->rows != 4) return 0; - if (s->warps < 1 || s->warps > 32) return 0; - if (s->k == 0 || s->k > OMEGA_BF16_MAX_K || s->k % 256u || s->k % (32u * s->warps)) return 0; - if (s->k > 512u * s->warps) return 0; /* at most 16 staging rounds */ - uint32_t ks = s->k / 256u; - if (s->depth == 0 || s->depth > 16 || ks % s->depth || s->rows * s->depth > 16u) return 0; - if (s->mutant < 0 || s->mutant > 3) return 0; - if ((uint64_t)(s->rows - 1) * s->k * 2u + 512u * s->depth >= (1u << 23)) return 0; /* 24-bit signed LDG immediate */ - return 1; -} -uint32_t omega_bf16_gemv_steps(const OmegaBf16GemvSpec *spec) { return spec_ok(spec) ? spec->k / 256u : 0; } -uint32_t omega_bf16_gemv_shared_bytes(const OmegaBf16GemvSpec *spec) { return spec_ok(spec) ? spec->k * 4u : 0u; } - -int omega_blackwell_codegen_bf16_gemv_prog(const OmegaBf16GemvSpec *spec, BlackwellIRProgram *prog) { - if (!spec_ok(spec) || !prog) return -1; - const uint32_t K = spec->k, R = spec->rows, W = spec->warps, D = spec->depth, KS = K / 256u; - omega_bw_ir_init(prog); - Em em; memset(&em, 0, sizeof em); em.p = prog; - Em *e = &em; - - /* ---- prologue ---- */ - int udesc = UV64(e), pwb = V64(e), px = V64(e), py = V64(e), rgn = V(e), stride = V(e); - int tid = V(e), cx = V(e), warp = V(e), lane = V(e), one = V(e); - int wlane = V(e), rg = V(e), xl0 = V(e); - if (e->err) return -1; - ldcu64(e, udesc, 0x358); - ldc64(e, pwb, OMEGA_BF16_ARG_W); ldc64(e, px, OMEGA_BF16_ARG_X); ldc64(e, py, OMEGA_BF16_ARG_Y); - ldc32(e, rgn, OMEGA_BF16_ARG_RG); ldc32(e, stride, OMEGA_BF16_ARG_STRIDE); - s2r(e, tid, BW_SR_TID_X); s2r(e, cx, BW_SR_CTAID_X); - movi(e, one, 1); - shr(e, warp, tid, 0, 5); - andi(e, lane, tid, 0, 31); - imadi(e, wlane, lane, 0, 16, NOREG); /* byte offset of this lane's chunk in a row step */ - imadi(e, rg, cx, 0, W, warp); /* first row group of this warp */ - imadi(e, xl0, lane, 0, 32, NOREG); /* this lane's x chunk (8 floats) of step 0, shared byte offset */ - - /* ---- stage x (k floats) in shared memory: one 32-bit word per thread per round ---- */ - { - const uint32_t rounds = K / (32u * W); - int pxt = V64(e), sa0 = V(e), sa = V(e), pxs[16], xst[16]; - for (uint32_t j = 0; j < rounds; j++) { pxs[j] = V64(e); xst[j] = V(e); } - if (e->err) return -1; - imadw(e, pxt, tid, 4, px); - imadi(e, sa0, tid, 0, 4, NOREG); - for (uint32_t j = 0; j < rounds; j++) imadw(e, pxs[j], one, j * 128u * W, pxt); - for (uint32_t j = 0; j < rounds; j++) ldg32(e, xst[j], pxs[j], udesc, j == 0); - for (uint32_t j = 0; j < rounds; j++) { imadi(e, sa, one, 0, j * 128u * W, sa0); sts32(e, sa, xst[j]); } - bar_sync(e); - } - - int pw = V64(e), pwr = V64(e), xl = V(e), cnt = V(e), pyr = V64(e), pya = V64(e), t = V(e), f = V(e); - int acc[4], wq[4][16], xq[2]; - for (uint32_t r = 0; r < R; r++) { acc[r] = V(e); for (uint32_t s = 0; s < D; s++) wq[r][s] = V128(e); } - xq[0] = V128(e); xq[1] = V128(e); - if (e->err) return -1; - - /* ---- row-group loop ---- */ - const int L_rg = here(e); - loop_begin(e); - isetp_ge_u32(e, rg, rgn); - const int bra_end = bra_if_p0_fwd(e); - imadw(e, pwr, rg, R * K * 2u, pwb); imadw(e, pw, wlane, 1, pwr); - imadi(e, xl, xl0, 0, 1, NOREG); - for (uint32_t r = 0; r < R; r++) movrz(e, acc[r]); - movi(e, cnt, KS / D); - - /* ---- K loop: D steps per iteration, every load issued before any use ---- */ - const int L_k = here(e); - loop_begin(e); - { - int first = 1; - for (uint32_t s = 0; s < D; s++) for (uint32_t r = 0; r < R; r++) { ldg_wide(e, BW_IR_LDG_E_128, wq[r][s], pw, r * K * 2u + s * OMEGA_BF16_STEP_BYTES, udesc, first); first = 0; } - } - for (uint32_t s = 0; s < D; s++) { - const uint32_t xoff = spec->mutant == 2 ? 0u : s * 1024u; - lds128(e, xq[0], xl, xoff, 1); lds128(e, xq[1], xl, xoff + 16u, 0); - for (uint32_t r = 0; r < R; r++) { - const uint32_t words = spec->mutant == 3 ? 3u : 4u; - for (uint32_t word = 0; word < words; word++) { - const int q = xq[word >> 1]; const unsigned sub = (word & 1u) * 2u; - /* low half = element 2 word, high half = element 2 word + 1 */ - imadi(e, f, wq[r][s], word, 65536u, NOREG); - ffma(e, acc[r], f, q, spec->mutant == 1 ? sub + 1u : sub, acc[r]); - andi(e, f, wq[r][s], word, 0xffff0000u); - ffma(e, acc[r], f, q, spec->mutant == 1 ? sub : sub + 1u, acc[r]); - } - } - } - imadw(e, pw, one, D * OMEGA_BF16_STEP_BYTES, pw); - imadi(e, xl, one, 0, spec->mutant == 2 ? 0u : D * 1024u, xl); - imadi(e, cnt, one, 0, 0xffffffffu, cnt); - isetp_ge_u32(e, cnt, one); - bra_if_p0(e, L_k); - loop_end(e); - - /* ---- warp reduction, lane 0 stores ---- */ - for (uint32_t r = 0; r < R; r++) - for (uint32_t o = 16; o >= 1; o /= 2) { shfl_down(e, t, acc[r], o); fadd(e, acc[r], acc[r], t); } - imadw(e, pyr, rg, R * 4u, py); - isetp_ge_u32(e, lane, one); - const int at_bssy = bssy_fwd(e); - const int bra_skip = bra_if_p0_fwd(e); - for (uint32_t r = 0; r < R; r++) { imadw(e, pya, one, r * 4u, pyr); stg(e, pya, 0, acc[r], udesc); } - const int at_bsync = here(e); - bsync(e); - patch_bra(e, bra_skip, at_bsync); - patch_bra(e, at_bssy, at_bsync + 1); - imadi(e, rg, stride, 0, 1, rg); - bra(e, L_rg); - loop_end(e); - const int L_end = here(e); - patch_bra(e, bra_end, L_end); - tail(e); - if (e->err) return -1; - return omega_bw_regalloc_solve(prog); -} - -int omega_bf16_gemv_prog_stats(const OmegaBf16GemvSpec *spec, uint32_t *insns, uint32_t *gprs) { - static BlackwellIRProgram prog; - if (omega_blackwell_codegen_bf16_gemv_prog(spec, &prog) != 0) return -1; - if (insns) *insns = (uint32_t)prog.count; - if (gprs) *gprs = prog.regalloc.peak_gpr_usage; - return 0; -} - -#ifndef OMEGA_GPU_NO_POSIX -int omega_blackwell_codegen_bf16_gemv(const OmegaBf16GemvSpec *spec, OmegaBlackwellKernel *kernel) { - if (!kernel) return -1; - BlackwellIRProgram *prog = calloc(1, sizeof *prog); - if (!prog) return -1; - int rc = omega_blackwell_codegen_bf16_gemv_prog(spec, prog); - size_t need = rc == 0 ? omega_bw_program_code_bytes(prog) : 0; - uint8_t *buf = need ? malloc(need) : NULL; - if (!buf) { free(prog); return -1; } - rc = omega_blackwell_kernel_encode_into(prog, buf, need, kernel); - free(prog); - if (rc != 0) { free(buf); return -1; } - return 0; -} -#endif diff --git a/src/omega_blackwell_codegen_bf16.h b/src/omega_blackwell_codegen_bf16.h deleted file mode 100644 index c7a22cdfd..000000000 --- a/src/omega_blackwell_codegen_bf16.h +++ /dev/null @@ -1,67 +0,0 @@ -#ifndef OMEGA_BLACKWELL_CODEGEN_BF16_H -#define OMEGA_BLACKWELL_CODEGEN_BF16_H -/* - * omega_blackwell_codegen_bf16: bf16 streaming GEMV (the lm_head: vocab x hidden, bf16 weights, f32 x and y) for the - * OWN-30B lane (2026-10-13). New file; it mirrors the structure of omega_blackwell_codegen_moe.c (the FP8 GEMV) with - * no scales and no group table. - * - * y[r] = sum_c W[r][c] * x[c], W bf16 row-major [n][k], x f32 [k], y f32 [n], f32 accumulate. - * - * bf16 -> f32 is exact and free: element 2i of a 32-bit weight word is (word << 16), element 2i+1 is - * (word & 0xFFFF0000). Thread mapping (as the FP8 kernel): a CTA has `warps` warps; warp w of CTA cx handles row - * groups rg = cx * warps + w, rg + stride, ... < RG (RG = n / rows, a row group is `rows` consecutive rows). Per K - * step a warp reads 512 contiguous bytes of each of its rows (lane L: one LDG.E.128 = 8 bf16 at byte 16 L); the CTA - * stages the whole x row in shared memory once (k * 4 bytes, one BAR.SYNC) and every loop iteration issues the loads - * of `depth` K steps of all its rows before using any of them. Per lane the 8 x depth x rows products of a row - * accumulate in one FFMA chain (element order: step, word, low half, high half), then a SHFL.DOWN tree (16,8,4,2,1) - * and lane 0 stores. - * - * Constant bank 0 argument words (cbank + 0x380): - * 0x380 w u64 weights base (row r at w + r * k * 2) - * 0x388 x u64 x row (f32, k) - * 0x390 y u64 y (f32, n) - * 0x398 RG u32 row groups (n / rows) - * 0x39c stride u32 row groups per warp iteration (grid_x * warps) - * Grid: grid_x CTAs, grid_y = 1. The kernel does not depend on n or the grid: one kernel per (k, rows, warps, depth, - * mutant) serves every n. - * - * mutant (tests only): 1 pairs the two halves of every word with the wrong x (low half takes the high half's x and - * back), 2 never advances the x pointer (every step uses step 0's x: wrong whenever KS >= 2), 3 drops the last word - * of each lane chunk. Each must be caught by the parity test. - */ -#include -#include "omega_blackwell_codegen.h" - -#ifdef __cplusplus -extern "C" { -#endif - -#define OMEGA_BF16_MAX_K 16384u -#define OMEGA_BF16_ARG_W 0x380u -#define OMEGA_BF16_ARG_X 0x388u -#define OMEGA_BF16_ARG_Y 0x390u -#define OMEGA_BF16_ARG_RG 0x398u -#define OMEGA_BF16_ARG_STRIDE 0x39cu -#define OMEGA_BF16_ARGS_WORDS 8u -#define OMEGA_BF16_STEP_BYTES 512u /* weight bytes per row per K step (32 lanes x 16) */ - -typedef struct { - uint32_t k; /* multiple of 256 (one step) and of 32 * warps (exact staging rounds), <= OMEGA_BF16_MAX_K */ - uint32_t rows; /* rows per warp: 1, 2 or 4 */ - uint32_t warps; /* 1 .. 32 (the host simulator runs up to 8) */ - uint32_t depth; /* K steps per loop iteration: divides k / 256; rows * depth <= 16 */ - int mutant; /* 0 production; 1, 2, 3 as above */ -} OmegaBf16GemvSpec; - -int omega_blackwell_codegen_bf16_gemv_prog(const OmegaBf16GemvSpec *spec, BlackwellIRProgram *prog); -int omega_blackwell_codegen_bf16_gemv(const OmegaBf16GemvSpec *spec, OmegaBlackwellKernel *kernel); -/* K steps: k / 256 (0 when refused). */ -uint32_t omega_bf16_gemv_steps(const OmegaBf16GemvSpec *spec); -/* Shared bytes the launch declares: k * 4. */ -uint32_t omega_bf16_gemv_shared_bytes(const OmegaBf16GemvSpec *spec); -int omega_bf16_gemv_prog_stats(const OmegaBf16GemvSpec *spec, uint32_t *insns, uint32_t *gprs); - -#ifdef __cplusplus -} -#endif -#endif diff --git a/src/omega_blackwell_codegen_dec.c b/src/omega_blackwell_codegen_dec.c deleted file mode 100644 index b2ec67d7f..000000000 --- a/src/omega_blackwell_codegen_dec.c +++ /dev/null @@ -1,368 +0,0 @@ -/* - * omega_blackwell_codegen_dec.c: decode glue kernels (see the header). The emitter helpers are the router's - * (omega_blackwell_codegen_router.c): control words, read barrier on every load, loop live ranges, lane-0 regions. - */ -#include "omega_blackwell_codegen_dec.h" -#include - -#define CW(stall, wbar, rbar, wait) \ - (((uint32_t)(stall) << 9) | ((uint32_t)(wbar) << 14) | ((uint32_t)(rbar) << 17) | ((uint32_t)(wait) << 20)) -#define CW_YIELD 0x2000u -#define SB_NONE 7u -#define SB_GPR 0u -#define SB_RD 1u -#define SB_UGPR 2u -#define NOREG (-1) -#define MAX_LOOPS 4 - -enum { K_FIXED, K_PRED, K_VAR, K_VAR_MORE, K_UVAR, K_STORE, K_SYNC, K_EXIT, K_BRA, K_BSSY, K_BSYNC }; - -typedef struct { - BlackwellIRProgram *p; - uint32_t pending; - int err; - int open_start[MAX_LOOPS]; - int depth; -} Em; - -static void emit(Em *e, BlackwellIRInsn in, int kind) { - uint32_t wait = e->pending; - switch (kind) { - case K_FIXED: in.control = CW(6, SB_NONE, SB_NONE, wait); e->pending = 0; break; - case K_PRED: in.control = CW(14, SB_NONE, SB_NONE, wait); e->pending = 0; break; - case K_VAR: in.control = CW(4, SB_GPR, SB_RD, wait) | CW_YIELD; e->pending = (1u << SB_GPR) | (1u << SB_RD); break; - case K_VAR_MORE: in.control = CW(4, SB_GPR, SB_RD, (e->pending & (1u << SB_RD))) | CW_YIELD; e->pending |= (1u << SB_GPR) | (1u << SB_RD); break; - case K_UVAR: in.control = CW(4, SB_UGPR, SB_NONE, wait) | CW_YIELD; e->pending = 1u << SB_UGPR; break; - case K_STORE: in.control = CW(4, SB_NONE, SB_RD, wait) | CW_YIELD; e->pending = 1u << SB_RD; break; - case K_SYNC: in.control = CW(6, SB_NONE, SB_NONE, wait); e->pending = 0; break; - case K_EXIT: in.control = CW(5, SB_NONE, SB_NONE, wait); e->pending = in.predicate_p0 ? wait : 0; break; - case K_BRA: in.control = CW(5, SB_NONE, SB_NONE, wait); e->pending = in.predicate_p0 ? wait : 0; break; - case K_BSSY: in.control = CW(1, SB_NONE, SB_NONE, wait) | CW_YIELD; e->pending = 0; break; - case K_BSYNC: in.control = CW(5, SB_NONE, SB_NONE, wait) | CW_YIELD; e->pending = 0; break; - default: e->err = -1; return; - } - if (omega_bw_ir_append(e->p, &in) < 0) e->err = -1; -} - -static BlackwellIRInsn I0(BlackwellIROpcode op) { - BlackwellIRInsn i; memset(&i, 0, sizeof i); - i.op = op; i.dst_vreg = NOREG; i.src1_vreg = NOREG; i.src2_vreg = NOREG; i.src3_vreg = NOREG; i.ureg = NOREG; - return i; -} -static int V(Em *e) { int v = omega_bw_ir_alloc_vreg(e->p); if (v < 0) e->err = -1; return v; } -static int V64(Em *e) { int v = omega_bw_ir_alloc_vreg64(e->p); if (v < 0) e->err = -1; return v; } -static int V128(Em *e) { int v = omega_bw_ir_alloc_vreg128(e->p); if (v < 0) e->err = -1; return v; } -static int UV64(Em *e) { int v = omega_bw_ir_alloc_uvreg64(e->p); if (v < 0) e->err = -1; return v; } -static int here(const Em *e) { return (int)e->p->count; } - -typedef struct { int v; unsigned s; } Op; -static Op R(int v) { Op o = { v, 0 }; return o; } -static Op S(int v, unsigned s) { Op o = { v, s }; return o; } - -static void ldcu64(Em *e, int ud, uint32_t off) { BlackwellIRInsn i = I0(BW_IR_LDCU64); i.dst_vreg = ud; i.imm = off; i.is_uniform = true; emit(e, i, K_UVAR); } -static void ldc64(Em *e, int d, uint32_t off) { BlackwellIRInsn i = I0(BW_IR_LDC64); i.dst_vreg = d; i.imm = off; emit(e, i, K_VAR); } -static void ldc32(Em *e, int d, uint32_t off) { BlackwellIRInsn i = I0(BW_IR_LDC); i.dst_vreg = d; i.imm = off; emit(e, i, K_VAR); } -static void s2r(Em *e, int d, uint32_t sr) { BlackwellIRInsn i = I0(BW_IR_S2R); i.dst_vreg = d; i.imm = sr; emit(e, i, K_VAR); } -static void movi(Em *e, int d, uint32_t imm) { BlackwellIRInsn i = I0(BW_IR_MOV_IMM); i.dst_vreg = d; i.imm = imm; emit(e, i, K_FIXED); } -static void movrz(Em *e, Op d) { BlackwellIRInsn i = I0(BW_IR_MOV_RZ); i.dst_vreg = d.v; i.dst_subreg = (uint8_t)d.s; emit(e, i, K_FIXED); } -static void imadi(Em *e, Op d, Op a, uint32_t imm, int c) { BlackwellIRInsn i = I0(BW_IR_IMAD); i.dst_vreg = d.v; i.dst_subreg = (uint8_t)d.s; i.src1_vreg = a.v; i.src1_subreg = (uint8_t)a.s; i.imm = imm; i.src3_vreg = c; emit(e, i, K_FIXED); } -static void imadw(Em *e, int d64, int a, uint32_t imm, int c64) { BlackwellIRInsn i = I0(BW_IR_IMAD_WIDE); i.dst_vreg = d64; i.src1_vreg = a; i.imm = imm; i.src3_vreg = c64; emit(e, i, K_FIXED); } -static void mov(Em *e, Op d, Op a) { imadi(e, d, a, 1u, NOREG); } -static void shr(Em *e, int d, int a, uint32_t imm) { BlackwellIRInsn i = I0(BW_IR_SHF_R); i.dst_vreg = d; i.src1_vreg = a; i.imm = imm; emit(e, i, K_FIXED); } -static void andi(Em *e, Op d, Op a, uint32_t imm) { BlackwellIRInsn i = I0(BW_IR_LOP3_AND); i.dst_vreg = d.v; i.dst_subreg = (uint8_t)d.s; i.src1_vreg = a.v; i.src1_subreg = (uint8_t)a.s; i.imm = imm; emit(e, i, K_FIXED); } -static void xorr(Em *e, Op d, Op a, Op b) { BlackwellIRInsn i = I0(BW_IR_LOP3_XOR); i.dst_vreg = d.v; i.dst_subreg = (uint8_t)d.s; i.src1_vreg = a.v; i.src1_subreg = (uint8_t)a.s; i.src2_vreg = b.v; i.src2_subreg = (uint8_t)b.s; emit(e, i, K_FIXED); } -static void lop3(Em *e, int d, int a, int b, int c, uint32_t lut) { BlackwellIRInsn i = I0(BW_IR_LOP3_LUT); i.dst_vreg = d; i.src1_vreg = a; i.src2_vreg = b; i.src3_vreg = c; i.imm = lut; emit(e, i, K_FIXED); } -static void ldg(Em *e, BlackwellIROpcode op, int d, int addr64, uint32_t imm, int udesc, int first) { - BlackwellIRInsn i = I0(op); i.dst_vreg = d; i.src1_vreg = addr64; i.imm = imm; i.ureg = udesc; emit(e, i, first ? K_VAR : K_VAR_MORE); -} -static void stg(Em *e, int addr64, Op val, int udesc) { BlackwellIRInsn i = I0(BW_IR_STG_E); i.src1_vreg = addr64; i.src2_vreg = val.v; i.src2_subreg = (uint8_t)val.s; i.ureg = udesc; emit(e, i, K_STORE); } -static void lds128(Em *e, int d128, int addr, uint32_t imm, int first) { BlackwellIRInsn i = I0(BW_IR_LDS128); i.dst_vreg = d128; i.src1_vreg = addr; i.imm = imm; emit(e, i, first ? K_VAR : K_VAR_MORE); } -static void sts32(Em *e, int addr, Op val) { BlackwellIRInsn i = I0(BW_IR_STS32); i.src1_vreg = addr; i.src2_vreg = val.v; i.src2_subreg = (uint8_t)val.s; emit(e, i, K_STORE); } -static void bar_sync(Em *e) { emit(e, I0(BW_IR_BAR_SYNC), K_SYNC); } -static void fop(Em *e, BlackwellIROpcode op, Op d, Op a, Op b, Op c) { - BlackwellIRInsn i = I0(op); i.dst_vreg = d.v; i.dst_subreg = (uint8_t)d.s; i.src1_vreg = a.v; i.src1_subreg = (uint8_t)a.s; - i.src2_vreg = b.v; i.src2_subreg = (uint8_t)b.s; i.src3_vreg = c.v; i.src3_subreg = (uint8_t)c.s; emit(e, i, K_FIXED); -} -static void fadd(Em *e, Op d, Op a, Op b) { fop(e, BW_IR_FADD, d, a, b, R(NOREG)); } -static void fmul(Em *e, Op d, Op a, Op b) { fop(e, BW_IR_FMUL, d, a, b, R(NOREG)); } -static void ffma(Em *e, Op d, Op a, Op b, Op c) { fop(e, BW_IR_FFMA, d, a, b, c); } -static void fmaxx(Em *e, Op d, Op a, Op b) { fop(e, BW_IR_FMNMX_MAX, d, a, b, R(NOREG)); } -static void fminn(Em *e, Op d, Op a, Op b) { fop(e, BW_IR_FMNMX_MIN, d, a, b, R(NOREG)); } -static void mufu(Em *e, BlackwellIROpcode op, Op d, Op a) { BlackwellIRInsn i = I0(op); i.dst_vreg = d.v; i.dst_subreg = (uint8_t)d.s; i.src1_vreg = a.v; i.src1_subreg = (uint8_t)a.s; emit(e, i, K_VAR); } -static void shfl_down(Em *e, int d, int a, uint32_t off) { BlackwellIRInsn i = I0(BW_IR_SHFL_DOWN); i.dst_vreg = d; i.src1_vreg = a; i.imm = off; emit(e, i, K_VAR); } -static void isetp_ge_u32(Em *e, int a, int b) { BlackwellIRInsn i = I0(BW_IR_ISETP_GE_U32); i.src1_vreg = a; i.src2_vreg = b; emit(e, i, K_PRED); } -static void bra(Em *e, int target) { BlackwellIRInsn i = I0(BW_IR_BRA); i.imm = (uint32_t)(target - here(e)); emit(e, i, K_BRA); } -static int bra_if_p0_fwd(Em *e) { int at = here(e); BlackwellIRInsn i = I0(BW_IR_BRA); i.predicate_p0 = true; i.imm = 1; emit(e, i, K_BRA); return at; } -static void patch_bra(Em *e, int at, int target) { if (!e->err) e->p->insns[at].imm = (uint32_t)(target - at); } -static int bssy_fwd(Em *e) { int at = here(e); BlackwellIRInsn i = I0(BW_IR_BSSY); i.bar_reg = 0; i.imm = 2; emit(e, i, K_BSSY); return at; } -static void bsync(Em *e) { BlackwellIRInsn i = I0(BW_IR_BSYNC); i.bar_reg = 0; emit(e, i, K_BSYNC); } -static void exit_k(Em *e) { emit(e, I0(BW_IR_EXIT), K_EXIT); } -static void tail(Em *e) { - exit_k(e); - BlackwellIRInsn self = I0(BW_IR_BRA); - self.control = 0x000fc000u; - if (omega_bw_ir_append(e->p, &self) < 0) e->err = -1; -} -static void loop_begin(Em *e) { if (e->depth >= MAX_LOOPS) { e->err = -1; return; } e->open_start[e->depth++] = here(e); } -static void loop_end(Em *e) { - if (e->depth <= 0) { e->err = -1; return; } - int start = e->open_start[--e->depth], end = here(e) - 1; - OmegaRegAlloc *ra = &e->p->regalloc; - for (int v = 0; v < ra->num_vregs; v++) { - OmegaLiveInterval *iv = &ra->intervals[v]; - if (iv->first_def >= 0 && iv->first_def < start && iv->last_use >= start && iv->last_use < end) iv->last_use = end; - } -} -typedef struct { int bssy, bra; } Region; -static Region region_skip_if_p0(Em *e) { Region r; r.bssy = bssy_fwd(e); r.bra = bra_if_p0_fwd(e); return r; } -static void region_end(Em *e, Region r) { int at = here(e); bsync(e); patch_bra(e, r.bra, at); patch_bra(e, r.bssy, at + 1); } -static void ne_of(Em *e, int ne, Op a, Op b, int t, int t2) { - xorr(e, R(t), a, b); - imadi(e, R(t2), R(t), 0xffffffffu, NOREG); - lop3(e, t, t, t2, NOREG, 0xfcu); - shr(e, ne, t, 31); -} -static void warp_sum(Em *e, int v, int t) { for (uint32_t o = 16; o >= 1; o /= 2) { shfl_down(e, t, v, o); fadd(e, R(v), R(v), R(t)); } } -static void warp_max(Em *e, int v, int t) { for (uint32_t o = 16; o >= 1; o /= 2) { shfl_down(e, t, v, o); fmaxx(e, R(v), R(v), R(t)); } } -static void warp_min(Em *e, int v, int t) { for (uint32_t o = 16; o >= 1; o /= 2) { shfl_down(e, t, v, o); fminn(e, R(v), R(v), R(t)); } } -static void lane0_sts(Em *e, int lane, int one, int addr, Op v) { - isetp_ge_u32(e, lane, one); - Region r = region_skip_if_p0(e); sts32(e, addr, v); region_end(e, r); -} -/* the 8 per-warp partials at shared [base]: reduce with op (0 sum, 1 max, 2 min) into d (every thread) */ -static void cta_reduce8(Em *e, int d, uint32_t base, int op, int a0) { - int q0 = V128(e), q1 = V128(e); - if (e->err) return; - movi(e, a0, base); - lds128(e, q0, a0, 0, 1); lds128(e, q1, a0, 16, 0); - mov(e, R(d), S(q0, 0)); - for (unsigned i = 1; i < 8; i++) { - Op s = i < 4 ? S(q0, i) : S(q1, i - 4); - if (op == 0) fadd(e, R(d), R(d), s); else if (op == 1) fmaxx(e, R(d), R(d), s); else fminn(e, R(d), R(d), s); - } -} - -static const uint32_t C_INV2048 = 0x3A000000u; /* 1/2048 */ -static const uint32_t C_NLOG2E = 0xBFB8AA3Bu; /* -1.4426950f */ -static const uint32_t C_ONE = 0x3F800000u; -static const uint32_t C_2P23 = 0x4B000000u; - -static int gen_axpy(Em *e, const OmegaDecSpec *sp) { - if (sp->stride == 0 || sp->stride % OMEGA_DEC_THREADS) return -1; - int udesc = UV64(e), px = V64(e), pa = V64(e), iters = V(e), tid = V(e), cta = V(e), gid = V(e), it = V(e), one = V(e); - int xa = V64(e), aa = V64(e), xv = V(e), av = V(e); - if (e->err) return -1; - ldcu64(e, udesc, 0x358); - ldc64(e, px, OMEGA_DEC_AXPY_X); ldc64(e, pa, OMEGA_DEC_AXPY_A); ldc32(e, iters, OMEGA_DEC_AXPY_ITERS); - s2r(e, tid, BW_SR_TID_X); s2r(e, cta, BW_SR_CTAID_X); - imadi(e, R(gid), R(cta), OMEGA_DEC_THREADS, tid); - imadw(e, xa, gid, 4u, px); imadw(e, aa, gid, 4u, pa); - movrz(e, R(it)); movi(e, one, 1); - const int L = here(e); - loop_begin(e); - isetp_ge_u32(e, it, iters); - const int done = bra_if_p0_fwd(e); - ldg(e, BW_IR_LDG_E, xv, xa, 0, udesc, 1); ldg(e, BW_IR_LDG_E, av, aa, 0, udesc, 0); - fadd(e, R(xv), R(xv), R(av)); - stg(e, xa, R(xv), udesc); - imadw(e, xa, one, sp->stride * 4u, xa); imadw(e, aa, one, sp->stride * 4u, aa); - imadi(e, R(it), R(one), 1u, it); - bra(e, L); - loop_end(e); - patch_bra(e, done, here(e)); - tail(e); - return e->err; -} - -static int gen_res(Em *e, const OmegaDecSpec *sp) { - const uint32_t M = sp->mode; - if (M == 0 || M > 7 || ((M & OMEGA_DEC_RES_ADD) && (M & OMEGA_DEC_RES_COMBINE))) return -1; - int udesc = UV64(e), px = V64(e), pa = V64(e), pw = V64(e), pnw = V64(e), py = V64(e), eps = V(e), scale = V(e); - int tid = V(e), warp = V(e), lane = V(e), one = V(e), toff = V(e), t = V(e), t2 = V(e), a0 = V(e); - int xa = V64(e), xq0 = V128(e), xq1 = V128(e); - if (e->err) return -1; - ldcu64(e, udesc, 0x358); - ldc64(e, px, OMEGA_DEC_RES_X); ldc64(e, pa, OMEGA_DEC_RES_A); ldc64(e, pw, OMEGA_DEC_RES_W); ldc64(e, pnw, OMEGA_DEC_RES_NW); ldc64(e, py, OMEGA_DEC_RES_Y); - ldc32(e, eps, OMEGA_DEC_RES_EPS); ldc32(e, scale, OMEGA_DEC_RES_SCALE); - s2r(e, tid, BW_SR_TID_X); - movi(e, one, 1); - shr(e, warp, tid, 5); - andi(e, R(lane), R(tid), 31u); - imadi(e, R(toff), R(tid), 32u, NOREG); - imadw(e, xa, toff, 1u, px); - ldg(e, BW_IR_LDG_E_128, xq0, xa, 0, udesc, 1); ldg(e, BW_IR_LDG_E_128, xq1, xa, 16, udesc, 0); - Op x[8]; - for (unsigned i = 0; i < 8; i++) x[i] = i < 4 ? S(xq0, i) : S(xq1, i - 4); - if (M & OMEGA_DEC_RES_ADD) { - int aa = V64(e), aq0 = V128(e), aq1 = V128(e); - if (e->err) return -1; - imadw(e, aa, toff, 1u, pa); - ldg(e, BW_IR_LDG_E_128, aq0, aa, 0, udesc, 1); ldg(e, BW_IR_LDG_E_128, aq1, aa, 16, udesc, 0); - for (unsigned i = 0; i < 8; i++) fadd(e, x[i], x[i], i < 4 ? S(aq0, i) : S(aq1, i - 4)); - } - if (M & OMEGA_DEC_RES_COMBINE) { - int wq0 = V128(e), wq1 = V128(e), da = V64(e), dq0 = V128(e), dq1 = V128(e), m[8]; - for (unsigned i = 0; i < 8; i++) m[i] = V(e); - if (e->err) return -1; - ldg(e, BW_IR_LDG_E_128, wq0, pw, 0, udesc, 1); ldg(e, BW_IR_LDG_E_128, wq1, pw, 16, udesc, 0); - imadw(e, da, toff, 1u, pa); - const unsigned G = sp->mutant == 2 ? 7u : 8u; - for (unsigned g = 0; g < G; g++) { - ldg(e, BW_IR_LDG_E_128, dq0, da, g * OMEGA_DEC_HIDDEN * 4u, udesc, 1); ldg(e, BW_IR_LDG_E_128, dq1, da, g * OMEGA_DEC_HIDDEN * 4u + 16u, udesc, 0); - Op w = g < 4 ? S(wq0, g) : S(wq1, g - 4); - for (unsigned i = 0; i < 8; i++) { - Op d = i < 4 ? S(dq0, i) : S(dq1, i - 4); - if (g == 0) fmul(e, R(m[i]), w, d); - else { fmul(e, R(t), w, d); fadd(e, R(m[i]), R(m[i]), R(t)); } - } - } - for (unsigned i = 0; i < 8; i++) fadd(e, x[i], x[i], R(m[i])); - } - if (M & (OMEGA_DEC_RES_ADD | OMEGA_DEC_RES_COMBINE)) { - int sa = V64(e); - if (e->err) return -1; - for (unsigned i = 0; i < 8; i++) { if (i == 0) imadw(e, sa, toff, 1u, px); else imadw(e, sa, one, 4u, sa); stg(e, sa, x[i], udesc); } - } - if (M & OMEGA_DEC_RES_NORM) { - int sq = V(e), rs = V(e), na = V64(e), nq0 = V128(e), nq1 = V128(e), ya = V64(e), yv = V(e); - if (e->err) return -1; - imadw(e, na, toff, 1u, pnw); - ldg(e, BW_IR_LDG_E_128, nq0, na, 0, udesc, 1); ldg(e, BW_IR_LDG_E_128, nq1, na, 16, udesc, 0); - fmul(e, R(sq), x[0], x[0]); - for (unsigned i = 1; i < 8; i++) ffma(e, R(sq), x[i], x[i], R(sq)); - warp_sum(e, sq, t); - imadi(e, R(a0), R(warp), 4u, NOREG); imadi(e, R(a0), R(one), OMEGA_DEC_SM_RED, a0); - lane0_sts(e, lane, one, a0, R(sq)); - bar_sync(e); - cta_reduce8(e, sq, OMEGA_DEC_SM_RED, 0, a0); - movi(e, t2, C_INV2048); - ffma(e, R(rs), R(sq), R(t2), R(eps)); - mufu(e, BW_IR_MUFU_RSQ, R(rs), R(rs)); - imadw(e, ya, toff, 1u, py); - for (unsigned i = 0; i < 8; i++) { - fmul(e, R(yv), x[i], R(rs)); - if (sp->mutant != 1) fmul(e, R(yv), R(yv), i < 4 ? S(nq0, i) : S(nq1, i - 4)); - fmul(e, R(yv), R(yv), R(scale)); - if (i) imadw(e, ya, one, 4u, ya); - stg(e, ya, R(yv), udesc); - } - } - tail(e); - return e->err; -} - -static int gen_swiglu(Em *e, const OmegaDecSpec *sp) { - int udesc = UV64(e), pgu = V64(e), pact = V64(e), scale = V(e), tid = V(e), cx = V(e), cy = V(e), c = V(e), gi = V(e), ai = V(e); - int ga = V64(e), ua = V64(e), aa = V64(e), gv = V(e), uv = V(e), t = V(e), cst = V(e), ui = V(e); - if (e->err) return -1; - ldcu64(e, udesc, 0x358); - ldc64(e, pgu, OMEGA_DEC_SW_GU); ldc64(e, pact, OMEGA_DEC_SW_ACT); ldc32(e, scale, OMEGA_DEC_SW_SCALE); - s2r(e, tid, BW_SR_TID_X); s2r(e, cx, BW_SR_CTAID_X); s2r(e, cy, BW_SR_CTAID_Y); - imadi(e, R(c), R(cx), OMEGA_DEC_THREADS, tid); - imadi(e, R(gi), R(cy), 2u * OMEGA_DEC_INTER, c); - imadi(e, R(ai), R(cy), OMEGA_DEC_INTER, c); - /* the up half sits INTER floats after the gate: its own address (the 32-bit LDG.E encoding carries no immediate - * offset, omega_blackwell_codegen.c BW_IR_LDG_E; the warp simulator caught the dropped offset, 2026-10-13) */ - movi(e, cst, sp->mutant == 1 ? OMEGA_DEC_INTER - 1u : OMEGA_DEC_INTER); - imadi(e, R(ui), R(cst), 1u, gi); - imadw(e, ga, gi, 4u, pgu); - imadw(e, ua, ui, 4u, pgu); - imadw(e, aa, ai, 4u, pact); - ldg(e, BW_IR_LDG_E, gv, ga, 0, udesc, 1); - ldg(e, BW_IR_LDG_E, uv, ua, 0, udesc, 0); - movi(e, cst, C_NLOG2E); - fmul(e, R(t), R(gv), R(cst)); - mufu(e, BW_IR_MUFU_EX2, R(t), R(t)); - movi(e, cst, C_ONE); - fadd(e, R(t), R(t), R(cst)); - mufu(e, BW_IR_MUFU_RCP, R(t), R(t)); - fmul(e, R(t), R(gv), R(t)); - fmul(e, R(t), R(t), R(uv)); - fmul(e, R(t), R(t), R(scale)); - stg(e, aa, R(t), udesc); - tail(e); - return e->err; -} - -static int gen_argmax(Em *e, const OmegaDecSpec *sp) { - const int indexed = sp->mode == 1; - int udesc = UV64(e), pv = V64(e), pi = V64(e), pov = V64(e), poi = V64(e); - int tid = V(e), cta = V(e), warp = V(e), lane = V(e), one = V(e), gid = V(e), off = V(e), t = V(e), t2 = V(e), a0 = V(e); - int va = V64(e), vq0 = V128(e), vq1 = V128(e), iq0 = 0, iq1 = 0, m = V(e), mx = V(e), ne = V(e), cand = V(e), best = V(e), c2p23 = V(e), idx = V(e); - if (indexed) { iq0 = V128(e); iq1 = V128(e); } - if (e->err) return -1; - ldcu64(e, udesc, 0x358); - ldc64(e, pv, OMEGA_DEC_AM_V); ldc64(e, pi, OMEGA_DEC_AM_I); ldc64(e, pov, OMEGA_DEC_AM_OV); ldc64(e, poi, OMEGA_DEC_AM_OI); - s2r(e, tid, BW_SR_TID_X); s2r(e, cta, BW_SR_CTAID_X); - movi(e, one, 1); movi(e, c2p23, C_2P23); - shr(e, warp, tid, 5); - andi(e, R(lane), R(tid), 31u); - imadi(e, R(gid), R(cta), OMEGA_DEC_THREADS, tid); - imadi(e, R(off), R(gid), 32u, NOREG); - imadw(e, va, off, 1u, pv); - ldg(e, BW_IR_LDG_E_128, vq0, va, 0, udesc, 1); ldg(e, BW_IR_LDG_E_128, vq1, va, 16, udesc, 0); - if (indexed) { - imadw(e, va, off, 1u, pi); - ldg(e, BW_IR_LDG_E_128, iq0, va, 0, udesc, 0); ldg(e, BW_IR_LDG_E_128, iq1, va, 16, udesc, 0); - } - Op v[8]; - for (unsigned j = 0; j < 8; j++) v[j] = j < 4 ? S(vq0, j) : S(vq1, j - 4); - const unsigned J = sp->mutant == 1 ? 7u : 8u; - mov(e, R(m), v[0]); - for (unsigned j = 1; j < J; j++) fmaxx(e, R(m), R(m), v[j]); - warp_max(e, m, t); - imadi(e, R(a0), R(warp), 4u, NOREG); imadi(e, R(a0), R(one), OMEGA_DEC_SM_RED, a0); - lane0_sts(e, lane, one, a0, R(m)); - bar_sync(e); - cta_reduce8(e, mx, OMEGA_DEC_SM_RED, 1, a0); - /* lowest index holding mx: candidate float bits 0x4B000000 | idx, lifted by 0x30000000 where v != mx */ - for (unsigned j = 0; j < J; j++) { - if (indexed) mov(e, R(idx), j < 4 ? S(iq0, j) : S(iq1, j - 4)); - else imadi(e, R(idx), R(gid), 8u, NOREG), imadi(e, R(idx), R(one), j, idx); - lop3(e, idx, idx, c2p23, NOREG, 0xfcu); - ne_of(e, ne, v[j], R(mx), t, t2); - imadi(e, R(cand), R(ne), 0x30000000u, idx); - if (j == 0) mov(e, R(best), R(cand)); else fminn(e, R(best), R(best), R(cand)); - } - warp_min(e, best, t); - imadi(e, R(a0), R(warp), 4u, NOREG); imadi(e, R(a0), R(one), OMEGA_DEC_SM_RED2, a0); - lane0_sts(e, lane, one, a0, R(best)); - bar_sync(e); - cta_reduce8(e, best, OMEGA_DEC_SM_RED2, 2, a0); - andi(e, R(best), R(best), 0x7fffffu); - isetp_ge_u32(e, tid, one); - { - Region r = region_skip_if_p0(e); - int oa = V64(e); - if (e->err) return -1; - imadw(e, oa, cta, 4u, pov); stg(e, oa, R(mx), udesc); - imadw(e, oa, cta, 4u, poi); stg(e, oa, R(best), udesc); - region_end(e, r); - } - tail(e); - return e->err; -} - -int omega_blackwell_codegen_dec_prog(const OmegaDecSpec *spec, BlackwellIRProgram *prog) { - if (!spec || !prog) return -1; - omega_bw_ir_init(prog); - Em em; memset(&em, 0, sizeof em); em.p = prog; - int rc; - switch (spec->kind) { - case OMEGA_DEC_AXPY: rc = gen_axpy(&em, spec); break; - case OMEGA_DEC_RES: rc = gen_res(&em, spec); break; - case OMEGA_DEC_SWIGLU: rc = gen_swiglu(&em, spec); break; - case OMEGA_DEC_ARGMAX: rc = gen_argmax(&em, spec); break; - default: return -1; - } - if (rc != 0 || em.err || em.depth != 0) return -1; - return omega_bw_regalloc_solve(prog); -} - -int omega_dec_prog_stats(const OmegaDecSpec *spec, uint32_t *insns, uint32_t *gprs) { - static BlackwellIRProgram prog; - if (omega_blackwell_codegen_dec_prog(spec, &prog) != 0) return -1; - if (insns) *insns = (uint32_t)prog.count; - if (gprs) *gprs = prog.regalloc.peak_gpr_usage; - return 0; -} diff --git a/src/omega_blackwell_codegen_dec.h b/src/omega_blackwell_codegen_dec.h deleted file mode 100644 index 0ea33231e..000000000 --- a/src/omega_blackwell_codegen_dec.h +++ /dev/null @@ -1,82 +0,0 @@ -#ifndef OMEGA_BLACKWELL_CODEGEN_DEC_H -#define OMEGA_BLACKWELL_CODEGEN_DEC_H -/* - * omega_blackwell_codegen_dec: the small glue kernels that keep a Qwen3-30B decode token on the chip (OWN-30B W-L2, - * 2026-10-13). Before them the host did the residual adds, the input RMSNorm, SwiGLU, the expert combine and the - * argmax between launches; with them a whole token is one command list (omega_gpu_cmdlist.h). - * - * AXPY x[i] += a[i] in place over iters * stride elements (grid-stride, uniform trip count). Chip test only: a - * chain of these in one list catches overlapping grids (a lost increment). - * RES one CTA of 256 threads over the 2048-wide residual row, 8 values per thread. Mode bits: - * ADD x += a (a = o_proj output) - * COMBINE x += sum_g w[g] * d[g] (g = 0..7, as the host: m = w0*d0, then m = m + w_g*d_g, x = x + m) - * NORM y = x * RSQ(mean(x^2) + eps) * nw * scale (scale 2^64 = the FP8 GEMV x' row, 1 = plain) - * x is written back when ADD or COMBINE is set. - * SWIGLU act[g][c] = silu(gu[g][c]) * gu[g][768 + c] * scale, g = ctaid.y (8 experts), c = ctaid.x * 256 + tid - * (grid 3 x 8). silu(v) = v * RCP(1 + EX2(-v * log2 e)). - * ARGMAX CTA c reads 2048 values v[c*2048 + 8*tid + j] (and indices, or the identity index), writes the max and the - * lowest index holding it to out_v[c], out_i[c]. Two launches give the greedy token of 151936 logits - * (75 CTAs, then 1 CTA over the 75 partials padded with -1e30). The lowest-index rule is the host's - * sample_argmax (strict >). Max by FMNMX, the index by the router's trick (float 2^23 + idx, lifted when the - * value differs from the max, FMNMX.MIN). No FSETP / FSEL. - * - * Constant bank: addresses at 0x380 + 8n, then scalars (offsets below). Every load carries the read barrier, as in the - * other OWN-30B emitters. - */ -#include "omega_blackwell_codegen.h" -#include - -typedef enum { OMEGA_DEC_AXPY = 1, OMEGA_DEC_RES = 2, OMEGA_DEC_SWIGLU = 3, OMEGA_DEC_ARGMAX = 4 } OmegaDecKind; - -#define OMEGA_DEC_RES_ADD 1u -#define OMEGA_DEC_RES_COMBINE 2u -#define OMEGA_DEC_RES_NORM 4u - -#define OMEGA_DEC_HIDDEN 2048u -#define OMEGA_DEC_THREADS 256u -#define OMEGA_DEC_TOPK 8u -#define OMEGA_DEC_INTER 768u -#define OMEGA_DEC_ARGMAX_SPAN 2048u /* values per ARGMAX CTA */ - -/* AXPY args */ -#define OMEGA_DEC_AXPY_X 0x380u -#define OMEGA_DEC_AXPY_A 0x388u -#define OMEGA_DEC_AXPY_ITERS 0x390u -#define OMEGA_DEC_AXPY_WORDS 5u -/* RES args */ -#define OMEGA_DEC_RES_X 0x380u /* f32 [2048] residual, read and (ADD/COMBINE) written */ -#define OMEGA_DEC_RES_A 0x388u /* ADD: f32 [2048]; COMBINE: f32 [8][2048] down outputs */ -#define OMEGA_DEC_RES_W 0x390u /* COMBINE: f32 [8] routing weights */ -#define OMEGA_DEC_RES_NW 0x398u /* NORM: f32 [2048] norm weight */ -#define OMEGA_DEC_RES_Y 0x3a0u /* NORM: f32 [2048] output */ -#define OMEGA_DEC_RES_EPS 0x3a8u -#define OMEGA_DEC_RES_SCALE 0x3acu -#define OMEGA_DEC_RES_WORDS 12u -/* SWIGLU args */ -#define OMEGA_DEC_SW_GU 0x380u /* f32 [8][1536] gate|up */ -#define OMEGA_DEC_SW_ACT 0x388u /* f32 [8][768] */ -#define OMEGA_DEC_SW_SCALE 0x390u -#define OMEGA_DEC_SW_WORDS 5u -/* ARGMAX args */ -#define OMEGA_DEC_AM_V 0x380u /* f32 values, CTA c reads [c*2048, +2048) */ -#define OMEGA_DEC_AM_I 0x388u /* u32 indices (indexed spec), same layout */ -#define OMEGA_DEC_AM_OV 0x390u /* f32 [ctas] */ -#define OMEGA_DEC_AM_OI 0x398u /* u32 [ctas] */ -#define OMEGA_DEC_AM_WORDS 8u - -#define OMEGA_DEC_SM_RED 0u /* 8 per-warp partials */ -#define OMEGA_DEC_SM_RED2 64u -#define OMEGA_DEC_SHARED_BYTES 128u - -typedef struct { - OmegaDecKind kind; - uint32_t mode; /* RES mode bits; ARGMAX: 1 = indexed (indices loaded), 0 = identity */ - uint32_t stride; /* AXPY: elements per iteration (grid_x * 256) */ - int mutant; /* TEST ONLY. RES: 1 = norm without the weight; 2 = COMBINE skips expert 7. - SWIGLU: 1 = up half read at +767. ARGMAX: 1 = last equal index instead of first. 0 none */ -} OmegaDecSpec; - -int omega_blackwell_codegen_dec_prog(const OmegaDecSpec *spec, BlackwellIRProgram *prog); -int omega_dec_prog_stats(const OmegaDecSpec *spec, uint32_t *insns, uint32_t *gprs); - -#endif diff --git a/src/omega_blackwell_codegen_gemm.c b/src/omega_blackwell_codegen_gemm.c deleted file mode 100644 index b804ce0da..000000000 --- a/src/omega_blackwell_codegen_gemm.c +++ /dev/null @@ -1,343 +0,0 @@ -/* omega_blackwell_codegen_gemm.c: FP8 e4m3 block-scaled grouped GEMM for prefill (OWN-30B builder F item 26). See the - * header for the contract. The emitter helpers are copies of omega_blackwell_codegen_moe.c's (same chip-learned - * scheduling rules) plus one addition: weight and scale prefetch loads use scoreboards 3 (buffer A) and 4 (buffer B) - * and are waited for only by the step that decodes them, so the next step's weights stream while this step computes. */ -#include "omega_blackwell_codegen_gemm.h" -#ifndef OMEGA_GPU_NO_POSIX -#include -#endif -#include - -#define CW(stall, wbar, rbar, wait) \ - (((uint32_t)(stall) << 9) | ((uint32_t)(wbar) << 14) | ((uint32_t)(rbar) << 17) | ((uint32_t)(wait) << 20)) -#define CW_YIELD 0x2000u -#define SB_NONE 7u -#define SB_GPR 0u -#define SB_RD 1u -#define SB_UGPR 2u -#define NOREG (-1) -#define MAX_LOOPS 4 - -enum { K_FIXED, K_PRED, K_VAR, K_VAR_MORE, K_UVAR, K_STORE, K_SYNC, K_EXIT, K_BRA, K_BSSY, K_BSYNC }; - -typedef struct { - BlackwellIRProgram *p; - uint32_t pending, held; - int err; - int open_start[MAX_LOOPS]; - int depth; -} Em; - -static void emit(Em *e, BlackwellIRInsn in, int kind) { - uint32_t wait = e->pending; - switch (kind) { - case K_FIXED: in.control = CW(6, SB_NONE, SB_NONE, wait); e->pending = 0; break; - case K_PRED: in.control = CW(14, SB_NONE, SB_NONE, wait); e->pending = 0; break; - /* Loads read their address operands late (the read barrier SB_RD): a later instruction must not overwrite an - * in-flight load's address register. Every load sets SB_RD and the next load of a batch waits on it (cheap: - * the operand read, not the data). Found on the chip: the deep k=2048 kernel hung when the allocator reused a - * dead scale address register as the next load's destination (evidence OWN30B-FP8-GEMV-c4006391). */ - case K_VAR: in.control = CW(4, SB_GPR, SB_RD, wait) | CW_YIELD; e->pending = (1u << SB_GPR) | (1u << SB_RD); break; - /* a further load of the same group: waits on nothing, joins the counting scoreboard */ - case K_VAR_MORE: in.control = CW(4, SB_GPR, SB_RD, (e->pending & (1u << SB_RD))) | CW_YIELD; e->pending |= (1u << SB_GPR) | (1u << SB_RD); break; - case K_UVAR: in.control = CW(4, SB_UGPR, SB_NONE, wait) | CW_YIELD; e->pending = 1u << SB_UGPR; break; - case K_STORE: in.control = CW(4, SB_NONE, SB_RD, wait) | CW_YIELD; e->pending = 1u << SB_RD; break; - case K_SYNC: in.control = CW(6, SB_NONE, SB_NONE, wait); e->pending = 0; break; /* BAR.SYNC after the STS: the attention kernel's chip-proven pattern */ - case K_EXIT: in.control = CW(5, SB_NONE, SB_NONE, wait); e->pending = in.predicate_p0 ? wait : 0; break; - case K_BRA: in.control = CW(5, SB_NONE, SB_NONE, wait); e->pending = in.predicate_p0 ? wait : 0; break; - case K_BSSY: in.control = CW(1, SB_NONE, SB_NONE, wait) | CW_YIELD; e->pending = 0; break; - case K_BSYNC: in.control = CW(5, SB_NONE, SB_NONE, wait) | CW_YIELD; e->pending = 0; break; - default: e->err = -1; return; - } - if (omega_bw_ir_append(e->p, &in) < 0) e->err = -1; -} - -static BlackwellIRInsn I0(BlackwellIROpcode op) { - BlackwellIRInsn in; memset(&in, 0, sizeof in); - in.op = op; in.dst_vreg = NOREG; in.src1_vreg = NOREG; in.src2_vreg = NOREG; in.src3_vreg = NOREG; in.ureg = NOREG; - return in; -} -static int V(Em *e) { int v = omega_bw_ir_alloc_vreg(e->p); if (v < 0) e->err = -1; return v; } -static int V64(Em *e) { int v = omega_bw_ir_alloc_vreg64(e->p); if (v < 0) e->err = -1; return v; } -static int V128(Em *e) { int v = omega_bw_ir_alloc_vreg128(e->p); if (v < 0) e->err = -1; return v; } -static int UV64(Em *e) { int v = omega_bw_ir_alloc_uvreg64(e->p); if (v < 0) e->err = -1; return v; } -static int here(const Em *e) { return (int)e->p->count; } - -static void ldcu64(Em *e, int ud, uint32_t off) { BlackwellIRInsn i = I0(BW_IR_LDCU64); i.dst_vreg = ud; i.imm = off; i.is_uniform = true; emit(e, i, K_UVAR); } -static void ldc64(Em *e, int d, uint32_t off) { BlackwellIRInsn i = I0(BW_IR_LDC64); i.dst_vreg = d; i.imm = off; emit(e, i, K_VAR); } -static void ldc32(Em *e, int d, uint32_t off) { BlackwellIRInsn i = I0(BW_IR_LDC); i.dst_vreg = d; i.imm = off; emit(e, i, K_VAR); } -static void s2r(Em *e, int d, uint32_t sr) { BlackwellIRInsn i = I0(BW_IR_S2R); i.dst_vreg = d; i.imm = sr; emit(e, i, K_VAR); } -static void movi(Em *e, int d, uint32_t imm) { BlackwellIRInsn i = I0(BW_IR_MOV_IMM); i.dst_vreg = d; i.imm = imm; emit(e, i, K_FIXED); } -static void movrz(Em *e, int d) { BlackwellIRInsn i = I0(BW_IR_MOV_RZ); i.dst_vreg = d; emit(e, i, K_FIXED); } -/* d = a * imm + c (c NOREG = RZ); a may be a sub-register of a quad */ -static void imadi(Em *e, int d, int a, unsigned asub, uint32_t imm, int c) { BlackwellIRInsn i = I0(BW_IR_IMAD); i.dst_vreg = d; i.src1_vreg = a; i.src1_subreg = (uint8_t)asub; i.imm = imm; i.src3_vreg = c; emit(e, i, K_FIXED); } -/* d = a * b + c (registers) */ -static void imadr(Em *e, int d, int a, int b, int c) { BlackwellIRInsn i = I0(BW_IR_IMAD); i.dst_vreg = d; i.src1_vreg = a; i.src2_vreg = b; i.src3_vreg = c; emit(e, i, K_FIXED); } -/* d64 = a * imm + c64 */ -static void imadw(Em *e, int d64, int a, uint32_t imm, int c64) { BlackwellIRInsn i = I0(BW_IR_IMAD_WIDE); i.dst_vreg = d64; i.src1_vreg = a; i.imm = imm; i.src3_vreg = c64; emit(e, i, K_FIXED); } -static void shr(Em *e, int d, int a, unsigned asub, uint32_t imm) { BlackwellIRInsn i = I0(BW_IR_SHF_R); i.dst_vreg = d; i.src1_vreg = a; i.src1_subreg = (uint8_t)asub; i.imm = imm; emit(e, i, K_FIXED); } -static void andi(Em *e, int d, int a, unsigned asub, uint32_t imm) { BlackwellIRInsn i = I0(BW_IR_LOP3_AND); i.dst_vreg = d; i.src1_vreg = a; i.src1_subreg = (uint8_t)asub; i.imm = imm; emit(e, i, K_FIXED); } -static void ldg_wide(Em *e, BlackwellIROpcode op, int d, int addr64, uint32_t imm, int udesc, int first) { - BlackwellIRInsn i = I0(op); i.dst_vreg = d; i.src1_vreg = addr64; i.imm = imm; i.ureg = udesc; emit(e, i, first ? K_VAR : K_VAR_MORE); -} -static void ldg32(Em *e, int d, int addr64, int udesc, int first) { BlackwellIRInsn i = I0(BW_IR_LDG_E); i.dst_vreg = d; i.src1_vreg = addr64; i.ureg = udesc; emit(e, i, first ? K_VAR : K_VAR_MORE); } -static void stg(Em *e, int addr64, uint32_t imm, int val, int udesc) { BlackwellIRInsn i = I0(BW_IR_STG_E); i.src1_vreg = addr64; i.src2_vreg = val; i.imm = imm; i.ureg = udesc; emit(e, i, K_STORE); } -static void lds128(Em *e, int d128, int addr, uint32_t imm, int first) { BlackwellIRInsn i = I0(BW_IR_LDS128); i.dst_vreg = d128; i.src1_vreg = addr; i.imm = imm; emit(e, i, first ? K_VAR : K_VAR_MORE); } -static void sts32(Em *e, int addr, int val) { BlackwellIRInsn i = I0(BW_IR_STS32); i.src1_vreg = addr; i.src2_vreg = val; emit(e, i, K_STORE); } -static void bar_sync(Em *e) { emit(e, I0(BW_IR_BAR_SYNC), K_SYNC); } -static void fadd(Em *e, int d, int a, int b) { BlackwellIRInsn i = I0(BW_IR_FADD); i.dst_vreg = d; i.src1_vreg = a; i.src2_vreg = b; emit(e, i, K_FIXED); } -/* d = a * b[bsub] + c */ -static void ffma(Em *e, int d, int a, int b, unsigned bsub, int c) { BlackwellIRInsn i = I0(BW_IR_FFMA); i.dst_vreg = d; i.src1_vreg = a; i.src2_vreg = b; i.src2_subreg = (uint8_t)bsub; i.src3_vreg = c; emit(e, i, K_FIXED); } -static void shfl_down(Em *e, int d, int a, uint32_t off) { BlackwellIRInsn i = I0(BW_IR_SHFL_DOWN); i.dst_vreg = d; i.src1_vreg = a; i.imm = off; emit(e, i, K_VAR); } -static void isetp_ge_u32(Em *e, int a, int b) { BlackwellIRInsn i = I0(BW_IR_ISETP_GE_U32); i.src1_vreg = a; i.src2_vreg = b; emit(e, i, K_PRED); } -static void bra_if_p0(Em *e, int target) { BlackwellIRInsn i = I0(BW_IR_BRA); i.predicate_p0 = true; i.imm = (uint32_t)(target - here(e)); emit(e, i, K_BRA); } -static void bra(Em *e, int target) { BlackwellIRInsn i = I0(BW_IR_BRA); i.imm = (uint32_t)(target - here(e)); emit(e, i, K_BRA); } -/* a forward branch whose target is patched when known: returns the instruction index */ -static int bra_if_p0_fwd(Em *e) { int at = here(e); BlackwellIRInsn i = I0(BW_IR_BRA); i.predicate_p0 = true; i.imm = 1; emit(e, i, K_BRA); return at; } -static void patch_bra(Em *e, int at, int target) { if (!e->err) e->p->insns[at].imm = (uint32_t)(target - at); } -static int bssy_fwd(Em *e) { int at = here(e); BlackwellIRInsn i = I0(BW_IR_BSSY); i.bar_reg = 0; i.imm = 2; emit(e, i, K_BSSY); return at; } -static void bsync(Em *e) { BlackwellIRInsn i = I0(BW_IR_BSYNC); i.bar_reg = 0; emit(e, i, K_BSYNC); } -static void tail(Em *e) { - emit(e, I0(BW_IR_EXIT), K_EXIT); - BlackwellIRInsn self = I0(BW_IR_BRA); - self.control = 0x000fc000u; - if (omega_bw_ir_append(e->p, &self) < 0) e->err = -1; -} - -/* Loops: a value defined before the loop and read inside it stays live until the back-branch - * (the linear-scan allocator knows no back edges; same rule as the elementwise emitter). */ -static void loop_begin(Em *e) { if (e->depth >= MAX_LOOPS) { e->err = -1; return; } e->open_start[e->depth++] = here(e); } -static void loop_end(Em *e) { - if (e->depth <= 0) { e->err = -1; return; } - int start = e->open_start[--e->depth], end = here(e) - 1; - OmegaRegAlloc *ra = &e->p->regalloc; - for (int v = 0; v < ra->num_vregs; v++) { - OmegaLiveInterval *iv = &ra->intervals[v]; - if (iv->first_def >= 0 && iv->first_def < start && iv->last_use >= start && iv->last_use < end) iv->last_use = end; - } -} - - -/* A weight/scale prefetch load: scoreboard `sb` (3 or 4) is held, not waited for, until consume() */ -static void ldw_pf(Em *e, BlackwellIROpcode op, int d, int addr64, uint32_t imm, int udesc, unsigned sb) { - BlackwellIRInsn in = I0(op); in.dst_vreg = d; in.src1_vreg = addr64; in.imm = imm; in.ureg = udesc; - in.control = CW(4, sb, SB_RD, (e->pending & (1u << SB_RD))) | CW_YIELD; - e->pending |= 1u << SB_RD; e->held |= 1u << sb; - if (omega_bw_ir_append(e->p, &in) < 0) e->err = -1; -} -static void consume(Em *e, unsigned sb) { e->pending |= e->held & (1u << sb); e->held &= ~(1u << sb); } - -static int log2u(uint32_t v) { int l = 0; while ((1u << l) < v) l++; return (1u << l) == v ? l : -1; } - -static int spec_ok(const OmegaFp8GemmSpec *s) { - if (!s) return 0; - if (s->lane_bytes != 8 && s->lane_bytes != 16) return 0; - if (s->mt == 0 || (s->mt & (s->mt - 1)) || s->mt * 128u * s->lane_bytes > OMEGA_GEMM_MAX_SHARED) return 0; - if (s->warps < 1 || s->warps > 32 || (s->warps & (s->warps - 1)) || (s->mt * s->lane_bytes) % s->warps) return 0; /* power-of-two warps: the staging words per thread are whole */ - if (s->k == 0 || s->k > 16384u || s->k % 128u || s->k % (32u * s->lane_bytes)) return 0; - uint32_t ks = s->k / (32u * s->lane_bytes); - if (ks % 2u && ks > 3u) return 0; /* odd step counts are fully unrolled: at most 3 */ - if (s->mutant < 0 || s->mutant > 3) return 0; - return 1; -} -uint32_t omega_fp8gemm_steps(const OmegaFp8GemmSpec *spec) { return spec_ok(spec) ? spec->k / (32u * spec->lane_bytes) : 0; } -uint32_t omega_fp8gemm_shared_bytes(const OmegaFp8GemmSpec *spec) { return spec_ok(spec) ? spec->mt * 128u * spec->lane_bytes : 0u; } - -/* e4m3 byte i of word w -> f32 bits (value * 2^-120): ((b & 0x7F) << 20) | ((b & 0x80) << 24) */ -static void decode_byte(Em *e, int f, int wq, unsigned word, unsigned i, int u, int a, int s) { - const uint32_t M = 0x07F00000u; - if (i < 3) imadi(e, u, wq, word, 1u << (20u - 8u * i), NOREG); - else shr(e, u, wq, word, 4); - andi(e, a, u, 0, M); - andi(e, s, wq, word, 0x80u << (8u * i)); - if (i < 3) imadi(e, f, s, 0, 1u << (24u - 8u * i), a); - else imadi(e, f, s, 0, 1u, a); -} - -int omega_blackwell_codegen_fp8gemm_prog(const OmegaFp8GemmSpec *spec, BlackwellIRProgram *prog) { - if (!spec_ok(spec) || !prog) return -1; - const uint32_t K = spec->k, LB = spec->lane_bytes, MT = spec->mt, W = spec->warps; - const uint32_t KS = K / (32u * LB), BPS = 32u * LB / 128u, LPB = 128u / LB, KB = K / 128u, WORDS = LB / 4u; - const uint32_t U = KS % 2u == 0 ? 2u : KS; /* K steps per loop iteration (the A/B register sets alternate) */ - const uint32_t NJ = MT * LB / W, ROWW = 32u * LB, T = 32u * W; /* staging words per thread, rows per staging j, words per row slab */ - const int log2lpb = log2u(LPB), log2mt = log2u(MT), log2roww = log2u(ROWW); - const BlackwellIROpcode ldw = LB == 16 ? BW_IR_LDG_E_128 : BW_IR_LDG_E_64; - if (log2lpb < 0 || log2mt < 0 || log2roww < 0) return -1; - omega_bw_ir_init(prog); - Em em; memset(&em, 0, sizeof em); em.p = prog; - Em *e = &em; - - /* ---- prologue ---- */ - int udesc = UV64(e), pseg = V64(e), px = V64(e), py = V64(e), nreg = V(e), stride = V(e); - int tid = V(e), cx = V(e), cy = V(e), warp = V(e), lane = V(e), one = V(e), tent = V64(e); - int wbase = V64(e), sbase = V64(e), rc = V64(e), row0 = V(e), cnt = V(e), tpass = V(e), passes = V(e); - int wlane = V(e), slane = V(e), xl = V(e), nm1 = V(e), ysn = V(e), mrow = V(e), off4 = V(e), sa0 = V(e), base = V(e); - if (e->err) return -1; - ldcu64(e, udesc, 0x358); - ldc64(e, pseg, OMEGA_GEMM_ARG_SEG); ldc64(e, px, OMEGA_GEMM_ARG_X); ldc64(e, py, OMEGA_GEMM_ARG_Y); - ldc32(e, nreg, OMEGA_GEMM_ARG_N); ldc32(e, stride, OMEGA_GEMM_ARG_STRIDE); - s2r(e, tid, BW_SR_TID_X); s2r(e, cx, BW_SR_CTAID_X); s2r(e, cy, BW_SR_CTAID_Y); - movi(e, one, 1); - shr(e, warp, tid, 0, 5); - andi(e, lane, tid, 0, 31); - imadw(e, tent, cy, OMEGA_GEMM_SEG_BYTES, pseg); - ldg_wide(e, BW_IR_LDG_E_64, wbase, tent, 0, udesc, 1); - ldg_wide(e, BW_IR_LDG_E_64, sbase, tent, 8, udesc, 0); - ldg_wide(e, BW_IR_LDG_E_64, rc, tent, 16, udesc, 0); - imadi(e, row0, rc, 0, 1, NOREG); imadi(e, cnt, rc, 1, 1, NOREG); - imadi(e, tpass, one, 0, MT - 1u, cnt); shr(e, passes, tpass, 0, (uint32_t)log2mt); - imadi(e, wlane, lane, 0, LB, NOREG); - shr(e, slane, lane, 0, (uint32_t)log2lpb); imadi(e, slane, slane, 0, 4, NOREG); - imadi(e, xl, lane, 0, 16, NOREG); - imadi(e, nm1, one, 0, 0xffffffffu, nreg); - imadi(e, ysn, nreg, 0, 4, NOREG); - shr(e, mrow, tid, 0, (uint32_t)log2roww); - andi(e, off4, tid, 0, ROWW - 1u); imadi(e, off4, off4, 0, 4, NOREG); - imadi(e, sa0, tid, 0, 4, NOREG); - imadi(e, base, cx, 0, W, NOREG); - - /* loop-wide registers */ - int rg = V(e), over = V(e), t1 = V(e), d = V(e), tmp = V(e), rgc = V(e), lo = V(e); - int pw = V64(e), ps = V64(e), pyrg = V64(e), rowp = V(e), rem = V(e), pc = V(e); - int xrp = V64(e), xs = V64(e), pwc = V64(e), psc = V64(e), cntk = V(e), pyr = V64(e), pya = V64(e), t = V(e), tq = V(e), fl = V(e), sv = V(e); - int acc[16], dot[16], xq[4], wq[2], sc[2], fr[4], u = V(e), a = V(e), sg = V(e), onef = V(e), dum = V(e); - for (uint32_t m = 0; m < MT; m++) { acc[m] = V(e); dot[m] = V(e); } - for (int q = 0; q < 4; q++) xq[q] = V128(e); - for (int b = 0; b < 2; b++) { wq[b] = LB == 16 ? V128(e) : V64(e); sc[b] = V(e); } - for (int i = 0; i < 4; i++) fr[i] = V(e); - if (e->err) return -1; - - /* ---- weight-row loop (uniform per CTA) ---- */ - const int L_rg = here(e); - loop_begin(e); - isetp_ge_u32(e, base, nreg); - const int bra_end = bra_if_p0_fwd(e); - imadi(e, rg, base, 0, 1, warp); - imadi(e, t1, rg, 0, 0xffffffffu, nm1); shr(e, over, t1, 0, 31); /* over = (rg > n - 1) */ - imadi(e, d, nm1, 0, 0xffffffffu, rg); /* rg - (n - 1) */ - imadr(e, tmp, over, d, NOREG); imadi(e, rgc, tmp, 0, 0xffffffffu, rg); /* rg clamped to n - 1 */ - imadw(e, pw, rgc, K, wbase); imadw(e, pw, wlane, 1, pw); - shr(e, tmp, rgc, 0, 7); - imadw(e, ps, tmp, KB * 4u, sbase); imadw(e, ps, slane, 1, ps); - imadw(e, pyrg, rg, 4, py); - imadi(e, lo, over, 0, 1, lane); - imadi(e, rowp, row0, 0, 1, NOREG); imadi(e, rem, cnt, 0, 1, NOREG); imadi(e, pc, passes, 0, 1, NOREG); - - /* ---- pass loop: MT rows of x per pass ---- */ - const int L_pass = here(e); - loop_begin(e); - imadi(e, t, rowp, 0, 1, mrow); imadw(e, xrp, t, K * 4u, px); imadw(e, xrp, off4, 1, xrp); - imadw(e, xs, one, 0, xrp); - imadw(e, pwc, one, 0, pw); imadw(e, psc, one, 0, ps); - for (uint32_t m = 0; m < MT; m++) movrz(e, acc[m]); - ldw_pf(e, ldw, wq[0], pwc, 0, udesc, 3); - ldw_pf(e, BW_IR_LDG_E, sc[0], psc, 0, udesc, 3); - movi(e, cntk, KS / U); - - /* ---- K loop: U steps per iteration ---- */ - const int L_k = here(e); - loop_begin(e); - for (uint32_t us = 0; us < U; us++) { - const int cur = (int)(us & 1u), nxt = 1 - cur; - const unsigned sbc = cur ? 4u : 3u, sbn = nxt ? 4u : 3u; - /* addresses first (fixed-latency ops would otherwise wait for the loads below), then loads back to back; the - * staging words go in rounds of 4 so the temporaries stay few */ - const int last = us + 1 == U, prefetch = !last || KS > U; - int pwn = -1, psn = -1; - if (prefetch) { pwn = V64(e); psn = V64(e); } - int sa = V(e); - if (e->err) return -1; - if (prefetch && !last) { - imadw(e, pwn, one, (us + 1) * 32u * LB, pwc); imadw(e, psn, one, spec->mutant == 2 ? 0u : (us + 1) * BPS * 4u, psc); - } else if (prefetch) { /* the next iteration's step 0; the last iteration re-reads its own step (harmless, drained after the loop) */ - int nf = V(e), inc = V(e), inc2 = V(e); - if (e->err) return -1; - imadi(e, nf, one, 0, 0xfffffffeu, cntk); /* cntk - 2 */ - shr(e, nf, nf, 0, 31); /* 1 iff cntk == 1 (the last iteration) */ - imadi(e, nf, nf, 0, 0xffffffffu, one); /* 1 iff another iteration follows */ - imadi(e, inc, nf, 0, U * 32u * LB, NOREG); imadi(e, inc2, nf, 0, spec->mutant == 2 ? 0u : U * BPS * 4u, NOREG); - imadw(e, pwn, inc, 1, pwc); imadw(e, psn, inc2, 1, psc); - } - for (uint32_t j0 = 0; j0 < NJ; j0 += 4) { - const uint32_t nj = NJ - j0 < 4 ? NJ - j0 : 4; - int xv[4], xa[4]; - for (uint32_t q = 0; q < nj; q++) { xv[q] = V(e); xa[q] = V64(e); } - if (e->err) return -1; - for (uint32_t q = 0; q < nj; q++) { const uint32_t pos = (j0 + q) * T; imadw(e, xa[q], one, ((pos / ROWW) * K + pos % ROWW) * 4u, xs); } - for (uint32_t q = 0; q < nj; q++) ldg32(e, xv[q], xa[q], udesc, q == 0); - if (prefetch && j0 == 0) { - ldw_pf(e, ldw, wq[nxt], pwn, 0, udesc, sbn); - ldw_pf(e, BW_IR_LDG_E, sc[nxt], psn, 0, udesc, sbn); - } - for (uint32_t q = 0; q < nj; q++) { imadi(e, sa, one, 0, (j0 + q) * T * 4u, sa0); sts32(e, sa, xv[q]); } - } - bar_sync(e); - consume(e, sbc); - /* compute */ - for (uint32_t m = 0; m < MT; m++) movrz(e, dot[m]); - for (uint32_t word = 0; word < WORDS; word++) { - for (unsigned i = 0; i < 4; i++) decode_byte(e, fr[i], wq[cur], word, i, u, a, sg); - for (uint32_t m0 = 0; m0 < MT; m0 += 4) { - const uint32_t nb = MT - m0 < 4 ? MT - m0 : 4; - for (uint32_t q = 0; q < nb; q++) { - const uint32_t m = m0 + q, mm = spec->mutant == 3 ? (m ^ 1u) % MT : m; - lds128(e, xq[q], xl, mm * ROWW * 4u + word * 512u, q == 0); - } - for (uint32_t q = 0; q < nb; q++) for (unsigned i = 0; i < 4; i++) ffma(e, dot[m0 + q], fr[i], xq[q], i, dot[m0 + q]); - } - } - if (spec->mutant == 1) { movi(e, onef, 0x3f800000u); ffma(e, dum, sc[cur], onef, 0, onef); } /* scales dropped, but the scale load stays consumed */ - for (uint32_t m = 0; m < MT; m++) { - if (spec->mutant == 1) ffma(e, acc[m], dot[m], onef, 0, acc[m]); - else ffma(e, acc[m], dot[m], sc[cur], 0, acc[m]); - } - bar_sync(e); - imadw(e, xs, one, ROWW * 4u, xs); - } - imadw(e, pwc, one, U * 32u * LB, pwc); - imadw(e, psc, one, spec->mutant == 2 ? 0u : U * BPS * 4u, psc); - imadi(e, cntk, one, 0, 0xffffffffu, cntk); - isetp_ge_u32(e, cntk, one); - bra_if_p0(e, L_k); - loop_end(e); - consume(e, 3); consume(e, 4); - - /* ---- reduce and store, per row of the pass ---- */ - for (uint32_t m = 0; m < MT; m++) - for (uint32_t o = 16; o >= 1; o /= 2) { shfl_down(e, t, acc[m], o); fadd(e, acc[m], acc[m], t); } - imadr(e, tq, rowp, ysn, NOREG); imadw(e, pyr, tq, 1, pyrg); - for (uint32_t m = 0; m < MT; m++) { - imadi(e, tq, one, 0, (uint32_t)(0u - 1u - m), rem); shr(e, fl, tq, 0, 31); /* 1 iff m >= rem: past this segment's rows */ - imadi(e, sv, fl, 0, 1, lo); - isetp_ge_u32(e, sv, one); - const int at_bssy = bssy_fwd(e); - const int bra_skip = bra_if_p0_fwd(e); - imadw(e, pya, ysn, m, pyr); stg(e, pya, 0, acc[m], udesc); - const int at_bsync = here(e); - bsync(e); - patch_bra(e, bra_skip, at_bsync); - patch_bra(e, at_bssy, at_bsync + 1); - } - imadi(e, rowp, one, 0, MT, rowp); imadi(e, rem, one, 0, (uint32_t)(0u - MT), rem); - imadi(e, pc, one, 0, 0xffffffffu, pc); - isetp_ge_u32(e, pc, one); - bra_if_p0(e, L_pass); - loop_end(e); - - imadi(e, base, stride, 0, 1, base); - bra(e, L_rg); - loop_end(e); - const int L_end = here(e); - patch_bra(e, bra_end, L_end); - tail(e); - if (e->err) return -1; - return omega_bw_regalloc_solve(prog); -} - -int omega_fp8gemm_prog_stats(const OmegaFp8GemmSpec *spec, uint32_t *insns, uint32_t *gprs) { - static BlackwellIRProgram prog; - if (omega_blackwell_codegen_fp8gemm_prog(spec, &prog) != 0) return -1; - if (insns) *insns = (uint32_t)prog.count; - if (gprs) *gprs = prog.regalloc.peak_gpr_usage; - return 0; -} diff --git a/src/omega_blackwell_codegen_gemm.h b/src/omega_blackwell_codegen_gemm.h deleted file mode 100644 index 7bcfba2cc..000000000 --- a/src/omega_blackwell_codegen_gemm.h +++ /dev/null @@ -1,68 +0,0 @@ -#ifndef OMEGA_BLACKWELL_CODEGEN_GEMM_H -#define OMEGA_BLACKWELL_CODEGEN_GEMM_H -/* - * omega_blackwell_codegen_gemm: FP8 e4m3 block-128-scaled grouped GEMM for PREFILL (OWN-30B, 2026-10-13, builder F - * item 26). New file; it mirrors omega_blackwell_codegen_moe.c (the FP8 GEMV) so that its result is bit-identical to - * that GEMV on the same row. - * - * for every segment g (one expert, `cnt` rows of x'): y[row0 + i][r] = sum_c W_g[r][c] * x'[row0 + i][c] * S_g[r/128][c/128] - * - * Each distinct expert's weights are streamed once per pass of MT rows (one pass for the typical expert: 4 rows at 64 - * tokens), not once per token. A warp owns one weight row r and keeps MT f32 accumulators; per K step the lane decodes - * its LB weight bytes once (the GEMV's exact bit trick) and FFMAs them into the MT rows' dot products, then - * acc[m] = fma(dot[m], S', acc[m]) once per 128-block exactly as the GEMV, then the same SHFL.DOWN tree. - * - * x' rows arrive PRE-PERMUTED by the host (omega_gpu_fp8gemm_api): for a row, column c = s*32*LB + L*LB + w*4 + e - * (K step s, lane L, word w, element e) sits at index s*32*LB + (w*32 + L)*4 + e, scaled by 2^64 as the GEMV. The CTA - * stages one K step of MT rows (MT*32*LB*4 <= 16384 bytes: the QMD shared-memory limit of omega_blackwell_qmd.h) in - * shared memory, so every lane reads its 4 columns of every row with one conflict-free LDS.128. Per step: stage, - * BAR.SYNC, compute, BAR.SYNC. The weights and scales of step s+1 are loaded before step s computes (two register - * sets, scoreboards 3 and 4 held until the step that uses them). - * - * Segment table (device, 32 bytes per segment, ctaid.y = segment): { w_va u64, s_va u64, row0 u32, cnt u32, pad[8] }. - * Constant bank 0 argument words (cbank + 0x380): - * 0x380 seg u64 segment table - * 0x388 xp u64 permuted x' rows, f32, k per row; MT padding rows must follow the last segment - * 0x390 y u64 y rows (f32, n each), row i = sorted row i - * 0x398 n u32 output rows per segment (weight rows) - * 0x39c stride u32 weight rows per warp iteration (grid_x * warps) - * Grid: grid_x CTAs per segment (each CTA: `warps` weight rows per iteration), grid_y = segments. All warps of a CTA - * execute the same trip counts (rows past n are clamped to n-1 and not stored; rows past cnt in the last pass read - * padding and are not stored), so the CTA barriers are uniform. - * - * mutant (tests only): 1 drops the block scale (acc += dot), 2 never advances the scale pointer (wrong when KS >= 2), - * 3 reads the neighbouring row's x (m ^ 1) for every row. Each must be caught. - */ -#include -#include "omega_blackwell_codegen.h" - -#ifdef __cplusplus -extern "C" { -#endif - -#define OMEGA_GEMM_ARG_SEG 0x380u -#define OMEGA_GEMM_ARG_X 0x388u -#define OMEGA_GEMM_ARG_Y 0x390u -#define OMEGA_GEMM_ARG_N 0x398u -#define OMEGA_GEMM_ARG_STRIDE 0x39cu -#define OMEGA_GEMM_ARGS_WORDS 8u -#define OMEGA_GEMM_SEG_BYTES 32u -#define OMEGA_GEMM_MAX_SHARED 16384u - -typedef struct { - uint32_t k; /* multiple of 128 and of 32 * lane_bytes; <= 16384 */ - uint32_t lane_bytes; /* 8 or 16 (the GEMV's choice: 16 when k % 512 == 0, else 8) */ - uint32_t mt; /* rows per pass: a power of two, mt * 128 * lane_bytes <= 16384 */ - uint32_t warps; /* warps % lane_bytes == 0, (mt * lane_bytes) % warps == 0, 1 .. 32 (host simulator: build with -DWS_MAX_THREADS=512) */ - int mutant; -} OmegaFp8GemmSpec; - -int omega_blackwell_codegen_fp8gemm_prog(const OmegaFp8GemmSpec *spec, BlackwellIRProgram *prog); -uint32_t omega_fp8gemm_shared_bytes(const OmegaFp8GemmSpec *spec); /* 0 when refused */ -uint32_t omega_fp8gemm_steps(const OmegaFp8GemmSpec *spec); -int omega_fp8gemm_prog_stats(const OmegaFp8GemmSpec *spec, uint32_t *insns, uint32_t *gprs); - -#ifdef __cplusplus -} -#endif -#endif diff --git a/src/omega_blackwell_codegen_moe.c b/src/omega_blackwell_codegen_moe.c deleted file mode 100644 index 14d9d5c4f..000000000 --- a/src/omega_blackwell_codegen_moe.c +++ /dev/null @@ -1,328 +0,0 @@ -/* omega_blackwell_codegen_moe.c: FP8 e4m3 block-scaled GEMV kernel (OWN-30B Cut 6 / Cut 5). - * See the header for the contract and the argument words. - * - * Scheduling: the same chip-learned rules as omega_gpu_elementwise_api.c (every instruction - * waits on everything still pending; variable-latency results go through scoreboard 0, stores - * set the read barrier 1, uniform loads scoreboard 2; fixed-latency producers stall 6; ISETP - * stalls 14 before a predicated branch). Loads of one K step are issued back to back (all set - * SB0, only the first waits), so a warp has every load of the step in flight at once. - */ -#include "omega_blackwell_codegen_moe.h" -#ifndef OMEGA_GPU_NO_POSIX -#include -#endif -#include - -#define CW(stall, wbar, rbar, wait) \ - (((uint32_t)(stall) << 9) | ((uint32_t)(wbar) << 14) | ((uint32_t)(rbar) << 17) | ((uint32_t)(wait) << 20)) -#define CW_YIELD 0x2000u -#define SB_NONE 7u -#define SB_GPR 0u -#define SB_RD 1u -#define SB_UGPR 2u -#define NOREG (-1) -#define MAX_LOOPS 4 - -enum { K_FIXED, K_PRED, K_VAR, K_VAR_MORE, K_UVAR, K_STORE, K_SYNC, K_EXIT, K_BRA, K_BSSY, K_BSYNC }; - -typedef struct { - BlackwellIRProgram *p; - uint32_t pending; - int err; - int open_start[MAX_LOOPS]; - int depth; -} Em; - -static void emit(Em *e, BlackwellIRInsn in, int kind) { - uint32_t wait = e->pending; - switch (kind) { - case K_FIXED: in.control = CW(6, SB_NONE, SB_NONE, wait); e->pending = 0; break; - case K_PRED: in.control = CW(14, SB_NONE, SB_NONE, wait); e->pending = 0; break; - /* Loads read their address operands late (the read barrier SB_RD): a later instruction must not overwrite an - * in-flight load's address register. Every load sets SB_RD and the next load of a batch waits on it (cheap: - * the operand read, not the data). Found on the chip: the deep k=2048 kernel hung when the allocator reused a - * dead scale address register as the next load's destination (evidence OWN30B-FP8-GEMV-c4006391). */ - case K_VAR: in.control = CW(4, SB_GPR, SB_RD, wait) | CW_YIELD; e->pending = (1u << SB_GPR) | (1u << SB_RD); break; - /* a further load of the same group: waits on nothing, joins the counting scoreboard */ - case K_VAR_MORE: in.control = CW(4, SB_GPR, SB_RD, (e->pending & (1u << SB_RD))) | CW_YIELD; e->pending |= (1u << SB_GPR) | (1u << SB_RD); break; - case K_UVAR: in.control = CW(4, SB_UGPR, SB_NONE, wait) | CW_YIELD; e->pending = 1u << SB_UGPR; break; - case K_STORE: in.control = CW(4, SB_NONE, SB_RD, wait) | CW_YIELD; e->pending = 1u << SB_RD; break; - case K_SYNC: in.control = CW(6, SB_NONE, SB_NONE, wait); e->pending = 0; break; /* BAR.SYNC after the STS: the attention kernel's chip-proven pattern */ - case K_EXIT: in.control = CW(5, SB_NONE, SB_NONE, wait); e->pending = in.predicate_p0 ? wait : 0; break; - case K_BRA: in.control = CW(5, SB_NONE, SB_NONE, wait); e->pending = in.predicate_p0 ? wait : 0; break; - case K_BSSY: in.control = CW(1, SB_NONE, SB_NONE, wait) | CW_YIELD; e->pending = 0; break; - case K_BSYNC: in.control = CW(5, SB_NONE, SB_NONE, wait) | CW_YIELD; e->pending = 0; break; - default: e->err = -1; return; - } - if (omega_bw_ir_append(e->p, &in) < 0) e->err = -1; -} - -static BlackwellIRInsn I0(BlackwellIROpcode op) { - BlackwellIRInsn in; memset(&in, 0, sizeof in); - in.op = op; in.dst_vreg = NOREG; in.src1_vreg = NOREG; in.src2_vreg = NOREG; in.src3_vreg = NOREG; in.ureg = NOREG; - return in; -} -static int V(Em *e) { int v = omega_bw_ir_alloc_vreg(e->p); if (v < 0) e->err = -1; return v; } -static int V64(Em *e) { int v = omega_bw_ir_alloc_vreg64(e->p); if (v < 0) e->err = -1; return v; } -static int V128(Em *e) { int v = omega_bw_ir_alloc_vreg128(e->p); if (v < 0) e->err = -1; return v; } -static int UV64(Em *e) { int v = omega_bw_ir_alloc_uvreg64(e->p); if (v < 0) e->err = -1; return v; } -static int here(const Em *e) { return (int)e->p->count; } - -static void ldcu64(Em *e, int ud, uint32_t off) { BlackwellIRInsn i = I0(BW_IR_LDCU64); i.dst_vreg = ud; i.imm = off; i.is_uniform = true; emit(e, i, K_UVAR); } -static void ldc64(Em *e, int d, uint32_t off) { BlackwellIRInsn i = I0(BW_IR_LDC64); i.dst_vreg = d; i.imm = off; emit(e, i, K_VAR); } -static void ldc32(Em *e, int d, uint32_t off) { BlackwellIRInsn i = I0(BW_IR_LDC); i.dst_vreg = d; i.imm = off; emit(e, i, K_VAR); } -static void s2r(Em *e, int d, uint32_t sr) { BlackwellIRInsn i = I0(BW_IR_S2R); i.dst_vreg = d; i.imm = sr; emit(e, i, K_VAR); } -static void movi(Em *e, int d, uint32_t imm) { BlackwellIRInsn i = I0(BW_IR_MOV_IMM); i.dst_vreg = d; i.imm = imm; emit(e, i, K_FIXED); } -static void movrz(Em *e, int d) { BlackwellIRInsn i = I0(BW_IR_MOV_RZ); i.dst_vreg = d; emit(e, i, K_FIXED); } -/* d = a * imm + c (c NOREG = RZ); a may be a sub-register of a quad */ -static void imadi(Em *e, int d, int a, unsigned asub, uint32_t imm, int c) { BlackwellIRInsn i = I0(BW_IR_IMAD); i.dst_vreg = d; i.src1_vreg = a; i.src1_subreg = (uint8_t)asub; i.imm = imm; i.src3_vreg = c; emit(e, i, K_FIXED); } -/* d = a * b + c (registers) */ -static void imadr(Em *e, int d, int a, int b, int c) { BlackwellIRInsn i = I0(BW_IR_IMAD); i.dst_vreg = d; i.src1_vreg = a; i.src2_vreg = b; i.src3_vreg = c; emit(e, i, K_FIXED); } -/* d64 = a * imm + c64 */ -static void imadw(Em *e, int d64, int a, uint32_t imm, int c64) { BlackwellIRInsn i = I0(BW_IR_IMAD_WIDE); i.dst_vreg = d64; i.src1_vreg = a; i.imm = imm; i.src3_vreg = c64; emit(e, i, K_FIXED); } -static void shr(Em *e, int d, int a, unsigned asub, uint32_t imm) { BlackwellIRInsn i = I0(BW_IR_SHF_R); i.dst_vreg = d; i.src1_vreg = a; i.src1_subreg = (uint8_t)asub; i.imm = imm; emit(e, i, K_FIXED); } -static void andi(Em *e, int d, int a, unsigned asub, uint32_t imm) { BlackwellIRInsn i = I0(BW_IR_LOP3_AND); i.dst_vreg = d; i.src1_vreg = a; i.src1_subreg = (uint8_t)asub; i.imm = imm; emit(e, i, K_FIXED); } -static void xorr(Em *e, int d, int a, int b) { BlackwellIRInsn i = I0(BW_IR_LOP3_XOR); i.dst_vreg = d; i.src1_vreg = a; i.src2_vreg = b; emit(e, i, K_FIXED); } -static void ldg_wide(Em *e, BlackwellIROpcode op, int d, int addr64, uint32_t imm, int udesc, int first) { - BlackwellIRInsn i = I0(op); i.dst_vreg = d; i.src1_vreg = addr64; i.imm = imm; i.ureg = udesc; emit(e, i, first ? K_VAR : K_VAR_MORE); -} -static void ldg32(Em *e, int d, int addr64, int udesc, int first) { BlackwellIRInsn i = I0(BW_IR_LDG_E); i.dst_vreg = d; i.src1_vreg = addr64; i.ureg = udesc; emit(e, i, first ? K_VAR : K_VAR_MORE); } -static void stg(Em *e, int addr64, uint32_t imm, int val, int udesc) { BlackwellIRInsn i = I0(BW_IR_STG_E); i.src1_vreg = addr64; i.src2_vreg = val; i.imm = imm; i.ureg = udesc; emit(e, i, K_STORE); } -static void lds128(Em *e, int d128, int addr, uint32_t imm, int first) { BlackwellIRInsn i = I0(BW_IR_LDS128); i.dst_vreg = d128; i.src1_vreg = addr; i.imm = imm; emit(e, i, first ? K_VAR : K_VAR_MORE); } -static void sts32(Em *e, int addr, int val) { BlackwellIRInsn i = I0(BW_IR_STS32); i.src1_vreg = addr; i.src2_vreg = val; emit(e, i, K_STORE); } -static void bar_sync(Em *e) { emit(e, I0(BW_IR_BAR_SYNC), K_SYNC); } -static void fadd(Em *e, int d, int a, int b) { BlackwellIRInsn i = I0(BW_IR_FADD); i.dst_vreg = d; i.src1_vreg = a; i.src2_vreg = b; emit(e, i, K_FIXED); } -/* d = a * b[bsub] + c */ -static void ffma(Em *e, int d, int a, int b, unsigned bsub, int c) { BlackwellIRInsn i = I0(BW_IR_FFMA); i.dst_vreg = d; i.src1_vreg = a; i.src2_vreg = b; i.src2_subreg = (uint8_t)bsub; i.src3_vreg = c; emit(e, i, K_FIXED); } -static void shfl_down(Em *e, int d, int a, uint32_t off) { BlackwellIRInsn i = I0(BW_IR_SHFL_DOWN); i.dst_vreg = d; i.src1_vreg = a; i.imm = off; emit(e, i, K_VAR); } -static void isetp_ge_u32(Em *e, int a, int b) { BlackwellIRInsn i = I0(BW_IR_ISETP_GE_U32); i.src1_vreg = a; i.src2_vreg = b; emit(e, i, K_PRED); } -static void bra_if_p0(Em *e, int target) { BlackwellIRInsn i = I0(BW_IR_BRA); i.predicate_p0 = true; i.imm = (uint32_t)(target - here(e)); emit(e, i, K_BRA); } -static void bra(Em *e, int target) { BlackwellIRInsn i = I0(BW_IR_BRA); i.imm = (uint32_t)(target - here(e)); emit(e, i, K_BRA); } -/* a forward branch whose target is patched when known: returns the instruction index */ -static int bra_if_p0_fwd(Em *e) { int at = here(e); BlackwellIRInsn i = I0(BW_IR_BRA); i.predicate_p0 = true; i.imm = 1; emit(e, i, K_BRA); return at; } -static void patch_bra(Em *e, int at, int target) { if (!e->err) e->p->insns[at].imm = (uint32_t)(target - at); } -static int bssy_fwd(Em *e) { int at = here(e); BlackwellIRInsn i = I0(BW_IR_BSSY); i.bar_reg = 0; i.imm = 2; emit(e, i, K_BSSY); return at; } -static void bsync(Em *e) { BlackwellIRInsn i = I0(BW_IR_BSYNC); i.bar_reg = 0; emit(e, i, K_BSYNC); } -static void tail(Em *e) { - emit(e, I0(BW_IR_EXIT), K_EXIT); - BlackwellIRInsn self = I0(BW_IR_BRA); - self.control = 0x000fc000u; - if (omega_bw_ir_append(e->p, &self) < 0) e->err = -1; -} - -/* Loops: a value defined before the loop and read inside it stays live until the back-branch - * (the linear-scan allocator knows no back edges; same rule as the elementwise emitter). */ -static void loop_begin(Em *e) { if (e->depth >= MAX_LOOPS) { e->err = -1; return; } e->open_start[e->depth++] = here(e); } -static void loop_end(Em *e) { - if (e->depth <= 0) { e->err = -1; return; } - int start = e->open_start[--e->depth], end = here(e) - 1; - OmegaRegAlloc *ra = &e->p->regalloc; - for (int v = 0; v < ra->num_vregs; v++) { - OmegaLiveInterval *iv = &ra->intervals[v]; - if (iv->first_def >= 0 && iv->first_def < start && iv->last_use >= start && iv->last_use < end) iv->last_use = end; - } -} - -static int log2u(uint32_t v) { int l = 0; while ((1u << l) < v) l++; return (1u << l) == v ? l : -1; } - -static int spec_ok(const OmegaFp8GemvSpec *s) { - if (!s) return 0; - if (s->lane_bytes != 8 && s->lane_bytes != 16) return 0; /* LDG.E (32-bit) carries no immediate offset: no 4-byte lanes */ - if (s->rows != 1 && s->rows != 2 && s->rows != 4 && s->rows != 8) return 0; - if (s->warps < 1 || s->warps > 32) return 0; /* threads_x = 32 * warps <= 1024 */ - if (s->k == 0 || s->k > OMEGA_FP8_MAX_K || s->k % OMEGA_FP8_BLOCK || s->k % (32u * s->lane_bytes)) return 0; - if (s->mutant < 0 || s->mutant > 3) return 0; - if (s->depth) { /* deep kernel: staging rounds (32-bit words, one per thread per round) must be exact and few */ - uint32_t ks = s->k / (32u * s->lane_bytes); - if (s->depth > 16 || ks % s->depth || s->rows * s->depth * s->lane_bytes > 128u) return 0; /* <= 128 weight bytes per lane per iteration: instruction and register budgets */ - if (s->k > 512u * s->warps) return 0; /* at most 16 staging rounds; a partial last round is clamped */ - } - /* row offsets of a step must fit the 24-bit signed LDG immediate */ - if ((uint64_t)(s->rows - 1) * s->k + s->lane_bytes >= (1u << 23)) return 0; - return 1; -} - -uint32_t omega_fp8_gemv_steps(const OmegaFp8GemvSpec *spec) { return spec_ok(spec) ? spec->k / (32u * spec->lane_bytes) : 0; } -uint32_t omega_fp8_gemv_shared_bytes(const OmegaFp8GemvSpec *spec) { return spec_ok(spec) && spec->depth ? spec->k * 4u + 16u : 0u; } /* + one dummy slot for the clamped threads of a partial round */ - -/* e4m3 byte i of word w -> f32 bits (value * 2^-120): ((b & 0x7F) << 20) | ((b & 0x80) << 24) */ -static void decode_byte(Em *e, int f, int wq, unsigned word, unsigned i, int u, int a, int s) { - const uint32_t M = 0x07F00000u; - if (i < 3) imadi(e, u, wq, word, 1u << (20u - 8u * i), NOREG); /* b at bits 20..27 */ - else shr(e, u, wq, word, 4); - andi(e, a, u, 0, M); - andi(e, s, wq, word, 0x80u << (8u * i)); - if (i < 3) imadi(e, f, s, 0, 1u << (24u - 8u * i), a); /* sign to bit 31, plus the magnitude */ - else imadi(e, f, s, 0, 1u, a); -} - -int omega_blackwell_codegen_fp8_gemv_prog(const OmegaFp8GemvSpec *spec, BlackwellIRProgram *prog) { - if (!spec_ok(spec) || !prog) return -1; - const uint32_t K = spec->k, LB = spec->lane_bytes, R = spec->rows, W = spec->warps; - const uint32_t KS = K / (32u * LB), BPS = 32u * LB / OMEGA_FP8_BLOCK, LPB = OMEGA_FP8_BLOCK / LB, KB = K / OMEGA_FP8_BLOCK; - const int log2lpb = log2u(LPB), log2rows_per_block = log2u(OMEGA_FP8_BLOCK / R); - const uint32_t WORDS = LB / 4u; - const BlackwellIROpcode ldw = LB == 16 ? BW_IR_LDG_E_128 : BW_IR_LDG_E_64; - if (log2lpb < 0 || log2rows_per_block < 0) return -1; - omega_bw_ir_init(prog); - Em em; memset(&em, 0, sizeof em); em.p = prog; - Em *e = &em; - - /* ---- prologue ---- */ - int udesc = UV64(e), ptab = V64(e), px = V64(e), py = V64(e); - int rgn = V(e), xs = V(e), ys = V(e), stride = V(e); - int tid = V(e), cx = V(e), cy = V(e), warp = V(e), lane = V(e), one = V(e), gidx = V(e); - int tent = V64(e), wbase = V64(e), sbase = V64(e), off = V(e), xrow = V64(e), yrow = V64(e), xlane = V64(e); - int wlane = V(e), slane = V(e), rg = V(e); - if (e->err) return -1; - ldcu64(e, udesc, 0x358); - ldc64(e, ptab, OMEGA_FP8_ARG_TAB); ldc64(e, px, OMEGA_FP8_ARG_X); ldc64(e, py, OMEGA_FP8_ARG_Y); - ldc32(e, rgn, OMEGA_FP8_ARG_RG); ldc32(e, xs, OMEGA_FP8_ARG_XSTRIDE); ldc32(e, ys, OMEGA_FP8_ARG_YSTRIDE); ldc32(e, stride, OMEGA_FP8_ARG_STRIDE); - s2r(e, tid, BW_SR_TID_X); s2r(e, cx, BW_SR_CTAID_X); s2r(e, cy, BW_SR_CTAID_Y); - movi(e, one, 1); - shr(e, warp, tid, 0, 5); - andi(e, lane, tid, 0, 31); - if (spec->mutant == 3) xorr(e, gidx, cy, one); else imadi(e, gidx, cy, 0, 1, NOREG); - imadw(e, tent, gidx, 16, ptab); - ldg_wide(e, BW_IR_LDG_E_64, wbase, tent, 0, udesc, 1); - ldg_wide(e, BW_IR_LDG_E_64, sbase, tent, 8, udesc, 0); - imadr(e, off, cy, xs, NOREG); imadw(e, xrow, off, 1, px); - imadr(e, off, cy, ys, NOREG); imadw(e, yrow, off, 1, py); - imadw(e, xlane, lane, LB * 4u, xrow); /* this lane's x' chunk of step 0 */ - imadi(e, wlane, lane, 0, LB, NOREG); /* byte offset of this lane's chunk in a row */ - shr(e, slane, lane, 0, (uint32_t)log2lpb); imadi(e, slane, slane, 0, 4, NOREG); /* scale word of this lane in a step */ - imadi(e, rg, cx, 0, W, warp); /* first row group of this warp */ - - const uint32_t D = spec->depth ? spec->depth : 1u; /* K steps per loop iteration */ - const int deep = spec->depth != 0; - int pw = V64(e), ps = V64(e), pxc = V64(e), pyr = V64(e), pya = V64(e), srg = V(e), cnt = V(e); - int acc[8], wq[8][16], xq[4], sc[16], psd[16], xl = -1, xl0 = -1; - /* xq[word]: the 4 x floats of weight word `word` (one 16-byte quad each); wq[r][s]: row r, step s of the iteration */ - for (uint32_t r = 0; r < R; r++) { acc[r] = V(e); for (uint32_t s = 0; s < D; s++) wq[r][s] = LB == 16 ? V128(e) : V64(e); } - for (uint32_t q = 0; q < WORDS; q++) xq[q] = V128(e); - for (uint32_t s = 0; s < D; s++) { sc[s] = V(e); psd[s] = V64(e); } - int dot = V(e), u = V(e), a = V(e), sg = V(e), f = V(e), t = V(e); - if (e->err) return -1; - - if (deep) { - /* ---- stage this group's x' row in shared memory (k floats), one 32-bit word per thread per round ---- */ - const uint32_t rounds = (K + 32u * W - 1u) / (32u * W); - int pxt = V64(e), sa0 = V(e), sa = V(e), sap = V(e), pxs[16], xst[16], kq = V(e), over = V(e), tq = V(e), qs = V(e), qg = V(e); - for (uint32_t j = 0; j < rounds; j++) { pxs[j] = V64(e); xst[j] = V(e); } - xl = V(e); xl0 = V(e); - if (e->err) return -1; - imadw(e, pxt, tid, 4, xrow); - imadi(e, sa0, tid, 0, 4, NOREG); - for (uint32_t j = 0; j < rounds; j++) imadw(e, pxs[j], one, j * 128u * W, pxt); - if (K % (32u * W)) { - /* partial last round: word q = tid + j * 32 W may pass K. over = (q >= K) as 0/1 = (K - 1 - q) >> 31; - * the load is clamped to x[K - 1] and the store to the dummy slot K (never read). */ - const uint32_t j = rounds - 1u; - imadi(e, kq, tid, 0, 1, NOREG); imadi(e, kq, one, 0, j * 32u * W, kq); /* q */ - imadi(e, tq, kq, 0, 0xffffffffu, NOREG); imadi(e, tq, one, 0, K - 1u, tq); /* K - 1 - q */ - shr(e, over, tq, 0, 31); /* 1 iff q >= K */ - imadi(e, tq, kq, 0, 1, NOREG); imadi(e, tq, one, 0, (uint32_t)(0u - K), tq); /* q - K */ - imadr(e, qs, over, tq, NOREG); imadi(e, qs, qs, 0, 0xffffffffu, kq); /* q - over * (q - K): K when over */ - imadi(e, qg, tq, 0, 1, NOREG); imadi(e, qg, one, 0, 1, qg); /* q - K + 1 */ - imadr(e, qg, over, qg, NOREG); imadi(e, qg, qg, 0, 0xffffffffu, kq); /* K - 1 when over */ - imadw(e, pxs[j], qg, 4, xrow); - imadi(e, sap, qs, 0, 4, NOREG); - } - for (uint32_t j = 0; j < rounds; j++) ldg32(e, xst[j], pxs[j], udesc, j == 0); - for (uint32_t j = 0; j < rounds; j++) { - if (K % (32u * W) && j == rounds - 1u) { sts32(e, sap, xst[j]); continue; } - imadi(e, sa, one, 0, j * 128u * W, sa0); sts32(e, sa, xst[j]); - } - bar_sync(e); - imadi(e, xl0, lane, 0, LB * 4u, NOREG); /* this lane's x' chunk of step 0, shared byte offset */ - } - - /* ---- row-group loop ---- */ - const int L_rg = here(e); - loop_begin(e); - isetp_ge_u32(e, rg, rgn); - const int bra_end = bra_if_p0_fwd(e); - imadw(e, pw, rg, R * K, wbase); imadw(e, pw, wlane, 1, pw); - shr(e, srg, rg, 0, (uint32_t)log2rows_per_block); - imadw(e, ps, srg, KB * 4u, sbase); imadw(e, ps, slane, 1, ps); - if (!deep) imadw(e, pxc, one, 0, xlane); else imadi(e, xl, xl0, 0, 1, NOREG); - for (uint32_t r = 0; r < R; r++) movrz(e, acc[r]); - movi(e, cnt, KS / D); - - /* ---- K loop: D steps per iteration, every load of the iteration issued before any use ---- */ - const int L_k = here(e); - loop_begin(e); - { - int first = 1; - if (!deep) { for (uint32_t q = 0; q < WORDS; q++) { ldg_wide(e, BW_IR_LDG_E_128, xq[q], pxc, q * 16u, udesc, first); first = 0; } } - for (uint32_t s = 0; s < D; s++) imadw(e, psd[s], one, spec->mutant == 2 ? 0u : s * BPS * 4u, ps); - for (uint32_t s = 0; s < D; s++) for (uint32_t r = 0; r < R; r++) { ldg_wide(e, ldw, wq[r][s], pw, r * K + s * 32u * LB, udesc, first); first = 0; } - for (uint32_t s = 0; s < D; s++) ldg32(e, sc[s], psd[s], udesc, 0); - } - for (uint32_t s = 0; s < D; s++) { - if (deep) { int first = 1; for (uint32_t q = 0; q < WORDS; q++) { lds128(e, xq[q], xl, s * 128u * LB + q * 16u, first); first = 0; } } - for (uint32_t r = 0; r < R; r++) { - movrz(e, dot); - for (uint32_t word = 0; word < WORDS; word++) - for (unsigned i = 0; i < 4; i++) { - decode_byte(e, f, wq[r][s], word, i, u, a, sg); - ffma(e, dot, f, xq[word], i, dot); - } - if (spec->mutant == 1) fadd(e, acc[r], acc[r], dot); - else ffma(e, acc[r], dot, sc[s], 0, acc[r]); - } - } - imadw(e, pw, one, D * 32u * LB, pw); - if (!deep) imadw(e, pxc, one, 32u * LB * 4u, pxc); - imadw(e, ps, one, spec->mutant == 2 ? 0u : D * BPS * 4u, ps); - if (deep) imadi(e, xl, one, 0, D * 128u * LB, xl); - imadi(e, cnt, one, 0, 0xffffffffu, cnt); - isetp_ge_u32(e, cnt, one); - bra_if_p0(e, L_k); - loop_end(e); - - /* ---- warp reduction, lane 0 stores ---- */ - for (uint32_t r = 0; r < R; r++) - for (uint32_t o = 16; o >= 1; o /= 2) { shfl_down(e, t, acc[r], o); fadd(e, acc[r], acc[r], t); } - imadw(e, pyr, rg, R * 4u, yrow); - isetp_ge_u32(e, lane, one); - const int at_bssy = bssy_fwd(e); - const int bra_skip = bra_if_p0_fwd(e); - for (uint32_t r = 0; r < R; r++) { imadw(e, pya, one, r * 4u, pyr); stg(e, pya, 0, acc[r], udesc); } /* STG.E with immediate 0 only (the chip-proven form) */ - const int at_bsync = here(e); - bsync(e); - patch_bra(e, bra_skip, at_bsync); - patch_bra(e, at_bssy, at_bsync + 1); - imadi(e, rg, stride, 0, 1, rg); - bra(e, L_rg); - loop_end(e); - const int L_end = here(e); - patch_bra(e, bra_end, L_end); - tail(e); - if (e->err) return -1; - return omega_bw_regalloc_solve(prog); -} - -int omega_fp8_gemv_prog_stats(const OmegaFp8GemvSpec *spec, uint32_t *insns, uint32_t *gprs) { - static BlackwellIRProgram prog; /* 1024 insns: too big for a stack frame in a test loop */ - if (omega_blackwell_codegen_fp8_gemv_prog(spec, &prog) != 0) return -1; - if (insns) *insns = (uint32_t)prog.count; - if (gprs) *gprs = prog.regalloc.peak_gpr_usage; - return 0; -} - -#ifndef OMEGA_GPU_NO_POSIX -int omega_blackwell_codegen_fp8_gemv(const OmegaFp8GemvSpec *spec, OmegaBlackwellKernel *kernel) { - if (!kernel) return -1; - BlackwellIRProgram *prog = calloc(1, sizeof *prog); - if (!prog) return -1; - int rc = omega_blackwell_codegen_fp8_gemv_prog(spec, prog); - size_t need = rc == 0 ? omega_bw_program_code_bytes(prog) : 0; - uint8_t *buf = need ? malloc(need) : NULL; - if (!buf) { free(prog); return -1; } - rc = omega_blackwell_kernel_encode_into(prog, buf, need, kernel); - free(prog); - if (rc != 0) { free(buf); return -1; } - return 0; /* kernel->code points into buf; omega_blackwell_kernel_free releases it */ -} -#endif diff --git a/src/omega_blackwell_codegen_moe.h b/src/omega_blackwell_codegen_moe.h deleted file mode 100644 index 4140f2fc7..000000000 --- a/src/omega_blackwell_codegen_moe.h +++ /dev/null @@ -1,107 +0,0 @@ -#ifndef OMEGA_BLACKWELL_CODEGEN_MOE_H -#define OMEGA_BLACKWELL_CODEGEN_MOE_H -/* - * omega_blackwell_codegen_moe: FP8 (e4m3) GEMV kernel family for the OWN-30B MoE lane - * (Cut 6 + Cut 5, 2026-10-13). New file: nothing in omega_blackwell_codegen.{c,h} changes. - * - * One kernel: y[g][r] = sum_k W_g[r][k] * x[g or 0][k] * S_g[r/128][k/128], m = 1 (decode), for - * G groups (experts) in one launch. Weights are e4m3 bytes in the checkpoint's row-major - * [n][k] order (n output rows, k input columns), scales are the checkpoint's 128 x 128 - * `weight_scale_inv` grid, stored on the device as f32 (times 2^OMEGA_FP8_SCALE_EXP, see below). - * - * Numeric scheme (hand-checked bit arithmetic, proven exhaustively in tests/gpu_fp8_api_test.c): - * f32 bits ((b & 0x7F) << 20) | ((b & 0x80) << 24) equal e4m3(b) * 2^-120 exactly, for normals, - * subnormals and both zeros (bf16 field exponent 0 with the 3 mantissa bits shifted reproduces - * 2^-6 * 0.M). The e4m3 NaN codes 0x7F / 0xFF decode wrong (a finite number): the upload refuses - * them. The 2^120 is folded on the host: x' = x * 2^OMEGA_FP8_X_EXP and S' = S * 2^OMEGA_FP8_SCALE_EXP - * (64 + 56 = 120), chosen so that every product w' * x' is a normal f32 for any practical x. - * The kernel accumulates in f32: per lane, a dot of lane_bytes weights (FFMA chain), then - * acc_r = fma(dot, S', acc_r) once per 128-block, then a warp reduction (SHFL.DOWN) per row. - * - * Thread mapping: a CTA has `warps` warps (threads_x = 32 * warps, threads_y = 1); warp w of CTA - * (cx, cy) handles group cy and row groups rg = cx * warps + w, rg + stride, ... < RG (RG = n / rows, - * stride = grid_x * warps, both launch arguments). A row group is `rows` consecutive rows, inside - * one 128-row scale block (rows divides 128). Per K step a warp reads 32 * lane_bytes contiguous - * bytes of each of its rows (lane L: bytes [L * lane_bytes, +lane_bytes)), the same lane_bytes - * floats of x', and one scale word per lane (the block of its chunk; 128 / lane_bytes lanes share - * it). Step count KS = k / (32 * lane_bytes) is baked into the kernel; k must be a multiple of - * 32 * lane_bytes and of 128. - * - * Constant bank 0 argument words (cbank + 0x380, as the other APIs): - * 0x380 tab u64 group table: entry g at tab + 16 g = { w_va u64, s_va u64 } - * 0x388 x u64 x' rows (f32, k each; row g at x + g * x_stride) - * 0x390 y u64 y rows (f32, n each; row g at y + g * y_stride) - * 0x398 RG u32 row groups per group (n / rows) - * 0x39c x_stride u32 bytes between x rows (0: one x row shared by every group) - * 0x3a0 y_stride u32 bytes between y rows - * 0x3a4 stride u32 row groups per warp iteration (grid_x * warps) - * Grid: grid_x CTAs per group, grid_y = groups. The kernel does not depend on n, RG, the strides - * or the grid, so one kernel per (k, lane_bytes, rows, warps, mutant) serves every n and G. - * - * Only lane 0 of a warp stores (R words per row group) inside a BSSY/BSYNC region; the warp is - * whole again for the next row group's SHFLs. - * - * mutant (tests only, never production): 1 drops the block scale (acc += dot), 2 never advances the - * scale pointer (every K step uses the step-0 block: wrong whenever KS >= 2), 3 reads the group - * table entry of group cy ^ 1 (swapped bases). Each must be caught by the parity test. - * - * Instruction encodings: every IR op used here already exists in omega_blackwell_codegen.h; no new - * encodings were added. ISETP_GE (immediate) and IADD3 carry fixed control words in the encoder, - * so this generator uses ISETP_GE_U32 (register) and IMAD for adds instead. - */ -#include -#include "omega_blackwell_codegen.h" - -#ifdef __cplusplus -extern "C" { -#endif - -#define OMEGA_FP8_X_EXP 64 /* x' = x * 2^64 (host, exact) */ -#define OMEGA_FP8_SCALE_EXP 56 /* S' = S * 2^56 (host, exact for every finite bf16 scale below 2^72) */ -#define OMEGA_FP8_BLOCK 128u /* scale block (rows and columns) */ -#define OMEGA_FP8_MAX_K 16384u -#define OMEGA_FP8_MAX_GROUPS 64u /* group table entries per launch buffer (8 experts today) */ - -/* argument word offsets */ -#define OMEGA_FP8_ARG_TAB 0x380u -#define OMEGA_FP8_ARG_X 0x388u -#define OMEGA_FP8_ARG_Y 0x390u -#define OMEGA_FP8_ARG_RG 0x398u -#define OMEGA_FP8_ARG_XSTRIDE 0x39cu -#define OMEGA_FP8_ARG_YSTRIDE 0x3a0u -#define OMEGA_FP8_ARG_STRIDE 0x3a4u -#define OMEGA_FP8_ARGS_WORDS 10u - -typedef struct { - uint32_t k; /* input columns: multiple of 128 and of 32 * lane_bytes, <= OMEGA_FP8_MAX_K */ - uint32_t lane_bytes; /* 8 or 16 weight bytes per lane per step (LDG.E.64 / LDG.E.128); k must be a multiple of 256 */ - uint32_t rows; /* rows per warp: 1, 2, 4 or 8 */ - uint32_t warps; /* warps per CTA: 1 .. 32 (the host simulator runs up to 8) */ - uint32_t depth; /* 0: one K step per loop iteration, x read from global memory each step. - * D >= 1: "deep" kernel: the CTA stages its group's x' row in shared memory once - * (k * 4 bytes, BAR.SYNC), then every loop iteration issues the weight and scale - * loads of D consecutive K steps before using any of them (D x more bytes in - * flight per warp); x comes from shared memory (LDS.128). Needs KS % D == 0, - * rows * D * lane_bytes <= 128, k % (32 * warps) == 0 and k <= 512 * warps (staging rounds). */ - int mutant; /* 0 production; 1, 2, 3 as above */ -} OmegaFp8GemvSpec; - -/* 0 on success (prog solved and register-allocated), -1 on a refused spec or an allocator failure. */ -int omega_blackwell_codegen_fp8_gemv_prog(const OmegaFp8GemvSpec *spec, BlackwellIRProgram *prog); - -/* Hosted build: allocate and encode (omega_blackwell_kernel_free releases). */ -int omega_blackwell_codegen_fp8_gemv(const OmegaFp8GemvSpec *spec, OmegaBlackwellKernel *kernel); - -/* K steps the kernel runs: k / (32 * lane_bytes). 0 when the spec is refused. */ -uint32_t omega_fp8_gemv_steps(const OmegaFp8GemvSpec *spec); - -/* Shared memory bytes the launch must declare: k * 4 for a deep kernel, 0 otherwise. */ -uint32_t omega_fp8_gemv_shared_bytes(const OmegaFp8GemvSpec *spec); - -/* The IR instruction count and peak GPR count of the solved program (for budgets in tests). */ -int omega_fp8_gemv_prog_stats(const OmegaFp8GemvSpec *spec, uint32_t *insns, uint32_t *gprs); - -#ifdef __cplusplus -} -#endif -#endif diff --git a/src/omega_blackwell_codegen_moe2.c b/src/omega_blackwell_codegen_moe2.c deleted file mode 100644 index 967822300..000000000 --- a/src/omega_blackwell_codegen_moe2.c +++ /dev/null @@ -1,342 +0,0 @@ -/* omega_blackwell_codegen_moe2.c: FP8 e4m3 block-scaled GEMV, variant 2 (row-pipelined). See the header. - * - * Scheduling: variant 1's chip-learned rules (every instruction waits on everything pending; variable-latency - * results through scoreboard 0, every load sets the read barrier 1 and the next load of a batch waits on it, - * uniform loads scoreboard 2; fixed-latency producers stall 6; ISETP stalls 14 before a predicated branch), - * plus one new rule: the loads of the NEXT row group land through scoreboard 3 (SB_B), which no instruction - * waits on except the register moves that take the set over (K_MOVB). The emitter's `pending` mask therefore - * never carries SB_B: the arithmetic of the current row group runs with the next one's loads in flight. - * Hazards covered: (1) the moves read set B before the next loads into B are issued (program order, fixed - * latency); (2) the address registers of a batch are protected by SB_RD as in variant 1; (3) every load that - * was issued is consumed by a move before EXIT (loads are issued iff the row group exists, and the loop runs - * exactly over the existing row groups). */ -#include "omega_blackwell_codegen_moe2.h" -#include - -#define CW(stall, wbar, rbar, wait) \ - (((uint32_t)(stall) << 9) | ((uint32_t)(wbar) << 14) | ((uint32_t)(rbar) << 17) | ((uint32_t)(wait) << 20)) -#define CW_YIELD 0x2000u -#define SB_NONE 7u -#define SB_GPR 0u -#define SB_RD 1u -#define SB_UGPR 2u -#define SB_B 3u -#define NOREG (-1) -#define MAX_LOOPS 4 -#define MAX_KS 16u -#define MAX_ROWS 8u - -enum { K_FIXED, K_PRED, K_VAR, K_VAR_MORE, K_UVAR, K_STORE, K_SYNC, K_EXIT, K_BRA, K_BSSY, K_BSYNC, K_LDB, K_LDB_MORE, K_MOVB }; - -typedef struct { - BlackwellIRProgram *p; - uint32_t pending; - int err; - int open_start[MAX_LOOPS]; - int depth; -} Em; - -static void emit(Em *e, BlackwellIRInsn in, int kind) { - uint32_t wait = e->pending; - switch (kind) { - case K_FIXED: in.control = CW(6, SB_NONE, SB_NONE, wait); e->pending = 0; break; - case K_PRED: in.control = CW(14, SB_NONE, SB_NONE, wait); e->pending = 0; break; - case K_VAR: in.control = CW(4, SB_GPR, SB_RD, wait) | CW_YIELD; e->pending = (1u << SB_GPR) | (1u << SB_RD); break; - case K_VAR_MORE: in.control = CW(4, SB_GPR, SB_RD, (e->pending & (1u << SB_RD))) | CW_YIELD; e->pending |= (1u << SB_GPR) | (1u << SB_RD); break; - case K_UVAR: in.control = CW(4, SB_UGPR, SB_NONE, wait) | CW_YIELD; e->pending = 1u << SB_UGPR; break; - case K_STORE: in.control = CW(4, SB_NONE, SB_RD, wait) | CW_YIELD; e->pending = 1u << SB_RD; break; - case K_SYNC: in.control = CW(6, SB_NONE, SB_NONE, wait); e->pending = 0; break; - case K_EXIT: in.control = CW(5, SB_NONE, SB_NONE, wait); e->pending = in.predicate_p0 ? wait : 0; break; - case K_BRA: in.control = CW(5, SB_NONE, SB_NONE, wait); e->pending = in.predicate_p0 ? wait : 0; break; - case K_BSSY: in.control = CW(1, SB_NONE, SB_NONE, wait) | CW_YIELD; e->pending = 0; break; - case K_BSYNC: in.control = CW(5, SB_NONE, SB_NONE, wait) | CW_YIELD; e->pending = 0; break; - /* next-row-group loads: data on SB_B (not auto-waited), address operands on SB_RD (waited by the next instruction) */ - case K_LDB: in.control = CW(4, SB_B, SB_RD, wait) | CW_YIELD; e->pending = 1u << SB_RD; break; - case K_LDB_MORE: in.control = CW(4, SB_B, SB_RD, (e->pending & (1u << SB_RD))) | CW_YIELD; e->pending = 1u << SB_RD; break; - /* the first move of set B into set A: the one wait on SB_B */ - case K_MOVB: in.control = CW(6, SB_NONE, SB_NONE, wait | (1u << SB_B)); e->pending = 0; break; - default: e->err = -1; return; - } - if (omega_bw_ir_append(e->p, &in) < 0) e->err = -1; -} - -static BlackwellIRInsn I0(BlackwellIROpcode op) { - BlackwellIRInsn in; memset(&in, 0, sizeof in); - in.op = op; in.dst_vreg = NOREG; in.src1_vreg = NOREG; in.src2_vreg = NOREG; in.src3_vreg = NOREG; in.ureg = NOREG; - return in; -} -static int V(Em *e) { int v = omega_bw_ir_alloc_vreg(e->p); if (v < 0) e->err = -1; return v; } -static int V64(Em *e) { int v = omega_bw_ir_alloc_vreg64(e->p); if (v < 0) e->err = -1; return v; } -static int V128(Em *e) { int v = omega_bw_ir_alloc_vreg128(e->p); if (v < 0) e->err = -1; return v; } -static int UV64(Em *e) { int v = omega_bw_ir_alloc_uvreg64(e->p); if (v < 0) e->err = -1; return v; } -static int here(const Em *e) { return (int)e->p->count; } - -static void ldcu64(Em *e, int ud, uint32_t off) { BlackwellIRInsn i = I0(BW_IR_LDCU64); i.dst_vreg = ud; i.imm = off; i.is_uniform = true; emit(e, i, K_UVAR); } -static void ldc64(Em *e, int d, uint32_t off) { BlackwellIRInsn i = I0(BW_IR_LDC64); i.dst_vreg = d; i.imm = off; emit(e, i, K_VAR); } -static void ldc32(Em *e, int d, uint32_t off) { BlackwellIRInsn i = I0(BW_IR_LDC); i.dst_vreg = d; i.imm = off; emit(e, i, K_VAR); } -static void s2r(Em *e, int d, uint32_t sr) { BlackwellIRInsn i = I0(BW_IR_S2R); i.dst_vreg = d; i.imm = sr; emit(e, i, K_VAR); } -static void movi(Em *e, int d, uint32_t imm) { BlackwellIRInsn i = I0(BW_IR_MOV_IMM); i.dst_vreg = d; i.imm = imm; emit(e, i, K_FIXED); } -static void movrz(Em *e, int d) { BlackwellIRInsn i = I0(BW_IR_MOV_RZ); i.dst_vreg = d; emit(e, i, K_FIXED); } -/* d = a * imm + c (c NOREG = RZ); a may be a sub-register of a quad */ -static void imadi(Em *e, int d, int a, unsigned asub, uint32_t imm, int c) { BlackwellIRInsn i = I0(BW_IR_IMAD); i.dst_vreg = d; i.src1_vreg = a; i.src1_subreg = (uint8_t)asub; i.imm = imm; i.src3_vreg = c; emit(e, i, K_FIXED); } -/* d = a * b + c (registers) */ -static void imadr(Em *e, int d, int a, int b, int c) { BlackwellIRInsn i = I0(BW_IR_IMAD); i.dst_vreg = d; i.src1_vreg = a; i.src2_vreg = b; i.src3_vreg = c; emit(e, i, K_FIXED); } -/* d64 = a * imm + c64 */ -static void imadw(Em *e, int d64, int a, uint32_t imm, int c64) { BlackwellIRInsn i = I0(BW_IR_IMAD_WIDE); i.dst_vreg = d64; i.src1_vreg = a; i.imm = imm; i.src3_vreg = c64; emit(e, i, K_FIXED); } -/* d[dsub] = s[ssub] (IMAD d = s * 1 + RZ, sub-register destination); kind K_MOVB for the first move of a set */ -static void movsub(Em *e, int d, unsigned dsub, int s, unsigned ssub, int kind) { BlackwellIRInsn i = I0(BW_IR_IMAD); i.dst_vreg = d; i.dst_subreg = (uint8_t)dsub; i.src1_vreg = s; i.src1_subreg = (uint8_t)ssub; i.imm = 1; emit(e, i, kind); } -static void shr(Em *e, int d, int a, unsigned asub, uint32_t imm) { BlackwellIRInsn i = I0(BW_IR_SHF_R); i.dst_vreg = d; i.src1_vreg = a; i.src1_subreg = (uint8_t)asub; i.imm = imm; emit(e, i, K_FIXED); } -static void andi(Em *e, int d, int a, unsigned asub, uint32_t imm) { BlackwellIRInsn i = I0(BW_IR_LOP3_AND); i.dst_vreg = d; i.src1_vreg = a; i.src1_subreg = (uint8_t)asub; i.imm = imm; emit(e, i, K_FIXED); } -static void xorr(Em *e, int d, int a, int b) { BlackwellIRInsn i = I0(BW_IR_LOP3_XOR); i.dst_vreg = d; i.src1_vreg = a; i.src2_vreg = b; emit(e, i, K_FIXED); } -static void ldg_wide(Em *e, BlackwellIROpcode op, int d, int addr64, uint32_t imm, int udesc, int kind) { - BlackwellIRInsn i = I0(op); i.dst_vreg = d; i.src1_vreg = addr64; i.imm = imm; i.ureg = udesc; emit(e, i, kind); -} -static void ldg32(Em *e, int d, int addr64, int udesc, int kind) { BlackwellIRInsn i = I0(BW_IR_LDG_E); i.dst_vreg = d; i.src1_vreg = addr64; i.ureg = udesc; emit(e, i, kind); } -static void stg(Em *e, int addr64, uint32_t imm, int val, int udesc) { BlackwellIRInsn i = I0(BW_IR_STG_E); i.src1_vreg = addr64; i.src2_vreg = val; i.imm = imm; i.ureg = udesc; emit(e, i, K_STORE); } -static void lds128(Em *e, int d128, int addr, uint32_t imm) { BlackwellIRInsn i = I0(BW_IR_LDS128); i.dst_vreg = d128; i.src1_vreg = addr; i.imm = imm; emit(e, i, K_VAR); } -static void sts32(Em *e, int addr, int val) { BlackwellIRInsn i = I0(BW_IR_STS32); i.src1_vreg = addr; i.src2_vreg = val; emit(e, i, K_STORE); } -static void bar_sync(Em *e) { emit(e, I0(BW_IR_BAR_SYNC), K_SYNC); } -static void fadd(Em *e, int d, int a, int b) { BlackwellIRInsn i = I0(BW_IR_FADD); i.dst_vreg = d; i.src1_vreg = a; i.src2_vreg = b; emit(e, i, K_FIXED); } -/* d = a * b[bsub] + c */ -static void ffma(Em *e, int d, int a, int b, unsigned bsub, int c) { BlackwellIRInsn i = I0(BW_IR_FFMA); i.dst_vreg = d; i.src1_vreg = a; i.src2_vreg = b; i.src2_subreg = (uint8_t)bsub; i.src3_vreg = c; emit(e, i, K_FIXED); } -static void shfl_down(Em *e, int d, int a, uint32_t off) { BlackwellIRInsn i = I0(BW_IR_SHFL_DOWN); i.dst_vreg = d; i.src1_vreg = a; i.imm = off; emit(e, i, K_VAR); } -static void isetp_ge_u32(Em *e, int a, int b) { BlackwellIRInsn i = I0(BW_IR_ISETP_GE_U32); i.src1_vreg = a; i.src2_vreg = b; emit(e, i, K_PRED); } -static void bra(Em *e, int target) { BlackwellIRInsn i = I0(BW_IR_BRA); i.imm = (uint32_t)(target - here(e)); emit(e, i, K_BRA); } -static int bra_if_p0_fwd(Em *e) { int at = here(e); BlackwellIRInsn i = I0(BW_IR_BRA); i.predicate_p0 = true; i.imm = 1; emit(e, i, K_BRA); return at; } -static void patch_bra(Em *e, int at, int target) { if (!e->err) e->p->insns[at].imm = (uint32_t)(target - at); } -static int bssy_fwd(Em *e) { int at = here(e); BlackwellIRInsn i = I0(BW_IR_BSSY); i.bar_reg = 0; i.imm = 2; emit(e, i, K_BSSY); return at; } -static void bsync(Em *e) { BlackwellIRInsn i = I0(BW_IR_BSYNC); i.bar_reg = 0; emit(e, i, K_BSYNC); } -static void tail(Em *e) { - emit(e, I0(BW_IR_EXIT), K_EXIT); - BlackwellIRInsn self = I0(BW_IR_BRA); - self.control = 0x000fc000u; - if (omega_bw_ir_append(e->p, &self) < 0) e->err = -1; -} - -/* Loops: a value defined before the loop and read inside it stays live until the back-branch - * (the linear-scan allocator knows no back edges; same rule as variant 1). */ -static void loop_begin(Em *e) { if (e->depth >= MAX_LOOPS) { e->err = -1; return; } e->open_start[e->depth++] = here(e); } -static void loop_end(Em *e) { - if (e->depth <= 0) { e->err = -1; return; } - int start = e->open_start[--e->depth], end = here(e) - 1; - OmegaRegAlloc *ra = &e->p->regalloc; - for (int v = 0; v < ra->num_vregs; v++) { - OmegaLiveInterval *iv = &ra->intervals[v]; - if (iv->first_def >= 0 && iv->first_def < start && iv->last_use >= start && iv->last_use < end) iv->last_use = end; - } -} - -static int log2u(uint32_t v) { int l = 0; while ((1u << l) < v) l++; return (1u << l) == v ? l : -1; } - -int omega_fp8_gemv2_spec_ok(const OmegaFp8GemvSpec *s) { - if (!s) return 0; - if (s->lane_bytes != 8 && s->lane_bytes != 16) return 0; - if (s->rows != 1 && s->rows != 2 && s->rows != 4 && s->rows != 8) return 0; - if (s->warps < 1 || s->warps > 32) return 0; - if (s->k == 0 || s->k > OMEGA_FP8_MAX_K || s->k % OMEGA_FP8_BLOCK || s->k % (32u * s->lane_bytes)) return 0; - if (s->mutant < 0 || s->mutant > 3) return 0; - const uint32_t ks = s->k / (32u * s->lane_bytes); - if (ks > MAX_KS || s->rows * ks * s->lane_bytes > 128u) return 0; /* whole row group per register set */ - if (s->k > 512u * s->warps) return 0; /* at most 16 staging rounds */ - if ((uint64_t)(s->rows - 1) * s->k + s->lane_bytes >= (1u << 23)) return 0; - return 1; -} - -/* k * 4 for the staged row, plus one 16-byte dummy slot only when the last staging round is partial (clamped threads store there): - * k = 4096 at 16 warps is exact and lands on the QMD cap of 16384 bytes (chip: 16400 was refused, receipt OWN30B-FP8-GEMV-V2 r1). */ -uint32_t omega_fp8_gemv2_shared_bytes(const OmegaFp8GemvSpec *spec) { - if (!omega_fp8_gemv2_spec_ok(spec)) return 0u; - return spec->k * 4u + (spec->k % (32u * spec->warps) ? 16u : 0u); -} - -/* e4m3 byte i of word w -> f32 bits (value * 2^-120): ((b & 0x7F) << 20) | ((b & 0x80) << 24). Variant 1's sequence. */ -static void decode_byte(Em *e, int f, int wq, unsigned word, unsigned i, int u, int a, int s) { - const uint32_t M = 0x07F00000u; - if (i < 3) imadi(e, u, wq, word, 1u << (20u - 8u * i), NOREG); - else shr(e, u, wq, word, 4); - andi(e, a, u, 0, M); - andi(e, s, wq, word, 0x80u << (8u * i)); - if (i < 3) imadi(e, f, s, 0, 1u << (24u - 8u * i), a); - else imadi(e, f, s, 0, 1u, a); -} - -typedef struct { - int w[MAX_ROWS][MAX_KS]; /* weight bytes: row r, step s (quad or pair) */ - int sc[MAX_KS]; /* scale word per step */ -} RegSet; - -int omega_blackwell_codegen_fp8_gemv2_prog(const OmegaFp8GemvSpec *spec, BlackwellIRProgram *prog) { - if (!omega_fp8_gemv2_spec_ok(spec) || !prog) return -1; - const uint32_t K = spec->k, LB = spec->lane_bytes, R = spec->rows, W = spec->warps; - const uint32_t KS = K / (32u * LB), BPS = 32u * LB / OMEGA_FP8_BLOCK, LPB = OMEGA_FP8_BLOCK / LB, KB = K / OMEGA_FP8_BLOCK; - const int log2lpb = log2u(LPB), log2rows_per_block = log2u(OMEGA_FP8_BLOCK / R); - const uint32_t WORDS = LB / 4u; - const BlackwellIROpcode ldw = LB == 16 ? BW_IR_LDG_E_128 : BW_IR_LDG_E_64; - if (log2lpb < 0 || log2rows_per_block < 0) return -1; - omega_bw_ir_init(prog); - Em em; memset(&em, 0, sizeof em); em.p = prog; - Em *e = &em; - - /* ---- prologue (variant 1's) ---- */ - int udesc = UV64(e), ptab = V64(e), px = V64(e), py = V64(e); - int rgn = V(e), xs = V(e), ys = V(e), stride = V(e); - int tid = V(e), cx = V(e), cy = V(e), warp = V(e), lane = V(e), one = V(e), gidx = V(e); - int tent = V64(e), wbase = V64(e), sbase = V64(e), off = V(e), xrow = V64(e), yrow = V64(e); - int wlane = V(e), slane = V(e), rgl = V(e), rgc = V(e); - if (e->err) return -1; - ldcu64(e, udesc, 0x358); - ldc64(e, ptab, OMEGA_FP8_ARG_TAB); ldc64(e, px, OMEGA_FP8_ARG_X); ldc64(e, py, OMEGA_FP8_ARG_Y); - ldc32(e, rgn, OMEGA_FP8_ARG_RG); ldc32(e, xs, OMEGA_FP8_ARG_XSTRIDE); ldc32(e, ys, OMEGA_FP8_ARG_YSTRIDE); ldc32(e, stride, OMEGA_FP8_ARG_STRIDE); - s2r(e, tid, BW_SR_TID_X); s2r(e, cx, BW_SR_CTAID_X); s2r(e, cy, BW_SR_CTAID_Y); - movi(e, one, 1); - shr(e, warp, tid, 0, 5); - andi(e, lane, tid, 0, 31); - if (spec->mutant == 3) xorr(e, gidx, cy, one); else imadi(e, gidx, cy, 0, 1, NOREG); - imadw(e, tent, gidx, 16, ptab); - ldg_wide(e, BW_IR_LDG_E_64, wbase, tent, 0, udesc, K_VAR); - ldg_wide(e, BW_IR_LDG_E_64, sbase, tent, 8, udesc, K_VAR_MORE); - imadr(e, off, cy, xs, NOREG); imadw(e, xrow, off, 1, px); - imadr(e, off, cy, ys, NOREG); imadw(e, yrow, off, 1, py); - imadi(e, wlane, lane, 0, LB, NOREG); /* byte offset of this lane's chunk in a row */ - shr(e, slane, lane, 0, (uint32_t)log2lpb); imadi(e, slane, slane, 0, 4, NOREG); /* scale word of this lane in a step */ - imadi(e, rgl, cx, 0, W, warp); /* first row group of this warp: the load cursor */ - imadi(e, rgc, rgl, 0, 1, NOREG); /* the compute cursor */ - - /* ---- registers: two sets, x quad, accumulators, temporaries ---- */ - RegSet A, B; - int acc[MAX_ROWS], dot[MAX_ROWS], xq = V128(e); - for (uint32_t r = 0; r < R; r++) { acc[r] = V(e); dot[r] = V(e); for (uint32_t s = 0; s < KS; s++) { A.w[r][s] = LB == 16 ? V128(e) : V64(e); B.w[r][s] = LB == 16 ? V128(e) : V64(e); } } - for (uint32_t s = 0; s < KS; s++) { A.sc[s] = V(e); B.sc[s] = V(e); } - int pw = V64(e), ps = V64(e), psd = V64(e), pyr = V64(e), pya = V64(e), srg = V(e), xl0 = V(e); - int u = V(e), a = V(e), sg = V(e), f = V(e), t = V(e); - if (e->err) return -1; - - /* ---- stage this group's x' row in shared memory (k floats), one 32-bit word per thread per round (variant 1 deep) ---- */ - { - const uint32_t rounds = (K + 32u * W - 1u) / (32u * W); - int pxt = V64(e), sa0 = V(e), sa = V(e), sap = V(e), pxs[16], xst[16], kq = V(e), over = V(e), tq = V(e), qs = V(e), qg = V(e); - for (uint32_t j = 0; j < rounds; j++) { pxs[j] = V64(e); xst[j] = V(e); } - if (e->err) return -1; - imadw(e, pxt, tid, 4, xrow); - imadi(e, sa0, tid, 0, 4, NOREG); - for (uint32_t j = 0; j < rounds; j++) imadw(e, pxs[j], one, j * 128u * W, pxt); - if (K % (32u * W)) { - const uint32_t j = rounds - 1u; - imadi(e, kq, tid, 0, 1, NOREG); imadi(e, kq, one, 0, j * 32u * W, kq); /* q */ - imadi(e, tq, kq, 0, 0xffffffffu, NOREG); imadi(e, tq, one, 0, K - 1u, tq); /* K - 1 - q */ - shr(e, over, tq, 0, 31); /* 1 iff q >= K */ - imadi(e, tq, kq, 0, 1, NOREG); imadi(e, tq, one, 0, (uint32_t)(0u - K), tq); /* q - K */ - imadr(e, qs, over, tq, NOREG); imadi(e, qs, qs, 0, 0xffffffffu, kq); /* K when over, else q */ - imadi(e, qg, tq, 0, 1, NOREG); imadi(e, qg, one, 0, 1, qg); /* q - K + 1 */ - imadr(e, qg, over, qg, NOREG); imadi(e, qg, qg, 0, 0xffffffffu, kq); /* K - 1 when over, else q */ - imadw(e, pxs[j], qg, 4, xrow); - imadi(e, sap, qs, 0, 4, NOREG); - } - for (uint32_t j = 0; j < rounds; j++) ldg32(e, xst[j], pxs[j], udesc, j == 0 ? K_VAR : K_VAR_MORE); - for (uint32_t j = 0; j < rounds; j++) { - if (K % (32u * W) && j == rounds - 1u) { sts32(e, sap, xst[j]); continue; } - imadi(e, sa, one, 0, j * 128u * W, sa0); sts32(e, sa, xst[j]); - } - bar_sync(e); - imadi(e, xl0, lane, 0, LB * 4u, NOREG); /* this lane's x' chunk of step 0, shared byte offset */ - } - - /* ---- the loads of one row group into set B (rows * KS weight loads, then KS scale words) ---- */ -#define ISSUE_B() do { \ - imadw(e, pw, rgl, R * K, wbase); imadw(e, pw, wlane, 1, pw); \ - shr(e, srg, rgl, 0, (uint32_t)log2rows_per_block); \ - imadw(e, ps, srg, KB * 4u, sbase); imadw(e, ps, slane, 1, ps); \ - int kind = K_LDB; \ - for (uint32_t s_ = 0; s_ < KS; s_++) for (uint32_t r_ = 0; r_ < R; r_++) { ldg_wide(e, ldw, B.w[r_][s_], pw, r_ * K + s_ * 32u * LB, udesc, kind); kind = K_LDB_MORE; } \ - for (uint32_t s_ = 0; s_ < KS; s_++) { \ - if (s_ == 0) ldg32(e, B.sc[0], ps, udesc, kind); \ - else { imadw(e, psd, one, spec->mutant == 2 ? 0u : s_ * BPS * 4u, ps); ldg32(e, B.sc[s_], psd, udesc, K_LDB_MORE); } \ - kind = K_LDB_MORE; \ - } \ - imadi(e, rgl, stride, 0, 1, rgl); \ - } while (0) - - /* ---- prologue: nothing to do, or the first row group's loads ---- */ - isetp_ge_u32(e, rgl, rgn); - const int bra_none = bra_if_p0_fwd(e); - ISSUE_B(); - - /* ---- row-group loop: take set B over, issue the next row group into B, compute set A, reduce, store ---- */ - const int L_rg = here(e); - loop_begin(e); - { - int kind = K_MOVB; - for (uint32_t s = 0; s < KS; s++) for (uint32_t r = 0; r < R; r++) for (uint32_t q = 0; q < WORDS; q++) { movsub(e, A.w[r][s], q, B.w[r][s], q, kind); kind = K_FIXED; } - for (uint32_t s = 0; s < KS; s++) movsub(e, A.sc[s], 0, B.sc[s], 0, K_FIXED); - } - isetp_ge_u32(e, rgl, rgn); - const int bra_skip_issue = bra_if_p0_fwd(e); - ISSUE_B(); - const int L_compute = here(e); - patch_bra(e, bra_skip_issue, L_compute); - for (uint32_t r = 0; r < R; r++) movrz(e, acc[r]); - for (uint32_t s = 0; s < KS; s++) { - for (uint32_t r = 0; r < R; r++) movrz(e, dot[r]); - for (uint32_t word = 0; word < WORDS; word++) { - lds128(e, xq, xl0, s * 128u * LB + word * 16u); - for (uint32_t r = 0; r < R; r++) - for (unsigned i = 0; i < 4; i++) { - decode_byte(e, f, A.w[r][s], word, i, u, a, sg); - ffma(e, dot[r], f, xq, i, dot[r]); - } - } - for (uint32_t r = 0; r < R; r++) { - if (spec->mutant == 1) fadd(e, acc[r], acc[r], dot[r]); - else ffma(e, acc[r], dot[r], A.sc[s], 0, acc[r]); - } - } - /* warp reduction, lane 0 stores (variant 1's sequence) */ - for (uint32_t r = 0; r < R; r++) - for (uint32_t o = 16; o >= 1; o /= 2) { shfl_down(e, t, acc[r], o); fadd(e, acc[r], acc[r], t); } - imadw(e, pyr, rgc, R * 4u, yrow); - isetp_ge_u32(e, lane, one); - const int at_bssy = bssy_fwd(e); - const int bra_skip = bra_if_p0_fwd(e); - for (uint32_t r = 0; r < R; r++) { imadw(e, pya, one, r * 4u, pyr); stg(e, pya, 0, acc[r], udesc); } - const int at_bsync = here(e); - bsync(e); - patch_bra(e, bra_skip, at_bsync); - patch_bra(e, at_bssy, at_bsync + 1); - imadi(e, rgc, stride, 0, 1, rgc); - isetp_ge_u32(e, rgc, rgn); - const int bra_done = bra_if_p0_fwd(e); - bra(e, L_rg); - loop_end(e); -#undef ISSUE_B - const int L_end = here(e); - patch_bra(e, bra_none, L_end); - patch_bra(e, bra_done, L_end); - tail(e); - if (e->err) return -1; - return omega_bw_regalloc_solve(prog); -} - -int omega_fp8_gemv2_prog_stats(const OmegaFp8GemvSpec *spec, uint32_t *insns, uint32_t *gprs) { - static BlackwellIRProgram prog; /* 1024 insns: too big for a stack frame in a test loop */ - if (omega_blackwell_codegen_fp8_gemv2_prog(spec, &prog) != 0) return -1; - if (insns) *insns = (uint32_t)prog.count; - if (gprs) *gprs = prog.regalloc.peak_gpr_usage; - return 0; -} - -#ifndef OMEGA_GPU_NO_POSIX -#include -int omega_blackwell_codegen_fp8_gemv2(const OmegaFp8GemvSpec *spec, OmegaBlackwellKernel *kernel) { - if (!kernel) return -1; - BlackwellIRProgram *prog = calloc(1, sizeof *prog); - if (!prog) return -1; - int rc = omega_blackwell_codegen_fp8_gemv2_prog(spec, prog); - size_t need = rc == 0 ? omega_bw_program_code_bytes(prog) : 0; - uint8_t *buf = need ? malloc(need) : NULL; - if (!buf) { free(prog); return -1; } - rc = omega_blackwell_kernel_encode_into(prog, buf, need, kernel); - free(prog); - if (rc != 0) { free(buf); return -1; } - return 0; /* kernel->code points into buf; omega_blackwell_kernel_free releases it */ -} -#endif diff --git a/src/omega_blackwell_codegen_moe2.h b/src/omega_blackwell_codegen_moe2.h deleted file mode 100644 index ab32d3d97..000000000 --- a/src/omega_blackwell_codegen_moe2.h +++ /dev/null @@ -1,57 +0,0 @@ -#ifndef OMEGA_BLACKWELL_CODEGEN_MOE2_H -#define OMEGA_BLACKWELL_CODEGEN_MOE2_H -/* - * omega_blackwell_codegen_moe2: FP8 (e4m3) block-scaled GEMV, variant 2 ("row-pipelined"), OWN-30B - * W-L2c (2026-10-14). New file: omega_blackwell_codegen_moe.{c,h} (variant 1) are untouched; the API - * (omega_gpu_fp8_api.c) selects the variant (omega_gpu_fp8_set_variant / AIEN_OMEGA_FP8_GEMV=2). - * - * Same contract as variant 1 (same OmegaFp8GemvSpec, argument words, grid, thread mapping, lane - * mapping, group table, x' / S' folding, mutants) and the SAME ARITHMETIC ORDER per output row: - * per lane, per K step: dot = FFMA chain over its lane_bytes weights (word 0..W-1, byte 0..3); - * acc_r = fma(dot, S'_step, acc_r) in K order; then SHFL.DOWN 16, 8, 4, 2, 1 with FADD. - * so y is bit-identical to variant 1 for every input (checked by the simulator battery and on the chip). - * - * What changes is only WHEN the loads are issued (bytes in flight per warp): - * - a warp's unit of work is a whole row group (rows x k bytes): every weight load and scale load of - * the row group is issued back to back (rows * KS LDG.E.64/128 + KS scale words per lane); - * - two register sets: while the warp decodes and multiplies row group i (set A), the loads of row - * group i + stride (set B) are already in flight on their own scoreboard (SB3). Set A is filled by - * register moves from set B after a single wait on SB3, so the arithmetic never waits on the loads - * of the next row group (variant 1 waits for every load of a step before using any of them, and has - * nothing in flight while it computes); - * - x' (the activation row, shared by every row of the group) is staged once per CTA in shared memory - * (variant 1's deep-kernel staging, BAR.SYNC) and read per word with LDS.128 (4 registers), so the - * global loads a lane keeps in flight are weights and scales only. - * Register sets: 2 x (rows * KS * lane_bytes / 4 + KS) registers, plus ~30. The spec is refused unless - * rows * KS * lane_bytes <= 128 (instruction and register budgets) and k <= 512 * warps (staging rounds). - * The register-file rule (32 * warps * declared <= 65536) is checked by the API as for variant 1; the - * intended shapes are 16 warps per CTA (two CTAs per SM) at the OWN-30B widths k = 768, 2048, 4096. - * - * spec->depth is ignored (the whole row group is always in flight); spec->mutant 1, 2, 3 as variant 1. - */ -#include -#include "omega_blackwell_codegen_moe.h" - -#ifdef __cplusplus -extern "C" { -#endif - -/* 0 on success (prog solved and register-allocated), -1 on a refused spec or an allocator failure. */ -int omega_blackwell_codegen_fp8_gemv2_prog(const OmegaFp8GemvSpec *spec, BlackwellIRProgram *prog); - -/* 1 if the spec is one this variant builds (shape rules above), 0 otherwise. */ -int omega_fp8_gemv2_spec_ok(const OmegaFp8GemvSpec *spec); - -/* Shared memory bytes the launch must declare: k * 4 (the staged x' row) + 16 (a dummy slot, only when the last staging round is partial). 0 when refused. */ -uint32_t omega_fp8_gemv2_shared_bytes(const OmegaFp8GemvSpec *spec); - -/* Hosted build: allocate and encode (omega_blackwell_kernel_free releases). */ -int omega_blackwell_codegen_fp8_gemv2(const OmegaFp8GemvSpec *spec, OmegaBlackwellKernel *kernel); - -/* The IR instruction count and peak GPR count of the solved program (for budgets in tests). */ -int omega_fp8_gemv2_prog_stats(const OmegaFp8GemvSpec *spec, uint32_t *insns, uint32_t *gprs); - -#ifdef __cplusplus -} -#endif -#endif diff --git a/src/omega_blackwell_codegen_pattn.c b/src/omega_blackwell_codegen_pattn.c deleted file mode 100644 index 676f84998..000000000 --- a/src/omega_blackwell_codegen_pattn.c +++ /dev/null @@ -1,558 +0,0 @@ -/* - * omega_blackwell_codegen_pattn.c: causal prefill attention on tensor cores (see the header). - * Emitter rules are those of omega_blackwell_codegen_attn.c / omega_blackwell_codegen_tc.c (control words, the read - * barrier on every load, held scoreboards 3/4 for prefetch loads, loop live ranges, HMMA spacing: >= 4 independent - * HMMAs at stall 8 between dependent ones, stall 15 + NOP 14 before a non-HMMA reader of an HMMA result). - * The prologue's per-head RMSNorm + RoPE is the decode kernel's instruction sequence (omega_blackwell_codegen_attn.c, - * prologue), so the appended K rows match the decode kernel bit for bit on the same chip. - */ -#include "omega_blackwell_codegen_pattn.h" -#include "omega_blackwell_codegen_tc.h" -#include - -#define CW(stall, wbar, rbar, wait) \ - (((uint32_t)(stall) << 9) | ((uint32_t)(wbar) << 14) | ((uint32_t)(rbar) << 17) | ((uint32_t)(wait) << 20)) -#define CW_YIELD 0x2000u -#define SB_NONE 7u -#define SB_GPR 0u -#define SB_RD 1u -#define SB_UGPR 2u -#define NOREG (-1) -#define MAX_LOOPS 4 - -enum { K_FIXED, K_PRED, K_VAR, K_VAR_MORE, K_UVAR, K_STORE, K_SYNC, K_EXIT, K_BRA, K_BSSY, K_BSYNC }; - -typedef struct { - BlackwellIRProgram *p; - uint32_t pending, held; - int err; - int open_start[MAX_LOOPS]; - int depth; - uint32_t placeholders; -} Em; - -static void emit(Em *e, BlackwellIRInsn in, int kind) { - uint32_t wait = e->pending; - switch (kind) { - case K_FIXED: in.control = CW(6, SB_NONE, SB_NONE, wait); e->pending = 0; break; - case K_PRED: in.control = CW(14, SB_NONE, SB_NONE, wait); e->pending = 0; break; - case K_VAR: in.control = CW(4, SB_GPR, SB_RD, wait) | CW_YIELD; e->pending = (1u << SB_GPR) | (1u << SB_RD); break; - case K_VAR_MORE: in.control = CW(4, SB_GPR, SB_RD, (e->pending & (1u << SB_RD))) | CW_YIELD; e->pending |= (1u << SB_GPR) | (1u << SB_RD); break; - case K_UVAR: in.control = CW(4, SB_UGPR, SB_NONE, wait) | CW_YIELD; e->pending = 1u << SB_UGPR; break; - case K_STORE: in.control = CW(4, SB_NONE, SB_RD, wait) | CW_YIELD; e->pending = 1u << SB_RD; break; - case K_SYNC: in.control = CW(6, SB_NONE, SB_NONE, wait); e->pending = 0; break; - case K_EXIT: in.control = CW(5, SB_NONE, SB_NONE, wait); e->pending = in.predicate_p0 ? wait : 0; break; - case K_BRA: in.control = CW(5, SB_NONE, SB_NONE, wait); e->pending = in.predicate_p0 ? wait : 0; break; - case K_BSSY: in.control = CW(1, SB_NONE, SB_NONE, wait) | CW_YIELD; e->pending = 0; break; - case K_BSYNC: in.control = CW(5, SB_NONE, SB_NONE, wait) | CW_YIELD; e->pending = 0; break; - default: e->err = -1; return; - } - if (omega_bw_ir_append(e->p, &in) < 0) e->err = -1; -} - -static BlackwellIRInsn I0(BlackwellIROpcode op) { - BlackwellIRInsn i; memset(&i, 0, sizeof i); - i.op = op; i.dst_vreg = NOREG; i.src1_vreg = NOREG; i.src2_vreg = NOREG; i.src3_vreg = NOREG; i.ureg = NOREG; - return i; -} -static int V(Em *e) { int v = omega_bw_ir_alloc_vreg(e->p); if (v < 0) e->err = -1; return v; } -static int V64(Em *e) { int v = omega_bw_ir_alloc_vreg64(e->p); if (v < 0) e->err = -1; return v; } -static int V128(Em *e) { int v = omega_bw_ir_alloc_vreg128(e->p); if (v < 0) e->err = -1; return v; } -static int UV64(Em *e) { int v = omega_bw_ir_alloc_uvreg64(e->p); if (v < 0) e->err = -1; return v; } -static int here(const Em *e) { return (int)e->p->count; } - -typedef struct { int v; unsigned s; } Op; -static Op R(int v) { Op o = { v, 0 }; return o; } -static Op S(int v, unsigned s) { Op o = { v, s }; return o; } - -static void ldcu64(Em *e, int ud, uint32_t off) { BlackwellIRInsn i = I0(BW_IR_LDCU64); i.dst_vreg = ud; i.imm = off; i.is_uniform = true; emit(e, i, K_UVAR); } -static void ldc64(Em *e, int d, uint32_t off) { BlackwellIRInsn i = I0(BW_IR_LDC64); i.dst_vreg = d; i.imm = off; emit(e, i, K_VAR); } -static void ldc32(Em *e, int d, uint32_t off) { BlackwellIRInsn i = I0(BW_IR_LDC); i.dst_vreg = d; i.imm = off; emit(e, i, K_VAR); } -static void s2r(Em *e, int d, uint32_t sr) { BlackwellIRInsn i = I0(BW_IR_S2R); i.dst_vreg = d; i.imm = sr; emit(e, i, K_VAR); } -static void movi(Em *e, Op d, uint32_t imm) { BlackwellIRInsn i = I0(BW_IR_MOV_IMM); i.dst_vreg = d.v; i.dst_subreg = (uint8_t)d.s; i.imm = imm; emit(e, i, K_FIXED); } -static void movrz(Em *e, Op d) { BlackwellIRInsn i = I0(BW_IR_MOV_RZ); i.dst_vreg = d.v; i.dst_subreg = (uint8_t)d.s; emit(e, i, K_FIXED); } -/* d = a * imm + c */ -static void imadi(Em *e, Op d, Op a, uint32_t imm, int c) { BlackwellIRInsn i = I0(BW_IR_IMAD); i.dst_vreg = d.v; i.dst_subreg = (uint8_t)d.s; i.src1_vreg = a.v; i.src1_subreg = (uint8_t)a.s; i.imm = imm; i.src3_vreg = c; emit(e, i, K_FIXED); } -/* d = a * b + c (registers) */ -static void imadr(Em *e, int d, int a, int b, int c) { BlackwellIRInsn i = I0(BW_IR_IMAD); i.dst_vreg = d; i.src1_vreg = a; i.src2_vreg = b; i.src3_vreg = c; emit(e, i, K_FIXED); } -/* d64 = a * imm + c64 */ -static void imadw(Em *e, int d64, int a, uint32_t imm, int c64) { BlackwellIRInsn i = I0(BW_IR_IMAD_WIDE); i.dst_vreg = d64; i.src1_vreg = a; i.imm = imm; i.src3_vreg = c64; emit(e, i, K_FIXED); } -static void mov(Em *e, Op d, Op a) { imadi(e, d, a, 1u, NOREG); } -static void shrs(Em *e, int d, Op a, uint32_t imm) { BlackwellIRInsn i = I0(BW_IR_SHF_R); i.dst_vreg = d; i.src1_vreg = a.v; i.src1_subreg = (uint8_t)a.s; i.imm = imm; emit(e, i, K_FIXED); } -static void shr(Em *e, int d, int a, uint32_t imm) { shrs(e, d, R(a), imm); } -static void andi(Em *e, Op d, Op a, uint32_t imm) { BlackwellIRInsn i = I0(BW_IR_LOP3_AND); i.dst_vreg = d.v; i.dst_subreg = (uint8_t)d.s; i.src1_vreg = a.v; i.src1_subreg = (uint8_t)a.s; i.imm = imm; emit(e, i, K_FIXED); } -static void xorr(Em *e, Op d, Op a, Op b) { BlackwellIRInsn i = I0(BW_IR_LOP3_XOR); i.dst_vreg = d.v; i.dst_subreg = (uint8_t)d.s; i.src1_vreg = a.v; i.src1_subreg = (uint8_t)a.s; i.src2_vreg = b.v; i.src2_subreg = (uint8_t)b.s; emit(e, i, K_FIXED); } -static void fop(Em *e, BlackwellIROpcode op, Op d, Op a, Op b, Op c) { - BlackwellIRInsn i = I0(op); i.dst_vreg = d.v; i.dst_subreg = (uint8_t)d.s; i.src1_vreg = a.v; i.src1_subreg = (uint8_t)a.s; - i.src2_vreg = b.v; i.src2_subreg = (uint8_t)b.s; i.src3_vreg = c.v; i.src3_subreg = (uint8_t)c.s; emit(e, i, K_FIXED); -} -static void iadd(Em *e, int d, int a, int b) { fop(e, BW_IR_IADD3, R(d), R(a), R(b), R(NOREG)); } -static void iaddo(Em *e, Op d, Op a, Op b) { fop(e, BW_IR_IADD3, d, a, b, R(NOREG)); } -static void fadd(Em *e, Op d, Op a, Op b) { fop(e, BW_IR_FADD, d, a, b, R(NOREG)); } -static void fsub(Em *e, Op d, Op a, Op b) { fop(e, BW_IR_FSUB, d, a, b, R(NOREG)); } -static void fmul(Em *e, Op d, Op a, Op b) { fop(e, BW_IR_FMUL, d, a, b, R(NOREG)); } -static void ffma(Em *e, Op d, Op a, Op b, Op c) { fop(e, BW_IR_FFMA, d, a, b, c); } -static void fmaxx(Em *e, Op d, Op a, Op b) { fop(e, BW_IR_FMNMX_MAX, d, a, b, R(NOREG)); } -static void mufu(Em *e, BlackwellIROpcode op, Op d, Op a, int first) { BlackwellIRInsn i = I0(op); i.dst_vreg = d.v; i.dst_subreg = (uint8_t)d.s; i.src1_vreg = a.v; i.src1_subreg = (uint8_t)a.s; emit(e, i, first ? K_VAR : K_VAR_MORE); } -static void ldg_wide(Em *e, BlackwellIROpcode op, int d, int addr64, uint32_t imm, int udesc, int first) { - BlackwellIRInsn i = I0(op); i.dst_vreg = d; i.src1_vreg = addr64; i.imm = imm; i.ureg = udesc; emit(e, i, first ? K_VAR : K_VAR_MORE); -} -static void stg(Em *e, int addr64, uint32_t imm, Op val, int udesc) { BlackwellIRInsn i = I0(BW_IR_STG_E); i.src1_vreg = addr64; i.imm = imm; i.src2_vreg = val.v; i.src2_subreg = (uint8_t)val.s; i.ureg = udesc; emit(e, i, K_STORE); } -static void membar(Em *e) { emit(e, I0(BW_IR_MEMBAR_ALL_SYS), K_FIXED); } -static void lds128(Em *e, int d128, int addr, uint32_t imm, int first) { BlackwellIRInsn i = I0(BW_IR_LDS128); i.dst_vreg = d128; i.src1_vreg = addr; i.imm = imm; emit(e, i, first ? K_VAR : K_VAR_MORE); } -static void lds32(Em *e, Op d, int addr, int first) { BlackwellIRInsn i = I0(BW_IR_LDS32); i.dst_vreg = d.v; i.dst_subreg = (uint8_t)d.s; i.src1_vreg = addr; emit(e, i, first ? K_VAR : K_VAR_MORE); } -static void sts32(Em *e, int addr, Op val) { BlackwellIRInsn i = I0(BW_IR_STS32); i.src1_vreg = addr; i.src2_vreg = val.v; i.src2_subreg = (uint8_t)val.s; emit(e, i, K_STORE); } -static void bar_sync(Em *e) { emit(e, I0(BW_IR_BAR_SYNC), K_SYNC); } -static void shfl_down(Em *e, int d, int a, uint32_t off) { BlackwellIRInsn i = I0(BW_IR_SHFL_DOWN); i.dst_vreg = d; i.src1_vreg = a; i.imm = off; emit(e, i, K_VAR); } -/* SHFL.BFLY placeholder (omega_blackwell_codegen_tc.h), patched after encoding */ -static void shfl_bfly(Em *e, int d, int a, uint32_t lanes) { shfl_down(e, d, a, OMEGA_TC_MARK_BFLY | (lanes & 31u)); e->placeholders++; } -/* F2FP.BF16.F32.PACK_AB placeholder: d = bf16_rn(hi) << 16 | bf16_rn(lo) */ -static void f2fp(Em *e, Op d, Op hi, Op lo) { - BlackwellIRInsn i = I0(BW_IR_FMNMX_MAX); i.dst_vreg = d.v; i.dst_subreg = (uint8_t)d.s; i.src1_vreg = hi.v; i.src1_subreg = (uint8_t)hi.s; - i.src2_vreg = lo.v; i.src2_subreg = (uint8_t)lo.s; i.imm = OMEGA_TC_MARK_F2FP; emit(e, i, K_FIXED); e->placeholders++; -} -static void isetp_ge_u32(Em *e, int a, int b) { BlackwellIRInsn i = I0(BW_IR_ISETP_GE_U32); i.src1_vreg = a; i.src2_vreg = b; emit(e, i, K_PRED); } -static void bra_if_p0(Em *e, int target) { BlackwellIRInsn i = I0(BW_IR_BRA); i.predicate_p0 = true; i.imm = (uint32_t)(target - here(e)); emit(e, i, K_BRA); } -static int bra_if_p0_fwd(Em *e) { int at = here(e); BlackwellIRInsn i = I0(BW_IR_BRA); i.predicate_p0 = true; i.imm = 1; emit(e, i, K_BRA); return at; } -static void patch_bra(Em *e, int at, int target) { if (!e->err) e->p->insns[at].imm = (uint32_t)(target - at); } -static int bssy_fwd(Em *e) { int at = here(e); BlackwellIRInsn i = I0(BW_IR_BSSY); i.bar_reg = 0; i.imm = 2; emit(e, i, K_BSSY); return at; } -static void bsync(Em *e) { BlackwellIRInsn i = I0(BW_IR_BSYNC); i.bar_reg = 0; emit(e, i, K_BSYNC); } -static void tail(Em *e) { - emit(e, I0(BW_IR_EXIT), K_EXIT); - BlackwellIRInsn self = I0(BW_IR_BRA); - self.control = 0x000fc000u; - if (omega_bw_ir_append(e->p, &self) < 0) e->err = -1; -} -static void loop_begin(Em *e) { if (e->depth >= MAX_LOOPS) { e->err = -1; return; } e->open_start[e->depth++] = here(e); } -static void loop_end(Em *e) { - if (e->depth <= 0) { e->err = -1; return; } - int start = e->open_start[--e->depth], end = here(e) - 1; - OmegaRegAlloc *ra = &e->p->regalloc; - for (int v = 0; v < ra->num_vregs; v++) { - OmegaLiveInterval *iv = &ra->intervals[v]; - if (iv->first_def >= 0 && iv->first_def < start && iv->last_use >= start && iv->last_use < end) iv->last_use = end; - } -} -typedef struct { int bssy, bra; } Region; -static Region region_skip_if_p0(Em *e) { Region r; r.bssy = bssy_fwd(e); r.bra = bra_if_p0_fwd(e); return r; } -static void region_end(Em *e, Region r) { int at = here(e); bsync(e); patch_bra(e, r.bra, at); patch_bra(e, r.bssy, at + 1); } - -/* HMMA.16816.F32.BF16 d = a * b + c; b is the register pair at b + bsub (0 or 2); c NOREG = RZ */ -static void hmma(Em *e, int d, int a, int b, unsigned bsub, int c, uint32_t stall) { - BlackwellIRInsn i = I0(BW_IR_HMMA_BF16); i.dst_vreg = d; i.src1_vreg = a; i.src2_vreg = b; i.src2_subreg = (uint8_t)bsub; i.src3_vreg = c; - i.control = CW(stall, SB_NONE, SB_NONE, e->pending); e->pending = 0; - if (omega_bw_ir_append(e->p, &i) < 0) e->err = -1; -} -static void nop(Em *e, uint32_t stall) { - BlackwellIRInsn i = I0(BW_IR_NOP); i.control = CW(stall, SB_NONE, SB_NONE, e->pending); e->pending = 0; - if (omega_bw_ir_append(e->p, &i) < 0) e->err = -1; -} -/* a prefetch LDS.128: scoreboard sb (3 or 4) is held, not waited for, until consume() */ -static void lds_pf(Em *e, int d, int addr, uint32_t imm, unsigned sb) { - BlackwellIRInsn in = I0(BW_IR_LDS128); in.dst_vreg = d; in.src1_vreg = addr; in.imm = imm; - in.control = CW(4, sb, SB_RD, (e->pending & (1u << SB_RD))) | CW_YIELD; - e->pending |= 1u << SB_RD; e->held |= 1u << sb; - if (omega_bw_ir_append(e->p, &in) < 0) e->err = -1; -} -static void consume(Em *e, unsigned sb) { e->pending |= e->held & (1u << sb); e->held &= ~(1u << sb); } - -/* bf16 round-to-nearest-even of f32 x into the low 16 bits of d (the decode kernel's sequence) */ -static void bf16_of(Em *e, int d, Op x, int c7fff, int t) { - shrs(e, t, x, 16); - andi(e, R(t), R(t), 1u); - iadd(e, t, t, c7fff); - fop(e, BW_IR_IADD3, R(d), x, R(t), R(NOREG)); - shr(e, d, d, 16); -} -/* two bf16 of x[q0], x[q0+1] packed (element q0 low) */ -static void bf16_pair(Em *e, Op d, int x4, unsigned q0, int c7fff, int h0, int h1, int tb) { - bf16_of(e, h0, S(x4, q0), c7fff, tb); bf16_of(e, h1, S(x4, q0 + 1), c7fff, tb); imadi(e, d, R(h1), 65536u, h0); -} - -/* shared memory map */ -#define SM_K 0u /* main loop: K block, [s 8][jp 2][lane 32][16 B] */ -#define SM_V 8192u /* main loop: V block, [kq 2][np 8][lane 32][16 B] */ -#define SM_Q 0u /* prologue: Q fragments of 2 tiles, [tile 2][s 8][lane 32][16 B] */ -#define SM_X 8192u /* prologue: per-warp f32 [128] partner staging */ -#define SM_N 12288u /* prologue: per-warp sum of squares */ -static const uint32_t NEG_BIG = 0xF149F2CAu; /* -1e30f */ - -/* The decode kernel's RMSNorm + RoPE of the quad x4 (dims 4*lane .. +3) of one head, in place. sin4 carries the - * sign already (lanes < 16 negative). Two CTA barriers (every warp runs the same number of items). */ -typedef struct { int lane, one, eps, snw, sxl, sxp, sq, ts, rs, a1, pt4; } NR; -static void norm_rope(Em *e, const NR *n, int x4, int w4, int cos4, int sin4, int rope) { - fmul(e, R(n->sq), S(x4, 0), S(x4, 0)); - for (unsigned i = 1; i < 4; i++) ffma(e, R(n->sq), S(x4, i), S(x4, i), R(n->sq)); - for (uint32_t o = 16; o >= 1; o /= 2) { shfl_down(e, n->ts, n->sq, o); fadd(e, R(n->sq), R(n->sq), R(n->ts)); } - isetp_ge_u32(e, n->lane, n->one); - { Region r = region_skip_if_p0(e); sts32(e, n->snw, R(n->sq)); region_end(e, r); } - bar_sync(e); - lds32(e, R(n->sq), n->snw, 1); - movi(e, R(n->ts), 0x3c000000u); /* 1/128 */ - ffma(e, R(n->rs), R(n->sq), R(n->ts), R(n->eps)); - mufu(e, BW_IR_MUFU_RSQ, R(n->rs), R(n->rs), 1); - for (unsigned i = 0; i < 4; i++) { fmul(e, S(x4, i), S(x4, i), R(n->rs)); fmul(e, S(x4, i), S(x4, i), S(w4, i)); } - sts32(e, n->sxl, S(x4, 0)); - for (unsigned i = 1; i < 4; i++) { imadi(e, R(n->a1), R(n->one), 4u * i, n->sxl); sts32(e, n->a1, S(x4, i)); } - bar_sync(e); - lds128(e, n->pt4, n->sxp, 0, 1); - if (rope) for (unsigned i = 0; i < 4; i++) { fmul(e, R(n->ts), S(x4, i), S(cos4, i)); ffma(e, S(x4, i), S(n->pt4, i), S(sin4, i), R(n->ts)); } -} - -int omega_pattn_spec_ok(const OmegaPrefillAttnSpec *s) { - return s && (s->warps == 4u || s->warps == 8u) && s->mutant >= 0 && s->mutant <= 4 && s->mode <= OMEGA_PATTN_MODE_ATTEND && (s->mirror == 0 || (s->mirror == 1 && s->mode == OMEGA_PATTN_MODE_APPEND)); -} - -int omega_blackwell_codegen_prefill_attn_prog(const OmegaPrefillAttnSpec *spec, BlackwellIRProgram *prog) { - if (!omega_pattn_spec_ok(spec) || !prog) return -1; - omega_bw_ir_init(prog); - Em em; memset(&em, 0, sizeof em); em.p = prog; Em *e = &em; - const uint32_t W = spec->warps, T = 32u * W, LW = W == 8u ? 3u : 2u; - const uint32_t NKU = 512u / T, NVU = 256u / T, ROUNDS = W / 4u; - const int MUT = spec->mutant; - - /* ---- arguments and identities ---- */ - int udesc = UV64(e); - int pqkv = V64(e), pkc = V64(e), pvc = V64(e), prope = V64(e), pqn = V64(e), pkn = V64(e); - int pos0 = V(e), mc = V(e), kvs = V(e), eps = V(e), qks = V(e); - int tid = V(e), cx = V(e), cy = V(e), warp = V(e), lane = V(e), one = V(e), zero = V(e), c7fff = V(e), c16 = V(e); - int cyk = V(e), nk = V(e), t1 = V(e), t2 = V(e), cxw = V(e); - if (e->err) return -1; - ldcu64(e, udesc, 0x358); - ldc64(e, pqkv, OMEGA_PATTN_ARG_QKV); ldc64(e, pkc, OMEGA_PATTN_ARG_KC); ldc64(e, pvc, OMEGA_PATTN_ARG_VC); - ldc64(e, prope, OMEGA_PATTN_ARG_ROPE); ldc64(e, pqn, OMEGA_PATTN_ARG_QN); ldc64(e, pkn, OMEGA_PATTN_ARG_KN); - ldc32(e, pos0, OMEGA_PATTN_ARG_POS0); ldc32(e, mc, OMEGA_PATTN_ARG_MC); ldc32(e, kvs, OMEGA_PATTN_ARG_KV_STRIDE); - ldc32(e, eps, OMEGA_PATTN_ARG_EPS); ldc32(e, qks, OMEGA_PATTN_ARG_QK_SCALE); - s2r(e, tid, BW_SR_TID_X); s2r(e, cx, BW_SR_CTAID_X); s2r(e, cy, BW_SR_CTAID_Y); - movi(e, R(one), 1); movrz(e, R(zero)); movi(e, R(c7fff), 0x7fffu); movi(e, R(c16), 16); - shr(e, warp, tid, 5); - andi(e, R(lane), R(tid), 31u); - imadr(e, cyk, cy, kvs, NOREG); /* byte offset of this KV head's plane */ - imadi(e, R(cxw), R(cx), W, NOREG); /* first chunk token of the CTA */ - /* nk = min(cxw + W, mc) = hi + over * (mc - hi), over = 1 when hi > mc */ - imadi(e, R(t2), R(one), W, cxw); /* hi */ - imadi(e, R(t1), R(t2), 0xffffffffu, mc); /* mc - hi */ - { int ov = V(e); if (e->err) return -1; - shr(e, ov, t1, 31); imadr(e, nk, ov, t1, t2); } - - /* ---- per-lane prologue constants ---- */ - NR n; memset(&n, 0, sizeof n); - n.lane = lane; n.one = one; n.eps = eps; - n.snw = V(e); n.sxl = V(e); n.sxp = V(e); n.sq = V(e); n.ts = V(e); n.rs = V(e); n.a1 = V(e); n.pt4 = V128(e); - int sgn = V(e), ropel = V(e), ad64 = V64(e), rope64 = V64(e), cos4 = V128(e), sin4 = V128(e), tokc = V(e), item = V(e), cnt = V(e); - int h0 = V(e), h1 = V(e), tb = V(e), w0 = V(e), w1 = V(e), w2 = V(e), w3 = V(e); - if (e->err) return -1; - imadi(e, R(n.snw), R(warp), 4u, NOREG); imadi(e, R(n.snw), R(one), SM_N, n.snw); - imadi(e, R(t1), R(warp), 512u, NOREG); imadi(e, R(t1), R(one), SM_X, t1); - imadi(e, R(n.sxl), R(lane), 16u, t1); - { int pl = V(e); if (e->err) return -1; - xorr(e, R(pl), R(lane), R(c16)); imadi(e, R(n.sxp), R(pl), 16u, t1); } - imadi(e, R(sgn), R(one), 0xfffffff0u, lane); shr(e, sgn, sgn, 31); imadi(e, R(sgn), R(sgn), 0x80000000u, NOREG); - andi(e, R(ropel), R(lane), 15u); imadi(e, R(ropel), R(ropel), 16u, NOREG); - - /* loads one token's rope row (position pos0 + tokc) with the sign on sin */ - #define ROPE_LOAD() do { iadd(e, t2, pos0, tokc); imadi(e, R(t2), R(t2), OMEGA_DATTN_ROPE_ROW_BYTES_PA, ropel); imadw(e, rope64, t2, 1u, prope); \ - ldg_wide(e, BW_IR_LDG_E_128, cos4, rope64, 0, udesc, 0); ldg_wide(e, BW_IR_LDG_E_128, sin4, rope64, 256, udesc, 0); } while (0) - #define OMEGA_DATTN_ROPE_ROW_BYTES_PA 512u - /* tokc = min(item + base, lim) for a token index t1 = item + base: tokc = t1 + over * (lim - t1) */ - #define CLAMP_TOK(tok, lim) do { int _d = V(e), _o = V(e); if (e->err) return -1; \ - imadi(e, R(_d), R(tok), 0xffffffffu, lim); shr(e, _o, _d, 31); imadr(e, tokc, _o, _d, tok); } while (0) - - /* ---- K/V append: chunk tokens 0 .. nk-1 (every CTA, identical bits); APPEND mode: only cxw .. nk-1 (one per warp); - * ATTEND mode: none (the APPEND launch wrote them) ---- */ - const bool APPEND = spec->mode == OMEGA_PATTN_MODE_APPEND; - if (spec->mode != OMEGA_PATTN_MODE_ATTEND) { - int kw4 = V128(e), k4 = V128(e), v4 = V128(e), nkm1 = V(e); - if (e->err) return -1; - imadw(e, ad64, lane, 16u, pkn); - ldg_wide(e, BW_IR_LDG_E_128, kw4, ad64, 0, udesc, 1); - imadi(e, R(nkm1), R(one), 0xffffffffu, nk); - if (APPEND) { movi(e, R(cnt), 1u); iadd(e, item, warp, cxw); } - else { imadi(e, R(cnt), R(one), W - 1u, nk); shr(e, cnt, cnt, LW); /* ceil(nk / W) >= 1 */ - mov(e, R(item), R(warp)); } - const int L_k = here(e); - loop_begin(e); - CLAMP_TOK(item, nkm1); - imadi(e, R(t1), R(tokc), OMEGA_PATTN_QKV_WORDS * 4u, NOREG); imadi(e, R(t1), R(cy), 512u, t1); imadi(e, R(t1), R(lane), 16u, t1); - imadi(e, R(t1), R(one), 32u * 128u * 4u, t1); - imadw(e, ad64, t1, 1u, pqkv); - ldg_wide(e, BW_IR_LDG_E_128, k4, ad64, 0, udesc, 1); - ldg_wide(e, BW_IR_LDG_E_128, v4, ad64, 4u * 128u * 4u, udesc, 0); - ROPE_LOAD(); - for (unsigned i = 0; i < 4; i++) xorr(e, S(sin4, i), S(sin4, i), R(sgn)); - norm_rope(e, &n, k4, kw4, cos4, sin4, MUT != 1); - bf16_pair(e, R(w0), k4, 0, c7fff, h0, h1, tb); bf16_pair(e, R(w1), k4, 2, c7fff, h0, h1, tb); - bf16_pair(e, R(w2), v4, 0, c7fff, h0, h1, tb); bf16_pair(e, R(w3), v4, 2, c7fff, h0, h1, tb); - if (MUT != 3) { - int ka = V64(e); if (e->err) return -1; - isetp_ge_u32(e, item, nk); - const int b = bra_if_p0_fwd(e); - iadd(e, t1, pos0, item); imadi(e, R(t1), R(t1), 256u, cyk); imadi(e, R(t1), R(lane), 8u, t1); - imadw(e, ka, t1, 1u, pkc); stg(e, ka, 0, R(w0), udesc); stg(e, ka, 4, R(w1), udesc); - imadw(e, ka, t1, 1u, pvc); stg(e, ka, 0, R(w2), udesc); stg(e, ka, 4, R(w3), udesc); - if (spec->mirror) { /* the same words into the mirror planes */ - int pk2 = V64(e), pv2 = V64(e), kvs2 = V(e); if (e->err) return -1; - ldc64(e, pk2, OMEGA_PATTN_ARG_KC2); ldc64(e, pv2, OMEGA_PATTN_ARG_VC2); ldc32(e, kvs2, OMEGA_PATTN_ARG_KV_STRIDE2); - imadr(e, t2, cy, kvs2, NOREG); - iadd(e, t1, pos0, item); imadi(e, R(t1), R(t1), 256u, t2); imadi(e, R(t1), R(lane), 8u, t1); - imadw(e, ka, t1, 1u, pk2); stg(e, ka, 0, R(w0), udesc); stg(e, ka, 4, R(w1), udesc); - imadw(e, ka, t1, 1u, pv2); stg(e, ka, 0, R(w2), udesc); stg(e, ka, 4, R(w3), udesc); - } - patch_bra(e, b, here(e)); - } - imadi(e, R(item), R(one), W, item); - imadi(e, R(cnt), R(one), 0xffffffffu, cnt); - isetp_ge_u32(e, cnt, one); - bra_if_p0(e, L_k); - loop_end(e); - membar(e); - if (APPEND) { - (void)h0; - tail(e); - if (e->err) return -1; - return omega_bw_regalloc_solve(prog) != 0 ? -1 : 0; - } - bar_sync(e); - } - - /* ---- Q fragments: rounds of 2 tiles (4 tokens) through shared memory ---- */ - int q[8]; - for (unsigned s = 0; s < 8; s++) q[s] = V128(e); - { - int qw4 = V128(e), x4 = V128(e), qlane = V(e), rr = V(e), cr = V(e), mcm1 = V(e), qbase = V(e), jt = V(e), hh = V(e); - if (e->err) return -1; - for (unsigned s = 0; s < 8; s++) for (unsigned k = 0; k < 4; k++) movrz(e, S(q[s], k)); /* defined before the rounds loop */ - imadw(e, ad64, lane, 16u, pqn); - ldg_wide(e, BW_IR_LDG_E_128, qw4, ad64, 0, udesc, 1); - imadi(e, R(mcm1), R(one), 0xffffffffu, mc); - /* lane part of the fragment address: (lane>>2)*512 + (lane&1)*32 + ((lane>>1)&1)*8 */ - shr(e, t1, lane, 2); imadi(e, R(qlane), R(t1), 512u, NOREG); - andi(e, R(t1), R(lane), 1u); imadi(e, R(qlane), R(t1), 32u, qlane); - shr(e, t1, lane, 1); andi(e, R(t1), R(t1), 1u); imadi(e, R(qlane), R(t1), 8u, qlane); - /* this warp's fragment base: tile (warp>>1)&1 of the round */ - shr(e, t1, warp, 1); andi(e, R(t1), R(t1), 1u); imadi(e, R(qbase), R(t1), 4096u, NOREG); imadi(e, R(qbase), R(lane), 16u, qbase); - movrz(e, R(rr)); movi(e, R(cr), ROUNDS); - const int L_r = here(e); - loop_begin(e); - mov(e, R(item), R(warp)); movi(e, R(cnt), 32u / W); - const int L_i = here(e); - loop_begin(e); - shr(e, jt, item, 3); andi(e, R(hh), R(item), 7u); - imadi(e, R(t1), R(rr), 4u, jt); iadd(e, t1, t1, cxw); /* chunk token */ - { int tk = V(e); if (e->err) return -1; mov(e, R(tk), R(t1)); CLAMP_TOK(tk, mcm1); } - imadi(e, R(t1), R(cy), 8u, hh); imadi(e, R(t1), R(t1), 512u, NOREG); - imadi(e, R(t1), R(tokc), OMEGA_PATTN_QKV_WORDS * 4u, t1); imadi(e, R(t1), R(lane), 16u, t1); - imadw(e, ad64, t1, 1u, pqkv); - ldg_wide(e, BW_IR_LDG_E_128, x4, ad64, 0, udesc, 1); - ROPE_LOAD(); - for (unsigned i = 0; i < 4; i++) xorr(e, S(sin4, i), S(sin4, i), R(sgn)); - norm_rope(e, &n, x4, qw4, cos4, sin4, MUT != 1); - for (unsigned i = 0; i < 4; i++) fmul(e, S(x4, i), S(x4, i), R(qks)); - bf16_pair(e, R(w0), x4, 0, c7fff, h0, h1, tb); bf16_pair(e, R(w1), x4, 2, c7fff, h0, h1, tb); - /* dst = qlane + (jt>>1)*4096 + h*64 + (jt&1)*4 */ - shr(e, t1, jt, 1); imadi(e, R(t1), R(t1), 4096u, qlane); imadi(e, R(t1), R(hh), 64u, t1); - andi(e, R(t2), R(jt), 1u); imadi(e, R(t1), R(t2), 4u, t1); - sts32(e, t1, R(w0)); imadi(e, R(t2), R(one), 16u, t1); sts32(e, t2, R(w1)); - imadi(e, R(item), R(one), W, item); - imadi(e, R(cnt), R(one), 0xffffffffu, cnt); ``` The diff is longer than this page shows; the raw diff has all of it.