]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm-project/clang/lib/Sema/OpenCLBuiltins.td
Merge llvm, clang, compiler-rt, libc++, libunwind, lld, lldb and openmp
[FreeBSD/FreeBSD.git] / contrib / llvm-project / clang / lib / Sema / OpenCLBuiltins.td
1 //==--- 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
22 class Version<int _Version> {
23   int ID = _Version;
24 }
25 def CLAll : Version<  0>;
26 def CL10  : Version<100>;
27 def CL11  : Version<110>;
28 def CL12  : Version<120>;
29 def CL20  : Version<200>;
30
31 // Address spaces
32 // Pointer types need to be assigned an address space.
33 class AddressSpace<string _AS> {
34   string Name = _AS;
35 }
36 def DefaultAS    : AddressSpace<"clang::LangAS::Default">;
37 def PrivateAS    : AddressSpace<"clang::LangAS::opencl_private">;
38 def GlobalAS     : AddressSpace<"clang::LangAS::opencl_global">;
39 def ConstantAS   : AddressSpace<"clang::LangAS::opencl_constant">;
40 def LocalAS      : AddressSpace<"clang::LangAS::opencl_local">;
41 def GenericAS    : AddressSpace<"clang::LangAS::opencl_generic">;
42
43 // OpenCL language extension.
44 class AbstractExtension<string _Ext> {
45   // One or more OpenCL extensions, space separated.  Each extension must be
46   // a valid extension name for the opencl extension pragma.
47   string ExtName = _Ext;
48 }
49
50 // Extension associated to a builtin function.
51 class FunctionExtension<string _Ext> : AbstractExtension<_Ext>;
52
53 // FunctionExtension definitions.
54 def FuncExtNone                          : FunctionExtension<"">;
55 def FuncExtKhrSubgroups                  : FunctionExtension<"cl_khr_subgroups">;
56 def FuncExtKhrGlobalInt32BaseAtomics     : FunctionExtension<"cl_khr_global_int32_base_atomics">;
57 def FuncExtKhrGlobalInt32ExtendedAtomics : FunctionExtension<"cl_khr_global_int32_extended_atomics">;
58 def FuncExtKhrLocalInt32BaseAtomics      : FunctionExtension<"cl_khr_local_int32_base_atomics">;
59 def FuncExtKhrLocalInt32ExtendedAtomics  : FunctionExtension<"cl_khr_local_int32_extended_atomics">;
60 def FuncExtKhrInt64BaseAtomics           : FunctionExtension<"cl_khr_int64_base_atomics">;
61 def FuncExtKhrInt64ExtendedAtomics       : FunctionExtension<"cl_khr_int64_extended_atomics">;
62 def FuncExtKhrMipmapImage                : FunctionExtension<"cl_khr_mipmap_image">;
63 def FuncExtKhrGlMsaaSharing              : FunctionExtension<"cl_khr_gl_msaa_sharing">;
64
65 // Multiple extensions
66 def FuncExtKhrMipmapAndWrite3d           : FunctionExtension<"cl_khr_mipmap_image cl_khr_3d_image_writes">;
67
68 // Qualified Type.  These map to ASTContext::QualType.
69 class QualType<string _Name, bit _IsAbstract=0> {
70   // Name of the field or function in a clang::ASTContext
71   // E.g. Name="IntTy" for the int type, and "getIntPtrType()" for an intptr_t
72   string Name = _Name;
73   // Some QualTypes in this file represent an abstract type for which there is
74   // no corresponding AST QualType, e.g. a GenType or an `image2d_t` type
75   // without access qualifiers.
76   bit IsAbstract = _IsAbstract;
77 }
78
79 // List of integers.
80 class IntList<string _Name, list<int> _List> {
81   string Name = _Name;
82   list<int> List = _List;
83 }
84
85 //===----------------------------------------------------------------------===//
86 //                      OpenCL C classes for types
87 //===----------------------------------------------------------------------===//
88 // OpenCL C basic data types (int, float, image2d_t, ...).
89 // Its child classes can represent concrete types (e.g. VectorType) or
90 // abstract types (e.g. GenType).
91 class Type<string _Name, QualType _QTName> {
92   // Name of the Type.
93   string Name = _Name;
94   // QualType associated with this type.
95   QualType QTName = _QTName;
96   // Size of the vector (if applicable).
97   int VecWidth = 1;
98   // Is a pointer.
99   bit IsPointer = 0;
100   // "const" qualifier.
101   bit IsConst = 0;
102   // "volatile" qualifier.
103   bit IsVolatile = 0;
104   // Access qualifier. Must be one of ("RO", "WO", "RW").
105   string AccessQualifier = "";
106   // Address space.
107   string AddrSpace = DefaultAS.Name;
108 }
109
110 // OpenCL vector types (e.g. int2, int3, int16, float8, ...).
111 class VectorType<Type _Ty, int _VecWidth> : Type<_Ty.Name, _Ty.QTName> {
112   let VecWidth = _VecWidth;
113   let AccessQualifier = "";
114   // Inherited fields
115   let IsPointer = _Ty.IsPointer;
116   let IsConst = _Ty.IsConst;
117   let IsVolatile = _Ty.IsVolatile;
118   let AddrSpace = _Ty.AddrSpace;
119 }
120
121 // OpenCL pointer types (e.g. int*, float*, ...).
122 class PointerType<Type _Ty, AddressSpace _AS = DefaultAS> :
123                                       Type<_Ty.Name, _Ty.QTName> {
124   let AddrSpace = _AS.Name;
125   // Inherited fields
126   let VecWidth = _Ty.VecWidth;
127   let IsPointer = 1;
128   let IsConst = _Ty.IsConst;
129   let IsVolatile = _Ty.IsVolatile;
130   let AccessQualifier = _Ty.AccessQualifier;
131 }
132
133 // OpenCL const types (e.g. const int).
134 class ConstType<Type _Ty> : Type<_Ty.Name, _Ty.QTName> {
135   let IsConst = 1;
136   // Inherited fields
137   let VecWidth = _Ty.VecWidth;
138   let IsPointer = _Ty.IsPointer;
139   let IsVolatile = _Ty.IsVolatile;
140   let AccessQualifier = _Ty.AccessQualifier;
141   let AddrSpace = _Ty.AddrSpace;
142 }
143
144 // OpenCL volatile types (e.g. volatile int).
145 class VolatileType<Type _Ty> : Type<_Ty.Name, _Ty.QTName> {
146   let IsVolatile = 1;
147   // Inherited fields
148   let VecWidth = _Ty.VecWidth;
149   let IsPointer = _Ty.IsPointer;
150   let IsConst = _Ty.IsConst;
151   let AccessQualifier = _Ty.AccessQualifier;
152   let AddrSpace = _Ty.AddrSpace;
153 }
154
155 // OpenCL image types (e.g. image2d).
156 class ImageType<Type _Ty, string _AccessQualifier> :
157           Type<_Ty.Name, QualType<_Ty.QTName.Name#_AccessQualifier#"Ty", 0>> {
158   let VecWidth = 0;
159   let AccessQualifier = _AccessQualifier;
160   // Inherited fields
161   let IsPointer = _Ty.IsPointer;
162   let IsConst = _Ty.IsConst;
163   let IsVolatile = _Ty.IsVolatile;
164   let AddrSpace = _Ty.AddrSpace;
165 }
166
167 // List of Types.
168 class TypeList<string _Name, list<Type> _Type> {
169   string Name = _Name;
170   list<Type> List = _Type;
171 }
172
173 // A GenericType is an abstract type that defines a set of types as a
174 // combination of Types and vector sizes.
175 //
176 // For example, if TypeList = <int, float> and VectorList = <1, 2, 4>, then it
177 // represents <int, int2, int4, float, float2, float4>.
178 //
179 // Some rules apply when using multiple GenericType arguments in a declaration:
180 //   1. The number of vector sizes must be equal or 1 for all gentypes in a
181 //      declaration.
182 //   2. The number of Types must be equal or 1 for all gentypes in a
183 //      declaration.
184 //   3. Generic types are combined by iterating over all generic types at once.
185 //      For example, for the following GenericTypes
186 //        GenT1 = GenericType<half, [1, 2]> and
187 //        GenT2 = GenericType<float, int, [1, 2]>
188 //      A declaration f(GenT1, GenT2) results in the combinations
189 //        f(half, float), f(half2, float2), f(half, int), f(half2, int2) .
190 //   4. "sgentype" from the OpenCL specification is supported by specifying
191 //      a single vector size.
192 //      For example, for the following GenericTypes
193 //        GenT = GenericType<half, int, [1, 2]> and
194 //        SGenT = GenericType<half, int, [1]>
195 //      A declaration f(GenT, SGenT) results in the combinations
196 //        f(half, half), f(half2, half), f(int, int), f(int2, int) .
197 class GenericType<string _Ty, TypeList _TypeList, IntList _VectorList> :
198                                             Type<_Ty, QualType<"null", 1>> {
199   // Possible element types of the generic type.
200   TypeList TypeList = _TypeList;
201   // Possible vector sizes of the types in the TypeList.
202   IntList VectorList = _VectorList;
203   // The VecWidth field is ignored for GenericTypes. Use VectorList instead.
204   let VecWidth = 0;
205 }
206
207 // Builtin function attributes.
208 def Attr {
209   list<bit> None = [0, 0, 0];
210   list<bit> Pure = [1, 0, 0];
211   list<bit> Const = [0, 1, 0];
212   list<bit> Convergent = [0, 0, 1];
213 }
214
215 //===----------------------------------------------------------------------===//
216 //                      OpenCL C class for builtin functions
217 //===----------------------------------------------------------------------===//
218 class Builtin<string _Name, list<Type> _Signature, list<bit> _Attributes = Attr.None> {
219   // Name of the builtin function
220   string Name = _Name;
221   // List of types used by the function. The first one is the return type and
222   // the following are the arguments. The list must have at least one element
223   // (the return type).
224   list<Type> Signature = _Signature;
225   // Function attribute __attribute__((pure))
226   bit IsPure = _Attributes[0];
227   // Function attribute __attribute__((const))
228   bit IsConst = _Attributes[1];
229   // Function attribute __attribute__((convergent))
230   bit IsConv = _Attributes[2];
231   // OpenCL extensions to which the function belongs.
232   FunctionExtension Extension = FuncExtNone;
233   // Version of OpenCL from which the function is available (e.g.: CL10).
234   // MinVersion is inclusive.
235   Version MinVersion = CL10;
236   // Version of OpenCL from which the function is not supported anymore.
237   // MaxVersion is exclusive.
238   // CLAll makes the function available for all versions.
239   Version MaxVersion = CLAll;
240 }
241
242 //===----------------------------------------------------------------------===//
243 //                 Definitions of OpenCL C types
244 //===----------------------------------------------------------------------===//
245
246 // OpenCL v1.0/1.2/2.0 s6.1.1: Built-in Scalar Data Types.
247 def Bool      : Type<"bool",      QualType<"BoolTy">>;
248 def Char      : Type<"char",      QualType<"CharTy">>;
249 def UChar     : Type<"uchar",     QualType<"UnsignedCharTy">>;
250 def Short     : Type<"short",     QualType<"ShortTy">>;
251 def UShort    : Type<"ushort",    QualType<"UnsignedShortTy">>;
252 def Int       : Type<"int",       QualType<"IntTy">>;
253 def UInt      : Type<"uint",      QualType<"UnsignedIntTy">>;
254 def Long      : Type<"long",      QualType<"LongTy">>;
255 def ULong     : Type<"ulong",     QualType<"UnsignedLongTy">>;
256 def Float     : Type<"float",     QualType<"FloatTy">>;
257 def Double    : Type<"double",    QualType<"DoubleTy">>;
258 def Half      : Type<"half",      QualType<"HalfTy">>;
259 def Size      : Type<"size_t",    QualType<"getSizeType()">>;
260 def PtrDiff   : Type<"ptrdiff_t", QualType<"getPointerDiffType()">>;
261 def IntPtr    : Type<"intptr_t",  QualType<"getIntPtrType()">>;
262 def UIntPtr   : Type<"uintPtr_t", QualType<"getUIntPtrType()">>;
263 def Void      : Type<"void_t",    QualType<"VoidTy">>;
264
265 // OpenCL v1.0/1.2/2.0 s6.1.2: Built-in Vector Data Types.
266 // Built-in vector data types are created by TableGen's OpenCLBuiltinEmitter.
267
268 // OpenCL v1.0/1.2/2.0 s6.1.3: Other Built-in Data Types.
269 // The image definitions are "abstract".  They should not be used without
270 // specifying an access qualifier (RO/WO/RW).
271 def Image1d               : Type<"Image1d", QualType<"OCLImage1d", 1>>;
272 def Image2d               : Type<"Image2d", QualType<"OCLImage2d", 1>>;
273 def Image3d               : Type<"Image3d", QualType<"OCLImage3d", 1>>;
274 def Image1dArray          : Type<"Image1dArray", QualType<"OCLImage1dArray", 1>>;
275 def Image1dBuffer         : Type<"Image1dBuffer", QualType<"OCLImage1dBuffer", 1>>;
276 def Image2dArray          : Type<"Image2dArray", QualType<"OCLImage2dArray", 1>>;
277 def Image2dDepth          : Type<"Image2dDepth", QualType<"OCLImage2dDepth", 1>>;
278 def Image2dArrayDepth     : Type<"Image2dArrayDepth", QualType<"OCLImage2dArrayDepth", 1>>;
279 def Image2dMsaa           : Type<"Image2dMsaa", QualType<"OCLImage2dMSAA", 1>>;
280 def Image2dArrayMsaa      : Type<"Image2dArrayMsaa", QualType<"OCLImage2dArrayMSAA", 1>>;
281 def Image2dMsaaDepth      : Type<"Image2dMsaaDepth", QualType<"OCLImage2dMSAADepth", 1>>;
282 def Image2dArrayMsaaDepth : Type<"Image2dArrayMsaaDepth", QualType<"OCLImage2dArrayMSAADepth", 1>>;
283
284 def Sampler           : Type<"Sampler", QualType<"OCLSamplerTy">>;
285 def Event             : Type<"Event", QualType<"OCLEventTy">>;
286
287 //===----------------------------------------------------------------------===//
288 //                 Definitions of OpenCL gentype variants
289 //===----------------------------------------------------------------------===//
290 // The OpenCL specification often uses "gentype" in builtin function
291 // declarations to indicate that a builtin function is available with various
292 // argument and return types.  The types represented by "gentype" vary between
293 // different parts of the specification.  The following definitions capture
294 // the different type lists for gentypes in different parts of the
295 // specification.
296
297 // Vector width lists.
298 def VecAndScalar: IntList<"VecAndScalar", [1, 2, 3, 4, 8, 16]>;
299 def VecNoScalar : IntList<"VecNoScalar", [2, 3, 4, 8, 16]>;
300 def Vec1        : IntList<"Vec1", [1]>;
301 def Vec2        : IntList<"Vec2", [2]>;
302 def Vec4        : IntList<"Vec4", [4]>;
303 def Vec8        : IntList<"Vec8", [8]>;
304 def Vec16       : IntList<"Vec16", [16]>;
305 def Vec1234     : IntList<"Vec1234", [1, 2, 3, 4]>;
306
307 // Type lists.
308 def TLAll         : TypeList<"TLAll", [Char,  UChar, Short,  UShort, Int,  UInt, Long,  ULong, Float, Double, Half]>;
309 def TLAllUnsigned : TypeList<"TLAllUnsigned", [UChar, UChar, UShort, UShort, UInt, UInt, ULong, ULong, UInt,  ULong,  UShort]>;
310 def TLFloat : TypeList<"TLFloat", [Float, Double, Half]>;
311 def TLSignedInts   : TypeList<"TLSignedInts", [Char, Short, Int, Long]>;
312 def TLUnsignedInts : TypeList<"TLUnsignedInts", [UChar, UShort, UInt, ULong]>;
313
314 def TLIntLongFloats : TypeList<"TLIntLongFloats", [Int, UInt, Long, ULong, Float, Double, Half]>;
315
316 // All unsigned integer types twice, to facilitate unsigned return types for e.g.
317 // uchar abs(char) and
318 // uchar abs(uchar).
319 def TLAllUIntsTwice : TypeList<"TLAllUIntsTwice", [UChar, UChar, UShort, UShort, UInt, UInt, ULong, ULong]>;
320
321 def TLAllInts : TypeList<"TLAllInts", [Char, UChar, Short, UShort, Int, UInt, Long, ULong]>;
322
323 // GenType definitions for multiple base types (e.g. all floating point types,
324 // or all integer types).
325 // All types
326 def AGenTypeN              : GenericType<"AGenTypeN", TLAll, VecAndScalar>;
327 def AGenTypeNNoScalar      : GenericType<"AGenTypeNNoScalar", TLAll, VecNoScalar>;
328 // All integer
329 def AIGenType1             : GenericType<"AIGenType1", TLAllInts, Vec1>;
330 def AIGenTypeN             : GenericType<"AIGenTypeN", TLAllInts, VecAndScalar>;
331 def AIGenTypeNNoScalar     : GenericType<"AIGenTypeNNoScalar", TLAllInts, VecNoScalar>;
332 // All integer to unsigned
333 def AI2UGenTypeN           : GenericType<"AI2UGenTypeN", TLAllUIntsTwice, VecAndScalar>;
334 // Signed integer
335 def SGenTypeN              : GenericType<"SGenTypeN", TLSignedInts, VecAndScalar>;
336 // Unsigned integer
337 def UGenTypeN              : GenericType<"UGenTypeN", TLUnsignedInts, VecAndScalar>;
338 // Float
339 def FGenTypeN              : GenericType<"FGenTypeN", TLFloat, VecAndScalar>;
340 // (u)int, (u)long, and all floats
341 def IntLongFloatGenType1   : GenericType<"IntLongFloatGenType1", TLIntLongFloats, Vec1>;
342
343 // GenType definitions for every single base type (e.g. fp32 only).
344 // Names are like: GenTypeFloatVecAndScalar.
345 foreach Type = [Char, UChar, Short, UShort,
346                 Int, UInt, Long, ULong,
347                 Float, Double, Half] in {
348   foreach VecSizes = [VecAndScalar, VecNoScalar] in {
349     def "GenType" # Type # VecSizes :
350               GenericType<"GenType" # Type # VecSizes,
351                           TypeList<"GL" # Type.Name, [Type]>,
352                           VecSizes>;
353   }
354 }
355
356 // GenType definitions for vec1234.
357 foreach Type = [Float, Double, Half] in {
358   def "GenType" # Type # Vec1234 :
359               GenericType<"GenType" # Type # Vec1234,
360                           TypeList<"GL" # Type.Name, [Type]>,
361                           Vec1234>;
362 }
363
364
365 //===----------------------------------------------------------------------===//
366 //                 Definitions of OpenCL builtin functions
367 //===----------------------------------------------------------------------===//
368 //--------------------------------------------------------------------
369 // OpenCL v1.1/1.2/2.0 s6.2.3 - Explicit conversions.
370 // OpenCL v2.0 Extensions s5.1.1 and s6.1.1 - Conversions.
371
372 // Generate the convert_* builtins functions.
373 foreach RType = [Float, Double, Half, Char, UChar, Short,
374                  UShort, Int, UInt, Long, ULong] in {
375   foreach IType = [Float, Double, Half, Char, UChar, Short,
376                    UShort, Int, UInt, Long, ULong] in {
377     foreach sat = ["", "_sat"] in {
378       foreach rnd = ["", "_rte", "_rtn", "_rtp", "_rtz"] in {
379         def : Builtin<"convert_" # RType.Name # sat # rnd, [RType, IType],
380                       Attr.Const>;
381         foreach v = [2, 3, 4, 8, 16] in {
382           def : Builtin<"convert_" # RType.Name # v # sat # rnd,
383                         [VectorType<RType, v>, VectorType<IType, v>],
384                         Attr.Const>;
385         }
386       }
387     }
388   }
389 }
390
391 //--------------------------------------------------------------------
392 // OpenCL v1.1 s6.11.1, v1.2 s6.12.1, v2.0 s6.13.1 - Work-item Functions
393 // --- Table 7 ---
394 def : Builtin<"get_work_dim", [UInt], Attr.Const>;
395 foreach name = ["get_global_size", "get_global_id", "get_local_size",
396                 "get_local_id", "get_num_groups", "get_group_id",
397                 "get_global_offset"] in {
398   def : Builtin<name, [Size, UInt], Attr.Const>;
399 }
400
401 let MinVersion = CL20 in {
402   def : Builtin<"get_enqueued_local_size", [Size, UInt]>;
403   foreach name = ["get_global_linear_id", "get_local_linear_id"] in {
404     def : Builtin<name, [Size]>;
405   }
406 }
407
408
409 //--------------------------------------------------------------------
410 // OpenCL v1.1 s6.11.2, v1.2 s6.12.2, v2.0 s6.13.2 - Math functions
411 // OpenCL Extension v2.0 s5.1.2 and s6.1.2 - Math Functions
412 // --- Table 8 ---
413 // --- 1 argument ---
414 foreach name = ["acos", "acosh", "acospi",
415                 "asin", "asinh", "asinpi",
416                 "atan", "atanh", "atanpi",
417                 "cbrt", "ceil",
418                 "cos", "cosh", "cospi",
419                 "erfc", "erf",
420                 "exp", "exp2", "exp10", "expm1",
421                 "fabs", "floor",
422                 "log", "log2", "log10", "log1p", "logb",
423                 "rint", "round", "rsqrt",
424                 "sin", "sinh", "sinpi",
425                 "sqrt",
426                 "tan", "tanh", "tanpi",
427                 "tgamma", "trunc",
428                 "lgamma"] in {
429     def : Builtin<name, [FGenTypeN, FGenTypeN], Attr.Const>;
430 }
431 foreach name = ["nan"] in {
432   def : Builtin<name, [GenTypeFloatVecAndScalar, GenTypeUIntVecAndScalar], Attr.Const>;
433   def : Builtin<name, [GenTypeDoubleVecAndScalar, GenTypeULongVecAndScalar], Attr.Const>;
434   def : Builtin<name, [GenTypeHalfVecAndScalar, GenTypeUShortVecAndScalar], Attr.Const>;
435 }
436
437 // --- 2 arguments ---
438 foreach name = ["atan2", "atan2pi", "copysign", "fdim", "fmod", "hypot",
439                 "maxmag", "minmag", "nextafter", "pow", "powr",
440                 "remainder"] in {
441   def : Builtin<name, [FGenTypeN, FGenTypeN, FGenTypeN], Attr.Const>;
442 }
443 foreach name = ["fmax", "fmin"] in {
444   def : Builtin<name, [FGenTypeN, FGenTypeN, FGenTypeN], Attr.Const>;
445   def : Builtin<name, [GenTypeFloatVecNoScalar, GenTypeFloatVecNoScalar, Float], Attr.Const>;
446   def : Builtin<name, [GenTypeDoubleVecNoScalar, GenTypeDoubleVecNoScalar, Double], Attr.Const>;
447   def : Builtin<name, [GenTypeHalfVecNoScalar, GenTypeHalfVecNoScalar, Half], Attr.Const>;
448 }
449 foreach name = ["ilogb"] in {
450   def : Builtin<name, [GenTypeIntVecAndScalar, GenTypeFloatVecAndScalar], Attr.Const>;
451   def : Builtin<name, [GenTypeIntVecAndScalar, GenTypeDoubleVecAndScalar], Attr.Const>;
452   def : Builtin<name, [GenTypeIntVecAndScalar, GenTypeHalfVecAndScalar], Attr.Const>;
453 }
454 foreach name = ["ldexp"] in {
455   def : Builtin<name, [GenTypeFloatVecAndScalar, GenTypeFloatVecAndScalar, GenTypeIntVecAndScalar], Attr.Const>;
456   def : Builtin<name, [GenTypeFloatVecNoScalar, GenTypeFloatVecNoScalar, Int], Attr.Const>;
457   def : Builtin<name, [GenTypeDoubleVecAndScalar, GenTypeDoubleVecAndScalar, GenTypeIntVecAndScalar], Attr.Const>;
458   def : Builtin<name, [GenTypeDoubleVecNoScalar, GenTypeDoubleVecNoScalar, Int], Attr.Const>;
459   def : Builtin<name, [GenTypeHalfVecAndScalar, GenTypeHalfVecAndScalar, GenTypeIntVecAndScalar], Attr.Const>;
460   def : Builtin<name, [GenTypeHalfVecNoScalar, GenTypeHalfVecNoScalar, Int], Attr.Const>;
461 }
462 foreach name = ["pown", "rootn"] in {
463   def : Builtin<name, [GenTypeFloatVecAndScalar, GenTypeFloatVecAndScalar, GenTypeIntVecAndScalar], Attr.Const>;
464   def : Builtin<name, [GenTypeDoubleVecAndScalar, GenTypeDoubleVecAndScalar, GenTypeIntVecAndScalar], Attr.Const>;
465   def : Builtin<name, [GenTypeHalfVecAndScalar, GenTypeHalfVecAndScalar, GenTypeIntVecAndScalar], Attr.Const>;
466 }
467
468 // --- 3 arguments ---
469 foreach name = ["fma", "mad"] in {
470   def : Builtin<name, [FGenTypeN, FGenTypeN, FGenTypeN, FGenTypeN], Attr.Const>;
471 }
472
473 // --- Version dependent ---
474 let MaxVersion = CL20 in {
475   foreach AS = [GlobalAS, LocalAS, PrivateAS] in {
476     foreach name = ["fract", "modf", "sincos"] in {
477       def : Builtin<name, [FGenTypeN, FGenTypeN, PointerType<FGenTypeN, AS>]>;
478     }
479     foreach name = ["frexp", "lgamma_r"] in {
480       foreach Type = [GenTypeFloatVecAndScalar, GenTypeDoubleVecAndScalar, GenTypeHalfVecAndScalar] in {
481         def : Builtin<name, [Type, Type, PointerType<GenTypeIntVecAndScalar, AS>]>;
482       }
483     }
484     foreach name = ["remquo"] in {
485       foreach Type = [GenTypeFloatVecAndScalar, GenTypeDoubleVecAndScalar, GenTypeHalfVecAndScalar] in {
486         def : Builtin<name, [Type, Type, Type, PointerType<GenTypeIntVecAndScalar, AS>]>;
487       }
488     }
489   }
490 }
491 let MinVersion = CL20 in {
492   foreach name = ["fract", "modf", "sincos"] in {
493     def : Builtin<name, [FGenTypeN, FGenTypeN, PointerType<FGenTypeN, GenericAS>]>;
494   }
495   foreach name = ["frexp", "lgamma_r"] in {
496     foreach Type = [GenTypeFloatVecAndScalar, GenTypeDoubleVecAndScalar, GenTypeHalfVecAndScalar] in {
497       def : Builtin<name, [Type, Type, PointerType<GenTypeIntVecAndScalar, GenericAS>]>;
498     }  }
499   foreach name = ["remquo"] in {
500     foreach Type = [GenTypeFloatVecAndScalar, GenTypeDoubleVecAndScalar, GenTypeHalfVecAndScalar] in {
501       def : Builtin<name, [Type, Type, Type, PointerType<GenTypeIntVecAndScalar, GenericAS>]>;
502     }
503   }
504 }
505
506 // --- Table 9 ---
507 foreach name = ["half_cos",
508                 "half_exp", "half_exp2", "half_exp10",
509                 "half_log", "half_log2", "half_log10",
510                 "half_recip", "half_rsqrt",
511                 "half_sin", "half_sqrt", "half_tan",
512                 "native_cos",
513                 "native_exp", "native_exp2", "native_exp10",
514                 "native_log", "native_log2", "native_log10",
515                 "native_recip", "native_rsqrt",
516                 "native_sin", "native_sqrt", "native_tan"] in {
517   def : Builtin<name, [GenTypeFloatVecAndScalar, GenTypeFloatVecAndScalar], Attr.Const>;
518 }
519 foreach name = ["half_divide", "half_powr",
520                 "native_divide", "native_powr"] in {
521   def : Builtin<name, [GenTypeFloatVecAndScalar, GenTypeFloatVecAndScalar, GenTypeFloatVecAndScalar], Attr.Const>;
522 }
523
524 //--------------------------------------------------------------------
525 // OpenCL v1.1 s6.11.3, v1.2 s6.12.3, v2.0 s6.13.3 - Integer Functions
526 // --- Table 10 ---
527 // --- 1 argument ---
528 foreach name = ["abs"] in {
529   def : Builtin<name, [AI2UGenTypeN, AIGenTypeN], Attr.Const>;
530 }
531 foreach name = ["clz", "popcount"] in {
532   def : Builtin<name, [AIGenTypeN, AIGenTypeN], Attr.Const>;
533 }
534 let MinVersion = CL20 in {
535   foreach name = ["ctz"] in {
536     def : Builtin<name, [AIGenTypeN, AIGenTypeN]>;
537   }
538 }
539
540 // --- 2 arguments ---
541 foreach name = ["abs_diff"] in {
542   def : Builtin<name, [AI2UGenTypeN, AIGenTypeN, AIGenTypeN], Attr.Const>;
543 }
544 foreach name = ["add_sat", "hadd", "rhadd", "mul_hi", "rotate", "sub_sat"] in {
545   def : Builtin<name, [AIGenTypeN, AIGenTypeN, AIGenTypeN], Attr.Const>;
546 }
547 foreach name = ["max", "min"] in {
548   def : Builtin<name, [AIGenTypeN, AIGenTypeN, AIGenTypeN], Attr.Const>;
549   def : Builtin<name, [AIGenTypeNNoScalar, AIGenTypeNNoScalar, AIGenType1], Attr.Const>;
550 }
551 foreach name = ["upsample"] in {
552   def : Builtin<name, [GenTypeShortVecAndScalar, GenTypeCharVecAndScalar, GenTypeUCharVecAndScalar], Attr.Const>;
553   def : Builtin<name, [GenTypeUShortVecAndScalar, GenTypeUCharVecAndScalar, GenTypeUCharVecAndScalar], Attr.Const>;
554   def : Builtin<name, [GenTypeIntVecAndScalar, GenTypeShortVecAndScalar, GenTypeUShortVecAndScalar], Attr.Const>;
555   def : Builtin<name, [GenTypeUIntVecAndScalar, GenTypeUShortVecAndScalar, GenTypeUShortVecAndScalar], Attr.Const>;
556   def : Builtin<name, [GenTypeLongVecAndScalar, GenTypeIntVecAndScalar, GenTypeUIntVecAndScalar], Attr.Const>;
557   def : Builtin<name, [GenTypeULongVecAndScalar, GenTypeUIntVecAndScalar, GenTypeUIntVecAndScalar], Attr.Const>;
558 }
559
560 // --- 3 arguments ---
561 foreach name = ["clamp"] in {
562   def : Builtin<name, [AIGenTypeN, AIGenTypeN, AIGenTypeN, AIGenTypeN], Attr.Const>;
563   def : Builtin<name, [AIGenTypeNNoScalar, AIGenTypeNNoScalar, AIGenType1, AIGenType1], Attr.Const>;
564 }
565 foreach name = ["mad_hi", "mad_sat"] in {
566   def : Builtin<name, [AIGenTypeN, AIGenTypeN, AIGenTypeN, AIGenTypeN], Attr.Const>;
567 }
568
569 // --- Table 11 ---
570 foreach name = ["mad24"] in {
571   def : Builtin<name, [GenTypeIntVecAndScalar, GenTypeIntVecAndScalar, GenTypeIntVecAndScalar, GenTypeIntVecAndScalar], Attr.Const>;
572   def : Builtin<name, [GenTypeUIntVecAndScalar, GenTypeUIntVecAndScalar, GenTypeUIntVecAndScalar, GenTypeUIntVecAndScalar], Attr.Const>;
573 }
574 foreach name = ["mul24"] in {
575   def : Builtin<name, [GenTypeIntVecAndScalar, GenTypeIntVecAndScalar, GenTypeIntVecAndScalar], Attr.Const>;
576   def : Builtin<name, [GenTypeUIntVecAndScalar, GenTypeUIntVecAndScalar, GenTypeUIntVecAndScalar], Attr.Const>;
577 }
578
579 //--------------------------------------------------------------------
580 // OpenCL v1.1 s6.11.4, v1.2 s6.12.4, v2.0 s6.13.4 - Common Functions
581 // OpenCL Extension v2.0 s5.1.3 and s6.1.3 - Common Functions
582 // --- Table 12 ---
583 // --- 1 argument ---
584 foreach name = ["degrees", "radians", "sign"] in {
585   def : Builtin<name, [FGenTypeN, FGenTypeN], Attr.Const>;
586 }
587
588 // --- 2 arguments ---
589 foreach name = ["max", "min"] in {
590   def : Builtin<name, [FGenTypeN, FGenTypeN, FGenTypeN], Attr.Const>;
591   def : Builtin<name, [GenTypeFloatVecNoScalar, GenTypeFloatVecNoScalar, Float], Attr.Const>;
592   def : Builtin<name, [GenTypeDoubleVecNoScalar, GenTypeDoubleVecNoScalar, Double], Attr.Const>;
593   def : Builtin<name, [GenTypeHalfVecNoScalar, GenTypeHalfVecNoScalar, Half], Attr.Const>;
594 }
595 foreach name = ["step"] in {
596   def : Builtin<name, [FGenTypeN, FGenTypeN, FGenTypeN], Attr.Const>;
597   def : Builtin<name, [GenTypeFloatVecNoScalar, Float, GenTypeFloatVecNoScalar], Attr.Const>;
598   def : Builtin<name, [GenTypeDoubleVecNoScalar, Double, GenTypeDoubleVecNoScalar], Attr.Const>;
599   def : Builtin<name, [GenTypeHalfVecNoScalar, Half, GenTypeHalfVecNoScalar], Attr.Const>;
600 }
601
602 // --- 3 arguments ---
603 foreach name = ["clamp", "mix"] in {
604   def : Builtin<name, [FGenTypeN, FGenTypeN, FGenTypeN, FGenTypeN], Attr.Const>;
605   def : Builtin<name, [GenTypeFloatVecNoScalar, GenTypeFloatVecNoScalar, Float, Float], Attr.Const>;
606   def : Builtin<name, [GenTypeDoubleVecNoScalar, GenTypeDoubleVecNoScalar, Double, Double], Attr.Const>;
607   def : Builtin<name, [GenTypeHalfVecNoScalar, GenTypeHalfVecNoScalar, Half, Half], Attr.Const>;
608 }
609 foreach name = ["smoothstep"] in {
610   def : Builtin<name, [FGenTypeN, FGenTypeN, FGenTypeN, FGenTypeN], Attr.Const>;
611   def : Builtin<name, [GenTypeFloatVecNoScalar, Float, Float, GenTypeFloatVecNoScalar], Attr.Const>;
612   def : Builtin<name, [GenTypeDoubleVecNoScalar, Double, Double, GenTypeDoubleVecNoScalar], Attr.Const>;
613   def : Builtin<name, [GenTypeHalfVecNoScalar, Half, Half, GenTypeHalfVecNoScalar], Attr.Const>;
614 }
615
616
617 //--------------------------------------------------------------------
618 // OpenCL v1.1 s6.11.5, v1.2 s6.12.5, v2.0 s6.13.5 - Geometric Functions
619 // OpenCL Extension v2.0 s5.1.4 and s6.1.4 - Geometric Functions
620 // --- Table 13 ---
621 // --- 1 argument ---
622 foreach name = ["length"] in {
623   def : Builtin<name, [Float, GenTypeFloatVec1234], Attr.Const>;
624   def : Builtin<name, [Double, GenTypeDoubleVec1234], Attr.Const>;
625   def : Builtin<name, [Half, GenTypeHalfVec1234], Attr.Const>;
626 }
627 foreach name = ["normalize"] in {
628   def : Builtin<name, [GenTypeFloatVec1234, GenTypeFloatVec1234], Attr.Const>;
629   def : Builtin<name, [GenTypeDoubleVec1234, GenTypeDoubleVec1234], Attr.Const>;
630   def : Builtin<name, [GenTypeHalfVec1234, GenTypeHalfVec1234], Attr.Const>;
631 }
632 foreach name = ["fast_length"] in {
633   def : Builtin<name, [Float, GenTypeFloatVec1234], Attr.Const>;
634 }
635 foreach name = ["fast_normalize"] in {
636   def : Builtin<name, [GenTypeFloatVec1234, GenTypeFloatVec1234], Attr.Const>;
637 }
638
639 // --- 2 arguments ---
640 foreach name = ["cross"] in {
641   foreach VSize = [3, 4] in {
642     def : Builtin<name, [VectorType<Float, VSize>, VectorType<Float, VSize>, VectorType<Float, VSize>], Attr.Const>;
643     def : Builtin<name, [VectorType<Double, VSize>, VectorType<Double, VSize>, VectorType<Double, VSize>], Attr.Const>;
644     def : Builtin<name, [VectorType<Half, VSize>, VectorType<Half, VSize>, VectorType<Half, VSize>], Attr.Const>;
645   }
646 }
647 foreach name = ["dot", "distance"] in {
648   def : Builtin<name, [Float, GenTypeFloatVec1234, GenTypeFloatVec1234], Attr.Const>;
649   def : Builtin<name, [Double, GenTypeDoubleVec1234, GenTypeDoubleVec1234], Attr.Const>;
650   def : Builtin<name, [Half, GenTypeHalfVec1234, GenTypeHalfVec1234], Attr.Const>;
651 }
652 foreach name = ["fast_distance"] in {
653   def : Builtin<name, [Float, GenTypeFloatVec1234, GenTypeFloatVec1234], Attr.Const>;
654 }
655
656
657 //--------------------------------------------------------------------
658 // OpenCL v1.1 s6.11.6, v1.2 s6.12.6, v2.0 s6.13.6 - Relational Functions
659 // OpenCL Extension v2.0 s5.1.5 and s6.1.5 - Relational Functions
660 // --- Table 14 ---
661 // --- 1 argument ---
662 foreach name = ["isfinite", "isinf", "isnan", "isnormal", "signbit"] in {
663   def : Builtin<name, [GenTypeIntVecAndScalar, GenTypeFloatVecAndScalar], Attr.Const>;
664   def : Builtin<name, [Int, Double], Attr.Const>;
665   def : Builtin<name, [GenTypeLongVecNoScalar, GenTypeDoubleVecNoScalar], Attr.Const>;
666   def : Builtin<name, [Int, Half], Attr.Const>;
667   def : Builtin<name, [GenTypeShortVecNoScalar, GenTypeHalfVecNoScalar], Attr.Const>;
668 }
669 foreach name = ["any", "all"] in {
670   def : Builtin<name, [Int, AIGenTypeN], Attr.Const>;
671 }
672
673 // --- 2 arguments ---
674 foreach name = ["isequal", "isnotequal", "isgreater", "isgreaterequal",
675                 "isless", "islessequal", "islessgreater", "isordered",
676                 "isunordered"] in {
677   def : Builtin<name, [GenTypeIntVecAndScalar, GenTypeFloatVecAndScalar, GenTypeFloatVecAndScalar], Attr.Const>;
678   def : Builtin<name, [Int, Double, Double], Attr.Const>;
679   def : Builtin<name, [GenTypeLongVecNoScalar, GenTypeDoubleVecNoScalar, GenTypeDoubleVecNoScalar], Attr.Const>;
680   def : Builtin<name, [Int, Half, Half], Attr.Const>;
681   def : Builtin<name, [GenTypeShortVecNoScalar, GenTypeHalfVecNoScalar, GenTypeHalfVecNoScalar], Attr.Const>;
682 }
683
684 // --- 3 arguments ---
685 foreach name = ["bitselect"] in {
686   def : Builtin<name, [AGenTypeN, AGenTypeN, AGenTypeN, AGenTypeN], Attr.Const>;
687 }
688 foreach name = ["select"] in {
689   def : Builtin<name, [SGenTypeN, SGenTypeN, SGenTypeN, SGenTypeN], Attr.Const>;
690   def : Builtin<name, [SGenTypeN, SGenTypeN, SGenTypeN, UGenTypeN], Attr.Const>;
691   def : Builtin<name, [UGenTypeN, UGenTypeN, UGenTypeN, UGenTypeN], Attr.Const>;
692   def : Builtin<name, [UGenTypeN, UGenTypeN, UGenTypeN, SGenTypeN], Attr.Const>;
693   def : Builtin<name, [GenTypeFloatVecAndScalar, GenTypeFloatVecAndScalar, GenTypeFloatVecAndScalar, GenTypeIntVecAndScalar], Attr.Const>;
694   def : Builtin<name, [GenTypeFloatVecAndScalar, GenTypeFloatVecAndScalar, GenTypeFloatVecAndScalar, GenTypeUIntVecAndScalar], Attr.Const>;
695   def : Builtin<name, [GenTypeDoubleVecAndScalar, GenTypeDoubleVecAndScalar, GenTypeDoubleVecAndScalar, GenTypeLongVecAndScalar], Attr.Const>;
696   def : Builtin<name, [GenTypeDoubleVecAndScalar, GenTypeDoubleVecAndScalar, GenTypeDoubleVecAndScalar, GenTypeULongVecAndScalar], Attr.Const>;
697   def : Builtin<name, [GenTypeHalfVecAndScalar, GenTypeHalfVecAndScalar, GenTypeHalfVecAndScalar, GenTypeShortVecAndScalar], Attr.Const>;
698   def : Builtin<name, [GenTypeHalfVecAndScalar, GenTypeHalfVecAndScalar, GenTypeHalfVecAndScalar, GenTypeUShortVecAndScalar], Attr.Const>;
699 }
700
701
702 //--------------------------------------------------------------------
703 // OpenCL v1.1 s6.11.7, v1.2 s6.12.7, v2.0 s6.13.7 - Vector Data Load and Store Functions
704 // OpenCL Extension v1.1 s9.3.6 and s9.6.6, v1.2 s9.5.6, v2.0 s5.1.6 and s6.1.6 - Vector Data Load and Store Functions
705 // --- Table 15 ---
706 // Variants for OpenCL versions below 2.0, using pointers to the global, local
707 // and private address spaces.
708 let MaxVersion = CL20 in {
709   foreach AS = [GlobalAS, LocalAS, PrivateAS] in {
710     foreach VSize = [2, 3, 4, 8, 16] in {
711       foreach name = ["vload" # VSize] in {
712         def : Builtin<name, [VectorType<Char, VSize>, Size, PointerType<ConstType<Char>, AS>]>;
713         def : Builtin<name, [VectorType<UChar, VSize>, Size, PointerType<ConstType<UChar>, AS>]>;
714         def : Builtin<name, [VectorType<Short, VSize>, Size, PointerType<ConstType<Short>, AS>]>;
715         def : Builtin<name, [VectorType<UShort, VSize>, Size, PointerType<ConstType<UShort>, AS>]>;
716         def : Builtin<name, [VectorType<Int, VSize>, Size, PointerType<ConstType<Int>, AS>]>;
717         def : Builtin<name, [VectorType<UInt, VSize>, Size, PointerType<ConstType<UInt>, AS>]>;
718         def : Builtin<name, [VectorType<Long, VSize>, Size, PointerType<ConstType<Long>, AS>]>;
719         def : Builtin<name, [VectorType<ULong, VSize>, Size, PointerType<ConstType<ULong>, AS>]>;
720         def : Builtin<name, [VectorType<Float, VSize>, Size, PointerType<ConstType<Float>, AS>]>;
721         def : Builtin<name, [VectorType<Double, VSize>, Size, PointerType<ConstType<Double>, AS>]>;
722         def : Builtin<name, [VectorType<Half, VSize>, Size, PointerType<ConstType<Half>, AS>]>;
723       }
724       foreach name = ["vstore" # VSize] in {
725         def : Builtin<name, [Void, VectorType<Char, VSize>, Size, PointerType<ConstType<Char>, AS>]>;
726         def : Builtin<name, [Void, VectorType<UChar, VSize>, Size, PointerType<ConstType<UChar>, AS>]>;
727         def : Builtin<name, [Void, VectorType<Short, VSize>, Size, PointerType<ConstType<Short>, AS>]>;
728         def : Builtin<name, [Void, VectorType<UShort, VSize>, Size, PointerType<ConstType<UShort>, AS>]>;
729         def : Builtin<name, [Void, VectorType<Int, VSize>, Size, PointerType<ConstType<Int>, AS>]>;
730         def : Builtin<name, [Void, VectorType<UInt, VSize>, Size, PointerType<ConstType<UInt>, AS>]>;
731         def : Builtin<name, [Void, VectorType<Long, VSize>, Size, PointerType<ConstType<Long>, AS>]>;
732         def : Builtin<name, [Void, VectorType<ULong, VSize>, Size, PointerType<ConstType<ULong>, AS>]>;
733         def : Builtin<name, [Void, VectorType<Float, VSize>, Size, PointerType<ConstType<Float>, AS>]>;
734         def : Builtin<name, [Void, VectorType<Double, VSize>, Size, PointerType<ConstType<Double>, AS>]>;
735         def : Builtin<name, [Void, VectorType<Half, VSize>, Size, PointerType<ConstType<Half>, AS>]>;
736       }
737       foreach name = ["vloada_half" # VSize] in {
738         def : Builtin<name, [VectorType<Float, VSize>, Size, PointerType<ConstType<Half>, AS>]>;
739       }
740       foreach rnd = ["", "_rte", "_rtz", "_rtp", "_rtn"] in {
741         foreach name = ["vstorea_half" # VSize # rnd] in {
742           def : Builtin<name, [Void, VectorType<Float, VSize>, Size, PointerType<Half, AS>]>;
743           def : Builtin<name, [Void, VectorType<Double, VSize>, Size, PointerType<Half, AS>]>;
744         }
745       }
746     }
747   }
748 }
749 // Variants for OpenCL versions above 2.0, using pointers to the generic
750 // address space.
751 let MinVersion = CL20 in {
752   foreach VSize = [2, 3, 4, 8, 16] in {
753     foreach name = ["vload" # VSize] in {
754       def : Builtin<name, [VectorType<Char, VSize>, Size, PointerType<ConstType<Char>, GenericAS>]>;
755       def : Builtin<name, [VectorType<UChar, VSize>, Size, PointerType<ConstType<UChar>, GenericAS>]>;
756       def : Builtin<name, [VectorType<Short, VSize>, Size, PointerType<ConstType<Short>, GenericAS>]>;
757       def : Builtin<name, [VectorType<UShort, VSize>, Size, PointerType<ConstType<UShort>, GenericAS>]>;
758       def : Builtin<name, [VectorType<Int, VSize>, Size, PointerType<ConstType<Int>, GenericAS>]>;
759       def : Builtin<name, [VectorType<UInt, VSize>, Size, PointerType<ConstType<UInt>, GenericAS>]>;
760       def : Builtin<name, [VectorType<Long, VSize>, Size, PointerType<ConstType<Long>, GenericAS>]>;
761       def : Builtin<name, [VectorType<ULong, VSize>, Size, PointerType<ConstType<ULong>, GenericAS>]>;
762       def : Builtin<name, [VectorType<Float, VSize>, Size, PointerType<ConstType<Float>, GenericAS>]>;
763       def : Builtin<name, [VectorType<Double, VSize>, Size, PointerType<ConstType<Double>, GenericAS>]>;
764       def : Builtin<name, [VectorType<Half, VSize>, Size, PointerType<ConstType<Half>, GenericAS>]>;
765     }
766     foreach name = ["vstore" # VSize] in {
767       def : Builtin<name, [Void, VectorType<Char, VSize>, Size, PointerType<ConstType<Char>, GenericAS>]>;
768       def : Builtin<name, [Void, VectorType<UChar, VSize>, Size, PointerType<ConstType<UChar>, GenericAS>]>;
769       def : Builtin<name, [Void, VectorType<Short, VSize>, Size, PointerType<ConstType<Short>, GenericAS>]>;
770       def : Builtin<name, [Void, VectorType<UShort, VSize>, Size, PointerType<ConstType<UShort>, GenericAS>]>;
771       def : Builtin<name, [Void, VectorType<Int, VSize>, Size, PointerType<ConstType<Int>, GenericAS>]>;
772       def : Builtin<name, [Void, VectorType<UInt, VSize>, Size, PointerType<ConstType<UInt>, GenericAS>]>;
773       def : Builtin<name, [Void, VectorType<Long, VSize>, Size, PointerType<ConstType<Long>, GenericAS>]>;
774       def : Builtin<name, [Void, VectorType<ULong, VSize>, Size, PointerType<ConstType<ULong>, GenericAS>]>;
775       def : Builtin<name, [Void, VectorType<Float, VSize>, Size, PointerType<ConstType<Float>, GenericAS>]>;
776       def : Builtin<name, [Void, VectorType<Double, VSize>, Size, PointerType<ConstType<Double>, GenericAS>]>;
777       def : Builtin<name, [Void, VectorType<Half, VSize>, Size, PointerType<ConstType<Half>, GenericAS>]>;
778     }
779     foreach name = ["vloada_half" # VSize] in {
780       def : Builtin<name, [VectorType<Float, VSize>, Size, PointerType<ConstType<Half>, GenericAS>]>;
781     }
782     foreach rnd = ["", "_rte", "_rtz", "_rtp", "_rtn"] in {
783       foreach name = ["vstorea_half" # VSize # rnd] in {
784         def : Builtin<name, [Void, VectorType<Float, VSize>, Size, PointerType<Half, GenericAS>]>;
785         def : Builtin<name, [Void, VectorType<Double, VSize>, Size, PointerType<Half, GenericAS>]>;
786       }
787     }
788   }
789 }
790 // Variants using pointers to the constant address space.
791 foreach VSize = [2, 3, 4, 8, 16] in {
792   foreach name = ["vload" # VSize] in {
793     def : Builtin<name, [VectorType<Char, VSize>, Size, PointerType<ConstType<Char>, ConstantAS>]>;
794     def : Builtin<name, [VectorType<UChar, VSize>, Size, PointerType<ConstType<UChar>, ConstantAS>]>;
795     def : Builtin<name, [VectorType<Short, VSize>, Size, PointerType<ConstType<Short>, ConstantAS>]>;
796     def : Builtin<name, [VectorType<UShort, VSize>, Size, PointerType<ConstType<UShort>, ConstantAS>]>;
797     def : Builtin<name, [VectorType<Int, VSize>, Size, PointerType<ConstType<Int>, ConstantAS>]>;
798     def : Builtin<name, [VectorType<UInt, VSize>, Size, PointerType<ConstType<UInt>, ConstantAS>]>;
799     def : Builtin<name, [VectorType<Long, VSize>, Size, PointerType<ConstType<Long>, ConstantAS>]>;
800     def : Builtin<name, [VectorType<ULong, VSize>, Size, PointerType<ConstType<ULong>, ConstantAS>]>;
801     def : Builtin<name, [VectorType<Float, VSize>, Size, PointerType<ConstType<Float>, ConstantAS>]>;
802     def : Builtin<name, [VectorType<Double, VSize>, Size, PointerType<ConstType<Double>, ConstantAS>]>;
803     def : Builtin<name, [VectorType<Half, VSize>, Size, PointerType<ConstType<Half>, ConstantAS>]>;
804   }
805   foreach name = ["vloada_half" # VSize] in {
806     def : Builtin<name, [VectorType<Float, VSize>, Size, PointerType<ConstType<Half>, ConstantAS>]>;
807   }
808   foreach rnd = ["", "_rte", "_rtz", "_rtp", "_rtn"] in {
809     foreach name = ["vstorea_half" # VSize # rnd] in {
810       def : Builtin<name, [Void, VectorType<Float, VSize>, Size, PointerType<Half, ConstantAS>]>;
811       def : Builtin<name, [Void, VectorType<Double, VSize>, Size, PointerType<Half, ConstantAS>]>;
812     }
813   }
814 }
815 let MaxVersion = CL20 in {
816   foreach AS = [GlobalAS, LocalAS, PrivateAS] in {
817     def : Builtin<"vload_half", [Float, Size, PointerType<ConstType<Half>, AS>]>;
818     foreach VSize = [2, 3, 4, 8, 16] in {
819       foreach name = ["vload_half" # VSize] in {
820         def : Builtin<name, [VectorType<Float, VSize>, Size, PointerType<ConstType<Half>, AS>]>;
821       }
822     }
823     foreach rnd = ["", "_rte", "_rtz", "_rtp", "_rtn"] in {
824       def : Builtin<"vstore_half" # rnd, [Void, Float, Size, PointerType<Half, AS>]>;
825       def : Builtin<"vstore_half" # rnd, [Void, Double, Size, PointerType<Half, AS>]>;
826       foreach VSize = [2, 3, 4, 8, 16] in {
827         foreach name = ["vstore_half" # VSize # rnd] in {
828           def : Builtin<name, [Void, VectorType<Float, VSize>, Size, PointerType<Half, AS>]>;
829           def : Builtin<name, [Void, VectorType<Double, VSize>, Size, PointerType<Half, AS>]>;
830         }
831       }
832     }
833   }
834 }
835 let MinVersion = CL20 in {
836   foreach AS = [GenericAS] in {
837     def : Builtin<"vload_half", [Float, Size, PointerType<ConstType<Half>, AS>]>;
838     foreach VSize = [2, 3, 4, 8, 16] in {
839       foreach name = ["vload_half" # VSize] in {
840         def : Builtin<name, [VectorType<Float, VSize>, Size, PointerType<ConstType<Half>, AS>]>;
841       }
842     }
843     foreach rnd = ["", "_rte", "_rtz", "_rtp", "_rtn"] in {
844       def : Builtin<"vstore_half" # rnd, [Void, Float, Size, PointerType<Half, AS>]>;
845       def : Builtin<"vstore_half" # rnd, [Void, Double, Size, PointerType<Half, AS>]>;
846       foreach VSize = [2, 3, 4, 8, 16] in {
847         foreach name = ["vstore_half" # VSize # rnd] in {
848           def : Builtin<name, [Void, VectorType<Float, VSize>, Size, PointerType<Half, AS>]>;
849           def : Builtin<name, [Void, VectorType<Double, VSize>, Size, PointerType<Half, AS>]>;
850         }
851       }
852     }
853   }
854 }
855
856 foreach AS = [ConstantAS] in {
857   def : Builtin<"vload_half", [Float, Size, PointerType<ConstType<Half>, AS>]>;
858   foreach VSize = [2, 3, 4, 8, 16] in {
859     foreach name = ["vload_half" # VSize] in {
860       def : Builtin<name, [VectorType<Float, VSize>, Size, PointerType<ConstType<Half>, AS>]>;
861     }
862   }
863 }
864
865 //--------------------------------------------------------------------
866 // OpenCL v1.1 s6.11.10, v1.2 s6.12.10, v2.0 s6.13.10: Async Copies from Global to Local Memory, Local to Global Memory, and Prefetch
867 // OpenCL Extension v2.0 s5.1.7 and s6.1.7: Async Copies from Global to Local Memory, Local to Global Memory, and Prefetch
868 // --- Table 18 ---
869 foreach name = ["async_work_group_copy"] in {
870   def : Builtin<name, [Event, PointerType<AGenTypeN, LocalAS>, PointerType<ConstType<AGenTypeN>, GlobalAS>, Size, Event]>;
871   def : Builtin<name, [Event, PointerType<AGenTypeN, GlobalAS>, PointerType<ConstType<AGenTypeN>, LocalAS>, Size, Event]>;
872 }
873 foreach name = ["async_work_group_strided_copy"] in {
874   def : Builtin<name, [Event, PointerType<AGenTypeN, LocalAS>, PointerType<ConstType<AGenTypeN>, GlobalAS>, Size, Size, Event]>;
875   def : Builtin<name, [Event, PointerType<AGenTypeN, GlobalAS>, PointerType<ConstType<AGenTypeN>, LocalAS>, Size, Size, Event]>;
876 }
877 foreach name = ["wait_group_events"] in {
878   def : Builtin<name, [Void, Int, PointerType<Event, GenericAS>]>;
879 }
880 foreach name = ["prefetch"] in {
881   def : Builtin<name, [Void, PointerType<ConstType<AGenTypeN>, GlobalAS>, Size]>;
882 }
883
884 //--------------------------------------------------------------------
885 // OpenCL v2.0 s6.13.11 - Atomics Functions.
886 // Functions that use memory_order and cl_mem_fence_flags enums are not
887 // declared here as the TableGen backend does not handle enums.
888
889 // OpenCL v1.0 s9.5, s9.6, s9.7 - Atomic Functions for 32-bit integers
890 // --- Table 9.1 ---
891 let Extension = FuncExtKhrGlobalInt32BaseAtomics in {
892   foreach Type = [Int, UInt] in {
893     foreach name = ["atom_add", "atom_sub", "atom_xchg"] in {
894       def : Builtin<name, [Type, PointerType<VolatileType<Type>, GlobalAS>, Type]>;
895     }
896     foreach name = ["atom_inc", "atom_dec"] in {
897       def : Builtin<name, [Type, PointerType<VolatileType<Type>, GlobalAS>]>;
898     }
899     foreach name = ["atom_cmpxchg"] in {
900       def : Builtin<name, [Type, PointerType<VolatileType<Type>, GlobalAS>, Type, Type]>;
901     }
902   }
903 }
904 // --- Table 9.3 ---
905 let Extension = FuncExtKhrLocalInt32BaseAtomics in {
906   foreach Type = [Int, UInt] in {
907     foreach name = ["atom_add", "atom_sub", "atom_xchg"] in {
908       def : Builtin<name, [Type, PointerType<VolatileType<Type>, LocalAS>, Type]>;
909     }
910     foreach name = ["atom_inc", "atom_dec"] in {
911       def : Builtin<name, [Type, PointerType<VolatileType<Type>, LocalAS>]>;
912     }
913     foreach name = ["atom_cmpxchg"] in {
914       def : Builtin<name, [Type, PointerType<VolatileType<Type>, LocalAS>, Type, Type]>;
915     }
916   }
917 }
918 // --- Table 9.5 ---
919 let Extension = FuncExtKhrInt64BaseAtomics in {
920   foreach AS = [GlobalAS, LocalAS] in {
921     foreach Type = [Long, ULong] in {
922       foreach name = ["atom_add", "atom_sub", "atom_xchg"] in {
923         def : Builtin<name, [Type, PointerType<VolatileType<Type>, AS>, Type]>;
924       }
925       foreach name = ["atom_inc", "atom_dec"] in {
926         def : Builtin<name, [Type, PointerType<VolatileType<Type>, AS>]>;
927       }
928       foreach name = ["atom_cmpxchg"] in {
929         def : Builtin<name, [Type, PointerType<VolatileType<Type>, AS>, Type, Type]>;
930       }
931     }
932   }
933 }
934 // --- Table 9.2 ---
935 let Extension = FuncExtKhrGlobalInt32ExtendedAtomics in {
936   foreach Type = [Int, UInt] in {
937     foreach name = ["atom_min", "atom_max", "atom_and",
938                     "atom_or", "atom_xor"] in {
939       def : Builtin<name, [Type, PointerType<VolatileType<Type>, GlobalAS>, Type]>;
940     }
941   }
942 }
943 // --- Table 9.4 ---
944 let Extension = FuncExtKhrLocalInt32ExtendedAtomics in {
945   foreach Type = [Int, UInt] in {
946     foreach name = ["atom_min", "atom_max", "atom_and",
947                     "atom_or", "atom_xor"] in {
948       def : Builtin<name, [Type, PointerType<VolatileType<Type>, LocalAS>, Type]>;
949     }
950   }
951 }
952 // --- Table 9.6 ---
953 let Extension = FuncExtKhrInt64ExtendedAtomics in {
954   foreach AS = [GlobalAS, LocalAS] in {
955     foreach Type = [Long, ULong] in {
956       foreach name = ["atom_min", "atom_max", "atom_and",
957                       "atom_or", "atom_xor"] in {
958         def : Builtin<name, [Type, PointerType<VolatileType<Type>, AS>, Type]>;
959       }
960     }
961   }
962 }
963 // OpenCL v1.1 s6.11.1, v1.2 s6.12.11 - Atomic Functions
964 foreach AS = [GlobalAS, LocalAS] in {
965   foreach Type = [Int, UInt] in {
966     foreach name = ["atomic_add", "atomic_sub", "atomic_xchg",
967                     "atomic_min", "atomic_max", "atomic_and",
968                     "atomic_or", "atomic_xor"] in {
969       def : Builtin<name, [Type, PointerType<VolatileType<Type>, AS>, Type]>;
970     }
971     foreach name = ["atomic_inc", "atomic_dec"] in {
972       def : Builtin<name, [Type, PointerType<VolatileType<Type>, AS>]>;
973     }
974     foreach name = ["atomic_cmpxchg"] in {
975       def : Builtin<name, [Type, PointerType<VolatileType<Type>, AS>, Type, Type]>;
976     }
977   }
978 }
979
980 //--------------------------------------------------------------------
981 // OpenCL v1.1 s6.11.12, v1.2 s6.12.12, v2.0 s6.13.12 - Miscellaneous Vector Functions
982 // --- Table 19 ---
983 foreach VSize1 = [Vec2, Vec4, Vec8, Vec16] in {
984   foreach VSize2 = [Vec2, Vec4, Vec8, Vec16] in {
985     def : Builtin<"shuffle", [GenericType<"TLAll" # VSize1.Name, TLAll, VSize1>,
986                               GenericType<"TLAll" # VSize2.Name, TLAll, VSize2>,
987                               GenericType<"TLAllUnsigned" # VSize1.Name, TLAllUnsigned, VSize1>],
988                   Attr.Const>;
989   }
990 }
991 foreach VSize1 = [Vec2, Vec4, Vec8, Vec16] in {
992   foreach VSize2 = [Vec2, Vec4, Vec8, Vec16] in {
993     def : Builtin<"shuffle2", [GenericType<"TLAll" # VSize1.Name, TLAll, VSize1>,
994                                GenericType<"TLAll" # VSize2.Name, TLAll, VSize2>,
995                                GenericType<"TLAll" # VSize2.Name, TLAll, VSize2>,
996                                GenericType<"TLAllUnsigned" # VSize1.Name, TLAllUnsigned, VSize1>],
997                   Attr.Const>;
998   }
999 }
1000
1001 //--------------------------------------------------------------------
1002 // OpenCL v1.1 s6.11.3, v1.2 s6.12.14, v2.0 s6.13.14: Image Read and Write Functions
1003 // OpenCL Extension v2.0 s5.1.8 and s6.1.8: Image Read and Write Functions
1004 // --- Table 22: Image Read Functions with Samplers ---
1005 foreach imgTy = [Image1d] in {
1006   foreach coordTy = [Int, Float] in {
1007     def : Builtin<"read_imagef", [VectorType<Float, 4>, ImageType<imgTy, "RO">, Sampler, coordTy], Attr.Pure>;
1008     def : Builtin<"read_imagei", [VectorType<Int, 4>, ImageType<imgTy, "RO">, Sampler, coordTy], Attr.Pure>;
1009     def : Builtin<"read_imageui", [VectorType<UInt, 4>, ImageType<imgTy, "RO">, Sampler, coordTy], Attr.Pure>;
1010   }
1011 }
1012 foreach imgTy = [Image2d, Image1dArray] in {
1013   foreach coordTy = [Int, Float] in {
1014     def : Builtin<"read_imagef", [VectorType<Float, 4>, ImageType<imgTy, "RO">, Sampler, VectorType<coordTy, 2>], Attr.Pure>;
1015     def : Builtin<"read_imagei", [VectorType<Int, 4>, ImageType<imgTy, "RO">, Sampler, VectorType<coordTy, 2>], Attr.Pure>;
1016     def : Builtin<"read_imageui", [VectorType<UInt, 4>, ImageType<imgTy, "RO">, Sampler, VectorType<coordTy, 2>], Attr.Pure>;
1017   }
1018 }
1019 foreach imgTy = [Image3d, Image2dArray] in {
1020   foreach coordTy = [Int, Float] in {
1021     def : Builtin<"read_imagef", [VectorType<Float, 4>, ImageType<imgTy, "RO">, Sampler, VectorType<coordTy, 4>], Attr.Pure>;
1022     def : Builtin<"read_imagei", [VectorType<Int, 4>, ImageType<imgTy, "RO">, Sampler, VectorType<coordTy, 4>], Attr.Pure>;
1023     def : Builtin<"read_imageui", [VectorType<UInt, 4>, ImageType<imgTy, "RO">, Sampler, VectorType<coordTy, 4>], Attr.Pure>;
1024   }
1025 }
1026 foreach coordTy = [Int, Float] in {
1027   def : Builtin<"read_imagef", [Float, ImageType<Image2dDepth, "RO">, Sampler, VectorType<coordTy, 2>], Attr.Pure>;
1028   def : Builtin<"read_imagef", [Float, ImageType<Image2dArrayDepth, "RO">, Sampler, VectorType<coordTy, 4>], Attr.Pure>;
1029 }
1030
1031 // --- Table 23: Sampler-less Read Functions ---
1032 foreach aQual = ["RO", "RW"] in {
1033   foreach imgTy = [Image2d, Image1dArray] in {
1034     def : Builtin<"read_imagef", [VectorType<Float, 4>, ImageType<imgTy, aQual>, VectorType<Int, 2>], Attr.Pure>;
1035     def : Builtin<"read_imagei", [VectorType<Int, 4>, ImageType<imgTy, aQual>, VectorType<Int, 2>], Attr.Pure>;
1036     def : Builtin<"read_imageui", [VectorType<UInt, 4>, ImageType<imgTy, aQual>, VectorType<Int, 2>], Attr.Pure>;
1037   }
1038   foreach imgTy = [Image3d, Image2dArray] in {
1039     def : Builtin<"read_imagef", [VectorType<Float, 4>, ImageType<imgTy, aQual>, VectorType<Int, 4>], Attr.Pure>;
1040     def : Builtin<"read_imagei", [VectorType<Int, 4>, ImageType<imgTy, aQual>, VectorType<Int, 4>], Attr.Pure>;
1041     def : Builtin<"read_imageui", [VectorType<UInt, 4>, ImageType<imgTy, aQual>, VectorType<Int, 4>], Attr.Pure>;
1042   }
1043   foreach imgTy = [Image1d, Image1dBuffer] in {
1044     def : Builtin<"read_imagef", [VectorType<Float, 4>, ImageType<imgTy, aQual>, Int], Attr.Pure>;
1045     def : Builtin<"read_imagei", [VectorType<Int, 4>, ImageType<imgTy, aQual>, Int], Attr.Pure>;
1046     def : Builtin<"read_imageui", [VectorType<UInt, 4>, ImageType<imgTy, aQual>, Int], Attr.Pure>;
1047   }
1048   def : Builtin<"read_imagef", [Float, ImageType<Image2dDepth, aQual>, VectorType<Int, 2>], Attr.Pure>;
1049   def : Builtin<"read_imagef", [Float, ImageType<Image2dArrayDepth, aQual>, VectorType<Int, 4>], Attr.Pure>;
1050 }
1051
1052 // --- Table 24: Image Write Functions ---
1053 foreach aQual = ["WO", "RW"] in {
1054   foreach imgTy = [Image2d] in {
1055     def : Builtin<"write_imagef", [Void, ImageType<imgTy, aQual>, VectorType<Int, 2>, VectorType<Float, 4>]>;
1056     def : Builtin<"write_imagei", [Void, ImageType<imgTy, aQual>, VectorType<Int, 2>, VectorType<Int, 4>]>;
1057     def : Builtin<"write_imageui", [Void, ImageType<imgTy, aQual>, VectorType<Int, 2>, VectorType<UInt, 4>]>;
1058   }
1059   foreach imgTy = [Image2dArray] in {
1060     def : Builtin<"write_imagef", [Void, ImageType<imgTy, aQual>, VectorType<Int, 4>, VectorType<Float, 4>]>;
1061     def : Builtin<"write_imagei", [Void, ImageType<imgTy, aQual>, VectorType<Int, 4>, VectorType<Int, 4>]>;
1062     def : Builtin<"write_imageui", [Void, ImageType<imgTy, aQual>, VectorType<Int, 4>, VectorType<UInt, 4>]>;
1063   }
1064   foreach imgTy = [Image1d, Image1dBuffer] in {
1065     def : Builtin<"write_imagef", [Void, ImageType<imgTy, aQual>, Int, VectorType<Float, 4>]>;
1066     def : Builtin<"write_imagei", [Void, ImageType<imgTy, aQual>, Int, VectorType<Int, 4>]>;
1067     def : Builtin<"write_imageui", [Void, ImageType<imgTy, aQual>, Int, VectorType<UInt, 4>]>;
1068   }
1069   foreach imgTy = [Image1dArray] in {
1070     def : Builtin<"write_imagef", [Void, ImageType<imgTy, aQual>, VectorType<Int, 2>, VectorType<Float, 4>]>;
1071     def : Builtin<"write_imagei", [Void, ImageType<imgTy, aQual>, VectorType<Int, 2>, VectorType<Int, 4>]>;
1072     def : Builtin<"write_imageui", [Void, ImageType<imgTy, aQual>, VectorType<Int, 2>, VectorType<UInt, 4>]>;
1073   }
1074   foreach imgTy = [Image3d] in {
1075     def : Builtin<"write_imagef", [Void, ImageType<imgTy, aQual>, VectorType<Int, 4>, VectorType<Float, 4>]>;
1076     def : Builtin<"write_imagei", [Void, ImageType<imgTy, aQual>, VectorType<Int, 4>, VectorType<Int, 4>]>;
1077     def : Builtin<"write_imageui", [Void, ImageType<imgTy, aQual>, VectorType<Int, 4>, VectorType<UInt, 4>]>;
1078   }
1079   def : Builtin<"write_imagef", [Void, ImageType<Image2dDepth, aQual>, VectorType<Int, 2>, Float]>;
1080   def : Builtin<"write_imagef", [Void, ImageType<Image2dArrayDepth, aQual>, VectorType<Int, 4>, Float]>;
1081 }
1082
1083 // --- Table 25: Image Query Functions ---
1084 foreach aQual = ["RO", "WO", "RW"] in {
1085   foreach imgTy = [Image1d, Image1dBuffer, Image2d, Image3d,
1086                    Image1dArray, Image2dArray, Image2dDepth,
1087                    Image2dArrayDepth] in {
1088     foreach name = ["get_image_width", "get_image_channel_data_type",
1089                     "get_image_channel_order"] in {
1090       def : Builtin<name, [Int, ImageType<imgTy, aQual>]>;
1091     }
1092   }
1093   foreach imgTy = [Image2d, Image3d, Image2dArray, Image2dDepth,
1094                    Image2dArrayDepth] in {
1095     def : Builtin<"get_image_height", [Int, ImageType<imgTy, aQual>]>;
1096   }
1097   def : Builtin<"get_image_depth", [Int, ImageType<Image3d, aQual>]>;
1098   foreach imgTy = [Image2d, Image2dArray, Image2dDepth,
1099                    Image2dArrayDepth] in {
1100     def : Builtin<"get_image_dim", [VectorType<Int, 2>, ImageType<imgTy, aQual>]>;
1101   }
1102   def : Builtin<"get_image_dim", [VectorType<Int, 4>, ImageType<Image3d, aQual>]>;
1103   foreach imgTy = [Image1dArray, Image2dArray, Image2dArrayDepth] in {
1104     def : Builtin<"get_image_array_size", [Size, ImageType<imgTy, aQual>]>;
1105   }
1106 }
1107
1108 // OpenCL extension v2.0 s5.1.9: Built-in Image Read Functions
1109 // --- Table 8 ---
1110 foreach aQual = ["RO"] in {
1111   foreach name = ["read_imageh"] in {
1112     foreach coordTy = [Int, Float] in {
1113       foreach imgTy = [Image2d, Image1dArray] in {
1114         def : Builtin<name, [VectorType<Half, 4>, ImageType<imgTy, aQual>, Sampler, VectorType<coordTy, 2>], Attr.Pure>;
1115       }
1116       foreach imgTy = [Image3d, Image2dArray] in {
1117         def : Builtin<name, [VectorType<Half, 4>, ImageType<imgTy, aQual>, Sampler, VectorType<coordTy, 4>], Attr.Pure>;
1118       }
1119       foreach imgTy = [Image1d] in {
1120         def : Builtin<name, [VectorType<Half, 4>, ImageType<imgTy, aQual>, Sampler, coordTy], Attr.Pure>;
1121       }
1122     }
1123   }
1124 }
1125 // OpenCL extension v2.0 s5.1.10: Built-in Image Sampler-less Read Functions
1126 // --- Table 9 ---
1127 foreach aQual = ["RO", "RW"] in {
1128   foreach name = ["read_imageh"] in {
1129     foreach imgTy = [Image2d, Image1dArray] in {
1130       def : Builtin<name, [VectorType<Half, 4>, ImageType<imgTy, aQual>, VectorType<Int, 2>], Attr.Pure>;
1131     }
1132     foreach imgTy = [Image3d, Image2dArray] in {
1133       def : Builtin<name, [VectorType<Half, 4>, ImageType<imgTy, aQual>, VectorType<Int, 4>], Attr.Pure>;
1134     }
1135     foreach imgTy = [Image1d, Image1dBuffer] in {
1136       def : Builtin<name, [VectorType<Half, 4>, ImageType<imgTy, aQual>, Int], Attr.Pure>;
1137     }
1138   }
1139 }
1140 // OpenCL extension v2.0 s5.1.11: Built-in Image Write Functions
1141 // --- Table 10 ---
1142 foreach aQual = ["WO", "RW"] in {
1143   foreach name = ["write_imageh"] in {
1144     def : Builtin<name, [Void, ImageType<Image2d, aQual>, VectorType<Int, 2>, VectorType<Half, 4>]>;
1145     def : Builtin<name, [Void, ImageType<Image2dArray, aQual>, VectorType<Int, 4>, VectorType<Half, 4>]>;
1146     def : Builtin<name, [Void, ImageType<Image1d, aQual>, Int, VectorType<Half, 4>]>;
1147     def : Builtin<name, [Void, ImageType<Image1dBuffer, aQual>, Int, VectorType<Half, 4>]>;
1148     def : Builtin<name, [Void, ImageType<Image1dArray, aQual>, VectorType<Int, 2>, VectorType<Half, 4>]>;
1149     def : Builtin<name, [Void, ImageType<Image3d, aQual>, VectorType<Int, 4>, VectorType<Half, 4>]>;
1150   }
1151 }
1152
1153
1154 //--------------------------------------------------------------------
1155 // OpenCL v2.0 s6.13.15 - Work-group Functions
1156 // --- Table 26 ---
1157 let MinVersion = CL20 in {
1158   foreach name = ["work_group_all", "work_group_any"] in {
1159     def : Builtin<name, [Int, Int], Attr.Convergent>;
1160   }
1161   foreach name = ["work_group_broadcast"] in {
1162     def : Builtin<name, [IntLongFloatGenType1, IntLongFloatGenType1, Size], Attr.Convergent>;
1163     def : Builtin<name, [IntLongFloatGenType1, IntLongFloatGenType1, Size, Size], Attr.Convergent>;
1164     def : Builtin<name, [IntLongFloatGenType1, IntLongFloatGenType1, Size, Size, Size], Attr.Convergent>;
1165   }
1166   foreach op = ["add", "min", "max"] in {
1167     foreach name = ["work_group_reduce_", "work_group_scan_exclusive_",
1168                     "work_group_scan_inclusive_"] in {
1169       def : Builtin<name # op, [IntLongFloatGenType1, IntLongFloatGenType1], Attr.Convergent>;
1170     }
1171   }
1172 }
1173
1174
1175 // OpenCL v2.0 s9.17.3: Additions to section 6.13.1: Work-Item Functions
1176 let MinVersion = CL20 in {
1177   let Extension = FuncExtKhrSubgroups in {
1178     def get_sub_group_size : Builtin<"get_sub_group_size", [UInt]>;
1179     def get_max_sub_group_size : Builtin<"get_max_sub_group_size", [UInt]>;
1180     def get_num_sub_groups : Builtin<"get_num_sub_groups", [UInt]>;
1181   }
1182 }
1183
1184 //--------------------------------------------------------------------
1185 // End of the builtin functions defined in the OpenCL C specification.
1186 // Builtin functions defined in the OpenCL C Extension are below.
1187 //--------------------------------------------------------------------
1188
1189
1190 // OpenCL Extension v2.0 s9.18 - Mipmaps
1191 let Extension = FuncExtKhrMipmapImage in {
1192   // Added to section 6.13.14.2.
1193   foreach aQual = ["RO"] in {
1194     foreach imgTy = [Image2d] in {
1195       foreach name = ["read_imagef"] in {
1196         def : Builtin<name, [VectorType<Float, 4>, ImageType<imgTy, aQual>, Sampler, VectorType<Float, 2>, Float], Attr.Pure>;
1197         def : Builtin<name, [VectorType<Float, 4>, ImageType<imgTy, aQual>, Sampler, VectorType<Float, 2>, VectorType<Float, 2>, VectorType<Float, 2>], Attr.Pure>;
1198       }
1199       foreach name = ["read_imagei"] in {
1200         def : Builtin<name, [VectorType<Int, 4>, ImageType<imgTy, aQual>, Sampler, VectorType<Float, 2>, Float], Attr.Pure>;
1201         def : Builtin<name, [VectorType<Int, 4>, ImageType<imgTy, aQual>, Sampler, VectorType<Float, 2>, VectorType<Float, 2>, VectorType<Float, 2>], Attr.Pure>;
1202       }
1203       foreach name = ["read_imageui"] in {
1204         def : Builtin<name, [VectorType<UInt, 4>, ImageType<imgTy, aQual>, Sampler, VectorType<Float, 2>, Float], Attr.Pure>;
1205         def : Builtin<name, [VectorType<UInt, 4>, ImageType<imgTy, aQual>, Sampler, VectorType<Float, 2>, VectorType<Float, 2>, VectorType<Float, 2>], Attr.Pure>;
1206       }
1207     }
1208     foreach imgTy = [Image2dDepth] in {
1209       foreach name = ["read_imagef"] in {
1210         def : Builtin<name, [Float, ImageType<imgTy, aQual>, Sampler, VectorType<Float, 2>, Float], Attr.Pure>;
1211         def : Builtin<name, [Float, ImageType<imgTy, aQual>, Sampler, VectorType<Float, 2>, VectorType<Float, 2>, VectorType<Float, 2>], Attr.Pure>;
1212       }
1213     }
1214     foreach imgTy = [Image1d] in {
1215       foreach name = ["read_imagef"] in {
1216         def : Builtin<name, [VectorType<Float, 4>, ImageType<imgTy, aQual>, Sampler, Float, Float], Attr.Pure>;
1217         def : Builtin<name, [VectorType<Float, 4>, ImageType<imgTy, aQual>, Sampler, Float, Float, Float], Attr.Pure>;
1218       }
1219       foreach name = ["read_imagei"] in {
1220         def : Builtin<name, [VectorType<Int, 4>, ImageType<imgTy, aQual>, Sampler, Float, Float], Attr.Pure>;
1221         def : Builtin<name, [VectorType<Int, 4>, ImageType<imgTy, aQual>, Sampler, Float, Float, Float], Attr.Pure>;
1222       }
1223       foreach name = ["read_imageui"] in {
1224         def : Builtin<name, [VectorType<UInt, 4>, ImageType<imgTy, aQual>, Sampler, Float, Float], Attr.Pure>;
1225         def : Builtin<name, [VectorType<UInt, 4>, ImageType<imgTy, aQual>, Sampler, Float, Float, Float], Attr.Pure>;
1226       }
1227     }
1228     foreach imgTy = [Image3d] in {
1229       foreach name = ["read_imagef"] in {
1230         def : Builtin<name, [VectorType<Float, 4>, ImageType<imgTy, aQual>, Sampler, VectorType<Float, 4>, VectorType<Float, 4>, VectorType<Float, 4>], Attr.Pure>;
1231         def : Builtin<name, [VectorType<Float, 4>, ImageType<imgTy, aQual>, Sampler, VectorType<Float, 4>, Float], Attr.Pure>;
1232       }
1233       foreach name = ["read_imagei"] in {
1234         def : Builtin<name, [VectorType<Int, 4>, ImageType<imgTy, aQual>, Sampler, VectorType<Float, 4>, VectorType<Float, 4>, VectorType<Float, 4>], Attr.Pure>;
1235         def : Builtin<name, [VectorType<Float, 4>, ImageType<imgTy, aQual>, Sampler, VectorType<Float, 4>, Float], Attr.Pure>;
1236       }
1237       foreach name = ["read_imageui"] in {
1238         def : Builtin<name, [VectorType<UInt, 4>, ImageType<imgTy, aQual>, Sampler, VectorType<Float, 4>, VectorType<Float, 4>, VectorType<Float, 4>], Attr.Pure>;
1239         def : Builtin<name, [VectorType<Float, 4>, ImageType<imgTy, aQual>, Sampler, VectorType<Float, 4>, Float], Attr.Pure>;
1240       }
1241     }
1242     foreach imgTy = [Image1dArray] in {
1243       foreach name = ["read_imagef"] in {
1244         def : Builtin<name, [VectorType<Float, 4>, ImageType<imgTy, aQual>, Sampler, VectorType<Float, 2>, Float], Attr.Pure>;
1245         def : Builtin<name, [VectorType<Float, 4>, ImageType<imgTy, aQual>, Sampler, VectorType<Float, 2>, Float, Float], Attr.Pure>;
1246       }
1247       foreach name = ["read_imagei"] in {
1248         def : Builtin<name, [VectorType<Int, 4>, ImageType<imgTy, aQual>, Sampler, VectorType<Float, 2>, Float], Attr.Pure>;
1249         def : Builtin<name, [VectorType<Int, 4>, ImageType<imgTy, aQual>, Sampler, VectorType<Float, 2>, Float, Float], Attr.Pure>;
1250       }
1251       foreach name = ["read_imageui"] in {
1252         def : Builtin<name, [VectorType<UInt, 4>, ImageType<imgTy, aQual>, Sampler, VectorType<Float, 2>, Float], Attr.Pure>;
1253         def : Builtin<name, [VectorType<UInt, 4>, ImageType<imgTy, aQual>, Sampler, VectorType<Float, 2>, Float, Float], Attr.Pure>;
1254       }
1255     }
1256     foreach imgTy = [Image2dArray] in {
1257       foreach name = ["read_imagef"] in {
1258         def : Builtin<name, [VectorType<Float, 4>, ImageType<imgTy, aQual>, Sampler, VectorType<Float, 4>, Float], Attr.Pure>;
1259         def : Builtin<name, [VectorType<Float, 4>, ImageType<imgTy, aQual>, Sampler, VectorType<Float, 4>, VectorType<Float, 2>, VectorType<Float, 2>], Attr.Pure>;
1260       }
1261       foreach name = ["read_imagei"] in {
1262         def : Builtin<name, [VectorType<Int, 4>, ImageType<imgTy, aQual>, Sampler, VectorType<Float, 4>, Float], Attr.Pure>;
1263         def : Builtin<name, [VectorType<Int, 4>, ImageType<imgTy, aQual>, Sampler, VectorType<Float, 4>, VectorType<Float, 2>, VectorType<Float, 2>], Attr.Pure>;
1264       }
1265       foreach name = ["read_imageui"] in {
1266         def : Builtin<name, [VectorType<UInt, 4>, ImageType<imgTy, aQual>, Sampler, VectorType<Float, 4>, Float], Attr.Pure>;
1267         def : Builtin<name, [VectorType<UInt, 4>, ImageType<imgTy, aQual>, Sampler, VectorType<Float, 4>, VectorType<Float, 2>, VectorType<Float, 2>], Attr.Pure>;
1268       }
1269     }
1270     foreach imgTy = [Image2dArrayDepth] in {
1271       foreach name = ["read_imagef"] in {
1272         def : Builtin<name, [VectorType<UInt, 4>, ImageType<imgTy, aQual>, Sampler, VectorType<Float, 4>, Float], Attr.Pure>;
1273         def : Builtin<name, [VectorType<UInt, 4>, ImageType<imgTy, aQual>, Sampler, VectorType<Float, 4>, VectorType<Float, 2>, VectorType<Float, 2>], Attr.Pure>;
1274       }
1275     }
1276   }
1277   // Added to section 6.13.14.4.
1278   foreach aQual = ["WO"] in {
1279     foreach imgTy = [Image2d] in {
1280       def : Builtin<"write_imagef", [Void, ImageType<imgTy, aQual>, VectorType<Int, 2>, Int, VectorType<Float, 4>]>;
1281       def : Builtin<"write_imagei", [Void, ImageType<imgTy, aQual>, VectorType<Int, 2>, Int, VectorType<Int, 4>]>;
1282       def : Builtin<"write_imageui", [Void, ImageType<imgTy, aQual>, VectorType<Int, 2>, Int, VectorType<UInt, 4>]>;
1283     }
1284     def : Builtin<"write_imagef", [Void, ImageType<Image2dDepth, aQual>, VectorType<Int, 2>, Int, Float]>;
1285     foreach imgTy = [Image1d] in {
1286       def : Builtin<"write_imagef", [Void, ImageType<imgTy, aQual>, Int, Int, VectorType<Float, 4>]>;
1287       def : Builtin<"write_imagei", [Void, ImageType<imgTy, aQual>, Int, Int, VectorType<Int, 4>]>;
1288       def : Builtin<"write_imageui", [Void, ImageType<imgTy, aQual>, Int, Int, VectorType<UInt, 4>]>;
1289     }
1290     foreach imgTy = [Image1dArray] in {
1291       def : Builtin<"write_imagef", [Void, ImageType<imgTy, aQual>, VectorType<Int, 2>, Int, VectorType<Float, 4>]>;
1292       def : Builtin<"write_imagei", [Void, ImageType<imgTy, aQual>, VectorType<Int, 2>, Int, VectorType<Int, 4>]>;
1293       def : Builtin<"write_imageui", [Void, ImageType<imgTy, aQual>, VectorType<Int, 2>, Int, VectorType<UInt, 4>]>;
1294     }
1295     foreach imgTy = [Image2dArray] in {
1296       def : Builtin<"write_imagef", [Void, ImageType<imgTy, aQual>, VectorType<Int, 4>, Int, VectorType<Float, 4>]>;
1297       def : Builtin<"write_imagei", [Void, ImageType<imgTy, aQual>, VectorType<Int, 4>, Int, VectorType<Int, 4>]>;
1298       def : Builtin<"write_imageui", [Void, ImageType<imgTy, aQual>, VectorType<Int, 4>, Int, VectorType<UInt, 4>]>;
1299     }
1300     def : Builtin<"write_imagef", [Void, ImageType<Image2dArrayDepth, aQual>, VectorType<Int, 4>, Int, Float]>;
1301     let Extension = FuncExtKhrMipmapAndWrite3d in {
1302       foreach imgTy = [Image3d] in {
1303         def : Builtin<"write_imagef", [Void, ImageType<imgTy, aQual>, VectorType<Int, 4>, Int, VectorType<Float, 4>]>;
1304         def : Builtin<"write_imagei", [Void, ImageType<imgTy, aQual>, VectorType<Int, 4>, Int, VectorType<Int, 4>]>;
1305         def : Builtin<"write_imageui", [Void, ImageType<imgTy, aQual>, VectorType<Int, 4>, Int, VectorType<UInt, 4>]>;
1306       }
1307     }
1308   }
1309   // Added to section 6.13.14.5
1310   foreach aQual = ["RO", "WO", "RW"] in {
1311     foreach imgTy = [Image1d, Image2d, Image3d, Image1dArray, Image2dArray, Image2dDepth, Image2dArrayDepth] in {
1312       def : Builtin<"get_image_num_mip_levels", [Int, ImageType<imgTy, aQual>]>;
1313     }
1314   }
1315 }
1316
1317
1318 //--------------------------------------------------------------------
1319 // OpenCL Extension v2.0 s18.3 - Creating OpenCL Memory Objects from OpenGL MSAA Textures
1320 let Extension = FuncExtKhrGlMsaaSharing in {
1321   // --- Table 6.13.14.3 ---
1322   foreach aQual = ["RO", "RW"] in {
1323     foreach imgTy = [Image2dMsaa] in {
1324       def : Builtin<"read_imagef", [VectorType<Float, 4>, ImageType<imgTy, aQual>, VectorType<Int, 2>, Int], Attr.Pure>;
1325       def : Builtin<"read_imagei", [VectorType<Int, 4>, ImageType<imgTy, aQual>, VectorType<Int, 2>, Int], Attr.Pure>;
1326       def : Builtin<"read_imageui", [VectorType<UInt, 4>, ImageType<imgTy, aQual>, VectorType<Int, 2>, Int], Attr.Pure>;
1327     }
1328     foreach imgTy = [Image2dArrayMsaa] in {
1329       def : Builtin<"read_imagef", [VectorType<Float, 4>, ImageType<imgTy, aQual>, VectorType<Int, 4>, Int], Attr.Pure>;
1330       def : Builtin<"read_imagei", [VectorType<Int, 4>, ImageType<imgTy, aQual>, VectorType<Int, 4>, Int], Attr.Pure>;
1331       def : Builtin<"read_imageui", [VectorType<UInt, 4>, ImageType<imgTy, aQual>, VectorType<Int, 4>, Int], Attr.Pure>;
1332     }
1333     foreach name = ["read_imagef"] in {
1334       def : Builtin<name, [Float, ImageType<Image2dMsaaDepth, aQual>, VectorType<Int, 2>, Int], Attr.Pure>;
1335       def : Builtin<name, [Float, ImageType<Image2dArrayMsaaDepth, aQual>, VectorType<Int, 4>, Int], Attr.Pure>;
1336     }
1337   }
1338
1339   // --- Table 6.13.14.5 ---
1340   foreach aQual = ["RO", "WO", "RW"] in {
1341     foreach imgTy = [Image2dMsaa, Image2dArrayMsaa, Image2dMsaaDepth, Image2dArrayMsaaDepth] in {
1342       foreach name = ["get_image_width", "get_image_height",
1343                       "get_image_channel_data_type", "get_image_channel_order",
1344                       "get_image_num_samples"] in {
1345         def : Builtin<name, [Int, ImageType<imgTy, aQual>], Attr.Const>;
1346       }
1347       def : Builtin<"get_image_dim", [VectorType<Int, 2>, ImageType<imgTy, aQual>], Attr.Const>;
1348     }
1349     def : Builtin<"get_image_array_size", [Size, ImageType<Image2dArrayMsaaDepth, aQual>], Attr.Const>;
1350   }
1351 }