Skip to content

Commit 1f545c6

Browse files
committed
Fix uninitialized record assignment in reverse mode
Fixes: #1283
1 parent 373d70d commit 1f545c6

2 files changed

Lines changed: 30 additions & 3 deletions

File tree

lib/Differentiator/ReverseModeVisitor.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3794,12 +3794,18 @@ Expr* ReverseModeVisitor::getStdInitListSizeExpr(const Expr* E) {
37943794
auto* decl = VDDiff.getDecl();
37953795
if (VD->getInit()) {
37963796
auto* declRef = BuildDeclRef(decl);
3797+
Expr* init = decl->getInit();
3798+
const auto* CE = dyn_cast_or_null<CXXConstructExpr>(
3799+
init ? init->IgnoreImplicit() : nullptr);
3800+
if (!init ||
3801+
(CE && !CE->getNumArgs() && !CE->isListInitialization()))
3802+
init = getZeroInit(VD->getType());
37973803
Expr* assignment = nullptr;
37983804
if (isa<ArrayType>(VD->getType()))
3799-
assignment = BuildArrayAssignment(declRef, decl->getInit(),
3800-
direction::forward);
3805+
assignment =
3806+
BuildArrayAssignment(declRef, init, direction::forward);
38013807
else
3802-
assignment = BuildOp(BO_Assign, declRef, decl->getInit());
3808+
assignment = BuildOp(BO_Assign, declRef, init);
38033809
if (isInsideLoop) {
38043810
if (m_DiffReq.shouldBeRecorded(DS)) {
38053811
auto pushPop = StoreAndRestore(declRef, /*prefix=*/"_t",

test/Regressions/issue-1283.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// RUN: %cladclang -fsyntax-only %s -I%S/../../include 2>&1 | %filecheck %s
2+
3+
#include "clad/Differentiator/Differentiator.h"
4+
5+
struct Struct {
6+
double val;
7+
};
8+
9+
void fn(double a) {
10+
for (int i = 0; i < 1; ++i) {
11+
Struct s;
12+
}
13+
}
14+
15+
int main() {
16+
auto grad = clad::gradient(fn);
17+
}
18+
19+
// CHECK-LABEL: void fn_grad
20+
// CHECK: for (i = 0; i < 1; ++i) {
21+
// CHECK: {{^ *s = \{0\.\};$}}

0 commit comments

Comments
 (0)