Index: clang/docs/ReleaseNotes.rst =================================================================== --- clang/docs/ReleaseNotes.rst +++ clang/docs/ReleaseNotes.rst @@ -115,6 +115,32 @@ if the source header file is not self-contained. This option is enabled by default for clang-cl. +- -fpch-codegen and -fpch-debuginfo generate shared code and/or debuginfo + for contents of a precompiled header in a separate object file. This object + file needs to be linked in, but its contents do not need to be generated + for other objects using the precompiled header. This should usually save + compile time. If not using clang-cl, the separate object file needs to + be created explicitly from the precompiled header. + Example of use: + + .. code-block:: console + + $ clang++ -x c++-header header.h -o header.pch -fpch-codegen -fpch-debuginfo + $ clang++ -c header.pch -o shared.o + $ clang++ -c source.cpp -o source.o -include-pch header.pch + $ clang++ -o binary source.o shared.o + + - Using -fpch-instantiate-templates when generating the precompiled header + usually increases the amount of code/debuginfo that can be shared. + - In some cases, especially when building with optimizations enabled, using + -fpch-codegen may generate so much code in the shared object that compiling + it may be a net loss in build time. + - Since headers may bring in private symbols of other libraries, it may be + sometimes necessary to discard unused symbols (such as by adding + -Wl,--gc-sections on ELF platforms to the linking command, and possibly + adding -fdata-sections -ffunction-sections to the command generating + the shared object). + Deprecated Compiler Flags ------------------------- Index: clang/include/clang/Driver/Options.td =================================================================== --- clang/include/clang/Driver/Options.td +++ clang/include/clang/Driver/Options.td @@ -1440,6 +1440,10 @@ def fno_pch_instantiate_templates: Flag <["-"], "fno-pch-instantiate-templates">, Group, Flags<[CC1Option]>; +defm pch_codegen: OptInFFlag<"pch-codegen", "Generate ", "Do not generate ", + "code for uses of this PCH that assumes an explicit object file will be built for the PCH">; +defm pch_debuginfo: OptInFFlag<"pch-debuginfo", "Generate ", "Do not generate ", + "debug info for types in an object file built from this PCH and do not generate them elsewhere">; def fmodules : Flag <["-"], "fmodules">, Group, Flags<[DriverOption, CC1Option]>, Index: clang/lib/Driver/ToolChains/Clang.cpp =================================================================== --- clang/lib/Driver/ToolChains/Clang.cpp +++ clang/lib/Driver/ToolChains/Clang.cpp @@ -5627,6 +5627,12 @@ if (Args.hasFlag(options::OPT_fpch_instantiate_templates, options::OPT_fno_pch_instantiate_templates, false)) CmdArgs.push_back("-fpch-instantiate-templates"); + if (Args.hasFlag(options::OPT_fpch_codegen, options::OPT_fno_pch_codegen, + false)) + CmdArgs.push_back("-fmodules-codegen"); + if (Args.hasFlag(options::OPT_fpch_debuginfo, options::OPT_fno_pch_debuginfo, + false)) + CmdArgs.push_back("-fmodules-debuginfo"); Args.AddLastArg(CmdArgs, options::OPT_fexperimental_new_pass_manager, options::OPT_fno_experimental_new_pass_manager); Index: clang/test/Driver/pch-codegen.cpp =================================================================== --- clang/test/Driver/pch-codegen.cpp +++ clang/test/Driver/pch-codegen.cpp @@ -7,32 +7,32 @@ // CHECK-PCH-CREATE-NOT: -fmodules-codegen // CHECK-PCH-CREATE-NOT: -fmodules-debuginfo -// Create PCH with -fmodules-codegen. -// 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 +// Create PCH with -fpch-codegen. +// 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 // CHECK-PCH-CODEGEN-CREATE: -emit-pch // CHECK-PCH-CODEGEN-CREATE: -fmodules-codegen // CHECK-PCH-CODEGEN-CREATE: "-x" "c++-header" // CHECK-PCH-CODEGEN-CREATE-NOT: -fmodules-debuginfo // Actually create the PCH. -// RUN: %clang -x c++-header -Xclang -fmodules-codegen %S/../Modules/Inputs/codegen-flags/foo.h -o %t/foo-cg.pch +// RUN: %clang -x c++-header -fpch-codegen %S/../Modules/Inputs/codegen-flags/foo.h -o %t/foo-cg.pch -// Create PCH with -fmodules-debuginfo. -// 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 +// Create PCH with -fpch-debuginfo. +// 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 // CHECK-PCH-DEBUGINFO-CREATE: -emit-pch // CHECK-PCH-DEBUGINFO-CREATE: -fmodules-debuginfo // CHECK-PCH-DEBUGINFO-CREATE: "-x" "c++-header" // CHECK-PCH-DEBUGINFO-CREATE-NOT: -fmodules-codegen // Actually create the PCH. -// RUN: %clang -x c++-header -Xclang -fmodules-debuginfo %S/../Modules/Inputs/codegen-flags/foo.h -g -o %t/foo-di.pch +// RUN: %clang -x c++-header -fpch-debuginfo %S/../Modules/Inputs/codegen-flags/foo.h -g -o %t/foo-di.pch -// Create PCH's object file for -fmodules-codegen. +// Create PCH's object file for -fpch-codegen. // RUN: %clang -c %t/foo-cg.pch -o %t/foo-cg.o -### 2>&1 | FileCheck %s -check-prefix=CHECK-PCH-CODEGEN-OBJ // CHECK-PCH-CODEGEN-OBJ: -emit-obj // CHECK-PCH-CODEGEN-OBJ: "-main-file-name" "foo-cg.pch" // CHECK-PCH-CODEGEN-OBJ: "-o" "{{.*}}foo-cg.o" // CHECK-PCH-CODEGEN-OBJ: "-x" "precompiled-header" -// Create PCH's object file for -fmodules-debuginfo. +// Create PCH's object file for -fpch-debuginfo. // RUN: %clang -c %t/foo-di.pch -g -o %t/foo-di.o -### 2>&1 | FileCheck %s -check-prefix=CHECK-PCH-DEBUGINFO-OBJ // CHECK-PCH-DEBUGINFO-OBJ: -emit-obj // CHECK-PCH-DEBUGINFO-OBJ: "-main-file-name" "foo-di.pch"