Skip to content

Commit c33b81a

Browse files
committed
[clang] Improve nested name specifier AST representation
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: ![image](https://github.com/user-attachments/assets/700dce98-2cab-4aa8-97d1-b038c0bee831) 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 #147000
1 parent 2485c51 commit c33b81a

File tree

90 files changed

+4908
-4656
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

90 files changed

+4908
-4656
lines changed

clang/include/clang/AST/ASTConcept.h

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
#define LLVM_CLANG_AST_ASTCONCEPT_H
1616

1717
#include "clang/AST/DeclarationName.h"
18-
#include "clang/AST/NestedNameSpecifier.h"
18+
#include "clang/AST/NestedNameSpecifierBase.h"
1919
#include "clang/AST/TemplateBase.h"
2020
#include "clang/Basic/SourceLocation.h"
2121
#include "clang/Basic/UnsignedOrNone.h"
@@ -175,12 +175,7 @@ class ConceptReference {
175175

176176
SourceLocation getLocation() const { return getConceptNameLoc(); }
177177

178-
SourceLocation getBeginLoc() const LLVM_READONLY {
179-
// Note that if the qualifier is null the template KW must also be null.
180-
if (auto QualifierLoc = getNestedNameSpecifierLoc())
181-
return QualifierLoc.getBeginLoc();
182-
return getConceptNameInfo().getBeginLoc();
183-
}
178+
SourceLocation getBeginLoc() const LLVM_READONLY;
184179

185180
SourceLocation getEndLoc() const LLVM_READONLY {
186181
return getTemplateArgsAsWritten() &&

clang/include/clang/AST/ASTContext.h

Lines changed: 94 additions & 107 deletions
Large diffs are not rendered by default.

clang/include/clang/AST/ASTImporter.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ class TypeSourceInfo;
404404
///
405405
/// \returns The equivalent nested-name-specifier in the "to"
406406
/// context, or the import error.
407-
llvm::Expected<NestedNameSpecifier *> Import(NestedNameSpecifier *FromNNS);
407+
llvm::Expected<NestedNameSpecifier> Import(NestedNameSpecifier FromNNS);
408408

409409
/// Import the given nested-name-specifier-loc from the "from"
410410
/// context into the "to" context.

clang/include/clang/AST/ASTNodeTraverser.h

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -394,12 +394,14 @@ class ASTNodeTraverser
394394
}
395395
void VisitMemberPointerType(const MemberPointerType *T) {
396396
// FIXME: Provide a NestedNameSpecifier visitor.
397-
NestedNameSpecifier *Qualifier = T->getQualifier();
398-
if (NestedNameSpecifier::SpecifierKind K = Qualifier->getKind();
399-
K == NestedNameSpecifier::TypeSpec)
400-
Visit(Qualifier->getAsType());
397+
NestedNameSpecifier Qualifier = T->getQualifier();
398+
if (NestedNameSpecifier::Kind K = Qualifier.getKind();
399+
K == NestedNameSpecifier::Kind::Type)
400+
Visit(Qualifier.getAsType());
401401
if (T->isSugared())
402-
Visit(T->getMostRecentCXXRecordDecl()->getTypeForDecl());
402+
Visit(cast<MemberPointerType>(T->getCanonicalTypeUnqualified())
403+
->getQualifier()
404+
.getAsType());
403405
Visit(T->getPointeeType());
404406
}
405407
void VisitArrayType(const ArrayType *T) { Visit(T->getElementType()); }
@@ -510,7 +512,7 @@ class ASTNodeTraverser
510512
}
511513
void VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
512514
// FIXME: Provide NestedNamespecifierLoc visitor.
513-
Visit(TL.getQualifierLoc().getTypeLoc());
515+
Visit(TL.getQualifierLoc().castAsTypeLoc());
514516
}
515517
void VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
516518
Visit(TL.getSizeExpr());
@@ -772,17 +774,16 @@ class ASTNodeTraverser
772774
}
773775

