From 0bb50d99c04c279180c0c7fd9384e302cddcf1b4 Mon Sep 17 00:00:00 2001 From: Cheng Date: Sun, 25 Jan 2026 12:25:01 +0900 Subject: [PATCH] Fix some NVCC warnings when building CUDA backend with MSVC (#3038) --- mlx/CMakeLists.txt | 39 ++++++++++++++++++++++----------- mlx/backend/cuda/CMakeLists.txt | 22 +++++++------------ mlx/io/load.cpp | 1 - 3 files changed, 34 insertions(+), 28 deletions(-) diff --git a/mlx/CMakeLists.txt b/mlx/CMakeLists.txt index 578078cc..0f122e61 100644 --- a/mlx/CMakeLists.txt +++ b/mlx/CMakeLists.txt @@ -22,13 +22,7 @@ target_sources( # Define MLX_VERSION only in the version.cpp file. add_library(mlx_version OBJECT ${CMAKE_CURRENT_SOURCE_DIR}/version.cpp) target_compile_definitions(mlx_version PRIVATE MLX_VERSION="${MLX_VERSION}") -# mlx_version needs access to api.h for MLX_API export macro target_include_directories(mlx_version PRIVATE ${PROJECT_SOURCE_DIR}) -# On Windows shared lib builds, mlx_version also needs MLX_EXPORT for proper DLL -# linkage -if(WIN32 AND BUILD_SHARED_LIBS) - target_compile_definitions(mlx_version PRIVATE MLX_EXPORT) -endif() target_link_libraries(mlx PRIVATE $) if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU") @@ -39,21 +33,40 @@ if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU") endif() if(MSVC) - # Disable some MSVC warnings to speed up compilation. - target_compile_options(mlx PUBLIC /wd4068 /wd4244 /wd4267 /wd4804) - # Enable /bigobj for heavily templated code (e.g., binary.cpp) that exceeds - # the default 65,535 section limit in COFF object files. Use generator - # expression to only apply to C/CXX, not CUDA (NVCC doesn't understand /bigobj - # directly). - target_compile_options(mlx PRIVATE $<$:/bigobj>) # Windows DLLs have a 65535 symbol export limit. We use explicit exports via # MLX_API macro (__declspec(dllexport)) on public API functions only. This # avoids exporting internal template instantiations. if(BUILD_SHARED_LIBS) target_compile_definitions(mlx PRIVATE MLX_EXPORT) + target_compile_definitions(mlx_version PRIVATE MLX_EXPORT) else() target_compile_definitions(mlx PUBLIC MLX_STATIC) + target_compile_definitions(mlx_version PRIVATE MLX_STATIC) endif() + # Some of CUDA's headers include windows.h, which defines min/max macros. + target_compile_definitions(mlx PRIVATE NOMINMAX) + # Disable some MSVC warnings to speed up compilation. + target_compile_options( + mlx + PUBLIC $<$:/wd4068 + /wd4244 + /wd4267 + /wd4700 + /wd4804> + $<$:-Xcompiler=/wd4068 + -Xcompiler=/wd4244 + -Xcompiler=/wd4267 + -Xcompiler=/wd4700 + -Xcompiler=/wd4804>) + # Enable /bigobj for heavily templated code (e.g., binary.cpp) that exceeds + # the default 65,535 section limit in COFF object files. + target_compile_options( + mlx PRIVATE $<$:/bigobj> + $<$:-Xcompiler=/bigobj>) + # Use modern preprocessor, otherwise CCCL would complain. + target_compile_options( + mlx PRIVATE $<$:/Zc:preprocessor> + $<$:-Xcompiler=/Zc:preprocessor>) endif() add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/backend/common) diff --git a/mlx/backend/cuda/CMakeLists.txt b/mlx/backend/cuda/CMakeLists.txt index 1c80323f..7f9b6c86 100644 --- a/mlx/backend/cuda/CMakeLists.txt +++ b/mlx/backend/cuda/CMakeLists.txt @@ -113,22 +113,16 @@ target_compile_options(mlx target_compile_options( mlx PRIVATE "$<$:--expt-relaxed-constexpr>") -# CUDA 12.8 emits warning #20280-D for copy kernels which is a false positive. -# Explicitly pass this flag to suppress the warning, it is safe to set it to -# true but the warning wouldn't be suppressed. -if(CMAKE_CUDA_COMPILER_VERSION VERSION_GREATER_EQUAL 12.8.0) - target_compile_options( - mlx - PRIVATE "$<$:--static-global-template-stub=false>") -endif() - -# Suppress warning when building for compute capability 7 used by V100. -target_compile_options( - mlx PRIVATE "$<$:--Wno-deprecated-gpu-targets>") - # Suppress nvcc warnings on C++ headers. target_compile_options( - mlx PRIVATE $<$:-Xcudafe="--diag_suppress=997,20208">) + mlx + PRIVATE + $<$:-Xcudafe="--diag_suppress=27,997,1394,20011,20208"> +) + +# Ignore some valid nvcc warnings, we might want to fix them in future. +target_compile_options( + mlx PRIVATE $<$:-Xcudafe="--diag_suppress=177,550">) # Use stronger binaries compression. This feature was introduced in CUDA 12.8 # and requires drivers released after CUDA 12.4. diff --git a/mlx/io/load.cpp b/mlx/io/load.cpp index 878d6d1a..15fc4bc2 100644 --- a/mlx/io/load.cpp +++ b/mlx/io/load.cpp @@ -7,7 +7,6 @@ // Used by pread implementation. #ifdef _WIN32 -#define NOMINMAX #include #endif // _WIN32