Skip to content

[mlir] Introduce OpAsmAttrInterface for pretty-print #124721

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions mlir/include/mlir/IR/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ add_mlir_interface(SymbolInterfaces)
add_mlir_interface(RegionKindInterface)

set(LLVM_TARGET_DEFINITIONS OpAsmInterface.td)
mlir_tablegen(OpAsmAttrInterface.h.inc -gen-attr-interface-decls)
mlir_tablegen(OpAsmAttrInterface.cpp.inc -gen-attr-interface-defs)
mlir_tablegen(OpAsmOpInterface.h.inc -gen-op-interface-decls)
mlir_tablegen(OpAsmOpInterface.cpp.inc -gen-op-interface-defs)
mlir_tablegen(OpAsmTypeInterface.h.inc -gen-type-interface-decls)
Expand Down
22 changes: 22 additions & 0 deletions mlir/include/mlir/IR/OpAsmInterface.td
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,28 @@ def OpAsmTypeInterface : TypeInterface<"OpAsmTypeInterface"> {
];
}

//===----------------------------------------------------------------------===//
// OpAsmAttrInterface
//===----------------------------------------------------------------------===//

def OpAsmAttrInterface : AttrInterface<"OpAsmAttrInterface"> {
let description = [{
This interface provides hooks to interact with the AsmPrinter and AsmParser
classes.
}];
let cppNamespace = "::mlir";

let methods = [
InterfaceMethod<[{
Get a name to use when generating an alias for this attribute.
}],
"::mlir::OpAsmDialectInterface::AliasResult", "getAlias",
(ins "::llvm::raw_ostream&":$os), "",
"return ::mlir::OpAsmDialectInterface::AliasResult::NoAlias;"
>,
];
}

//===----------------------------------------------------------------------===//
// ResourceHandleParameter
//===----------------------------------------------------------------------===//
Expand Down
1 change: 1 addition & 0 deletions mlir/include/mlir/IR/OpImplementation.h
Original file line number Diff line number Diff line change
Expand Up @@ -1825,6 +1825,7 @@ ParseResult parseDimensionList(OpAsmParser &parser,
//===--------------------------------------------------------------------===//

/// The OpAsmOpInterface, see OpAsmInterface.td for more details.
#include "mlir/IR/OpAsmAttrInterface.h.inc"
#include "mlir/IR/OpAsmOpInterface.h.inc"
#include "mlir/IR/OpAsmTypeInterface.h.inc"

Expand Down
35 changes: 26 additions & 9 deletions mlir/lib/IR/AsmPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ void OpAsmPrinter::printFunctionalType(Operation *op) {
//===----------------------------------------------------------------------===//

/// The OpAsmOpInterface, see OpAsmInterface.td for more details.
#include "mlir/IR/OpAsmAttrInterface.cpp.inc"
#include "mlir/IR/OpAsmOpInterface.cpp.inc"
#include "mlir/IR/OpAsmTypeInterface.cpp.inc"

Expand Down Expand Up @@ -1159,15 +1160,31 @@ template <typename T>
void AliasInitializer::generateAlias(T symbol, InProgressAliasInfo &alias,
bool canBeDeferred) {
SmallString<32> nameBuffer;
for (const auto &interface : interfaces) {
OpAsmDialectInterface::AliasResult result =
interface.getAlias(symbol, aliasOS);
if (result == OpAsmDialectInterface::AliasResult::NoAlias)
continue;
nameBuffer = std::move(aliasBuffer);
assert(!nameBuffer.empty() && "expected valid alias name");
if (result == OpAsmDialectInterface::AliasResult::FinalAlias)
break;

OpAsmDialectInterface::AliasResult symbolInterfaceResult =
OpAsmDialectInterface::AliasResult::NoAlias;
if constexpr (std::is_base_of_v<Attribute, T>) {
if (auto symbolInterface = dyn_cast<OpAsmAttrInterface>(symbol)) {
symbolInterfaceResult = symbolInterface.getAlias(aliasOS);
if (symbolInterfaceResult !=
OpAsmDialectInterface::AliasResult::NoAlias) {
nameBuffer = std::move(aliasBuffer);
assert(!nameBuffer.empty() && "expected valid alias name");
}
}
}

if (symbolInterfaceResult != OpAsmDialectInterface::AliasResult::FinalAlias) {
for (const auto &interface : interfaces) {
OpAsmDialectInterface::AliasResult result =
interface.getAlias(symbol, aliasOS);
if (result == OpAsmDialectInterface::AliasResult::NoAlias)
continue;
nameBuffer = std::move(aliasBuffer);
assert(!nameBuffer.empty() && "expected valid alias name");
if (result == OpAsmDialectInterface::AliasResult::FinalAlias)
break;
}
}

if (nameBuffer.empty())
Expand Down
14 changes: 14 additions & 0 deletions mlir/test/IR/op-asm-interface.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,17 @@ func.func @block_argument_name_from_op_asm_type_interface_asmprinter() {
}
return
}

// -----

//===----------------------------------------------------------------------===//
// Test OpAsmAttrInterface
//===----------------------------------------------------------------------===//

// CHECK: #op_asm_attr_interface_test
#attr = #test.op_asm_attr_interface<value = "test">

func.func @test_op_asm_attr_interface() {
%1 = "test.result_name_from_type"() {attr = #attr} : () -> !test.op_asm_type_interface
return
}
10 changes: 10 additions & 0 deletions mlir/test/lib/Dialect/Test/TestAttrDefs.td
Original file line number Diff line number Diff line change
Expand Up @@ -395,4 +395,14 @@ def TestCustomLocationAttr : Test_LocAttr<"TestCustomLocation"> {
let assemblyFormat = "`<` $file `*` $line `>`";
}

// Test OpAsmAttrInterface.
def TestOpAsmAttrInterfaceAttr : Test_Attr<"TestOpAsmAttrInterface",
[DeclareAttrInterfaceMethods<OpAsmAttrInterface, ["getAlias"]>]> {
let mnemonic = "op_asm_attr_interface";
let parameters = (ins "mlir::StringAttr":$value);
let assemblyFormat = [{
`<` struct(params) `>`
}];
}

#endif // TEST_ATTRDEFS
13 changes: 12 additions & 1 deletion mlir/test/lib/Dialect/Test/TestAttributes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ void CompoundAAttr::print(AsmPrinter &printer) const {
//===----------------------------------------------------------------------===//

Attribute TestDecimalShapeAttr::parse(AsmParser &parser, Type type) {
if (parser.parseLess()){
if (parser.parseLess()) {
return Attribute();
}
SmallVector<int64_t> shape;
Expand Down Expand Up @@ -316,6 +316,17 @@ static ParseResult parseCustomFloatAttr(AsmParser &p, StringAttr &typeStrAttr,
return success();
}

//===----------------------------------------------------------------------===//
// TestOpAsmAttrInterfaceAttr
//===----------------------------------------------------------------------===//

::mlir::OpAsmDialectInterface::AliasResult
TestOpAsmAttrInterfaceAttr::getAlias(::llvm::raw_ostream &os) const {
os << "op_asm_attr_interface_";
os << getValue().getValue();
return ::mlir::OpAsmDialectInterface::AliasResult::FinalAlias;
}

//===----------------------------------------------------------------------===//
// Tablegen Generated Definitions
//===----------------------------------------------------------------------===//
Expand Down