xref: /llvm-project/mlir/include/mlir/IR/BuiltinDialectBytecode.td (revision 3b35b4c7f9141c59fbac415e335489494b7d507e)
1//===-- BuiltinBytecode.td - Builtin bytecode defs ---------*- 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// This is the Builtin bytecode reader/writer definition file.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef BUILTIN_BYTECODE
14#define BUILTIN_BYTECODE
15
16include "mlir/IR/BytecodeBase.td"
17
18def LocationAttr : AttributeKind;
19def ShapedType: WithType<"ShapedType", Type>;
20
21def Location : CompositeBytecode {
22  dag members = (attr
23    WithGetter<"(LocationAttr)$_attrType", WithType<"LocationAttr", LocationAttr>>:$value
24  );
25  let cBuilder = "Location($_args)";
26}
27
28def String :
29  WithParser <"succeeded($_reader.readString($_var))",
30  WithBuilder<"$_args",
31  WithPrinter<"$_writer.writeOwnedString($_getter)",
32  WithGetter <"$_attrType",
33  WithType   <"StringRef">>>>>;
34
35def ArrayAttr : DialectAttribute<(attr
36  Array<Attribute>:$value
37)>;
38
39let cType = "StringAttr" in {
40def StringAttr : DialectAttribute<(attr
41  String:$value
42)> {
43  let printerPredicate = "::llvm::isa<NoneType>($_val.getType())";
44}
45
46def StringAttrWithType : DialectAttribute<(attr
47  String:$value,
48  Type:$type
49)> { let printerPredicate = "!::llvm::isa<NoneType>($_val.getType())"; }
50}
51
52def NamedAttribute : CompositeBytecode {
53  dag members = (attr
54    StringAttr:$name,
55    Attribute:$value
56  );
57  let cBuilder = "NamedAttribute($_args)";
58}
59def DictionaryAttr : DialectAttribute<(attr
60  Array<NamedAttribute>:$value
61)>;
62
63def FlatSymbolRefAttr: DialectAttribute<(attr
64  StringAttr:$rootReference
65)>;
66
67def SymbolRefAttr: DialectAttribute<(attr
68  StringAttr:$rootReference,
69  Array<FlatSymbolRefAttr>:$nestedReferences
70)>;
71
72def TypeAttr: DialectAttribute<(attr
73  Type:$value
74)>;
75
76def UnitAttr: DialectAttribute<(attr)>;
77
78def IntegerAttr: DialectAttribute<(attr
79  Type:$type,
80  KnownWidthAPInt<"type">:$value
81)> {
82  let cBuilder = "get<$_resultType>(context, type, *value)";
83}
84
85defvar FloatType = Type;
86def FloatAttr : DialectAttribute<(attr
87  FloatType:$type,
88  KnownSemanticsAPFloat<"type">:$value
89)> {
90  let cBuilder = "get<$_resultType>(context, type, *value)";
91}
92
93def CallSiteLoc : DialectAttribute<(attr
94  LocationAttr:$callee,
95  LocationAttr:$caller
96)>;
97
98let cType = "FileLineColRange" in {
99def FileLineColRange : DialectAttribute<(attr
100  StringAttr:$filename,
101  WithBuilder<"$_args",
102    WithType<"SmallVector<uint64_t>",
103    WithParser<"succeeded(readFileLineColRangeLocs($_reader, $_var))",
104    WithPrinter<"writeFileLineColRangeLocs($_writer, $_name)">>>>:$rawLocData
105)> {
106  let cBuilder = "getFileLineColRange(context, filename, rawLocData)";
107  let printerPredicate = "!isStrictFileLineColLoc($_val)";
108}
109
110def FileLineColLoc : DialectAttribute<(attr
111  StringAttr:$filename,
112  VarInt:$start_line,
113  VarInt:$start_column
114)> {
115  let printerPredicate = "isStrictFileLineColLoc($_val)";
116}
117}
118
119let cType = "FusedLoc",
120    cBuilder = "cast<FusedLoc>(get<FusedLoc>(context, $_args))" in {
121def FusedLoc : DialectAttribute<(attr
122  Array<Location>:$locations
123)> {
124  let printerPredicate = "!$_val.getMetadata()";
125}
126
127def FusedLocWithMetadata : DialectAttribute<(attr
128  Array<Location>:$locations,
129  Attribute:$metadata
130)> {
131  let printerPredicate = "$_val.getMetadata()";
132}
133}
134
135def NameLoc : DialectAttribute<(attr
136  StringAttr:$name,
137  LocationAttr:$childLoc
138)>;
139
140def UnknownLoc : DialectAttribute<(attr)>;
141
142def DenseResourceElementsAttr : DialectAttribute<(attr
143  ShapedType:$type,
144  ResourceHandle<"DenseResourceElementsHandle">:$rawHandle
145)> {
146  // Note: order of serialization does not match order of builder.
147  let cBuilder = "get<$_resultType>(context, type, *rawHandle)";
148}
149
150let cType = "RankedTensorType" in {
151def RankedTensorType : DialectType<(type
152  Array<SignedVarIntList>:$shape,
153  Type:$elementType
154)> {
155  let printerPredicate = "!$_val.getEncoding()";
156}
157
158def RankedTensorTypeWithEncoding : DialectType<(type
159  Attribute:$encoding,
160  Array<SignedVarIntList>:$shape,
161  Type:$elementType
162)> {
163  let printerPredicate = "$_val.getEncoding()";
164  // Note: order of serialization does not match order of builder.
165  let cBuilder = "get<$_resultType>(context, shape, elementType, encoding)";
166}
167}
168
169def DenseArrayAttr : DialectAttribute<(attr
170  Type:$elementType,
171  VarInt:$size,
172  Blob:$rawData
173)>;
174
175def DenseElementsAttr : WithType<"DenseElementsAttr", Attribute>;
176def DenseIntOrFPElementsAttr : DialectAttribute<(attr
177  ShapedType:$type,
178  Blob:$rawData
179)> {
180  let cBuilder = "cast<$_resultType>($_resultType::getFromRawBuffer($_args))";
181}
182
183def DenseStringElementsAttr : DialectAttribute<(attr
184  ShapedType:$type,
185  WithGetter<"$_attrType.isSplat()", VarInt>:$_isSplat,
186  WithBuilder<"$_args",
187    WithType<"SmallVector<StringRef>",
188    WithParser <"succeeded(readPotentiallySplatString($_reader, type, _isSplat, $_var))",
189    WithPrinter<"writePotentiallySplatString($_writer, $_name)">>>>:$rawStringData
190)>;
191
192def DenseIntElementsAttr : WithType<"DenseIntElementsAttr", Attribute>;
193def SparseElementsAttr : DialectAttribute<(attr
194  ShapedType:$type,
195  DenseIntElementsAttr:$indices,
196  DenseElementsAttr:$values
197)>;
198
199def DistinctAttr : DialectAttribute<(attr
200  Attribute:$referencedAttr
201)>;
202
203// Types
204// -----
205
206def IntegerType : DialectType<(type
207  // Yes not pretty,
208  WithParser<"succeeded($_reader.readVarInt($_var))",
209    WithBuilder<"$_args",
210    WithPrinter<"$_writer.writeVarInt($_name.getWidth() << 2 | $_name.getSignedness())",
211    WithType   <"uint64_t">>>>:$_widthAndSignedness,
212  // Split up parsed varint for create method.
213  LocalVar<"uint64_t", "_widthAndSignedness >> 2">:$width,
214  LocalVar<"IntegerType::SignednessSemantics",
215    "static_cast<IntegerType::SignednessSemantics>(_widthAndSignedness & 0x3)">:$signedness
216)>;
217
218def IndexType : DialectType<(type)>;
219
220def FunctionType : DialectType<(type
221  Array<Type>:$inputs,
222  Array<Type>:$results
223)>;
224
225def BFloat16Type : DialectType<(type)>;
226
227def Float16Type : DialectType<(type)>;
228
229def Float32Type : DialectType<(type)>;
230
231def Float64Type : DialectType<(type)>;
232
233def Float80Type : DialectType<(type)>;
234
235def Float128Type : DialectType<(type)>;
236
237def ComplexType : DialectType<(type
238  Type:$elementType
239)>;
240
241def MemRefLayout: WithType<"MemRefLayoutAttrInterface", Attribute>;
242
243let cType = "MemRefType" in {
244def MemRefType : DialectType<(type
245  Array<SignedVarIntList>:$shape,
246  Type:$elementType,
247  MemRefLayout:$layout
248)> {
249  let printerPredicate = "!$_val.getMemorySpace()";
250}
251
252def MemRefTypeWithMemSpace : DialectType<(type
253  Attribute:$memorySpace,
254  Array<SignedVarIntList>:$shape,
255  Type:$elementType,
256  MemRefLayout:$layout
257)> {
258  let printerPredicate = "!!$_val.getMemorySpace()";
259  // Note: order of serialization does not match order of builder.
260  let cBuilder = "get<$_resultType>(context, shape, elementType, layout, memorySpace)";
261}
262}
263
264def NoneType : DialectType<(type)>;
265
266def TupleType : DialectType<(type
267  Array<Type>:$types
268)>;
269
270let cType = "UnrankedMemRefType" in {
271def UnrankedMemRefType : DialectType<(type
272  Type:$elementType
273)> {
274  let printerPredicate = "!$_val.getMemorySpace()";
275  let cBuilder = "get<$_resultType>(context, elementType, Attribute())";
276}
277
278def UnrankedMemRefTypeWithMemSpace : DialectType<(type
279  Attribute:$memorySpace,
280  Type:$elementType
281)> {
282  let printerPredicate = "$_val.getMemorySpace()";
283  // Note: order of serialization does not match order of builder.
284  let cBuilder = "get<$_resultType>(context, elementType, memorySpace)";
285}
286}
287
288def UnrankedTensorType : DialectType<(type
289  Type:$elementType
290)>;
291
292let cType = "VectorType" in {
293def VectorType : DialectType<(type
294  Array<SignedVarIntList>:$shape,
295  Type:$elementType
296)> {
297  let printerPredicate = "!$_val.isScalable()";
298}
299
300def VectorTypeWithScalableDims : DialectType<(type
301  Array<BoolList>:$scalableDims,
302  Array<SignedVarIntList>:$shape,
303  Type:$elementType
304)> {
305  let printerPredicate = "$_val.isScalable()";
306  // Note: order of serialization does not match order of builder.
307  let cBuilder = "get<$_resultType>(context, shape, elementType, scalableDims)";
308}
309}
310
311/// This enum contains marker codes used to indicate which attribute is
312/// currently being decoded, and how it should be decoded. The order of these
313/// codes should generally be unchanged, as any changes will inevitably break
314/// compatibility with older bytecode.
315
316def BuiltinDialectAttributes : DialectAttributes<"Builtin"> {
317  let elems = [
318    ArrayAttr,
319    DictionaryAttr,
320    StringAttr,
321    StringAttrWithType,
322    FlatSymbolRefAttr,
323    SymbolRefAttr,
324    TypeAttr,
325    UnitAttr,
326    IntegerAttr,
327    FloatAttr,
328    CallSiteLoc,
329    FileLineColLoc,
330    FusedLoc,
331    FusedLocWithMetadata,
332    NameLoc,
333    UnknownLoc,
334    DenseResourceElementsAttr,
335    DenseArrayAttr,
336    DenseIntOrFPElementsAttr,
337    DenseStringElementsAttr,
338    SparseElementsAttr,
339    DistinctAttr,
340    FileLineColRange,
341  ];
342}
343
344def BuiltinDialectTypes : DialectTypes<"Builtin"> {
345  let elems = [
346    IntegerType,
347    IndexType,
348    FunctionType,
349    BFloat16Type,
350    Float16Type,
351    Float32Type,
352    Float64Type,
353    Float80Type,
354    Float128Type,
355    ComplexType,
356    MemRefType,
357    MemRefTypeWithMemSpace,
358    NoneType,
359    RankedTensorType,
360    RankedTensorTypeWithEncoding,
361    TupleType,
362    UnrankedMemRefType,
363    UnrankedMemRefTypeWithMemSpace,
364    UnrankedTensorType,
365    VectorType,
366    VectorTypeWithScalableDims
367  ];
368}
369
370#endif // BUILTIN_BYTECODE
371