Skip to content

cmake: fix clang build when CUDA is enabled (#4208) #4323

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

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -399,13 +399,17 @@ endif()

if (LLAMA_ALL_WARNINGS)
if (NOT MSVC)
# warning_flags is common to C and C++ but not CUDA
set(warning_flags -Wall -Wextra -Wpedantic -Wcast-qual -Wno-unused-function)
set(c_flags -Wshadow -Wstrict-prototypes -Wpointer-arith -Wmissing-prototypes -Werror=implicit-int -Werror=implicit-function-declaration)
set(cxx_flags -Wmissing-declarations -Wmissing-noreturn)
set(host_cxx_flags "")
# host_cxx_flags gets passed to nvcc after '-Xcompiler', i.e., it then gets sent to gcc (which is the default
# preprocessor for nvcc, even when clang would be used .c/.cpp files)
set(host_cxx_flags -Wmissing-declarations -Wmissing-noreturn)

if (CMAKE_C_COMPILER_ID MATCHES "Clang")
set(warning_flags ${warning_flags} -Wunreachable-code-break -Wunreachable-code-return)
set(c_flags ${c_flags} -Wunreachable-code-break -Wunreachable-code-return)
# cxx_flags are used for C++ files but not for CUDA
set(cxx_flags -Wunreachable-code-break -Wunreachable-code-return)
Comment on lines -408 to +412
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, I think we should be passing cxx_flags to nvcc as in the Makefile - that's how I intended it. We probably shouldn't pass host_cxx_flags to nvcc at all, since we can't assume that it uses the same compiler as the host. We could possibly use -Xcompiler for cxx_flags, not sure if there's any benefit.

So I think warning_flags should be split into c_flags and host_cxx_flags.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's a valid point that it would be nice if the Makefile and CMakeLists.txt would have somewhat similar patterns and variable names. Anyway, now that I checked, it it seems that also the Makefile fails in building CUDA enabled version with clang.
E.g.:

export CXX=/usr/bin/clang++
export CC=/usr/bin/clang
make LLAMA_CUBLAS=1

...
/usr/bin/clang++ -I. -Icommon -D_XOPEN_SOURCE=600 -D_GNU_SOURCE -DNDEBUG -DGGML_USE_CUBLAS -I/usr/local/cuda/include -I/opt/cuda/include -I/targets/x86_64-linux/include  -std=c++11 -fPIC -O3 -Wall -Wextra -Wpedantic -Wcast-qual -Wno-unused-function -Wmissing-declarations -Wmissing-noreturn -pthread  -Wunreachable-code-break -Wunreachable-code-return -Wmissing-prototypes -Wextra-semi -march=native -mtune=native  -c common/build-info.cpp -o build-info.o
nvcc --forward-unknown-to-host-compiler -use_fast_math -arch=native -DGGML_CUDA_DMMV_X=32 -DGGML_CUDA_MMV_Y=1 -DK_QUANTS_PER_ITERATION=2 -DGGML_CUDA_PEER_MAX_BATCH_SIZE=128 -I. -Icommon -D_XOPEN_SOURCE=600 -D_GNU_SOURCE -DNDEBUG -DGGML_USE_CUBLAS -I/usr/local/cuda/include -I/opt/cuda/include -I/targets/x86_64-linux/include  -std=c++11 -fPIC -O3 -Wall -Wextra -Wpedantic -Wcast-qual -Wno-unused-function -Wmissing-declarations -Wmissing-noreturn -pthread    -Wno-pedantic -Xcompiler "-Wunreachable-code-break -Wunreachable-code-return -Wmissing-prototypes -Wextra-semi -march=native -mtune=native " -c ggml-cuda.cu -o ggml-cuda.o
gcc: error: unrecognized command-line option '-Wunreachable-code-break'; did you mean '-Wunreachable-code'?
gcc: error: unrecognized command-line option '-Wunreachable-code-return'; did you mean '-Wunreachable-code'?
make: *** [Makefile:451: ggml-cuda.o] Error 1

To sum up, I think the parameters get set correctly using this PR. But perhaps the variable names could be clarified, and possible there could be some changes in the way they get set.
Then the Makefile should probably have similar changes.

set(host_cxx_flags ${host_cxx_flags} -Wmissing-prototypes -Wextra-semi)

if (
Expand Down Expand Up @@ -440,7 +444,7 @@ endif()
if (NOT MSVC)
set(cuda_flags -Wno-pedantic)
endif()
set(cuda_flags ${cxx_flags} -use_fast_math ${cuda_flags})
set(cuda_flags -use_fast_math ${cuda_flags})

list(JOIN host_cxx_flags " " cuda_host_flags) # pass host compiler flags as a single argument
if (NOT cuda_host_flags STREQUAL "")
Expand Down