xref: /llvm-project/mlir/test/lib/Dialect/Test/TestTypeDefs.td (revision 3c64f86314fbf9a3cd578419f16e621a4de57eaa)
1//===-- TestTypeDefs.td - Test dialect type 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-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// TableGen data type definitions for Test dialect.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef TEST_TYPEDEFS
14#define TEST_TYPEDEFS
15
16// To get the test dialect def.
17include "TestDialect.td"
18include "TestAttrDefs.td"
19include "TestInterfaces.td"
20include "mlir/IR/BuiltinTypes.td"
21include "mlir/Interfaces/DataLayoutInterfaces.td"
22
23// All of the types will extend this class.
24class Test_Type<string name, list<Trait> traits = []>
25    : TypeDef<Test_Dialect, name, traits>;
26
27def SimpleTypeA : Test_Type<"SimpleA"> {
28  let mnemonic = "smpla";
29}
30
31// A more complex parameterized type.
32def CompoundTypeA : Test_Type<"CompoundA"> {
33  let mnemonic = "cmpnd_a";
34
35  // List of type parameters.
36  let parameters = (
37    ins
38    "int":$widthOfSomething,
39    "::mlir::Type":$oneType,
40    // This is special syntax since ArrayRefs require allocation in the
41    // constructor.
42    ArrayRefParameter<
43      "int", // The parameter C++ type.
44      "An example of an array of ints" // Parameter description.
45    >:$arrayOfInts
46  );
47
48  let extraClassDeclaration = [{
49    struct SomeCppStruct {};
50  }];
51  let hasCustomAssemblyFormat = 1;
52}
53
54// A more complex and nested parameterized type.
55def CompoundNestedInnerType : Test_Type<"CompoundNestedInner"> {
56  let mnemonic = "cmpnd_inner";
57  // List of type parameters.
58  let parameters = (
59    ins
60    "int":$some_int,
61    CompoundTypeA:$cmpdA
62  );
63  let assemblyFormat = "`<` $some_int $cmpdA `>`";
64}
65
66def CompoundNestedOuterType : Test_Type<"CompoundNestedOuter"> {
67  let mnemonic = "cmpnd_nested_outer";
68
69  // List of type parameters.
70  let parameters = (ins CompoundNestedInnerType:$inner);
71  let assemblyFormat = "`<` `i`  $inner `>`";
72}
73
74def CompoundNestedOuterTypeQual : Test_Type<"CompoundNestedOuterQual"> {
75  let mnemonic = "cmpnd_nested_outer_qual";
76
77  // List of type parameters.
78  let parameters = (
79    ins
80    CompoundNestedInnerType:$inner
81  );
82  let assemblyFormat = "`<` `i`  qualified($inner) `>`";
83}
84
85// An example of how one could implement a standard integer.
86def IntegerType : Test_Type<"TestInteger"> {
87  let mnemonic = "int";
88  let genVerifyDecl = 1;
89  let parameters = (
90    ins
91    "unsigned":$width,
92    // SignednessSemantics is defined below.
93    "::test::TestIntegerType::SignednessSemantics":$signedness
94  );
95
96  // Indicate we use a custom format.
97  let hasCustomAssemblyFormat = 1;
98
99  // Define custom builder methods.
100  let builders = [
101    TypeBuilder<(ins "unsigned":$width,
102                     CArg<"SignednessSemantics", "Signless">:$signedness), [{
103      return $_get($_ctxt, width, signedness);
104    }]>
105  ];
106  let skipDefaultBuilders = 1;
107
108  // Any extra code one wants in the type's class declaration.
109  let extraClassDeclaration = [{
110    /// Signedness semantics.
111    enum SignednessSemantics {
112      Signless, /// No signedness semantics
113      Signed,   /// Signed integer
114      Unsigned, /// Unsigned integer
115    };
116
117    /// Return true if this is a signless integer type.
118    bool isSignless() const { return getSignedness() == Signless; }
119    /// Return true if this is a signed integer type.
120    bool isSigned() const { return getSignedness() == Signed; }
121    /// Return true if this is an unsigned integer type.
122    bool isUnsigned() const { return getSignedness() == Unsigned; }
123  }];
124}
125
126// A parent type for any type which is just a list of fields (e.g. structs,
127// unions).
128class FieldInfo_Type<string name> : Test_Type<name> {
129  let parameters = (
130    ins
131    // An ArrayRef of something which requires allocation in the storage
132    // constructor.
133    ArrayRefOfSelfAllocationParameter<
134      "::test::FieldInfo", // FieldInfo is defined/declared in TestTypes.h.
135      "Models struct fields">: $fields
136  );
137  let hasCustomAssemblyFormat = 1;
138}
139
140def StructType : FieldInfo_Type<"Struct"> {
141    let mnemonic = "struct";
142}
143
144def TestType : Test_Type<"Test", [
145  DeclareTypeInterfaceMethods<TestTypeInterface>
146]> {
147  let mnemonic = "test_type";
148}
149
150def TestTypeWithLayoutType : Test_Type<"TestTypeWithLayout", [
151  DeclareTypeInterfaceMethods<DataLayoutTypeInterface, ["getIndexBitwidth",
152                                                        "areCompatible"]>
153]> {
154  let mnemonic = "test_type_with_layout";
155  let parameters = (ins "unsigned":$key);
156  let extraClassDeclaration = [{
157    ::llvm::LogicalResult verifyEntries(::mlir::DataLayoutEntryListRef params,
158                                ::mlir::Location loc) const;
159
160  private:
161    uint64_t extractKind(::mlir::DataLayoutEntryListRef params,
162                         ::llvm::StringRef expectedKind) const;
163
164  public:
165  }];
166  let hasCustomAssemblyFormat = 1;
167}
168
169def TestMemRefElementType : Test_Type<"TestMemRefElementType",
170                                      [MemRefElementTypeInterface]> {
171  let mnemonic = "memref_element";
172}
173
174def TestTypeTrait : NativeTypeTrait<"TestTypeTrait">;
175
176// The definition of a singleton type that has a trait.
177def TestTypeWithTrait : Test_Type<"TestTypeWithTrait", [TestTypeTrait]> {
178  let mnemonic = "test_type_with_trait";
179}
180
181// Type with assembly format.
182def TestTypeWithFormat : Test_Type<"TestTypeWithFormat"> {
183  let parameters = (
184    ins
185    TestParamOne:$one,
186    TestParamTwo:$two,
187    "::mlir::Attribute":$three
188  );
189
190  let mnemonic = "type_with_format";
191  let assemblyFormat = "`<` $one `,` struct($three, $two) `>`";
192}
193
194// Test dispatch to parseField
195def TestTypeNoParser : Test_Type<"TestTypeNoParser"> {
196  let parameters = (
197    ins
198    "uint32_t":$one,
199    ArrayRefParameter<"int64_t">:$two,
200    StringRefParameter<>:$three,
201    "::test::CustomParam":$four
202  );
203
204  let mnemonic = "no_parser";
205  let assemblyFormat = "`<` $one `,` `[` $two `]` `,` $three `,` $four `>`";
206}
207
208def TestTypeStructCaptureAll : Test_Type<"TestStructTypeCaptureAll"> {
209  let parameters = (
210    ins
211    "int":$v0,
212    "int":$v1,
213    "int":$v2,
214    "int":$v3
215  );
216
217  let mnemonic = "struct_capture_all";
218  let assemblyFormat = "`<` struct(params) `>`";
219}
220
221def TestTypeOptionalParam : Test_Type<"TestTypeOptionalParam"> {
222  let parameters = (ins
223    OptionalParameter<"std::optional<int>">:$a,
224    "int":$b,
225    DefaultValuedParameter<"std::optional<::mlir::Attribute>",
226                           "std::nullopt">:$c
227  );
228  let mnemonic = "optional_param";
229  let assemblyFormat = "`<` $a `,` $b ( `,` $c^)? `>`";
230}
231
232def TestTypeOptionalParams : Test_Type<"TestTypeOptionalParams"> {
233  let parameters = (ins OptionalParameter<"std::optional<int>">:$a,
234                        StringRefParameter<>:$b);
235  let mnemonic = "optional_params";
236  let assemblyFormat = "`<` params `>`";
237}
238
239def TestTypeOptionalParamsAfterRequired
240    : Test_Type<"TestTypeOptionalParamsAfterRequired"> {
241  let parameters = (ins StringRefParameter<>:$a,
242                        OptionalParameter<"std::optional<int>">:$b);
243  let mnemonic = "optional_params_after";
244  let assemblyFormat = "`<` params `>`";
245}
246
247def TestTypeOptionalStruct : Test_Type<"TestTypeOptionalStruct"> {
248  let parameters = (ins OptionalParameter<"std::optional<int>">:$a,
249                        StringRefParameter<>:$b);
250  let mnemonic = "optional_struct";
251  let assemblyFormat = "`<` struct(params) `>`";
252}
253
254def TestTypeAllOptionalParams : Test_Type<"TestTypeAllOptionalParams"> {
255  let parameters = (ins OptionalParameter<"std::optional<int>">:$a,
256                        OptionalParameter<"std::optional<int>">:$b);
257  let mnemonic = "all_optional_params";
258  let assemblyFormat = "`<` params `>`";
259}
260
261def TestTypeAllOptionalStruct : Test_Type<"TestTypeAllOptionalStruct"> {
262  let parameters = (ins OptionalParameter<"std::optional<int>">:$a,
263                        OptionalParameter<"std::optional<int>">:$b);
264  let mnemonic = "all_optional_struct";
265  let assemblyFormat = "`<` struct(params) `>`";
266}
267
268def TestTypeOptionalGroup : Test_Type<"TestTypeOptionalGroup"> {
269  let parameters = (ins "int":$a, OptionalParameter<"std::optional<int>">:$b);
270  let mnemonic = "optional_group";
271  let assemblyFormat = "`<` (`(` $b^ `)`) : (`x`)? $a `>`";
272}
273
274def TestTypeOptionalGroupParams : Test_Type<"TestTypeOptionalGroupParams"> {
275  let parameters = (ins DefaultValuedParameter<"std::optional<int>", "10">:$a,
276                        OptionalParameter<"std::optional<int>">:$b);
277  let mnemonic = "optional_group_params";
278  let assemblyFormat = "`<` (`(` params^ `)`) : (`x`)? `>`";
279}
280
281def TestTypeOptionalGroupStruct : Test_Type<"TestTypeOptionalGroupStruct"> {
282  let parameters = (ins OptionalParameter<"std::optional<int>">:$a,
283                        OptionalParameter<"std::optional<int>">:$b);
284  let mnemonic = "optional_group_struct";
285  let assemblyFormat = "`<` (`(` struct(params)^ `)`) : (`x`)? `>`";
286}
287
288def TestTypeSpaces : Test_Type<"TestTypeSpaceS"> {
289  let parameters = (ins "int":$a, "int":$b);
290  let mnemonic = "spaces";
291  let assemblyFormat = "`<` ` ` $a `\\n` `(` `)` `` `(` `)` $b `>`";
292}
293
294class DefaultValuedAPFloat<string value>
295    : DefaultValuedParameter<"llvm::APFloat", "llvm::APFloat(" # value # ")"> {
296  let comparator = "$_lhs.bitwiseIsEqual($_rhs)";
297  let parser = [{ [&]() -> mlir::FailureOr<llvm::APFloat> {
298    mlir::FloatAttr attr;
299    auto result = $_parser.parseOptionalAttribute(attr);
300    if (result.has_value() && mlir::succeeded(*result))
301      return attr.getValue();
302    if (!result.has_value())
303      return llvm::APFloat(}] # value # [{);
304    return mlir::failure();
305  }() }];
306}
307
308def TestTypeAPFloat : Test_Type<"TestTypeAPFloat"> {
309  let parameters = (ins
310    DefaultValuedAPFloat<"APFloat::getZero(APFloat::IEEEdouble())">:$a
311  );
312  let mnemonic = "ap_float";
313  let assemblyFormat = "`<` $a `>`";
314}
315
316def TestTypeOptionalValueType : Test_Type<"TestTypeOptionalValueType"> {
317  let parameters = (ins
318    OptionalParameter<"std::optional<int>">:$value
319  );
320  let mnemonic = "optional_value_type";
321  let assemblyFormat = "(`<` $value^ `>`)?";
322}
323
324def TestTypeDefaultValuedType : Test_Type<"TestTypeDefaultValuedType"> {
325  let parameters = (ins
326    DefaultValuedParameter<"mlir::IntegerType",
327                           "mlir::IntegerType::get($_ctxt, 32)">:$type
328  );
329  let mnemonic = "default_valued_type";
330  let assemblyFormat = "`<` (`(` $type^ `)`)? `>`";
331}
332
333def TestTypeCustom : Test_Type<"TestTypeCustom"> {
334  let parameters = (ins "int":$a, OptionalParameter<"std::optional<int>">:$b);
335  let mnemonic = "custom_type";
336  let assemblyFormat = [{ `<` custom<CustomTypeA>($a) ``
337                              custom<CustomTypeB>(ref($a), $b) `>` }];
338}
339
340def TestTypeCustomSpacing : Test_Type<"TestTypeCustomSpacing"> {
341  let parameters = (ins "int":$a, "int":$b);
342  let mnemonic = "custom_type_spacing";
343  let assemblyFormat = [{ `<` custom<CustomTypeA>($a)
344                              custom<CustomTypeA>($b) `>` }];
345}
346
347def TestTypeCustomString : Test_Type<"TestTypeCustomString"> {
348  let parameters = (ins StringRefParameter<>:$foo);
349  let mnemonic = "custom_type_string";
350  let assemblyFormat = [{ `<` custom<FooString>($foo)
351                              custom<BarString>(ref($foo)) `>` }];
352}
353
354def TestTypeOptionalString : Test_Type<"TestTypeOptionalString"> {
355  let parameters = (ins StringRefParameter<"description", [{"default"}]>:$str);
356  let mnemonic = "optional_type_string";
357  let assemblyFormat = [{ (`<` $str^ `>`)? }];
358}
359
360def TestTypeElseAnchor : Test_Type<"TestTypeElseAnchor"> {
361  let parameters = (ins OptionalParameter<"std::optional<int>">:$a);
362  let mnemonic = "else_anchor";
363  let assemblyFormat = "`<` (`?`) : ($a^)? `>`";
364}
365
366def TestTypeElseAnchorStruct : Test_Type<"TestTypeElseAnchorStruct"> {
367  let parameters = (ins OptionalParameter<"std::optional<int>">:$a,
368                        OptionalParameter<"std::optional<int>">:$b);
369  let mnemonic = "else_anchor_struct";
370  let assemblyFormat = "`<` (`?`) : (struct($a, $b)^)? `>`";
371}
372
373def TestI32 : Test_Type<"TestI32"> {
374  let mnemonic = "i32";
375}
376
377def TestRecursiveAlias
378    : Test_Type<"TestRecursiveAlias", [MutableType]> {
379  let mnemonic = "test_rec_alias";
380  let storageClass = "TestRecursiveTypeStorage";
381  let storageNamespace = "test";
382  let genStorageClass = 0;
383
384  let parameters = (ins "llvm::StringRef":$name);
385
386  let hasCustomAssemblyFormat = 1;
387
388  let extraClassDeclaration = [{
389    Type getBody() const;
390
391    void setBody(Type type);
392  }];
393}
394
395def TestTypeVerification : Test_Type<"TestTypeVerification"> {
396  let parameters = (ins AnyTypeOf<[I16, I32]>:$param);
397  let mnemonic = "type_verification";
398  let assemblyFormat = "`<` $param `>`";
399}
400
401def TestTypeOpAsmTypeInterface : Test_Type<"TestTypeOpAsmTypeInterface",
402    [DeclareTypeInterfaceMethods<OpAsmTypeInterface, ["getAsmName"]>]> {
403  let mnemonic = "op_asm_type_interface";
404}
405
406#endif // TEST_TYPEDEFS
407