To get the most out of this book
The chapters are self-contained and, aside from the compiler concepts introduced in Chapters 3 and 4, you should be able to jump to any of them and get started. If you are new to the codegen space, we still recommend that you follow the chapters in order as they follow the natural flow of a compiler backend from the input IR progressively lowering it to assembly code. If you are new to compilers, we still recommend that you read at least Chapters 3 and 4 first.
For people with a compiler background but new to backends or wanting to refresh or refine their knowledge of the backends, you can use the Quiz time sections at the end of each chapter to first check your knowledge against what the chapter covers to decide whether the chapter may cover something that you may not know before reading it.
Finally, we are firm believers that practicing something helps to digest it; therefore, we recommend you spend time doing the coding exercises and implement your own solutions without looking at the provided solutions.
Software covered in the book |
Operating system requirements |
LLVM 20 |
Windows, macOS, or Linux |
If you are using the digital version of this book, we advise you to type the code yourself or access the code from the book’s GitHub repository (a link is available in the next section). Doing so will help you avoid any potential errors related to the copying and pasting of code.
Note that the code snippets and APIs are based on the open source release of LLVM 20.1.1 from February 2025. All the code has been tested solely on macOS but should work equally well on Windows and Linux. The code snippets are also available for the LLVM 19.0.1 release from September 2024 in the release_llvm_19_0_1
branch of the repositories listed in the next section.
Download the example code files
You can download the example code files and exercises for this book from GitHub at https://github.com/PacktPublishing/LLVM-Code-Generation. If there’s an update to the code, it will be updated in the GitHub repository. Additionally, the book comes with a second companion repository at https://github.com/PacktPublishing/LLVM-Code-Generation-by-example. This is a fork of LLVM that is used throughout the book to illustrate how to develop a full backend.
We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!
Download the color images
We also provide a PDF file that has color images of the screenshots/diagrams used in this book. You can download it here: https://packt.link/gbp/9781837637782.
Conventions used
There are a number of text conventions used throughout this book.
Code in text
: Indicates code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and X/Twitter handles. Here is an example: “Since the SDISel framework is a single MachineFunctionPass
instance, the only way to augment it is through the hooks provided by the various TargetXXX
classes of your backend.”
A block of code is set as follows:
SelectionDAG has 9 nodes:
t0: ch,glue = EntryToken
t2: i16,ch = CopyFromReg t0, Register:i16 %0
t4: i16,ch = CopyFromReg t0, Register:i16 %1
t5: i16 = add t2, t4
t7: ch,glue = CopyToReg t0, Register:i16 $r1, t5
t8: ch = H2BLBISD::RETURN_GLUE t7, Register:i16 $r1, t7:1
Warnings or important notes appear like this.
Tips and tricks appear like this.