C++ autodiff modifying const objects
- Dominant language
- LLVM
- Stars
- 1.7k
- Forks
- 188
- Avg merge
- 1d 22h
- Merged PRs (30d)
- 26
Description
Following on from #2994 and possibly related to #2914:
```C++
#include
#include
extern int enzyme_out;
extern int enzyme_const;
template < typename return_type, typename ... T >
return_type __enzyme_autodiff(void*, T ... );
struct DataStructure
{
const std::vector m_vec{};
DataStructure(const std::vector& V): m_vec{V} {};
double multiplyAndSumBy(double num) const;
};
double DataStructure::multiplyAndSumBy(double num) const
{
double sum{0};
for (double element: m_vec)
sum += num*element;
return sum;
}
double calculate(double num, const DataStructure& DS) {return DS.multiplyAndSumBy(num);}
int main()
{
const DataStructure DS{{1, 2, 3}};
const DataStructure DSCopy{DS};
double num{2};
double grad{
__enzyme_autodiff((void*)calculate, enzyme_out, num, enzyme_const, &DS)
};
assert(DS.m_vec==DSCopy.m_vec && "const object mutated!");
return 0;
}
```
Looks like enzyme is definitely modifying const objects passed to differentiated functions.
If there is anything i can do to help, I really like this library and would love to start contributing.
Contributor guide
Research direction
Start by compiling and running the provided C++ reproducer, focusing on the __enzyme_autodiff call with enzyme_const and the calculate entry point. Trace how the const DataStructure argument is handled during differentiation; done means the final assertion passes without modifying DS.m_vec.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100