Skip to content

Commit

Permalink
Fix pointer zero initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
kchristin22 committed Aug 13, 2024
1 parent aee91cd commit 6130c91
Show file tree
Hide file tree
Showing 19 changed files with 171 additions and 171 deletions.
2 changes: 1 addition & 1 deletion lib/Differentiator/VisitorBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ namespace clad {
// FIXME: Consolidate other uses of synthesizeLiteral for creation 0 or 1.
if (T->isVoidType())
return nullptr;
if (T->isScalarType()) {
else if (T->isScalarType() && !T->isPointerType()) {
ExprResult Zero =
ConstantFolder::synthesizeLiteral(T, m_Context, 0);
CastKind CK = m_Sema.PrepareScalarCast(Zero, T);
Expand Down
2 changes: 1 addition & 1 deletion test/Gradient/Assignments.C
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ double f12(double x, double y) {
//CHECK-NEXT: {
//CHECK-NEXT: *_t2 = _t3;
//CHECK-NEXT: double _r_d2 = (_cond0 ? _d_t : _d_t);
//CHECK-NEXT: (_cond0 ? _d_t : _d_t) = 0;
//CHECK-NEXT: (_cond0 ? _d_t : _d_t) = 0.;
//CHECK-NEXT: (_cond0 ? _d_t : _d_t) += _r_d2 * y;
//CHECK-NEXT: *_d_y += *_t2 * _r_d2;
//CHECK-NEXT: if (_cond0) {
Expand Down
18 changes: 9 additions & 9 deletions test/Gradient/FunctionCalls.C
Original file line number Diff line number Diff line change
Expand Up @@ -818,7 +818,7 @@ double sq_defined_later(double x) {
// CHECK-NEXT: i += j;
// CHECK-NEXT: double _t1 = j;
// CHECK-NEXT: j += j;
// CHECK-NEXT: double _d_res = 0;
// CHECK-NEXT: double _d_res = 0.;
// CHECK-NEXT: double res = i + j;
// CHECK-NEXT: _d_res += _d_y;
// CHECK-NEXT: {
Expand Down Expand Up @@ -858,7 +858,7 @@ double sq_defined_later(double x) {
// CHECK-NEXT: int _d_i = 0;
// CHECK-NEXT: int i = 0;
// CHECK-NEXT: clad::tape<float> _t1 = {};
// CHECK-NEXT: float _d_res = 0;
// CHECK-NEXT: float _d_res = 0.F;
// CHECK-NEXT: float res = 0;
// CHECK-NEXT: unsigned {{int|long}} _t0 = {{0U|0UL}};
// CHECK-NEXT: for (i = 0; ; ++i) {
Expand Down Expand Up @@ -896,7 +896,7 @@ double sq_defined_later(double x) {
// CHECK-NEXT: {
// CHECK-NEXT: d = _t0;
// CHECK-NEXT: double _r_d0 = *_d_d;
// CHECK-NEXT: *_d_d = 0;
// CHECK-NEXT: *_d_d = 0.;
// CHECK-NEXT: *_d_d += 2 * _r_d0;
// CHECK-NEXT: }
// CHECK-NEXT: }
Expand All @@ -907,15 +907,15 @@ double sq_defined_later(double x) {
// CHECK-NEXT: {
// CHECK-NEXT: arr[0] = _t0;
// CHECK-NEXT: double _r_d0 = _d_arr[0];
// CHECK-NEXT: _d_arr[0] = 0;
// CHECK-NEXT: _d_arr[0] = 0.;
// CHECK-NEXT: _d_arr[0] += 5 * _r_d0;
// CHECK-NEXT: _d_arr[1] += _r_d0;
// CHECK-NEXT: }
// CHECK-NEXT: }

// CHECK: void identity_pullback(double &i, double _d_y, double *_d_i) {
// CHECK-NEXT: MyStruct::myFunction();
// CHECK-NEXT: double _d__d_i = 0;
// CHECK-NEXT: double _d__d_i = 0.;
// CHECK-NEXT: double _d_i0 = i;
// CHECK-NEXT: double _t0 = _d_i0;
// CHECK-NEXT: _d_i0 += 1;
Expand All @@ -929,7 +929,7 @@ double sq_defined_later(double x) {

// CHECK: clad::ValueAndAdjoint<double &, double &> identity_forw(double &i, double *_d_i) {
// CHECK-NEXT: MyStruct::myFunction();
// CHECK-NEXT: double _d__d_i = 0;
// CHECK-NEXT: double _d__d_i = 0.;
// CHECK-NEXT: double _d_i0 = i;
// CHECK-NEXT: double _t0 = _d_i0;
// CHECK-NEXT: _d_i0 += 1;
Expand Down Expand Up @@ -967,7 +967,7 @@ double sq_defined_later(double x) {
// CHECK-NEXT: if (_cond1) {
// CHECK-NEXT: _cond0 = _t0;
// CHECK-NEXT: double _r_d0 = _d_cond0;
// CHECK-NEXT: _d_cond0 = 0;
// CHECK-NEXT: _d_cond0 = 0.;
// CHECK-NEXT: }
// CHECK-NEXT: }
// CHECK-NEXT: }
Expand Down Expand Up @@ -1009,8 +1009,8 @@ double sq_defined_later(double x) {
//CHECK-NEXT: if (_cond0)
//CHECK-NEXT: _label0:
//CHECK-NEXT: {
//CHECK-NEXT: double _r0 = 0;
//CHECK-NEXT: double _r1 = 0;
//CHECK-NEXT: double _r0 = 0.;
//CHECK-NEXT: double _r1 = 0.;
//CHECK-NEXT: recFun_pullback(x - 1, y, _d_y0, &_r0, &_r1);
//CHECK-NEXT: *_d_x += _r0;
//CHECK-NEXT: *_d_y += _r1;
Expand Down
22 changes: 11 additions & 11 deletions test/Gradient/Loops.C
Original file line number Diff line number Diff line change
Expand Up @@ -1605,7 +1605,7 @@ double fn19(double* arr, int n) {
// CHECK-NEXT: int i = 0;
// CHECK-NEXT: clad::tape<double *> _t1 = {};
// CHECK-NEXT: clad::tape<double *> _t2 = {};
// CHECK-NEXT: double *_d_ref = 0.;
// CHECK-NEXT: double *_d_ref = {};
// CHECK-NEXT: double *ref = {};
// CHECK-NEXT: clad::tape<double> _t3 = {};
// CHECK-NEXT: double _d_res = 0.;
Expand Down Expand Up @@ -1652,7 +1652,7 @@ double f_loop_init_var(double lower, double upper) {

// CHECK: void f_loop_init_var_grad(double lower, double upper, double *_d_lower, double *_d_upper) {
// CHECK-NEXT: double _d_x = 0.;
// CHECK-NEXT: double x = 0;
// CHECK-NEXT: double x = 0.;
// CHECK-NEXT: clad::tape<double> _t1 = {};
// CHECK-NEXT: clad::tape<double> _t2 = {};
// CHECK-NEXT: double _d_sum = 0.;
Expand Down Expand Up @@ -2696,10 +2696,10 @@ double fn34(double x, double y){

//CHECK: void fn34_grad(double x, double y, double *_d_x, double *_d_y) {
//CHECK-NEXT: unsigned {{int|long}} _t0;
//CHECK-NEXT: double (*_d___range1)[3] = 0.;
//CHECK-NEXT: double (*_d___range1)[3] = {};
//CHECK-NEXT: double (*__range10)[3] = {};
//CHECK-NEXT: double *_d___begin1 = 0;
//CHECK-NEXT: double *__begin10 = 0;
//CHECK-NEXT: double *_d___begin1 = {};
//CHECK-NEXT: double *__begin10 = {};
//CHECK-NEXT: clad::tape<double> _t1 = {};
//CHECK-NEXT: clad::tape<double *> _t2 = {};
//CHECK-NEXT: clad::tape<double *> _t3 = {};
Expand All @@ -2713,8 +2713,8 @@ double fn34(double x, double y){
//CHECK-NEXT: __range10 = &a;
//CHECK-NEXT: __begin10 = *__range10;
//CHECK-NEXT: double *__end10 = *__range10 + {{3|3L}};
//CHECK-NEXT: double *_d_i = 0.;
//CHECK-NEXT: double *i = 0;
//CHECK-NEXT: double *_d_i = {};
//CHECK-NEXT: double *i = {};
//CHECK-NEXT: for (; __begin10 != __end10; ++__begin10 , ++_d___begin1) {
//CHECK-NEXT: {
//CHECK-NEXT: _t0++;
Expand Down Expand Up @@ -2762,10 +2762,10 @@ double fn35(double x, double y){

//CHECK: void fn35_grad(double x, double y, double *_d_x, double *_d_y) {
//CHECK-NEXT: unsigned {{int|long}} _t0;
//CHECK-NEXT: double (*_d___range1)[3] = 0.;
//CHECK-NEXT: double (*_d___range1)[3] = {};
//CHECK-NEXT: double (*__range10)[3] = {};
//CHECK-NEXT: double *_d___begin1 = 0.;
//CHECK-NEXT: double *__begin10 = 0;
//CHECK-NEXT: double *_d___begin1 = {};
//CHECK-NEXT: double *__begin10 = {};
//CHECK-NEXT: clad::tape<double> _t1 = {};
//CHECK-NEXT: clad::tape<double> _t2 = {};
//CHECK-NEXT: clad::tape<double> _t3 = {};
Expand All @@ -2781,7 +2781,7 @@ double fn35(double x, double y){
//CHECK-NEXT: __begin10 = *__range10;
//CHECK-NEXT: double *__end10 = *__range10 + {{3|3L}};
//CHECK-NEXT: double _d_i = 0.;
//CHECK-NEXT: double i = 0;
//CHECK-NEXT: double i = 0.;
//CHECK-NEXT: for (; __begin10 != __end10; ++__begin10 , ++_d___begin1) {
//CHECK-NEXT: {
//CHECK-NEXT: _t0++;
Expand Down
22 changes: 11 additions & 11 deletions test/Gradient/MemberFunctions.C
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ public:
double partial_mem_fn(double i, double j) { return (x + y) * i + i * j; }

// CHECK: void partial_mem_fn_grad_0(double i, double j, SimpleFunctions *_d_this, double *_d_i) {
// CHECK-NEXT: double _d_j = 0;
// CHECK-NEXT: double _d_j = 0.;
// CHECK-NEXT: {
// CHECK-NEXT: (*_d_this).x += 1 * i;
// CHECK-NEXT: (*_d_this).y += 1 * i;
Expand Down Expand Up @@ -438,7 +438,7 @@ double fn2(SimpleFunctions& sf, double i) {
// CHECK-NEXT: SimpleFunctions _t0 = sf;
// CHECK-NEXT: clad::ValueAndAdjoint<double &, double &> _t1 = _t0.ref_mem_fn_forw(i, &(*_d_sf), nullptr);
// CHECK-NEXT: {
// CHECK-NEXT: double _r0 = 0;
// CHECK-NEXT: double _r0 = 0.;
// CHECK-NEXT: _t0.ref_mem_fn_pullback(i, 1, &(*_d_sf), &_r0);
// CHECK-NEXT: *_d_i += _r0;
// CHECK-NEXT: }
Expand All @@ -463,7 +463,7 @@ double fn5(SimpleFunctions& v, double value) {
// CHECK-NEXT: clad::ValueAndAdjoint<SimpleFunctions &, SimpleFunctions &> _t1 = _t0.operator_plus_equal_forw(value, &(*_d_v), nullptr);
// CHECK-NEXT: (*_d_v).x += 1;
// CHECK-NEXT: {
// CHECK-NEXT: double _r0 = 0;
// CHECK-NEXT: double _r0 = 0.;
// CHECK-NEXT: _t0.operator_plus_equal_pullback(value, {}, &(*_d_v), &_r0);
// CHECK-NEXT: *_d_value += _r0;
// CHECK-NEXT: }
Expand Down Expand Up @@ -537,7 +537,7 @@ int main() {
auto d_const_volatile_lval_ref_mem_fn_i = clad::gradient(&SimpleFunctions::const_volatile_lval_ref_mem_fn, "i");

// CHECK: void const_volatile_lval_ref_mem_fn_grad_0(double i, double j, volatile SimpleFunctions *_d_this, double *_d_i) const volatile & {
// CHECK-NEXT: double _d_j = 0;
// CHECK-NEXT: double _d_j = 0.;
// CHECK-NEXT: double _t0 = (this->x + this->y);
// CHECK-NEXT: {
// CHECK-NEXT: (*_d_this).x += 1 * i;
Expand All @@ -551,7 +551,7 @@ int main() {
auto d_const_volatile_rval_ref_mem_fn_j = clad::gradient(&SimpleFunctions::const_volatile_rval_ref_mem_fn, "j");

// CHECK: void const_volatile_rval_ref_mem_fn_grad_1(double i, double j, volatile SimpleFunctions *_d_this, double *_d_j) const volatile && {
// CHECK-NEXT: double _d_i = 0;
// CHECK-NEXT: double _d_i = 0.;
// CHECK-NEXT: double _t0 = (this->x + this->y);
// CHECK-NEXT: {
// CHECK-NEXT: (*_d_this).x += 1 * i;
Expand All @@ -568,14 +568,14 @@ int main() {
printf("%.2f %.2f", result[0], result[1]); // CHECK-EXEC: 10.00 4.00

// CHECK: void fn3_grad_2_3(double x, double y, double i, double j, double *_d_i, double *_d_j) {
// CHECK-NEXT: double _d_x = 0;
// CHECK-NEXT: double _d_y = 0;
// CHECK-NEXT: double _d_x = 0.;
// CHECK-NEXT: double _d_y = 0.;
// CHECK-NEXT: SimpleFunctions _d_sf({});
// CHECK-NEXT: SimpleFunctions sf(x, y);
// CHECK-NEXT: SimpleFunctions _t0 = sf;
// CHECK-NEXT: {
// CHECK-NEXT: double _r0 = 0;
// CHECK-NEXT: double _r1 = 0;
// CHECK-NEXT: double _r0 = 0.;
// CHECK-NEXT: double _r1 = 0.;
// CHECK-NEXT: _t0.mem_fn_pullback(i, j, 1, &_d_sf, &_r0, &_r1);
// CHECK-NEXT: *_d_i += _r0;
// CHECK-NEXT: *_d_j += _r1;
Expand All @@ -591,13 +591,13 @@ int main() {
// CHECK-NEXT: {
// CHECK-NEXT: this->x = _t1;
// CHECK-NEXT: double _r_d1 = (*_d_this).x;
// CHECK-NEXT: (*_d_this).x = 0;
// CHECK-NEXT: (*_d_this).x = 0.;
// CHECK-NEXT: *_d_i += -_r_d1;
// CHECK-NEXT: }
// CHECK-NEXT: {
// CHECK-NEXT: this->x = _t0;
// CHECK-NEXT: double _r_d0 = (*_d_this).x;
// CHECK-NEXT: (*_d_this).x = 0;
// CHECK-NEXT: (*_d_this).x = 0.;
// CHECK-NEXT: *_d_i += _r_d0;
// CHECK-NEXT: }
// CHECK-NEXT: }
Expand Down
6 changes: 3 additions & 3 deletions test/Gradient/NonDifferentiable.C
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ int main() {
// CHECK-NEXT: SimpleFunctions1 obj(2, 3);
// CHECK-NEXT: SimpleFunctions1 _t0 = obj;
// CHECK-NEXT: {
// CHECK-NEXT: double _r0 = 0;
// CHECK-NEXT: double _r1 = 0;
// CHECK-NEXT: double _r0 = 0.;
// CHECK-NEXT: double _r1 = 0.;
// CHECK-NEXT: _t0.mem_fn_1_pullback(i, j, 1, &_d_obj, &_r0, &_r1);
// CHECK-NEXT: *_d_i += _r0;
// CHECK-NEXT: *_d_j += _r1;
Expand Down Expand Up @@ -169,7 +169,7 @@ int main() {
// CHECK-NEXT: }

// CHECK: void fn_non_diff_var_grad(double i, double j, double *_d_i, double *_d_j) {
// CHECK-NEXT: double _d_k = 0;
// CHECK-NEXT: double _d_k = 0.;
// CHECK-NEXT: double k = i * i * j;
// CHECK-NEXT: _d_k += 1;
// CHECK-NEXT: }
Expand Down
36 changes: 18 additions & 18 deletions test/Gradient/Pointers.C
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ double minimalPointer(double x) {
// CHECK-NEXT: {
// CHECK-NEXT: *p = _t0;
// CHECK-NEXT: double _r_d0 = *_d_p;
// CHECK-NEXT: *_d_p = 0;
// CHECK-NEXT: *_d_p = 0.;
// CHECK-NEXT: *_d_p += _r_d0 * (*p);
// CHECK-NEXT: *_d_p += *p * _r_d0;
// CHECK-NEXT: }
Expand Down Expand Up @@ -62,7 +62,7 @@ double arrayPointer(const double* arr) {
// CHECK-NEXT: double *_t1 = _d_p;
// CHECK-NEXT: _d_p = _d_p + 1;
// CHECK-NEXT: p = p + 1;
// CHECK-NEXT: double _d_sum = 0;
// CHECK-NEXT: double _d_sum = 0.;
// CHECK-NEXT: double sum = *p;
// CHECK-NEXT: _d_p++;
// CHECK-NEXT: p++;
Expand Down Expand Up @@ -147,17 +147,17 @@ double pointerParam(const double* arr, size_t n) {
}

// CHECK: void pointerParam_grad_0(const double *arr, size_t n, double *_d_arr) {
// CHECK-NEXT: size_t _d_n = 0;
// CHECK-NEXT: size_t _d_i = 0;
// CHECK-NEXT: size_t i = 0;
// CHECK-NEXT: size_t _d_n = 0UL;
// CHECK-NEXT: size_t _d_i = 0UL;
// CHECK-NEXT: size_t i = 0UL;
// CHECK-NEXT: clad::tape<size_t *> _t1 = {};
// CHECK-NEXT: clad::tape<size_t *> _t3 = {};
// CHECK-NEXT: size_t *_d_j = 0;
// CHECK-NEXT: size_t *j = 0;
// CHECK-NEXT: size_t *_d_j = {};
// CHECK-NEXT: size_t *j = {};
// CHECK-NEXT: clad::tape<double> _t4 = {};
// CHECK-NEXT: clad::tape<const double *> _t5 = {};
// CHECK-NEXT: clad::tape<double *> _t6 = {};
// CHECK-NEXT: double _d_sum = 0;
// CHECK-NEXT: double _d_sum = 0.;
// CHECK-NEXT: double sum = 0;
// CHECK-NEXT: unsigned {{int|long}} _t0 = {{0U|0UL}};
// CHECK-NEXT: for (i = 0; ; ++i) {
Expand Down Expand Up @@ -214,7 +214,7 @@ double pointerMultipleParams(const double* a, const double* b) {
}

// CHECK: void pointerMultipleParams_grad(const double *a, const double *b, double *_d_a, double *_d_b) {
// CHECK-NEXT: double _d_sum = 0;
// CHECK-NEXT: double _d_sum = 0.;
// CHECK-NEXT: double sum = b[2];
// CHECK-NEXT: const double *_t0 = b;
// CHECK-NEXT: double *_t1 = _d_b;
Expand Down Expand Up @@ -334,7 +334,7 @@ double newAndDeletePointer(double i, double j) {
// CHECK-NEXT: r[0] = i + j;
// CHECK-NEXT: double _t1 = r[1];
// CHECK-NEXT: r[1] = i * j;
// CHECK-NEXT: double _d_sum = 0;
// CHECK-NEXT: double _d_sum = 0.;
// CHECK-NEXT: double sum = *p + *q + r[0] + r[1];
// CHECK-NEXT: _d_sum += 1;
// CHECK-NEXT: {
Expand All @@ -346,14 +346,14 @@ double newAndDeletePointer(double i, double j) {
// CHECK-NEXT: {
// CHECK-NEXT: r[1] = _t1;
// CHECK-NEXT: double _r_d1 = _d_r[1];
// CHECK-NEXT: _d_r[1] = 0;
// CHECK-NEXT: _d_r[1] = 0.;
// CHECK-NEXT: *_d_i += _r_d1 * j;
// CHECK-NEXT: *_d_j += i * _r_d1;
// CHECK-NEXT: }
// CHECK-NEXT: {
// CHECK-NEXT: r[0] = _t0;
// CHECK-NEXT: double _r_d0 = _d_r[0];
// CHECK-NEXT: _d_r[0] = 0;
// CHECK-NEXT: _d_r[0] = 0.;
// CHECK-NEXT: *_d_i += _r_d0;
// CHECK-NEXT: *_d_j += _r_d0;
// CHECK-NEXT: }
Expand Down Expand Up @@ -382,7 +382,7 @@ double structPointer (double x) {
// CHECK: void structPointer_grad(double x, double *_d_x) {
// CHECK-NEXT: T *_d_t = new T();
// CHECK-NEXT: T *t = new T({x, /*implicit*/(int)0});
// CHECK-NEXT: double _d_res = 0;
// CHECK-NEXT: double _d_res = 0.;
// CHECK-NEXT: double res = t->x;
// CHECK-NEXT: _d_res += 1;
// CHECK-NEXT: _d_t->x += _d_res;
Expand All @@ -407,7 +407,7 @@ double cStyleMemoryAlloc(double x, size_t n) {
}

// CHECK: void cStyleMemoryAlloc_grad_0(double x, size_t n, double *_d_x) {
// CHECK-NEXT: size_t _d_n = 0;
// CHECK-NEXT: size_t _d_n = 0UL;
// CHECK-NEXT: T *_d_t = (T *)malloc(n * sizeof(T));
// CHECK-NEXT: T *t = (T *)malloc(n * sizeof(T));
// CHECK-NEXT: memset(_d_t, 0, n * sizeof(T));
Expand All @@ -418,7 +418,7 @@ double cStyleMemoryAlloc(double x, size_t n) {
// CHECK-NEXT: double *p = (double *)calloc(1, sizeof(double));
// CHECK-NEXT: double _t1 = *p;
// CHECK-NEXT: *p = x;
// CHECK-NEXT: double _d_res = 0;
// CHECK-NEXT: double _d_res = 0.;
// CHECK-NEXT: double res = t->x + *p;
// CHECK-NEXT: double *_t2 = p;
// CHECK-NEXT: double *_t3 = _d_p;
Expand All @@ -437,7 +437,7 @@ double cStyleMemoryAlloc(double x, size_t n) {
// CHECK-NEXT: {
// CHECK-NEXT: p[1] = _t4;
// CHECK-NEXT: double _r_d2 = _d_p[1];
// CHECK-NEXT: _d_p[1] = 0;
// CHECK-NEXT: _d_p[1] = 0.;
// CHECK-NEXT: *_d_x += 2 * _r_d2;
// CHECK-NEXT: }
// CHECK-NEXT: {
Expand All @@ -451,13 +451,13 @@ double cStyleMemoryAlloc(double x, size_t n) {
// CHECK-NEXT: {
// CHECK-NEXT: *p = _t1;
// CHECK-NEXT: double _r_d1 = *_d_p;
// CHECK-NEXT: *_d_p = 0;
// CHECK-NEXT: *_d_p = 0.;
// CHECK-NEXT: *_d_x += _r_d1;
// CHECK-NEXT: }
// CHECK-NEXT: {
// CHECK-NEXT: t->x = _t0;
// CHECK-NEXT: double _r_d0 = _d_t->x;
// CHECK-NEXT: _d_t->x = 0;
// CHECK-NEXT: _d_t->x = 0.;
// CHECK-NEXT: *_d_x += _r_d0;
// CHECK-NEXT: }
// CHECK-NEXT: free(p);
Expand Down
Loading

0 comments on commit 6130c91

Please sign in to comment.