Skip to content

Commit aa16ca3

Browse files
[mlir] Add getAlias for OpAsmTypeInterface (#126364)
See https://discourse.llvm.org/t/rfc-introduce-opasm-type-attr-interface-for-pretty-print-in-asmprinter/83792 for detailed introduction. This PR should be rebased once #124721 is merged. This PR adds * Definition of `getAlias` for `OpAsmTypeInterface` * Integration of `OpAsmTypeInterface` with `AsmPrinter` alias handling part This is partly in response to https://github.com/llvm/llvm-project/pull/124721/files#r1940399862 Cc @River707 for review.
1 parent f796747 commit aa16ca3

File tree

5 files changed

+31
-9
lines changed

5 files changed

+31
-9
lines changed

mlir/include/mlir/IR/OpAsmInterface.td

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,13 @@ def OpAsmTypeInterface : TypeInterface<"OpAsmTypeInterface"> {
127127
"void", "getAsmName",
128128
(ins "::mlir::OpAsmSetNameFn":$setNameFn), "", ";"
129129
>,
130+
InterfaceMethod<[{
131+
Get a name to use when generating an alias for this type.
132+
}],
133+
"::mlir::OpAsmDialectInterface::AliasResult", "getAlias",
134+
(ins "::llvm::raw_ostream&":$os), "",
135+
"return ::mlir::OpAsmDialectInterface::AliasResult::NoAlias;"
136+
>,
130137
];
131138
}
132139

mlir/lib/IR/AsmPrinter.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1163,14 +1163,13 @@ void AliasInitializer::generateAlias(T symbol, InProgressAliasInfo &alias,
11631163

11641164
OpAsmDialectInterface::AliasResult symbolInterfaceResult =
11651165
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-
}
1166+
using InterfaceT = std::conditional_t<std::is_base_of_v<Attribute, T>,
1167+
OpAsmAttrInterface, OpAsmTypeInterface>;
1168+
if (auto symbolInterface = dyn_cast<InterfaceT>(symbol)) {
1169+
symbolInterfaceResult = symbolInterface.getAlias(aliasOS);
1170+
if (symbolInterfaceResult != OpAsmDialectInterface::AliasResult::NoAlias) {
1171+
nameBuffer = std::move(aliasBuffer);
1172+
assert(!nameBuffer.empty() && "expected valid alias name");
11741173
}
11751174
}
11761175

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,16 @@ func.func @block_argument_name_from_op_asm_type_interface_asmprinter() {
6161

6262
// -----
6363

64+
// CHECK: !op_asm_type_interface_type =
65+
!type = !test.op_asm_type_interface
66+
67+
func.func @alias_from_op_asm_type_interface() {
68+
%0 = "test.result_name_from_type"() : () -> !type
69+
return
70+
}
71+
72+
// -----
73+
6474
//===----------------------------------------------------------------------===//
6575
// Test OpAsmAttrInterface
6676
//===----------------------------------------------------------------------===//

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ def TestTypeVerification : Test_Type<"TestTypeVerification"> {
399399
}
400400

401401
def TestTypeOpAsmTypeInterface : Test_Type<"TestTypeOpAsmTypeInterface",
402-
[DeclareTypeInterfaceMethods<OpAsmTypeInterface, ["getAsmName"]>]> {
402+
[DeclareTypeInterfaceMethods<OpAsmTypeInterface, ["getAsmName", "getAlias"]>]> {
403403
let mnemonic = "op_asm_type_interface";
404404
}
405405

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,3 +537,9 @@ void TestTypeOpAsmTypeInterfaceType::getAsmName(
537537
OpAsmSetNameFn setNameFn) const {
538538
setNameFn("op_asm_type_interface");
539539
}
540+
541+
::mlir::OpAsmDialectInterface::AliasResult
542+
TestTypeOpAsmTypeInterfaceType::getAlias(::llvm::raw_ostream &os) const {
543+
os << "op_asm_type_interface_type";
544+
return ::mlir::OpAsmDialectInterface::AliasResult::FinalAlias;
545+
}

0 commit comments

Comments
 (0)