Skip to content

Commit 27cea97

Browse files
committed
[Serialization] Code cleanups and polish 83233
fmt load specializations before writing specialization decls address comments Revert "load specializations before writing specialization decls" This reverts commit 61c451d. Do not omit data from imported modules with same key Handle merging spec info manually
1 parent d4928ba commit 27cea97

24 files changed

+612
-294
lines changed

clang/include/clang/AST/DeclTemplate.h

Lines changed: 7 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -256,9 +256,6 @@ class TemplateArgumentList final
256256
TemplateArgumentList(const TemplateArgumentList &) = delete;
257257
TemplateArgumentList &operator=(const TemplateArgumentList &) = delete;
258258

259-
/// Create hash for the given arguments.
260-
static unsigned ComputeODRHash(ArrayRef<TemplateArgument> Args);
261-
262259
/// Create a new template argument list that copies the given set of
263260
/// template arguments.
264261
static TemplateArgumentList *CreateCopy(ASTContext &Context,
@@ -732,25 +729,6 @@ class RedeclarableTemplateDecl : public TemplateDecl,
732729
}
733730

734731
void anchor() override;
735-
struct LazySpecializationInfo {
736-
GlobalDeclID DeclID = GlobalDeclID();
737-
unsigned ODRHash = ~0U;
738-
bool IsPartial = false;
739-
LazySpecializationInfo(GlobalDeclID ID, unsigned Hash = ~0U,
740-
bool Partial = false)
741-
: DeclID(ID), ODRHash(Hash), IsPartial(Partial) {}
742-
LazySpecializationInfo() {}
743-
bool operator<(const LazySpecializationInfo &Other) const {
744-
return DeclID < Other.DeclID;
745-
}
746-
bool operator==(const LazySpecializationInfo &Other) const {
747-
assert((DeclID != Other.DeclID || ODRHash == Other.ODRHash) &&
748-
"Hashes differ!");
749-
assert((DeclID != Other.DeclID || IsPartial == Other.IsPartial) &&
750-
"Both must be the same kinds!");
751-
return DeclID == Other.DeclID;
752-
}
753-
};
754732

