Skip to content

Commit 959f0be

Browse files
committed
Initialise f3's locals in ActivityReverse.
f3 declared `double x1, x2, x3, x4, x5 = 0;`, leaving x1..x4 uninitialised, and looped on `while (!x3)`. The expected gradient {0.00} only holds when that loop does not run, which relied on the indeterminate x3 happening to be nonzero -- undefined behaviour Valgrind reports as a conditional jump on an uninitialised value in the executed f3_grad. Initialise the locals with x3 nonzero so the loop is deterministically skipped and x stays inactive, preserving the checked result. Update the reproduced declaration in the CHECK block and drop the XFAIL: valgrind.
1 parent df61a9a commit 959f0be

1 file changed

Lines changed: 5 additions & 4 deletions

File tree

test/Analyses/ActivityReverse.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
// RUN: ./Activity.out | %filecheck_exec %s
33
// RUN: %cladclang -Xclang -plugin-arg-clad -Xclang -enable-va -Xclang -plugin-arg-clad -Xclang -disable-tbr %s -I%S/../../include -oActivity.out
44
// RUN: ./Activity.out | %filecheck_exec %s
5-
// FIXME: f3 reads uninitialised locals (test-source UB); drop when addressed.
6-
// XFAIL: valgrind
75
//CHECK-NOT: {{.*error|warning|note:.*}}
86

97
#include "clad/Differentiator/Differentiator.h"
@@ -86,7 +84,10 @@ double f2(double x){
8684
//CHECK-NEXT: }
8785

8886
double f3(double x){
89-
double x1, x2, x3, x4, x5 = 0;
87+
// x3 is nonzero so the loop does not run: x stays inactive (gradient 0),
88+
// which is what this exercises. Leaving x3 uninitialised was undefined
89+
// behaviour that only happened to skip the loop.
90+
double x1 = 0, x2 = 0, x3 = 1, x4 = 0, x5 = 0;
9091
while(!x3){
9192
x5 = x4;
9293
x4 = x3;
@@ -104,7 +105,7 @@ double f3(double x){
104105
//CHECK-NEXT: clad::tape<double> _t4 = {};
105106
//CHECK-NEXT: clad::tape<double> _t5 = {};
106107
//CHECK-NEXT: double _d_x1 = 0., _d_x2 = 0., _d_x3 = 0., _d_x4 = 0., _d_x5 = 0.;
107-
//CHECK-NEXT: double x1, x2, x3, x4, x5 = 0;
108+
//CHECK-NEXT: double x1 = 0, x2 = 0, x3 = 1, x4 = 0, x5 = 0;
108109
//CHECK-NEXT: unsigned {{int|long}} _t0 = 0;
109110
//CHECK-NEXT: while (!x3)
110111
//CHECK-NEXT: {

0 commit comments

Comments
 (0)