Skip to content

Builtin

builtin

BuiltinFunctions dataclass

Bases: InterpreterFunctions

Source code in xdsl/interpreters/builtin.py
@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(AnyFloat)
    def float_attr_value(
        self, interpreter: Interpreter, attr: Attribute, attr_type: AnyFloat
    ) -> FloatAttr:
        interpreter.interpreter_assert(isa(attr, FloatAttr))
        attr = cast(FloatAttr, attr)
        return attr

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

    @impl_attr(builtin.IndexType)
    def index_attr_value(
        self, interpreter: Interpreter, attr: Attribute, attr_type: builtin.IndexType
    ) -> IntegerAttr[IntegerType]:
        interpreter.interpreter_assert(isa(attr, IntegerAttr))
        attr = cast(IntegerAttr, attr)
        return IntegerAttr(attr.value, interpreter.index_bitwidth, truncate_bits=True)

    @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
@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)
    )

float_attr_value(interpreter: Interpreter, attr: Attribute, attr_type: AnyFloat) -> FloatAttr

Source code in xdsl/interpreters/builtin.py
@impl_attr(AnyFloat)
def float_attr_value(
    self, interpreter: Interpreter, attr: Attribute, attr_type: AnyFloat
) -> FloatAttr:
    interpreter.interpreter_assert(isa(attr, FloatAttr))
    attr = cast(FloatAttr, attr)
    return attr

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

Source code in xdsl/interpreters/builtin.py
@impl_attr(IntegerType)
def integer_attr_value(
    self, interpreter: Interpreter, attr: Attribute, attr_type: IntegerType
) -> IntegerAttr:
    interpreter.interpreter_assert(isa(attr, IntegerAttr))
    attr = cast(IntegerAttr, attr)
    return attr

index_attr_value(interpreter: Interpreter, attr: Attribute, attr_type: builtin.IndexType) -> IntegerAttr[IntegerType]

Source code in xdsl/interpreters/builtin.py
@impl_attr(builtin.IndexType)
def index_attr_value(
    self, interpreter: Interpreter, attr: Attribute, attr_type: builtin.IndexType
) -> IntegerAttr[IntegerType]:
    interpreter.interpreter_assert(isa(attr, IntegerAttr))
    attr = cast(IntegerAttr, attr)
    return IntegerAttr(attr.value, interpreter.index_bitwidth, truncate_bits=True)

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

Source code in xdsl/interpreters/builtin.py
@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))

scalar_attr(value: int | float, value_type: Attribute, index_bitwidth: Literal[32, 64]) -> IntegerAttr[IntegerType] | FloatAttr

Wrap a numeric storage value or computation result as a runtime scalar.

Source code in xdsl/interpreters/builtin.py
def scalar_attr(
    value: int | float, value_type: Attribute, index_bitwidth: Literal[32, 64]
) -> IntegerAttr[IntegerType] | FloatAttr:
    """Wrap a numeric storage value or computation result as a runtime scalar."""
    if isinstance(value_type, builtin.IndexType):
        value_type = IntegerType(index_bitwidth)
    if isa(value_type, IntegerType):
        assert isinstance(value, int)
        return IntegerAttr(value, value_type, truncate_bits=True)
    assert isinstance(value_type, builtin.AnyFloat)
    return FloatAttr(float(value), value_type)

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
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]