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 TestAttrWithOptionalEnum : Test_Attr<"TestAttrWithOptionalEnum"> {
230 let parameters = (ins OptionalParameter<"std::optional<SimpleEnum>">:$value);
231 let assemblyFormat = "`<` $value `>`";
232 let mnemonic = "attr_with_optional_enum";
235 def TestAttrUgly : Test_Attr<"TestAttrUgly"> {
236 let parameters = (ins "::mlir::Attribute":$attr);
238 let mnemonic = "attr_ugly";
239 let assemblyFormat = "`begin` $attr `end`";
242 def TestAttrParams: Test_Attr<"TestAttrParams"> {
243 let parameters = (ins "int":$v0, "int":$v1);
245 let mnemonic = "attr_params";
246 let assemblyFormat = "`<` params `>`";
249 // Test types can be parsed/printed.
250 def TestAttrWithTypeParam : Test_Attr<"TestAttrWithTypeParam"> {
251 let parameters = (ins "::mlir::IntegerType":$int_type,
252 "::mlir::Type":$any_type);
253 let mnemonic = "attr_with_type";
254 let assemblyFormat = "`<` $int_type `,` $any_type `>`";
257 // Test self type parameter with assembly format.
258 def TestAttrSelfTypeParameterFormat
259 : Test_Attr<"TestAttrSelfTypeParameterFormat", [TypedAttrInterface]> {
260 let parameters = (ins "int":$a, AttributeSelfTypeParameter<"">:$type);
262 let mnemonic = "attr_self_type_format";
263 let assemblyFormat = "`<` $a `>`";
266 def TestAttrSelfTypeParameterStructFormat
267 : Test_Attr<"TestAttrSelfTypeParameterStructFormat", [TypedAttrInterface]> {
268 let parameters = (ins "int":$a, AttributeSelfTypeParameter<"">:$type);
270 let mnemonic = "attr_self_type_struct_format";
271 let assemblyFormat = "`<` struct(params) `>`";
274 // Test overridding attribute builders with a custom builder.
275 def TestOverrideBuilderAttr : Test_Attr<"TestOverrideBuilder"> {
276 let mnemonic = "override_builder";
277 let parameters = (ins "int":$a);
278 let assemblyFormat = "`<` $a `>`";
280 let skipDefaultBuilders = 1;
281 let builders = [AttrBuilder<(ins "int":$a), [{
282 return ::mlir::IntegerAttr::get(::mlir::IndexType::get($_ctxt), a);
283 }], "::mlir::Attribute">];
286 // Test simple extern 1D vector using ElementsAttrInterface.
287 def TestExtern1DI64ElementsAttr : Test_Attr<"TestExtern1DI64Elements", [ElementsAttrInterface]> {
288 let mnemonic = "e1di64_elements";
289 let parameters = (ins
290 AttributeSelfTypeParameter<"", "::mlir::ShapedType">:$type,
291 ResourceHandleParameter<"TestDialectResourceBlobHandle">:$handle
293 let extraClassDeclaration = [{
294 /// Return the elements referenced by this attribute.
295 llvm::ArrayRef<uint64_t> getElements() const;
297 /// The set of data types that can be iterated by this attribute.
298 using ContiguousIterableTypesT = std::tuple<uint64_t>;
300 /// Provide begin iterators for the various iterable types.
302 mlir::FailureOr<const uint64_t *>
303 try_value_begin_impl(OverloadToken<uint64_t>) const {
304 return getElements().begin();
307 let assemblyFormat = "`<` $handle `>`";
310 // An array of nested attributes.
311 def TestArrayOfUglyAttrs : ArrayOfAttr<Test_Dialect, "ArrayOfUglyAttrs",
312 "array_of_ugly", "TestAttrUglyAttr"> {
313 let assemblyFormat = "`[` (`]`) : ($value^ ` ` `]`)?";
316 // An array of integers.
317 def TestArrayOfInts : ArrayOfAttr<Test_Dialect, "ArrayOfInts",
318 "array_of_ints", "int32_t">;
320 // An array of enum attributes.
321 def TestSimpleEnumAttr : EnumAttr<Test_Dialect, TestSimpleEnum, "simple_enum"> {
322 let assemblyFormat = "`` $value";
324 def TestArrayOfEnums : ArrayOfAttr<Test_Dialect, "ArrayOfEnums",
325 "array_of_enums", "SimpleEnumAttr">;
327 // Test custom directive as optional group anchor.
328 def TestCustomAnchor : Test_Attr<"TestCustomAnchor"> {
329 let parameters = (ins "int":$a, OptionalParameter<"std::optional<int>">:$b);
330 let mnemonic = "custom_anchor";
331 let assemblyFormat = "`<` $a (`>`) : (`,` custom<TrueFalse>($b)^ `>`)?";
334 def Test_IteratorTypeEnum
335 : EnumAttr<Test_Dialect, IteratorType, "iterator_type"> {
336 let assemblyFormat = "`<` $value `>`";
339 def Test_IteratorTypeArrayAttr
340 : TypedArrayAttrBase<Test_IteratorTypeEnum,
341 "Iterator type should be an enum.">;
343 def TestParamCopyCount : AttrParameter<"CopyCount", "", "const CopyCount &"> {}
345 // Test overridding attribute builders with a custom builder.
346 def TestCopyCount : Test_Attr<"TestCopyCount"> {
347 let mnemonic = "copy_count";
348 let parameters = (ins TestParamCopyCount:$copy_count);
349 let assemblyFormat = "`<` $copy_count `>`";
352 def TestConditionalAliasAttr : Test_Attr<"TestConditionalAlias"> {
353 let mnemonic = "conditional_alias";
354 let parameters = (ins "mlir::StringAttr":$value);
355 let assemblyFormat = [{
356 `<` custom<ConditionalAlias>($value) `>`
360 // Test AsmParser::parseFloat(const fltSemnatics&, APFloat&) API through the
361 // custom parser and printer.
362 def TestCustomFloatAttr : Test_Attr<"TestCustomFloat"> {
363 let mnemonic = "custom_float";
364 let parameters = (ins "mlir::StringAttr":$type_str, APFloatParameter<"">:$value);
366 let assemblyFormat = [{
367 `<` custom<CustomFloatAttr>($type_str, $value) `>`
371 def NestedPolynomialAttr : Test_Attr<"NestedPolynomialAttr"> {
372 let mnemonic = "nested_polynomial";
373 let parameters = (ins Polynomial_IntPolynomialAttr:$poly);
374 let assemblyFormat = [{
375 `<` struct(params) `>`
379 def NestedPolynomialAttr2 : Test_Attr<"NestedPolynomialAttr2"> {
380 let mnemonic = "nested_polynomial2";
381 let parameters = (ins OptionalParameter<"::mlir::polynomial::IntPolynomialAttr">:$poly);
382 let assemblyFormat = [{
383 `<` struct(params) `>`
388 // Test custom location handling.
389 def TestCustomLocationAttr : Test_LocAttr<"TestCustomLocation"> {
390 let mnemonic = "custom_location";
391 let parameters = (ins "mlir::StringAttr":$file, "unsigned":$line);
393 // Choose a silly separator token so we know it's hitting this code path
395 let assemblyFormat = "`<` $file `*` $line `>`";
398 #endif // TEST_ATTRDEFS