3

I can't figure out how to get clang CUDA specific flags for only CUDA files:

# building all except 20
set(BUILD_CUDA_ARCH "30;32;35;50;52;53")

# According to the docs, we need static cudart:
# http://llvm.org/docs/CompileCudaWithLLVM.html#compiling-cuda-code
list(APPEND EXTRA_LIBS "cudart_static")

# Let clang know exactly where cuda came from
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --cuda-path=${CUDA_TOOLKIT_ROOT_DIR}")

# Add the compilation trajectories for different architectures
# go to the docs linked above
foreach(arch ${BUILD_CUDA_ARCH})
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --cuda-gpu-arch=sm_${arch}")
endforeach()

# Add the executable like normal, now that CMAKE_CXX_FLAGS has been adjusted.
# clang will compile the .cu files separately
add_library(${LIB_NAME} ${LIBRARY_TYPE} ${CXX_INC} ${CXX_SRC} ${CUDA_INC} ${CUDA_SRC})

Where elsewhere in the file is defined

  • LIB_NAME: the name of the library being built
  • LIBRARY_TYPE: either SHARED or STATIC
  • CXX_INC and CXX_SRC: pure C++ headers / source code
  • CUDA_INC and CUDA_SRC: straight CUDA code
  • EXTRA_LIBS: just used in target_link_libraries with other stuff

The problem is that this approach results in:

clang-4.0: warning: argument unused during compilation: '--cuda-gpu-arch=sm_30' [-Wunused-command-line-argument]
clang-4.0: warning: argument unused during compilation: '--cuda-gpu-arch=sm_32' [-Wunused-command-line-argument]
clang-4.0: warning: argument unused during compilation: '--cuda-gpu-arch=sm_35' [-Wunused-command-line-argument]
clang-4.0: warning: argument unused during compilation: '--cuda-gpu-arch=sm_50' [-Wunused-command-line-argument]
clang-4.0: warning: argument unused during compilation: '--cuda-gpu-arch=sm_52' [-Wunused-command-line-argument]
clang-4.0: warning: argument unused during compilation: '--cuda-gpu-arch=sm_53' [-Wunused-command-line-argument]

warnings for every non-cuda compilation. Do I have to separate this out into two separate libraries, or is there some way of telling clang? The linked docs in the comments seem to indicate that it should only be doing cuda stuff with .cu files, but I can't seem to find specific flags for only that mode. It feels awkward to split it into two libraries with how things are split up logically - the CPU version includes different files that effectively do the same thing w/o the gpu.

svenevs
  • 833
  • 9
  • 24

2 Answers2

3

You should add the CUDA compilation flags as cmake compilation properties only for CUDA source files, rather than adding them unilaterally and then trying to suppress them selectively.

mabraham
  • 2,806
  • 3
  • 28
  • 25
  • Lol DUH that's way better, since I already have a list of which files need to be setup for CUDA, only add the CUDA flags to them! – svenevs Sep 07 '17 at 23:08
0

There is a way to ignore it as shown in this answer

# Squelch warnings about cuda flags for C++ files
foreach(cxx ${CXX_SRC} ${CXX_INC})
  set_source_files_properties(${cxx} PROPERTIES COMPILE_FLAGS -Wno-unused-command-line-argument)
endforeach()

I don't think it's appropriate to give a blanket -Wno-unused-command-line-argument, but it seems likely separate treatment of flags is not in llvm.

svenevs
  • 833
  • 9
  • 24