Nacke, Kai, Kwan, Amy - Learn LLVM 17 - A Beginner's Guide To Learning LLVM Compiler Tools and Core Libraries With C++-Packt (2023)
Nacke, Kai, Kwan, Amy - Learn LLVM 17 - A Beginner's Guide To Learning LLVM Compiler Tools and Core Libraries With C++-Packt (2023)
Learn LLVM 17
Kai Nacke
Amy Kwan
BIRMINGHAM—MUMBAI
Learn LLVM 17
Copyright © 2024 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted
in any form or by any means, without the prior written permission of the publisher, except in the case
of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information
presented. However, the information contained in this book is sold without warranty, either express
or implied. Neither the authors, nor Packt Publishing or its dealers and distributors, will be held liable
for any damages caused or alleged to have been caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and
products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot
guarantee the accuracy of this information.
ISBN 978-1-83763-134-6
www.packtpub.com
Writing a book takes time and energy. Without the support and understanding of my wife, Tanya,
and my daughter Polina, this book would not have been possible. Thank you both for always
encouraging me!
Because of some personal challenges, this project was at risk, and I am grateful to Amy for joining
me as an author. Without her, the book would not be as good as it is now.
Once again, the team at Packt not only provided guidance on my writing but also showed
an understanding of my slow writing, and always motivated me to carry on. I owe them
a great thank you.
- Kai Nacke
2023 has been a very transformative year for me, and contributing my knowledge of LLVM to this
book has been one of the reasons why this year has been so significant. I never would have thought
that I would be approached by Kai to embark on this exciting journey to share LLVM 17 with you all!
Thank you to Kai, for his technical mentorship and guidance, the team at Packt, and, of course, to my
family and close loved ones for providing me with the support and motivation in writing this book.
- Amy Kwan
Contributors
Amy Kwan is a compiler developer currently residing in Toronto, Canada. Originally, from the Canadian
prairies, Amy holds a Bachelor of Science in Computer Science from the University of Saskatchewan.
In her current role, she leverages LLVM technology as a backend compiler developer. Previously, Amy
has been a speaker at the LLVM Developer Conference in 2022 alongside Kai Nacke.
About the reviewers
Akash Kothari is a Research Assistant at the Illinois LLVM Compiler Research Lab. He earned his
Ph.D. in Computer Science from the University of Illinois at Urbana-Champaign. Specializing in
performance engineering, program synthesis, and formal semantics and verification, Akash’s interests
extend to exploring the history of computing and programming systems.
Shuo Niu, a Master of Engineering in computer engineering, is a dynamic force in the realm of
compiler technology. With five prolific years at Intel PSG specializing in FPGA HLD compilers, he led
innovations in the compiler middle-end optimizer. His expertise in developing cutting-edge features
has empowered users to achieve remarkable performance enhancements on FPGA boards.
Table of Contents
Preface xiii
2
The Structure of a Compiler 19
Building blocks of a compiler 19 How does grammar help the compiler writer? 21
An arithmetic expression language 20 Lexical analysis 21
Formalism for specifying the syntax of a A hand-written lexer 22
programming language 20
viii Table of Contents
Summary 48
4
Basics of IR Code Generation 83
Generating IR from the AST 83 Sealing a block 96
Understanding the IR code 84 Creating the IR code for expressions 97
Learning about the load-and-store approach 88 Emitting the IR code for a function 98
Mapping the control flow to basic blocks 89 Controlling visibility with linkage and name
mangling 98
Using AST numbering to generate Converting a type from an AST description
IR code in SSA form 91 into LLVM types 99
Defining the data structure to hold values 92 Creating the LLVM IR function 100
Reading and writing values local to Emitting the function body 102
a basic block 92
Searching the predecessor blocks for a value 93
Setting up the module and the driver 103
Optimizing the generated phi instructions 95 Wrapping all in the code generator 103
Table of Contents ix
5
IR Generation for High-Level Language Constructs 111
Technical requirements 111 Creating IR code for classes and
Working with arrays, structs, virtual functions 118
and pointers 112 Implementing single inheritance 118
Getting the application binary Extending single inheritance with interfaces 122
interface right 116 Adding support for multiple inheritance 123
Summary 126
6
Advanced IR Generation 127
Throwing and catching exceptions 127 Creating TBAA metadata in LLVM 141
Raising an exception 132 Adding TBAA metadata to tinylang 141
Catching an exception 135 Adding debug metadata 146
Integrating the exception handling code into
Understanding the general structure
the application 137
of debug metadata 146
Generating metadata for type-based Tracking variables and their values 150
alias analysis 138 Adding line numbers 153
Understanding the need for additional Adding debug support to tinylang 153
metadata 139
Summary 160
7
Optimizing IR 161
Technical requirements 161 Adding an optimization pipeline
The LLVM pass manager 162 to your compiler 178
Implementing a new pass 163 Creating an optimization pipeline 178
Extending the pass pipeline 184
Developing the ppprofiler pass as a plugin 163
Adding the pass to the LLVM source tree 168 Summary 187
Using the ppprofiler pass with
LLVM tools 171