Skip to content

Commit 629460a

Browse files
committed
[mlir] Improve syntax of distinct[n]<unit>
In cases where memory is of less of a concern (e.g. small attributes where all instances have to be distinct by definition), using `DistinctAttr` with a unit attribute is a useful and conscious way of generating deterministic unique IDs. The syntax as is however, makes them less useful to use, as it 1) always prints `<unit>` at the back and 2) always aliases them leading to not very useful `#distinct = distinct[n]<unit>` lines in the printer output. This patch fixes that by special casing `UnitAttr` to simply elide the `unit` attribute in the back and not printing it as alias in that case. Differential Revision: https://reviews.llvm.org/D155162
1 parent cf40fde commit 629460a

File tree

4 files changed

+28
-12
lines changed

4 files changed

+28
-12
lines changed

mlir/lib/AsmParser/AttributeParser.cpp

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1244,10 +1244,20 @@ Attribute Parser::parseDistinctAttr(Type type) {
12441244
if (parseToken(Token::r_square, "expected ']' to close distinct ID") ||
12451245
parseToken(Token::less, "expected '<' after distinct ID"))
12461246
return {};
1247-
Attribute referencedAttr = parseAttribute(type);
1248-
if (!referencedAttr) {
1249-
emitError("expected attribute");
1250-
return {};
1247+
1248+
Attribute referencedAttr;
1249+
if (getToken().is(Token::greater)) {
1250+
consumeToken();
1251+
referencedAttr = builder.getUnitAttr();
1252+
} else {
1253+
referencedAttr = parseAttribute(type);
1254+
if (!referencedAttr) {
1255+
emitError("expected attribute");
1256+
return {};
1257+
}
1258+
1259+
if (parseToken(Token::greater, "expected '>' to close distinct attribute"))
1260+
return {};
12511261
}
12521262

12531263
// Add the distinct attribute to the parser state, if it has not been parsed
@@ -1265,8 +1275,5 @@ Attribute Parser::parseDistinctAttr(Type type) {
12651275
return {};
12661276
}
12671277

1268-
if (parseToken(Token::greater, "expected '>' to close distinct attribute"))
1269-
return {};
1270-
12711278
return it->getSecond();
12721279
}

mlir/lib/IR/AsmPrinter.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2141,7 +2141,9 @@ void AsmPrinter::Impl::printAttributeImpl(Attribute attr,
21412141
return;
21422142
} else if (auto distinctAttr = llvm::dyn_cast<DistinctAttr>(attr)) {
21432143
os << "distinct[" << state.getDistinctState().getId(distinctAttr) << "]<";
2144-
printAttribute(distinctAttr.getReferencedAttr());
2144+
if (!llvm::isa<UnitAttr>(distinctAttr.getReferencedAttr())) {
2145+
printAttribute(distinctAttr.getReferencedAttr());
2146+
}
21452147
os << '>';
21462148
return;
21472149
} else if (auto dictAttr = llvm::dyn_cast<DictionaryAttr>(attr)) {

mlir/lib/IR/BuiltinDialect.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,11 @@ struct BuiltinOpAsmDialectInterface : public OpAsmDialectInterface {
6060
os << "loc";
6161
return AliasResult::OverridableAlias;
6262
}
63-
if (llvm::isa<DistinctAttr>(attr)) {
64-
os << "distinct";
65-
return AliasResult::OverridableAlias;
66-
}
63+
if (auto distinct = llvm::dyn_cast<DistinctAttr>(attr))
64+
if (!llvm::isa<UnitAttr>(distinct.getReferencedAttr())) {
65+
os << "distinct";
66+
return AliasResult::OverridableAlias;
67+
}
6768
return AliasResult::NoAlias;
6869
}
6970

mlir/test/IR/distinct-attr.mlir

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,9 @@
2020
// CHECK: distinct.attr = #[[DISTINCT2]]
2121
// CHECK-GENERIC: distinct.attr = distinct[2]<42 : i32>
2222
"test.op"() {distinct.attr = distinct[42]<42 : i32>} : () -> ()
23+
24+
// CHECK: distinct.attr = distinct[3]<>
25+
"test.op"() {distinct.attr = distinct[3]<>} : () -> ()
26+
27+
// CHECK: distinct.attr = distinct[4]<>
28+
"test.op"() {distinct.attr = distinct[4]<unit>} : () -> ()

0 commit comments

Comments
 (0)