Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lambda support in the reverse mode #1126

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions include/clad/Differentiator/CladUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ namespace clad {
clang::StringLiteral* CreateStringLiteral(clang::ASTContext& C,
llvm::StringRef str);

bool isLambdaQType(clang::QualType QT);

/// Returns true if `QT` is Array or Pointer Type, otherwise returns false.
bool isArrayOrPointerType(clang::QualType QT);

Expand Down
10 changes: 10 additions & 0 deletions include/clad/Differentiator/Compatibility.h
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,16 @@ getConstantArrayType(const ASTContext& Ctx, QualType EltTy,
#define CLAD_COMPAT_CLANG15_Declarator_DeclarationAttrs_ExtraParam clang::ParsedAttributesView::none(),
#endif

#if CLANG_VERSION_MAJOR > 12
#define CLAD_COMPAT_CXXRecordDecl_CreateLambda_DependencyKind( \
Copy link
Contributor

Choose a reason for hiding this comment

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

warning: function-like macro 'CLAD_COMPAT_CXXRecordDecl_CreateLambda_DependencyKind' used; consider a 'constexpr' template function [cppcoreguidelines-macro-usage]

#define CLAD_COMPAT_CXXRecordDecl_CreateLambda_DependencyKind(                 \
        ^

LAMBDACXXRECORDDECL) \
LAMBDACXXRECORDDECL->getLambdaDependencyKind()
#else
#define CLAD_COMPAT_CXXRecordDecl_CreateLambda_DependencyKind( \
LAMBDACXXRECORDDECL) \
LAMBDACXXRECORDDECL->isDependentLambda()
#endif

// Clang 12 add one extra param (FPO) that we get from Node in Create method of:
// ImplicitCastExpr, CStyleCastExpr, CXXStaticCastExpr and CXXFunctionalCastExpr

Expand Down
3 changes: 3 additions & 0 deletions include/clad/Differentiator/DiffPlanner.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ struct DiffRequest {
/// order derivatives.
const clang::CXXRecordDecl* Functor = nullptr;

/// If we're creating a pullback for a lambda function, we need to put the diffed function into a call operator of another lambda class. Therefore, we need to used a pre-created function declaration to put our derived code into. Only applicable if working with lambdas in the reverse mode.
clang::FunctionDecl* LambdaPreCreatedDerivativeTarget = nullptr;

/// Stores differentiation parameters information. Stored information
/// includes info on indices range for array parameters, and nested data
/// member information for record (class) type parameters.
Expand Down
7 changes: 7 additions & 0 deletions include/clad/Differentiator/ReverseModeVisitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,11 @@ namespace clad {
llvm::SmallVectorImpl<clang::Expr*>& outputArgs,
clang::Expr* CUDAExecConfig = nullptr);

clang::CXXRecordDecl*
diffLambdaCXXRecordDecl(const clang::CXXRecordDecl* Original);
clang::CXXMethodDecl*
DifferentiateCallOperatorIfLambda(const clang::CXXRecordDecl* RD, clang::FunctionDecl* preCreatedDerivativeDecl);

public:
ReverseModeVisitor(DerivativeBuilder& builder, const DiffRequest& request);
virtual ~ReverseModeVisitor();
Expand All @@ -381,8 +386,10 @@ namespace clad {
/// y" will give 'f_grad_0_1' and "x, z" will give 'f_grad_0_2'.
DerivativeAndOverload Derive();
DerivativeAndOverload DerivePullback();
DerivativeAndOverload DerivePullback(clang::FunctionDecl* preDerivative);
StmtDiff VisitArraySubscriptExpr(const clang::ArraySubscriptExpr* ASE);
StmtDiff VisitBinaryOperator(const clang::BinaryOperator* BinOp);
StmtDiff VisitLambdaExpr(const clang::LambdaExpr* LE);
StmtDiff VisitCallExpr(const clang::CallExpr* CE);
virtual StmtDiff VisitCompoundStmt(const clang::CompoundStmt* CS);
StmtDiff VisitConditionalOperator(const clang::ConditionalOperator* CO);
Expand Down
6 changes: 3 additions & 3 deletions include/clad/Differentiator/VisitorBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ namespace clad {
clang::Scope* scope, clang::Expr* Init = nullptr,
bool DirectInit = false, clang::TypeSourceInfo* TSI = nullptr,
clang::VarDecl::InitializationStyle IS =
clang::VarDecl::InitializationStyle::CInit);
clang::VarDecl::InitializationStyle::CInit, bool pushCodeSynthCtxt = false);
/// Builds variable declaration to be used inside the derivative
/// body.
/// \param[in] Type The type of variable declaration to build.
Expand All @@ -310,7 +310,7 @@ namespace clad {
clang::Expr* Init = nullptr, bool DirectInit = false,
clang::TypeSourceInfo* TSI = nullptr,
clang::VarDecl::InitializationStyle IS =
clang::VarDecl::InitializationStyle::CInit);
clang::VarDecl::InitializationStyle::CInit, bool pushCodeSynthCtxt = false);
/// Builds variable declaration to be used inside the derivative
/// body.
/// \param[in] Type The type of variable declaration to build.
Expand All @@ -326,7 +326,7 @@ namespace clad {
clang::Expr* Init = nullptr, bool DirectInit = false,
clang::TypeSourceInfo* TSI = nullptr,
clang::VarDecl::InitializationStyle IS =
clang::VarDecl::InitializationStyle::CInit);
clang::VarDecl::InitializationStyle::CInit, bool pushCodeSynthCtxt = false);
/// Builds variable declaration to be used inside the derivative
/// body in the derivative function global scope.
clang::VarDecl*
Expand Down
7 changes: 7 additions & 0 deletions lib/Differentiator/CladUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,13 @@ namespace clad {
return false;
}

bool isLambdaQType(QualType QT) {
if (const RecordType* RT = QT->getAs<RecordType>())
if (const CXXRecordDecl* RD = dyn_cast<CXXRecordDecl>(RT->getDecl()))
return RD->isLambda();
return false;
}

bool IsReferenceOrPointerArg(const Expr* arg) {
// The argument is passed by reference if it's passed as an L-value.
// However, if arg is a MaterializeTemporaryExpr, then arg is a
Expand Down
2 changes: 1 addition & 1 deletion lib/Differentiator/DerivativeBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ static void registerDerivative(FunctionDecl* derivedFD, Sema& semaRef) {
InitErrorEstimation(m_ErrorEstHandler, m_EstModel, *this, request);
V.AddExternalSource(*m_ErrorEstHandler.back());
}
result = V.DerivePullback();
result = V.DerivePullback(request.LambdaPreCreatedDerivativeTarget);
if (!m_ErrorEstHandler.empty())
CleanupErrorEstimation(m_ErrorEstHandler, m_EstModel);
} else if (request.Mode == DiffMode::reverse_mode_forward_pass) {
Expand Down
Loading