Skip to main content

llvm-nanobind

Python bindings for the LLVM-C API using nanobind.

This project provides a Pythonic interface to LLVM's compiler infrastructure, enabling you to build compilers, analyzers, and code transformation tools in Python.

Current status: Experimental but usable. The bindings target LLVM 21.1.6 and are published as llvm-nanobind on PyPI (import name: llvm). Wheels bundle LLVM and are built/tested in CI for Linux x86_64/aarch64, macOS arm64, and Windows x86_64. The API is still evolving; expect breaking changes until a stable release.

Version numbers track LLVM: 21.1.6.1 means LLVM 21.1.6 plus binding/package revision 1.

Note: This project is 90%+ vibe coded. It is mostly an experiment to see what LLMs can do when you set things up properly.

Installation

Released wheels bundle LLVM for supported platforms, so normal installation is:

pip install llvm-nanobind
python -c "import llvm; print(llvm)"

For source/development builds, see Development.

See llvm-nanobind-example for a simple example project.

Quick Start

import llvm

# Create a simple function that returns 42.
with llvm.create_context() as ctx:
    i32 = ctx.types.i32
    fn_type = ctx.types.function(i32, [])

    with ctx.create_module("example") as mod:
        fn = mod.add_function("get_answer", fn_type)
        entry = fn.append_basic_block("entry")

        with entry.create_builder() as builder:
            builder.ret(i32.constant(42))

        assert mod.verify(), mod.verification_error
        print(mod)

This exact snippet is available as examples/quick_start.py.

Examples

Runnable examples live in examples/ and are covered by tests/test_examples.py.

uv run python examples/quick_start.py
uv run python examples/transform_replace_add.py

More examples worth browsing:

Current capabilities

  • Pythonic wrappers for contexts, modules, types, values, functions, basic blocks, builders, metadata, debug info, object files, targets, target machines, and pass builder options
  • IR construction, traversal, and transformation helpers: constants, globals, PHI/control flow/memory/cast/cmp instructions, operands, predecessors, RAUW, split blocks, move/clone/erase instructions
  • IR and bitcode parsing/writing, lazy bitcode modules, module cloning/linking, diagnostics, attributes, COMDATs, calling conventions, linkage/visibility/storage controls
  • Metadata/debug-info APIs including named metadata views, module flag views, instruction/global metadata mappings, DIBuilder recipes, and debug-location scopes
  • Target lookup, data layouts, Module.emit_object(), Module.emit_assembly(), target-machine emission, and PassBuilder pipeline execution via Module.optimize(), Function.optimize(), and Module.run_passes()
  • Generic intrinsic calls with Builder.intrinsic(...) and in-process JIT execution through the LLVM-C ORC LLJIT API
  • Lifetime/validity guards that turn many use-after-free, disposed-object, null-reference, and wrong-kind mistakes into Python exceptions instead of hard crashes
  • Auto-generated typed .pyi stubs for IDEs/type checkers
  • Golden-master tests, Python regression scripts, examples, and vendored llvm-c-test lit tests, including CI coverage against installed wheels

Documentation

Type stubs are auto-generated and provide IDE intellisense. After building, find them at:

.venv/lib/python3.*/site-packages/llvm/__init__.pyi

For development documentation, see devdocs/README.md.

Known limitations

  • The API is not stable yet; method names and ownership rules may still change.
  • Scope follows the LLVM-C API, not the full LLVM C++ API. JIT support is intentionally limited to what is exposed cleanly through LLVM-C ORC LLJIT.
  • Docs are currently the README, examples, devdocs/, and the generated .pyi stub; there is no hosted API reference yet.
  • Prebuilt wheels currently target CPython 3.12+ stable ABI on Linux x86_64/aarch64, macOS arm64/x86_64, and Windows x86_64. Other platforms require a source build.

Development

Setup

Source/development builds need LLVM 21.1.6 available to CMake. The LLVM archives used for packaging are published at LLVMParty/llvm-builds v21.1.6.

From a checkout, download the matching LLVM archive and then run uv sync:

# Choose the archive matching your platform:
#   llvm-21.1.6-linux-x86_64.zip
#   llvm-21.1.6-linux-aarch64.zip
#   llvm-21.1.6-macos-arm64.zip
#   llvm-21.1.6-macos-x86_64.zip
#   llvm-21.1.6-windows-x86_64.zip
python tools/ci/install_llvm.py \
  --version 21.1.6 \
  --archive llvm-21.1.6-linux-x86_64.zip \
  --dest .llvm \
  --prefix-file .llvm-prefix

uv sync --verbose

If you are using your own LLVM install instead, set LLVM_ROOT to its install prefix:

export LLVM_ROOT=/path/to/llvm
uv sync --verbose

If .llvm-prefix already points at another LLVM install, delete it first; CMake may reuse it from a previous configure.

