Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core: add CallableOpInterface and use in interpreter #1166

Merged
merged 1 commit into from
Jun 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/Toy/toy/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def main(path: Path, emit: str):
if emit == "interpret":
interpreter = Interpreter(module_op)
interpreter.register_implementations(ToyFunctions())
interpreter.run_op("main", ())
interpreter.call_op("main", ())
return

print(f"Unknown option {emit}")
Expand Down
17 changes: 15 additions & 2 deletions docs/Toy/toy/dialects/toy.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,13 @@
from xdsl.utils.exceptions import VerifyException
from xdsl.utils.hints import isa

from xdsl.traits import Pure, OpTrait, SymbolOpInterface, IsTerminator
from xdsl.traits import (
CallableOpInterface,
Pure,
OpTrait,
SymbolOpInterface,
IsTerminator,
)

TensorTypeF64: TypeAlias = TensorType[Float64Type]
UnrankedTensorTypeF64: TypeAlias = UnrankedTensorType[Float64Type]
Expand Down Expand Up @@ -167,6 +173,13 @@ def verify_(self):
)


class FuncOpCallableInterface(CallableOpInterface):
@classmethod
def get_callable_region(cls, op: Operation) -> Region:
assert isinstance(op, FuncOp)
return op.body


@irdl_op_definition
class FuncOp(IRDLOperation):
"""
Expand All @@ -191,7 +204,7 @@ class FuncOp(IRDLOperation):
function_type: FunctionType = attr_def(FunctionType)
sym_visibility: StringAttr | None = opt_attr_def(StringAttr)

traits = frozenset((SymbolOpInterface(),))
traits = frozenset((SymbolOpInterface(), FuncOpCallableInterface()))
webmiche marked this conversation as resolved.
Show resolved Hide resolved

def __init__(
self,
Expand Down
13 changes: 1 addition & 12 deletions docs/Toy/toy/interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,6 @@ def run_print(
interpreter.print(f"{args[0]}")
return ()

@impl(toy.FuncOp)
def run_func(
self, interpreter: Interpreter, op: toy.FuncOp, args: tuple[Any, ...]
) -> tuple[Any, ...]:
results = interpreter.run_ssacfg_region(op.body, args, op.sym_name.data)
interpreter.interpreter_assert(
results is not None, f"Expected toy.func region to have a terminator"
)
assert results is not None
return results

@impl(toy.ConstantOp)
def run_const(
self, interpreter: Interpreter, op: toy.ConstantOp, args: tuple[Any, ...]
Expand Down Expand Up @@ -118,7 +107,7 @@ def run_return(
def run_generic_call(
self, interpreter: Interpreter, op: toy.GenericCallOp, args: tuple[Any, ...]
) -> tuple[Any, ...]:
return interpreter.run_op(op.callee.string_value(), args)
return interpreter.call_op(op.callee.string_value(), args)

@impl(toy.TransposeOp)
def run_transpose(
Expand Down
30 changes: 28 additions & 2 deletions xdsl/interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from xdsl.dialects.builtin import ModuleOp
from xdsl.ir import OperationInvT, SSAValue, Operation
from xdsl.ir.core import Block, Region
from xdsl.traits import IsTerminator, SymbolOpInterface
from xdsl.traits import CallableOpInterface, IsTerminator, SymbolOpInterface
from xdsl.utils.exceptions import InterpretationError


Expand Down Expand Up @@ -323,13 +323,39 @@ def run_op(self, op: Operation | str, inputs: PythonValues) -> PythonValues:
"""
Calls the implementation for the given operation.
"""
# TODO: replace this with CallableOp trait implementation.
if isinstance(op, str):
op = self.get_op_for_symbol(op)

result = self._impls.run(self, op, inputs)
return result.values

def call_op(self, op: Operation | str, inputs: PythonValues) -> PythonValues:
"""
Calls the implementation for the given operation.
"""
if isinstance(op, str):
name = op
op = self.get_op_for_symbol(op)
Comment on lines +336 to +338
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's a trap!
CallableOp is not SymbolOp. But I've always seen them used in pairs.. not sure what to do in the long run, but I would just restrict call_op to CallableOp here

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like it should be ok, this function accepts ops that are not symbol ops

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh right, it's not the proper SymbolRefAttr fetching just yet.
I can leave with polishing later then 👍

else:
name = "unknown"

interface = op.get_trait(CallableOpInterface)

self.interpreter_assert(
interface is not None,
f"Operation {op.name} does not have trait CallableOpInterface",
)
assert interface is not None

body = interface.get_callable_region(op)

results = self.run_ssacfg_region(body, inputs, name)
self.interpreter_assert(
results is not None, f"Expected {op.name} body to have a terminator"
)
assert results is not None
return results

def run_block(self, block: Block, args: PythonValues) -> PythonValues | None:
"""
Interpret a basic block, using `args` as the block argument values.
Expand Down
19 changes: 19 additions & 0 deletions xdsl/traits.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations
from dataclasses import dataclass, field
import abc
from xdsl.utils.exceptions import VerifyException
from typing import (
TYPE_CHECKING,
Expand Down Expand Up @@ -150,3 +151,21 @@ def verify(self, op: Operation) -> None:
f'Operation {op.name} must have a "sym_name" attribute of type '
f"`StringAttr` to conform to {SymbolOpInterface.__name__}"
)


class CallableOpInterface(OpTrait, abc.ABC):
"""
Interface for function-like Operations that can be called in a generic way.

Please see MLIR documentation for CallOpInterface and CallableOpInterface for more
information.

https://mlir.llvm.org/docs/Interfaces/#callinterfaces
"""

@classmethod
def get_callable_region(cls, op: Operation) -> Region:
webmiche marked this conversation as resolved.
Show resolved Hide resolved
"""
Returns the body of the operation
"""
raise NotImplementedError