blob: c99d5073ef2557f069de78d21bf7e2cc7ff1f140 [file] [log] [blame]
Sven van Haastregt79a222f2019-06-03 09:39:11 +00001//==--- OpenCLBuiltins.td - OpenCL builtin declarations -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
6// See https://llvm.org/LICENSE.txt for license information.
7// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8//
9//===----------------------------------------------------------------------===//
10//
11// This file contains TableGen definitions for OpenCL builtin function
12// declarations. In case of an unresolved function name in OpenCL, Clang will
13// check for a function described in this file when -fdeclare-opencl-builtins
14// is specified.
15//
16//===----------------------------------------------------------------------===//
17
18//===----------------------------------------------------------------------===//
19// Definitions of miscellaneous basic entities.
20//===----------------------------------------------------------------------===//
21// Versions of OpenCL
22class Version<int _Version> {
23 int Version = _Version;
24}
25def CL10: Version<100>;
26def CL11: Version<110>;
27def CL12: Version<120>;
28def CL20: Version<200>;
29
30// Address spaces
31// Pointer types need to be assigned an address space.
32class AddressSpace<string _AS> {
Sven van Haastregt89fb9e82019-07-29 14:55:29 +000033 string Name = _AS;
Sven van Haastregt79a222f2019-06-03 09:39:11 +000034}
Sven van Haastregt89fb9e82019-07-29 14:55:29 +000035def DefaultAS : AddressSpace<"clang::LangAS::Default">;
36def PrivateAS : AddressSpace<"clang::LangAS::opencl_private">;
37def GlobalAS : AddressSpace<"clang::LangAS::opencl_global">;
38def ConstantAS : AddressSpace<"clang::LangAS::opencl_constant">;
39def LocalAS : AddressSpace<"clang::LangAS::opencl_local">;
40def GenericAS : AddressSpace<"clang::LangAS::opencl_generic">;
Sven van Haastregt79a222f2019-06-03 09:39:11 +000041
42
Sven van Haastregtb21a3652019-08-19 11:56:03 +000043// Qualified Type. These map to ASTContext::QualType.
44class QualType<string _Name, bit _IsAbstract=0> {
Sven van Haastregt79a222f2019-06-03 09:39:11 +000045 // Name of the field or function in a clang::ASTContext
46 // E.g. Name="IntTy" for the int type, and "getIntPtrType()" for an intptr_t
47 string Name = _Name;
Sven van Haastregtb21a3652019-08-19 11:56:03 +000048 // Some QualTypes in this file represent an abstract type for which there is
49 // no corresponding AST QualType, e.g. a GenType or an `image2d_t` type
50 // without access qualifiers.
51 bit IsAbstract = _IsAbstract;
Sven van Haastregt79a222f2019-06-03 09:39:11 +000052}
53
54// Helper class to store type access qualifiers (volatile, const, ...).
55class Qualifier<string _QualName> {
56 string QualName = _QualName;
57}
58
Sven van Haastregtb21a3652019-08-19 11:56:03 +000059// List of integers.
60class IntList<string _Name, list<int> _List> {
61 string Name = _Name;
62 list<int> List = _List;
63}
64
Sven van Haastregt79a222f2019-06-03 09:39:11 +000065//===----------------------------------------------------------------------===//
66// OpenCL C classes for types
67//===----------------------------------------------------------------------===//
Sven van Haastregtb21a3652019-08-19 11:56:03 +000068// OpenCL C basic data types (int, float, image2d_t, ...).
69// Its Child classes can represent concrete types (e.g.: VectorType) or
70// custom types (e.g.: GenType).
71// Instances of these child classes should be used in Builtin function
72// arguments. See the definition of the "read_imagef" function as example.
Sven van Haastregt79a222f2019-06-03 09:39:11 +000073class Type<string _Name, QualType _QTName> {
Sven van Haastregtb21a3652019-08-19 11:56:03 +000074 // Name of the Type.
Sven van Haastregt79a222f2019-06-03 09:39:11 +000075 string Name = _Name;
Sven van Haastregtb21a3652019-08-19 11:56:03 +000076 // QualType associated with this type.
Sven van Haastregt79a222f2019-06-03 09:39:11 +000077 QualType QTName = _QTName;
Sven van Haastregtb21a3652019-08-19 11:56:03 +000078 // Size of the vector (if applicable).
79 int VecWidth = 1;
80 // Is a pointer.
Sven van Haastregt79a222f2019-06-03 09:39:11 +000081 bit IsPointer = 0;
82 // List of qualifiers associated with the type (volatile, ...)
83 list<Qualifier> QualList = [];
Sven van Haastregt79a222f2019-06-03 09:39:11 +000084 // Access qualifier. Must be one of ("RO", "WO", "RW").
85 string AccessQualifier = "";
Sven van Haastregtb21a3652019-08-19 11:56:03 +000086 // Address space.
87 string AddrSpace = "clang::LangAS::Default";
Sven van Haastregt79a222f2019-06-03 09:39:11 +000088}
89
90// OpenCL vector types (e.g. int2, int3, int16, float8, ...)
91class VectorType<Type _Ty, int _VecWidth> : Type<_Ty.Name, _Ty.QTName> {
92 int VecWidth = _VecWidth;
93}
94
Sven van Haastregtb21a3652019-08-19 11:56:03 +000095// OpenCL pointer types (e.g. int*, float*, ...).
Sven van Haastregt89fb9e82019-07-29 14:55:29 +000096class PointerType<Type _Ty, AddressSpace _AS = GlobalAS> :
Sven van Haastregt79a222f2019-06-03 09:39:11 +000097 Type<_Ty.Name, _Ty.QTName> {
98 bit IsPointer = 1;
Sven van Haastregt89fb9e82019-07-29 14:55:29 +000099 string AddrSpace = _AS.Name;
Sven van Haastregt79a222f2019-06-03 09:39:11 +0000100}
101
102// OpenCL image types (e.g. image2d_t, ...)
103class ImageType<Type _Ty, QualType _QTName, string _AccessQualifier> :
104 Type<_Ty.Name, _QTName> {
105 let AccessQualifier = _AccessQualifier;
106}
107
Sven van Haastregtb21a3652019-08-19 11:56:03 +0000108// List of Types.
109class TypeList<string _Name, list<Type> _Type> {
110 string Name = _Name;
111 list<Type> List = _Type;
112}
113
114// A GenericType is an abstract type that defines a set of types as a
115// combination of Types and vector sizes.
116//
117// E.g.: If TypeList = <int, float> and VectorList = <1, 2, 4>, then it
118// represents <int, int2, int4, float, float2, float4>.
119// _Ty : Name of the GenType.
120// _TypeList : List of basic data Types.
121// _VectorList : Sizes of the vector for each type of the _TypeList, 1 being a
122// scalar.
123//
124// Some rules apply when using multiple GenericType arguments in a declaration:
125// 1. The number of vector sizes must be equal or 1 for all gentypes in a
126// declaration.
127// 2. The number of Types must be equal or 1 for all gentypes in a
128// declaration.
129// 3. Generic types are combined by iterating over all generic types at once.
130// For example, for the following GenericTypes
131// GenT1 = GenericType<half, [1, 2]> and
132// GenT2 = GenericType<float, int, [1, 2]>
133// A declaration f(GenT1, GenT2) results in the combinations
134// f(half, float), f(half2, float2), f(half, int), f(half2, int2) .
135// 4. "sgentype" from the OpenCL specification is supported by specifying
136// a single vector size.
137// For example, for the following GenericTypes
138// GenT = GenericType<half, int, [1, 2]> and
139// SGenT = GenericType<half, int, [1]>
140// A declaration f(GenT, SGenT) results in the combinations
141// f(half, half), f(half2, half), f(int, int), f(int2, int) .
142class GenericType<string _Ty, TypeList _TypeList, IntList _VectorList> :
143 Type<_Ty, QualType<"null", 1>> {
144 // Possible element types of the generic type.
145 TypeList TypeList = _TypeList;
146 // Possible vector sizes of the types in the TypeList.
147 IntList VectorList = _VectorList;
148 // The VecWidth field is ignored for GenericTypes. Use VectorList instead.
149 let VecWidth = 0;
150}
151
Sven van Haastregt79a222f2019-06-03 09:39:11 +0000152//===----------------------------------------------------------------------===//
153// OpenCL C class for builtin functions
154//===----------------------------------------------------------------------===//
155class Builtin<string _Name, list<Type> _Signature> {
156 // Name of the builtin function
157 string Name = _Name;
158 // List of types used by the function. The first one is the return type and
159 // the following are the arguments. The list must have at least one element
160 // (the return type).
161 list<Type> Signature = _Signature;
162 // OpenCL Extension to which the function belongs (cl_khr_subgroups, ...)
163 string Extension = "";
164 // OpenCL Version to which the function belongs (CL10, ...)
165 Version Version = CL10;
166}
167
168//===----------------------------------------------------------------------===//
Sven van Haastregt79a222f2019-06-03 09:39:11 +0000169// Definitions of OpenCL C types
170//===----------------------------------------------------------------------===//
Sven van Haastregtb21a3652019-08-19 11:56:03 +0000171
172// OpenCL v1.0/1.2/2.0 s6.1.1: Built-in Scalar Data Types.
Sven van Haastregt89fb9e82019-07-29 14:55:29 +0000173def Bool : Type<"bool", QualType<"BoolTy">>;
174def Char : Type<"char", QualType<"CharTy">>;
175def UChar : Type<"uchar", QualType<"UnsignedCharTy">>;
176def Short : Type<"short", QualType<"ShortTy">>;
177def UShort : Type<"ushort", QualType<"UnsignedShortTy">>;
178def Int : Type<"int", QualType<"IntTy">>;
179def UInt : Type<"uint", QualType<"UnsignedIntTy">>;
180def Long : Type<"long", QualType<"LongTy">>;
181def ULong : Type<"ulong", QualType<"UnsignedLongTy">>;
182def Float : Type<"float", QualType<"FloatTy">>;
183def Double : Type<"double", QualType<"DoubleTy">>;
184def Half : Type<"half", QualType<"HalfTy">>;
185def Size : Type<"size_t", QualType<"getSizeType()">>;
186def PtrDiff : Type<"ptrdiff_t", QualType<"getPointerDiffType()">>;
187def IntPtr : Type<"intptr_t", QualType<"getIntPtrType()">>;
188def UIntPtr : Type<"uintPtr_t", QualType<"getUIntPtrType()">>;
189def Void : Type<"void_t", QualType<"VoidTy">>;
Sven van Haastregt79a222f2019-06-03 09:39:11 +0000190
Sven van Haastregtb21a3652019-08-19 11:56:03 +0000191// OpenCL v1.0/1.2/2.0 s6.1.2: Built-in Vector Data Types.
192// Built-in vector data types are created by TableGen's OpenCLBuiltinEmitter.
Sven van Haastregt79a222f2019-06-03 09:39:11 +0000193
194// OpenCL v1.2 s6.1.3: Other Built-in Data Types
195// These definitions with a "null" name are "abstract". They should not
196// be used in definitions of Builtin functions.
Sven van Haastregtb21a3652019-08-19 11:56:03 +0000197def image2d_t : Type<"image2d_t", QualType<"null", 1>>;
198def image3d_t : Type<"image3d_t", QualType<"null", 1>>;
199def image2d_array_t : Type<"image2d_array_t", QualType<"null", 1>>;
200def image1d_t : Type<"image1d_t", QualType<"null", 1>>;
201def image1d_buffer_t : Type<"image1d_buffer_t", QualType<"null", 1>>;
202def image1d_array_t : Type<"image1d_array_t", QualType<"null", 1>>;
Sven van Haastregt79a222f2019-06-03 09:39:11 +0000203// Unlike the few functions above, the following definitions can be used
204// in definitions of Builtin functions (they have a QualType with a name).
205foreach v = ["RO", "WO", "RW"] in {
206 def image2d_#v#_t : ImageType<image2d_t,
207 QualType<"OCLImage2d"#v#"Ty">,
208 v>;
209 def image3d_#v#_t : ImageType<image3d_t,
210 QualType<"OCLImage3d"#v#"Ty">,
211 v>;
212 def image2d_array#v#_t : ImageType<image2d_array_t,
213 QualType<"OCLImage2dArray"#v#"Ty">,
214 v>;
215 def image1d_#v#_t : ImageType<image1d_t,
216 QualType<"OCLImage1d"#v#"Ty">,
217 v>;
218 def image1d_buffer#v#_t : ImageType<image1d_buffer_t,
219 QualType<"OCLImage1dBuffer"#v#"Ty">,
220 v>;
221 def image1d_array#v#_t : ImageType<image1d_array_t,
222 QualType<"OCLImage1dArray"#v#"Ty">,
223 v>;
224}
225
Sven van Haastregt89fb9e82019-07-29 14:55:29 +0000226def Sampler : Type<"Sampler", QualType<"OCLSamplerTy">>;
227def Event : Type<"Event", QualType<"OCLEventTy">>;
Sven van Haastregt79a222f2019-06-03 09:39:11 +0000228
229//===----------------------------------------------------------------------===//
Sven van Haastregtb21a3652019-08-19 11:56:03 +0000230// Definitions of OpenCL gentype variants
231//===----------------------------------------------------------------------===//
232// The OpenCL specification often uses "gentype" in builtin function
233// declarations to indicate that a builtin function is available with various
234// argument and return types. The types represented by "gentype" vary between
235// different parts of the specification. The following definitions capture
236// the different type lists for gentypes in different parts of the
237// specification.
238
239// Vector width lists.
240def VecAndScalar: IntList<"VecAndScalar", [1, 2, 3, 4, 8, 16]>;
241def VecNoScalar : IntList<"VecNoScalar", [2, 3, 4, 8, 16]>;
242def Vec1 : IntList<"Vec1", [1]>;
243
244// Type lists.
245def TLFloat : TypeList<"TLFloat", [Float, Double, Half]>;
246
247def TLAllInts : TypeList<"TLAllInts", [Char, UChar, Short, UShort, Int, UInt, Long, ULong]>;
248
249// GenType definitions for multiple base types (e.g. all floating point types,
250// or all integer types).
251// All integer
252def AIGenType1 : GenericType<"AIGenType1", TLAllInts, Vec1>;
253def AIGenTypeN : GenericType<"AIGenTypeN", TLAllInts, VecAndScalar>;
254def AIGenTypeNNoScalar : GenericType<"AIGenTypeNNoScalar", TLAllInts, VecNoScalar>;
255// Float
256def FGenTypeN : GenericType<"FGenTypeN", TLFloat, VecAndScalar>;
257
258// GenType definitions for every single base type (e.g. fp32 only).
259// Names are like: GenTypeFloatVecAndScalar.
260foreach Type = [Char, UChar, Short, UShort,
261 Int, UInt, Long, ULong,
262 Float, Double, Half] in {
263 foreach VecSizes = [VecAndScalar, VecNoScalar] in {
264 def "GenType" # Type # VecSizes :
265 GenericType<"GenType" # Type # VecSizes,
266 TypeList<"GL" # Type.Name, [Type]>,
267 VecSizes>;
268 }
269}
270
271
272//===----------------------------------------------------------------------===//
Sven van Haastregt79a222f2019-06-03 09:39:11 +0000273// Definitions of OpenCL builtin functions
274//===----------------------------------------------------------------------===//
Sven van Haastregt89fb9e82019-07-29 14:55:29 +0000275//--------------------------------------------------------------------
276// OpenCL v1.1/1.2/2.0 s6.2.3 - Explicit conversions.
277// OpenCL v2.0 Extensions s5.1.1 and s6.1.1 - Conversions.
278
279// Generate the convert_* builtins functions.
280foreach RType = [Float, Double, Half, Char, UChar, Short,
281 UShort, Int, UInt, Long, ULong] in {
282 foreach IType = [Float, Double, Half, Char, UChar, Short,
283 UShort, Int, UInt, Long, ULong] in {
Sven van Haastregt79a222f2019-06-03 09:39:11 +0000284 foreach sat = ["", "_sat"] in {
Sven van Haastregt89fb9e82019-07-29 14:55:29 +0000285 foreach rnd = ["", "_rte", "_rtn", "_rtp", "_rtz"] in {
286 def : Builtin<"convert_" # RType.Name # sat # rnd, [RType, IType]>;
Sven van Haastregt79a222f2019-06-03 09:39:11 +0000287 foreach v = [2, 3, 4, 8, 16] in {
Sven van Haastregt89fb9e82019-07-29 14:55:29 +0000288 def : Builtin<"convert_" # RType.Name # v # sat # rnd,
Sven van Haastregt79a222f2019-06-03 09:39:11 +0000289 [VectorType<RType, v>,
290 VectorType<IType, v>]>;
291 }
292 }
293 }
294 }
295}
296
297// OpenCL v1.2 s6.12.1: Work-Item Functions
Sven van Haastregt89fb9e82019-07-29 14:55:29 +0000298def get_work_dim : Builtin<"get_work_dim", [UInt]>;
Sven van Haastregt79a222f2019-06-03 09:39:11 +0000299foreach name = ["get_global_size", "get_global_id", "get_local_size",
300 "get_local_id", "get_num_groups", "get_group_id",
301 "get_global_offset"] in {
Sven van Haastregt89fb9e82019-07-29 14:55:29 +0000302 def : Builtin<name, [Size, UInt]>;
Sven van Haastregt79a222f2019-06-03 09:39:11 +0000303}
304
305// OpenCL v1.2 s6.12.2: Math Functions
306foreach name = ["acos", "acosh", "acospi",
307 "asin", "asinh", "asinpi",
308 "atan", "atanh", "atanpi"] in {
Sven van Haastregtb21a3652019-08-19 11:56:03 +0000309 def : Builtin<name, [FGenTypeN, FGenTypeN]>;
Sven van Haastregt79a222f2019-06-03 09:39:11 +0000310}
311
312foreach name = ["atan2", "atan2pi"] in {
Sven van Haastregtb21a3652019-08-19 11:56:03 +0000313 def : Builtin<name, [FGenTypeN, FGenTypeN, FGenTypeN]>;
Sven van Haastregt79a222f2019-06-03 09:39:11 +0000314}
315
316foreach name = ["fmax", "fmin"] in {
Sven van Haastregtb21a3652019-08-19 11:56:03 +0000317 def : Builtin<name, [FGenTypeN, FGenTypeN, FGenTypeN]>;
318 def : Builtin<name, [GenTypeFloatVecNoScalar, GenTypeFloatVecNoScalar, Float]>;
319 def : Builtin<name, [GenTypeDoubleVecNoScalar, GenTypeDoubleVecNoScalar, Double]>;
320 def : Builtin<name, [GenTypeHalfVecNoScalar, GenTypeHalfVecNoScalar, Half]>;
321}
322
323// OpenCL v1.1 s6.11.3, v1.2 s6.12.3, v2.0 s6.13.3 - Integer Functions
324foreach name = ["max", "min"] in {
325 def : Builtin<name, [AIGenTypeN, AIGenTypeN, AIGenTypeN]>;
326 def : Builtin<name, [AIGenTypeNNoScalar, AIGenTypeNNoScalar, AIGenType1]>;
Sven van Haastregt79a222f2019-06-03 09:39:11 +0000327}
328
329// OpenCL v1.2 s6.12.14: Built-in Image Read Functions
330def read_imagef : Builtin<"read_imagef",
Sven van Haastregtb21a3652019-08-19 11:56:03 +0000331 [VectorType<Float, 4>, image2d_RO_t, VectorType<Int, 2>]>;
Sven van Haastregt79a222f2019-06-03 09:39:11 +0000332def write_imagef : Builtin<"write_imagef",
Sven van Haastregt89fb9e82019-07-29 14:55:29 +0000333 [Void,
Sven van Haastregt79a222f2019-06-03 09:39:11 +0000334 image2d_WO_t,
Sven van Haastregt89fb9e82019-07-29 14:55:29 +0000335 VectorType<Int, 2>,
336 VectorType<Float, 4>]>;
Sven van Haastregt79a222f2019-06-03 09:39:11 +0000337
338
339// OpenCL v2.0 s9.17.3: Additions to section 6.13.1: Work-Item Functions
340let Version = CL20 in {
341 let Extension = "cl_khr_subgroups" in {
Sven van Haastregt89fb9e82019-07-29 14:55:29 +0000342 def get_sub_group_size : Builtin<"get_sub_group_size", [UInt]>;
343 def get_max_sub_group_size : Builtin<"get_max_sub_group_size", [UInt]>;
344 def get_num_sub_groups : Builtin<"get_num_sub_groups", [UInt]>;
Sven van Haastregt79a222f2019-06-03 09:39:11 +0000345 }
346}