Skip to main content

LLVM-C Python bindings with nanobind.

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

llvm_nanobind-21.1.6.3.tar.gz (645.1 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

llvm_nanobind-21.1.6.3-cp312-abi3-win_amd64.whl (44.1 MB view details)

Uploaded CPython 3.12+Windows x86-64

llvm_nanobind-21.1.6.3-cp312-abi3-manylinux_2_28_x86_64.whl (61.8 MB view details)

Uploaded CPython 3.12+manylinux: glibc 2.28+ x86-64

llvm_nanobind-21.1.6.3-cp312-abi3-manylinux_2_28_aarch64.whl (58.5 MB view details)

Uploaded CPython 3.12+manylinux: glibc 2.28+ ARM64

llvm_nanobind-21.1.6.3-cp312-abi3-macosx_13_0_x86_64.whl (47.0 MB view details)

Uploaded CPython 3.12+macOS 13.0+ x86-64

llvm_nanobind-21.1.6.3-cp312-abi3-macosx_13_0_arm64.whl (44.0 MB view details)

Uploaded CPython 3.12+macOS 13.0+ ARM64

File details

Details for the file llvm_nanobind-21.1.6.3.tar.gz.

File metadata

  • Download URL: llvm_nanobind-21.1.6.3.tar.gz
  • Upload date:
  • Size: 645.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for llvm_nanobind-21.1.6.3.tar.gz
Algorithm Hash digest
SHA256 0629ce80418a94b2e1a0019d66372cba86e1327b6c9fd16d63a10aa3c09204e2
MD5 634d7b999808c8e1485f24cebd27d7e1
BLAKE2b-256 786e8501bb26482f231bc22815f4dde05fb05e0c52474cf5c455a211ed847474

See more details on using hashes here.

Provenance

The following attestation bundles were made for llvm_nanobind-21.1.6.3.tar.gz:

Publisher: ci.yml on LLVMParty/llvm-nanobind

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file llvm_nanobind-21.1.6.3-cp312-abi3-win_amd64.whl.

File metadata

File hashes

Hashes for llvm_nanobind-21.1.6.3-cp312-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 113a38f38201f7af137b3d7e19e1de9571ed7c7e648cb9f184e26471a1457193
MD5 c45cd2e57f613e43ad49843fb25f17c6
BLAKE2b-256 e9946bce36ab093677ba6643c6a8b9d51bb9893be3c4d9e4604bf797e93077a2

See more details on using hashes here.

Provenance

The following attestation bundles were made for llvm_nanobind-21.1.6.3-cp312-abi3-win_amd64.whl:

Publisher: ci.yml on LLVMParty/llvm-nanobind

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file llvm_nanobind-21.1.6.3-cp312-abi3-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for llvm_nanobind-21.1.6.3-cp312-abi3-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 8be86943cd9c7a7e69ac07ee1f948d7971089377831b0c180de9c5ba714f97d7
MD5 e2b4b573e2a81f3455260e3b10375594
BLAKE2b-256 1a857830240b5d1bac8bc69c0b039478d2342f2809c0809825f0e3f157571f1d

See more details on using hashes here.

Provenance

The following attestation bundles were made for llvm_nanobind-21.1.6.3-cp312-abi3-manylinux_2_28_x86_64.whl:

Publisher: ci.yml on LLVMParty/llvm-nanobind

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file llvm_nanobind-21.1.6.3-cp312-abi3-manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for llvm_nanobind-21.1.6.3-cp312-abi3-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 4e4e814a68c7a63c62badc845d0d1941b04a4cdcf11eafc9398e08c3e3a1bc40
MD5 3ee8a5413aa0343df349d9d53f404e5a
BLAKE2b-256 4d81ca1b5ac53c272dcf1f4db98c0b2246c0a532f4eea42193a05e3ded90099b

See more details on using hashes here.

Provenance

The following attestation bundles were made for llvm_nanobind-21.1.6.3-cp312-abi3-manylinux_2_28_aarch64.whl:

Publisher: ci.yml on LLVMParty/llvm-nanobind

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file llvm_nanobind-21.1.6.3-cp312-abi3-macosx_13_0_x86_64.whl.

File metadata

File hashes

Hashes for llvm_nanobind-21.1.6.3-cp312-abi3-macosx_13_0_x86_64.whl
Algorithm Hash digest
SHA256 fa46ac4e99698190eda9b4e6760d21618bac7c814997da63039e959ca7f9e8fa
MD5 75293bd79948a3df9d0109c5a9df7a32
BLAKE2b-256 9eaf77b62c2dbe20ba8425e1d358da40436599b5540357145a76349098bdb33a

See more details on using hashes here.

Provenance

The following attestation bundles were made for llvm_nanobind-21.1.6.3-cp312-abi3-macosx_13_0_x86_64.whl:

Publisher: ci.yml on LLVMParty/llvm-nanobind

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file llvm_nanobind-21.1.6.3-cp312-abi3-macosx_13_0_arm64.whl.

File metadata

File hashes

Hashes for llvm_nanobind-21.1.6.3-cp312-abi3-macosx_13_0_arm64.whl
Algorithm Hash digest
SHA256 b153bfe4f2008f96dd0395f72e1e4413f8e5d2d9dc254cd51ae49cc48e7f84e9
MD5 b52f8fc74c3b8397ef4a0b6bc21b4266
BLAKE2b-256 b7033ffd26b70fe0db6f6be411b57f396c81a1ee01fcefea6392707192bcdba4

See more details on using hashes here.

Provenance

The following attestation bundles were made for llvm_nanobind-21.1.6.3-cp312-abi3-macosx_13_0_arm64.whl:

Publisher: ci.yml on LLVMParty/llvm-nanobind

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page