xref: /llvm-project/mlir/include/mlir/Dialect/EmitC/IR/EmitCTypes.td (revision e47b507562624bf291ab2515699d39c2669b6131)
1//===- EmitCTypes.td - EmitC types -------------------------*- 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// Defines the MLIR EmitC types.
10//
11//===----------------------------------------------------------------------===//
12
13
14#ifndef MLIR_DIALECT_EMITC_IR_EMITCTYPES
15#define MLIR_DIALECT_EMITC_IR_EMITCTYPES
16
17include "mlir/IR/AttrTypeBase.td"
18include "mlir/Dialect/EmitC/IR/EmitCBase.td"
19include "mlir/IR/BuiltinTypeInterfaces.td"
20
21//===----------------------------------------------------------------------===//
22// EmitC type definitions
23//===----------------------------------------------------------------------===//
24
25def EmitCType : Type<CPred<"emitc::isSupportedEmitCType($_self)">,
26    "type supported by EmitC">;
27
28def EmitCIntegerType : Type<CPred<"emitc::isSupportedIntegerType($_self)">,
29    "integer type supported by EmitC">;
30
31def EmitCFloatType : Type<CPred<"emitc::isSupportedFloatType($_self)">,
32    "floating-point type supported by EmitC">;
33
34class EmitC_Type<string name, string typeMnemonic, list<Trait> traits = []>
35    : TypeDef<EmitC_Dialect, name, traits> {
36  let mnemonic = typeMnemonic;
37}
38
39def EmitC_ArrayType : EmitC_Type<"Array", "array", [ShapedTypeInterface]> {
40  let summary = "EmitC array type";
41
42  let description = [{
43    An array data type.
44
45    Example:
46
47    ```mlir
48    // Array emitted as `int32_t[10]`
49    !emitc.array<10xi32>
50    // Array emitted as `float[10][20]`
51    !emitc.array<10x20xf32>
52    ```
53  }];
54
55  let parameters = (ins
56    ArrayRefParameter<"int64_t">:$shape,
57    "Type":$elementType
58  );
59
60  let builders = [
61    TypeBuilderWithInferredContext<(ins
62      "ArrayRef<int64_t>":$shape,
63      "Type":$elementType
64    ), [{
65      return $_get(elementType.getContext(), shape, elementType);
66    }]>
67  ];
68  let extraClassDeclaration = [{
69    /// Returns if this type is ranked (always true).
70    bool hasRank() const { return true; }
71
72    /// Clone this array type with the given shape and element type. If the
73    /// provided shape is `std::nullopt`, the current shape of the type is used.
74    ArrayType cloneWith(std::optional<ArrayRef<int64_t>> shape,
75                        Type elementType) const;
76
77    static bool isValidElementType(Type type) {
78      return emitc::isSupportedFloatType(type) ||
79         emitc::isIntegerIndexOrOpaqueType(type) ||
80         llvm::isa<PointerType>(type);
81    }
82  }];
83  let genVerifyDecl = 1;
84  let hasCustomAssemblyFormat = 1;
85}
86
87def EmitC_LValueType : EmitC_Type<"LValue", "lvalue"> {
88  let summary = "EmitC lvalue type";
89
90  let description = [{
91    Values of this type can be assigned to and their address can be taken.
92  }];
93
94  let parameters = (ins "Type":$valueType);
95  let builders = [
96    TypeBuilderWithInferredContext<(ins "Type":$valueType), [{
97      return $_get(valueType.getContext(), valueType);
98    }]>
99  ];
100  let assemblyFormat = "`<` qualified($valueType) `>`";
101  let genVerifyDecl = 1;
102}
103
104def EmitC_OpaqueType : EmitC_Type<"Opaque", "opaque"> {
105  let summary = "EmitC opaque type";
106
107  let description = [{
108    An opaque data type of which the value gets emitted as is.
109
110    Example:
111
112    ```mlir
113    !emitc.opaque<"int">
114    !emitc.opaque<"mytype">
115    !emitc.opaque<"std::vector<std::string>">
116    ```
117  }];
118
119  let parameters = (ins StringRefParameter<"the opaque value">:$value);
120  let assemblyFormat = "`<` $value `>`";
121  let genVerifyDecl = 1;
122}
123
124def EmitC_PointerType : EmitC_Type<"Pointer", "ptr"> {
125  let summary = "EmitC pointer type";
126
127  let description = [{
128    A pointer data type.
129
130    Example:
131
132    ```mlir
133    // Pointer emitted as `int32_t*`
134    !emitc.ptr<i32>
135    // Pointer emitted as `float*`
136    !emitc.ptr<f32>
137    // Pointer emitted as `int*`
138    !emitc.ptr<!emitc.opaque<"int">>
139    ```
140  }];
141
142  let parameters = (ins "Type":$pointee);
143  let builders = [
144    TypeBuilderWithInferredContext<(ins "Type":$pointee), [{
145      return $_get(pointee.getContext(), pointee);
146    }]>
147  ];
148  let assemblyFormat = "`<` qualified($pointee) `>`";
149  let genVerifyDecl = 1;
150}
151
152def EmitC_SignedSizeT : EmitC_Type<"SignedSizeT", "ssize_t"> {
153  let summary = "EmitC signed size type";
154  let description = [{
155    Data type representing all values of `emitc.size_t`, plus -1.
156    It corresponds to `ssize_t` found in `<sys/types.h>`.
157
158    Use of this type causes the code to be non-C99 compliant.
159  }];
160}
161
162def EmitC_PtrDiffT : EmitC_Type<"PtrDiffT", "ptrdiff_t"> {
163  let summary = "EmitC signed pointer diff type";
164  let description = [{
165    Signed data type as wide as platform-specific pointer types.
166    In particular, it is as wide as `emitc.size_t`.
167    It corresponds to `ptrdiff_t` found in `<stddef.h>`.
168  }];
169}
170
171def EmitC_SizeT : EmitC_Type<"SizeT", "size_t"> {
172  let summary = "EmitC unsigned size type";
173  let description = [{
174    Unsigned data type as wide as platform-specific pointer types.
175    It corresponds to `size_t` found in `<stddef.h>`.
176  }];
177}
178
179class EmitC_LValueOf<list<Type> allowedTypes> :
180  ContainerType<
181    AnyTypeOf<allowedTypes>,
182    CPred<"::llvm::isa<::mlir::emitc::LValueType>($_self)">,
183    "::llvm::cast<::mlir::emitc::LValueType>($_self).getValueType()",
184    "emitc.lvalue",
185    "::mlir::emitc::LValueType"
186  >;
187
188#endif // MLIR_DIALECT_EMITC_IR_EMITCTYPES
189