Skip to content

Builtin

builtin

BuiltinFunctions dataclass

Bases: InterpreterFunctions

Source code in xdsl/interpreters/builtin.py
42
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
@register_impls
class BuiltinFunctions(InterpreterFunctions):
    @impl(UnrealizedConversionCastOp)
    def run_cast(
        self,
        interpreter: Interpreter,
        op: UnrealizedConversionCastOp,
        args: tuple[Any, ...],
    ):
        return tuple(
            interpreter.cast_value(o.type, r.type, arg)
            for (o, r, arg) in zip(op.operands, op.results, args)
        )

    @impl_attr(Float64Type)
    def float64_attr_value(
        self, interpreter: Interpreter, attr: Attribute, attr_type: Float64Type
    ) -> float:
        interpreter.interpreter_assert(isa(attr, FloatAttr))
        attr = cast(FloatAttr, attr)
        return attr.value.data

    @impl_attr(Float32Type)
    def float32_attr_value(
        self, interpreter: Interpreter, attr: Attribute, attr_type: Float32Type
    ) -> float:
        interpreter.interpreter_assert(isa(attr, FloatAttr))
        attr = cast(FloatAttr, attr)
        return attr.value.data

    @impl_attr(IntegerType)
    def integer_attr_value(
        self, interpreter: Interpreter, attr: Attribute, attr_type: IntegerType
    ) -> float:
        interpreter.interpreter_assert(isa(attr, IntegerAttr))
        attr = cast(IntegerAttr, attr)
        return attr.value.data

    @impl_attr(builtin.MemRefType)
    def dense_int_or_fp_elements_value(
        self,
        interpreter: Interpreter,
        attr: Attribute,
        type_attr: builtin.MemRefType[Any],
    ) -> ShapedArray[Any]:
        assert isa(attr, builtin.DenseIntOrFPElementsAttr)
        shape = attr.get_shape()
        data = attr.get_values()
        data_ptr = ptr.TypedPtr[Any].new(
            data,
            xtype=xtype_for_el_type(
                attr.get_element_type(), interpreter.index_bitwidth
            ),
        )
        return ShapedArray(data_ptr, list(shape))

run_cast(interpreter: Interpreter, op: UnrealizedConversionCastOp, args: tuple[Any, ...])

Source code in xdsl/interpreters/builtin.py
44
45
46
47
48
49
50
51
52
53
54
@impl(UnrealizedConversionCastOp)
def run_cast(
    self,
    interpreter: Interpreter,
    op: UnrealizedConversionCastOp,
    args: tuple[Any, ...],
):
    return tuple(
        interpreter.cast_value(o.type, r.type, arg)
        for (o, r, arg) in zip(op.operands, op.results, args)
    )

float64_attr_value(interpreter: Interpreter, attr: Attribute, attr_type: Float64Type) -> float

Source code in xdsl/interpreters/builtin.py
56
57
58
59
60
61
62
@impl_attr(Float64Type)
def float64_attr_value(
    self, interpreter: Interpreter, attr: Attribute, attr_type: Float64Type
) -> float:
    interpreter.interpreter_assert(isa(attr, FloatAttr))
    attr = cast(FloatAttr, attr)
    return attr.value.data

float32_attr_value(interpreter: Interpreter, attr: Attribute, attr_type: Float32Type) -> float

Source code in xdsl/interpreters/builtin.py
64
65
66
67
68
69
70
@impl_attr(Float32Type)
def float32_attr_value(
    self, interpreter: Interpreter, attr: Attribute, attr_type: Float32Type
) -> float:
    interpreter.interpreter_assert(isa(attr, FloatAttr))
    attr = cast(FloatAttr, attr)
    return attr.value.data

integer_attr_value(interpreter: Interpreter, attr: Attribute, attr_type: IntegerType) -> float

Source code in xdsl/interpreters/builtin.py
72
73
74
75
76
77
78
@impl_attr(IntegerType)
def integer_attr_value(
    self, interpreter: Interpreter, attr: Attribute, attr_type: IntegerType
) -> float:
    interpreter.interpreter_assert(isa(attr, IntegerAttr))
    attr = cast(IntegerAttr, attr)
    return attr.value.data

dense_int_or_fp_elements_value(interpreter: Interpreter, attr: Attribute, type_attr: builtin.MemRefType[Any]) -> ShapedArray[Any]

Source code in xdsl/interpreters/builtin.py
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
@impl_attr(builtin.MemRefType)
def dense_int_or_fp_elements_value(
    self,
    interpreter: Interpreter,
    attr: Attribute,
    type_attr: builtin.MemRefType[Any],
) -> ShapedArray[Any]:
    assert isa(attr, builtin.DenseIntOrFPElementsAttr)
    shape = attr.get_shape()
    data = attr.get_values()
    data_ptr = ptr.TypedPtr[Any].new(
        data,
        xtype=xtype_for_el_type(
            attr.get_element_type(), interpreter.index_bitwidth
        ),
    )
    return ShapedArray(data_ptr, list(shape))

xtype_for_el_type(el_type: Attribute, index_bitwidth: Literal[32, 64]) -> PackableType[Any]

Returns the datatype to use during interpretation for a given value. For index, uses either i32 or i64, depending on the index_bitwidth. For all other types returns the input, as long as it's a subclass of PackableType, raising an error otherwise.

Source code in xdsl/interpreters/builtin.py
26
27
28
29
30
31
32
33
34
35
36
37
38
39
def xtype_for_el_type(
    el_type: Attribute, index_bitwidth: Literal[32, 64]
) -> PackableType[Any]:
    """
    Returns the datatype to use during interpretation for a given value.
    For `index`, uses either `i32` or `i64`, depending on the `index_bitwidth`.
    For all other types returns the input, as long as it's a subclass of
    `PackableType`, raising an error otherwise.
    """
    if isinstance(el_type, builtin.IndexType):
        return ptr.index(index_bitwidth)
    if not isinstance(el_type, PackableType):
        raise NotImplementedError(f"Unknown format for element type {el_type}")
    return el_type  # pyright: ignore[reportUnknownVariableType]