LLVM Techniques, Tips, and Best Practices Clang and Middle-End Libraries

Book description

Learn how you can build the next big programming language, compiler, or source code analyzer using LLVM and Clang

Key Features

  • Explore Clang, LLVM's middle-end and backend, in a pragmatic way
  • Develop your LLVM skillset and get to grips with a variety of common use cases
  • Engage with real-world LLVM development through various coding examples

Book Description

Every programmer or engineer, at some point in their career, works with compilers to optimize their applications. Compilers convert a high-level programming language into low-level machine-executable code. LLVM provides the infrastructure, reusable libraries, and tools needed for developers to build their own compilers. With LLVM's extensive set of tooling, you can effectively generate code for different backends as well as optimize them.

In this book, you'll explore the LLVM compiler infrastructure and understand how to use it to solve different problems. You'll start by looking at the structure and design philosophy of important components of LLVM and gradually move on to using Clang libraries to build tools that help you analyze high-level source code. As you advance, the book will show you how to process LLVM IR – a powerful way to transform and optimize the source program for various purposes. Equipped with this knowledge, you'll be able to leverage LLVM and Clang to create a wide range of useful programming language tools, including compilers, interpreters, IDEs, and source code analyzers.

By the end of this LLVM book, you'll have developed the skills to create powerful tools using the LLVM framework to overcome different real-world challenges.

What you will learn

  • Find out how LLVM's build system works and how to reduce the building resource
  • Get to grips with running custom testing with LLVM's LIT framework
  • Build different types of plugins and extensions for Clang
  • Customize Clang's toolchain and compiler flags
  • Write LLVM passes for the new PassManager
  • Discover how to inspect and modify LLVM IR
  • Understand how to use LLVM's profile-guided optimizations (PGO) framework
  • Create custom compiler sanitizers

Who this book is for

This book is for software engineers of all experience levels who work with LLVM. If you are an academic researcher, this book will help you learn useful LLVM skills in a short time and enable you to build your prototypes and projects quickly. Programming language enthusiasts will also find this book useful for building a new programming language with the help of LLVM.

Publisher resources

Download Example Code

