[Mlir-commits] [mlir] [mlir][docs] Document custom op parser and printer methods (PR #195579)

Mehdi Amini llvmlistbot at llvm.org
Mon May 4 04:12:26 PDT 2026


================
@@ -663,7 +663,54 @@ MyOp::build(::mlir::OpBuilder &builder, ::mlir::OperationState &state,
 
 ### Custom parser and printer methods
 
-Functions to parse and print the operation's custom assembly form.
+Most operations should define their custom assembly form declaratively with
+[`assemblyFormat`](#declarative-assembly-format). If an operation needs parsing
+or printing logic that cannot be expressed with the declarative format, set
+`hasCustomAssemblyFormat` to `1`.
+
+```tablegen
+def MyOp : Op<"my_op", []> {
+  let arguments = (ins AnyType:$input);
+  let results = (outs AnyType:$output);
+
+  let hasCustomAssemblyFormat = 1;
+}
+```
+
+This generates declarations for custom parser and printer methods on the op
+class:
+
+```c++
+static ::mlir::ParseResult parse(::mlir::OpAsmParser &parser,
+                                 ::mlir::OperationState &result);
+void print(::mlir::OpAsmPrinter &printer);
+```
+
+The parser consumes the operation's custom assembly form and populates the
+provided `OperationState` with operands, result types, attributes, properties,
+regions, and successors as needed. The printer emits the same custom form from
+the operation instance.
+
+The implementations are defined in C++:
+
+```c++
+ParseResult MyOp::parse(OpAsmParser &parser, OperationState &result) {
+  // Parse the custom syntax and populate `result`.
+  return success();
+}
+
+void MyOp::print(OpAsmPrinter &printer) {
+  // Print the custom syntax for this operation.
+}
+```
+
+If `assemblyFormat` is specified, it takes precedence over
+`hasCustomAssemblyFormat`. Older operation definitions may refer to inline
+`let parser` or `let printer` hooks; those hooks are no longer used for
+operations. Shared helper logic can still be factored into ordinary C++
----------------
joker-eph wrote:

Not clear to me the part about `let parser =` is useful here. I'd leave it out.

https://github.com/llvm/llvm-project/pull/195579


More information about the Mlir-commits mailing list