Index: clang/include/clang/Basic/LangOptions.def =================================================================== --- clang/include/clang/Basic/LangOptions.def +++ clang/include/clang/Basic/LangOptions.def @@ -160,6 +160,7 @@ BENIGN_LANGOPT(CompilingPCH, 1, 0, "building a pch") BENIGN_LANGOPT(BuildingPCHWithObjectFile, 1, 0, "building a pch which has a corresponding object file") BENIGN_LANGOPT(CacheGeneratedPCH, 1, 0, "cache generated PCH files in memory") +BENIGN_LANGOPT(PCHInstantiateTemplates, 1, 0, "performing pending template instantiations already while building a pch") COMPATIBLE_LANGOPT(ModulesDeclUse , 1, 0, "require declaration of module uses") BENIGN_LANGOPT(ModulesSearchAll , 1, 1, "searching even non-imported modules to find unresolved references") COMPATIBLE_LANGOPT(ModulesStrictDeclUse, 1, 0, "requiring declaration of module uses and all headers to be in modules") Index: clang/include/clang/Driver/CC1Options.td =================================================================== --- clang/include/clang/Driver/CC1Options.td +++ clang/include/clang/Driver/CC1Options.td @@ -672,6 +672,8 @@ HelpText<"Disable inclusion of timestamp in precompiled headers">; def building_pch_with_obj : Flag<["-"], "building-pch-with-obj">, HelpText<"This compilation is part of building a PCH with corresponding object file.">; +def pch_instantiate_templates : Flag<["-"], "pch-instantiate-templates">, + HelpText<"Perform pending template instantiations already while building the PCH.">; def aligned_alloc_unavailable : Flag<["-"], "faligned-alloc-unavailable">, HelpText<"Aligned allocation/deallocation functions are unavailable">; Index: clang/lib/Frontend/CompilerInvocation.cpp =================================================================== --- clang/lib/Frontend/CompilerInvocation.cpp +++ clang/lib/Frontend/CompilerInvocation.cpp @@ -3223,6 +3223,7 @@ Opts.CompleteMemberPointers = Args.hasArg(OPT_fcomplete_member_pointers); Opts.BuildingPCHWithObjectFile = Args.hasArg(OPT_building_pch_with_obj); + Opts.PCHInstantiateTemplates = Args.hasArg(OPT_pch_instantiate_templates); } static bool isStrictlyPreprocessorAction(frontend::ActionKind Action) { Index: clang/lib/Sema/Sema.cpp =================================================================== --- clang/lib/Sema/Sema.cpp +++ clang/lib/Sema/Sema.cpp @@ -981,6 +981,14 @@ LateParsedInstantiations.begin(), LateParsedInstantiations.end()); LateParsedInstantiations.clear(); + + // FIXME: Instantiating implicit templates already in the PCH breaks some + // OpenMP-specific code paths, see https://reviews.llvm.org/D69585 . + if(LangOpts.PCHInstantiateTemplates && !LangOpts.OpenMP) { + llvm::TimeTraceScope TimeScope("PerformPendingInstantiations", + StringRef("")); + PerformPendingInstantiations(); + } } DiagnoseUnterminatedPragmaPack();