755733
protected:
756734
template <typename EntryType> struct SpecEntryTraits {
@@ -794,16 +772,20 @@ class RedeclarableTemplateDecl : public TemplateDecl,
794772

795773
void loadLazySpecializationsImpl(bool OnlyPartial = false) const;
796774

797-
void loadLazySpecializationsImpl(llvm::ArrayRef<TemplateArgument> Args,
775+
bool loadLazySpecializationsImpl(llvm::ArrayRef<TemplateArgument> Args,
798776
TemplateParameterList *TPL = nullptr) const;
799777

800-
Decl *loadLazySpecializationImpl(LazySpecializationInfo &LazySpecInfo) const;
801-
802778
template <class EntryType, typename ...ProfileArguments>
803779
typename SpecEntryTraits<EntryType>::DeclType*
804780
findSpecializationImpl(llvm::FoldingSetVector<EntryType> &Specs,
805781
void *&InsertPos, ProfileArguments &&...ProfileArgs);
806782

783+
template <class EntryType, typename... ProfileArguments>
784+
typename SpecEntryTraits<EntryType>::DeclType *
785+
findSpecializationLocally(llvm::FoldingSetVector<EntryType> &Specs,
786+
void *&InsertPos,
787+
ProfileArguments &&...ProfileArgs);
788+
807789
template <class Derived, class EntryType>
808790
void addSpecializationImpl(llvm::FoldingSetVector<EntryType> &Specs,
809791
EntryType *Entry, void *InsertPos);
@@ -815,13 +797,6 @@ class RedeclarableTemplateDecl : public TemplateDecl,
815797
/// directly instantiated (or null).
816798
RedeclarableTemplateDecl *InstantiatedFromMember = nullptr;
817799

818-
/// If non-null, points to an array of specializations (including
819-
/// partial specializations) known only by their external declaration IDs.
820-
///
821-
/// The first value in the array is the number of specializations/partial
822-
/// specializations that follow.
823-
LazySpecializationInfo *LazySpecializations = nullptr;
824-
825800
/// The set of "injected" template arguments used within this
826801
/// template.
827802
///

clang/include/clang/AST/ExternalASTSource.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,11 +155,15 @@ class ExternalASTSource : public RefCountedBase<ExternalASTSource> {
155155
/// Load all the external specializations for the Decl \param D if \param
156156
/// OnlyPartial is false. Otherwise, load all the external **partial**
157157
/// specializations for the \param D.
158-
virtual void LoadExternalSpecializations(const Decl *D, bool OnlyPartial);
158+
///
159+
/// Return true if any new specializations get loaded. Return false otherwise.
160+
virtual bool LoadExternalSpecializations(const Decl *D, bool OnlyPartial);
159161

160162
/// Load all the specializations for the Decl \param D with the same template
161163
/// args specified by \param TemplateArgs.
162-
virtual void
164+
///
165+
/// Return true if any new specializations get loaded. Return false otherwise.
166+
virtual bool
163167
LoadExternalSpecializations(const Decl *D,
164168
ArrayRef<TemplateArgument> TemplateArgs);
165169

clang/include/clang/Sema/MultiplexExternalSemaSource.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,9 @@ class MultiplexExternalSemaSource : public ExternalSemaSource {
9797
bool FindExternalVisibleDeclsByName(const DeclContext *DC,
9898
DeclarationName Name) override;
9999

100-
void LoadExternalSpecializations(const Decl *D, bool OnlyPartial) override;
100+
bool LoadExternalSpecializations(const Decl *D, bool OnlyPartial) override;
101101

102-
void
102+
bool
103103
LoadExternalSpecializations(const Decl *D,
104104
ArrayRef<TemplateArgument> TemplateArgs) override;
105105

clang/include/clang/Serialization/ASTBitCodes.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -737,6 +737,8 @@ enum ASTRecordTypes {
737737

738738
/// Record code for updated specialization
739739
UPDATE_SPECIALIZATION = 73,
740+
741+
CXX_ADDED_TEMPLATE_SPECIALIZATION = 74,
740742
};
741743

742744
/// Record types used within a source manager block.

clang/include/clang/Serialization/ASTReader.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2058,9 +2058,9 @@ class ASTReader
20582058
unsigned BlockID,
20592059
uint64_t *StartOfBlockOffset = nullptr);
20602060

2061-
void LoadExternalSpecializations(const Decl *D, bool OnlyPartial) override;
2061+
bool LoadExternalSpecializations(const Decl *D, bool OnlyPartial) override;
20622062

2063-
void
2063+
bool
20642064
LoadExternalSpecializations(const Decl *D,
20652065
ArrayRef<TemplateArgument> TemplateArgs) override;
20662066

clang/lib/AST/DeclTemplate.cpp

Lines changed: 31 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -351,63 +351,30 @@ void RedeclarableTemplateDecl::loadLazySpecializationsImpl(
351351
ExternalSource->LoadExternalSpecializations(this->getCanonicalDecl(),
352352
OnlyPartial);
353353
return;
354-
355-
// Grab the most recent declaration to ensure we've loaded any lazy
356-
// redeclarations of this template.
357-
CommonBase *CommonBasePtr = getMostRecentDecl()->getCommonPtr();
358-
if (auto *Specs = CommonBasePtr->LazySpecializations) {
359-
if (!OnlyPartial)
360-
CommonBasePtr->LazySpecializations = nullptr;
361-
unsigned N = Specs[0].DeclID.getRawValue();
362-
for (unsigned I = 0; I != N; ++I) {
363-
// Skip over already loaded specializations.
364-
if (!Specs[I + 1].ODRHash)
365-
continue;
366-
if (!OnlyPartial || Specs[I + 1].IsPartial)
367-
(void)loadLazySpecializationImpl(Specs[I + 1]);
368-
}
369-
}
370-
}
371-
372-
Decl *RedeclarableTemplateDecl::loadLazySpecializationImpl(
373-
LazySpecializationInfo &LazySpecInfo) const {
374-
llvm_unreachable("We don't use LazySpecializationInfo any more");
375-
376-
GlobalDeclID ID = LazySpecInfo.DeclID;
377-
assert(ID.isValid() && "Loading already loaded specialization!");
378-
// Note that we loaded the specialization.
379-
LazySpecInfo.DeclID = GlobalDeclID();
380-
LazySpecInfo.ODRHash = LazySpecInfo.IsPartial = 0;
381-
return getASTContext().getExternalSource()->GetExternalDecl(ID);
382354
}
383355

384-
void RedeclarableTemplateDecl::loadLazySpecializationsImpl(
356+
bool RedeclarableTemplateDecl::loadLazySpecializationsImpl(
385357
ArrayRef<TemplateArgument> Args, TemplateParameterList *TPL) const {
386358
auto *ExternalSource = getASTContext().getExternalSource();
387359
if (!ExternalSource)
388-
return;
360+
return false;
389361

390-
ExternalSource->LoadExternalSpecializations(this->getCanonicalDecl(), Args);
391-
return;
362+
// If TPL is not null, it implies that we're loading specializations for
363+
// partial templates. We need to load all specializations in such cases.
364+
if (TPL)
365+
return ExternalSource->LoadExternalSpecializations(this->getCanonicalDecl(),
366+
/*OnlyPartial=*/false);
392367

393-
CommonBase *CommonBasePtr = getMostRecentDecl()->getCommonPtr();
394-
if (auto *Specs = CommonBasePtr->LazySpecializations) {
395-
unsigned Hash = TemplateArgumentList::ComputeODRHash(Args);
396-
unsigned N = Specs[0].DeclID.getRawValue();
397-
for (unsigned I = 0; I != N; ++I)
398-
if (Specs[I + 1].ODRHash && Specs[I + 1].ODRHash == Hash)
399-
(void)loadLazySpecializationImpl(Specs[I + 1]);
400-
}
368+
return ExternalSource->LoadExternalSpecializations(this->getCanonicalDecl(),
369+
Args);
401370
}
402371

403-
template<class EntryType, typename... ProfileArguments>
372+
template <class EntryType, typename... ProfileArguments>
404373
typename RedeclarableTemplateDecl::SpecEntryTraits<EntryType>::DeclType *
405-
RedeclarableTemplateDecl::findSpecializationImpl(
374+
RedeclarableTemplateDecl::findSpecializationLocally(
406375
llvm::FoldingSetVector<EntryType> &Specs, void *&InsertPos,
407-
ProfileArguments&&... ProfileArgs) {
408-
using SETraits = SpecEntryTraits<EntryType>;
409-
410-
loadLazySpecializationsImpl(std::forward<ProfileArguments>(ProfileArgs)...);
376+
ProfileArguments &&...ProfileArgs) {
377+
using SETraits = RedeclarableTemplateDecl::SpecEntryTraits<EntryType>;
411378

412379
llvm::FoldingSetNodeID ID;
413380
EntryType::Profile(ID, std::forward<ProfileArguments>(ProfileArgs)...,
@@ -416,6 +383,24 @@ RedeclarableTemplateDecl::findSpecializationImpl(
416383
return Entry ? SETraits::getDecl(Entry)->getMostRecentDecl() : nullptr;
417384
}
418385

386+
template <class EntryType, typename... ProfileArguments>
387+
typename RedeclarableTemplateDecl::SpecEntryTraits<EntryType>::DeclType *
388+
RedeclarableTemplateDecl::findSpecializationImpl(
389+
llvm::FoldingSetVector<EntryType> &Specs, void *&InsertPos,
390+
ProfileArguments &&...ProfileArgs) {
391+
392+
if (auto *Found = findSpecializationLocally(
393+
Specs, InsertPos, std::forward<ProfileArguments>(ProfileArgs)...))
394+
return Found;
395+
396+
if (!loadLazySpecializationsImpl(
397+
std::forward<ProfileArguments>(ProfileArgs)...))
398+
return nullptr;
399+
400+
return findSpecializationLocally(
401+
Specs, InsertPos, std::forward<ProfileArguments>(ProfileArgs)...);
402+
}
403+
419404
template<class Derived, class EntryType>
420405
void RedeclarableTemplateDecl::addSpecializationImpl(
421406
llvm::FoldingSetVector<EntryType> &Specializations, EntryType *Entry,
@@ -962,14 +947,6 @@ TemplateArgumentList::CreateCopy(ASTContext &Context,
962947
return new (Mem) TemplateArgumentList(Args);
963948
}
964949

965-
unsigned TemplateArgumentList::ComputeODRHash(ArrayRef<TemplateArgument> Args) {
966-
ODRHash Hasher;
967-
for (TemplateArgument TA : Args)
968-
Hasher.AddTemplateArgument(TA);
969-
970-
return Hasher.CalculateHash();
971-
}
972-
973950
FunctionTemplateSpecializationInfo *FunctionTemplateSpecializationInfo::Create(
974951
ASTContext &C, FunctionDecl *FD, FunctionTemplateDecl *Template,
975952
TemplateSpecializationKind TSK, TemplateArgumentList *TemplateArgs,

clang/lib/AST/ExternalASTSource.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,14 @@ ExternalASTSource::FindExternalVisibleDeclsByName(const DeclContext *DC,
9898
return false;
9999
}
100100

101-
void ExternalASTSource::LoadExternalSpecializations(const Decl *D, bool) {}
101+
bool ExternalASTSource::LoadExternalSpecializations(const Decl *D, bool) {
102+
return false;
103+
}
102104

103-
void ExternalASTSource::LoadExternalSpecializations(
104-
const Decl *D, ArrayRef<TemplateArgument>) {}
105+
bool ExternalASTSource::LoadExternalSpecializations(
106+
const Decl *D, ArrayRef<TemplateArgument>) {
107+
return false;
108+
}
105109

106110
void ExternalASTSource::completeVisibleDeclsMap(const DeclContext *DC) {}
107111

clang/lib/AST/ODRHash.cpp

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -819,16 +819,6 @@ void ODRHash::AddDecl(const Decl *D) {
819819

820820
AddDeclarationName(ND->getDeclName());
821821

822-
const auto *Specialization =
823-
dyn_cast<ClassTemplateSpecializationDecl>(D);
824-
AddBoolean(Specialization);
825-
if (Specialization) {
826-
const TemplateArgumentList &List = Specialization->getTemplateArgs();
827-
ID.AddInteger(List.size());
828-
for (const TemplateArgument &TA : List.asArray())
829-
AddTemplateArgument(TA);
830-
}
831-
832822
// If this was a specialization we should take into account its template
833823
// arguments. This helps to reduce collisions coming when visiting template
834824
// specialization types (eg. when processing type template arguments).

clang/lib/Sema/MultiplexExternalSemaSource.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,16 +115,21 @@ FindExternalVisibleDeclsByName(const DeclContext *DC, DeclarationName Name) {
115115
return AnyDeclsFound;
116116
}
117117

118-
void MultiplexExternalSemaSource::LoadExternalSpecializations(
118+
bool MultiplexExternalSemaSource::LoadExternalSpecializations(
119119
const Decl *D, bool OnlyPartial) {
120+
bool Loaded = false;
120121
for (size_t i = 0; i < Sources.size(); ++i)
121-
Sources[i]->LoadExternalSpecializations(D, OnlyPartial);
122+
Loaded |= Sources[i]->LoadExternalSpecializations(D, OnlyPartial);
123+
return Loaded;
122124
}
123125

124-
void MultiplexExternalSemaSource::LoadExternalSpecializations(
126+
bool MultiplexExternalSemaSource::LoadExternalSpecializations(
125127
const Decl *D, ArrayRef<TemplateArgument> TemplateArgs) {
128+
bool AnyNewSpecsLoaded = false;
126129
for (size_t i = 0; i < Sources.size(); ++i)
127-
Sources[i]->LoadExternalSpecializations(D, TemplateArgs);
130+
AnyNewSpecsLoaded |=
131+
Sources[i]->LoadExternalSpecializations(D, TemplateArgs);
132+
return AnyNewSpecsLoaded;
128133
}
129134

130135
void MultiplexExternalSemaSource::completeVisibleDeclsMap(const DeclContext *DC){

clang/lib/Serialization/ASTCommon.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ namespace serialization {
2323

2424
enum DeclUpdateKind {
2525
UPD_CXX_ADDED_IMPLICIT_MEMBER,
26-
UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION,
2726
UPD_CXX_ADDED_ANONYMOUS_NAMESPACE,
2827
UPD_CXX_ADDED_FUNCTION_DEFINITION,
2928
UPD_CXX_ADDED_VAR_DEFINITION,

0 commit comments

Comments
 (0)