๐”– Scriptorium
โœฆ   LIBER   โœฆ

๐Ÿ“

Learn LLVM 17: A beginner's guide to learning LLVM compiler tools and core libraries with C++

โœ Scribed by Nacke, Kai, Kwan, Amy


Publisher
Packt
Year
2023
Tongue
English
Leaves
416
Category
Library

โฌ‡  Acquire This Volume

No coin nor oath required. For personal study only.

โœฆ Synopsis


Learn how to build and use the complete spectrum of real-world compilers, including the frontend, optimization pipeline, and a new backend by leveraging the power of LLVM core libraries

Key Features

Get to grips with using LLVM libraries step by step

Understand the high-level design of LLVM compilers and apply these principles to your own compiler

Add a new backend to target an unsupported CPU architecture

Purchase of the print or Kindle book includes a free PDF eBook

Book Description

LLVM was built to bridge the gap between the theoretical knowledge found in compiler textbooks and the practical demands of compiler development. With a modular codebase and advanced tools, LLVM empowers developers to build compilers with ease. This book serves as a practical introduction to LLVM, guiding you progressively through complex scenarios and ensuring that you navigate the challenges of building and working with compilers like a pro.

The book starts by showing you how to configure, build, and install LLVM libraries, tools, and external projects. Youโ€™ll then be introduced to LLVM's design, unraveling its applications in each compiler stage: frontend, optimizer, and backend. Using a real programming language subset, you'll build a frontend, generate LLVM IR, optimize it through the pipeline, and generate machine code. Advanced chapters extend your expertise, covering topics such as extending LLVM with a new pass, using LLVM tools for debugging, and enhancing the quality of your code. You'll also focus on just-in-time compilation issues and the current state of JIT-compilation support with LLVM. Finally, youโ€™ll develop a new backend for LLVM, gaining insights into target description and how instruction selection works.

By the end of this book, you'll have hands-on experience with the LLVM compiler development framework through real-world examples and source code snippets.

What you will learn

Configure, compile, and install the LLVM framework

Understand how the LLVM

โœฆ Table of Contents


Cover
Title Page
Copyright and Credits
Contributors
Table of Contents
Preface
Part 1: The Basics of Compiler Construction with LLVM
Chapter 1: Installing LLVM
Compiling LLVM versus installing binaries
Getting the prerequisites ready
Ubuntu
Fedora and RedHat
FreeBSD
OS X
Windows
Cloning the repository and building from source
Configuring Git
Cloning the repository
Creating a build directory
Generating the build system files
Compiling and installing LLVM
Customizing the build process
Variables defined by CMake
Using LLVM-defined build configuration variables
Summary
Chapter 2: The Structure of a Compiler
Building blocks of a compiler
An arithmetic expression language
Formalism for specifying the syntax of a programming language
How does grammar help the compiler writer?
Lexical analysis
A hand-written lexer
Syntactical analysis
A hand-written parser
The abstract syntax tree
Semantic analysis
Generating code with the LLVM backend
Textual representation of LLVM IR
Generating the IR from the AST
The missing pieces โ€“ the driver and the runtime library
Summary
Part 2: From Source to Machine Code Generation
Chapter 3: Turning the Source File into an Abstract Syntax Tree
Defining a real programming language
Creating the project layout
Managing the input files for the compiler
Handling messages for the user
Structuring the lexer
Constructing a recursive descent parser
Performing semantic analysis
Handling the scope of names
Using an LLVM-style RTTI for the AST
Creating the semantic analyzer
Summary
Chapter 4: Basics of IR Code Generation
Generating IR from the AST
Understanding the IR code
Learning about the load-and-store approach
Mapping the control flow to basic blocks
Using AST numbering to generate IR code in SSA form
Defining the data structure to hold values
Reading and writing values local to a basic block
Searching the predecessor blocks for a value
Optimizing the generated phi instructions
Sealing a block
Creating the IR code for expressions
Emitting the IR code for a function
Controlling visibility with linkage and name mangling
Converting a type from an AST description into LLVM types
Creating the LLVM IR function
Emitting the function body
Setting up the module and the driver
Wrapping all in the code generator
Initializing the target machine class
Emitting assembler text and object code
Summary
Chapter 5: IR Generation for High-Level Language Constructs
Technical requirements
Working with arrays, structs, and pointers
Getting the application binary interface right
Creating IR code for classes and virtual functions
Implementing single inheritance
Extending single inheritance with interfaces
Adding support for multiple inheritance
Summary
Chapter 6: Advanced IR Generation
Throwing and catching exceptions
Raising an exception
Catching an exception
Integrating the exception handling code into the application
Generating metadata for type-based alias analysis
Understanding the need for additional metadata
Creating TBAA metadata in LLVM
Adding TBAA metadata to tinylang
Adding debug metadata
Understanding the general structure of debug metadata
Tracking variables and their values
Adding line numbers
Adding debug support to tinylang
Summary
Chapter 7: Optimizing IR
Technical requirements
The LLVM pass manager
Implementing a new pass
Developing the ppprofiler pass as a plugin
Adding the pass to the LLVM source tree
Using the ppprofiler pass with LLVM tools
Adding an optimization pipeline to your compiler
Creating an optimization pipeline
Extending the pass pipeline
Summary
Part 3: Taking LLVM to the Next Level
Chapter 8: The TableGen Language
Technical requirements
Understanding the TableGen language
Experimenting with the TableGen language
Defining records and classes
Creating multiple records at once with multiclasses
Simulating function calls
Generating C++ code from a TableGen file
Defining data in the TableGen language
Implementing a TableGen backend
Drawbacks of TableGen
Summary
Chapter 9: JIT Compilation
Technical requirements
LLVMโ€™s overall JIT implementation and use cases
Using JIT compilation for direct execution
Exploring the lli tool
Implementing our own JIT compiler with LLJIT
Integrating the LLJIT engine into the calculator
Code generation changes to support JIT compilation via LLJIT
Building an LLJIT-based calculator
Building a JIT compiler class from scratch
Creating a JIT compiler class
Using our new JIT compiler class
Summary
Chapter 10: Debugging Using LLVM Tools
Technical requirements
Instrumenting an application with sanitizers
Detecting memory access problems with the address sanitizer
Finding uninitialized memory accesses with the memory sanitizer
Pointing out data races with the thread sanitizer
Finding bugs with libFuzzer
Limitations and alternatives
Performance profiling with XRay
Checking the source with the clang static analyzer
Adding a new checker to the clang static analyzer
Creating your own clang-based tool
Summary
Part 4: Roll Your Own Backend
Chapter 11: The Target Description
Setting the stage for a new backend
Adding the new architecture to the Triple class
Extending the ELF file format definition in LLVM
Creating the target description
Adding the register definition
Defining the instruction formats and the instruction information
Creating the top-level file for the target description
Adding the M88k backend to LLVM
Implementing the assembler parser
Creating the disassembler
Summary
Chapter 12: Instruction Selection
Defining the rules of the calling convention
Implementing the rules of the calling convention
Instruction selection via the selection DAG
Implementing DAG lowering โ€“ handling legal types and setting operations
Implementing DAG lowering โ€“ lowering formal arguments
Implementing DAG lowering โ€“ lowering return values
Implementing DAG-to-DAG transformations within instruction selection
Adding register and instruction information
Putting an empty frame lowering in place
Emitting machine instructions
Creating the target machine and the sub-target
Implementing M88kSubtarget
Implementing M88kTargetMachine โ€“ defining the definitions
Implementing M88kTargetMachine โ€“ adding the implementation
Global instruction selection
Lowering arguments and return values
Legalizing the generic machine instructions
Selecting a register bank for operands
Translating generic machine instructions
Running an example
How to further evolve the backend
Summary
Chapter 13: Beyond Instruction Selection
Adding a new machine function pass to LLVM
Implementing the top-level interface for the M88k target
Adding the TargetMachine implementation for machine function passes
Developing the specifics of the machine function pass
Building newly implemented machine function passes
A glimpse of running a machine function pass with llc
Integrating a new target into the clang frontend
Implementing the driver integration within clang
Implementing ABI support for M88k within clang
Implementing the toolchain support for M88k within clang
Building the M88k target with clang integration
Targeting a different CPU architecture
Summary
Index
Other Books You May Enjoy


