Give the _d_this and independent-var-count caches leak-free references. NFC - #1895
Conversation
…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 Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
| /// 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; |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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(), |
There was a problem hiding this comment.
warning: no header providing "llvm::SmallVector" is directly included [misc-include-cleaner]
lib/Differentiator/VectorForwardModeVisitor.cpp:12:
+ #include <llvm/ADT/SmallVector.h>
Two per-derivative caches were handed to several consumers as one Expr node that must not be parented twice -- m_ThisExprDerivative (the
_d_thisadjoint) 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.