Skip to content

Commit 375f8db

Browse files
Shubham Shuklavgvassilev
authored andcommitted
Fix 1682
1 parent dc2dd69 commit 375f8db

5 files changed

Lines changed: 91 additions & 4 deletions

File tree

include/clad/Differentiator/BaseForwardModeVisitor.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ class BaseForwardModeVisitor
124124
StmtDiff VisitNullStmt(const clang::NullStmt* NS) { return StmtDiff{}; };
125125
StmtDiff
126126
VisitCXXStdInitializerListExpr(const clang::CXXStdInitializerListExpr* ILE);
127+
StmtDiff VisitGNUNullExpr(const clang::GNUNullExpr* E);
128+
StmtDiff VisitPredefinedExpr(const clang::PredefinedExpr* E);
127129

128130
StmtDiff VisitOMPExecutableDirective(const clang::OMPExecutableDirective* D);
129131
StmtDiff VisitOMPParallelDirective(const clang::OMPParallelDirective* D);

include/clad/Differentiator/ReverseModeVisitor.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,8 @@ namespace clad {
407407
StmtDiff VisitForStmt(const clang::ForStmt* FS);
408408
StmtDiff VisitIfStmt(const clang::IfStmt* If);
409409
StmtDiff VisitImplicitCastExpr(const clang::ImplicitCastExpr* ICE);
410+
StmtDiff VisitGNUNullExpr(const clang::GNUNullExpr* E);
411+
StmtDiff VisitPredefinedExpr(const clang::PredefinedExpr* E);
410412

411413
#if CLANG_VERSION_MAJOR > 16
412414
StmtDiff VisitLambdaExpr(const clang::LambdaExpr* LE);

lib/Differentiator/BaseForwardModeVisitor.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1612,6 +1612,21 @@ BaseForwardModeVisitor::VisitCStyleCastExpr(const CStyleCastExpr* CSCE) {
16121612
return StmtDiff(castExpr, castExprDiff);
16131613
}
16141614

1615+
StmtDiff BaseForwardModeVisitor::VisitGNUNullExpr(const clang::GNUNullExpr* E) {
1616+
auto* Constant0 =
1617+
ConstantFolder::synthesizeLiteral(m_Context.IntTy, m_Context, /*val=*/0);
1618+
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast)
1619+
return StmtDiff(const_cast<clang::GNUNullExpr*>(E), Constant0);
1620+
}
1621+
1622+
StmtDiff
1623+
BaseForwardModeVisitor::VisitPredefinedExpr(const clang::PredefinedExpr* E) {
1624+
auto* Constant0 =
1625+
ConstantFolder::synthesizeLiteral(m_Context.IntTy, m_Context, /*val=*/0);
1626+
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast)
1627+
return StmtDiff(const_cast<clang::PredefinedExpr*>(E), Constant0);
1628+
}
1629+
16151630
StmtDiff
16161631
BaseForwardModeVisitor::VisitCXXNamedCastExpr(const CXXNamedCastExpr* NCE) {
16171632
StmtDiff subExprDiff = Visit(NCE->getSubExpr());

lib/Differentiator/ReverseModeVisitor.cpp

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1931,10 +1931,6 @@ Expr* ReverseModeVisitor::getStdInitListSizeExpr(const Expr* E) {
19311931
}
19321932
}
19331933

1934-
// FIXME: Revisit this when variadic functions are supported.
1935-
if (FD->getNameAsString() == "printf" || FD->getNameAsString() == "fprintf")
1936-
return StmtDiff(Clone(CE));
1937-
19381934
Expr* CUDAExecConfig = nullptr;
19391935
if (const auto* KCE = dyn_cast<CUDAKernelCallExpr>(CE))
19401936
CUDAExecConfig = Clone(KCE->getConfig());
@@ -2148,6 +2144,10 @@ Expr* ReverseModeVisitor::getStdInitListSizeExpr(const Expr* E) {
21482144
i = skip_this, e = CE->getNumArgs();
21492145
i != e; ++i) {
21502146
const Expr* arg = CE->getArg(i);
2147+
if ((i - skip_this) >= FD->getNumParams()) {
2148+
CallArgs.push_back(Clone(arg));
2149+
continue;
2150+
}
21512151
const auto* PVD = FD->getParamDecl(i - skip_this);
21522152
StmtDiff argDiff =
21532153
DifferentiateCallArg(arg, PVD, PreCallStmts, /*isNonDiff=*/nonDiff,
@@ -2907,6 +2907,8 @@ Expr* ReverseModeVisitor::getStdInitListSizeExpr(const Expr* E) {
29072907
direction::reverse);
29082908
auto* condDiffStored = IfStmtDiff.getRevSweepAsExpr();
29092909
return BuildOp(BO_LAnd, condDiffStored, condVarRef);
2910+
} else if (opCode == BO_Rem) {
2911+
return BuildOp(opCode, Visit(L).getExpr(), Visit(R).getExpr());
29102912
} else {
29112913
// We should not output any warning on visiting boolean conditions
29122914
// FIXME: We should support boolean differentiation or ignore it
@@ -3429,6 +3431,21 @@ Expr* ReverseModeVisitor::getStdInitListSizeExpr(const Expr* E) {
34293431
return result;
34303432
}
34313433

3434+
StmtDiff ReverseModeVisitor::VisitGNUNullExpr(const clang::GNUNullExpr* E) {
3435+
auto* Constant0 = ConstantFolder::synthesizeLiteral(m_Context.IntTy,
3436+
m_Context, /*val=*/0);
3437+
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast)
3438+
return StmtDiff(const_cast<clang::GNUNullExpr*>(E), Constant0);
3439+
}
3440+
3441+
StmtDiff
3442+
ReverseModeVisitor::VisitPredefinedExpr(const clang::PredefinedExpr* E) {
3443+
auto* Constant0 = ConstantFolder::synthesizeLiteral(m_Context.IntTy,
3444+
m_Context, /*val=*/0);
3445+
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast)
3446+
return StmtDiff(const_cast<clang::PredefinedExpr*>(E), Constant0);
3447+
}
3448+
34323449
StmtDiff ReverseModeVisitor::VisitCXXFunctionalCastExpr(
34333450
const clang::CXXFunctionalCastExpr* FCE) {
34343451
StmtDiff castExprDiff = Visit(FCE->getSubExpr(), dfdx());

test/Regressions/issue-1682.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// RUN: %cladclang -fsyntax-only -Xclang -verify %s -I%S/../../include
2+
3+
#include "clad/Differentiator/Differentiator.h"
4+
5+
extern "C" int printf(const char*, ...);
6+
void a(...);
7+
int b; // expected-warning {{gradient uses a global variable 'b'; rerunning the gradient requires 'b' to be reset}}
8+
9+
void c(float) {
10+
// expected-warning@+2 {{attempted differentiation of function 'a' without definition and no suitable overload was found in namespace 'custom_derivatives'}}
11+
// expected-note@+1 {{numerical differentiation is not viable for 'a'; considering 'a' as 0}}
12+
a(b);
13+
}
14+
15+
double f0(double x) {
16+
int index = 10 % 3;
17+
const char* func = __func__;
18+
void* ptr = __null;
19+
20+
// expected-warning@+2 {{attempted differentiation of function 'printf' without definition and no suitable overload was found in namespace 'custom_derivatives'}}
21+
// expected-note@+1 {{numerical differentiation is not viable for 'printf'; considering 'printf' as 0}}
22+
printf("%f", x);
23+
24+
return x * x;
25+
}
26+
27+
auto df = clad::gradient(f0);
28+
auto dg = clad::gradient(c);
29+
30+
int b_fwd;
31+
32+
void c_fwd(float) {
33+
// expected-warning@+2 {{attempted differentiation of function 'a' without definition and no suitable overload was found in namespace 'custom_derivatives'}}
34+
// expected-note@+1 {{numerical differentiation is not viable for 'a'; considering 'a' as 0}}
35+
a(b_fwd);
36+
}
37+
38+
double f0_fwd(double x) {
39+
int index = 10 % 3;
40+
const char* func = __func__;
41+
void* ptr = __null;
42+
43+
// expected-warning@+2 {{attempted differentiation of function 'printf' without definition and no suitable overload was found in namespace 'custom_derivatives'}}
44+
// expected-note@+1 {{numerical differentiation is not viable for 'printf'; considering 'printf' as 0}}
45+
printf("%f", x);
46+
47+
return x * x;
48+
}
49+
50+
auto df_fwd = clad::differentiate(f0_fwd);
51+
auto dg_fwd = clad::differentiate(c_fwd);

0 commit comments

Comments
 (0)