Skip to content

Remove static CladUtils AST caches - #1856

Open
Elvand-Lie wants to merge 2 commits into
vgvassilev:masterfrom
Elvand-Lie:fix/remove-cladutils-static-caches
Open

Remove static CladUtils AST caches#1856
Elvand-Lie wants to merge 2 commits into
vgvassilev:masterfrom
Elvand-Lie:fix/remove-cladutils-static-caches

Conversation

@Elvand-Lie

Copy link
Copy Markdown
Contributor

Problem

Several CladUtils.cpp helpers cache Clang AST-owned declarations or types in function-local static variables:

  • GetCladNamespace() cached NamespaceDecl*
  • GetRestoreTrackerType() cached QualType
  • GetCladMatrixOfType(), GetCladArrayOfType(), GetCladArrayRefOfType(), and GetCladTagOfType() cached TemplateDecl*

Those objects are tied to the current Sema / ASTContext. Reusing them across translation units in a long-lived process can leave stale AST pointers, similar to the LookupResult cache issue fixed in #1838.

Solution

Remove those function-local static caches and perform the lookup for the current Sema when needed.

Testing

  • git diff --check HEAD

Local build was not run because this Windows environment does not have clang, llvm-config, cmake, or clang-format on PATH.

@github-actions

Copy link
Copy Markdown
Contributor

clang-tidy review says "All clean, LGTM! 👍"

@codecov

codecov Bot commented Jun 26, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@vgvassilev vgvassilev left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

That patch essentially makes things slower. While it is right in its core that we can have multiple instances of clad in the same process it is not yet a practical use-case. An alternative approach would be to provide some per plugin instance cache area which is somehow shared with the rest of the infrastructure. I think overall it is not worth the effort and the complexity right now.

@Elvand-Lie
Elvand-Lie force-pushed the fix/remove-cladutils-static-caches branch 2 times, most recently from bf419fc to 26b4acf Compare July 1, 2026 07:12

@github-actions github-actions Bot left a comment

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.

clang-tidy made some suggestions

There were too many comments to post at once. Showing the first 10 out of 11. Check the log or trigger a new build to see more.

}

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;
                   ^

}

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' provides global access to a non-const object; consider making the pointed-to data 'const' [cppcoreguidelines-avoid-non-const-global-variables]

    NamespaceDecl* CachedCladNS = nullptr;
                   ^


namespace {
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;
             ^

namespace {
NamespaceDecl* CachedCladNS = nullptr;
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;
                  ^

namespace {
NamespaceDecl* CachedCladNS = nullptr;
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' provides global access to a non-const object; consider making the pointed-to data 'const' [cppcoreguidelines-avoid-non-const-global-variables]

    TemplateDecl* CachedMatrixDecl = nullptr;
                  ^

NamespaceDecl* CachedCladNS = nullptr;
QualType CachedRestoreTrackerTy;
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;
                  ^

NamespaceDecl* CachedCladNS = nullptr;
QualType CachedRestoreTrackerTy;
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' provides global access to a non-const object; consider making the pointed-to data 'const' [cppcoreguidelines-avoid-non-const-global-variables]

    TemplateDecl* CachedArrayDecl = nullptr;
                  ^

QualType CachedRestoreTrackerTy;
TemplateDecl* CachedMatrixDecl = nullptr;
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;
                  ^

QualType CachedRestoreTrackerTy;
TemplateDecl* CachedMatrixDecl = nullptr;
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' 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* CachedMatrixDecl = nullptr;
TemplateDecl* CachedArrayDecl = nullptr;
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;
                  ^

@Elvand-Lie
Elvand-Lie force-pushed the fix/remove-cladutils-static-caches branch from 26b4acf to 8be8e96 Compare July 1, 2026 07:24
@github-actions

github-actions Bot commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

clang-tidy review says "All clean, LGTM! 👍"

@Elvand-Lie

Copy link
Copy Markdown
Contributor Author

@vgvassilev thanks again for the review. The first version was trying to avoid reusing AST-owned cached declarations/types across ASTContexts. After testing and looking at the hot path more carefully, I see why removing the caches entirely was the wrong tradeoff: it avoided stale pointers by making normal differentiation redo namespace/type/template lookups.

I reworked the PR to keep the cache fast path and reset the cached AST-owned values from CladPlugin::Initialize(ASTContext&). So the patch now keeps the cached NamespaceDecl*, QualType, and TemplateDecl* values, but clears them when a new plugin ASTContext is initialized.

Validation:

  • local WSL Linux build of clad
  • targeted lit suites: Arrays, ForwardMode, Gradient
  • full local check-clad: 181 discovered, 158 passed, 23 unsupported, 0 failed
  • GitHub CI is passing across the full matrix

I think this version addresses the practical stale-cache concern while preserving the existing performance model. If this still feels too narrow or not worth carrying right now, I am fine closing the PR.

@github-actions

github-actions Bot commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

clang-tidy review says "All clean, LGTM! 👍"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants