forked from vgvassilev/clad
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8ed2707
commit 558c337
Showing
5 changed files
with
74 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#ifndef CLAD_DIFFERENTIATOR_JACOBIANMODEVISITOR_H | ||
#define CLAD_DIFFERENTIATOR_JACOBIANMODEVISITOR_H | ||
|
||
#include "VectorPushForwardModeVisitor.h" | ||
|
||
namespace clad { | ||
class JacobianModeVisitor : public VectorPushForwardModeVisitor { | ||
|
||
public: | ||
JacobianModeVisitor(DerivativeBuilder& builder, const DiffRequest& request); | ||
|
||
DiffMode GetPushForwardMode() override; | ||
|
||
std::string GetPushForwardFunctionSuffix() override; | ||
|
||
DerivativeAndOverload DeriveJacobian(); | ||
|
||
StmtDiff VisitReturnStmt(const clang::ReturnStmt* RS) override; | ||
}; | ||
} // end namespace clad | ||
|
||
#endif // CLAD_DIFFERENTIATOR_JACOBIANMODEVISITOR_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#include "clad/Differentiator/JacobianModeVisitor.h" | ||
|
||
#include "ConstantFolder.h" | ||
#include "clad/Differentiator/CladUtils.h" | ||
|
||
#include "llvm/Support/SaveAndRestore.h" | ||
|
||
using namespace clang; | ||
|
||
namespace clad { | ||
JacobianModeVisitor::JacobianModeVisitor(DerivativeBuilder& builder, | ||
const DiffRequest& request) | ||
: VectorPushForwardModeVisitor(builder, request) {} | ||
|
||
DiffMode JacobianModeVisitor::GetPushForwardMode() { | ||
return DiffMode::jacobian; | ||
} | ||
|
||
std::string JacobianModeVisitor::GetPushForwardFunctionSuffix() { | ||
return "_jac"; | ||
} | ||
|
||
DerivativeAndOverload JacobianModeVisitor::DeriveJacobian() { | ||
return DerivePushforward(m_DiffReq.Function, m_DiffReq); | ||
} | ||
|
||
StmtDiff JacobianModeVisitor::VisitReturnStmt(const clang::ReturnStmt* RS) { | ||
// If there is no return value, we must not attempt to differentiate | ||
if (!RS->getRetValue()) | ||
return nullptr; | ||
|
||
StmtDiff retValDiff = Visit(RS->getRetValue()); | ||
// return StmtDiff(retValDiff.getExpr_dx()); | ||
// This can instantiate as part of the move or copy initialization and | ||
// needs a fake source location. | ||
SourceLocation fakeLoc = utils::GetValidSLoc(m_Sema); | ||
Stmt* returnStmt = | ||
m_Sema | ||
.ActOnReturnStmt(fakeLoc, retValDiff.getExpr_dx(), getCurrentScope()) | ||
.get(); | ||
return StmtDiff(returnStmt); | ||
} | ||
} // end namespace clad |