Skip to content

Function

function

P = ParamSpec('P') module-attribute

R = TypeVar('R') module-attribute

CFunc

Bases: Protocol

A callable native function pointer.

Source code in xdsl/jit/function.py
11
12
13
14
class CFunc(Protocol):
    """A callable native function pointer."""

    def __call__(self, *args: Any, **kwargs: Any) -> Any: ...

__call__(*args: Any, **kwargs: Any) -> Any

Source code in xdsl/jit/function.py
14
def __call__(self, *args: Any, **kwargs: Any) -> Any: ...

CFuncType

Bases: Protocol

A ctypes function-pointer type.

Source code in xdsl/jit/function.py
17
18
19
20
21
22
23
24
25
26
27
class CFuncType(Protocol):
    """A ctypes function-pointer type."""

    @overload
    def __call__(self) -> CFunc: ...

    @overload
    def __call__(self, address: int, /) -> CFunc: ...

    @overload
    def __call__(self, func: Callable[..., Any], /) -> CFunc: ...

RawJITFunc dataclass

A jitted function exposed as a ctypes callable.

Backends may subclass this to retain native runtime state that must outlive calls through c_func.

Source code in xdsl/jit/function.py
30
31
32
33
34
35
36
37
38
39
40
41
42
43
@dataclass(slots=True)
class RawJITFunc:
    """
    A jitted function exposed as a ctypes callable.

    Backends may subclass this to retain native runtime state that must outlive
    calls through ``c_func``.
    """

    c_func_type: CFuncType
    """The ``CFUNCTYPE`` describing the native calling convention."""

    c_func: CFunc
    """Bound ctypes function object for the native entry point."""

c_func_type: CFuncType instance-attribute

The CFUNCTYPE describing the native calling convention.

c_func: CFunc instance-attribute

Bound ctypes function object for the native entry point.

__init__(c_func_type: CFuncType, c_func: CFunc) -> None

WrappedJITFunc dataclass

Bases: Generic[P, R]

A Python-callable wrapper around a :class:RawJITFunc.

Invoking the instance marshals arguments to ctypes, calls the native function, and converts the result back to a Python value.

Source code in xdsl/jit/function.py
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
@dataclass(slots=True)
class WrappedJITFunc(Generic[P, R]):
    """
    A Python-callable wrapper around a :class:`RawJITFunc`.

    Invoking the instance marshals arguments to ctypes, calls the native function,
    and converts the result back to a Python value.
    """

    raw_func: RawJITFunc
    """Underlying ctypes binding."""

    original_func: Callable[P, R]
    """The undecorated Python function."""

    __call__: Callable[P, R]
    """Marshaling entry point for calls."""

raw_func: RawJITFunc instance-attribute

Underlying ctypes binding.

original_func: Callable[P, R] instance-attribute

The undecorated Python function.

__call__: Callable[P, R] instance-attribute

Marshaling entry point for calls.

__init__(raw_func: RawJITFunc, original_func: Callable[P, R], __call__: Callable[P, R]) -> None

wrap_jit_func(raw_func: RawJITFunc, original_func: Callable[P, R], signature: TypeForm[Callable[P, R]], py_type_context: PyTypeContext) -> WrappedJITFunc[P, R]

Wrap a :class:RawJITFunc as a :class:WrappedJITFunc.

Builds argument/result converters from signature and checks that the resulting CFUNCTYPE matches raw_func.c_func_type.

Source code in xdsl/jit/function.py
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
def wrap_jit_func(
    raw_func: RawJITFunc,
    original_func: Callable[P, R],
    signature: TypeForm[Callable[P, R]],
    py_type_context: PyTypeContext,
) -> WrappedJITFunc[P, R]:
    """
    Wrap a :class:`RawJITFunc` as a :class:`WrappedJITFunc`.

    Builds argument/result converters from ``signature`` and checks that the
    resulting ``CFUNCTYPE`` matches ``raw_func.c_func_type``.
    """
    func_type_map = py_type_context.func_type_map(signature)
    expected_c_func_type = func_type_map.c_func_type()
    mismatched_type = raw_func.c_func_type != expected_c_func_type
    if mismatched_type:
        raise JITException(
            f"CTypes signature from IR ({raw_func.c_func_type}) does not "
            f"match signature from Python TypeMaps ({expected_c_func_type})."
        )

    def fn(*args: P.args, **kwargs: P.kwargs) -> R:
        if kwargs:
            raise JITException("JIT functions do not support keyword arguments.")
        ctype_args = tuple(
            m.to_ctype(a) for m, a in zip(func_type_map.arg_maps, args, strict=True)
        )
        ctype_res = raw_func.c_func(*ctype_args)
        return func_type_map.res_map.from_ctype(ctype_res)

    return WrappedJITFunc(raw_func, original_func, fn)