0%

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.

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
    3. Replacing GNU Make with Ninja
    4. Avoiding the use of the BFD linker
    5. Tweaking CMake arguments
    6. Choosing the right build type
    7. Avoiding building all targets
    8. Building as shared libraries
    9. Splitting the debug info
    10. Building an optimized version of llvm-tblgen
    11. Using the new PassManager and Clang
    12. Using GN for a faster turnaround time
    13. Summary
    14. Further reading
  8. Chapter 2: Exploring LLVM's Build System Features
    1. Technical requirements
    2. Exploring a glossary of LLVM's important CMake directives
    3. Using the CMake function to add new libraries
    4. Using the CMake function to add executables and tools
    5. Using the CMake function to add Pass plugins
    6. Understanding CMake integration for out-of-tree projects
    7. Summary
  9. Chapter 3: Testing with LLVM LIT
    1. Technical requirements
    2. Using LIT in out-of-tree projects
    3. Preparing for our example project
    4. Writing LIT configurations
    5. LIT internals
    6. Learning useful FileCheck tricks
    7. Preparing for our example project
    8. Writing FileCheck directives
    9. Exploring the TestSuite framework
    10. Preparing for our example project
    11. Importing code into llvm-test-suite
    12. Summary
    13. Further reading
  10. Chapter 4: TableGen Development
    1. Technical requirements
    2. Introduction to TableGen syntax
    3. Layout and records
    4. Bang operators
    5. Multiclass
    6. The DAG data type
    7. Writing a donut recipe in TableGen
    8. Printing a recipe via the TableGen backend
    9. TableGen's high-level workflow
    10. Writing the TableGen backend
    11. Integrating the RecipePrinter TableGen backend
    12. Summary
    13. 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
    3. Driver
    4. LLVM, assemblers, and linkers
    5. Exploring Clang's tooling features and extension options
    6. The FrontendAction class
    7. Clang plugins
    8. LibTooling and Clang Tools
    9. Summary
    10. Further reading
  13. Chapter 6: Extending the Preprocessor
    1. Technical requirements
    2. Working with SourceLocation and SourceManager
    3. Introducing SourceLocation
    4. Introducing SourceManager
    5. Learning preprocessor and lexer essentials
    6. Understanding the role of the preprocessor and lexer in Clang
    7. Understanding Token
    8. Handling macros
    9. Developing custom preprocessor plugins and callbacks
    10. The project goal and preparation
    11. Implementing a custom pragma handler
    12. Implementing custom preprocessor callbacks
    13. Summary
    14. Exercises
  14. Chapter 7: Handling AST
    1. Technical requirements
    2. Learning about AST in Clang
    3. In-memory structure of Clang AST
    4. Types in Clang AST
    5. ASTMatcher
    6. Writing AST plugins
    7. Project overview
    8. Printing diagnostic messages
    9. Creating the AST plugin
    10. 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
    4. Project overview
    5. Declaring custom driver flags
    6. Translating custom driver flags
    7. Passing flags to the frontend
    8. Adding a custom toolchain
    9. Project overview
    10. Creating the toolchain and adding a custom include path
    11. Creating a custom assembling stage
    12. Creating a custom linking stage
    13. Verifying the custom toolchain
    14. Summary
    15. 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
    3. Project overview
    4. Writing the StrictOpt Pass
    5. Working with the new AnalysisManager
    6. Overview of the project
    7. Writing the HaltAnalyzer Pass
    8. Learning instrumentations in the new PassManager
    9. Printing Pass pipeline details
    10. Printing changes to the IR after each Pass
    11. Bisecting the Pass pipeline
    12. Summary
    13. Questions
  18. Chapter 10: Processing LLVM IR
    1. Technical requirements
    2. Learning LLVM IR basics
    3. Iterating different IR units
    4. Iterating instructions
    5. Iterating basic blocks
    6. Iterating the call graph
    7. Learning about GraphTraits
    8. Working with values and instructions
    9. Understanding SSA
    10. Working with values
    11. Working with instructions
    12. Working with loops
    13. Learning about loop representation in LLVM
    14. Learning about loop infrastructure in LLVM
    15. Summary
  19. Chapter 11: Gearing Up with Support Utilities
    1. Technical requirements
    2. Printing diagnostic messages
    3. Collecting statistics
    4. Using the Statistic class
    5. Using an optimization remark
    6. Adding time measurements
    7. Using the Timer class
    8. Collecting the time trace
    9. Error-handling utilities in LLVM
    10. Introducing the Error class
    11. Learning about the Expected and ErrorOr classes
    12. The Expected class
    13. The ErrorOr class
    14. Summary
  20. Chapter 12: Learning LLVM IR Instrumentation
    1. Technical requirements
    2. Developing a sanitizer
    3. An example of using an address sanitizer
    4. Creating a loop counter sanitizer
    5. Working with PGO
    6. Introduction to instrumentation-based PGO
    7. Introduction to sampling-based PGO
    8. Using profiling data analyses
    9. 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
44.204.218.79