Skip to content

Commit 70edfb8

Browse files
PetroZarytskyivgvassilev
authored andcommitted
Support CompoundLiteralExpr in the reverse mode
Fixes #1522
1 parent 60831dc commit 70edfb8

3 files changed

Lines changed: 27 additions & 0 deletions

File tree

include/clad/Differentiator/ReverseModeVisitor.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,7 @@ namespace clad {
371371
StmtDiff VisitCallExpr(const clang::CallExpr* CE);
372372
virtual StmtDiff VisitCompoundStmt(const clang::CompoundStmt* CS);
373373
StmtDiff VisitConditionalOperator(const clang::ConditionalOperator* CO);
374+
StmtDiff VisitCompoundLiteralExpr(const clang::CompoundLiteralExpr* CLE);
374375
StmtDiff VisitCXXBoolLiteralExpr(const clang::CXXBoolLiteralExpr* BL);
375376
StmtDiff VisitCXXBindTemporaryExpr(const clang::CXXBindTemporaryExpr* BTE);
376377
StmtDiff VisitCharacterLiteral(const clang::CharacterLiteral* CL);

lib/Differentiator/ReverseModeVisitor.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -714,6 +714,18 @@ Expr* ReverseModeVisitor::getStdInitListSizeExpr(const Expr* E) {
714714
return StmtDiff(Clone(S));
715715
}
716716

717+
StmtDiff
718+
ReverseModeVisitor::VisitCompoundLiteralExpr(const CompoundLiteralExpr* CLE) {
719+
StmtDiff result = Visit(CLE->getInitializer());
720+
ParsedType PT = ParsedType::make(CLE->getType());
721+
result.updateStmt(
722+
m_Sema.ActOnCompoundLiteral(noLoc, PT, noLoc, result.getExpr()).get());
723+
result.updateStmtDx(
724+
m_Sema.ActOnCompoundLiteral(noLoc, PT, noLoc, result.getExpr_dx())
725+
.get());
726+
return result;
727+
}
728+
717729
StmtDiff ReverseModeVisitor::VisitCompoundStmt(const CompoundStmt* CS) {
718730
int scopeFlags = Scope::DeclScope;
719731
// If this is the outermost compound statement of the function,

test/Gradient/UserDefinedTypes.C

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1285,6 +1285,17 @@ double fn34(double x, double y) {
12851285
// CHECK-NEXT: *_d_x += _d_obj_x.data;
12861286
// CHECK-NEXT:}
12871287

1288+
double fn35(double x, double y) {
1289+
double& ref = *(PtrAndValAggr){x, &y}.ptr;
1290+
return ref;
1291+
}
1292+
1293+
// CHECK: void fn35_grad(double x, double y, double *_d_x, double *_d_y) {
1294+
// CHECK-NEXT: double &_d_ref = *(PtrAndValAggr){0., _d_y}.ptr;
1295+
// CHECK-NEXT: double &ref = *(PtrAndValAggr){x, &y}.ptr;
1296+
// CHECK-NEXT: _d_ref += 1;
1297+
// CHECK-NEXT: }
1298+
12881299
void print(const Tangent& t) {
12891300
for (int i = 0; i < 5; ++i) {
12901301
printf("%.2f", t.data[i]);
@@ -1422,4 +1433,7 @@ int main() {
14221433

14231434
INIT_GRADIENT(fn34);
14241435
TEST_GRADIENT(fn34, /*numOfDerivativeArgs=*/2, -5, 6, &d_i, &d_j); // CHECK-EXEC: {1.00, 2.00}
1436+
1437+
INIT_GRADIENT(fn35);
1438+
TEST_GRADIENT(fn35, /*numOfDerivativeArgs=*/2, -5, 6, &d_i, &d_j); // CHECK-EXEC: {0.00, 1.00}
14251439
}

0 commit comments

Comments
 (0)