Reader small image

You're reading from  Clang Compiler Frontend

Product typeBook
Published inMar 2024
PublisherPackt
ISBN-139781837630981
Edition1st Edition
Right arrow
Author (1)
Ivan Murashko
Ivan Murashko
author image
Ivan Murashko

Ivan V. Murashko is a C++ software engineer: He got his PhD from Peter the Great St.Petersburg Polytechnic University and has over 20 years of C++ programming experience; since 2020 he has worked with LLVM compilers. His area of interest includes clang compiler frontend and clang tools (clang-tidy, clangd).
Read more about Ivan Murashko

Right arrow

Appendix 2

Build Speed Optimization

Clang has implemented several features with the goal of improving build speed for large projects. One of the most interesting features is precompiled headers and modules. They can be considered techniques that allow caching some parts of the AST and reusing it for different compiler invocations. Caching can significantly improve build speed for your project, and some of these features can be used to speed up different Clang tool executions. For instance, precompiled headers are used as the primary Clangd optimization for document editing.

In this appendix, we will cover two primary topics

  • Precompiled headers

  • Modules

10.1 Technical requirements

The source code for this appendix is located in the chapter10 folder of the book’s GitHub repository: https://github.com/PacktPublishing/Clang-Compiler-Frontend-Packt/tree/main/chapter10.

10.2 Precompiled headers

Precompiled headers PCH, are a Clang feature designed to improve Clang’s frontend performance. The basic idea is to create an AST (Abstract Syntax Tree) for a header file and reuse this AST during compilation for sources that include the header file.

Generating a precompiled header file is simple [5]. Suppose you have the following header file, header.h :

#pragma once 
 
 
 
void foo() { 
 
}

Figure 10.1: Header file to be compiled to PCH

You can generate a PCH for it with the following command:

$ <...>/llvm-project/install/bin/clang -cc1 -emit-pch        \
                                        -x c++-header header.h \
  ...

10.3 Clang modules

Modules, or Precompiled Modules (PCMs), can be considered the next step in the evolution of precompiled headers. They also represent a parsed AST in binary form but form a DAG (tree), meaning one module can include more than one other module.

This is a major improvement compared to precompiled headers, where only one precompiled header can be introduced for each compilation unit.

The C++20 standard [21] introduced two concepts related to modules. The first one is ordinary modules, described in section 10 of [21]. The other one is the so-called header unit , mostly described in section 15.5. Header units can be considered an intermediate step between ordinary headers and modules and allow the use of the import directive to import ordinary headers.

We will focus on Clang modules, which can be considered an implementation of header units from the C++ standard. There are two different options to use Clang modules. The first one is called explicit modules. The second is...

10.4 Further reading

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Clang Compiler Frontend
Published in: Mar 2024Publisher: PacktISBN-13: 9781837630981
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Ivan Murashko

Ivan V. Murashko is a C++ software engineer: He got his PhD from Peter the Great St.Petersburg Polytechnic University and has over 20 years of C++ programming experience; since 2020 he has worked with LLVM compilers. His area of interest includes clang compiler frontend and clang tools (clang-tidy, clangd).
Read more about Ivan Murashko