diff options
| author | River Riddle <riverriddle@google.com> | 2019-11-20 10:19:01 -0800 |
|---|---|---|
| committer | A. Unique TensorFlower <gardener@tensorflow.org> | 2019-11-20 10:45:45 -0800 |
| commit | eb418559ef29716cc34c891c93490c38ac5ea1dd (patch) | |
| tree | be1e4ac3d32e5df31b8668785ba15f92fe1895a9 /mlir/test/lib/TestDialect/TestDialect.cpp | |
| parent | 3c055957de7e47e53d3ee8f5ab283cdb5c0ea535 (diff) | |
| download | bcm5719-llvm-eb418559ef29716cc34c891c93490c38ac5ea1dd.tar.gz bcm5719-llvm-eb418559ef29716cc34c891c93490c38ac5ea1dd.zip | |
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
Diffstat (limited to 'mlir/test/lib/TestDialect/TestDialect.cpp')
| -rw-r--r-- | mlir/test/lib/TestDialect/TestDialect.cpp | 31 |
1 files changed, 30 insertions, 1 deletions
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<AsmDialectInterfaceOp>(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<TestOpFolderDialectInterface, TestInlinerInterface>(); + addInterfaces<TestOpAsmInterface, TestOpFolderDialectInterface, + TestInlinerInterface>(); 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<OpAsmParser::OperandType, 4> ivsInfo; // Parse list of region arguments without a delimiter. @@ -241,6 +255,21 @@ static ParseResult parsePolyForOp(OpAsmParser &parser, OperationState &result) { } //===----------------------------------------------------------------------===// +// Test OpAsmInterface. +//===----------------------------------------------------------------------===// + +void AsmInterfaceOp::getAsmResultNames( + function_ref<void(Value *, StringRef)> 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. //===----------------------------------------------------------------------===// |

