Skip to content

Commit 8180ebe

Browse files
ovdiiuvvgvassilev
authored andcommitted
Rework lambdas in Clad
1 parent ef731f5 commit 8180ebe

5 files changed

Lines changed: 284 additions & 113 deletions

File tree

include/clad/Differentiator/ReverseModeVisitor.h

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@
2222
#include "clang/AST/StmtVisitor.h"
2323
#include "clang/Basic/Diagnostic.h"
2424
#include "clang/Basic/SourceLocation.h"
25+
#include "clang/Basic/Version.h"
2526
#include "clang/Sema/Sema.h"
26-
27-
#include <llvm/ADT/ArrayRef.h>
28-
#include <llvm/ADT/SmallVector.h>
27+
#include "llvm/ADT/ArrayRef.h"
28+
#include "llvm/ADT/SmallVector.h"
2929

3030
#include <array>
3131
#include <limits>
@@ -90,7 +90,7 @@ namespace clad {
9090

9191
unsigned outputArrayCursor = 0;
9292
unsigned numParams = 0;
93-
clang::Expr* m_Pullback = nullptr;
93+
llvm::SmallVector<clang::Expr*, 1> m_Pullback;
9494
const char* funcPostfix() const {
9595
if (m_DiffReq.Mode == DiffMode::jacobian)
9696
return "_jac";
@@ -386,6 +386,10 @@ namespace clad {
386386
StmtDiff VisitForStmt(const clang::ForStmt* FS);
387387
StmtDiff VisitIfStmt(const clang::IfStmt* If);
388388
StmtDiff VisitImplicitCastExpr(const clang::ImplicitCastExpr* ICE);
389+
390+
#if CLANG_VERSION_MAJOR > 16
391+
StmtDiff VisitLambdaExpr(const clang::LambdaExpr* LE);
392+
#endif // CLANG_VERSION_MAJOR
389393
StmtDiff
390394
VisitCXXFunctionalCastExpr(const clang::CXXFunctionalCastExpr* FCE);
391395
StmtDiff VisitCStyleCastExpr(const clang::CStyleCastExpr* CSCE);
@@ -696,8 +700,21 @@ namespace clad {
696700
///\paramp[in] source An external RMV source
697701
void AddExternalSource(ExternalRMVSource& source);
698702

703+
clang::QualType GetLambdaDerivativeType(const clang::LambdaExpr* LE) {
704+
clang::FunctionDecl* FD = LE->getCallOperator();
705+
llvm::SmallVector<const clang::ValueDecl*, 4> diffParams{};
706+
for (const auto* param : FD->parameters())
707+
diffParams.push_back(param);
708+
709+
return utils::GetDerivativeType(m_Sema, FD, DiffMode::pullback,
710+
diffParams,
711+
/*forCustomDerv=*/false,
712+
/*shouldUseRestoreTracker=*/false);
713+
}
714+
clang::Expr* buildDerivedLambda(const clang::LambdaExpr* LE);
699715
/// Builds and returns the sequence of derived function parameters.
700-
void BuildParams(llvm::SmallVectorImpl<clang::ParmVarDecl*>& params);
716+
void BuildParams(llvm::SmallVectorImpl<clang::ParmVarDecl*>& params,
717+
const clang::LambdaExpr* LE = nullptr);
701718

702719
/// Stores data required for differentiating a switch statement.
703720
struct SwitchStmtInfo {

lib/Differentiator/DiffPlanner.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1155,8 +1155,11 @@ static QualType GetDerivedFunctionType(const CallExpr* CE) {
11551155

11561156
const auto* MD = dyn_cast<CXXMethodDecl>(FD);
11571157
if (MD) {
1158-
if (isLambdaCallOperator(MD))
1158+
if (isLambdaCallOperator(MD) &&
1159+
m_TopMostReq->Mode == DiffMode::reverse) {
11591160
request.EnableVariedAnalysis = false;
1161+
return true;
1162+
}
11601163
const CXXRecordDecl* CD = MD->getParent();
11611164
if (clad::utils::hasNonDifferentiableAttribute(CD))
11621165
nonDiff = true;

0 commit comments

Comments
 (0)