0% found this document useful (0 votes)
7K views10 pages

Nacke, Kai, Kwan, Amy - Learn LLVM 17 - A Beginner's Guide To Learning LLVM Compiler Tools and Core Libraries With C++-Packt (2023)

Uploaded by

Almir Polverini
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7K views10 pages

Nacke, Kai, Kwan, Amy - Learn LLVM 17 - A Beginner's Guide To Learning LLVM Compiler Tools and Core Libraries With C++-Packt (2023)

Uploaded by

Almir Polverini
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

权归Packt Publishing所有

Learn LLVM 17

A beginner’s guide to learning LLVM compiler tools


and core libraries with C++

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.

Group Product Manager: Kunal Sawant


Publishing Product Manager: Teny Thomas
Book Project Manager: Prajakta Naik
Senior Editor: Ruvika Rao and Nithya Sadanandan
Technical Editor: Jubit Pincy
Copy Editor: Safis Editing
Indexer: Pratik Shirodkar
Production Designer: Vijay Kamble
DevRel Marketing Coordinator: Shrinidhi Manoharan
Business Development Executive: Kriti Sharma

First published: April 2021


Second published: January 2024

Production reference: 1271223

Published by Packt Publishing Ltd.


Grosvenor House
11 St Paul’s Square
Birmingham
B3 1R.

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

About the authors


Kai Nacke is a professional IT architect currently residing in Toronto, Canada. He holds a diploma
in computer science from the Technical University of Dortmund, Germany. and his diploma thesis
on universal hash functions was recognized as the best of the semester.
With over 20 years of experience in the IT industry, Kai has extensive expertise in the development
and architecture of business and enterprise applications. In his current role, he evolves an
LLVM/clang-based compiler.
For several years, Kai served as the maintainer of LDC, the LLVM-based D compiler. He is the
author of D Web Development and Learn LLVM 12, both published by Packt. In the past, he was a
speaker in the LLVM developer room at the Free and Open Source Software Developers’ European
Meeting (FOSDEM).

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

Part 1: The Basics of Compiler Construction


with LLVM
1
Installing LLVM 3
Compiling LLVM versus Cloning the repository 8
installing binaries 3 Creating a build directory 9
Getting the prerequisites ready 4 Generating the build system files 9
Ubuntu 5 Compiling and installing LLVM 10
Fedora and RedHat 5 Customizing the build process 11
FreeBSD 5 Variables defined by CMake 12
OS X 6 Using LLVM-defined build configuration
Windows 6 variables 14
Cloning the repository and building Summary 17
from source 7
Configuring Git 7

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

Syntactical analysis 26 Generating code with the LLVM


A hand-written parser 27 backend 38
The abstract syntax tree 33 Textual representation of LLVM IR 38
Generating the IR from the AST 40
Semantic analysis 36
The missing pieces – the driver and the
runtime library 44

Summary 48

Part 2: From Source to Machine Code Generation


3
Turning the Source File into an Abstract Syntax Tree 51
Defining a real programming Constructing a recursive
language 52 descent parser 65
Creating the project layout 55 Performing semantic analysis 70
Managing the input files for Handling the scope of names 71
the compiler 56 Using an LLVM-style RTTI for the AST 74
Handling messages for the user 56 Creating the semantic analyzer 75

Structuring the lexer 59 Summary 81

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

Initializing the target machine class 104 Summary 110


Emitting assembler text and object code 106

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

You might also like