blob: ef1dd37b555582dc88d83879be446cf72da5ac44 [file] [log] [blame]
Douglas Gregor32110df2009-05-20 00:16:32 +00001<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
2<html> <head>
3<title>Precompiled Headers (PCH)</title>
4</head>
5
6<body>
7
8<!--#include virtual="../menu.html.incl"-->
9
10<div id="content">
11
12<h1>Precompiled Headers</h1>
13
14 <p>This document describes the design and implementation of Clang's
15 precompiled headers (PCH). If you are interested in the end-user
16 view, please see the <a
17 href="UsersManual.html#precompiledheaders">User's Manual</a>.</p>
18
19<h2>Using precompiled headers with <tt>clang-cc</tt></h2>
20
21<p>The low-level Clang compiler, <tt>clang-cc</tt>, supports two command
22line options for generating and using PCH files.<p>
23
24<p>To generate PCH files using <tt>clang-cc</tt>, use the option
25<b><tt>-emit-pch</tt></b>:
26
27<pre> $ clang-cc test.h -emit-pch -o test.h.pch </pre>
28
29<p>This option is transparently used by <tt>clang</tt> when generating
30PCH files. The resulting PCH file contains the serialized form of the
31compiler's internal representation after it has completed parsing and
32semantic analysis. The PCH file can then be used as a prefix header
33with the <b><tt>-include-pch</tt></b> option:</p>
34
35<pre>
36 $ clang-cc -include-pch test.h.pch test.c -o test.s
37</pre>
38
39<h2>PCH Design Philosophy</h2>
40
41<p>Precompiled headers are meant to improve overall compile times for
42 projects, so the design of precompiled headers is entirely driven by
43 performance concerns. The use case for precompiled headers is
44 relatively simple: when there is a common set of headers that is
45 included in nearly every source file in the project, we
46 <i>precompile</i> that bundle of headers into a single precompiled
47 header (PCH file). Then, when compiling the source files in the
48 project, we load the PCH file first (as a prefix header), which acts
49 as a stand-in for that bundle of headers.</p>
50
51<p>A precompiled header implementation improves performance when:</p>
52<ul>
53 <li>Loading the PCH file is significantly faster than re-parsing the
54 bundle of headers stored within the PCH file. Thus, a precompiled
55 header design attempts to minimize the cost of reading the PCH
56 file. Ideally, this cost should not vary with the size of the
57 precompiled header file.</li>
58
59 <li>The cost of generating the PCH file initially is not so large
60 that it counters the per-source-file performance improvement due to
61 eliminating the need to parse the bundled headers in the first
62 place. This is particularly important on multi-core systems, because
63 PCH file generation serializes the build when all compilations
64 require the PCH file to be up-to-date.</li>
65</ul>
Douglas Gregor2cc390e2009-06-02 22:08:07 +000066
67<p>Clang's precompiled headers are designed with a compact on-disk
68representation, which minimizes both PCH creation time and the time
69required to initially load the PCH file. The PCH file itself contains
70a serialized representation of Clang's abstract syntax trees and
71supporting data structures, stored using the same compressed bitstream
72as <a href="http://llvm.org/docs/BitCodeFormat.html">LLVM's bitcode
73file format</a>.</p>
74
75<p>Clang's precompiled headers are loaded "lazily" from disk. When a
76PCH file is initially loaded, Clang reads only a small amount of data
77from the PCH file to establish where certain important data structures
78are stored. The amount of data read in this initial load is
79independent of the size of the PCH file, such that a larger PCH file
80does not lead to longer PCH load times. The actual header data in the
81PCH file--macros, functions, variables, types, etc.--is loaded only
82when it is referenced from the user's code, at which point only that
83entity (and those entities it depends on) are deserialized from the
84PCH file. With this approach, the cost of using a precompiled header
85for a translation unit is proportional to the amount of code actually
86used from the header, rather than being proportional to the size of
87the header itself.</p> </body>
88
89<h2>Precompiled Header Contents</h2>
90
91<img src="PCHLayout.png" align="right" alt="Precompiled header layout">
92
93<p>Clang's precompiled headers are organized into several different
94blocks, each of which contains the serialized representation of a part
95of Clang's internal representation. Each of the blocks corresponds to
96either a block or a record within <a
97 href="http://llvm.org/docs/BitCodeFormat.html">LLVM's bitstream
98format</a>. The contents of each of these logical blocks are described
99below.</p>
100
101<h3 name="metadata">Metadata Block</h3>
102
103<p>The metadata block contains several records that provide
104information about how the precompiled header was built. This metadata
105is primarily used to validate the use of a precompiled header. For
106example, a precompiled header built for x86 (32-bit) cannot be used
107when compiling for x86-64 (64-bit). The metadata block contains
108information about:</p>
109
110<dl>
111 <dt>Language options</dt>
112 <dd>Describes the particular language dialect used to compile the
113PCH file, including major options (e.g., Objective-C support) and more
114minor options (e.g., support for "//" comments). The contents of this
115record correspond to the <code>LangOptions</code> class.</dd>
Douglas Gregor32110df2009-05-20 00:16:32 +0000116
Douglas Gregor2cc390e2009-06-02 22:08:07 +0000117 <dt>Target architecture</dt>
118 <dd>The target triple that describes the architecture, platform, and
119ABI for which the PCH file was generated, e.g.,
120<code>i386-apple-darwin9</code>.</dd>
121
122 <dt>PCH version</dt>
123 <dd>The major and minor version numbers of the precompiled header
124format. Changes in the minor version number should not affect backward
125compatibility, while changes in the major version number imply that a
126newer compiler cannot read an older precompiled header (and
127vice-versa).</dd>
128
129 <dt>Original file name</dt>
130 <dd>The full path of the header that was used to generate the
131precompiled header.</dd> </dl>
132
133 <dt>Predefines buffer</dt>
134 <dd>Although not explicitly stored as part of the metadata, the
135predefines buffer is used in the validation of the precompiled header.
136The predefines buffer itself contains code generated by the compiler
137to initialize the preprocessor state according to the current target,
138platform, and command-line options. For example, the predefines buffer
139will contain "<code>#define __STDC__ 1</code>" when we are compiling C
140without Microsoft extensions. The predefines buffer itself is stored
141within the <a href="#sourcemgr">source manager block</a>, but its
142contents are verified along with the rest of the metadata.</dd> </dl>
143
144<h3 name="sourcemgr">Source Manager Block</h3>
145
146<p>The source manager block contains the serialized representation of
147Clang's <a
148 href="InternalsManual.html#SourceLocation">SourceManager</a> class,
149which handles the mapping from source locations (as represented in
150Clang's abstract syntax tree) into actual column/line positions within
151a source file or macro instantiation. The precompiled header's
152representation of the source manager also includes information about
153all of the headers that were (transitively) included when building the
154precompiled header.</p>
155
156<p>The bulk of the source manager block is dedicated to information
157about the various files, buffers, and macro instantiations into which
158a source location can refer. Each of these is referenced by a numeric
159"file ID", which is a unique number (allocated starting at 1) stored
160in the source location. Clang serializes the information for each kind
161of file ID, along with an index that maps file IDs to the position
162within the PCH file where the information about that file ID is
163stored. The data associated with a file ID is loaded only when
164required by the front end, e.g., to emit a diagnostic that includes a
165macro instantiation history inside the header itself.</p>
166
167<p>The source manager block also contains information about all of the
168headers that were included when building the precompiled header. This
169includes information about the controlling macro for the header (e.g.,
170when the preprocessor identified that the contents of the header
171dependent on a macro like <code>LLVM_CLANG_SOURCEMANAGER_H</code>)
172along with a cached version of the results of the <code>stat()</code>
173system calls performed when building the precompiled header. The
174latter is particularly useful in reducing system time when searching
175for include files.</p>
176
177<h3 name="preprocessor">Preprocessor Block</h3>
178
179<p>The preprocessor block contains the serialized representation of
180the preprocessor. Specifically, it contains all of the macros that
181have been defined by the end of the header used to build the
182precompiled header, along with the token sequences that comprise each
183macro. The macro definitions are only read from the PCH file when the
184name of the macro first occurs in the program. This lazy loading of
185macro definitions is trigged by lookups into the <a
186 href="#idtable">identifier table</a>.</p>
187
188<h3 name="types">Types Block</h3>
189
190<p>The types block contains the serialized representation of all of
191the types referenced in the translation unit. Each Clang type node
192(<code>PointerType</code>, <code>FunctionProtoType</code>, etc.) has a
193corresponding record type in the PCH file. When types are deserialized
194from the precompiled header, the data within the record is used to
195reconstruct the appropriate type node using the AST context.</p>
196
197<p>Each type has a unique type ID, which is an integer that uniquely
198identifies that type. Type ID 0 represents the NULL type, type IDs
199less than <code>NUM_PREDEF_TYPE_IDS</code> represent predefined types
200(<code>void</code>, <code>float</code>, etc.), while other
201"user-defined" type IDs are assigned consecutively from
202<code>NUM_PREDEF_TYPE_IDS</code> upward as the types are encountered.
203The PCH file has an associated mapping from the user-defined types
204block to the location within the types block where the serialized
205representation of that type resides, enabling lazy deserialization of
206types. When a type is referenced from within the PCH file, that
207reference is encoded using the type ID shifted left by 3 bits. The
208lower three bits are used to represent the <code>const</code>,
209<code>volatile</code>, and <code>restrict</code> qualifiers, as in
210Clang's <a
211 href="http://clang.llvm.org/docs/InternalsManual.html#Type">QualType</a>
212class.</p>
213
214<h3 name="decls">Declarations Block</h3>
215
216<p>The declarations block contains the serialized representation of
217all of the declarations referenced in the translation unit. Each Clang
218declaration node (<code>VarDecl</code>, <code>FunctionDecl</code>,
219etc.) has a corresponding record type in the PCH file. When
220declarations are deserialized from the precompiled header, the data
221within the record is used to build and populate a new instance of the
222corresponding <code>Decl</code> node. As with types, each declaration
223node has a numeric ID that is used to refer to that declaration within
224the PCH file. In addition, a lookup table provides a mapping from that
225numeric ID to the offset within the precompiled header where that
226declaration is described.</p>
227
228<p>Declarations in Clang's abstract syntax trees are stored
229hierarchically. At the top of the hierarchy is the translation unit
230(<code>TranslationUnitDecl</code>), which contains all of the
231declarations in the translation unit. These declarations---such as
232functions or struct types---may also contain other declarations inside
233them, and so on. Within Clang, each declaration is stored within a <a
234href="http://clang.llvm.org/docs/InternalsManual.html#DeclContext">declaration
235context</a>, as represented by the <code>DeclContext</code> class.
236Declaration contexts provide the mechanism to perform name lookup
237within a given declaration (e.g., find the member named <code>x</code>
238in a structure) and iterate over the declarations stored within a
239context (e.g., iterate over all of the fields of a structure for
240structure layout).</p>
241
242<p>In Clang's precompiled header format, deserializing a declaration
243that is a <code>DeclContext</code> is a separate operation from
244deserializing all of the declarations stored within that declaration
245context. Therefore, Clang will deserialize the translation unit
246declaration without deserializing the declarations within that
247translation unit. When required, the declarations stored within a
248declaration context will be serialized. There are two representations
249of the declarations within a declaration context, which correspond to
250the name-lookup and iteration behavior described above:</p>
251
252<ul>
253 <li>When the front end performs name lookup to find a name
254 <code>x</code> within a given declaration context (for example,
255 during semantic analysis of the expression <code>p-&gt;x</code>,
256 where <code>p</code>'s type is defined in the precompiled header),
257 Clang deserializes a hash table mapping from the names within that
258 declaration context to the declaration IDs that represent each
259 visible declaration with that name. The entire hash table is
260 deserialized at this point (into the <code>llvm::DenseMap</code>
261 stored within each <code>DeclContext</code> object), but the actual
262 declarations are not yet deserialized. In a second step, those
263 declarations with the name <code>x</code> will be deserialized and
264 will be used as the result of name lookup.</li>
265
266 <li>When the front end performs iteration over all of the
267 declarations within a declaration context, all of those declarations
268 are immediately de-serialized. For large declaration contexts (e.g.,
269 the translation unit), this operation is expensive; however, large
270 declaration contexts are not traversed in normal compilation, since
271 such a traversal is unnecessary. However, it is common for the code
272 generator and semantic analysis to traverse declaration contexts for
273 structs, classes, unions, and enumerations, although those contexts
274 contain relatively few declarations in the common case.</li>
275</ul>
276
277<h3 name="idtable">Identifier Table Block</h3>
278
279<p>The identifier table block contains an on-disk hash table that maps
280each identifier mentioned within the precompiled header to the
281serialized representation of the identifier's information (e.g, the
282<code>IdentifierInfo</code> structure). The serialized representation
283contains:</p>
284
285<ul>
286 <li>The actual identifier string.</li>
287 <li>Flags that describe whether this identifier is the name of a
288 built-in, a poisoned identifier, an extension token, or a
289 macro.</li>
290 <li>If the identifier names a macro, the offset of the macro
291 definition within the <a href="#preprocessor">preprocessor
292 block</a>.</li>
293 <li>If the identifier names one or more declarations visible from
294 translation unit scope, the <a href="#decls">declaration IDs</a> of these
295 declarations.</li>
296</ul>
297
298<p>When a precompiled header is loaded, the precompiled header
299mechanism introduces itself into the identifier table as an external
300lookup source. Thus, when the user program refers to an identifier
301that has not yet been seen, Clang will perform a lookup into the
302on-disk hash table ... FINISH THIS!
303
304<p>A separate table provides a mapping from the numeric representation
305of identifiers used in the PCH file to the location within the on-disk
306hash table where that identifier is stored. This mapping is used when
307deserializing the name of a declaration, the identifier of a token, or
308any other construct in the PCH file that refers to a name.</p>
309
Douglas Gregor32110df2009-05-20 00:16:32 +0000310</div>
311
Douglas Gregor32110df2009-05-20 00:16:32 +0000312</html>