From eb418559ef29716cc34c891c93490c38ac5ea1dd Mon Sep 17 00:00:00 2001 From: River Riddle Date: Wed, 20 Nov 2019 10:19:01 -0800 Subject: Add a new OpAsmOpInterface to allow for ops to directly hook into the AsmPrinter. This interface provides more fine-grained hooks into the AsmPrinter than the dialect interface, allowing for operations to define the asm name to use for results directly on the operations themselves. The hook is also expanded to enable defining named result "groups". Get a special name to use when printing the results of this operation. The given callback is invoked with a specific result value that starts a result "pack", and the name to give this result pack. To signal that a result pack should use the default naming scheme, a None can be passed in instead of the name. For example, if you have an operation that has four results and you want to split these into three distinct groups you could do the following: setNameFn(getResult(0), "first_result"); setNameFn(getResult(1), "middle_results"); setNameFn(getResult(3), ""); // use the default numbering. This would print the operation as follows: %first_result, %middle_results:2, %0 = "my.op" ... PiperOrigin-RevId: 281546873 --- mlir/test/lib/TestDialect/TestDialect.cpp | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) (limited to 'mlir/test/lib/TestDialect/TestDialect.cpp') diff --git a/mlir/test/lib/TestDialect/TestDialect.cpp b/mlir/test/lib/TestDialect/TestDialect.cpp index 01780432a1a..d838f75f7e7 100644 --- a/mlir/test/lib/TestDialect/TestDialect.cpp +++ b/mlir/test/lib/TestDialect/TestDialect.cpp @@ -30,6 +30,18 @@ using namespace mlir; //===----------------------------------------------------------------------===// namespace { + +// Test support for interacting with the AsmPrinter. +struct TestOpAsmInterface : public OpAsmDialectInterface { + using OpAsmDialectInterface::OpAsmDialectInterface; + + void getAsmResultNames(Operation *op, + OpAsmSetValueNameFn setNameFn) const final { + if (auto asmOp = dyn_cast(op)) + setNameFn(asmOp, "result"); + } +}; + struct TestOpFolderDialectInterface : public OpFolderDialectInterface { using OpFolderDialectInterface::OpFolderDialectInterface; @@ -112,7 +124,8 @@ TestDialect::TestDialect(MLIRContext *context) #define GET_OP_LIST #include "TestOps.cpp.inc" >(); - addInterfaces(); + addInterfaces(); allowUnknownOperations(); } @@ -227,6 +240,7 @@ static void print(OpAsmPrinter &p, WrappingRegionOp op) { //===----------------------------------------------------------------------===// // Test PolyForOp - parse list of region arguments. //===----------------------------------------------------------------------===// + static ParseResult parsePolyForOp(OpAsmParser &parser, OperationState &result) { SmallVector ivsInfo; // Parse list of region arguments without a delimiter. @@ -240,6 +254,21 @@ static ParseResult parsePolyForOp(OpAsmParser &parser, OperationState &result) { return parser.parseRegion(*body, ivsInfo, argTypes); } +//===----------------------------------------------------------------------===// +// Test OpAsmInterface. +//===----------------------------------------------------------------------===// + +void AsmInterfaceOp::getAsmResultNames( + function_ref setNameFn) { + // Give a name to the first and middle results. + setNameFn(firstResult(), "first"); + if (!llvm::empty(middleResults())) + setNameFn(*middleResults().begin(), "middle_results"); + + // Use default numbering for the last result. + setNameFn(getResult(getNumResults() - 1), ""); +} + //===----------------------------------------------------------------------===// // Test removing op with inner ops. //===----------------------------------------------------------------------===// -- cgit v1.2.3