Repository navigation
Bringup 06 calling conventions
Relevant source files
The following files were used as context for generating this wiki page:
The minimum a new target needs for calls to work, with AArch64 as reference. The calling conventions wiki covers each part in depth: the TableGen language, CCState, the three hooks, varargs and tail calls. Where call lowering happens during DAG building is on the SelectionDAG DAG construction page.
flowchart LR
TD["ToyCallingConv.td<br/>CC_Toy, RetCC_Toy, CSR_Toy"] -->|-gen-callingconv| INC["ToyGenCallingConv.inc"]
INC --> LFA["LowerFormalArguments<br/>(incoming args)"]
INC --> LC["LowerCall<br/>(outgoing args, call node, results)"]
INC --> LR["LowerReturn<br/>(return values)"]
TD -->|-gen-register-info| CSR["CSR_Toy_SaveList / RegMask"]
CSR --> TRI["ToyRegisterInfo::getCalleeSavedRegs,<br/>getCallPreservedMask"]
AArch64's AAPCS rules are one list of actions tried in order for each value. Integer rules from that list:
CCIfType<[i1, i8, i16], CCPromoteToType<i32>>,
CCIfType<[i32], CCAssignToReg<[W0, W1, W2, W3, W4, W5, W6, W7]>>,
...
CCIfType<[i64], CCAssignToReg<[X0, X1, X2, X3, X4, X5, X6, X7]>>,
...
// If more than will fit in registers, pass them on the stack instead.
CCIfType<[i1, i8, i16, f16, bf16], CCAssignToStack<8, 8>>,
CCIfType<[i32, f32], CCAssignToStack<8, 8>>,
let Entry = 1 in def CC_AArch64_AAPCS : CallingConv<AArch64_Common>; makes the generated function visible to C++. The callee-saved set is a separate record:
def CSR_AArch64_AAPCS : CalleeSavedRegs<(add X19, X20, X21, X22, X23, X24,
-gen-register-info turns it into CSR_AArch64_AAPCS_SaveList and CSR_AArch64_AAPCS_RegMask, returned by getCalleeSavedRegs and getCallPreservedMask (see the regalloc registers page).
Sources: llvm/lib/Target/AArch64/AArch64CallingConvention.td:95-126, llvm/lib/Target/AArch64/AArch64CallingConvention.td:594
AArch64 picks a generated function by calling convention and OS:
CCAssignFn *AArch64TargetLowering::CCAssignFnForCall(CallingConv::ID CC,
bool IsVarArg) const {
switch (CC) {
...
case CallingConv::C:
case CallingConv::Fast:
...
if (!Subtarget->isTargetDarwin())
return CC_AArch64_AAPCS;A new target with one convention just names CC_Toy directly.
Sources: llvm/lib/Target/AArch64/AArch64ISelLowering.cpp:9197-9257
| Hook | Runs for | Builds |
|---|---|---|
LowerFormalArguments |
Function entry |
CopyFromReg from live-in registers; loads from fixed stack objects |
LowerCall |
Each call site |
CALLSEQ_START, copies to argument registers, stores to the stack, target call node with a register mask, CALLSEQ_END, CopyFromReg of results |
LowerReturn |
Each ret
|
Copies to return registers and a target return node |
CanLowerReturn |
Optional | Says whether the return fits in registers; if not, LLVM adds an sret pointer |
Each hook follows the same pattern: make a CCState, call Analyze* with the generated function, then walk the resulting CCValAssign list.
SmallVector<CCValAssign, 16> ArgLocs;
CCState CCInfo(CallConv, isVarArg, MF, ArgLocs, *DAG.getContext());Sources: llvm/lib/Target/AArch64/AArch64ISelLowering.cpp:9314-9335, llvm/lib/Target/AArch64/AArch64ISelLowering.cpp:10442, llvm/lib/Target/AArch64/AArch64ISelLowering.cpp:11172-11194, llvm/include/llvm/CodeGen/CallingConvLower.h:171
For a call with ten i64 arguments, llc -O2 -mtriple=aarch64-linux-gnu puts arguments 1–8 in x0–x7 and the last two on the stack (abridged):
mov w8, #9 // =0x9
mov w9, #8 // =0x8
mov w1, #1 // =0x1
...
stp x8, x30, [sp, #8] // 8-byte Folded Spill
str x9, [sp]
bl calleeThe calling conventions walkthroughs show the full output and the MIR after instruction selection.
Illustrative, not LLVM source.
// ToyCallingConv.td
def CC_Toy : CallingConv<[
CCIfType<[i1, i8, i16], CCPromoteToType<i32>>,
CCIfType<[i32], CCAssignToReg<[R0, R1, R2, R3]>>,
CCIfType<[i32], CCAssignToStack<4, 4>>
]>;
def RetCC_Toy : CallingConv<[
CCIfType<[i32], CCAssignToReg<[R0, R1]>>
]>;
def CSR_Toy : CalleeSavedRegs<(add R8, R9, R10, R11, LR)>;
A full LowerFormalArguments, LowerCall and LowerReturn for this convention is on the calling conventions custom backend page.
Every function goes through LowerFormalArguments and LowerReturn, so those two are needed before even the first function compiles. LowerCall is only reached when the function contains a call, so it can come second. The defaults in TargetLowering do not report a useful error:
virtual SDValue
LowerCall(CallLoweringInfo &/*CLI*/,
SmallVectorImpl<SDValue> &/*InVals*/) const {
llvm_unreachable("Not Implemented");
}With assertions enabled this prints Not Implemented and aborts; in a release build llvm_unreachable is undefined behaviour, so a missing hook can crash anywhere. Build LLVM with -DLLVM_ENABLE_ASSERTIONS=ON while bringing up a target.
Sources: llvm/include/llvm/CodeGen/TargetLowering.h:4893-4898, llvm/include/llvm/CodeGen/TargetLowering.h:5187-5191, llvm/include/llvm/CodeGen/TargetLowering.h:5215-5222
- 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