aboutsummaryrefslogtreecommitdiff
path: root/clang/lib/Sema/MultiplexExternalSemaSource.cpp
diff options
context:
space:
mode:
authorChuanqi Xu <yedeng.yd@linux.alibaba.com>2024-12-11 09:40:47 +0800
committerGitHub <noreply@github.com>2024-12-11 09:40:47 +0800
commit20e904950967c125abc1e91f57e5a373987ff016 (patch)
tree617483fae537e543940d0d9ee7802af75dc4e304 /clang/lib/Sema/MultiplexExternalSemaSource.cpp
parentc00a708fc954f450679bf0e171029f8da4841cfb (diff)
downloadllvm-20e904950967c125abc1e91f57e5a373987ff016.zip
llvm-20e904950967c125abc1e91f57e5a373987ff016.tar.gz
llvm-20e904950967c125abc1e91f57e5a373987ff016.tar.bz2
[Serialization] Support loading template specializations lazily (#119333)
Reland https://github.com/llvm/llvm-project/pull/83237 --- (Original comments) Currently all the specializations of a template (including instantiation, specialization and partial specializations) will be loaded at once if we want to instantiate another instance for the template, or find instantiation for the template, or just want to complete the redecl chain. This means basically we need to load every specializations for the template once the template declaration got loaded. This is bad since when we load a specialization, we need to load all of its template arguments. Then we have to deserialize a lot of unnecessary declarations. For example, ``` // M.cppm export module M; export template <class T> class A {}; export class ShouldNotBeLoaded {}; export class Temp { A<ShouldNotBeLoaded> AS; }; // use.cpp import M; A<int> a; ``` We have a specialization ` A<ShouldNotBeLoaded>` in `M.cppm` and we instantiate the template `A` in `use.cpp`. Then we will deserialize `ShouldNotBeLoaded` surprisingly when compiling `use.cpp`. And this patch tries to avoid that. Given that the templates are heavily used in C++, this is a pain point for the performance. This patch adds MultiOnDiskHashTable for specializations in the ASTReader. Then we will only deserialize the specializations with the same template arguments. We made that by using ODRHash for the template arguments as the key of the hash table. To review this patch, I think `ASTReaderDecl::AddLazySpecializations` may be a good entry point.
Diffstat (limited to 'clang/lib/Sema/MultiplexExternalSemaSource.cpp')
-rw-r--r--clang/lib/Sema/MultiplexExternalSemaSource.cpp17
1 files changed, 17 insertions, 0 deletions
diff --git a/clang/lib/Sema/MultiplexExternalSemaSource.cpp b/clang/lib/Sema/MultiplexExternalSemaSource.cpp
index cd44483..5494426 100644
--- a/clang/lib/Sema/MultiplexExternalSemaSource.cpp
+++ b/clang/lib/Sema/MultiplexExternalSemaSource.cpp
@@ -115,6 +115,23 @@ FindExternalVisibleDeclsByName(const DeclContext *DC, DeclarationName Name) {
return AnyDeclsFound;
}
+bool MultiplexExternalSemaSource::LoadExternalSpecializations(
+ const Decl *D, bool OnlyPartial) {
+ bool Loaded = false;
+ for (size_t i = 0; i < Sources.size(); ++i)
+ Loaded |= Sources[i]->LoadExternalSpecializations(D, OnlyPartial);
+ return Loaded;
+}
+
+bool MultiplexExternalSemaSource::LoadExternalSpecializations(
+ const Decl *D, ArrayRef<TemplateArgument> TemplateArgs) {
+ bool AnyNewSpecsLoaded = false;
+ for (size_t i = 0; i < Sources.size(); ++i)
+ AnyNewSpecsLoaded |=
+ Sources[i]->LoadExternalSpecializations(D, TemplateArgs);
+ return AnyNewSpecsLoaded;
+}
+
void MultiplexExternalSemaSource::completeVisibleDeclsMap(const DeclContext *DC){
for(size_t i = 0; i < Sources.size(); ++i)
Sources[i]->completeVisibleDeclsMap(DC);