Should there be a higher level API? #18
Draft
+1,812
−1
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Inspired by Beaver I've been thinking about the possiblity of a higher level API to write MLIR IR. At the lowest level, adding operations to a block could be achieved by using a similar mechanism as the context in thread local storage:
using
esc
in the macro would make sure that the created values are available outside the@Block
expression, enabling other blocks to reference those values as well.Things fall apart when introducing control flow.
cf.br
needs to be able to reference blocks that are yet to be created. I tried writing@Region
and@Block
which I believe is a dumbed-down version of how things work in Beaver (I don't really understand Elixir, code). This is the code in this PR but it isn't meant for merging, rather for showing what I tried and didn't work very well.Here,
@Block
pushes the code it encompasses on a stack and first creates all blocks. Only at the end is the actual code from within each block run. I got this working usingeval
which isn't good all variables are then visible in module scope.Also, even if this approach worked, I believe it's not entirely correct because blocks that are dominated by a block can access its values, this doesn't necessarily mean that those values are defined in order in code. e.g. if bb3 dominates bb2, bb2 can access it's values.
Lastly, block arguments aren't accounted for here but I imagine these could be added to the macro (
@Block arg::argtype begin
)At this point I think it might not be worth it to try writing an all-encompassing high-level API that is quickly becoming riddled by magic (which I've already been warned of by @maleadt, who mentors my master thesis). But maybe there is some middleground such as operation functions returning their value or perhaps even using the automatic pushing to the block on the thread-local stack?
Any feedback would be greatly appreciated!
Jules