Enzyme
Enzyme copied to clipboard
Metadata Flags Break in CMake Projects Containing `cmake_minimum_required`
Tested with clang
version 15.0.7
on Ubuntu.
The main.cpp
:
#include <iostream>
#include <math.h>
#include <tuple>
int enzyme_dup;
int enzyme_dupnoneed;
int enzyme_out;
int enzyme_const;
template <typename return_type, typename... T>
return_type __enzyme_fwddiff(void *, T...);
template <typename return_type, typename... T>
return_type __enzyme_autodiff(void *, T...);
double logpdf(double* x, double* mu, double* std) {
double z = (*x - *mu) / *std;
return -(pow(z, 2) + log(M_PI*2.))/2. - log(*std);
}
std::tuple<double,double,double> grad_logpdf(
double x,
double mu,
double std
) {
double diffs[3] = { 0.0 };
__enzyme_autodiff<void>(
(void*)logpdf,
enzyme_dup, &x, &diffs[0],
enzyme_dup, &mu, &diffs[1],
enzyme_dup, &std, &diffs[2]
);
return std::make_tuple(diffs[0], diffs[1], diffs[2]);
}
int main() {
double x = 0.5;
double mu = 0.0;
double std = 1.0;
auto logprob = logpdf(&x, &mu, &std);
printf("logprob = %f\n", logprob);
auto [a, b, c] = grad_logpdf(x, mu, std);
printf("dlogpdf/dx[i] = [%f, %f, %f]\n", a, b, c);
return 0;
}
The CMakeLists.txt
:
cmake_minimum_required(VERSION 3.25.1)
project(BuggedCMake)
message("CMake Version: ${CMAKE_VERSION}")
set(CMAKE_C_COMPILER "${Enzyme_LLVM_BINARY_DIR}/bin/clang")
set(CMAKE_CXX_COMPILER "${Enzyme_LLVM_BINARY_DIR}/bin/clang++")
find_package(Enzyme REQUIRED CONFIG)
add_executable(test)
target_sources(test PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/main.cpp
)
target_link_libraries(test PUBLIC LLDEnzymeFlags)
Output:
austingarrett:build $ cmake -DEnzyme_DIR=$PWD/../../extern/Enzyme/enzyme/build .. && make && ./test
CMake Version: 3.25.1
-- Configuring done
-- Generating done
-- Build files have been written to: /home/austin-garrett/src/Enzyme-Bug/working/build
[ 50%] Building CXX object CMakeFiles/test.dir/main.cpp.o
/home/austin-garrett/src/Enzyme-Bug/working/main.cpp:44:8: warning: decomposition declarations are a C++17 extension [-Wc++17-extensions]
auto [a, b, c] = grad_logpdf(x, mu, std);
^~~~~~~~~
1 warning generated.
[100%] Linking CXX executable test
ld.lld: error: <unknown>:0:0: in function _Z11grad_logpdfddd void (ptr, double, double, double): Enzyme: Cannot cast __enzyme_autodiff primal argument 1, found i32 0, type i32 - to arg 0 ptr
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [CMakeFiles/test.dir/build.make:97: test] Error 1
make[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/test.dir/all] Error 2
make: *** [Makefile:91: all] Error 2
Removing cmake_minimum_required(VERSION 3.25.1)
gives the following output:
austingarrett:build ⟩ cmake -DEnzyme_DIR=$PWD/../../extern/Enzyme/enzyme/build .. && make && ./test
CMake Version: 3.25.1
CMake Warning (dev) in CMakeLists.txt:
No cmake_minimum_required command is present. A line of code such as
cmake_minimum_required(VERSION 3.25)
should be added at the top of the file. The version specified may be lower
if you wish to support older CMake versions for this project. For more
information run "cmake --help-policy CMP0000".
This warning is for project developers. Use -Wno-dev to suppress it.
-- Configuring done
-- Generating done
-- Build files have been written to: /home/austin-garrett/src/Enzyme-Bug/working/build
[ 50%] Building CXX object CMakeFiles/test.dir/main.o
/home/austin-garrett/src/Enzyme-Bug/working/main.cpp:44:8: warning: decomposition declarations are a C++17 extension [-Wc++17-extensions]
auto [a, b, c] = grad_logpdf(x, mu, std);
^~~~~~~~~
1 warning generated.
[100%] Linking CXX executable test
[100%] Built target test
logprob = -1.043939
dlogprob/dx[i] = [-0.500000, 0.500000, -0.750000]
Note it's still CMake
version 3.25.1
, which leads me to think it's the directive itself causing issues.