Table Of ContentLLVM Techniques,
Tips, and Best
Practices Clang and
Middle-End Libraries
Design powerful and reliable compilers using
the latest libraries and tools from LLVM
Min-Yih Hsu
BIRMINGHAM—MUMBAI
LLVM Techniques, Tips, and Best Practices
Clang and Middle-End Libraries
Copyright © 2021 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 author(s), 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: Aaron Lazar
Publishing Product Manager: Shweta Bairoliya
Senior Editor: Nitee Shetty
Content Development Editor: Kinnari Chohan
Technical Editor: Pradeep Sahu
Copy Editor: Safis Editing
Project Coordinator: Deeksha Thakkar
Proofreader: Safis Editing
Indexer: Tejal Daruwale Soni
Production Designer: Jyoti Chauhan
First published: April 2021
Production reference: 1220421
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham
B3 2PB, UK.
ISBN 978-1-83882-495-2
www.packt.com
To my parents.
– Min-Yih Hsu
Contributors
About the author
Min-Yih "Min" Hsu is a Ph.D. candidate in computer science at the University of
California, Irvine. His research focuses on compiler engineering, code optimization,
advanced hardware architectures, and system security. He has been an active member of
the LLVM community since 2015 and has contributed numerous patches upstream. Min
also dedicates his time to advocating LLVM and compiler engineering through various
avenues, such as writing blog posts and giving talks at conferences. In his spare time, Min
likes to explore a variety of different coffee beans and ways of brewing.
I want to thank the people who have supported me, especially my family
and my academic advisors. I also want to thank the LLVM community
for always being inclusive and kind to every member regardless of their
background and origin.
About the reviewer
Suyog Sarda completed his B.Tech from the College of Engineering, Pune. His work so far
has mostly been related to compilers. He is especially interested in the performance aspect
of compilers. He has worked on domain-specific language for image processing for DSPs.
LLVM's modularity makes it interesting to learn and implement quickly according to the
compiler's requirement. However, the documentation of LLVM is scattered. He hopes this
book provides a consolidated overview of the LLVM compiler infrastructure.
Table of Contents
Preface
Section 1:
Build System and LLVM-Specific Tooling
1
Saving Resources When Building LLVM
Technical requirements 4 Building as shared libraries 9
Cutting down building Splitting the debug info 9
resources with Building an optimized version of llvm-
better tooling 5 tblgen 10
Using the new PassManager and Clang 10
Replacing GNU Make with Ninja 5
Avoiding the use of the BFD linker 6 Using GN for a faster
turnaround time 11
Tweaking CMake arguments 7
Summary 13
Choosing the right build type 7
Further reading 14
Avoiding building all targets 8
2
Exploring LLVM's Build System Features
Technical requirements 16 Using the CMake function to add Pass
plugins 20
Exploring a glossary of LLVM's
important CMake directives 16
Understanding CMake
Using the CMake function to add new integration for
libraries 16 out-of-tree projects 21
Using the CMake function to add
Summary 24
executables and tools 20
ii Table of Contents
3
Testing with LLVM LIT
Technical requirements 26 Writing FileCheck directives 36
Using LIT in out-of-tree projects 26
Exploring the TestSuite
Preparing for our example project 27 framework 42
Writing LIT configurations 29
Preparing for our example project 42
LIT internals 32
Importing code into llvm-test-suite 43
Learning useful FileCheck tricks 34
Summary 46
Preparing for our example project 34
Further reading 46
4
TableGen Development
Technical requirements 48 Printing a recipe via the
Introduction to TableGen syntax 48 TableGen backend 60
Layout and records 50 TableGen's high-level workflow 62
Bang operators 51 Writing the TableGen backend 63
Multiclass 52 Integrating the RecipePrinter TableGen
backend 67
The DAG data type 53
Summary 70
Writing a donut recipe in
TableGen 55 Further reading 70
Section 2:
Frontend Development
5
Exploring Clang's Architecture
Technical requirements 74 LLVM, assemblers, and linkers 80
Learning Clang's subsystems
Exploring Clang's tooling
and their roles 75
features and extension options 81
Driver 76
Table of Contents iii
The FrontendAction class 81 Summary 85
Clang plugins 82 Further reading 85
LibTooling and Clang Tools 83
6
Extending the Preprocessor
Technical requirements 88 Developing custom
Working with SourceLocation preprocessor plugins and
and SourceManager 89 callbacks 99
Introducing SourceLocation 89 The project goal and preparation 100
Introducing SourceManager 90 Implementing a custom pragma
handler 103
Learning preprocessor and Implementing custom preprocessor
lexer essentials 91 callbacks 105
Understanding the role of the
Summary 108
preprocessor and lexer in Clang 92
Exercises 109
Understanding Token 93
Handling macros 97
7
Handling AST
Technical requirements 112 Project overview 125
Learning about AST in Clang 113 Printing diagnostic messages 128
Creating the AST plugin 132
In-memory structure of Clang AST 113
Types in Clang AST 116 Summary 144
ASTMatcher 118
Writing AST plugins 125
8
Working with Compiler Flags and Toolchains
Technical requirements 146 Project overview 150
Understanding drivers and Declaring custom driver flags 152
toolchains in Clang 147 Translating custom driver flags 155
Passing flags to the frontend 161
Adding custom driver flags 150