Skip to content

C type context

c_type_context

CFuncSignature

Bases: NamedTuple

C function signature.

Source code in xdsl/jit/c_type_context.py
 9
10
11
12
13
class CFuncSignature(NamedTuple):
    """C function signature."""

    inputs: tuple[str, ...]
    output: str

inputs: tuple[str, ...] instance-attribute

output: str instance-attribute

CTypeContext

Registry of xDSL attribute classes to C type converters.

Source code in xdsl/jit/c_type_context.py
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
class CTypeContext:
    """Registry of xDSL attribute classes to C type converters."""

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

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

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

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

    def to_c_func_type(
        self, inputs: Iterable[Attribute], output: Attribute
    ) -> CFuncSignature:
        """Build a C function signature from IR argument and result types."""
        return CFuncSignature(
            tuple(self.to_c_type(arg) for arg in inputs), self.to_c_type(output)
        )

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

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

__init__() -> None

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

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

Register how attr_type instances map to a C type.

Source code in xdsl/jit/c_type_context.py
25
26
27
28
29
30
31
def register_type(
    self,
    attr_type: type[Attribute],
    converter: Callable[[Any], str],
) -> None:
    """Register how ``attr_type`` instances map to a C type."""
    self.registry[attr_type] = converter

to_c_type(type_attr: Attribute) -> str

Return the C type for type_attr.

Source code in xdsl/jit/c_type_context.py
33
34
35
36
37
38
39
def to_c_type(self, type_attr: Attribute) -> str:
    """Return the C type for ``type_attr``."""
    try:
        converter = self.registry[type(type_attr)]
    except KeyError:
        raise JITException(f"No C type mapping for type: {type_attr}")
    return converter(type_attr)

to_c_func_type(inputs: Iterable[Attribute], output: Attribute) -> CFuncSignature

Build a C function signature from IR argument and result types.

Source code in xdsl/jit/c_type_context.py
41
42
43
44
45
46
47
def to_c_func_type(
    self, inputs: Iterable[Attribute], output: Attribute
) -> CFuncSignature:
    """Build a C function signature from IR argument and result types."""
    return CFuncSignature(
        tuple(self.to_c_type(arg) for arg in inputs), self.to_c_type(output)
    )

register_builtin_types(ctx: CTypeContext) -> None

Source code in xdsl/jit/c_type_context.py
67
68
69
70
71
def register_builtin_types(ctx: CTypeContext) -> None:
    ctx.register_type(Float32Type, lambda _: "float")
    ctx.register_type(Float64Type, lambda _: "double")
    ctx.register_type(IntegerType, _int_to_c_type)
    ctx.register_type(NoneType, lambda _: "void")