Interpreter
interpreter
¶
PythonValues: TypeAlias = tuple[Any, ...]
module-attribute
¶
A tuple of result values corresponding to the results of the operation being interpreted.
The standard dialect implementations represent integer scalars with IntegerAttr
and floating-point scalars with FloatAttr, carrying the SSA value's type. Index
values use IntegerAttr with an i32 or i64 type according to index_bitwidth.
Conditions use IntegerAttr.__bool__, which tests whether the payload is nonzero.
ShapedArray and TypedPtr keep native numeric storage; scalar loads and region
arguments are wrapped, and stores unwrap attribute payloads. Callers and external
function implementations must use the same scalar representations.
Other types have dialect-specific representations; the interpreter dispatch itself does not impose a representation on custom implementations.
TerminatorValue: TypeAlias = ReturnedValues | Successor
module-attribute
¶
A terminator operation either yields control to the parent operation or jumps to a successor.
NonTerminatorOpImpl: TypeAlias = Callable[[_FT, 'Interpreter', OperationInvT, PythonValues], PythonValues]
module-attribute
¶
TerminatorOpImpl: TypeAlias = Callable[[_FT, 'Interpreter', OperationInvT, PythonValues], tuple[TerminatorValue, PythonValues]]
module-attribute
¶
OpImpl: TypeAlias = Callable[[_FT, 'Interpreter', OperationInvT, PythonValues], OpImplResult]
module-attribute
¶
CastImpl: TypeAlias = Callable[[_FT, _AttributeInvT0, _AttributeInvT1, Any], Any]
module-attribute
¶
AttrImpl: TypeAlias = Callable[[_FT, 'Interpreter', Attribute, AttributeInvT], Any]
module-attribute
¶
ExtFuncImpl: TypeAlias = Callable[[_FT, 'Interpreter', Operation, PythonValues], PythonValues]
module-attribute
¶
P = ParamSpec('P')
module-attribute
¶
AttributeInvNoDefaultT = TypeVar('AttributeInvNoDefaultT', bound=Attribute)
module-attribute
¶
ReturnedValues
¶
Bases: NamedTuple
If the terminator exits the region being interpreted, such as for function returns, return these values to yield control back to the parent operation of the block.
Source code in xdsl/interpreter.py
values: PythonValues
instance-attribute
¶
Successor
¶
Bases: NamedTuple
If the terminator jumps to another block within the region being interpreted, such as for jumps, return a block along with the values to pass as arguments.
Source code in xdsl/interpreter.py
OpImplResult
¶
Bases: NamedTuple
The result of interpreting an Operation. If and only if the Operation is a terminator, it must set the terminator_value.
Source code in xdsl/interpreter.py
InterpreterFunctions
dataclass
¶
Holds the Python implementations for Operations. Users should subclass this class, and define the functions to run during interpretation. For example:
@register_impls
class ArithFunctions(InterpreterFunctions):
@impl(arith.Addi)
def run_addi(self, interpreter: Interpreter, op: arith.Addi,
args: tuple[Any, ...]) -> tuple[Any, ...]:
lhs, rhs = args
return lhs + rhs,
The interpreter will take care of fetching the Python values associated with the operand SSAValues, and setting the return values to the appropriate OpResults.
To override the definition of an operation implementation, subclass the
class to override, and redefine the functions, annotating them with
@impl.
@register_impls
class DebugArithFunctions(ArithFunctions):
@impl(arith.Addi)
def run_addi(self, interpreter: Interpreter, op: arith.Addi,
args: tuple[Any, ...]) -> tuple[Any, ...]:
lhs, rhs = args
print(lhs, rhs, lhs + rhs)
return lhs + rhs,
To register an implementation of a cast for UnrealizedConversionCastOp, use
impl_cast, like so:
@register_impls
class ArithFunctions(InterpreterFunctions):
@impl_cast(IntegerType, IndexType)
def cast_integer_to_index(
self,
input_type: IntegerType,
output_type: IndexType,
value: Any,
) -> Any:
# Both input and output represented by a Python `int`
return value
Source code in xdsl/interpreter.py
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 | |
__init__() -> None
¶
Interpreter
dataclass
¶
An extensible interpreter, initialised with a Module to interpret. The
implementation for each Operation subclass should be provided via a
InterpretationFunctions instance. Interpretations can be overridden, and
the override must be specified explicitly, by passing override=True to
the register_functions method.
Source code in xdsl/interpreter.py
670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 | |
SYSTEM_BITWIDTH: Literal[32, 64] | None = _get_system_bitwidth()
class-attribute
¶
DEFAULT_BITWIDTH: Literal[32, 64] = 32 if SYSTEM_BITWIDTH is None else SYSTEM_BITWIDTH
class-attribute
¶
module: ModuleOp
instance-attribute
¶
index_bitwidth: Literal[32, 64] = field(default=DEFAULT_BITWIDTH)
class-attribute
instance-attribute
¶
Number of bits in the binary representation of the index
file: IO[str] | None = field(default=None)
class-attribute
instance-attribute
¶
listeners: tuple[Listener, ...] = field(default=())
class-attribute
instance-attribute
¶
Listener
¶
Base class for observing the operations that are interpreted during a run.
Source code in xdsl/interpreter.py
will_interpret_op(op: Operation, args: PythonValues) -> None
¶
Source code in xdsl/interpreter.py
__init__(module: ModuleOp, index_bitwidth: Literal[32, 64] = DEFAULT_BITWIDTH, _impls: _InterpreterFunctionImpls = _InterpreterFunctionImpls(), _ctx: ScopedDict[SSAValue, Any] = (lambda: ScopedDict[SSAValue, Any](name='root'))(), file: IO[str] | None = None, _symbol_table: dict[str, Operation] | None = None, _impl_data: _IMPL_DATA = _IMPL_DATA(), listeners: tuple[Listener, ...] = ()) -> None
¶
get_values(values: Iterable[SSAValue]) -> tuple[Any, ...]
¶
set_values(pairs: Iterable[tuple[SSAValue, Any]])
¶
Set values to current scope. Raises InterpretationError if len(ssa_values) != len(result_values), or if SSA value already has a Python value in the current scope.
Source code in xdsl/interpreter.py
push_scope(name: str | None = None) -> None
¶
Create new scope in current environment, with optional custom name.
pop_scope() -> None
¶
Discard the current scope, and all the values registered in it. Sets parent scope of current scope to new current scope. Raises InterpretationError if current scope is root scope.
Source code in xdsl/interpreter.py
register_implementations(impls: InterpreterFunctions, /, override: bool = False) -> None
¶
Register implementations for operations defined in given
InterpreterFunctions object. Raise InterpretationError if an
operation already has an implementation registered, unless override is
set to True.
Source code in xdsl/interpreter.py
run_op(op: Operation | str | SymbolRefAttr, inputs: PythonValues = ()) -> PythonValues
¶
Calls the implementation for the given operation.
Source code in xdsl/interpreter.py
call_op(op: Operation | str | SymbolRefAttr, inputs: PythonValues = ()) -> PythonValues
¶
Calls the implementation for the given operation.
Source code in xdsl/interpreter.py
call_external(sym_name: str, op: Operation, inputs: PythonValues = ()) -> PythonValues
¶
run_ssacfg_region(region: Region, args: PythonValues, name: str = 'unknown') -> PythonValues
¶
Interpret an SSACFG-semantic Region. Creates a new scope, then executes the first block in the region. The first block is expected to return the results of the region directly.
Source code in xdsl/interpreter.py
cast_value(o: Attribute, r: Attribute, value: Any) -> Any
¶
If the type of the operand and result are not the same, then look up the user-provided conversion function.
Source code in xdsl/interpreter.py
value_for_attribute(attr: Attribute, type_attr: Attribute) -> Any
¶
get_op_for_symbol(symbol: str | SymbolRefAttr) -> Operation
¶
get_data(functions: type[InterpreterFunctions], key: str, factory: Callable[[], Any]) -> Any
¶
Get data associated with a specific interpreter functions class, with a given key.
If the data is missing, the factory argument will be executed and stored on the
interpreter.
Source code in xdsl/interpreter.py
set_data(functions: type[InterpreterFunctions], key: str, value: Any)
¶
Source code in xdsl/interpreter.py
print(*args: Any, **kwargs: Any)
¶
interpreter_assert(condition: bool, message: str | None = None)
¶
scope_names()
¶
OpCounter
dataclass
¶
impl(op_type: type[OperationInvT]) -> Callable[[NonTerminatorOpImpl[_FT, OperationInvT]], OpImpl[_FT, OperationInvT]]
¶
Marks the Python implementation of an xDSL Operation instance, to be used
by an Interpreter. The Interpreter will fetch the Python values
associated with the operands from the current environment, and pass them as
the args parameter. The returned values are assigned to the results
values.
See InterpreterFunctions
Source code in xdsl/interpreter.py
impl_terminator(op_type: type[OperationInvT]) -> Callable[[TerminatorOpImpl[_FT, OperationInvT]], OpImpl[_FT, OperationInvT]]
¶
Marks the Python implementation of an xDSL Operation instance, to be used
by an Interpreter. The Interpreter will fetch the Python values
associated with the operands from the current environment, and pass them as
the args parameter. The returned values are assigned to the results
values.
See InterpreterFunctions
Source code in xdsl/interpreter.py
impl_cast(input_type: type[_AttributeInvT0], output_type: type[_AttributeInvT1]) -> Callable[[CastImpl[_FT, _AttributeInvT0, _AttributeInvT1]], CastImpl[_FT, _AttributeInvT0, _AttributeInvT1]]
¶
Marks the Python implementation of a value cast from one type to another. The
cast_value method on Interpreter will call into this implementation for matching
input and output types.
See InterpreterFunctions for more documentation.
Source code in xdsl/interpreter.py
impl_attr(input_type: IRDLAttrConstraint[AttributeInvNoDefaultT]) -> Callable[[AttrImpl[_FT, AttributeInvNoDefaultT]], AttrImpl[_FT, AttributeInvNoDefaultT]]
¶
Marks the conversion from an attribute to a Python value. The
value_for_attribute method on Interpreter will call into this implementation for
matching input and output types. The input must convert to a BaseAttr or an
AnyOf of BaseAttr constraints, all with runtime-final attribute classes.
Each class is registered separately for exact-class lookup. Constraints on
attribute values or parameters are not supported.
See InterpreterFunctions for more documentation.
Source code in xdsl/interpreter.py
impl_external(sym_name: str) -> Callable[[ExtFuncImpl[_FT]], ExtFuncImpl[_FT]]
¶
Marks the Python implementation of an external function.
Source code in xdsl/interpreter.py
impl_callable(op_type: type[OperationInvT]) -> Callable[[NonTerminatorOpImpl[_FT, OperationInvT]], NonTerminatorOpImpl[_FT, OperationInvT]]
¶
Marks the Python implementation of a callable operation.
Source code in xdsl/interpreter.py
register_impls(ft: type[_FT]) -> type[_FT]
¶
Enumerates the methods on a given class, and registers the ones marked with
@impl in a way that an Interpreter instance can find them for dynamic
dispatch during interpretation.
See InterpreterFunctions