Skip to content

Commit 308f58c

Browse files
authored
[mlir][attrtypedef] Sort by def order in file. (#71083)
Enables having attribute that has another from same file as member.
1 parent 46732e2 commit 308f58c

File tree

2 files changed

+28
-23
lines changed

2 files changed

+28
-23
lines changed

mlir/test/mlir-tblgen/attrdefs.td

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,23 @@ include "mlir/IR/OpBase.td"
1414

1515
// DEF: #ifdef GET_ATTRDEF_LIST
1616
// DEF: #undef GET_ATTRDEF_LIST
17+
// DEF: ::test::IndexAttr,
1718
// DEF: ::test::SimpleAAttr,
1819
// DEF: ::test::CompoundAAttr,
19-
// DEF: ::test::IndexAttr,
2020
// DEF: ::test::SingleParameterAttr
2121

2222
// DEF-LABEL: ::mlir::OptionalParseResult generatedAttributeParser(
2323
// DEF-SAME: ::mlir::AsmParser &parser,
2424
// DEF-SAME: ::llvm::StringRef *mnemonic, ::mlir::Type type,
2525
// DEF-SAME: ::mlir::Attribute &value) {
2626
// DEF: return ::mlir::AsmParser::KeywordSwitch<::mlir::OptionalParseResult>(parser)
27+
// DEF: .Case(::test::IndexAttr::getMnemonic()
28+
// DEF-NEXT: value = ::test::IndexAttr::parse(parser, type);
29+
// DEF-NEXT: return ::mlir::success(!!value);
2730
// DEF: .Case(::test::CompoundAAttr::getMnemonic()
2831
// DEF-NEXT: value = ::test::CompoundAAttr::parse(parser, type);
2932
// DEF-NEXT: return ::mlir::success(!!value);
3033
// DEF-NEXT: })
31-
// DEF-NEXT: .Case(::test::IndexAttr::getMnemonic()
32-
// DEF-NEXT: value = ::test::IndexAttr::parse(parser, type);
33-
// DEF-NEXT: return ::mlir::success(!!value);
3434
// DEF: .Default([&](llvm::StringRef keyword,
3535
// DEF-NEXT: *mnemonic = keyword;
3636
// DEF-NEXT: return std::nullopt;
@@ -44,6 +44,24 @@ def Test_Dialect: Dialect {
4444

4545
class TestAttr<string name> : AttrDef<Test_Dialect, name> { }
4646

47+
def C_IndexAttr : TestAttr<"Index"> {
48+
let mnemonic = "index";
49+
50+
let parameters = (
51+
ins
52+
StringRefParameter<"Label for index">:$label
53+
);
54+
let hasCustomAssemblyFormat = 1;
55+
56+
// DECL-LABEL: class IndexAttr : public ::mlir::Attribute
57+
// DECL: static constexpr ::llvm::StringLiteral getMnemonic() {
58+
// DECL: return {"index"};
59+
// DECL: }
60+
// DECL: static ::mlir::Attribute parse(
61+
// DECL-SAME: ::mlir::AsmParser &odsParser, ::mlir::Type odsType);
62+
// DECL: void print(::mlir::AsmPrinter &odsPrinter) const;
63+
}
64+
4765
def A_SimpleAttrA : TestAttr<"SimpleA"> {
4866
// DECL: class SimpleAAttr : public ::mlir::Attribute
4967
}
@@ -100,24 +118,6 @@ def B_CompoundAttrA : TestAttr<"CompoundA"> {
100118
// DEF-NEXT: return getImpl()->inner;
101119
}
102120

103-
def C_IndexAttr : TestAttr<"Index"> {
104-
let mnemonic = "index";
105-
106-
let parameters = (
107-
ins
108-
StringRefParameter<"Label for index">:$label
109-
);
110-
let hasCustomAssemblyFormat = 1;
111-
112-
// DECL-LABEL: class IndexAttr : public ::mlir::Attribute
113-
// DECL: static constexpr ::llvm::StringLiteral getMnemonic() {
114-
// DECL: return {"index"};
115-
// DECL: }
116-
// DECL: static ::mlir::Attribute parse(
117-
// DECL-SAME: ::mlir::AsmParser &odsParser, ::mlir::Type odsType);
118-
// DECL: void print(::mlir::AsmPrinter &odsPrinter) const;
119-
}
120-
121121
def D_SingleParameterAttr : TestAttr<"SingleParameter"> {
122122
let parameters = (
123123
ins

mlir/tools/mlir-tblgen/AttrOrTypeDefGen.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -587,7 +587,12 @@ class DefGenerator {
587587
DefGenerator(std::vector<llvm::Record *> &&defs, raw_ostream &os,
588588
StringRef defType, StringRef valueType, bool isAttrGenerator)
589589
: defRecords(std::move(defs)), os(os), defType(defType),
590-
valueType(valueType), isAttrGenerator(isAttrGenerator) {}
590+
valueType(valueType), isAttrGenerator(isAttrGenerator) {
591+
// Sort by occurrence in file.
592+
llvm::sort(defRecords, [](llvm::Record *lhs, llvm::Record *rhs) {
593+
return lhs->getID() < rhs->getID();
594+
});
595+
}
591596

592597
/// Emit the list of def type names.
593598
void emitTypeDefList(ArrayRef<AttrOrTypeDef> defs);

0 commit comments

Comments
 (0)