Open zhen8838 opened 1 month ago
The easiest way to start is usually using the assembly format API, our assembly format closely follows the MLIR one, so it's sometimes possible to copy/paste (without the operation name), and it often works. If it doesn't, then we usually use the parse
and print
methods, as in the scf.for
operation, which should have a similar structure. A PR implementing the custom format would be fantastic, if you don't mind implementing and testing it. Alternatively, we should be able to parse the generic format already, you can get mlir-opt
to use it by adding the --mlir-print-op-generic
argument.
I just checked the documentation, and it looks like there's no assembly format for this operation, and a custom parse
and print
functions are the way to go.
thanks for your answer. I will try to customize those functions.
Let's keep the issue around until the fix is merged, just to keep track of it. Can you please comment with #fixes #2673
when/if you open the PR?
Alright, I have encountered problems when I try to implement the affine. load
parse function, can you help me?
the affine load's syntax:
operation ::= ssa-id `=` `affine.load` ssa-use `[` multi-dim-affine-map-of-ssa-ids `]` `:` memref-type
test.mlir:
%0 = affine.load %arg0[%arg5 + 3, %arg6, %arg8] : memref<8x128x384xf32>
my question is how to parse multi-dim-affine-map-of-ssa-ids
, I think the mlir needs to parse the index expression and use it to construct the affine map attribute. But in the xdsl, it seems like no parse function can do this stuff.
here is my code and the error message:
@classmethod
def parse(cls: Self, parser: Parser) -> Self:
memref = parser.parse_operand()
def parse_index():
return parser.parse_operand() # <-- need to parse operand expreesion
indices = parser.parse_comma_separated_list(
Parser.Delimiter.SQUARE, parse_index)
parser.parse_punctuation(':')
rtype: MemRefType = parser.parse_type()
return cls(memref, indices, None, rtype.get_element_type())
xdsl.utils.exceptions.ParseError: <unknown>:7:41
%0 = affine.load %arg0[%arg5 + 3, %arg6, %arg8] : memref<8x128x384xf32>
^
Expected ']'
A search for multi-dim-affine-map-of-ssa-ids
in the LLVM project got 0 hits apart from the documentation, I'm guessing it's stale. Similarly, parseAffineLoad
yields nothing. I'm not sure where else to look. Without the MLIR implementation as reference, I'd say that the best bet is to look at our AffineParser, and to borrow the _parse_multi_affine_expr
implementation as a private helper in your file. I'd copy/paste the code, and replace the bits that parse identifiers with parsers for SSA values. I think copy/pasting here would be better than making the existing parser parametric, but that's somewhat subjective. If you happen to find the way MLIR does this that would be very helpful.
It might be the case that the affine expressions supported in affine
custom syntax are much simpler than what affine maps support, in which case writing a new parsing function from scratch might be easier.
ok, I will be writing a new parsing function to deal with this.
I am trying to use xdsl to parse a mlir file:
here is my code:
but I got error:
I found the xdsl needs to override the
parse
method manually. How can I use the mlir ODS to generate this method implement?