Skip to content

Type

type

Type utilities.

get_element_type_or_self(maybe_container_type: Attribute) -> Attribute

If the input is a ContainerType, then returns it's element type, otherwise returns input.

Source code in xdsl/utils/type.py
16
17
18
19
20
21
22
23
def get_element_type_or_self(maybe_container_type: Attribute) -> Attribute:
    """
    If the input is a `ContainerType`, then returns it's element type, otherwise returns
    input.
    """
    if isa(maybe_container_type, ContainerType):
        return maybe_container_type.get_element_type()
    return maybe_container_type

get_encoding(maybe_shaped_type: Attribute) -> Attribute

Source code in xdsl/utils/type.py
26
27
28
29
def get_encoding(maybe_shaped_type: Attribute) -> Attribute:
    if isinstance(maybe_shaped_type, TensorType):
        return maybe_shaped_type.encoding
    return NoneAttr()

have_compatible_shape(lhs_type: Attribute, rhs_type: Attribute) -> bool

Source code in xdsl/utils/type.py
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
def have_compatible_shape(lhs_type: Attribute, rhs_type: Attribute) -> bool:
    is_lhs_container = isinstance(lhs_type, ContainerType)
    is_rhs_container = isinstance(rhs_type, ContainerType)

    # both are scalars
    if not is_lhs_container and not is_rhs_container:
        return True

    # one is scalar and the other shaped
    if is_lhs_container != is_rhs_container:
        return False

    # at least one is unranked
    if not isinstance(lhs_type, ShapedType) or not isinstance(rhs_type, ShapedType):
        return True

    # both ranked, so check ranks
    if lhs_type.get_num_dims() != rhs_type.get_num_dims():
        return False

    return all(
        dim1 == DYNAMIC_INDEX or dim2 == DYNAMIC_INDEX or dim1 == dim2
        for dim1, dim2 in zip(lhs_type.get_shape(), rhs_type.get_shape())
    )