Repository navigation
SelectionDAG 10 tooling
Relevant source files
The following files were used as context for generating this wiki page:
This page lists the llc switches that matter for SelectionDAG work, how to read a DAG dump, a debugging recipe for bad code, and the three ways to extend selection: a pattern, a combine and a custom lowering.
cmake -G Ninja -S llvm -B build -DCMAKE_BUILD_TYPE=Release \
-DLLVM_ENABLE_ASSERTIONS=ON -DLLVM_TARGETS_TO_BUILD=AArch64
ninja -C build llc llvm-tblgen FileCheckRelease with assertions is much faster than a full Debug build and has all the -debug-only output.
| Goal | Switch | Notes |
|---|---|---|
| Print every DAG stage plus the matcher trace | -debug-only=isel |
Needs assertions. Titles: Initial selection DAG, Optimized lowered selection DAG, Type-legalized selection DAG, Optimized type-legalized selection DAG, Vector-legalized selection DAG, Vector/type-legalized selection DAG, Optimized vector-legalized selection DAG, Legalized selection DAG, Optimized legalized selection DAG, Selected selection DAG |
| Print only the DAG dumps | -debug-only=isel-dump |
ISEL_DUMP_DEBUG_TYPE is DEBUG_TYPE "-dump"
|
| One block only | -filter-view-dags=<block name> |
Also limits the graph viewers |
| Draw a stage as a graph (needs Graphviz) |
-view-dag-combine1-dags, -view-legalize-types-dags, -view-dag-combine-lt-dags, -view-legalize-dags, -view-dag-combine2-dags, -view-isel-dags, -view-sched-dags, -view-sunit-dags
|
One per phase |
| Sort nodes in dumps | -dump-sorted-dags |
Topological order, easier to diff |
| Stop after instruction selection and print MIR | -stop-after=finalize-isel |
Works in release builds; also -stop-before=..., -print-after-all
|
| Show the pass pipeline | -debug-pass=Structure |
Find aarch64-isel, finalize-isel, phi-node-elimination, register-coalescer
|
| Pick the selector |
-global-isel=0 / 1, -fast-isel, -O0..-O3
|
At -O0 AArch64 uses GlobalISel by default, with SelectionDAG fallback |
| Pick the DAG scheduler |
-pre-RA-sched=source, list-burr, list-ilp, list-hybrid, fast, linearize
|
See Scheduling and MIR Emission |
| Disable the generic combiner | -combiner-disabled |
Quick check whether a miscompile comes from a combine |
| Bisect combines | -debug-counter=dagcombine-skip=N,dagcombine-count=M |
Needs assertions; DAGCombiner::combine checks the counter |
| Features that change legality |
-mattr=+cssc, -mcpu=...
|
Changes setOperationAction results |
| Make fallbacks loud |
-global-isel-abort=1, -fast-isel-abort=1
|
Turns silent fallbacks into errors |
Sources: llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp:118, llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp:130, llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp:149-182, llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp:219, llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp:98, llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp:157-160, llvm/lib/CodeGen/SelectionDAG/ScheduleDAGRRList.cpp:70-87
t0: ch,glue = EntryToken
t2: i64,ch = CopyFromReg t0, Register:i64 %0
t4: i32 = truncate t2
t5: i32 = add nsw t4, Constant:i32<1>
t6: ch = store<(store (s32) into %ir.p)> t0, t5, t2, undef:i64
-
tNis a node, printed asresult types = opcode operands;t2:1names result 1 oft2. -
chis a chain (MVT::Other),glueis a glue edge, others are data types. - Flags such as
nswfollow the opcode; the<...>afterloadorstoreis its memory operand. -
Constant:i32<1>is anISD::Constant;TargetConstant:i32<1>is a final immediate. -
undefas the offset of a load or store is the unindexed placeholder.
flowchart TD
A["Reduce to a small .ll"] --> B["llc -O2 -stop-after=finalize-isel"]
B --> C{"MIR as expected?"}
C -- yes --> L["Look at later MIR passes<br/>print-after-all, find the first change"]
C -- no --> D["Assertion build: -debug-only=isel-dump"]
D --> E["Read Optimized legalized selection DAG"]
E --> F{"DAG shape already wrong?"}
F -- yes --> G["Compare consecutive dumps,<br/>find the combine or legalization"]
F -- no --> H["Wrong instruction chosen:<br/>search .td patterns and AddedComplexity,<br/>read the isel matcher trace"]
A new instruction pattern (selection only). Add a Pat next to similar ones in AArch64InstrInfo.td with a sensible AddedComplexity, then rebuild. The shape, from the existing file:
def : Pat<(i32 (mul GPR32:$Rn, GPR32:$Rm)),
(MADDWrrr GPR32:$Rn, GPR32:$Rm, WZR)>;
The build regenerates AArch64GenDAGISel.inc; its // Src: and // Dst: comments show how the pattern was placed in the decision tree.
A new DAG combine (optimisation). Register interest in the constructor with setTargetDAGCombine(ISD::XYZ), add case ISD::XYZ: return performXYZCombine(N, DCI, DAG); in AArch64TargetLowering::PerformDAGCombine, and write the function using DCI.isBeforeLegalize(), isBeforeLegalizeOps() and DAG.getNode(...). Always check legality when the level is past legalization, and make sure the replacement does not match your own fold again.
A new custom lowering (a generic node the target cannot select). Call setOperationAction(ISD::FOO, MVT::i32, Custom) in the constructor, add case ISD::FOO: return LowerFOO(Op, DAG); in LowerOperation, build target nodes there (for example an AArch64ISD::BAR declared by an SDNode<"AArch64ISD::BAR", ...> record in the .td files) and add a Pat that maps AArch64bar to the machine instruction. If the result type is illegal, implement ReplaceNodeResults instead.
Sources: llvm/lib/Target/AArch64/AArch64InstrInfo.td:2911-2921, llvm/lib/Target/AArch64/AArch64ISelLowering.cpp:30803-31168, llvm/lib/Target/AArch64/AArch64ISelLowering.cpp:8432-8850
Backend tests live in llvm/test/CodeGen/AArch64/ and look like ; RUN: llc < %s -mtriple=aarch64 | FileCheck %s. The script llvm/utils/update_llc_test_checks.py regenerates the CHECK lines. For MIR-level tests use -stop-after, -run-pass, or llvm/utils/update_mir_test_checks.py.
- 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