Skip to content

Commit 7c104b6

Browse files
[mlir] Introduce OpAsmAttrInterface for pretty-print (#124721)
See https://discourse.llvm.org/t/rfc-introduce-opasm-type-attr-interface-for-pretty-print-in-asmprinter/83792 for detailed introduction. This PR adds * Definition of `OpAsmAttrInterface` * Integration of `OpAsmAttrInterface` with `AsmPrinter` In #121187 (comment) I mentioned splitting them into two PRs, but I realized that a PR with only definition of `OpAsmAttrInterface` is hard to test as it requires a custom Dialect with `OpAsmDialectInterface` to hook with `AsmPrinter`, so I just put them together to have a e2e test. Cc @River707 @jpienaar @ftynse for review.
1 parent 055872a commit 7c104b6

File tree

7 files changed

+87
-10
lines changed

7 files changed

+87
-10
lines changed

mlir/include/mlir/IR/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ add_mlir_interface(SymbolInterfaces)
22
add_mlir_interface(RegionKindInterface)
33

44
set(LLVM_TARGET_DEFINITIONS OpAsmInterface.td)
5+
mlir_tablegen(OpAsmAttrInterface.h.inc -gen-attr-interface-decls)
6+
mlir_tablegen(OpAsmAttrInterface.cpp.inc -gen-attr-interface-defs)
57
mlir_tablegen(OpAsmOpInterface.h.inc -gen-op-interface-decls)
68
mlir_tablegen(OpAsmOpInterface.cpp.inc -gen-op-interface-defs)
79
mlir_tablegen(OpAsmTypeInterface.h.inc -gen-type-interface-decls)

mlir/include/mlir/IR/OpAsmInterface.td

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,28 @@ def OpAsmTypeInterface : TypeInterface<"OpAsmTypeInterface"> {
130130
];
131131
}
132132

133+
//===----------------------------------------------------------------------===//
134+
// OpAsmAttrInterface
135+
//===----------------------------------------------------------------------===//
136+
137+
def OpAsmAttrInterface : AttrInterface<"OpAsmAttrInterface"> {
138+
let description = [{
139+
This interface provides hooks to interact with the AsmPrinter and AsmParser
140+
classes.
141+
}];
142+
let cppNamespace = "::mlir";
143+
144+
let methods = [
145+
InterfaceMethod<[{
146+
Get a name to use when generating an alias for this attribute.
147+
}],
148+
"::mlir::OpAsmDialectInterface::AliasResult", "getAlias",
149+
(ins "::llvm::raw_ostream&":$os), "",
150+
"return ::mlir::OpAsmDialectInterface::AliasResult::NoAlias;"
151+
>,
152+
];
153+
}
154+
133155
//===----------------------------------------------------------------------===//
134156
// ResourceHandleParameter
135157
//===----------------------------------------------------------------------===//

