Repository navigation
Bringup 12 toy walkthrough
Relevant source files
The following files were used as context for generating this wiki page:
The order of work and the test after each step. The code itself is on the pages linked from each step. For object-file output, continue with the MC checklist.
| Property | Choice |
|---|---|
| Word size | 32-bit, little endian |
| Registers |
r0–r15, sp, lr; r11 is the frame pointer when one is needed |
| Calling convention | Arguments in r0–r3, then the stack; return in r0/r1; r8–r11 and lr callee-saved |
| Instructions |
add, addi, lw, sw, call, ret, a compare and a branch |
| Data layout | e-m:e-p:32:32-i64:64-n32-S64 |
llvm/lib/Target/Toy/
CMakeLists.txt
Toy.td ToyRegisterInfo.td ToyCallingConv.td ToySchedule.td ToyInstrInfo.td
ToyTargetMachine.{h,cpp} ToySubtarget.{h,cpp}
ToyISelLowering.{h,cpp} ToyISelDAGToDAG.cpp
ToyInstrInfo.{h,cpp} ToyRegisterInfo.{h,cpp} ToyFrameLowering.{h,cpp}
ToyAsmPrinter.cpp ToyMCInstLower.{h,cpp}
TargetInfo/{CMakeLists.txt, ToyTargetInfo.cpp}
MCTargetDesc/{CMakeLists.txt, ToyMCTargetDesc.cpp, ToyMCAsmInfo.cpp, ToyInstPrinter.cpp}
Outside the directory: Triple::toy in llvm/include/llvm/TargetParser/Triple.h and its parser, a case Triple::toy: in llvm/lib/TargetParser/TargetDataLayout.cpp (the file where AArch64's string is built, lines 81–97), and Toy added to LLVM_ALL_EXPERIMENTAL_TARGETS in llvm/CMakeLists.txt.
Sources: llvm/lib/TargetParser/TargetDataLayout.cpp:81-97, llvm/CMakeLists.txt:602-607
flowchart TD
S1["1. Triple + TargetInfo"] --> S2["2. Toy.td: registers, empty instr set"]
S2 --> S3["3. MC glue: MCAsmInfo, generated infos, InstPrinter"]
S3 --> S4["4. TargetMachine + Subtarget + PassConfig"]
S4 --> S5["5. TargetLowering, LowerFormalArguments, LowerReturn"]
S5 --> S6["6. ADD, RET + selector"]
S6 --> S7["7. FrameLowering + eliminateFrameIndex + AsmPrinter"]
S7 --> S8["8. Loads, stores, branches, LowerCall"]
S8 --> S9["9. -O2: analyzeBranch, copyPhysReg, spills"]
| Step | Write | Page | Test |
|---|---|---|---|
| 1 |
Triple::toy, ToyTargetInfo.cpp, CMake for TargetInfo/
|
Layout and CMake |
llc --version lists toy
|
| 2 |
Toy.td, ToyRegisterInfo.td, empty ToyInstrInfo.td
|
The Target .td File, Registers |
llvm-tblgen -gen-register-info succeeds |
| 3 |
ToyMCTargetDesc.cpp, ToyMCAsmInfo, ToyInstPrinter
|
AsmPrinter and MC | Library links |
| 4 |
ToyTargetMachine, ToySubtarget, ToyPassConfig::addInstSelector
|
TargetMachine and Subtarget |
llc -march=toy on an empty module succeeds |
| 5 |
ToyTargetLowering constructor, CC_Toy, RetCC_Toy, LowerFormalArguments, LowerReturn
|
TargetLowering, Calling Conventions |
define void @f() { ret void } reaches instruction selection |
| 6 |
ADD, RET records, ToyDAGToDAGISel
|
Instructions and ISel |
llc -march=toy -stop-after=finalize-isel prints ADD
|
| 7 |
ToyFrameLowering, getReservedRegs, eliminateFrameIndex, ToyAsmPrinter, ToyMCInstLower
|
Frame Lowering, AsmPrinter and MC |
llc -march=toy -O0 prints add r0, r0, r1 / ret
|
| 8 |
LW, SW, branch, LowerCall, CALL pattern, getCallPreservedMask
|
Calling Conventions | A function that calls another compiles at -O0
|
| 9 |
copyPhysReg, storeRegToStackSlot, loadRegFromStackSlot, analyzeBranch
|
Instructions and ISel | The same tests at -O2; -verify-machineinstrs passes |
For the same add function as on the overview, the Toy target should print something like this (illustrative: Toy does not exist, so this was not produced by llc):
add:
add r0, r0, r1
retCompare it with the real AArch64 output from the overview (add w0, w0, w1 / ret): the same shape, with Toy's register names and mnemonics.
| Symptom | Usual cause |
|---|---|
No available targets are compatible with triple |
Triple parser does not know toy, or RegisterTarget uses a different ArchType
|
Crash in initAsmInfo
|
An MC factory (asm info, register info, instr info, subtarget info) is not registered |
Cannot select: ... |
Missing pattern or missing setOperationAction (TargetLowering) |
Not Implemented (assertions build) |
A required call-lowering hook is missing (Calling Conventions) |
Call Frame Pseudo Instructions do not exist on this target! |
ADJCALLSTACKDOWN/UP used but eliminateCallFramePseudoInstr not overridden (Frame Lowering) |
Bad machine code from -verify-machineinstrs
|
Wrong register class in a pattern, missing Defs/Uses on calls and returns |
Unknown operand type in MCInstLower
|
An operand kind not handled (often MO_RegisterMask or an implicit register) |
Always test with -verify-machineinstrs: it catches most mistakes right after the pass that made them. The regalloc wiki's backend checklist has more checks for the allocation-related steps.
- 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