Skip to content

Commit

Permalink
Add custom derivative to DerivativeSet
Browse files Browse the repository at this point in the history
This is required if the definition of the custom derivative is not found in the current translation unit and is linked in from another.
Adding it to the set of derivatives ensures that the custom derivative is not differentiated again using numerical differentiation due to an unavailable definition.
  • Loading branch information
vaithak committed May 18, 2024
1 parent d1fec23 commit e2e5128
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 4 deletions.
4 changes: 2 additions & 2 deletions include/clad/Differentiator/DerivativeBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ namespace clad {
clang::Sema& m_Sema;
plugin::CladPlugin& m_CladPlugin;
clang::ASTContext& m_Context;
const DerivedFnCollector& m_DFC;
DerivedFnCollector& m_DFC;
clad::DynamicGraph<DiffRequest>& m_DiffRequestGraph;
std::unique_ptr<utils::StmtClone> m_NodeCloner;
clang::NamespaceDecl* m_BuiltinDerivativesNSD;
Expand Down Expand Up @@ -135,7 +135,7 @@ namespace clad {

public:
DerivativeBuilder(clang::Sema& S, plugin::CladPlugin& P,
const DerivedFnCollector& DFC,
DerivedFnCollector& DFC,
clad::DynamicGraph<DiffRequest>& DRG);
~DerivativeBuilder();
/// Reset the model use for error estimation (if any).
Expand Down
3 changes: 3 additions & 0 deletions include/clad/Differentiator/DerivedFnCollector.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ class DerivedFnCollector {
/// Adds a derived function to the collection.
void Add(const DerivedFnInfo& DFI);

/// Adds a function to derivative set.
void AddToDerivativeSet(const clang::FunctionDecl* FD);

/// Finds a `DerivedFnInfo` object in the collection that satisfies the
/// given differentiation request.
DerivedFnInfo Find(const DiffRequest& request) const;
Expand Down
11 changes: 10 additions & 1 deletion lib/Differentiator/DerivativeBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ using namespace clang;
namespace clad {

DerivativeBuilder::DerivativeBuilder(clang::Sema& S, plugin::CladPlugin& P,
const DerivedFnCollector& DFC,
DerivedFnCollector& DFC,
clad::DynamicGraph<DiffRequest>& G)
: m_Sema(S), m_CladPlugin(P), m_Context(S.getASTContext()), m_DFC(DFC),
m_DiffRequestGraph(G),
Expand Down Expand Up @@ -244,6 +244,15 @@ static void registerDerivative(FunctionDecl* derivedFD, Sema& semaRef) {
Expr* UnresolvedLookup =
m_Sema.BuildDeclarationNameExpr(SS, R, /*ADL*/ false).get();

// Add the custom derivative to the set of derivatives.
// This is required in case the definition of the custom derivative
// is not found in the current translation unit and is linked in
// from another translation unit.
// Adding it to the set of derivatives ensures that the custom
// derivative is not differentiated again using numerical
// differentiation due to unavailable definition.
m_DFC.AddToDerivativeSet(R.getAsSingle<FunctionDecl>());

auto MARargs = llvm::MutableArrayRef<Expr*>(CallArgs);

SourceLocation Loc;
Expand Down
6 changes: 5 additions & 1 deletion lib/Differentiator/DerivedFnCollector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ void DerivedFnCollector::Add(const DerivedFnInfo& DFI) {
"`DerivedFnCollector::Add` more than once for the same derivative "
". Ideally, we shouldn't do either.");
m_DerivedFnInfoCollection[DFI.OriginalFn()].push_back(DFI);
m_DerivativeSet.insert(DFI.DerivedFn());
AddToDerivativeSet(DFI.DerivedFn());
}

void DerivedFnCollector::AddToDerivativeSet(const clang::FunctionDecl* FD) {
m_DerivativeSet.insert(FD);
}

bool DerivedFnCollector::AlreadyExists(const DerivedFnInfo& DFI) const {
Expand Down

0 comments on commit e2e5128

Please sign in to comment.