<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/87902>87902</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
No clear examples of how -fpch-codegen works
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
alecazam
</td>
</tr>
</table>
<pre>
So this should make clang pch on par with MSVC pch. Where MSVC gens an links an obj file containing the pch data. But the sample code that is scattered around the internet shows something that just doesn't work in practice. I feel like all clang projects are using a slow form of pch, where the pch is prepended to the tops of every .o file and then the compiler runs on that.
This is roughly the sample code shown by @llunark. I'm hoping he can provided some more guidance. I also pinged the CMake thread that was trying to integrate this. I'm trying to get this to work in a Makefile, and on an M2 Max with macOS 14.4. I already see a 2x speedup by using pch, but feel like the benefit would be even more if the codegen can occur once.
This is taken from a discussion here. The clang docs and manual don't cover this at all. I've added my interpretation of what is happening with the commands.
https://github.com/bkaradzic/GENie/discussions/535
```
This does debug and codegen directly into the .pch file, which is odd, since you'd want the codegen in the .o file, right?
$ clang++ -x c++-header header.h -o header.pch -fpch-codegen -fpch-debuginfo
This prepends the pch file to the .o file, no force include is needed in clang, and clang ignores #include "header.h" for pch use
$ clang++ -c source.cpp -o source.o -include-pch header.pch
This is supposed to compile the pch into the .o file.
Is it stripping the code?
Do flags used in the compile need to be supplied here?
$ clang++ -c header.pch -o shared.o
Finally this links the source and pch object file into the library/exe
$ clang++ -o binary source.o shared.o
```
The problem is when I run line 3 to convert pch to .o, I get "unknown type name CPCH". But that's what the pch starts with. So these commands just seem invalid. CMake tries to use a .cpp, and I guess -include-pch to that. I basically just need the pch splied to the top of that .o, but with an empty .cpp, the output is like 680 bytes, where the pch is 33MB. So that's not going to work.
```
SystemPrecompiled-BaseDebugmacosx.h.pch:1:1: unknown type name 'CPCH'
CPCH<U+0001><U+0008><U+0000><U+0000>L<U+000B><U+0000><U+0000><U+0007><C1><B3>Ќ<C2>+<B8><U+0002>-<C8><C2>+<B0><C2>/<88><U+0002>+<BC><C2>(<AC><C2>C<U+0012><B4>B)<D0>B(<90>B(<D0>B(<0F+<BC><U+0002>)<D4><U+0002>+<94><C2>/<B8>B(<B4>B)<DH+<BC><U+0002>)<D4><U+0002>+<94><C2>/<90>B*<C8>B)<8C><U+0002>-<BC><82>,<E4><C2><U+0003>t<B4><C2>+<90>B-<B0>B)<FC>B+<84><U+0002>,<FC><82>)<A4><U+0002>+<94><C2><U+0003>"<A4>B+<C0><C2>+<C8><U+0002>-<CC><U+0083>3<BC><82>,<A4><C2>)<A4><82>+<84><U+0002>+<FC><82>)<A4><U+0002>+<94><C2>CD<BC><82>,<A4><C2>)<A4><82>+<84><U+0002>+<FC><82>)<A4><U+0002>+<94><C2>/<A4><U+0002>)<LE*<B8><U+0002>,<D4><U+0002>-<FC><82>)<A4><U+0002>+<94><C2>/<BC><82>)<98><C2>,<94><U+0002>-<CC><C2>C<94><B4><C2>+<90>B-<B0>B)<FC><C2>(<84><C2>(<A0>B)<FC><C2>*<94>B.<8<CA>(<84><C2>,<98><C2>,<FC><82>,<BC><C2>+<D0><C2>/<A4><U+0002>)<P<CB>(<84><C2>,<FC>B*<B8><C2>(<B0>B-<90>B)<FC><U+0002>-<C8>B)<94><C2>/<A4><U+0002>)<U+001C><U+0004><D1>B<F3>
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzMWN9z4jgS_mvESxcuI8MADzwEJ9ymarM3Vdm7e5atBmtiSy5JDmH_-quW7IAHmNutuYet2h0s0T--_rr1KUY4pw4accMWW7Z4nIjOV8ZuRI2l-EM0k8LI0-bVgK-UA1eZrpbQiDeEshb6AG1ZgdHQCgtH5St4ef13TpsJwH8qtBg3DqgdCA210m_hwRTfYK9qhNJoL5RW-gC-whBOCi8SgG3nw5YTTRsMJYKvhAfCUQrv0aIEYU2nZTBU2qPV6Anl0YEzDfoqBhYevnXOgzToNONLD0dj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