Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Building Enzyme CMake - Undefined symbol: main #1871

Closed
jpmedinagl opened this issue May 9, 2024 · 3 comments · Fixed by #1883
Closed

Building Enzyme CMake - Undefined symbol: main #1871

jpmedinagl opened this issue May 9, 2024 · 3 comments · Fixed by #1883

Comments

@jpmedinagl
Copy link
Contributor

Hello,

My group is trying to build Enzyme. We chose an older version of LLVM (https://github.com/llvm/llvm-project/releases/tag/llvmorg-17.0.6) because the newer versions were giving us problems.
We built LLVM using:

cmake -G Ninja ../llvm -DLLVM_TARGETS_TO_BUILD="host" -DLLVM_ENABLE_ASSERTIONS=ON -DLLVM_ENABLE_PROJECTS="clang;lld"  -DLLVM_ENABLE_PLUGINS=ON -DCMAKE_BUILD_TYPE=Release -DLLVM_BUILD_LLVM_DYLIB=ON  -DLLVM_ENABLE_TERMINFO=OFF -DBUILD_SHARED_LIBS=ON
ninja

Which was successful.

Then we built Enzyme using:

cmake -G Ninja .. -DLLVM_DIR=/path/to/llvm/lib/cmake/llvm
ninja

Which was successful.

To test Enzyme we're using the example test.c file from: https://enzyme.mit.edu/getting_started/UsingEnzyme/#differentiating-cc
We're using the same cmake file as: https://github.com/EnzymeAD/Enzyme/blob/main/enzyme/test/test_find_package/CMakeLists.txt.
We decided to remove the ClangEnzymeFlags as we wanted to focus on LLDEnzyme.

We used the command

cmake -DEnzyme_DIR=/u/jpmedina/Enzyme/enzyme/build

to build the project which was successful.

However, when running make we get the following errors.

found dir /u/jpmedina/Enzyme/enzyme/build
found 1
imported targets LLVMEnzyme-17;ClangEnzyme-17;ClangEnzymeFlags;LLDEnzyme-17;LLDEnzymeFlags;LLDEnzymeAssumeUnknownNoFree;LLDEnzymeLooseTypeFlags;LLDEnzymePrintTypeFlags;LLDEnzymePrintFlags;LLDEnzymeNoStrictAliasingFlags
-- Configuring done (0.0s)
-- Generating done (0.1s)
-- Build files have been written to: /u/jpmedina/test
[ 50%] Building C object CMakeFiles/enzyme_test_2.dir/main.c.o
[100%] Linking C executable enzyme_test_2
ld.lld: error: undefined symbol: main
>>> referenced by /usr/lib/gcc/x86_64-linux-gnu/9/../../../x86_64-linux-gnu/Scrt1.o:(_start)
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/enzyme_test_2.dir/build.make:97: enzyme_test_2] Error 1
make[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/enzyme_test_2.dir/all] Error 2
make: *** [Makefile:91: all] Error 2

We believe that this is likely due to a cmake issue due to linking but we're not sure what this error means.

Any help is appreciated.

@wsmoses
Copy link
Member

wsmoses commented May 10, 2024

Hm that looks like the linker didn't find your main function for some reason. Mind sharing the whole code which fails?

@jpmedinagl
Copy link
Contributor Author

We were originally building the test files outside of the Enzyme library. However we're currently trying inside the Enzyme/enzyme/test/test_find_package folder. Trying to build the enzyme_test_2 executable (we removed the enzyme_test_1 executable due to errors with Clang). We just want to test (and compile) the main.c source file to see if __enzyme_autodiff works.

In that folder, we tried building the executable with

cmake -DEnzyme_DIR=/u/jpmedina/Enzyme/enzyme/build .

This was successful

But then when we type make
we get the same error again

[ 50%] Building C object CMakeFiles/enzyme_test_2.dir/main.c.o
[100%] Linking C executable enzyme_test_2
ld.lld: error: undefined symbol: main
>>> referenced by /usr/lib/gcc/x86_64-linux-gnu/9/../../../x86_64-linux-gnu/Scrt1.o:(_start)
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/enzyme_test_2.dir/build.make:97: enzyme_test_2] Error 1
make[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/enzyme_test_2.dir/all] Error 2
make: *** [Makefile:91: all] Error 2

For reference:
The code in /Enzyme/enzyme/test/test_find_package/CMakeLists.txt

cmake_minimum_required(VERSION 3.16)

project(enzyme_consumer LANGUAGES C)

find_package(Enzyme REQUIRED CONFIG NO_DEFAULT_PATH)
message("found dir ${Enzyme_DIR}")
message("found ${Enzyme_FOUND}")
get_property(importTargetsAfter DIRECTORY "${CMAKE_SOURCE_DIR}" PROPERTY IMPORTED_TARGETS)
message("imported targets ${importTargetsAfter}")

#add_subdirectory(${Enzyme_DIR} ${PROJECT_SOURCE_DIR}/build/enzyme)

#add_executable(enzyme_test_1 main.c)
#target_link_libraries(enzyme_test_1 PUBLIC ClangEnzymeFlags)

add_executable(enzyme_test_2 main.c)
target_compile_definitions(enzyme_test_2 PUBLIC -DNOVERSION)
target_link_libraries(enzyme_test_2 PUBLIC LLDEnzymeFlags)

The code for /Enzyme/enzyme/test/test_find_package/main.c

int printf(const char*, ...);

extern double __enzyme_autodiff(void*, double);

double square(double x) {
  return x * x;
}

double dsquare(double x) {
  return __enzyme_autodiff((void*) square, x);
}

int main() {
  for(double i=1; i<5; i++) {
    printf("%f %f\n", square(i), dsquare(i));
  }
#ifndef NOVERSION
  printf("Enzyme version %d %d %d\n", ENZYME_VERSION_MAJOR, ENZYME_VERSION_MINOR, ENZYME_VERSION_PATCH);
#endif
}

@jpmedinagl
Copy link
Contributor Author

@wsmoses

We realized that we were unintentionally using gcc instead of clang.

Would it be possible to recognize this and throw and error to the user?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants