Skip to content

概览

本页中的每一个偏移、值和地址都从 libtpu-0.0.40-cp314 wheel 中的 libtpu.so 逐字节读取而来(BuildID md5 89edbbe81c5b328a958fe628a9f2207d)。其他版本会有所不同。

摘要

第 VI 部分记录 TPU TensorCore 指令集以及编译器如何对其编码。TensorCore 是一台静态调度的 VLIW 机器:其片上 sequencer 每个周期取一条固定宽度的 bundle,并在同一周期把该 bundle 中的每个 slot 发射到一个不同的执行单元,包括 matrix unit、vector ALU、scalar pipe、memory port、sequencer 自身的控制逻辑;运行时不做依赖跟踪。硬件 bundle 之上是 LLO(后期的、TPU 专用的编译器 IR):它是一串带类型的指令,每条指令包含 opcode + operands + memory-space + predicate,由 HLO → MHLO → TLP → tpu → LLO 的下降过程生成,并由 bundle packer 和各代 encoder 消费。LloOpcodeProto enum 包含 462 个 opcode,MemorySpaceProto 包含 17 个 memory space;它们合在一起,构成了本部分其余内容要解码成 bit 的词汇表。

编码问题有两面,本部分把二者分开处理。一面是 LLVM-MC 层TPUMCCodeEmitter::getBinaryCodeForInstr0x13c74da0)通过每个 opcode 的 base-bits 表和 insertBits 空洞,把一个 MCInst 降低为一个 239-bit APInt record;这条路径实际只为 BarnaCore-Pxc(Pufferfish)HwMode 携带 bit。另一面是 proto-bundle 层:各代的 Encoder<gen>::EncodeBundleInternal 以及 IsaEmitterEmitX / <Slot>Encoder::Encode 路径;它编码 MC 层返回全零的所有 TensorCore 和 V5+ opcode。重新实现者需要知道哪些 opcode 由哪条路径拥有;MC-Emitter 页面精确划出了这条线(5667 个 MC opcode 中有 4956 个走向 zero-base default)。

本页用于导航。它让读者定位 VLIW 模型,列出各代 bundle 宽度(41 B Jellyfish → 51 B Pufferfish → 64 B Viperfish / Ghostlite / 6acc60406),概述 slot 分类,然后指向每一块内容所属的页面。更深层的机制在其他页面:slot/issue 语义见 bundle model,bit layout 见各 slot 页面,完整 slot map 见各代 bundle 页面,LLVM-MC wire path 见 record format / MC-emitter / InstBits DB 三个页面。

对重新实现而言,契约是:

  • 两级编码拆分:上层是 LLO IR(LloOpcodeProto,462 个 opcode),下层是固定宽度的按代 VLIW bundle;并且有两个不同的 encoder(MC insertBits record 与 proto-bundle EmitX)。
  • VLIW slot 模型:每个 slot 驱动哪个 engine,以及编译器必须遵守的 simultaneous-issue / no-forwarding 规则。
  • 六代 TpuVersion 轴(0..5),并且 bundle 宽度和 slot 集合按 version 选择,而不是通过字节扩展得到。