For offline builds:

uv sync --offline --no-build-isolation --verbose

For Windows C++/LSP setup, see the Windows development section below.

Testing

# Main golden-master suite:
# - runs C++ test executables from build/
# - runs paired Python scripts
# - compares Python output against stored C++ behavior
uv run run_tests.py

# Python-only regression scripts in tests/regressions/
uv run run_tests.py --regressions

# Vendored llvm-c-test lit suite against the C test binary
# Rebuilds the vendored C binary before running lit.
uv run run_llvm_c_tests.py
uv run run_llvm_c_tests.py -v

# Vendored llvm-c-test lit suite against the Python implementation
uv run run_llvm_c_tests.py --use-python

# Run the Python llvm-c-test port directly during development
uv run python -m llvm_c_test --targets-list

# Type checking (not a test suite, but commonly run in CI/dev)
uvx ty check

If you want the closest thing to “run everything in this repo”, use:

uv run run_tests.py
uv run run_tests.py --regressions
uv run run_llvm_c_tests.py
uv run run_llvm_c_tests.py --use-python

Python tests here are intended to be executable as standalone scripts (e.g. uv run tests/test_module.py or uv run tests/regressions/test_const_bytes.py). They are generally pytest-compatible too, but direct script execution is the historical/default style used by run_tests.py and for one-off debugging.

pytest is still useful for targeting specific regression files or subsets, but it is not our complete top-level test entrypoint by itself.

Coverage

# Run with coverage
uv run coverage run run_llvm_c_tests.py --use-python
uv run coverage combine
uv run coverage report --include="llvm_c_test/*"

Windows source builds and C++ IntelliSense

The wheel is the easiest path on Windows. If you need a source/development build, install Visual Studio or Visual Studio Build Tools with the C++ workload, then fetch LLVM and run uv sync:

py -3.12 tools\ci\install_llvm.py `
  --version 21.1.6 `
  --archive llvm-21.1.6-windows-x86_64.zip `
  --dest .llvm `
  --prefix-file .llvm-prefix

uv sync --verbose

That command downloads from LLVMParty/llvm-builds v21.1.6, installs into .llvm, and writes .llvm-prefix for CMake.

If you are using your own LLVM install instead:

$env:LLVM_ROOT = "C:\path\to\llvm"
uv sync --verbose

For C++ local development with an LSP, create a local CMakeUserPresets.json at the repository root. CMake gets LLVM from .llvm-prefix created above, or from LLVM_ROOT on first configure. The compiler paths below assume the usual LLVM installer location; adjust them if your clang-cl.exe is elsewhere:

{
    "version": 3,
    "configurePresets": [
        {
            "name": "clang-cl",
            "displayName": "Ninja with clang-cl",
            "generator": "Ninja",
            "binaryDir": "${sourceDir}/build",
            "cacheVariables": {
                "CMAKE_C_COMPILER": "C:/Program Files/LLVM/bin/clang-cl.exe",
                "CMAKE_CXX_COMPILER": "C:/Program Files/LLVM/bin/clang-cl.exe",
                "CMAKE_EXPORT_COMPILE_COMMANDS": "ON",
                "CMAKE_BUILD_TYPE": "RelWithDebInfo"
            }
        }
    ],
    "buildPresets": [
        {
            "name": "clang-cl",
            "configurePreset": "clang-cl"
        }
    ]
}

Then configure/build with:

cmake --preset clang-cl
cmake --build --preset clang-cl

If you move or replace the LLVM install, delete or regenerate .llvm-prefix from any previous configure.

License

This project is licensed under the MIT License. See LICENSE for details.

LLVM is licensed under the Apache License v2.0 with LLVM Exceptions.

Metadata

Release files for llvm-nanobind 21.1.6.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for llvm-nanobind 21.1.6.3
File Size Uploaded Provenance Actions
llvm_nanobind-21.1.6.3.tar.gz 645.1 kB With provenance Details for llvm_nanobind-21.1.6.3.tar.gz

Built distributions (wheels)

Table of built distributions (wheels) for llvm-nanobind 21.1.6.3
File Actions
llvm_nanobind-21.1.6.3-cp312-abi3-win_amd64.whl CPython 3.12 abi3 Windows x86-64 Details for llvm_nanobind-21.1.6.3-cp312-abi3-win_amd64.whl
llvm_nanobind-21.1.6.3-cp312-abi3-manylinux_2_28_x86_64.whl CPython 3.12 abi3 Linux glibc 2.28+ x86-64 Details for llvm_nanobind-21.1.6.3-cp312-abi3-manylinux_2_28_x86_64.whl
llvm_nanobind-21.1.6.3-cp312-abi3-manylinux_2_28_aarch64.whl CPython 3.12 abi3 Linux glibc 2.28+ ARM64 Details for llvm_nanobind-21.1.6.3-cp312-abi3-manylinux_2_28_aarch64.whl
llvm_nanobind-21.1.6.3-cp312-abi3-macosx_13_0_x86_64.whl CPython 3.12 abi3 macOS 13.0+ x86-64 Details for llvm_nanobind-21.1.6.3-cp312-abi3-macosx_13_0_x86_64.whl
llvm_nanobind-21.1.6.3-cp312-abi3-macosx_13_0_arm64.whl CPython 3.12 abi3 macOS 13.0+ ARM64 Details for llvm_nanobind-21.1.6.3-cp312-abi3-macosx_13_0_arm64.whl

