EnzymeAD / EnzymeAD/Enzyme

[Bug] `std::list` Insertion

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

Description

In [ImpactX](https://github.com/BLAST-ImpactX/impactx/pull/825), we have a central data structure to represent a beamline of a particle accelerator in form of a `std::list`, that we iterate over in the most outer loop of a simulation.

Enzyme currently has issues when list inserts are on the path of differentiation. Here is a minimal reproducer:

https://fwd.gymni.ch/BnMxGB
```C++
#include
#include

inline int enzyme_dup;
inline int enzyme_const;

template
return_type __enzyme_autodiff (void*, T ...);

struct S { double v; };

struct P {
std::list m_list;
};

double compute (P * p, double r)
{
p->m_list.push_back(S{r});

double res = 0.0;

for (auto & element : p->m_list)
{
res += element.v;
}

return res;
}

int main()
{
P p{};

double q1_k = -3.0;
double dq1_k = 1.0;

// normal
double const alpha_x = compute(&p, q1_k);

// diff (fails)
double ddx = __enzyme_autodiff(
(void*) compute,
enzyme_const, &p,
enzyme_dup, q1_k, dq1_k);

}
```

This fails with:
```
In file included from :2:
In file included from /usr/lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/list:63:
/usr/lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/bits/stl_list.h:1913:10: error: Enzyme: No augmented forward pass found for _ZNSt8__detail15_List_node_base7_M_hookEPS0_(std::__detail::_List_node_base::_M_hook(std::__detail::_List_node_base*))
at context: tail call void @_ZNSt8__detail15_List_node_base7_M_hookEPS0_(ptr noundef nonnull align 8 dereferenceable(16) %3, ptr noundef nonnull %0) #13, !dbg !1142
__tmp->_M_hook(__position._M_node);
^
/usr/lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/bits/stl_list.h:1913:10: error: Enzyme: No reverse pass found for _ZNSt8__detail15_List_node_base7_M_hookEPS0_
at context: tail call void @_ZNSt8__detail15_List_node_base7_M_hookEPS0_(ptr noundef nonnull align 8 dereferenceable(16) %3, ptr noundef nonnull %0) #14, !dbg !1142
2 errors generated.
Compiler returned: 1
```

I am not sure if the right way is to simply mark this as inactive or if this actually can influence results? We definitely store our configuration in that list, which usually depends on the to-be-differentiated parameters, as in the example above.

### More Background & Links

A central data type of ours is a:
```C++
std::list>
```
that we iterate over in an outer loop to represent a beamline of a particle accelerator.
- https://github.com/BLAST-ImpactX/impactx/blob/25.06/src/ImpactX.H#L141
- https://github.com/BLAST-ImpactX/impactx/blob/25.06/src/elements/All.H#L54

The issue already appears with `std::list`, even w/o a variant.

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.