Douglas Gregor | 32110df | 2009-05-20 00:16:32 +0000 | [diff] [blame] | 1 | <!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 |
| 22 | line 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 |
| 30 | PCH files. The resulting PCH file contains the serialized form of the |
| 31 | compiler's internal representation after it has completed parsing and |
| 32 | semantic analysis. The PCH file can then be used as a prefix header |
| 33 | with 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 Gregor | 2cc390e | 2009-06-02 22:08:07 +0000 | [diff] [blame^] | 66 | |
| 67 | <p>Clang's precompiled headers are designed with a compact on-disk |
| 68 | representation, which minimizes both PCH creation time and the time |
| 69 | required to initially load the PCH file. The PCH file itself contains |
| 70 | a serialized representation of Clang's abstract syntax trees and |
| 71 | supporting data structures, stored using the same compressed bitstream |
| 72 | as <a href="http://llvm.org/docs/BitCodeFormat.html">LLVM's bitcode |
| 73 | file format</a>.</p> |
| 74 | |
| 75 | <p>Clang's precompiled headers are loaded "lazily" from disk. When a |
| 76 | PCH file is initially loaded, Clang reads only a small amount of data |
| 77 | from the PCH file to establish where certain important data structures |
| 78 | are stored. The amount of data read in this initial load is |
| 79 | independent of the size of the PCH file, such that a larger PCH file |
| 80 | does not lead to longer PCH load times. The actual header data in the |
| 81 | PCH file--macros, functions, variables, types, etc.--is loaded only |
| 82 | when it is referenced from the user's code, at which point only that |
| 83 | entity (and those entities it depends on) are deserialized from the |
| 84 | PCH file. With this approach, the cost of using a precompiled header |
| 85 | for a translation unit is proportional to the amount of code actually |
| 86 | used from the header, rather than being proportional to the size of |
| 87 | the 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 |
| 94 | blocks, each of which contains the serialized representation of a part |
| 95 | of Clang's internal representation. Each of the blocks corresponds to |
| 96 | either a block or a record within <a |
| 97 | href="http://llvm.org/docs/BitCodeFormat.html">LLVM's bitstream |
| 98 | format</a>. The contents of each of these logical blocks are described |
| 99 | below.</p> |
| 100 | |
| 101 | <h3 name="metadata">Metadata Block</h3> |
| 102 | |
| 103 | <p>The metadata block contains several records that provide |
| 104 | information about how the precompiled header was built. This metadata |
| 105 | is primarily used to validate the use of a precompiled header. For |
| 106 | example, a precompiled header built for x86 (32-bit) cannot be used |
| 107 | when compiling for x86-64 (64-bit). The metadata block contains |
| 108 | information about:</p> |
| 109 | |
| 110 | <dl> |
| 111 | <dt>Language options</dt> |
| 112 | <dd>Describes the particular language dialect used to compile the |
| 113 | PCH file, including major options (e.g., Objective-C support) and more |
| 114 | minor options (e.g., support for "//" comments). The contents of this |
| 115 | record correspond to the <code>LangOptions</code> class.</dd> |
Douglas Gregor | 32110df | 2009-05-20 00:16:32 +0000 | [diff] [blame] | 116 | |
Douglas Gregor | 2cc390e | 2009-06-02 22:08:07 +0000 | [diff] [blame^] | 117 | <dt>Target architecture</dt> |
| 118 | <dd>The target triple that describes the architecture, platform, and |
| 119 | ABI 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 |
| 124 | format. Changes in the minor version number should not affect backward |
| 125 | compatibility, while changes in the major version number imply that a |
| 126 | newer compiler cannot read an older precompiled header (and |
| 127 | vice-versa).</dd> |
| 128 | |
| 129 | <dt>Original file name</dt> |
| 130 | <dd>The full path of the header that was used to generate the |
| 131 | precompiled header.</dd> </dl> |
| 132 | |
| 133 | <dt>Predefines buffer</dt> |
| 134 | <dd>Although not explicitly stored as part of the metadata, the |
| 135 | predefines buffer is used in the validation of the precompiled header. |
| 136 | The predefines buffer itself contains code generated by the compiler |
| 137 | to initialize the preprocessor state according to the current target, |
| 138 | platform, and command-line options. For example, the predefines buffer |
| 139 | will contain "<code>#define __STDC__ 1</code>" when we are compiling C |
| 140 | without Microsoft extensions. The predefines buffer itself is stored |
| 141 | within the <a href="#sourcemgr">source manager block</a>, but its |
| 142 | contents 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 |
| 147 | Clang's <a |
| 148 | href="InternalsManual.html#SourceLocation">SourceManager</a> class, |
| 149 | which handles the mapping from source locations (as represented in |
| 150 | Clang's abstract syntax tree) into actual column/line positions within |
| 151 | a source file or macro instantiation. The precompiled header's |
| 152 | representation of the source manager also includes information about |
| 153 | all of the headers that were (transitively) included when building the |
| 154 | precompiled header.</p> |
| 155 | |
| 156 | <p>The bulk of the source manager block is dedicated to information |
| 157 | about the various files, buffers, and macro instantiations into which |
| 158 | a 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 |
| 160 | in the source location. Clang serializes the information for each kind |
| 161 | of file ID, along with an index that maps file IDs to the position |
| 162 | within the PCH file where the information about that file ID is |
| 163 | stored. The data associated with a file ID is loaded only when |
| 164 | required by the front end, e.g., to emit a diagnostic that includes a |
| 165 | macro instantiation history inside the header itself.</p> |
| 166 | |
| 167 | <p>The source manager block also contains information about all of the |
| 168 | headers that were included when building the precompiled header. This |
| 169 | includes information about the controlling macro for the header (e.g., |
| 170 | when the preprocessor identified that the contents of the header |
| 171 | dependent on a macro like <code>LLVM_CLANG_SOURCEMANAGER_H</code>) |
| 172 | along with a cached version of the results of the <code>stat()</code> |
| 173 | system calls performed when building the precompiled header. The |
| 174 | latter is particularly useful in reducing system time when searching |
| 175 | for include files.</p> |
| 176 | |
| 177 | <h3 name="preprocessor">Preprocessor Block</h3> |
| 178 | |
| 179 | <p>The preprocessor block contains the serialized representation of |
| 180 | the preprocessor. Specifically, it contains all of the macros that |
| 181 | have been defined by the end of the header used to build the |
| 182 | precompiled header, along with the token sequences that comprise each |
| 183 | macro. The macro definitions are only read from the PCH file when the |
| 184 | name of the macro first occurs in the program. This lazy loading of |
| 185 | macro 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 |
| 191 | the types referenced in the translation unit. Each Clang type node |
| 192 | (<code>PointerType</code>, <code>FunctionProtoType</code>, etc.) has a |
| 193 | corresponding record type in the PCH file. When types are deserialized |
| 194 | from the precompiled header, the data within the record is used to |
| 195 | reconstruct 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 |
| 198 | identifies that type. Type ID 0 represents the NULL type, type IDs |
| 199 | less 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. |
| 203 | The PCH file has an associated mapping from the user-defined types |
| 204 | block to the location within the types block where the serialized |
| 205 | representation of that type resides, enabling lazy deserialization of |
| 206 | types. When a type is referenced from within the PCH file, that |
| 207 | reference is encoded using the type ID shifted left by 3 bits. The |
| 208 | lower three bits are used to represent the <code>const</code>, |
| 209 | <code>volatile</code>, and <code>restrict</code> qualifiers, as in |
| 210 | Clang's <a |
| 211 | href="http://clang.llvm.org/docs/InternalsManual.html#Type">QualType</a> |
| 212 | class.</p> |
| 213 | |
| 214 | <h3 name="decls">Declarations Block</h3> |
| 215 | |
| 216 | <p>The declarations block contains the serialized representation of |
| 217 | all of the declarations referenced in the translation unit. Each Clang |
| 218 | declaration node (<code>VarDecl</code>, <code>FunctionDecl</code>, |
| 219 | etc.) has a corresponding record type in the PCH file. When |
| 220 | declarations are deserialized from the precompiled header, the data |
| 221 | within the record is used to build and populate a new instance of the |
| 222 | corresponding <code>Decl</code> node. As with types, each declaration |
| 223 | node has a numeric ID that is used to refer to that declaration within |
| 224 | the PCH file. In addition, a lookup table provides a mapping from that |
| 225 | numeric ID to the offset within the precompiled header where that |
| 226 | declaration is described.</p> |
| 227 | |
| 228 | <p>Declarations in Clang's abstract syntax trees are stored |
| 229 | hierarchically. At the top of the hierarchy is the translation unit |
| 230 | (<code>TranslationUnitDecl</code>), which contains all of the |
| 231 | declarations in the translation unit. These declarations---such as |
| 232 | functions or struct types---may also contain other declarations inside |
| 233 | them, and so on. Within Clang, each declaration is stored within a <a |
| 234 | href="http://clang.llvm.org/docs/InternalsManual.html#DeclContext">declaration |
| 235 | context</a>, as represented by the <code>DeclContext</code> class. |
| 236 | Declaration contexts provide the mechanism to perform name lookup |
| 237 | within a given declaration (e.g., find the member named <code>x</code> |
| 238 | in a structure) and iterate over the declarations stored within a |
| 239 | context (e.g., iterate over all of the fields of a structure for |
| 240 | structure layout).</p> |
| 241 | |
| 242 | <p>In Clang's precompiled header format, deserializing a declaration |
| 243 | that is a <code>DeclContext</code> is a separate operation from |
| 244 | deserializing all of the declarations stored within that declaration |
| 245 | context. Therefore, Clang will deserialize the translation unit |
| 246 | declaration without deserializing the declarations within that |
| 247 | translation unit. When required, the declarations stored within a |
| 248 | declaration context will be serialized. There are two representations |
| 249 | of the declarations within a declaration context, which correspond to |
| 250 | the 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->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 |
| 280 | each identifier mentioned within the precompiled header to the |
| 281 | serialized representation of the identifier's information (e.g, the |
| 282 | <code>IdentifierInfo</code> structure). The serialized representation |
| 283 | contains:</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 |
| 299 | mechanism introduces itself into the identifier table as an external |
| 300 | lookup source. Thus, when the user program refers to an identifier |
| 301 | that has not yet been seen, Clang will perform a lookup into the |
| 302 | on-disk hash table ... FINISH THIS! |
| 303 | |
| 304 | <p>A separate table provides a mapping from the numeric representation |
| 305 | of identifiers used in the PCH file to the location within the on-disk |
| 306 | hash table where that identifier is stored. This mapping is used when |
| 307 | deserializing the name of a declaration, the identifier of a token, or |
| 308 | any other construct in the PCH file that refers to a name.</p> |
| 309 | |
Douglas Gregor | 32110df | 2009-05-20 00:16:32 +0000 | [diff] [blame] | 310 | </div> |
| 311 | |
Douglas Gregor | 32110df | 2009-05-20 00:16:32 +0000 | [diff] [blame] | 312 | </html> |