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

Linking Issues #484

Closed
pvelesko opened this issue Sep 22, 2023 · 7 comments
Closed

Linking Issues #484

pvelesko opened this issue Sep 22, 2023 · 7 comments

Comments

@pvelesko
Copy link

Using clang17

[ 98%] Linking CXX shared library libopencl-clang.so
/usr/bin/ld: CMakeFiles/opencl-clang.dir/opencl_clang.cpp.o: in function `llvm::InitializeAllTargetInfos()':
/opt/install/llvm/16.0/include/llvm/Config/Targets.def:26: undefined reference to `LLVMInitializeX86TargetInfo'
/usr/bin/ld: CMakeFiles/opencl-clang.dir/opencl_clang.cpp.o: in function `llvm::InitializeAllTargets()':
/opt/install/llvm/16.0/include/llvm/Config/Targets.def:26: undefined reference to `LLVMInitializeX86Target'
/usr/bin/ld: CMakeFiles/opencl-clang.dir/opencl_clang.cpp.o: in function `llvm::InitializeAllAsmPrinters()':
/opt/install/llvm/16.0/include/llvm/Config/AsmPrinters.def:27: undefined reference to `LLVMInitializeX86AsmPrinter'
/usr/bin/ld: CMakeFiles/opencl-clang.dir/opencl_clang.cpp.o: in function `llvm::InitializeAllAsmParsers()':
/opt/install/llvm/16.0/include/llvm/Config/AsmParsers.def:27: undefined reference to `LLVMInitializeX86AsmParser'
/usr/bin/ld: CMakeFiles/opencl-clang.dir/opencl_clang.cpp.o: in function `llvm::InitializeAllTargetMCs()':
/opt/install/llvm/16.0/include/llvm/Config/Targets.def:26: undefined reference to `LLVMInitializeX86TargetMC'
/usr/bin/ld: CMakeFiles/opencl-clang.dir/opencl_clang.cpp.o: in function `Compile':
/home/pvelesko/opencl-clang/opencl_clang.cpp:339: undefined reference to `llvm::writeSpirv(llvm::Module*, SPIRV::TranslatorOpts const&, std::ostream&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&)'
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/opencl-clang.dir/build.make:223: libopencl-clang.so.16] Error 1
make[1]: *** [CMakeFiles/Makefile2:258: CMakeFiles/opencl-clang.dir/all] Error 2
make: *** [Makefile:136: all] Error 2

Using clang16

[ 98%] Linking CXX shared library libopencl-clang.so
/usr/bin/ld: CMakeFiles/opencl-clang.dir/opencl_clang.cpp.o: in function `Compile':
/home/pvelesko/opencl-clang/opencl_clang.cpp:321: undefined reference to `llvm::writeSpirv(llvm::Module*, SPIRV::TranslatorOpts const&, std::ostream&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&)'
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/opencl-clang.dir/build.make:224: libopencl-clang.so.17rc] Error 1
make[1]: *** [CMakeFiles/Makefile2:258: CMakeFiles/opencl-clang.dir/all] Error 2
make: *** [Makefile:136: all] Error 2
@pvelesko
Copy link
Author

I had to recompile LLVM to use dynamic linking (not default) which fixed the issue. Maybe a note somewhere in the readme?

@wenju-he
Copy link
Contributor

@pvelesko could you please provide build step for the above failure so that we can reproduce?
Are you using out-of-tree build, i.e. build LLVM and SPIR-V Translator first and then build opencl-clang?

@pvelesko
Copy link
Author

Using a pre-built LLVM + SPIR-V Translator, then try for opencl-clang. Here is the script used to configure LLVM:

#!/bin/bash

