Skip to content

Commit e3d372d

Browse files
Fermats-Last-TheoremShubham Shukla
andauthored
Fix pointer type mismatch 1804 (#1805)
This fixes the const pointer mismatch issue that was unveiled due to the fixes in #1749. Previously the fallback for an inactive const parameter output a const double * type which resulted in a type mismatch. This PR adds a check for const pointees and remove their constness before returning the double *nullptr. Fixes #1804 Co-authored-by: Shubham Shukla <shubham@localhost>
1 parent a46878f commit e3d372d

2 files changed

Lines changed: 27 additions & 2 deletions

File tree

lib/Differentiator/BaseForwardModeVisitor.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1215,8 +1215,15 @@ StmtDiff BaseForwardModeVisitor::VisitCallExpr(const CallExpr* CE) {
12151215
}
12161216
}
12171217
}
1218-
if (!dArg)
1219-
dArg = getZeroInit(arg->getType());
1218+
if (!dArg) {
1219+
QualType zeroTy = arg->getType();
1220+
if (zeroTy->isPointerType()) {
1221+
QualType pointeeTy = zeroTy->getPointeeType();
1222+
if (pointeeTy.isConstQualified())
1223+
zeroTy = m_Context.getPointerType(pointeeTy.getUnqualifiedType());
1224+
}
1225+
dArg = getZeroInit(zeroTy);
1226+
}
12201227
// pointer/array arguments are dynamically synthesized above
12211228
diffArgs.push_back(dArg);
12221229
}

test/Regressions/issue-1804.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// RUN: %cladclang -c -std=c++17 -I%S/../../include %s
2+
3+
#include "clad/Differentiator/Differentiator.h"
4+
5+
double f_inner(double *params, double const *obs) {
6+
double arg = (obs[0] - params[1]) / params[2];
7+
return arg * arg;
8+
}
9+
10+
double f_outer(double *params, double const *obs) {
11+
return f_inner(params, obs) + params[0];
12+
}
13+
14+
void check() {
15+
auto hess = clad::hessian(f_outer, "params[0:2]");
16+
}
17+
18+
// CHECK: f_inner_pushforward_pullback(params, obs, (double[3]){1., 0., 0.}, nullptr, _d_t0, _d_params, (double[3]){0., 0., 0.}, nullptr);

0 commit comments

Comments
 (0)