https://github.com/ChuanqiXu9 updated https://github.com/llvm/llvm-project/pull/83108
>From 1f7c6650f739092a1b8908de3793aa8bbdc7de34 Mon Sep 17 00:00:00 2001 From: Vassil Vassilev <v.g.vassi...@gmail.com> Date: Sun, 7 Jan 2018 15:16:11 +0200 Subject: [PATCH 1/2] D41416: [modules] [pch] Do not deserialize all lazy template specializations when looking for one. --- clang/include/clang/AST/DeclTemplate.h | 36 +++++++- clang/lib/AST/DeclTemplate.cpp | 100 +++++++++++++++++----- clang/lib/AST/ODRHash.cpp | 15 ++++ clang/lib/Serialization/ASTReader.cpp | 25 ++++-- clang/lib/Serialization/ASTReaderDecl.cpp | 46 ++++++---- clang/lib/Serialization/ASTWriter.cpp | 21 ++++- clang/lib/Serialization/ASTWriterDecl.cpp | 76 +++++++++++++--- clang/test/Modules/cxx-templates.cpp | 8 +- clang/test/Modules/odr_hash.cpp | 4 +- 9 files changed, 255 insertions(+), 76 deletions(-) diff --git a/clang/include/clang/AST/DeclTemplate.h b/clang/include/clang/AST/DeclTemplate.h index 5b6a6b40b28ef..d76c293706369 100644 --- a/clang/include/clang/AST/DeclTemplate.h +++ b/clang/include/clang/AST/DeclTemplate.h @@ -256,6 +256,9 @@ class TemplateArgumentList final TemplateArgumentList(const TemplateArgumentList &) = delete; TemplateArgumentList &operator=(const TemplateArgumentList &) = delete; + /// Create hash for the given arguments. + static unsigned ComputeODRHash(ArrayRef<TemplateArgument> Args); + /// Create a new template argument list that copies the given set of /// template arguments. static TemplateArgumentList *CreateCopy(ASTContext &Context, @@ -729,6 +732,26 @@ class RedeclarableTemplateDecl : public TemplateDecl, } void anchor() override; + struct LazySpecializationInfo { + GlobalDeclID DeclID = GlobalDeclID(); + unsigned ODRHash = ~0U; + bool IsPartial = false; + LazySpecializationInfo(GlobalDeclID ID, unsigned Hash = ~0U, + bool Partial = false) + : DeclID(ID), ODRHash(Hash), IsPartial(Partial) { } + LazySpecializationInfo() { } + bool operator<(const LazySpecializationInfo &Other) const { + return DeclID < Other.DeclID; + } + bool operator==(const LazySpecializationInfo &Other) const { + assert((DeclID != Other.DeclID || ODRHash == Other.ODRHash) && + "Hashes differ!"); + assert((DeclID != Other.DeclID || IsPartial == Other.IsPartial) && + "Both must be the same kinds!"); + return DeclID == Other.DeclID; + } + }; + protected: template <typename EntryType> struct SpecEntryTraits { using DeclType = EntryType; @@ -769,7 +792,12 @@ class RedeclarableTemplateDecl : public TemplateDecl, return SpecIterator<EntryType>(isEnd ? Specs.end() : Specs.begin()); } - void loadLazySpecializationsImpl() const; + void loadLazySpecializationsImpl(bool OnlyPartial = false) const; + + void loadLazySpecializationsImpl(llvm::ArrayRef<TemplateArgument> Args, + TemplateParameterList *TPL = nullptr) const; + + Decl *loadLazySpecializationImpl(LazySpecializationInfo &LazySpecInfo) const; template <class EntryType, typename ...ProfileArguments> typename SpecEntryTraits<EntryType>::DeclType* @@ -796,7 +824,7 @@ class RedeclarableTemplateDecl : public TemplateDecl, /// /// The first value in the array is the number of specializations/partial /// specializations that follow. - GlobalDeclID *LazySpecializations = nullptr; + LazySpecializationInfo *LazySpecializations = nullptr; /// The set of "injected" template arguments used within this /// template. @@ -2279,7 +2307,7 @@ class ClassTemplateDecl : public RedeclarableTemplateDecl { friend class TemplateDeclInstantiator; /// Load any lazily-loaded specializations from the external source. - void LoadLazySpecializations() const; + void LoadLazySpecializations(bool OnlyPartial = false) const; /// Get the underlying class declarations of the template. CXXRecordDecl *getTemplatedDecl() const { @@ -3023,7 +3051,7 @@ class VarTemplateDecl : public RedeclarableTemplateDecl { friend class ASTDeclWriter; /// Load any lazily-loaded specializations from the external source. - void LoadLazySpecializations() const; + void LoadLazySpecializations(bool OnlyPartial = false) const; /// Get the underlying variable declarations of the template. VarDecl *getTemplatedDecl() const { diff --git a/clang/lib/AST/DeclTemplate.cpp b/clang/lib/AST/DeclTemplate.cpp index 722c7fcf0b0df..021454c770741 100644 --- a/clang/lib/AST/DeclTemplate.cpp +++ b/clang/lib/AST/DeclTemplate.cpp @@ -20,6 +20,8 @@ #include "clang/AST/TemplateBase.h" #include "clang/AST/TemplateName.h" #include "clang/AST/Type.h" +#include "clang/AST/ODRHash.h" +#include "clang/AST/ExprCXX.h" #include "clang/AST/TypeLoc.h" #include "clang/Basic/Builtins.h" #include "clang/Basic/LLVM.h" @@ -331,17 +333,46 @@ RedeclarableTemplateDecl::CommonBase *RedeclarableTemplateDecl::getCommonPtr() c return Common; } -void RedeclarableTemplateDecl::loadLazySpecializationsImpl() const { +void RedeclarableTemplateDecl::loadLazySpecializationsImpl( + bool OnlyPartial/*=false*/) const { // Grab the most recent declaration to ensure we've loaded any lazy // redeclarations of this template. CommonBase *CommonBasePtr = getMostRecentDecl()->getCommonPtr(); - if (CommonBasePtr->LazySpecializations) { - ASTContext &Context = getASTContext(); - GlobalDeclID *Specs = CommonBasePtr->LazySpecializations; - CommonBasePtr->LazySpecializations = nullptr; - unsigned SpecSize = (*Specs++).getRawValue(); - for (unsigned I = 0; I != SpecSize; ++I) - (void)Context.getExternalSource()->GetExternalDecl(Specs[I]); + if (auto *Specs = CommonBasePtr->LazySpecializations) { + if (!OnlyPartial) + CommonBasePtr->LazySpecializations = nullptr; + unsigned N = Specs[0].DeclID.getRawValue(); + for (unsigned I = 0; I != N; ++I) { + // Skip over already loaded specializations. + if (!Specs[I+1].ODRHash) + continue; + if (!OnlyPartial || Specs[I+1].IsPartial) + (void)loadLazySpecializationImpl(Specs[I+1]); + } + } +} + +Decl *RedeclarableTemplateDecl::loadLazySpecializationImpl( + LazySpecializationInfo &LazySpecInfo) const { + GlobalDeclID ID = LazySpecInfo.DeclID; + assert(ID.isValid() && "Loading already loaded specialization!"); + // Note that we loaded the specialization. + LazySpecInfo.DeclID = GlobalDeclID(); + LazySpecInfo.ODRHash = LazySpecInfo.IsPartial = 0; + return getASTContext().getExternalSource()->GetExternalDecl(ID); +} + +void +RedeclarableTemplateDecl::loadLazySpecializationsImpl(ArrayRef<TemplateArgument> + Args, + TemplateParameterList *TPL) const { + CommonBase *CommonBasePtr = getMostRecentDecl()->getCommonPtr(); + if (auto *Specs = CommonBasePtr->LazySpecializations) { + unsigned Hash = TemplateArgumentList::ComputeODRHash(Args); + unsigned N = Specs[0].DeclID.getRawValue(); + for (uint32_t I = 0; I != N; ++I) + if (Specs[I+1].ODRHash && Specs[I+1].ODRHash == Hash) + (void)loadLazySpecializationImpl(Specs[I+1]); } } @@ -352,6 +383,8 @@ RedeclarableTemplateDecl::findSpecializationImpl( ProfileArguments&&... ProfileArgs) { using SETraits = SpecEntryTraits<EntryType>; + loadLazySpecializationsImpl(std::forward<ProfileArguments>(ProfileArgs)...); + llvm::FoldingSetNodeID ID; EntryType::Profile(ID, std::forward<ProfileArguments>(ProfileArgs)..., getASTContext()); @@ -367,10 +400,14 @@ void RedeclarableTemplateDecl::addSpecializationImpl( if (InsertPos) { #ifndef NDEBUG + auto Args = SETraits::getTemplateArgs(Entry); + // Due to hash collisions, it can happen that we load another template + // specialization with the same hash. This is fine, as long as the next + // call to findSpecializationImpl does not find a matching Decl for the + // template arguments. + loadLazySpecializationsImpl(Args); void *CorrectInsertPos; - assert(!findSpecializationImpl(Specializations, - CorrectInsertPos, - SETraits::getTemplateArgs(Entry)) && + assert(!findSpecializationImpl(Specializations, CorrectInsertPos, Args) && InsertPos == CorrectInsertPos && "given incorrect InsertPos for specialization"); #endif @@ -444,12 +481,14 @@ FunctionTemplateDecl::getSpecializations() const { FunctionDecl * FunctionTemplateDecl::findSpecialization(ArrayRef<TemplateArgument> Args, void *&InsertPos) { - return findSpecializationImpl(getSpecializations(), InsertPos, Args); + auto *Common = getCommonPtr(); + return findSpecializationImpl(Common->Specializations, InsertPos, Args); } void FunctionTemplateDecl::addSpecialization( FunctionTemplateSpecializationInfo *Info, void *InsertPos) { - addSpecializationImpl<FunctionTemplateDecl>(getSpecializations(), Info, + auto *Common = getCommonPtr(); + addSpecializationImpl<FunctionTemplateDecl>(Common->Specializations, Info, InsertPos); } @@ -509,8 +548,9 @@ ClassTemplateDecl *ClassTemplateDecl::CreateDeserialized(ASTContext &C, DeclarationName(), nullptr, nullptr); } -void ClassTemplateDecl::LoadLazySpecializations() const { - loadLazySpecializationsImpl(); +void ClassTemplateDecl::LoadLazySpecializations( + bool OnlyPartial/*=false*/) const { + loadLazySpecializationsImpl(OnlyPartial); } llvm::FoldingSetVector<ClassTemplateSpecializationDecl> & @@ -521,7 +561,7 @@ ClassTemplateDecl::getSpecializations() const { llvm::FoldingSetVector<ClassTemplatePartialSpecializationDecl> & ClassTemplateDecl::getPartialSpecializations() const { - LoadLazySpecializations(); + LoadLazySpecializations(/*PartialOnly = */ true); return getCommonPtr()->PartialSpecializations; } @@ -535,12 +575,15 @@ ClassTemplateDecl::newCommon(ASTContext &C) const { ClassTemplateSpecializationDecl * ClassTemplateDecl::findSpecialization(ArrayRef<TemplateArgument> Args, void *&InsertPos) { - return findSpecializationImpl(getSpecializations(), InsertPos, Args); + auto *Common = getCommonPtr(); + return findSpecializationImpl(Common->Specializations, InsertPos, Args); } void ClassTemplateDecl::AddSpecialization(ClassTemplateSpecializationDecl *D, void *InsertPos) { - addSpecializationImpl<ClassTemplateDecl>(getSpecializations(), D, InsertPos); + auto *Common = getCommonPtr(); + addSpecializationImpl<ClassTemplateDecl>(Common->Specializations, D, + InsertPos); } ClassTemplatePartialSpecializationDecl * @@ -897,6 +940,14 @@ TemplateArgumentList::CreateCopy(ASTContext &Context, return new (Mem) TemplateArgumentList(Args); } +unsigned TemplateArgumentList::ComputeODRHash(ArrayRef<TemplateArgument> Args) { + ODRHash Hasher; + for (TemplateArgument TA : Args) + Hasher.AddTemplateArgument(TA); + + return Hasher.CalculateHash(); +} + FunctionTemplateSpecializationInfo *FunctionTemplateSpecializationInfo::Create( ASTContext &C, FunctionDecl *FD, FunctionTemplateDecl *Template, TemplateSpecializationKind TSK, TemplateArgumentList *TemplateArgs, @@ -1262,8 +1313,9 @@ VarTemplateDecl *VarTemplateDecl::CreateDeserialized(ASTContext &C, DeclarationName(), nullptr, nullptr); } -void VarTemplateDecl::LoadLazySpecializations() const { - loadLazySpecializationsImpl(); +void VarTemplateDecl::LoadLazySpecializations( + bool OnlyPartial/*=false*/) const { + loadLazySpecializationsImpl(OnlyPartial); } llvm::FoldingSetVector<VarTemplateSpecializationDecl> & @@ -1274,7 +1326,7 @@ VarTemplateDecl::getSpecializations() const { llvm::FoldingSetVector<VarTemplatePartialSpecializationDecl> & VarTemplateDecl::getPartialSpecializations() const { - LoadLazySpecializations(); + LoadLazySpecializations(/*PartialOnly = */ true); return getCommonPtr()->PartialSpecializations; } @@ -1288,12 +1340,14 @@ VarTemplateDecl::newCommon(ASTContext &C) const { VarTemplateSpecializationDecl * VarTemplateDecl::findSpecialization(ArrayRef<TemplateArgument> Args, void *&InsertPos) { - return findSpecializationImpl(getSpecializations(), InsertPos, Args); + auto *Common = getCommonPtr(); + return findSpecializationImpl(Common->Specializations, InsertPos, Args); } void VarTemplateDecl::AddSpecialization(VarTemplateSpecializationDecl *D, void *InsertPos) { - addSpecializationImpl<VarTemplateDecl>(getSpecializations(), D, InsertPos); + auto *Common = getCommonPtr(); + addSpecializationImpl<VarTemplateDecl>(Common->Specializations, D, InsertPos); } VarTemplatePartialSpecializationDecl * diff --git a/clang/lib/AST/ODRHash.cpp b/clang/lib/AST/ODRHash.cpp index fbfe92318dc5e..7b361c958c54f 100644 --- a/clang/lib/AST/ODRHash.cpp +++ b/clang/lib/AST/ODRHash.cpp @@ -825,6 +825,21 @@ void ODRHash::AddDecl(const Decl *D) { for (const TemplateArgument &TA : List.asArray()) AddTemplateArgument(TA); } + + // If this was a specialization we should take into account its template + // arguments. This helps to reduce collisions coming when visiting template + // specialization types (eg. when processing type template arguments). + ArrayRef<TemplateArgument> Args; + if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(D)) + Args = CTSD->getTemplateArgs().asArray(); + else if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(D)) + Args = VTSD->getTemplateArgs().asArray(); + else if (auto *FD = dyn_cast<FunctionDecl>(D)) + if (FD->getTemplateSpecializationArgs()) + Args = FD->getTemplateSpecializationArgs()->asArray(); + + for (auto &TA : Args) + AddTemplateArgument(TA); } namespace { diff --git a/clang/lib/Serialization/ASTReader.cpp b/clang/lib/Serialization/ASTReader.cpp index afdeccaf93a9d..8ac1b41936d34 100644 --- a/clang/lib/Serialization/ASTReader.cpp +++ b/clang/lib/Serialization/ASTReader.cpp @@ -7587,14 +7587,23 @@ void ASTReader::CompleteRedeclChain(const Decl *D) { } } - if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(D)) - CTSD->getSpecializedTemplate()->LoadLazySpecializations(); - if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(D)) - VTSD->getSpecializedTemplate()->LoadLazySpecializations(); - if (auto *FD = dyn_cast<FunctionDecl>(D)) { - if (auto *Template = FD->getPrimaryTemplate()) - Template->LoadLazySpecializations(); - } + RedeclarableTemplateDecl *Template = nullptr; + ArrayRef<TemplateArgument> Args; + if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(D)) { + Template = CTSD->getSpecializedTemplate(); + Args = CTSD->getTemplateArgs().asArray(); + } else if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(D)) { + Template = VTSD->getSpecializedTemplate(); + Args = VTSD->getTemplateArgs().asArray(); + } else if (auto *FD = dyn_cast<FunctionDecl>(D)) { + if (auto *Tmplt = FD->getPrimaryTemplate()) { + Template = Tmplt; + Args = FD->getTemplateSpecializationArgs()->asArray(); + } + } + + if (Template) + Template->loadLazySpecializationsImpl(Args); } CXXCtorInitializer ** diff --git a/clang/lib/Serialization/ASTReaderDecl.cpp b/clang/lib/Serialization/ASTReaderDecl.cpp index cbaf1b0a98c61..be5483c4136d3 100644 --- a/clang/lib/Serialization/ASTReaderDecl.cpp +++ b/clang/lib/Serialization/ASTReaderDecl.cpp @@ -89,6 +89,8 @@ namespace clang { const SourceLocation ThisDeclLoc; using RecordData = ASTReader::RecordData; + using LazySpecializationInfo + = RedeclarableTemplateDecl::LazySpecializationInfo; TypeID DeferredTypeID = 0; unsigned AnonymousDeclNumber = 0; @@ -131,9 +133,16 @@ namespace clang { return Record.readString(); } - void readDeclIDList(SmallVectorImpl<GlobalDeclID> &IDs) { + LazySpecializationInfo ReadLazySpecializationInfo() { + GlobalDeclID ID = readDeclID(); + unsigned Hash = Record.readInt(); + bool IsPartial = Record.readInt(); + return LazySpecializationInfo(ID, Hash, IsPartial); + } + + void readDeclIDList(SmallVectorImpl<LazySpecializationInfo> &IDs) { for (unsigned I = 0, Size = Record.readInt(); I != Size; ++I) - IDs.push_back(readDeclID()); + IDs.push_back(ReadLazySpecializationInfo()); } Decl *readDecl() { @@ -262,9 +271,9 @@ namespace clang { : Reader(Reader), Record(Record), Loc(Loc), ThisDeclID(thisDeclID), ThisDeclLoc(ThisDeclLoc) {} - template <typename T> - static void AddLazySpecializations(T *D, - SmallVectorImpl<GlobalDeclID> &IDs) { + template <typename T> static + void AddLazySpecializations(T *D, + SmallVectorImpl<LazySpecializationInfo>& IDs) { if (IDs.empty()) return; @@ -274,14 +283,12 @@ namespace clang { auto *&LazySpecializations = D->getCommonPtr()->LazySpecializations; if (auto &Old = LazySpecializations) { - IDs.insert(IDs.end(), Old + 1, Old + 1 + Old[0].getRawValue()); + IDs.insert(IDs.end(), Old + 1, Old + 1 + Old[0].DeclID.getRawValue()); llvm::sort(IDs); IDs.erase(std::unique(IDs.begin(), IDs.end()), IDs.end()); } - - auto *Result = new (C) GlobalDeclID[1 + IDs.size()]; - *Result = GlobalDeclID(IDs.size()); - + auto *Result = new (C) LazySpecializationInfo[1 + IDs.size()]; + Result->DeclID = GlobalDeclID(IDs.size()); std::copy(IDs.begin(), IDs.end(), Result + 1); LazySpecializations = Result; @@ -315,7 +322,7 @@ namespace clang { void ReadFunctionDefinition(FunctionDecl *FD); void Visit(Decl *D); - void UpdateDecl(Decl *D, SmallVectorImpl<GlobalDeclID> &); + void UpdateDecl(Decl *D, llvm::SmallVectorImpl<LazySpecializationInfo>&); static void setNextObjCCategory(ObjCCategoryDecl *Cat, ObjCCategoryDecl *Next) { @@ -2455,7 +2462,7 @@ void ASTDeclReader::VisitClassTemplateDecl(ClassTemplateDecl *D) { if (ThisDeclID == Redecl.getFirstID()) { // This ClassTemplateDecl owns a CommonPtr; read it to keep track of all of // the specializations. - SmallVector<GlobalDeclID, 32> SpecIDs; + SmallVector<LazySpecializationInfo, 32> SpecIDs; readDeclIDList(SpecIDs); ASTDeclReader::AddLazySpecializations(D, SpecIDs); } @@ -2483,7 +2490,7 @@ void ASTDeclReader::VisitVarTemplateDecl(VarTemplateDecl *D) { if (ThisDeclID == Redecl.getFirstID()) { // This VarTemplateDecl owns a CommonPtr; read it to keep track of all of // the specializations. - SmallVector<GlobalDeclID, 32> SpecIDs; + SmallVector<LazySpecializationInfo, 32> SpecIDs; readDeclIDList(SpecIDs); ASTDeclReader::AddLazySpecializations(D, SpecIDs); } @@ -2585,7 +2592,7 @@ void ASTDeclReader::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) { if (ThisDeclID == Redecl.getFirstID()) { // This FunctionTemplateDecl owns a CommonPtr; read it. - SmallVector<GlobalDeclID, 32> SpecIDs; + SmallVector<LazySpecializationInfo, 32> SpecIDs; readDeclIDList(SpecIDs); ASTDeclReader::AddLazySpecializations(D, SpecIDs); } @@ -4246,7 +4253,9 @@ void ASTReader::loadDeclUpdateRecords(PendingUpdateRecord &Record) { ProcessingUpdatesRAIIObj ProcessingUpdates(*this); DeclUpdateOffsetsMap::iterator UpdI = DeclUpdateOffsets.find(ID); - SmallVector<GlobalDeclID, 8> PendingLazySpecializationIDs; + using LazySpecializationInfo + = RedeclarableTemplateDecl::LazySpecializationInfo; + llvm::SmallVector<LazySpecializationInfo, 8> PendingLazySpecializationIDs; if (UpdI != DeclUpdateOffsets.end()) { auto UpdateOffsets = std::move(UpdI->second); @@ -4511,9 +4520,8 @@ static void forAllLaterRedecls(DeclT *D, Fn F) { } } -void ASTDeclReader::UpdateDecl( - Decl *D, - llvm::SmallVectorImpl<GlobalDeclID> &PendingLazySpecializationIDs) { +void ASTDeclReader::UpdateDecl(Decl *D, + SmallVectorImpl<LazySpecializationInfo> &PendingLazySpecializationIDs) { while (Record.getIdx() < Record.size()) { switch ((DeclUpdateKind)Record.readInt()) { case UPD_CXX_ADDED_IMPLICIT_MEMBER: { @@ -4526,7 +4534,7 @@ void ASTDeclReader::UpdateDecl( case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION: // It will be added to the template's lazy specialization set. - PendingLazySpecializationIDs.push_back(readDeclID()); + PendingLazySpecializationIDs.push_back(ReadLazySpecializationInfo()); break; case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE: { diff --git a/clang/lib/Serialization/ASTWriter.cpp b/clang/lib/Serialization/ASTWriter.cpp index 5b5b468532f32..bfc4c19af146d 100644 --- a/clang/lib/Serialization/ASTWriter.cpp +++ b/clang/lib/Serialization/ASTWriter.cpp @@ -5744,12 +5744,29 @@ void ASTWriter::WriteDeclUpdatesBlocks(RecordDataImpl &OffsetsRecord) { switch (Kind) { case UPD_CXX_ADDED_IMPLICIT_MEMBER: - case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION: case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE: assert(Update.getDecl() && "no decl to add?"); Record.AddDeclRef(Update.getDecl()); break; - + case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION: { + const Decl *Spec = Update.getDecl(); + assert(Spec && "no decl to add?"); + Record.AddDeclRef(Spec); + ArrayRef<TemplateArgument> Args; + if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(Spec)) + Args = CTSD->getTemplateArgs().asArray(); + else if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(Spec)) + Args = VTSD->getTemplateArgs().asArray(); + else if (auto *FD = dyn_cast<FunctionDecl>(Spec)) + Args = FD->getTemplateSpecializationArgs()->asArray(); + assert(Args.size()); + Record.push_back(TemplateArgumentList::ComputeODRHash(Args)); + bool IsPartialSpecialization + = isa<ClassTemplatePartialSpecializationDecl>(Spec) || + isa<VarTemplatePartialSpecializationDecl>(Spec); + Record.push_back(IsPartialSpecialization); + break; + } case UPD_CXX_ADDED_FUNCTION_DEFINITION: case UPD_CXX_ADDED_VAR_DEFINITION: break; diff --git a/clang/lib/Serialization/ASTWriterDecl.cpp b/clang/lib/Serialization/ASTWriterDecl.cpp index b6583c54c9ba1..93a4f02e01bc9 100644 --- a/clang/lib/Serialization/ASTWriterDecl.cpp +++ b/clang/lib/Serialization/ASTWriterDecl.cpp @@ -178,11 +178,11 @@ namespace clang { Record.AddSourceLocation(typeParams->getRAngleLoc()); } - /// Add to the record the first declaration from each module file that - /// provides a declaration of D. The intent is to provide a sufficient - /// set such that reloading this set will load all current redeclarations. - void AddFirstDeclFromEachModule(const Decl *D, bool IncludeLocal) { - llvm::MapVector<ModuleFile*, const Decl*> Firsts; + /// Collect the first declaration from each module file that provides a + /// declaration of D. + void CollectFirstDeclFromEachModule(const Decl *D, bool IncludeLocal, + llvm::MapVector<ModuleFile*, const Decl*> &Firsts) { + // FIXME: We can skip entries that we know are implied by others. for (const Decl *R = D->getMostRecentDecl(); R; R = R->getPreviousDecl()) { if (R->isFromASTFile()) @@ -190,10 +190,49 @@ namespace clang { else if (IncludeLocal) Firsts[nullptr] = R; } + } + + /// Add to the record the first declaration from each module file that + /// provides a declaration of D. The intent is to provide a sufficient + /// set such that reloading this set will load all current redeclarations. + void AddFirstDeclFromEachModule(const Decl *D, bool IncludeLocal) { + llvm::MapVector<ModuleFile*, const Decl*> Firsts; + CollectFirstDeclFromEachModule(D, IncludeLocal, Firsts); + for (const auto &F : Firsts) Record.AddDeclRef(F.second); } + /// Add to the record the first template specialization from each module + /// file that provides a declaration of D. We store the DeclId and an + /// ODRHash of the template arguments of D which should provide enough + /// information to load D only if the template instantiator needs it. + void AddFirstSpecializationDeclFromEachModule(const Decl *D, + bool IncludeLocal) { + assert(isa<ClassTemplateSpecializationDecl>(D) || + isa<VarTemplateSpecializationDecl>(D) || isa<FunctionDecl>(D) && + "Must not be called with other decls"); + llvm::MapVector<ModuleFile*, const Decl*> Firsts; + CollectFirstDeclFromEachModule(D, IncludeLocal, Firsts); + + for (const auto &F : Firsts) { + Record.AddDeclRef(F.second); + ArrayRef<TemplateArgument> Args; + if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(D)) + Args = CTSD->getTemplateArgs().asArray(); + else if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(D)) + Args = VTSD->getTemplateArgs().asArray(); + else if (auto *FD = dyn_cast<FunctionDecl>(D)) + Args = FD->getTemplateSpecializationArgs()->asArray(); + assert(Args.size()); + Record.push_back(TemplateArgumentList::ComputeODRHash(Args)); + bool IsPartialSpecialization + = isa<ClassTemplatePartialSpecializationDecl>(D) || + isa<VarTemplatePartialSpecializationDecl>(D); + Record.push_back(IsPartialSpecialization); + } + } + /// Get the specialization decl from an entry in the specialization list. template <typename EntryType> typename RedeclarableTemplateDecl::SpecEntryTraits<EntryType>::DeclType * @@ -206,7 +245,8 @@ namespace clang { decltype(T::PartialSpecializations) &getPartialSpecializations(T *Common) { return Common->PartialSpecializations; } - ArrayRef<Decl> getPartialSpecializations(FunctionTemplateDecl::Common *) { + MutableArrayRef<FunctionTemplateSpecializationInfo> + getPartialSpecializations(FunctionTemplateDecl::Common *) { return std::nullopt; } @@ -223,9 +263,11 @@ namespace clang { assert(!Common->LazySpecializations); } - ArrayRef<GlobalDeclID> LazySpecializations; + using LazySpecializationInfo + = RedeclarableTemplateDecl::LazySpecializationInfo; + ArrayRef<LazySpecializationInfo> LazySpecializations; if (auto *LS = Common->LazySpecializations) - LazySpecializations = llvm::ArrayRef(LS + 1, LS[0].getRawValue()); + LazySpecializations = llvm::ArrayRef(LS + 1, LS[0].DeclID.getRawValue()); // Add a slot to the record for the number of specializations. unsigned I = Record.size(); @@ -241,14 +283,20 @@ namespace clang { for (auto *D : Specs) { assert(D->isCanonicalDecl() && "non-canonical decl in set"); - AddFirstDeclFromEachModule(D, /*IncludeLocal*/true); + AddFirstSpecializationDeclFromEachModule(D, /*IncludeLocal*/true); + } + for (auto &SpecInfo : LazySpecializations) { + Record.push_back(SpecInfo.DeclID.getRawValue()); + Record.push_back(SpecInfo.ODRHash); + Record.push_back(SpecInfo.IsPartial); } - Record.append( - DeclIDIterator<GlobalDeclID, DeclID>(LazySpecializations.begin()), - DeclIDIterator<GlobalDeclID, DeclID>(LazySpecializations.end())); - // Update the size entry we added earlier. - Record[I] = Record.size() - I - 1; + // Update the size entry we added earlier. We linerized the + // LazySpecializationInfo members and we need to adjust the size as we + // will read them always together. + assert ((Record.size() - I - 1) % 3 == 0 + && "Must be divisible by LazySpecializationInfo count!"); + Record[I] = (Record.size() - I - 1) / 3; } /// Ensure that this template specialization is associated with the specified diff --git a/clang/test/Modules/cxx-templates.cpp b/clang/test/Modules/cxx-templates.cpp index b7d5741e69af6..e10ba7c2ac3ef 100644 --- a/clang/test/Modules/cxx-templates.cpp +++ b/clang/test/Modules/cxx-templates.cpp @@ -251,7 +251,7 @@ namespace Std { // CHECK-DUMP: ClassTemplateDecl {{.*}} <{{.*[/\\]}}cxx-templates-common.h:1:1, {{.*}}> col:{{.*}} in cxx_templates_common SomeTemplate // CHECK-DUMP: ClassTemplateSpecializationDecl {{.*}} prev {{.*}} SomeTemplate -// CHECK-DUMP-NEXT: TemplateArgument type 'char[2]' +// CHECK-DUMP-NEXT: TemplateArgument type 'char[1]' // CHECK-DUMP: ClassTemplateSpecializationDecl {{.*}} SomeTemplate definition // CHECK-DUMP-NEXT: DefinitionData // CHECK-DUMP-NEXT: DefaultConstructor @@ -260,9 +260,9 @@ namespace Std { // CHECK-DUMP-NEXT: CopyAssignment // CHECK-DUMP-NEXT: MoveAssignment // CHECK-DUMP-NEXT: Destructor -// CHECK-DUMP-NEXT: TemplateArgument type 'char[2]' -// CHECK-DUMP: ClassTemplateSpecializationDecl {{.*}} prev {{.*}} SomeTemplate // CHECK-DUMP-NEXT: TemplateArgument type 'char[1]' +// CHECK-DUMP: ClassTemplateSpecializationDecl {{.*}} prev {{.*}} SomeTemplate +// CHECK-DUMP-NEXT: TemplateArgument type 'char[2]' // CHECK-DUMP: ClassTemplateSpecializationDecl {{.*}} SomeTemplate definition // CHECK-DUMP-NEXT: DefinitionData // CHECK-DUMP-NEXT: DefaultConstructor @@ -271,4 +271,4 @@ namespace Std { // CHECK-DUMP-NEXT: CopyAssignment // CHECK-DUMP-NEXT: MoveAssignment // CHECK-DUMP-NEXT: Destructor -// CHECK-DUMP-NEXT: TemplateArgument type 'char[1]' +// CHECK-DUMP-NEXT: TemplateArgument type 'char[2]' diff --git a/clang/test/Modules/odr_hash.cpp b/clang/test/Modules/odr_hash.cpp index fa8b2c81ab46e..7cea3af3f41bd 100644 --- a/clang/test/Modules/odr_hash.cpp +++ b/clang/test/Modules/odr_hash.cpp @@ -3084,8 +3084,8 @@ struct S5 { }; #else S5 s5; -// expected-error@second.h:* {{'PointersAndReferences::S5::x' from module 'SecondModule' is not present in definition of 'PointersAndReferences::S5' in module 'FirstModule'}} -// expected-note@first.h:* {{declaration of 'x' does not match}} +// expected-error@first.h:* {{'PointersAndReferences::S5::x' from module 'FirstModule' is not present in definition of 'PointersAndReferences::S5' in module 'SecondModule'}} +// expected-note@second.h:* {{declaration of 'x' does not match}} #endif #if defined(FIRST) >From 45ed7aa243654a114e326538fd5cdc5096df1b35 Mon Sep 17 00:00:00 2001 From: Chuanqi Xu <yedeng...@linux.alibaba.com> Date: Tue, 27 Feb 2024 15:46:39 +0800 Subject: [PATCH 2/2] fmt --- clang/include/clang/AST/DeclTemplate.h | 4 ++-- clang/lib/AST/DeclTemplate.cpp | 26 ++++++++++---------- clang/lib/Serialization/ASTReaderDecl.cpp | 21 ++++++++-------- clang/lib/Serialization/ASTWriter.cpp | 6 ++--- clang/lib/Serialization/ASTWriterDecl.cpp | 29 ++++++++++++----------- 5 files changed, 43 insertions(+), 43 deletions(-) diff --git a/clang/include/clang/AST/DeclTemplate.h b/clang/include/clang/AST/DeclTemplate.h index d76c293706369..44f840d297465 100644 --- a/clang/include/clang/AST/DeclTemplate.h +++ b/clang/include/clang/AST/DeclTemplate.h @@ -738,8 +738,8 @@ class RedeclarableTemplateDecl : public TemplateDecl, bool IsPartial = false; LazySpecializationInfo(GlobalDeclID ID, unsigned Hash = ~0U, bool Partial = false) - : DeclID(ID), ODRHash(Hash), IsPartial(Partial) { } - LazySpecializationInfo() { } + : DeclID(ID), ODRHash(Hash), IsPartial(Partial) {} + LazySpecializationInfo() {} bool operator<(const LazySpecializationInfo &Other) const { return DeclID < Other.DeclID; } diff --git a/clang/lib/AST/DeclTemplate.cpp b/clang/lib/AST/DeclTemplate.cpp index 021454c770741..a6381d78cd3ac 100644 --- a/clang/lib/AST/DeclTemplate.cpp +++ b/clang/lib/AST/DeclTemplate.cpp @@ -16,12 +16,12 @@ #include "clang/AST/DeclCXX.h" #include "clang/AST/DeclarationName.h" #include "clang/AST/Expr.h" +#include "clang/AST/ExprCXX.h" #include "clang/AST/ExternalASTSource.h" +#include "clang/AST/ODRHash.h" #include "clang/AST/TemplateBase.h" #include "clang/AST/TemplateName.h" #include "clang/AST/Type.h" -#include "clang/AST/ODRHash.h" -#include "clang/AST/ExprCXX.h" #include "clang/AST/TypeLoc.h" #include "clang/Basic/Builtins.h" #include "clang/Basic/LLVM.h" @@ -334,7 +334,7 @@ RedeclarableTemplateDecl::CommonBase *RedeclarableTemplateDecl::getCommonPtr() c } void RedeclarableTemplateDecl::loadLazySpecializationsImpl( - bool OnlyPartial/*=false*/) const { + bool OnlyPartial /*=false*/) const { // Grab the most recent declaration to ensure we've loaded any lazy // redeclarations of this template. CommonBase *CommonBasePtr = getMostRecentDecl()->getCommonPtr(); @@ -344,16 +344,16 @@ void RedeclarableTemplateDecl::loadLazySpecializationsImpl( unsigned N = Specs[0].DeclID.getRawValue(); for (unsigned I = 0; I != N; ++I) { // Skip over already loaded specializations. - if (!Specs[I+1].ODRHash) + if (!Specs[I + 1].ODRHash) continue; - if (!OnlyPartial || Specs[I+1].IsPartial) - (void)loadLazySpecializationImpl(Specs[I+1]); + if (!OnlyPartial || Specs[I + 1].IsPartial) + (void)loadLazySpecializationImpl(Specs[I + 1]); } } } Decl *RedeclarableTemplateDecl::loadLazySpecializationImpl( - LazySpecializationInfo &LazySpecInfo) const { + LazySpecializationInfo &LazySpecInfo) const { GlobalDeclID ID = LazySpecInfo.DeclID; assert(ID.isValid() && "Loading already loaded specialization!"); // Note that we loaded the specialization. @@ -362,15 +362,13 @@ Decl *RedeclarableTemplateDecl::loadLazySpecializationImpl( return getASTContext().getExternalSource()->GetExternalDecl(ID); } -void -RedeclarableTemplateDecl::loadLazySpecializationsImpl(ArrayRef<TemplateArgument> - Args, - TemplateParameterList *TPL) const { +void RedeclarableTemplateDecl::loadLazySpecializationsImpl( + ArrayRef<TemplateArgument> Args, TemplateParameterList *TPL) const { CommonBase *CommonBasePtr = getMostRecentDecl()->getCommonPtr(); if (auto *Specs = CommonBasePtr->LazySpecializations) { unsigned Hash = TemplateArgumentList::ComputeODRHash(Args); unsigned N = Specs[0].DeclID.getRawValue(); - for (uint32_t I = 0; I != N; ++I) + for (unsigned I = 0; I != N; ++I) if (Specs[I+1].ODRHash && Specs[I+1].ODRHash == Hash) (void)loadLazySpecializationImpl(Specs[I+1]); } @@ -549,7 +547,7 @@ ClassTemplateDecl *ClassTemplateDecl::CreateDeserialized(ASTContext &C, } void ClassTemplateDecl::LoadLazySpecializations( - bool OnlyPartial/*=false*/) const { + bool OnlyPartial /*=false*/) const { loadLazySpecializationsImpl(OnlyPartial); } @@ -1314,7 +1312,7 @@ VarTemplateDecl *VarTemplateDecl::CreateDeserialized(ASTContext &C, } void VarTemplateDecl::LoadLazySpecializations( - bool OnlyPartial/*=false*/) const { + bool OnlyPartial /*=false*/) const { loadLazySpecializationsImpl(OnlyPartial); } diff --git a/clang/lib/Serialization/ASTReaderDecl.cpp b/clang/lib/Serialization/ASTReaderDecl.cpp index be5483c4136d3..fd400cdcb8685 100644 --- a/clang/lib/Serialization/ASTReaderDecl.cpp +++ b/clang/lib/Serialization/ASTReaderDecl.cpp @@ -89,8 +89,8 @@ namespace clang { const SourceLocation ThisDeclLoc; using RecordData = ASTReader::RecordData; - using LazySpecializationInfo - = RedeclarableTemplateDecl::LazySpecializationInfo; + using LazySpecializationInfo = + RedeclarableTemplateDecl::LazySpecializationInfo; TypeID DeferredTypeID = 0; unsigned AnonymousDeclNumber = 0; @@ -271,9 +271,9 @@ namespace clang { : Reader(Reader), Record(Record), Loc(Loc), ThisDeclID(thisDeclID), ThisDeclLoc(ThisDeclLoc) {} - template <typename T> static - void AddLazySpecializations(T *D, - SmallVectorImpl<LazySpecializationInfo>& IDs) { + template <typename T> + static void + AddLazySpecializations(T *D, SmallVectorImpl<LazySpecializationInfo> &IDs) { if (IDs.empty()) return; @@ -322,7 +322,7 @@ namespace clang { void ReadFunctionDefinition(FunctionDecl *FD); void Visit(Decl *D); - void UpdateDecl(Decl *D, llvm::SmallVectorImpl<LazySpecializationInfo>&); + void UpdateDecl(Decl *D, llvm::SmallVectorImpl<LazySpecializationInfo> &); static void setNextObjCCategory(ObjCCategoryDecl *Cat, ObjCCategoryDecl *Next) { @@ -4253,8 +4253,8 @@ void ASTReader::loadDeclUpdateRecords(PendingUpdateRecord &Record) { ProcessingUpdatesRAIIObj ProcessingUpdates(*this); DeclUpdateOffsetsMap::iterator UpdI = DeclUpdateOffsets.find(ID); - using LazySpecializationInfo - = RedeclarableTemplateDecl::LazySpecializationInfo; + using LazySpecializationInfo = + RedeclarableTemplateDecl::LazySpecializationInfo; llvm::SmallVector<LazySpecializationInfo, 8> PendingLazySpecializationIDs; if (UpdI != DeclUpdateOffsets.end()) { @@ -4520,8 +4520,9 @@ static void forAllLaterRedecls(DeclT *D, Fn F) { } } -void ASTDeclReader::UpdateDecl(Decl *D, - SmallVectorImpl<LazySpecializationInfo> &PendingLazySpecializationIDs) { +void ASTDeclReader::UpdateDecl( + Decl *D, + SmallVectorImpl<LazySpecializationInfo> &PendingLazySpecializationIDs) { while (Record.getIdx() < Record.size()) { switch ((DeclUpdateKind)Record.readInt()) { case UPD_CXX_ADDED_IMPLICIT_MEMBER: { diff --git a/clang/lib/Serialization/ASTWriter.cpp b/clang/lib/Serialization/ASTWriter.cpp index bfc4c19af146d..f898fae53a0a6 100644 --- a/clang/lib/Serialization/ASTWriter.cpp +++ b/clang/lib/Serialization/ASTWriter.cpp @@ -5761,9 +5761,9 @@ void ASTWriter::WriteDeclUpdatesBlocks(RecordDataImpl &OffsetsRecord) { Args = FD->getTemplateSpecializationArgs()->asArray(); assert(Args.size()); Record.push_back(TemplateArgumentList::ComputeODRHash(Args)); - bool IsPartialSpecialization - = isa<ClassTemplatePartialSpecializationDecl>(Spec) || - isa<VarTemplatePartialSpecializationDecl>(Spec); + bool IsPartialSpecialization = + isa<ClassTemplatePartialSpecializationDecl>(Spec) || + isa<VarTemplatePartialSpecializationDecl>(Spec); Record.push_back(IsPartialSpecialization); break; } diff --git a/clang/lib/Serialization/ASTWriterDecl.cpp b/clang/lib/Serialization/ASTWriterDecl.cpp index 93a4f02e01bc9..1ab2c38007570 100644 --- a/clang/lib/Serialization/ASTWriterDecl.cpp +++ b/clang/lib/Serialization/ASTWriterDecl.cpp @@ -180,8 +180,9 @@ namespace clang { /// Collect the first declaration from each module file that provides a /// declaration of D. - void CollectFirstDeclFromEachModule(const Decl *D, bool IncludeLocal, - llvm::MapVector<ModuleFile*, const Decl*> &Firsts) { + void CollectFirstDeclFromEachModule( + const Decl *D, bool IncludeLocal, + llvm::MapVector<ModuleFile *, const Decl *> &Firsts) { // FIXME: We can skip entries that we know are implied by others. for (const Decl *R = D->getMostRecentDecl(); R; R = R->getPreviousDecl()) { @@ -196,7 +197,7 @@ namespace clang { /// provides a declaration of D. The intent is to provide a sufficient /// set such that reloading this set will load all current redeclarations. void AddFirstDeclFromEachModule(const Decl *D, bool IncludeLocal) { - llvm::MapVector<ModuleFile*, const Decl*> Firsts; + llvm::MapVector<ModuleFile *, const Decl *> Firsts; CollectFirstDeclFromEachModule(D, IncludeLocal, Firsts); for (const auto &F : Firsts) @@ -210,9 +211,9 @@ namespace clang { void AddFirstSpecializationDeclFromEachModule(const Decl *D, bool IncludeLocal) { assert(isa<ClassTemplateSpecializationDecl>(D) || - isa<VarTemplateSpecializationDecl>(D) || isa<FunctionDecl>(D) && - "Must not be called with other decls"); - llvm::MapVector<ModuleFile*, const Decl*> Firsts; + isa<VarTemplateSpecializationDecl>(D) || + isa<FunctionDecl>(D) && "Must not be called with other decls"); + llvm::MapVector<ModuleFile *, const Decl *> Firsts; CollectFirstDeclFromEachModule(D, IncludeLocal, Firsts); for (const auto &F : Firsts) { @@ -226,9 +227,9 @@ namespace clang { Args = FD->getTemplateSpecializationArgs()->asArray(); assert(Args.size()); Record.push_back(TemplateArgumentList::ComputeODRHash(Args)); - bool IsPartialSpecialization - = isa<ClassTemplatePartialSpecializationDecl>(D) || - isa<VarTemplatePartialSpecializationDecl>(D); + bool IsPartialSpecialization = + isa<ClassTemplatePartialSpecializationDecl>(D) || + isa<VarTemplatePartialSpecializationDecl>(D); Record.push_back(IsPartialSpecialization); } } @@ -263,8 +264,8 @@ namespace clang { assert(!Common->LazySpecializations); } - using LazySpecializationInfo - = RedeclarableTemplateDecl::LazySpecializationInfo; + using LazySpecializationInfo = + RedeclarableTemplateDecl::LazySpecializationInfo; ArrayRef<LazySpecializationInfo> LazySpecializations; if (auto *LS = Common->LazySpecializations) LazySpecializations = llvm::ArrayRef(LS + 1, LS[0].DeclID.getRawValue()); @@ -283,7 +284,7 @@ namespace clang { for (auto *D : Specs) { assert(D->isCanonicalDecl() && "non-canonical decl in set"); - AddFirstSpecializationDeclFromEachModule(D, /*IncludeLocal*/true); + AddFirstSpecializationDeclFromEachModule(D, /*IncludeLocal*/ true); } for (auto &SpecInfo : LazySpecializations) { Record.push_back(SpecInfo.DeclID.getRawValue()); @@ -294,8 +295,8 @@ namespace clang { // Update the size entry we added earlier. We linerized the // LazySpecializationInfo members and we need to adjust the size as we // will read them always together. - assert ((Record.size() - I - 1) % 3 == 0 - && "Must be divisible by LazySpecializationInfo count!"); + assert((Record.size() - I - 1) % 3 == 0 && + "Must be divisible by LazySpecializationInfo count!"); Record[I] = (Record.size() - I - 1) / 3; } _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits