Files
carbon-lang/toolchain/sem_ir/cpp_file.h
T
Chandler Carruth 7901fb3857 Don't include expensive Clang headers in widely-included headers (#7319)
Fundamentally, this uses forward declarations of Clang types to reduce
the overall compile time cost of Clang headers across the codebase.

Tracing and profiling showed ~2s of every check TU's ~8-12s compile time
going just to parsing Clang frontend and AST headers pulled in via a few
sem_ir and check headers that only use the Clang types by pointer or
reference:

- sem_ir/cpp_file.h (reached via sem_ir/file.h by ~150 TUs) included
clang/Frontend/CompilerInstance.h, clang/CodeGen/ModuleBuilder.h,
clang/AST/Mangle.h, and llvm/IR/Module.h. CppFile's accessors move out
of line to a new cpp_file.cpp and the header now forward-declares the
Clang types.
- check/cpp/context.h (reached via check/context.h by ~100 TUs) included
clang/Frontend/FrontendAction.h and clang/Parse/Parser.h, pulling in
clang's Sema.h and ASTUnit.h.
- sem_ir/clang_decl.h included clang/AST/Decl.h; the three small
functions that need complete Clang types move out of line.
- sem_ir/cpp_overload_set.h included clang/Sema/Overload.h solely for
the three-field OverloadCandidateSet::OperatorRewriteInfo, which is now
mirrored as CppOverloadSet::OperatorRewriteInfo, and clang/AST/Decl.h
solely for a pointer.
- sem_ir/name_scope.h's clang/AST/DeclBase.h include was vestigial.

TUs (and more narrowly included headers) that genuinely use the Clang
definitions now include the Clang headers directly.

Representative compile times (fastbuild, aarch64), combined with the
preceding instantiation-cost changes, relative to trunk:
- check/eval.cpp: 11.85s -> 6.94s (-41%)
- check/handle_operator.cpp: 7.71s -> 3.30s (-57%)
- language_server.cpp: 6.68s -> 3.16s (-53%)
- lower/handle.cpp: 6.75s -> 3.66s (-46%)
- sem_ir/file.cpp: 8.60s -> 6.11s (-29%)
- driver.cpp: 6.68s -> 4.78s (-28%)

Measured full-rebuild impact (316 first-party TUs, fastbuild): -689.5s
CPU, -29.9% relative to trunk.

Assisted-by: Claude
2026-06-07 16:27:22 +00:00

77 lines
2.5 KiB
C++

// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#ifndef CARBON_TOOLCHAIN_SEM_IR_CPP_FILE_H_
#define CARBON_TOOLCHAIN_SEM_IR_CPP_FILE_H_
#include <memory>
namespace clang {
class ASTContext;
class CodeGenerator;
class CompilerInstance;
class DiagnosticOptions;
class DiagnosticsEngine;
class LangOptions;
class MangleContext;
class SourceManager;
} // namespace clang
namespace llvm {
class LLVMContext;
} // namespace llvm
namespace Carbon::SemIR {
// The result of compiling the C++ portion of a `File`, including both any
// imported C++ headers and any inline C++ fragments.
class CppFile {
public:
explicit CppFile(std::unique_ptr<clang::CompilerInstance> clang,
llvm::LLVMContext* llvm_context);
~CppFile();
// Access to compilation options.
auto diagnostic_options() const -> const clang::DiagnosticOptions&;
auto lang_options() const -> const clang::LangOptions&;
// Access to Clang's compilation environment.
auto source_manager() -> clang::SourceManager&;
auto source_manager() const -> const clang::SourceManager&;
// TODO: This doesn't really belong here, but is currently used by lowering
// because Clang's code generation may produce diagnostics.
auto diagnostics() const -> clang::DiagnosticsEngine&;
// Access to layers of Clang's C++ representation.
auto ast_context() -> clang::ASTContext&;
auto ast_context() const -> const clang::ASTContext&;
// Creates the mangle context for this file's C++ AST. Must be called once the
// AST context is available (after the frontend begins the source file) and
// before `mangle_context()` is used.
auto CreateMangleContext() -> void;
auto mangle_context() const -> clang::MangleContext&;
auto llvm_context() const -> llvm::LLVMContext* { return llvm_context_; }
auto SetCodeGenerator(clang::CodeGenerator* code_generator) -> void {
code_generator_ = code_generator;
}
auto GetCodeGenerator() const -> clang::CodeGenerator* {
// Clang code generation should not actually modify the AST, but isn't
// const-correct.
return code_generator_;
}
private:
std::unique_ptr<clang::CompilerInstance> clang_;
llvm::LLVMContext* llvm_context_;
clang::CodeGenerator* code_generator_ = nullptr;
// Created by `CreateMangleContext()` once the AST context is available.
std::unique_ptr<clang::MangleContext> mangle_context_;
};
} // namespace Carbon::SemIR
#endif // CARBON_TOOLCHAIN_SEM_IR_CPP_FILE_H_