
This is a major change on how we represent nested name qualifications in the AST. * The nested name specifier itself and how it's stored is changed. The prefixes for types are handled within the type hierarchy, which makes canonicalization for them super cheap, no memory allocation required. Also translating a type into nested name specifier form becomes a no-op. An identifier is stored as a DependentNameType. The nested name specifier gains a lightweight handle class, to be used instead of passing around pointers, which is similar to what is implemented for TemplateName. There is still one free bit available, and this handle can be used within a PointerUnion and PointerIntPair, which should keep bit-packing aficionados happy. * The ElaboratedType node is removed, all type nodes in which it could previously apply to can now store the elaborated keyword and name qualifier, tail allocating when present. * TagTypes can now point to the exact declaration found when producing these, as opposed to the previous situation of there only existing one TagType per entity. This increases the amount of type sugar retained, and can have several applications, for example in tracking module ownership, and other tools which care about source file origins, such as IWYU. These TagTypes are lazily allocated, in order to limit the increase in AST size. This patch offers a great performance benefit. It greatly improves compilation time for [stdexec](https://github.com/NVIDIA/stdexec). For one datapoint, for `test_on2.cpp` in that project, which is the slowest compiling test, this patch improves `-c` compilation time by about 7.2%, with the `-fsyntax-only` improvement being at ~12%. This has great results on compile-time-tracker as well:  This patch also further enables other optimziations in the future, and will reduce the performance impact of template specialization resugaring when that lands. It has some other miscelaneous drive-by fixes. About the review: Yes the patch is huge, sorry about that. Part of the reason is that I started by the nested name specifier part, before the ElaboratedType part, but that had a huge performance downside, as ElaboratedType is a big performance hog. I didn't have the steam to go back and change the patch after the fact. There is also a lot of internal API changes, and it made sense to remove ElaboratedType in one go, versus removing it from one type at a time, as that would present much more churn to the users. Also, the nested name specifier having a different API avoids missing changes related to how prefixes work now, which could make existing code compile but not work. How to review: The important changes are all in `clang/include/clang/AST` and `clang/lib/AST`, with also important changes in `clang/lib/Sema/TreeTransform.h`. The rest and bulk of the changes are mostly consequences of the changes in API. PS: TagType::getDecl is renamed to `getOriginalDecl` in this patch, just for easier to rebasing. I plan to rename it back after this lands. Fixes #136624 Fixes https://github.com/llvm/llvm-project/issues/43179 Fixes https://github.com/llvm/llvm-project/issues/68670 Fixes https://github.com/llvm/llvm-project/issues/92757
218 lines
7.4 KiB
C++
218 lines
7.4 KiB
C++
//===- VTTBuilder.cpp - C++ VTT layout builder ----------------------------===//
|
|
//
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This contains code dealing with generation of the layout of virtual table
|
|
// tables (VTT).
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "clang/AST/VTTBuilder.h"
|
|
#include "clang/AST/ASTContext.h"
|
|
#include "clang/AST/BaseSubobject.h"
|
|
#include "clang/AST/CharUnits.h"
|
|
#include "clang/AST/Decl.h"
|
|
#include "clang/AST/DeclCXX.h"
|
|
#include "clang/AST/RecordLayout.h"
|
|
#include "clang/AST/Type.h"
|
|
#include "clang/Basic/LLVM.h"
|
|
#include <cassert>
|
|
#include <cstdint>
|
|
|
|
using namespace clang;
|
|
|
|
#define DUMP_OVERRIDERS 0
|
|
|
|
VTTBuilder::VTTBuilder(ASTContext &Ctx,
|
|
const CXXRecordDecl *MostDerivedClass,
|
|
bool GenerateDefinition)
|
|
: Ctx(Ctx), MostDerivedClass(MostDerivedClass),
|
|
MostDerivedClassLayout(Ctx.getASTRecordLayout(MostDerivedClass)),
|
|
GenerateDefinition(GenerateDefinition) {
|
|
// Lay out this VTT.
|
|
LayoutVTT(BaseSubobject(MostDerivedClass, CharUnits::Zero()),
|
|
/*BaseIsVirtual=*/false);
|
|
}
|
|
|
|
void VTTBuilder::AddVTablePointer(BaseSubobject Base, uint64_t VTableIndex,
|
|
const CXXRecordDecl *VTableClass) {
|
|
// Store the vtable pointer index if we're generating the primary VTT.
|
|
if (VTableClass == MostDerivedClass) {
|
|
assert(!SecondaryVirtualPointerIndices.count(Base) &&
|
|
"A virtual pointer index already exists for this base subobject!");
|
|
SecondaryVirtualPointerIndices[Base] = VTTComponents.size();
|
|
}
|
|
|
|
if (!GenerateDefinition) {
|
|
VTTComponents.push_back(VTTComponent());
|
|
return;
|
|
}
|
|
|
|
VTTComponents.push_back(VTTComponent(VTableIndex, Base));
|
|
}
|
|
|
|
void VTTBuilder::LayoutSecondaryVTTs(BaseSubobject Base) {
|
|
const CXXRecordDecl *RD = Base.getBase();
|
|
|
|
for (const auto &I : RD->bases()) {
|
|
// Don't layout virtual bases.
|
|
if (I.isVirtual())
|
|
continue;
|
|
|
|
const auto *BaseDecl =
|
|
cast<CXXRecordDecl>(
|
|
I.getType()->castAs<RecordType>()->getOriginalDecl())
|
|
->getDefinitionOrSelf();
|
|
|
|
const ASTRecordLayout &Layout = Ctx.getASTRecordLayout(RD);
|
|
CharUnits BaseOffset = Base.getBaseOffset() +
|
|
Layout.getBaseClassOffset(BaseDecl);
|
|
|
|
// Layout the VTT for this base.
|
|
LayoutVTT(BaseSubobject(BaseDecl, BaseOffset), /*BaseIsVirtual=*/false);
|
|
}
|
|
}
|
|
|
|
void
|
|
VTTBuilder::LayoutSecondaryVirtualPointers(BaseSubobject Base,
|
|
bool BaseIsMorallyVirtual,
|
|
uint64_t VTableIndex,
|
|
const CXXRecordDecl *VTableClass,
|
|
VisitedVirtualBasesSetTy &VBases) {
|
|
const CXXRecordDecl *RD = Base.getBase();
|
|
|
|
// We're not interested in bases that don't have virtual bases, and not
|
|
// morally virtual bases.
|
|
if (!RD->getNumVBases() && !BaseIsMorallyVirtual)
|
|
return;
|
|
|
|
for (const auto &I : RD->bases()) {
|
|
const auto *BaseDecl =
|
|
cast<CXXRecordDecl>(
|
|
I.getType()->castAs<RecordType>()->getOriginalDecl())
|
|
->getDefinitionOrSelf();
|
|
|
|
// Itanium C++ ABI 2.6.2:
|
|
// Secondary virtual pointers are present for all bases with either
|
|
// virtual bases or virtual function declarations overridden along a
|
|
// virtual path.
|
|
//
|
|
// If the base class is not dynamic, we don't want to add it, nor any
|
|
// of its base classes.
|
|
if (!BaseDecl->isDynamicClass())
|
|
continue;
|
|
|
|
bool BaseDeclIsMorallyVirtual = BaseIsMorallyVirtual;
|
|
bool BaseDeclIsNonVirtualPrimaryBase = false;
|
|
CharUnits BaseOffset;
|
|
if (I.isVirtual()) {
|
|
// Ignore virtual bases that we've already visited.
|
|
if (!VBases.insert(BaseDecl).second)
|
|
continue;
|
|
|
|
BaseOffset = MostDerivedClassLayout.getVBaseClassOffset(BaseDecl);
|
|
BaseDeclIsMorallyVirtual = true;
|
|
} else {
|
|
const ASTRecordLayout &Layout = Ctx.getASTRecordLayout(RD);
|
|
|
|
BaseOffset = Base.getBaseOffset() +
|
|
Layout.getBaseClassOffset(BaseDecl);
|
|
|
|
if (!Layout.isPrimaryBaseVirtual() &&
|
|
Layout.getPrimaryBase() == BaseDecl)
|
|
BaseDeclIsNonVirtualPrimaryBase = true;
|
|
}
|
|
|
|
// Itanium C++ ABI 2.6.2:
|
|
// Secondary virtual pointers: for each base class X which (a) has virtual
|
|
// bases or is reachable along a virtual path from D, and (b) is not a
|
|
// non-virtual primary base, the address of the virtual table for X-in-D
|
|
// or an appropriate construction virtual table.
|
|
if (!BaseDeclIsNonVirtualPrimaryBase &&
|
|
(BaseDecl->getNumVBases() || BaseDeclIsMorallyVirtual)) {
|
|
// Add the vtable pointer.
|
|
AddVTablePointer(BaseSubobject(BaseDecl, BaseOffset), VTableIndex,
|
|
VTableClass);
|
|
}
|
|
|
|
// And lay out the secondary virtual pointers for the base class.
|
|
LayoutSecondaryVirtualPointers(BaseSubobject(BaseDecl, BaseOffset),
|
|
BaseDeclIsMorallyVirtual, VTableIndex,
|
|
VTableClass, VBases);
|
|
}
|
|
}
|
|
|
|
void
|
|
VTTBuilder::LayoutSecondaryVirtualPointers(BaseSubobject Base,
|
|
uint64_t VTableIndex) {
|
|
VisitedVirtualBasesSetTy VBases;
|
|
LayoutSecondaryVirtualPointers(Base, /*BaseIsMorallyVirtual=*/false,
|
|
VTableIndex, Base.getBase(), VBases);
|
|
}
|
|
|
|
void VTTBuilder::LayoutVirtualVTTs(const CXXRecordDecl *RD,
|
|
VisitedVirtualBasesSetTy &VBases) {
|
|
for (const auto &I : RD->bases()) {
|
|
const auto *BaseDecl =
|
|
cast<CXXRecordDecl>(
|
|
I.getType()->castAs<RecordType>()->getOriginalDecl())
|
|
->getDefinitionOrSelf();
|
|
|
|
// Check if this is a virtual base.
|
|
if (I.isVirtual()) {
|
|
// Check if we've seen this base before.
|
|
if (!VBases.insert(BaseDecl).second)
|
|
continue;
|
|
|
|
CharUnits BaseOffset =
|
|
MostDerivedClassLayout.getVBaseClassOffset(BaseDecl);
|
|
|
|
LayoutVTT(BaseSubobject(BaseDecl, BaseOffset), /*BaseIsVirtual=*/true);
|
|
}
|
|
|
|
// We only need to layout virtual VTTs for this base if it actually has
|
|
// virtual bases.
|
|
if (BaseDecl->getNumVBases())
|
|
LayoutVirtualVTTs(BaseDecl, VBases);
|
|
}
|
|
}
|
|
|
|
void VTTBuilder::LayoutVTT(BaseSubobject Base, bool BaseIsVirtual) {
|
|
const CXXRecordDecl *RD = Base.getBase();
|
|
|
|
// Itanium C++ ABI 2.6.2:
|
|
// An array of virtual table addresses, called the VTT, is declared for
|
|
// each class type that has indirect or direct virtual base classes.
|
|
if (RD->getNumVBases() == 0)
|
|
return;
|
|
|
|
bool IsPrimaryVTT = Base.getBase() == MostDerivedClass;
|
|
|
|
if (!IsPrimaryVTT) {
|
|
// Remember the sub-VTT index.
|
|
SubVTTIndices[Base] = VTTComponents.size();
|
|
}
|
|
|
|
uint64_t VTableIndex = VTTVTables.size();
|
|
VTTVTables.push_back(VTTVTable(Base, BaseIsVirtual));
|
|
|
|
// Add the primary vtable pointer.
|
|
AddVTablePointer(Base, VTableIndex, RD);
|
|
|
|
// Add the secondary VTTs.
|
|
LayoutSecondaryVTTs(Base);
|
|
|
|
// Add the secondary virtual pointers.
|
|
LayoutSecondaryVirtualPointers(Base, VTableIndex);
|
|
|
|
// If this is the primary VTT, we want to lay out virtual VTTs as well.
|
|
if (IsPrimaryVTT) {
|
|
VisitedVirtualBasesSetTy VBases;
|
|
LayoutVirtualVTTs(Base.getBase(), VBases);
|
|
}
|
|
}
|