Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions include/clad/Differentiator/CladUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,8 @@ namespace clad {

/// Find namespace clad declaration.
clang::NamespaceDecl* GetCladNamespace(clang::Sema& S);
/// Reset AST-owned lookup caches for a new translation unit.
void ResetCladUtilsASTCache(clang::ASTContext& C);
/// Create clad::array<T> type.
clang::QualType GetCladArrayOfType(clang::Sema& S, clang::QualType T);
/// Create clad::matrix<T> type.
Expand Down
72 changes: 44 additions & 28 deletions lib/Differentiator/CladUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -892,17 +892,36 @@ namespace clad {
return true;
}

// NOLINTBEGIN(cppcoreguidelines-avoid-non-const-global-variables)
namespace {
NamespaceDecl* CachedCladNS = nullptr;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: variable 'CachedCladNS' is non-const and globally accessible, consider making it const [cppcoreguidelines-avoid-non-const-global-variables]

    NamespaceDecl* CachedCladNS = nullptr;
                   ^

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: variable 'CachedCladNS' provides global access to a non-const object; consider making the pointed-to data 'const' [cppcoreguidelines-avoid-non-const-global-variables]

    NamespaceDecl* CachedCladNS = nullptr;
                   ^

QualType CachedRestoreTrackerTy;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: variable 'CachedRestoreTrackerTy' is non-const and globally accessible, consider making it const [cppcoreguidelines-avoid-non-const-global-variables]

    QualType CachedRestoreTrackerTy;
             ^

TemplateDecl* CachedMatrixDecl = nullptr;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: variable 'CachedMatrixDecl' is non-const and globally accessible, consider making it const [cppcoreguidelines-avoid-non-const-global-variables]

    TemplateDecl* CachedMatrixDecl = nullptr;
                  ^

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: variable 'CachedMatrixDecl' provides global access to a non-const object; consider making the pointed-to data 'const' [cppcoreguidelines-avoid-non-const-global-variables]

    TemplateDecl* CachedMatrixDecl = nullptr;
                  ^

TemplateDecl* CachedArrayDecl = nullptr;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: variable 'CachedArrayDecl' is non-const and globally accessible, consider making it const [cppcoreguidelines-avoid-non-const-global-variables]

    TemplateDecl* CachedArrayDecl = nullptr;
                  ^

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: variable 'CachedArrayDecl' provides global access to a non-const object; consider making the pointed-to data 'const' [cppcoreguidelines-avoid-non-const-global-variables]

    TemplateDecl* CachedArrayDecl = nullptr;
                  ^

TemplateDecl* CachedArrayRefDecl = nullptr;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: variable 'CachedArrayRefDecl' is non-const and globally accessible, consider making it const [cppcoreguidelines-avoid-non-const-global-variables]

    TemplateDecl* CachedArrayRefDecl = nullptr;
                  ^

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: variable 'CachedArrayRefDecl' provides global access to a non-const object; consider making the pointed-to data 'const' [cppcoreguidelines-avoid-non-const-global-variables]

    TemplateDecl* CachedArrayRefDecl = nullptr;
                  ^

TemplateDecl* CachedTagDecl = nullptr;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: variable 'CachedTagDecl' is non-const and globally accessible, consider making it const [cppcoreguidelines-avoid-non-const-global-variables]

    TemplateDecl* CachedTagDecl = nullptr;
                  ^

} // namespace
// NOLINTEND(cppcoreguidelines-avoid-non-const-global-variables)

void ResetCladUtilsASTCache(ASTContext&) {
CachedCladNS = nullptr;
CachedRestoreTrackerTy = QualType();
CachedMatrixDecl = nullptr;
CachedArrayDecl = nullptr;
CachedArrayRefDecl = nullptr;
CachedTagDecl = nullptr;
}

NamespaceDecl* GetCladNamespace(Sema& S) {
static NamespaceDecl* Result = nullptr;
if (Result)
return Result;
if (CachedCladNS)
return CachedCladNS;
DeclarationName CladName = &S.getASTContext().Idents.get("clad");
LookupResult CladR(S, CladName, noLoc, Sema::LookupNamespaceName,
CLAD_COMPAT_Sema_ForVisibleRedeclaration);
S.LookupQualifiedName(CladR, S.getASTContext().getTranslationUnitDecl());
assert(!CladR.empty() && "cannot find clad namespace");
Result = cast<NamespaceDecl>(CladR.getFoundDecl());
return Result;
CachedCladNS = cast<NamespaceDecl>(CladR.getFoundDecl());
return CachedCladNS;
}

Expr* getZeroInit(QualType T, Sema& S) {
Expand Down Expand Up @@ -945,9 +964,8 @@ namespace clad {
}

clang::QualType GetRestoreTrackerType(clang::Sema& S) {
static QualType T;
if (!T.isNull())
return T;
if (!CachedRestoreTrackerTy.isNull())
return CachedRestoreTrackerTy;
NamespaceDecl* CladNS = GetCladNamespace(S);
CXXScopeSpec CSS;
CSS.Extend(S.getASTContext(), CladNS, noLoc, noLoc);
Expand All @@ -961,15 +979,16 @@ namespace clad {
// This will instantiate restore_tracker<T> type and return it.
auto* RD = cast<RecordDecl>(TrackerR.getFoundDecl());
ASTContext& C = S.getASTContext();
T = clad_compat::getRecordType(C, RD);
CachedRestoreTrackerTy = clad_compat::getRecordType(C, RD);
// Get clad namespace and its identifier clad::.
clad_compat::NestedNameSpecifierTy NS = CSS.getScopeRep();

// Create elaborated type with namespace specifier,
// i.e. class<T> -> clad::class<T>
T = clad_compat::getElaboratedType(
C, clad_compat::ElaboratedTypeKeyword_None, NS, T);
return T;
CachedRestoreTrackerTy = clad_compat::getElaboratedType(
C, clad_compat::ElaboratedTypeKeyword_None, NS,
CachedRestoreTrackerTy);
return CachedRestoreTrackerTy;
}

TemplateDecl* LookupTemplateDeclInCladNamespace(Sema& S,
Expand Down Expand Up @@ -1134,19 +1153,18 @@ namespace clad {
}

QualType GetCladMatrixOfType(Sema& S, clang::QualType T) {
static TemplateDecl* matrixDecl = nullptr;
if (!matrixDecl)
matrixDecl =
if (!CachedMatrixDecl)
CachedMatrixDecl =
utils::LookupTemplateDeclInCladNamespace(S,
/*ClassName=*/"matrix");
return InstantiateTemplate(S, matrixDecl, {T});
return InstantiateTemplate(S, CachedMatrixDecl, {T});
}

QualType GetCladArrayOfType(Sema& S, clang::QualType T) {
static TemplateDecl* arrayDecl = nullptr;
if (!arrayDecl)
arrayDecl = LookupTemplateDeclInCladNamespace(S, /*ClassName=*/"array");
return utils::InstantiateTemplate(S, arrayDecl, {T});
if (!CachedArrayDecl)
CachedArrayDecl =
LookupTemplateDeclInCladNamespace(S, /*ClassName=*/"array");
return utils::InstantiateTemplate(S, CachedArrayDecl, {T});
}

bool IsDifferentiableType(QualType T) {
Expand Down Expand Up @@ -1188,11 +1206,10 @@ namespace clad {
}

QualType GetCladArrayRefOfType(Sema& S, QualType T) {
static TemplateDecl* arrayRefDecl = nullptr;
if (!arrayRefDecl)
arrayRefDecl = utils::LookupTemplateDeclInCladNamespace(
if (!CachedArrayRefDecl)
CachedArrayRefDecl = utils::LookupTemplateDeclInCladNamespace(
S, /*ClassName=*/"array_ref");
return utils::InstantiateTemplate(S, arrayRefDecl, {T});
return utils::InstantiateTemplate(S, CachedArrayRefDecl, {T});
}

QualType GetParameterDerivativeType(Sema& S, DiffMode Mode, QualType Type) {
Expand Down Expand Up @@ -1392,10 +1409,9 @@ namespace clad {
}

QualType GetCladTagOfType(Sema& S, QualType T) {
static clang::TemplateDecl* CladTag = nullptr;
if (!CladTag)
CladTag = utils::LookupTemplateDeclInCladNamespace(S, "Tag");
return utils::InstantiateTemplate(S, CladTag, {T});
if (!CachedTagDecl)
CachedTagDecl = utils::LookupTemplateDeclInCladNamespace(S, "Tag");
return utils::InstantiateTemplate(S, CachedTagDecl, {T});
}

Expr* GetCladTagExpr(Sema& S, QualType T) {
Expand Down
1 change: 1 addition & 0 deletions tools/ClangPlugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ void InitTimers();
std::vector<std::unique_ptr<ASTConsumer>>);

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