xref: /llvm-project/mlir/test/mlir-tblgen/typedefs.td (revision db791b278a414fb6df1acc1799adcf11d8fb9169)
1// RUN: mlir-tblgen -gen-typedef-decls -I %S/../../include %s | FileCheck %s --check-prefix=DECL
2// RUN: mlir-tblgen -gen-typedef-defs -I %S/../../include %s | FileCheck %s --check-prefix=DEF
3
4include "mlir/IR/AttrTypeBase.td"
5include "mlir/IR/OpBase.td"
6
7// DECL: #ifdef GET_TYPEDEF_CLASSES
8// DECL: #undef GET_TYPEDEF_CLASSES
9
10// DECL: namespace mlir {
11// DECL: class AsmParser;
12// DECL: class AsmPrinter;
13// DECL: } // namespace mlir
14
15// DEF: #ifdef GET_TYPEDEF_LIST
16// DEF: #undef GET_TYPEDEF_LIST
17// DEF: ::test::SimpleAType,
18// DEF: ::test::CompoundAType,
19// DEF: ::test::IndexType,
20// DEF: ::test::SingleParameterType,
21// DEF: ::test::IntegerType
22
23// DEF-LABEL: ::mlir::OptionalParseResult generatedTypeParser(
24// DEF-SAME: ::mlir::AsmParser &parser,
25// DEF-SAME: ::llvm::StringRef *mnemonic,
26// DEF-SAME: ::mlir::Type &value) {
27// DEF: .Case(::test::CompoundAType::getMnemonic()
28// DEF-NEXT:   value = ::test::CompoundAType::parse(parser);
29// DEF-NEXT:   return ::mlir::success(!!value);
30// DEF-NEXT: })
31// DEF-NEXT: .Case(::test::IndexType::getMnemonic()
32// DEF-NEXT:   value = ::test::IndexType::parse(parser);
33// DEF-NEXT:   return ::mlir::success(!!value);
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: TestDialect
40  let name = "TestDialect";
41  let cppNamespace = "::test";
42}
43
44class TestType<string name> : TypeDef<Test_Dialect, name> { }
45
46def A_SimpleTypeA : TestType<"SimpleA"> {
47// DECL: class SimpleAType : public ::mlir::Type
48  let typeName = "test.simple_a";
49}
50
51def RTLValueType : Type<CPred<"isRTLValueType($_self)">, "Type"> {
52  string cppType = "::mlir::Type";
53}
54
55// A more complex parameterized type
56def B_CompoundTypeA : TestType<"CompoundA"> {
57  let summary = "A more complex parameterized type";
58  let description = "This type is to test a reasonably complex type";
59  let mnemonic = "cmpnd_a";
60  let parameters = (ins
61    "int":$widthOfSomething,
62    "::test::SimpleTypeA": $exampleTdType,
63    "SomeCppStruct": $exampleCppType,
64    ArrayRefParameter<"int", "Matrix dimensions">:$dims,
65    RTLValueType:$inner
66  );
67
68  let genVerifyDecl = 1;
69  let hasCustomAssemblyFormat = 1;
70
71// DECL-LABEL: class CompoundAType : public ::mlir::Type
72// DECL: static CompoundAType getChecked(::llvm::function_ref<::mlir::InFlightDiagnostic()> emitError, ::mlir::MLIRContext *context, int widthOfSomething, ::test::SimpleTypeA exampleTdType, SomeCppStruct exampleCppType, ::llvm::ArrayRef<int> dims, ::mlir::Type inner);
73// DECL: static ::llvm::LogicalResult verify(::llvm::function_ref<::mlir::InFlightDiagnostic()> emitError, int widthOfSomething, ::test::SimpleTypeA exampleTdType, SomeCppStruct exampleCppType, ::llvm::ArrayRef<int> dims, ::mlir::Type inner);
74// DECL: static constexpr ::llvm::StringLiteral getMnemonic() {
75// DECL:   return {"cmpnd_a"};
76// DECL: }
77// DECL: static ::mlir::Type parse(::mlir::AsmParser &odsParser);
78// DECL: void print(::mlir::AsmPrinter &odsPrinter) const;
79// DECL: int getWidthOfSomething() const;
80// DECL: ::test::SimpleTypeA getExampleTdType() const;
81// DECL: SomeCppStruct getExampleCppType() const;
82}
83
84def C_IndexType : TestType<"Index"> {
85  let mnemonic = "index";
86
87  let parameters = (ins
88    StringRefParameter<"Label for index">:$label
89  );
90  let hasCustomAssemblyFormat = 1;
91
92// DECL-LABEL: class IndexType : public ::mlir::Type
93// DECL: static constexpr ::llvm::StringLiteral getMnemonic() {
94// DECL:   return {"index"};
95// DECL: }
96// DECL: static ::mlir::Type parse(::mlir::AsmParser &odsParser);
97// DECL: void print(::mlir::AsmPrinter &odsPrinter) const;
98}
99
100def D_SingleParameterType : TestType<"SingleParameter"> {
101  let typeName = "test.d_single_parameter";
102  let parameters = (ins
103    "int": $num
104  );
105// DECL-LABEL: struct SingleParameterTypeStorage;
106// DECL-LABEL: class SingleParameterType
107// DECL-SAME:  detail::SingleParameterTypeStorage
108}
109
110def E_IntegerType : TestType<"Integer"> {
111  let mnemonic = "int";
112  let genVerifyDecl = 1;
113  let hasCustomAssemblyFormat = 1;
114  let parameters = (ins
115      "SignednessSemantics":$signedness,
116      TypeParameter<"unsigned", "Bitwidth of integer">:$width
117  );
118
119// DECL-LABEL: IntegerType : public ::mlir::Type
120
121  let extraClassDeclaration = [{
122  /// Signedness semantics.
123  enum SignednessSemantics {
124    Signless, /// No signedness semantics
125    Signed,   /// Signed integer
126    Unsigned, /// Unsigned integer
127  };
128
129  /// This extra function is necessary since it doesn't include signedness
130  static IntegerType getChecked(unsigned width, Location location);
131
132  /// Return true if this is a signless integer type.
133  bool isSignless() const { return getSignedness() == Signless; }
134  /// Return true if this is a signed integer type.
135  bool isSigned() const { return getSignedness() == Signed; }
136  /// Return true if this is an unsigned integer type.
137  bool isUnsigned() const { return getSignedness() == Unsigned; }
138  }];
139
140// DECL: /// Signedness semantics.
141// DECL-NEXT: enum SignednessSemantics {
142// DECL-NEXT:   Signless, /// No signedness semantics
143// DECL-NEXT:   Signed,   /// Signed integer
144// DECL-NEXT:   Unsigned, /// Unsigned integer
145// DECL-NEXT: };
146// DECL: /// This extra function is necessary since it doesn't include signedness
147// DECL-NEXT: static IntegerType getChecked(unsigned width, Location location);
148
149// DECL: /// Return true if this is a signless integer type.
150// DECL-NEXT: bool isSignless() const { return getSignedness() == Signless; }
151// DECL-NEXT: /// Return true if this is a signed integer type.
152// DECL-NEXT: bool isSigned() const { return getSignedness() == Signed; }
153// DECL-NEXT: /// Return true if this is an unsigned integer type.
154// DECL-NEXT: bool isUnsigned() const { return getSignedness() == Unsigned; }
155}
156