Skip to content

fix: missing compilation flag on recent msvc version - #2032

Merged
Titus-von-Koeller merged 3 commits into
bitsandbytes-foundation:mainfrom
mfuntowicz:fix/cuda_121_win
Aug 12, 2026
Merged

fix: missing compilation flag on recent msvc version#2032
Titus-von-Koeller merged 3 commits into
bitsandbytes-foundation:mainfrom
mfuntowicz:fix/cuda_121_win

Conversation

@mfuntowicz

Copy link
Copy Markdown
Contributor

In recent MSVC compiler versions it needs to specify which preprocessor to use.

This PR addresses this adding the required flag for MSVC.

@github-actions

Copy link
Copy Markdown

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

@Titus-von-Koeller

Titus-von-Koeller commented Aug 11, 2026

Copy link
Copy Markdown
Collaborator

Thanks for this — the /fp:fast de-duplication is a nice simplification and behavior-identical, so that part is good to go. Two things on the new flag, plus one ask.

1. It also hits compilers that don't want it. CMake's MSVC variable is true for any MSVC-emulating compiler, which includes icx (used by the XPU backend) and clang-cl. Clang's preprocessor is already conformant, so the flag is rejected. This PR's own build-xpu (windows-2022, 2026) job logs it on every translation unit:

icx: warning: argument unused during compilation: '-Zc:preprocessor' [-Wunused-command-line-argument]

Suggest gating on the compiler ID instead:

if(MSVC)
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /fp:fast")

    # Only real cl.exe needs (and accepts) the conformant preprocessor switch;
    # clang-cl / icx are already conformant and warn on it.
    if(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
        set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /Zc:preprocessor")
    endif()

    # /arch:AVX2 is only valid for x86/x64 targets, not ARM64
    string(TOLOWER "${CMAKE_SYSTEM_PROCESSOR}" _msvc_arch)
    if(_msvc_arch MATCHES "x86|x64|amd64")
        set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /arch:AVX2")
    endif()
endif()

2. It may not fix the failure it's aimed at. The flag lands in CMAKE_CXX_FLAGS, which never reaches the .cu translation units — CMake compiles those with CMAKE_CUDA_FLAGS. Given the branch name (fix/cuda_121_win), if the error was in a CUDA file this patch is a no-op for it. The CUDA-side equivalent already exists, but is gated to CUDA 13.2+:

https://github.com/bitsandbytes-foundation/bitsandbytes/blob/a2b90e6/CMakeLists.txt#L197-L200

If the failure is nvcc-side, loosening that gate (e.g. keying on MSVC_VERSION rather than the CUDA version) is the actual fix. Either way, the two halves of the build currently disagree on preprocessor conformance for CUDA < 13.2, which is worth aligning.

3. Could you add the error to the description? All Windows jobs are green on main — including build-cpu (windows-2025) and build-cuda (windows-2025, 12.1.1) — so nothing in CI reproduces this, and it's hard to tell which of the two fixes above is correct. The failing file, the full error text, and your cl.exe + CUDA versions would settle it.

If it turns out to be BNB_OMP_PARALLEL_FOR in csrc/cpu_ops.cpp (it uses _Pragma("omp parallel for"), historically MSVC-conformant-preprocessor-only), then __pragma(omp parallel for) guarded by defined(_MSC_VER) && !defined(__clang__) would be a more surgical fix than a global conformance switch — though I'm fine with the switch too.

mfuntowicz and others added 2 commits August 12, 2026 10:14
CMake's MSVC variable is also true for MSVC-emulating compilers
(clang-cl, and Intel icx used by the XPU backend on Windows), which
ship an already-conformant preprocessor and warn on the flag:

  icx: warning: argument unused during compilation: '-Zc:preprocessor'

Restrict the switch to CMAKE_CXX_COMPILER_ID == MSVC.
@Titus-von-Koeller
Titus-von-Koeller merged commit 930274c into bitsandbytes-foundation:main Aug 12, 2026
@mfuntowicz
mfuntowicz deleted the fix/cuda_121_win branch August 12, 2026 13:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants