diff --git a/.crumb b/.crumb index f3e51fe46..1f61664d4 100644 --- a/.crumb +++ b/.crumb @@ -72,7 +72,7 @@ }, { "digest": "f765ad87fdddd9b47f690b07298c951a0f7e7675dc5de4ffe44e33a0c14b1986", - "last_commit": "a8a6a3d3c", + "last_commit": "d94a503f7", "name": "spec", "purpose": "Directory spec. Purpose not yet described by a human." }, @@ -84,26 +84,26 @@ }, { "digest": "5395ff8ff9afceebe8e453788f80a3064b831513cc4cb92fd55869115e06b874", - "last_commit": "a8a6a3d3c", + "last_commit": "dcccc0211", "name": "tests", "purpose": "Directory tests. Purpose not yet described by a human." }, { "digest": "308db0e6cf45c0a48f01f5f5f228450f191389de68006915b9ecf9c65b9a2e29", - "last_commit": "a8a6a3d3c", + "last_commit": "a50c3bd41", "name": "tools", "purpose": "Directory tools. Purpose not yet described by a human." } ], "children_root": "sha256:07bf1706dcffbad66c0353871ff67a6749055da03cee8d8ebdf9a4c91e36bddc", - "compiled_at": "2026-10-30T12:31:50Z", + "compiled_at": "2026-10-30T16:28:41Z", "compiler": "crumb-compile/1", - "digest": "sha256:904fca8c0e0cf3e3a5e5adb73393e01c7018b920bb542d289904240f70e400d6", + "digest": "sha256:e3fb16f9efd52ccc578e171d3fb5a6c52dfc1beda79e7d9e7a9dbd0187b8eaf9", "evidence_files": 12622, "evidence_root": "sha256:b1a08e2ce56e6feada0d8a31f06fbd7adee57adc1c1ad8dec220293ee5d439f9", - "files": 17849, - "generated_at_commit": "52a82f392fa513ebbc2d9d719a0f14c385289b06", - "source_tree": "sha256:75458da6553a0ee5ea2af95f46afb658e8a3f6e53cca6e3b4b9874035f78c2ed" + "files": 17868, + "generated_at_commit": "96d83eb3c457d90ef286edd79bb432b177f98348", + "source_tree": "sha256:f194c18be750c387d0f4bead802b8048c6d515df2de6cd8e67611c10f53c34e1" }, "owner_lane": "", "provenance": { diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index dda6d20b5..267a1cd19 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -35,6 +35,7 @@ Every model below did real work. A model is listed only for work it did. The orc | Claude Opus 5.5 | Omega 1.0 roadmap: source-pinned readiness audit of the seeded compiler, IR, WebAssembly and bridge paths, milestone acceptance gates, tracking issues #790-#800, OM-2 contract files brought to main byte-identical, and stale status text corrected | | Claude Opus 5.5 | Function-mode constants in the seeded compiler (`fnstore`): consts accepted without `fn main`, globals/texts/host words still error 9, gate16 byte-identity fixtures and old-compiler/mutant negative controls | | Codex (GPT-6.1 Sol) | Repaired qty float-domain classification and checked ingress, diagnostic categories and query/output streaming; permanent adversarial regression fixtures for the #909/#911 audit | +| Claude Opus 5.5 | Tenstorrent Tensix vecadd (`seed/tensix`): RV32IM asm2 table, hand-written kernel and start code, seeded Omega emitter `irrv32.om` (OSC IR v1 scalar unit -> RV32 elementwise kernel, byte-identical to asm2), hand-written edge disassembly, ttsim WH/BH and qemu interop-oracle gate with negative controls | This table starts with the numeric work on this branch. Earlier work in this repository is not yet attributed here; add rows only for work that can be traced to a model. diff --git a/seed/tensix/.crumb b/seed/tensix/.crumb new file mode 100644 index 000000000..ce8d649dc --- /dev/null +++ b/seed/tensix/.crumb @@ -0,0 +1,20 @@ +{ + "extensions": { + "generated": { + "children": [], + "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", + "compiled_at": "2026-10-30T02:52:07Z", + "compiler": "crumb-compile/1", + "digest": "sha256:470bfa499639f9ec5724dc0a9f23b0e5dcc00c2bb4572ba2586d522dfecf58d5", + "evidence_files": 0, + "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", + "files": 19, + "generated_at_commit": "d3a08b533f1057b26a7dc32aee5594c5b6fca40f", + "source_tree": "sha256:e75194da94e193067a2f00aad9e47ce19af72a88e487a36eae040c2dda64dfda" + } + }, + "layer": "seed", + "name": "tensix", + "purpose": "Tenstorrent Tensix RV32IM vecadd: seeded Omega emitter irrv32.om (OSC IR v1 -> RV32) and asm2 table, interop-oracle gate (ttsim WH/BH, qemu, two disassemblers)", + "schema_version": "1.0.0" +} diff --git a/seed/tensix/README.md b/seed/tensix/README.md new file mode 100644 index 000000000..45092b270 --- /dev/null +++ b/seed/tensix/README.md @@ -0,0 +1,138 @@ +# seed/tensix: vecadd for the Tenstorrent Tensix RISC-V cores + +Each Tensix tile on Tenstorrent Wormhole (n150, n300) and Blackhole (p100, p150) cards contains five +small RISC-V cores: RISCV B, NC, T0, T1 and T2. This directory writes a 32-bit integer vector add for +these cores, `out[i] = a[i] + b[i]` modulo 2^32, and checks the result with outside tools. + +There are two producers of the kernel words, and the gate requires them to agree byte for byte. + +1. **Seeded Omega (`irrv32.om`).** `seed/out/omc` builds `irrv32.om` into an AArch64 program. That program reads the OSC IR v1 of a scalar unit (`fn addu a uint b uint to uint`, in `vecadd.unit.om`) and lifts it into the elementwise loop `out[i] = f(a[i], b[i])`. The words are built from instruction fields inside Omega. +2. **Rung 6, as in `seed/sm121`.** The unchanged rung-2 assembler `seed/rung2/asm2.bin` reads a plain-text instruction table, `rv32im.tbl` (all 48 RV32I + M instructions), and hand-written listings. + +No C, Rust or Python program produces any instruction word. + +```sh +seed/out/omc < seed/tensix/irrv32.om > irrv32 && chmod +x irrv32 +{ printf 'ir\n'; cat seed/tensix/vecadd.unit.om; } | seed/out/omc | ./irrv32 > vecadd.bin # 44 bytes +``` + +`irrv32` accepts exactly one function that takes two `uint` parameters and returns `uint`. The function must be a single block that uses only CONST (emitted as lui+addi), MOV (addi), BIN add/sub/mul/and/or/xor, and RET. It may use at most 7 vregs, which map to x5-x7 and then x14-x17. `div`, `rem`, `shl` and `shr` are refused, because Omega traps on a zero divisor or an out-of-range shift and this kernel has no trap path. Other types, control flow and calls are also refused. Every refusal sets a nonzero exit code (20-28) and writes no bytes. + +| File | What it is | +|---|---| +| `rv32im.tbl` | RV32IM encodings as asm2 table lines. Registers are written as numbers 0-31, because asm2's register parser uses ARM names. | +| `irrv32.om` | Seeded Omega emitter: OSC IR v1 scalar uint unit → RV32IM elementwise kernel. | +| `vecadd.unit.om` | The scalar unit `ret add a b`. Its emitted kernel must equal `vecadd.bin`. | +| `mix.unit.om`, `mix.expect`, `mix-expect.hex` | A second unit, `((a+b)*3000000000) xor a`, which exercises CONST, MOV, mul and xor. It comes with its hand-written disassembly and 64 expected words. | +| `vecadd.s2` | The kernel: 11 instructions, 44 bytes. a0 = a, a1 = b, a2 = out, a3 = n; it returns through ra. | +| `vecadd.bin` | The committed kernel bytes. The gate rebuilds them twice and requires the same bytes both times. | +| `boot-tt.s2` | 9 instructions placed at L1 0x0, where RISCV B starts after reset. It loads the arguments from 0x1000, calls vecadd, then writes the done mark 0x0600d000 to 0x1010 and spins. | +| `boot-qemu.s2` | Linux user-mode start code for qemu. It calls vecadd, then makes the `write(1, out, 4n)` and `exit(0)` system calls. | +| `edges.s2`, `edges.expect` | Every table entry once, with fields at their edge values. `edges.expect` is the disassembly written by hand before any disassembler was run. | +| `vecadd.expect` | The expected disassembly of the kernel. | +| `a.hex`, `b.hex`, `expect.hex` | 64 little-endian words each. The first six are hand-picked edge cases; 61 of the 64 sums wrap. | +| `oracle.py` | The oracle driver: interop oracle, not Omega-native, and it never generates kernels. It loads ttsim and talks to it over PCIe the way a host driver would, or wraps an image in an ELF file for qemu. | +| `gate-tensix.sh` | The gate script. | + +Expected values were computed in Wolfram with this query: +`WolframLanguageEvaluator: a=Join[{0,2^32-1,2^31,2^31-1,2^32-1,16^^12345678}, Table[Mod[2654435761(i+1),2^32],{i,6,63}]]; b=Join[{0,1,2^31,1,2^32-1,16^^87654321}, Table[Mod[2^32-1-40503 i^2+977 i,2^32],{i,6,63}]]; Mod[a+b,2^32]`. +It reported 64 words, of which 61 have a+b ≥ 2^32. +`mix-expect.hex` uses the same `a` and `b` with `BitXor[Mod[Mod[a+b,2^32]*3000000000,2^32],a]`. The CONST split for 3000000000 is lui 0xb2d06, addi -512, also from Wolfram. + +## Run + +```sh +mkdir -p build/oracle/ttsim-v1.15.0 && cd build/oracle/ttsim-v1.15.0 +for a in wh bh; do curl -LO https://github.com/tenstorrent/ttsim/releases/download/v1.15.0/libttsim_${a}_aarch64.so; done +sha256sum libttsim_* # the gate refuses anything but the pinned sha256 values below +cd - && nice -n 19 sh seed/tensix/gate-tensix.sh # 0 PASS, 1 FAIL, 3 NOT_RUN +``` + +The host must be AArch64 Linux, because asm2.bin is an AArch64 program. The gate also needs: + +- `riscv64-unknown-elf-objdump` and `riscv64-unknown-elf-objcopy`; +- `llvm-objdump` (version 18); +- `qemu-riscv32`; +- `python3`. + +If any of these, or a ttsim library, is missing, the gate prints NOT_RUN and exits with code 3. + +## What the gate checks + +All of these checks are interop oracles, not Omega-native. + +1. **Same bytes every build.** Each image is assembled twice and both builds must match byte for byte. The kernel must also equal the committed `vecadd.bin`, and both start images must end with exactly those bytes. omc (pinned sha256) builds `irrv32.om` twice; both builds must be identical. `irrv32` emits each unit twice. Its vecadd output must be byte-identical to asm2's `vecadd.bin`. GNU objdump must print exactly `mix.expect` for the mix kernel. +2. **Two independent disassemblers.** + - GNU objdump must print exactly `vecadd.expect` and `edges.expect`. These cover all 48 table names: 51 words in total. + - LLVM objdump must agree with GNU on every mnemonic and register. +3. **Three runs that compare results.** The kernel runs on: + - ttsim Wormhole (Tensix tile (1,1)); + - ttsim Blackhole (Tensix tile (1,2)); + - qemu-riscv32. + + Each run must give all 64 sums equal to `expect.hex`. On ttsim, the output buffer is first filled with 0xee bytes, so a kernel that never ran cannot pass. The Omega-emitted vecadd and mix kernels then run on the same three oracles, behind the same start code, against `expect.hex` and `mix-expect.hex`. +4. **Negative controls.** Every one of these must be caught: + - C1: one bit flipped in the table (add becomes sub). Caught by GNU objdump and by ttsim. + - C2: one wrong expected value. + - C3: the wrong register in the listing. Caught by qemu. + - C4: one wrong branch-offset bit. ttsim stops with "invalid pc". + - C5: an out-of-range immediate. asm2 refuses it. + - C6: the unit mutated from add to sub. The emitted bytes change, and ttsim's output differs. + - C7: the emitter mutated (a0 stride 8). Caught by qemu. + - C8: a `div` unit. `irrv32` refuses it with exit 24 and emits 0 bytes. + +## What is established, and what is NOT + +**Established (EXERCISED on oracles, 2026-10-26):** + +- The table encodes RV32IM the way two independent disassemblers read it. +- The kernel computes the right sums on ttsim's models of Wormhole B0 and Blackhole A0 RISCV B, and on qemu. +- Seeded Omega (`irrv32.om`) emits the same vecadd bytes from the scalar unit's OSC IR, and a second kernel (mix) that is correct on the same three oracles. + +**NOT established:** + +- **Nothing has run on a Tenstorrent card** (NOT_RUN on hardware). +- ttsim is Tenstorrent's software model, not silicon. +- The fp32 SFPU path and the Tensix coprocessor (matrix unit) are not used. +- `irrv32.om` covers straight-line u32 scalar bodies only. Signed types, traps, control flow, loops inside the body and Tensix coprocessor instructions are not lowered. +- Only RISCV B is used, on a single tile. +- There is no NoC traffic, no DRAM, and no tt-metal runtime. + +## What is open (sources read 2026-10-26) + +- **[tt-isa-documentation](https://github.com/tenstorrent/tt-isa-documentation)** at be833101 (code Apache-2.0, images CC BY-ND 4.0; `LICENSE_understanding.txt` notes that patent rights may be needed). It documents: + - the baby RISC-V cores. Wormhole is RV32IM. Blackhole adds Zicsr, Zaamo, Zba, Zbb, part of F/Zfh, and `.ttinsn`; T2 also has V. + - L1, soft reset, the PCIe TLB windows and NoC coordinates; + - about 120 Tensix coprocessor instructions, each with a bit-encoding diagram and a functional model (for example `TensixCoprocessor/MVMUL.md`). + + Coprocessor instructions are pushed by RISC-V `sw` (`BabyRISCV/PushTensixInstruction.md`). So the RISC-V level, the coprocessor instruction level, and the host-to-tile path are all documented well enough for Omega to emit code without closed tools. +- **[ttsim](https://github.com/tenstorrent/ttsim)** (Apache-2.0), source at b3620244. [Release v1.15.0](https://github.com/tenstorrent/ttsim/releases/tag/v1.15.0) was published 2026-10-25. Pinned libraries: + - `libttsim_wh_aarch64.so`: `e2a56ffe04072a10d477db1ef85cdbd5d392d9fc52043bb949b90a4c01ff6b4f`; + - `libttsim_bh_aarch64.so`: `2367dccc964c0ce4a20b14520e5f7ac6191bb227fb20e87aca1e94bf7432ced2`. +- **[tt-metal](https://github.com/tenstorrent/tt-metal)**, **[tt-umd](https://github.com/tenstorrent/tt-umd)**, **[sfpi](https://github.com/tenstorrent/sfpi)** (GCC-based SFPU compiler) and **[tt-llk](https://github.com/tenstorrent/tt-llk)** are Apache-2.0. **[tt-kmd](https://github.com/tenstorrent/tt-kmd)** (the kernel driver) is GPL-2.0. Licences were read from the GitHub API on 2026-10-26. None of these is used here. +- **RISC-V Unprivileged ISA 20191213**, chapters 2, 7 and 24, for the instruction formats. + +## Before a hardware receipt + +1. **A card.** An n150 or p150 card, or Tenstorrent cloud access. We have neither. +2. **The kernel driver.** tt-kmd (GPL-2.0) must be loaded to map BAR0. The same TLB and soft-reset sequence that `oracle.py` uses on ttsim then works on the card. tt-umd does this from C++. +3. **Firmware and tile choice.** On real cards the ARC firmware and the tt-metal firmware own the tiles. A raw run must use a tile that nothing else is using and must restore soft reset afterwards. Harvested rows and columns change which NoC coordinates are Tensix tiles. +4. **Running beyond one tile.** NoC reads and writes are needed for DRAM inputs and for using many tiles. +5. **What the receipt must contain.** + - the card model and serial; + - the firmware version; + - the tt-kmd version; + - the image sha256; + - the tile coordinates; + - the output compared with `expect.hex`; + - the same negative controls, run on the card. + +## Host dependencies (oracles only) + +| Dependency | Version | Role | Omega replacement | +|---|---|---|---| +| GNU objdump | binutils 2.42 | disassembler | none needed (read-only oracle) | +| LLVM objdump | 18.1.3 | second disassembler | none needed (read-only oracle) | +| ttsim | v1.15.0 | Tensix simulator | none needed (vendor oracle) | +| qemu-riscv32 | 8.2.2 | RV32 executor | none needed (read-only oracle) | +| Python | 3.12.3 | `oracle.py` byte transport | an `.om` host loader (dlopen/FFI word plus ELF byte writer; proposal) | diff --git a/seed/tensix/a.hex b/seed/tensix/a.hex new file mode 100644 index 000000000..8155e3d73 --- /dev/null +++ b/seed/tensix/a.hex @@ -0,0 +1,64 @@ +00000000 +ffffffff +00000080 +ffffff7f +ffffffff +78563412 +d7538453 +88cdbbf1 +3947f38f +eac02a2e +9b3a62cc +4cb4996a +fd2dd108 +aea708a7 +5f214045 +109b77e3 +c114af81 +728ee61f +23081ebe +d481555c +85fb8cfa +3675c498 +e7eefb36 +986833d5 +49e26a73 +fa5ba211 +abd5d9af +5c4f114e +0dc948ec +be42808a +6fbcb728 +2036efc6 +d1af2665 +82295e03 +33a395a1 +e41ccd3f +959604de +46103c7c +f789731a +a803abb8 +597de256 +0af719f5 +bb705193 +6cea8831 +1d64c0cf +ceddf76d +7f572f0c +30d166aa +e14a9e48 +92c4d5e6 +433e0d85 +f4b74423 +a5317cc1 +56abb35f +0725ebfd +b89e229c +69185a3a +1a9291d8 +cb0bc976 +7c850015 +2dff37b3 +de786f51 +8ff2a6ef +406cde8d diff --git a/seed/tensix/b.hex b/seed/tensix/b.hex new file mode 100644 index 000000000..735977899 --- /dev/null +++ b/seed/tensix/b.hex @@ -0,0 +1,64 @@ +00000000 +01000000 +00000080 +01000000 +ffffffff +21436587 +29d7e9ff +2fd2e1ff +c790d8ff +f112ceff +ad58c2ff +fb61b5ff +db2ea7ff +4dbf97ff +511387ff +e72a75ff +0f0662ff +c9a44dff +150738ff +f32c21ff +631609ff +65c3effe +f933d5fe +1f68b9fe +d75f9cfe +211b7efe +fd995efe +6bdc3dfe +6be21bfe +fdabf8fd +2139d4fd +d789aefd +1f9e87fd +f9755ffd +651136fd +63700bfd +f392dffc +1579b2fc +c92284fc +0f9054fc +e7c023fc +51b5f1fb +4d6dbefb +dbe889fb +fb2754fb +ad2a1dfb +f1f0e4fa +c77aabfa +2fc870fa +29d934fa +b5adf7f9 +d345b9f9 +83a179f9 +c5c038f9 +99a3f6f8 +ff49b3f8 +f7b36ef8 +81e128f8 +9dd2e1f7 +4b8799f7 +8bff4ff7 +5d3b05f7 +c13ab9f6 +b7fd6bf6 diff --git a/seed/tensix/boot-qemu.s2 b/seed/tensix/boot-qemu.s2 new file mode 100644 index 000000000..c80ecfb4f --- /dev/null +++ b/seed/tensix/boot-qemu.s2 @@ -0,0 +1,20 @@ +; boot-qemu.s2 -- start-up words for the second oracle, qemu-riscv32 in Linux user mode. Same job as +; boot-tt.s2, different world: the image is loaded at 0x10000 (code at 0x10100), the argument block is at +; 0x11000 (+0 a, +4 b, +8 out, +12 n words), and the result leaves through Linux system calls: +; write(1, out, 4n) (a7 = 64) then exit(0) (a7 = 93), RISC-V Linux syscall numbers. +; 15 instructions (60 bytes); vecadd follows at byte 60. + lui 28 0x11 ; t3 = 0x11000, the argument block + lw 10 28 0 ; a0 = a + lw 11 28 4 ; a1 = b + lw 12 28 8 ; a2 = out + lw 13 28 12 ; a3 = n + jal 1 0 20 0 0 ; ra = next, call vecadd: +40 bytes (imm[10:1] = 20), 10 instructions ahead + addi 10 0 1 ; a0 = 1 (stdout) + lw 11 28 8 ; a1 = out + lw 12 28 12 ; a2 = n + slli 12 12 2 ; a2 = 4n bytes + addi 17 0 64 ; a7 = write + ecall + addi 10 0 0 ; a0 = 0 + addi 17 0 93 ; a7 = exit + ecall diff --git a/seed/tensix/boot-tt.s2 b/seed/tensix/boot-tt.s2 new file mode 100644 index 000000000..3110e8baf --- /dev/null +++ b/seed/tensix/boot-tt.s2 @@ -0,0 +1,16 @@ +; boot-tt.s2 -- start-up words for RISCV B of one Tensix tile (Wormhole B0 / Blackhole A0), placed at L1 +; address 0x0000, the reset pc of RISCV B (tt-isa-documentation */TensixTile/SoftReset.md). The host +; writes this stub followed by vecadd.s2 to L1, writes the argument block, then releases RISCV B from +; soft reset. No tt-metal firmware is involved: this is the bare-metal launch the firmware would do. +; Argument block at L1 0x1000: +0 a, +4 b, +8 out, +12 n words, +16 done word. +; When vecadd returns, the stub writes 0x0600d000 to the done word and spins on itself. +; 9 instructions (36 bytes); vecadd follows at byte 36. + lui 28 1 ; t3 = 0x1000, the argument block + lw 10 28 0 ; a0 = a + lw 11 28 4 ; a1 = b + lw 12 28 8 ; a2 = out + lw 13 28 12 ; a3 = n + jal 1 0 8 0 0 ; ra = next, call vecadd: +16 bytes (imm[10:1] = 8), 4 instructions ahead + lui 29 0x600d ; t4 = 0x0600d000, the done mark + sw 29 28 0 16 ; done word = t4 + jal 0 0 0 0 0 ; spin: jump to itself diff --git a/seed/tensix/edges.expect b/seed/tensix/edges.expect new file mode 100644 index 000000000..83abb9a7f --- /dev/null +++ b/seed/tensix/edges.expect @@ -0,0 +1,51 @@ +lui x31,0xfffff +auipc x1,0x80000 +jal x0,0x100006 +jal x30,0xa +jalr x5,-2048(x6) +jalr x0,2047(x1) +beq x1,x2,0x1016 +bne x31,x30,0x1a +blt x3,x4,0x28 +bge x5,x6,0x4 +bltu x7,x8,0x48 +bgeu x9,x10,0x82c +lb x11,-1(x12) +lh x13,2047(x14) +lw x15,-2048(x16) +lbu x17,1(x18) +lhu x19,0(x20) +sb x21,-2017(x22) +sh x23,2047(x24) +sw x25,-1(x26) +addi x27,x28,-2048 +slti x29,x30,2047 +sltiu x31,x0,-1 +xori x1,x2,-1 +ori x3,x4,1365 +andi x5,x6,255 +slli x7,x8,0x1f +srli x9,x10,0x1 +srai x11,x12,0x1f +add x31,x30,x29 +sub x1,x2,x3 +sll x4,x5,x6 +slt x7,x8,x9 +sltu x10,x11,x12 +xor x13,x14,x15 +srl x16,x17,x18 +sra x19,x20,x21 +or x22,x23,x24 +and x25,x26,x27 +fence iorw,iorw +fence r,w +ecall +ebreak +mul x28,x29,x30 +mulh x31,x1,x2 +mulhsu x3,x4,x5 +mulhu x6,x7,x8 +div x9,x10,x11 +divu x12,x13,x14 +rem x15,x16,x17 +remu x18,x19,x20 diff --git a/seed/tensix/edges.s2 b/seed/tensix/edges.s2 new file mode 100644 index 000000000..669ee2eb4 --- /dev/null +++ b/seed/tensix/edges.s2 @@ -0,0 +1,55 @@ +; edges.s2 -- every line of rv32im.tbl once, with registers and numbers at the edges of their fields. +; edges.expect holds, written by hand before any disassembler was run on it, what GNU objdump +; (-M no-aliases,numeric) must print for each word. Branch and jump targets are absolute (pc + offset), +; pc = 4 x line number counting from 0. + lui 31 0xfffff + auipc 1 0x80000 + jal 0 0 1023 1 255 + jal 30 -1 1023 1 255 + jalr 5 6 -2048 + jalr 0 1 2047 + beq 1 2 0 63 15 1 + bne 31 30 -1 63 15 1 + blt 3 4 0 0 4 0 + bge 5 6 -1 63 0 1 + bltu 7 8 0 1 0 0 + bgeu 9 10 0 0 0 1 + lb 11 12 -1 + lh 13 14 2047 + lw 15 16 -2048 + lbu 17 18 1 + lhu 19 20 0 + sb 21 22 -64 31 + sh 23 24 63 31 + sw 25 26 -1 31 + addi 27 28 -2048 + slti 29 30 2047 + sltiu 31 0 -1 + xori 1 2 -1 + ori 3 4 1365 + andi 5 6 255 + slli 7 8 31 + srli 9 10 1 + srai 11 12 31 + add 31 30 29 + sub 1 2 3 + sll 4 5 6 + slt 7 8 9 + sltu 10 11 12 + xor 13 14 15 + srl 16 17 18 + sra 19 20 21 + or 22 23 24 + and 25 26 27 + fence 15 15 + fence 2 1 + ecall + ebreak + mul 28 29 30 + mulh 31 1 2 + mulhsu 3 4 5 + mulhu 6 7 8 + div 9 10 11 + divu 12 13 14 + rem 15 16 17 + remu 18 19 20 diff --git a/seed/tensix/expect.hex b/seed/tensix/expect.hex new file mode 100644 index 000000000..e41a18bdc --- /dev/null +++ b/seed/tensix/expect.hex @@ -0,0 +1,64 @@ +00000000 +00000000 +00000000 +00000080 +feffffff +99999999 +002b6e53 +b79f9df1 +00d8cb8f +dbd3f82d +489324cc +47164f6a +d85c7808 +fb66a0a6 +b034c744 +f7c5ece2 +d01a1181 +3b33341f +380f56bd +c7ae765b +e81196f9 +9b38b497 +e022d135 +b7d0ecd3 +20420772 +1b772010 +a86f38ae +c72b4f4c +78ab64ea +bbee7888 +90f58b26 +f7bf9dc4 +f04dae62 +7b9fbd00 +98b4cb9e +478dd83c +8829e4da +5b89ee78 +c0acf716 +b793ffb4 +403e0653 +5bac0bf1 +08de0f8f +47d3122d +188c14cb +7b081569 +70481407 +f74b12a5 +10130f43 +bb9d0ae1 +f8eb047f +c7fdfd1c +28d3f5ba +1b6cec58 +a0c8e1f6 +b7e8d594 +60ccc832 +9b73bad0 +68deaa6e +c70c9a0c +b8fe87aa +3bb47448 +502d60e6 +f7694a84 diff --git a/seed/tensix/gate-tensix.sh b/seed/tensix/gate-tensix.sh new file mode 100755 index 000000000..1db0c764f --- /dev/null +++ b/seed/tensix/gate-tensix.sh @@ -0,0 +1,336 @@ +#!/bin/sh +# Host-only check of the Tenstorrent Tensix RISC-V vecadd (seed/tensix). Two producers of the +# same RV32IM kernel: (1) the unchanged rung-2 assembler seed/rung2/asm2.bin with the plain-text +# table rv32im.tbl and hand-written listings; (2) seeded Omega: seed/out/omc builds irrv32.om, +# which lifts the OSC IR v1 of a scalar uint unit (vecadd.unit.om, mix.unit.om) into the +# elementwise loop. Every checker below is an interop oracle, not Omega-native: GNU objdump and +# LLVM objdump disassemble; ttsim (Tenstorrent's open simulator, Wormhole and Blackhole +# libraries) and qemu-riscv32 execute; oracle.py only moves bytes in and out. None of them +# generates or edits an instruction. Nothing here touches a Tenstorrent card. +# Optional: TTSIM_DIR (holds libttsim_wh_aarch64.so and libttsim_bh_aarch64.so of ttsim v1.15.0), +# RISCV_OBJDUMP, RISCV_OBJCOPY, LLVM_OBJDUMP, QEMU_RISCV32, PYTHON. +# Exit 0 PASS, 1 FAIL, 3 NOT_RUN (some oracle absent, nothing failed). Artifacts: build/seed/tensix. +set -u +cd "$(dirname "$0")/../.." +R=seed/tensix +B=build/seed/tensix +rm -rf "$B" +mkdir -p "$B" +: > "$B/gate.log" +say() { printf '%s\n' "$*" | tee -a "$B/gate.log"; } +fails=0 +notrun=0 +ok() { say "PASS $*"; } +bad() { say "FAIL $*"; fails=$((fails + 1)); } +nr() { say "NOT_RUN $*"; notrun=$((notrun + 1)); } +sha() { sha256sum "$1" | cut -d ' ' -f 1; } +first_diff() { cmp "$1" "$2" 2>&1 | sed -n 1p; } + +ASM=seed/rung2/asm2.bin +TAB=$R/rv32im.tbl +ASM_PIN=6988abe072fac3cdef2240d5ad13a3ecd8fb576fa09112d344b9e7c2315969c4 +OMC=seed/out/omc +OMC_PIN=62775d2a391cfef7cde64e387655caa9809a267093112910ec76bfa568f3712b +TTSIM_VERSION=v1.15.0 +TTSIM_WH_PIN=e2a56ffe04072a10d477db1ef85cdbd5d392d9fc52043bb949b90a4c01ff6b4f +TTSIM_BH_PIN=2367dccc964c0ce4a20b14520e5f7ac6191bb227fb20e87aca1e94bf7432ced2 +TTSIM_DIR=${TTSIM_DIR:-build/oracle/ttsim-$TTSIM_VERSION} +GNU=${RISCV_OBJDUMP:-riscv64-unknown-elf-objdump} +OBJCOPY=${RISCV_OBJCOPY:-riscv64-unknown-elf-objcopy} +LLVM=${LLVM_OBJDUMP:-/usr/lib/llvm-18/bin/llvm-objdump} +QEMU=${QEMU_RISCV32:-qemu-riscv32} +PY=${PYTHON:-python3} +have() { command -v "$1" >/dev/null 2>&1; } + +[ "$(uname -m)" = aarch64 ] || { say "NOT_RUN asm2.bin is an AArch64 Linux program"; exit 3; } +[ -f "$ASM" ] && [ "$(sha "$ASM")" = "$ASM_PIN" ] || { say "FAIL assembler $ASM pin differs"; exit 1; } +say "SOURCE $(git rev-parse HEAD 2>/dev/null || echo unknown)" +say "COMPILER $ASM sha256=$ASM_PIN (unchanged rung-2 assembler); $OMC sha256=$(sha "$OMC" 2>/dev/null) (pin $OMC_PIN; builds irrv32.om)" +say "TABLE $TAB sha256=$(sha "$TAB") entries=$(grep -c '^= ' "$TAB")" +say "PLATFORM $(uname -srm)" + +# ---- 1. assemble twice; both builds must be identical and equal the committed vecadd.bin ---- +asm() { cat "$TAB" "$@" | nice -n 19 "$ASM"; } +build() { # name listing... + n=$1 + shift + asm "$@" > "$B/$n.bin" 2> "$B/$n.err" || { bad "asm2 refused $n: $(cat "$B/$n.err")"; return; } + asm "$@" > "$B/$n.again.bin" 2>/dev/null + cmp -s "$B/$n.bin" "$B/$n.again.bin" && ok "determinism $n ($(wc -c < "$B/$n.bin") bytes, sha256=$(sha "$B/$n.bin"))" || + bad "determinism $n: two builds differ" +} +build vecadd $R/vecadd.s2 +build tt $R/boot-tt.s2 $R/vecadd.s2 +build qemu $R/boot-qemu.s2 $R/vecadd.s2 +build edges $R/edges.s2 +cmp -s "$B/vecadd.bin" $R/vecadd.bin && ok "vecadd.bin equals the committed bytes" || bad "vecadd.bin differs from committed $R/vecadd.bin" +dd if="$B/tt.bin" bs=4 skip=9 2>/dev/null | cmp -s - "$B/vecadd.bin" && ok "tt image = boot-tt (36 bytes) + vecadd" || bad "tt image tail is not vecadd" +dd if="$B/qemu.bin" bs=4 skip=15 2>/dev/null | cmp -s - "$B/vecadd.bin" && ok "qemu image = boot-qemu (60 bytes) + vecadd" || bad "qemu image tail is not vecadd" + +# Fixtures: little-endian uint32 words; expect = (a + b) mod 2^32, computed with Wolfram (see README). +for f in a b expect; do xxd -r -p $R/$f.hex > "$B/$f.bin"; done +N=$(grep -c . $R/a.hex) +for f in a b expect; do + [ "$(wc -c < "$B/$f.bin")" -eq $((4 * N)) ] || bad "$f.hex does not hold $N words" +done +say "FIXTURE n=$N a=$(sha "$B/a.bin") b=$(sha "$B/b.bin") expect=$(sha "$B/expect.bin")" +xxd -r -p $R/mix-expect.hex > "$B/mix-expect.bin" +[ "$(wc -c < "$B/mix-expect.bin")" -eq $((4 * N)) ] || bad "mix-expect.hex does not hold $N words" + +# ---- 1b. seeded Omega path: omc builds irrv32.om; irrv32 lifts a scalar unit's OSC IR v1 ---- +# into the elementwise RV32 loop. The kernel bytes come from Omega; the checkers stay oracles. +omir() { { printf 'ir\n'; cat "$1"; } | nice -n 19 "$OMC" > "$2" 2> "$2.err"; } +OM_OK= +if [ ! -x "$OMC" ]; then + nr "seeded Omega path: $OMC absent" +elif [ "$(sha "$OMC")" != "$OMC_PIN" ]; then + bad "compiler $OMC pin differs" +elif ! nice -n 19 "$OMC" < $R/irrv32.om > "$B/irrv32" 2> "$B/irrv32.err"; then + bad "omc refused irrv32.om: $(cat "$B/irrv32.err")" +else + nice -n 19 "$OMC" < $R/irrv32.om > "$B/irrv32.again" + cmp -s "$B/irrv32" "$B/irrv32.again" && ok "determinism omc irrv32.om ($(wc -c < "$B/irrv32") bytes, sha256=$(sha "$B/irrv32"))" || + bad "determinism: two omc builds of irrv32.om differ" + chmod +x "$B/irrv32" + OM_OK=1 + for u in vecadd mix; do + if omir $R/$u.unit.om "$B/$u.ir" && "$B/irrv32" < "$B/$u.ir" > "$B/om-$u.bin" 2> "$B/om-$u.err"; then + "$B/irrv32" < "$B/$u.ir" > "$B/om-$u.again.bin" + cmp -s "$B/om-$u.bin" "$B/om-$u.again.bin" && ok "determinism irrv32 $u.unit.om ($(wc -c < "$B/om-$u.bin") bytes, sha256=$(sha "$B/om-$u.bin"), IR sha256=$(sha "$B/$u.ir"))" || + bad "determinism irrv32 $u: two emissions differ" + dd if="$B/tt.bin" bs=4 count=9 2>/dev/null | cat - "$B/om-$u.bin" > "$B/om-$u-tt.bin" + dd if="$B/qemu.bin" bs=4 count=15 2>/dev/null | cat - "$B/om-$u.bin" > "$B/om-$u-qemu.bin" + else + bad "irrv32 path refused $u.unit.om: $(cat "$B/$u.ir.err" "$B/om-$u.err" 2>/dev/null)" + OM_OK= + fi + done + [ -n "$OM_OK" ] && { cmp -s "$B/om-vecadd.bin" $R/vecadd.bin && ok "irrv32 vecadd output is byte-identical to the asm2 vecadd.bin" || + bad "irrv32 vecadd differs from asm2 vecadd.bin: $(first_diff "$B/om-vecadd.bin" $R/vecadd.bin)"; } +fi + +# ---- 2. two independent disassemblers ---- +dis_gnu() { "$GNU" -D -b binary -m riscv:rv32 -M no-aliases,numeric "$1" | grep -E '^ +[0-9a-f]+:' | cut -f3- | sed 's/ *#.*//; s/[[:space:]]*$//'; } +dis_llvm() { + "$OBJCOPY" -I binary -O elf32-littleriscv -B riscv --rename-section .data=.text,code,alloc,load,contents "$1" "$1.o" && + "$LLVM" -d -M no-aliases -M numeric --mattr=+m "$1.o" | grep -E '^ +[0-9a-f]+:' | cut -f2- | sed 's/ <.*//; s/[[:space:]]*$//' +} +# mnemonic followed by its register operands, in order (immediates are printed in different bases) +regs() { tr ',()' ' ' | awk '{ s = $1; for (i = 2; i <= NF; i++) if ($i ~ /^x[0-9]+$/) s = s " " $i; print s }'; } +if have "$GNU"; then + say "ORACLE gnu $("$GNU" --version | sed -n 1p)" + for p in vecadd edges; do + dis_gnu "$B/$p.bin" > "$B/$p.gnu" + diff "$B/$p.gnu" $R/$p.expect > "$B/$p.gnu.diff" && ok "GNU objdump $p: $(wc -l < "$B/$p.gnu") words match $p.expect (written by hand)" || + bad "GNU objdump $p differs from $p.expect: $(sed -n 2,3p "$B/$p.gnu.diff" | tr '\n' ' ')" + done + grep '^ ' $R/edges.s2 | awk '{ print $1 }' > "$B/edges.names" + cut -f1 "$B/edges.gnu" | diff - "$B/edges.names" > /dev/null && ok "edges.s2 covers every table entry: $(sort -u "$B/edges.names" | wc -l) of $(grep -c '^= ' "$TAB") names" || + bad "GNU mnemonics differ from the edges.s2 names" + [ "$(sort -u "$B/edges.names" | wc -l)" -eq "$(grep -c '^= ' "$TAB")" ] || bad "edges.s2 misses a table entry" +else + nr "GNU objdump ($GNU) absent" +fi +if have "$GNU" && [ -n "$OM_OK" ]; then + dis_gnu "$B/om-mix.bin" > "$B/om-mix.gnu" + diff "$B/om-mix.gnu" $R/mix.expect > "$B/om-mix.gnu.diff" && ok "GNU objdump om-mix (CONST lui+addi, MOV, mul, xor): $(wc -l < "$B/om-mix.gnu") words match mix.expect (written by hand)" || + bad "GNU objdump om-mix differs from mix.expect: $(sed -n 2,3p "$B/om-mix.gnu.diff" | tr '\n' ' ')" +fi +if have "$LLVM" && have "$OBJCOPY" && have "$GNU"; then + say "ORACLE llvm $("$LLVM" --version | grep -i 'llvm version' | sed 's/^ *//')" + for p in vecadd edges; do + dis_llvm "$B/$p.bin" > "$B/$p.llvm" || bad "LLVM objdump failed on $p" + regs < "$B/$p.llvm" > "$B/$p.llvm.regs" + regs < "$B/$p.gnu" > "$B/$p.gnu.regs" + cmp -s "$B/$p.llvm.regs" "$B/$p.gnu.regs" && ok "LLVM objdump $p: mnemonics and registers equal GNU's on all $(wc -l < "$B/$p.llvm") words" || + bad "LLVM and GNU disagree on $p" + done +else + nr "LLVM objdump ($LLVM) or objcopy ($OBJCOPY) absent" +fi + +# ---- 3. execution oracles ---- +ttsim_run() { # arch image out + timeout 600 nice -n 19 "$PY" $R/oracle.py ttsim "$TTSIM_DIR/libttsim_$1_aarch64.so" "$1" "$2" \ + "$B/a.bin" "$B/b.bin" "$N" "$3" > "$3.log" 2>&1 +} +qemu_run() { # image out + "$PY" $R/oracle.py elf "$1" "$B/a.bin" "$B/b.bin" "$N" "$1.elf" && chmod +x "$1.elf" && + timeout 60 nice -n 19 "$QEMU" "$1.elf" > "$2" 2> "$2.err" +} +ttsim_ok() { # arch: 0 when the pinned library is present + lib=$TTSIM_DIR/libttsim_$1_aarch64.so + eval "pin=\$TTSIM_$(echo "$1" | tr a-z A-Z)_PIN" + [ -f "$lib" ] && [ "$(sha "$lib")" = "$pin" ] +} +have "$PY" || nr "Python ($PY) absent; ttsim and qemu oracles skipped" +TT_DONE= +for arch in wh bh; do + if have "$PY" && ttsim_ok $arch; then + say "ORACLE ttsim $TTSIM_VERSION libttsim_${arch}_aarch64.so sha256=$(sha "$TTSIM_DIR/libttsim_${arch}_aarch64.so") driver oracle.py sha256=$(sha $R/oracle.py) $("$PY" --version 2>&1)" + if ttsim_run $arch "$B/tt.bin" "$B/out-$arch.bin" && cmp -s "$B/out-$arch.bin" "$B/expect.bin"; then + ok "ttsim $arch: $N of $N sums equal expect ($(grep '^ttsim' "$B/out-$arch.bin.log"))" + TT_DONE=$arch + else + bad "ttsim $arch: $(tail -n 1 "$B/out-$arch.bin.log") $(first_diff "$B/out-$arch.bin" "$B/expect.bin")" + fi + else + nr "ttsim $arch: $TTSIM_DIR/libttsim_${arch}_aarch64.so absent or not the pinned $TTSIM_VERSION build" + fi +done +Q_DONE= +if have "$PY" && have "$QEMU"; then + say "ORACLE qemu $("$QEMU" --version | sed -n 1p)" + if qemu_run "$B/qemu.bin" "$B/out-qemu.bin" && cmp -s "$B/out-qemu.bin" "$B/expect.bin"; then + ok "qemu-riscv32: $N of $N sums equal expect" + Q_DONE=1 + else + bad "qemu-riscv32: $(first_diff "$B/out-qemu.bin" "$B/expect.bin") $(cat "$B/out-qemu.bin.err" 2>/dev/null)" + fi +else + nr "qemu-riscv32 ($QEMU) absent" +fi +# Seeded Omega kernels: the same boot stubs followed by irrv32's output. +if [ -n "$OM_OK" ]; then + for u in vecadd mix; do + want=$B/expect.bin + [ $u = mix ] && want=$B/mix-expect.bin + for arch in wh bh; do + if have "$PY" && ttsim_ok $arch; then + if ttsim_run $arch "$B/om-$u-tt.bin" "$B/om-$u-$arch.out" && cmp -s "$B/om-$u-$arch.out" "$want"; then + ok "ttsim $arch on Omega-emitted $u: $N of $N words equal $(basename "$want") ($(grep '^ttsim' "$B/om-$u-$arch.out.log"))" + else + bad "ttsim $arch on Omega-emitted $u: $(tail -n 1 "$B/om-$u-$arch.out.log") $(first_diff "$B/om-$u-$arch.out" "$want")" + fi + else + nr "ttsim $arch on Omega-emitted $u (library absent)" + fi + done + if have "$PY" && have "$QEMU"; then + if qemu_run "$B/om-$u-qemu.bin" "$B/om-$u-qemu.out" && cmp -s "$B/om-$u-qemu.out" "$want"; then + ok "qemu-riscv32 on Omega-emitted $u: $N of $N words equal $(basename "$want")" + else + bad "qemu-riscv32 on Omega-emitted $u: $(first_diff "$B/om-$u-qemu.out" "$want") $(cat "$B/om-$u-qemu.out.err" 2>/dev/null)" + fi + else + nr "qemu-riscv32 on Omega-emitted $u (absent)" + fi + done +fi + +# ---- 4. negative controls: each mutation must be caught ---- +mutate() { # sed-expr in out: the edit must change the file + sed "$1" "$2" > "$3" + ! cmp -s "$2" "$3" || { bad "control edit '$1' did not change $2"; return 1; } +} +# C1: one bit flipped in the table (add's funct7 bit 30: add becomes sub). +if mutate 's/^= add 0x00000033/= add 0x40000033/' "$TAB" "$B/c1.tbl"; then + cat "$B/c1.tbl" $R/vecadd.s2 | "$ASM" > "$B/c1.bin" + if have "$GNU"; then + dis_gnu "$B/c1.bin" | cmp -s - $R/vecadd.expect && bad "C1 table bit flip not seen by GNU objdump" || + ok "C1 table bit flip (add->sub) caught by GNU objdump" + fi + if [ -n "$TT_DONE" ]; then + cat "$B/c1.tbl" $R/boot-tt.s2 $R/vecadd.s2 | "$ASM" > "$B/c1-tt.bin" + ttsim_run "$TT_DONE" "$B/c1-tt.bin" "$B/c1-out.bin" + cmp -s "$B/c1-out.bin" "$B/expect.bin" && bad "C1 table bit flip not seen by ttsim $TT_DONE" || + ok "C1 table bit flip caught by ttsim $TT_DONE ($(first_diff "$B/c1-out.bin" "$B/expect.bin"))" + else + nr "C1 on ttsim (no ttsim run)" + fi +fi +# C2: one wrong expected value (last word) must fail against the real ttsim/qemu output. +sed '$ s/^../00/' $R/expect.hex | xxd -r -p > "$B/c2-expect.bin" +GOOD= +[ -n "$TT_DONE" ] && GOOD=$B/out-$TT_DONE.bin +[ -z "$GOOD" ] && [ -n "$Q_DONE" ] && GOOD=$B/out-qemu.bin +if [ -n "$GOOD" ]; then + cmp -s "$GOOD" "$B/c2-expect.bin" && bad "C2 wrong expected value accepted" || + ok "C2 wrong expected value caught ($(first_diff "$GOOD" "$B/c2-expect.bin"))" +else + nr "C2 (no executed output)" +fi +# C3: wrong register in the listing (add t2,t0,t0) must change qemu's output. +if mutate 's/^ add 7 5 6 / add 7 5 5 /' $R/vecadd.s2 "$B/c3.s2"; then + if [ -n "$Q_DONE" ]; then + asm $R/boot-qemu.s2 "$B/c3.s2" > "$B/c3.bin" + qemu_run "$B/c3.bin" "$B/c3-out.bin" + cmp -s "$B/c3-out.bin" "$B/expect.bin" && bad "C3 wrong register not seen by qemu" || + ok "C3 wrong register caught by qemu-riscv32 ($(first_diff "$B/c3-out.bin" "$B/expect.bin"))" + else + nr "C3 (no qemu run)" + fi +fi +# C4: one branch-offset piece wrong (bne imm[11]) must stop ttsim from finishing correctly. +if mutate 's/^ bne 13 0 -1 63 0 1 / bne 13 0 -1 63 0 0 /' $R/vecadd.s2 "$B/c4.s2"; then + if [ -n "$TT_DONE" ]; then + asm $R/boot-tt.s2 "$B/c4.s2" > "$B/c4.bin" + if ttsim_run "$TT_DONE" "$B/c4.bin" "$B/c4-out.bin" && cmp -s "$B/c4-out.bin" "$B/expect.bin"; then + bad "C4 wrong branch offset not seen by ttsim $TT_DONE" + else + ok "C4 wrong branch offset caught by ttsim $TT_DONE ($(tail -n 1 "$B/c4-out.bin.log"))" + fi + else + nr "C4 (no ttsim run)" + fi +fi +# C5: an immediate outside its 12-bit field must be refused by asm2. +if mutate 's/^ lw 5 10 0 / lw 5 10 2048 /' $R/vecadd.s2 "$B/c5.s2"; then + asm "$B/c5.s2" > "$B/c5.bin" 2> "$B/c5.err" && bad "C5 out-of-range immediate accepted by asm2" || + ok "C5 out-of-range immediate refused by asm2 ($(sed -n 1p "$B/c5.err"))" +fi +# C6: the scalar unit mutated (add -> sub) must change irrv32's bytes and ttsim's output. +if [ -n "$OM_OK" ] && mutate 's/^ret add a b$/ret sub a b/' $R/vecadd.unit.om "$B/c6.unit.om"; then + omir "$B/c6.unit.om" "$B/c6.ir" && "$B/irrv32" < "$B/c6.ir" > "$B/c6.bin" + cmp -s "$B/c6.bin" $R/vecadd.bin && bad "C6 mutated unit gave the vecadd bytes" || + ok "C6 mutated unit (add->sub) changes irrv32 output ($(first_diff "$B/c6.bin" $R/vecadd.bin))" + if [ -n "$TT_DONE" ]; then + dd if="$B/tt.bin" bs=4 count=9 2>/dev/null | cat - "$B/c6.bin" > "$B/c6-tt.bin" + ttsim_run "$TT_DONE" "$B/c6-tt.bin" "$B/c6-out.bin" + cmp -s "$B/c6-out.bin" "$B/expect.bin" && bad "C6 mutated unit not seen by ttsim $TT_DONE" || + ok "C6 mutated unit caught by ttsim $TT_DONE ($(first_diff "$B/c6-out.bin" "$B/expect.bin"))" + else + nr "C6 on ttsim (no ttsim run)" + fi +elif [ -z "$OM_OK" ]; then + nr "C6 (seeded Omega path did not run)" +fi +# C7: the emitter mutated (pointer a0 stride 4 -> 8) must change the kernel that qemu runs. +if [ -n "$OM_OK" ] && mutate 's/^itype 4 10 0 10 19$/itype 8 10 0 10 19/' $R/irrv32.om "$B/c7.om"; then + if "$OMC" < "$B/c7.om" > "$B/c7-irrv32" && chmod +x "$B/c7-irrv32" && "$B/c7-irrv32" < "$B/vecadd.ir" > "$B/c7.bin"; then + if [ -n "$Q_DONE" ]; then + dd if="$B/qemu.bin" bs=4 count=15 2>/dev/null | cat - "$B/c7.bin" > "$B/c7-qemu.bin" + qemu_run "$B/c7-qemu.bin" "$B/c7-out.bin" + cmp -s "$B/c7-out.bin" "$B/expect.bin" && bad "C7 mutated emitter not seen by qemu" || + ok "C7 mutated emitter (stride 8) caught by qemu-riscv32 ($(first_diff "$B/c7-out.bin" "$B/expect.bin"))" + else + nr "C7 (no qemu run)" + fi + else + bad "C7 mutated emitter did not build or run" + fi +elif [ -z "$OM_OK" ]; then + nr "C7 (seeded Omega path did not run)" +fi +# C8: a unit irrv32 cannot lower faithfully (div traps on zero in Omega) must be refused, no bytes out. +if [ -n "$OM_OK" ] && mutate 's/^ret add a b$/ret div a b/' $R/vecadd.unit.om "$B/c8.unit.om"; then + omir "$B/c8.unit.om" "$B/c8.ir" + "$B/irrv32" < "$B/c8.ir" > "$B/c8.bin" 2> "$B/c8.err" + rc=$? + [ "$rc" -ne 0 ] && [ ! -s "$B/c8.bin" ] && ok "C8 div unit refused by irrv32 (exit $rc, $(cat "$B/c8.err"), 0 bytes)" || + bad "C8 div unit not refused (exit $rc, $(wc -c < "$B/c8.bin") bytes)" +elif [ -z "$OM_OK" ]; then + nr "C8 (seeded Omega path did not run)" +fi + +sha256sum $R/* "$ASM" "$OMC" > "$B/SHA256SUMS" 2>/dev/null +if [ "$fails" -gt 0 ]; then + say "RESULT FAIL ($fails failed, $notrun not run)" + exit 1 +fi +if [ "$notrun" -gt 0 ]; then + say "RESULT NOT_RUN ($notrun checks could not run; nothing failed)" + exit 3 +fi +say "RESULT PASS (interop oracles, not Omega-native; no Tenstorrent hardware was used)" +exit 0 diff --git a/seed/tensix/irrv32.om b/seed/tensix/irrv32.om new file mode 100644 index 000000000..1491f3c95 --- /dev/null +++ b/seed/tensix/irrv32.om @@ -0,0 +1,360 @@ +note SPDX-License-Identifier: Apache-2.0 +note OSC IR v1 scalar uint unit to an RV32IM elementwise kernel for a Tensix baby RISC-V core. +note Wire authority spec/one-layer-format.md. Encodings: RISC-V unprivileged ISA, RV32I and M. +note Input: one function of two uint params returning uint, one block, CONST MOV BIN RET only. +note Output: raw little-endian RV32 words of the loop out[i] = f(a[i], b[i]) for i below n. +note Kernel ABI: a0 = a, a1 = b, a2 = out, a3 = n (32-bit words), return through ra. +note Vreg k maps to x5 x6 x7 for k below 3, then x14 x15 x16 x17; at most 7 vregs. +note Supported BIN: add sub mul and or xor (u32 wraps, as RV32 does). div rem shl shr are +note refused because Omega traps on zero divisors and shift range and this kernel has no trap path. +note Buffered output: every refusal happens before any stdout write. +global input uchar 65536 +global output uchar 4096 +note state slots: 0 input cursor, 1 input size, 2 output size, 3 nvregs, 4 ninsns, 5 defined mask +global state ulong 8 +global ops ulong 64 +global subs ulong 64 +global dest ulong 64 +global arg0 ulong 64 +global arg1 ulong 64 +global immed ulong 64 +text emagic "irrv32: MAGIC\n" +text ever "irrv32: VERSION\n" +text etrunc "irrv32: TRUNCATED\n" +text ecap "irrv32: CAPACITY\n" +text eop "irrv32: UNSUPPORTED_OP\n" +text eshape "irrv32: UNSUPPORTED_SHAPE\n" +text einval "irrv32: INVALID\n" +text etrail "irrv32: TRAILING\n" +text eio "irrv32: IO\n" +fn refuse code int to void +k = long 0 +if eq code 20 +k = write 2 emagic 0 len emagic +end +if eq code 21 +k = write 2 ever 0 len ever +end +if eq code 22 +k = write 2 etrunc 0 len etrunc +end +if eq code 23 +k = write 2 ecap 0 len ecap +end +if eq code 24 +k = write 2 eop 0 len eop +end +if eq code 25 +k = write 2 eshape 0 len eshape +end +if eq code 26 +k = write 2 einval 0 len einval +end +if eq code 27 +k = write 2 etrail 0 len etrail +end +if eq code 28 +k = write 2 eio 0 len eio +end +exit code +end +fn need ok bool to void +if not ok +refuse 26 +end +end +fn rd n ulong to ulong +p = get state 0 +if gt n sub get state 1 p +refuse 22 +end +v = ulong 0 +for j ulong n +v = or v shl ulong get input add p j mul j 8 +end +put state 0 add p n +ret v +end +fn vr to ulong +v = rd 2 +need lt v get state 3 +ret v +end +fn isdef v ulong to bool +ret ne and get state 5 shl ulong 1 v 0 +end +fn def v ulong to void +put state 5 or get state 5 shl ulong 1 v +end +fn reg v ulong to ulong +if lt v 3 +ret add v 5 +end +ret add v 11 +end +fn emitw w ulong to void +p = get state 2 +if gt add p 4 len output +refuse 23 +end +for j ulong 4 +put output add p j uchar and shr w mul j 8 255 +end +put state 2 add p 4 +end +note R-type: funct7 rs2 rs1 funct3 rd, opcode 51 (OP). +fn rtype f7 ulong rs2 ulong rs1 ulong f3 ulong dr ulong to void +w = or shl f7 25 shl rs2 20 +w = or w or shl rs1 15 shl f3 12 +w = or w or shl dr 7 51 +emitw w +end +note I-type: imm is the low 12 bits of the two's-complement immediate (4095 is -1). +fn itype imm ulong rs1 ulong f3 ulong dr ulong op ulong to void +w = or shl and imm 4095 20 shl rs1 15 +w = or w or shl f3 12 shl dr 7 +w = or w op +emitw w +end +note S-type: store word rs2 to imm(rs1), opcode 35 (STORE). +fn stype imm ulong rs2 ulong rs1 ulong f3 ulong to void +w = or shl shr imm 5 25 shl rs2 20 +w = or w or shl rs1 15 shl f3 12 +w = or w or shl and imm 31 7 35 +emitw w +end +note B-type: off is the 13-bit two's-complement byte offset (8192 minus k is -k), opcode 99. +fn btype off ulong rs1 ulong rs2 ulong f3 ulong to void +b12 = and shr off 12 1 +b11 = and shr off 11 1 +b10 = and shr off 5 63 +b4 = and shr off 1 15 +w = or shl b12 31 shl b10 25 +w = or w or shl rs2 20 shl rs1 15 +w = or w or shl f3 12 shl b4 8 +w = or w or shl b11 7 99 +emitw w +end +note U-type: 20-bit upper immediate, opcode 55 (LUI). +fn utype imm ulong dr ulong op ulong to void +emitw or shl and imm 1048575 12 or shl dr 7 op +end +fn insn i ulong to void +op = rd 1 +put ops i op +known = or or eq op 1 eq op 2 or eq op 3 eq op 15 +if not known +refuse 24 +end +if eq op 1 +d = vr +v = rd 8 +if gt v 4294967295 +refuse 26 +end +put dest i d +put immed i v +def d +end +if eq op 2 +d = vr +a = vr +need isdef a +put dest i d +put arg0 i a +def d +end +if eq op 3 +s = rd 1 +d = vr +a = vr +b = vr +ok = or or eq s 1 eq s 2 eq s 3 +ok = or ok or or eq s 6 eq s 7 eq s 8 +if not ok +refuse 24 +end +need isdef a +need isdef b +put subs i s +put dest i d +put arg0 i a +put arg1 i b +def d +end +if eq op 15 +a = vr +need isdef a +put arg0 i a +end +end +fn skip n ulong to void +for j ulong n +k = rd 1 +end +end +fn loadfn to void +skip rd 1 +if ne rd 1 2 +refuse 25 +end +if ne rd 1 4 +refuse 25 +end +nv = rd 2 +if gt nv 7 +refuse 23 +end +need ge nv 2 +put state 3 nv +for j ulong nv +if ne rd 1 4 +refuse 25 +end +end +if ne rd 2 1 +refuse 24 +end +bf = rd 4 +bc = rd 4 +ni = rd 4 +if gt ni 64 +refuse 23 +end +need gt ni 0 +need eq bf 0 +need eq bc ni +put state 4 ni +put state 5 3 +for i ulong ni +insn i +end +skip rd 2 +skip rd 2 +for i ulong ni +isret = eq get ops i 15 +last = eq i sub ni 1 +if ne isret last +refuse 24 +end +end +end +fn kernel to void +ni = get state 4 +nl = ulong 0 +for i ulong ni +op = get ops i +if eq op 1 +nl = add nl 2 +end +if or eq op 2 eq op 3 +nl = add nl 1 +end +end +note beq a3, x0, done: forward 4 * (9 + body words) bytes +btype mul 4 add 9 nl 13 0 0 +note lw t0, 0(a0); lw t1, 0(a1): the two params, vregs 0 and 1 +itype 0 10 2 5 3 +itype 0 11 2 6 3 +rv = ulong 0 +for i ulong ni +op = get ops i +d = reg get dest i +if eq op 1 +v = get immed i +utype shr add v 2048 12 d 55 +itype and v 4095 d 0 d 19 +end +if eq op 2 +itype 0 reg get arg0 i 0 d 19 +end +if eq op 3 +s = get subs i +f7 = ulong 0 +f3 = ulong 0 +if eq s 2 +f7 = ulong 32 +end +if eq s 3 +f7 = ulong 1 +end +if eq s 6 +f3 = ulong 7 +end +if eq s 7 +f3 = ulong 6 +end +if eq s 8 +f3 = ulong 4 +end +rtype f7 reg get arg1 i reg get arg0 i f3 d +end +if eq op 15 +rv = reg get arg0 i +end +end +note sw result, 0(a2); advance a0 a1 a2 by 4; a3 -= 1; bne a3, x0, loop; jalr x0, 0(ra) +stype 0 rv 12 2 +itype 4 10 0 10 19 +itype 4 11 0 11 19 +itype 4 12 0 12 19 +itype 4095 13 0 13 19 +btype sub 8192 mul 4 add 7 nl 13 0 1 +itype 0 1 0 0 103 +end +fn main to int +n = ulong 0 +for attempt ulong 65536 +if eq n len input +refuse 23 +end +k = read 0 input n sub len input n +if lt k 0 +refuse 28 +end +if eq k 0 +break +end +n = add n ulong k +end +if eq n len input +refuse 23 +end +put state 1 n +magic = new uchar 7 0 +put magic 0 79 +put magic 1 83 +put magic 2 67 +put magic 3 49 +put magic 4 73 +put magic 5 82 +for j ulong 7 +v = rd 1 +if ne v ulong get magic j +refuse 20 +end +end +if ne rd 1 1 +refuse 21 +end +if ne rd 2 1 +refuse 25 +end +loadfn +if ne get state 0 n +refuse 27 +end +kernel +p = ulong 0 +for attempt ulong 4096 +if eq p get state 2 +break +end +k = write 1 output p sub get state 2 p +if le k 0 +refuse 28 +end +p = add p ulong k +end +ret 0 +end diff --git a/seed/tensix/mix-expect.hex b/seed/tensix/mix-expect.hex new file mode 100644 index 000000000..02d32cf56 --- /dev/null +++ b/seed/tensix/mix-expect.hex @@ -0,0 +1,64 @@ +00000000 +ffffffff +00000080 +ffffff7f +ffbba065 +78788286 +d7534e30 +88ffee12 +3947a3d6 +eaaa90eb +9b4af6e4 +4ca647eb +fd7d46d9 +ae8dc8b0 +5f811811 +10291764 +c1747731 +722459cf +23980859 +d49388af +85cb9f0d +369f7c68 +e7ae35d6 +985a6012 +49222dd7 +fab10966 +ab65a6ee +5c5dd24f +0dd93e54 +bee818ef +6f5c9d21 +2084c365 +d18fb856 +820321b9 +33735a96 +e40e5d94 +9566bbcb +467a630a +f7091dd7 +a83146a2 +59fd3908 +0a9d206c +bb805759 +6cf8ccc0 +1db430c1 +cef7fa4e +7f77b621 +3063f284 +e1aa616d +926e0ff5 +432e287d +f4a59bf1 +a5817465 +56411234 +07e541d4 +b8ac0142 +695851b3 +1a78f3f2 +cb3be3b4 +7c976145 +2d6f3066 +ded27201 +8f92052e +40de46a4 diff --git a/seed/tensix/mix.expect b/seed/tensix/mix.expect new file mode 100644 index 000000000..9aafd51ac --- /dev/null +++ b/seed/tensix/mix.expect @@ -0,0 +1,16 @@ +beq x13,x0,0x3c +lw x5,0(x10) +lw x6,0(x11) +lui x14,0xb2d06 +addi x14,x14,-512 +add x7,x5,x6 +mul x15,x7,x14 +addi x16,x15,0 +xor x17,x16,x5 +sw x17,0(x12) +addi x10,x10,4 +addi x11,x11,4 +addi x12,x12,4 +addi x13,x13,-1 +bne x13,x0,0x4 +jalr x0,0(x1) diff --git a/seed/tensix/mix.unit.om b/seed/tensix/mix.unit.om new file mode 100644 index 000000000..c014ff30f --- /dev/null +++ b/seed/tensix/mix.unit.om @@ -0,0 +1,7 @@ +note Exercises CONST (lui+addi), MOV, mul and xor in irrv32.om: out[i] = ((a[i]+b[i])*3000000000) xor a[i]. +fn mix a uint b uint to uint +x = add a b +y = mul x uint 3000000000 +z = y +ret xor z a +end diff --git a/seed/tensix/oracle.py b/seed/tensix/oracle.py new file mode 100755 index 000000000..5990f32f6 --- /dev/null +++ b/seed/tensix/oracle.py @@ -0,0 +1,137 @@ +#!/usr/bin/env python3 +# oracle.py -- interop oracle, not Omega-native; never generates kernels. +# +# Host dependency (oracle only): Python 3 standard library (ctypes, struct). It moves bytes that asm2 made +# into two outside checkers and moves the result back out; it never writes, changes or chooses an +# instruction word. Omega replacement: an .om host loader once Omega has a dlopen/FFI word (ttsim path) +# and a byte-writer for ELF headers (qemu path); both are proposals, not implemented. +# +# oracle.py ttsim LIB ARCH IMAGE A B N OUT +# Tenstorrent ttsim (libttsim_{wh,bh}), the vendor's open simulator, driven through its PCI Express +# interface only, the way a host driver drives a card: read BAR0 from config space, point one TLB +# window at Tensix tile L1, hold the tile's five RISC-V cores in soft reset, write IMAGE at L1 0x0 +# (the reset pc of RISCV B), the argument block at 0x1000, A at 0x2000, B at 0x3000, then release +# RISCV B only and clock until the stub writes the done mark 0x0600d000 at 0x1010. OUT gets the 4N +# bytes at 0x4000. ARCH is wh (Wormhole B0, n150) or bh (Blackhole A0, p150). +# oracle.py elf IMAGE A B N OUT +# Wraps IMAGE in a 32-bit RISC-V ELF executable for qemu-riscv32 (Linux user mode): one load segment +# at 0x10000, IMAGE at 0x10100 (the entry), argument block 0x11000, A 0x12000, B 0x13000, out 0x14000. +# Sources (read 2026-10-26): tt-isa-documentation be833101 WormholeB0/PCIExpressTile/TLBs.md, +# BlackholeA0/PCIExpressTile/HostToDeviceTLBs.md, */TensixTile/SoftReset.md, */NoC/Coordinates.md; +# ttsim b3620244 src/libttsim.cpp (API), src/pcie.cpp (config space); ELF: System V gABI, RISC-V psABI. +import ctypes, struct, sys + +L1_ARGS, L1_A, L1_B, L1_OUT, DONE = 0x1000, 0x2000, 0x3000, 0x4000, 0x0600D000 +SOFT_RESET_0 = 0xFFB121B0 # RISCV_DEBUG_REG_SOFT_RESET_0 (BH SoftReset.md; WH tile_regs 0xFFB12000+0x1B0) +ALL_CORES, BRISC = 0x47800, 0x800 # bits 11 (B), 12-14 (T0-T2), 18 (NC) +TLB_CFG = 0x1FC00000 # TLB configuration array in BAR0 (both architectures) + + +def die(msg): + sys.stderr.write("oracle: " + msg + "\n") + sys.exit(1) + + +def words(path, n): + data = open(path, "rb").read() + if len(data) != 4 * n: + die("%s has %d bytes, want %d" % (path, len(data), 4 * n)) + return data + + +def ttsim(lib_path, arch, image_path, a_path, b_path, n, out_path): + lib = ctypes.CDLL(lib_path) + u32, u64, vp = ctypes.c_uint32, ctypes.c_uint64, ctypes.c_void_p + lib.libttsim_pci_config_rd32.restype = u32 + lib.libttsim_pci_config_rd32.argtypes = [u32, u32] + lib.libttsim_pci_mem_wr_bytes.argtypes = [u64, vp, u32] + lib.libttsim_pci_mem_rd_bytes.argtypes = [u64, vp, u32] + lib.libttsim_clock.argtypes = [u32] + lib.libttsim_init() + ident = lib.libttsim_pci_config_rd32(0, 0x0) + want = {"wh": 0x401E1E52, "bh": 0xB1401E52}[arch] + if ident != want: + die("config space id 0x%08x, want 0x%08x for %s" % (ident, want, arch)) + if not lib.libttsim_pci_config_rd32(0, 0x4) & 2: + die("BAR memory decode (Command.MSE) is off") + bar0 = (lib.libttsim_pci_config_rd32(0, 0x10) & ~0xF) | (lib.libttsim_pci_config_rd32(0, 0x14) << 32) + + def wr(addr, data): + buf = ctypes.create_string_buffer(data, len(data)) + lib.libttsim_pci_mem_wr_bytes(bar0 + addr, buf, len(data)) + + def rd(addr, size): + buf = ctypes.create_string_buffer(size) + lib.libttsim_pci_mem_rd_bytes(bar0 + addr, buf, size) + return buf.raw + + if arch == "wh": + # 1 MiB windows: local_offset = top 16 bits of the 36-bit tile address, x_end at bit 16, y_end at 22. + x, y, shift = 1, 1, 20 # NoC #0 (1,1) is a Tensix tile (WormholeB0/NoC/Coordinates.md) + + def point(window, tile_addr): + cfg = (tile_addr >> shift) | (x << 16) | (y << 22) + wr(TLB_CFG + 8 * window, struct.pack("> 21, x_end at bit 43, y_end at 49. + x, y, shift = 1, 2, 21 # NoC #0 (1,2) is a Tensix tile (BlackholeA0/NoC/Coordinates.md) + + def point(window, tile_addr): + cfg = (tile_addr >> shift) | (x << 43) | (y << 49) + for i in range(3): # three 32-bit register writes + wr(TLB_CFG + 12 * window + 4 * i, struct.pack("> (32 * i)) & 0xFFFFFFFF)) + return window << shift + + l1 = point(0, 0) + regs = point(1, SOFT_RESET_0 & ~((1 << shift) - 1)) + (SOFT_RESET_0 & ((1 << shift) - 1)) + reset_before = struct.unpack("= limit: + die("no done mark after %d clocks" % clocks) + lib.libttsim_clock(step) + clocks += step + open(out_path, "wb").write(rd(l1 + L1_OUT, 4 * n)) + wr(regs, struct.pack(" args: + die("image too large") + body = bytearray(out + 4 * n) + body[code:code + len(image)] = image + body[args:args + 16] = struct.pack(" vaddr 0x10000, read/write/execute, page aligned. + phdr = struct.pack("