mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-24 21:10:12 +01:00
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
147 lines
4.5 KiB
C++
147 lines
4.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
|
|
|
|
#include "toolchain/sem_ir/clang_decl.h"
|
|
|
|
#include "clang/AST/DeclBase.h"
|
|
#include "clang/AST/TextNodeDumper.h"
|
|
#include "common/ostream.h"
|
|
#include "common/raw_string_ostream.h"
|
|
#include "toolchain/base/canonical_value_store_impl.h"
|
|
#include "toolchain/base/value_store_impl.h"
|
|
|
|
namespace Carbon::SemIR {
|
|
|
|
auto ClangDeclSignature::Print(llvm::raw_ostream& out) const -> void {
|
|
out << "{kind: ";
|
|
switch (kind) {
|
|
case Normal:
|
|
out << "normal";
|
|
break;
|
|
case TuplePattern:
|
|
out << "tuple";
|
|
break;
|
|
}
|
|
out << ", num_params: " << num_params;
|
|
|
|
auto print_mode = [&](PassingMode mode) {
|
|
switch (mode) {
|
|
case PassingMode::ByValue:
|
|
out << "value";
|
|
break;
|
|
case PassingMode::ByVar:
|
|
out << "var";
|
|
break;
|
|
case PassingMode::ByRef:
|
|
out << "ref";
|
|
break;
|
|
}
|
|
};
|
|
|
|
if (!passing_modes.empty() && llvm::any_of(passing_modes, [](auto mode) {
|
|
return mode != PassingMode::ByVar;
|
|
})) {
|
|
out << ", modes: [";
|
|
llvm::ListSeparator sep;
|
|
for (auto mode : passing_modes) {
|
|
out << sep;
|
|
print_mode(mode);
|
|
}
|
|
out << "]";
|
|
}
|
|
if (self_passing_mode != PassingMode::ByRef) {
|
|
out << ", self_mode: ";
|
|
print_mode(self_passing_mode);
|
|
}
|
|
out << "}";
|
|
}
|
|
|
|
auto ClangDeclKey::ForFunctionDecl(clang::FunctionDecl* decl,
|
|
ClangDeclSignatureId signature_id)
|
|
-> ClangDeclKey {
|
|
return ClangDeclKey(decl, signature_id, UncheckedTag());
|
|
}
|
|
|
|
auto ClangDeclKey::ForNonFunctionDecl(clang::Decl* decl) -> ClangDeclKey {
|
|
CARBON_CHECK(!isa<clang::FunctionDecl>(decl));
|
|
return ClangDeclKey(decl, ClangDeclSignatureId::None, UncheckedTag());
|
|
}
|
|
|
|
ClangDeclKey::ClangDeclKey(clang::Decl* decl, ClangDeclSignatureId signature_id,
|
|
UncheckedTag /*_*/)
|
|
: decl(decl->getCanonicalDecl()), signature_id(signature_id) {}
|
|
|
|
auto ClangDeclKey::Print(llvm::raw_ostream& out) const -> void {
|
|
RawStringOstream decl_stream;
|
|
auto policy = decl->getASTContext().getPrintingPolicy();
|
|
policy.TerseOutput = true;
|
|
if (isa<clang::TranslationUnitDecl>(decl)) {
|
|
decl_stream << "<translation unit>";
|
|
} else {
|
|
decl->print(decl_stream, policy);
|
|
}
|
|
|
|
out << "{decl: \"" << FormatEscaped(decl_stream.TakeStr()) << "\"";
|
|
if (signature_id != ClangDeclSignatureId::None) {
|
|
out << ", clang_decl_signature_id: " << signature_id;
|
|
}
|
|
out << "}";
|
|
}
|
|
|
|
auto ClangDecl::Print(llvm::raw_ostream& out) const -> void {
|
|
out << "{key: " << key << ", inst_id: " << inst_id << "}";
|
|
}
|
|
|
|
ClangDeclStore::ClangDeclStore(CheckIRId check_ir_id) : values_(check_ir_id) {}
|
|
|
|
auto ClangDeclStore::Add(ClangDecl value) -> ClangDeclId {
|
|
CARBON_CHECK(!isa<clang::VarDecl>(value.decl()));
|
|
auto id = values_.Add(value);
|
|
inst_id_to_clang_decl_id_.Insert(value.inst_id, id);
|
|
return id;
|
|
}
|
|
|
|
auto ClangDeclStore::AddVar(ClangDecl value, InstId pattern_id) -> ClangDeclId {
|
|
CARBON_CHECK(isa<clang::VarDecl>(value.decl()));
|
|
auto id = values_.Add(value);
|
|
inst_id_to_clang_decl_id_.Insert(pattern_id, id);
|
|
return id;
|
|
}
|
|
|
|
auto ClangDeclStore::LookupId(ClangDeclKey key) const -> ClangDeclId {
|
|
return values_.Lookup(key);
|
|
}
|
|
|
|
auto ClangDeclStore::Lookup(InstId inst_id) const -> const ClangDecl* {
|
|
if (auto result = inst_id_to_clang_decl_id_.Lookup(inst_id)) {
|
|
return &Get(result.value());
|
|
}
|
|
return nullptr;
|
|
}
|
|
|
|
auto ClangDeclStore::OutputYaml() const -> Yaml::OutputMapping {
|
|
return values_.OutputYaml();
|
|
}
|
|
|
|
auto ClangDeclStore::CollectMemUsage(MemUsage& mem_usage,
|
|
llvm::StringRef label) const -> void {
|
|
values_.CollectMemUsage(mem_usage, label);
|
|
mem_usage.Collect(MemUsage::ConcatLabel(label, "inst_id_to_clang_decl_id_"),
|
|
inst_id_to_clang_decl_id_);
|
|
}
|
|
|
|
} // namespace Carbon::SemIR
|
|
|
|
namespace Carbon {
|
|
template class CanonicalValueStore<SemIR::ClangDeclId, SemIR::ClangDeclKey,
|
|
Tag<SemIR::CheckIRId>, SemIR::ClangDecl>;
|
|
template class ValueStore<SemIR::ClangDeclId, SemIR::ClangDecl,
|
|
Tag<SemIR::CheckIRId>>;
|
|
template class CanonicalValueStore<
|
|
SemIR::ClangDeclSignatureId, SemIR::ClangDeclSignature,
|
|
Tag<SemIR::CheckIRId>, SemIR::ClangDeclSignature>;
|
|
template class ValueStore<SemIR::ClangDeclSignatureId,
|
|
SemIR::ClangDeclSignature, Tag<SemIR::CheckIRId>>;
|
|
} // namespace Carbon
|