Skip to content

Conversation

makslevental
Copy link
Contributor

@makslevental makslevental commented Sep 30, 2025

If PYTHONPATH is set and points to the build location of the python bindings package then when stubgen runs, _mlir will get imported twice and bad things will happen (e.g., Assertion !instance && “PyGlobals already constructed”’). This happens because mlir is a namespace package and the importer/loader can't distinguish between
mlir._mlir_libs._mlir and _mlir_libs._mlir imported from CWD. Or something like that. The fix is to filter out any entries in PYTHONPATH that end in MLIR_BINDINGS_PYTHON_INSTALL_PREFIX/.. (e.g., python_packages/mlir_core/).

@llvmbot
Copy link
Member

llvmbot commented Sep 30, 2025

@llvm/pr-subscribers-mlir

Author: Maksim Levental (makslevental)

Changes

If PYTHONPATH is set and points to the build location of the python bindings package then when stubgen runs, _mlir will get imported twice and bad things will happen (e.g., Assertion !instance && “PyGlobals already constructed”’). This happens because mlir is a namespace package and so importer/loader can't distinguish between
mlir._mlir_libs._mlir and _mlir_libs._mlir imported from CWD. Or something like that. The fix is to filter out any entries in PYTHONPATH that end in MLIR_BINDINGS_PYTHON_INSTALL_PREFIX/.. (e.g., python_packages/mlir_core/).


Full diff: https://github.com/llvm/llvm-project/pull/161307.diff

1 Files Affected:

  • (modified) mlir/cmake/modules/AddMLIRPython.cmake (+21-1)
diff --git a/mlir/cmake/modules/AddMLIRPython.cmake b/mlir/cmake/modules/AddMLIRPython.cmake
index 208cbdd1dd535..1127f9410e488 100644
--- a/mlir/cmake/modules/AddMLIRPython.cmake
+++ b/mlir/cmake/modules/AddMLIRPython.cmake
@@ -173,9 +173,29 @@ function(mlir_generate_type_stubs)
   if(ARG_VERBOSE)
     message(STATUS "Generating type-stubs outputs ${_generated_type_stubs}")
   endif()
+
+  # If PYTHONPATH is set and points to the build location of the python package then when stubgen runs, _mlir will get
+  # imported twice and bad things will happen (e.g., Assertion `!instance && “PyGlobals already constructed”’).
+  # This happens because mlir is a namespace package and so importer/loader can't distinguish between
+  # mlir._mlir_libs._mlir and _mlir_libs._mlir imported from CWD.
+  # So try to filter out any entries in PYTHONPATH that end in "MLIR_BINDINGS_PYTHON_INSTALL_PREFIX/.."
+  # (e.g., python_packages/mlir_core/).
+  set(_pythonpath "$ENV{PYTHONPATH}")
+  cmake_path(SET _install_prefix NORMALIZE "${MLIR_BINDINGS_PYTHON_INSTALL_PREFIX}/..")
+  string(REGEX REPLACE "/$" "" _install_prefix "${_install_prefix}")
+  if(WIN32)
+    set(_path_sep ";")
+  else()
+    set(_path_sep ":")
+    # `;` is the CMake list delimiter so Windows paths are automatically lists
+    # but Unix paths can be made into lists by replacing `:` with `;`
+    string(REPLACE "${_path_sep}" ";" _pythonpath "${_pythonpath}")
+  endif()
+  list(FILTER _pythonpath EXCLUDE REGEX "(${_install_prefix}|${_install_prefix}/)$")
+  string(JOIN "${_path_sep}" _pythonpath ${_pythonpath})
   add_custom_command(
     OUTPUT ${_generated_type_stubs}
-    COMMAND ${_nb_stubgen_cmd}
+    COMMAND ${CMAKE_COMMAND} -E env PYTHONPATH=${_pythonpath} ${_nb_stubgen_cmd}
     WORKING_DIRECTORY "${CMAKE_CURRENT_FUNCTION_LIST_DIR}"
     DEPENDS "${ARG_DEPENDS_TARGETS}"
     DEPFILE "${_depfile}"

@makslevental makslevental force-pushed the users/makslevental/fix-stubgen-path-collision branch 3 times, most recently from fd0ec66 to 13f2900 Compare September 30, 2025 03:16
Copy link
Member

@PragmaTwice PragmaTwice left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tried on my local and it did fix the issue. Thanks!

@makslevental makslevental enabled auto-merge (squash) September 30, 2025 05:12
@makslevental makslevental force-pushed the users/makslevental/fix-stubgen-path-collision branch from 13f2900 to 55eeaa5 Compare September 30, 2025 05:17
@makslevental makslevental force-pushed the users/makslevental/fix-stubgen-path-collision branch from 55eeaa5 to be8ebc8 Compare September 30, 2025 05:22
@makslevental makslevental force-pushed the users/makslevental/fix-stubgen-path-collision branch from be8ebc8 to 583dec4 Compare September 30, 2025 05:29
@makslevental makslevental enabled auto-merge (squash) September 30, 2025 05:37
@makslevental makslevental merged commit e911eba into main Sep 30, 2025
9 checks passed
@makslevental makslevental deleted the users/makslevental/fix-stubgen-path-collision branch September 30, 2025 05:37
mahesh-attarde pushed a commit to mahesh-attarde/llvm-project that referenced this pull request Oct 3, 2025
If `PYTHONPATH` is set and points to the build location of the python
bindings package then when stubgen runs, `_mlir` will get imported twice
and bad things will happen (e.g., `Assertion !instance && “PyGlobals
already constructed”’`). This happens because `mlir` is a namespace
package and the importer/loader can't distinguish between
`mlir._mlir_libs._mlir` and `_mlir_libs._mlir` imported from `CWD`. Or
something like that. The fix is to filter out any entries in
`PYTHONPATH` that end in `MLIR_BINDINGS_PYTHON_INSTALL_PREFIX/..` (e.g.,
`python_packages/mlir_core/`).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants