460 lines · plain
1//===-- TestAttrDefs.td - Test dialect attr definitions ----*- tablegen -*-===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8//9// TableGen data attribute definitions for Test dialect.10//11//===----------------------------------------------------------------------===//12 13#ifndef TEST_ATTRDEFS14#define TEST_ATTRDEFS15 16// To get the test dialect definition.17include "TestDialect.td"18include "TestEnumDefs.td"19include "mlir/Dialect/Ptr/IR/MemorySpaceInterfaces.td"20include "mlir/Dialect/Utils/StructuredOpsUtils.td"21include "mlir/IR/AttrTypeBase.td"22include "mlir/IR/BuiltinAttributeInterfaces.td"23include "mlir/IR/EnumAttr.td"24include "mlir/IR/OpAsmInterface.td"25include "mlir/IR/TensorEncoding.td"26 27// All of the attributes will extend this class.28class Test_Attr<string name, list<Trait> traits = []>29 : AttrDef<Test_Dialect, name, traits>;30 31class Test_LocAttr<string name> : LocationAttrDef<Test_Dialect, name, []>;32 33def SimpleAttrA : Test_Attr<"SimpleA"> {34 let mnemonic = "smpla";35}36 37// A more complex parameterized attribute.38def CompoundAttrA : Test_Attr<"CompoundA"> {39 let mnemonic = "cmpnd_a";40 41 // List of type parameters.42 let parameters = (43 ins44 "int":$widthOfSomething,45 "::mlir::Type":$oneType,46 // This is special syntax since ArrayRefs require allocation in the47 // constructor.48 ArrayRefParameter<49 "int", // The parameter C++ type.50 "An example of an array of ints" // Parameter description.51 >: $arrayOfInts52 );53 let hasCustomAssemblyFormat = 1;54}55def CompoundAttrNested : Test_Attr<"CompoundAttrNested"> {56 let mnemonic = "cmpnd_nested";57 let parameters = (ins CompoundAttrA : $nested );58 let assemblyFormat = "`<` `nested` `=` $nested `>`";59}60 61// An attribute testing AttributeSelfTypeParameter.62def AttrWithSelfTypeParam63 : Test_Attr<"AttrWithSelfTypeParam", [TypedAttrInterface]> {64 let mnemonic = "attr_with_self_type_param";65 let parameters = (ins AttributeSelfTypeParameter<"">:$type);66 let assemblyFormat = "";67}68 69// An attribute testing AttributeSelfTypeParameter.70def AttrWithTypeBuilder71 : Test_Attr<"AttrWithTypeBuilder", [TypedAttrInterface]> {72 let mnemonic = "attr_with_type_builder";73 let parameters = (ins74 "::mlir::IntegerAttr":$attr,75 AttributeSelfTypeParameter<"", "mlir::Type", "$attr.getType()">:$type76 );77 let assemblyFormat = "$attr";78}79 80def TestAttrTrait : NativeAttrTrait<"TestAttrTrait">;81 82// The definition of a singleton attribute that has a trait.83def AttrWithTrait : Test_Attr<"AttrWithTrait", [TestAttrTrait]> {84 let mnemonic = "attr_with_trait";85}86 87// An attribute of a list of decimal formatted integers in similar format to shapes.88def TestDecimalShapeAttr : Test_Attr<"TestDecimalShape"> {89 let mnemonic = "decimal_shape";90 91 let parameters = (ins ArrayRefParameter<"int64_t">:$shape);92 93 let hasCustomAssemblyFormat = 1;94}95 96// Test support for ElementsAttrInterface.97def TestI64ElementsAttr : Test_Attr<"TestI64Elements", [ElementsAttrInterface]> {98 let mnemonic = "i64_elements";99 let parameters = (ins100 AttributeSelfTypeParameter<"", "::mlir::ShapedType">:$type,101 ArrayRefParameter<"uint64_t">:$elements102 );103 let extraClassDeclaration = [{104 /// The set of data types that can be iterated by this attribute.105 using ContiguousIterableTypesT = std::tuple<uint64_t>;106 using NonContiguousIterableTypesT = std::tuple<mlir::Attribute, llvm::APInt>;107 108 /// Provide begin iterators for the various iterable types.109 // * uint64_t110 mlir::FailureOr<const uint64_t *>111 try_value_begin_impl(OverloadToken<uint64_t>) const {112 return getElements().begin();113 }114 // * Attribute115 auto try_value_begin_impl(OverloadToken<mlir::Attribute>) const {116 mlir::Type elementType = getType().getElementType();117 return mlir::success(llvm::map_range(getElements(), [=](uint64_t value) {118 return mlir::IntegerAttr::get(elementType,119 llvm::APInt(/*numBits=*/64, value));120 }).begin());121 }122 // * APInt123 auto try_value_begin_impl(OverloadToken<llvm::APInt>) const {124 return mlir::success(llvm::map_range(getElements(), [=](uint64_t value) {125 return llvm::APInt(/*numBits=*/64, value);126 }).begin());127 }128 }];129 let genVerifyDecl = 1;130 let hasCustomAssemblyFormat = 1;131}132 133def TestSubElementsAccessAttr : Test_Attr<"TestSubElementsAccess"> {134 let mnemonic = "sub_elements_access";135 136 let parameters = (ins137 "::mlir::Attribute":$first,138 "::mlir::Attribute":$second,139 "::mlir::Attribute":$third140 );141 let hasCustomAssemblyFormat = 1;142}143 144// A more complex parameterized attribute with multiple level of nesting.145def CompoundNestedInner : Test_Attr<"CompoundNestedInner"> {146 let mnemonic = "cmpnd_nested_inner";147 // List of type parameters.148 let parameters = (149 ins150 "int":$some_int,151 CompoundAttrA:$cmpdA152 );153 let assemblyFormat = "`<` $some_int $cmpdA `>`";154}155 156def CompoundNestedOuter : Test_Attr<"CompoundNestedOuter"> {157 let mnemonic = "cmpnd_nested_outer";158 159 // List of type parameters.160 let parameters = (161 ins162 CompoundNestedInner:$inner163 );164 let assemblyFormat = "`<` `i` $inner `>`";165}166 167def CompoundNestedOuterQual : Test_Attr<"CompoundNestedOuterQual"> {168 let mnemonic = "cmpnd_nested_outer_qual";169 170 // List of type parameters.171 let parameters = (ins CompoundNestedInner:$inner);172 let assemblyFormat = "`<` `i` qualified($inner) `>`";173}174 175def TestParamOne : AttrParameter<"int64_t", ""> {}176 177def TestParamTwo : AttrParameter<"std::string", "", "llvm::StringRef"> {178 let printer = "$_printer << '\"' << $_self << '\"'";179}180 181def TestParamFour : ArrayRefParameter<"int", ""> {182 let cppStorageType = "llvm::SmallVector<int>";183 let parser = "::parseIntArray($_parser)";184 let printer = "::printIntArray($_printer, $_self)";185}186 187 188def TestParamVector : ArrayRefParameter<"int", ""> {189 let cppStorageType = "std::vector<int>";190}191 192def TestParamUnsigned : AttrParameter<"uint64_t", ""> {}193 194def TestAttrWithFormat : Test_Attr<"TestAttrWithFormat"> {195 let parameters = (196 ins197 TestParamOne:$one,198 TestParamTwo:$two,199 "::mlir::IntegerAttr":$three,200 TestParamFour:$four,201 TestParamUnsigned:$five,202 TestParamVector:$six,203 // Array of another attribute.204 ArrayRefParameter<205 "AttrWithTypeBuilderAttr", // The parameter C++ type.206 "An example of an array of another Attribute" // Parameter description.207 >: $arrayOfAttrWithTypeBuilderAttr208 );209 210 let mnemonic = "attr_with_format";211 let assemblyFormat = [{212 `<` $one `:` struct($two, $four) `:` $three `:` $five `:` `[` $six `]` `,`213 `[` `` $arrayOfAttrWithTypeBuilderAttr `]` `>`214 }];215 let genVerifyDecl = 1;216}217 218def TestAttrWithOptionalSigned : Test_Attr<"TestAttrWithOptionalSigned"> {219 let parameters = (ins OptionalParameter<"std::optional<int64_t>">:$value);220 let assemblyFormat = "`<` $value `>`";221 let mnemonic = "attr_with_optional_signed";222}223 224def TestAttrWithOptionalUnsigned : Test_Attr<"TestAttrWithOptionalUnsigned"> {225 let parameters = (ins OptionalParameter<"std::optional<uint64_t>">:$value);226 let assemblyFormat = "`<` $value `>`";227 let mnemonic = "attr_with_optional_unsigned";228}229 230def TestAttrWithOptionalEnum : Test_Attr<"TestAttrWithOptionalEnum"> {231 let parameters = (ins OptionalParameter<"std::optional<SimpleEnum>">:$value);232 let assemblyFormat = "`<` $value `>`";233 let mnemonic = "attr_with_optional_enum";234}235 236def TestAttrUgly : Test_Attr<"TestAttrUgly"> {237 let parameters = (ins "::mlir::Attribute":$attr);238 239 let mnemonic = "attr_ugly";240 let assemblyFormat = "`begin` $attr `end`";241}242 243def TestAttrParams: Test_Attr<"TestAttrParams"> {244 let parameters = (ins "int":$v0, "int":$v1);245 246 let mnemonic = "attr_params";247 let assemblyFormat = "`<` params `>`";248}249 250// Test types can be parsed/printed.251def TestAttrWithTypeParam : Test_Attr<"TestAttrWithTypeParam"> {252 let parameters = (ins "::mlir::IntegerType":$int_type,253 "::mlir::Type":$any_type);254 let mnemonic = "attr_with_type";255 let assemblyFormat = "`<` $int_type `,` $any_type `>`";256}257 258// Test self type parameter with assembly format.259def TestAttrSelfTypeParameterFormat260 : Test_Attr<"TestAttrSelfTypeParameterFormat", [TypedAttrInterface]> {261 let parameters = (ins "int":$a, AttributeSelfTypeParameter<"">:$type);262 263 let mnemonic = "attr_self_type_format";264 let assemblyFormat = "`<` $a `>`";265}266 267def TestAttrSelfTypeParameterStructFormat268 : Test_Attr<"TestAttrSelfTypeParameterStructFormat", [TypedAttrInterface]> {269 let parameters = (ins "int":$a, AttributeSelfTypeParameter<"">:$type);270 271 let mnemonic = "attr_self_type_struct_format";272 let assemblyFormat = "`<` struct(params) `>`";273}274 275// Test overridding attribute builders with a custom builder.276def TestOverrideBuilderAttr : Test_Attr<"TestOverrideBuilder"> {277 let mnemonic = "override_builder";278 let parameters = (ins "int":$a);279 let assemblyFormat = "`<` $a `>`";280 281 let skipDefaultBuilders = 1;282 let builders = [AttrBuilder<(ins "int":$a), [{283 return ::mlir::IntegerAttr::get(::mlir::IndexType::get($_ctxt), a);284 }], "::mlir::Attribute">];285}286 287// Test simple extern 1D vector using ElementsAttrInterface.288def TestExtern1DI64ElementsAttr : Test_Attr<"TestExtern1DI64Elements", [ElementsAttrInterface]> {289 let mnemonic = "e1di64_elements";290 let parameters = (ins291 AttributeSelfTypeParameter<"", "::mlir::ShapedType">:$type,292 ResourceHandleParameter<"TestDialectResourceBlobHandle">:$handle293 );294 let extraClassDeclaration = [{295 /// Return the elements referenced by this attribute.296 llvm::ArrayRef<uint64_t> getElements() const;297 298 /// The set of data types that can be iterated by this attribute.299 using ContiguousIterableTypesT = std::tuple<uint64_t>;300 301 /// Provide begin iterators for the various iterable types.302 // * uint64_t303 mlir::FailureOr<const uint64_t *>304 try_value_begin_impl(OverloadToken<uint64_t>) const {305 return getElements().begin();306 }307 }];308 let assemblyFormat = "`<` $handle `>`";309}310 311// An array of nested attributes.312def TestArrayOfUglyAttrs : ArrayOfAttr<Test_Dialect, "ArrayOfUglyAttrs",313 "array_of_ugly", "TestAttrUglyAttr"> {314 let assemblyFormat = "`[` (`]`) : ($value^ ` ` `]`)?";315}316 317// An array of integers.318def TestArrayOfInts : ArrayOfAttr<Test_Dialect, "ArrayOfInts",319 "array_of_ints", "int32_t">;320 321// An array of enum attributes.322def TestSimpleEnumAttr : EnumAttr<Test_Dialect, TestSimpleEnum, "simple_enum"> {323 let assemblyFormat = "`` $value";324}325def TestArrayOfEnums : ArrayOfAttr<Test_Dialect, "ArrayOfEnums",326 "array_of_enums", "SimpleEnumAttr">;327 328// Test custom directive as optional group anchor.329def TestCustomAnchor : Test_Attr<"TestCustomAnchor"> {330 let parameters = (ins "int":$a, OptionalParameter<"std::optional<int>">:$b);331 let mnemonic = "custom_anchor";332 let assemblyFormat = "`<` $a (`>`) : (`,` custom<TrueFalse>($b)^ `>`)?";333}334 335def Test_IteratorTypeEnum336 : EnumAttr<Test_Dialect, IteratorType, "iterator_type"> {337 let assemblyFormat = "`<` $value `>`";338}339 340def Test_IteratorTypeArrayAttr341 : TypedArrayAttrBase<Test_IteratorTypeEnum,342 "Iterator type should be an enum.">;343 344def TestParamCopyCount : AttrParameter<"CopyCount", "", "const CopyCount &"> {}345 346// Test overridding attribute builders with a custom builder.347def TestCopyCount : Test_Attr<"TestCopyCount"> {348 let mnemonic = "copy_count";349 let parameters = (ins TestParamCopyCount:$copy_count);350 let assemblyFormat = "`<` $copy_count `>`";351 let genVerifyDecl = 1;352}353 354def TestConditionalAliasAttr : Test_Attr<"TestConditionalAlias"> {355 let mnemonic = "conditional_alias";356 let parameters = (ins "mlir::StringAttr":$value);357 let assemblyFormat = [{358 `<` custom<ConditionalAlias>($value) `>`359 }];360}361 362// Test AsmParser::parseFloat(const fltSemnatics&, APFloat&) API through the363// custom parser and printer.364def TestCustomFloatAttr : Test_Attr<"TestCustomFloat"> {365 let mnemonic = "custom_float";366 let parameters = (ins "mlir::StringAttr":$type_str, APFloatParameter<"">:$value);367 368 let assemblyFormat = [{369 `<` custom<CustomFloatAttr>($type_str, $value) `>`370 }];371}372 373// Test `struct` with nested `custom` assembly format.374def TestCustomStructAttr : Test_Attr<"TestCustomStruct"> {375 let mnemonic = "custom_struct";376 let parameters = (ins "mlir::StringAttr":$type_str, "int64_t":$value,377 OptionalParameter<"mlir::ArrayAttr">:$opt_value);378 let assemblyFormat = [{379 `<` struct($type_str, custom<CustomStructAttr>($value), custom<CustomOptStructFieldAttr>($opt_value)) `>`380 }];381}382 383// Test a ptr constant memory space.384def TestConstMemorySpaceAttr : Test_Attr<"TestConstMemorySpace", [385 DeclareAttrInterfaceMethods<MemorySpaceAttrInterface>386 ]> {387 let mnemonic = "const_memory_space";388 let parameters = (ins DefaultValuedParameter<"unsigned", "0">:$addressSpace);389 let assemblyFormat = "(`<` $addressSpace^ `>`)?";390}391 392// Test custom location handling.393def TestCustomLocationAttr : Test_LocAttr<"TestCustomLocation"> {394 let mnemonic = "custom_location";395 let parameters = (ins "mlir::StringAttr":$file, "unsigned":$line);396 397 // Choose a silly separator token so we know it's hitting this code path398 // and not another.399 let assemblyFormat = "`<` $file `*` $line `>`";400}401 402// Test OpAsmAttrInterface.403def TestOpAsmAttrInterfaceAttr : Test_Attr<"TestOpAsmAttrInterface",404 [DeclareAttrInterfaceMethods<OpAsmAttrInterface, ["getAlias"]>]> {405 let mnemonic = "op_asm_attr_interface";406 let parameters = (ins "mlir::StringAttr":$value);407 let assemblyFormat = [{408 `<` struct(params) `>`409 }];410}411 412// Test OpAsmAttrInterface from tablegen genMnemonicAlias option.413def TestOpAsmAttrInterfaceTablegenDefaultAttr : Test_Attr<"TestOpAsmAttrInterfaceTablegenDefault"> {414 let mnemonic = "op_asm_attr_interface_tablegen_default";415 let parameters = (ins "mlir::StringAttr":$value);416 let assemblyFormat = [{417 `<` struct(params) `>`418 }];419 420 let genMnemonicAlias = 1;421}422 423// Test attribute containing a slash token424def SlashAttr: Test_Attr<"Slash">{425 let mnemonic = "slash_attr";426 427 let parameters = (428 ins429 "int":$lhs,430 "int":$rhs431 );432 433 let hasCustomAssemblyFormat = 1;434}435 436def TestCustomStorageCtorAttr : Test_Attr<"TestCustomStorageCtorAttr"> {437 let mnemonic = "custom_storage_ctor_attr";438 let parameters = (ins "int":$value);439 let assemblyFormat = "`<` $value `>`";440 let hasStorageCustomConstructor = 1;441}442 443def TestTensorEncodingAttr : Test_Attr<"TestTensorEncoding",444 [DeclareAttrInterfaceMethods<VerifiableTensorEncoding>]> {445 let mnemonic = "tensor_encoding";446 447 let parameters = (ins "mlir::StringAttr":$dummy);448 let assemblyFormat = "`<` $dummy `>`";449}450 451def TestMemRefLayoutAttr : Test_Attr<"TestMemRefLayout",452 [DeclareAttrInterfaceMethods<MemRefLayoutAttrInterface>]> {453 let mnemonic = "memref_layout";454 455 let parameters = (ins "mlir::StringAttr":$dummy);456 let assemblyFormat = "`<` $dummy `>`";457}458 459#endif // TEST_ATTRDEFS460