Skip to content

Commit

Permalink
[mlir] Update Toy operations to use the hasCustomAssemblyFormat field
Browse files Browse the repository at this point in the history
The parser/printer fields are deprecated and in the process of being removed.
  • Loading branch information
River707 committed Feb 8, 2022
1 parent 60cac0c commit 12bfd15
Show file tree
Hide file tree
Showing 13 changed files with 157 additions and 94 deletions.
17 changes: 7 additions & 10 deletions mlir/docs/Tutorials/Toy/Ch-2.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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;
}
```

Expand All @@ -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();
Expand All @@ -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;
Expand Down
15 changes: 6 additions & 9 deletions mlir/examples/toy/Ch2/include/toy/Ops.td
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 = [
Expand Down Expand Up @@ -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 = [
Expand Down
24 changes: 19 additions & 5 deletions mlir/examples/toy/Ch2/mlir/Dialect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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

Expand Down
15 changes: 6 additions & 9 deletions mlir/examples/toy/Ch3/include/toy/Ops.td
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 = [
Expand Down Expand Up @@ -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 = [
Expand Down
24 changes: 19 additions & 5 deletions mlir/examples/toy/Ch3/mlir/Dialect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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

Expand Down
15 changes: 6 additions & 9 deletions mlir/examples/toy/Ch4/include/toy/Ops.td
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 = [
Expand Down Expand Up @@ -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 = [
Expand Down
24 changes: 19 additions & 5 deletions mlir/examples/toy/Ch4/mlir/Dialect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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
Expand Down Expand Up @@ -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()); }
Expand Down Expand Up @@ -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()); }
Expand Down
15 changes: 6 additions & 9 deletions mlir/examples/toy/Ch5/include/toy/Ops.td
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 = [
Expand Down Expand Up @@ -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 = [
Expand Down
Loading

0 comments on commit 12bfd15

Please sign in to comment.