Derivative through a view into a Simbody matrix fails #2153
Description
The following example tries to take the derivative of function which sums elements of a Simbody Matrix
accessed through a sub-vector of a view into a column of the matrix (i.e., a VectorView
). This example was built with LLVM 18 with and Enzyme v0.0.157.
This example is part of my Simbody-Enzyme testing repo here. Simbody is compiled with static libraries and LTO enabled. The executable is compiled with LLDEnzymeFlags
and LLDEnzymePrintFlags
.
#include <cstdio>
#include "Simbody.h"
using namespace SimTK;
extern int enzyme_dup;
template <typename return_type, typename... T>
return_type __enzyme_fwddiff(void *, T...);
Real foo(const Matrix& mat) {
// Grab the second column as a VectorView and from that view,
// grab a subvector (another VectorView) from the second element
// to the third element. Finally, sum the elements of the subvector.
VectorView vec = mat.col(1)(1, 2);
return vec(0) + vec(1);
}
int main () {
Matrix mat = Test::randMatrix(4, 4);
Matrix dmat(4, 4, 0.0);
dmat(1, 1) = 1.0;
dmat(1, 2) = 1.0;
Real dfoo = __enzyme_fwddiff<Real>((void*)foo,
enzyme_dup, &mat, &dmat);
return 0;
}
Building this example fails during type analysis with the following error message:
[build] ld.lld: error: dependencies/install/simbody/include/simbody/SimTKcommon/internal/BigMatrix.h:217:26: in function preprocess__Z3fooRKN5SimTK7Matrix_IdEE double (ptr): Enzyme: Cannot deduce type of copy call void @llvm.memcpy.p0.p0.i64(ptr align 8 %45, ptr align 4 %3, i64 20, i1 false) #30, !dbg !14589, !tbaa.struct !14493, !noalias !14586
This error points to this line in Simbody, at the construction of a "MatrixHelper" class used here to create a modifiable view into a block of the original matrix (presumably the same sub-vector as above). I can provide more output around the build error above for context if requested.
Adding LLDEnzymeLooseTypeFlags
allows the executable to compile, but upon running I receive the error message "Attempting to call an indirect active function whose runtime value is inactive". Replacing foo with:
Real bar(const Matrix& mat) {
// Grab and sum the same elements as in foo(), but directly.
return mat.getElt(1, 1) + mat.getElt(1, 2);
}
compiles without LLDEnzymeLooseTypeFlags
, but also produces "Attempting to call an indirect active function whose runtime value is inactive".
Activity