Skip to content

Commit 54738b6

Browse files
[mlir] Add OpAsmTypeInterface for pretty-print
1 parent 409ca49 commit 54738b6

File tree

6 files changed

+53
-5
lines changed

6 files changed

+53
-5
lines changed

mlir/include/mlir/IR/CMakeLists.txt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
1-
add_mlir_interface(OpAsmInterface)
21
add_mlir_interface(SymbolInterfaces)
32
add_mlir_interface(RegionKindInterface)
43

4+
set(LLVM_TARGET_DEFINITIONS OpAsmInterface.td)
5+
mlir_tablegen(OpAsmOpInterface.h.inc -gen-op-interface-decls)
6+
mlir_tablegen(OpAsmOpInterface.cpp.inc -gen-op-interface-defs)
7+
mlir_tablegen(OpAsmTypeInterface.h.inc -gen-type-interface-decls)
8+
mlir_tablegen(OpAsmTypeInterface.cpp.inc -gen-type-interface-defs)
9+
add_public_tablegen_target(MLIROpAsmInterfaceIncGen)
10+
add_dependencies(mlir-generic-headers MLIROpAsmInterfaceIncGen)
11+
512
set(LLVM_TARGET_DEFINITIONS BuiltinAttributes.td)
613
mlir_tablegen(BuiltinAttributes.h.inc -gen-attrdef-decls)
714
mlir_tablegen(BuiltinAttributes.cpp.inc -gen-attrdef-defs)

mlir/include/mlir/IR/OpAsmInterface.td

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,31 @@ def OpAsmOpInterface : OpInterface<"OpAsmOpInterface"> {
109109
];
110110
}
111111

112+
//===----------------------------------------------------------------------===//
113+
// OpAsmTypeInterface
114+
//===----------------------------------------------------------------------===//
115+
116+
def OpAsmTypeInterface : TypeInterface<"OpAsmTypeInterface"> {
117+
let description = [{
118+
This interface provides hooks to interact with the AsmPrinter and AsmParser
119+
classes.
120+
}];
121+
let cppNamespace = "::mlir";
122+
123+
let methods = [
124+
InterfaceMethod<[{
125+
Get a special name to use when printing value of this type.
126+
127+
For example, the default implementation of OpAsmOpInterface
128+
will respect this method when printing the results of an operation
129+
and/or block argument of it.
130+
}],
131+
"void", "getAsmName",
132+
(ins "::mlir::OpAsmSetNameFn":$setNameFn), "", ";"
133+
>,
134+
];
135+
}
136+
112137
//===----------------------------------------------------------------------===//
113138
// ResourceHandleParameter
114139
//===----------------------------------------------------------------------===//

mlir/include/mlir/IR/OpImplementation.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -734,7 +734,7 @@ class AsmParser {
734734
virtual OptionalParseResult parseOptionalInteger(APInt &result) = 0;
735735
virtual OptionalParseResult parseOptionalDecimalInteger(APInt &result) = 0;
736736

737-
private:
737+
private:
738738
template <typename IntT, typename ParseFn>
739739
OptionalParseResult parseOptionalIntegerAndCheck(IntT &result,
740740
ParseFn &&parseFn) {
@@ -756,7 +756,7 @@ class AsmParser {
756756
return success();
757757
}
758758

759-
public:
759+
public:
760760
template <typename IntT>
761761
OptionalParseResult parseOptionalInteger(IntT &result) {
762762
return parseOptionalIntegerAndCheck(
@@ -1727,6 +1727,10 @@ class OpAsmParser : public AsmParser {
17271727
// Dialect OpAsm interface.
17281728
//===--------------------------------------------------------------------===//
17291729

1730+
/// A functor used to set the name of the result. See 'getAsmResultNames' below
1731+
/// for more details.
1732+
using OpAsmSetNameFn = function_ref<void(StringRef)>;
1733+
17301734
/// A functor used to set the name of the start of a result group of an
17311735
/// operation. See 'getAsmResultNames' below for more details.
17321736
using OpAsmSetValueNameFn = function_ref<void(Value, StringRef)>;
@@ -1820,7 +1824,9 @@ ParseResult parseDimensionList(OpAsmParser &parser,
18201824
//===--------------------------------------------------------------------===//
18211825

18221826
/// The OpAsmOpInterface, see OpAsmInterface.td for more details.
1823-
#include "mlir/IR/OpAsmInterface.h.inc"
1827+
#include "mlir/IR/OpAsmTypeInterface.h.inc"
1828+
// put Attr/Type before Op
1829+
#include "mlir/IR/OpAsmOpInterface.h.inc"
18241830

18251831
namespace llvm {
18261832
template <>

mlir/lib/IR/AsmPrinter.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,8 @@ void OpAsmPrinter::printFunctionalType(Operation *op) {
125125
//===----------------------------------------------------------------------===//
126126

127127
/// The OpAsmOpInterface, see OpAsmInterface.td for more details.
128-
#include "mlir/IR/OpAsmInterface.cpp.inc"
128+
#include "mlir/IR/OpAsmOpInterface.cpp.inc"
129+
#include "mlir/IR/OpAsmTypeInterface.cpp.inc"
129130

130131
LogicalResult
131132
OpAsmDialectInterface::parseResource(AsmParsedResourceEntry &entry) const {

mlir/test/lib/Dialect/Test/TestTypeDefs.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,4 +398,8 @@ def TestTypeVerification : Test_Type<"TestTypeVerification"> {
398398
let assemblyFormat = "`<` $param `>`";
399399
}
400400

401+
def TestTypeOpAsmTypeInterface : Test_Type<"TestTypeOpAsmTypeInterface", [DeclareTypeInterfaceMethods<OpAsmTypeInterface, ["getAsmName"]>]> {
402+
let mnemonic = "op_asm_type_interface";
403+
}
404+
401405
#endif // TEST_TYPEDEFS

mlir/test/lib/Dialect/Test/TestTypes.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,3 +532,8 @@ void TestRecursiveAliasType::print(AsmPrinter &printer) const {
532532
}
533533
printer << ">";
534534
}
535+
536+
void TestTypeOpAsmTypeInterfaceType::getAsmName(
537+
OpAsmSetNameFn setNameFn) const {
538+
setNameFn("op_asm_type_interface");
539+
}

0 commit comments

Comments
 (0)