๐Ÿ“œ SIMILAR VOLUMES


Learn LLVM 12: A beginner's guide to lea
โœ Kai Nacke ๐Ÿ“‚ Library ๐Ÿ“… 2021 ๐Ÿ› Packt Publishing ๐ŸŒ English

<p><b>Learn how to build and use all parts of real-world compilers, including the frontend, optimization pipeline, and a new backend by leveraging the power of LLVM core libraries</b></p><h4>Key Features</h4><ul><li>Get to grips with effectively using LLVM libraries step-by-step </li><li>Understand

Learn LLVM 12: A beginner's guide to lea
โœ Kai Nacke ๐Ÿ“‚ Library ๐Ÿ“… 2021 ๐Ÿ› Packt Publishing ๐ŸŒ English

Learn how to build and use all parts of real-world compilers, including the frontend, optimization pipeline, and a new backend by leveraging the power of LLVM core libraries Key Features โ€ข Get to grips with effectively using LLVM libraries step-by-step โ€ข Understand LLVM compiler high-level desi

Learn LLVM 12: A beginner's guide to lea
โœ Kai Nacke ๐Ÿ“‚ Library ๐Ÿ“… 2021 ๐Ÿ› Packt Publishing ๐ŸŒ English

Learn how to build and use all parts of real-world compilers, including the frontend, optimization pipeline, and a new backend by leveraging the power of LLVM core libraries Key Features โ€ข Get to grips with effectively using LLVM libraries step-by-step โ€ข Understand LLVM compiler high-level desi

Learn LLVM 12: A beginner's guide to lea
โœ Kai Nacke ๐Ÿ“‚ Library ๐Ÿ“… 2021 ๐Ÿ› Packt Publishing ๐ŸŒ English

Learn how to build and use all parts of real-world compilers, including the frontend, optimization pipeline, and a new backend by leveraging the power of LLVM core libraries Key Features โ€ข Get to grips with effectively using LLVM libraries step-by-step โ€ข Understand LLVM compiler high-level desi

Learn LLVM 17: A beginner's guide to lea
โœ Kai Nacke; Amy Kwan ๐Ÿ“‚ Library ๐Ÿ“… 2024 ๐Ÿ› Packt Publishing ๐ŸŒ English

<span><p><b>Learn how to build and use the complete spectrum of real-world compilers, including the frontend, optimization pipeline, and a new backend by leveraging the power of LLVM core libraries</b></p><h4>Key Features</h4><ul><li>Get to grips with using LLVM libraries step by step</li><li>Unders

Getting Started with LLVM Core Libraries
โœ Bruno Cardoso Lopes, Rafael Auler ๐Ÿ“‚ Library ๐Ÿ“… 2014 ๐Ÿ› Packt Publishing ๐ŸŒ English

LLVM is a bleeding edge compiler technology framework. Easily extendable and designed as a multitude of libraries, LLVM provides a smooth experience for compiler newcomers and reduces the steep learning curve often associated with compiler development. To start, this book will show you how to confi