Skip to content

Commit bf419fc

Browse files
committed
Reset CladUtils AST caches at plugin initialization
1 parent 7be825b commit bf419fc

3 files changed

Lines changed: 45 additions & 28 deletions

File tree

include/clad/Differentiator/CladUtils.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,8 @@ namespace clad {
397397

398398
/// Find namespace clad declaration.
399399
clang::NamespaceDecl* GetCladNamespace(clang::Sema& S);
400+
/// Reset AST-owned lookup caches for a new translation unit.
401+
void ResetCladUtilsASTCache(clang::ASTContext& C);
400402
/// Create clad::array<T> type.
401403
clang::QualType GetCladArrayOfType(clang::Sema& S, clang::QualType T);
402404
/// Create clad::matrix<T> type.

lib/Differentiator/CladUtils.cpp

Lines changed: 42 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -892,17 +892,34 @@ namespace clad {
892892
return true;
893893
}
894894

895+
namespace {
896+
NamespaceDecl* CachedCladNS = nullptr;
897+
QualType CachedRestoreTrackerTy;
898+
TemplateDecl* CachedMatrixDecl = nullptr;
899+
TemplateDecl* CachedArrayDecl = nullptr;
900+
TemplateDecl* CachedArrayRefDecl = nullptr;
901+
TemplateDecl* CachedTagDecl = nullptr;
902+
}
903+
904+
void ResetCladUtilsASTCache(ASTContext&) {
905+
CachedCladNS = nullptr;
906+
CachedRestoreTrackerTy = QualType();
907+
CachedMatrixDecl = nullptr;
908+
CachedArrayDecl = nullptr;
909+
CachedArrayRefDecl = nullptr;
910+
CachedTagDecl = nullptr;
911+
}
912+
895913
NamespaceDecl* GetCladNamespace(Sema& S) {
896-
static NamespaceDecl* Result = nullptr;
897-
if (Result)
898-
return Result;
914+
if (CachedCladNS)
915+
return CachedCladNS;
899916
DeclarationName CladName = &S.getASTContext().Idents.get("clad");
900917
LookupResult CladR(S, CladName, noLoc, Sema::LookupNamespaceName,
901918
CLAD_COMPAT_Sema_ForVisibleRedeclaration);
902919
S.LookupQualifiedName(CladR, S.getASTContext().getTranslationUnitDecl());
903920
assert(!CladR.empty() && "cannot find clad namespace");
904-
Result = cast<NamespaceDecl>(CladR.getFoundDecl());
905-
return Result;
921+
CachedCladNS = cast<NamespaceDecl>(CladR.getFoundDecl());
922+
return CachedCladNS;
906923
}
907924

908925
Expr* getZeroInit(QualType T, Sema& S) {
@@ -945,9 +962,8 @@ namespace clad {
945962
}
946963

947964
clang::QualType GetRestoreTrackerType(clang::Sema& S) {
948-
static QualType T;
949-
if (!T.isNull())
950-
return T;
965+
if (!CachedRestoreTrackerTy.isNull())
966+
return CachedRestoreTrackerTy;
951967
NamespaceDecl* CladNS = GetCladNamespace(S);
952968
CXXScopeSpec CSS;
953969
CSS.Extend(S.getASTContext(), CladNS, noLoc, noLoc);
@@ -961,15 +977,16 @@ namespace clad {
961977
// This will instantiate restore_tracker<T> type and return it.
962978
auto* RD = cast<RecordDecl>(TrackerR.getFoundDecl());
963979
ASTContext& C = S.getASTContext();
964-
T = clad_compat::getRecordType(C, RD);
980+
CachedRestoreTrackerTy = clad_compat::getRecordType(C, RD);
965981
// Get clad namespace and its identifier clad::.
966982
clad_compat::NestedNameSpecifierTy NS = CSS.getScopeRep();
967983

968984
// Create elaborated type with namespace specifier,
969985
// i.e. class<T> -> clad::class<T>
970-
T = clad_compat::getElaboratedType(
971-
C, clad_compat::ElaboratedTypeKeyword_None, NS, T);
972-
return T;
986+
CachedRestoreTrackerTy = clad_compat::getElaboratedType(
987+
C, clad_compat::ElaboratedTypeKeyword_None, NS,
988+
CachedRestoreTrackerTy);
989+
return CachedRestoreTrackerTy;
973990
}
974991

975992
TemplateDecl* LookupTemplateDeclInCladNamespace(Sema& S,
@@ -1134,19 +1151,18 @@ namespace clad {
11341151
}
11351152

11361153
QualType GetCladMatrixOfType(Sema& S, clang::QualType T) {
1137-
static TemplateDecl* matrixDecl = nullptr;
1138-
if (!matrixDecl)
1139-
matrixDecl =
1154+
if (!CachedMatrixDecl)
1155+
CachedMatrixDecl =
11401156
utils::LookupTemplateDeclInCladNamespace(S,
11411157
/*ClassName=*/"matrix");
1142-
return InstantiateTemplate(S, matrixDecl, {T});
1158+
return InstantiateTemplate(S, CachedMatrixDecl, {T});
11431159
}
11441160

11451161
QualType GetCladArrayOfType(Sema& S, clang::QualType T) {
1146-
static TemplateDecl* arrayDecl = nullptr;
1147-
if (!arrayDecl)
1148-
arrayDecl = LookupTemplateDeclInCladNamespace(S, /*ClassName=*/"array");
1149-
return utils::InstantiateTemplate(S, arrayDecl, {T});
1162+
if (!CachedArrayDecl)
1163+
CachedArrayDecl =
1164+
LookupTemplateDeclInCladNamespace(S, /*ClassName=*/"array");
1165+
return utils::InstantiateTemplate(S, CachedArrayDecl, {T});
11501166
}
11511167

11521168
bool IsDifferentiableType(QualType T) {
@@ -1188,11 +1204,10 @@ namespace clad {
11881204
}
11891205

11901206
QualType GetCladArrayRefOfType(Sema& S, QualType T) {
1191-
static TemplateDecl* arrayRefDecl = nullptr;
1192-
if (!arrayRefDecl)
1193-
arrayRefDecl = utils::LookupTemplateDeclInCladNamespace(
1207+
if (!CachedArrayRefDecl)
1208+
CachedArrayRefDecl = utils::LookupTemplateDeclInCladNamespace(
11941209
S, /*ClassName=*/"array_ref");
1195-
return utils::InstantiateTemplate(S, arrayRefDecl, {T});
1210+
return utils::InstantiateTemplate(S, CachedArrayRefDecl, {T});
11961211
}
11971212

11981213
QualType GetParameterDerivativeType(Sema& S, DiffMode Mode, QualType Type) {
@@ -1392,10 +1407,9 @@ namespace clad {
13921407
}
13931408

13941409
QualType GetCladTagOfType(Sema& S, QualType T) {
1395-
static clang::TemplateDecl* CladTag = nullptr;
1396-
if (!CladTag)
1397-
CladTag = utils::LookupTemplateDeclInCladNamespace(S, "Tag");
1398-
return utils::InstantiateTemplate(S, CladTag, {T});
1410+
if (!CachedTagDecl)
1411+
CachedTagDecl = utils::LookupTemplateDeclInCladNamespace(S, "Tag");
1412+
return utils::InstantiateTemplate(S, CachedTagDecl, {T});
13991413
}
14001414

14011415
Expr* GetCladTagExpr(Sema& S, QualType T) {

tools/ClangPlugin.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ void InitTimers();
175175
std::vector<std::unique_ptr<ASTConsumer>>);
176176

177177
void CladPlugin::Initialize(clang::ASTContext& C) {
178+
utils::ResetCladUtilsASTCache(C);
178179
// We know we have a multiplexer. We commit a sin here by stealing it and
179180
// making the consumer pass-through so that we can delay all operations
180181
// until clad is happy.

0 commit comments

Comments
 (0)