-
Notifications
You must be signed in to change notification settings - Fork 145
Description
I encounter an issue in ImpactX when differentiating through functions with return types of value std::unordered_map<std::string, double>.
I use those dictionary style types to return named tuples of ~20 values in certain physics (analysis) routines, usually then optimizing on a select few parameters in them. Unfortunately, I get some LLVM error that I do not (yet) know how to read (error: Enzyme: Cannot deduce type of copy call) .
Here is the full reproducer:
https://fwd.gymni.ch/DTQR3r
#include <cmath>
#include <string>
#include <unordered_map>
std::unordered_map<std::string, double>
reduced_beam_characteristics (double x, double y)
{
std::unordered_map<std::string, double> data;
data["alpha_x"] = x*x + y;
data["alpha_y"] = y*y + 2.;
return data;
}
double compute (double r)
{
double x = std::sqrt(r);
double y = std::log(r);
std::unordered_map<std::string, double> const rbc =
reduced_beam_characteristics(x, y);
return rbc.at("alpha_x");
}
double __enzyme_autodiff(void*, double);
int main()
{
double q1_k = 3.0;
// normal
double const alpha_x = compute(q1_k);
// diff (fails)
double ddx = __enzyme_autodiff((void*) compute, q1_k);
}Tried Alternative 1
I also tried rewriting to explicit:
double val = rbc.at("alpha_x");
return val;which also fails to build.
Tried Alternative 2
I can rewrite to return a simpler data structure (e.g., a double) from reduced_beam_characteristics, but that does not work well for my needs here.
Tried Alternative 3
Adding -mllvm -enzyme-loose-types to my command line makes it compile, but likely to wrong code as it emits:
freeing without malloc %50 = load ptr, ptr %0, align 8, !dbg !3703, !tbaa !3709
freeing without malloc %16 = load ptr, ptr %4, align 8, !dbg !3713, !tbaa !3714
freeing without malloc %25 = load ptr, ptr %5, align 8, !dbg !3797, !tbaa !3714
freeing without malloc %50 = load ptr, ptr %0, align 8, !dbg !3703, !tbaa !3709
freeing without malloc %16 = load ptr, ptr %4, align 8, !dbg !3713, !tbaa !3714
freeing without malloc %25 = load ptr, ptr %5, align 8, !dbg !3797, !tbaa !3714
freeing without malloc %15 = load ptr, ptr %3, align 8, !dbg !3696, !tbaa !3697
freeing without malloc %29 = load ptr, ptr %28, align 8, !dbg !3861, !tbaa !3697
freeing without malloc %25 = phi ptr [ %27, %33 ], [ %20, %22 ]
freeing without malloc %41 = load ptr, ptr %2, align 8, !dbg !3941, !tbaa !3930
ASM generation compiler returned: 0
freeing without malloc %50 = load ptr, ptr %0, align 8, !dbg !3703, !tbaa !3709
freeing without malloc %16 = load ptr, ptr %4, align 8, !dbg !3713, !tbaa !3714
freeing without malloc %25 = load ptr, ptr %5, align 8, !dbg !3797, !tbaa !3714
freeing without malloc %50 = load ptr, ptr %0, align 8, !dbg !3703, !tbaa !3709
freeing without malloc %16 = load ptr, ptr %4, align 8, !dbg !3713, !tbaa !3714
freeing without malloc %25 = load ptr, ptr %5, align 8, !dbg !3797, !tbaa !3714
freeing without malloc %15 = load ptr, ptr %3, align 8, !dbg !3696, !tbaa !3697
freeing without malloc %29 = load ptr, ptr %28, align 8, !dbg !3861, !tbaa !3697
freeing without malloc %25 = phi ptr [ %27, %33 ], [ %20, %22 ]
freeing without malloc %41 = load ptr, ptr %2, align 8, !dbg !3941, !tbaa !3930
Execution build compiler returned: 0
Program returned: 0