vgvassilev / clad

clad -- automatic differentiation for C/C++
GNU Lesser General Public License v3.0
280 stars 123 forks source link

Clad fails to differentiate functor call expressions on debug build #957

Open gojakuch opened 3 months ago

gojakuch commented 3 months ago

I spotted this issue when writing tests for my PR #955. the debug build check kept crashing when trying to differentiate a new test I'd written. when I tried to replicate the issue on my machine to debug and see what I did wrong in the PR, I noticed to my surprise that the same crash persists for functors and not only for lambda functions, so this was not my code's fault but another issue. running the following code with the debug build of llvm/clang and clad results into a crash:

#include "clad/Differentiator/Differentiator.h"
#include <iostream>

struct F {
    double operator()(double t, double k) {
    return t + k;
  }
};

double func(double i, double j) {
  F _f;
  double x = _f(i + j + _f(j, i), i);
  return x;
}

int main() {
  auto func_grad = clad::gradient(func);
  double di = 0, dj = 0;
  std::cout << "func(3,4) = " << func(3, 4) << '\n';
  func_grad.execute(3., 4., &di, &dj);
  std::cout << "gradient at (3,4): (" << di << ", " << dj << ")\n";
  return 0;
}

the message I get when it crashes is the following: debug-issue.txt

again, running this code with a normal build of clang+clad seems to work fine on all setups, producing valid code and no errors at all. this only crashes with the debug build (clang-16 in my case).