LLO IR enumLloOpcodeProto — 462 个 opcode — LloOpcode Enum
Memory spaceMemorySpaceProto — 17 个值 — MemorySpace Enum
MC recordllvm::APInt,239 bits,4 words — Record Format
MC emitterTPUMCCodeEmitter::getBinaryCodeForInstr @ 0x13c74da0MC-Emitter
Bundle 宽度41 B(Jellyfish)/ 51 B(Pufferfish)/ 64 B(Viperfish、Ghostlite、6acc60406)— Bundle Model
TpuVersion6 个值(TpuVersionToString @ 0x20b3a480≥ 6 时 trap)
Proto-bundle encodeEncoder<gen>::EncodeBundleInternal(Jellyfish @ 0x1e86c7c0

两个编码层级

LLO 是编译器 IR;bundle 是 wire form。这个拆分在二进制中真实存在,重新实现者不能把二者混为一谈。

  • Level 1 — LLO instruction stream。 一个程序是一串 LloInstruction,每条都是 (opcode, operand-list, memory-space, predicate)。opcode 是 462 个 LloOpcodeProto 值之一;memory space 是 17 个 MemorySpaceProto 值之一。这是 optimizer 和 scheduler 操作的对象。见 LloOpcode EnumLloOpcode ↔ Proto
  • Level 2 — VLIW bundle word。 bundle packer 把相互独立的 LLO op 分组到一个 Bundle 中,其带类型的 sub-field 映射到硬件 slot;随后各代 encoder 将其序列化为固定宽度的 byte buffer。见 Bundle Model

在 Level 2 内部有 两个 encoder,某个 opcode 的 bit 由哪个 encoder 携带取决于 generation:

Encoder拥有内容页面
LLVM-MC insertBits record仅 BarnaCore-Pxc(Pufferfish)lane + native opMC-Emitter, Record Format
proto-bundle EmitX / <Slot>Encoder::Encode每个 TensorCore + Viperfish / Ghostlite / 6acc60406 opcodeIsaEmitter Registry, V5+ EmitX Bit Positions

NOTE — 对绝大多数 opcode(5667 个中的 4956 个),MC getBinaryCodeForInstr 会返回全零的 239-bit record。这不是 bug,也不是 stub;这些 opcode 由 proto-bundle 路径编码。若重新实现把 MC 层当成唯一 encoder,就会为每条 V5+ 指令发出全零 bundle。见 MC-Emitter


VLIW Bundle 及其 Slot

bundle 是一条固定宽度的 VLIW word,在一个周期内发射;由于硬件不做证明,编译器必须证明 slot 之间相互独立。宽度按 generation 固定,并由 (TpuVersion, TpuSequencerType) codec-metadata 查找来选择。下面的 codename ↔ external-name 映射,是 Codename Matrix 根据 TpuVersionToString / TpuVersionToExternalName 这对函数固定下来的;6acc60406TpuVersion 5)是二进制中的字面 codename,不是市场名称(Trillium/Ironwoodlibtpu.so 中出现 0 次)。

TpuVersionCodenameExternal nameBundle bytesBundle bits
0JellyfishTPU v241328
1DragonfishTPU v341328
2PufferfishTPU v451408
3ViperfishTPU v5e64512
4GhostliteTPU v6 lite64512
56acc60406TPU7x64512

41-byte Jellyfish 宽度是最硬的固定值:它是 EncoderJf::EncodeBundleInternal0x1e86c7c0)内部字面量 operator new(0x29)(= 41)的分配大小,而不是一次 metadata 读取。51-byte 和 64-byte 宽度在运行时计算:EncoderPfTensorCore::BundleSizeBytes inline 返回 51,而 v5+ codec 通过一个 (TpuVersion, TpuSequencerType) vtable call 得到 64(SparseCore overlayer 的 GetTileInstructionBundleSizeInBytes 则把 per-tile size 推导为 field[32] / field[31])。各代完整的 byte 来源核算见 Bundle Model 页面。

slot 按执行单元划分。每类 slot 都是编译器侧 Bundle 对象中的一个带类型 sub-instruction,并有自己的页面:

EngineSlot 页面
Matrix unit(systolic MXU)MXU Slot, Matprep / IAR / Latch, ResultFifo & ArchRegister
Vector ALU(VPU)VPU Slot
Scalar pipe(SPU)SPU / Scalar Slot
Sequencer(control flow / sync)Sequencer Slot, Sequencer Ops Per Gen
Memory portMemory-Load, Memory-Store, cmem_load (Pufferfish)
Predicate / loop / immediatePredicate, Hardware Loop-Counter, Immediate
Extended unit(transcendentals)EUP / Transcendental Slot
Mask / M-registervcreate_mask / M-Register
SparseCore(v5+)Sparsity Slot

完整的各代 slot map,也就是哪些 slot 存在以及位于哪些 byte offset,见各代页面:Jellyfish 41-BDragonfishPufferfish 51-BViperfish 64-BGhostlite6acc60406。约束它们的 simultaneous-issue 和 empty-slot(kNeverExecute)语义见 Bundle Model 页面。


