Repository navigation
Hexagon ISA 02 fundamentals
Relevant source files
The following files were used as context for generating this wiki page:
- llvm/lib/Target/Hexagon/HexagonInstrFormats.td
- llvm/lib/Target/Hexagon/HexagonPseudo.td
- Wikipedia: Barrel processor
- Qualcomm: Hexagon V6x/V7x Programmer's Reference Manual, chapters on packets and pipeline
The single most important idea in Hexagon is the packet: a group of one to four instructions, written between { and }, that the hardware issues together. Everything else (slots, .new values, hardware loops) follows from it.
Assembly on this page is illustrative and was not assembled.
This page covers packets and their rules, execution slots and instruction classes, reading values inside a packet (.new), predication, zero-overhead hardware loops, and hardware threads. Bit-level encoding is on Encoding and Mnemonics.
| Kitchen team | Hexagon |
|---|---|
| A ticket listing up to four jobs that start together | A packet |
| Four stations, each able to do certain jobs | Slots 0-3 |
| Chef plans the tickets in advance | The compiler schedules packets |
| A job can use a result another job on the same ticket just made |
.new operands |
| Several orders cooking at once, taking turns at the stations | Hardware threads |
// illustrative: four instructions issued together
{
r1 = add(r2, r3) // ALU
r4 = memw(r5 + #8) // load
memw(r6 + #0) = r7 // store
p0 = cmp.eq(r8, #0) // compare
}
flowchart LR
P["Packet {i0; i1; i2; i3}"] --> S3["Slot 3"]
P --> S2["Slot 2"]
P --> S1["Slot 1"]
P --> S0["Slot 0"]
S3 --> E["All instructions read operands,<br/>execute, then write results together"]
S2 --> E
S1 --> E
S0 --> E
Rules that follow from "issued together":
| Rule | Consequence |
|---|---|
| Instructions in a packet see register values from before the packet |
{ r0 = r1; r1 = r0 } swaps the two registers |
| Two instructions may not write the same register | The assembler rejects it |
| Resource limits per packet | At most two memory operations, one branch, limited slot types |
| The packet is atomic for interrupts | An interrupt is taken between packets, never inside one |
Sources: llvm/lib/Target/Hexagon/HexagonInstrFormats.td:50
Each instruction belongs to a class that says which slots can run it. In general:
| Class | Typical instructions | Slots (general) |
|---|---|---|
| LD (load) |
memw, memb, memd reads |
0, 1 |
| ST (store) | stores | 0, 1 |
| ALU32 | add, sub, logical, simple moves, compares | 0-3 |
| XTYPE (complex) | multiply, shift, bit manipulation, 64-bit ops | 2, 3 |
| J (jump) | branches, calls | 2, 3 |
| CR (control register) | loops, predicate logic | 3 |
| HVX | vector instructions, with their own resources | see HVX |
LLVM's instruction descriptions carry flags for these constraints, for example a flag restricting slot 1 to ALU-only instructions and one marking instructions that prefer slot 3.
Sources: llvm/lib/Target/Hexagon/HexagonInstrFormats.td:75-77, llvm/lib/Target/Hexagon/HexagonInstrFormats.td:151-152
Normally a packet only sees old values. A few instructions may consume a value produced in the same packet by writing .new:
// illustrative
{ p0 = cmp.eq(r0, #0) // produce predicate
if (p0.new) jump:t done } // use it in the same packet ("dot-new predicate")
{ r2 = add(r1, #1)
memw(r3 + #0) = r2.new } // store the value computed this packet ("new-value store")
sequenceDiagram
participant C as cmp.eq (slot 2)
participant J as jump (slot 3)
C->>J: p0 forwarded inside the packet (.new)
Note over C,J: without .new, the jump would see p0 from before the packet
:t / :nt are static branch hints (taken / not taken).
Most instructions can be made conditional on a predicate register, which removes short branches:
// illustrative: r0 = (r1 > r2) ? r3 : r4, without a branch
{ p0 = cmp.gt(r1, r2) }
{ if (p0) r0 = add(r3, #0)
if (!p0) r0 = add(r4, #0) }
The two writes to r0 are allowed in one packet because their predicates are complementary.
// illustrative: sum 64 words
{ loop0(.Lbody, #64) // start address, iteration count
r0 = #0 }
.Lbody:
{ r1 = memw(r2++#4)
r0 = add(r0, r1) }:endloop0 // end-of-loop marker; no branch instruction
flowchart LR
L["loop0(start, N):<br/>SA0 = start, LC0 = N"] --> B["Loop body packets"]
B --> E{"Packet marked :endloop0"}
E -->|"LC0 > 1: LC0--, jump to SA0"| B
E -->|"LC0 = 1: fall through"| X["After loop"]
There are two loops, loop0 and loop1 (which can nest), using control registers SA0/LC0 and SA1/LC1 (Registers). The end of the loop is marked by bits in the last packet's encoding, not by a branch, so the loop costs no extra cycles.
Sources: llvm/lib/Target/Hexagon/HexagonPseudo.td:92-107
A Hexagon core runs several hardware threads and interleaves their packets, so while one thread waits for memory others keep the pipeline busy. This resembles a barrel processor. Each thread has its own register set; caches and execution units are shared. HVX contexts are a separate, smaller pool that threads must acquire. See the Architecture wiki: Multithreading.
Sources: Wikipedia: Barrel processor
- A packet of up to four instructions reads old values, executes together and writes together.
- Slots limit which instruction classes share a packet;
.newpasses a value within a packet. - Predication and zero-overhead
loop0/loop1remove most branch cost; hardware threads hide latency.
- Computer Architecture Overview
- Fundamentals: CPU, Memory and Instructions
- Pipelining
- Branches and Remedies
- Instruction-Level Parallelism
- Memory and Ordering
- Caches and Coherence
- Multicore
- Multithreading in Hardware
- SIMD and GPUs
- Glossary and Summary
- Hexagon Overview and Processor Properties
- ISA Fundamentals: Packets, Slots and Threads
- Registers
- Instruction Encoding and Mnemonics
- Data, Memory and Control Instructions
- Floating Point, HVX and Alignment
- Caches, MMU and Glossary
- Operating Systems Overview
- Processes
- Threads
- CPU Scheduling
- Synchronization
- Deadlock
- Memory Management
- Virtual Memory
- Files, Storage and I/O
- Kernel and System Calls
- Interview Q&A and Glossary
- C++ for LLVM Development: Overview
- C++ Basics
- Classes and RAII
- Ownership and Move Semantics
- Templates and Lambdas
- LLVM Data Structures (ADT)
- LLVM-Style RTTI: isa, cast, dyn_cast
- Errors, Output and Assertions
- Iterators and Ranges
- Building, Debugging and Exercises
- LLVM IR Overview
- Structure: Modules, Functions, Blocks and Values
- Type System
- Arithmetic, Bitwise and Cast Instructions
- Compare and Select
- Memory: alloca, load, store and getelementptr
- Control Flow: br, switch, ret and call
- SSA and Phi
- Control Flow Graph and Loops
- Dominance, Dominator Tree and Dominance Frontier
- Data-flow Analysis
- Writing an IR Pass
- Common Passes
- Exercises, Tooling and Glossary
- SelectionDAG Overview
- Pipeline, Phases and Traversal Orders
- Core Data Structures
- DAG Construction
- DAG Combiner
- Legalization
- Type Legalization
- Operation and Vector Legalization
- Instruction Selection
- TableGen Pattern Language
- Matcher Interpreter and Folding
- AArch64 Instruction Selection
- Scheduling and MIR Emission
- PHI Node Resolution
- AArch64 Walkthroughs
- Arithmetic and Data Movement
- Memory Instructions
- Control Flow and Calls
- End-to-End Example
- Tooling, Debugging and Extending
- Selector Comparison and Glossary
- Assignments: Build the Toy Backend
- Stage 1: Build Setup
- Stage 2: Target Registration
- Stage 3: TableGen Skeleton
- Stage 4: MC Layer for Assembly Output
- Stage 5: TargetMachine and Subtarget
- Stage 6: TargetLowering, Arguments and Returns
- Stage 7: Instruction Selection and the First Function
- Stage 8: Registers, Copies and Spills
- Stage 9: Frame Lowering
- Stage 10: Memory, Globals and Control Flow
- Stage 11: Calls
- Stage 12: Scheduling Model
- Stage 13: Object Files
- Stage 14: Capstone, a Complete Toy Backend
- Target Bring-up Overview
- Directory Layout, CMake and Registration
- The Target .td File
- TargetMachine and Subtarget
- TargetLowering Setup
- Calling Conventions
- Instructions and Instruction Selection
- Registers and Allocation Hooks
- Frame Lowering
- AsmPrinter and the MC Layer
- Scheduling
- A Minimal Toy Target
- Bring-up Checklist and AArch64 File Map
- Tooling and Glossary
- Calling Conventions Overview
- The CallingConv.td Language
- CCState and CCValAssign
- LowerFormalArguments
- LowerCall
- LowerReturn and CanLowerReturn
- Varargs, byval, sret and swifterror
- Tail Calls and Sibling Calls
- Custom Backend Guide and Checklist
- AArch64 Walkthroughs
- Tooling and Glossary
- Frame Lowering Overview
- MachineFrameInfo
- TargetFrameLowering Hooks
- The PEI Pass
- Callee-Saved Registers and Stack Layout
- Frame Index Elimination and the Register Scavenger
- Shrink Wrapping
- CFI and Unwind Information
- Custom Backend Guide and Checklist
- AArch64 Walkthroughs
- Tooling and Glossary
- MC Layer Overview
- Core Data Structures
- Streamers
- Assembler and Layout
- Fixups and Relocations
- Writing an MC Layer for a Custom Backend
- MCAsmInfo and the Instruction Printer
- Code Emitter
- AsmBackend and Object Writer
- Assembly Parser and Disassembler
- Bring-up Checklist and Common Failures
- Tooling and Glossary
- Register Allocation Overview
- The Register Allocation Pipeline
- Registers, Virtual Registers and Register Classes
- Liveness: Slot Indexes and Live Intervals
- Preparing Code for Allocation
- PHI Elimination and Two-Address Lowering
- Register Coalescer
- Allocation Framework
- The Register Allocators
- Fast Register Allocator
- Basic Register Allocator
- Greedy Register Allocator
- PBQP Register Allocator
- Spilling and Splitting
- Spill Weights and Copy Hints
- The Inline Spiller
- Live Range Splitting
- Rewriting and Post-RA Passes
- Writing Register Allocation Support for a Custom Backend
- Register Definitions in TableGen
- TargetRegisterInfo Hooks
- TargetInstrInfo Hooks
- Pass Pipeline Hooks
- Bring-up Checklist and Common Failures
- AArch64 Walkthroughs
- Tooling and Debugging
- Glossary and Where Do I Look