-
Notifications
You must be signed in to change notification settings - Fork 85
/
CIRTypes.td
248 lines (204 loc) · 8 KB
/
CIRTypes.td
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
//===- CIRTypes.td - CIR dialect types ---------------------*- tablegen -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file declares the CIR dialect types.
//
//===----------------------------------------------------------------------===//
#ifndef MLIR_CIR_DIALECT_CIR_TYPES
#define MLIR_CIR_DIALECT_CIR_TYPES
include "clang/CIR/Dialect/IR/CIRDialect.td"
include "mlir/Interfaces/DataLayoutInterfaces.td"
include "mlir/IR/AttrTypeBase.td"
//===----------------------------------------------------------------------===//
// CIR Types
//===----------------------------------------------------------------------===//
class CIR_Type<string name, string typeMnemonic, list<Trait> traits = []> :
TypeDef<CIR_Dialect, name, traits> {
let mnemonic = typeMnemonic;
}
//===----------------------------------------------------------------------===//
// IntType
//===----------------------------------------------------------------------===//
def CIR_IntType : CIR_Type<"Int", "int",
[DeclareTypeInterfaceMethods<DataLayoutTypeInterface>]> {
let summary = "Integer type with arbitrary precision up to a fixed limit";
let description = [{
CIR type that represents C/C++ primitive integer types.
Said types are: `char`, `short`, `int`, `long`, `long long`, and their \
unsigned variations.
}];
let parameters = (ins "unsigned":$width, "bool":$isSigned);
let hasCustomAssemblyFormat = 1;
let extraClassDeclaration = [{
/// Return true if this is a signed integer type.
bool isSigned() const { return getIsSigned(); }
/// Return true if this is an unsigned integer type.
bool isUnsigned() const { return !getIsSigned(); }
/// Return type alias.
std::string getAlias() const {
return (isSigned() ? 's' : 'u') + std::to_string(getWidth()) + 'i';
};
}];
let genVerifyDecl = 1;
}
//===----------------------------------------------------------------------===//
// PointerType
//===----------------------------------------------------------------------===//
def CIR_PointerType : CIR_Type<"Pointer", "ptr",
[DeclareTypeInterfaceMethods<DataLayoutTypeInterface>]> {
let summary = "CIR pointer type";
let description = [{
`CIR.ptr` is a type returned by any op generating a pointer in C++.
}];
let parameters = (ins "mlir::Type":$pointee);
let hasCustomAssemblyFormat = 1;
}
//===----------------------------------------------------------------------===//
// BoolType
//
// An alternative here is to represent bool as mlir::i1, but let's be more
// generic.
//
//===----------------------------------------------------------------------===//
def CIR_BoolType :
CIR_Type<"Bool", "bool",
[DeclareTypeInterfaceMethods<DataLayoutTypeInterface>]> {
let summary = "CIR bool type";
let description = [{
`cir.bool` represent's C++ bool type.
}];
let hasCustomAssemblyFormat = 1;
}
//===----------------------------------------------------------------------===//
// StructType
//
// The base type for all RecordDecls.
//
//===----------------------------------------------------------------------===//
def CIR_StructType : CIR_Type<"Struct", "struct",
[DeclareTypeInterfaceMethods<DataLayoutTypeInterface>]> {
let summary = "CIR struct type";
let description = [{
Each unique clang::RecordDecl is mapped to a `cir.struct` and any object in
C/C++ that has a struct type will have a `cir.struct` in CIR.
}];
let parameters = (ins
ArrayRefParameter<"mlir::Type", "members">:$members,
"mlir::StringAttr":$typeName,
"bool":$body,
"bool":$packed,
"std::optional<::mlir::cir::ASTRecordDeclAttr>":$ast
);
let builders = [
TypeBuilder<(ins
"ArrayRef<mlir::Type>":$members, "StringRef":$typeName,
"bool":$body
), [{
auto id = mlir::StringAttr::get(context, typeName);
auto sTy = StructType::get(context, members, id, body,
/*packed=*/false, std::nullopt);
return sTy;
}]>
];
let hasCustomAssemblyFormat = 1;
let extraClassDeclaration = [{
private:
// All these support lazily computation and storage
// for the struct size and alignment.
mutable std::optional<unsigned> size{}, align{};
mutable std::optional<bool> padded{};
void computeSizeAndAlignment(const ::mlir::DataLayout &dataLayout) const;
public:
void dropAst();
size_t getNumElements() const { return getMembers().size(); }
bool isOpaque() const { return !getBody(); }
bool isPadded(const ::mlir::DataLayout &dataLayout) const;
}];
let extraClassDefinition = [{
void $cppClass::dropAst() {
getImpl()->ast = std::nullopt;
}
}];
}
//===----------------------------------------------------------------------===//
// ArrayType
//===----------------------------------------------------------------------===//
def CIR_ArrayType : CIR_Type<"Array", "array",
[DeclareTypeInterfaceMethods<DataLayoutTypeInterface>]> {
let summary = "CIR array type";
let description = [{
`CIR.array` represents C/C++ constant arrays.
}];
let parameters = (ins "mlir::Type":$eltType, "uint64_t":$size);
let assemblyFormat = [{
`<` $eltType `x` $size `>`
}];
}
//===----------------------------------------------------------------------===//
// FuncType
//===----------------------------------------------------------------------===//
def CIR_FuncType : CIR_Type<"Func", "func"> {
let summary = "CIR function type";
let description = [{
The `!cir.func` is a function type. It consists of a single return type, a
list of parameter types and can optionally be variadic.
Example:
```mlir
!cir.func<!bool ()>
!cir.func<!s32i (!s8i, !s8i)>
!cir.func<!s32i (!s32i, ...)>
```
}];
let parameters = (ins ArrayRefParameter<"Type">:$inputs, "Type":$returnType,
"bool":$varArg);
let assemblyFormat = [{
`<` $returnType ` ` `(` custom<FuncTypeArgs>($inputs, $varArg) `>`
}];
let builders = [
TypeBuilderWithInferredContext<(ins
"ArrayRef<Type>":$inputs, "Type":$returnType,
CArg<"bool", "false">:$isVarArg), [{
return $_get(returnType.getContext(), inputs, returnType, isVarArg);
}]>
];
let extraClassDeclaration = [{
/// Returns whether the function is variadic.
bool isVarArg() const { return getVarArg(); }
/// Returns the `i`th input operand type. Asserts if out of bounds.
Type getInput(unsigned i) const { return getInputs()[i]; }
/// Returns the number of arguments to the function.
unsigned getNumInputs() const { return getInputs().size(); }
/// Returns the result type of the function as an ArrayRef, enabling better
/// integration with generic MLIR utilities.
ArrayRef<Type> getReturnTypes() const;
/// Returns whether the function is returns void.
bool isVoid() const;
/// Returns a clone of this function type with the given argument
/// and result types.
FuncType clone(TypeRange inputs, TypeRange results) const;
}];
}
//===----------------------------------------------------------------------===//
// Void type
//===----------------------------------------------------------------------===//
def CIR_VoidType : CIR_Type<"Void", "void"> {
let summary = "CIR void type";
let description = [{
The `!cir.void` type represents the C/C++ `void` type.
}];
let extraClassDeclaration = [{
/// Returns a clone of this type with the given context.
std::string getAlias() const { return "void"; };
}];
}
//===----------------------------------------------------------------------===//
// One type to bind them all
//===----------------------------------------------------------------------===//
def CIR_AnyCIRType : AnyTypeOf<[CIR_PointerType, CIR_BoolType, CIR_StructType,
CIR_ArrayType, CIR_FuncType, CIR_VoidType]>;
#endif // MLIR_CIR_DIALECT_CIR_TYPES