[Mlir-commits] [mlir] 12bfd15 - [mlir] Update Toy operations to use the `hasCustomAssemblyFormat` field
River Riddle
llvmlistbot at llvm.org
Mon Feb 7 19:04:31 PST 2022
Author: River Riddle
Date: 2022-02-07T19:03:58-08:00
New Revision: 12bfd159b7602fee776d4a0d4b2f1193e875fd35
URL: https://github.com/llvm/llvm-project/commit/12bfd159b7602fee776d4a0d4b2f1193e875fd35
DIFF: https://github.com/llvm/llvm-project/commit/12bfd159b7602fee776d4a0d4b2f1193e875fd35.diff
LOG: [mlir] Update Toy operations to use the `hasCustomAssemblyFormat` field
The parser/printer fields are deprecated and in the process of being removed.
Added:
Modified:
mlir/docs/Tutorials/Toy/Ch-2.md
mlir/examples/toy/Ch2/include/toy/Ops.td
mlir/examples/toy/Ch2/mlir/Dialect.cpp
mlir/examples/toy/Ch3/include/toy/Ops.td
mlir/examples/toy/Ch3/mlir/Dialect.cpp
mlir/examples/toy/Ch4/include/toy/Ops.td
mlir/examples/toy/Ch4/mlir/Dialect.cpp
mlir/examples/toy/Ch5/include/toy/Ops.td
mlir/examples/toy/Ch5/mlir/Dialect.cpp
mlir/examples/toy/Ch6/include/toy/Ops.td
mlir/examples/toy/Ch6/mlir/Dialect.cpp
mlir/examples/toy/Ch7/include/toy/Ops.td
mlir/examples/toy/Ch7/mlir/Dialect.cpp
Removed:
################################################################################
diff --git a/mlir/docs/Tutorials/Toy/Ch-2.md b/mlir/docs/Tutorials/Toy/Ch-2.md
index 648bfbd896c56..d329b9239a235 100644
--- a/mlir/docs/Tutorials/Toy/Ch-2.md
+++ b/mlir/docs/Tutorials/Toy/Ch-2.md
@@ -600,7 +600,7 @@ toy.print %5 : tensor<*xf64> loc(...)
Here we have stripped much of the format down to the bare essentials, and it has
become much more readable. To provide a custom assembly format, an operation can
-either override the `parser` and `printer` fields for a C++ format, or the
+either override the `hasCustomAssemblyFormat` field for a C++ format, or the
`assemblyFormat` field for the declarative format. Let's look at the C++ variant
first, as this is what the declarative format maps to internally.
@@ -609,12 +609,9 @@ first, as this is what the declarative format maps to internally.
def PrintOp : Toy_Op<"print"> {
let arguments = (ins F64Tensor:$input);
- // Divert the printer and parser to static functions in our .cpp
- // file that correspond to 'print' and 'printPrintOp'. 'printer' and 'parser'
- // here correspond to an instance of a 'OpAsmParser' and 'OpAsmPrinter'. More
- // details on these classes is shown below.
- let printer = [{ return ::print(printer, *this); }];
- let parser = [{ return ::parse$cppClass(parser, result); }];
+ // Divert the printer and parser to `parse` and `print` methods on our operation,
+ // to be implemented in the .cpp file. More details on these methods is shown below.
+ let hasCustomAssemblyFormat = 1;
}
```
@@ -623,7 +620,7 @@ A C++ implementation for the printer and parser is shown below:
```c++
/// The 'OpAsmPrinter' class is a stream that will allows for formatting
/// strings, attributes, operands, types, etc.
-static void print(mlir::OpAsmPrinter &printer, PrintOp op) {
+void PrintOp::print(mlir::OpAsmPrinter &printer) {
printer << "toy.print " << op.input();
printer.printOptionalAttrDict(op.getAttrs());
printer << " : " << op.input().getType();
@@ -636,8 +633,8 @@ static void print(mlir::OpAsmPrinter &printer, PrintOp op) {
/// or `false` on success. This allows for easily chaining together a set of
/// parser rules. These rules are used to populate an `mlir::OperationState`
/// similarly to the `build` methods described above.
-static mlir::ParseResult parsePrintOp(mlir::OpAsmParser &parser,
- mlir::OperationState &result) {
+mlir::ParseResult PrintOp::parse(mlir::OpAsmParser &parser,
+ mlir::OperationState &result) {
// Parse the input operand, the attribute dictionary, and the type of the
// input.
mlir::OpAsmParser::OperandType inputOperand;
diff --git a/mlir/examples/toy/Ch2/include/toy/Ops.td b/mlir/examples/toy/Ch2/include/toy/Ops.td
index eebe7ea65fc91..4e7e2312d1df6 100644
--- a/mlir/examples/toy/Ch2/include/toy/Ops.td
+++ b/mlir/examples/toy/Ch2/include/toy/Ops.td
@@ -59,9 +59,8 @@ def ConstantOp : Toy_Op<"constant", [NoSideEffect]> {
// The constant operation returns a single value of TensorType.
let results = (outs F64Tensor);
- // Specify a parser and printer method.
- let parser = [{ return ::parseConstantOp(parser, result); }];
- let printer = [{ return ::print(p, *this); }];
+ // Indicate that the operation has a custom parser and printer method.
+ let hasCustomAssemblyFormat = 1;
// Add custom build methods for the constant operation. These method populates
// the `state` that MLIR uses to create operations, i.e. these are used when
@@ -90,9 +89,8 @@ def AddOp : Toy_Op<"add"> {
let arguments = (ins F64Tensor:$lhs, F64Tensor:$rhs);
let results = (outs F64Tensor);
- // Specify a parser and printer method.
- let parser = [{ return ::parseBinaryOp(parser, result); }];
- let printer = [{ return ::printBinaryOp(p, *this); }];
+ // Indicate that the operation has a custom parser and printer method.
+ let hasCustomAssemblyFormat = 1;
// Allow building an AddOp with from the two input operands.
let builders = [
@@ -145,9 +143,8 @@ def MulOp : Toy_Op<"mul"> {
let arguments = (ins F64Tensor:$lhs, F64Tensor:$rhs);
let results = (outs F64Tensor);
- // Specify a parser and printer method.
- let parser = [{ return ::parseBinaryOp(parser, result); }];
- let printer = [{ return ::printBinaryOp(p, *this); }];
+ // Indicate that the operation has a custom parser and printer method.
+ let hasCustomAssemblyFormat = 1;
// Allow building a MulOp with from the two input operands.
let builders = [
diff --git a/mlir/examples/toy/Ch2/mlir/Dialect.cpp b/mlir/examples/toy/Ch2/mlir/Dialect.cpp
index 159b55d1bc82b..46b55f57a0d1d 100644
--- a/mlir/examples/toy/Ch2/mlir/Dialect.cpp
+++ b/mlir/examples/toy/Ch2/mlir/Dialect.cpp
@@ -107,8 +107,8 @@ void ConstantOp::build(mlir::OpBuilder &builder, mlir::OperationState &state,
/// or `false` on success. This allows for easily chaining together a set of
/// parser rules. These rules are used to populate an `mlir::OperationState`
/// similarly to the `build` methods described above.
-static mlir::ParseResult parseConstantOp(mlir::OpAsmParser &parser,
- mlir::OperationState &result) {
+mlir::ParseResult ConstantOp::parse(mlir::OpAsmParser &parser,
+ mlir::OperationState &result) {
mlir::DenseElementsAttr value;
if (parser.parseOptionalAttrDict(result.attributes) ||
parser.parseAttribute(value, "value", result.attributes))
@@ -120,10 +120,10 @@ static mlir::ParseResult parseConstantOp(mlir::OpAsmParser &parser,
/// The 'OpAsmPrinter' class is a stream that allows for formatting
/// strings, attributes, operands, types, etc.
-static void print(mlir::OpAsmPrinter &printer, ConstantOp op) {
+void ConstantOp::print(mlir::OpAsmPrinter &printer) {
printer << " ";
- printer.printOptionalAttrDict(op->getAttrs(), /*elidedAttrs=*/{"value"});
- printer << op.value();
+ printer.printOptionalAttrDict((*this)->getAttrs(), /*elidedAttrs=*/{"value"});
+ printer << value();
}
/// Verifier for the constant operation. This corresponds to the
@@ -165,6 +165,13 @@ void AddOp::build(mlir::OpBuilder &builder, mlir::OperationState &state,
state.addOperands({lhs, rhs});
}
+mlir::ParseResult AddOp::parse(mlir::OpAsmParser &parser,
+ mlir::OperationState &result) {
+ return parseBinaryOp(parser, result);
+}
+
+void AddOp::print(mlir::OpAsmPrinter &p) { printBinaryOp(p, *this); }
+
//===----------------------------------------------------------------------===//
// GenericCallOp
@@ -186,6 +193,13 @@ void MulOp::build(mlir::OpBuilder &builder, mlir::OperationState &state,
state.addOperands({lhs, rhs});
}
+mlir::ParseResult MulOp::parse(mlir::OpAsmParser &parser,
+ mlir::OperationState &result) {
+ return parseBinaryOp(parser, result);
+}
+
+void MulOp::print(mlir::OpAsmPrinter &p) { printBinaryOp(p, *this); }
+
//===----------------------------------------------------------------------===//
// ReturnOp
diff --git a/mlir/examples/toy/Ch3/include/toy/Ops.td b/mlir/examples/toy/Ch3/include/toy/Ops.td
index fc59739a25a07..d995d159f6927 100644
--- a/mlir/examples/toy/Ch3/include/toy/Ops.td
+++ b/mlir/examples/toy/Ch3/include/toy/Ops.td
@@ -58,9 +58,8 @@ def ConstantOp : Toy_Op<"constant", [NoSideEffect]> {
// The constant operation returns a single value of TensorType.
let results = (outs F64Tensor);
- // Specify a parser and printer method.
- let parser = [{ return ::parseConstantOp(parser, result); }];
- let printer = [{ return ::print(p, *this); }];
+ // Indicate that the operation has a custom parser and printer method.
+ let hasCustomAssemblyFormat = 1;
// Add custom build methods for the constant operation. These method populates
// the `state` that MLIR uses to create operations, i.e. these are used when
@@ -89,9 +88,8 @@ def AddOp : Toy_Op<"add", [NoSideEffect]> {
let arguments = (ins F64Tensor:$lhs, F64Tensor:$rhs);
let results = (outs F64Tensor);
- // Specify a parser and printer method.
- let parser = [{ return ::parseBinaryOp(parser, result); }];
- let printer = [{ return ::printBinaryOp(p, *this); }];
+ // Indicate that the operation has a custom parser and printer method.
+ let hasCustomAssemblyFormat = 1;
// Allow building an AddOp with from the two input operands.
let builders = [
@@ -144,9 +142,8 @@ def MulOp : Toy_Op<"mul", [NoSideEffect]> {
let arguments = (ins F64Tensor:$lhs, F64Tensor:$rhs);
let results = (outs F64Tensor);
- // Specify a parser and printer method.
- let parser = [{ return ::parseBinaryOp(parser, result); }];
- let printer = [{ return ::printBinaryOp(p, *this); }];
+ // Indicate that the operation has a custom parser and printer method.
+ let hasCustomAssemblyFormat = 1;
// Allow building a MulOp with from the two input operands.
let builders = [
diff --git a/mlir/examples/toy/Ch3/mlir/Dialect.cpp b/mlir/examples/toy/Ch3/mlir/Dialect.cpp
index 159b55d1bc82b..46b55f57a0d1d 100644
--- a/mlir/examples/toy/Ch3/mlir/Dialect.cpp
+++ b/mlir/examples/toy/Ch3/mlir/Dialect.cpp
@@ -107,8 +107,8 @@ void ConstantOp::build(mlir::OpBuilder &builder, mlir::OperationState &state,
/// or `false` on success. This allows for easily chaining together a set of
/// parser rules. These rules are used to populate an `mlir::OperationState`
/// similarly to the `build` methods described above.
-static mlir::ParseResult parseConstantOp(mlir::OpAsmParser &parser,
- mlir::OperationState &result) {
+mlir::ParseResult ConstantOp::parse(mlir::OpAsmParser &parser,
+ mlir::OperationState &result) {
mlir::DenseElementsAttr value;
if (parser.parseOptionalAttrDict(result.attributes) ||
parser.parseAttribute(value, "value", result.attributes))
@@ -120,10 +120,10 @@ static mlir::ParseResult parseConstantOp(mlir::OpAsmParser &parser,
/// The 'OpAsmPrinter' class is a stream that allows for formatting
/// strings, attributes, operands, types, etc.
-static void print(mlir::OpAsmPrinter &printer, ConstantOp op) {
+void ConstantOp::print(mlir::OpAsmPrinter &printer) {
printer << " ";
- printer.printOptionalAttrDict(op->getAttrs(), /*elidedAttrs=*/{"value"});
- printer << op.value();
+ printer.printOptionalAttrDict((*this)->getAttrs(), /*elidedAttrs=*/{"value"});
+ printer << value();
}
/// Verifier for the constant operation. This corresponds to the
@@ -165,6 +165,13 @@ void AddOp::build(mlir::OpBuilder &builder, mlir::OperationState &state,
state.addOperands({lhs, rhs});
}
+mlir::ParseResult AddOp::parse(mlir::OpAsmParser &parser,
+ mlir::OperationState &result) {
+ return parseBinaryOp(parser, result);
+}
+
+void AddOp::print(mlir::OpAsmPrinter &p) { printBinaryOp(p, *this); }
+
//===----------------------------------------------------------------------===//
// GenericCallOp
@@ -186,6 +193,13 @@ void MulOp::build(mlir::OpBuilder &builder, mlir::OperationState &state,
state.addOperands({lhs, rhs});
}
+mlir::ParseResult MulOp::parse(mlir::OpAsmParser &parser,
+ mlir::OperationState &result) {
+ return parseBinaryOp(parser, result);
+}
+
+void MulOp::print(mlir::OpAsmPrinter &p) { printBinaryOp(p, *this); }
+
//===----------------------------------------------------------------------===//
// ReturnOp
diff --git a/mlir/examples/toy/Ch4/include/toy/Ops.td b/mlir/examples/toy/Ch4/include/toy/Ops.td
index 13357ff1f4fe8..b070b1380b130 100644
--- a/mlir/examples/toy/Ch4/include/toy/Ops.td
+++ b/mlir/examples/toy/Ch4/include/toy/Ops.td
@@ -61,9 +61,8 @@ def ConstantOp : Toy_Op<"constant", [NoSideEffect]> {
// The constant operation returns a single value of TensorType.
let results = (outs F64Tensor);
- // Specify a parser and printer method.
- let parser = [{ return ::parseConstantOp(parser, result); }];
- let printer = [{ return ::print(p, *this); }];
+ // Indicate that the operation has a custom parser and printer method.
+ let hasCustomAssemblyFormat = 1;
// Add custom build methods for the constant operation. These method populates
// the `state` that MLIR uses to create operations, i.e. these are used when
@@ -93,9 +92,8 @@ def AddOp : Toy_Op<"add",
let arguments = (ins F64Tensor:$lhs, F64Tensor:$rhs);
let results = (outs F64Tensor);
- // Specify a parser and printer method.
- let parser = [{ return ::parseBinaryOp(parser, result); }];
- let printer = [{ return ::printBinaryOp(p, *this); }];
+ // Indicate that the operation has a custom parser and printer method.
+ let hasCustomAssemblyFormat = 1;
// Allow building an AddOp with from the two input operands.
let builders = [
@@ -171,9 +169,8 @@ def MulOp : Toy_Op<"mul",
let arguments = (ins F64Tensor:$lhs, F64Tensor:$rhs);
let results = (outs F64Tensor);
- // Specify a parser and printer method.
- let parser = [{ return ::parseBinaryOp(parser, result); }];
- let printer = [{ return ::printBinaryOp(p, *this); }];
+ // Indicate that the operation has a custom parser and printer method.
+ let hasCustomAssemblyFormat = 1;
// Allow building a MulOp with from the two input operands.
let builders = [
diff --git a/mlir/examples/toy/Ch4/mlir/Dialect.cpp b/mlir/examples/toy/Ch4/mlir/Dialect.cpp
index b5ef04450aace..fd5afd211cd07 100644
--- a/mlir/examples/toy/Ch4/mlir/Dialect.cpp
+++ b/mlir/examples/toy/Ch4/mlir/Dialect.cpp
@@ -163,8 +163,8 @@ void ConstantOp::build(mlir::OpBuilder &builder, mlir::OperationState &state,
/// or `false` on success. This allows for easily chaining together a set of
/// parser rules. These rules are used to populate an `mlir::OperationState`
/// similarly to the `build` methods described above.
-static mlir::ParseResult parseConstantOp(mlir::OpAsmParser &parser,
- mlir::OperationState &result) {
+mlir::ParseResult ConstantOp::parse(mlir::OpAsmParser &parser,
+ mlir::OperationState &result) {
mlir::DenseElementsAttr value;
if (parser.parseOptionalAttrDict(result.attributes) ||
parser.parseAttribute(value, "value", result.attributes))
@@ -176,10 +176,10 @@ static mlir::ParseResult parseConstantOp(mlir::OpAsmParser &parser,
/// The 'OpAsmPrinter' class is a stream that allows for formatting
/// strings, attributes, operands, types, etc.
-static void print(mlir::OpAsmPrinter &printer, ConstantOp op) {
+void ConstantOp::print(mlir::OpAsmPrinter &printer) {
printer << " ";
- printer.printOptionalAttrDict(op->getAttrs(), /*elidedAttrs=*/{"value"});
- printer << op.value();
+ printer.printOptionalAttrDict((*this)->getAttrs(), /*elidedAttrs=*/{"value"});
+ printer << value();
}
/// Verifier for the constant operation. This corresponds to the
@@ -221,6 +221,13 @@ void AddOp::build(mlir::OpBuilder &builder, mlir::OperationState &state,
state.addOperands({lhs, rhs});
}
+mlir::ParseResult AddOp::parse(mlir::OpAsmParser &parser,
+ mlir::OperationState &result) {
+ return parseBinaryOp(parser, result);
+}
+
+void AddOp::print(mlir::OpAsmPrinter &p) { printBinaryOp(p, *this); }
+
/// Infer the output shape of the AddOp, this is required by the shape inference
/// interface.
void AddOp::inferShapes() { getResult().setType(getOperand(0).getType()); }
@@ -278,6 +285,13 @@ void MulOp::build(mlir::OpBuilder &builder, mlir::OperationState &state,
state.addOperands({lhs, rhs});
}
+mlir::ParseResult MulOp::parse(mlir::OpAsmParser &parser,
+ mlir::OperationState &result) {
+ return parseBinaryOp(parser, result);
+}
+
+void MulOp::print(mlir::OpAsmPrinter &p) { printBinaryOp(p, *this); }
+
/// Infer the output shape of the MulOp, this is required by the shape inference
/// interface.
void MulOp::inferShapes() { getResult().setType(getOperand(0).getType()); }
diff --git a/mlir/examples/toy/Ch5/include/toy/Ops.td b/mlir/examples/toy/Ch5/include/toy/Ops.td
index 22f96abeb02c3..a66326825152b 100644
--- a/mlir/examples/toy/Ch5/include/toy/Ops.td
+++ b/mlir/examples/toy/Ch5/include/toy/Ops.td
@@ -61,9 +61,8 @@ def ConstantOp : Toy_Op<"constant", [NoSideEffect]> {
// The constant operation returns a single value of TensorType.
let results = (outs F64Tensor);
- // Specify a parser and printer method.
- let parser = [{ return ::parseConstantOp(parser, result); }];
- let printer = [{ return ::print(p, *this); }];
+ // Indicate that the operation has a custom parser and printer method.
+ let hasCustomAssemblyFormat = 1;
// Add custom build methods for the constant operation. These method populates
// the `state` that MLIR uses to create operations, i.e. these are used when
@@ -93,9 +92,8 @@ def AddOp : Toy_Op<"add",
let arguments = (ins F64Tensor:$lhs, F64Tensor:$rhs);
let results = (outs F64Tensor);
- // Specify a parser and printer method.
- let parser = [{ return ::parseBinaryOp(parser, result); }];
- let printer = [{ return ::printBinaryOp(p, *this); }];
+ // Indicate that the operation has a custom parser and printer method.
+ let hasCustomAssemblyFormat = 1;
// Allow building an AddOp with from the two input operands.
let builders = [
@@ -171,9 +169,8 @@ def MulOp : Toy_Op<"mul",
let arguments = (ins F64Tensor:$lhs, F64Tensor:$rhs);
let results = (outs F64Tensor);
- // Specify a parser and printer method.
- let parser = [{ return ::parseBinaryOp(parser, result); }];
- let printer = [{ return ::printBinaryOp(p, *this); }];
+ // Indicate that the operation has a custom parser and printer method.
+ let hasCustomAssemblyFormat = 1;
// Allow building a MulOp with from the two input operands.
let builders = [
diff --git a/mlir/examples/toy/Ch5/mlir/Dialect.cpp b/mlir/examples/toy/Ch5/mlir/Dialect.cpp
index bf1d2a581d037..2aecfe811970a 100644
--- a/mlir/examples/toy/Ch5/mlir/Dialect.cpp
+++ b/mlir/examples/toy/Ch5/mlir/Dialect.cpp
@@ -163,8 +163,8 @@ void ConstantOp::build(mlir::OpBuilder &builder, mlir::OperationState &state,
/// or `false` on success. This allows for easily chaining together a set of
/// parser rules. These rules are used to populate an `mlir::OperationState`
/// similarly to the `build` methods described above.
-static mlir::ParseResult parseConstantOp(mlir::OpAsmParser &parser,
- mlir::OperationState &result) {
+mlir::ParseResult ConstantOp::parse(mlir::OpAsmParser &parser,
+ mlir::OperationState &result) {
mlir::DenseElementsAttr value;
if (parser.parseOptionalAttrDict(result.attributes) ||
parser.parseAttribute(value, "value", result.attributes))
@@ -176,10 +176,10 @@ static mlir::ParseResult parseConstantOp(mlir::OpAsmParser &parser,
/// The 'OpAsmPrinter' class is a stream that allows for formatting
/// strings, attributes, operands, types, etc.
-static void print(mlir::OpAsmPrinter &printer, ConstantOp op) {
+void ConstantOp::print(mlir::OpAsmPrinter &printer) {
printer << " ";
- printer.printOptionalAttrDict(op->getAttrs(), /*elidedAttrs=*/{"value"});
- printer << op.value();
+ printer.printOptionalAttrDict((*this)->getAttrs(), /*elidedAttrs=*/{"value"});
+ printer << value();
}
/// Verifier for the constant operation. This corresponds to the
@@ -221,6 +221,13 @@ void AddOp::build(mlir::OpBuilder &builder, mlir::OperationState &state,
state.addOperands({lhs, rhs});
}
+mlir::ParseResult AddOp::parse(mlir::OpAsmParser &parser,
+ mlir::OperationState &result) {
+ return parseBinaryOp(parser, result);
+}
+
+void AddOp::print(mlir::OpAsmPrinter &p) { printBinaryOp(p, *this); }
+
/// Infer the output shape of the AddOp, this is required by the shape inference
/// interface.
void AddOp::inferShapes() { getResult().setType(getOperand(0).getType()); }
@@ -278,6 +285,13 @@ void MulOp::build(mlir::OpBuilder &builder, mlir::OperationState &state,
state.addOperands({lhs, rhs});
}
+mlir::ParseResult MulOp::parse(mlir::OpAsmParser &parser,
+ mlir::OperationState &result) {
+ return parseBinaryOp(parser, result);
+}
+
+void MulOp::print(mlir::OpAsmPrinter &p) { printBinaryOp(p, *this); }
+
/// Infer the output shape of the MulOp, this is required by the shape inference
/// interface.
void MulOp::inferShapes() { getResult().setType(getOperand(0).getType()); }
diff --git a/mlir/examples/toy/Ch6/include/toy/Ops.td b/mlir/examples/toy/Ch6/include/toy/Ops.td
index 355c39bfce2f5..373fdeee2c278 100644
--- a/mlir/examples/toy/Ch6/include/toy/Ops.td
+++ b/mlir/examples/toy/Ch6/include/toy/Ops.td
@@ -61,9 +61,8 @@ def ConstantOp : Toy_Op<"constant", [NoSideEffect]> {
// The constant operation returns a single value of TensorType.
let results = (outs F64Tensor);
- // Specify a parser and printer method.
- let parser = [{ return ::parseConstantOp(parser, result); }];
- let printer = [{ return ::print(p, *this); }];
+ // Indicate that the operation has a custom parser and printer method.
+ let hasCustomAssemblyFormat = 1;
// Add custom build methods for the constant operation. These method populates
// the `state` that MLIR uses to create operations, i.e. these are used when
@@ -93,9 +92,8 @@ def AddOp : Toy_Op<"add",
let arguments = (ins F64Tensor:$lhs, F64Tensor:$rhs);
let results = (outs F64Tensor);
- // Specify a parser and printer method.
- let parser = [{ return ::parseBinaryOp(parser, result); }];
- let printer = [{ return ::printBinaryOp(p, *this); }];
+ // Indicate that the operation has a custom parser and printer method.
+ let hasCustomAssemblyFormat = 1;
// Allow building an AddOp with from the two input operands.
let builders = [
@@ -171,9 +169,8 @@ def MulOp : Toy_Op<"mul",
let arguments = (ins F64Tensor:$lhs, F64Tensor:$rhs);
let results = (outs F64Tensor);
- // Specify a parser and printer method.
- let parser = [{ return ::parseBinaryOp(parser, result); }];
- let printer = [{ return ::printBinaryOp(p, *this); }];
+ // Indicate that the operation has a custom parser and printer method.
+ let hasCustomAssemblyFormat = 1;
// Allow building a MulOp with from the two input operands.
let builders = [
diff --git a/mlir/examples/toy/Ch6/mlir/Dialect.cpp b/mlir/examples/toy/Ch6/mlir/Dialect.cpp
index bf1d2a581d037..2aecfe811970a 100644
--- a/mlir/examples/toy/Ch6/mlir/Dialect.cpp
+++ b/mlir/examples/toy/Ch6/mlir/Dialect.cpp
@@ -163,8 +163,8 @@ void ConstantOp::build(mlir::OpBuilder &builder, mlir::OperationState &state,
/// or `false` on success. This allows for easily chaining together a set of
/// parser rules. These rules are used to populate an `mlir::OperationState`
/// similarly to the `build` methods described above.
-static mlir::ParseResult parseConstantOp(mlir::OpAsmParser &parser,
- mlir::OperationState &result) {
+mlir::ParseResult ConstantOp::parse(mlir::OpAsmParser &parser,
+ mlir::OperationState &result) {
mlir::DenseElementsAttr value;
if (parser.parseOptionalAttrDict(result.attributes) ||
parser.parseAttribute(value, "value", result.attributes))
@@ -176,10 +176,10 @@ static mlir::ParseResult parseConstantOp(mlir::OpAsmParser &parser,
/// The 'OpAsmPrinter' class is a stream that allows for formatting
/// strings, attributes, operands, types, etc.
-static void print(mlir::OpAsmPrinter &printer, ConstantOp op) {
+void ConstantOp::print(mlir::OpAsmPrinter &printer) {
printer << " ";
- printer.printOptionalAttrDict(op->getAttrs(), /*elidedAttrs=*/{"value"});
- printer << op.value();
+ printer.printOptionalAttrDict((*this)->getAttrs(), /*elidedAttrs=*/{"value"});
+ printer << value();
}
/// Verifier for the constant operation. This corresponds to the
@@ -221,6 +221,13 @@ void AddOp::build(mlir::OpBuilder &builder, mlir::OperationState &state,
state.addOperands({lhs, rhs});
}
+mlir::ParseResult AddOp::parse(mlir::OpAsmParser &parser,
+ mlir::OperationState &result) {
+ return parseBinaryOp(parser, result);
+}
+
+void AddOp::print(mlir::OpAsmPrinter &p) { printBinaryOp(p, *this); }
+
/// Infer the output shape of the AddOp, this is required by the shape inference
/// interface.
void AddOp::inferShapes() { getResult().setType(getOperand(0).getType()); }
@@ -278,6 +285,13 @@ void MulOp::build(mlir::OpBuilder &builder, mlir::OperationState &state,
state.addOperands({lhs, rhs});
}
+mlir::ParseResult MulOp::parse(mlir::OpAsmParser &parser,
+ mlir::OperationState &result) {
+ return parseBinaryOp(parser, result);
+}
+
+void MulOp::print(mlir::OpAsmPrinter &p) { printBinaryOp(p, *this); }
+
/// Infer the output shape of the MulOp, this is required by the shape inference
/// interface.
void MulOp::inferShapes() { getResult().setType(getOperand(0).getType()); }
diff --git a/mlir/examples/toy/Ch7/include/toy/Ops.td b/mlir/examples/toy/Ch7/include/toy/Ops.td
index d17e0944a8196..828b416f4581c 100644
--- a/mlir/examples/toy/Ch7/include/toy/Ops.td
+++ b/mlir/examples/toy/Ch7/include/toy/Ops.td
@@ -77,9 +77,8 @@ def ConstantOp : Toy_Op<"constant",
// The constant operation returns a single value of TensorType.
let results = (outs F64Tensor);
- // Specify a parser and printer method.
- let parser = [{ return ::parseConstantOp(parser, result); }];
- let printer = [{ return ::print(p, *this); }];
+ // Indicate that the operation has a custom parser and printer method.
+ let hasCustomAssemblyFormat = 1;
// Add custom build methods for the constant operation. These method populates
// the `state` that MLIR uses to create operations, i.e. these are used when
@@ -112,9 +111,8 @@ def AddOp : Toy_Op<"add",
let arguments = (ins F64Tensor:$lhs, F64Tensor:$rhs);
let results = (outs F64Tensor);
- // Specify a parser and printer method.
- let parser = [{ return ::parseBinaryOp(parser, result); }];
- let printer = [{ return ::printBinaryOp(p, *this); }];
+ // Indicate that the operation has a custom parser and printer method.
+ let hasCustomAssemblyFormat = 1;
// Allow building an AddOp with from the two input operands.
let builders = [
@@ -191,9 +189,8 @@ def MulOp : Toy_Op<"mul",
let arguments = (ins F64Tensor:$lhs, F64Tensor:$rhs);
let results = (outs F64Tensor);
- // Specify a parser and printer method.
- let parser = [{ return ::parseBinaryOp(parser, result); }];
- let printer = [{ return ::printBinaryOp(p, *this); }];
+ // Indicate that the operation has a custom parser and printer method.
+ let hasCustomAssemblyFormat = 1;
// Allow building a MulOp with from the two input operands.
let builders = [
diff --git a/mlir/examples/toy/Ch7/mlir/Dialect.cpp b/mlir/examples/toy/Ch7/mlir/Dialect.cpp
index 3974c111e16a0..a86f80f0014a0 100644
--- a/mlir/examples/toy/Ch7/mlir/Dialect.cpp
+++ b/mlir/examples/toy/Ch7/mlir/Dialect.cpp
@@ -150,8 +150,8 @@ void ConstantOp::build(mlir::OpBuilder &builder, mlir::OperationState &state,
/// or `false` on success. This allows for easily chaining together a set of
/// parser rules. These rules are used to populate an `mlir::OperationState`
/// similarly to the `build` methods described above.
-static mlir::ParseResult parseConstantOp(mlir::OpAsmParser &parser,
- mlir::OperationState &result) {
+mlir::ParseResult ConstantOp::parse(mlir::OpAsmParser &parser,
+ mlir::OperationState &result) {
mlir::DenseElementsAttr value;
if (parser.parseOptionalAttrDict(result.attributes) ||
parser.parseAttribute(value, "value", result.attributes))
@@ -163,10 +163,10 @@ static mlir::ParseResult parseConstantOp(mlir::OpAsmParser &parser,
/// The 'OpAsmPrinter' class is a stream that allows for formatting
/// strings, attributes, operands, types, etc.
-static void print(mlir::OpAsmPrinter &printer, ConstantOp op) {
+void ConstantOp::print(mlir::OpAsmPrinter &printer) {
printer << " ";
- printer.printOptionalAttrDict(op->getAttrs(), /*elidedAttrs=*/{"value"});
- printer << op.value();
+ printer.printOptionalAttrDict((*this)->getAttrs(), /*elidedAttrs=*/{"value"});
+ printer << value();
}
/// Verify that the given attribute value is valid for the given type.
@@ -248,6 +248,13 @@ void AddOp::build(mlir::OpBuilder &builder, mlir::OperationState &state,
state.addOperands({lhs, rhs});
}
+mlir::ParseResult AddOp::parse(mlir::OpAsmParser &parser,
+ mlir::OperationState &result) {
+ return parseBinaryOp(parser, result);
+}
+
+void AddOp::print(mlir::OpAsmPrinter &p) { printBinaryOp(p, *this); }
+
/// Infer the output shape of the AddOp, this is required by the shape inference
/// interface.
void AddOp::inferShapes() { getResult().setType(getOperand(0).getType()); }
@@ -305,6 +312,13 @@ void MulOp::build(mlir::OpBuilder &builder, mlir::OperationState &state,
state.addOperands({lhs, rhs});
}
+mlir::ParseResult MulOp::parse(mlir::OpAsmParser &parser,
+ mlir::OperationState &result) {
+ return parseBinaryOp(parser, result);
+}
+
+void MulOp::print(mlir::OpAsmPrinter &p) { printBinaryOp(p, *this); }
+
/// Infer the output shape of the MulOp, this is required by the shape inference
/// interface.
void MulOp::inferShapes() { getResult().setType(getOperand(0).getType()); }
More information about the Mlir-commits
mailing list