Skip to content

Py type context

py_type_context

TypeMap

Bases: NamedTuple

Correspondence between a Python type and its ctypes representation.

to_ctype / from_ctype convert values at call boundaries.

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):
    """
    Correspondence between a Python type and its ctypes representation.

    ``to_ctype`` / ``from_ctype`` convert values at call boundaries.
    """

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

    ctype_type: type[Any]
    """ctypes type used in the native ``CFUNCTYPE``."""

    to_ctype: Callable[[Any], Any]
    """Convert a Python argument to a ctypes-compatible value."""

    from_ctype: Callable[[Any], Any]
    """Convert a ctypes result back to a Python value."""

python_type: type[Any] instance-attribute

Python type on the wrapped-function boundary.

ctype_type: type[Any] instance-attribute

ctypes type used in the native CFUNCTYPE.

to_ctype: Callable[[Any], Any] instance-attribute

Convert a Python argument to a ctypes-compatible value.

from_ctype: Callable[[Any], Any] instance-attribute

Convert a ctypes 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
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):
        """Return the ``CFUNCTYPE`` for this signature."""
        return CFUNCTYPE(
            self.res_map.ctype_type, *(m.ctype_type for m in self.arg_maps)
        )

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

res_map: TypeMap instance-attribute

c_func_type()

Return the CFUNCTYPE for this signature.

Source code in xdsl/jit/py_type_context.py
36
37
38
39
40
def c_func_type(self):
    """Return the ``CFUNCTYPE`` for this signature."""
    return CFUNCTYPE(
        self.res_map.ctype_type, *(m.ctype_type for m in self.arg_maps)
    )

PyTypeContext

Registry of Python types to :class:TypeMap entries.

Used to marshal values 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
43
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
class PyTypeContext:
    """
    Registry of Python types to :class:`TypeMap` entries.

    Used to marshal values 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
54
55
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
57
58
59
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
61
62
63
64
65
66
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
68
69
70
71
72
73
74
75
76
77
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}")