Skip to content

feat: integrate numpy as statically-linked built-in module#7

Merged
ppenna merged 3 commits into
mainfrom
feat/add-numpy-integration
Feb 28, 2026
Merged

feat: integrate numpy as statically-linked built-in module#7
ppenna merged 3 commits into
mainfrom
feat/add-numpy-integration

Conversation

@ppenna

@ppenna ppenna commented Feb 28, 2026

Copy link
Copy Markdown
Contributor

Summary

Integrate nanvix/numpy into nanvix-python, following the same pattern used for cymem.

NumPy's core C extension (_multiarray_umath) is statically linked into the CPython binary as a built-in module, enabling import numpy and basic array operations on Nanvix.

Changes

Build pipeline (z)

  • Add deps/numpy submodule (branch nanvix/v1.26.4, shallow)
  • Build numpy static archives via Meson cross-compilation in Docker (with Cython shim)
  • Register _np_multiarray_umath as a CPython built-in in Modules/Setup.local
  • Install numpy Python package with generated version.py and __config__.py stubs
  • Add __kernel_tanl libm compatibility stub for Nanvix newlib

C source patches (patches/numpy_static_builtin.patch)

  • Skip CPUID/XGETBV instructions that hang on the Nanvix microkernel
  • Rename built-in to _np_multiarray_umath to break circular import deadlock with CPython's per-module import lock
  • Defer npy_cache_import calls during module init (#ifndef __nanvix__)
  • Pre-register partially-initialised module in sys.modules to prevent re-entrant PyInit

Runtime bootstrap (patches/nanvix_numpy_bootstrap.py)

  • find_spec-based meta-path finder redirects numpy.core._multiarray_umath to the renamed built-in
  • Permissive stub modules (_StubModule) for C extensions not statically linked (linalg, fft, random, tests)

Test

  • Add test_102_numpy.py — validates array creation + sum
  • All 102 tests pass (0 failed, 0 skipped), including flatbuffers which depends on numpy

Patch documentation

See patches/numpy_static_builtin.md for a detailed explanation of each patch section.

Add numpy (https://github.com/nanvix/numpy) as a git submodule and wire
it into the setup/build/test pipeline following the same pattern used
for cymem.

Build changes:
- Add deps/numpy submodule (branch nanvix/v1.26.4)
- Build numpy static archives via Meson cross-compilation in Docker
- Register _np_multiarray_umath as a CPython built-in in Setup.local
- Install numpy Python package with generated version.py and __config__.py
- Add __kernel_tanl libm compatibility stub for Nanvix newlib

Runtime patches (patches/numpy_static_builtin.patch):
- Skip CPUID/XGETBV instructions that hang on Nanvix microkernel
- Rename built-in to _np_multiarray_umath to break circular import
  deadlock with CPython per-module import lock
- Defer npy_cache_import calls during init (#ifndef __nanvix__)
- Pre-register partially-initialised module in sys.modules

Runtime bootstrap (patches/nanvix_numpy_bootstrap.py):
- find_spec-based meta-path finder redirects numpy.core._multiarray_umath
  to the renamed _np_multiarray_umath built-in
- Permissive stub modules for unlinked C extensions (linalg, fft, random)

Test:
- Add test_102_numpy.py functional test (array creation + sum)
- All 102 tests pass (including flatbuffers which depends on numpy)
The NumPy Meson build requires Cython to transpile .pyx files, but it
was not available in the CI container. Create a virtual environment
during setup and activate it during build so that cython is on PATH.
- Use 'python3' from PATH instead of hardcoded /usr/bin/python3 so
  Meson runs with the venv Python that has Cython installed.
- Add -Wl,--allow-multiple-definition to link args to resolve
  'multiple definition of environ' between libposix.a and libc.a.
- Apply the patch during build alongside the existing numpy patches.
@ppenna ppenna merged commit e4c2b8a into main Feb 28, 2026
7 checks passed
@ppenna ppenna deleted the feat/add-numpy-integration branch February 28, 2026 04:30
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.

1 participant