Skip to content

Commit

Permalink
add back factory
Browse files Browse the repository at this point in the history
  • Loading branch information
superlopuh committed May 29, 2023
1 parent ce7dead commit 24fd442
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 21 deletions.
8 changes: 3 additions & 5 deletions docs/Toy/toy/dialects/toy.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,11 +191,9 @@ def __init__(

@staticmethod
def implicit_builder(name: str, ftype: FunctionType, /, private: bool = False):
Builder.assert_implicit()
block = Block(arg_types=ftype.inputs)
region = Region(block)
_op = FuncOp(name, ftype, region, private=private)
return Builder(block).implicit()
return Builder.op(
lambda body: FuncOp(name, ftype, body, private=private), ftype.inputs
)

@staticmethod
def from_callable(
Expand Down
18 changes: 17 additions & 1 deletion xdsl/builder.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations
from types import TracebackType

from typing import Callable, ClassVar, Sequence, TypeAlias, overload
from typing import Callable, ClassVar, Iterable, Sequence, TypeAlias, overload
import threading
import contextlib

Expand Down Expand Up @@ -207,6 +207,22 @@ def assert_implicit():
"op_builder must be called within an implicit builder block"
)

@staticmethod
def op(
func: Callable[[Region], Operation],
arg_types: Iterable[Attribute] = (),
) -> _ImplicitBuilder:
"""
Will construct an op with a region, and populate child region.
Must be called within an implicit builder context.
"""
Builder.assert_implicit()
b = Block(arg_types=arg_types)
r = Region(b)
# will be added to parent implicit builder
_op = func(r)
return Builder(b).implicit()


# Implicit builders

Expand Down
9 changes: 4 additions & 5 deletions xdsl/dialects/func.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,10 @@ def implicit_builder(
input_types: Sequence[Attribute],
return_types: Sequence[Attribute],
):
Builder.assert_implicit()
block = Block(arg_types=input_types)
region = Region(block)
_op = FuncOp.from_region(name, input_types, return_types, region)
return Builder(block).implicit()
return Builder.op(
lambda body: FuncOp.from_region(name, input_types, return_types, body),
input_types,
)

def replace_argument_type(self, arg: int | BlockArgument, new_type: Attribute):
"""
Expand Down
12 changes: 2 additions & 10 deletions xdsl/dialects/pdl.py
Original file line number Diff line number Diff line change
Expand Up @@ -495,11 +495,7 @@ def implicit_builder(
benefit: int | IntegerAttr[IntegerType],
sym_name: str | StringAttr | None,
):
Builder.assert_implicit()
block = Block()
body = Region(block)
PatternOp(benefit, sym_name, body)
return Builder(block).implicit()
return Builder.op(lambda body: PatternOp(benefit, sym_name, body))

@classmethod
def parse(cls, parser: Parser) -> PatternOp:
Expand Down Expand Up @@ -824,11 +820,7 @@ def implicit_builder(
name: str | StringAttr | None = None,
external_args: Sequence[SSAValue] = (),
):
Builder.assert_implicit()
block = Block()
body = Region(block)
RewriteOp(root, body, name, external_args)
return Builder(block).implicit()
return Builder.op(lambda body: RewriteOp(root, body, name, external_args))


@irdl_op_definition
Expand Down

0 comments on commit 24fd442

Please sign in to comment.