Total release size: 256.0 MB

Release files / llvm_nanobind-21.1.6.3.tar.gz

Download URL llvm_nanobind-21.1.6.3.tar.gz
Size 645.1 kB
Tags Source
SHA-256 checksum
How to use checksums
0629ce80418a94b2e1a0019d66372cba86e1327b6c9fd16d63a10aa3c09204e2
BLAKE2b-256 checksum
How to use checksums
786e8501bb26482f231bc22815f4dde05fb05e0c52474cf5c455a211ed847474
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on May 14, 2026.

Transparency log

Release files / llvm_nanobind-21.1.6.3-cp312-abi3-win_amd64.whl

Download URL llvm_nanobind-21.1.6.3-cp312-abi3-win_amd64.whl
Size 44.1 MB
Tags CPython 3.12 Windows x86-64 abi3
SHA-256 checksum
How to use checksums
113a38f38201f7af137b3d7e19e1de9571ed7c7e648cb9f184e26471a1457193
BLAKE2b-256 checksum
How to use checksums
e9946bce36ab093677ba6643c6a8b9d51bb9893be3c4d9e4604bf797e93077a2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on May 14, 2026.

Transparency log

Release files / llvm_nanobind-21.1.6.3-cp312-abi3-manylinux_2_28_x86_64.whl

Download URL llvm_nanobind-21.1.6.3-cp312-abi3-manylinux_2_28_x86_64.whl
Size 61.8 MB
Tags CPython 3.12 Linux glibc 2.28+ x86-64 abi3
SHA-256 checksum
How to use checksums
8be86943cd9c7a7e69ac07ee1f948d7971089377831b0c180de9c5ba714f97d7
BLAKE2b-256 checksum
How to use checksums
1a857830240b5d1bac8bc69c0b039478d2342f2809c0809825f0e3f157571f1d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on May 14, 2026.

Transparency log

Release files / llvm_nanobind-21.1.6.3-cp312-abi3-manylinux_2_28_aarch64.whl

Download URL llvm_nanobind-21.1.6.3-cp312-abi3-manylinux_2_28_aarch64.whl
Size 58.5 MB
Tags CPython 3.12 Linux glibc 2.28+ ARM64 abi3
SHA-256 checksum
How to use checksums
4e4e814a68c7a63c62badc845d0d1941b04a4cdcf11eafc9398e08c3e3a1bc40
BLAKE2b-256 checksum
How to use checksums
4d81ca1b5ac53c272dcf1f4db98c0b2246c0a532f4eea42193a05e3ded90099b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on May 14, 2026.

Transparency log

Release files / llvm_nanobind-21.1.6.3-cp312-abi3-macosx_13_0_x86_64.whl

Download URL llvm_nanobind-21.1.6.3-cp312-abi3-macosx_13_0_x86_64.whl
Size 47.0 MB
Tags CPython 3.12 abi3 macOS 13.0+ x86-64
SHA-256 checksum
How to use checksums
fa46ac4e99698190eda9b4e6760d21618bac7c814997da63039e959ca7f9e8fa
BLAKE2b-256 checksum
How to use checksums
9eaf77b62c2dbe20ba8425e1d358da40436599b5540357145a76349098bdb33a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on May 14, 2026.

Transparency log

Release files / llvm_nanobind-21.1.6.3-cp312-abi3-macosx_13_0_arm64.whl

Download URL llvm_nanobind-21.1.6.3-cp312-abi3-macosx_13_0_arm64.whl
Size 44.0 MB
Tags CPython 3.12 abi3 macOS 13.0+ ARM64
SHA-256 checksum
How to use checksums
b153bfe4f2008f96dd0395f72e1e4413f8e5d2d9dc254cd51ae49cc48e7f84e9
BLAKE2b-256 checksum
How to use checksums
b7033ffd26b70fe0db6f6be411b57f396c81a1ee01fcefea6392707192bcdba4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on May 14, 2026.

Transparency log

Security for llvm-nanobind 21.1.6.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 6 files in this release have been uploaded with provenance metadata.

Select from 6 files

Attestation for llvm_nanobind-21.1.6.3.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on May 14, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page