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: use with blocks for implicit building #1017

Merged
merged 16 commits into from Jun 2, 2023
Merged

Conversation

superlopuh
Copy link
Member

One thing that I'm unhappy about with the current implicit builder APIs is that one has to first define the function that builds the region, and then construct the operation with that region. This is less than ideal, as they are pretty closely tied together, so the visual separation can make things harder to parse. I tried solving this first with higher-order functions but that got messy pretty quickly.

This PR introduces an alternative to this, which I now think is superior, and would like to transition to for the whole codebase if we decide to accept this PR:

Before:

@Builder.implicit_region(multiply_transpose_type.inputs)
def multiply_transpose(args: tuple[BlockArgument, ...]) -> None:
    a, b = args

...

toy.FuncOp(
    "multiply_transpose",
    multiply_transpose_type,
    multiply_transpose,
    private=True,
)

After:

with toy.FuncOp.implicit_builder(
    "multiply_transpose", multiply_transpose_type, private=True
) as (a, b):

The new approach gets rid of Python functions altogether, exposes args as part of the with block API, brings the order in which the operations are created in Python in line with their order in the IR. If we migrate all our functions this way, it'll let us have an API where the user doesn't need to import the Builder at all, only using helpers on Operations.

@superlopuh superlopuh added the core xDSL core (ir, textual format, ...) label May 29, 2023
@superlopuh superlopuh self-assigned this May 29, 2023
@codecov
Copy link

codecov bot commented May 29, 2023

Codecov Report

Patch coverage: 92.53% and project coverage change: +0.15 🎉

Comparison is base (680c7f4) 86.92% compared to head (5380055) 87.07%.

❗ Current head 5380055 differs from pull request most recent head fc2f2cc. Consider uploading reports for the commit fc2f2cc to get more accurate results

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1017      +/-   ##
==========================================
+ Coverage   86.92%   87.07%   +0.15%     
==========================================
  Files         129      127       -2     
  Lines       19799    19546     -253     
  Branches     3005     2976      -29     
==========================================
- Hits        17210    17020     -190     
+ Misses       2079     2023      -56     
+ Partials      510      503       -7     
Impacted Files Coverage Δ
xdsl/dialects/pdl.py 85.37% <63.63%> (+0.20%) ⬆️
xdsl/builder.py 93.57% <92.85%> (-1.13%) ⬇️
docs/Toy/toy/dialects/toy.py 67.69% <100.00%> (+0.88%) ⬆️
docs/Toy/toy/tests/test_ir_gen.py 100.00% <100.00%> (ø)
tests/dialects/test_pdl.py 100.00% <100.00%> (ø)
tests/interpreters/test_pdl_interpreter.py 92.75% <100.00%> (-0.58%) ⬇️
xdsl/dialects/func.py 95.70% <100.00%> (+1.22%) ⬆️
xdsl/ir.py 86.31% <100.00%> (ø)

... and 16 files with indirect coverage changes

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

Copy link
Collaborator

@webmiche webmiche left a comment

Choose a reason for hiding this comment

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

I like it, nice idea!

This will require all ops with regions that want access to this functionality to override implicit_builder, right? Is there a way to autogen this? Those you add here all look somewhat biolerplatey 🤔

@superlopuh
Copy link
Member Author

Yeah, generating the builder automatically would be nice. One way that I know would work would require the region to be the first parameter of the init, not sure if we'd be ok imposing such a constraint. We can look for further sugar after merging this, if everyone's ok with it.

@superlopuh
Copy link
Member Author

@webmiche I added back a lambda-based factory, WDYT?

Copy link
Collaborator

@webmiche webmiche left a comment

Choose a reason for hiding this comment

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

Nice, yes I like it 👍

I think imposing constraints on the arg order of init is not something we want to do. Let's merge this version and see if we find something better later on.

@superlopuh
Copy link
Member Author

Great, I'll just wait for @math-fehr to have a look at it when he's back, there's no particular rush to merge this in

Copy link
Collaborator

@math-fehr math-fehr left a comment

Choose a reason for hiding this comment

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

Really nice! That looks good!

xdsl/dialects/func.py Outdated Show resolved Hide resolved
xdsl/dialects/pdl.py Outdated Show resolved Hide resolved
xdsl/dialects/pdl.py Outdated Show resolved Hide resolved
xdsl/builder.py Outdated Show resolved Hide resolved
xdsl/builder.py Outdated Show resolved Hide resolved
Copy link
Collaborator

@AntonLydike AntonLydike left a comment

Choose a reason for hiding this comment

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

Looks pretty good, just missing some documentation I think

Comment on lines 34 to 39
with toy.FuncOp.implicit_builder(
"multiply_transpose", multiply_transpose_type, private=True
) as (a, b):
a_t = toy.TransposeOp(a).res
b_t = toy.TransposeOp(b).res
prod = toy.MulOp(a_t, b_t).res
Copy link
Collaborator

Choose a reason for hiding this comment

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

😱 That's awesome!

xdsl/dialects/func.py Outdated Show resolved Hide resolved
@superlopuh superlopuh changed the base branch from main to sasha/builder/implicit-public May 31, 2023 09:24
Base automatically changed from sasha/builder/implicit-public to main May 31, 2023 09:35
superlopuh added a commit that referenced this pull request May 31, 2023
From the discussion on #1017, I thought it might be better to make the
ImplicitBuilder public in a separate PR.
@superlopuh superlopuh force-pushed the sasha/builder/sugar branch 3 times, most recently from 8c32c41 to 7d94f9e Compare May 31, 2023 13:36
xdsl/ir.py Outdated
Comment on lines 1362 to 1367
class Default:
"""
A class to be used as a default parameter to functions when a default region
should be constructed.
"""

Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm not 100% sure this is the correct way of doing it. I'd rather see a specific sentinel value?

But then, I'm not sure how that interacts with the type system. Maybe that can be a literal of some sort?

I guess the main problem with this is that people will want to instantiate this class, so I'd recommend adding an error to the __init__ that this class is not to be constructed. But that feels quite silly to me...

Copy link
Member Author

Choose a reason for hiding this comment

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

I've seen it done this way before, and I feel like this is good enough. There's a proposal for a sentinel: https://peps.python.org/pep-0661/ it's a draft, we can use it when it gets accepted, I guess

@superlopuh superlopuh changed the base branch from main to sasha/builder/default-region June 1, 2023 09:19
@superlopuh superlopuh force-pushed the sasha/builder/default-region branch from 684f9f8 to f68677e Compare June 1, 2023 09:20
Base automatically changed from sasha/builder/default-region to main June 2, 2023 12:13
superlopuh added a commit that referenced this pull request Jun 2, 2023
Another offshoot of #1017, wanted to discuss some extra inits directly.
This PR allows the creation of ops without the regions populated, to
allow for this sort of thing once 1017 is merged:

``` python
with ImplicitBuilder(toy.FuncOp("main", ((), ())).body):
  ...
```
@superlopuh superlopuh merged commit b2d1bba into main Jun 2, 2023
11 checks passed
@superlopuh superlopuh deleted the sasha/builder/sugar branch June 2, 2023 12:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core xDSL core (ir, textual format, ...)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants