Skip to content

Commit

Permalink
[mlir][attrtypedef] Sort by def order in file. (#71083)
Browse files Browse the repository at this point in the history
Enables having attribute that has another from same file as member.
  • Loading branch information
jpienaar authored and zahiraam committed Nov 20, 2023
1 parent 8e743ba commit 79c52a8
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 23 deletions.
44 changes: 22 additions & 22 deletions mlir/test/mlir-tblgen/attrdefs.td
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,23 @@ include "mlir/IR/OpBase.td"

// DEF: #ifdef GET_ATTRDEF_LIST
// DEF: #undef GET_ATTRDEF_LIST
// DEF: ::test::IndexAttr,
// DEF: ::test::SimpleAAttr,
// DEF: ::test::CompoundAAttr,
// DEF: ::test::IndexAttr,
// DEF: ::test::SingleParameterAttr

// DEF-LABEL: ::mlir::OptionalParseResult generatedAttributeParser(
// DEF-SAME: ::mlir::AsmParser &parser,
// DEF-SAME: ::llvm::StringRef *mnemonic, ::mlir::Type type,
// DEF-SAME: ::mlir::Attribute &value) {
// DEF: return ::mlir::AsmParser::KeywordSwitch<::mlir::OptionalParseResult>(parser)
// DEF: .Case(::test::IndexAttr::getMnemonic()
// DEF-NEXT: value = ::test::IndexAttr::parse(parser, type);
// DEF-NEXT: return ::mlir::success(!!value);
// DEF: .Case(::test::CompoundAAttr::getMnemonic()
// DEF-NEXT: value = ::test::CompoundAAttr::parse(parser, type);
// DEF-NEXT: return ::mlir::success(!!value);
// DEF-NEXT: })
// DEF-NEXT: .Case(::test::IndexAttr::getMnemonic()
// DEF-NEXT: value = ::test::IndexAttr::parse(parser, type);
// DEF-NEXT: return ::mlir::success(!!value);
// DEF: .Default([&](llvm::StringRef keyword,
// DEF-NEXT: *mnemonic = keyword;
// DEF-NEXT: return std::nullopt;
Expand All @@ -44,6 +44,24 @@ def Test_Dialect: Dialect {

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

def C_IndexAttr : TestAttr<"Index"> {
let mnemonic = "index";

let parameters = (
ins
StringRefParameter<"Label for index">:$label
);
let hasCustomAssemblyFormat = 1;

// DECL-LABEL: class IndexAttr : public ::mlir::Attribute
// DECL: static constexpr ::llvm::StringLiteral getMnemonic() {
// DECL: return {"index"};
// DECL: }
// DECL: static ::mlir::Attribute parse(
// DECL-SAME: ::mlir::AsmParser &odsParser, ::mlir::Type odsType);
// DECL: void print(::mlir::AsmPrinter &odsPrinter) const;
}

def A_SimpleAttrA : TestAttr<"SimpleA"> {
// DECL: class SimpleAAttr : public ::mlir::Attribute
}
Expand Down Expand Up @@ -100,24 +118,6 @@ def B_CompoundAttrA : TestAttr<"CompoundA"> {
// DEF-NEXT: return getImpl()->inner;
}

def C_IndexAttr : TestAttr<"Index"> {
let mnemonic = "index";

let parameters = (
ins
StringRefParameter<"Label for index">:$label
);
let hasCustomAssemblyFormat = 1;

// DECL-LABEL: class IndexAttr : public ::mlir::Attribute
// DECL: static constexpr ::llvm::StringLiteral getMnemonic() {
// DECL: return {"index"};
// DECL: }
// DECL: static ::mlir::Attribute parse(
// DECL-SAME: ::mlir::AsmParser &odsParser, ::mlir::Type odsType);
// DECL: void print(::mlir::AsmPrinter &odsPrinter) const;
}

def D_SingleParameterAttr : TestAttr<"SingleParameter"> {
let parameters = (
ins
Expand Down
7 changes: 6 additions & 1 deletion mlir/tools/mlir-tblgen/AttrOrTypeDefGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,12 @@ class DefGenerator {
DefGenerator(std::vector<llvm::Record *> &&defs, raw_ostream &os,
StringRef defType, StringRef valueType, bool isAttrGenerator)
: defRecords(std::move(defs)), os(os), defType(defType),
valueType(valueType), isAttrGenerator(isAttrGenerator) {}
valueType(valueType), isAttrGenerator(isAttrGenerator) {
// Sort by occurrence in file.
llvm::sort(defRecords, [](llvm::Record *lhs, llvm::Record *rhs) {
return lhs->getID() < rhs->getID();
});
}

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

0 comments on commit 79c52a8

Please sign in to comment.