EnzymeAD / EnzymeAD/Enzyme

`enzyme::autodiff` compile error when a callable struct is passed

Open
#2,485 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
LLVM
Stars
1.7k
Forks
188
Avg merge
2d 4h
Merged PRs (30d)
22

Description

Hi, if I try the following code:

[Enzyme Explorer link](https://fwd.gymni.ch/g8plpr)

``` c++
#include
#include

struct objective {
double operator()(double x) { return x * 10; }
};

int main() {
// ok
// auto y = enzyme::autodiff(objective{}, enzyme::Active(3.1));

// not ok
objective fn;
auto y = enzyme::autodiff(fn, enzyme::Active(3.1));

auto y1 = enzyme::get<0>(enzyme::get<0>(y));
std::printf("dmul %f\n", y1);
}
```
It results in a compiler error:
```
error: 'f' declared as a pointer to a reference of type 'objective &'
```

I believe this could (hopefully) be fixed by changing to something like:
``` c++
template
struct templated_call {
static RT wrap(std::remove_reference_t* __restrict__ f, T... args) {
return (*f)(args...);
}
};
```
in this: https://github.com/EnzymeAD/Enzyme/blob/19612937cdd073991336fe6bb087f5be262b6543/enzyme/include/enzyme/utils#L330-L335

I guess in the tests only the `// ok` part of the code is tested, see e.g. https://github.com/EnzymeAD/Enzyme/blob/19612937cdd073991336fe6bb087f5be262b6543/enzyme/test/Integration/ReverseMode/sugar.cpp#L92
I can set up a pull request, or if someone who knows a better fix or is more familiar with the code (I am not really familiar at all) perhaps they could fix accordingly and I would be very happy :)

Thank you.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.