llvmlite

raw JSON →
0.46.0 verified Tue May 12 auth: no python install: draft quickstart: draft

llvmlite is a lightweight Python binding to LLVM, primarily used for just-in-time (JIT) compilation. It allows Python code to construct LLVM Intermediate Representation (IR) in pure Python, which is then passed to LLVM for compilation and optimization. Maintained by the Numba team, it releases several times a year, often in conjunction with Numba updates and LLVM version bumps. The current stable version is 0.46.0.

pip install llvmlite
error FileNotFoundError: [Errno 2] No such file or directory: 'llvm-config'
cause This error occurs during `llvmlite` installation (especially when building from source) because the `llvm-config` executable, which is used to locate LLVM libraries and headers, cannot be found in the system's PATH or at the location specified by the `LLVM_CONFIG` environment variable. This often happens on systems without a proper LLVM development installation or when `pip` attempts to build `llvmlite` from a source distribution due to an unsupported Python version or architecture.
fix
Ensure that LLVM development packages are installed and llvm-config is in your system's PATH. If using pip, try pip install llvmlite and if it fails, consider using conda install llvmlite as it typically provides pre-built binaries that include a compatible LLVM. If building from source, set the LLVM_CONFIG environment variable to the full path of your llvm-config executable (e.g., export LLVM_CONFIG=/usr/local/opt/llvm/bin/llvm-config).
error ImportError: Numba requires at least version X.Y.Z of llvmlite. Installed version is A.B.C. Please update llvmlite.
cause This error arises when the installed versions of `Numba` and `llvmlite` are incompatible. `Numba` has a strict dependency on specific `llvmlite` versions, and if `llvmlite` is older than required, this error is raised.
fix
Update llvmlite to the version specified or a compatible one with your Numba installation, usually by running pip install --upgrade llvmlite or conda update llvmlite. It's often best to let conda manage both, e.g., conda install numba llvmlite.
error RuntimeError: Building llvmlite requires LLVM X.Y+ Be sure to set LLVM_CONFIG to the right executable path.
cause This indicates that `llvmlite` could not find a suitable LLVM installation (specifically version X.Y or newer) during its build process. It explicitly suggests setting the `LLVM_CONFIG` environment variable to point to the correct `llvm-config` executable.
fix
Install the required LLVM version (e.g., via your system's package manager, Homebrew, or by compiling from source) and then set the LLVM_CONFIG environment variable to the absolute path of the llvm-config executable for that LLVM installation before attempting to install llvmlite again. For example: export LLVM_CONFIG=/path/to/llvm-X.Y/bin/llvm-config followed by pip install llvmlite.
error OSError: Could not find/load shared object file
cause This error occurs when `llvmlite`'s Python bindings (specifically `llvmlite.binding`) cannot find or load the underlying shared library (`libllvmlite.so`, `.dll`, or `.dylib`). This often points to a failed or incomplete build of `llvmlite` itself, or an incompatibility with the system's LLVM libraries if `llvmlite` was not statically linked or linked against a mismatched LLVM version.
fix
Reinstall llvmlite using a reliable method, preferably conda install llvmlite to leverage pre-built, statically linked packages. If using pip and encountering issues, ensure you have the necessary build tools (like cmake, g++, Visual Studio on Windows) and a compatible LLVM installation (as referenced by LLVM_CONFIG) for your system and Python version.
error ModuleNotFoundError: No module named 'llvmlite'
cause This error means that the Python interpreter cannot find the `llvmlite` package. This typically happens if `llvmlite` was not successfully installed, was installed in a different Python environment, or the current environment's `PYTHONPATH` does not include the installation directory.
fix
Install llvmlite using pip install llvmlite or conda install llvmlite. Ensure you are installing it into the correct Python environment that you are currently using. If it was already installed, verify the environment or check your PYTHONPATH.
breaking The `llvm.binding.initialize()` function, previously used to initialize LLVM, is deprecated and removed. In `llvmlite` 0.45.x, it raises a `RuntimeError`, and in 0.46.x it is completely removed. LLVM core initialization is now automatic.
fix Remove any calls to `llvm.binding.initialize()` from your code.
breaking Support for Typed Pointers in the binding layer was removed in `llvmlite` 0.45.0. Future versions (>=0.46, specifically >=0.47) will make Opaque Pointers the default for the IR layer and eventually remove Typed Pointer support entirely, aligning with LLVM 17+. Code relying on Typed Pointers will break if not migrated.
fix Migrate code to use Opaque Pointers. Refer to the llvmlite documentation's migration guide for specifics on updating existing code.
gotcha Each `llvmlite` version is specifically targeted and compatible with particular LLVM feature versions. Using a mismatched LLVM version (e.g., when building from source or with dynamic linking) can lead to build failures or runtime issues. Consult the `llvmlite` documentation for the compatibility matrix. For example, `llvmlite` 0.45.0+ requires LLVM 20.x.x.
fix Ensure your LLVM installation (if explicitly managed) matches the version required by your `llvmlite` installation. Using pre-built binaries (pip wheels or conda packages) often avoids this issue as they bundle compatible LLVM versions.
gotcha Building `llvmlite` from source is a complex process requiring specific development tools (C++ compiler, CMake, LLVM development libraries). It is generally recommended to install pre-built binaries via `pip` or `conda` unless you have specific reasons to build from source.
fix Prefer `pip install llvmlite` or `conda install llvmlite` for most use cases to leverage pre-compiled binaries.
deprecated Official support for Python 3.9 was dropped with `llvmlite` 0.44.0. The minimum supported Python version is now 3.10.
fix Upgrade your Python environment to Python 3.10 or newer.
conda install llvmlite
python os / libc status wheel install import disk
3.10 alpine (musl) - - - -
3.10 slim (glibc) - - 0.01s 180M
3.11 alpine (musl) - - - -
3.11 slim (glibc) - - 0.02s 182M
3.12 alpine (musl) - - - -
3.12 slim (glibc) - - 0.02s 174M
3.13 alpine (musl) - - - -
3.13 slim (glibc) - - 0.01s 173M
3.9 alpine (musl) - - - -
3.9 slim (glibc) - - 0.01s 147M

This example demonstrates how to define a simple floating-point addition function using `llvmlite.ir`, compile it using `llvmlite.binding`, and execute it via `ctypes`. It covers IR construction, module parsing, execution engine setup, and function invocation.

from ctypes import CFUNCTYPE, c_double
from llvmlite import ir
import llvmlite.binding as llvm

# All these initializations are required for JIT compilation
llvm.initialize_native_target()
llvm.initialize_native_asmprinter()

# Define the LLVM IR for a simple function (double fpadd(double a, double b) { return a + b; })
double = ir.DoubleType()
fnty = ir.FunctionType(double, (double, double))

module = ir.Module(name=__file__)
func = ir.Function(module, fnty, name="fpadd")

block = func.append_basic_block(name="entry")
builder = ir.IRBuilder(block)
a, b = func.args
result = builder.fadd(a, b, name="res")
builder.ret(result)

# Convert textual LLVM IR into in-memory representation
llvm_module = llvm.parse_assembly(str(module))
llvm_module.verify()

# Create an ExecutionEngine suitable for JIT code generation
target = llvm.Target.from_default_triple()
target_machine = target.create_target_machine()
engine = llvm.create_mcjit_compiler(llvm_module, target_machine)

# Finalize the engine to make the function available
engine.finalize_object()
engine.run_static_constructors()

# Look up the function pointer and cast it to a C function
func_ptr = engine.get_function_address("fpadd")
cfunc = CFUNCTYPE(c_double, c_double, c_double)(func_ptr)

# Run the function
res = cfunc(1.0, 2.5)
print(f"Result of fpadd(1.0, 2.5): {res}") # Expected: 3.5