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
Conversation
Codecov ReportPatch coverage:
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
☔ View full report in Codecov by Sentry. |
There was a problem hiding this 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 🤔
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. |
@webmiche I added back a lambda-based factory, WDYT? |
There was a problem hiding this 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.
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 |
There was a problem hiding this 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!
There was a problem hiding this 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
docs/Toy/toy/tests/test_ir_gen.py
Outdated
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😱 That's awesome!
24fd442
to
37213da
Compare
From the discussion on #1017, I thought it might be better to make the ImplicitBuilder public in a separate PR.
8c32c41
to
7d94f9e
Compare
xdsl/ir.py
Outdated
class Default: | ||
""" | ||
A class to be used as a default parameter to functions when a default region | ||
should be constructed. | ||
""" | ||
|
There was a problem hiding this comment.
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...
There was a problem hiding this comment.
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
684f9f8
to
f68677e
Compare
7d94f9e
to
13e1e77
Compare
13e1e77
to
5380055
Compare
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): ... ```
This reverts commit 8e677f1.
5380055
to
fc2f2cc
Compare
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:
After:
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.