nanobind: [BUG]: Internal compiler error with MSVC 2022 when building `nb_func.h`

Problem description

We have recently started building our library Basix on Windows using MSVC 2022 using GitHub Actions. We are using scikit-build-core to orchestrate the CMake build and we build nanobind statically. We use vcpkg to bring in LAPACK and BLAS.

After switching to nanobind 2.0.0 we are consistently triggering an internal compiler error when building nanobind statically. The error points to L42 in nb_func.cpp, but that line looks relatively innocent.

https://github.com/FEniCS/basix/actions/runs/9404421938/job/25903298628#step:5:763 https://github.com/wjakob/nanobind/blob/master/src/nb_func.cpp#L42

We don’t have Windows machines locally so we haven’t tried reproducing this locally. Your CI seems to be running green on MSVC 2022. Any ideas?

Reproducible example code

No response

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 4
  • Comments: 35 (15 by maintainers)

Commits related to this issue

Most upvoted comments

A fork of nanobind with the above patch applied to v2.0.0 is available at: https://github.com/jhale/nanobind/tree/jhale/msvc2022-workaround

I have applied the following patch, which resolves the Internal compiler error. I believe this patch will become unnecessary once the MSVC version is updated and applied to GitHub Actions in the future.

diff --git a/include/nanobind/nb_func.h b/include/nanobind/nb_func.h
index e53e1a4..428ca81 100644
--- a/include/nanobind/nb_func.h
+++ b/include/nanobind/nb_func.h
@@ -199,14 +199,24 @@ NB_INLINE PyObject *func_create(Func &&func, Return (*)(Args...),
 
         PyObject *result;
         if constexpr (std::is_void_v<Return>) {
+#if defined(_WIN32)
+            cap->func(static_cast<cast_t<Args>>(in.template get<Is>())...);
+#else
             cap->func(in.template get<Is>().operator cast_t<Args>()...);
+#endif
             result = Py_None;
             Py_INCREF(result);
         } else {
+#if defined(_WIN32)
+            result = cast_out::from_cpp(
+                       cap->func(static_cast<cast_t<Args>>(in.template get<Is>())...),
+                       policy, cleanup).ptr();
+#else
             result = cast_out::from_cpp(
                        cap->func((in.template get<Is>())
                                      .operator cast_t<Args>()...),
                        policy, cleanup).ptr();
+#endif
         }
 
         if constexpr (Info::keep_alive)

Hi. I also noticed this issue on my own project. I notice that if I add the /permissive- to nanobind’s CMake I am also able to reproduce the issue locally on nanobind’s repo itself:

diff --git i/CMakeLists.txt w/CMakeLists.txt
index 82f9b28..7d9a03e 100644
--- i/CMakeLists.txt
+++ w/CMakeLists.txt
@@ -22,6 +22,8 @@ if (NOT MSVC)
   option(NB_TEST_SANITZE    "Build tests with address and undefined behavior sanitizers?" OFF)
 endif()

+add_compile_options("/permissive-")
+
 # ---------------------------------------------------------------------------
 # Do a release build if nothing was specified
 # ---------------------------------------------------------------------------

Which result in the following error:

  inter_module.vcxproj -> C:\Users\jedumas\workspace\github\nanobind\build\tests\Debug\inter_module.dll
C:\Users\jedumas\workspace\github\nanobind\include\nanobind\nb_func.h(42,1): fatal  error C1001: Internal compiler error. [C:\Users\jedumas\workspace\github\nanobind\build\tests\nanobind-static-abi3.vcxproj]

This with the latest version of Visual Studio 17.10.4 / MSVC 19.40.33812.0.

The good news is I’ve tried to update to the preview release of the next Visual Studio version (17.11.2+c078802d4), and the error goes away.

Config settings lets you do this, pip install -Ccmake.define.CMAKE_GENERATOR_TOOLSET=ClangCL . Or if you are using cibuildwheel, that can set config-settings too. You can also use environment variables, such as:

[tool.scikit-build.cmake.define]
CMAKE_GENERATOR_TOOLSET = { env="CMAKE_GENERATOR_TOOLSET" }

Now you can set CMAKE_GENERATOR_TOOLSET as an environment variable, and it will get passed in as a define.

(If you check the link above, apparently it has been fixed internally, but isn’t in a released version yet, as of 17.10.2)

I’ve merged a temporary fix for this issue but plan to remove it once GHA is functional again.

@melpon @jhale I’m wondering if it makes sense to temporarily merge this patch into the master branch. Does GHA still serve the broken version of MSVC?

@jhale I found out that windows-2019 image can be used instead of windows-latest as a temporary solution to compile nanobind.

https://developercommunity.visualstudio.com/t/Internal-compiler-error-w-nanobind-v19/10662883?sort=newest

@jhale The issue has been reported and fixed, but they haven’t released the patch.

The bug is introduced in MSVC 19.40.xxxxx