Skip to content

Commit 54eea61

Browse files
committed
add -fpch-codegen/debuginfo mapping to -fmodules-codegen/debuginfo
Using -fmodules-* options for PCHs is a bit confusing, so add -fpch-* variants. Having extra options also makes it simple to do a configure check for the feature. Also document the options in the release notes. Differential Revision: https://reviews.llvm.org/D83623
1 parent 3895466 commit 54eea61

File tree

4 files changed

+42
-6
lines changed

4 files changed

+42
-6
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,32 @@ New Compiler Flags
6363

6464
- ...
6565

66+
- -fpch-codegen and -fpch-debuginfo generate shared code and/or debuginfo
67+
for contents of a precompiled header in a separate object file. This object
68+
file needs to be linked in, but its contents do not need to be generated
69+
for other objects using the precompiled header. This should usually save
70+
compile time. If not using clang-cl, the separate object file needs to
71+
be created explicitly from the precompiled header.
72+
Example of use:
73+
74+
.. code-block:: console
75+
76+
$ clang++ -x c++-header header.h -o header.pch -fpch-codegen -fpch-debuginfo
77+
$ clang++ -c header.pch -o shared.o
78+
$ clang++ -c source.cpp -o source.o -include-pch header.pch
79+
$ clang++ -o binary source.o shared.o
80+
81+
- Using -fpch-instantiate-templates when generating the precompiled header
82+
usually increases the amount of code/debuginfo that can be shared.
83+
- In some cases, especially when building with optimizations enabled, using
84+
-fpch-codegen may generate so much code in the shared object that compiling
85+
it may be a net loss in build time.
86+
- Since headers may bring in private symbols of other libraries, it may be
87+
sometimes necessary to discard unused symbols (such as by adding
88+
-Wl,--gc-sections on ELF platforms to the linking command, and possibly
89+
adding -fdata-sections -ffunction-sections to the command generating
90+
the shared object).
91+
6692
Deprecated Compiler Flags
6793
-------------------------
6894

clang/include/clang/Driver/Options.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1440,6 +1440,10 @@ def fpch_instantiate_templates:
14401440
def fno_pch_instantiate_templates:
14411441
Flag <["-"], "fno-pch-instantiate-templates">,
14421442
Group<f_Group>, Flags<[CC1Option]>;
1443+
defm pch_codegen: OptInFFlag<"pch-codegen", "Generate ", "Do not generate ",
1444+
"code for uses of this PCH that assumes an explicit object file will be built for the PCH">;
1445+
defm pch_debuginfo: OptInFFlag<"pch-debuginfo", "Generate ", "Do not generate ",
1446+
"debug info for types in an object file built from this PCH and do not generate them elsewhere">;
14431447

14441448
def fmodules : Flag <["-"], "fmodules">, Group<f_Group>,
14451449
Flags<[DriverOption, CC1Option]>,

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5642,6 +5642,12 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
56425642
if (Args.hasFlag(options::OPT_fpch_instantiate_templates,
56435643
options::OPT_fno_pch_instantiate_templates, false))
56445644
CmdArgs.push_back("-fpch-instantiate-templates");
5645+
if (Args.hasFlag(options::OPT_fpch_codegen, options::OPT_fno_pch_codegen,
5646+
false))
5647+
CmdArgs.push_back("-fmodules-codegen");
5648+
if (Args.hasFlag(options::OPT_fpch_debuginfo, options::OPT_fno_pch_debuginfo,
5649+
false))
5650+
CmdArgs.push_back("-fmodules-debuginfo");
56455651

56465652
Args.AddLastArg(CmdArgs, options::OPT_fexperimental_new_pass_manager,
56475653
options::OPT_fno_experimental_new_pass_manager);

clang/test/Driver/pch-codegen.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,29 @@
77
// CHECK-PCH-CREATE-NOT: -fmodules-codegen
88
// CHECK-PCH-CREATE-NOT: -fmodules-debuginfo
99

10-
// Create PCH with -fmodules-codegen.
11-
// RUN: %clang -x c++-header -Xclang -fmodules-codegen %S/../Modules/Inputs/codegen-flags/foo.h -o %t/foo-cg.pch -### 2>&1 | FileCheck %s -check-prefix=CHECK-PCH-CODEGEN-CREATE
10+
// Create PCH with -fpch-codegen.
11+
// RUN: %clang -x c++-header -fpch-codegen %S/../Modules/Inputs/codegen-flags/foo.h -o %t/foo-cg.pch -### 2>&1 | FileCheck %s -check-prefix=CHECK-PCH-CODEGEN-CREATE
1212
// CHECK-PCH-CODEGEN-CREATE: -emit-pch
1313
// CHECK-PCH-CODEGEN-CREATE: -fmodules-codegen
1414
// CHECK-PCH-CODEGEN-CREATE: "-x" "c++-header"
1515
// CHECK-PCH-CODEGEN-CREATE-NOT: -fmodules-debuginfo
1616

17-
// Create PCH with -fmodules-debuginfo.
18-
// RUN: %clang -x c++-header -Xclang -fmodules-debuginfo %S/../Modules/Inputs/codegen-flags/foo.h -g -o %t/foo-di.pch -### 2>&1 | FileCheck %s -check-prefix=CHECK-PCH-DEBUGINFO-CREATE
17+
// Create PCH with -fpch-debuginfo.
18+
// RUN: %clang -x c++-header -fpch-debuginfo %S/../Modules/Inputs/codegen-flags/foo.h -g -o %t/foo-di.pch -### 2>&1 | FileCheck %s -check-prefix=CHECK-PCH-DEBUGINFO-CREATE
1919
// CHECK-PCH-DEBUGINFO-CREATE: -emit-pch
2020
// CHECK-PCH-DEBUGINFO-CREATE: -fmodules-debuginfo
2121
// CHECK-PCH-DEBUGINFO-CREATE: "-x" "c++-header"
2222
// CHECK-PCH-DEBUGINFO-CREATE-NOT: -fmodules-codegen
2323

24-
// Create PCH's object file for -fmodules-codegen.
24+
// Create PCH's object file for -fpch-codegen.
2525
// RUN: touch %t/foo-cg.pch
2626
// RUN: %clang -c %t/foo-cg.pch -o %t/foo-cg.o -### 2>&1 | FileCheck %s -check-prefix=CHECK-PCH-CODEGEN-OBJ
2727
// CHECK-PCH-CODEGEN-OBJ: -emit-obj
2828
// CHECK-PCH-CODEGEN-OBJ: "-main-file-name" "foo-cg.pch"
2929
// CHECK-PCH-CODEGEN-OBJ: "-o" "{{.*}}foo-cg.o"
3030
// CHECK-PCH-CODEGEN-OBJ: "-x" "precompiled-header"
3131

32-
// Create PCH's object file for -fmodules-debuginfo.
32+
// Create PCH's object file for -fpch-debuginfo.
3333
// RUN: touch %t/foo-di.pch
3434
// RUN: %clang -c %t/foo-di.pch -g -o %t/foo-di.o -### 2>&1 | FileCheck %s -check-prefix=CHECK-PCH-DEBUGINFO-OBJ
3535
// CHECK-PCH-DEBUGINFO-OBJ: -emit-obj

0 commit comments

Comments
 (0)