774776
void VisitUsingShadowDecl(const UsingShadowDecl *D) {
775-
if (auto *TD = dyn_cast<TypeDecl>(D->getUnderlyingDecl()))
776-
Visit(TD->getTypeForDecl());
777+
Visit(D->getTargetDecl());
777778
}
778779

779780
void VisitFriendDecl(const FriendDecl *D) {
780781
if (D->getFriendType()) {
781782
// Traverse any CXXRecordDecl owned by this type, since
782783
// it will not be in the parent context:
783-
if (auto *ET = D->getFriendType()->getType()->getAs<ElaboratedType>())
784-
if (auto *TD = ET->getOwnedTagDecl())
785-
Visit(TD);
784+
if (auto *TT = D->getFriendType()->getType()->getAs<TagType>())
785+
if (TT->isTagOwned())
786+
Visit(TT->getOriginalDecl());
786787
} else {
787788
Visit(D->getFriendDecl());
788789
}

clang/include/clang/AST/ASTTypeTraits.h

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ class DynTypedNode {
307307

308308
/// For nodes which represent textual entities in the source code,
309309
/// return their SourceRange. For all other nodes, return SourceRange().
310-
SourceRange getSourceRange() const;
310+
SourceRange getSourceRange(bool IncludeQualifier = false) const;
311311

312312
/// @{
313313
/// Imposes an order on \c DynTypedNode.
@@ -336,9 +336,9 @@ class DynTypedNode {
336336
NodeKind)) {
337337
auto NNSLA = getUnchecked<NestedNameSpecifierLoc>();
338338
auto NNSLB = Other.getUnchecked<NestedNameSpecifierLoc>();
339-
return std::make_pair(NNSLA.getNestedNameSpecifier(),
339+
return std::make_pair(NNSLA.getNestedNameSpecifier().getAsVoidPointer(),
340340
NNSLA.getOpaqueData()) <
341-
std::make_pair(NNSLB.getNestedNameSpecifier(),
341+
std::make_pair(NNSLB.getNestedNameSpecifier().getAsVoidPointer(),
342342
NNSLB.getOpaqueData());
343343
}
344344

@@ -393,8 +393,9 @@ class DynTypedNode {
393393
if (ASTNodeKind::getFromNodeKind<NestedNameSpecifierLoc>().isSame(
394394
Val.NodeKind)) {
395395
auto NNSL = Val.getUnchecked<NestedNameSpecifierLoc>();
396-
return llvm::hash_combine(NNSL.getNestedNameSpecifier(),
397-
NNSL.getOpaqueData());
396+
return llvm::hash_combine(
397+
NNSL.getNestedNameSpecifier().getAsVoidPointer(),
398+
NNSL.getOpaqueData());
398399
}
399400

400401
assert(Val.getMemoizationData());
@@ -539,8 +540,8 @@ struct DynTypedNode::BaseConverter<
539540
: public DynCastPtrConverter<T, Attr> {};
540541

541542
template <>
542-
struct DynTypedNode::BaseConverter<
543-
NestedNameSpecifier, void> : public PtrConverter<NestedNameSpecifier> {};
543+
struct DynTypedNode::BaseConverter<NestedNameSpecifier, void>
544+
: public ValueConverter<NestedNameSpecifier> {};
544545

545546
template <>
546547
struct DynTypedNode::BaseConverter<

clang/include/clang/AST/AbstractBasicReader.h

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ class DataStreamBasicReader : public BasicReaderBase<Impl> {
197197
unsigned int_ = asImpl().readUInt32();
198198
Decl *decl = asImpl().template readDeclAs<Decl>();
199199
if (auto *recordDecl = dyn_cast<CXXRecordDecl>(decl))
200-
elemTy = getASTContext().getRecordType(recordDecl);
200+
elemTy = getASTContext().getCanonicalTagType(recordDecl);
201201
else
202202
elemTy = cast<ValueDecl>(decl)->getType();
203203
path.push_back(
@@ -252,44 +252,34 @@ class DataStreamBasicReader : public BasicReaderBase<Impl> {
252252
return EffectConditionExpr{asImpl().readExprRef()};
253253
}
254254

255-
NestedNameSpecifier *readNestedNameSpecifier() {
255+
NestedNameSpecifier readNestedNameSpecifier() {
256256
auto &ctx = getASTContext();
257257

258258
// We build this up iteratively.
259-
NestedNameSpecifier *cur = nullptr;
259+
NestedNameSpecifier cur = std::nullopt;
260260

261261
uint32_t depth = asImpl().readUInt32();
262262
for (uint32_t i = 0; i != depth; ++i) {
263263
auto kind = asImpl().readNestedNameSpecifierKind();
264264
switch (kind) {
265-
case NestedNameSpecifier::Identifier:
266-
cur = NestedNameSpecifier::Create(ctx, cur,
267-
asImpl().readIdentifier());
265+
case NestedNameSpecifier::Kind::Namespace:
266+
cur =
267+
NestedNameSpecifier(ctx, asImpl().readNamespaceBaseDeclRef(), cur);
268268
continue;
269-
270-
case NestedNameSpecifier::Namespace:
271-
cur = NestedNameSpecifier::Create(ctx, cur,
272-
asImpl().readNamespaceDeclRef());
273-
continue;
274-
275-
case NestedNameSpecifier::NamespaceAlias:
276-
cur = NestedNameSpecifier::Create(ctx, cur,
277-
asImpl().readNamespaceAliasDeclRef());
269+
case NestedNameSpecifier::Kind::Type:
270+
assert(!cur);
271+
cur = NestedNameSpecifier(asImpl().readQualType().getTypePtr());
278272
continue;
279-
280-
case NestedNameSpecifier::TypeSpec:
281-
cur = NestedNameSpecifier::Create(ctx, cur,
282-
asImpl().readQualType().getTypePtr());
273+
case NestedNameSpecifier::Kind::Global:
274+
assert(!cur);
275+
cur = NestedNameSpecifier::getGlobal();
283276
continue;
284-
285-
case NestedNameSpecifier::Global:
286-
cur = NestedNameSpecifier::GlobalSpecifier(ctx);
287-
continue;
288-
289-
case NestedNameSpecifier::Super:
290-
cur = NestedNameSpecifier::SuperSpecifier(ctx,
291-
asImpl().readCXXRecordDeclRef());
277+
case NestedNameSpecifier::Kind::Super:
278+
assert(!cur);
279+
cur = NestedNameSpecifier(asImpl().readCXXRecordDeclRef());
292280
continue;
281+
case NestedNameSpecifier::Kind::Null:
282+
llvm_unreachable("unexpected null nested name specifier");
293283
}
294284
llvm_unreachable("bad nested name specifier kind");
295285
}

clang/include/clang/AST/AbstractBasicWriter.h

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ class DataStreamBasicWriter : public BasicWriterBase<Impl> {
181181
const Decl *baseOrMember = elem.getAsBaseOrMember().getPointer();
182182
if (const auto *recordDecl = dyn_cast<CXXRecordDecl>(baseOrMember)) {
183183
asImpl().writeDeclRef(recordDecl);
184-
elemTy = ctx.getRecordType(recordDecl);
184+
elemTy = ctx.getCanonicalTagType(recordDecl);
185185
} else {
186186
const auto *valueDecl = cast<ValueDecl>(baseOrMember);
187187
asImpl().writeDeclRef(valueDecl);
@@ -229,46 +229,44 @@ class DataStreamBasicWriter : public BasicWriterBase<Impl> {
229229
asImpl().writeExprRef(CE.getCondition());
230230
}
231231

232-
void writeNestedNameSpecifier(NestedNameSpecifier *NNS) {
232+
void writeNestedNameSpecifier(NestedNameSpecifier NNS) {
233233
// Nested name specifiers usually aren't too long. I think that 8 would
234234
// typically accommodate the vast majority.
235-
SmallVector<NestedNameSpecifier *, 8> nestedNames;
235+
SmallVector<NestedNameSpecifier, 8> nestedNames;
236236

237237
// Push each of the NNS's onto a stack for serialization in reverse order.
238238
while (NNS) {
239239
nestedNames.push_back(NNS);
240-
NNS = NNS->getPrefix();
240+
NNS = NNS.getKind() == NestedNameSpecifier::Kind::Namespace
241+
? NNS.getAsNamespaceAndPrefix().Prefix
242+
: std::nullopt;
241243
}
242244

243245
asImpl().writeUInt32(nestedNames.size());
244246
while (!nestedNames.empty()) {
245247
NNS = nestedNames.pop_back_val();
246-
NestedNameSpecifier::SpecifierKind kind = NNS->getKind();
248+
NestedNameSpecifier::Kind kind = NNS.getKind();
247249
asImpl().writeNestedNameSpecifierKind(kind);
248250
switch (kind) {
249-
case NestedNameSpecifier::Identifier:
250-
asImpl().writeIdentifier(NNS->getAsIdentifier());
251+
case NestedNameSpecifier::Kind::Namespace:
252+
asImpl().writeNamespaceBaseDeclRef(
253+
NNS.getAsNamespaceAndPrefix().Namespace);
251254
continue;
252255

253-
case NestedNameSpecifier::Namespace:
254-
asImpl().writeNamespaceDeclRef(NNS->getAsNamespace());
256+
case NestedNameSpecifier::Kind::Type:
257+
asImpl().writeQualType(QualType(NNS.getAsType(), 0));
255258
continue;
256259

257-
case NestedNameSpecifier::NamespaceAlias:
258-
asImpl().writeNamespaceAliasDeclRef(NNS->getAsNamespaceAlias());
259-
continue;
260-
261-
case NestedNameSpecifier::TypeSpec:
262-
asImpl().writeQualType(QualType(NNS->getAsType(), 0));
263-
continue;
264-
265-
case NestedNameSpecifier::Global:
260+
case NestedNameSpecifier::Kind::Global:
266261
// Don't need to write an associated value.
267262
continue;
268263

269-
case NestedNameSpecifier::Super:
270-
asImpl().writeDeclRef(NNS->getAsRecordDecl());
264+
case NestedNameSpecifier::Kind::Super:
265+
asImpl().writeDeclRef(NNS.getAsSuper());
271266
continue;
267+
268+
case NestedNameSpecifier::Kind::Null:
269+
llvm_unreachable("unexpected null nested name specifier");
272270
}
273271
llvm_unreachable("bad nested name specifier kind");
274272
}

clang/include/clang/AST/CanonicalType.h

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ template<>
453453
struct CanProxyAdaptor<MemberPointerType>
454454
: public CanProxyBase<MemberPointerType> {
455455
LLVM_CLANG_CANPROXY_TYPE_ACCESSOR(getPointeeType)
456-
LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(NestedNameSpecifier *, getQualifier)
456+
LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(NestedNameSpecifier, getQualifier)
457457
LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(const CXXRecordDecl *,
458458
getMostRecentCXXRecordDecl)
459459
};
@@ -551,21 +551,18 @@ struct CanProxyAdaptor<UnaryTransformType>
551551

552552
template<>
553553
struct CanProxyAdaptor<TagType> : public CanProxyBase<TagType> {
554-
LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(TagDecl *, getDecl)
555-
LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(bool, isBeingDefined)
554+
LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(TagDecl *, getOriginalDecl)
556555
};
557556

558557
template<>
559558
struct CanProxyAdaptor<RecordType> : public CanProxyBase<RecordType> {
560-
LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(RecordDecl *, getDecl)
561-
LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(bool, isBeingDefined)
559+
LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(RecordDecl *, getOriginalDecl)
562560
LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(bool, hasConstFields)
563561
};
564562

565563
template<>
566564
struct CanProxyAdaptor<EnumType> : public CanProxyBase<EnumType> {
567-
LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(EnumDecl *, getDecl)
568-
LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(bool, isBeingDefined)
565+
LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(EnumDecl *, getOriginalDecl)
569566
};
570567

571568
template<>

0 commit comments

Comments
 (0)