Table of contents

  1. LLVM Techniques, Tips, and Best Practices Clang and Middle-End Libraries
  2. Contributors
  3. About the author
  4. About the reviewer
  5. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
    4. Download the example code files
    5. Download the color images
    6. Conventions used
    7. Get in touch
    8. Reviews
  6. Section 1: Build System and LLVM-Specific Tooling
  7. Chapter 1: Saving Resources When Building LLVM
    1. Technical requirements
    2. Cutting down building resources with better tooling
      1. Replacing GNU Make with Ninja
      2. Avoiding the use of the BFD linker
    3. Tweaking CMake arguments
      1. Choosing the right build type
      2. Avoiding building all targets
      3. Building as shared libraries
      4. Splitting the debug info
      5. Building an optimized version of llvm-tblgen
      6. Using the new PassManager and Clang
    4. Using GN for a faster turnaround time
    5. Summary
    6. Further reading
  8. Chapter 2: Exploring LLVM's Build System Features
    1. Technical requirements
    2. Exploring a glossary of LLVM's important CMake directives
      1. Using the CMake function to add new libraries
      2. Using the CMake function to add executables and tools
      3. Using the CMake function to add Pass plugins
    3. Understanding CMake integration for out-of-tree projects
    4. Summary
  9. Chapter 3: Testing with LLVM LIT
    1. Technical requirements
    2. Using LIT in out-of-tree projects
      1. Preparing for our example project
      2. Writing LIT configurations
      3. LIT internals
    3. Learning useful FileCheck tricks
      1. Preparing for our example project
      2. Writing FileCheck directives
    4. Exploring the TestSuite framework
      1. Preparing for our example project
      2. Importing code into llvm-test-suite
    5. Summary
    6. Further reading
  10. Chapter 4: TableGen Development
    1. Technical requirements
    2. Introduction to TableGen syntax
      1. Layout and records
      2. Bang operators
      3. Multiclass
      4. The DAG data type
    3. Writing a donut recipe in TableGen
    4. Printing a recipe via the TableGen backend
      1. TableGen's high-level workflow
      2. Writing the TableGen backend
      3. Integrating the RecipePrinter TableGen backend
    5. Summary
    6. Further reading
  11. Section 2: Frontend Development
  12. Chapter 5: Exploring Clang's Architecture
    1. Technical requirements
    2. Learning Clang's subsystems and their roles
      1. Driver
      2. LLVM, assemblers, and linkers
    3. Exploring Clang's tooling features and extension options
      1. The FrontendAction class
      2. Clang plugins
      3. LibTooling and Clang Tools
    4. Summary
    5. Further reading
  13. Chapter 6: Extending the Preprocessor
    1. Technical requirements
    2. Working with SourceLocation and SourceManager
      1. Introducing SourceLocation
      2. Introducing SourceManager
    3. Learning preprocessor and lexer essentials
      1. Understanding the role of the preprocessor and lexer in Clang
      2. Understanding Token
      3. Handling macros
    4. Developing custom preprocessor plugins and callbacks
      1. The project goal and preparation
      2. Implementing a custom pragma handler
      3. Implementing custom preprocessor callbacks
    5. Summary
    6. Exercises
  14. Chapter 7: Handling AST
    1. Technical requirements
    2. Learning about AST in Clang
      1. In-memory structure of Clang AST
      2. Types in Clang AST
      3. ASTMatcher
    3. Writing AST plugins
      1. Project overview
      2. Printing diagnostic messages
      3. Creating the AST plugin
    4. Summary
  15. Chapter 8: Working with Compiler Flags and Toolchains
    1. Technical requirements
    2. Understanding drivers and toolchains in Clang
    3. Adding custom driver flags
      1. Project overview
      2. Declaring custom driver flags
      3. Translating custom driver flags
      4. Passing flags to the frontend
    4. Adding a custom toolchain
      1. Project overview
      2. Creating the toolchain and adding a custom include path
      3. Creating a custom assembling stage
      4. Creating a custom linking stage
      5. Verifying the custom toolchain
    5. Summary
    6. Exercises
  16. Section 3: "Middle-End" Development
  17. Chapter 9: Working with PassManager and AnalysisManager
    1. Technical requirements
    2. Writing an LLVM Pass for the new PassManager
      1. Project overview
      2. Writing the StrictOpt Pass
    3. Working with the new AnalysisManager
      1. Overview of the project
      2. Writing the HaltAnalyzer Pass
    4. Learning instrumentations in the new PassManager
      1. Printing Pass pipeline details
      2. Printing changes to the IR after each Pass
      3. Bisecting the Pass pipeline
    5. Summary
    6. Questions
  18. Chapter 10: Processing LLVM IR
    1. Technical requirements
    2. Learning LLVM IR basics
      1. Iterating different IR units
      2. Iterating instructions
      3. Iterating basic blocks
      4. Iterating the call graph
      5. Learning about GraphTraits
    3. Working with values and instructions
      1. Understanding SSA
      2. Working with values
      3. Working with instructions
    4. Working with loops
      1. Learning about loop representation in LLVM
      2. Learning about loop infrastructure in LLVM
    5. Summary
  19. Chapter 11: Gearing Up with Support Utilities
    1. Technical requirements
    2. Printing diagnostic messages
    3. Collecting statistics
      1. Using the Statistic class
      2. Using an optimization remark
    4. Adding time measurements
      1. Using the Timer class
      2. Collecting the time trace
    5. Error-handling utilities in LLVM
      1. Introducing the Error class
    6. Learning about the Expected and ErrorOr classes
      1. The Expected class
      2. The ErrorOr class
    7. Summary
  20. Chapter 12: Learning LLVM IR Instrumentation
    1. Technical requirements
    2. Developing a sanitizer
      1. An example of using an address sanitizer
      2. Creating a loop counter sanitizer
    3. Working with PGO
      1. Introduction to instrumentation-based PGO
      2. Introduction to sampling-based PGO
      3. Using profiling data analyses
    4. Summary
  21. Assessments
    1. Chapter 6, Extending the Preprocessor
    2. Chapter 8, Working with Compiler Flags and Toolchains
    3. Chapter 9, Working with PassManager and AnalysisManager
    4. Why subscribe?
  22. Other Books You May Enjoy
    1. Packt is searching for authors like you
    2. Leave a review - let other readers know what you think

Product information

  • Title: LLVM Techniques, Tips, and Best Practices Clang and Middle-End Libraries
  • Author(s): Min-Yih Hsu
  • Release date: April 2021
  • Publisher(s): Packt Publishing
  • ISBN: 9781838824952