From 91c0deccbc02ef1a9a6aa5adecae639d2a8ca38a Mon Sep 17 00:00:00 2001 From: Vaibhav Thakkar Date: Wed, 10 Apr 2024 12:53:59 +0200 Subject: [PATCH] Reorder filechecks in tests for pullbacks --- .../clad/Differentiator/DerivativeBuilder.h | 2 + lib/Differentiator/DiffPlanner.cpp | 6 +- lib/Differentiator/ReverseModeVisitor.cpp | 12 +- test/Arrays/ArrayInputsReverseMode.C | 162 +++--- test/Gradient/FunctionCalls.C | 460 ++++++++++-------- test/Gradient/Functors.C | 28 +- test/Gradient/Loops.C | 18 +- test/Gradient/MemberFunctions.C | 152 +++--- test/Gradient/UserDefinedTypes.C | 208 ++++---- test/Hessian/BuiltinDerivatives.C | 232 ++++----- test/Hessian/NestedFunctionCalls.C | 38 +- test/Jacobian/Jacobian.C | 18 +- test/Misc/RunDemos.C | 20 +- test/NestedCalls/NestedCalls.C | 54 +- 14 files changed, 757 insertions(+), 653 deletions(-) diff --git a/include/clad/Differentiator/DerivativeBuilder.h b/include/clad/Differentiator/DerivativeBuilder.h index b95312c93..d51280bb1 100644 --- a/include/clad/Differentiator/DerivativeBuilder.h +++ b/include/clad/Differentiator/DerivativeBuilder.h @@ -37,6 +37,8 @@ namespace clad { class CladPlugin; clang::FunctionDecl* ProcessDiffRequest(CladPlugin& P, DiffRequest& request); + // FIXME: This function should be removed and the entire plans array + // should be somehow made accessible to all the visitors. void AddRequestToSchedule(CladPlugin& P, const DiffRequest& request); } // namespace plugin diff --git a/lib/Differentiator/DiffPlanner.cpp b/lib/Differentiator/DiffPlanner.cpp index 7d80c3e39..e4d76d0d4 100644 --- a/lib/Differentiator/DiffPlanner.cpp +++ b/lib/Differentiator/DiffPlanner.cpp @@ -276,11 +276,13 @@ namespace clad { // Diff info for pullbacks is generated automatically, // its parameters are not provided by the user. if (Mode == DiffMode::experimental_pullback) { + // Might need to update DVI args, as they may be pointing to the + // declaration parameters, not the definition parameters. if (!Function->getPreviousDecl()) + // If the function was never declared before, we can safely assume + // that the parameters are correctly referring to the definition ones. return; const FunctionDecl* FD = Function->getPreviousDecl(); - // Might need to update DVI args, as they may be pointing to the - // declaration parameters, not the definition parameters. for (size_t i = 0, e = DVI.size(), paramIdx = 0; i < e && paramIdx < FD->getNumParams(); ++i) { const auto* param = DVI[i].param; diff --git a/lib/Differentiator/ReverseModeVisitor.cpp b/lib/Differentiator/ReverseModeVisitor.cpp index c9f8e97f3..b461604a0 100644 --- a/lib/Differentiator/ReverseModeVisitor.cpp +++ b/lib/Differentiator/ReverseModeVisitor.cpp @@ -1804,17 +1804,21 @@ Expr* getArraySizeExpr(const ArrayType* AT, ASTContext& context, if (!m_ExternalSource) { // Derive the declaration of the pullback function. pullbackRequest.DeclarationOnly = true; - pullbackFD = plugin::ProcessDiffRequest(m_CladPlugin, pullbackRequest); + pullbackFD = + plugin::ProcessDiffRequest(m_CladPlugin, pullbackRequest); // Add the request to derive the definition of the pullback function. pullbackRequest.DeclarationOnly = false; pullbackRequest.DerivedFDPrototype = pullbackFD; plugin::AddRequestToSchedule(m_CladPlugin, pullbackRequest); } else { - // FIXME: Error estimation currently uses singleton objects - m_ErrorEstHandler and m_EstModel, which is cleared after each error_estimate request. This requires the pullback to be derived at the same time to access the singleton objects. - pullbackFD = plugin::ProcessDiffRequest(m_CladPlugin, pullbackRequest); + // FIXME: Error estimation currently uses singleton objects - + // m_ErrorEstHandler and m_EstModel, which is cleared after each + // error_estimate request. This requires the pullback to be derived at + // the same time to access the singleton objects. + pullbackFD = + plugin::ProcessDiffRequest(m_CladPlugin, pullbackRequest); } - // Clad failed to derive it. // FIXME: Add support for reference arguments to the numerical diff. If diff --git a/test/Arrays/ArrayInputsReverseMode.C b/test/Arrays/ArrayInputsReverseMode.C index 618dc8874..36413f8e4 100644 --- a/test/Arrays/ArrayInputsReverseMode.C +++ b/test/Arrays/ArrayInputsReverseMode.C @@ -16,31 +16,7 @@ double addArr(const double *arr, int n) { return ret; } -//CHECK: void addArr_pullback(const double *arr, int n, double _d_y, double *_d_arr, int *_d_n) { -//CHECK-NEXT: double _d_ret = 0; -//CHECK-NEXT: unsigned {{int|long}} _t0; -//CHECK-NEXT: int _d_i = 0; -//CHECK-NEXT: int i = 0; -//CHECK-NEXT: clad::tape _t1 = {}; -//CHECK-NEXT: double ret = 0; -//CHECK-NEXT: _t0 = 0; -//CHECK-NEXT: for (i = 0; i < n; i++) { -//CHECK-NEXT: _t0++; -//CHECK-NEXT: clad::push(_t1, ret); -//CHECK-NEXT: ret += arr[i]; -//CHECK-NEXT: } -//CHECK-NEXT: goto _label0; -//CHECK-NEXT: _label0: -//CHECK-NEXT: _d_ret += _d_y; -//CHECK-NEXT: for (; _t0; _t0--) { -//CHECK-NEXT: i--; -//CHECK-NEXT: { -//CHECK-NEXT: ret = clad::pop(_t1); -//CHECK-NEXT: double _r_d0 = _d_ret; -//CHECK-NEXT: _d_arr[i] += _r_d0; -//CHECK-NEXT: } -//CHECK-NEXT: } -//CHECK-NEXT: } +//CHECK: void addArr_pullback(const double *arr, int n, double _d_y, double *_d_arr, int *_d_n); double f(double *arr) { return addArr(arr, 3); @@ -104,11 +80,7 @@ float helper(float x) { return 2 * x; } -// CHECK: void helper_pullback(float x, float _d_y, float *_d_x) { -// CHECK-NEXT: goto _label0; -// CHECK-NEXT: _label0: -// CHECK-NEXT: *_d_x += 2 * _d_y; -// CHECK-NEXT: } +// CHECK: void helper_pullback(float x, float _d_y, float *_d_x); float func2(float* a) { float sum = 0; @@ -345,17 +317,7 @@ double inv_square(double *params) { return 1 / (params[0] * params[0]); } -//CHECK: void inv_square_pullback(double *params, double _d_y, double *_d_params) { -//CHECK-NEXT: double _t0; -//CHECK-NEXT: _t0 = (params[0] * params[0]); -//CHECK-NEXT: goto _label0; -//CHECK-NEXT: _label0: -//CHECK-NEXT: { -//CHECK-NEXT: double _r0 = _d_y * -1 / (_t0 * _t0); -//CHECK-NEXT: _d_params[0] += _r0 * params[0]; -//CHECK-NEXT: _d_params[0] += params[0] * _r0; -//CHECK-NEXT: } -//CHECK-NEXT: } +//CHECK: void inv_square_pullback(double *params, double _d_y, double *_d_params); double func7(double *params) { double out = 0.0; @@ -407,14 +369,7 @@ double helper2(double i, double *arr, int n) { return arr[0]*i; } -//CHECK: void helper2_pullback(double i, double *arr, int n, double _d_y, double *_d_i, double *_d_arr, int *_d_n) { -//CHECK-NEXT: goto _label0; -//CHECK-NEXT: _label0: -//CHECK-NEXT: { -//CHECK-NEXT: _d_arr[0] += _d_y * i; -//CHECK-NEXT: *_d_i += arr[0] * _d_y; -//CHECK-NEXT: } -//CHECK-NEXT: } +//CHECK: void helper2_pullback(double i, double *arr, int n, double _d_y, double *_d_i, double *_d_arr, int *_d_n); double func8(double i, double *arr, int n) { double res = 0; @@ -465,17 +420,7 @@ void modify(double& elem, double val) { elem = val; } -//CHECK: void modify_pullback(double &elem, double val, double *_d_elem, double *_d_val) { -//CHECK-NEXT: double _t0; -//CHECK-NEXT: _t0 = elem; -//CHECK-NEXT: elem = val; -//CHECK-NEXT: { -//CHECK-NEXT: elem = _t0; -//CHECK-NEXT: double _r_d0 = *_d_elem; -//CHECK-NEXT: *_d_elem -= _r_d0; -//CHECK-NEXT: *_d_val += _r_d0; -//CHECK-NEXT: } -//CHECK-NEXT: } +//CHECK: void modify_pullback(double &elem, double val, double *_d_elem, double *_d_val); double func9(double i, double j) { double arr[5] = {}; @@ -525,21 +470,7 @@ double sq(double& elem) { return elem; } -//CHECK: void sq_pullback(double &elem, double _d_y, double *_d_elem) { -//CHECK-NEXT: double _t0; -//CHECK-NEXT: _t0 = elem; -//CHECK-NEXT: elem = elem * elem; -//CHECK-NEXT: goto _label0; -//CHECK-NEXT: _label0: -//CHECK-NEXT: *_d_elem += _d_y; -//CHECK-NEXT: { -//CHECK-NEXT: elem = _t0; -//CHECK-NEXT: double _r_d0 = *_d_elem; -//CHECK-NEXT: *_d_elem -= _r_d0; -//CHECK-NEXT: *_d_elem += _r_d0 * elem; -//CHECK-NEXT: *_d_elem += elem * _r_d0; -//CHECK-NEXT: } -//CHECK-NEXT: } +//CHECK: void sq_pullback(double &elem, double _d_y, double *_d_elem); double func10(double *arr, int n) { double res = 0; @@ -645,3 +576,84 @@ int main() { func10grad.execute(arr3, 5, _d_arr3); printf("Result (arr) = {%.2f, %.2f, %.2f, %.2f, %.2f}\n", _d_arr3[0], _d_arr3[1], _d_arr3[2], _d_arr3[3], _d_arr3[4]); // CHECK-EXEC: Result (arr) = {2.00, 4.00, 6.00, 8.00, 10.00} } + +//CHECK: void addArr_pullback(const double *arr, int n, double _d_y, double *_d_arr, int *_d_n) { +//CHECK-NEXT: double _d_ret = 0; +//CHECK-NEXT: unsigned {{int|long}} _t0; +//CHECK-NEXT: int _d_i = 0; +//CHECK-NEXT: int i = 0; +//CHECK-NEXT: clad::tape _t1 = {}; +//CHECK-NEXT: double ret = 0; +//CHECK-NEXT: _t0 = 0; +//CHECK-NEXT: for (i = 0; i < n; i++) { +//CHECK-NEXT: _t0++; +//CHECK-NEXT: clad::push(_t1, ret); +//CHECK-NEXT: ret += arr[i]; +//CHECK-NEXT: } +//CHECK-NEXT: goto _label0; +//CHECK-NEXT: _label0: +//CHECK-NEXT: _d_ret += _d_y; +//CHECK-NEXT: for (; _t0; _t0--) { +//CHECK-NEXT: i--; +//CHECK-NEXT: { +//CHECK-NEXT: ret = clad::pop(_t1); +//CHECK-NEXT: double _r_d0 = _d_ret; +//CHECK-NEXT: _d_arr[i] += _r_d0; +//CHECK-NEXT: } +//CHECK-NEXT: } +//CHECK-NEXT: } + +// CHECK: void helper_pullback(float x, float _d_y, float *_d_x) { +// CHECK-NEXT: goto _label0; +// CHECK-NEXT: _label0: +// CHECK-NEXT: *_d_x += 2 * _d_y; +// CHECK-NEXT: } + +//CHECK: void inv_square_pullback(double *params, double _d_y, double *_d_params) { +//CHECK-NEXT: double _t0; +//CHECK-NEXT: _t0 = (params[0] * params[0]); +//CHECK-NEXT: goto _label0; +//CHECK-NEXT: _label0: +//CHECK-NEXT: { +//CHECK-NEXT: double _r0 = _d_y * -1 / (_t0 * _t0); +//CHECK-NEXT: _d_params[0] += _r0 * params[0]; +//CHECK-NEXT: _d_params[0] += params[0] * _r0; +//CHECK-NEXT: } +//CHECK-NEXT: } + +//CHECK: void helper2_pullback(double i, double *arr, int n, double _d_y, double *_d_i, double *_d_arr, int *_d_n) { +//CHECK-NEXT: goto _label0; +//CHECK-NEXT: _label0: +//CHECK-NEXT: { +//CHECK-NEXT: _d_arr[0] += _d_y * i; +//CHECK-NEXT: *_d_i += arr[0] * _d_y; +//CHECK-NEXT: } +//CHECK-NEXT: } + +//CHECK: void modify_pullback(double &elem, double val, double *_d_elem, double *_d_val) { +//CHECK-NEXT: double _t0; +//CHECK-NEXT: _t0 = elem; +//CHECK-NEXT: elem = val; +//CHECK-NEXT: { +//CHECK-NEXT: elem = _t0; +//CHECK-NEXT: double _r_d0 = *_d_elem; +//CHECK-NEXT: *_d_elem -= _r_d0; +//CHECK-NEXT: *_d_val += _r_d0; +//CHECK-NEXT: } +//CHECK-NEXT: } + +//CHECK: void sq_pullback(double &elem, double _d_y, double *_d_elem) { +//CHECK-NEXT: double _t0; +//CHECK-NEXT: _t0 = elem; +//CHECK-NEXT: elem = elem * elem; +//CHECK-NEXT: goto _label0; +//CHECK-NEXT: _label0: +//CHECK-NEXT: *_d_elem += _d_y; +//CHECK-NEXT: { +//CHECK-NEXT: elem = _t0; +//CHECK-NEXT: double _r_d0 = *_d_elem; +//CHECK-NEXT: *_d_elem -= _r_d0; +//CHECK-NEXT: *_d_elem += _r_d0 * elem; +//CHECK-NEXT: *_d_elem += elem * _r_d0; +//CHECK-NEXT: } +//CHECK-NEXT: } \ No newline at end of file diff --git a/test/Gradient/FunctionCalls.C b/test/Gradient/FunctionCalls.C index 93a7cb89a..6ffe45adf 100644 --- a/test/Gradient/FunctionCalls.C +++ b/test/Gradient/FunctionCalls.C @@ -9,11 +9,7 @@ namespace A { template T constantFn(T i) { return 3; } - // CHECK: void constantFn_pullback(float i, float _d_y, float *_d_i) { - // CHECK-NEXT: goto _label0; - // CHECK-NEXT: _label0: - // CHECK-NEXT: ; - // CHECK-NEXT: } + // CHECK: void constantFn_pullback(float i, float _d_y, float *_d_i); } // namespace A double constantFn(double i) { @@ -52,33 +48,7 @@ double modify1(double& i, double& j) { return res; } -// CHECK: void modify1_pullback(double &i, double &j, double _d_y, double *_d_i, double *_d_j) { -// CHECK-NEXT: double _t0; -// CHECK-NEXT: double _t1; -// CHECK-NEXT: double _d_res = 0; -// CHECK-NEXT: _t0 = i; -// CHECK-NEXT: i += j; -// CHECK-NEXT: _t1 = j; -// CHECK-NEXT: j += j; -// CHECK-NEXT: double res = i + j; -// CHECK-NEXT: goto _label0; -// CHECK-NEXT: _label0: -// CHECK-NEXT: _d_res += _d_y; -// CHECK-NEXT: { -// CHECK-NEXT: *_d_i += _d_res; -// CHECK-NEXT: *_d_j += _d_res; -// CHECK-NEXT: } -// CHECK-NEXT: { -// CHECK-NEXT: j = _t1; -// CHECK-NEXT: double _r_d1 = *_d_j; -// CHECK-NEXT: *_d_j += _r_d1; -// CHECK-NEXT: } -// CHECK-NEXT: { -// CHECK-NEXT: i = _t0; -// CHECK-NEXT: double _r_d0 = *_d_i; -// CHECK-NEXT: *_d_j += _r_d0; -// CHECK-NEXT: } -// CHECK-NEXT: } +// CHECK: void modify1_pullback(double &i, double &j, double _d_y, double *_d_i, double *_d_j); double fn2(double i, double j) { double temp = 0; @@ -130,24 +100,7 @@ void update1(double& i, double& j) { j += j; } -// CHECK: void update1_pullback(double &i, double &j, double *_d_i, double *_d_j) { -// CHECK-NEXT: double _t0; -// CHECK-NEXT: double _t1; -// CHECK-NEXT: _t0 = i; -// CHECK-NEXT: i += j; -// CHECK-NEXT: _t1 = j; -// CHECK-NEXT: j += j; -// CHECK-NEXT: { -// CHECK-NEXT: j = _t1; -// CHECK-NEXT: double _r_d1 = *_d_j; -// CHECK-NEXT: *_d_j += _r_d1; -// CHECK-NEXT: } -// CHECK-NEXT: { -// CHECK-NEXT: i = _t0; -// CHECK-NEXT: double _r_d0 = *_d_i; -// CHECK-NEXT: *_d_j += _r_d0; -// CHECK-NEXT: } -// CHECK-NEXT: } +// CHECK: void update1_pullback(double &i, double &j, double *_d_i, double *_d_j); double fn3(double i, double j) { update1(i, j); @@ -189,53 +142,13 @@ float sum(double* arr, int n) { return res; } -// CHECK: void sum_pullback(double *arr, int n, float _d_y, double *_d_arr, int *_d_n) { -// CHECK-NEXT: float _d_res = 0; -// CHECK-NEXT: unsigned {{int|long}} _t0; -// CHECK-NEXT: int _d_i = 0; -// CHECK-NEXT: int i = 0; -// CHECK-NEXT: clad::tape _t1 = {}; -// CHECK-NEXT: double _t2; -// CHECK-NEXT: float res = 0; -// CHECK-NEXT: _t0 = 0; -// CHECK-NEXT: for (i = 0; i < n; ++i) { -// CHECK-NEXT: _t0++; -// CHECK-NEXT: clad::push(_t1, res); -// CHECK-NEXT: res += arr[i]; -// CHECK-NEXT: } -// CHECK-NEXT: _t2 = arr[0]; -// CHECK-NEXT: arr[0] += 10 * arr[0]; -// CHECK-NEXT: goto _label0; -// CHECK-NEXT: _label0: -// CHECK-NEXT: _d_res += _d_y; -// CHECK-NEXT: { -// CHECK-NEXT: arr[0] = _t2; -// CHECK-NEXT: double _r_d1 = _d_arr[0]; -// CHECK-NEXT: _d_arr[0] += 10 * _r_d1; -// CHECK-NEXT: } -// CHECK-NEXT: for (; _t0; _t0--) { -// CHECK-NEXT: --i; -// CHECK-NEXT: res = clad::pop(_t1); -// CHECK-NEXT: float _r_d0 = _d_res; -// CHECK-NEXT: _d_arr[i] += _r_d0; -// CHECK-NEXT: } -// CHECK-NEXT: } +// CHECK: void sum_pullback(double *arr, int n, float _d_y, double *_d_arr, int *_d_n); void twice(double& d) { d = 2*d; } -// CHECK: void twice_pullback(double &d, double *_d_d) { -// CHECK-NEXT: double _t0; -// CHECK-NEXT: _t0 = d; -// CHECK-NEXT: d = 2 * d; -// CHECK-NEXT: { -// CHECK-NEXT: d = _t0; -// CHECK-NEXT: double _r_d0 = *_d_d; -// CHECK-NEXT: *_d_d -= _r_d0; -// CHECK-NEXT: *_d_d += 2 * _r_d0; -// CHECK-NEXT: } -// CHECK-NEXT: } +// CHECK: void twice_pullback(double &d, double *_d_d); double fn4(double* arr, int n) { double res = 0; @@ -296,21 +209,7 @@ double modify2(double* arr) { return 1; } -// CHECK: void modify2_pullback(double *arr, double _d_y, double *_d_arr) { -// CHECK-NEXT: double _t0; -// CHECK-NEXT: _t0 = arr[0]; -// CHECK-NEXT: arr[0] = 5 * arr[0] + arr[1]; -// CHECK-NEXT: goto _label0; -// CHECK-NEXT: _label0: -// CHECK-NEXT: ; -// CHECK-NEXT: { -// CHECK-NEXT: arr[0] = _t0; -// CHECK-NEXT: double _r_d0 = _d_arr[0]; -// CHECK-NEXT: _d_arr[0] -= _r_d0; -// CHECK-NEXT: _d_arr[0] += 5 * _r_d0; -// CHECK-NEXT: _d_arr[1] += _r_d0; -// CHECK-NEXT: } -// CHECK-NEXT: } +// CHECK: void modify2_pullback(double *arr, double _d_y, double *_d_arr); double fn5(double* arr, int n) { double temp = modify2(arr); @@ -358,32 +257,9 @@ double fn7(double i, double j) { // CHECK-NEXT: } // CHECK-NEXT: } -// CHECK: void identity_pullback(double &i, double _d_y, double *_d_i) { -// CHECK-NEXT: double _d__d_i = 0; -// CHECK-NEXT: double _t0; -// CHECK-NEXT: MyStruct::myFunction(); -// CHECK-NEXT: double _d_i0 = i; -// CHECK-NEXT: _t0 = _d_i0; -// CHECK-NEXT: _d_i0 += 1; -// CHECK-NEXT: goto _label0; -// CHECK-NEXT: _label0: -// CHECK-NEXT: *_d_i += _d_y; -// CHECK-NEXT: { -// CHECK-NEXT: _d_i0 = _t0; -// CHECK-NEXT: double _r_d0 = _d__d_i; -// CHECK-NEXT: } -// CHECK-NEXT: *_d_i += _d__d_i; -// CHECK-NEXT: } +// CHECK: void identity_pullback(double &i, double _d_y, double *_d_i); -// CHECK: clad::ValueAndAdjoint identity_forw(double &i, double *_d_i) { -// CHECK-NEXT: double _d__d_i = 0; -// CHECK-NEXT: double _t0; -// CHECK-NEXT: MyStruct::myFunction(); -// CHECK-NEXT: double _d_i0 = i; -// CHECK-NEXT: _t0 = _d_i0; -// CHECK-NEXT: _d_i0 += 1; -// CHECK-NEXT: return {i, *_d_i}; -// CHECK-NEXT: } +// CHECK: clad::ValueAndAdjoint identity_forw(double &i, double *_d_i); // CHECK: void fn7_grad(double i, double j, double *_d_i, double *_d_j) { // CHECK-NEXT: double _t0; @@ -435,18 +311,8 @@ double check_and_return(double x, char c, const char* s) { return x; return 1; } -// CHECK: void check_and_return_pullback(double x, char c, const char *s, double _d_y, double *_d_x, char *_d_c, char *_d_s) { -// CHECK-NEXT: bool _cond0; -// CHECK-NEXT: _cond0 = c == 'a' && s[0] == 'a'; -// CHECK-NEXT: if (_cond0) -// CHECK-NEXT: goto _label0; -// CHECK-NEXT: goto _label1; -// CHECK-NEXT: _label1: -// CHECK-NEXT: ; -// CHECK-NEXT: if (_cond0) -// CHECK-NEXT: _label0: -// CHECK-NEXT: *_d_x += _d_y; -// CHECK-NEXT: } + +// CHECK: void check_and_return_pullback(double x, char c, const char *s, double _d_y, double *_d_x, char *_d_c, char *_d_s); double fn8(double x, double y) { return check_and_return(x, 'a', "aa") * y * std::tanh(1.0) * std::max(1.0, 2.0); // expected-warning {{ISO C++11 does not allow conversion from string literal to 'char *' [-Wwritable-strings]}} @@ -474,16 +340,7 @@ double custom_max(const double& a, const double& b) { return a > b ? a : b; } -// CHECK: void custom_max_pullback(const double &a, const double &b, double _d_y, double *_d_a, double *_d_b) { -// CHECK-NEXT: bool _cond0; -// CHECK-NEXT: _cond0 = a > b; -// CHECK-NEXT: goto _label0; -// CHECK-NEXT: _label0: -// CHECK-NEXT: if (_cond0) -// CHECK-NEXT: *_d_a += _d_y; -// CHECK-NEXT: else -// CHECK-NEXT: *_d_b += _d_y; -// CHECK-NEXT: } +// CHECK: void custom_max_pullback(const double &a, const double &b, double _d_y, double *_d_a, double *_d_b); double fn9(double x, double y) { return custom_max(x*y, y); @@ -606,11 +463,7 @@ double do_nothing(double* u, double* v, double* w) { return u[0]; } -// CHECK: void do_nothing_pullback(double *u, double *v, double *w, double _d_y, double *_d_u, double *_d_v, double *_d_w) { -// CHECK-NEXT: goto _label0; -// CHECK-NEXT: _label0: -// CHECK-NEXT: _d_u[0] += _d_y; -// CHECK-NEXT: } +// CHECK: void do_nothing_pullback(double *u, double *v, double *w, double _d_y, double *_d_u, double *_d_v, double *_d_w); double fn12(double x, double y) { return do_nothing(&x, nullptr, 0); @@ -626,14 +479,7 @@ double multiply(double* a, double* b) { return a[0] * b[0]; } -// CHECK: void multiply_pullback(double *a, double *b, double _d_y, double *_d_a, double *_d_b) { -// CHECK-NEXT: goto _label0; -// CHECK-NEXT: _label0: -// CHECK-NEXT: { -// CHECK-NEXT: _d_a[0] += _d_y * b[0]; -// CHECK-NEXT: _d_b[0] += a[0] * _d_y; -// CHECK-NEXT: } -// CHECK-NEXT: } +// CHECK: void multiply_pullback(double *a, double *b, double _d_y, double *_d_a, double *_d_b); double fn13(double* x, const double* w) { double wCopy[2]; @@ -669,8 +515,7 @@ double fn13(double* x, const double* w) { void emptyFn(double &x, double y) {} -// CHECK: void emptyFn_pullback(double &x, double y, double *_d_x, double *_d_y) { -// CHECK-NEXT: } +// CHECK: void emptyFn_pullback(double &x, double y, double *_d_x, double *_d_y); double fn14(double x, double y) { emptyFn(x, y); @@ -720,27 +565,7 @@ double recFun (double x, double y) { return x * y; } -//CHECK: void recFun_pullback(double x, double y, double _d_y0, double *_d_x, double *_d_y) { -//CHECK-NEXT: bool _cond0; -//CHECK-NEXT: _cond0 = x > y; -//CHECK-NEXT: if (_cond0) -//CHECK-NEXT: goto _label0; -//CHECK-NEXT: goto _label1; -//CHECK-NEXT: _label1: -//CHECK-NEXT: { -//CHECK-NEXT: *_d_x += _d_y0 * y; -//CHECK-NEXT: *_d_y += x * _d_y0; -//CHECK-NEXT: } -//CHECK-NEXT: if (_cond0) -//CHECK-NEXT: _label0: -//CHECK-NEXT: { -//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; -//CHECK-NEXT: } -//CHECK-NEXT: } +//CHECK: void recFun_pullback(double x, double y, double _d_y0, double *_d_x, double *_d_y); double fn16(double x, double y) { return recFun(x, y); @@ -762,20 +587,10 @@ double add(double a, double* b) { return a + b[0]; } -//CHECK: void add_pullback(double a, double *b, double _d_y, double *_d_a) { -//CHECK-NEXT: goto _label0; -//CHECK-NEXT: _label0: -//CHECK-NEXT: *_d_a += _d_y; -//CHECK-NEXT: } -//CHECK: void add_pullback(double a, double *b, double _d_y, double *_d_a, double *_d_b) { -//CHECK-NEXT: goto _label0; -//CHECK-NEXT: _label0: -//CHECK-NEXT: { -//CHECK-NEXT: *_d_a += _d_y; -//CHECK-NEXT: _d_b[0] += _d_y; -//CHECK-NEXT: } -//CHECK-NEXT: } +//CHECK: void add_pullback(double a, double *b, double _d_y, double *_d_a); + +//CHECK: void add_pullback(double a, double *b, double _d_y, double *_d_a, double *_d_b); double fn17 (double x, double* y) { x = add(x, y); @@ -813,14 +628,7 @@ double fn17 (double x, double* y) { double sq_defined_later(double x); -// CHECK: void sq_defined_later_pullback(double x, double _d_y, double *_d_x) { -// CHECK-NEXT: goto _label0; -// CHECK-NEXT: _label0: -// CHECK-NEXT: { -// CHECK-NEXT: *_d_x += _d_y * x; -// CHECK-NEXT: *_d_x += x * _d_y; -// CHECK-NEXT: } -// CHECK-NEXT: } +// CHECK: void sq_defined_later_pullback(double x, double _d_y, double *_d_x); double fn18(double x, double y) { return sq_defined_later(x) + sq_defined_later(y); @@ -937,4 +745,232 @@ int main() { double sq_defined_later(double x) { return x*x; -} \ No newline at end of file +} + +// CHECK: void constantFn_pullback(float i, float _d_y, float *_d_i) { +// CHECK-NEXT: goto _label0; +// CHECK-NEXT: _label0: +// CHECK-NEXT: ; +// CHECK-NEXT: } + +// CHECK: void modify1_pullback(double &i, double &j, double _d_y, double *_d_i, double *_d_j) { +// CHECK-NEXT: double _t0; +// CHECK-NEXT: double _t1; +// CHECK-NEXT: double _d_res = 0; +// CHECK-NEXT: _t0 = i; +// CHECK-NEXT: i += j; +// CHECK-NEXT: _t1 = j; +// CHECK-NEXT: j += j; +// CHECK-NEXT: double res = i + j; +// CHECK-NEXT: goto _label0; +// CHECK-NEXT: _label0: +// CHECK-NEXT: _d_res += _d_y; +// CHECK-NEXT: { +// CHECK-NEXT: *_d_i += _d_res; +// CHECK-NEXT: *_d_j += _d_res; +// CHECK-NEXT: } +// CHECK-NEXT: { +// CHECK-NEXT: j = _t1; +// CHECK-NEXT: double _r_d1 = *_d_j; +// CHECK-NEXT: *_d_j += _r_d1; +// CHECK-NEXT: } +// CHECK-NEXT: { +// CHECK-NEXT: i = _t0; +// CHECK-NEXT: double _r_d0 = *_d_i; +// CHECK-NEXT: *_d_j += _r_d0; +// CHECK-NEXT: } +// CHECK-NEXT: } + +// CHECK: void update1_pullback(double &i, double &j, double *_d_i, double *_d_j) { +// CHECK-NEXT: double _t0; +// CHECK-NEXT: double _t1; +// CHECK-NEXT: _t0 = i; +// CHECK-NEXT: i += j; +// CHECK-NEXT: _t1 = j; +// CHECK-NEXT: j += j; +// CHECK-NEXT: { +// CHECK-NEXT: j = _t1; +// CHECK-NEXT: double _r_d1 = *_d_j; +// CHECK-NEXT: *_d_j += _r_d1; +// CHECK-NEXT: } +// CHECK-NEXT: { +// CHECK-NEXT: i = _t0; +// CHECK-NEXT: double _r_d0 = *_d_i; +// CHECK-NEXT: *_d_j += _r_d0; +// CHECK-NEXT: } +// CHECK-NEXT: } + +// CHECK: void sum_pullback(double *arr, int n, float _d_y, double *_d_arr, int *_d_n) { +// CHECK-NEXT: float _d_res = 0; +// CHECK-NEXT: unsigned {{int|long}} _t0; +// CHECK-NEXT: int _d_i = 0; +// CHECK-NEXT: int i = 0; +// CHECK-NEXT: clad::tape _t1 = {}; +// CHECK-NEXT: double _t2; +// CHECK-NEXT: float res = 0; +// CHECK-NEXT: _t0 = 0; +// CHECK-NEXT: for (i = 0; i < n; ++i) { +// CHECK-NEXT: _t0++; +// CHECK-NEXT: clad::push(_t1, res); +// CHECK-NEXT: res += arr[i]; +// CHECK-NEXT: } +// CHECK-NEXT: _t2 = arr[0]; +// CHECK-NEXT: arr[0] += 10 * arr[0]; +// CHECK-NEXT: goto _label0; +// CHECK-NEXT: _label0: +// CHECK-NEXT: _d_res += _d_y; +// CHECK-NEXT: { +// CHECK-NEXT: arr[0] = _t2; +// CHECK-NEXT: double _r_d1 = _d_arr[0]; +// CHECK-NEXT: _d_arr[0] += 10 * _r_d1; +// CHECK-NEXT: } +// CHECK-NEXT: for (; _t0; _t0--) { +// CHECK-NEXT: --i; +// CHECK-NEXT: res = clad::pop(_t1); +// CHECK-NEXT: float _r_d0 = _d_res; +// CHECK-NEXT: _d_arr[i] += _r_d0; +// CHECK-NEXT: } +// CHECK-NEXT: } + +// CHECK: void twice_pullback(double &d, double *_d_d) { +// CHECK-NEXT: double _t0; +// CHECK-NEXT: _t0 = d; +// CHECK-NEXT: d = 2 * d; +// CHECK-NEXT: { +// CHECK-NEXT: d = _t0; +// CHECK-NEXT: double _r_d0 = *_d_d; +// CHECK-NEXT: *_d_d -= _r_d0; +// CHECK-NEXT: *_d_d += 2 * _r_d0; +// CHECK-NEXT: } +// CHECK-NEXT: } + +// CHECK: void modify2_pullback(double *arr, double _d_y, double *_d_arr) { +// CHECK-NEXT: double _t0; +// CHECK-NEXT: _t0 = arr[0]; +// CHECK-NEXT: arr[0] = 5 * arr[0] + arr[1]; +// CHECK-NEXT: goto _label0; +// CHECK-NEXT: _label0: +// CHECK-NEXT: ; +// CHECK-NEXT: { +// CHECK-NEXT: arr[0] = _t0; +// CHECK-NEXT: double _r_d0 = _d_arr[0]; +// CHECK-NEXT: _d_arr[0] -= _r_d0; +// 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: double _d__d_i = 0; +// CHECK-NEXT: double _t0; +// CHECK-NEXT: MyStruct::myFunction(); +// CHECK-NEXT: double _d_i0 = i; +// CHECK-NEXT: _t0 = _d_i0; +// CHECK-NEXT: _d_i0 += 1; +// CHECK-NEXT: goto _label0; +// CHECK-NEXT: _label0: +// CHECK-NEXT: *_d_i += _d_y; +// CHECK-NEXT: { +// CHECK-NEXT: _d_i0 = _t0; +// CHECK-NEXT: double _r_d0 = _d__d_i; +// CHECK-NEXT: } +// CHECK-NEXT: *_d_i += _d__d_i; +// CHECK-NEXT: } + +// CHECK: clad::ValueAndAdjoint identity_forw(double &i, double *_d_i) { +// CHECK-NEXT: double _d__d_i = 0; +// CHECK-NEXT: double _t0; +// CHECK-NEXT: MyStruct::myFunction(); +// CHECK-NEXT: double _d_i0 = i; +// CHECK-NEXT: _t0 = _d_i0; +// CHECK-NEXT: _d_i0 += 1; +// CHECK-NEXT: return {i, *_d_i}; +// CHECK-NEXT: } + +// CHECK: void check_and_return_pullback(double x, char c, const char *s, double _d_y, double *_d_x, char *_d_c, char *_d_s) { +// CHECK-NEXT: bool _cond0; +// CHECK-NEXT: _cond0 = c == 'a' && s[0] == 'a'; +// CHECK-NEXT: if (_cond0) +// CHECK-NEXT: goto _label0; +// CHECK-NEXT: goto _label1; +// CHECK-NEXT: _label1: +// CHECK-NEXT: ; +// CHECK-NEXT: if (_cond0) +// CHECK-NEXT: _label0: +// CHECK-NEXT: *_d_x += _d_y; +// CHECK-NEXT: } + +// CHECK: void custom_max_pullback(const double &a, const double &b, double _d_y, double *_d_a, double *_d_b) { +// CHECK-NEXT: bool _cond0; +// CHECK-NEXT: _cond0 = a > b; +// CHECK-NEXT: goto _label0; +// CHECK-NEXT: _label0: +// CHECK-NEXT: if (_cond0) +// CHECK-NEXT: *_d_a += _d_y; +// CHECK-NEXT: else +// CHECK-NEXT: *_d_b += _d_y; +// CHECK-NEXT: } + +// CHECK: void do_nothing_pullback(double *u, double *v, double *w, double _d_y, double *_d_u, double *_d_v, double *_d_w) { +// CHECK-NEXT: goto _label0; +// CHECK-NEXT: _label0: +// CHECK-NEXT: _d_u[0] += _d_y; +// CHECK-NEXT: } + +// CHECK: void multiply_pullback(double *a, double *b, double _d_y, double *_d_a, double *_d_b) { +// CHECK-NEXT: goto _label0; +// CHECK-NEXT: _label0: +// CHECK-NEXT: { +// CHECK-NEXT: _d_a[0] += _d_y * b[0]; +// CHECK-NEXT: _d_b[0] += a[0] * _d_y; +// CHECK-NEXT: } +// CHECK-NEXT: } + +// CHECK: void emptyFn_pullback(double &x, double y, double *_d_x, double *_d_y) { +// CHECK-NEXT: } + +//CHECK: void recFun_pullback(double x, double y, double _d_y0, double *_d_x, double *_d_y) { +//CHECK-NEXT: bool _cond0; +//CHECK-NEXT: _cond0 = x > y; +//CHECK-NEXT: if (_cond0) +//CHECK-NEXT: goto _label0; +//CHECK-NEXT: goto _label1; +//CHECK-NEXT: _label1: +//CHECK-NEXT: { +//CHECK-NEXT: *_d_x += _d_y0 * y; +//CHECK-NEXT: *_d_y += x * _d_y0; +//CHECK-NEXT: } +//CHECK-NEXT: if (_cond0) +//CHECK-NEXT: _label0: +//CHECK-NEXT: { +//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; +//CHECK-NEXT: } +//CHECK-NEXT: } + +//CHECK: void add_pullback(double a, double *b, double _d_y, double *_d_a) { +//CHECK-NEXT: goto _label0; +//CHECK-NEXT: _label0: +//CHECK-NEXT: *_d_a += _d_y; +//CHECK-NEXT: } + +//CHECK: void add_pullback(double a, double *b, double _d_y, double *_d_a, double *_d_b) { +//CHECK-NEXT: goto _label0; +//CHECK-NEXT: _label0: +//CHECK-NEXT: { +//CHECK-NEXT: *_d_a += _d_y; +//CHECK-NEXT: _d_b[0] += _d_y; +//CHECK-NEXT: } +//CHECK-NEXT: } + +// CHECK: void sq_defined_later_pullback(double x, double _d_y, double *_d_x) { +// CHECK-NEXT: goto _label0; +// CHECK-NEXT: _label0: +// CHECK-NEXT: { +// CHECK-NEXT: *_d_x += _d_y * x; +// CHECK-NEXT: *_d_x += x * _d_y; +// CHECK-NEXT: } +// CHECK-NEXT: } \ No newline at end of file diff --git a/test/Gradient/Functors.C b/test/Gradient/Functors.C index c9640700b..a930de83f 100644 --- a/test/Gradient/Functors.C +++ b/test/Gradient/Functors.C @@ -281,19 +281,7 @@ int main() { FunctorAsArg_grad.execute(E_temp, 7, 9, &dE_temp, &di, &dj); printf("%.2f %.2f\n", di, dj); // CHECK-EXEC: 27.00 21.00 - // CHECK: void FunctorAsArg_pullback(Experiment fn, double i, double j, double _d_y, Experiment *_d_fn, double *_d_i, double *_d_j) { - // CHECK-NEXT: Experiment _t0; - // CHECK-NEXT: _t0 = fn; - // CHECK-NEXT: goto _label0; - // CHECK-NEXT: _label0: - // CHECK-NEXT: { - // CHECK-NEXT: double _r0 = 0; - // CHECK-NEXT: double _r1 = 0; - // CHECK-NEXT: _t0.operator_call_pullback(i, j, _d_y, &(*_d_fn), &_r0, &_r1); - // CHECK-NEXT: *_d_i += _r0; - // CHECK-NEXT: *_d_j += _r1; - // CHECK-NEXT: } - // CHECK-NEXT: } + // CHECK: void FunctorAsArg_pullback(Experiment fn, double i, double j, double _d_y, Experiment *_d_fn, double *_d_i, double *_d_j); // CHECK: void FunctorAsArgWrapper_grad(double i, double j, double *_d_i, double *_d_j) { // CHECK-NEXT: Experiment _d_E({}); @@ -316,3 +304,17 @@ int main() { FunctorAsArgWrapper_grad.execute(7, 9, &di, &dj); printf("%.2f %.2f\n", di, dj); // CHECK-EXEC: 27.00 21.00 } + +// CHECK: void FunctorAsArg_pullback(Experiment fn, double i, double j, double _d_y, Experiment *_d_fn, double *_d_i, double *_d_j) { +// CHECK-NEXT: Experiment _t0; +// CHECK-NEXT: _t0 = fn; +// CHECK-NEXT: goto _label0; +// CHECK-NEXT: _label0: +// CHECK-NEXT: { +// CHECK-NEXT: double _r0 = 0; +// CHECK-NEXT: double _r1 = 0; +// CHECK-NEXT: _t0.operator_call_pullback(i, j, _d_y, &(*_d_fn), &_r0, &_r1); +// CHECK-NEXT: *_d_i += _r0; +// CHECK-NEXT: *_d_j += _r1; +// CHECK-NEXT: } +// CHECK-NEXT: } diff --git a/test/Gradient/Loops.C b/test/Gradient/Loops.C index 357bb374f..139fa48b8 100644 --- a/test/Gradient/Loops.C +++ b/test/Gradient/Loops.C @@ -277,14 +277,7 @@ double f_sum(double *p, int n) { //CHECK-NEXT: } double sq(double x) { return x * x; } -//CHECK: void sq_pullback(double x, double _d_y, double *_d_x) { -//CHECK-NEXT: goto _label0; -//CHECK-NEXT: _label0: -//CHECK-NEXT: { -//CHECK-NEXT: *_d_x += _d_y * x; -//CHECK-NEXT: *_d_x += x * _d_y; -//CHECK-NEXT: } -//CHECK-NEXT: } +//CHECK: void sq_pullback(double x, double _d_y, double *_d_x); double f_sum_squares(double *p, int n) { double s = 0; @@ -1734,3 +1727,12 @@ int main() { d_fn20.execute(x, 5, result); printf("{%.2f, %.2f, %.2f, %.2f, %.2f}\n", result[0], result[1], result[2], result[3], result[4]); // CHECK-EXEC: {5.00, 5.00, 5.00, 5.00, 5.00} } + +//CHECK: void sq_pullback(double x, double _d_y, double *_d_x) { +//CHECK-NEXT: goto _label0; +//CHECK-NEXT: _label0: +//CHECK-NEXT: { +//CHECK-NEXT: *_d_x += _d_y * x; +//CHECK-NEXT: *_d_x += x * _d_y; +//CHECK-NEXT: } +//CHECK-NEXT: } diff --git a/test/Gradient/MemberFunctions.C b/test/Gradient/MemberFunctions.C index 12905f366..26d5e2221 100644 --- a/test/Gradient/MemberFunctions.C +++ b/test/Gradient/MemberFunctions.C @@ -496,39 +496,9 @@ double fn2(SimpleFunctions& sf, double i) { return sf.ref_mem_fn(i); } -// CHECK: void ref_mem_fn_pullback(double i, double _d_y, SimpleFunctions *_d_this, double *_d_i) { -// CHECK-NEXT: double _t0; -// CHECK-NEXT: double _t1; -// CHECK-NEXT: _t0 = this->x; -// CHECK-NEXT: this->x = +i; -// CHECK-NEXT: _t1 = this->x; -// CHECK-NEXT: this->x = -i; -// CHECK-NEXT: goto _label0; -// CHECK-NEXT: _label0: -// CHECK-NEXT: (*_d_this).x += _d_y; -// CHECK-NEXT: { -// CHECK-NEXT: this->x = _t1; -// CHECK-NEXT: double _r_d1 = (*_d_this).x; -// CHECK-NEXT: (*_d_this).x -= _r_d1; -// 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 -= _r_d0; -// CHECK-NEXT: *_d_i += _r_d0; -// CHECK-NEXT: } -// CHECK-NEXT: } +// CHECK: void ref_mem_fn_pullback(double i, double _d_y, SimpleFunctions *_d_this, double *_d_i); -// CHECK: clad::ValueAndAdjoint ref_mem_fn_forw(double i, SimpleFunctions *_d_this, double *_d_i) { -// CHECK-NEXT: double _t0; -// CHECK-NEXT: double _t1; -// CHECK-NEXT: _t0 = this->x; -// CHECK-NEXT: this->x = +i; -// CHECK-NEXT: _t1 = this->x; -// CHECK-NEXT: this->x = -i; -// CHECK-NEXT: return {this->x, (*_d_this).x}; -// CHECK-NEXT: } +// CHECK: clad::ValueAndAdjoint ref_mem_fn_forw(double i, SimpleFunctions *_d_this, double *_d_i); // CHECK: void fn2_grad(SimpleFunctions &sf, double i, SimpleFunctions *_d_sf, double *_d_i) { // CHECK-NEXT: SimpleFunctions _t0; @@ -553,26 +523,9 @@ double fn5(SimpleFunctions& v, double value) { return v.x; } -// CHECK: void operator_plus_equal_pullback(double value, SimpleFunctions _d_y, SimpleFunctions *_d_this, double *_d_value) { -// CHECK-NEXT: double _t0; -// CHECK-NEXT: _t0 = this->x; -// CHECK-NEXT: this->x += value; -// CHECK-NEXT: goto _label0; -// CHECK-NEXT: _label0: -// CHECK-NEXT: ; -// CHECK-NEXT: { -// CHECK-NEXT: this->x = _t0; -// CHECK-NEXT: double _r_d0 = (*_d_this).x; -// CHECK-NEXT: *_d_value += _r_d0; -// CHECK-NEXT: } -// CHECK-NEXT: } +// CHECK: void operator_plus_equal_pullback(double value, SimpleFunctions _d_y, SimpleFunctions *_d_this, double *_d_value); -// CHECK: clad::ValueAndAdjoint operator_plus_equal_forw(double value, SimpleFunctions *_d_this, SimpleFunctions *_d_value) { -// CHECK-NEXT: double _t0; -// CHECK-NEXT: _t0 = this->x; -// CHECK-NEXT: this->x += value; -// CHECK-NEXT: return {*this, (*_d_this)}; -// CHECK-NEXT: } +// CHECK: clad::ValueAndAdjoint operator_plus_equal_forw(double value, SimpleFunctions *_d_this, SimpleFunctions *_d_value); // CHECK: void fn5_grad(SimpleFunctions &v, double value, SimpleFunctions *_d_v, double *_d_value) { // CHECK-NEXT: SimpleFunctions _t0; @@ -593,25 +546,9 @@ double fn4(SimpleFunctions& v) { return v.x; } -// CHECK: void operator_plus_plus_pullback(SimpleFunctions _d_y, SimpleFunctions *_d_this) { -// CHECK-NEXT: double _t0; -// CHECK-NEXT: _t0 = this->x; -// CHECK-NEXT: this->x += 1.; -// CHECK-NEXT: goto _label0; -// CHECK-NEXT: _label0: -// CHECK-NEXT: ; -// CHECK-NEXT: { -// CHECK-NEXT: this->x = _t0; -// CHECK-NEXT: double _r_d0 = (*_d_this).x; -// CHECK-NEXT: } -// CHECK-NEXT: } +// CHECK: void operator_plus_plus_pullback(SimpleFunctions _d_y, SimpleFunctions *_d_this); -// CHECK: clad::ValueAndAdjoint operator_plus_plus_forw(SimpleFunctions *_d_this) { -// CHECK-NEXT: double _t0; -// CHECK-NEXT: _t0 = this->x; -// CHECK-NEXT: this->x += 1.; -// CHECK-NEXT: return {*this, (*_d_this)}; -// CHECK-NEXT: } +// CHECK: clad::ValueAndAdjoint operator_plus_plus_forw(SimpleFunctions *_d_this); // CHECK: void fn4_grad(SimpleFunctions &v, SimpleFunctions *_d_v) { // CHECK-NEXT: SimpleFunctions _t0; @@ -711,7 +648,7 @@ int main() { d_fn3.execute(2, 3, 4, 5, &result[0], &result[1]); 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: 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: SimpleFunctions _d_sf({}); @@ -728,4 +665,79 @@ int main() { // CHECK-NEXT: *_d_j += _r1; // CHECK-NEXT: } // CHECK-NEXT: } + +// CHECK: void ref_mem_fn_pullback(double i, double _d_y, SimpleFunctions *_d_this, double *_d_i) { +// CHECK-NEXT: double _t0; +// CHECK-NEXT: double _t1; +// CHECK-NEXT: _t0 = this->x; +// CHECK-NEXT: this->x = +i; +// CHECK-NEXT: _t1 = this->x; +// CHECK-NEXT: this->x = -i; +// CHECK-NEXT: goto _label0; +// CHECK-NEXT: _label0: +// CHECK-NEXT: (*_d_this).x += _d_y; +// CHECK-NEXT: { +// CHECK-NEXT: this->x = _t1; +// CHECK-NEXT: double _r_d1 = (*_d_this).x; +// CHECK-NEXT: (*_d_this).x -= _r_d1; +// 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 -= _r_d0; +// CHECK-NEXT: *_d_i += _r_d0; +// CHECK-NEXT: } +// CHECK-NEXT: } + +// CHECK: clad::ValueAndAdjoint ref_mem_fn_forw(double i, SimpleFunctions *_d_this, double *_d_i) { +// CHECK-NEXT: double _t0; +// CHECK-NEXT: double _t1; +// CHECK-NEXT: _t0 = this->x; +// CHECK-NEXT: this->x = +i; +// CHECK-NEXT: _t1 = this->x; +// CHECK-NEXT: this->x = -i; +// CHECK-NEXT: return {this->x, (*_d_this).x}; +// CHECK-NEXT: } + +// CHECK: void operator_plus_equal_pullback(double value, SimpleFunctions _d_y, SimpleFunctions *_d_this, double *_d_value) { +// CHECK-NEXT: double _t0; +// CHECK-NEXT: _t0 = this->x; +// CHECK-NEXT: this->x += value; +// CHECK-NEXT: goto _label0; +// CHECK-NEXT: _label0: +// CHECK-NEXT: ; +// CHECK-NEXT: { +// CHECK-NEXT: this->x = _t0; +// CHECK-NEXT: double _r_d0 = (*_d_this).x; +// CHECK-NEXT: *_d_value += _r_d0; +// CHECK-NEXT: } +// CHECK-NEXT: } + +// CHECK: clad::ValueAndAdjoint operator_plus_equal_forw(double value, SimpleFunctions *_d_this, SimpleFunctions *_d_value) { +// CHECK-NEXT: double _t0; +// CHECK-NEXT: _t0 = this->x; +// CHECK-NEXT: this->x += value; +// CHECK-NEXT: return {*this, (*_d_this)}; +// CHECK-NEXT: } + +// CHECK: void operator_plus_plus_pullback(SimpleFunctions _d_y, SimpleFunctions *_d_this) { +// CHECK-NEXT: double _t0; +// CHECK-NEXT: _t0 = this->x; +// CHECK-NEXT: this->x += 1.; +// CHECK-NEXT: goto _label0; +// CHECK-NEXT: _label0: +// CHECK-NEXT: ; +// CHECK-NEXT: { +// CHECK-NEXT: this->x = _t0; +// CHECK-NEXT: double _r_d0 = (*_d_this).x; +// CHECK-NEXT: } +// CHECK-NEXT: } + +// CHECK: clad::ValueAndAdjoint operator_plus_plus_forw(SimpleFunctions *_d_this) { +// CHECK-NEXT: double _t0; +// CHECK-NEXT: _t0 = this->x; +// CHECK-NEXT: this->x += 1.; +// CHECK-NEXT: return {*this, (*_d_this)}; +// CHECK-NEXT: } } diff --git a/test/Gradient/UserDefinedTypes.C b/test/Gradient/UserDefinedTypes.C index 37e4fcde7..37ac0bb37 100644 --- a/test/Gradient/UserDefinedTypes.C +++ b/test/Gradient/UserDefinedTypes.C @@ -55,29 +55,7 @@ double sum(Tangent& t) { return res; } -// CHECK: void sum_pullback(Tangent &t, double _d_y, Tangent *_d_t) { -// CHECK-NEXT: double _d_res = 0; -// CHECK-NEXT: unsigned {{int|long}} _t0; -// CHECK-NEXT: int _d_i = 0; -// CHECK-NEXT: int i = 0; -// CHECK-NEXT: clad::tape _t1 = {}; -// CHECK-NEXT: double res = 0; -// CHECK-NEXT: _t0 = 0; -// CHECK-NEXT: for (i = 0; i < 5; ++i) { -// CHECK-NEXT: _t0++; -// CHECK-NEXT: clad::push(_t1, res); -// CHECK-NEXT: res += t.data[i]; -// CHECK-NEXT: } -// CHECK-NEXT: goto _label0; -// CHECK-NEXT: _label0: -// CHECK-NEXT: _d_res += _d_y; -// CHECK-NEXT: for (; _t0; _t0--) { -// CHECK-NEXT: --i; -// CHECK-NEXT: res = clad::pop(_t1); -// CHECK-NEXT: double _r_d0 = _d_res; -// CHECK-NEXT: (*_d_t).data[i] += _r_d0; -// CHECK-NEXT: } -// CHECK-NEXT: } +// CHECK: void sum_pullback(Tangent &t, double _d_y, Tangent *_d_t); double sum(double *data) { double res = 0; @@ -86,29 +64,7 @@ double sum(double *data) { return res; } -// CHECK: void sum_pullback(double *data, double _d_y, double *_d_data) { -// CHECK-NEXT: double _d_res = 0; -// CHECK-NEXT: unsigned {{int|long}} _t0; -// CHECK-NEXT: int _d_i = 0; -// CHECK-NEXT: int i = 0; -// CHECK-NEXT: clad::tape _t1 = {}; -// CHECK-NEXT: double res = 0; -// CHECK-NEXT: _t0 = 0; -// CHECK-NEXT: for (i = 0; i < 5; ++i) { -// CHECK-NEXT: _t0++; -// CHECK-NEXT: clad::push(_t1, res); -// CHECK-NEXT: res += data[i]; -// CHECK-NEXT: } -// CHECK-NEXT: goto _label0; -// CHECK-NEXT: _label0: -// CHECK-NEXT: _d_res += _d_y; -// CHECK-NEXT: for (; _t0; _t0--) { -// CHECK-NEXT: --i; -// CHECK-NEXT: res = clad::pop(_t1); -// CHECK-NEXT: double _r_d0 = _d_res; -// CHECK-NEXT: _d_data[i] += _r_d0; -// CHECK-NEXT: } -// CHECK-NEXT: } +// CHECK: void sum_pullback(double *data, double _d_y, double *_d_data); double fn2(Tangent t, double i) { double res = sum(t); @@ -222,17 +178,7 @@ double fn5(const Tangent& t, double i) { return t.someMemFn2(i, i); } -// CHECK: void someMemFn2_pullback(double i, double j, double _d_y, Tangent *_d_this, double *_d_i, double *_d_j) const { -// CHECK-NEXT: goto _label0; -// CHECK-NEXT: _label0: -// CHECK-NEXT: { -// CHECK-NEXT: (*_d_this).data[0] += _d_y * i; -// CHECK-NEXT: *_d_i += this->data[0] * _d_y; -// CHECK-NEXT: (*_d_this).data[1] += _d_y * j * i; -// CHECK-NEXT: *_d_i += this->data[1] * _d_y * j; -// CHECK-NEXT: *_d_j += this->data[1] * i * _d_y; -// CHECK-NEXT: } -// CHECK-NEXT: } +// CHECK: void someMemFn2_pullback(double i, double j, double _d_y, Tangent *_d_this, double *_d_i, double *_d_j) const; // CHECK: void fn5_grad(const Tangent &t, double i, Tangent *_d_t, double *_d_i) { // CHECK-NEXT: Tangent _t0; @@ -256,29 +202,11 @@ double fn6(dcomplex c, double i) { res += 4*c.real(); return res; } -// CHECK: void real_pullback({{.*}} [[__val:.*]], std{{(::__1)?}}::complex *_d_this, {{.*}} *[[_d___val:[a-zA-Z_]*]]){{.*}} { -// CHECK-NEXT: double _t0; -// CHECK-NEXT: _t0 ={{( __real)?}} this->[[_M_value:.*]]; -// CHECK-NEXT: {{(__real)?}} this->[[_M_value:.*]] = [[__val]]; -// CHECK-NEXT: { -// CHECK-NEXT: {{(__real)?}} this->[[_M_value:.*]] = _t0; -// CHECK-NEXT: double _r_d0 ={{( __real)?}} (*_d_this).[[_M_value]]; -// CHECK-NEXT: {{(__real)?}} (*_d_this).[[_M_value]] -= _r_d0; -// CHECK-NEXT: *[[_d___val]] += _r_d0; -// CHECK-NEXT: } -// CHECK-NEXT: } +// CHECK: void real_pullback({{.*}} [[__val:.*]], std{{(::__1)?}}::complex *_d_this, {{.*}} *[[_d___val:[a-zA-Z_]*]]){{.*}}; -// CHECK: constexpr void real_pullback(double _d_y, std{{(::__1)?}}::complex *_d_this){{.*}} { -// CHECK-NEXT: goto _label0; -// CHECK-NEXT: _label0: -// CHECK-NEXT: {{(__real)?}} (*_d_this).{{.*}} += _d_y; -// CHECK-NEXT: } +// CHECK: constexpr void real_pullback(double _d_y, std{{(::__1)?}}::complex *_d_this){{.*}}; -// CHECK: constexpr void imag_pullback(double _d_y, std{{(::__1)?}}::complex *_d_this){{.*}} { -// CHECK-NEXT: goto _label0; -// CHECK-NEXT: _label0: -// CHECK-NEXT: {{(__imag)?}} (*_d_this).{{.*}} += _d_y; -// CHECK-NEXT: } +// CHECK: constexpr void imag_pullback(double _d_y, std{{(::__1)?}}::complex *_d_this){{.*}}; // CHECK: void fn6_grad(dcomplex c, double i, dcomplex *_d_c, double *_d_i) { // CHECK-NEXT: dcomplex _t0; @@ -359,25 +287,7 @@ double fn8(Tangent t, dcomplex c) { return sum(t); } -// CHECK: void updateTo_pullback(double d, Tangent *_d_this, double *_d_d) { -// CHECK-NEXT: unsigned {{int|long}} _t0; -// CHECK-NEXT: int _d_i = 0; -// CHECK-NEXT: int i = 0; -// CHECK-NEXT: clad::tape _t1 = {}; -// CHECK-NEXT: _t0 = 0; -// CHECK-NEXT: for (i = 0; i < 5; ++i) { -// CHECK-NEXT: _t0++; -// CHECK-NEXT: clad::push(_t1, this->data[i]); -// CHECK-NEXT: this->data[i] = d; -// CHECK-NEXT: } -// CHECK-NEXT: for (; _t0; _t0--) { -// CHECK-NEXT: --i; -// CHECK-NEXT: this->data[i] = clad::pop(_t1); -// CHECK-NEXT: double _r_d0 = (*_d_this).data[i]; -// CHECK-NEXT: (*_d_this).data[i] -= _r_d0; -// CHECK-NEXT: *_d_d += _r_d0; -// CHECK-NEXT: } -// CHECK-NEXT: } +// CHECK: void updateTo_pullback(double d, Tangent *_d_this, double *_d_d); // CHECK: void fn8_grad(Tangent t, dcomplex c, Tangent *_d_t, dcomplex *_d_c) { // CHECK-NEXT: dcomplex _t0; @@ -493,3 +403,107 @@ int main() { TEST_GRADIENT(fn8, /*numOfDerivativeArgs=*/2, t, c1, &d_t, &d_c1); // CHECK-EXEC: {0.00, 0.00, 0.00, 0.00, 0.00, 5.00, 0.00} TEST_GRADIENT(fn9, /*numOfDerivativeArgs=*/2, t, c1, &d_t, &d_c1); // CHECK-EXEC: {1.00, 1.00, 1.00, 1.00, 1.00, 5.00, 10.00} } + +// CHECK: void sum_pullback(Tangent &t, double _d_y, Tangent *_d_t) { +// CHECK-NEXT: double _d_res = 0; +// CHECK-NEXT: unsigned {{int|long}} _t0; +// CHECK-NEXT: int _d_i = 0; +// CHECK-NEXT: int i = 0; +// CHECK-NEXT: clad::tape _t1 = {}; +// CHECK-NEXT: double res = 0; +// CHECK-NEXT: _t0 = 0; +// CHECK-NEXT: for (i = 0; i < 5; ++i) { +// CHECK-NEXT: _t0++; +// CHECK-NEXT: clad::push(_t1, res); +// CHECK-NEXT: res += t.data[i]; +// CHECK-NEXT: } +// CHECK-NEXT: goto _label0; +// CHECK-NEXT: _label0: +// CHECK-NEXT: _d_res += _d_y; +// CHECK-NEXT: for (; _t0; _t0--) { +// CHECK-NEXT: --i; +// CHECK-NEXT: res = clad::pop(_t1); +// CHECK-NEXT: double _r_d0 = _d_res; +// CHECK-NEXT: (*_d_t).data[i] += _r_d0; +// CHECK-NEXT: } +// CHECK-NEXT: } + +// CHECK: void sum_pullback(double *data, double _d_y, double *_d_data) { +// CHECK-NEXT: double _d_res = 0; +// CHECK-NEXT: unsigned {{int|long}} _t0; +// CHECK-NEXT: int _d_i = 0; +// CHECK-NEXT: int i = 0; +// CHECK-NEXT: clad::tape _t1 = {}; +// CHECK-NEXT: double res = 0; +// CHECK-NEXT: _t0 = 0; +// CHECK-NEXT: for (i = 0; i < 5; ++i) { +// CHECK-NEXT: _t0++; +// CHECK-NEXT: clad::push(_t1, res); +// CHECK-NEXT: res += data[i]; +// CHECK-NEXT: } +// CHECK-NEXT: goto _label0; +// CHECK-NEXT: _label0: +// CHECK-NEXT: _d_res += _d_y; +// CHECK-NEXT: for (; _t0; _t0--) { +// CHECK-NEXT: --i; +// CHECK-NEXT: res = clad::pop(_t1); +// CHECK-NEXT: double _r_d0 = _d_res; +// CHECK-NEXT: _d_data[i] += _r_d0; +// CHECK-NEXT: } +// CHECK-NEXT: } + +// CHECK: void someMemFn2_pullback(double i, double j, double _d_y, Tangent *_d_this, double *_d_i, double *_d_j) const { +// CHECK-NEXT: goto _label0; +// CHECK-NEXT: _label0: +// CHECK-NEXT: { +// CHECK-NEXT: (*_d_this).data[0] += _d_y * i; +// CHECK-NEXT: *_d_i += this->data[0] * _d_y; +// CHECK-NEXT: (*_d_this).data[1] += _d_y * j * i; +// CHECK-NEXT: *_d_i += this->data[1] * _d_y * j; +// CHECK-NEXT: *_d_j += this->data[1] * i * _d_y; +// CHECK-NEXT: } +// CHECK-NEXT: } + +// CHECK: void real_pullback({{.*}} [[__val:.*]], std{{(::__1)?}}::complex *_d_this, {{.*}} *[[_d___val:[a-zA-Z_]*]]){{.*}} { +// CHECK-NEXT: double _t0; +// CHECK-NEXT: _t0 ={{( __real)?}} this->[[_M_value:.*]]; +// CHECK-NEXT: {{(__real)?}} this->[[_M_value:.*]] = [[__val]]; +// CHECK-NEXT: { +// CHECK-NEXT: {{(__real)?}} this->[[_M_value:.*]] = _t0; +// CHECK-NEXT: double _r_d0 ={{( __real)?}} (*_d_this).[[_M_value]]; +// CHECK-NEXT: {{(__real)?}} (*_d_this).[[_M_value]] -= _r_d0; +// CHECK-NEXT: *[[_d___val]] += _r_d0; +// CHECK-NEXT: } +// CHECK-NEXT: } + +// CHECK: constexpr void real_pullback(double _d_y, std{{(::__1)?}}::complex *_d_this){{.*}} { +// CHECK-NEXT: goto _label0; +// CHECK-NEXT: _label0: +// CHECK-NEXT: {{(__real)?}} (*_d_this).{{.*}} += _d_y; +// CHECK-NEXT: } + +// CHECK: constexpr void imag_pullback(double _d_y, std{{(::__1)?}}::complex *_d_this){{.*}} { +// CHECK-NEXT: goto _label0; +// CHECK-NEXT: _label0: +// CHECK-NEXT: {{(__imag)?}} (*_d_this).{{.*}} += _d_y; +// CHECK-NEXT: } + +// CHECK: void updateTo_pullback(double d, Tangent *_d_this, double *_d_d) { +// CHECK-NEXT: unsigned {{int|long}} _t0; +// CHECK-NEXT: int _d_i = 0; +// CHECK-NEXT: int i = 0; +// CHECK-NEXT: clad::tape _t1 = {}; +// CHECK-NEXT: _t0 = 0; +// CHECK-NEXT: for (i = 0; i < 5; ++i) { +// CHECK-NEXT: _t0++; +// CHECK-NEXT: clad::push(_t1, this->data[i]); +// CHECK-NEXT: this->data[i] = d; +// CHECK-NEXT: } +// CHECK-NEXT: for (; _t0; _t0--) { +// CHECK-NEXT: --i; +// CHECK-NEXT: this->data[i] = clad::pop(_t1); +// CHECK-NEXT: double _r_d0 = (*_d_this).data[i]; +// CHECK-NEXT: (*_d_this).data[i] -= _r_d0; +// CHECK-NEXT: *_d_d += _r_d0; +// CHECK-NEXT: } +// CHECK-NEXT: } \ No newline at end of file diff --git a/test/Hessian/BuiltinDerivatives.C b/test/Hessian/BuiltinDerivatives.C index 15ca77e67..9d9106350 100644 --- a/test/Hessian/BuiltinDerivatives.C +++ b/test/Hessian/BuiltinDerivatives.C @@ -147,37 +147,9 @@ int main() { TEST1(f5, 3); // CHECK-EXEC: Result is = {3.84} TEST2(f6, 3, 4); // CHECK-EXEC: Result is = {108.00, 145.65, 145.65, 97.76} -// CHECK: void sin_pushforward_pullback(float x, float d_x, ValueAndPushforward _d_y, float *_d_x, float *_d_d_x) { -// CHECK-NEXT: float _t0; -// CHECK-NEXT: _t0 = ::std::cos(x); -// CHECK-NEXT: goto _label0; -// CHECK-NEXT: _label0: -// CHECK-NEXT: { -// CHECK-NEXT: float _r0 = 0; -// CHECK-NEXT: _r0 += _d_y.value * clad::custom_derivatives{{(::std)?}}::sin_pushforward(x, 1.F).pushforward; -// CHECK-NEXT: *_d_x += _r0; -// CHECK-NEXT: float _r1 = 0; -// CHECK-NEXT: _r1 += _d_y.pushforward * d_x * clad::custom_derivatives{{(::std)?}}::cos_pushforward(x, 1.F).pushforward; -// CHECK-NEXT: *_d_x += _r1; -// CHECK-NEXT: *_d_d_x += _t0 * _d_y.pushforward; -// CHECK-NEXT: } -// CHECK-NEXT: } +// CHECK: void sin_pushforward_pullback(float x, float d_x, ValueAndPushforward _d_y, float *_d_x, float *_d_d_x); -// CHECK: void cos_pushforward_pullback(float x, float d_x, ValueAndPushforward _d_y, float *_d_x, float *_d_d_x) { -// CHECK-NEXT: float _t0; -// CHECK-NEXT: _t0 = ::std::sin(x); -// CHECK-NEXT: goto _label0; -// CHECK-NEXT: _label0: -// CHECK-NEXT: { -// CHECK-NEXT: float _r0 = 0; -// CHECK-NEXT: _r0 += _d_y.value * clad::custom_derivatives{{(::std)?}}::cos_pushforward(x, 1.F).pushforward; -// CHECK-NEXT: *_d_x += _r0; -// CHECK-NEXT: float _r1 = 0; -// CHECK-NEXT: _r1 += -1 * _d_y.pushforward * d_x * clad::custom_derivatives{{(::std)?}}::sin_pushforward(x, 1.F).pushforward; -// CHECK-NEXT: *_d_x += _r1; -// CHECK-NEXT: *_d_d_x += -1 * _t0 * _d_y.pushforward; -// CHECK-NEXT: } -// CHECK-NEXT: } +// CHECK: void cos_pushforward_pullback(float x, float d_x, ValueAndPushforward _d_y, float *_d_x, float *_d_d_x); // CHECK: void f1_darg0_grad(float x, float *_d_x) { // CHECK-NEXT: float _d__d_x = 0; @@ -208,21 +180,7 @@ int main() { // CHECK-NEXT: } // CHECK-NEXT: } -// CHECK: void exp_pushforward_pullback(float x, float d_x, ValueAndPushforward _d_y, float *_d_x, float *_d_d_x) { -// CHECK-NEXT: float _t0; -// CHECK-NEXT: _t0 = ::std::exp(x); -// CHECK-NEXT: goto _label0; -// CHECK-NEXT: _label0: -// CHECK-NEXT: { -// CHECK-NEXT: float _r0 = 0; -// CHECK-NEXT: _r0 += _d_y.value * clad::custom_derivatives{{(::std)?}}::exp_pushforward(x, 1.F).pushforward; -// CHECK-NEXT: *_d_x += _r0; -// CHECK-NEXT: float _r1 = 0; -// CHECK-NEXT: _r1 += _d_y.pushforward * d_x * clad::custom_derivatives{{(::std)?}}::exp_pushforward(x, 1.F).pushforward; -// CHECK-NEXT: *_d_x += _r1; -// CHECK-NEXT: *_d_d_x += _t0 * _d_y.pushforward; -// CHECK-NEXT: } -// CHECK-NEXT: } +// CHECK: void exp_pushforward_pullback(float x, float d_x, ValueAndPushforward _d_y, float *_d_x, float *_d_d_x); // CHECK: void f2_darg0_grad(float x, float *_d_x) { // CHECK-NEXT: float _d__d_x = 0; @@ -241,18 +199,7 @@ int main() { // CHECK-NEXT: } // CHECK-NEXT: } -// CHECK: void log_pushforward_pullback(float x, float d_x, ValueAndPushforward _d_y, float *_d_x, float *_d_d_x) { -// CHECK-NEXT: goto _label0; -// CHECK-NEXT: _label0: -// CHECK-NEXT: { -// CHECK-NEXT: float _r0 = 0; -// CHECK-NEXT: _r0 += _d_y.value * clad::custom_derivatives{{(::std)?}}::log_pushforward(x, 1.F).pushforward; -// CHECK-NEXT: *_d_x += _r0; -// CHECK-NEXT: double _r1 = _d_y.pushforward * d_x * -1. / (x * x); -// CHECK-NEXT: *_d_x += _r1; -// CHECK-NEXT: *_d_d_x += (1. / x) * _d_y.pushforward; -// CHECK-NEXT: } -// CHECK-NEXT: } +// CHECK: void log_pushforward_pullback(float x, float d_x, ValueAndPushforward _d_y, float *_d_x, float *_d_d_x); // CHECK: void f3_darg0_grad(float x, float *_d_x) { // CHECK-NEXT: float _d__d_x = 0; @@ -271,60 +218,7 @@ int main() { // CHECK-NEXT: } // CHECK-NEXT: } -// CHECK: void pow_pushforward_pullback(float x, float exponent, float d_x, float d_exponent, ValueAndPushforward _d_y, float *_d_x, float *_d_exponent, float *_d_d_x, float *_d_d_exponent) { -// CHECK-NEXT: float _d_val = 0; -// CHECK-NEXT: float _t0; -// CHECK-NEXT: float _d_derivative = 0; -// CHECK-NEXT: float _cond0; -// CHECK-NEXT: float _t1; -// CHECK-NEXT: float _t2; -// CHECK-NEXT: float _t3; -// CHECK-NEXT: float val = ::std::pow(x, exponent); -// CHECK-NEXT: _t0 = ::std::pow(x, exponent - 1); -// CHECK-NEXT: float derivative = (exponent * _t0) * d_x; -// CHECK-NEXT: _cond0 = d_exponent; -// CHECK-NEXT: if (_cond0) { -// CHECK-NEXT: _t1 = derivative; -// CHECK-NEXT: _t3 = ::std::pow(x, exponent); -// CHECK-NEXT: _t2 = ::std::log(x); -// CHECK-NEXT: derivative += (_t3 * _t2) * d_exponent; -// CHECK-NEXT: } -// CHECK-NEXT: goto _label0; -// CHECK-NEXT: _label0: -// CHECK-NEXT: { -// CHECK-NEXT: _d_val += _d_y.value; -// CHECK-NEXT: _d_derivative += _d_y.pushforward; -// CHECK-NEXT: } -// CHECK-NEXT: if (_cond0) { -// CHECK-NEXT: derivative = _t1; -// CHECK-NEXT: float _r_d0 = _d_derivative; -// CHECK-NEXT: float _r4 = 0; -// CHECK-NEXT: float _r5 = 0; -// CHECK-NEXT: clad::custom_derivatives{{(::std)?}}::pow_pullback(x, exponent, _r_d0 * d_exponent * _t2, &_r4, &_r5); -// CHECK-NEXT: *_d_x += _r4; -// CHECK-NEXT: *_d_exponent += _r5; -// CHECK-NEXT: float _r6 = 0; -// CHECK-NEXT: _r6 += _t3 * _r_d0 * d_exponent * clad::custom_derivatives{{(::std)?}}::log_pushforward(x, 1.F).pushforward; -// CHECK-NEXT: *_d_x += _r6; -// CHECK-NEXT: *_d_d_exponent += (_t3 * _t2) * _r_d0; -// CHECK-NEXT: } -// CHECK-NEXT: { -// CHECK-NEXT: *_d_exponent += _d_derivative * d_x * _t0; -// CHECK-NEXT: float _r2 = 0; -// CHECK-NEXT: float _r3 = 0; -// CHECK-NEXT: clad::custom_derivatives{{(::std)?}}::pow_pullback(x, exponent - 1, exponent * _d_derivative * d_x, &_r2, &_r3); -// CHECK-NEXT: *_d_x += _r2; -// CHECK-NEXT: *_d_exponent += _r3; -// CHECK-NEXT: *_d_d_x += (exponent * _t0) * _d_derivative; -// CHECK-NEXT: } -// CHECK-NEXT: { -// CHECK-NEXT: float _r0 = 0; -// CHECK-NEXT: float _r1 = 0; -// CHECK-NEXT: clad::custom_derivatives{{(::std)?}}::pow_pullback(x, exponent, _d_val, &_r0, &_r1); -// CHECK-NEXT: *_d_x += _r0; -// CHECK-NEXT: *_d_exponent += _r1; -// CHECK-NEXT: } -// CHECK-NEXT: } +// CHECK: void pow_pushforward_pullback(float x, float exponent, float d_x, float d_exponent, ValueAndPushforward _d_y, float *_d_x, float *_d_exponent, float *_d_d_x, float *_d_d_exponent); // CHECK: void f4_darg0_grad(float x, float *_d_x) { // CHECK-NEXT: float _d__d_x = 0; @@ -409,4 +303,120 @@ int main() { // CHECK-NEXT: _d__d_y += _r3; // CHECK-NEXT: } // CHECK-NEXT: } + +// CHECK: void sin_pushforward_pullback(float x, float d_x, ValueAndPushforward _d_y, float *_d_x, float *_d_d_x) { +// CHECK-NEXT: float _t0; +// CHECK-NEXT: _t0 = ::std::cos(x); +// CHECK-NEXT: goto _label0; +// CHECK-NEXT: _label0: +// CHECK-NEXT: { +// CHECK-NEXT: float _r0 = 0; +// CHECK-NEXT: _r0 += _d_y.value * clad::custom_derivatives{{(::std)?}}::sin_pushforward(x, 1.F).pushforward; +// CHECK-NEXT: *_d_x += _r0; +// CHECK-NEXT: float _r1 = 0; +// CHECK-NEXT: _r1 += _d_y.pushforward * d_x * clad::custom_derivatives{{(::std)?}}::cos_pushforward(x, 1.F).pushforward; +// CHECK-NEXT: *_d_x += _r1; +// CHECK-NEXT: *_d_d_x += _t0 * _d_y.pushforward; +// CHECK-NEXT: } +// CHECK-NEXT: } + +// CHECK: void cos_pushforward_pullback(float x, float d_x, ValueAndPushforward _d_y, float *_d_x, float *_d_d_x) { +// CHECK-NEXT: float _t0; +// CHECK-NEXT: _t0 = ::std::sin(x); +// CHECK-NEXT: goto _label0; +// CHECK-NEXT: _label0: +// CHECK-NEXT: { +// CHECK-NEXT: float _r0 = 0; +// CHECK-NEXT: _r0 += _d_y.value * clad::custom_derivatives{{(::std)?}}::cos_pushforward(x, 1.F).pushforward; +// CHECK-NEXT: *_d_x += _r0; +// CHECK-NEXT: float _r1 = 0; +// CHECK-NEXT: _r1 += -1 * _d_y.pushforward * d_x * clad::custom_derivatives{{(::std)?}}::sin_pushforward(x, 1.F).pushforward; +// CHECK-NEXT: *_d_x += _r1; +// CHECK-NEXT: *_d_d_x += -1 * _t0 * _d_y.pushforward; +// CHECK-NEXT: } +// CHECK-NEXT: } + +// CHECK: void exp_pushforward_pullback(float x, float d_x, ValueAndPushforward _d_y, float *_d_x, float *_d_d_x) { +// CHECK-NEXT: float _t0; +// CHECK-NEXT: _t0 = ::std::exp(x); +// CHECK-NEXT: goto _label0; +// CHECK-NEXT: _label0: +// CHECK-NEXT: { +// CHECK-NEXT: float _r0 = 0; +// CHECK-NEXT: _r0 += _d_y.value * clad::custom_derivatives{{(::std)?}}::exp_pushforward(x, 1.F).pushforward; +// CHECK-NEXT: *_d_x += _r0; +// CHECK-NEXT: float _r1 = 0; +// CHECK-NEXT: _r1 += _d_y.pushforward * d_x * clad::custom_derivatives{{(::std)?}}::exp_pushforward(x, 1.F).pushforward; +// CHECK-NEXT: *_d_x += _r1; +// CHECK-NEXT: *_d_d_x += _t0 * _d_y.pushforward; +// CHECK-NEXT: } +// CHECK-NEXT: } + +// CHECK: void log_pushforward_pullback(float x, float d_x, ValueAndPushforward _d_y, float *_d_x, float *_d_d_x) { +// CHECK-NEXT: goto _label0; +// CHECK-NEXT: _label0: +// CHECK-NEXT: { +// CHECK-NEXT: float _r0 = 0; +// CHECK-NEXT: _r0 += _d_y.value * clad::custom_derivatives{{(::std)?}}::log_pushforward(x, 1.F).pushforward; +// CHECK-NEXT: *_d_x += _r0; +// CHECK-NEXT: double _r1 = _d_y.pushforward * d_x * -1. / (x * x); +// CHECK-NEXT: *_d_x += _r1; +// CHECK-NEXT: *_d_d_x += (1. / x) * _d_y.pushforward; +// CHECK-NEXT: } +// CHECK-NEXT: } + +// CHECK: void pow_pushforward_pullback(float x, float exponent, float d_x, float d_exponent, ValueAndPushforward _d_y, float *_d_x, float *_d_exponent, float *_d_d_x, float *_d_d_exponent) { +// CHECK-NEXT: float _d_val = 0; +// CHECK-NEXT: float _t0; +// CHECK-NEXT: float _d_derivative = 0; +// CHECK-NEXT: float _cond0; +// CHECK-NEXT: float _t1; +// CHECK-NEXT: float _t2; +// CHECK-NEXT: float _t3; +// CHECK-NEXT: float val = ::std::pow(x, exponent); +// CHECK-NEXT: _t0 = ::std::pow(x, exponent - 1); +// CHECK-NEXT: float derivative = (exponent * _t0) * d_x; +// CHECK-NEXT: _cond0 = d_exponent; +// CHECK-NEXT: if (_cond0) { +// CHECK-NEXT: _t1 = derivative; +// CHECK-NEXT: _t3 = ::std::pow(x, exponent); +// CHECK-NEXT: _t2 = ::std::log(x); +// CHECK-NEXT: derivative += (_t3 * _t2) * d_exponent; +// CHECK-NEXT: } +// CHECK-NEXT: goto _label0; +// CHECK-NEXT: _label0: +// CHECK-NEXT: { +// CHECK-NEXT: _d_val += _d_y.value; +// CHECK-NEXT: _d_derivative += _d_y.pushforward; +// CHECK-NEXT: } +// CHECK-NEXT: if (_cond0) { +// CHECK-NEXT: derivative = _t1; +// CHECK-NEXT: float _r_d0 = _d_derivative; +// CHECK-NEXT: float _r4 = 0; +// CHECK-NEXT: float _r5 = 0; +// CHECK-NEXT: clad::custom_derivatives{{(::std)?}}::pow_pullback(x, exponent, _r_d0 * d_exponent * _t2, &_r4, &_r5); +// CHECK-NEXT: *_d_x += _r4; +// CHECK-NEXT: *_d_exponent += _r5; +// CHECK-NEXT: float _r6 = 0; +// CHECK-NEXT: _r6 += _t3 * _r_d0 * d_exponent * clad::custom_derivatives{{(::std)?}}::log_pushforward(x, 1.F).pushforward; +// CHECK-NEXT: *_d_x += _r6; +// CHECK-NEXT: *_d_d_exponent += (_t3 * _t2) * _r_d0; +// CHECK-NEXT: } +// CHECK-NEXT: { +// CHECK-NEXT: *_d_exponent += _d_derivative * d_x * _t0; +// CHECK-NEXT: float _r2 = 0; +// CHECK-NEXT: float _r3 = 0; +// CHECK-NEXT: clad::custom_derivatives{{(::std)?}}::pow_pullback(x, exponent - 1, exponent * _d_derivative * d_x, &_r2, &_r3); +// CHECK-NEXT: *_d_x += _r2; +// CHECK-NEXT: *_d_exponent += _r3; +// CHECK-NEXT: *_d_d_x += (exponent * _t0) * _d_derivative; +// CHECK-NEXT: } +// CHECK-NEXT: { +// CHECK-NEXT: float _r0 = 0; +// CHECK-NEXT: float _r1 = 0; +// CHECK-NEXT: clad::custom_derivatives{{(::std)?}}::pow_pullback(x, exponent, _d_val, &_r0, &_r1); +// CHECK-NEXT: *_d_x += _r0; +// CHECK-NEXT: *_d_exponent += _r1; +// CHECK-NEXT: } +// CHECK-NEXT: } } diff --git a/test/Hessian/NestedFunctionCalls.C b/test/Hessian/NestedFunctionCalls.C index 2bf974b3f..65021b12c 100644 --- a/test/Hessian/NestedFunctionCalls.C +++ b/test/Hessian/NestedFunctionCalls.C @@ -54,24 +54,7 @@ double f2(double x, double y){ // CHECK-NEXT: } -// CHECK: void f_pushforward_pullback(double x, double y, double _d_x, double _d_y, clad::ValueAndPushforward _d_y0, double *_d_x, double *_d_y, double *_d__d_x, double *_d__d_y) { -// CHECK-NEXT: goto _label0; -// CHECK-NEXT: _label0: -// CHECK-NEXT: { -// CHECK-NEXT: *_d_x += _d_y0.value * x; -// CHECK-NEXT: *_d_x += x * _d_y0.value; -// CHECK-NEXT: *_d_y += _d_y0.value * y; -// CHECK-NEXT: *_d_y += y * _d_y0.value; -// CHECK-NEXT: *_d__d_x += _d_y0.pushforward * x; -// CHECK-NEXT: *_d_x += _d_x * _d_y0.pushforward; -// CHECK-NEXT: *_d_x += _d_y0.pushforward * _d_x; -// CHECK-NEXT: *_d__d_x += x * _d_y0.pushforward; -// CHECK-NEXT: *_d__d_y += _d_y0.pushforward * y; -// CHECK-NEXT: *_d_y += _d_y * _d_y0.pushforward; -// CHECK-NEXT: *_d_y += _d_y0.pushforward * _d_y; -// CHECK-NEXT: *_d__d_y += y * _d_y0.pushforward; -// CHECK-NEXT: } -// CHECK-NEXT: } +// CHECK: void f_pushforward_pullback(double x, double y, double _d_x, double _d_y, clad::ValueAndPushforward _d_y0, double *_d_x, double *_d_y, double *_d__d_x, double *_d__d_y); // CHECK: void f2_darg0_grad(double x, double y, double *_d_x, double *_d_y) { // CHECK-NEXT: double _d__d_x = 0; @@ -132,7 +115,24 @@ double f2(double x, double y){ // CHECK-NEXT: } // CHECK-NEXT: } - +// CHECK: void f_pushforward_pullback(double x, double y, double _d_x, double _d_y, clad::ValueAndPushforward _d_y0, double *_d_x, double *_d_y, double *_d__d_x, double *_d__d_y) { +// CHECK-NEXT: goto _label0; +// CHECK-NEXT: _label0: +// CHECK-NEXT: { +// CHECK-NEXT: *_d_x += _d_y0.value * x; +// CHECK-NEXT: *_d_x += x * _d_y0.value; +// CHECK-NEXT: *_d_y += _d_y0.value * y; +// CHECK-NEXT: *_d_y += y * _d_y0.value; +// CHECK-NEXT: *_d__d_x += _d_y0.pushforward * x; +// CHECK-NEXT: *_d_x += _d_x * _d_y0.pushforward; +// CHECK-NEXT: *_d_x += _d_y0.pushforward * _d_x; +// CHECK-NEXT: *_d__d_x += x * _d_y0.pushforward; +// CHECK-NEXT: *_d__d_y += _d_y0.pushforward * y; +// CHECK-NEXT: *_d_y += _d_y * _d_y0.pushforward; +// CHECK-NEXT: *_d_y += _d_y0.pushforward * _d_y; +// CHECK-NEXT: *_d__d_y += y * _d_y0.pushforward; +// CHECK-NEXT: } +// CHECK-NEXT: } int main() { auto f_hess = clad::hessian(f2); diff --git a/test/Jacobian/Jacobian.C b/test/Jacobian/Jacobian.C index 318acd0f2..ffe52060c 100644 --- a/test/Jacobian/Jacobian.C +++ b/test/Jacobian/Jacobian.C @@ -80,14 +80,7 @@ void f_3_jac(double x, double y, double z, double *_result, double *jacobianMatr //CHECK-NEXT:} double multiply(double x, double y) { return x * y; } -//CHECK: void multiply_pullback(double x, double y, double _d_y0, double *_d_x, double *_d_y) { -//CHECK-NEXT: goto _label0; -//CHECK-NEXT: _label0: -//CHECK-NEXT: { -//CHECK-NEXT: *_d_x += _d_y0 * y; -//CHECK-NEXT: *_d_y += x * _d_y0; -//CHECK-NEXT: } -//CHECK-NEXT:} +//CHECK: void multiply_pullback(double x, double y, double _d_y0, double *_d_x, double *_d_y); void f_4(double x, double y, double z, double *_result) { double constant = 42; @@ -189,3 +182,12 @@ int main() { TEST(f_4, 1, 2, 3); // CHECK-EXEC: Result is = {84.00, 42.00, 0.00, 0.00, 126.00, 84.00, 126.00, 0.00, 42.00} TEST_F_1_SINGLE_PARAM(1, 2, 3); // CHECK-EXEC: Result is = {3.00, 3.00, -2.00} } + +//CHECK: void multiply_pullback(double x, double y, double _d_y0, double *_d_x, double *_d_y) { +//CHECK-NEXT: goto _label0; +//CHECK-NEXT: _label0: +//CHECK-NEXT: { +//CHECK-NEXT: *_d_x += _d_y0 * y; +//CHECK-NEXT: *_d_y += x * _d_y0; +//CHECK-NEXT: } +//CHECK-NEXT:} \ No newline at end of file diff --git a/test/Misc/RunDemos.C b/test/Misc/RunDemos.C index e9779ee8f..d92406659 100644 --- a/test/Misc/RunDemos.C +++ b/test/Misc/RunDemos.C @@ -213,15 +213,7 @@ //-----------------------------------------------------------------------------/ // RUN: %cladclang %S/../../demos/GradientDescent.cpp -I%S/../../include -oGradientDescent.out | FileCheck -check-prefix CHECK_GRADIENT_DESCENT %s -//CHECK_GRADIENT_DESCENT: void f_pullback(double theta_0, double theta_1, double x, double _d_y, double *_d_theta_0, double *_d_theta_1, double *_d_x) { -//CHECK_GRADIENT_DESCENT-NEXT: goto _label0; -//CHECK_GRADIENT_DESCENT-NEXT: _label0: -//CHECK_GRADIENT_DESCENT-NEXT: { -//CHECK_GRADIENT_DESCENT-NEXT: *_d_theta_0 += _d_y; -//CHECK_GRADIENT_DESCENT-NEXT: *_d_theta_1 += _d_y * x; -//CHECK_GRADIENT_DESCENT-NEXT: *_d_x += theta_1 * _d_y; -//CHECK_GRADIENT_DESCENT-NEXT: } -//CHECK_GRADIENT_DESCENT-NEXT: } +//CHECK_GRADIENT_DESCENT: void f_pullback(double theta_0, double theta_1, double x, double _d_y, double *_d_theta_0, double *_d_theta_1, double *_d_x); //CHECK_GRADIENT_DESCENT-NEXT: void cost_grad(double theta_0, double theta_1, double x, double y, double *_d_theta_0, double *_d_theta_1, double *_d_x, double *_d_y) { //CHECK_GRADIENT_DESCENT-NEXT: double _d_f_x = 0; @@ -245,6 +237,16 @@ //CHECK_GRADIENT_DESCENT-NEXT: } //CHECK_GRADIENT_DESCENT-NEXT: } +//CHECK_GRADIENT_DESCENT: void f_pullback(double theta_0, double theta_1, double x, double _d_y, double *_d_theta_0, double *_d_theta_1, double *_d_x) { +//CHECK_GRADIENT_DESCENT-NEXT: goto _label0; +//CHECK_GRADIENT_DESCENT-NEXT: _label0: +//CHECK_GRADIENT_DESCENT-NEXT: { +//CHECK_GRADIENT_DESCENT-NEXT: *_d_theta_0 += _d_y; +//CHECK_GRADIENT_DESCENT-NEXT: *_d_theta_1 += _d_y * x; +//CHECK_GRADIENT_DESCENT-NEXT: *_d_x += theta_1 * _d_y; +//CHECK_GRADIENT_DESCENT-NEXT: } +//CHECK_GRADIENT_DESCENT-NEXT: } + //-----------------------------------------------------------------------------/ // Demo: Custom Type Numerical Diff //-----------------------------------------------------------------------------/ diff --git a/test/NestedCalls/NestedCalls.C b/test/NestedCalls/NestedCalls.C index d44560018..dafe6ba87 100644 --- a/test/NestedCalls/NestedCalls.C +++ b/test/NestedCalls/NestedCalls.C @@ -29,31 +29,7 @@ double f(double x, double y) { // CHECK-NEXT: return _d_t * y + t * _d_y; // CHECK-NEXT: } -//CHECK: void sq_pullback(double x, double _d_y, double *_d_x) { -//CHECK-NEXT: goto _label0; -//CHECK-NEXT: _label0: -//CHECK-NEXT: { -//CHECK-NEXT: *_d_x += _d_y * x; -//CHECK-NEXT: *_d_x += x * _d_y; -//CHECK-NEXT: } -//CHECK-NEXT: } - -//CHECK: void one_pullback(double x, double _d_y, double *_d_x) { -//CHECK-NEXT: goto _label0; -//CHECK-NEXT: _label0: -//CHECK-NEXT: { -//CHECK-NEXT: double _r0 = 0; -//CHECK-NEXT: sq_pullback(std::sin(x), _d_y, &_r0); -//CHECK-NEXT: double _r1 = 0; -//CHECK-NEXT: _r1 += _r0 * clad::custom_derivatives::sin_pushforward(x, 1.).pushforward; -//CHECK-NEXT: *_d_x += _r1; -//CHECK-NEXT: double _r2 = 0; -//CHECK-NEXT: sq_pullback(std::cos(x), _d_y, &_r2); -//CHECK-NEXT: double _r3 = 0; -//CHECK-NEXT: _r3 += _r2 * clad::custom_derivatives::cos_pushforward(x, 1.).pushforward; -//CHECK-NEXT: *_d_x += _r3; -//CHECK-NEXT: } -//CHECK-NEXT: } +//CHECK: void one_pullback(double x, double _d_y, double *_d_x); //CHECK: void f_grad(double x, double y, double *_d_x, double *_d_y) { //CHECK-NEXT: double _d_t = 0; @@ -92,7 +68,35 @@ int main () { // expected-no-diagnostics // CHECK-NEXT: return {_t1.value + _t3.value, _t1.pushforward + _t3.pushforward}; // CHECK-NEXT: } +//CHECK: void sq_pullback(double x, double _d_y, double *_d_x); + +//CHECK: void one_pullback(double x, double _d_y, double *_d_x) { +//CHECK-NEXT: goto _label0; +//CHECK-NEXT: _label0: +//CHECK-NEXT: { +//CHECK-NEXT: double _r0 = 0; +//CHECK-NEXT: sq_pullback(std::sin(x), _d_y, &_r0); +//CHECK-NEXT: double _r1 = 0; +//CHECK-NEXT: _r1 += _r0 * clad::custom_derivatives::sin_pushforward(x, 1.).pushforward; +//CHECK-NEXT: *_d_x += _r1; +//CHECK-NEXT: double _r2 = 0; +//CHECK-NEXT: sq_pullback(std::cos(x), _d_y, &_r2); +//CHECK-NEXT: double _r3 = 0; +//CHECK-NEXT: _r3 += _r2 * clad::custom_derivatives::cos_pushforward(x, 1.).pushforward; +//CHECK-NEXT: *_d_x += _r3; +//CHECK-NEXT: } +//CHECK-NEXT: } + // CHECK: clad::ValueAndPushforward sq_pushforward(double x, double _d_x) { // CHECK-NEXT: return {x * x, _d_x * x + x * _d_x}; // CHECK-NEXT: } + +//CHECK: void sq_pullback(double x, double _d_y, double *_d_x) { +//CHECK-NEXT: goto _label0; +//CHECK-NEXT: _label0: +//CHECK-NEXT: { +//CHECK-NEXT: *_d_x += _d_y * x; +//CHECK-NEXT: *_d_x += x * _d_y; +//CHECK-NEXT: } +//CHECK-NEXT: } }