LLVM
To quickly explore LLVM output, select any clang
compiler on Compiler Explorer and add -emit-llvm
option.
To see all optimization passes in Clang:
llvm-as < /dev/null | opt -disable-output -passes='default<O3>' -print-pipeline-passes
Result of each pass can be examined on Compiler Explorer through Add New > Opt Pipeline.
Some important passes:
sroa
- Scalar Replacement Of Aggregates, breaks up alloca instructions into smaller allocasmem2reg
– optimizes loads and stores into SSA valuessimplifycfg
– hoists common code from if/else, turn switch statement into a lookup tableinstcombine
andinstsimplify
– peephole optimizations, constant folding, fromx * 4
tox << 2
Useful articles
- Kaleidoscope: Implementing a Language with LLVM
- Adrian Sampson: LLVM for Grad Students (2015)
- A Gentle Introduction to LLVM IR · mcyoung (2023)
- LLVM's getelementptr, by example (2020)
- Understanding static single assignment forms (2020)
- How to learn compilers: LLVM Edition - Low Level Bits 🇺🇦 (2021)
- Mapping High Level Constructs to LLVM IR — Mapping High Level Constructs to LLVM IR documentation
- A Tourist’s Guide to the LLVM Source Code – Embedded in Academia (2017)
- A Complete Guide to LLVM for Programming Language Creators (2020)
- The Architecture of Open Source Applications (Volume 1) – LLVM
- GitHub - banach-space/llvm-tutor: A collection of out-of-tree LLVM passes for teaching and learning
MLIR
YouTube
- 2019 EuroLLVM Developers’ Meeting: V. Bridgers & F. Piovezan “LLVM IR Tutorial - Phis, GEPs ...” - YouTube
- 2019 LLVM Developers’ Meeting: E. Christopher & J. Doerfert “Introduction to LLVM” - YouTube
- How Compilers Work: Introduction to LLVM Passes - Andrzej Warzynski - C++ on Sea 2022 - YouTube
Books
Installing on macOS 13+
brew install llvm
Add to .zshrc
:
export PATH="/opt/homebrew/opt/llvm/bin:$PATH"
export SDKROOT="$(xcrun --show-sdk-path)"
export LIBRARY_PATH="$LIBRARY_PATH:$SDKROOT/usr/lib"
export LDFLAGS="-L/opt/homebrew/opt/llvm/lib"
export CPPFLAGS="-I/opt/homebrew/opt/llvm/include"