Thanks to visit codestin.com
Credit goes to github.com

Skip to content

xdslproject/xdsl

Repository files navigation

Build Status for the Core backend PyPI version Downloads Downloads Code Coverage Zulip Status

xDSL: A Python-native SSA Compiler Framework

xDSL is a Python-native framework for building compiler infrastructure. It provides SSA-based intermediate representations (IRs) and Pythonic APIs to define, assemble, and optimize custom IRs—all with seamless compatibility with MLIR from the LLVM project.

Inspired by MLIR, xDSL enables smooth translation of programs and abstractions between frameworks. This lets users prototype compilers entirely in Python, while still accessing MLIR’s powerful optimization and code generation pipeline. All IRs in xDSL employ a unified SSA-based data structure, with regions and basic blocks, making it easy to write generic analyses and transformation passes.

xDSL supports assembling compilers from predefined or custom IRs, and organizing transformations across a multi-level IR stack. This layered approach enables abstraction-specific optimization passes, similar to the architecture of projects like Devito, PSyclone, and Firedrake.

In short, xDSL makes it possible to:

  • Prototype compilers quickly in Python
  • Build DSLs with custom IRs
  • Run analyses and transformations with simple scripts
  • Interoperate smoothly with MLIR and benefit from LLVM's backend

Contents

Installation

To use xDSL as part of a larger project for developing your own compiler, just install xDSL via pip:

pip install xdsl

To quickly install xDSL for development and contribution purposes, use:

pip install xdsl[dev]

This may be useful for projects wanting to replicate the xDSL testing setup. For a more comprehensive experience, follow: xDSL Developer Setup

Note: This version of xDSL is validated against a specific MLIR version, interoperability with other versions is not guaranteed. The supported MLIR version is 20.1.7.

Subprojects With Extra Dependencies

xDSL has a number of subprojects, some of which require extra dependencies. To keep the set of dependencies to a minimum, these extra dependencies have to be specified explicitly, e.g. by using:

pip install xdsl[gui] # or [jax], [riscv]

Getting Started

Check out the dedicated Getting Started guide for a comprehensive tutorial.

To get familiar with xDSL, we recommend starting with our Jupyter notebooks. The notebooks provide hands-on examples and documentation of xDSL's core concepts: data structures, the Python-embedded abstraction definition language, and end-to-end custom compilers construction, like a database compiler. There also exists a small documentation showing how to connect xDSL with MLIR for users interested in that use case.

We provide a Makefile containing a lot of common tasks, which might provide an overview of common actions.

xDSL Developer Setup

To contribute to the development of xDSL follow the subsequent steps.

Developer Installation

We use uv for dependency management of xDSL. See uv's getting started page for more details.

# Ensure uv is installed
uv -v

Then, here are the commands to locally set up your development repository:

# Clone repo
git clone https://github.com/xdslproject/xdsl.git
cd xdsl
# Set up local environment with all optional and dev dependencies
# Creates a virtual environment called `.venv`
make venv
# Set up pre-commit hook for automatic formatting
make precommit-install
# Run all tests to verify installation was successful
make tests

Please take a look at the Makefile for the available commands such as running specific tests, running the documentation website locally, and others.

To make a custom mlir-opt available in the virtual environment, set the XDSL_MLIR_OPT_PATH variable when running make venv, like so:

XDSL_MLIR_OPT_PATH=/PATH/TO/LLVM/BUILD/bin/mlir-opt make venv

Alternative installations

For some use-cases, such as running xDSL with PyPy, it may be preferable to install a minimal set of dependencies instead. This can be done with uv sync. Note that Pyright will then complain about missing dependencies, so run make tests-functional instead of make tests to test the functionality of xDSL.

Testing and benchmarking

The xDSL project uses pytest unit tests and LLVM-style filecheck tests. They can be executed from the root directory:

# Executes pytests which are located in tests/
uv run pytest

# Executes filecheck tests
uv run lit tests/filecheck

# run all tests using makefile
make tests

Benchmarks for the project are tracked in the https://github.com/xdslproject/xdsl-bench repository. These run automatically every day on the main branch, reporting their results to https://xdsl.dev/xdsl-bench/. However, they can also be ran manually by cloning the repository and pointing the submodule at your feature branch to benchmark.

Formatting and Typechecking

All Python code used in xDSL uses ruff to format the code in a uniform manner.

To automate the formatting, we use pre-commit hooks from the pre-commit package.

# Install the pre-commit on your `.git` folder
make precommit-install
# to run the hooks:
make precommit
# alternatively, run ruff directly:
uv run ruff format

When commiting to xDSL, try to pass all Python code through pyright without errors. Pyright checks all staged files through the makefile using make pyright.

Important

Experimental Pyright Features

xDSL currently relies on an experimental feature of Pyright called TypeForm. TypeForm is in discussion and will likely land in some future version of Python.

For xDSL to type check correctly using Pyright, please add this to your pyproject.toml:

[tool.pyright]
enableExperimentalFeatures = true

Discussion

You can also join the discussion at our Zulip chat room, kindly supported by community hosting from Zulip.

About

A Python compiler design toolkit.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 104

Languages