Skip to content

Commit 63834a2

Browse files
[mlir] Add OpAsmTypeInterface for pretty-print
1 parent 9c1d2f8 commit 63834a2

File tree

9 files changed

+120
-5
lines changed

9 files changed

+120
-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: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,27 @@ 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 name to use when printing a value of this type.
126+
}],
127+
"void", "getAsmName",
128+
(ins "::mlir::OpAsmSetNameFn":$setNameFn), "", ";"
129+
>,
130+
];
131+
}
132+
112133
//===----------------------------------------------------------------------===//
113134
// ResourceHandleParameter
114135
//===----------------------------------------------------------------------===//

mlir/include/mlir/IR/OpImplementation.h

Lines changed: 8 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,8 @@ 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/OpAsmOpInterface.h.inc"
1828+
#include "mlir/IR/OpAsmTypeInterface.h.inc"
18241829

18251830
namespace llvm {
18261831
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/IR/op-asm-interface.mlir

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// RUN: mlir-opt %s -split-input-file -verify-diagnostics | FileCheck %s
2+
3+
//===----------------------------------------------------------------------===//
4+
// Test OpAsmOpInterface
5+
//===----------------------------------------------------------------------===//
6+
7+
func.func @result_name_from_op_asm_type_interface() {
8+
// CHECK-LABEL: @result_name_from_op_asm_type_interface
9+
// CHECK: %op_asm_type_interface
10+
%0 = "test.result_name_from_type"() : () -> !test.op_asm_type_interface
11+
return
12+
}
13+
14+
// -----
15+
16+
func.func @block_argument_name_from_op_asm_type_interface() {
17+
// CHECK-LABEL: @block_argument_name_from_op_asm_type_interface
18+
// CHECK: ^bb0(%op_asm_type_interface
19+
test.block_argument_name_from_type {
20+
^bb0(%arg0: !test.op_asm_type_interface):
21+
"test.terminator"() : ()->()
22+
}
23+
return
24+
}

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,38 @@ void CustomResultsNameOp::getAsmResultNames(
506506
setNameFn(getResult(i), str.getValue());
507507
}
508508

509+
//===----------------------------------------------------------------------===//
510+
// ResultNameFromTypeOp
511+
//===----------------------------------------------------------------------===//
512+
513+
void ResultNameFromTypeOp::getAsmResultNames(
514+
function_ref<void(Value, StringRef)> setNameFn) {
515+
auto result = getResult();
516+
auto setResultNameFn = [&](::llvm::StringRef name) {
517+
setNameFn(result, name);
518+
};
519+
auto opAsmTypeInterface =
520+
::mlir::cast<::mlir::OpAsmTypeInterface>(result.getType());
521+
opAsmTypeInterface.getAsmName(setResultNameFn);
522+
}
523+
524+
//===----------------------------------------------------------------------===//
525+
// BlockArgumentNameFromTypeOp
526+
//===----------------------------------------------------------------------===//
527+
528+
void BlockArgumentNameFromTypeOp::getAsmBlockArgumentNames(
529+
::mlir::Region &region, ::mlir::OpAsmSetValueNameFn setNameFn) {
530+
for (auto &block : region) {
531+
for (auto arg : block.getArguments()) {
532+
if (auto opAsmTypeInterface =
533+
::mlir::dyn_cast<::mlir::OpAsmTypeInterface>(arg.getType())) {
534+
auto setArgNameFn = [&](StringRef name) { setNameFn(arg, name); };
535+
opAsmTypeInterface.getAsmName(setArgNameFn);
536+
}
537+
}
538+
}
539+
}
540+
509541
//===----------------------------------------------------------------------===//
510542
// ResultTypeWithTraitOp
511543
//===----------------------------------------------------------------------===//

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -924,6 +924,21 @@ def CustomResultsNameOp
924924
let results = (outs Variadic<AnyInteger>:$r);
925925
}
926926

927+
// This is used to test OpAsmTypeInterface::getAsmName for op result name,
928+
def ResultNameFromTypeOp
929+
: TEST_Op<"result_name_from_type",
930+
[DeclareOpInterfaceMethods<OpAsmOpInterface, ["getAsmResultNames"]>]> {
931+
let results = (outs AnyType:$r);
932+
}
933+
934+
// This is used to test OpAsmTypeInterface::getAsmName for block argument,
935+
def BlockArgumentNameFromTypeOp
936+
: TEST_Op<"block_argument_name_from_type",
937+
[DeclareOpInterfaceMethods<OpAsmOpInterface, ["getAsmBlockArgumentNames"]>]> {
938+
let regions = (region AnyRegion:$body);
939+
let assemblyFormat = "regions attr-dict-with-keyword";
940+
}
941+
927942
// This is used to test the OpAsmOpInterface::getDefaultDialect() feature:
928943
// operations nested in a region under this op will drop the "test." dialect
929944
// prefix.

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

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

401+
def TestTypeOpAsmTypeInterface : Test_Type<"TestTypeOpAsmTypeInterface",
402+
[DeclareTypeInterfaceMethods<OpAsmTypeInterface, ["getAsmName"]>]> {
403+
let mnemonic = "op_asm_type_interface";
404+
}
405+
401406
#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)