Tensix: Dst
tenstorrent_tt_isa)Tenstorrent Wormhole B0 & Blackhole A0
main
Tensix, WormholeB0Summary
The Dst register can be viewed as either having 1024 rows and 16 columns of 16-bit data, or 512 rows and 16 columns of 32-bit data. Instruction descriptions use Dst16b for the former, and Dst32b for the latter. In either case, the underlying storage is the same:
Reference Notes
# `Dst`
The `Dst` register can be viewed as either having 1024 rows and 16 columns of 16-bit data, or 512 rows and 16 columns of 32-bit data. Instruction descriptions use `Dst16b` for the former, and `Dst32b` for the latter. In either case, the underlying storage is the same:
```c
uint16_t DstBits[1024][16] = UnpredictableValue(); // no power-on reset value
```
In addition, each of the 1024 rows has 1 valid bit associated with it:
```c
bool DstRowValid[1024] = {true, ..., true};
```
Note that the power-on reset state can result in reads of `UnpredictableValue()`. Software should scrub `Dst` at initialization time, preferably with an `SFPSTORE` of all 1024 rows to a known value, or minimally with a `ZEROACC` of all rows to clear `DstRowValid` to `false`.
Most of the time, `Dst16b[Row][Col]` is simple syntactic sugar for `DstBits[Row][Col]`.
The same region of memory can instead be viewed as a 512x16 matrix of 32-bit data; reads from `Dst32b[Row][Col]` give `(DstBits[AdjRow][Col] << 16) | DstBits[AdjRow + 8][Col]` where `AdjRow = Adj32(Row)`, and writes to `Dst32b[Row][Col]` perform the inverse unpacking and write to `DstBits[AdjRow][Col]` and `DstBits[AdjRow + 8][Col]`. At any given time, software is expected to be exclusively using `Dst16b` or exclusively using `Dst32b`; mixing and matching `Dst16b` and `Dst32b` is _possible_, but tends to require great care. When using `Dst32b`, `Row` remains a 10-bit index, though the mapping down to `AdjRow` can only yield 512 distinct values.
If bit 11 of `RISCV_DEBUG_REG_DBG_FEATURE_DISABLE` is set, then `Dst16b[Row][Col]` stops being syntactic sugar for `DstBits[Row][Col]`, and instead reads from `Dst16b[Row][Col]` give the high 16 bits of `Dst32b[Row][Col]`, and writes to `Dst16b[Row][Col]` write to the high 16 bits of `Dst32b[Row][Col]` (and also write an `UnpredictableValue()` to the low 16 bits). Use of this bit in software is `UnsupportedFunctionality` because the resulting `UnpredictableValue` semantics can lead to difficult-to-diagnose issues.
The `Adj32` function is:
```c
uint10_t Adj32(uint10_t Row) {
return ((Row & 0x1f8) << 1) | (Row & 0x207);
}
```
Each of the valid bits is indexed by physical row: an access to `Dst32b[Row]` consults `DstRowValid[Adj32(Row)]` (and does not consult `Adj32(Row) + 8`), whereas an access to `Dst16b[Row]` consults `DstRowValid[Row]`.
If writes to `Dst` from different clients (Matrix Unit, Vector Unit, unpacker) execute in the same cycle and would set the valid bits of physical rows in the same 256-row group that are congruent modulo 8, only one valid bit is set: that of the row whose number is the bitwise OR of theirs, which can be none of them. Software must not let such writes race with each other.
Not every client consults them; [`ZEROACC`](ZEROACC.md) says how each reads an undefined row. In particular, `ZEROACC` doesn't give the Vector Unit (SFPU) zeros: [`SFPLOAD`](SFPLOAD.md) of an undefined row is `UndefinedBehavior`, so software that wants zeros in `Dst` for the Vector Unit to read must write them, e.g. with [`SFPSTORE`](SFPSTORE.md).
## Data types
Each datum in `Dst16b` is up to 16 bits wide, holding one of:
* **BF16 (1 sign bit, 8 bit exponent, 7 bit mantissa)**. Unpackers can losslessly convert BFP8 / BFP4 / BFP2 to BF16.
* **FP16 (1 sign bit, 5 bit exponent, 10 bit mantissa)**. Unpackers can losslessly convert FP8 (1 sign bit, 5 bit exponent, 2 bit mantissa) to FP16. They can also losslessly convert BFP8a / BFP4a / BFP2a to FP16.
* **Integer "8" (1 sign bit, 10 bit magnitude)**. The range of this type is -1023 through +1023, albeit the majority of hardware conversions to/from this type involve a range of -127 through +127 or 0 through 255. Internally, this type is overlaid onto FP16, using a fixed raw exponent of 16 (or sometimes a raw exponent of 0 when the magnitude is zero).
* **Integer "16" (1 sign bit, 15 bit magnitude)**. This type is intended only for opaque data transfer of 16 bits; there are no computation instructions involving this type. If used for opaque data transfer, this type can be used to contain _any_ 16 bits, including unsigned 16-bit data.
Each datum in `Dst32b` is 32 bits wide, holding one of:
* **FP32 (1 sign bit, 8 bit exponent, 23 bit mantissa)**. TF32 data can also be stored in this form, though most computation instructions will operate on it with full FP32 precision for as long as it is in `Dst32b`.
* **Integer "32" (1 sign bit, 31 bit magnitude)**. This is a sign/magnitude format rather than a two's complement format, though the Vector Unit (SFPU) can convert between the two.
Note that the coprocessor does not entirely conform to IEEE 754 for FP32 / FP16 / BF16. See [floating-point bit patterns](FloatBitPatterns.md) for details.
## Data type bit layout
Most of the time, software does not need to care about the exact bit layout of types in `Dst`. Nevertheless, the information is provided here to clarify the semantics of what happens if software does an implicit `bitcast` from one type to another.
**BF16:**

