CUDA: using Microsoft compiler with Clang is not working on Windows
I am trying to replace nvcc as CUDA compiler on Windows by clang. I am using one of the latest cmake nightly builds (cmake-3.28.20240130-g 83db6b30) as there were a couple of improvements in this area lately. It is nice to see, that most of the compiler combination simply work out of box now:
- host:
msvc
cuda:nvcc
-> works - host:
clang
cuda:nvcc
-> works - host:
clang
cuda:clang
-> works - host:
msvc
cuda:clang
-> fails
From the log output it seems as msvc compiler options are integrated into the clang command line. This looks like the problem, which was mentioned in !4888 (closed). Also #21934 could be related. Is this supposed to work or am I doing anything wrong here?
I have prepared a small CMake test project to play with the different compiler options. The toolchain looks as follows:
Click to expand
set (ENABLE_CLANG_FOR_HOST OFF)
set (ENABLE_CLANG_FOR_CUDA ON)
set (clang_ROOT "C:/3rd/clang-windows-msvc-17.6-amd64-release.16.0.1-z2")
#------------------------------------------------------------------------------
# Setup Visual Studio
set (VCRoot "C:/Program Files/Microsoft Visual Studio/2022/17.6/VC/Tools/MSVC/14.36.32532")
if(ENABLE_CLANG_FOR_HOST)
set (CMAKE_C_COMPILER "${clang_ROOT}/bin/clang.exe")
set (CMAKE_CXX_COMPILER "${clang_ROOT}/bin/clang++.exe")
else()
set (CMAKE_C_COMPILER "${VCRoot}/bin/Hostx64/x64/cl.exe")
set (CMAKE_CXX_COMPILER "${VCRoot}/bin/Hostx64/x64/cl.exe")
endif()
link_directories ("${VCRoot}/lib/x64")
#------------------------------------------------------------------------------
# Setup Windows SDK
set (WindowsSDKDir "C:/3rd/windows_kits-windows-msvc-17.6-amd64-release.10.0.22000.0-z1/10")
set (WindowsSDKBinDir "${WindowsSDKDir}/bin/10.0.22000.0/x64")
set (WindowsSDKLibDir "${WindowsSDKDir}/Lib/10.0.22000.0")
set (CMAKE_RC_COMPILER "${WindowsSDKBinDir}/rc.exe")
set (CMAKE_MC_COMPILER "${WindowsSDKBinDir}/mc.exe")
set (CMAKE_MT "${WindowsSDKBinDir}/mt.exe")
link_directories ("${WindowsSDKLibDir}/um/x64")
link_directories ("${WindowsSDKLibDir}/ucrt/x64")
#------------------------------------------------------------------------------
# Setup CUDA
set (CUDAToolkit_ROOT "C:/3rd/Cuda-11.4_gom2")
set (CMAKE_CUDA_ARCHITECTURES 52)
if (ENABLE_CLANG_FOR_CUDA)
set (CMAKE_CUDA_COMPILER "${clang_ROOT}/bin/clang++.exe")
else()
set (CMAKE_CUDA_COMPILER "${CUDAToolkit_ROOT}/bin/nvcc.exe")
set (CMAKE_CUDA_FLAGS_INIT "-allow-unsupported-compiler -D_ALLOW_COMPILER_AND_STL_VERSION_MISMATCH")
endif()
And the configure output:
Click to expand
[main] Configuring project: vs-cuda
[driver] Removing C:/workspaces/vs-cuda/out/build/Release/CMakeCache.txt
[driver] Removing C:\workspaces\vs-cuda\out\build\Release\CMakeFiles
[proc] Executing command: C:/gom/cmake-3.29.0/bin/cmake.exe -DCMAKE_MAKE_PROGRAM=c:/gom/gom-build-tools-1.2/ninja.exe -DCMAKE_TOOLCHAIN_FILE=C:/workspaces/vs-cuda/toolchain.cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=C:/workspaces/vs-cuda/out/install/Release -SC:/workspaces/vs-cuda -BC:/workspaces/vs-cuda/out/build/Release -G Ninja
[cmake] -- The CXX compiler identification is MSVC 19.36.32538.0
[cmake] -- The CUDA compiler identification is Clang 16.0.1 with GNU-like command-line
[cmake] -- Detecting CXX compiler ABI info
[cmake] -- Detecting CXX compiler ABI info - done
[cmake] -- Check for working CXX compiler: C:/Program Files/Microsoft Visual Studio/2022/17.6/VC/Tools/MSVC/14.36.32532/bin/Hostx64/x64/cl.exe - skipped
[cmake] -- Detecting CXX compile features
[cmake] -- Detecting CXX compile features - done
[cmake] -- Detecting CUDA compiler ABI info
[cmake] -- Detecting CUDA compiler ABI info - failed
[cmake] -- Check for working CUDA compiler: C:/3rd/clang-windows-msvc-17.6-amd64-release.16.0.1-z2/bin/clang++.exe
[cmake] -- Check for working CUDA compiler: C:/3rd/clang-windows-msvc-17.6-amd64-release.16.0.1-z2/bin/clang++.exe - broken
[cmake] CMake Error at C:/gom/cmake-3.29.0/share/cmake-3.28/Modules/CMakeTestCUDACompiler.cmake:59 (message):
[cmake] The CUDA compiler
[cmake]
[cmake] "C:/3rd/clang-windows-msvc-17.6-amd64-release.16.0.1-z2/bin/clang++.exe"
[cmake]
[cmake] is not able to compile a simple test program.
[cmake]
[cmake] It fails with the following output:
[cmake]
[cmake] Change Dir: 'C:/workspaces/vs-cuda/out/build/Release/CMakeFiles/CMakeScratch/TryCompile-168q5g'
[cmake]
[cmake] Run Build Command(s): c:/gom/gom-build-tools-1.2/ninja.exe -v cmTC_9df71
[cmake] [1/2] C:\3rd\clang-windows-msvc-17.6-amd64-release.16.0.1-z2\bin\clang++.exe --cuda-gpu-arch=sm_52 --cuda-path=C:\3rd\Cuda-11.4_gom2 -D_DEBUG -D_DLL -D_MT -Xclang --dependent-lib=msvcrtd -g -Xclang -gcodeview -MD -MT CMakeFiles/cmTC_9df71.dir/main.cu.obj -MF CMakeFiles\cmTC_9df71.dir\main.cu.obj.d -x cuda -c C:/workspaces/vs-cuda/out/build/Release/CMakeFiles/CMakeScratch/TryCompile-168q5g/main.cu -o CMakeFiles/cmTC_9df71.dir/main.cu.obj
[cmake] [2/2] C:\WINDOWS\system32\cmd.exe /C "cd . && C:\3rd\clang-windows-msvc-17.6-amd64-release.16.0.1-z2\bin\clang++.exe -nostartfiles -nostdlib --cuda-path=C:\3rd\Cuda-11.4_gom2 -D_DEBUG -D_DLL -D_MT -Xclang --dependent-lib=msvcrtd -g -Xclang -gcodeview /machine:x64 -Xlinker /subsystem:console -fuse-ld=lld-link CMakeFiles/cmTC_9df71.dir/main.cu.obj -o cmTC_9df71.exe -Xlinker /MANIFEST:EMBED -Xlinker /implib:cmTC_9df71.lib -Xlinker /pdb:cmTC_9df71.pdb -Xlinker /version:0.0 -LC:/PROGRA~1/MICROS~4/2022/17.6/VC/Tools/MSVC/1436~1.325/lib/x64 -LC:/3rd/windows_kits-windows-msvc-17.6-amd64-release.10.0.22000.0-z1/10/Lib/10.0.22000.0/um/x64 -LC:/3rd/windows_kits-windows-msvc-17.6-amd64-release.10.0.22000.0-z1/10/Lib/10.0.22000.0/ucrt/x64 -lcudadevrt.lib -lcudart_static.lib -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32 -loldnames -L"C:/3rd/Cuda-11.4_gom2/lib/x64" && cd ."
[cmake] FAILED: cmTC_9df71.exe
[cmake] C:\WINDOWS\system32\cmd.exe /C "cd . && C:\3rd\clang-windows-msvc-17.6-amd64-release.16.0.1-z2\bin\clang++.exe -nostartfiles -nostdlib --cuda-path=C:\3rd\Cuda-11.4_gom2 -D_DEBUG -D_DLL -D_MT -Xclang --dependent-lib=msvcrtd -g -Xclang -gcodeview /machine:x64 -Xlinker /subsystem:console -fuse-ld=lld-link CMakeFiles/cmTC_9df71.dir/main.cu.obj -o cmTC_9df71.exe -Xlinker /MANIFEST:EMBED -Xlinker /implib:cmTC_9df71.lib -Xlinker /pdb:cmTC_9df71.pdb -Xlinker /version:0.0 -LC:/PROGRA~1/MICROS~4/2022/17.6/VC/Tools/MSVC/1436~1.325/lib/x64 -LC:/3rd/windows_kits-windows-msvc-17.6-amd64-release.10.0.22000.0-z1/10/Lib/10.0.22000.0/um/x64 -LC:/3rd/windows_kits-windows-msvc-17.6-amd64-release.10.0.22000.0-z1/10/Lib/10.0.22000.0/ucrt/x64 -lcudadevrt.lib -lcudart_static.lib -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32 -loldnames -L"C:/3rd/Cuda-11.4_gom2/lib/x64" && cd ."
[cmake] clang++: error: no such file or directory: '/machine:x64'
[cmake] ninja: build stopped: subcommand failed.
[cmake]
[cmake]
[cmake]
[cmake]
[cmake]
[cmake] CMake will not be able to correctly generate this project.
[cmake] Call Stack (most recent call first):
[cmake] CMakeLists.txt:2 (project)
[cmake]
[cmake]
[cmake] -- Configuring incomplete, errors occurred!
[proc] The command: C:/gom/cmake-3.29.0/bin/cmake.exe -DCMAKE_MAKE_PROGRAM=c:/gom/gom-build-tools-1.2/ninja.exe -DCMAKE_TOOLCHAIN_FILE=C:/workspaces/vs-cuda/toolchain.cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=C:/workspaces/vs-cuda/out/install/Release -SC:/workspaces/vs-cuda -BC:/workspaces/vs-cuda/out/build/Release -G Ninja exited with code: 1