vgvassilev / clad

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

Clad is not building the nested name qualifiers correctly #1050

Open gojakuch opened 2 months ago

gojakuch commented 2 months ago

There's a plenty of examples in Kokkos unittests, for instance. Using Clad with the generate-source-file option thus produces invalid code with some nested name qualifiers missing.

Here's a reproducer:

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

namespace TN {
    struct Test {
        static int multiplier;
    };
    int Test::multiplier = 3;
}

double fn(double x) {
    return TN::Test::multiplier*x;
}

int main() {
  auto df = clad::differentiate(fn, "x");
  std::cout << "f(3)=" << fn(3.0);
  std::cout << "; df(3)=" << df.execute(3.0) << '\n';
  return 0;
}

In the generated code, the TN::Test::multiplier is just used as multiplier which isn't correct.

gojakuch commented 1 month ago

this was partially solved in #1088. we now support namespace names in the nested name chain, but nothing else, so the reproducer still produces invalid code. an example of what now works can be found in the ValidCodeGen tests in clad.

vgvassilev commented 1 month ago

Any clue how to support this?

gojakuch commented 1 month ago

well, I'd been trying to solve this issue fully in #1088 but as we saw to little avail, since the debug build kept crushing. I tried a bunch of different approaches and none worked for the debug build. but there should be a way. my guess is that we need to somehow use source locations properly (since these nested names seem to be built by using type locations and other locations), but there's a chance that I overlooked something because of working on it for too long.

vgvassilev commented 1 month ago

Can you give me a small standalone reproducer to take a look?

gojakuch commented 1 month ago

the reproducer in the issue text is still relevant