Repository navigation
Bringup 02 layout cmake
Relevant source files
The following files were used as context for generating this wiki page:
The files and directories a target needs, how CMake builds them, how LLVM_TARGETS_TO_BUILD selects targets, and the registration functions. Instruction and register TableGen are on The Target .td File.
| Directory | Contents | Library | Init function |
|---|---|---|---|
lib/Target/<T>/ |
<T>.td, <T>TargetMachine, <T>Subtarget, <T>ISelLowering, <T>ISelDAGToDAG, <T>InstrInfo, <T>RegisterInfo, <T>FrameLowering, <T>AsmPrinter, <T>MCInstLower
|
LLVM<T>CodeGen |
LLVMInitialize<T>Target, LLVMInitialize<T>AsmPrinter
|
lib/Target/<T>/TargetInfo/ |
getThe<T>Target() |
LLVM<T>Info |
LLVMInitialize<T>TargetInfo |
lib/Target/<T>/MCTargetDesc/ |
MC objects (asm info, printer, code emitter, asm backend) | LLVM<T>Desc |
LLVMInitialize<T>TargetMC |
lib/Target/<T>/AsmParser/ |
Assembly parser | LLVM<T>AsmParser |
LLVMInitialize<T>AsmParser |
lib/Target/<T>/Disassembler/ |
Disassembler | LLVM<T>Disassembler |
LLVMInitialize<T>Disassembler |
The TargetInfo library is tiny and depends on nothing else in the target, so tools that only need the target's name can link it alone. The MC libraries must not depend on CodeGen (see the MC custom backend page).
llvm/CMakeLists.txt lists the core targets in LLVM_ALL_TARGETS and the experimental ones in LLVM_ALL_EXPERIMENTAL_TARGETS. LLVM_TARGETS_TO_BUILD defaults to all core targets; a new target starts as experimental and is enabled with LLVM_EXPERIMENTAL_TARGETS_TO_BUILD.
set(LLVM_ALL_TARGETS
AArch64
AMDGPU
...
X86
XCore
)
set(LLVM_ALL_EXPERIMENTAL_TARGETS
ARC
CSKY
M68k
Xtensa
)For a new target, configure with -DLLVM_EXPERIMENTAL_TARGETS_TO_BUILD=Toy (and, to save build time, -DLLVM_TARGETS_TO_BUILD=AArch64 or similar). CMake checks every requested name against these lists and stops with an error listing the valid names if one is unknown.
Sources: llvm/CMakeLists.txt:578-607, llvm/CMakeLists.txt:716, llvm/CMakeLists.txt:1157-1165
AArch64's file does three things:
- Names the root TableGen file:
set(LLVM_TARGET_DEFINITIONS AArch64.td). - Runs
llvm-tblgenonce per backend, each producing one.incfile:
tablegen(LLVM AArch64GenAsmMatcher.inc -gen-asm-matcher)
tablegen(LLVM AArch64GenAsmWriter.inc -gen-asm-writer)
tablegen(LLVM AArch64GenCallingConv.inc -gen-callingconv)
tablegen(LLVM AArch64GenDAGISel.inc -gen-dag-isel)
tablegen(LLVM AArch64GenDisassemblerTables.inc -gen-disassembler)
tablegen(LLVM AArch64GenInstrInfo.inc -gen-instr-info)
tablegen(LLVM AArch64GenMCCodeEmitter.inc -gen-emitter)
tablegen(LLVM AArch64GenRegisterInfo.inc -gen-register-info)
tablegen(LLVM AArch64GenSubtargetInfo.inc -gen-subtarget)
...
add_public_tablegen_target(AArch64CommonTableGen)- Declares the CodeGen library with
add_llvm_target(AArch64CodeGen ...)listing the.cppfiles andLINK_COMPONENTS, then adds the subdirectories (TargetInfo,MCTargetDesc,AsmParser,Disassembler,Utils).
The first line, add_llvm_component_group(AArch64 HAS_JIT), creates the AArch64 component name used by llvm-config and by other components' LINK_COMPONENTS.
tablegen backend |
Output used by | Needed for first milestone |
|---|---|---|
-gen-register-info |
<T>RegisterInfo, MC register info |
Yes |
-gen-instr-info |
<T>InstrInfo, MC instr info |
Yes |
-gen-subtarget |
<T>Subtarget, scheduling model |
Yes |
-gen-callingconv |
<T>ISelLowering.cpp |
Yes |
-gen-dag-isel |
<T>ISelDAGToDAG.cpp |
Yes |
-gen-asm-writer |
<T>InstPrinter |
Yes |
-gen-emitter |
<T>MCCodeEmitter |
For .o
|
-gen-asm-matcher |
<T>AsmParser |
For .s input |
-gen-disassembler |
<T>Disassembler |
For llvm-objdump
|
Sources: llvm/lib/Target/AArch64/CMakeLists.txt:1-33
sequenceDiagram
participant llc
participant Info as LLVMInitializeToyTargetInfo
participant TM as LLVMInitializeToyTarget
participant MC as LLVMInitializeToyTargetMC
participant AP as LLVMInitializeToyAsmPrinter
llc->>Info: RegisterTarget (name, description, triple arch)
llc->>TM: RegisterTargetMachine, initialize passes
llc->>MC: Register MCAsmInfo, InstrInfo, RegInfo, ...
llc->>AP: RegisterAsmPrinter
llc->>llc: TargetRegistry::lookupTarget(triple)
Each target is a single static Target object returned by a function. RegisterTarget<Arch, HasJIT> binds it to a Triple::ArchType, a short name for -march, and a description:
Target &llvm::getTheAArch64leTarget() {
static Target TheAArch64leTarget;
return TheAArch64leTarget;
}
...
extern "C" LLVM_ABI LLVM_EXTERNAL_VISIBILITY void
LLVMInitializeAArch64TargetInfo() {
...
RegisterTarget<Triple::aarch64, /*HasJIT=*/true> Z(
getTheAArch64leTarget(), "aarch64", "AArch64 (little endian)", "AArch64");
...
}A new architecture also needs an entry in Triple::ArchType and the triple parser (llvm/lib/TargetParser/Triple.cpp), so that toy-unknown-elf parses to Triple::toy. The data layout for the triple is computed in llvm/lib/TargetParser/TargetDataLayout.cpp (see TargetMachine and Subtarget).
Sources: llvm/lib/Target/AArch64/TargetInfo/AArch64TargetInfo.cpp:14-52
LLVMInitializeAArch64Target registers the target machine class for each Target object and initializes the target's own machine passes so they can be named on the command line:
extern "C" LLVM_ABI LLVM_EXTERNAL_VISIBILITY void
LLVMInitializeAArch64Target() {
// Register the target.
RegisterTargetMachine<AArch64leTargetMachine> X(getTheAArch64leTarget());
RegisterTargetMachine<AArch64beTargetMachine> Y(getTheAArch64beTarget());
...
auto &PR = *PassRegistry::getPassRegistry();
initializeGlobalISel(PR);
initializeAArch64A53Fix835769LegacyPass(PR);
...
}The AsmPrinter is registered from the CodeGen library too:
LLVMInitializeAArch64AsmPrinter() {
RegisterAsmPrinter<AArch64AsmPrinter> X(getTheAArch64leTarget());
RegisterAsmPrinter<AArch64AsmPrinter> Y(getTheAArch64beTarget());
...
}MC registration (LLVMInitializeAArch64TargetMC) is shown on the MC custom backend page.
Sources: llvm/lib/Target/AArch64/AArch64TargetMachine.cpp:234-287, llvm/lib/Target/AArch64/AArch64AsmPrinter.cpp:4187-4192
Illustrative, not LLVM source.
# lib/Target/Toy/CMakeLists.txt (illustrative)
add_llvm_component_group(Toy)
set(LLVM_TARGET_DEFINITIONS Toy.td)
tablegen(LLVM ToyGenRegisterInfo.inc -gen-register-info)
tablegen(LLVM ToyGenInstrInfo.inc -gen-instr-info)
tablegen(LLVM ToyGenSubtargetInfo.inc -gen-subtarget)
tablegen(LLVM ToyGenCallingConv.inc -gen-callingconv)
tablegen(LLVM ToyGenDAGISel.inc -gen-dag-isel)
tablegen(LLVM ToyGenAsmWriter.inc -gen-asm-writer)
tablegen(LLVM ToyGenMCCodeEmitter.inc -gen-emitter)
add_public_tablegen_target(ToyCommonTableGen)
add_llvm_target(ToyCodeGen
ToyAsmPrinter.cpp ToyFrameLowering.cpp ToyInstrInfo.cpp
ToyISelDAGToDAG.cpp ToyISelLowering.cpp ToyMCInstLower.cpp
ToyRegisterInfo.cpp ToySubtarget.cpp ToyTargetMachine.cpp
LINK_COMPONENTS
AsmPrinter CodeGen CodeGenTypes Core MC SelectionDAG Support Target
TargetParser ToyDesc ToyInfo
ADD_TO_COMPONENT Toy)
add_subdirectory(MCTargetDesc)
add_subdirectory(TargetInfo)// TargetInfo/ToyTargetInfo.cpp (illustrative)
Target &llvm::getTheToyTarget() {
static Target TheToyTarget;
return TheToyTarget;
}
extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeToyTargetInfo() {
RegisterTarget<Triple::toy> X(getTheToyTarget(), "toy", "Toy example", "Toy");
}After this step llc --version lists toy under "Registered Targets", even before the target machine exists.
- 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