1 //===-- TestAttrDefs.td - Test dialect attr definitions ----*- tablegen -*-===//
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-exception
7 //===----------------------------------------------------------------------===//
9 // TableGen data attribute definitions for Test dialect.
11 //===----------------------------------------------------------------------===//
16 // To get the test dialect definition.
17 include "TestDialect.td"
18 include "TestEnumDefs.td"
19 include "mlir/Dialect/Polynomial/IR/PolynomialAttributes.td"
20 include "mlir/Dialect/Utils/StructuredOpsUtils.td"
21 include "mlir/IR/AttrTypeBase.td"
22 include "mlir/IR/BuiltinAttributeInterfaces.td"
23 include "mlir/IR/EnumAttr.td"
24 include "mlir/IR/OpAsmInterface.td"
26 // All of the attributes will extend this class.
27 class Test_Attr<string name, list<Trait> traits = []>
28 : AttrDef<Test_Dialect, name, traits>;
30 class Test_LocAttr<string name> : LocationAttrDef<Test_Dialect, name, []>;
32 def SimpleAttrA : Test_Attr<"SimpleA"> {
33 let mnemonic = "smpla";
36 // A more complex parameterized attribute.
37 def CompoundAttrA : Test_Attr<"CompoundA"> {
38 let mnemonic = "cmpnd_a";
40 // List of type parameters.
43 "int":$widthOfSomething,
44 "::mlir::Type":$oneType,
45 // This is special syntax since ArrayRefs require allocation in the
48 "int", // The parameter C++ type.
49 "An example of an array of ints" // Parameter description.
52 let hasCustomAssemblyFormat = 1;
54 def CompoundAttrNested : Test_Attr<"CompoundAttrNested"> {
55 let mnemonic = "cmpnd_nested";
56 let parameters = (ins CompoundAttrA : $nested );
57 let assemblyFormat = "`<` `nested` `=` $nested `>`";
60 // An attribute testing AttributeSelfTypeParameter.
61 def AttrWithSelfTypeParam
62 : Test_Attr<"AttrWithSelfTypeParam", [TypedAttrInterface]> {
63 let mnemonic = "attr_with_self_type_param";
64 let parameters = (ins AttributeSelfTypeParameter<"">:$type);
65 let assemblyFormat = "";
68 // An attribute testing AttributeSelfTypeParameter.
69 def AttrWithTypeBuilder
70 : Test_Attr<"AttrWithTypeBuilder", [TypedAttrInterface]> {
71 let mnemonic = "attr_with_type_builder";
73 "::mlir::IntegerAttr":$attr,
74 AttributeSelfTypeParameter<"", "mlir::Type", "$attr.getType()">:$type
76 let assemblyFormat = "$attr";
79 def TestAttrTrait : NativeAttrTrait<"TestAttrTrait">;
81 // The definition of a singleton attribute that has a trait.
82 def AttrWithTrait : Test_Attr<"AttrWithTrait", [TestAttrTrait]> {
83 let mnemonic = "attr_with_trait";
86 // An attribute of a list of decimal formatted integers in similar format to shapes.
87 def TestDecimalShapeAttr : Test_Attr<"TestDecimalShape"> {
88 let mnemonic = "decimal_shape";
90 let parameters = (ins ArrayRefParameter<"int64_t">:$shape);
92 let hasCustomAssemblyFormat = 1;
95 // Test support for ElementsAttrInterface.
96 def TestI64ElementsAttr : Test_Attr<"TestI64Elements", [ElementsAttrInterface]> {
97 let mnemonic = "i64_elements";
99 AttributeSelfTypeParameter<"", "::mlir::ShapedType">:$type,
100 ArrayRefParameter<"uint64_t">:$elements
102 let extraClassDeclaration = [{
103 /// The set of data types that can be iterated by this attribute.
104 using ContiguousIterableTypesT = std::tuple<uint64_t>;
105 using NonContiguousIterableTypesT = std::tuple<mlir::Attribute, llvm::APInt>;
107 /// Provide begin iterators for the various iterable types.
109 mlir::FailureOr<const uint64_t *>
110 try_value_begin_impl(OverloadToken<uint64_t>) const {
111 return getElements().begin();
114 auto try_value_begin_impl(OverloadToken<mlir::Attribute>) const {
115 mlir::Type elementType = getType().getElementType();
116 return mlir::success(llvm::map_range(getElements(), [=](uint64_t value) {
117 return mlir::IntegerAttr::get(elementType,
118 llvm::APInt(/*numBits=*/64, value));
122 auto try_value_begin_impl(OverloadToken<llvm::APInt>) const {
123 return mlir::success(llvm::map_range(getElements(), [=](uint64_t value) {
124 return llvm::APInt(/*numBits=*/64, value);
128 let genVerifyDecl = 1;
129 let hasCustomAssemblyFormat = 1;
132 def TestSubElementsAccessAttr : Test_Attr<"TestSubElementsAccess"> {
133 let mnemonic = "sub_elements_access";
135 let parameters = (ins
136 "::mlir::Attribute":$first,
137 "::mlir::Attribute":$second,
138 "::mlir::Attribute":$third
140 let hasCustomAssemblyFormat = 1;
143 // A more complex parameterized attribute with multiple level of nesting.
144 def CompoundNestedInner : Test_Attr<"CompoundNestedInner"> {
145 let mnemonic = "cmpnd_nested_inner";
146 // List of type parameters.
152 let assemblyFormat = "`<` $some_int $cmpdA `>`";
155 def CompoundNestedOuter : Test_Attr<"CompoundNestedOuter"> {
156 let mnemonic = "cmpnd_nested_outer";
158 // List of type parameters.
161 CompoundNestedInner:$inner
163 let assemblyFormat = "`<` `i` $inner `>`";
166 def CompoundNestedOuterQual : Test_Attr<"CompoundNestedOuterQual"> {
167 let mnemonic = "cmpnd_nested_outer_qual";
169 // List of type parameters.
170 let parameters = (ins CompoundNestedInner:$inner);
171 let assemblyFormat = "`<` `i` qualified($inner) `>`";
174 def TestParamOne : AttrParameter<"int64_t", ""> {}
176 def TestParamTwo : AttrParameter<"std::string", "", "llvm::StringRef"> {
177 let printer = "$_printer << '\"' << $_self << '\"'";
180 def TestParamFour : ArrayRefParameter<"int", ""> {
181 let cppStorageType = "llvm::SmallVector<int>";
182 let parser = "::parseIntArray($_parser)";
183 let printer = "::printIntArray($_printer, $_self)";
187 def TestParamVector : ArrayRefParameter<"int", ""> {
188 let cppStorageType = "std::vector<int>";
191 def TestParamUnsigned : AttrParameter<"uint64_t", ""> {}
193 def TestAttrWithFormat : Test_Attr<"TestAttrWithFormat"> {
198 "::mlir::IntegerAttr":$three,
200 TestParamUnsigned:$five,
201 TestParamVector:$six,
202 // Array of another attribute.
204 "AttrWithTypeBuilderAttr", // The parameter C++ type.
205 "An example of an array of another Attribute" // Parameter description.
206 >: $arrayOfAttrWithTypeBuilderAttr
209 let mnemonic = "attr_with_format";
210 let assemblyFormat = [{
211 `<` $one `:` struct($two, $four) `:` $three `:` $five `:` `[` $six `]` `,`
212 `[` `` $arrayOfAttrWithTypeBuilderAttr `]` `>`
214 let genVerifyDecl = 1;
217 def TestAttrWithOptionalSigned : Test_Attr<"TestAttrWithOptionalSigned"> {
218 let parameters = (ins OptionalParameter<"std::optional<int64_t>">:$value);
219 let assemblyFormat = "`<` $value `>`";
220 let mnemonic = "attr_with_optional_signed";
223 def TestAttrWithOptionalUnsigned : Test_Attr<"TestAttrWithOptionalUnsigned"> {
224 let parameters = (ins OptionalParameter<"std::optional<uint64_t>">:$value);
225 let assemblyFormat = "`<` $value `>`";
226 let mnemonic = "attr_with_optional_unsigned";
229 def TestAttrUgly : Test_Attr<"TestAttrUgly"> {
230 let parameters = (ins "::mlir::Attribute":$attr);
232 let mnemonic = "attr_ugly";
233 let assemblyFormat = "`begin` $attr `end`";
236 def TestAttrParams: Test_Attr<"TestAttrParams"> {
237 let parameters = (ins "int":$v0, "int":$v1);
239 let mnemonic = "attr_params";
240 let assemblyFormat = "`<` params `>`";
243 // Test types can be parsed/printed.
244 def TestAttrWithTypeParam : Test_Attr<"TestAttrWithTypeParam"> {
245 let parameters = (ins "::mlir::IntegerType":$int_type,
246 "::mlir::Type":$any_type);
247 let mnemonic = "attr_with_type";
248 let assemblyFormat = "`<` $int_type `,` $any_type `>`";
251 // Test self type parameter with assembly format.
252 def TestAttrSelfTypeParameterFormat
253 : Test_Attr<"TestAttrSelfTypeParameterFormat", [TypedAttrInterface]> {
254 let parameters = (ins "int":$a, AttributeSelfTypeParameter<"">:$type);
256 let mnemonic = "attr_self_type_format";
257 let assemblyFormat = "`<` $a `>`";
260 def TestAttrSelfTypeParameterStructFormat
261 : Test_Attr<"TestAttrSelfTypeParameterStructFormat", [TypedAttrInterface]> {
262 let parameters = (ins "int":$a, AttributeSelfTypeParameter<"">:$type);
264 let mnemonic = "attr_self_type_struct_format";
265 let assemblyFormat = "`<` struct(params) `>`";
268 // Test overridding attribute builders with a custom builder.
269 def TestOverrideBuilderAttr : Test_Attr<"TestOverrideBuilder"> {
270 let mnemonic = "override_builder";
271 let parameters = (ins "int":$a);
272 let assemblyFormat = "`<` $a `>`";
274 let skipDefaultBuilders = 1;
275 let builders = [AttrBuilder<(ins "int":$a), [{
276 return ::mlir::IntegerAttr::get(::mlir::IndexType::get($_ctxt), a);
277 }], "::mlir::Attribute">];
280 // Test simple extern 1D vector using ElementsAttrInterface.
281 def TestExtern1DI64ElementsAttr : Test_Attr<"TestExtern1DI64Elements", [ElementsAttrInterface]> {
282 let mnemonic = "e1di64_elements";
283 let parameters = (ins
284 AttributeSelfTypeParameter<"", "::mlir::ShapedType">:$type,
285 ResourceHandleParameter<"TestDialectResourceBlobHandle">:$handle
287 let extraClassDeclaration = [{
288 /// Return the elements referenced by this attribute.
289 llvm::ArrayRef<uint64_t> getElements() const;
291 /// The set of data types that can be iterated by this attribute.
292 using ContiguousIterableTypesT = std::tuple<uint64_t>;
294 /// Provide begin iterators for the various iterable types.
296 mlir::FailureOr<const uint64_t *>
297 try_value_begin_impl(OverloadToken<uint64_t>) const {
298 return getElements().begin();
301 let assemblyFormat = "`<` $handle `>`";
304 // An array of nested attributes.
305 def TestArrayOfUglyAttrs : ArrayOfAttr<Test_Dialect, "ArrayOfUglyAttrs",
306 "array_of_ugly", "TestAttrUglyAttr"> {
307 let assemblyFormat = "`[` (`]`) : ($value^ ` ` `]`)?";
310 // An array of integers.
311 def TestArrayOfInts : ArrayOfAttr<Test_Dialect, "ArrayOfInts",
312 "array_of_ints", "int32_t">;
314 // An array of enum attributes.
315 def TestSimpleEnumAttr : EnumAttr<Test_Dialect, TestSimpleEnum, "simple_enum"> {
316 let assemblyFormat = "`` $value";
318 def TestArrayOfEnums : ArrayOfAttr<Test_Dialect, "ArrayOfEnums",
319 "array_of_enums", "SimpleEnumAttr">;
321 // Test custom directive as optional group anchor.
322 def TestCustomAnchor : Test_Attr<"TestCustomAnchor"> {
323 let parameters = (ins "int":$a, OptionalParameter<"std::optional<int>">:$b);
324 let mnemonic = "custom_anchor";
325 let assemblyFormat = "`<` $a (`>`) : (`,` custom<TrueFalse>($b)^ `>`)?";
328 def Test_IteratorTypeEnum
329 : EnumAttr<Test_Dialect, IteratorType, "iterator_type"> {
330 let assemblyFormat = "`<` $value `>`";
333 def Test_IteratorTypeArrayAttr
334 : TypedArrayAttrBase<Test_IteratorTypeEnum,
335 "Iterator type should be an enum.">;
337 def TestParamCopyCount : AttrParameter<"CopyCount", "", "const CopyCount &"> {}
339 // Test overridding attribute builders with a custom builder.
340 def TestCopyCount : Test_Attr<"TestCopyCount"> {
341 let mnemonic = "copy_count";
342 let parameters = (ins TestParamCopyCount:$copy_count);
343 let assemblyFormat = "`<` $copy_count `>`";
346 def TestConditionalAliasAttr : Test_Attr<"TestConditionalAlias"> {
347 let mnemonic = "conditional_alias";
348 let parameters = (ins "mlir::StringAttr":$value);
349 let assemblyFormat = [{
350 `<` custom<ConditionalAlias>($value) `>`
354 // Test AsmParser::parseFloat(const fltSemnatics&, APFloat&) API through the
355 // custom parser and printer.
356 def TestCustomFloatAttr : Test_Attr<"TestCustomFloat"> {
357 let mnemonic = "custom_float";
358 let parameters = (ins "mlir::StringAttr":$type_str, APFloatParameter<"">:$value);
360 let assemblyFormat = [{
361 `<` custom<CustomFloatAttr>($type_str, $value) `>`
365 def NestedPolynomialAttr : Test_Attr<"NestedPolynomialAttr"> {
366 let mnemonic = "nested_polynomial";
367 let parameters = (ins Polynomial_IntPolynomialAttr:$poly);
368 let assemblyFormat = [{
369 `<` struct(params) `>`
373 def NestedPolynomialAttr2 : Test_Attr<"NestedPolynomialAttr2"> {
374 let mnemonic = "nested_polynomial2";
375 let parameters = (ins OptionalParameter<"::mlir::polynomial::IntPolynomialAttr">:$poly);
376 let assemblyFormat = [{
377 `<` struct(params) `>`
382 // Test custom location handling.
383 def TestCustomLocationAttr : Test_LocAttr<"TestCustomLocation"> {
384 let mnemonic = "custom_location";
385 let parameters = (ins "mlir::StringAttr":$file, "unsigned":$line);
387 // Choose a silly separator token so we know it's hitting this code path
389 let assemblyFormat = "`<` $file `*` $line `>`";
392 #endif // TEST_ATTRDEFS