Repository navigation
IR 03 types
Relevant source files
The following files were used as context for generating this wiki page:
Every value in LLVM IR has a type, and every instruction states the types it works on. Types are simple and structural: there are no classes, no signed versus unsigned integers, and only one kind of pointer. This page lists the types a beginner meets and how they are written.
This page covers integer, floating-point, pointer, vector, array, struct, function, void and label types, and how they are represented in C++ by Type and its subclasses. How instructions use these types is covered in Arithmetic and Memory.
Each Type object has a TypeID that says which kind it is. The enum is in Type.h:
| Kind | Written as | TypeID |
C++ class |
|---|---|---|---|
| Integer |
i1, i8, i32, i64, any iN
|
IntegerTyID |
IntegerType |
| Floating point |
half, bfloat, float, double, fp128
|
HalfTyID, FloatTyID, DoubleTyID, ... |
Type |
| Pointer |
ptr, ptr addrspace(N)
|
PointerTyID |
PointerType |
| Fixed vector |
<4 x i32>, <2 x double>
|
FixedVectorTyID |
FixedVectorType |
| Scalable vector | <vscale x 4 x i32> |
ScalableVectorTyID |
ScalableVectorType |
| Array | [10 x i32] |
ArrayTyID |
ArrayType |
| Struct |
{ i32, ptr }, %struct.Point
|
StructTyID |
StructType |
| Function | i32 (i32, ptr) |
FunctionTyID |
FunctionType |
| Void | void |
VoidTyID |
Type |
| Label |
label (a basic block) |
LabelTyID |
Type |
Type provides predicates such as isIntegerTy(), isPointerTy(), isStructTy() and isArrayTy() that test the TypeID.
Sources: llvm/include/llvm/IR/Type.h:46-79, llvm/include/llvm/IR/Type.h:257-282
i32 is just 32 bits. Whether those bits are signed is decided by the instruction: sdiv versus udiv, icmp slt versus icmp ult, sext versus zext. C int and unsigned both become i32.
i1 is the boolean type: comparisons produce it and conditional branches consume it.
; illustrative
%q1 = sdiv i32 %a, %b ; signed division
%q2 = udiv i32 %a, %b ; unsigned division of the same bits
%lt = icmp slt i32 %a, %b ; i1 resultSources: llvm/include/llvm/IR/DerivedTypes.h:42
Older LLVM had typed pointers such as i32*. Current LLVM has one pointer type, ptr, that says nothing about what it points to. The type of the data comes from the instruction that accesses it:
; illustrative
%x = load i32, ptr %p ; read 4 bytes as i32
%y = load double, ptr %p ; read 8 bytes as double from the same pointerOnly the address space can differ (ptr addrspace(1) is common on GPUs). If you read old tutorials, mentally replace every T* with ptr and remove the bitcasts between pointer types.
Sources: llvm/include/llvm/IR/DerivedTypes.h:758
| Type | Example | Element access |
|---|---|---|
| Array | [4 x float] |
Same type for every element; index can be a variable in getelementptr
|
| Struct (literal) | { i32, double } |
Fields can differ; field index must be a constant |
| Struct (identified) | %struct.Point = type { i32, i32 } |
Same, but has a name and can be recursive through ptr
|
| Packed struct | <{ i8, i32 }> |
No padding between fields |
; illustrative
%struct.Node = type { i32, ptr } ; int value; struct Node *next
@table = global [3 x i32] [i32 1, i32 2, i32 3]How these map to byte offsets (alignment and padding) is decided by the module's target datalayout string. See Memory for the getelementptr arithmetic.
Sources: llvm/include/llvm/IR/DerivedTypes.h:278, llvm/include/llvm/IR/DerivedTypes.h:458
A vector type holds a fixed number of scalars that one instruction processes together, for SIMD:
; illustrative
%s = add <4 x i32> %a, %b ; four adds at once
%e = extractelement <4 x i32> %s, i32 0 ; take lane 0Scalable vectors, <vscale x 4 x i32>, have a length that is a multiple of a runtime constant vscale; they model AArch64 SVE and RISC-V V.
Sources: llvm/include/llvm/IR/DerivedTypes.h:490, llvm/include/llvm/IR/DerivedTypes.h:650
A function type is a return type and a list of parameter types, optionally variadic:
; illustrative
declare i32 @printf(ptr, ...) ; type: i32 (ptr, ...)
declare void @free(ptr) ; type: void (ptr)Functions themselves are values of type ptr; the function type is stored separately and used by call.
Sources: llvm/include/llvm/IR/DerivedTypes.h:165
Types are created once per LLVMContext and shared: there is exactly one i32 object, so passes compare types by pointer, A->getType() == B->getType(). In C++ you get them with calls such as Type::getInt32Ty(Ctx), PointerType::getUnqual(Ctx) and ArrayType::get(Elt, N).
- Integers carry no sign; instructions decide signedness.
- There is one pointer type,
ptr; loads and stores state the accessed type. - Struct and array layout comes from the data layout string.
- 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