Skip to content

Commit 8773bd0

Browse files
authored
[mlir] Print aliases for recursive types (#110346)
We're already keeping track of the alias depth to ensure that aliases are printed before they're referenced. For recursive types, we can additionally track whether an alias has been printed and only reference it if so, to lift the restrictions on not printing aliases inside mutable types.
1 parent e203a67 commit 8773bd0

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

mlir/lib/IR/AsmPrinter.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,10 @@ class SymbolAlias {
545545
bool isType : 1;
546546
/// A flag indicating whether this alias may be deferred or not.
547547
bool isDeferrable : 1;
548+
549+
public:
550+
/// Used to avoid printing incomplete aliases for recursive types.
551+
bool isPrinted = false;
548552
};
549553

550554
/// This class represents a utility that initializes the set of attribute and
@@ -1222,6 +1226,8 @@ LogicalResult AliasState::getAlias(Type ty, raw_ostream &os) const {
12221226
const auto *it = attrTypeToAlias.find(ty.getAsOpaquePointer());
12231227
if (it == attrTypeToAlias.end())
12241228
return failure();
1229+
if (!it->second.isPrinted)
1230+
return failure();
12251231

12261232
it->second.print(os);
12271233
return success();
@@ -1238,12 +1244,9 @@ void AliasState::printAliases(AsmPrinter::Impl &p, NewLineCounter &newLine,
12381244
p.getStream() << " = ";
12391245

12401246
if (alias.isTypeAlias()) {
1241-
// TODO: Support nested aliases in mutable types.
12421247
Type type = Type::getFromOpaquePointer(opaqueSymbol);
1243-
if (type.hasTrait<TypeTrait::IsMutable>())
1244-
p.getStream() << type;
1245-
else
1246-
p.printTypeImpl(type);
1248+
p.printTypeImpl(type);
1249+
alias.isPrinted = true;
12471250
} else {
12481251
// TODO: Support nested aliases in mutable attributes.
12491252
Attribute attr = Attribute::getFromOpaquePointer(opaqueSymbol);

mlir/test/IR/recursive-type.mlir

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
// CHECK: !testrec = !test.test_rec<type_to_alias, test_rec<type_to_alias>>
44
// CHECK: ![[$NAME:.*]] = !test.test_rec_alias<name, !test.test_rec_alias<name>>
5+
// CHECK: ![[$NAME5:.*]] = !test.test_rec_alias<name5, !test.test_rec_alias<name3, !test.test_rec_alias<name4, !test.test_rec_alias<name5>>>>
56
// CHECK: ![[$NAME2:.*]] = !test.test_rec_alias<name2, tuple<!test.test_rec_alias<name2>, i32>>
7+
// CHECK: ![[$NAME4:.*]] = !test.test_rec_alias<name4, !name5_>
8+
// CHECK: ![[$NAME3:.*]] = !test.test_rec_alias<name3, !name4_>
69

710
// CHECK-LABEL: @roundtrip
811
func.func @roundtrip() {
@@ -24,6 +27,14 @@ func.func @roundtrip() {
2427
// CHECK: () -> ![[$NAME2]]
2528
"test.dummy_op_for_roundtrip"() : () -> !test.test_rec_alias<name2, tuple<!test.test_rec_alias<name2>, i32>>
2629
"test.dummy_op_for_roundtrip"() : () -> !test.test_rec_alias<name2, tuple<!test.test_rec_alias<name2>, i32>>
30+
31+
// Mutual recursion.
32+
// CHECK: () -> ![[$NAME3]]
33+
// CHECK: () -> ![[$NAME4]]
34+
// CHECK: () -> ![[$NAME5]]
35+
"test.dummy_op_for_roundtrip"() : () -> !test.test_rec_alias<name3, !test.test_rec_alias<name4, !test.test_rec_alias<name5, !test.test_rec_alias<name3>>>>
36+
"test.dummy_op_for_roundtrip"() : () -> !test.test_rec_alias<name4, !test.test_rec_alias<name5, !test.test_rec_alias<name3, !test.test_rec_alias<name4>>>>
37+
"test.dummy_op_for_roundtrip"() : () -> !test.test_rec_alias<name5, !test.test_rec_alias<name3, !test.test_rec_alias<name4, !test.test_rec_alias<name5>>>>
2738
return
2839
}
2940

0 commit comments

Comments
 (0)