Hmm, that’s an interesting example. Up until now, I’d only been considering line comments, and inline comments which append statements.
But I hadn’t been considering inline comments which intersect statements, as well as block comments, e.g., /*param=*/ (these are not currently valid in MLIR, but could be added).
We would need to add a check to see if a given comment is inside a statement, and if so, gather information as to where, so we can reinsert it in the correct place later.
I can imagine that before post-processing, but after our initial IR processing, we are handling an MLIR file like:
%result1, %result2 = my.foo(%operand1, %value, %some_value) {powerful = true} : i32, f32
"mlirformat.comment"() {str = "param=", type = "block", inline = true, inline_pos = "some indentifying data" }: () -> ()
"mlirformat.comment"() {str = " This next argument...", type = "single", inline = true, inline_pos = "some indentifying data" }: () -> ()
"mlirformat.comment"() {str = " the XXXXX aspects...", type = "single", inline = true, inline_pos = "some indentifying data" }: () -> ()
To identify the inline positon for reinsertion, one naive way I can think of is to count the number of non-comment, non-whitespace printable characters from the start of the statement that the comment was found.
E.g., /*param=*/ is 35 characters in, the next comment is 42, etc
That of course assumes two things: that variable names are not changed, and that formatting does not introduce/remove any visible characters. The first assumption I’m working on, the second one I would hope is true right now, but I’m not sure.
Another question is assuming it is practically implemented, what formatting would be applied here. The simple answer would be whatever formatting that parsing and printing the statement without the comments generates, and then insert the comments with the rule: any line breaks introduced by comments should be use the indentation of the statement plus 1
It would be good if we could start applying more complex rules, like limiting line length. E.g., if we had:
%result1, %result2 = my.foo(%operand1, /*param_but_this_comment_is_really_long_for_some_reason=*/ %value,
Then a formatter could introduce line breaks as appropriate, e.g.,
%result1, %result2 = my.foo(
%operand1,
/*param_but_this_comment_is_really_long_for_some_reason=*/ %value,
However, given the existence of custom printers, I’m not sure if the syntax of MLIR statements is consistent enough to manipulate correctly. Even if we had access to the operation object, I understand the printer to be a black box where anything could happen.