LLO 如何打包进 Bundle

从一个 LLO op 到其 bit 的路径有固定形状:

text
LloInstruction  (opcode + operands + memory-space + predicate)
   │  bundle packer — 将独立 op 分组成带类型的 Bundle slot

Bundle  (ScalarInstruction, VectorAluInstruction, VectorExtendedInstruction,
         VectorLoadInstruction, VectorStoreInstruction, VectorResultInstruction,
         MiscInstruction, + HardwareBundleBits header)
   │  Encoder<gen>::EncodeBundleInternal — 在每个存在的 slot 的 byte offset 写入

N-byte bundle word   (41 / 51 / 64, per TpuVersion)
```text

packer 是 scheduler 的职责([LLO Bundle Packing](../sched/llo-bundle-packing.md));serialization 是 encoder 的职责。对 BarnaCore-Pxc 路径,per-instruction bit 还会经过 [239-bit MC record](record-format.md);对其他所有情况,[IsaEmitter](isa-emitter-registry.md) 会直接写入 byte。MXU 是一个两阶段例外:matrix push 通过 EUP / latch 路径进入 systolic array,结果若干周期后通过 result-FIFO slot 读回,见 [MXU Slot](slot-mxu.md)。

---

## 本部分如何组织

这些页面分成五组:

- **基础** — 本页、[LloOpcode Enum](llo-opcode-enum.md)、[MemorySpace Enum](memory-space-enum.md) 和 [Bundle Model](bundle-model-overview.md)。
- **MC wire path** — [InstBits Master DB](instbits-master-db.md)、[TPUInstrNameData / Descs / RegEncoding](instr-name-data.md)、[LloOpcode ↔ Proto](llo-opcode-to-proto.md)、[MC-Emitter](mc-emitter.md)、[239-Bit Record Format](record-format.md)、[TPUMCImm / SyImm32](tpumcimm-syimm32.md)、[ArchRegno Numbering](archregno-numbering.md)。
- **各代 bundle** — [Jellyfish](bundle-jf-41b.md)、[Dragonfish](bundle-df.md)、[Pufferfish](bundle-pf-51b.md)、[Viperfish](bundle-vf-64b.md)、[Ghostlite](bundle-gl.md)、[6acc60406](bundle-gf.md)。
- **各 slot 编码** — 上文链接的 MXU / VPU / SPU / sequencer / memory / predicate / loop / immediate / EUP / matprep / mask / cmem / sparsity slot 页面,另有 [V5+ EmitX Bit Positions](v5plus-emitx-bit-positions.md)。
- **Encode / decode 支持** — [IsaEmitter Registry](isa-emitter-registry.md)、[Decode-Side JF/PF](decode-side-jf-pf.md)、[Decode-Side VF/GXC](decode-side-vf-gxc.md)、[NOP / Unused-Slot Canonical Encoding](nop-canonical.md)、[kIsaTable Data Sections](kisatable-data-sections.md)、[ResultFifo & ArchRegister](resultfifo-archregister.md)、[Bias-Add & Quant/Dequant](bias-quantization-helpers.md)、[XLU Op Roster](xlu-op-roster.md)、[Pack/Unpack Precision](pack-unpack-precision.md)。

各代 silicon family 自身(cost model、sub-core taxonomy、address-space ID)位于 targets 部分;从 [Targets Overview](../targets/overview.md) 开始。

---

## 交叉引用

- [Bundle Model](bundle-model-overview.md) — 本页概述的 VLIW bundle、slot taxonomy 和 simultaneous-issue 语义。
- [LloOpcode Enum](llo-opcode-enum.md) — 462 个值的 `LloOpcodeProto` 指令词汇表。
- [InstBits Master DB](instbits-master-db.md) — MC emitter 读取的 base-bits、descriptor、name 和 register-encoding 表。
- [MC-Emitter](mc-emitter.md) — `getBinaryCodeForInstr`、per-opcode dispatch,以及 MC-vs-proto-bundle ownership 分界线。
- [Targets Overview](../targets/overview.md) — 各代 silicon family、cost model 和 sub-core taxonomy。