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.
This is a draft PR as a PoC of a basic formatting tool for MLIR. This was first described in this forum thread.
It does not aim to be anything close to
clang-format
. Instead, it uses the existing dialect printers to format IR.An example use-case might be "I'm making some small edits to this MLIR test case, and I want it to be better formatted". Right now, users would either need to use
mlir-opt
(and reinsert their comments and SSA names by-hand), or do the formatting manually.⬇️ 🪄
The two features that this tool introduces that cannot be done simply using
mlir-opt
are the following:Feature 1 is achieved by using the original source buffer, and only replacing the character range of the formatted op. This means comments are left as-is.
Feature 2 is achieved by using the recently added NameLoc support, where we can retain identifier names for debugging.
%alice = op()
->%0 = op() loc("alice")
, which can then be printed again as%alice = op()
.From a design perspective, this tool adapts
mlir-rewrite
. It creates 2 rewrite buffers, the first one it uses to insert theloc
, which is then used to generate the formatted ops. The second rewrite buffer is where these formatted ops are inserted. I might be able to do it with one buffer, and there are some other code improvements that could be made.There's a laundry list of small features that would be good to have to make this a good tool:
NameLoc
(e.g.,%group1:2 %group2:3 = return_5vals
)., if so do not format those ops.For 2 I have an idea for a workaround to enable this, for 1. we could try and get clever with reinsertion of the comment, but I'd prefer to keep the tool simple for now.
For 6, I've added a mini-parser to re-insert the original type aliases. But it might be better to allow alias printing as a general feature.
For 3., this Emacs function works, something more general, also with VScode support would be desirable.
Emacs mlir-format function