# check arguments
if [ $# -ne 2 ]; then
  echo "Usage: $0 <version> <install_dir>"
  echo "version: LLVM version 15, 16, 17"
  exit 1
fi

# if an error is enountered, exit
set -e

VERSION=$1
INSTALL_DIR=$2
export LLVM_DIR=`pwd`/llvm-project/llvm

# check if llvm-project exists, if not clone it
if [ ! -d llvm-project ]; then
  git clone git@github.com:CHIP-SPV/llvm-project.git -b chipspv-llvm-${VERSION}-patches --depth 1
  cd ${LLVM_DIR}/projects
  git clone git@github.com:CHIP-SPV/SPIRV-LLVM-Translator.git -b chipspv-llvm-${VERSION}-patches --depth 1
  cd ${LLVM_DIR}
else
  # Warn the user, error out
  echo "llvm-project directory already exists. Assuming it's cloned from chipStar."
  cd ${LLVM_DIR}
  # check if already on the desired branch 
  if [ `git branch --show-current` == "chipspv-llvm-${VERSION}-patches" ]; then
    echo "Already on branch chipspv-llvm-${VERSION}-patches"
  else
    echo "Switching to branch chipspv-llvm-${VERSION}-patches"
    git br -D chipspv-llvm-${VERSION}-patches &> /dev/null
    git fetch origin chipspv-llvm-${VERSION}-patches:chipspv-llvm-${VERSION}-patches
    git checkout chipspv-llvm-${VERSION}-patches
  fi
  cd ${LLVM_DIR}/projects/SPIRV-LLVM-Translator
  # check if already on the desired branch
  if [ `git branch --show-current` == "chipspv-llvm-${VERSION}-patches" ]; then
    echo "Already on branch chipspv-llvm-${VERSION}-patches"
  else
    echo "Switching to branch chipspv-llvm-${VERSION}-patches"
    git br -D chipspv-llvm-${VERSION}-patches &> /dev/null
    git fetch origin chipspv-llvm-${VERSION}-patches:chipspv-llvm-${VERSION}-patches
    git checkout chipspv-llvm-${VERSION}-patches
  fi
  cd ${LLVM_DIR}
fi

# check if the build directory exists, if not create it
if [ ! -d build_$VERSION ]; then
  mkdir build_$VERSION
  cd build_$VERSION
else
  # Warn the user, error out
  echo "Build directory build_$VERSION already exists, please remove it and re-run the script"
  exit 1
fi

cmake ../  \
  -DCMAKE_INSTALL_PREFIX=${INSTALL_DIR} \
  -DCMAKE_BUILD_TYPE=Release \
  -DLLVM_ENABLE_PROJECTS="clang;openmp" \
  -DLLVM_TARGETS_TO_BUILD=X86

@wenju-he
Copy link
Contributor

I'll take a look.

@wenju-he
Copy link
Contributor

wenju-he commented Sep 26, 2023

On container ubuntu 22.04 and 16 branch I reproduced the same error if llvm and llvm-spirv is built to static libraries:

[ 98%] Linking CXX shared library libopencl-clang.so
/usr/bin/ld: CMakeFiles/opencl-clang.dir/opencl_clang.cpp.o: in function `Compile':
opencl_clang.cpp:(.text+0x14c4): undefined reference to `llvm::writeSpirv(llvm::Module*, SPIRV::TranslatorOpts const&, std::ostream&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&)'
/usr/bin/ld: CMakeFiles/opencl-clang.dir/opencl_clang.cpp.o: in function `llvm::InitializeAllTargetInfos()':
opencl_clang.cpp:(.text._ZN4llvm24InitializeAllTargetInfosEv[_ZN4llvm24InitializeAllTargetInfosEv]+0x9): undefined reference to `LLVMInitializeX86TargetInfo'
/usr/bin/ld: CMakeFiles/opencl-clang.dir/opencl_clang.cpp.o: in function `llvm::InitializeAllTargets()':
opencl_clang.cpp:(.text._ZN4llvm20InitializeAllTargetsEv[_ZN4llvm20InitializeAllTargetsEv]+0xe): undefined reference to `LLVMInitializeX86Target'
/usr/bin/ld: CMakeFiles/opencl-clang.dir/opencl_clang.cpp.o: in function `llvm::InitializeAllTargetMCs()':
opencl_clang.cpp:(.text._ZN4llvm22InitializeAllTargetMCsEv[_ZN4llvm22InitializeAllTargetMCsEv]+0x9): undefined reference to `LLVMInitializeX86TargetMC'
/usr/bin/ld: CMakeFiles/opencl-clang.dir/opencl_clang.cpp.o: in function `llvm::InitializeAllAsmPrinters()':
opencl_clang.cpp:(.text._ZN4llvm24InitializeAllAsmPrintersEv[_ZN4llvm24InitializeAllAsmPrintersEv]+0x9): undefined reference to `LLVMInitializeX86AsmPrinter'
/usr/bin/ld: CMakeFiles/opencl-clang.dir/opencl_clang.cpp.o: in function `llvm::InitializeAllAsmParsers()':
opencl_clang.cpp:(.text._ZN4llvm23InitializeAllAsmParsersEv[_ZN4llvm23InitializeAllAsmParsersEv]+0x9): undefined reference to `LLVMInitializeX86AsmParser'
collect2: error: ld returned 1 exit status
gmake[2]: *** [CMakeFiles/opencl-clang.dir/build.make:223: libopencl-clang.so.16] Error 1
gmake[1]: *** [CMakeFiles/Makefile2:258: CMakeFiles/opencl-clang.dir/all] Error 2
gmake: *** [Makefile:136: all] Error 2

@wenju-he
Copy link
Contributor

The above build fail could be fixed using llvm-config, same as in #454 (comment)

execute_process(COMMAND llvm-config --libnames all OUTPUT_VARIABLE ALL_LIBS)
string(REPLACE "lib" "" ALL_LIBS1 ${ALL_LIBS})
string(REPLACE ".a" "" ALL_LIBS2 ${ALL_LIBS1})
string(STRIP ${ALL_LIBS2} ALL_LIBS3)
string(REPLACE " " ";" ALL_LIBS4 ${ALL_LIBS3})
list(APPEND OPENCL_CLANG_LINK_LIBS ${ALL_LLVM_LIBS} ${ALL_LIBS4} LLVMSPIRVLib)

@wenju-he
Copy link
Contributor

Propose a fix PR to use llvm-config: #490

wenju-he added a commit to wenju-he/opencl-clang that referenced this issue Oct 7, 2023
… or shared

llvm_map_components_to_libnames is empty in these cases.
This fixes link error in Ubuntu 22.04 container. intel#484 and intel#454
wenju-he added a commit that referenced this issue Oct 8, 2023
… or shared (#493)

llvm_map_components_to_libnames is empty in these cases.
This fixes link error in Ubuntu 22.04 container. #484 and #454
wenju-he added a commit that referenced this issue Oct 8, 2023
… or shared (#494)

llvm_map_components_to_libnames is empty in these cases.
This fixes link error in Ubuntu 22.04 container. #484 and #454
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

No branches or pull requests

2 participants