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/compare/3659d64f59734d7d8f8cd710b1f41e3fbe2d82ce...af657e26e4c790f5825ac83352a3fae544185ab0 # 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 ## Comparing `3659d64f59734d7d8f8cd710b1f41e3fbe2d82ce` ... `af657e26e4c790f5825ac83352a3fae544185ab0` - Merge reviewed OSC, tensor and runtime codec migrations `af657e2` · Drake Stapleton · 2026-11-20 23:39 · Browse - Document the DeepSeek expert benchmark researcher setup `1182979` · Drake Stapleton · 2026-11-20 23:30 · Browse - Merge checkpoint-backed DeepSeek expert kernels and native head `f9bea33` · Drake Stapleton · 2026-11-20 23:26 · Browse - Build seeded SM121a mixed-format expert projection probes `0e77c63` · Drake Stapleton · 2026-11-20 23:25 · Browse - Preserve the pinned tensor mutation suite with seeded scalar comparison `81552c5` · Drake Stapleton · 2026-11-20 23:04 · Browse - Port OSC records, tensor identity and runtime and training codecs to Omega `d055ff2` · Drake Stapleton · 2026-11-20 22:56 · Browse - Port OSC AArch64 register encoding and autodiff shape planning to Omega `4c2bcf0` · Drake Stapleton · 2026-11-20 22:39 · Browse - Limit contract parameter types to active OSC registers `1b57af2` · Drake Stapleton · 2026-11-20 22:28 · Browse - Port OSC IR register checks, scoped declarations and artifact emission to Omega `f228475` · Drake Stapleton · 2026-11-20 22:27 · Browse - Move OSC control flow analysis and IR block layout into Omega `535d83f` · Drake Stapleton · 2026-11-20 22:08 · Browse - Match FP32 normalization and pre-mix and handle head shard errors `cd39d9d` · Drake Stapleton · 2026-11-20 21:35 · Browse - Run the DeepSeek V4.1 Flash text head through native Omega QMDs `fd43cef` · Drake Stapleton · 2026-11-20 21:34 · Browse - Preserve routing ties and validate kernel launch bounds `e9b0e05` · Drake Stapleton · 2026-11-20 21:25 · Browse - Build DeepSeek V4.1 Flash text kernels in seeded Omega `e5c2b4c` · Drake Stapleton · 2026-11-20 21:24 · Browse - Resolve OSC struct and field names and evaluate return contracts in Omega `86b8826` · Drake Stapleton · 2026-11-20 21:19 · Browse - Run float32 SGD parameter and momentum updates in seeded Omega `6ed539b` · Drake Stapleton · 2026-11-20 21:16 · Browse - Use seeded Omega for tensor shapes, strides and view bounds `5d2799e` · Drake Stapleton · 2026-11-20 21:10 · Browse - Check OSC calls, contracts and external slice arguments in Omega `a7ce511` · Drake Stapleton · 2026-11-20 21:09 · Browse - Save and restore OSC ownership state in compact Omega snapshots `f926452` · Drake Stapleton · 2026-11-20 20:26 · Browse - Restore final identifiers in completions without a trailing newline `d2d1ee9` · Drake Stapleton · 2026-11-20 20:31 · Browse - Train the coding controller to choose a second behavior check `a8caf6b` · Drake Stapleton · 2026-11-20 20:28 · Browse - Track OSC pooled handle provenance and branch snapshots in Omega `0088891` · Drake Stapleton · 2026-11-20 19:02 · Browse - Evaluate lexical destruction and arena escape in Omega `b1fefff` · Drake Stapleton · 2026-11-20 18:20 · Browse - Write OSC checker model traces in seeded Omega `fbfb0c1` · Drake Stapleton · 2026-11-20 18:20 · Browse - Combine OSC encoder checks and avoid clearing unused checker storage `cb09e65` · Drake Stapleton · 2026-11-20 18:19 · Browse 90 files changed, +11663 -1198 CONTRIBUTORS.md +23 -1 FOLDERS.md +5 -0 docs/autodiff/M21_OMEGA_AUTODIFF.md +2 -1 mk/compiler.mk +9 -1 mk/train.mk +4 -3 om/compiler/osc_a64_fields.om +248 -0 om/compiler/osc_artifact.om +94 -0 om/compiler/osc_flow.om +776 -0 om/compiler/osc_lifetimes.om +150 -0 om/compiler/osc_ownership.om +155 -0 om/compiler/osc_pool.om +100 -0 om/compiler/osc_scalar.om +252 -0 om/compiler/osc_scope.om +88 -0 om/compiler/osc_statement.om +93 -17 om/compiler/osc_trace.om +88 -0 om/gpu/dsv41/README.md +85 -0 om/gpu/dsv41/build.sh +34 -0 om/gpu/dsv41/check-head.py +101 -0 om/gpu/dsv41/check.py +346 -0 om/gpu/dsv41/emit-cli.om +151 -0 om/gpu/dsv41/emit.om +1159 -0 om/gpu/dsv41/head/launch.om +163 -0 om/gpu/dsv41/head/session.om +282 -0 om/gpu/dsv41/head/weights.om +152 -0 om/gpu/dsv41/kernels.list +28 -0 om/gpu/dsv41/lookup.om +40 -0 om/gpu/dsv41/mma/README.md +9 -0 om/gpu/dsv41/mma/RESEARCHER-PROMPT.md +23 -0 om/gpu/dsv41/mma/mma_fp4_s2.om +175 -0 om/gpu/dsv41/mma/mma_fp4_s2_tile.om +191 -0 om/gpu/dsv41/mma/mma_fp8.om +143 -0 om/gpu/dsv41/mma/mma_fp8_tile.om +159 -0 om/runtime/rx_costmodel_core.om +69 -0 om/units.sh +28 -4 seed/kflow/check-driver-abi.py +34 -0 seed/kflow/kpack.om +73 -7 src/autodiff/gradient_shape.om +52 -0 src/autodiff/omega_autodiff.c +12 -21 src/compiler/osc_a64.c +74 -122 src/compiler/osc_cg.c +9 -23 src/compiler/osc_check.c +319 -355 src/compiler/osc_ir.c +103 -246 src/compiler/osc_lower.c +15 -13 src/compiler/osc_parse.c +25 -29 src/compiler/oscc_main.c +22 -30 src/omega_api_decl.h +289 -0 src/omega_native.h +143 -7 src/omega_om_units.c +2270 -116 src/omega_om_units.ident +15 -7 src/runtime/rx_costmodel.c +28 -77 src/tensor/omega_tensor.c +83 -60 src/tensor/omega_tensor_store.c +16 -13 src/tensor/tensor_shape.om +170 -0 src/train/generation.om +58 -0 src/train/sgd_update.om +44 -0 src/train/tg_sgd.c +41 -17 src/train/tg_store.c +9 -24 tests/compiler/neg/flow_return_from_regions.osc +10 -0 tests/compiler/neg/flow_zero_trip_return.osc +6 -0 tests/compiler/test_osc_compiler.c +6 -0 tests/compiler/test_osc_flow.c +65 -0 tests/train/test_train.c +44 -0 tools/autodiff_mutations.sh +24 -4 train/omega-llm/README.md +2 -0 train/omega-llm/tools/controller/README.md +34 -0 train/omega-llm/tools/controller/build.sh +31 -0 train/omega-llm/tools/controller/calibration/checkpoints.sha256 +3 -0 train/omega-llm/tools/controller/calibration/second-augmented-training.hex +6 -0 train/omega-llm/tools/controller/calibration/second-training.hex +4 -0 train/omega-llm/tools/controller/calibration/training.hex +8 -0 train/omega-llm/tools/controller/controller.om +207 -0 train/omega-llm/tools/controller/example/answer.om +3 -0 train/omega-llm/tools/controller/example/example.om +6 -0 train/omega-llm/tools/controller/example/prompt.om +2 -0 train/omega-llm/tools/controller/example/second.om +6 -0 train/omega-llm/tools/controller/normalize.om +166 -0 train/omega-llm/tools/controller/policy-train.om +27 -0 train/omega-llm/tools/controller/policy.om +123 -0 train/omega-llm/tools/controller/replay.sh +74 -0 train/omega-llm/tools/controller/resident.om +217 -0 train/omega-llm/tools/controller/run-01/answers.json +1 -0 train/omega-llm/tools/controller/run-01/cases.tsv +241 -0 train/omega-llm/tools/controller/run-01/panel.json +105 -0 train/omega-llm/tools/controller/run-01/scores.json +140 -0 train/omega-llm/tools/controller/run-02/answers.json +1 -0 train/omega-llm/tools/controller/run-02/cases.tsv +321 -0 train/omega-llm/tools/controller/run-02/panel.json +125 -0 train/omega-llm/tools/controller/run-02/scores.json +186 -0 train/omega-llm/tools/controller/second.om +115 -0 train/omega-llm/tools/controller/train-second.om +25 -0 CONTRIBUTORS.md @@ -7,6 +7,15 @@ The current development method is [Human-Directed Agentic Engineering (HDAE)](RE 7 7 8 8 | Model | What it did | 9 9 |---|---| 10 +| OpenAI Codex | Migrated OSC AST node initialization and contract token-stream rendering to seeded Omega. | 11 +| OpenAI Codex | Ported the M22 training snapshot header codec and generation validation to seeded Omega. | 12 +| OpenAI Codex | Migrated the reaction runtime cost model’s OGCM v1 state encoding, validation and restoration to seeded Omega. | 13 +| OpenAI Codex | Ported canonical tensor value serialization for F32, F16 and BF16 identities and equality to seeded Omega. | 14 +| OpenAI Codex | Ported branch drop-flag assignment, definite arena reservations and IR instruction initialization to seeded Omega. | 15 +| OpenAI Codex | Ported the OSC AArch64 fixed-opcode matcher, register encoding and typed-register decoding to seeded Omega. | 16 +| OpenAI Codex | Ported M21 gradient unbroadcast reduction planning, reduced-axis restoration and shape matching to seeded Omega. | 17 +| OpenAI Codex | Ported OSC IR register dataflow, arena and pool discipline, structured contract validation, scoped symbol declarations, AArch64 move-wide constant selection, and CLI artifact entry serialization to seeded Omega. | 18 +| OpenAI Codex | Trained and tested the seeded Omega coding controller on Spark, including its learned second-example check and CPU behavior replay. | 10 19 | OpenAI Codex | Brought the bundle corpus builder and compiler feedback harness from aien-train into Omega LLM with their original Git history. | 11 20 | Gemini 4 Neon (via Antigravity) | Closed remaining OM-2 Blackwell IR boundary gaps: `omega_bw_ir_from_om` (`src/omega_bw_om_ir.h`) now clears `prog` via `omega_bw_ir_init` on every error path (preventing partial-program residue), all remaining OM-2 codegen wrappers (`bf16`, `moe2`, `pf`, `pfhost`, `qmma`) zero-initialize `ins`/`meta` and init `prog`, and `tests/test_bw_om_codegen_bounds.c` adds partial-residue checks, `qmma`/`pfhost` invalid-spec sweeps, and max-size legal input headroom checks (173/173 PASS on Spark AArch64; zero calls to aborting `apic` GPU/codegen wrappers in `src/`). Completed live GB10 silicon validation (`spark-window 20261119T233031Z-d8ecb9`, exclusive, quiet machine): `gpu_qmma_probe_test` PASS (262,144/262,144 exact, 238.47 TFLOPS peak, 1,048,576/1,048,576 F2E4 exact), `gpu_pfhost_test --chip` PASS (router `rl` 0/262,144 diffs 0.314 ms, combine `mc` 0/4,194,304 diffs 0.818 ms, mutants 1..2 caught), `gpu_fp8gemm_test` 36/36 PASS (bit-identical, 1.90–2.78× vs GEMV), `gpu_moe_window_test --small` 101/101 PASS. | 12 21 | Gemini 4 Neon (via Antigravity) | Hardened the pure-OM-2 Blackwell IR boundary (`src/omega_bw_om_ir.h` validates `meta[5]`, instruction/vreg/uvreg/loop bounds, vreg/uvreg widths, opcode range, register/subregister/barrier/flag bounds; `*_patch` in `cg_{bf16,gemm,moe,moe2}.om` guards `pos < 1024`; `gemm`/`moe` wrappers zero-init buffers and reset `prog` on any failure) and added host boundary + kernel-metrics test `tests/test_bw_om_codegen_bounds.c` (76/76 PASS; 10/10 compiled kernels byte-for-byte identical SM121 SASS SHA-256, GPRs, occupancy, and avg stalls vs C parent ``3d0cb3500``). Re-verified on live GB10 silicon (`spark-window 20261119T231305Z-1a8c84`, exclusive): `gpu_q38_graph_test` 22/22 PASS, `gpu_q38_stroke_exact_test` 71/71 PASS, 4-layer 27B `q38_stress_bench` PASS (`N=8` batched `plain bf16` 33.919 ms / 4.16×, `64x8` sustained `bat_exact` mean 41.826 ms / 191.27 tok/s). | @@ -362,7 +371,9 @@ Update this table in the same commit as the work it credits. 362 371 | Codex (under Drake Stapleton’s direction) | Replayed the nine-check Spark GB10 Q38 stress suite on ``371a882a5`` and published the [November 20 measurements](docs/gpu/Q38_27B_GB10_STRESS_`20261120`.md). | 363 372 | Codex (under Drake Stapleton’s direction) | Implemented the seeded Omega training harness for native tokenization, CPU and GB10 fine-tuning and held-out evaluation. | 364 373 | Codex (under Drake Stapleton’s direction) | Trained and evaluated Omega coding checkpoints on Spark and packaged the native example-checked chain and behavior replay. | 365 -| OpenAI Codex (OSC semantic checker) | Ported the owner access, borrow, reborrow and move decisions to seeded Omega. Ported branch joins for ownership, handle provenance and definite frees. Migrated owner initialization and direct and nested borrow lifecycles. Integrated the OSC parser and scalar series, migrated the scoped symbol index, pool accounting and whole-input token scan, and batched owner joins. | 374 +| OpenAI Codex | Migrated binary32 SGD and heavy-ball momentum updates, retaining transaction replay and gradient alias behavior. | 375 +| OpenAI Codex | Migrated tensor shape validation, row-major strides, view bounds and broadcast shape calculation to seeded Omega. | 376 +| OpenAI Codex (OSC semantic checker) | Migrated struct and field name resolution and return-site postcondition evaluation. Migrated call argument pairing, argument forms, cell-slice alias checks and call-site contract evaluation to seeded Omega. Ported native and interpreter slice argument validation, including address range and alias checks. Ported the owner access, borrow, reborrow and move decisions to seeded Omega. Ported branch joins for ownership, handle provenance and definite frees. Migrated owner initialization and direct and nested borrow lifecycles. Integrated the OSC parser and scalar series, migrated the scoped symbol index, pool accounting and whole-input token scan, and batched owner joins. | 366 377 | Codex (under Drake Stapleton's direction) | Migrated OSC token scanning to seeded Omega and added differential tests against the C lexer. | 367 378 | Codex (under Drake Stapleton's direction) | Moved OSC scalar, array, reference and parameter type recognition into the seeded Omega parser. | 368 379 | Codex (under Drake Stapleton's direction) | Converted OSC binary-expression precedence, associativity and comparison-chain refusal to seeded Omega. | @@ -370,3 +381,14 @@ Update this table in the same commit as the work it credits. 370 381 | Codex (under Drake Stapleton's direction) | Migrated OSC name-led and return statements, loop bounds, import declarations, and contract text formatting to seeded Omega and checked their ASTs and diagnostics against the historical parser. | 371 382 | Codex (under Drake Stapleton's direction) | Converted the OSC constant-expression walk, integer folding and contract expression evaluation to seeded Omega. | 372 383 | Codex (under Drake Stapleton's direction) | Migrated scalar operator type inference, operand contexts and checked refusal ordering into seeded Omega. | 384 +| Codex (under Drake Stapleton's direction) | Moved OSC AArch64 instruction round-trip verification into the seeded encoder and combined field validation with packing. Reduced checker initialization and literal-classification work. | 385 +| Codex (under Drake Stapleton's direction) | Converted OSC ownership, arena, handle and branch trace serialization, model ID limits and overflow tracking to seeded Omega. | 386 +| Codex (under Drake Stapleton's direction) | Rewrote lexical scope destruction in Omega, including nested borrow closure, conditional owner release, arena escape and pool teardown. | 387 +| Codex (under Drake Stapleton's direction) | Converted OSC handle provenance, stale read/write/free decisions, branch snapshots and loop settlement to seeded Omega. | 388 +| Codex (under Drake Stapleton's direction) | Converted OSC owner and borrow snapshots, rollback of fresh borrows, branch joins and move replay to seeded Omega. | 389 +| Codex (under Drake Stapleton's direction) | Migrated OSC statement reachability, return-path analysis, IR block layout and partition validation, and definite-assignment propagation to seeded Omega. | 390 +| Codex (under Drake Stapleton's direction) | Implemented seeded Omega SM121 kernels and decode tables for the DeepSeek-V4.1-Flash text backbone, added driver-loadable cubin metadata and single-Spark numerical checks. | 391 +| Codex (under Drake Stapleton's direction) | Built the resident pure-Omega DeepSeek-V4.1-Flash final-head executor, native QMD submission chain, packed-BF16 vocabulary projection, and full-vocabulary checkpoint comparison. | 392 + 393 +- Drake Stapleton and Codex — calibrated and measured DeepSeek mixed-format SM121a expert projections against the pinned checkpoint on GB10. 394 +- Codex, under Drake Stapleton’s direction, documented the partner researcher setup and benchmark replay. FOLDERS.md @@ -551,3 +551,8 @@ Trinary Doctrine wake-signal tool (`trit`, T-002) in seeded OM-2 sharing `tools/ 551 551 ## `seed/omc/tests/struct-bool/` 552 552 553 553 Boolean struct-field execution regressions, exact expected outcomes, scalar controls and deliberate broken-compiler controls. Run by the seeded host gate. Results are kept in unique build directories. 554 +Boolean struct-field execution regressions, exact expected outcomes, scalar controls and deliberate broken-compiler controls. Run by the seeded host gate. Results are kept in unique build directories. 555 + 556 +## `om/gpu/dsv41/` 557 + 558 +Seeded Omega SM121 kernel emitters, their byte-level ABI, the native final-head executor, and single-Spark numerical checks for the original DeepSeek-V4.1-Flash text checkpoint. docs/autodiff/M21_OMEGA_AUTODIFF.md @@ -42,7 +42,8 @@ any edit of `src/tensor` or `src/train`. 42 42 | File | Role | 43 43 |---|---| 44 44 | `src/autodiff/omega_autodiff.h` | The meaning: ops, ownership, determinism rules, per-op error contract | 45 -| `src/autodiff/omega_autodiff.c` | The tape: recording, backward walk, unbroadcast, M22 flatten. No heap allocation | 45 +| `src/autodiff/omega_autodiff.c` | Tape recording, reverse walk, tensor ownership and M22 flatten | 46 +| `src/autodiff/gradient_shape.om` | Unbroadcast reduction order, shape equality and restoration of a reduced axis on the caller’s stack | 46 47 | `tests/test_omega_autodiff.c` | Contract, bit-exact replay, finite-difference, determinism, error-path and leak checks | 47 48 | `tools/autodiff_mutations.sh` | Source mutation sweep (12 mutants) | 48 49 | `mk/autodiff.mk` | `make test-autodiff`, `make test-autodiff-mutations` (alias `autodiff-mutants`) | mk/compiler.mk @@ -38,7 +38,15 @@ $(OSC_DIR)/test_osc_lex: $(OSC_LEX_TEST_SRC) $(OSC_HDRS) src/omega_api_decl.h 38 38 test-osc-lex: $(OSC_DIR)/test_osc_lex 39 39 @$(OSC_DIR)/test_osc_lex $(OSC_LEX_FIXTURES) 40 40 41 -test-compiler test-compiler-quick: test-osc-lex 41 +$(OSC_DIR)/test_osc_flow: tests/compiler/test_osc_flow.c $(OSC_LIB) $(OSC_HDRS) src/omega_native.h 42 + @mkdir -p $(OSC_DIR) 43 + $(CC) $(OSC_CFLAGS) -Wl,--wrap=mmap -o $@ $< $(OSC_LIB) 44 + 45 +.PHONY: test-osc-flow 46 +test-osc-flow: $(OSC_DIR)/test_osc_flow 47 + @$(OSC_DIR)/test_osc_flow 48 + 49 +test-compiler test-compiler-quick: test-osc-lex test-osc-flow 42 50 43 51 # Optional differential run for source checkouts with Git history. Compile the 44 52 # last all-C parser independently and keep it out of the production link. mk/train.mk @@ -18,7 +18,7 @@ TRAIN_DIR = $(OUT_DIR)/train 18 18 TRAIN_CFLAGS = -std=gnu11 -Wall -Wextra -Werror -D_GNU_SOURCE -O2 -ffp-contract=off -Isrc 19 19 TRAIN_ASAN = -O1 -g -fsanitize=address,undefined -fno-sanitize-recover=all 20 20 TRAIN_SRCS = src/train/tg_store.c src/train/tg_sgd.c src/omega_om_units.c 21 -TRAIN_HDRS = src/train/tg_store.h src/train/tg_sgd.h src/sha256.h 21 +TRAIN_HDRS = src/train/tg_store.h src/train/tg_sgd.h src/sha256.h src/omega_api_decl.h 22 22 23 23 $(TRAIN_DIR)/test_train: tests/train/test_train.c $(TRAIN_SRCS) $(TRAIN_HDRS) 24 24 @mkdir -p $(TRAIN_DIR) @@ -29,10 +29,11 @@ $(TRAIN_DIR)/test_train_asan: tests/train/test_train.c $(TRAIN_SRCS) $(TRAIN_HDR 29 29 $(CC) $(TRAIN_CFLAGS) $(TRAIN_ASAN) -o $@ tests/train/test_train.c $(TRAIN_SRCS) -lm 30 30 31 31 train-purity: 32 - @bad=$$(grep -hE '^[[:space:]]*#[[:space:]]*include' src/train/*.c src/train/*.h | \ 32 + @bad=$$(grep -HE '^[[:space:]]*#[[:space:]]*include' src/train/*.c src/train/*.h | \ 33 + grep -vE '^src/train/tg_(sgd|store)\.c:#[[:space:]]*include[[:space:]]+"omega_api_decl\.h"$$' | \ 33 34 grep -vE '#[[:space:]]*include[[:space:]]+(<[a-z/]+\.h>|"sha256\.h"|"train/tg_[a-z]+\.h")' || true); \ 34 35 if [ -n "$$bad" ]; then echo "train-purity: FAIL, unexpected includes:"; echo "$$bad"; exit 1; fi; \ 35 - echo "train-purity: src/train depends only on libc, sha256 and itself" 36 + echo "train-purity: src/train depends only on libc, sha256, the seeded SGD and generation bindings and itself" 36 37 37 38 test-train: train-purity $(TRAIN_DIR)/test_train $(TRAIN_DIR)/test_train_asan 38 39 ./$(TRAIN_DIR)/test_train $(TRAIN_DIR)/scratch > $(TRAIN_DIR)/plain.log 2>&1; rc=$$?; \ om/compiler/osc_a64_fields.om @@ -22,7 +22,25 @@ note bridge c-define OSC_A64_ERR_WRITEBACK_ALIAS 16 22 22 note bridge c-define OSC_A64_ERR_LOAD_ALIAS 17 23 23 note bridge c-define OSC_A64_ERR_BRANCH_OFFSET 18 24 24 note bridge c-define OSC_A64_ERR_BRANCH_COND 19 25 +note bridge c-define OSC_A64_ERR_UNUSED_REG 32 26 +note bridge c-define OSC_A64_ERR_REG_SP 36 27 +note bridge c-define OSC_A64_ERR_REG_ZR 40 28 +note bridge c-define OSC_A64_ERR_BAD_REG 44 25 29 om2 30 +layout a64spec 24 31 +field base uint at 8 little 32 +field form uchar at 12 little 33 +field role uchar 4 at 13 little 34 +field flags uchar at 17 little 35 +end 36 +layout a64insn 24 37 +field opcode uchar at 0 little 38 +field reg uchar 4 at 1 little 39 +field shift uchar at 5 little 40 +field cond uchar at 6 little 41 +field imm long at 8 little 42 +field imm2 long at 16 little 43 +end 26 44 layout a64field 40 27 45 field rd uint at 0 little 28 46 field rn uint at 4 little @@ -274,3 +292,233 @@ if or eq cls CLS_BCOND eq cls CLS_CB 274 292 end 275 293 ret 0 276 294 end 295 + 296 +note Checked encoder result: the low word is an instruction on success. 297 +note The high word carries the existing diagnostic code on refusal. 298 +export fn osc_a64_checked_word form ulong base ulong f view a64field flags ulong to ulong 299 + status = osc_a64_check_fields form f and flags 1 shr flags 1 300 + if ne status 0 301 + ret shl status 32 302 + end 303 + ret osc_a64_pack_fields form base f 304 +end 305 + 306 +note The opcode matcher has already checked the fixed bits. Unpack, check 307 +note and re-encode each candidate inside the same Omega invocation, so a 308 +note successful decode always proves the original word is canonical. 309 +export fn osc_a64_canonical_word form ulong base ulong word ulong flags ulong fields edit a64field to ulong 310 + osc_a64_unpack_fields form word fields 311 + if and eq form CLS_BREG eq get fields rn 31 312 + ret 0 313 + end 314 + if ne osc_a64_checked_word form base fields flags word 315 + ret 0 316 + end 317 + ret 1 318 +end 319 + 320 +note Plan the move-wide instructions for a 64-bit constant in lane order. 321 +note Each record holds the opcode in the high word, shift and immediate in 322 +note the low word. A tied MOVZ/MOVN cost selects MOVZ for stable code bytes. 323 +export fn osc_a64_materialize value ulong plan edit ulong 4 to ulong 324 + zeros = ulong 0 325 + ones = ulong 0 326 + for lane ulong 4 327 + chunk = and shr value mul lane 16 65535 328 + if eq chunk 0 329 + zeros = add zeros 1 330 + end 331 + if eq chunk 65535 332 + ones = add ones 1 333 + end 334 + end 335 + fill = ulong 0 336 + initial = ulong 25 337 + if gt ones zeros 338 + fill = 65535 339 + initial = 26 340 + end 341 + count = ulong 0 342 + for lane ulong 4 343 + shift = mul lane 16 344 + chunk = and shr value shift 65535 345 + if ne chunk fill 346 + opcode = ulong 27 347 + if eq count 0 348 + opcode = initial 349 + chunk = xor chunk fill 350 + end 351 + payload = or chunk shl shift 16 352 + put plan count or shl opcode 32 payload 353 + count = add count 1 354 + end 355 + end 356 + if eq count 0 357 + put plan 0 shl initial 32 358 + ret 1 359 + end 360 + ret count 361 +end 362 + 363 +note The descriptor table is indexed in public opcode order. Class masks 364 +note select the variable bits excluded from the fixed opcode comparison. 365 +fn a64_mask form ulong to ulong 366 + if eq form CLS_RRR_SH 367 + ret 0x00dfffff 368 + end 369 + if eq form CLS_RI 370 + ret 0x007fffff 371 + end 372 + if eq form CLS_R4 373 + ret 0x001f7fff 374 + end 375 + if eq form CLS_MULH 376 + ret 0x001f03ff 377 + end 378 + if eq form CLS_R3 379 + ret 0x001f03ff 380 + end 381 + if eq form CLS_BFM 382 + ret 0x003fffff 383 + end 384 + if eq form CLS_CSINC 385 + ret 0x001ff3ff 386 + end 387 + if eq form CLS_MOV16 388 + ret 0x007fffff 389 + end 390 + if eq form CLS_MEM 391 + ret 0x003fffff 392 + end 393 + if eq form CLS_PAIR 394 + ret 0x003fffff 395 + end 396 + if eq form CLS_B26 397 + ret 0x03ffffff 398 + end 399 + if eq form CLS_BCOND 400 + ret 0x00ffffef 401 + end 402 + if eq form CLS_CB 403 + ret 0x00ffffff 404 + end 405 + if eq form CLS_BREG 406 + ret 0x000003e0 407 + end 408 + if eq form CLS_BRK 409 + ret 0x001fffe0 410 + end 411 + ret 0 412 +end 413 + 414 +fn a64_encode_registers spec view a64spec insn view a64insn fields edit a64field to ulong 415 + regs = new uint 4 0 416 + for i ulong 4 417 + if and eq get spec role i 0 ne get insn reg i 0 418 + ret shl add 32 i 32 419 + end 420 + end 421 + for i ulong 4 422 + role = get spec role i 423 + regno = get insn reg i 424 + if le regno 30 425 + put regs i uint regno 426 + else 427 + if eq regno 33 428 + if ne role 2 429 + ret shl add 36 i 32 430 + end 431 + else 432 + if eq regno 32 433 + if ne role 1 434 + ret shl add 40 i 32 435 + end 436 + else 437 + ret shl add 44 i 32 438 + end 439 + end 440 + put regs i 31 441 + end 442 + end 443 + put fields rd get regs 0 444 + put fields rn get regs 1 445 + put fields rm get regs 2 446 + put fields ra get regs 3 447 + put fields shift uint get insn shift 448 + put fields cond uint get insn cond 449 + put fields imm get insn imm 450 + put fields imm2 get insn imm2 451 + ret osc_a64_checked_word ulong get spec form ulong get spec base fields ulong get spec flags 452 +end 453 + 454 +export fn osc_a64_write spec view uchar 24 insn view uchar 24 to ulong 455 + fields = new uchar 40 0 456 + ret a64_encode_registers spec insn fields 457 +end 458 + 459 +fn a64_decode_register role uchar bits uint to uchar 460 + if eq role 0 461 + ret 0 462 + end 463 + if lt bits 31 464 + ret uchar bits 465 + end 466 + if eq role 1 467 + ret 32 468 + end 469 + if eq role 2 470 + ret 33 471 + end 472 + ret 255 473 +end 474 + 475 +fn a64_try_opcode word ulong op ulong spec view a64spec insn edit a64insn fields edit a64field to ulong 476 + form = ulong get spec form 477 + base = ulong get spec base 478 + if eq osc_a64_canonical_word form base word ulong get spec flags fields 0 479 + ret 0 480 + end 481 + put insn opcode uchar op 482 + put insn reg 0 a64_decode_register get spec role 0 get fields rd 483 + put insn reg 1 a64_decode_register get spec role 1 get fields rn 484 + put insn reg 2 a64_decode_register get spec role 2 get fields rm 485 + put insn reg 3 a64_decode_register get spec role 3 get fields ra 486 + put insn shift uchar get fields shift 487 + put insn cond uchar get fields cond 488 + put insn imm get fields imm 489 + put insn imm2 get fields imm2 490 + ret 1 491 +end 492 + 493 +fn a64_matches specs view uchar * pos ulong word ulong to bool 494 + form = ulong get specs add pos 12 495 + base = ulong get specs add pos 8 496 + base = or base shl ulong get specs add pos 9 8 497 + base = or base shl ulong get specs add pos 10 16 498 + base = or base shl ulong get specs add pos 11 24 499 + ret and ne form 0 eq and word xor a64_mask form 0xffffffff base 500 +end 501 + 502 +fn a64_copy_spec specs view uchar * pos ulong spec edit uchar 24 to void 503 + for i ulong 10 504 + put spec add 8 i get specs add add pos 8 i 505 + end 506 +end 507 + 508 +export fn osc_a64_read word ulong specs view uchar * insn edit uchar 24 to ulong 509 + fields = new uchar 40 0 510 + spec = new uchar 24 0 511 + n = div len specs 24 512 + op = ulong 1 513 + while lt op n bound 255 514 + pos = mul op 24 515 + if a64_matches specs pos word 516 + a64_copy_spec specs pos spec 517 + if ne a64_try_opcode word op spec insn fields 0 518 + ret 1 519 + end 520 + end 521 + op = add op 1 522 + end 523 + ret 0 524 +end om/compiler/osc_artifact.om new file mode 100644 @@ -0,0 +1,94 @@ 1 +note SPDX-License-Identifier: Apache-2.0 2 +note oscc artifact entry, in the text format consumed by the unit packager. 3 +note bridge c-type entry OscArtifactEntry 4 +om2 5 +layout entry 40 6 +field index uint at 0 little 7 +field count uint at 4 little 8 +field result uint at 8 little 9 +field kind uint 6 at 12 little 10 +field offset uint at 36 little 11 +end 12 + 13 +fn decimal value uint dst edit uchar * pos ulong to ulong 14 + digits = new uchar 10 0 15 + n = ulong 0 16 + while true 17 + put digits n uchar add rem value 10 48 18 + n = add n 1 19 + value = div value 10 20 + if eq value 0 21 + break 22 + end 23 + end 24 + if gt add pos n len dst 25 + ret len dst 26 + end 27 + for i ulong n 28 + put dst add pos i get digits sub sub n 1 i 29 + end 30 + ret add pos n 31 +end 32 + 33 +note Negative results -1 through -6 name a refused parameter register. 34 +note -7 means insufficient space or an arity outside the artifact format. 35 +export fn osc_artifact_entry row view entry name view uchar * dst edit uchar * to long 36 + count = ulong get row count 37 + if gt count 6 38 + ret -7 39 + end 40 + for i ulong count 41 + kind = get row kind i 42 + if or eq kind 0 or eq kind 10 gt kind 12 43 + ret sub long -1 long i 44 + end 45 + end 46 + pos = decimal get row index dst 0 47 + for i ulong 9 48 + if ge pos len dst 49 + ret -7 50 + end 51 + put dst pos 32 52 + value = uint 0 53 + if eq i 0 54 + value = uint count 55 + end 56 + if eq i 1 57 + value = get row result 58 + end 59 + if and ge i 2 lt i add count 2 60 + value = get row kind sub i 2 61 + end 62 + if eq i 8 63 + value = get row offset 64 + end 65 + pos = decimal value dst add pos 1 66 + end 67 + if gt add add pos len name 2 len dst 68 + ret -7 69 + end 70 + put dst pos 32 71 + pos = add pos 1 72 + for i ulong len name 73 + put dst add pos i get name i 74 + end 75 + pos = add pos len name 76 + put dst pos 10 77 + ret long add pos 1 78 +end 79 + 80 +fn digit_hex nibble uchar to uchar 81 + if lt nibble 10 82 + ret add nibble 48 83 + end 84 + ret add nibble 87 85 +end 86 + 87 +export fn osc_artifact_hex digest view uchar 32 dst edit uchar 65 to void 88 + for i ulong 32 89 + value = get digest i 90 + put dst mul i 2 digit_hex shr value 4 91 + put dst add mul i 2 1 digit_hex and value 15 92 + end 93 + put dst 64 0 94 +end om/compiler/osc_flow.om new file mode 100644 @@ -0,0 +1,776 @@ 1 +note SPDX-License-Identifier: Apache-2.0 2 +note OSC statement reachability and IR basic-block partitioning. 3 +note bridge c-define OSC_FLOW_RETURNS 1 4 +note bridge c-define OSC_FLOW_UNREACHABLE 2 5 +note bridge c-define OSC_BLOCK_BAD_RANGE 1 6 +note bridge c-define OSC_BLOCK_OVERLAP 2 7 +note bridge c-define OSC_BLOCK_NO_TERMINATOR 3 8 +note bridge c-define OSC_BLOCK_EARLY_TERMINATOR 4 9 +note bridge c-define OSC_BLOCK_UNCOVERED 5 10 +om2 11 + 12 +const node_size ulong 104 13 +const block_node ulong 3 14 +const if_node ulong 10 15 +const while_node ulong 11 16 +const for_node ulong 12 17 +const return_node ulong 13 18 +const arena_node ulong 27 19 +const pool_node ulong 28 20 +const absent ulong `4294967295` 21 +const ir_insn_size ulong 48 22 +const ir_br uchar 13 23 +const ir_ret uchar 15 24 + 25 +fn read_le bytes view uchar * pos ulong width ulong to ulong 26 + value = ulong 0 27 + for i ulong width 28 + value = or value shl ulong get bytes add pos i mul i 8 29 + end 30 + ret value 31 +end 32 + 33 +fn summarize_stmt nodes view uchar * id ulong flow edit uchar * to void 34 + base = mul id node_size 35 + kind = read_le nodes base 2 36 + returns = uchar 0 37 + if eq kind return_node 38 + returns = 1 39 + end 40 + if or eq kind arena_node eq kind pool_node 41 + returns = get flow read_le nodes add base 16 4 42 + end 43 + if eq kind if_node 44 + other = read_le nodes add base 20 4 45 + if ne other absent 46 + then_id = read_le nodes add base 16 4 47 + returns = and get flow other get flow then_id 48 + end 49 + end 50 + if eq kind block_node 51 + stmt = read_le nodes add base 12 4 52 + while ne stmt absent 53 + if ne returns 0 54 + put flow stmt or get flow stmt uchar 2 55 + else 56 + returns = get flow stmt 57 + end 58 + stmt = read_le nodes add mul stmt node_size 24 4 59 + end 60 + end 61 + put flow id returns 62 +end 63 + 64 +note Visit only statements, recording parents before their children. Reversing 65 +note this order computes each child's return status before its parent needs it. 66 +note A loop may execute zero times even if its entire body returns. 67 +export fn osc_statement_flow nodes view uchar * order edit ulong * flow edit uchar * to void 68 +requires eq len nodes mul len flow node_size 69 +requires ge len order len flow 70 + note The caller puts the function body in order[0]. 71 + count = ulong 1 72 + cursor = ulong 0 73 + while lt cursor count 74 + base = mul get order cursor node_size 75 + cursor = add cursor 1 76 + kind = read_le nodes base 2 77 + if eq kind block_node 78 + stmt = read_le nodes add base 12 4 79 + while ne stmt absent 80 + put order count stmt 81 + count = add count 1 82 + stmt = read_le nodes add mul stmt node_size 24 4 83 + end 84 + else 85 + loop_body = or eq kind while_node eq kind for_node 86 + region_body = or eq kind arena_node eq kind pool_node 87 + if or eq kind if_node or loop_body region_body 88 + put order count read_le nodes add base 16 4 89 + count = add count 1 90 + if eq kind if_node 91 + other = read_le nodes add base 20 4 92 + if ne other absent 93 + put order count other 94 + count = add count 1 95 + end 96 + end 97 + end 98 + end 99 + end 100 + for step ulong count 101 + id = get order sub sub count 1 step 102 + summarize_stmt nodes id flow 103 + end 104 +end 105 + 106 +fn check_block insns view uchar * cover edit uchar 4096 first ulong count ulong location edit ulong 1 to ulong 107 + if or eq count 0 gt add first count div len insns ir_insn_size 108 + ret 1 109 + end 110 + for offset ulong count 111 + i = add first offset 112 + if ne get cover i 0 113 + put location 0 i 114 + ret 2 115 + end 116 + put cover i 1 117 + op = get insns mul i ir_insn_size 118 + term = and ge op ir_br le op ir_ret 119 + if eq add offset 1 count 120 + if eq term false 121 + ret 3 122 + end 123 + else 124 + if term 125 + ret 4 126 + end 127 + end 128 + end 129 + ret 0 130 +end 131 + 132 +note Block order determines the first diagnostic. Report its block index for a 133 +note bad range or terminator and its instruction index for a coverage error. 134 +export fn osc_block_partition blocks view uchar * insns view uchar * location out ulong 1 to ulong 135 +requires eq rem len blocks 8 0 136 +requires eq rem len insns ir_insn_size 0 137 +requires le div len insns ir_insn_size 4096 138 + cover = new uchar 4096 0 139 + ninsns = div len insns ir_insn_size 140 + for b ulong div len blocks 8 141 + base = mul b 8 142 + first = read_le blocks base 4 143 + count = read_le blocks add base 4 4 144 + put location 0 b 145 + failure = check_block insns cover first count location 146 + if ne failure 0 147 + ret failure 148 + end 149 + end 150 + for i ulong ninsns 151 + if eq get cover i 0 152 + put location 0 i 153 + ret 5 154 + end 155 + end 156 + put location 0 0 157 + ret 0 158 +end 159 + 160 +note Each transfer record holds a block's generated register bitset followed 161 +note by its two successor indices, with the block count meaning no successor. 162 +note Propagate IN union GEN along each edge. Intersection at the destination 163 +note retains only registers defined on every incoming path. 164 +fn meet_words inputs edit ulong * generated view ulong * source ulong dest ulong to bool 165 + changed = false 166 + for k ulong len generated 167 + outgoing = or get inputs add source k get generated k 168 + old = get inputs add dest k 169 + restricted = and old outgoing 170 + if ne restricted old 171 + put inputs add dest k restricted 172 + changed = true 173 + end 174 + end 175 + ret changed 176 +end 177 + 178 +fn meet_edge inputs edit ulong * record view ulong * source ulong successor ulong to bool 179 + width = sub len record 2 180 + target = get record add width successor 181 + if eq target div len inputs width 182 + ret false 183 + end 184 + ret meet_words inputs part record 0 width source mul target width 185 +end 186 + 187 +export fn osc_definite_inputs transfers view ulong * inputs edit ulong * params ulong width ulong to void 188 +requires gt width 0 189 +requires eq rem len inputs width 0 190 +requires ge len inputs width 191 +requires eq len transfers mul div len inputs width add width 2 192 +requires le params 6 193 + blocks = div len inputs width 194 + for i ulong len inputs 195 + put inputs i 0xffffffffffffffff 196 + end 197 + for k ulong width 198 + put inputs k 0 199 + end 200 + put inputs 0 sub shl ulong 1 params 1 201 + changed = true 202 + while changed 203 + changed = false 204 + for b ulong blocks 205 + record = mul b add width 2 206 + for successor ulong 2 207 + if meet_edge inputs part transfers record add width 2 mul b width successor 208 + changed = true 209 + end 210 + end 211 + end 212 + end 213 +end 214 + 215 +fn copy_insn source view ulong 24576 dest edit ulong 24576 src ulong dst ulong to void 216 + for k ulong 6 217 + put dest add dst k get source add src k 218 + end 219 +end 220 + 221 +note Stable counting sort of emitted instructions by block, preserving both 222 +note block creation order and instruction emission order within each block. 223 +export fn osc_layout_blocks tags view short * source view ulong 24576 dest edit ulong 24576 blocks edit uint 1024 nblocks ulong to void 224 +requires le nblocks 512 225 +requires le len tags 4096 226 + for i ulong len tags 227 + label = get tags i 228 + if or lt label 0 ge long label long nblocks 229 + trap 3 230 + end 231 + end 232 + for b ulong nblocks 233 + put blocks add mul b 2 1 0 234 + end 235 + for i ulong len tags 236 + pos = add mul ulong get tags i 2 1 237 + put blocks pos add get blocks pos 1 238 + end 239 + first = uint 0 240 + for b ulong nblocks 241 + pos = mul b 2 242 + put blocks pos first 243 + first = add first get blocks add pos 1 244 + put blocks add pos 1 0 245 + end 246 + for i ulong len tags 247 + pos = mul ulong get tags i 2 248 + count = get blocks add pos 1 249 + dst = mul ulong add get blocks pos count 6 250 + put blocks add pos 1 add count 1 251 + copy_insn source dest mul i 6 dst 252 + end 253 +end 254 + 255 +note OscInsn stores signed 16-bit register indices. A decoded value with 256 +note the high bit set is absent or invalid. validate_insn checks every 257 +note mandatory operand before the resource and dataflow walks. 258 +const IR_CONST uchar 1 259 +const IR_MOV uchar 2 260 +const IR_BIN uchar 3 261 +const IR_UN uchar 4 262 +const IR_CMP uchar 5 263 +const IR_CAST uchar 6 264 +const IR_ALLOC uchar 7 265 +const IR_RELEASE uchar 8 266 +const IR_LOAD uchar 9 267 +const IR_STORE uchar 10 268 +const IR_CALL uchar 11 269 +const IR_CBR uchar 14 270 +const IR_FLOAD uchar 16 271 +const IR_FSTORE uchar 17 272 +const IR_AOPEN uchar 18 273 +const IR_AALLOC uchar 19 274 +const IR_ADESTROY uchar 20 275 +const IR_POPEN uchar 21 276 +const IR_PCLOSE uchar 22 277 +const IR_HALLOC uchar 23 278 +const IR_HGEN uchar 24 279 +const IR_HFREE uchar 25 280 +const IR_HLOAD uchar 26 281 +const IR_HSTORE uchar 27 282 +const IR_SLOAD uchar 28 283 +const IR_SSTORE uchar 29 284 +const NO_REG ulong 65535 285 + 286 +fn ir_def insn view uchar * to ulong 287 + op = get insn 0 288 + if and ge op IR_CONST le op IR_ALLOC 289 + ret read_le insn 2 2 290 + end 291 + if or eq op IR_LOAD or eq op IR_CALL eq op IR_FLOAD 292 + ret read_le insn 2 2 293 + end 294 + if or eq op IR_AOPEN or eq op IR_AALLOC eq op IR_POPEN 295 + ret read_le insn 2 2 296 + end 297 + if or eq op IR_HALLOC or eq op IR_HGEN or eq op IR_HLOAD eq op IR_SLOAD 298 + ret read_le insn 2 2 299 + end 300 + ret NO_REG 301 +end 302 + 303 +note Return uses in diagnostic order. Fixed slots match OSC_MAX_PARAMS. 304 +fn ir_uses insn view uchar * uses edit ushort 6 to ulong 305 + op = get insn 0 306 + a = ushort read_le insn 4 2 307 + b = ushort read_le insn 6 2 308 + c = ushort read_le insn 8 2 309 + put uses 0 a 310 + if or eq op IR_MOV or eq op IR_UN or eq op IR_CAST eq op IR_ALLOC 311 + ret 1 312 + end 313 + if or eq op IR_RELEASE or eq op IR_CBR or eq op IR_ADESTROY eq op IR_PCLOSE 314 + ret 1 315 + end 316 + if eq op ir_ret 317 + if lt a 32768 318 + ret 1 319 + end 320 + ret 0 321 + end 322 + put uses 1 b 323 + if or eq op IR_AALLOC or eq op IR_HALLOC eq op IR_HGEN 324 + ret 2 325 + end 326 + if or eq op IR_BIN or eq op IR_CMP eq op IR_LOAD 327 + ret 2 328 + end 329 + put uses 2 c 330 + if or eq op IR_STORE or eq op IR_HFREE eq op IR_HLOAD 331 + ret 3 332 + end 333 + if eq op IR_HSTORE 334 + put uses 3 ushort read_le insn 18 2 335 + ret 4 336 + end 337 + if or eq op IR_SLOAD eq op IR_SSTORE 338 + put uses 1 add a 1 339 + put uses 2 b 340 + if eq op IR_SSTORE 341 + put uses 3 c 342 + ret 4 343 + end 344 + ret 3 345 + end 346 + if or eq op IR_FLOAD eq op IR_FSTORE 347 + count = ulong 1 348 + if lt b 32768 349 + count = 2 350 + end 351 + if eq op IR_FSTORE 352 + put uses count c 353 + count = add count 1 354 + end 355 + ret count 356 + end 357 + if eq op IR_CALL 358 + count = ulong get insn 16 359 + if gt count 6 360 + count = 6 361 + end 362 + for i ulong count 363 + put uses i ushort read_le insn add 18 mul i 2 2 364 + end 365 + ret count 366 + end 367 + ret 0 368 +end 369 + 370 +export fn osc_ir_operands insn view uchar 48 uses edit ushort 6 to ulong 371 + ret ir_uses insn uses 372 +end 373 + 374 +fn reads_marked insn view uchar * marks view uchar * to bool 375 + uses = new ushort 6 0 376 + count = ir_uses insn uses 377 + for i ulong count 378 + if ne get marks ulong get uses i 0 379 + ret true 380 + end 381 + end 382 + ret false 383 +end 384 + 385 +note These passes follow the validator's public refusal order. Collect all 386 +note defining instructions first because a handle may occur in earlier blocks. 387 +fn arena_use insn view uchar * arenas view uchar * to ulong 388 + op = get insn 0 389 + dst = ir_def insn 390 + if and ne op IR_AOPEN lt dst len arenas 391 + if ne get arenas dst 0 392 + ret 3 393 + end 394 + end 395 + if eq op IR_AALLOC 396 + handle = read_le insn 6 2 397 + init = read_le insn 4 2 398 + if or eq get arenas handle 0 ne get arenas init 0 399 + ret 4 400 + end 401 + else 402 + if eq op IR_ADESTROY 403 + if eq get arenas read_le insn 4 2 0 404 + ret 5 405 + end 406 + else 407 + if reads_marked insn arenas 408 + ret 6 409 + end 410 + end 411 + end 412 + ret 0 413 +end 414 + 415 +fn check_store_value pools view uchar * types view uchar * pool ulong value ulong to ulong 416 + if ne get pools value 0 417 + ret 14 418 + end 419 + if ne get types value get pools pool 420 + ret 14 421 + end 422 + ret 0 423 +end 424 + 425 +fn pool_use insn view uchar * pools view uchar * types view uchar * to ulong 426 + op = get insn 0 427 + dst = ir_def insn 428 + if and ne op IR_POPEN lt dst len pools 429 + if ne get pools dst 0 430 + ret 8 431 + end 432 + end 433 + a = read_le insn 4 2 434 + b = read_le insn 6 2 435 + if eq op IR_PCLOSE 436 + if eq get pools a 0 437 + ret 9 438 + end 439 + ret 0 440 + end 441 + if eq op IR_HALLOC 442 + if or eq get pools b 0 ne get pools a 0 443 + ret 10 444 + end 445 + if ne get types a get pools b 446 + ret 11 447 + end 448 + ret 0 449 + end 450 + if and ge op IR_HGEN le op IR_HSTORE 451 + if or eq get pools a 0 ne get pools b 0 452 + ret 12 453 + end 454 + if ne op IR_HGEN 455 + if ne get pools read_le insn 8 2 0 456 + ret 12 457 + end 458 + end 459 + if eq op IR_HLOAD 460 + if ne get types dst get pools a 461 + ret 13 462 + end 463 + end 464 + if eq op IR_HSTORE 465 + ret check_store_value pools types a read_le insn 18 2 466 + end 467 + ret 0 468 + end 469 + if reads_marked insn pools 470 + ret 15 471 + end 472 + ret 0 473 +end 474 + 475 +fn slice_defs insns view uchar * slices view uchar 480 to ulong 476 + for i ulong div len insns ir_insn_size 477 + dst = ir_def part insns mul i ir_insn_size ir_insn_size 478 + if lt dst len slices 479 + if ne get slices dst 0 480 + ret or shl i 8 1 481 + end 482 + end 483 + end 484 + ret 0 485 +end 486 + 487 +fn check_arenas insns view uchar * arenas edit uchar 480 params ulong to ulong 488 + for i ulong div len insns ir_insn_size 489 + start = mul i ir_insn_size 490 + if eq get insns start IR_AOPEN 491 + dst = read_le insns add start 2 2 492 + if or ne get arenas dst 0 lt dst params 493 + ret or shl i 8 2 494 + end 495 + put arenas dst 1 496 + end 497 + end 498 + for i ulong div len insns ir_insn_size 499 + status = arena_use part insns mul i ir_insn_size ir_insn_size arenas 500 + if ne status 0 501 + ret or shl i 8 status 502 + end 503 + end 504 + ret 0 505 +end 506 + 507 +fn check_pools insns view uchar * arenas view uchar 480 types view uchar 480 params ulong to ulong 508 + pools = new uchar 480 0 509 + for i ulong div len insns ir_insn_size 510 + start = mul i ir_insn_size 511 + if eq get insns start IR_POPEN 512 + dst = read_le insns add start 2 2 513 + used = or get pools dst get arenas dst 514 + if or ne used 0 lt dst params 515 + ret or shl i 8 7 516 + end 517 + put pools dst get insns add start 1 518 + end 519 + end 520 + for i ulong div len insns ir_insn_size 521 + status = pool_use part insns mul i ir_insn_size ir_insn_size pools types 522 + if ne status 0 523 + ret or shl i 8 status 524 + end 525 + end 526 + ret 0 527 +end 528 + 529 +note Return the first refused instruction above the low diagnostic byte. 530 +export fn osc_ir_resources insns view uchar * types view uchar 480 slices view uchar 480 params ulong to ulong 531 +requires eq rem len insns ir_insn_size 0 532 + arenas = new uchar 480 0 533 + status = slice_defs insns slices 534 + if ne status 0 535 + ret status 536 + end 537 + status = check_arenas insns arenas params 538 + if ne status 0 539 + ret status 540 + end 541 + ret check_pools insns arenas types params 542 +end 543 + 544 +fn mark_def bits edit ulong * dst ulong to void 545 + if ne dst NO_REG 546 + slot = div dst 64 547 + mask = shl ulong 1 rem dst 64 548 + value = or get bits slot mask 549 + put bits slot value 550 + end 551 +end 552 + 553 +fn register_gen insns view uchar * bits edit ulong * to void 554 + for j ulong len bits 555 + put bits j 0 556 + end 557 + for j ulong div len insns ir_insn_size 558 + dst = ir_def part insns mul j ir_insn_size ir_insn_size 559 + mark_def bits dst 560 + end 561 +end 562 + 563 +fn record_edges term view uchar * record edit ulong * absent_block ulong to void 564 + width = sub len record 2 565 + first = absent_block 566 + other = absent_block 567 + if ne get term 0 ir_ret 568 + first = read_le term 10 2 569 + if eq get term 0 IR_CBR 570 + other = read_le term 12 2 571 + end 572 + end 573 + put record width first 574 + put record add width 1 other 575 +end 576 + 577 +export fn osc_ir_block_transfer insns view uchar * record edit ulong * blocks ulong to void 578 + register_gen insns part record 0 sub len record 2 579 + record_edges part insns sub len insns ir_insn_size ir_insn_size record blocks 580 +end 581 + 582 +fn first_missing insn view uchar * available view ulong * to ulong 583 + uses = new ushort 6 0 584 + count = ir_uses insn uses 585 + for i ulong count 586 + reg = ulong get uses i 587 + word = get available div reg 64 588 + if eq and shr word rem reg 64 1 0 589 + ret add reg 1 590 + end 591 + end 592 + ret 0 593 +end 594 + 595 +export fn osc_ir_block_reads insns view uchar * available edit ulong * to ulong 596 + for j ulong div len insns ir_insn_size 597 + base = mul j ir_insn_size 598 + missing = first_missing part insns base ir_insn_size available 599 + if ne missing 0 600 + ret or shl j 16 missing 601 + end 602 + dst = ir_def part insns base ir_insn_size 603 + mark_def available dst 604 + end 605 + ret 0 606 +end 607 + 608 +note Version 6 structured contracts are postfix programs over value scalars. 609 +note The scalar type tags and term discriminants come from osc_ir.h. 610 +const CT_PARAM uchar 1 611 +const CT_RESULT uchar 2 612 +const CT_CONST uchar 3 613 +const CT_BIN uchar 4 614 +const CT_UN uchar 5 615 +const CT_CMP uchar 6 616 +const CT_CAST uchar 7 617 +const T_BOOL uchar 1 618 +const T_U8 uchar 2 619 +const T_U64 uchar 5 620 +const T_I8 uchar 6 621 +const T_I64 uchar 9 622 + 623 +export fn osc_ir_canonical scalar ulong value ulong to ulong 624 + if eq scalar ulong T_BOOL 625 + if lt value 2 626 + ret 1 627 + end 628 + ret 0 629 + end 630 + if or lt scalar ulong T_U8 gt scalar ulong T_I64 631 + ret 0 632 + end 633 + width = shl ulong 8 and sub scalar ulong T_U8 3 634 + if eq width 64 635 + ret 1 636 + end 637 + limit = shl ulong 1 width 638 + bits = and value sub limit 1 639 + if ge scalar ulong T_I8 640 + sign = shr limit 1 641 + bits = sub xor bits sign sign 642 + end 643 + if eq bits value 644 + ret 1 645 + end 646 + ret 0 647 +end 648 + 649 +fn term_operator op uchar subop uchar stack edit uchar 64 depth ulong to ulong 650 + if eq depth 0 651 + ret 0 652 + end 653 + b = get stack sub depth 1 654 + if eq op CT_UN 655 + if eq subop 3 656 + if eq b T_BOOL 657 + ret depth 658 + end 659 + else 660 + if and ne b T_BOOL or eq subop 1 eq subop 2 661 + ret depth 662 + end 663 + end 664 + ret 0 665 + end 666 + if eq op CT_CAST 667 + if or eq b T_BOOL or lt subop T_U8 gt subop T_I64 668 + ret 0 669 + end 670 + put stack sub depth 1 subop 671 + ret depth 672 + end 673 + if lt depth 2 674 + ret 0 675 + end 676 + a = get stack sub depth 2 677 + if eq op CT_CMP 678 + if or ne a b or lt subop 1 gt subop 6 679 + ret 0 680 + end 681 + put stack sub depth 2 T_BOOL 682 + ret sub depth 1 683 + end 684 + if or ne op CT_BIN or lt subop 1 gt subop 10 685 + ret 0 686 + end 687 + if ge subop 9 688 + if or eq a T_BOOL eq b T_BOOL 689 + ret 0 690 + end 691 + else 692 + if or ne a b and lt subop 6 eq a T_BOOL 693 + ret 0 694 + end 695 + end 696 + ret sub depth 1 697 +end 698 + 699 +fn contract_term term view uchar * params view uchar 256 result uchar stack edit uchar 64 depth ulong to ulong 700 + op = get term 0 701 + subop = get term 1 702 + arg = ulong get term 2 703 + imm = read_le term 8 8 704 + if eq op CT_PARAM 705 + if or ne subop 0 or ne imm 0 ge arg len params 706 + ret 0 707 + end 708 + ty = get params arg 709 + if eq ty 0 710 + ret 0 711 + end 712 + put stack depth ty 713 + ret add depth 1 714 + end 715 + if ne arg 0 716 + ret 0 717 + end 718 + if eq op CT_CONST 719 + if eq osc_ir_canonical ulong subop imm 0 720 + ret 0 721 + end 722 + put stack depth subop 723 + ret add depth 1 724 + end 725 + if ne imm 0 726 + ret 0 727 + end 728 + if eq op CT_RESULT 729 + if or ne subop 0 eq result 0 730 + ret 0 731 + end 732 + put stack depth result 733 + ret add depth 1 734 + end 735 + ret term_operator op subop stack depth 736 +end 737 + 738 +export fn osc_ir_contract terms view uchar * params view uchar 256 result ulong to ulong 739 + if eq len terms 0 740 + ret 1 741 + end 742 + if or ne rem len terms 16 0 gt div len terms 16 64 743 + ret 0 744 + end 745 + stack = new uchar 64 0 746 + depth = ulong 0 747 + for i ulong div len terms 16 748 + depth = contract_term part terms mul i 16 16 params uchar result stack depth 749 + if eq depth 0 750 + ret 0 751 + end 752 + end 753 + if and eq depth 1 eq get stack 0 T_BOOL 754 + ret 1 755 + end 756 + ret 0 757 +end 758 + 759 +note Start a fresh IR instruction with absent register, target and callee 760 +note fields. The result signals that the current block has terminated. 761 +export fn osc_begin_insn record out uchar 48 opcode ulong line ulong to ulong 762 + for i ulong 48 763 + put record i 0 764 + end 765 + for i ulong 14 766 + put record add 2 i 255 767 + end 768 + put record 0 uchar opcode 769 + for i ulong 4 770 + put record add 40 i uchar and shr line mul i 8 255 771 + end 772 + if and ge opcode ulong ir_br le opcode ulong ir_ret 773 + ret 1 774 + end 775 + ret 0 776 +end om/compiler/osc_lifetimes.om new file mode 100644 @@ -0,0 +1,150 @@ 1 +note SPDX-License-Identifier: Apache-2.0 2 +note End a range of lexical scopes, newest binding first. Emit one action for 3 +note each destruction that the trace or lowering release list needs. A live 4 +note borrow at arena exit stops the walk after all preceding actions. 5 +note bridge c-define OSC_SCOPE_END_BORROW 1 6 +note bridge c-define OSC_SCOPE_RELEASE_OWNER 2 7 +note bridge c-define OSC_SCOPE_DESTROY_ARENA 3 8 +note bridge c-define OSC_SCOPE_CLOSE_POOL 4 9 +note bridge c-define OSC_SCOPE_REFUSE_ESCAPE 5 10 +note bridge c-type exitwork OscScopeExit 11 +om2 12 + 13 +layout exitwork 1936 14 +field first uint at 0 little 15 +field count uint at 4 little 16 +field objects uint at 8 little 17 +field borrows uint at 12 little 18 +field actions uint 480 at 16 little 19 +end 20 + 21 +fn load32 data view uchar * pos ulong to ulong 22 + value = ulong 0 23 + for j ulong 4 24 + value = or value shl ulong get data add pos j mul j 8 25 + end 26 + ret value 27 +end 28 + 29 +fn dec16 data edit uchar * pos ulong to void 30 + high = shl ulong get data add pos 1 8 31 + value = sub or high ulong get data pos 1 32 + put data pos uchar and value 255 33 + put data add pos 1 uchar and shr value 8 255 34 +end 35 + 36 +fn end_borrow owners edit uchar * borrows edit uchar * id ulong to ulong 37 + if eq id `4294967295` 38 + ret 0 39 + end 40 + child = mul id 24 41 + if eq get borrows child 0 42 + ret 0 43 + end 44 + put borrows child 0 45 + count_offset = ulong 0 46 + if ne get borrows add child 1 0 47 + count_offset = 2 48 + end 49 + parent = load32 borrows add child 8 50 + if eq parent `4294967295` 51 + object = mul load32 borrows add child 4 20 52 + dec16 owners add add object 2 count_offset 53 + else 54 + dec16 borrows add add mul parent 24 12 count_offset 55 + end 56 + ret 1 57 +end 58 + 59 +fn end_owner owners edit uchar * id ulong to ulong 60 + pos = mul id 20 61 + if ne load32 owners add pos 16 0 62 + ret 0 63 + end 64 + moved = get owners pos 65 + if and ne moved 0 ne moved 3 66 + ret 0 67 + end 68 + put owners pos 2 69 + ret 2 70 +end 71 + 72 +fn end_arena owners edit uchar 5120 borrows view uchar 12288 symbol ulong work view exitwork to ulong 73 + region = add symbol 1 74 + for b ulong ulong get work borrows 75 + pos = mul b 24 76 + if ne get borrows pos 0 77 + object = mul load32 borrows add pos 4 20 78 + if eq load32 owners add object 16 region 79 + ret 5 80 + end 81 + end 82 + end 83 + for o ulong ulong get work objects 84 + pos = mul o 20 85 + if and eq get owners pos 0 eq load32 owners add pos 16 region 86 + put owners pos 2 87 + end 88 + end 89 + ret 3 90 +end 91 + 92 +fn end_symbol symbols view uchar 76800 owners edit uchar 5120 borrows edit uchar 12288 id ulong work view exitwork to ulong 93 + base = mul id 160 94 + kind = get symbols add base 68 95 + if eq kind 3 96 + ret end_borrow owners borrows load32 symbols add base 96 97 + end 98 + if eq kind 2 99 + ret end_owner owners load32 symbols add base 92 100 + end 101 + if eq kind 4 102 + ret end_arena owners borrows id work 103 + end 104 + if eq kind 5 105 + ret 4 106 + end 107 + ret 0 108 +end 109 + 110 +export fn osc_scope_exit visible view ushort 480 symbols view uchar 76800 owners edit uchar 5120 borrows edit uchar 12288 work edit exitwork to ulong 111 +requires le get work count 480 112 +requires le get work first get work count 113 +requires le get work objects 256 114 +requires le get work borrows 512 115 + count = ulong 0 116 + pos = ulong get work count 117 + while gt pos ulong get work first bound 480 118 + pos = sub pos 1 119 + id = ulong get visible pos 120 + action = end_symbol symbols owners borrows id work 121 + if ne action 0 122 + put work actions count uint or shl action 16 id 123 + count = add count 1 124 + if eq action 5 125 + break 126 + end 127 + end 128 + end 129 + ret count 130 +end 131 + 132 +note Only an allocation at the declaration's loop and branch depth is 133 +note guaranteed to consume cells. Refusals leave the arena counter intact. 134 +export fn osc_reserve_arena symbol edit uchar 160 loop_depth ulong branch_depth ulong cells ulong to ulong 135 + if or ne load32 symbol 88 loop_depth ne load32 symbol 108 branch_depth 136 + ret 1 137 + end 138 + cap = or ulong get symbol 104 shl ulong get symbol 105 8 139 + used = or ulong get symbol 106 shl ulong get symbol 107 8 140 + if gt used cap 141 + ret 0 142 + end 143 + if gt cells sub cap used 144 + ret 0 145 + end 146 + total = add used cells 147 + put symbol 106 uchar and total 255 148 + put symbol 107 uchar shr total 8 149 + ret 1 150 +end om/compiler/osc_ownership.om @@ -8,8 +8,18 @@ note bridge c-define OSC_OWN_READ_ONLY 3 8 8 note bridge c-define OSC_OWN_LOOP_MOVE 4 9 9 note bridge c-type oscowner OscCheckedOwner 10 10 note bridge c-type oscbor OscCheckedBorrow 11 +note bridge c-type snapdim OscSnapshotShape 12 +note bridge c-define OSC_SNAP_OWNER_WORDS 3 13 +note bridge c-define OSC_SNAP_BORROW_WORDS 2 11 14 om2 12 15 16 +layout snapdim 16 17 +field owners uint at 0 little 18 +field borrows uint at 4 little 19 +field symbols uint at 8 little 20 +field active uint at 12 little 21 +end 22 + 13 23 note The checker snapshots this record at each branch. sym is the owning 14 24 note symbol or -1, and arena1 is the arena symbol plus one or zero. 15 25 layout oscowner 20 @@ -197,3 +207,148 @@ requires eq len remaining len taken 197 207 end 198 208 ret count 199 209 end 210 + 211 +note The first three owner words carry the move state, child counts and move 212 +note line. Owner identity and arena affiliation are fixed at creation. For a 213 +note borrow only liveness and child counts can change after it is bound. 214 +note The remaining snapshot words hold each existing symbol's borrow index. 215 +fn save_owner_words owners view uint 1280 saved edit uint * count ulong to void 216 + for i ulong count 217 + src = mul i 5 218 + dst = mul i 3 219 + put saved dst get owners src 220 + put saved add dst 1 get owners add src 1 221 + put saved add dst 2 get owners add src 2 222 + end 223 +end 224 + 225 +fn save_borrow_words borrows view uint 3072 saved edit uint * base ulong count ulong to void 226 + for i ulong count 227 + src = mul i 6 228 + dst = add base mul i 2 229 + put saved dst get borrows src 230 + put saved add dst 1 get borrows add src 3 231 + end 232 +end 233 + 234 +fn save_bindings symbols view uint 19200 saved edit uint * base ulong count ulong to void 235 + for j ulong count 236 + put saved add base j get symbols add mul j 40 24 237 + end 238 +end 239 + 240 +export fn osc_snapshot_save shape view snapdim owners view uint 1280 borrows view uint 3072 symbols view uint 19200 saved edit uint * to void 241 +requires le get shape owners 256 242 +requires le get shape borrows 512 243 +requires le get shape symbols 480 244 + save_owner_words owners saved ulong get shape owners 245 + base = mul ulong get shape owners 3 246 + save_borrow_words borrows saved base ulong get shape borrows 247 + base = add base mul ulong get shape borrows 2 248 + save_bindings symbols saved base ulong get shape symbols 249 +end 250 + 251 +fn restore_owner_words saved view uint * owners edit uint 1280 count ulong to void 252 + for i ulong count 253 + src = mul i 3 254 + dst = mul i 5 255 + put owners dst get saved src 256 + put owners add dst 1 get saved add src 1 257 + put owners add dst 2 get saved add src 2 258 + end 259 +end 260 + 261 +fn restore_borrow_words shape view snapdim saved view uint * borrows edit uint 3072 base ulong to void 262 + count = ulong get shape borrows 263 + for i ulong count 264 + src = add base mul i 2 265 + dst = mul i 6 266 + put borrows dst get saved src 267 + put borrows add dst 3 get saved add src 1 268 + end 269 + note Preserve fresh IDs and all metadata. Only their live byte is cleared. 270 + for j ulong sub ulong get shape active count 271 + pos = mul add count j 6 272 + put borrows pos and get borrows pos `4294967040` 273 + end 274 +end 275 + 276 +fn restore_bindings symbols edit uint 19200 saved view uint * base ulong count ulong to void 277 + for j ulong count 278 + put symbols add mul j 40 24 get saved add base j 279 + end 280 +end 281 + 282 +export fn osc_snapshot_restore shape view snapdim owners edit uint 1280 borrows edit uint 3072 symbols edit uint 19200 saved view uint * to void 283 +requires le get shape owners 256 284 +requires le get shape borrows 512 285 +requires le get shape symbols 480 286 +requires ge get shape active get shape borrows 287 +requires le get shape active 512 288 + restore_owner_words saved owners ulong get shape owners 289 + base = mul ulong get shape owners 3 290 + restore_borrow_words shape saved borrows base 291 + base = add base mul ulong get shape borrows 2 292 + restore_bindings symbols saved base ulong get shape symbols 293 +end 294 + 295 +note Join the entry owners present in both falling-through paths. The saved 296 +note arm has three words per owner; the live checker has five. 297 +fn join_saved_owner saved view uint * owners edit uint 1280 i ulong to bool 298 + pos = mul i 5 299 + src = mul i 3 300 + record = get owners pos 301 + current = uchar and record 255 302 + taken = uchar and get saved src 255 303 + if eq taken current 304 + ret false 305 + end 306 + if keep_taken_line taken current 307 + put owners add pos 2 get saved add src 2 308 + end 309 + put owners pos or and record `4294967040` uint owner_maybe 310 + ret true 311 +end 312 + 313 +export fn osc_join_snapshot saved view uint * owners edit uint 1280 changes edit int * to ulong 314 +requires eq len saved mul len changes 3 315 + count = ulong 0 316 + for i ulong len changes 317 + if join_saved_owner saved owners i 318 + put changes count int i 319 + count = add count 1 320 + end 321 + end 322 + ret count 323 +end 324 + 325 +export fn osc_snapshot_moves saved view uint * owners view uint 1280 ids edit int * to ulong 326 +requires eq len saved mul len ids 3 327 + count = ulong 0 328 + for i ulong len ids 329 + before = and get saved mul i 3 255 330 + now = and get owners mul i 5 255 331 + if and ne before 1 eq now 1 332 + put ids count int i 333 + count = add count 1 334 + end 335 + end 336 + ret count 337 +end 338 + 339 +note When branches disagree about a move, the owning declaration needs a 340 +note runtime drop flag. A borrow parameter's backing object has no owner. 341 +export fn osc_join_branch_owners saved view uint * owners edit uint 1280 symbols view uint 19200 nodes edit uchar * to void 342 +requires eq rem len saved 3 0 343 +requires le len saved 768 344 +requires eq rem len nodes 104 0 345 + for id ulong div len saved 3 346 + if join_saved_owner saved owners id 347 + symbol = get owners add mul id 5 3 348 + if ne symbol `4294967295` 349 + decl = get symbols add mul ulong symbol 40 38 350 + put nodes add mul ulong decl 104 74 1 351 + end 352 + end 353 + end 354 +end om/compiler/osc_pool.om @@ -4,8 +4,23 @@ note free invalidates the live-slot bound. The generation budget still applies. 4 4 note bridge c-type poolstat OscCheckedPool 5 5 note bridge c-define OSC_POOL_FULL 1 6 6 note bridge c-define OSC_POOL_RETIRED 2 7 +note bridge c-type handles OscHandleState 8 +note bridge c-define OSC_HANDLE_UNKNOWN 0 9 +note bridge c-define OSC_HANDLE_LIVE 1 10 +note bridge c-define OSC_HANDLE_STALE 2 11 +note bridge c-define OSC_HANDLE_STALE_COPY 3 7 12 om2 8 13 14 +note Each allocation creates exactly one provenance, one model slot and one 15 +note model handle. Model IDs are one-based. Bindings have their own monotonically 16 +note assigned indices so a branch snapshot visits only actual handle bindings. 17 +layout handles 2952 18 +field count uint at 0 little 19 +field nbind uint at 4 little 20 +field freed uchar 1024 at 8 little 21 +field binding int 480 at 1032 little 22 +end 23 + 9 24 layout poolstat 32 10 25 field gbase ulong at 0 little 11 26 field dallocs ulong at 8 little @@ -61,3 +76,88 @@ export fn osc_pool_free p edit poolstat loop_depth long cond_depth long to void 61 76 end 62 77 end 63 78 end 79 + 80 +export fn osc_handle_allocate h edit handles to long 81 + p = ulong get h count 82 + if ge p 1024 83 + ret -1 84 + end 85 + put h count uint add p 1 86 + put h freed p 0 87 + ret long p 88 +end 89 + 90 +export fn osc_handle_bind h edit handles provenance long to ulong 91 +requires lt get h nbind 480 92 + b = ulong get h nbind 93 + put h nbind uint add b 1 94 + put h binding b int provenance 95 + ret b 96 +end 97 + 98 +note An unknown provenance produces no model event. A refused free retains 99 +note the first free site, including when the second access is through a copy. 100 +export fn osc_handle_visit h edit handles sites edit ulong 1024 b ulong freeing ulong symbol long line ulong to ulong 101 +requires lt b ulong get h nbind 102 + provenance = get h binding b 103 + if lt provenance 0 104 + ret 0 105 + end 106 + p = ulong provenance 107 + if ne get h freed p 0 108 + if ne shr get sites p 32 ulong symbol 109 + ret 3 110 + end 111 + ret 2 112 + end 113 + if ne freeing 0 114 + put h freed p 1 115 + put sites p or shl ulong symbol 32 ulong uint line 116 + end 117 + ret 1 118 +end 119 + 120 +export fn osc_handles_save h view handles s edit handles to void 121 + put s count get h count 122 + put s nbind get h nbind 123 + for p ulong ulong get h count 124 + put s freed p get h freed p 125 + end 126 + for b ulong ulong get h nbind 127 + put s binding b get h binding b 128 + end 129 +end 130 + 131 +note Restoring a branch retains fresh allocation and binding IDs. A loop 132 +note restores definite frees only, then settles possible binding assignments. 133 +export fn osc_handles_restore h edit handles s view handles bindings ulong to void 134 + for p ulong ulong get s count 135 + put h freed p get s freed p 136 + end 137 + if ne bindings 0 138 + for b ulong ulong get s nbind 139 + put h binding b get s binding b 140 + end 141 + end 142 +end 143 + 144 +export fn osc_handles_settle h edit handles before view handles other view handles to void 145 + for b ulong ulong get before nbind 146 + initial = get before binding b 147 + if or ne get h binding b initial ne get other binding b initial 148 + put h binding b -1 149 + end 150 + end 151 +end 152 + 153 +note Return newly definite frees in original model-event order. 154 +export fn osc_handle_frees h view handles before view handles ids edit uint 1024 to ulong 155 + count = ulong 0 156 + for p ulong ulong get before count 157 + if and ne get h freed p 0 eq get before freed p 0 158 + put ids count uint add p 1 159 + count = add count 1 160 + end 161 + end 162 + ret count 163 +end om/compiler/osc_scalar.om @@ -18,6 +18,27 @@ const BAD_OPCODE ulong 8 18 18 19 19 note Contract bindings contain known scalar arguments and the optional result. 20 20 note bridge c-type contract OscContractBindings 21 +note Function calls preserve left-to-right checking. This frame pairs parsed 22 +note arguments with parameters and records cell slices already bound at the call. 23 +note bridge c-type callargs OscCallArgs 24 +note bridge c-define OSC_ARG_SCALAR 1 25 +note bridge c-define OSC_ARG_SLICE 2 26 +note bridge c-define OSC_ARG_OWNER 3 27 +note bridge c-define OSC_ARG_BORROW 4 28 +note bridge c-define OSC_ARG_SCALAR_BORROW 5 29 +note bridge c-define OSC_ARG_SLICE_BORROW 6 30 +note bridge c-define OSC_ARG_OWNER_FORM 7 31 +note bridge c-define OSC_ARG_BORROW_FORM 8 32 +layout callargs 112 33 +field params int 6 at 0 little 34 +field args int 7 at 24 little 35 +field actions uint 6 at 52 little 36 +field cells int 6 at 76 little 37 +field np uint at 100 little 38 +field na uint at 104 little 39 +field clause uint at 108 little 40 +end 41 + 21 42 layout contract 80 22 43 field v ulong 6 at 0 little 23 44 field k uchar 6 at 48 little @@ -29,6 +50,20 @@ end 29 50 30 51 note Offsets refer to the OscNode representation asserted at its C boundary. 31 52 const ast_stride ulong 104 53 +const arg_scalar uint 1 54 +const arg_slice uint 2 55 +const arg_owner uint 3 56 +const arg_borrow uint 4 57 +const bad_scalar_borrow uint 5 58 +const bad_slice_borrow uint 6 59 +const bad_owner_form uint 7 60 +const bad_borrow_form uint 8 61 +const node_borrow ulong 20 62 +const type_ref ulong 10 63 +const type_bytes ulong 11 64 +const type_cells ulong 12 65 +const ref_own ulong 1 66 +const ref_mut ulong 3 32 67 const node_int ulong 15 33 68 const node_bool ulong 16 34 69 const node_name ulong 17 @@ -829,3 +864,220 @@ export fn osc_infer_step nodes edit uchar * stack edit ulong * frame edit infer 829 864 end 830 865 ret infer_after_second nodes frame checked 831 866 end 867 + 868 +fn arg_action nodes view uchar * param ulong arg ulong to uint 869 + typ = ast_read nodes add param 56 4 870 + kind = ast_read nodes arg 2 871 + if or eq typ type_bytes eq typ type_cells 872 + if eq kind node_borrow 873 + ret bad_slice_borrow 874 + end 875 + ret arg_slice 876 + end 877 + if ne typ type_ref 878 + if eq kind node_borrow 879 + ret bad_scalar_borrow 880 + end 881 + ret arg_scalar 882 + end 883 + refkind = ast_read nodes add param 60 4 884 + if eq refkind ref_own 885 + if ne kind node_name 886 + ret bad_owner_form 887 + end 888 + ret arg_owner 889 + end 890 + mutable = ne get nodes add arg 72 0 891 + expected_mutable = eq refkind ref_mut 892 + if or ne kind node_borrow ne mutable expected_mutable 893 + ret bad_borrow_form 894 + end 895 + ret arg_borrow 896 +end 897 + 898 +fn call_next nodes view uchar * id long to long 899 + raw = ast_read nodes add mul ulong id ast_stride 24 4 900 + if eq raw `4294967295` 901 + ret -1 902 + end 903 + ret long raw 904 +end 905 + 906 +export fn osc_call_args nodes view uchar * param long arg long call edit callargs to void 907 + np = ulong 0 908 + while and ge param 0 lt np 6 bound 6 909 + put call params np int param 910 + param = call_next nodes param 911 + np = add np 1 912 + end 913 + na = ulong 0 914 + while and ge arg 0 lt na 7 bound 7 915 + put call args na int arg 916 + arg = call_next nodes arg 917 + na = add na 1 918 + end 919 + put call np uint np 920 + put call na uint na 921 + if eq np na 922 + for k ulong na 923 + p = mul ulong get call params k ast_stride 924 + a = mul ulong get call args k ast_stride 925 + put call actions k arg_action nodes p a 926 + put call cells k int -1 927 + end 928 + end 929 +end 930 + 931 +export fn osc_bind_cell_argument call edit callargs index ulong symbol long to ulong 932 +requires lt index 6 933 + for k ulong index 934 + if eq long get call cells k symbol 935 + ret 0 936 + end 937 + end 938 + put call cells index int symbol 939 + ret 1 940 +end 941 + 942 +note Only scalar parameters bind constants. Unknown actual arguments keep 943 +note their slots unknown, including names in the caller's scope. 944 +export fn osc_call_requires nodes view uchar * call view callargs bindings edit contract stack edit ulong * to ulong 945 +requires le get call na 6 946 + request = new ulong 2 0 947 + count = ulong get call na 948 + note nk stays zero while actuals are evaluated in the caller. 949 + for i ulong count 950 + if eq get call actions i arg_scalar 951 + put request 0 ulong get call args i 952 + if ne osc_contract_value nodes bindings stack request 0 953 + put bindings k i uchar 1 954 + put bindings v i get request 1 955 + end 956 + end 957 + end 958 + put bindings nk int count 959 + put request 0 ulong get call clause 960 + if eq osc_contract_value nodes bindings stack request 0 961 + ret 1 962 + end 963 + ret alu_truth ne get request 1 0 964 +end 965 + 966 +note Native and interpreted calls use the same byte range rule. A pair of 967 +note read-only byte slices may overlap. A nonempty cell slice is exclusive. 968 +fn slice_width types view uint * p ulong to ulong 969 + base = mul p 4 970 + if ne get types add base 1 0 971 + ret 0 972 + end 973 + if ne get types add base 2 0 974 + ret 0 975 + end 976 + note Length and struct id occupy the low 24 bits of the last word. 977 + if ne and get types add base 3 0x00ffffff 0 978 + ret 0 979 + end 980 + typ = get types base 981 + if eq ulong typ type_bytes 982 + ret 1 983 + end 984 + if eq ulong typ type_cells 985 + ret 8 986 + end 987 + ret 0 988 +end 989 + 990 +fn slice_limit base ulong count ulong width ulong to ulong 991 + maxaddr = ulong 0xffffffffffffffff 992 + if eq base 0 993 + ret 0 994 + end 995 + if eq width 8 996 + if or ne and base 7 0 gt count shr maxaddr 3 997 + ret 0 998 + end 999 + end 1000 + size = mul count width 1001 + if gt size sub maxaddr base 1002 + ret 0 1003 + end 1004 + ret add base size 1005 +end 1006 + 1007 +fn slices_overlap args view ulong * p ulong q ulong limit ulong width ulong to bool 1008 + count = get args add q 1 1009 + if eq count 0 1010 + ret false 1011 + end 1012 + begin = get args q 1013 + note This earlier range passed the arithmetic checks. 1014 + ret and lt begin limit lt get args p add begin mul count width 1015 +end 1016 + 1017 +fn check_aliases types view uint * args view ulong * p ulong limit ulong to bool 1018 + for q ulong p 1019 + width = slice_width types q 1020 + if ne width 0 1021 + if or eq width 8 eq ulong get types mul p 4 type_cells 1022 + if slices_overlap args p q limit width 1023 + ret false 1024 + end 1025 + end 1026 + end 1027 + end 1028 + ret true 1029 +end 1030 + 1031 +export fn osc_slice_args types view uint * args view ulong * to long 1032 +requires eq len types mul len args 4 1033 + if lt len args 2 1034 + ret 0 1035 + end 1036 + for p ulong sub len args 1 1037 + width = slice_width types p 1038 + if eq width 0 1039 + continue 1040 + end 1041 + count = get args add p 1 1042 + if ne count 0 1043 + limit = slice_limit get args p count width 1044 + if eq limit 0 1045 + ret -1 1046 + end 1047 + if not check_aliases types args p limit 1048 + ret -1 1049 + end 1050 + end 1051 + end 1052 + ret 0 1053 +end 1054 + 1055 +note A return expression and its postcondition use different result bindings. 1056 +note Decision 0 leaves the run-time check, 1 elides it, 2 refuses the return. 1057 +export fn osc_return_ensures nodes view uchar * selectors view uint 3 bindings edit contract stack edit ulong * to ulong 1058 + returned = ulong get selectors 0 1059 + post = ulong get selectors 1 1060 + count = div len nodes ast_stride 1061 + if or ge returned count ge post count 1062 + ret 0 1063 + end 1064 + put bindings nk 0 1065 + put bindings res_sym -1 1066 + put bindings known 0 1067 + request = new ulong 2 0 1068 + put request 0 returned 1069 + if eq osc_contract_value nodes bindings stack request 0 1070 + ret 0 1071 + end 1072 + put bindings res_sym int get selectors 2 1073 + put bindings known 1 1074 + put bindings result get request 1 1075 + put request 0 post 1076 + if eq osc_contract_value nodes bindings stack request 0 1077 + ret 0 1078 + end 1079 + if eq get request 1 0 1080 + ret 2 1081 + end 1082 + ret 1 1083 +end om/compiler/osc_scope.om @@ -3,8 +3,18 @@ note Visible OSC names are indexed by spelling. Declaration IDs live for the 3 3 note whole function; leaving a block unlinks its entries in reverse order. 4 4 note The zero bucket sentinel is distinct from symbol zero. 5 5 note bridge c-type scopetab OscCheckedScope 6 +note bridge c-type declsite OscDeclSite 6 7 om2 7 8 9 +layout declsite 20 10 +field id uint at 0 little 11 +field line uint at 4 little 12 +field node uint at 8 little 13 +field loop uint at 12 little 14 +field kind uchar at 16 little 15 +field mut uchar at 17 little 16 +end 17 + 8 18 layout scopetab 3672 9 19 field head ushort 256 at 0 little 10 20 field prior ushort 480 at 512 little @@ -87,3 +97,81 @@ export fn osc_scope_pop table edit scopetab to void 87 97 put table depth uint d 88 98 put table count uint first 89 99 end 100 + 101 +note Search declaration order so a duplicate and a forward call both report 102 +note the first matching declaration. Names use the checker's 64-byte spelling. 103 +fn same_record_name records view uchar * name view uchar * base ulong to bool 104 + for j ulong len name 105 + ch = get name j 106 + if or eq ch 0 ne get records add base j ch 107 + ret false 108 + end 109 + end 110 + ret eq get records add base len name 0 111 +end 112 + 113 +note Struct, field and function records all start with a 64-byte name. 114 +note Reverse lookup preserves the semantic checker's last-match convention. 115 +export fn osc_record_name records view uchar * name view uchar * stride ulong newest ulong to long 116 +requires ge stride 64 117 +requires eq rem len records stride 0 118 + if gt len name 63 119 + ret -1 120 + end 121 + count = div len records stride 122 + for step ulong count 123 + index = step 124 + if ne newest 0 125 + index = sub sub count 1 step 126 + end 127 + if same_record_name records name mul index stride 128 + ret long index 129 + end 130 + end 131 + ret -1 132 +end 133 + 134 +export fn osc_find_function names view uchar * name view uchar * to long 135 +requires le len name 63 136 +requires eq rem len names 64 0 137 + ret osc_record_name names name 64 0 138 +end 139 + 140 +note The checker supplies the declaration site. The scoped index and symbol 141 +note record are installed together after duplicate and capacity checks. 142 +fn symbol_word symbols edit uchar 76800 pos ulong value uint to void 143 + for j ulong 4 144 + put symbols add pos j uchar and shr value mul j 8 255 145 + end 146 +end 147 + 148 +export fn osc_declare_symbol table edit scopetab symbols edit uchar 76800 name view uchar * ty view uchar 16 site view declsite to long 149 +requires le len name 63 150 + previous = osc_scope_find table symbols 160 name 151 + if ge previous 0 152 + ret previous 153 + end 154 + id = ulong get site id 155 + if ge id 480 156 + ret -2 157 + end 158 + base = mul id 160 159 + for j ulong 160 160 + put symbols add base j 0 161 + end 162 + for j ulong len name 163 + put symbols add base j get name j 164 + end 165 + for j ulong 16 166 + put symbols add add base 72 j get ty j 167 + end 168 + symbol_word symbols add base 64 get site line 169 + put symbols add base 68 get site kind 170 + put symbols add base 69 get site mut 171 + symbol_word symbols add base 88 get site loop 172 + symbol_word symbols add base 92 `4294967295` 173 + symbol_word symbols add base 96 `4294967295` 174 + symbol_word symbols add base 152 get site node 175 + osc_scope_bind table id name 176 + ret -1 177 +end om/compiler/osc_statement.om @@ -220,28 +220,104 @@ end 220 220 ret 0 221 221 end 222 222 223 -note Render a contract one spelling at a time. Parentheses suppress their 224 -note adjacent space. Refusal leaves the already formatted prefix intact. 223 +note Keep the last complete token on capacity refusal. Parentheses suppress 224 +note the space immediately after an opening or before a closing parenthesis. 225 +note The token ABI stores its offset and byte length as unsigned 32-bit fields. 226 +fn clause_spelling dst edit uchar 256 used ulong space bool source view uchar * offset_and_len ulong to ulong 227 + first = shr offset_and_len 32 228 + width = and offset_and_len 0xffffffff 229 + if or ge used len dst gt first len source 230 + ret len dst 231 + end 232 + if gt width sub len source first 233 + ret len dst 234 + end 235 + room = sub sub len dst 1 used 236 + if space 237 + if eq room 0 238 + ret len dst 239 + end 240 + room = sub room 1 241 + end 242 + if gt width room 243 + ret len dst 244 + end 245 + if space 246 + put dst used 32 247 + used = add used 1 248 + end 249 + for i ulong width 250 + put dst add used i get source add first i 251 + end 252 + used = add used width 253 + put dst used 0 254 + ret used 255 +end 256 + 225 257 export fn osc_clause_word dst edit uchar 256 used ulong prev ulong kind ulong word view uchar * to ulong 226 -space = and ne used 0 and ne prev 30 ne kind 31 227 -endpos = add used len word 228 -if space 229 - endpos = add endpos 1 258 + if ge len word len dst 259 + ret len dst 260 + end 261 + space = and ne used 0 and ne prev 30 ne kind 31 262 + ret clause_spelling dst used space word len word 263 +end 264 + 265 +fn clause_token_field tokens view uchar * pos ulong width ulong to ulong 266 + value = ulong 0 267 + for i ulong width 268 + value = or value shl ulong get tokens add pos i mul i 8 269 + end 270 + ret value 230 271 end 231 -if ge endpos len dst 232 - ret len dst 272 + 273 +fn clause_segment tokens view uchar * pos ulong to ulong 274 + offset = clause_token_field tokens add pos 12 4 275 + width = clause_token_field tokens add pos 16 4 276 + ret or shl offset 32 width 233 277 end 234 -if space 235 - put dst used 32 236 - used = add used 1 278 + 279 +export fn osc_clause_text dst edit uchar 256 tokens view uchar * source view uchar * to ulong 280 + used = ulong 0 281 + prev = ulong 0 282 + put dst 0 0 283 + if ne rem len tokens 32 0 284 + ret len dst 285 + end 286 + pos = ulong 0 287 + while lt pos len tokens 288 + kind = clause_token_field tokens pos 2 289 + used = clause_spelling dst used and ne used 0 and ne prev 30 ne kind 31 source clause_segment tokens pos 290 + if eq used len dst 291 + ret used 292 + end 293 + prev = kind 294 + pos = add pos 32 295 + end 296 + ret used 237 297 end 238 -i = ulong 0 239 -while lt i len word 240 - put dst add used i get word i 241 - i = add i 1 298 + 299 +note Each parser node starts with absent children, sibling and checker 300 +note symbols. Its source token index and coordinates survive every later pass. 301 +fn node_word node edit uchar 104 pos ulong width ulong value ulong to void 302 + for i ulong width 303 + put node add pos i uchar and shr value mul i 8 255 304 + end 242 305 end 243 -put dst endpos 0 244 -ret endpos 306 + 307 +export fn osc_new_node node out uchar 104 kind ulong token ulong line ulong col ulong to void 308 + for i ulong 104 309 + put node i 0 310 + end 311 + node_word node 0 2 kind 312 + node_word node 4 4 line 313 + node_word node 8 4 col 314 + for i ulong 16 315 + put node add 12 i 255 316 + end 317 + node_word node 28 4 token 318 + for i ulong 8 319 + put node add 88 i 255 320 + end 245 321 end 246 322 247 323 const LPAREN uint 30 om/compiler/osc_trace.om new file mode 100644 @@ -0,0 +1,88 @@ 1 +note SPDX-License-Identifier: Apache-2.0 2 +note OSC ownership trace. Object and borrow arguments are zero-based checker 3 +note IDs or -1. Region, slot and handle arguments already use model IDs. 4 +note bridge c-type trinput OscTraceInput 5 +om2 6 + 7 +layout trinput 48 8 +field op uchar at 0 little 9 +field refused uchar at 1 little 10 +field func ushort at 2 little 11 +field line uint at 4 little 12 +field kind uint at 8 little 13 +field obj int at 12 little 14 +field obj2 int at 16 little 15 +field borrow int at 20 little 16 +field via int at 24 little 17 +field region uint at 28 little 18 +field slot uint at 32 little 19 +field handle uint at 36 little 20 +field rights ulong at 40 little 21 +end 22 + 23 +const prefix ulong 8 24 +const entry_size ulong 88 25 +const capacity ulong 16384 26 + 27 +fn store_bytes trace edit uchar * pos ulong count ulong value ulong to void 28 + for j ulong count 29 + put trace add pos j uchar and shr value mul j 8 255 30 + end 31 +end 32 + 33 +fn model_id id int to ulong 34 + if lt id 0 35 + ret 0 36 + end 37 + ret add ulong id 1 38 +end 39 + 40 +fn exceeds_model event view trinput to bool 41 + if or ge get event obj 64 ge get event obj2 64 42 + ret true 43 + end 44 + if or ge get event borrow 64 ge get event via 64 45 + ret true 46 + end 47 + if or gt get event region 16 gt get event slot 16 48 + ret true 49 + end 50 + ret gt get event handle 64 51 +end 52 + 53 +export fn osc_trace_record trace edit uchar * event view trinput to void 54 +requires eq len trace add prefix mul capacity entry_size 55 + n = ulong 0 56 + for j ulong 4 57 + n = or n shl ulong get trace j mul j 8 58 + end 59 + if ge n capacity 60 + put trace 4 1 61 + ret 62 + end 63 + base = add prefix mul n entry_size 64 + store_bytes trace 0 4 add n 1 65 + put trace base get event op 66 + put trace add base 1 get event refused 67 + store_bytes trace add base 2 2 ulong get event func 68 + store_bytes trace add base 4 4 ulong get event line 69 + store_bytes trace add base 8 4 ulong get event kind 70 + store_bytes trace add base 12 4 model_id get event obj 71 + store_bytes trace add base 16 4 model_id get event obj2 72 + store_bytes trace add base 20 4 model_id get event borrow 73 + store_bytes trace add base 24 4 model_id get event via 74 + store_bytes trace add base 28 4 ulong get event region 75 + store_bytes trace add base 32 4 ulong get event slot 76 + store_bytes trace add base 36 4 ulong get event handle 77 + note This checker emits neither model cell/token fields nor generations. 78 + for unused ulong 40 79 + put trace add add base 40 unused 0 80 + end 81 + store_bytes trace add base 80 8 get event rights 82 + if exceeds_model event 83 + put trace 4 1 84 + end 85 + if ne get event refused 0 86 + put trace 5 1 87 + end 88 +end om/gpu/dsv41/README.md new file mode 100644 @@ -0,0 +1,85 @@ 1 +# DeepSeek-V4.1-Flash text decode kernels for GB10 2 + 3 +These SM121 cubins implement operator baselines for the original DeepSeek-V4.1-Flash text backbone. The architecture, formats and tensor dimensions come from revision ``314113f8a7dadb388215396ba5fbee2c610a04d6``. The model uses 40 layers, a hidden width of 5120, six routed experts per token, and a learned attention sink per head. [Published inference implementation](https://huggingface.co/deepseek-ai/DeepSeek-V4.1-Flash/tree/`314113f8a7dadb388215396ba5fbee2c610a04d6`/inference) 4 + 5 +The seeded compiler builds `emit.om` with the repository's SM121 encoder. The resulting program writes SASS instructions directly. The seeded Omega packer constructs each ELF image, including the linked toolkit note needed by the Spark driver. `lookup.om` generates the shared 272-entry floating-point decode table. Shell orchestrates the build. The independent numerical test runs through Python, NumPy and the installed CUDA Driver API. 6 + 7 +## Build and run 8 + 9 +From the repo root on AArch64 Linux, grow the compiler and build the images. 10 + 11 +```sh 12 +sh seed/omega-from-seed.sh 13 +sh om/gpu/dsv41/build.sh build/dsv41 14 +``` 15 + 16 +Inside an authorized Spark window, compare the compiled images against the numerical references. 17 + 18 +```sh 19 +spark-window run python3 om/gpu/dsv41/check.py build/dsv41 20 +``` 21 + 22 +The checks include the individual operators and a synthetic routed expert at checkpoint widths. The expert check quantizes a 5120-component activation, executes two packed FP4 projections to 2304 components, evaluates weighted SwiGLU, requantizes and projects back to 5120. Full-checkpoint logits, sustained serving throughput, multimodal inference, speculative decoding, and multi-Spark integration require separate qualification. 23 + 24 +## Launch contract 25 + 26 +Each image exports one symbol with the same name as its file stem. `kernels.list` gives parameter order and sizes. Pointers refer to contiguous row-major device data. A scalar occupies four bytes. Dimensions and indices are unsigned or signed 32-bit integers as indicated. The rotary sign is a float32 scalar. Grid dimensions omitted from the right are one. The block has shape `(threads,1,1)`. 27 + 28 +In the table, B counts token rows, H local heads, T cache positions, S selected positions, D vector width, M activation rows, N output rows and K projection width. `tiles(n)` is ceiling division by 128. All dimensions and K for selection are positive. The packer assigns 64 registers per thread and zero shared bytes. Inputs are finite except for index-selection masking and pinned candidate blocks. Four-byte packed loads require the last allocation word to be readable. Flattened element/word indices must fit in 32 bits. The Engram and vocabulary gathers form their shard-row addresses in 64 bits. 29 + 30 +| Kernel | Arguments in launch order | Grid · threads | 31 +|---|---|---| 32 +| hc_pre | residual[B,4,5120], pre[B,4], out[B,5120] | (40,B) · 128 | 33 +| hc_post | sublayer[B,5120], residual, post[B,4], comb[B,4,4], out[B,4,5120] | (40,B) · 128 | 34 +| hc_sinkhorn | raw[B,24], scales[3], bias[24], pre[B,4], post[B,4], comb[B,4,4] | (B) · 1 | 35 +| expert_act | gate[B,2304], up[B,2304], selected weight[B], out[B,2304] | (18,B) · 128 | 36 +| fp8_gemm, fp4_gemm | packed A[M,K], packed W[N,K], scales A, scales W, decode table, out[M,N], K, N | (tiles(N),M) · 128 | 37 +| sparse_scores | query[B,H,512], KV[B,T,512], indices[B,S], scores[B,H,S], H,T,S | (S,H,B) · 1 | 38 +| sparse_mix | scores[B,H,S], KV[B,T,512], indices[B,S], sink[H], out[B,H,512], H,T,S | (4,H,B) · 128 | 39 +| quant_fp8, quant_fp4, quant_kv | input, decode table, packed output, byte scales, reconstructed output | (total groups) · half the group width | 40 +| dense | activation[M,K], weights[N,K], out[M,N], K,N | (tiles(N),M) · 128 | 41 +| rms_stat | input[B,D], reciprocal RMS[B], D | (B) · 1 | 42 +| rms_apply | input[B,D], gain[D], reciprocal RMS[B], out[B,D], D | (tiles(D),B) · 128 | 43 +| rotary_tail | in/out[B,H,D], interleaved cos/sin[B,32,2], D (at least 64),H,sign | (H,B) · 32 | 44 +| compress_pair | KV[B,2,512], score[B,2,512], out[B,512] | (4,B) · 128 | 45 +| route6 | logits[B,384], selection bias[384], expert IDs[B,6], weights[B,6] | (B) · 1 | 46 +| engram_rows | absolute IDs[B], shard FP8 rows[R,256], shard byte scales[R,8], decode table, out[B,256], start ID,R | (B) · 256 | 47 +| bf16_pack, bf16_unpack | source, destination, element count | (tiles(count)) · 128 | 48 +| index_scores | query[B,H,128], key[B,T,128], prescaled BF16-rounded head weights stored as FP32[B,H], uint32 lengths[B], int32 keep mask[B,ceil(T/8)], out[B,T], H,T | (tiles(T),B) · 128 | 49 +| candidate_blocks | index scores[B,T], reachable lengths[B], out[B,ceil(T/8)], T | (ceil(T/8),B) · 1 | 50 +| top_positions | scores[B,T], indices[B,K], keep mask[B,T], T,K,index offset | (B) · 1 | 51 +| engram_gate | residual[B,4,5120], keys[B,4,5120], product of query and key weights[4,5120], int32 token mask[B], gates[B,4] | (4,B) · 1 | 52 +| engram_add | residual[B,4,5120], value[B,5120], gates[B,4], out[B,4,5120] | (40,4,B) · 128 | 53 +| token_embed | token IDs[B], shard BF16 table[R,5120], out[B,4,5120], start ID,R | (40,B) · 128 | 54 +| moe_sum | six down-projections[B,6,5120], shared output[B,5120], out[B,5120] | (40,B) · 128 | 55 +| window_step | new KV[B,512], position per row[B], in/out cache[B,128,512], indices[B,128] | (4,B) · 128 | 56 + 57 +**Precision and layout.** Unqualified floating-point buffers in the table use FP32 storage. Supply BF16 activations widened to FP32 where the checkpoint expects BF16, and round at each checkpoint dtype boundary. The pack and unpack kernels perform those conversions. FP4 uses low nibble first, E2M1. Both GEMMs consume E4M3 activations and require K divisible by 32. Activation scale shape is [M,K/32]. FP4 weight scales are [N,K/32], and FP8 weight scales are [ceil(N/32),K/32]. Both use E8M0 bytes. Supply `decode.f32le` as 272 contiguous device float32 values. The quantizers produce both packed values and widened BF16 reconstructions. FP8 and FP4 indexer groups have width 32 and a power-of-two scale. Compressed KV groups have width 16 and an E4M3 scale. Compressed-KV tests include the minimum scale, ordinary groups, halfway cases and the largest finite E4M3 scale (448). The Engram gather test exercises a seven-row local shard with a large global starting ID. 58 + 59 +**Attention and routing.** Pass one query per batch row to the sparse pair. Supply identical indices to both stages. Each index is a cache position or -1. The mixer follows the reference's 64-position online softmax and rounds each numerator weight to BF16. Apply the residual gates in the upstream layer order. Send the previous sublayer's pre coefficients to hc_pre. The router ranks biased scores, takes weights from the unbiased scores, normalizes them and applies the checkpoint's factor of 1.5. It resolves ties by lower expert ID. top_positions returns ascending positions, fills unreachable entries with -1, and gives a 32-bit keep mask. It also resolves score ties by lower index. Supply K at most T and an offset that fits signed int32. For two-level selection, run candidate_blocks, top_positions on the block scores, index_scores with the resulting mask, then top_positions on the filtered scores. 60 + 61 +**Integration.** The collection operates on caller-owned buffers. A model executor must load and shard weights and Engram tables, construct hash IDs and rotary frequencies, schedule the 40 layers and BF16 boundaries, maintain cache state, and perform rank collectives. The CUDA Driver API is used for this hardware check. The final-head executor below uses the native Omega submission path. 62 + 63 +## Native final-head executor 64 + 65 +The build also produces `dsv41-head`, a seeded AArch64 Linux executable for the original checkpoint's final text head. It opens the installed NVIDIA kernel devices directly, creates an RM/UVM context, and submits Omega-emitted SM121 instructions through QMDs. It loads `head.weight` and `norm.weight` from shard 43 once and keeps them resident across input frames. The head matrix occupies 1,323,827,200 bytes in BF16. The binary consumes the final decoder layer's residual streams and pre-mix coefficients. 66 + 67 +For a reproducible researcher run, obtain the head shard from the pinned release and create four input frames. The fixture command fetches 80 KiB of embedding rows 1234 through 1241 and verifies their SHA-256 before writing a new file. Two frames use those rows. A third exercises FP32 product cancellation at the BF16 pre-mix boundary. The fourth covers FP32 overflow when squaring a finite BF16 residual during normalization. 68 + 69 +```sh 70 +curl -fL 'https://huggingface.co/deepseek-ai/DeepSeek-V4.1-Flash/resolve/`314113f8a7dadb388215396ba5fbee2c610a04d6`/model-00043-of-00048.safetensors' -o model-00043-of-00048.safetensors 71 +python3 om/gpu/dsv41/check-head.py --fixture frames.bin 72 +``` 73 + 74 +From the repository root, in a single-Spark correctness window: 75 + 76 +```sh 77 +spark-window run env --ignore-signal=PIPE build/dsv41/dsv41-head model-00043-of-00048.safetensors < frames.bin > logits.f32 78 +python3 om/gpu/dsv41/check-head.py model-00043-of-00048.safetensors frames.bin logits.f32 79 +``` 80 + 81 +A frame is 40,976 bytes. First write four row-major 5120-element residual streams as little-endian BF16, then four little-endian FP32 mixing coefficients. Each complete frame produces 129,280 little-endian FP32 logits in token-ID order (517,120 bytes). EOF between frames ends the session. After emitting one million frames the process closes its resources and exits 75. The caller may open a new session for subsequent frames. A partial frame exits 65. Exit 74 reports a native resource, submission, completion, or IO error. The example inherits an ignored SIGPIPE so a closed output pipe returns 74 through explicit device cleanup. Direct invocation with the default POSIX signal disposition terminates on SIGPIPE when a reader disconnects. Keep the checkpoint immutable for the lifetime of the process. 82 + 83 +Every frame goes through BF16 widening, the four-stream pre-mix, BF16 rounding, RMS normalization, weighted scaling, a second BF16 rounding, and the packed vocabulary projection. The projection uses one warp per vocabulary row and accumulates in FP32. The earlier 40 decoder layers supply the frame in a complete inference graph. The check accepts independent test frames and compares every vocabulary row with a CPU calculation using the released weights. 84 + 85 +The original head shard is 1,323,837,624 bytes with SHA-256 ``d762b688f138e00a24eac96f27b842715bb4b534ecc9ca39076bed2b8c33201e``. The loader validates the two tensor descriptors, dtype, shapes, payload coverage and non-overlap before opening a device. The numerical check additionally verifies the published file checksum. [Pinned checkpoint shard](https://huggingface.co/deepseek-ai/DeepSeek-V4.1-Flash/blob/`314113f8a7dadb388215396ba5fbee2c610a04d6`/model-00043-of-00048.safetensors) om/gpu/dsv41/build.sh new file mode 100644 @@ -0,0 +1,34 @@ 1 +#!/bin/sh 2 +# Run from the repository root after growing seed/out/omc. 3 +set -eu 4 +out=${1:?usage: sh om/gpu/dsv41/build.sh OUTPUT_DIR} 5 +mkdir -p "$out" 6 +tmp=$(mktemp -d "$out/build.XXXXXX") 7 +trap 'rm -rf "$tmp"' EXIT 8 +( echo om2; cat om/encoder/bw_enc_sm121.om om/gpu/dsv41/emit.om om/gpu/dsv41/emit-cli.om ) > "$tmp/input.om" 9 +seed/out/omc < "$tmp/input.om" > "$tmp/emit" 10 +seed/out/omc < seed/kflow/kpack.om > "$tmp/kpack" 11 +seed/out/omc < om/gpu/dsv41/lookup.om > "$tmp/lookup" 12 +chmod +x "$tmp/emit" "$tmp/kpack" "$tmp/lookup" 13 +"$tmp/lookup" > "$tmp/decode.f32le" 14 +while read -r mode name abi; do 15 + "$tmp/emit" "$mode" > "$tmp/$name.bin" 16 + "$tmp/kpack" 121-tk "$name" "$tmp/$name.bin" "$tmp/$name.cubin" 64 0 $abi 17 +done < om/gpu/dsv41/kernels.list 18 +( echo om2 19 + cat seed/json/json.om gpu/nvidia/native/nvrm/context.om \ 20 + gpu/nvidia/native/blackwell/vector.om gpu/nvidia/native/blackwell/launch.om \ 21 + gpu/nvidia/native/blackwell/batch.om om/encoder/bw_enc_sm121.om \ 22 + om/gpu/dsv41/emit.om om/gpu/dsv41/head/weights.om om/gpu/dsv41/head/launch.om om/gpu/dsv41/head/session.om 23 +) > "$tmp/head.om" 24 +seed/out/omc < "$tmp/head.om" > "$tmp/dsv41-head" 25 +chmod +x "$tmp/dsv41-head" 26 +for src in om/gpu/dsv41/mma/*.om; do 27 + name=$(basename "$src" .om) 28 + seed/out/omc < "$src" > "$tmp/$name" 29 + chmod +x "$tmp/$name" 30 + "$tmp/$name" > "$tmp/$name.bin" 31 + "$tmp/kpack" 121-tk "$name" "$tmp/$name.bin" "$tmp/$name.cubin" 64 0 p8 p8 p8 p8 p8 4 32 +done 33 +mv "$tmp/dsv41-head" "$out" 34 +mv "$tmp"/*.cubin "$tmp/decode.f32le" "$out" om/gpu/dsv41/check-head.py new file mode 100644 @@ -0,0 +1,101 @@ 1 +"""Compare every vocabulary logit against an independent CPU head using the pinned BF16 shard.""" 2 +import hashlib 3 +import json 4 +import struct 5 +import sys 6 +from urllib.request import Request, urlopen 7 +from pathlib import Path 8 + 9 +import numpy as np 10 + 11 + 12 +def widened(words): 13 + return (words.astype(np.uint32) << 16).view(np.float32) 14 + 15 + 16 +def rounded(x): 17 + bits = np.ascontiguousarray(x, dtype="> 16) & 1)) & 0xffff0000).view(np.float32) 19 + 20 + 21 +def load_tensors(path): 22 + expected = "`d762b688f138e00a24eac96f27b842715bb4b534ecc9ca39076bed2b8c33201e`" 23 + with path.open("rb") as f: 24 + if hashlib.file_digest(f, "sha256").hexdigest() != expected: 25 + raise ValueError("checkpoint shard SHA-256 mismatch") 26 + f.seek(0) 27 + n, = struct.unpack("