207 lines · plain
1// RUN: mlir-tblgen -gen-attrdef-decls -I %S/../../include %s | FileCheck %s --check-prefix=DECL2// RUN: mlir-tblgen -gen-attrdef-defs -I %S/../../include %s | FileCheck %s --check-prefix=DEF3 4include "mlir/IR/AttrTypeBase.td"5include "mlir/IR/OpBase.td"6 7// DECL: #ifdef GET_ATTRDEF_CLASSES8// DECL: #undef GET_ATTRDEF_CLASSES9 10// DECL: namespace mlir {11// DECL: class AsmParser;12// DECL: class AsmPrinter;13// DECL: } // namespace mlir14 15// DEF: #ifdef GET_ATTRDEF_LIST16// DEF: #undef GET_ATTRDEF_LIST17// DEF: ::test::IndexAttr,18// DEF: ::test::SimpleAAttr,19// DEF: ::test::CompoundAAttr,20// DEF: ::test::SingleParameterAttr21 22// DEF-LABEL: ::mlir::OptionalParseResult generatedAttributeParser(23// DEF-SAME: ::mlir::AsmParser &parser,24// DEF-SAME: ::llvm::StringRef *mnemonic, ::mlir::Type type,25// DEF-SAME: ::mlir::Attribute &value) {26// 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);30// DEF: .Case(::test::CompoundAAttr::getMnemonic()31// DEF-NEXT: value = ::test::CompoundAAttr::parse(parser, type);32// DEF-NEXT: return ::mlir::success(!!value);33// DEF-NEXT: })34// DEF: .Default([&](llvm::StringRef keyword,35// DEF-NEXT: *mnemonic = keyword;36// DEF-NEXT: return std::nullopt;37 38def Test_Dialect: Dialect {39// DECL-NOT: TestDialect40// DEF-NOT: TestDialect41 let name = "TestDialect";42 let cppNamespace = "::test";43}44 45class TestAttr<string name> : AttrDef<Test_Dialect, name> { }46 47def C_IndexAttr : TestAttr<"Index"> {48 let mnemonic = "index";49 50 let parameters = (51 ins52 StringRefParameter<"Label for index">:$label53 );54 let hasCustomAssemblyFormat = 1;55 56// DECL-LABEL: class IndexAttr : public ::mlir::Attribute57// 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 65def A_SimpleAttrA : TestAttr<"SimpleA"> {66 let attrName = "test.simple";67// DECL: class SimpleAAttr : public ::mlir::Attribute68}69 70// A more complex parameterized type71def B_CompoundAttrA : TestAttr<"CompoundA"> {72 let summary = "A more complex parameterized attribute";73 let description = "This attribute is to test a reasonably complex attribute";74 let mnemonic = "cmpnd_a";75 let parameters = (76 ins77 "int":$widthOfSomething,78 "::test::SimpleTypeA": $exampleTdType,79 APFloatParameter<"">: $apFloat,80 ArrayRefParameter<"int", "Matrix dimensions">:$dims,81 AttributeSelfTypeParameter<"">:$inner82 );83 84 let genVerifyDecl = 1;85 let hasCustomAssemblyFormat = 1;86 87// DECL-LABEL: class CompoundAAttr : public ::mlir::Attribute88// 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);89// DECL: static ::llvm::LogicalResult verify(::llvm::function_ref<::mlir::InFlightDiagnostic()> emitError, int widthOfSomething, ::test::SimpleTypeA exampleTdType, ::llvm::APFloat apFloat, ::llvm::ArrayRef<int> dims, ::mlir::Type inner);90// DECL: static constexpr ::llvm::StringLiteral getMnemonic() {91// DECL: return {"cmpnd_a"};92// DECL: }93// DECL: static ::mlir::Attribute parse(94// DECL-SAME: ::mlir::AsmParser &odsParser, ::mlir::Type odsType);95// DECL: void print(::mlir::AsmPrinter &odsPrinter) const;96// DECL: int getWidthOfSomething() const;97// DECL: ::test::SimpleTypeA getExampleTdType() const;98// DECL: ::llvm::APFloat getApFloat() const;99// DECL: ::mlir::Type getInner() const;100 101// Check that AttributeSelfTypeParameter is handled properly.102// DEF-LABEL: struct CompoundAAttrStorage103// DEF: CompoundAAttrStorage(104// DEF-SAME: inner(std::move(inner))105 106// DEF: bool operator==(const KeyTy &tblgenKey) const {107// DEF-NEXT: return108// DEF-SAME: (widthOfSomething == std::get<0>(tblgenKey)) &&109// DEF-SAME: (exampleTdType == std::get<1>(tblgenKey)) &&110// DEF-SAME: (apFloat.bitwiseIsEqual(std::get<2>(tblgenKey))) &&111// DEF-SAME: (dims == std::get<3>(tblgenKey)) &&112// DEF-SAME: (inner == std::get<4>(tblgenKey));113 114// DEF: static CompoundAAttrStorage *construct115// DEF: return new (allocator.allocate<CompoundAAttrStorage>())116// DEF-SAME: CompoundAAttrStorage(std::move(widthOfSomething), std::move(exampleTdType), std::move(apFloat), std::move(dims), std::move(inner));117 118// DEF: CompoundAAttr CompoundAAttr::getChecked(119// DEF-SAME: int widthOfSomething, ::test::SimpleTypeA exampleTdType, ::llvm::APFloat apFloat, ::llvm::ArrayRef<int> dims, ::mlir::Type inner120// DEF-SAME: )121// DEF-NEXT: return Base::getChecked(emitError, context, std::move(widthOfSomething), std::move(exampleTdType), std::move(apFloat), std::move(dims), std::move(inner));122 123// DEF: ::mlir::Type CompoundAAttr::getInner() const {124// DEF-NEXT: return getImpl()->inner;125}126 127def D_SingleParameterAttr : TestAttr<"SingleParameter"> {128 let parameters = (129 ins130 "int": $num131 );132 let attrName = "test.single_parameter";133// DECL-LABEL: struct SingleParameterAttrStorage;134// DECL-LABEL: class SingleParameterAttr135// DECL-SAME: detail::SingleParameterAttrStorage136}137 138def F_ParamWithAccessorTypeAttr : TestAttr<"ParamWithAccessorType"> {139 let parameters = (ins AttrParameter<"std::string", "", "StringRef">:$param);140 let attrName = "test.param_with_accessor_type";141}142 143// DECL-LABEL: class ParamWithAccessorTypeAttr144// DECL: StringRef getParam()145// DEF: ParamWithAccessorTypeAttrStorage146// DEF: ParamWithAccessorTypeAttrStorage(std::string param)147// DEF: StringRef ParamWithAccessorTypeAttr::getParam()148 149def G_BuilderWithReturnTypeAttr : TestAttr<"BuilderWithReturnType"> {150 let parameters = (ins "int":$a);151 let genVerifyDecl = 1;152 let builders = [AttrBuilder<(ins), [{ return {}; }], "::mlir::Attribute">];153 let attrName = "test.builder_with_return_type";154}155 156// DECL-LABEL: class BuilderWithReturnTypeAttr157// DECL: ::mlir::Attribute get(158// DECL: ::mlir::Attribute getChecked(159 160def H_TestExtraClassAttr : TestAttr<"TestExtraClass"> {161 let extraClassDeclaration = [{162 /// Test Method163 static int getFoo(int i);164 }];165 let extraClassDefinition = [{166 int $cppClass::getFoo(int i) {167 return i+1;168 }169 }];170 let attrName = "test.test_extra_class";171}172 173// DECL-LABEL: TestExtraClassAttr : public ::mlir::Attribute174// DECL: /// Test Method175// DECL-NEXT: static int getFoo(int i);176 177// DEF-LABEL: int TestExtraClassAttr::getFoo(int i) {178// DEF: return i+1;179// DEF-NEXT: }180 181def I_TestGenMnemonicAliasAttr : TestAttr<"TestGenMnemonicAlias"> {182 let mnemonic = "test_gen_mnemonic_alias";183 let genMnemonicAlias = 1;184}185 186// DECL-LABEL: class TestGenMnemonicAliasAttr : public ::mlir::Attribute187// DECL-SAME: ::mlir::OpAsmAttrInterface::Trait188// DECL: ::mlir::OpAsmAliasResult getAlias(::llvm::raw_ostream &os) const;189 190// DEF-LABEL: ::mlir::OpAsmAliasResult TestGenMnemonicAliasAttr::getAlias(::llvm::raw_ostream &os) const {191// DEF-NEXT: os << "test_gen_mnemonic_alias";192// DEF-NEXT: return ::mlir::OpAsmAliasResult::OverridableAlias;193// DEF-NEXT: }194 195def J_CustomStorageCtorAttr : AttrDef<Test_Dialect, "CustomStorageCtorAttr"> {196 let attrName = "test_custom_storage_ctor_attr";197 let parameters = (ins "bool":$flag);198 let hasStorageCustomConstructor = 1;199}200 201// Note: ';' at the end of construct method declaration is important - otherwise202// one cannot provide custom definition203 204// DEF-LABEL: struct CustomStorageCtorAttrAttrStorage : public ::mlir::AttributeStorage205// DEF: static CustomStorageCtorAttrAttrStorage *construct206// DEF-SAME: (::mlir::AttributeStorageAllocator &allocator, KeyTy &&tblgenKey);207