**FP16:**

The representation of infinity differs from IEEE 754, and NaN is not supported; see [FP16 bit patterns](FloatBitPatterns.md#fp16) for details.
**Integer "8":**

The low five bits usually contain the value `16`, but unpackers will instead set these bits to `0` when the magnitude is zero.
**Integer "16":**

**FP32:**

**Integer "32":**

## Moving data around
Unpacker 0 can be used to move from L1 to Dst (`UNPACR`), and [packers](Packers/README.md) can be used to move from Dst back to L1 ([`PACR`](PACR.md)).
The Vector Unit (SFPU) can move from Dst to LReg (`SFPLOAD`) and from LReg to Dst (`SFPSTORE`).
The Matrix Unit (FPU) can move from Dst to SrcA (`MOVD2A`) and from Dst to SrcB (`MOVD2B`). The other direction usually involves computation instructions rather than movement instructions, but plain movement is possible via `MOVA2D` and `MOVB2D`.
## Instruction scheduling
After issuing a Matrix Unit (FPU) instruction which writes to `Dst`, then for the next five cycles, the aligned 8x16 block of `Dst` containing that write cannot be read. If a thread presents a Matrix Unit (FPU) instruction which wants to read from that block, then hardware will automatically stall the thread for an appropriate number of cycles. In particular, for instructions which accumulate onto `Dst` (such as [`MVMUL`](MVMUL.md), [`GAPOOL`](GAPOOL.md), [`DOTPV`](DOTPV.md), [`GMPOOL`](GMPOOL.md), [`ELWMUL`](ELWMUL.md)), software needs to be looping over at least six distinct 8x16 blocks of `Dst` to avoid being stalled. Additionally, if using multiple fidelity phases, with one loop over blocks of `Dst` and one loop over fidelity phases, then the fidelity loop should be the _outer_ loop to avoid being stalled.
[`PACR`](PACR.md) is interlocked at a coarser granularity, namely half of `Dst` (either rows 0 through 511 of `DstBits`, or rows 512 through 1023): a packer will not read from a half of `Dst` while a Matrix Unit (FPU) [`MVMUL`](MVMUL.md), [`DOTPV`](DOTPV.md), [`ELWADD`](ELWADD.md), [`ELWSUB`](ELWSUB.md), [`ELWMUL`](ELWMUL.md), [`GAPOOL`](GAPOOL.md) or [`GMPOOL`](GMPOOL.md) instruction, or a Vector Unit (SFPU) [`SFPLOAD`](SFPLOAD.md) or [`SFPSTORE`](SFPSTORE.md) instruction, is still in progress against that half of `Dst`. This wait lasts until the instruction in question has finished, rather than for a fixed number of cycles.
This interlock does not cover [`MOVA2D`](MOVA2D.md) or [`MOVB2D`](MOVB2D.md). If the Matrix Unit (FPU) is still working on earlier instructions from the thread when the thread issues the move, the move starts later than the thread issues it, and a `PACR` issued shortly after the move can read the old contents of some of the moved rows, whichever half of `Dst` the earlier instructions write to (e.g. `MVMUL`, `MOVA2D`, then a `PACR` of the rows `MOVA2D` wrote). One reliable way to avoid this is to use [`STALLWAIT`](STALLWAIT.md) (with block bit B2 and condition code C7) between the move and the `PACR`. Condition code C7 only considers the current thread's instructions, so if the move and the `PACR` are on different threads, the thread which issued the move needs to wait for it (e.g. `STALLWAIT` with condition code C7) before telling the packing thread (e.g. via a semaphore) that the rows are ready. [`SFPLOAD`](SFPLOAD.md#instruction-scheduling) has the same consideration.
The `PACR` interlock only works in one direction: it makes a packer wait for earlier writes to `Dst`, but nothing makes a later write to `Dst` wait for a packer. A Matrix Unit (FPU) instruction, an `SFPSTORE` or a [`ZEROACC`](ZEROACC.md) issued after a `PACR` can change rows of `Dst` before the packers have read them, at any distance until the packers finish (e.g. a `PACR` of 1024 datums, then an `ELWADD` into rows 0 through 7 thirty instructions later: some of the `ELWADD`'s results are packed). Unpacks to `Dst` are not interlocked with `PACR` either: a `PACR` issued shortly after an [`UNPACR`](UNPACR.md) to `Dst` can read the old contents of the rows the unpack writes first (seen with up to 10 instructions between them, not with 12 or more). To reuse rows that a `PACR` reads, wait for the packers first, e.g. `STALLWAIT` with block bits B6 and B8 and conditions C3 through C6; to pack rows that an unpack to `Dst` wrote, wait for the unpacker first, e.g. `STALLWAIT` with block bit B2 and condition C1. If the two instructions are on different threads, the thread that issued the first one waits before handing the rows over, e.g. `STALLWAIT` with block bit B1 before its `SEMGET` or `SEMPOST`.
Other than the [`PACR`](PACR.md) interlock, Vector Unit (SFPU) accesses to `Dst` are not tracked by hardw