mlir/include/mlir/IR/OpImplementation.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1825,6 +1825,7 @@ ParseResult parseDimensionList(OpAsmParser &parser,
18251825
//===--------------------------------------------------------------------===//
18261826

18271827
/// The OpAsmOpInterface, see OpAsmInterface.td for more details.
1828+
#include "mlir/IR/OpAsmAttrInterface.h.inc"
18281829
#include "mlir/IR/OpAsmOpInterface.h.inc"
18291830
#include "mlir/IR/OpAsmTypeInterface.h.inc"
18301831

mlir/lib/IR/AsmPrinter.cpp

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ void OpAsmPrinter::printFunctionalType(Operation *op) {
125125
//===----------------------------------------------------------------------===//
126126

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

@@ -1159,15 +1160,31 @@ template <typename T>
11591160
void AliasInitializer::generateAlias(T symbol, InProgressAliasInfo &alias,
11601161
bool canBeDeferred) {
11611162
SmallString<32> nameBuffer;
1162-
for (const auto &interface : interfaces) {
1163-
OpAsmDialectInterface::AliasResult result =
1164-
interface.getAlias(symbol, aliasOS);
1165-
if (result == OpAsmDialectInterface::AliasResult::NoAlias)
1166-
continue;
1167-
nameBuffer = std::move(aliasBuffer);
1168-
assert(!nameBuffer.empty() && "expected valid alias name");
1169-
if (result == OpAsmDialectInterface::AliasResult::FinalAlias)
1170-
break;
1163+
1164+
OpAsmDialectInterface::AliasResult symbolInterfaceResult =
1165+
OpAsmDialectInterface::AliasResult::NoAlias;
1166+
if constexpr (std::is_base_of_v<Attribute, T>) {
1167+
if (auto symbolInterface = dyn_cast<OpAsmAttrInterface>(symbol)) {
1168+
symbolInterfaceResult = symbolInterface.getAlias(aliasOS);
1169+
if (symbolInterfaceResult !=
1170+
OpAsmDialectInterface::AliasResult::NoAlias) {
1171+
nameBuffer = std::move(aliasBuffer);
1172+
assert(!nameBuffer.empty() && "expected valid alias name");
1173+
}
1174+
}
1175+
}
1176+
1177+
if (symbolInterfaceResult != OpAsmDialectInterface::AliasResult::FinalAlias) {
1178+
for (const auto &interface : interfaces) {
1179+
OpAsmDialectInterface::AliasResult result =
1180+
interface.getAlias(symbol, aliasOS);
1181+
if (result == OpAsmDialectInterface::AliasResult::NoAlias)
1182+
continue;
1183+
nameBuffer = std::move(aliasBuffer);
1184+
assert(!nameBuffer.empty() && "expected valid alias name");
1185+
if (result == OpAsmDialectInterface::AliasResult::FinalAlias)
1186+
break;
1187+
}
11711188
}
11721189

11731190
if (nameBuffer.empty())

mlir/test/IR/op-asm-interface.mlir

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,17 @@ func.func @block_argument_name_from_op_asm_type_interface_asmprinter() {
5858
}
5959
return
6060
}
61+
62+
// -----
63+
64+
//===----------------------------------------------------------------------===//
65+
// Test OpAsmAttrInterface
66+
//===----------------------------------------------------------------------===//
67+
68+
// CHECK: #op_asm_attr_interface_test
69+
#attr = #test.op_asm_attr_interface<value = "test">
70+
71+
func.func @test_op_asm_attr_interface() {
72+
%1 = "test.result_name_from_type"() {attr = #attr} : () -> !test.op_asm_type_interface
73+
return
74+
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,4 +395,14 @@ def TestCustomLocationAttr : Test_LocAttr<"TestCustomLocation"> {
395395
let assemblyFormat = "`<` $file `*` $line `>`";
396396
}
397397

398+
// Test OpAsmAttrInterface.
399+
def TestOpAsmAttrInterfaceAttr : Test_Attr<"TestOpAsmAttrInterface",
400+
[DeclareAttrInterfaceMethods<OpAsmAttrInterface, ["getAlias"]>]> {
401+
let mnemonic = "op_asm_attr_interface";
402+
let parameters = (ins "mlir::StringAttr":$value);
403+
let assemblyFormat = [{
404+
`<` struct(params) `>`
405+
}];
406+
}
407+
398408
#endif // TEST_ATTRDEFS

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ void CompoundAAttr::print(AsmPrinter &printer) const {
6767
//===----------------------------------------------------------------------===//
6868

6969
Attribute TestDecimalShapeAttr::parse(AsmParser &parser, Type type) {
70-
if (parser.parseLess()){
70+
if (parser.parseLess()) {
7171
return Attribute();
7272
}
7373
SmallVector<int64_t> shape;
@@ -316,6 +316,17 @@ static ParseResult parseCustomFloatAttr(AsmParser &p, StringAttr &typeStrAttr,
316316
return success();
317317
}
318318

319+
//===----------------------------------------------------------------------===//
320+
// TestOpAsmAttrInterfaceAttr
321+
//===----------------------------------------------------------------------===//
322+
323+
::mlir::OpAsmDialectInterface::AliasResult
324+
TestOpAsmAttrInterfaceAttr::getAlias(::llvm::raw_ostream &os) const {
325+
os << "op_asm_attr_interface_";
326+
os << getValue().getValue();
327+
return ::mlir::OpAsmDialectInterface::AliasResult::FinalAlias;
328+
}
329+
319330
//===----------------------------------------------------------------------===//
320331
// Tablegen Generated Definitions
321332
//===----------------------------------------------------------------------===//

0 commit comments

Comments
 (0)