Skip to content

Py type context

py_type_context

TypeMap

Bases: NamedTuple

Map a Python type to a C type and optional call-boundary conversions.

Missing converters pass values through unchanged.

Source code in xdsl/jit/py_type_context.py
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class TypeMap(NamedTuple):
    """
    Map a Python type to a C type and optional call-boundary conversions.

    Missing converters pass values through unchanged.
    """

    python_type: type[Any]
    """Python type on the wrapped-function boundary."""

    c_type: str
    """C type used by the native function."""

    to_c: Callable[[Any], Any] | None = None
    """Convert a Python argument before the native call."""

    from_c: Callable[[Any], Any] | None = None
    """Convert the native result back to a Python value."""

python_type: type[Any] instance-attribute

Python type on the wrapped-function boundary.

c_type: str instance-attribute

C type used by the native function.

to_c: Callable[[Any], Any] | None = None class-attribute instance-attribute

Convert a Python argument before the native call.

from_c: Callable[[Any], Any] | None = None class-attribute instance-attribute

Convert the native result back to a Python value.

FuncTypeMap

Bases: NamedTuple

Per-argument and result :class:TypeMap entries for a function signature.

Source code in xdsl/jit/py_type_context.py
30
31
32
33
34
35
36
37
38
39
40
41
class FuncTypeMap(NamedTuple):
    """Per-argument and result :class:`TypeMap` entries for a function signature."""

    arg_maps: tuple[TypeMap, ...]
    res_map: TypeMap

    def c_func_type(self) -> CFuncSignature:
        """Return the C function signature."""
        return CFuncSignature(
            tuple(type_map.c_type for type_map in self.arg_maps),
            self.res_map.c_type,
        )

arg_maps: tuple[TypeMap, ...] instance-attribute

res_map: TypeMap instance-attribute

c_func_type() -> CFuncSignature

Return the C function signature.

Source code in xdsl/jit/py_type_context.py
36
37
38
39
40
41
def c_func_type(self) -> CFuncSignature:
    """Return the C function signature."""
    return CFuncSignature(
        tuple(type_map.c_type for type_map in self.arg_maps),
        self.res_map.c_type,
    )

PyTypeContext

Registry of Python types to :class:TypeMap entries.

Configures native call signatures and optional value conversions when wrapping a :class:RawJITFunc. Registrations must agree with the frontend type mapping and with :class:~xdsl.jit.c_type_context.CTypeContext for the same logical types.

Source code in xdsl/jit/py_type_context.py
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
class PyTypeContext:
    """
    Registry of Python types to :class:`TypeMap` entries.

    Configures native call signatures and optional value conversions when wrapping
    a :class:`RawJITFunc`. Registrations must agree with the frontend type mapping and
    with
    :class:`~xdsl.jit.c_type_context.CTypeContext` for the same logical types.
    """

    _mapping: dict[type[Any], TypeMap]

    def __init__(self):
        self._mapping = {}

    def register_type_map(self, type_map: TypeMap):
        """Register a :class:`TypeMap` for its ``python_type``."""
        self._mapping[type_map.python_type] = type_map

    def type_map(self, python_type: type[Any]) -> TypeMap:
        """Return the :class:`TypeMap` for ``python_type``."""
        try:
            return self._mapping[python_type]
        except KeyError:
            raise JITException(f"No type map for Python type: {python_type}")

    def func_type_map(self, signature: TypeForm[Callable[..., Any]]) -> FuncTypeMap:
        """Build a :class:`FuncTypeMap` from a ``Callable`` signature."""
        match get_args(signature):
            case [[*param_types], return_type]:
                return FuncTypeMap(
                    tuple(self.type_map(py_type) for py_type in param_types),
                    self.type_map(return_type),
                )
            case _:
                raise JITException(f"Unsupported signature: {signature}")

__init__()

Source code in xdsl/jit/py_type_context.py
56
57
def __init__(self):
    self._mapping = {}

register_type_map(type_map: TypeMap)

Register a :class:TypeMap for its python_type.

Source code in xdsl/jit/py_type_context.py
59
60
61
def register_type_map(self, type_map: TypeMap):
    """Register a :class:`TypeMap` for its ``python_type``."""
    self._mapping[type_map.python_type] = type_map

type_map(python_type: type[Any]) -> TypeMap

Return the :class:TypeMap for python_type.

Source code in xdsl/jit/py_type_context.py
63
64
65
66
67
68
def type_map(self, python_type: type[Any]) -> TypeMap:
    """Return the :class:`TypeMap` for ``python_type``."""
    try:
        return self._mapping[python_type]
    except KeyError:
        raise JITException(f"No type map for Python type: {python_type}")

func_type_map(signature: TypeForm[Callable[..., Any]]) -> FuncTypeMap

Build a :class:FuncTypeMap from a Callable signature.

Source code in xdsl/jit/py_type_context.py
70
71
72
73
74
75
76
77
78
79
def func_type_map(self, signature: TypeForm[Callable[..., Any]]) -> FuncTypeMap:
    """Build a :class:`FuncTypeMap` from a ``Callable`` signature."""
    match get_args(signature):
        case [[*param_types], return_type]:
            return FuncTypeMap(
                tuple(self.type_map(py_type) for py_type in param_types),
                self.type_map(return_type),
            )
        case _:
            raise JITException(f"Unsupported signature: {signature}")