Skip to content

Commit 32e16e8

Browse files
committed
fix: resolve build failures on iluvatar, metax, and cambricon
- Iluvatar: move `-x ivcore` from CMAKE_CUDA_FLAGS to compile-only options so it doesn't get passed during linking (which caused clang++ to re-parse .o files as source code) - MetaX: add `-DUSE_MACA=1` to g++ flags for torch source compilation (MetaX torch fork headers require this define) - Cambricon: query `torch.compiled_with_cxx11_abi()` and set `_GLIBCXX_USE_CXX11_ABI` globally to match torch's ABI setting (fixes undefined reference to `c10::Device::Device(std::string)`)
1 parent 55bef7d commit 32e16e8

2 files changed

Lines changed: 39 additions & 2 deletions

File tree

CMakeLists.txt

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,21 @@ if(WITH_TORCH)
130130
find_library(C10_LIB c10 HINTS ${_torch_lib_dirs} REQUIRED)
131131
set(TORCH_LIBRARIES ${TORCH_LIB} ${TORCH_CPU_LIB} ${C10_LIB})
132132

133+
# Query the CXX11 ABI setting torch was compiled with.
134+
# Mismatched ABI causes linker errors (e.g. undefined reference to
135+
# c10::Device::Device(std::string const&)).
136+
execute_process(
137+
COMMAND ${Python_EXECUTABLE} -c "import torch; print(int(torch.compiled_with_cxx11_abi()))"
138+
OUTPUT_VARIABLE TORCH_CXX11_ABI
139+
OUTPUT_STRIP_TRAILING_WHITESPACE
140+
RESULT_VARIABLE _torch_abi_result
141+
)
142+
143+
if(_torch_abi_result EQUAL 0)
144+
add_compile_definitions(_GLIBCXX_USE_CXX11_ABI=${TORCH_CXX11_ABI})
145+
message(STATUS "PyTorch CXX11 ABI: ${TORCH_CXX11_ABI}")
146+
endif()
147+
133148
message(STATUS "Found PyTorch: ${TORCH_INCLUDE_DIRS}")
134149
endif()
135150

@@ -164,12 +179,24 @@ if(WITH_ILUVATAR)
164179
else()
165180
set(CMAKE_CUDA_COMPILER "clang++" CACHE STRING "Iluvatar CUDA compiler (clang++)")
166181
endif()
167-
set(CMAKE_CUDA_FLAGS "-x ivcore -std=c++17 --cuda-gpu-arch=${ILUVATAR_ARCH} -fPIC -Wno-error=unused-variable -Wno-error=unused-private-field -Wno-unused-variable" CACHE STRING "Iluvatar CUDA flags")
182+
# `-x ivcore` must NOT be in CMAKE_CUDA_FLAGS — CMake passes those flags
183+
# to both compile and link steps. During linking, `-x ivcore` causes
184+
# clang++ to re-parse .o files as source code.
185+
set(CMAKE_CUDA_FLAGS "--cuda-gpu-arch=${ILUVATAR_ARCH} -fPIC -Wno-error=unused-variable -Wno-error=unused-private-field -Wno-unused-variable" CACHE STRING "Iluvatar CUDA flags")
168186
set(CMAKE_CUDA_SEPARABLE_COMPILATION OFF CACHE BOOL "Disable RDC for Iluvatar")
169187
set(CMAKE_CUDA_ARCHITECTURES OFF CACHE STRING "Iluvatar CUDA architectures (passed via CMAKE_CUDA_FLAGS)")
188+
# Disable CMake's automatic CUDA runtime linking — Iluvatar doesn't
189+
# ship `libcudadevrt` which CMake's compiler test tries to link.
190+
# We link the runtime manually via `find_package(CUDAToolkit)`.
191+
set(CMAKE_CUDA_RUNTIME_LIBRARY NONE)
170192
message(STATUS "Iluvatar: CUDA compiler ${CMAKE_CUDA_COMPILER}, arch ${ILUVATAR_ARCH}")
171193
enable_language(CUDA)
172194
find_package(CUDAToolkit REQUIRED)
195+
# Add `-x ivcore` as compile-only flag (not during linking).
196+
# Iluvatar's clang++ accepts both `-x ivcore` and `-x cuda` during
197+
# compilation (warning on the latter), but `-x ivcore` in link flags
198+
# causes .o files to be re-parsed as source.
199+
add_compile_options($<$<COMPILE_LANGUAGE:CUDA>:-x$<SEMICOLON>ivcore>)
173200
endif()
174201

175202
if(WITH_METAX)

src/CMakeLists.txt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ if(WITH_CPU)
1616

1717
target_compile_definitions(infiniops PUBLIC WITH_CPU=1)
1818

19-
find_package(OpenMP REQUIRED)
19+
find_package(OpenMP REQUIRED COMPONENTS CXX)
2020
target_link_libraries(infiniops PRIVATE OpenMP::OpenMP_CXX)
2121

2222
list(APPEND DEVICE_LIST "cpu")
@@ -239,6 +239,15 @@ if(WITH_TORCH)
239239
list(APPEND _torch_include_flags "-isystem" "${_dir}")
240240
endforeach()
241241

242+
# Vendor-specific defines required by forked torch headers.
243+
set(_torch_extra_flags "")
244+
if(WITH_METAX)
245+
list(APPEND _torch_extra_flags "-DUSE_MACA=1")
246+
endif()
247+
if(DEFINED TORCH_CXX11_ABI)
248+
list(APPEND _torch_extra_flags "-D_GLIBCXX_USE_CXX11_ABI=${TORCH_CXX11_ABI}")
249+
endif()
250+
242251
set(TORCH_OBJECT_DIR "${CMAKE_CURRENT_BINARY_DIR}/torch_objs")
243252
file(MAKE_DIRECTORY "${TORCH_OBJECT_DIR}")
244253

@@ -256,6 +265,7 @@ if(WITH_TORCH)
256265
-std=c++17 -fPIC -O2
257266
"-I${CMAKE_CURRENT_SOURCE_DIR}"
258267
${_torch_include_flags}
268+
${_torch_extra_flags}
259269
-c "${_src}" -o "${_obj}"
260270
DEPENDS "${_src}"
261271
COMMENT "Compiling ${_rel} with system C++ compiler"

0 commit comments

Comments
 (0)