xDSL

raw JSON →
0.63.0 verified Fri May 01 auth: no python

xDSL is a Python library for defining and manipulating domain-specific languages (DSLs) with a focus on compiler infrastructure. It provides IR building blocks, pattern rewrites, and verification passes. Current version: 0.63.0. Released monthly.

pip install xdsl
error AttributeError: module 'xdsl' has no attribute 'ir'
cause Import path is wrong; the package does not expose submodules at top level.
fix
Use from xdsl.ir import Block, Region, Operation, SSAValue.
error ValueError: Operation must have at least one operand or result
cause Creating an operation without specifying operands and results in the new API (post 0.60).
fix
Pass operands and result_types when constructing an operation: arith.Addi(operands=[...], result_types=[...]) or use helper methods.
error TypeError: cannot instantiate 'Block' with positional arguments
cause Block no longer accepts operations as positional arguments in newer versions.
fix
Use Block.from_ops([op1, op2]) or Block(ops=[op1, op2]) keyword argument.
breaking In version 0.60.0, the entire IR API was refactored. Operations now require explicit operands and results; the old mutable operation pattern is removed.
fix Use Block.from_ops() with SSAValue references and explicit operands. See migration guide.
deprecated The xdsl.util module is deprecated; use xdsl.utils or xdsl.ir utilities instead.
fix Replace xdsl.util imports with xdsl.utils equivalents.
gotcha SSAValue indices are positional and must match the block's arguments. A common mistake is to assume SSAValue(0) refers to the first operation's result; it refers to the first block argument.
fix Ensure block arguments are defined first, then use SSAValue(index) where index corresponds to the argument list order.

Construct an MLIR-like module with a function containing an add operation.

from xdsl.ir import Block, Region, Operation, SSAValue
from xdsl.dialects.builtin import ModuleOp, IntegerType, FunctionType
from xdsl.dialects import arith, func

# Create a simple function: i32 add
int32 = IntegerType(32)
func_type = FunctionType.from_lists([int32, int32], [int32])

# Build region with add operation
block = Block.from_ops([
    arith.Addi(SSAValue(0), SSAValue(1)),
    func.Return()
])
region = Region(block)

# Wrap in FunctionOp
func_op = func.FuncOp("add", func_type, region)
module = ModuleOp([func_op])
print(module)