1 // RUN: mlir-tblgen -gen-attrdef-decls -I %S/../../include %s | FileCheck %s --check-prefix=DECL
2 // RUN: mlir-tblgen -gen-attrdef-defs -I %S/../../include %s | FileCheck %s --check-prefix=DEF
4 include "mlir/IR/OpBase.td"
6 // DECL: #ifdef GET_ATTRDEF_CLASSES
7 // DECL: #undef GET_ATTRDEF_CLASSES
9 // DECL: namespace mlir {
10 // DECL: class AsmParser;
11 // DECL: class AsmPrinter;
12 // DECL: } // namespace mlir
14 // DEF: #ifdef GET_ATTRDEF_LIST
15 // DEF: #undef GET_ATTRDEF_LIST
16 // DEF: ::test::SimpleAAttr,
17 // DEF: ::test::CompoundAAttr,
18 // DEF: ::test::IndexAttr,
19 // DEF: ::test::SingleParameterAttr
21 // DEF-LABEL: ::mlir::OptionalParseResult generatedAttributeParser(
22 // DEF-SAME: ::mlir::AsmParser &parser,
23 // DEF-SAME: ::llvm::StringRef mnemonic, ::mlir::Type type,
24 // DEF-SAME: ::mlir::Attribute &value) {
25 // DEF: if (mnemonic == ::test::CompoundAAttr::getMnemonic()) {
26 // DEF-NEXT: value = ::test::CompoundAAttr::parse(parser, type);
27 // DEF-NEXT: return ::mlir::success(!!value);
29 // DEF-NEXT: if (mnemonic == ::test::IndexAttr::getMnemonic()) {
30 // DEF-NEXT: value = ::test::IndexAttr::parse(parser, type);
31 // DEF-NEXT: return ::mlir::success(!!value);
34 def Test_Dialect: Dialect {
35 // DECL-NOT: TestDialect
36 // DEF-NOT: TestDialect
37 let name = "TestDialect";
38 let cppNamespace = "::test";
41 class TestAttr<string name> : AttrDef<Test_Dialect, name> { }
43 def A_SimpleAttrA : TestAttr<"SimpleA"> {
44 // DECL: class SimpleAAttr : public ::mlir::Attribute
47 // A more complex parameterized type
48 def B_CompoundAttrA : TestAttr<"CompoundA"> {
49 let summary = "A more complex parameterized attribute";
50 let description = "This attribute is to test a reasonably complex attribute";
51 let mnemonic = "cmpnd_a";
54 "int":$widthOfSomething,
55 "::test::SimpleTypeA": $exampleTdType,
56 APFloatParameter<"">: $apFloat,
57 ArrayRefParameter<"int", "Matrix dimensions">:$dims,
58 AttributeSelfTypeParameter<"">:$inner
61 let genVerifyDecl = 1;
63 // DECL-LABEL: class CompoundAAttr : public ::mlir::Attribute
64 // DECL: static CompoundAAttr getChecked(::llvm::function_ref<::mlir::InFlightDiagnostic()> emitError, ::mlir::MLIRContext *context, int widthOfSomething, ::test::SimpleTypeA exampleTdType, ::llvm::APFloat apFloat, ::llvm::ArrayRef<int> dims, ::mlir::Type inner);
65 // DECL: static ::mlir::LogicalResult verify(::llvm::function_ref<::mlir::InFlightDiagnostic()> emitError, int widthOfSomething, ::test::SimpleTypeA exampleTdType, ::llvm::APFloat apFloat, ::llvm::ArrayRef<int> dims, ::mlir::Type inner);
66 // DECL: static constexpr ::llvm::StringLiteral getMnemonic() {
67 // DECL: return {"cmpnd_a"};
69 // DECL: static ::mlir::Attribute parse(
70 // DECL-SAME: ::mlir::AsmParser &parser, ::mlir::Type type);
71 // DECL: void print(::mlir::AsmPrinter &printer) const;
72 // DECL: int getWidthOfSomething() const;
73 // DECL: ::test::SimpleTypeA getExampleTdType() const;
74 // DECL: ::llvm::APFloat getApFloat() const;
76 // Check that AttributeSelfTypeParameter is handled properly.
77 // DEF-LABEL: struct CompoundAAttrStorage
78 // DEF: CompoundAAttrStorage(
79 // DEF-SAME: : ::mlir::AttributeStorage(inner),
81 // DEF: bool operator==(const KeyTy &tblgenKey) const {
83 // DEF-SAME: (widthOfSomething == std::get<0>(tblgenKey)) &&
84 // DEF-SAME: (exampleTdType == std::get<1>(tblgenKey)) &&
85 // DEF-SAME: (apFloat.bitwiseIsEqual(std::get<2>(tblgenKey))) &&
86 // DEF-SAME: (dims == std::get<3>(tblgenKey)) &&
87 // DEF-SAME: (getType() == std::get<4>(tblgenKey));
89 // DEF: static CompoundAAttrStorage *construct
90 // DEF: return new (allocator.allocate<CompoundAAttrStorage>())
91 // DEF-SAME: CompoundAAttrStorage(widthOfSomething, exampleTdType, apFloat, dims, inner);
93 // DEF: ::mlir::Type CompoundAAttr::getInner() const {
94 // DEF-NEXT: return getImpl()->getType().cast<::mlir::Type>();
97 def C_IndexAttr : TestAttr<"Index"> {
98 let mnemonic = "index";
102 StringRefParameter<"Label for index">:$label
105 // DECL-LABEL: class IndexAttr : public ::mlir::Attribute
106 // DECL: static constexpr ::llvm::StringLiteral getMnemonic() {
107 // DECL: return {"index"};
109 // DECL: static ::mlir::Attribute parse(
110 // DECL-SAME: ::mlir::AsmParser &parser, ::mlir::Type type);
111 // DECL: void print(::mlir::AsmPrinter &printer) const;
114 def D_SingleParameterAttr : TestAttr<"SingleParameter"> {
119 // DECL-LABEL: struct SingleParameterAttrStorage;
120 // DECL-LABEL: class SingleParameterAttr
121 // DECL-SAME: detail::SingleParameterAttrStorage
124 // An attribute testing AttributeSelfTypeParameter.
125 def E_AttrWithTypeBuilder : TestAttr<"AttrWithTypeBuilder"> {
126 let mnemonic = "attr_with_type_builder";
127 let parameters = (ins "::mlir::IntegerAttr":$attr);
128 let typeBuilder = "$_attr.getType()";
131 // DEF-LABEL: struct AttrWithTypeBuilderAttrStorage
132 // DEF: AttrWithTypeBuilderAttrStorage(::mlir::IntegerAttr attr)
133 // DEF-SAME: : ::mlir::AttributeStorage(attr.getType()), attr(attr)
135 def F_ParamWithAccessorTypeAttr : TestAttr<"ParamWithAccessorType"> {
136 let parameters = (ins AttrParameter<"std::string", "", "StringRef">:$param);
139 // DECL-LABEL: class ParamWithAccessorTypeAttr
140 // DECL: StringRef getParam()
141 // DEF: ParamWithAccessorTypeAttrStorage
142 // DEF: ParamWithAccessorTypeAttrStorage(std::string param)
143 // DEF: StringRef ParamWithAccessorTypeAttr::getParam()