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
22class Version<int _Version> {
23  int ID = _Version;
24}
25def CLAll : Version<  0>;
26def CL10  : Version<100>;
27def CL11  : Version<110>;
28def CL12  : Version<120>;
29def CL20  : Version<200>;
30
31// Address spaces
32// Pointer types need to be assigned an address space.
33class AddressSpace<string _AS> {
34  string Name = _AS;
35}
36def DefaultAS    : AddressSpace<"clang::LangAS::Default">;
37def PrivateAS    : AddressSpace<"clang::LangAS::opencl_private">;
38def GlobalAS     : AddressSpace<"clang::LangAS::opencl_global">;
39def ConstantAS   : AddressSpace<"clang::LangAS::opencl_constant">;
40def LocalAS      : AddressSpace<"clang::LangAS::opencl_local">;
41def GenericAS    : AddressSpace<"clang::LangAS::opencl_generic">;
42
43// OpenCL language extension.
44class 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.
51class FunctionExtension<string _Ext> : AbstractExtension<_Ext>;
52
53// Extension associated to a type.  This enables implicit conditionalization of
54// builtin function overloads containing a type that depends on an extension.
55// During overload resolution, when a builtin function overload contains a type
56// with a TypeExtension, those overloads are skipped when the extension is
57// disabled.
58class TypeExtension<string _Ext> : AbstractExtension<_Ext>;
59
60// TypeExtension definitions.
61def NoTypeExt   : TypeExtension<"">;
62def Fp16TypeExt : TypeExtension<"cl_khr_fp16">;
63def Fp64TypeExt : TypeExtension<"cl_khr_fp64">;
64
65// FunctionExtension definitions.
66def FuncExtNone                          : FunctionExtension<"">;
67def FuncExtKhrSubgroups                  : FunctionExtension<"cl_khr_subgroups">;
68def FuncExtKhrSubgroupExtendedTypes      : FunctionExtension<"cl_khr_subgroup_extended_types">;
69def FuncExtKhrSubgroupNonUniformVote     : FunctionExtension<"cl_khr_subgroup_non_uniform_vote">;
70def FuncExtKhrSubgroupBallot             : FunctionExtension<"cl_khr_subgroup_ballot">;
71def FuncExtKhrSubgroupNonUniformArithmetic: FunctionExtension<"cl_khr_subgroup_non_uniform_arithmetic">;
72def FuncExtKhrSubgroupShuffle            : FunctionExtension<"cl_khr_subgroup_shuffle">;
73def FuncExtKhrSubgroupShuffleRelative    : FunctionExtension<"cl_khr_subgroup_shuffle_relative">;
74def FuncExtKhrSubgroupClusteredReduce    : FunctionExtension<"cl_khr_subgroup_clustered_reduce">;
75def FuncExtKhrExtendedBitOps             : FunctionExtension<"cl_khr_extended_bit_ops">;
76def FuncExtKhrGlobalInt32BaseAtomics     : FunctionExtension<"cl_khr_global_int32_base_atomics">;
77def FuncExtKhrGlobalInt32ExtendedAtomics : FunctionExtension<"cl_khr_global_int32_extended_atomics">;
78def FuncExtKhrLocalInt32BaseAtomics      : FunctionExtension<"cl_khr_local_int32_base_atomics">;
79def FuncExtKhrLocalInt32ExtendedAtomics  : FunctionExtension<"cl_khr_local_int32_extended_atomics">;
80def FuncExtKhrInt64BaseAtomics           : FunctionExtension<"cl_khr_int64_base_atomics">;
81def FuncExtKhrInt64ExtendedAtomics       : FunctionExtension<"cl_khr_int64_extended_atomics">;
82def FuncExtKhrMipmapImage                : FunctionExtension<"cl_khr_mipmap_image">;
83def FuncExtKhrMipmapImageWrites          : FunctionExtension<"cl_khr_mipmap_image_writes">;
84def FuncExtKhrGlMsaaSharing              : FunctionExtension<"cl_khr_gl_msaa_sharing">;
85
86// Not a real extension, but a workaround to add C++ for OpenCL specific builtins.
87def FuncExtOpenCLCxx                     : FunctionExtension<"__cplusplus">;
88
89// Multiple extensions
90def FuncExtKhrMipmapWritesAndWrite3d     : FunctionExtension<"cl_khr_mipmap_image_writes cl_khr_3d_image_writes">;
91
92// Arm extensions.
93def ArmIntegerDotProductInt8                   : FunctionExtension<"cl_arm_integer_dot_product_int8">;
94def ArmIntegerDotProductAccumulateInt8         : FunctionExtension<"cl_arm_integer_dot_product_accumulate_int8">;
95def ArmIntegerDotProductAccumulateInt16        : FunctionExtension<"cl_arm_integer_dot_product_accumulate_int16">;
96def ArmIntegerDotProductAccumulateSaturateInt8 : FunctionExtension<"cl_arm_integer_dot_product_accumulate_saturate_int8">;
97
98// Qualified Type.  These map to ASTContext::QualType.
99class QualType<string _TypeExpr, bit _IsAbstract=0> {
100  // Expression to obtain the QualType inside OCL2Qual.
101  // E.g. TypeExpr="Context.IntTy" for the int type.
102  string TypeExpr = _TypeExpr;
103  // Some QualTypes in this file represent an abstract type for which there is
104  // no corresponding AST QualType, e.g. a GenType or an `image2d_t` type
105  // without access qualifiers.
106  bit IsAbstract = _IsAbstract;
107}
108
109// List of integers.
110class IntList<string _Name, list<int> _List> {
111  string Name = _Name;
112  list<int> List = _List;
113}
114
115//===----------------------------------------------------------------------===//
116//                      OpenCL C classes for types
117//===----------------------------------------------------------------------===//
118// OpenCL C basic data types (int, float, image2d_t, ...).
119// Its child classes can represent concrete types (e.g. VectorType) or
120// abstract types (e.g. GenType).
121class Type<string _Name, QualType _QTExpr> {
122  // Name of the Type.
123  string Name = _Name;
124  // QualType associated with this type.
125  QualType QTExpr = _QTExpr;
126  // Size of the vector (if applicable).
127  int VecWidth = 1;
128  // Is a pointer.
129  bit IsPointer = 0;
130  // "const" qualifier.
131  bit IsConst = 0;
132  // "volatile" qualifier.
133  bit IsVolatile = 0;
134  // Access qualifier. Must be one of ("RO", "WO", "RW").
135  string AccessQualifier = "";
136  // Address space.
137  string AddrSpace = DefaultAS.Name;
138  // Extension that needs to be enabled to expose a builtin that uses this type.
139  TypeExtension Extension = NoTypeExt;
140}
141
142// OpenCL vector types (e.g. int2, int3, int16, float8, ...).
143class VectorType<Type _Ty, int _VecWidth> : Type<_Ty.Name, _Ty.QTExpr> {
144  let VecWidth = _VecWidth;
145  let AccessQualifier = "";
146  // Inherited fields
147  let IsPointer = _Ty.IsPointer;
148  let IsConst = _Ty.IsConst;
149  let IsVolatile = _Ty.IsVolatile;
150  let AddrSpace = _Ty.AddrSpace;
151  let Extension = _Ty.Extension;
152}
153
154// OpenCL pointer types (e.g. int*, float*, ...).
155class PointerType<Type _Ty, AddressSpace _AS = DefaultAS> :
156    Type<_Ty.Name, _Ty.QTExpr> {
157  let AddrSpace = _AS.Name;
158  // Inherited fields
159  let VecWidth = _Ty.VecWidth;
160  let IsPointer = 1;
161  let IsConst = _Ty.IsConst;
162  let IsVolatile = _Ty.IsVolatile;
163  let AccessQualifier = _Ty.AccessQualifier;
164  let Extension = _Ty.Extension;
165}
166
167// OpenCL const types (e.g. const int).
168class ConstType<Type _Ty> : Type<_Ty.Name, _Ty.QTExpr> {
169  let IsConst = 1;
170  // Inherited fields
171  let VecWidth = _Ty.VecWidth;
172  let IsPointer = _Ty.IsPointer;
173  let IsVolatile = _Ty.IsVolatile;
174  let AccessQualifier = _Ty.AccessQualifier;
175  let AddrSpace = _Ty.AddrSpace;
176  let Extension = _Ty.Extension;
177}
178
179// OpenCL volatile types (e.g. volatile int).
180class VolatileType<Type _Ty> : Type<_Ty.Name, _Ty.QTExpr> {
181  let IsVolatile = 1;
182  // Inherited fields
183  let VecWidth = _Ty.VecWidth;
184  let IsPointer = _Ty.IsPointer;
185  let IsConst = _Ty.IsConst;
186  let AccessQualifier = _Ty.AccessQualifier;
187  let AddrSpace = _Ty.AddrSpace;
188  let Extension = _Ty.Extension;
189}
190
191// OpenCL image types (e.g. image2d).
192class ImageType<Type _Ty, string _AccessQualifier> :
193    Type<_Ty.Name, QualType<_Ty.QTExpr.TypeExpr # _AccessQualifier # "Ty", 0>> {
194  let VecWidth = 0;
195  let AccessQualifier = _AccessQualifier;
196  // Inherited fields
197  let IsPointer = _Ty.IsPointer;
198  let IsConst = _Ty.IsConst;
199  let IsVolatile = _Ty.IsVolatile;
200  let AddrSpace = _Ty.AddrSpace;
201  let Extension = _Ty.Extension;
202}
203
204// OpenCL enum type (e.g. memory_scope).
205class EnumType<string _Name> :
206    Type<_Name, QualType<"getOpenCLEnumType(S, \"" # _Name # "\")", 0>> {
207}
208
209// OpenCL typedef type (e.g. cl_mem_fence_flags).
210class TypedefType<string _Name> :
211    Type<_Name, QualType<"getOpenCLTypedefType(S, \"" # _Name # "\")", 0>> {
212}
213
214// List of Types.
215class TypeList<list<Type> _Type> {
216  list<Type> List = _Type;
217}
218
219// A GenericType is an abstract type that defines a set of types as a
220// combination of Types and vector sizes.
221//
222// For example, if TypeList = <int, float> and VectorList = <1, 2, 4>, then it
223// represents <int, int2, int4, float, float2, float4>.
224//
225// Some rules apply when using multiple GenericType arguments in a declaration:
226//   1. The number of vector sizes must be equal or 1 for all gentypes in a
227//      declaration.
228//   2. The number of Types must be equal or 1 for all gentypes in a
229//      declaration.
230//   3. Generic types are combined by iterating over all generic types at once.
231//      For example, for the following GenericTypes
232//        GenT1 = GenericType<half, [1, 2]> and
233//        GenT2 = GenericType<float, int, [1, 2]>
234//      A declaration f(GenT1, GenT2) results in the combinations
235//        f(half, float), f(half2, float2), f(half, int), f(half2, int2) .
236//   4. "sgentype" from the OpenCL specification is supported by specifying
237//      a single vector size.
238//      For example, for the following GenericTypes
239//        GenT = GenericType<half, int, [1, 2]> and
240//        SGenT = GenericType<half, int, [1]>
241//      A declaration f(GenT, SGenT) results in the combinations
242//        f(half, half), f(half2, half), f(int, int), f(int2, int) .
243class GenericType<string _Ty, TypeList _TypeList, IntList _VectorList> :
244    Type<_Ty, QualType<"null", 1>> {
245  // Possible element types of the generic type.
246  TypeList TypeList = _TypeList;
247  // Possible vector sizes of the types in the TypeList.
248  IntList VectorList = _VectorList;
249  // The VecWidth field is ignored for GenericTypes. Use VectorList instead.
250  let VecWidth = 0;
251}
252
253// Builtin function attributes.
254def Attr {
255  list<bit> None = [0, 0, 0];
256  list<bit> Pure = [1, 0, 0];
257  list<bit> Const = [0, 1, 0];
258  list<bit> Convergent = [0, 0, 1];
259}
260
261//===----------------------------------------------------------------------===//
262//                      OpenCL C class for builtin functions
263//===----------------------------------------------------------------------===//
264class Builtin<string _Name, list<Type> _Signature, list<bit> _Attributes = Attr.None> {
265  // Name of the builtin function
266  string Name = _Name;
267  // List of types used by the function. The first one is the return type and
268  // the following are the arguments. The list must have at least one element
269  // (the return type).
270  list<Type> Signature = _Signature;
271  // Function attribute __attribute__((pure))
272  bit IsPure = _Attributes[0];
273  // Function attribute __attribute__((const))
274  bit IsConst = _Attributes[1];
275  // Function attribute __attribute__((convergent))
276  bit IsConv = _Attributes[2];
277  // OpenCL extensions to which the function belongs.
278  FunctionExtension Extension = FuncExtNone;
279  // Version of OpenCL from which the function is available (e.g.: CL10).
280  // MinVersion is inclusive.
281  Version MinVersion = CL10;
282  // Version of OpenCL from which the function is not supported anymore.
283  // MaxVersion is exclusive.
284  // CLAll makes the function available for all versions.
285  Version MaxVersion = CLAll;
286}
287
288//===----------------------------------------------------------------------===//
289//                 Definitions of OpenCL C types
290//===----------------------------------------------------------------------===//
291
292// OpenCL v1.0/1.2/2.0 s6.1.1: Built-in Scalar Data Types.
293def Bool      : Type<"bool",      QualType<"Context.BoolTy">>;
294def Char      : Type<"char",      QualType<"Context.CharTy">>;
295def UChar     : Type<"uchar",     QualType<"Context.UnsignedCharTy">>;
296def Short     : Type<"short",     QualType<"Context.ShortTy">>;
297def UShort    : Type<"ushort",    QualType<"Context.UnsignedShortTy">>;
298def Int       : Type<"int",       QualType<"Context.IntTy">>;
299def UInt      : Type<"uint",      QualType<"Context.UnsignedIntTy">>;
300def Long      : Type<"long",      QualType<"Context.LongTy">>;
301def ULong     : Type<"ulong",     QualType<"Context.UnsignedLongTy">>;
302def Float     : Type<"float",     QualType<"Context.FloatTy">>;
303let Extension = Fp64TypeExt in {
304  def Double    : Type<"double",    QualType<"Context.DoubleTy">>;
305}
306let Extension = Fp16TypeExt in {
307  def Half      : Type<"half",      QualType<"Context.HalfTy">>;
308}
309def Size      : Type<"size_t",    QualType<"Context.getSizeType()">>;
310def PtrDiff   : Type<"ptrdiff_t", QualType<"Context.getPointerDiffType()">>;
311def IntPtr    : Type<"intptr_t",  QualType<"Context.getIntPtrType()">>;
312def UIntPtr   : Type<"uintptr_t", QualType<"Context.getUIntPtrType()">>;
313def Void      : Type<"void",      QualType<"Context.VoidTy">>;
314
315// OpenCL v1.0/1.2/2.0 s6.1.2: Built-in Vector Data Types.
316// Built-in vector data types are created by TableGen's OpenCLBuiltinEmitter.
317
318// OpenCL v1.0/1.2/2.0 s6.1.3: Other Built-in Data Types.
319// The image definitions are "abstract".  They should not be used without
320// specifying an access qualifier (RO/WO/RW).
321def Image1d               : Type<"image1d_t", QualType<"Context.OCLImage1d", 1>>;
322def Image2d               : Type<"image2d_t", QualType<"Context.OCLImage2d", 1>>;
323def Image3d               : Type<"image3d_t", QualType<"Context.OCLImage3d", 1>>;
324def Image1dArray          : Type<"image1d_array_t", QualType<"Context.OCLImage1dArray", 1>>;
325def Image1dBuffer         : Type<"image1d_buffer_t", QualType<"Context.OCLImage1dBuffer", 1>>;
326def Image2dArray          : Type<"image2d_array_t", QualType<"Context.OCLImage2dArray", 1>>;
327def Image2dDepth          : Type<"image2d_depth_t", QualType<"Context.OCLImage2dDepth", 1>>;
328def Image2dArrayDepth     : Type<"image2d_array_depth_t", QualType<"Context.OCLImage2dArrayDepth", 1>>;
329def Image2dMsaa           : Type<"image2d_msaa_t", QualType<"Context.OCLImage2dMSAA", 1>>;
330def Image2dArrayMsaa      : Type<"image2d_array_msaa_t", QualType<"Context.OCLImage2dArrayMSAA", 1>>;
331def Image2dMsaaDepth      : Type<"image2d_msaa_depth_t", QualType<"Context.OCLImage2dMSAADepth", 1>>;
332def Image2dArrayMsaaDepth : Type<"image2d_array_msaa_depth_t", QualType<"Context.OCLImage2dArrayMSAADepth", 1>>;
333
334def Sampler               : Type<"sampler_t", QualType<"Context.OCLSamplerTy">>;
335def ClkEvent              : Type<"clk_event_t", QualType<"Context.OCLClkEventTy">>;
336def Event                 : Type<"event_t", QualType<"Context.OCLEventTy">>;
337def Queue                 : Type<"queue_t", QualType<"Context.OCLQueueTy">>;
338def ReserveId             : Type<"reserve_id_t", QualType<"Context.OCLReserveIDTy">>;
339def MemFenceFlags         : TypedefType<"cl_mem_fence_flags">;
340def ClkProfilingInfo      : TypedefType<"clk_profiling_info">;
341def NDRange               : TypedefType<"ndrange_t">;
342
343// OpenCL v2.0 s6.13.11: Atomic integer and floating-point types.
344def AtomicInt             : Type<"atomic_int", QualType<"Context.getAtomicType(Context.IntTy)">>;
345def AtomicUInt            : Type<"atomic_uint", QualType<"Context.getAtomicType(Context.UnsignedIntTy)">>;
346def AtomicLong            : Type<"atomic_long", QualType<"Context.getAtomicType(Context.LongTy)">>;
347def AtomicULong           : Type<"atomic_ulong", QualType<"Context.getAtomicType(Context.UnsignedLongTy)">>;
348def AtomicFloat           : Type<"atomic_float", QualType<"Context.getAtomicType(Context.FloatTy)">>;
349def AtomicDouble          : Type<"atomic_double", QualType<"Context.getAtomicType(Context.DoubleTy)">>;
350def AtomicIntPtr          : Type<"atomic_intptr_t", QualType<"Context.getAtomicType(Context.getIntPtrType())">>;
351def AtomicUIntPtr         : Type<"atomic_uintptr_t", QualType<"Context.getAtomicType(Context.getUIntPtrType())">>;
352def AtomicSize            : Type<"atomic_size_t", QualType<"Context.getAtomicType(Context.getSizeType())">>;
353def AtomicPtrDiff         : Type<"atomic_ptrdiff_t", QualType<"Context.getAtomicType(Context.getPointerDiffType())">>;
354
355def AtomicFlag            : TypedefType<"atomic_flag">;
356def MemoryOrder           : EnumType<"memory_order">;
357def MemoryScope           : EnumType<"memory_scope">;
358
359//===----------------------------------------------------------------------===//
360//                 Definitions of OpenCL gentype variants
361//===----------------------------------------------------------------------===//
362// The OpenCL specification often uses "gentype" in builtin function
363// declarations to indicate that a builtin function is available with various
364// argument and return types.  The types represented by "gentype" vary between
365// different parts of the specification.  The following definitions capture
366// the different type lists for gentypes in different parts of the
367// specification.
368
369// Vector width lists.
370def VecAndScalar: IntList<"VecAndScalar", [1, 2, 3, 4, 8, 16]>;
371def VecNoScalar : IntList<"VecNoScalar", [2, 3, 4, 8, 16]>;
372def Vec1        : IntList<"Vec1", [1]>;
373def Vec1234     : IntList<"Vec1234", [1, 2, 3, 4]>;
374
375// Type lists.
376def TLAll           : TypeList<[Char,  UChar, Short,  UShort, Int,  UInt, Long,  ULong, Float, Double, Half]>;
377def TLFloat         : TypeList<[Float, Double, Half]>;
378def TLSignedInts    : TypeList<[Char, Short, Int, Long]>;
379def TLUnsignedInts  : TypeList<[UChar, UShort, UInt, ULong]>;
380
381def TLIntLongFloats : TypeList<[Int, UInt, Long, ULong, Float, Double, Half]>;
382
383// All unsigned integer types twice, to facilitate unsigned return types for e.g.
384// uchar abs(char) and
385// uchar abs(uchar).
386def TLAllUIntsTwice : TypeList<[UChar, UChar, UShort, UShort, UInt, UInt, ULong, ULong]>;
387
388def TLAllInts       : TypeList<[Char, UChar, Short, UShort, Int, UInt, Long, ULong]>;
389
390// GenType definitions for multiple base types (e.g. all floating point types,
391// or all integer types).
392// All types
393def AGenType1              : GenericType<"AGenType1", TLAll, Vec1>;
394def AGenTypeN              : GenericType<"AGenTypeN", TLAll, VecAndScalar>;
395def AGenTypeNNoScalar      : GenericType<"AGenTypeNNoScalar", TLAll, VecNoScalar>;
396// All integer
397def AIGenType1             : GenericType<"AIGenType1", TLAllInts, Vec1>;
398def AIGenTypeN             : GenericType<"AIGenTypeN", TLAllInts, VecAndScalar>;
399def AIGenTypeNNoScalar     : GenericType<"AIGenTypeNNoScalar", TLAllInts, VecNoScalar>;
400// All integer to unsigned
401def AI2UGenTypeN           : GenericType<"AI2UGenTypeN", TLAllUIntsTwice, VecAndScalar>;
402// Signed integer
403def SGenTypeN              : GenericType<"SGenTypeN", TLSignedInts, VecAndScalar>;
404// Unsigned integer
405def UGenTypeN              : GenericType<"UGenTypeN", TLUnsignedInts, VecAndScalar>;
406// Float
407def FGenTypeN              : GenericType<"FGenTypeN", TLFloat, VecAndScalar>;
408// (u)int, (u)long, and all floats
409def IntLongFloatGenType1   : GenericType<"IntLongFloatGenType1", TLIntLongFloats, Vec1>;
410// (u)char and (u)short
411def CharShortGenType1      : GenericType<"CharShortGenType1",
412                                 TypeList<[Char, UChar, Short, UShort]>, Vec1>;
413
414// GenType definitions for every single base type (e.g. fp32 only).
415// Names are like: GenTypeFloatVecAndScalar.
416foreach Type = [Char, UChar, Short, UShort,
417                Int, UInt, Long, ULong,
418                Float, Double, Half] in {
419  foreach VecSizes = [VecAndScalar, VecNoScalar] in {
420    def "GenType" # Type # VecSizes :
421              GenericType<"GenType" # Type # VecSizes,
422                          TypeList<[Type]>, VecSizes>;
423  }
424}
425
426// GenType definitions for vec1234.
427foreach Type = [Float, Double, Half] in {
428  def "GenType" # Type # Vec1234 :
429              GenericType<"GenType" # Type # Vec1234,
430                          TypeList<[Type]>, Vec1234>;
431}
432
433
434//===----------------------------------------------------------------------===//
435//                 Definitions of OpenCL builtin functions
436//===----------------------------------------------------------------------===//
437//--------------------------------------------------------------------
438// OpenCL v1.1/1.2/2.0 s6.2.3 - Explicit conversions.
439// OpenCL v2.0 Extensions s5.1.1 and s6.1.1 - Conversions.
440
441// Generate the convert_* builtins functions.
442foreach RType = [Float, Double, Half, Char, UChar, Short,
443                 UShort, Int, UInt, Long, ULong] in {
444  foreach IType = [Float, Double, Half, Char, UChar, Short,
445                   UShort, Int, UInt, Long, ULong] in {
446    // Conversions to integer type have a sat and non-sat variant.
447    foreach sat = !cond(!eq(RType.Name, "float") : [""],
448                        !eq(RType.Name, "double") : [""],
449                        !eq(RType.Name, "half") : [""],
450                        1 : ["", "_sat"]) in {
451      foreach rnd = ["", "_rte", "_rtn", "_rtp", "_rtz"] in {
452        def : Builtin<"convert_" # RType.Name # sat # rnd, [RType, IType],
453                      Attr.Const>;
454        foreach v = [2, 3, 4, 8, 16] in {
455          def : Builtin<"convert_" # RType.Name # v # sat # rnd,
456                        [VectorType<RType, v>, VectorType<IType, v>],
457                        Attr.Const>;
458        }
459      }
460    }
461  }
462}
463
464//--------------------------------------------------------------------
465// OpenCL v1.1 s6.11.1, v1.2 s6.12.1, v2.0 s6.13.1 - Work-item Functions
466// --- Table 7 ---
467def : Builtin<"get_work_dim", [UInt], Attr.Const>;
468foreach name = ["get_global_size", "get_global_id", "get_local_size",
469                "get_local_id", "get_num_groups", "get_group_id",
470                "get_global_offset"] in {
471  def : Builtin<name, [Size, UInt], Attr.Const>;
472}
473
474let MinVersion = CL20 in {
475  def : Builtin<"get_enqueued_local_size", [Size, UInt]>;
476  foreach name = ["get_global_linear_id", "get_local_linear_id"] in {
477    def : Builtin<name, [Size]>;
478  }
479}
480
481
482//--------------------------------------------------------------------
483// OpenCL v1.1 s6.11.2, v1.2 s6.12.2, v2.0 s6.13.2 - Math functions
484// OpenCL Extension v2.0 s5.1.2 and s6.1.2 - Math Functions
485// --- Table 8 ---
486// --- 1 argument ---
487foreach name = ["acos", "acosh", "acospi",
488                "asin", "asinh", "asinpi",
489                "atan", "atanh", "atanpi",
490                "cbrt", "ceil",
491                "cos", "cosh", "cospi",
492                "erfc", "erf",
493                "exp", "exp2", "exp10", "expm1",
494                "fabs", "floor",
495                "log", "log2", "log10", "log1p", "logb",
496                "rint", "round", "rsqrt",
497                "sin", "sinh", "sinpi",
498                "sqrt",
499                "tan", "tanh", "tanpi",
500                "tgamma", "trunc",
501                "lgamma"] in {
502    def : Builtin<name, [FGenTypeN, FGenTypeN], Attr.Const>;
503}
504foreach name = ["nan"] in {
505  def : Builtin<name, [GenTypeFloatVecAndScalar, GenTypeUIntVecAndScalar], Attr.Const>;
506  def : Builtin<name, [GenTypeDoubleVecAndScalar, GenTypeULongVecAndScalar], Attr.Const>;
507  def : Builtin<name, [GenTypeHalfVecAndScalar, GenTypeUShortVecAndScalar], Attr.Const>;
508}
509
510// --- 2 arguments ---
511foreach name = ["atan2", "atan2pi", "copysign", "fdim", "fmod", "hypot",
512                "maxmag", "minmag", "nextafter", "pow", "powr",
513                "remainder"] in {
514  def : Builtin<name, [FGenTypeN, FGenTypeN, FGenTypeN], Attr.Const>;
515}
516foreach name = ["fmax", "fmin"] in {
517  def : Builtin<name, [FGenTypeN, FGenTypeN, FGenTypeN], Attr.Const>;
518  def : Builtin<name, [GenTypeFloatVecNoScalar, GenTypeFloatVecNoScalar, Float], Attr.Const>;
519  def : Builtin<name, [GenTypeDoubleVecNoScalar, GenTypeDoubleVecNoScalar, Double], Attr.Const>;
520  def : Builtin<name, [GenTypeHalfVecNoScalar, GenTypeHalfVecNoScalar, Half], Attr.Const>;
521}
522foreach name = ["ilogb"] in {
523  def : Builtin<name, [GenTypeIntVecAndScalar, GenTypeFloatVecAndScalar], Attr.Const>;
524  def : Builtin<name, [GenTypeIntVecAndScalar, GenTypeDoubleVecAndScalar], Attr.Const>;
525  def : Builtin<name, [GenTypeIntVecAndScalar, GenTypeHalfVecAndScalar], Attr.Const>;
526}
527foreach name = ["ldexp"] in {
528  def : Builtin<name, [GenTypeFloatVecAndScalar, GenTypeFloatVecAndScalar, GenTypeIntVecAndScalar], Attr.Const>;
529  def : Builtin<name, [GenTypeFloatVecNoScalar, GenTypeFloatVecNoScalar, Int], Attr.Const>;
530  def : Builtin<name, [GenTypeDoubleVecAndScalar, GenTypeDoubleVecAndScalar, GenTypeIntVecAndScalar], Attr.Const>;
531  def : Builtin<name, [GenTypeDoubleVecNoScalar, GenTypeDoubleVecNoScalar, Int], Attr.Const>;
532  def : Builtin<name, [GenTypeHalfVecAndScalar, GenTypeHalfVecAndScalar, GenTypeIntVecAndScalar], Attr.Const>;
533  def : Builtin<name, [GenTypeHalfVecNoScalar, GenTypeHalfVecNoScalar, Int], Attr.Const>;
534}
535foreach name = ["pown", "rootn"] in {
536  def : Builtin<name, [GenTypeFloatVecAndScalar, GenTypeFloatVecAndScalar, GenTypeIntVecAndScalar], Attr.Const>;
537  def : Builtin<name, [GenTypeDoubleVecAndScalar, GenTypeDoubleVecAndScalar, GenTypeIntVecAndScalar], Attr.Const>;
538  def : Builtin<name, [GenTypeHalfVecAndScalar, GenTypeHalfVecAndScalar, GenTypeIntVecAndScalar], Attr.Const>;
539}
540
541// --- 3 arguments ---
542foreach name = ["fma", "mad"] in {
543  def : Builtin<name, [FGenTypeN, FGenTypeN, FGenTypeN, FGenTypeN], Attr.Const>;
544}
545
546// --- Version dependent ---
547let MaxVersion = CL20 in {
548  foreach AS = [GlobalAS, LocalAS, PrivateAS] in {
549    foreach name = ["fract", "modf", "sincos"] in {
550      def : Builtin<name, [FGenTypeN, FGenTypeN, PointerType<FGenTypeN, AS>]>;
551    }
552    foreach name = ["frexp", "lgamma_r"] in {
553      foreach Type = [GenTypeFloatVecAndScalar, GenTypeDoubleVecAndScalar, GenTypeHalfVecAndScalar] in {
554        def : Builtin<name, [Type, Type, PointerType<GenTypeIntVecAndScalar, AS>]>;
555      }
556    }
557    foreach name = ["remquo"] in {
558      foreach Type = [GenTypeFloatVecAndScalar, GenTypeDoubleVecAndScalar, GenTypeHalfVecAndScalar] in {
559        def : Builtin<name, [Type, Type, Type, PointerType<GenTypeIntVecAndScalar, AS>]>;
560      }
561    }
562  }
563}
564let MinVersion = CL20 in {
565  foreach name = ["fract", "modf", "sincos"] in {
566    def : Builtin<name, [FGenTypeN, FGenTypeN, PointerType<FGenTypeN, GenericAS>]>;
567  }
568  foreach name = ["frexp", "lgamma_r"] in {
569    foreach Type = [GenTypeFloatVecAndScalar, GenTypeDoubleVecAndScalar, GenTypeHalfVecAndScalar] in {
570      def : Builtin<name, [Type, Type, PointerType<GenTypeIntVecAndScalar, GenericAS>]>;
571    }  }
572  foreach name = ["remquo"] in {
573    foreach Type = [GenTypeFloatVecAndScalar, GenTypeDoubleVecAndScalar, GenTypeHalfVecAndScalar] in {
574      def : Builtin<name, [Type, Type, Type, PointerType<GenTypeIntVecAndScalar, GenericAS>]>;
575    }
576  }
577}
578
579// --- Table 9 ---
580foreach name = ["half_cos",
581                "half_exp", "half_exp2", "half_exp10",
582                "half_log", "half_log2", "half_log10",
583                "half_recip", "half_rsqrt",
584                "half_sin", "half_sqrt", "half_tan",
585                "native_cos",
586                "native_exp", "native_exp2", "native_exp10",
587                "native_log", "native_log2", "native_log10",
588                "native_recip", "native_rsqrt",
589                "native_sin", "native_sqrt", "native_tan"] in {
590  def : Builtin<name, [GenTypeFloatVecAndScalar, GenTypeFloatVecAndScalar], Attr.Const>;
591}
592foreach name = ["half_divide", "half_powr",
593                "native_divide", "native_powr"] in {
594  def : Builtin<name, [GenTypeFloatVecAndScalar, GenTypeFloatVecAndScalar, GenTypeFloatVecAndScalar], Attr.Const>;
595}
596
597//--------------------------------------------------------------------
598// OpenCL v1.1 s6.11.3, v1.2 s6.12.3, v2.0 s6.13.3 - Integer Functions
599// --- Table 10 ---
600// --- 1 argument ---
601foreach name = ["abs"] in {
602  def : Builtin<name, [AI2UGenTypeN, AIGenTypeN], Attr.Const>;
603}
604def : Builtin<"clz", [AIGenTypeN, AIGenTypeN], Attr.Const>;
605let MinVersion = CL12 in {
606  def : Builtin<"popcount", [AIGenTypeN, AIGenTypeN], Attr.Const>;
607}
608let MinVersion = CL20 in {
609  foreach name = ["ctz"] in {
610    def : Builtin<name, [AIGenTypeN, AIGenTypeN], Attr.Const>;
611  }
612}
613
614// --- 2 arguments ---
615foreach name = ["abs_diff"] in {
616  def : Builtin<name, [AI2UGenTypeN, AIGenTypeN, AIGenTypeN], Attr.Const>;
617}
618foreach name = ["add_sat", "hadd", "rhadd", "mul_hi", "rotate", "sub_sat"] in {
619  def : Builtin<name, [AIGenTypeN, AIGenTypeN, AIGenTypeN], Attr.Const>;
620}
621foreach name = ["max", "min"] in {
622  def : Builtin<name, [AIGenTypeN, AIGenTypeN, AIGenTypeN], Attr.Const>;
623  def : Builtin<name, [AIGenTypeNNoScalar, AIGenTypeNNoScalar, AIGenType1], Attr.Const>;
624}
625foreach name = ["upsample"] in {
626  def : Builtin<name, [GenTypeShortVecAndScalar, GenTypeCharVecAndScalar, GenTypeUCharVecAndScalar], Attr.Const>;
627  def : Builtin<name, [GenTypeUShortVecAndScalar, GenTypeUCharVecAndScalar, GenTypeUCharVecAndScalar], Attr.Const>;
628  def : Builtin<name, [GenTypeIntVecAndScalar, GenTypeShortVecAndScalar, GenTypeUShortVecAndScalar], Attr.Const>;
629  def : Builtin<name, [GenTypeUIntVecAndScalar, GenTypeUShortVecAndScalar, GenTypeUShortVecAndScalar], Attr.Const>;
630  def : Builtin<name, [GenTypeLongVecAndScalar, GenTypeIntVecAndScalar, GenTypeUIntVecAndScalar], Attr.Const>;
631  def : Builtin<name, [GenTypeULongVecAndScalar, GenTypeUIntVecAndScalar, GenTypeUIntVecAndScalar], Attr.Const>;
632}
633
634// --- 3 arguments ---
635foreach name = ["clamp"] in {
636  def : Builtin<name, [AIGenTypeN, AIGenTypeN, AIGenTypeN, AIGenTypeN], Attr.Const>;
637  def : Builtin<name, [AIGenTypeNNoScalar, AIGenTypeNNoScalar, AIGenType1, AIGenType1], Attr.Const>;
638}
639foreach name = ["mad_hi", "mad_sat"] in {
640  def : Builtin<name, [AIGenTypeN, AIGenTypeN, AIGenTypeN, AIGenTypeN], Attr.Const>;
641}
642
643// --- Table 11 ---
644foreach name = ["mad24"] in {
645  def : Builtin<name, [GenTypeIntVecAndScalar, GenTypeIntVecAndScalar, GenTypeIntVecAndScalar, GenTypeIntVecAndScalar], Attr.Const>;
646  def : Builtin<name, [GenTypeUIntVecAndScalar, GenTypeUIntVecAndScalar, GenTypeUIntVecAndScalar, GenTypeUIntVecAndScalar], Attr.Const>;
647}
648foreach name = ["mul24"] in {
649  def : Builtin<name, [GenTypeIntVecAndScalar, GenTypeIntVecAndScalar, GenTypeIntVecAndScalar], Attr.Const>;
650  def : Builtin<name, [GenTypeUIntVecAndScalar, GenTypeUIntVecAndScalar, GenTypeUIntVecAndScalar], Attr.Const>;
651}
652
653//--------------------------------------------------------------------
654// OpenCL v1.1 s6.11.4, v1.2 s6.12.4, v2.0 s6.13.4 - Common Functions
655// OpenCL Extension v2.0 s5.1.3 and s6.1.3 - Common Functions
656// --- Table 12 ---
657// --- 1 argument ---
658foreach name = ["degrees", "radians", "sign"] in {
659  def : Builtin<name, [FGenTypeN, FGenTypeN], Attr.Const>;
660}
661
662// --- 2 arguments ---
663foreach name = ["max", "min"] in {
664  def : Builtin<name, [FGenTypeN, FGenTypeN, FGenTypeN], Attr.Const>;
665  def : Builtin<name, [GenTypeFloatVecNoScalar, GenTypeFloatVecNoScalar, Float], Attr.Const>;
666  def : Builtin<name, [GenTypeDoubleVecNoScalar, GenTypeDoubleVecNoScalar, Double], Attr.Const>;
667  def : Builtin<name, [GenTypeHalfVecNoScalar, GenTypeHalfVecNoScalar, Half], Attr.Const>;
668}
669foreach name = ["step"] in {
670  def : Builtin<name, [FGenTypeN, FGenTypeN, FGenTypeN], Attr.Const>;
671  def : Builtin<name, [GenTypeFloatVecNoScalar, Float, GenTypeFloatVecNoScalar], Attr.Const>;
672  def : Builtin<name, [GenTypeDoubleVecNoScalar, Double, GenTypeDoubleVecNoScalar], Attr.Const>;
673  def : Builtin<name, [GenTypeHalfVecNoScalar, Half, GenTypeHalfVecNoScalar], Attr.Const>;
674}
675
676// --- 3 arguments ---
677foreach name = ["clamp"] in {
678  def : Builtin<name, [FGenTypeN, FGenTypeN, FGenTypeN, FGenTypeN], Attr.Const>;
679  def : Builtin<name, [GenTypeFloatVecNoScalar, GenTypeFloatVecNoScalar, Float, Float], Attr.Const>;
680  def : Builtin<name, [GenTypeDoubleVecNoScalar, GenTypeDoubleVecNoScalar, Double, Double], Attr.Const>;
681  def : Builtin<name, [GenTypeHalfVecNoScalar, GenTypeHalfVecNoScalar, Half, Half], Attr.Const>;
682}
683foreach name = ["mix"] in {
684  def : Builtin<name, [FGenTypeN, FGenTypeN, FGenTypeN, FGenTypeN], Attr.Const>;
685  def : Builtin<name, [GenTypeFloatVecNoScalar, GenTypeFloatVecNoScalar, GenTypeFloatVecNoScalar, Float], Attr.Const>;
686  def : Builtin<name, [GenTypeDoubleVecNoScalar, GenTypeDoubleVecNoScalar, GenTypeDoubleVecNoScalar, Double], Attr.Const>;
687  def : Builtin<name, [GenTypeHalfVecNoScalar, GenTypeHalfVecNoScalar, GenTypeHalfVecNoScalar, Half], Attr.Const>;
688}
689foreach name = ["smoothstep"] in {
690  def : Builtin<name, [FGenTypeN, FGenTypeN, FGenTypeN, FGenTypeN], Attr.Const>;
691  def : Builtin<name, [GenTypeFloatVecNoScalar, Float, Float, GenTypeFloatVecNoScalar], Attr.Const>;
692  def : Builtin<name, [GenTypeDoubleVecNoScalar, Double, Double, GenTypeDoubleVecNoScalar], Attr.Const>;
693  def : Builtin<name, [GenTypeHalfVecNoScalar, Half, Half, GenTypeHalfVecNoScalar], Attr.Const>;
694}
695
696
697//--------------------------------------------------------------------
698// OpenCL v1.1 s6.11.5, v1.2 s6.12.5, v2.0 s6.13.5 - Geometric Functions
699// OpenCL Extension v2.0 s5.1.4 and s6.1.4 - Geometric Functions
700// --- Table 13 ---
701// --- 1 argument ---
702foreach name = ["length"] in {
703  def : Builtin<name, [Float, GenTypeFloatVec1234], Attr.Const>;
704  def : Builtin<name, [Double, GenTypeDoubleVec1234], Attr.Const>;
705  def : Builtin<name, [Half, GenTypeHalfVec1234], Attr.Const>;
706}
707foreach name = ["normalize"] in {
708  def : Builtin<name, [GenTypeFloatVec1234, GenTypeFloatVec1234], Attr.Const>;
709  def : Builtin<name, [GenTypeDoubleVec1234, GenTypeDoubleVec1234], Attr.Const>;
710  def : Builtin<name, [GenTypeHalfVec1234, GenTypeHalfVec1234], Attr.Const>;
711}
712foreach name = ["fast_length"] in {
713  def : Builtin<name, [Float, GenTypeFloatVec1234], Attr.Const>;
714}
715foreach name = ["fast_normalize"] in {
716  def : Builtin<name, [GenTypeFloatVec1234, GenTypeFloatVec1234], Attr.Const>;
717}
718
719// --- 2 arguments ---
720foreach name = ["cross"] in {
721  foreach VSize = [3, 4] in {
722    def : Builtin<name, [VectorType<Float, VSize>, VectorType<Float, VSize>, VectorType<Float, VSize>], Attr.Const>;
723    def : Builtin<name, [VectorType<Double, VSize>, VectorType<Double, VSize>, VectorType<Double, VSize>], Attr.Const>;
724    def : Builtin<name, [VectorType<Half, VSize>, VectorType<Half, VSize>, VectorType<Half, VSize>], Attr.Const>;
725  }
726}
727foreach name = ["dot", "distance"] in {
728  def : Builtin<name, [Float, GenTypeFloatVec1234, GenTypeFloatVec1234], Attr.Const>;
729  def : Builtin<name, [Double, GenTypeDoubleVec1234, GenTypeDoubleVec1234], Attr.Const>;
730  def : Builtin<name, [Half, GenTypeHalfVec1234, GenTypeHalfVec1234], Attr.Const>;
731}
732foreach name = ["fast_distance"] in {
733  def : Builtin<name, [Float, GenTypeFloatVec1234, GenTypeFloatVec1234], Attr.Const>;
734}
735
736
737//--------------------------------------------------------------------
738// OpenCL v1.1 s6.11.6, v1.2 s6.12.6, v2.0 s6.13.6 - Relational Functions
739// OpenCL Extension v2.0 s5.1.5 and s6.1.5 - Relational Functions
740// --- Table 14 ---
741// --- 1 argument ---
742foreach name = ["isfinite", "isinf", "isnan", "isnormal", "signbit"] in {
743  def : Builtin<name, [GenTypeIntVecAndScalar, GenTypeFloatVecAndScalar], Attr.Const>;
744  def : Builtin<name, [Int, Double], Attr.Const>;
745  def : Builtin<name, [GenTypeLongVecNoScalar, GenTypeDoubleVecNoScalar], Attr.Const>;
746  def : Builtin<name, [Int, Half], Attr.Const>;
747  def : Builtin<name, [GenTypeShortVecNoScalar, GenTypeHalfVecNoScalar], Attr.Const>;
748}
749foreach name = ["any", "all"] in {
750  def : Builtin<name, [Int, SGenTypeN], Attr.Const>;
751}
752
753// --- 2 arguments ---
754foreach name = ["isequal", "isnotequal", "isgreater", "isgreaterequal",
755                "isless", "islessequal", "islessgreater", "isordered",
756                "isunordered"] in {
757  def : Builtin<name, [GenTypeIntVecAndScalar, GenTypeFloatVecAndScalar, GenTypeFloatVecAndScalar], Attr.Const>;
758  def : Builtin<name, [Int, Double, Double], Attr.Const>;
759  def : Builtin<name, [GenTypeLongVecNoScalar, GenTypeDoubleVecNoScalar, GenTypeDoubleVecNoScalar], Attr.Const>;
760  def : Builtin<name, [Int, Half, Half], Attr.Const>;
761  def : Builtin<name, [GenTypeShortVecNoScalar, GenTypeHalfVecNoScalar, GenTypeHalfVecNoScalar], Attr.Const>;
762}
763
764// --- 3 arguments ---
765foreach name = ["bitselect"] in {
766  def : Builtin<name, [AGenTypeN, AGenTypeN, AGenTypeN, AGenTypeN], Attr.Const>;
767}
768foreach name = ["select"] in {
769  def : Builtin<name, [SGenTypeN, SGenTypeN, SGenTypeN, SGenTypeN], Attr.Const>;
770  def : Builtin<name, [SGenTypeN, SGenTypeN, SGenTypeN, UGenTypeN], Attr.Const>;
771  def : Builtin<name, [UGenTypeN, UGenTypeN, UGenTypeN, UGenTypeN], Attr.Const>;
772  def : Builtin<name, [UGenTypeN, UGenTypeN, UGenTypeN, SGenTypeN], Attr.Const>;
773  def : Builtin<name, [GenTypeFloatVecAndScalar, GenTypeFloatVecAndScalar, GenTypeFloatVecAndScalar, GenTypeIntVecAndScalar], Attr.Const>;
774  def : Builtin<name, [GenTypeFloatVecAndScalar, GenTypeFloatVecAndScalar, GenTypeFloatVecAndScalar, GenTypeUIntVecAndScalar], Attr.Const>;
775  def : Builtin<name, [GenTypeDoubleVecAndScalar, GenTypeDoubleVecAndScalar, GenTypeDoubleVecAndScalar, GenTypeLongVecAndScalar], Attr.Const>;
776  def : Builtin<name, [GenTypeDoubleVecAndScalar, GenTypeDoubleVecAndScalar, GenTypeDoubleVecAndScalar, GenTypeULongVecAndScalar], Attr.Const>;
777  def : Builtin<name, [GenTypeHalfVecAndScalar, GenTypeHalfVecAndScalar, GenTypeHalfVecAndScalar, GenTypeShortVecAndScalar], Attr.Const>;
778  def : Builtin<name, [GenTypeHalfVecAndScalar, GenTypeHalfVecAndScalar, GenTypeHalfVecAndScalar, GenTypeUShortVecAndScalar], Attr.Const>;
779}
780
781
782//--------------------------------------------------------------------
783// OpenCL v1.1 s6.11.7, v1.2 s6.12.7, v2.0 s6.13.7 - Vector Data Load and Store Functions
784// 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
785// --- Table 15 ---
786// Variants for OpenCL versions below 2.0, using pointers to the global, local
787// and private address spaces.
788let MaxVersion = CL20 in {
789  foreach AS = [GlobalAS, LocalAS, PrivateAS] in {
790    foreach VSize = [2, 3, 4, 8, 16] in {
791      foreach name = ["vload" # VSize] in {
792        def : Builtin<name, [VectorType<Char, VSize>, Size, PointerType<ConstType<Char>, AS>]>;
793        def : Builtin<name, [VectorType<UChar, VSize>, Size, PointerType<ConstType<UChar>, AS>]>;
794        def : Builtin<name, [VectorType<Short, VSize>, Size, PointerType<ConstType<Short>, AS>]>;
795        def : Builtin<name, [VectorType<UShort, VSize>, Size, PointerType<ConstType<UShort>, AS>]>;
796        def : Builtin<name, [VectorType<Int, VSize>, Size, PointerType<ConstType<Int>, AS>]>;
797        def : Builtin<name, [VectorType<UInt, VSize>, Size, PointerType<ConstType<UInt>, AS>]>;
798        def : Builtin<name, [VectorType<Long, VSize>, Size, PointerType<ConstType<Long>, AS>]>;
799        def : Builtin<name, [VectorType<ULong, VSize>, Size, PointerType<ConstType<ULong>, AS>]>;
800        def : Builtin<name, [VectorType<Float, VSize>, Size, PointerType<ConstType<Float>, AS>]>;
801        def : Builtin<name, [VectorType<Double, VSize>, Size, PointerType<ConstType<Double>, AS>]>;
802        def : Builtin<name, [VectorType<Half, VSize>, Size, PointerType<ConstType<Half>, AS>]>;
803      }
804      foreach name = ["vstore" # VSize] in {
805        def : Builtin<name, [Void, VectorType<Char, VSize>, Size, PointerType<Char, AS>]>;
806        def : Builtin<name, [Void, VectorType<UChar, VSize>, Size, PointerType<UChar, AS>]>;
807        def : Builtin<name, [Void, VectorType<Short, VSize>, Size, PointerType<Short, AS>]>;
808        def : Builtin<name, [Void, VectorType<UShort, VSize>, Size, PointerType<UShort, AS>]>;
809        def : Builtin<name, [Void, VectorType<Int, VSize>, Size, PointerType<Int, AS>]>;
810        def : Builtin<name, [Void, VectorType<UInt, VSize>, Size, PointerType<UInt, AS>]>;
811        def : Builtin<name, [Void, VectorType<Long, VSize>, Size, PointerType<Long, AS>]>;
812        def : Builtin<name, [Void, VectorType<ULong, VSize>, Size, PointerType<ULong, AS>]>;
813        def : Builtin<name, [Void, VectorType<Float, VSize>, Size, PointerType<Float, AS>]>;
814        def : Builtin<name, [Void, VectorType<Double, VSize>, Size, PointerType<Double, AS>]>;
815        def : Builtin<name, [Void, VectorType<Half, VSize>, Size, PointerType<Half, AS>]>;
816      }
817      foreach name = ["vloada_half" # VSize] in {
818        def : Builtin<name, [VectorType<Float, VSize>, Size, PointerType<ConstType<Half>, AS>]>;
819      }
820      foreach rnd = ["", "_rte", "_rtz", "_rtp", "_rtn"] in {
821        foreach name = ["vstorea_half" # VSize # rnd] in {
822          def : Builtin<name, [Void, VectorType<Float, VSize>, Size, PointerType<Half, AS>]>;
823          def : Builtin<name, [Void, VectorType<Double, VSize>, Size, PointerType<Half, AS>]>;
824        }
825      }
826    }
827  }
828}
829// Variants for OpenCL versions above 2.0, using pointers to the generic
830// address space.
831let MinVersion = CL20 in {
832  foreach VSize = [2, 3, 4, 8, 16] in {
833    foreach name = ["vload" # VSize] in {
834      def : Builtin<name, [VectorType<Char, VSize>, Size, PointerType<ConstType<Char>, GenericAS>]>;
835      def : Builtin<name, [VectorType<UChar, VSize>, Size, PointerType<ConstType<UChar>, GenericAS>]>;
836      def : Builtin<name, [VectorType<Short, VSize>, Size, PointerType<ConstType<Short>, GenericAS>]>;
837      def : Builtin<name, [VectorType<UShort, VSize>, Size, PointerType<ConstType<UShort>, GenericAS>]>;
838      def : Builtin<name, [VectorType<Int, VSize>, Size, PointerType<ConstType<Int>, GenericAS>]>;
839      def : Builtin<name, [VectorType<UInt, VSize>, Size, PointerType<ConstType<UInt>, GenericAS>]>;
840      def : Builtin<name, [VectorType<Long, VSize>, Size, PointerType<ConstType<Long>, GenericAS>]>;
841      def : Builtin<name, [VectorType<ULong, VSize>, Size, PointerType<ConstType<ULong>, GenericAS>]>;
842      def : Builtin<name, [VectorType<Float, VSize>, Size, PointerType<ConstType<Float>, GenericAS>]>;
843      def : Builtin<name, [VectorType<Double, VSize>, Size, PointerType<ConstType<Double>, GenericAS>]>;
844      def : Builtin<name, [VectorType<Half, VSize>, Size, PointerType<ConstType<Half>, GenericAS>]>;
845    }
846    foreach name = ["vstore" # VSize] in {
847      def : Builtin<name, [Void, VectorType<Char, VSize>, Size, PointerType<Char, GenericAS>]>;
848      def : Builtin<name, [Void, VectorType<UChar, VSize>, Size, PointerType<UChar, GenericAS>]>;
849      def : Builtin<name, [Void, VectorType<Short, VSize>, Size, PointerType<Short, GenericAS>]>;
850      def : Builtin<name, [Void, VectorType<UShort, VSize>, Size, PointerType<UShort, GenericAS>]>;
851      def : Builtin<name, [Void, VectorType<Int, VSize>, Size, PointerType<Int, GenericAS>]>;
852      def : Builtin<name, [Void, VectorType<UInt, VSize>, Size, PointerType<UInt, GenericAS>]>;
853      def : Builtin<name, [Void, VectorType<Long, VSize>, Size, PointerType<Long, GenericAS>]>;
854      def : Builtin<name, [Void, VectorType<ULong, VSize>, Size, PointerType<ULong, GenericAS>]>;
855      def : Builtin<name, [Void, VectorType<Float, VSize>, Size, PointerType<Float, GenericAS>]>;
856      def : Builtin<name, [Void, VectorType<Double, VSize>, Size, PointerType<Double, GenericAS>]>;
857      def : Builtin<name, [Void, VectorType<Half, VSize>, Size, PointerType<Half, GenericAS>]>;
858    }
859    foreach name = ["vloada_half" # VSize] in {
860      def : Builtin<name, [VectorType<Float, VSize>, Size, PointerType<ConstType<Half>, GenericAS>]>;
861    }
862    foreach rnd = ["", "_rte", "_rtz", "_rtp", "_rtn"] in {
863      foreach name = ["vstorea_half" # VSize # rnd] in {
864        def : Builtin<name, [Void, VectorType<Float, VSize>, Size, PointerType<Half, GenericAS>]>;
865        def : Builtin<name, [Void, VectorType<Double, VSize>, Size, PointerType<Half, GenericAS>]>;
866      }
867    }
868  }
869}
870// Variants using pointers to the constant address space.
871foreach VSize = [2, 3, 4, 8, 16] in {
872  foreach name = ["vload" # VSize] in {
873    def : Builtin<name, [VectorType<Char, VSize>, Size, PointerType<ConstType<Char>, ConstantAS>]>;
874    def : Builtin<name, [VectorType<UChar, VSize>, Size, PointerType<ConstType<UChar>, ConstantAS>]>;
875    def : Builtin<name, [VectorType<Short, VSize>, Size, PointerType<ConstType<Short>, ConstantAS>]>;
876    def : Builtin<name, [VectorType<UShort, VSize>, Size, PointerType<ConstType<UShort>, ConstantAS>]>;
877    def : Builtin<name, [VectorType<Int, VSize>, Size, PointerType<ConstType<Int>, ConstantAS>]>;
878    def : Builtin<name, [VectorType<UInt, VSize>, Size, PointerType<ConstType<UInt>, ConstantAS>]>;
879    def : Builtin<name, [VectorType<Long, VSize>, Size, PointerType<ConstType<Long>, ConstantAS>]>;
880    def : Builtin<name, [VectorType<ULong, VSize>, Size, PointerType<ConstType<ULong>, ConstantAS>]>;
881    def : Builtin<name, [VectorType<Float, VSize>, Size, PointerType<ConstType<Float>, ConstantAS>]>;
882    def : Builtin<name, [VectorType<Double, VSize>, Size, PointerType<ConstType<Double>, ConstantAS>]>;
883    def : Builtin<name, [VectorType<Half, VSize>, Size, PointerType<ConstType<Half>, ConstantAS>]>;
884  }
885  foreach name = ["vloada_half" # VSize] in {
886    def : Builtin<name, [VectorType<Float, VSize>, Size, PointerType<ConstType<Half>, ConstantAS>]>;
887  }
888}
889let MaxVersion = CL20 in {
890  foreach AS = [GlobalAS, LocalAS, PrivateAS] in {
891    def : Builtin<"vload_half", [Float, Size, PointerType<ConstType<Half>, AS>]>;
892    def : Builtin<"vloada_half", [Float, Size, PointerType<ConstType<Half>, AS>]>;
893    foreach VSize = [2, 3, 4, 8, 16] in {
894      foreach name = ["vload_half" # VSize] in {
895        def : Builtin<name, [VectorType<Float, VSize>, Size, PointerType<ConstType<Half>, AS>]>;
896      }
897    }
898    foreach rnd = ["", "_rte", "_rtz", "_rtp", "_rtn"] in {
899      foreach name = ["vstore_half" # rnd, "vstorea_half" # rnd] in {
900        def : Builtin<name, [Void, Float, Size, PointerType<Half, AS>]>;
901        def : Builtin<name, [Void, Double, Size, PointerType<Half, AS>]>;
902      }
903      foreach VSize = [2, 3, 4, 8, 16] in {
904        foreach name = ["vstore_half" # VSize # rnd] in {
905          def : Builtin<name, [Void, VectorType<Float, VSize>, Size, PointerType<Half, AS>]>;
906          def : Builtin<name, [Void, VectorType<Double, VSize>, Size, PointerType<Half, AS>]>;
907        }
908      }
909    }
910  }
911}
912let MinVersion = CL20 in {
913  foreach AS = [GenericAS] in {
914    def : Builtin<"vload_half", [Float, Size, PointerType<ConstType<Half>, AS>]>;
915    def : Builtin<"vloada_half", [Float, Size, PointerType<ConstType<Half>, AS>]>;
916    foreach VSize = [2, 3, 4, 8, 16] in {
917      foreach name = ["vload_half" # VSize] in {
918        def : Builtin<name, [VectorType<Float, VSize>, Size, PointerType<ConstType<Half>, AS>]>;
919      }
920    }
921    foreach rnd = ["", "_rte", "_rtz", "_rtp", "_rtn"] in {
922      foreach name = ["vstore_half" # rnd, "vstorea_half" # rnd] in {
923        def : Builtin<name, [Void, Float, Size, PointerType<Half, AS>]>;
924        def : Builtin<name, [Void, Double, Size, PointerType<Half, AS>]>;
925      }
926      foreach VSize = [2, 3, 4, 8, 16] in {
927        foreach name = ["vstore_half" # VSize # rnd] in {
928          def : Builtin<name, [Void, VectorType<Float, VSize>, Size, PointerType<Half, AS>]>;
929          def : Builtin<name, [Void, VectorType<Double, VSize>, Size, PointerType<Half, AS>]>;
930        }
931      }
932    }
933  }
934}
935
936foreach AS = [ConstantAS] in {
937  def : Builtin<"vload_half", [Float, Size, PointerType<ConstType<Half>, AS>]>;
938  def : Builtin<"vloada_half", [Float, Size, PointerType<ConstType<Half>, AS>]>;
939  foreach VSize = [2, 3, 4, 8, 16] in {
940    foreach name = ["vload_half" # VSize] in {
941      def : Builtin<name, [VectorType<Float, VSize>, Size, PointerType<ConstType<Half>, AS>]>;
942    }
943  }
944}
945
946// OpenCL v3.0 s6.15.8 - Synchronization Functions.
947def : Builtin<"barrier", [Void, MemFenceFlags], Attr.Convergent>;
948let MinVersion = CL20 in {
949  def : Builtin<"work_group_barrier", [Void, MemFenceFlags], Attr.Convergent>;
950  def : Builtin<"work_group_barrier", [Void, MemFenceFlags, MemoryScope], Attr.Convergent>;
951}
952
953// OpenCL v3.0 s6.15.9 - Legacy Explicit Memory Fence Functions.
954def : Builtin<"mem_fence", [Void, MemFenceFlags]>;
955def : Builtin<"read_mem_fence", [Void, MemFenceFlags]>;
956def : Builtin<"write_mem_fence", [Void, MemFenceFlags]>;
957
958// OpenCL v3.0 s6.15.10 - Address Space Qualifier Functions.
959// to_global, to_local, to_private are declared in Builtins.def.
960
961let MinVersion = CL20 in {
962  // The OpenCL 3.0 specification defines these with a "gentype" argument indicating any builtin
963  // type or user-defined type, which cannot be represented currently.  Hence we slightly diverge
964  // by providing only the following overloads with a void pointer.
965  def : Builtin<"get_fence", [MemFenceFlags, PointerType<Void, GenericAS>]>;
966  def : Builtin<"get_fence", [MemFenceFlags, PointerType<ConstType<Void>, GenericAS>]>;
967}
968
969//--------------------------------------------------------------------
970// 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
971// OpenCL Extension v2.0 s5.1.7 and s6.1.7: Async Copies from Global to Local Memory, Local to Global Memory, and Prefetch
972// --- Table 18 ---
973foreach name = ["async_work_group_copy"] in {
974  def : Builtin<name, [Event, PointerType<AGenTypeN, LocalAS>, PointerType<ConstType<AGenTypeN>, GlobalAS>, Size, Event]>;
975  def : Builtin<name, [Event, PointerType<AGenTypeN, GlobalAS>, PointerType<ConstType<AGenTypeN>, LocalAS>, Size, Event]>;
976}
977foreach name = ["async_work_group_strided_copy"] in {
978  def : Builtin<name, [Event, PointerType<AGenTypeN, LocalAS>, PointerType<ConstType<AGenTypeN>, GlobalAS>, Size, Size, Event]>;
979  def : Builtin<name, [Event, PointerType<AGenTypeN, GlobalAS>, PointerType<ConstType<AGenTypeN>, LocalAS>, Size, Size, Event]>;
980}
981foreach name = ["wait_group_events"] in {
982  def : Builtin<name, [Void, Int, PointerType<Event, GenericAS>]>;
983}
984foreach name = ["prefetch"] in {
985  def : Builtin<name, [Void, PointerType<ConstType<AGenTypeN>, GlobalAS>, Size]>;
986}
987
988//--------------------------------------------------------------------
989// OpenCL v2.0 s6.13.11 - Atomics Functions.
990// Functions that use memory_order and cl_mem_fence_flags enums are not
991// declared here as the TableGen backend does not handle enums.
992
993// OpenCL v1.0 s9.5, s9.6, s9.7 - Atomic Functions for 32-bit integers
994// --- Table 9.1 ---
995let Extension = FuncExtKhrGlobalInt32BaseAtomics in {
996  foreach Type = [Int, UInt] in {
997    foreach name = ["atom_add", "atom_sub", "atom_xchg"] in {
998      def : Builtin<name, [Type, PointerType<VolatileType<Type>, GlobalAS>, Type]>;
999    }
1000    foreach name = ["atom_inc", "atom_dec"] in {
1001      def : Builtin<name, [Type, PointerType<VolatileType<Type>, GlobalAS>]>;
1002    }
1003    foreach name = ["atom_cmpxchg"] in {
1004      def : Builtin<name, [Type, PointerType<VolatileType<Type>, GlobalAS>, Type, Type]>;
1005    }
1006  }
1007}
1008// --- Table 9.3 ---
1009let Extension = FuncExtKhrLocalInt32BaseAtomics in {
1010  foreach Type = [Int, UInt] in {
1011    foreach name = ["atom_add", "atom_sub", "atom_xchg"] in {
1012      def : Builtin<name, [Type, PointerType<VolatileType<Type>, LocalAS>, Type]>;
1013    }
1014    foreach name = ["atom_inc", "atom_dec"] in {
1015      def : Builtin<name, [Type, PointerType<VolatileType<Type>, LocalAS>]>;
1016    }
1017    foreach name = ["atom_cmpxchg"] in {
1018      def : Builtin<name, [Type, PointerType<VolatileType<Type>, LocalAS>, Type, Type]>;
1019    }
1020  }
1021}
1022// --- Table 9.5 ---
1023let Extension = FuncExtKhrInt64BaseAtomics in {
1024  foreach AS = [GlobalAS, LocalAS] in {
1025    foreach Type = [Long, ULong] in {
1026      foreach name = ["atom_add", "atom_sub", "atom_xchg"] in {
1027        def : Builtin<name, [Type, PointerType<VolatileType<Type>, AS>, Type]>;
1028      }
1029      foreach name = ["atom_inc", "atom_dec"] in {
1030        def : Builtin<name, [Type, PointerType<VolatileType<Type>, AS>]>;
1031      }
1032      foreach name = ["atom_cmpxchg"] in {
1033        def : Builtin<name, [Type, PointerType<VolatileType<Type>, AS>, Type, Type]>;
1034      }
1035    }
1036  }
1037}
1038// --- Table 9.2 ---
1039let Extension = FuncExtKhrGlobalInt32ExtendedAtomics in {
1040  foreach Type = [Int, UInt] in {
1041    foreach name = ["atom_min", "atom_max", "atom_and",
1042                    "atom_or", "atom_xor"] in {
1043      def : Builtin<name, [Type, PointerType<VolatileType<Type>, GlobalAS>, Type]>;
1044    }
1045  }
1046}
1047// --- Table 9.4 ---
1048let Extension = FuncExtKhrLocalInt32ExtendedAtomics in {
1049  foreach Type = [Int, UInt] in {
1050    foreach name = ["atom_min", "atom_max", "atom_and",
1051                    "atom_or", "atom_xor"] in {
1052      def : Builtin<name, [Type, PointerType<VolatileType<Type>, LocalAS>, Type]>;
1053    }
1054  }
1055}
1056// --- Table 9.6 ---
1057let Extension = FuncExtKhrInt64ExtendedAtomics in {
1058  foreach AS = [GlobalAS, LocalAS] in {
1059    foreach Type = [Long, ULong] in {
1060      foreach name = ["atom_min", "atom_max", "atom_and",
1061                      "atom_or", "atom_xor"] in {
1062        def : Builtin<name, [Type, PointerType<VolatileType<Type>, AS>, Type]>;
1063      }
1064    }
1065  }
1066}
1067// OpenCL v1.1 s6.11.1, v1.2 s6.12.11 - Atomic Functions
1068foreach AS = [GlobalAS, LocalAS] in {
1069  def : Builtin<"atomic_xchg", [Float, PointerType<VolatileType<Float>, AS>, Float]>;
1070  foreach Type = [Int, UInt] in {
1071    foreach name = ["atomic_add", "atomic_sub", "atomic_xchg",
1072                    "atomic_min", "atomic_max", "atomic_and",
1073                    "atomic_or", "atomic_xor"] in {
1074      def : Builtin<name, [Type, PointerType<VolatileType<Type>, AS>, Type]>;
1075    }
1076    foreach name = ["atomic_inc", "atomic_dec"] in {
1077      def : Builtin<name, [Type, PointerType<VolatileType<Type>, AS>]>;
1078    }
1079    foreach name = ["atomic_cmpxchg"] in {
1080      def : Builtin<name, [Type, PointerType<VolatileType<Type>, AS>, Type, Type]>;
1081    }
1082  }
1083}
1084
1085let Extension = FuncExtOpenCLCxx in {
1086  foreach Type = [Int, UInt] in {
1087    foreach name = ["atomic_add", "atomic_sub", "atomic_xchg",
1088                    "atomic_min", "atomic_max", "atomic_and",
1089                    "atomic_or", "atomic_xor"] in {
1090      def : Builtin<name, [Type, PointerType<VolatileType<Type>, GenericAS>, Type]>;
1091    }
1092    foreach name = ["atomic_inc", "atomic_dec"] in {
1093      def : Builtin<name, [Type, PointerType<VolatileType<Type>, GenericAS>]>;
1094    }
1095    foreach name = ["atomic_cmpxchg"] in {
1096      def : Builtin<name, [Type, PointerType<VolatileType<Type>, GenericAS>, Type, Type]>;
1097    }
1098  }
1099}
1100
1101// OpenCL v2.0 s6.13.11 - Atomic Functions.
1102let MinVersion = CL20 in {
1103  def : Builtin<"atomic_work_item_fence", [Void, MemFenceFlags, MemoryOrder, MemoryScope]>;
1104
1105  foreach TypePair = [[AtomicInt, Int], [AtomicUInt, UInt],
1106                      [AtomicLong, Long], [AtomicULong, ULong],
1107                      [AtomicFloat, Float], [AtomicDouble, Double]] in {
1108    def : Builtin<"atomic_init",
1109        [Void, PointerType<VolatileType<TypePair[0]>, GenericAS>, TypePair[1]]>;
1110    def : Builtin<"atomic_store",
1111        [Void, PointerType<VolatileType<TypePair[0]>, GenericAS>, TypePair[1]]>;
1112    def : Builtin<"atomic_store_explicit",
1113        [Void, PointerType<VolatileType<TypePair[0]>, GenericAS>, TypePair[1], MemoryOrder]>;
1114    def : Builtin<"atomic_store_explicit",
1115        [Void, PointerType<VolatileType<TypePair[0]>, GenericAS>, TypePair[1], MemoryOrder, MemoryScope]>;
1116    def : Builtin<"atomic_load",
1117        [TypePair[1], PointerType<VolatileType<TypePair[0]>, GenericAS>]>;
1118    def : Builtin<"atomic_load_explicit",
1119        [TypePair[1], PointerType<VolatileType<TypePair[0]>, GenericAS>, MemoryOrder]>;
1120    def : Builtin<"atomic_load_explicit",
1121        [TypePair[1], PointerType<VolatileType<TypePair[0]>, GenericAS>, MemoryOrder, MemoryScope]>;
1122    def : Builtin<"atomic_exchange",
1123        [TypePair[1], PointerType<VolatileType<TypePair[0]>, GenericAS>, TypePair[1]]>;
1124    def : Builtin<"atomic_exchange_explicit",
1125        [TypePair[1], PointerType<VolatileType<TypePair[0]>, GenericAS>, TypePair[1], MemoryOrder]>;
1126    def : Builtin<"atomic_exchange_explicit",
1127        [TypePair[1], PointerType<VolatileType<TypePair[0]>, GenericAS>, TypePair[1], MemoryOrder, MemoryScope]>;
1128    foreach Variant = ["weak", "strong"] in {
1129      def : Builtin<"atomic_compare_exchange_" # Variant,
1130          [Bool, PointerType<VolatileType<TypePair[0]>, GenericAS>,
1131           PointerType<TypePair[1], GenericAS>, TypePair[1]]>;
1132      def : Builtin<"atomic_compare_exchange_" # Variant # "_explicit",
1133          [Bool, PointerType<VolatileType<TypePair[0]>, GenericAS>,
1134           PointerType<TypePair[1], GenericAS>, TypePair[1], MemoryOrder, MemoryOrder]>;
1135      def : Builtin<"atomic_compare_exchange_" # Variant # "_explicit",
1136          [Bool, PointerType<VolatileType<TypePair[0]>, GenericAS>,
1137           PointerType<TypePair[1], GenericAS>, TypePair[1], MemoryOrder, MemoryOrder, MemoryScope]>;
1138    }
1139  }
1140
1141  foreach TypePair = [[AtomicInt, Int, Int], [AtomicUInt, UInt, UInt],
1142                      [AtomicLong, Long, Long], [AtomicULong, ULong, ULong],
1143                      [AtomicUIntPtr, UIntPtr, PtrDiff]] in {
1144    foreach ModOp = ["add", "sub"] in {
1145      def : Builtin<"atomic_fetch_" # ModOp,
1146          [TypePair[1], PointerType<VolatileType<TypePair[0]>, GenericAS>, TypePair[2]]>;
1147      def : Builtin<"atomic_fetch_" # ModOp # "_explicit",
1148          [TypePair[1], PointerType<VolatileType<TypePair[0]>, GenericAS>, TypePair[2], MemoryOrder]>;
1149      def : Builtin<"atomic_fetch_" # ModOp # "_explicit",
1150          [TypePair[1], PointerType<VolatileType<TypePair[0]>, GenericAS>, TypePair[2], MemoryOrder, MemoryScope]>;
1151    }
1152  }
1153  foreach TypePair = [[AtomicInt, Int, Int], [AtomicUInt, UInt, UInt],
1154                      [AtomicLong, Long, Long], [AtomicULong, ULong, ULong]] in {
1155    foreach ModOp = ["or", "xor", "and", "min", "max"] in {
1156      def : Builtin<"atomic_fetch_" # ModOp,
1157          [TypePair[1], PointerType<VolatileType<TypePair[0]>, GenericAS>, TypePair[2]]>;
1158      def : Builtin<"atomic_fetch_" # ModOp # "_explicit",
1159          [TypePair[1], PointerType<VolatileType<TypePair[0]>, GenericAS>, TypePair[2], MemoryOrder]>;
1160      def : Builtin<"atomic_fetch_" # ModOp # "_explicit",
1161          [TypePair[1], PointerType<VolatileType<TypePair[0]>, GenericAS>, TypePair[2], MemoryOrder, MemoryScope]>;
1162    }
1163  }
1164
1165  def : Builtin<"atomic_flag_clear",
1166      [Void, PointerType<VolatileType<AtomicFlag>, GenericAS>]>;
1167  def : Builtin<"atomic_flag_clear_explicit",
1168      [Void, PointerType<VolatileType<AtomicFlag>, GenericAS>, MemoryOrder]>;
1169  def : Builtin<"atomic_flag_clear_explicit",
1170      [Void, PointerType<VolatileType<AtomicFlag>, GenericAS>, MemoryOrder, MemoryScope]>;
1171
1172  def : Builtin<"atomic_flag_test_and_set",
1173      [Bool, PointerType<VolatileType<AtomicFlag>, GenericAS>]>;
1174  def : Builtin<"atomic_flag_test_and_set_explicit",
1175      [Bool, PointerType<VolatileType<AtomicFlag>, GenericAS>, MemoryOrder]>;
1176  def : Builtin<"atomic_flag_test_and_set_explicit",
1177      [Bool, PointerType<VolatileType<AtomicFlag>, GenericAS>, MemoryOrder, MemoryScope]>;
1178}
1179
1180//--------------------------------------------------------------------
1181// OpenCL v1.1 s6.11.12, v1.2 s6.12.12, v2.0 s6.13.12 - Miscellaneous Vector Functions
1182// --- Table 19 ---
1183foreach VSize1 = [2, 4, 8, 16] in {
1184  foreach VSize2 = [2, 4, 8, 16] in {
1185    foreach VecAndMaskType = [[Char, UChar], [UChar, UChar],
1186                              [Short, UShort], [UShort, UShort],
1187                              [Int, UInt], [UInt, UInt],
1188                              [Long, ULong], [ULong, ULong],
1189                              [Float, UInt], [Double, ULong], [Half, UShort]] in {
1190      def : Builtin<"shuffle", [VectorType<VecAndMaskType[0], VSize1>,
1191                                VectorType<VecAndMaskType[0], VSize2>,
1192                                VectorType<VecAndMaskType[1], VSize1>],
1193                               Attr.Const>;
1194    }
1195  }
1196}
1197foreach VSize1 = [2, 4, 8, 16] in {
1198  foreach VSize2 = [2, 4, 8, 16] in {
1199    foreach VecAndMaskType = [[Char, UChar], [UChar, UChar],
1200                              [Short, UShort], [UShort, UShort],
1201                              [Int, UInt], [UInt, UInt],
1202                              [Long, ULong], [ULong, ULong],
1203                              [Float, UInt], [Double, ULong], [Half, UShort]] in {
1204      def : Builtin<"shuffle2", [VectorType<VecAndMaskType[0], VSize1>,
1205                                 VectorType<VecAndMaskType[0], VSize2>,
1206                                 VectorType<VecAndMaskType[0], VSize2>,
1207                                 VectorType<VecAndMaskType[1], VSize1>],
1208                                Attr.Const>;
1209    }
1210  }
1211}
1212
1213//--------------------------------------------------------------------
1214// OpenCL v1.1 s6.11.3, v1.2 s6.12.14, v2.0 s6.13.14: Image Read and Write Functions
1215// OpenCL Extension v2.0 s5.1.8 and s6.1.8: Image Read and Write Functions
1216// --- Table 22: Image Read Functions with Samplers ---
1217foreach imgTy = [Image1d] in {
1218  foreach coordTy = [Int, Float] in {
1219    def : Builtin<"read_imagef", [VectorType<Float, 4>, ImageType<imgTy, "RO">, Sampler, coordTy], Attr.Pure>;
1220    def : Builtin<"read_imagei", [VectorType<Int, 4>, ImageType<imgTy, "RO">, Sampler, coordTy], Attr.Pure>;
1221    def : Builtin<"read_imageui", [VectorType<UInt, 4>, ImageType<imgTy, "RO">, Sampler, coordTy], Attr.Pure>;
1222  }
1223}
1224foreach imgTy = [Image2d, Image1dArray] in {
1225  foreach coordTy = [Int, Float] in {
1226    def : Builtin<"read_imagef", [VectorType<Float, 4>, ImageType<imgTy, "RO">, Sampler, VectorType<coordTy, 2>], Attr.Pure>;
1227    def : Builtin<"read_imagei", [VectorType<Int, 4>, ImageType<imgTy, "RO">, Sampler, VectorType<coordTy, 2>], Attr.Pure>;
1228    def : Builtin<"read_imageui", [VectorType<UInt, 4>, ImageType<imgTy, "RO">, Sampler, VectorType<coordTy, 2>], Attr.Pure>;
1229  }
1230}
1231foreach imgTy = [Image3d, Image2dArray] in {
1232  foreach coordTy = [Int, Float] in {
1233    def : Builtin<"read_imagef", [VectorType<Float, 4>, ImageType<imgTy, "RO">, Sampler, VectorType<coordTy, 4>], Attr.Pure>;
1234    def : Builtin<"read_imagei", [VectorType<Int, 4>, ImageType<imgTy, "RO">, Sampler, VectorType<coordTy, 4>], Attr.Pure>;
1235    def : Builtin<"read_imageui", [VectorType<UInt, 4>, ImageType<imgTy, "RO">, Sampler, VectorType<coordTy, 4>], Attr.Pure>;
1236  }
1237}
1238foreach coordTy = [Int, Float] in {
1239  def : Builtin<"read_imagef", [Float, ImageType<Image2dDepth, "RO">, Sampler, VectorType<coordTy, 2>], Attr.Pure>;
1240  def : Builtin<"read_imagef", [Float, ImageType<Image2dArrayDepth, "RO">, Sampler, VectorType<coordTy, 4>], Attr.Pure>;
1241}
1242
1243// --- Table 23: Sampler-less Read Functions ---
1244let MinVersion = CL12 in {
1245  foreach aQual = ["RO", "RW"] in {
1246    foreach imgTy = [Image2d, Image1dArray] in {
1247      def : Builtin<"read_imagef", [VectorType<Float, 4>, ImageType<imgTy, aQual>, VectorType<Int, 2>], Attr.Pure>;
1248      def : Builtin<"read_imagei", [VectorType<Int, 4>, ImageType<imgTy, aQual>, VectorType<Int, 2>], Attr.Pure>;
1249      def : Builtin<"read_imageui", [VectorType<UInt, 4>, ImageType<imgTy, aQual>, VectorType<Int, 2>], Attr.Pure>;
1250    }
1251    foreach imgTy = [Image3d, Image2dArray] in {
1252      def : Builtin<"read_imagef", [VectorType<Float, 4>, ImageType<imgTy, aQual>, VectorType<Int, 4>], Attr.Pure>;
1253      def : Builtin<"read_imagei", [VectorType<Int, 4>, ImageType<imgTy, aQual>, VectorType<Int, 4>], Attr.Pure>;
1254      def : Builtin<"read_imageui", [VectorType<UInt, 4>, ImageType<imgTy, aQual>, VectorType<Int, 4>], Attr.Pure>;
1255    }
1256    foreach imgTy = [Image1d, Image1dBuffer] in {
1257      def : Builtin<"read_imagef", [VectorType<Float, 4>, ImageType<imgTy, aQual>, Int], Attr.Pure>;
1258      def : Builtin<"read_imagei", [VectorType<Int, 4>, ImageType<imgTy, aQual>, Int], Attr.Pure>;
1259      def : Builtin<"read_imageui", [VectorType<UInt, 4>, ImageType<imgTy, aQual>, Int], Attr.Pure>;
1260    }
1261    def : Builtin<"read_imagef", [Float, ImageType<Image2dDepth, aQual>, VectorType<Int, 2>], Attr.Pure>;
1262    def : Builtin<"read_imagef", [Float, ImageType<Image2dArrayDepth, aQual>, VectorType<Int, 4>], Attr.Pure>;
1263  }
1264}
1265
1266// --- Table 24: Image Write Functions ---
1267foreach aQual = ["WO", "RW"] in {
1268  foreach imgTy = [Image2d] in {
1269    def : Builtin<"write_imagef", [Void, ImageType<imgTy, aQual>, VectorType<Int, 2>, VectorType<Float, 4>]>;
1270    def : Builtin<"write_imagei", [Void, ImageType<imgTy, aQual>, VectorType<Int, 2>, VectorType<Int, 4>]>;
1271    def : Builtin<"write_imageui", [Void, ImageType<imgTy, aQual>, VectorType<Int, 2>, VectorType<UInt, 4>]>;
1272  }
1273  foreach imgTy = [Image2dArray] in {
1274    def : Builtin<"write_imagef", [Void, ImageType<imgTy, aQual>, VectorType<Int, 4>, VectorType<Float, 4>]>;
1275    def : Builtin<"write_imagei", [Void, ImageType<imgTy, aQual>, VectorType<Int, 4>, VectorType<Int, 4>]>;
1276    def : Builtin<"write_imageui", [Void, ImageType<imgTy, aQual>, VectorType<Int, 4>, VectorType<UInt, 4>]>;
1277  }
1278  foreach imgTy = [Image1d, Image1dBuffer] in {
1279    def : Builtin<"write_imagef", [Void, ImageType<imgTy, aQual>, Int, VectorType<Float, 4>]>;
1280    def : Builtin<"write_imagei", [Void, ImageType<imgTy, aQual>, Int, VectorType<Int, 4>]>;
1281    def : Builtin<"write_imageui", [Void, ImageType<imgTy, aQual>, Int, VectorType<UInt, 4>]>;
1282  }
1283  foreach imgTy = [Image1dArray] in {
1284    def : Builtin<"write_imagef", [Void, ImageType<imgTy, aQual>, VectorType<Int, 2>, VectorType<Float, 4>]>;
1285    def : Builtin<"write_imagei", [Void, ImageType<imgTy, aQual>, VectorType<Int, 2>, VectorType<Int, 4>]>;
1286    def : Builtin<"write_imageui", [Void, ImageType<imgTy, aQual>, VectorType<Int, 2>, VectorType<UInt, 4>]>;
1287  }
1288  foreach imgTy = [Image3d] in {
1289    def : Builtin<"write_imagef", [Void, ImageType<imgTy, aQual>, VectorType<Int, 4>, VectorType<Float, 4>]>;
1290    def : Builtin<"write_imagei", [Void, ImageType<imgTy, aQual>, VectorType<Int, 4>, VectorType<Int, 4>]>;
1291    def : Builtin<"write_imageui", [Void, ImageType<imgTy, aQual>, VectorType<Int, 4>, VectorType<UInt, 4>]>;
1292  }
1293  def : Builtin<"write_imagef", [Void, ImageType<Image2dDepth, aQual>, VectorType<Int, 2>, Float]>;
1294  def : Builtin<"write_imagef", [Void, ImageType<Image2dArrayDepth, aQual>, VectorType<Int, 4>, Float]>;
1295}
1296
1297// --- Table 25: Image Query Functions ---
1298foreach aQual = ["RO", "WO", "RW"] in {
1299  foreach imgTy = [Image1d, Image1dBuffer, Image2d, Image3d,
1300                   Image1dArray, Image2dArray, Image2dDepth,
1301                   Image2dArrayDepth] in {
1302    foreach name = ["get_image_width", "get_image_channel_data_type",
1303                    "get_image_channel_order"] in {
1304      def : Builtin<name, [Int, ImageType<imgTy, aQual>], Attr.Const>;
1305    }
1306  }
1307  foreach imgTy = [Image2d, Image3d, Image2dArray, Image2dDepth,
1308                   Image2dArrayDepth] in {
1309    def : Builtin<"get_image_height", [Int, ImageType<imgTy, aQual>], Attr.Const>;
1310  }
1311  def : Builtin<"get_image_depth", [Int, ImageType<Image3d, aQual>], Attr.Const>;
1312  foreach imgTy = [Image2d, Image2dArray, Image2dDepth,
1313                   Image2dArrayDepth] in {
1314    def : Builtin<"get_image_dim", [VectorType<Int, 2>, ImageType<imgTy, aQual>], Attr.Const>;
1315  }
1316  def : Builtin<"get_image_dim", [VectorType<Int, 4>, ImageType<Image3d, aQual>], Attr.Const>;
1317  foreach imgTy = [Image1dArray, Image2dArray, Image2dArrayDepth] in {
1318    def : Builtin<"get_image_array_size", [Size, ImageType<imgTy, aQual>], Attr.Const>;
1319  }
1320}
1321
1322// OpenCL extension v2.0 s5.1.9: Built-in Image Read Functions
1323// --- Table 8 ---
1324foreach aQual = ["RO"] in {
1325  foreach name = ["read_imageh"] in {
1326    foreach coordTy = [Int, Float] in {
1327      foreach imgTy = [Image2d, Image1dArray] in {
1328        def : Builtin<name, [VectorType<Half, 4>, ImageType<imgTy, aQual>, Sampler, VectorType<coordTy, 2>], Attr.Pure>;
1329      }
1330      foreach imgTy = [Image3d, Image2dArray] in {
1331        def : Builtin<name, [VectorType<Half, 4>, ImageType<imgTy, aQual>, Sampler, VectorType<coordTy, 4>], Attr.Pure>;
1332      }
1333      foreach imgTy = [Image1d] in {
1334        def : Builtin<name, [VectorType<Half, 4>, ImageType<imgTy, aQual>, Sampler, coordTy], Attr.Pure>;
1335      }
1336    }
1337  }
1338}
1339// OpenCL extension v2.0 s5.1.10: Built-in Image Sampler-less Read Functions
1340// --- Table 9 ---
1341let MinVersion = CL12 in {
1342  foreach aQual = ["RO", "RW"] in {
1343    foreach name = ["read_imageh"] in {
1344      foreach imgTy = [Image2d, Image1dArray] in {
1345        def : Builtin<name, [VectorType<Half, 4>, ImageType<imgTy, aQual>, VectorType<Int, 2>], Attr.Pure>;
1346      }
1347      foreach imgTy = [Image3d, Image2dArray] in {
1348        def : Builtin<name, [VectorType<Half, 4>, ImageType<imgTy, aQual>, VectorType<Int, 4>], Attr.Pure>;
1349      }
1350      foreach imgTy = [Image1d, Image1dBuffer] in {
1351        def : Builtin<name, [VectorType<Half, 4>, ImageType<imgTy, aQual>, Int], Attr.Pure>;
1352      }
1353    }
1354  }
1355}
1356// OpenCL extension v2.0 s5.1.11: Built-in Image Write Functions
1357// --- Table 10 ---
1358foreach aQual = ["WO", "RW"] in {
1359  foreach name = ["write_imageh"] in {
1360    def : Builtin<name, [Void, ImageType<Image2d, aQual>, VectorType<Int, 2>, VectorType<Half, 4>]>;
1361    def : Builtin<name, [Void, ImageType<Image2dArray, aQual>, VectorType<Int, 4>, VectorType<Half, 4>]>;
1362    def : Builtin<name, [Void, ImageType<Image1d, aQual>, Int, VectorType<Half, 4>]>;
1363    def : Builtin<name, [Void, ImageType<Image1dBuffer, aQual>, Int, VectorType<Half, 4>]>;
1364    def : Builtin<name, [Void, ImageType<Image1dArray, aQual>, VectorType<Int, 2>, VectorType<Half, 4>]>;
1365    def : Builtin<name, [Void, ImageType<Image3d, aQual>, VectorType<Int, 4>, VectorType<Half, 4>]>;
1366  }
1367}
1368
1369
1370//--------------------------------------------------------------------
1371// OpenCL v2.0 s6.13.15 - Work-group Functions
1372// --- Table 26 ---
1373let MinVersion = CL20 in {
1374  foreach name = ["work_group_all", "work_group_any"] in {
1375    def : Builtin<name, [Int, Int], Attr.Convergent>;
1376  }
1377  foreach name = ["work_group_broadcast"] in {
1378    def : Builtin<name, [IntLongFloatGenType1, IntLongFloatGenType1, Size], Attr.Convergent>;
1379    def : Builtin<name, [IntLongFloatGenType1, IntLongFloatGenType1, Size, Size], Attr.Convergent>;
1380    def : Builtin<name, [IntLongFloatGenType1, IntLongFloatGenType1, Size, Size, Size], Attr.Convergent>;
1381  }
1382  foreach op = ["add", "min", "max"] in {
1383    foreach name = ["work_group_reduce_", "work_group_scan_exclusive_",
1384                    "work_group_scan_inclusive_"] in {
1385      def : Builtin<name # op, [IntLongFloatGenType1, IntLongFloatGenType1], Attr.Convergent>;
1386    }
1387  }
1388}
1389
1390
1391//--------------------------------------------------------------------
1392// OpenCL2.0 : 6.13.16 : Pipe Functions
1393// --- Table 27 ---
1394// Defined in Builtins.def
1395
1396// --- Table 28 ---
1397// Builtins taking pipe arguments are defined in Builtins.def
1398def : Builtin<"is_valid_reserve_id", [Bool, ReserveId]>;
1399
1400// --- Table 29 ---
1401// Defined in Builtins.def
1402
1403
1404//--------------------------------------------------------------------
1405// OpenCL2.0 : 6.13.17 : Enqueuing Kernels
1406// --- Table 30 ---
1407// Defined in Builtins.def
1408
1409// --- Table 32 ---
1410// Defined in Builtins.def
1411
1412// --- Table 33 ---
1413let MinVersion = CL20 in {
1414  def : Builtin<"enqueue_marker",
1415      [Int, Queue, UInt, PointerType<ConstType<ClkEvent>, GenericAS>, PointerType<ClkEvent, GenericAS>]>;
1416
1417  // --- Table 34 ---
1418  def : Builtin<"retain_event", [Void, ClkEvent]>;
1419  def : Builtin<"release_event", [Void, ClkEvent]>;
1420  def : Builtin<"create_user_event", [ClkEvent]>;
1421  def : Builtin<"is_valid_event", [Bool, ClkEvent]>;
1422  def : Builtin<"set_user_event_status", [Void, ClkEvent, Int]>;
1423  def : Builtin<"capture_event_profiling_info",
1424      [Void, ClkEvent, ClkProfilingInfo, PointerType<Void, GlobalAS>]>;
1425
1426  // --- Table 35 ---
1427  def : Builtin<"get_default_queue", [Queue]>;
1428
1429  def : Builtin<"ndrange_1D", [NDRange, Size]>;
1430  def : Builtin<"ndrange_1D", [NDRange, Size, Size]>;
1431  def : Builtin<"ndrange_1D", [NDRange, Size, Size, Size]>;
1432  def : Builtin<"ndrange_2D", [NDRange, PointerType<ConstType<Size>, PrivateAS>]>;
1433  def : Builtin<"ndrange_2D", [NDRange, PointerType<ConstType<Size>, PrivateAS>,
1434                                        PointerType<ConstType<Size>, PrivateAS>]>;
1435  def : Builtin<"ndrange_2D", [NDRange, PointerType<ConstType<Size>, PrivateAS>,
1436                                        PointerType<ConstType<Size>, PrivateAS>,
1437                                        PointerType<ConstType<Size>, PrivateAS>]>;
1438  def : Builtin<"ndrange_3D", [NDRange, PointerType<ConstType<Size>, PrivateAS>]>;
1439  def : Builtin<"ndrange_3D", [NDRange, PointerType<ConstType<Size>, PrivateAS>,
1440                                        PointerType<ConstType<Size>, PrivateAS>]>;
1441  def : Builtin<"ndrange_3D", [NDRange, PointerType<ConstType<Size>, PrivateAS>,
1442                                        PointerType<ConstType<Size>, PrivateAS>,
1443                                        PointerType<ConstType<Size>, PrivateAS>]>;
1444}
1445
1446
1447//--------------------------------------------------------------------
1448// End of the builtin functions defined in the OpenCL C specification.
1449// Builtin functions defined in the OpenCL C Extension are below.
1450//--------------------------------------------------------------------
1451
1452
1453// OpenCL Extension v2.0 s9.18 - Mipmaps
1454let Extension = FuncExtKhrMipmapImage in {
1455  // Added to section 6.13.14.2.
1456  foreach aQual = ["RO"] in {
1457    foreach imgTy = [Image2d] in {
1458      foreach name = ["read_imagef"] in {
1459        def : Builtin<name, [VectorType<Float, 4>, ImageType<imgTy, aQual>, Sampler, VectorType<Float, 2>, Float], Attr.Pure>;
1460        def : Builtin<name, [VectorType<Float, 4>, ImageType<imgTy, aQual>, Sampler, VectorType<Float, 2>, VectorType<Float, 2>, VectorType<Float, 2>], Attr.Pure>;
1461      }
1462      foreach name = ["read_imagei"] in {
1463        def : Builtin<name, [VectorType<Int, 4>, ImageType<imgTy, aQual>, Sampler, VectorType<Float, 2>, Float], Attr.Pure>;
1464        def : Builtin<name, [VectorType<Int, 4>, ImageType<imgTy, aQual>, Sampler, VectorType<Float, 2>, VectorType<Float, 2>, VectorType<Float, 2>], Attr.Pure>;
1465      }
1466      foreach name = ["read_imageui"] in {
1467        def : Builtin<name, [VectorType<UInt, 4>, ImageType<imgTy, aQual>, Sampler, VectorType<Float, 2>, Float], Attr.Pure>;
1468        def : Builtin<name, [VectorType<UInt, 4>, ImageType<imgTy, aQual>, Sampler, VectorType<Float, 2>, VectorType<Float, 2>, VectorType<Float, 2>], Attr.Pure>;
1469      }
1470    }
1471    foreach imgTy = [Image2dDepth] in {
1472      foreach name = ["read_imagef"] in {
1473        def : Builtin<name, [Float, ImageType<imgTy, aQual>, Sampler, VectorType<Float, 2>, Float], Attr.Pure>;
1474        def : Builtin<name, [Float, ImageType<imgTy, aQual>, Sampler, VectorType<Float, 2>, VectorType<Float, 2>, VectorType<Float, 2>], Attr.Pure>;
1475      }
1476    }
1477    foreach imgTy = [Image1d] in {
1478      foreach name = ["read_imagef"] in {
1479        def : Builtin<name, [VectorType<Float, 4>, ImageType<imgTy, aQual>, Sampler, Float, Float], Attr.Pure>;
1480        def : Builtin<name, [VectorType<Float, 4>, ImageType<imgTy, aQual>, Sampler, Float, Float, Float], Attr.Pure>;
1481      }
1482      foreach name = ["read_imagei"] in {
1483        def : Builtin<name, [VectorType<Int, 4>, ImageType<imgTy, aQual>, Sampler, Float, Float], Attr.Pure>;
1484        def : Builtin<name, [VectorType<Int, 4>, ImageType<imgTy, aQual>, Sampler, Float, Float, Float], Attr.Pure>;
1485      }
1486      foreach name = ["read_imageui"] in {
1487        def : Builtin<name, [VectorType<UInt, 4>, ImageType<imgTy, aQual>, Sampler, Float, Float], Attr.Pure>;
1488        def : Builtin<name, [VectorType<UInt, 4>, ImageType<imgTy, aQual>, Sampler, Float, Float, Float], Attr.Pure>;
1489      }
1490    }
1491    foreach imgTy = [Image3d] in {
1492      foreach name = ["read_imagef"] in {
1493        def : Builtin<name, [VectorType<Float, 4>, ImageType<imgTy, aQual>, Sampler, VectorType<Float, 4>, VectorType<Float, 4>, VectorType<Float, 4>], Attr.Pure>;
1494        def : Builtin<name, [VectorType<Float, 4>, ImageType<imgTy, aQual>, Sampler, VectorType<Float, 4>, Float], Attr.Pure>;
1495      }
1496      foreach name = ["read_imagei"] in {
1497        def : Builtin<name, [VectorType<Int, 4>, ImageType<imgTy, aQual>, Sampler, VectorType<Float, 4>, VectorType<Float, 4>, VectorType<Float, 4>], Attr.Pure>;
1498        def : Builtin<name, [VectorType<Int, 4>, ImageType<imgTy, aQual>, Sampler, VectorType<Float, 4>, Float], Attr.Pure>;
1499      }
1500      foreach name = ["read_imageui"] in {
1501        def : Builtin<name, [VectorType<UInt, 4>, ImageType<imgTy, aQual>, Sampler, VectorType<Float, 4>, VectorType<Float, 4>, VectorType<Float, 4>], Attr.Pure>;
1502        def : Builtin<name, [VectorType<UInt, 4>, ImageType<imgTy, aQual>, Sampler, VectorType<Float, 4>, Float], Attr.Pure>;
1503      }
1504    }
1505    foreach imgTy = [Image1dArray] in {
1506      foreach name = ["read_imagef"] in {
1507        def : Builtin<name, [VectorType<Float, 4>, ImageType<imgTy, aQual>, Sampler, VectorType<Float, 2>, Float], Attr.Pure>;
1508        def : Builtin<name, [VectorType<Float, 4>, ImageType<imgTy, aQual>, Sampler, VectorType<Float, 2>, Float, Float], Attr.Pure>;
1509      }
1510      foreach name = ["read_imagei"] in {
1511        def : Builtin<name, [VectorType<Int, 4>, ImageType<imgTy, aQual>, Sampler, VectorType<Float, 2>, Float], Attr.Pure>;
1512        def : Builtin<name, [VectorType<Int, 4>, ImageType<imgTy, aQual>, Sampler, VectorType<Float, 2>, Float, Float], Attr.Pure>;
1513      }
1514      foreach name = ["read_imageui"] in {
1515        def : Builtin<name, [VectorType<UInt, 4>, ImageType<imgTy, aQual>, Sampler, VectorType<Float, 2>, Float], Attr.Pure>;
1516        def : Builtin<name, [VectorType<UInt, 4>, ImageType<imgTy, aQual>, Sampler, VectorType<Float, 2>, Float, Float], Attr.Pure>;
1517      }
1518    }
1519    foreach imgTy = [Image2dArray] in {
1520      foreach name = ["read_imagef"] in {
1521        def : Builtin<name, [VectorType<Float, 4>, ImageType<imgTy, aQual>, Sampler, VectorType<Float, 4>, Float], Attr.Pure>;
1522        def : Builtin<name, [VectorType<Float, 4>, ImageType<imgTy, aQual>, Sampler, VectorType<Float, 4>, VectorType<Float, 2>, VectorType<Float, 2>], Attr.Pure>;
1523      }
1524      foreach name = ["read_imagei"] in {
1525        def : Builtin<name, [VectorType<Int, 4>, ImageType<imgTy, aQual>, Sampler, VectorType<Float, 4>, Float], Attr.Pure>;
1526        def : Builtin<name, [VectorType<Int, 4>, ImageType<imgTy, aQual>, Sampler, VectorType<Float, 4>, VectorType<Float, 2>, VectorType<Float, 2>], Attr.Pure>;
1527      }
1528      foreach name = ["read_imageui"] in {
1529        def : Builtin<name, [VectorType<UInt, 4>, ImageType<imgTy, aQual>, Sampler, VectorType<Float, 4>, Float], Attr.Pure>;
1530        def : Builtin<name, [VectorType<UInt, 4>, ImageType<imgTy, aQual>, Sampler, VectorType<Float, 4>, VectorType<Float, 2>, VectorType<Float, 2>], Attr.Pure>;
1531      }
1532    }
1533    foreach imgTy = [Image2dArrayDepth] in {
1534      foreach name = ["read_imagef"] in {
1535        def : Builtin<name, [Float, ImageType<imgTy, aQual>, Sampler, VectorType<Float, 4>, Float], Attr.Pure>;
1536        def : Builtin<name, [Float, ImageType<imgTy, aQual>, Sampler, VectorType<Float, 4>, VectorType<Float, 2>, VectorType<Float, 2>], Attr.Pure>;
1537      }
1538    }
1539  }
1540  // Added to section 6.13.14.5
1541  foreach aQual = ["RO", "WO", "RW"] in {
1542    foreach imgTy = [Image1d, Image2d, Image3d, Image1dArray, Image2dArray, Image2dDepth, Image2dArrayDepth] in {
1543      def : Builtin<"get_image_num_mip_levels", [Int, ImageType<imgTy, aQual>]>;
1544    }
1545  }
1546}
1547
1548// Write functions are enabled using a separate extension.
1549let Extension = FuncExtKhrMipmapImageWrites in {
1550  // Added to section 6.13.14.4.
1551  foreach aQual = ["WO"] in {
1552    foreach imgTy = [Image2d] in {
1553      def : Builtin<"write_imagef", [Void, ImageType<imgTy, aQual>, VectorType<Int, 2>, Int, VectorType<Float, 4>]>;
1554      def : Builtin<"write_imagei", [Void, ImageType<imgTy, aQual>, VectorType<Int, 2>, Int, VectorType<Int, 4>]>;
1555      def : Builtin<"write_imageui", [Void, ImageType<imgTy, aQual>, VectorType<Int, 2>, Int, VectorType<UInt, 4>]>;
1556    }
1557    def : Builtin<"write_imagef", [Void, ImageType<Image2dDepth, aQual>, VectorType<Int, 2>, Int, Float]>;
1558    foreach imgTy = [Image1d] in {
1559      def : Builtin<"write_imagef", [Void, ImageType<imgTy, aQual>, Int, Int, VectorType<Float, 4>]>;
1560      def : Builtin<"write_imagei", [Void, ImageType<imgTy, aQual>, Int, Int, VectorType<Int, 4>]>;
1561      def : Builtin<"write_imageui", [Void, ImageType<imgTy, aQual>, Int, Int, VectorType<UInt, 4>]>;
1562    }
1563    foreach imgTy = [Image1dArray] in {
1564      def : Builtin<"write_imagef", [Void, ImageType<imgTy, aQual>, VectorType<Int, 2>, Int, VectorType<Float, 4>]>;
1565      def : Builtin<"write_imagei", [Void, ImageType<imgTy, aQual>, VectorType<Int, 2>, Int, VectorType<Int, 4>]>;
1566      def : Builtin<"write_imageui", [Void, ImageType<imgTy, aQual>, VectorType<Int, 2>, Int, VectorType<UInt, 4>]>;
1567    }
1568    foreach imgTy = [Image2dArray] in {
1569      def : Builtin<"write_imagef", [Void, ImageType<imgTy, aQual>, VectorType<Int, 4>, Int, VectorType<Float, 4>]>;
1570      def : Builtin<"write_imagei", [Void, ImageType<imgTy, aQual>, VectorType<Int, 4>, Int, VectorType<Int, 4>]>;
1571      def : Builtin<"write_imageui", [Void, ImageType<imgTy, aQual>, VectorType<Int, 4>, Int, VectorType<UInt, 4>]>;
1572    }
1573    def : Builtin<"write_imagef", [Void, ImageType<Image2dArrayDepth, aQual>, VectorType<Int, 4>, Int, Float]>;
1574    let Extension = FuncExtKhrMipmapWritesAndWrite3d in {
1575      foreach imgTy = [Image3d] in {
1576        def : Builtin<"write_imagef", [Void, ImageType<imgTy, aQual>, VectorType<Int, 4>, Int, VectorType<Float, 4>]>;
1577        def : Builtin<"write_imagei", [Void, ImageType<imgTy, aQual>, VectorType<Int, 4>, Int, VectorType<Int, 4>]>;
1578        def : Builtin<"write_imageui", [Void, ImageType<imgTy, aQual>, VectorType<Int, 4>, Int, VectorType<UInt, 4>]>;
1579      }
1580    }
1581  }
1582}
1583
1584//--------------------------------------------------------------------
1585// OpenCL Extension v2.0 s18.3 - Creating OpenCL Memory Objects from OpenGL MSAA Textures
1586let Extension = FuncExtKhrGlMsaaSharing in {
1587  // --- Table 6.13.14.3 ---
1588  foreach aQual = ["RO", "RW"] in {
1589    foreach imgTy = [Image2dMsaa] in {
1590      def : Builtin<"read_imagef", [VectorType<Float, 4>, ImageType<imgTy, aQual>, VectorType<Int, 2>, Int], Attr.Pure>;
1591      def : Builtin<"read_imagei", [VectorType<Int, 4>, ImageType<imgTy, aQual>, VectorType<Int, 2>, Int], Attr.Pure>;
1592      def : Builtin<"read_imageui", [VectorType<UInt, 4>, ImageType<imgTy, aQual>, VectorType<Int, 2>, Int], Attr.Pure>;
1593    }
1594    foreach imgTy = [Image2dArrayMsaa] in {
1595      def : Builtin<"read_imagef", [VectorType<Float, 4>, ImageType<imgTy, aQual>, VectorType<Int, 4>, Int], Attr.Pure>;
1596      def : Builtin<"read_imagei", [VectorType<Int, 4>, ImageType<imgTy, aQual>, VectorType<Int, 4>, Int], Attr.Pure>;
1597      def : Builtin<"read_imageui", [VectorType<UInt, 4>, ImageType<imgTy, aQual>, VectorType<Int, 4>, Int], Attr.Pure>;
1598    }
1599    foreach name = ["read_imagef"] in {
1600      def : Builtin<name, [Float, ImageType<Image2dMsaaDepth, aQual>, VectorType<Int, 2>, Int], Attr.Pure>;
1601      def : Builtin<name, [Float, ImageType<Image2dArrayMsaaDepth, aQual>, VectorType<Int, 4>, Int], Attr.Pure>;
1602    }
1603  }
1604
1605  // --- Table 6.13.14.5 ---
1606  foreach aQual = ["RO", "WO", "RW"] in {
1607    foreach imgTy = [Image2dMsaa, Image2dArrayMsaa, Image2dMsaaDepth, Image2dArrayMsaaDepth] in {
1608      foreach name = ["get_image_width", "get_image_height",
1609                      "get_image_channel_data_type", "get_image_channel_order",
1610                      "get_image_num_samples"] in {
1611        def : Builtin<name, [Int, ImageType<imgTy, aQual>], Attr.Const>;
1612      }
1613      def : Builtin<"get_image_dim", [VectorType<Int, 2>, ImageType<imgTy, aQual>], Attr.Const>;
1614    }
1615    foreach imgTy = [Image2dArrayMsaa, Image2dArrayMsaaDepth] in {
1616      def : Builtin<"get_image_array_size", [Size, ImageType<imgTy, aQual>], Attr.Const>;
1617    }
1618  }
1619}
1620
1621//--------------------------------------------------------------------
1622// OpenCL Extension v2.0 s28 - Subgroups
1623// --- Table 28.2.1 ---
1624let Extension = FuncExtKhrSubgroups in {
1625  foreach name = ["get_sub_group_size", "get_max_sub_group_size",
1626                  "get_num_sub_groups", "get_sub_group_id",
1627                  "get_sub_group_local_id"] in {
1628    def : Builtin<name, [UInt]>;
1629  }
1630  let MinVersion = CL20 in {
1631    foreach name = ["get_enqueued_num_sub_groups"] in {
1632      def : Builtin<name, [UInt]>;
1633    }
1634  }
1635}
1636
1637// --- Table 28.2.2 ---
1638let Extension = FuncExtKhrSubgroups in {
1639  def : Builtin<"sub_group_barrier", [Void, MemFenceFlags], Attr.Convergent>;
1640  def : Builtin<"sub_group_barrier", [Void, MemFenceFlags, MemoryScope], Attr.Convergent>;
1641}
1642
1643// --- Table 28.2.4 ---
1644let Extension = FuncExtKhrSubgroups in {
1645  foreach name = ["sub_group_all", "sub_group_any"] in {
1646    def : Builtin<name, [Int, Int], Attr.Convergent>;
1647  }
1648  foreach name = ["sub_group_broadcast"] in {
1649    def : Builtin<name, [IntLongFloatGenType1, IntLongFloatGenType1, UInt], Attr.Convergent>;
1650  }
1651  foreach name = ["sub_group_reduce_", "sub_group_scan_exclusive_",
1652                  "sub_group_scan_inclusive_"] in {
1653    foreach op = ["add", "min", "max"] in {
1654      def : Builtin<name # op, [IntLongFloatGenType1, IntLongFloatGenType1], Attr.Convergent>;
1655    }
1656  }
1657}
1658
1659// OpenCL Extension v3.0 s38 - Extended Subgroup Functions
1660
1661// Section 38.4.1 - cl_khr_subgroup_extended_types
1662let Extension = FuncExtKhrSubgroupExtendedTypes in {
1663  // For sub_group_broadcast, add scalar char, uchar, short, and ushort support,
1664  def : Builtin<"sub_group_broadcast", [CharShortGenType1, CharShortGenType1, UInt], Attr.Convergent>;
1665  // gentype may additionally be one of the supported built-in vector data types.
1666  def : Builtin<"sub_group_broadcast", [AGenTypeNNoScalar, AGenTypeNNoScalar, UInt], Attr.Convergent>;
1667
1668  foreach name = ["sub_group_reduce_", "sub_group_scan_exclusive_",
1669                  "sub_group_scan_inclusive_"] in {
1670    foreach op = ["add", "min", "max"] in {
1671      def : Builtin<name # op, [CharShortGenType1, CharShortGenType1], Attr.Convergent>;
1672    }
1673  }
1674}
1675
1676// Section 38.5.1 - cl_khr_subgroup_non_uniform_vote
1677let Extension = FuncExtKhrSubgroupNonUniformVote in {
1678  def : Builtin<"sub_group_elect", [Int]>;
1679  def : Builtin<"sub_group_non_uniform_all", [Int, Int]>;
1680  def : Builtin<"sub_group_non_uniform_any", [Int, Int]>;
1681  def : Builtin<"sub_group_non_uniform_all_equal", [Int, AGenType1]>;
1682}
1683
1684// Section 38.6.1 - cl_khr_subgroup_ballot
1685let Extension = FuncExtKhrSubgroupBallot in {
1686  def : Builtin<"sub_group_non_uniform_broadcast", [AGenTypeN, AGenTypeN, UInt]>;
1687  def : Builtin<"sub_group_broadcast_first", [AGenType1, AGenType1]>;
1688  def : Builtin<"sub_group_ballot", [VectorType<UInt, 4>, Int]>;
1689  def : Builtin<"sub_group_inverse_ballot", [Int, VectorType<UInt, 4>], Attr.Const>;
1690  def : Builtin<"sub_group_ballot_bit_extract", [Int, VectorType<UInt, 4>, UInt], Attr.Const>;
1691  def : Builtin<"sub_group_ballot_bit_count", [UInt, VectorType<UInt, 4>], Attr.Const>;
1692  def : Builtin<"sub_group_ballot_inclusive_scan", [UInt, VectorType<UInt, 4>]>;
1693  def : Builtin<"sub_group_ballot_exclusive_scan", [UInt, VectorType<UInt, 4>]>;
1694  def : Builtin<"sub_group_ballot_find_lsb", [UInt, VectorType<UInt, 4>]>;
1695  def : Builtin<"sub_group_ballot_find_msb", [UInt, VectorType<UInt, 4>]>;
1696
1697  foreach op = ["eq", "ge", "gt", "le", "lt"] in {
1698    def : Builtin<"get_sub_group_" # op # "_mask", [VectorType<UInt, 4>], Attr.Const>;
1699  }
1700}
1701
1702// Section 38.7.1 - cl_khr_subgroup_non_uniform_arithmetic
1703let Extension = FuncExtKhrSubgroupNonUniformArithmetic in {
1704  foreach name = ["reduce_", "scan_exclusive_", "scan_inclusive_"] in {
1705    foreach op = ["add", "min", "max", "mul"] in {
1706      def : Builtin<"sub_group_non_uniform_" # name # op, [AGenType1, AGenType1]>;
1707    }
1708    foreach op = ["and", "or", "xor"] in {
1709      def : Builtin<"sub_group_non_uniform_" # name # op, [AIGenType1, AIGenType1]>;
1710    }
1711    foreach op = ["and", "or", "xor"] in {
1712      def : Builtin<"sub_group_non_uniform_" # name # "logical_" # op, [Int, Int]>;
1713    }
1714  }
1715}
1716
1717// Section 38.8.1 - cl_khr_subgroup_shuffle
1718let Extension = FuncExtKhrSubgroupShuffle in {
1719  def : Builtin<"sub_group_shuffle", [AGenType1, AGenType1, UInt]>;
1720  def : Builtin<"sub_group_shuffle_xor", [AGenType1, AGenType1, UInt]>;
1721}
1722
1723// Section 38.9.1 - cl_khr_subgroup_shuffle_relative
1724let Extension = FuncExtKhrSubgroupShuffleRelative in {
1725  def : Builtin<"sub_group_shuffle_up", [AGenType1, AGenType1, UInt]>;
1726  def : Builtin<"sub_group_shuffle_down", [AGenType1, AGenType1, UInt]>;
1727}
1728
1729// Section 38.10.1 - cl_khr_subgroup_clustered_reduce
1730let Extension = FuncExtKhrSubgroupClusteredReduce in {
1731  foreach op = ["add", "min", "max", "mul"] in {
1732    def : Builtin<"sub_group_clustered_reduce_" # op, [AGenType1, AGenType1, UInt]>;
1733  }
1734  foreach op = ["and", "or", "xor"] in {
1735    def : Builtin<"sub_group_clustered_reduce_" # op, [AIGenType1, AIGenType1, UInt]>;
1736  }
1737  foreach op = ["and", "or", "xor"] in {
1738    def : Builtin<"sub_group_clustered_reduce_logical_" # op, [Int, Int, UInt]>;
1739  }
1740}
1741
1742// Section 40.3.1 - cl_khr_extended_bit_ops
1743let Extension = FuncExtKhrExtendedBitOps in {
1744  def : Builtin<"bitfield_insert", [AIGenTypeN, AIGenTypeN, AIGenTypeN, UInt, UInt], Attr.Const>;
1745  def : Builtin<"bitfield_extract_signed", [SGenTypeN, SGenTypeN, UInt, UInt], Attr.Const>;
1746  def : Builtin<"bitfield_extract_signed", [SGenTypeN, UGenTypeN, UInt, UInt], Attr.Const>;
1747  def : Builtin<"bitfield_extract_unsigned", [UGenTypeN, SGenTypeN, UInt, UInt], Attr.Const>;
1748  def : Builtin<"bitfield_extract_unsigned", [UGenTypeN, UGenTypeN, UInt, UInt], Attr.Const>;
1749  def : Builtin<"bit_reverse", [AIGenTypeN, AIGenTypeN], Attr.Const>;
1750}
1751
1752// Section 42.3 - cl_khr_integer_dot_product
1753let Extension = FunctionExtension<"__opencl_c_integer_dot_product_input_4x8bit"> in {
1754  def : Builtin<"dot", [UInt, VectorType<UChar, 4>, VectorType<UChar, 4>], Attr.Const>;
1755  def : Builtin<"dot", [Int, VectorType<Char, 4>, VectorType<Char, 4>], Attr.Const>;
1756  def : Builtin<"dot", [Int, VectorType<UChar, 4>, VectorType<Char, 4>], Attr.Const>;
1757  def : Builtin<"dot", [Int, VectorType<Char, 4>, VectorType<UChar, 4>], Attr.Const>;
1758
1759  def : Builtin<"dot_acc_sat", [UInt, VectorType<UChar, 4>, VectorType<UChar, 4>, UInt], Attr.Const>;
1760  def : Builtin<"dot_acc_sat", [Int, VectorType<Char, 4>, VectorType<Char, 4>, Int], Attr.Const>;
1761  def : Builtin<"dot_acc_sat", [Int, VectorType<UChar, 4>, VectorType<Char, 4>, Int], Attr.Const>;
1762  def : Builtin<"dot_acc_sat", [Int, VectorType<Char, 4>, VectorType<UChar, 4>, Int], Attr.Const>;
1763}
1764
1765let Extension = FunctionExtension<"__opencl_c_integer_dot_product_input_4x8bit_packed"> in {
1766  def : Builtin<"dot_4x8packed_uu_uint", [UInt, UInt, UInt], Attr.Const>;
1767  def : Builtin<"dot_4x8packed_ss_int", [Int, UInt, UInt], Attr.Const>;
1768  def : Builtin<"dot_4x8packed_us_int", [Int, UInt, UInt], Attr.Const>;
1769  def : Builtin<"dot_4x8packed_su_int", [Int, UInt, UInt], Attr.Const>;
1770
1771  def : Builtin<"dot_acc_sat_4x8packed_uu_uint", [UInt, UInt, UInt, UInt], Attr.Const>;
1772  def : Builtin<"dot_acc_sat_4x8packed_ss_int", [Int, UInt, UInt, Int], Attr.Const>;
1773  def : Builtin<"dot_acc_sat_4x8packed_us_int", [Int, UInt, UInt, Int], Attr.Const>;
1774  def : Builtin<"dot_acc_sat_4x8packed_su_int", [Int, UInt, UInt, Int], Attr.Const>;
1775}
1776
1777//--------------------------------------------------------------------
1778// Arm extensions.
1779let Extension = ArmIntegerDotProductInt8 in {
1780  foreach name = ["arm_dot"] in {
1781    def : Builtin<name, [UInt, VectorType<UChar, 4>, VectorType<UChar, 4>]>;
1782    def : Builtin<name, [Int, VectorType<Char, 4>, VectorType<Char, 4>]>;
1783  }
1784}
1785let Extension = ArmIntegerDotProductAccumulateInt8 in {
1786  foreach name = ["arm_dot_acc"] in {
1787    def : Builtin<name, [UInt, VectorType<UChar, 4>, VectorType<UChar, 4>, UInt]>;
1788    def : Builtin<name, [Int, VectorType<Char, 4>, VectorType<Char, 4>, Int]>;
1789  }
1790}
1791let Extension = ArmIntegerDotProductAccumulateInt16 in {
1792  foreach name = ["arm_dot_acc"] in {
1793    def : Builtin<name, [UInt, VectorType<UShort, 2>, VectorType<UShort, 2>, UInt]>;
1794    def : Builtin<name, [Int, VectorType<Short, 2>, VectorType<Short, 2>, Int]>;
1795  }
1796}
1797let Extension = ArmIntegerDotProductAccumulateSaturateInt8 in {
1798  foreach name = ["arm_dot_acc_sat"] in {
1799    def : Builtin<name, [UInt, VectorType<UChar, 4>, VectorType<UChar, 4>, UInt]>;
1800    def : Builtin<name, [Int, VectorType<Char, 4>, VectorType<Char, 4>, Int]>;
1801  }
1802}
1803