Skip to content

C type context

c_type_context

CTypeContext

Registry of xDSL attribute classes to ctypes converters.

Source code in xdsl/jit/c_type_context.py
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
class CTypeContext:
    """Registry of xDSL attribute classes to ctypes converters."""

    registry: dict[type[Attribute], Callable[[Any], Any]]
    """Map from an xDSL attribute class to a converter producing its ctypes type."""

    def __init__(self) -> None:
        self.registry = {}

    def register_ctype(
        self,
        attr_type: type[Attribute],
        converter: Callable[[Any], Any],
    ) -> None:
        """Register how ``attr_type`` instances map to a ctypes type."""
        self.registry[attr_type] = converter

    def to_ctype(self, type_attr: Attribute) -> Any:
        """Return the ctypes type class for ``type_attr``."""
        try:
            converter = self.registry[type(type_attr)]
        except KeyError:
            raise JITException(f"No ctypes mapping for type: {type_attr}")
        return converter(type_attr)

    def to_c_func_type(self, inputs: Iterable[Attribute], output: Attribute):
        """Build a ``CFUNCTYPE`` from IR argument and result types."""
        return ctypes.CFUNCTYPE(
            self.to_ctype(output), *(self.to_ctype(arg) for arg in inputs)
        )

registry: dict[type[Attribute], Callable[[Any], Any]] = {} instance-attribute

Map from an xDSL attribute class to a converter producing its ctypes type.

__init__() -> None

Source code in xdsl/jit/c_type_context.py
16
17
def __init__(self) -> None:
    self.registry = {}

register_ctype(attr_type: type[Attribute], converter: Callable[[Any], Any]) -> None

Register how attr_type instances map to a ctypes type.

Source code in xdsl/jit/c_type_context.py
19
20
21
22
23
24
25
def register_ctype(
    self,
    attr_type: type[Attribute],
    converter: Callable[[Any], Any],
) -> None:
    """Register how ``attr_type`` instances map to a ctypes type."""
    self.registry[attr_type] = converter

to_ctype(type_attr: Attribute) -> Any

Return the ctypes type class for type_attr.

Source code in xdsl/jit/c_type_context.py
27
28
29
30
31
32
33
def to_ctype(self, type_attr: Attribute) -> Any:
    """Return the ctypes type class for ``type_attr``."""
    try:
        converter = self.registry[type(type_attr)]
    except KeyError:
        raise JITException(f"No ctypes mapping for type: {type_attr}")
    return converter(type_attr)

to_c_func_type(inputs: Iterable[Attribute], output: Attribute)

Build a CFUNCTYPE from IR argument and result types.

Source code in xdsl/jit/c_type_context.py
35
36
37
38
39
def to_c_func_type(self, inputs: Iterable[Attribute], output: Attribute):
    """Build a ``CFUNCTYPE`` from IR argument and result types."""
    return ctypes.CFUNCTYPE(
        self.to_ctype(output), *(self.to_ctype(arg) for arg in inputs)
    )

register_builtin_ctypes(ctx: CTypeContext) -> None

Source code in xdsl/jit/c_type_context.py
59
60
61
62
63
def register_builtin_ctypes(ctx: CTypeContext) -> None:
    ctx.register_ctype(Float32Type, lambda _: ctypes.c_float)
    ctx.register_ctype(Float64Type, lambda _: ctypes.c_double)
    ctx.register_ctype(IntegerType, _int_to_ctype)
    ctx.register_ctype(NoneType, lambda _: None)