Skip to content

Give the _d_this and independent-var-count caches leak-free references. NFC - #1895

Merged
vgvassilev merged 1 commit into
masterfrom
clone-in-owner
Jul 16, 2026
Merged

Give the _d_this and independent-var-count caches leak-free references. NFC#1895
vgvassilev merged 1 commit into
masterfrom
clone-in-owner

Conversation

@vgvassilev

Copy link
Copy Markdown
Owner

Two per-derivative caches were handed to several consumers as one Expr node that must not be parented twice -- m_ThisExprDerivative (the _d_this adjoint) and the independent-variable count. A consumer that forgot to copy reintroduced the sharing findSharedNode now asserts against.

Route _d_this through a cloneThisExprDerivative() accessor that clones on read: it is a reused compound expression, so a structural copy is the right tool. For the count, cache the indepVarCount VarDecl (m_IndVarCountDecl) instead of a DeclRef to it and rebuild a fresh reference on every read -- a DeclRef is trivially rebuilt, so no node is shared and nothing is cloned, the discipline the m_Variables FIXME asks for. Its accumulator becomes a local threaded out of BuildVectorModeParams.

…s. NFC

Two per-derivative caches were handed to several consumers as one Expr node
that must not be parented twice -- m_ThisExprDerivative (the `_d_this` adjoint)
and the independent-variable count. A consumer that forgot to copy reintroduced
the sharing findSharedNode now asserts against.

Route _d_this through a cloneThisExprDerivative() accessor that clones on read:
it is a reused compound expression, so a structural copy is the right tool. For
the count, cache the indepVarCount VarDecl (m_IndVarCountDecl) instead of a
DeclRef to it and rebuild a fresh reference on every read -- a DeclRef is
trivially rebuilt, so no node is shared and nothing is cloned, the discipline
the m_Variables FIXME asks for. Its accumulator becomes a local threaded out of
BuildVectorModeParams.
@codecov

codecov Bot commented Jul 16, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@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

/// The generated `indepVarCount` variable (total number of independent
/// variables). Cached as the decl so each read rebuilds a fresh DeclRef
/// instead of sharing one node.
clang::VarDecl* m_IndVarCountDecl = 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: member variable 'm_IndVarCountDecl' has protected visibility [cppcoreguidelines-non-private-member-variables-in-classes]

  clang::VarDecl* m_IndVarCountDecl = nullptr;
                  ^

/// The generated `indepVarCount` variable (total number of independent
/// variables). Cached as the decl so each read rebuilds a fresh DeclRef
/// instead of sharing one node.
clang::VarDecl* m_IndVarCountDecl = 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: no header providing "clang::VarDecl" is directly included [misc-include-cleaner]

  clang::VarDecl* m_IndVarCountDecl = nullptr;
         ^

/// taken from the data member variables.
llvm::SmallVector<clang::ParmVarDecl*, 8>
BuildVectorModeParams(DiffParams& diffParams);
BuildVectorModeParams(DiffParams& diffParams, clang::Expr*& indVarCountExpr);

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: no header providing "clad::DiffParams" is directly included [misc-include-cleaner]

include/clad/Differentiator/VectorForwardModeVisitor.h:6:

- #include <unordered_map>
+ #include <clad/Differentiator/ParseDiffArgsTypes.h>
+ #include <unordered_map>

m_IndVarCountExpr =
BuildOp(BinaryOperatorKind::BO_Add, m_IndVarCountExpr, getSize);
indVarCountExpr =
BuildOp(BinaryOperatorKind::BO_Add, indVarCountExpr, getSize);

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: no header providing "clang::BinaryOperatorKind" is directly included [misc-include-cleaner]

              BuildOp(BinaryOperatorKind::BO_Add, indVarCountExpr, getSize);
                      ^

// and number of columns equal to the number of independent variables
llvm::SmallVector<Expr*, 3> args = {
getSize, CloneNode(m_IndVarCountExpr), offsetExpr};
llvm::SmallVector<Expr*, 3> args = {getSize, buildIndVarCountRef(),

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: no header providing "llvm::SmallVector" is directly included [misc-include-cleaner]

lib/Differentiator/VectorForwardModeVisitor.cpp:12:

+ #include <llvm/ADT/SmallVector.h>

@vgvassilev
vgvassilev merged commit 3cbe474 into master Jul 16, 2026
40 checks passed
@vgvassilev
vgvassilev deleted the clone-in-owner branch July 16, 2026 14:23
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.

1 participant