Skip to content

[Bug] std::list Insertion #2369

@ax3l

Description

@ax3l

In ImpactX, 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

#include <cmath>
#include <list>


inline int enzyme_dup;
inline int enzyme_const;

template <typename return_type, typename ... T>
return_type __enzyme_autodiff (void*, T ...);


struct S { double v; };

struct P {
    std::list<S> 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<double>(
        (void*) compute,
        enzyme_const, &p,
        enzyme_dup, q1_k, dq1_k);

}

This fails with:

In file included from <source>: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:

std::list<std::variant<...>>

that we iterate over in an outer loop to represent a beamline of a particle accelerator.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions