1 //==--- OpenCLBuiltins.td - OpenCL builtin declarations -------------------===//
3 // The LLVM Compiler Infrastructure
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
9 //===----------------------------------------------------------------------===//
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
16 //===----------------------------------------------------------------------===//
18 //===----------------------------------------------------------------------===//
19 // Definitions of miscellaneous basic entities.
20 //===----------------------------------------------------------------------===//
22 class Version<int _Version> {
23 int Version = _Version;
25 def CL10: Version<100>;
26 def CL11: Version<110>;
27 def CL12: Version<120>;
28 def CL20: Version<200>;
31 // Pointer types need to be assigned an address space.
32 class AddressSpace<string _AS> {
33 string AddrSpace = _AS;
35 def default_as : AddressSpace<"clang::LangAS::Default">;
36 def private_as : AddressSpace<"clang::LangAS::opencl_private">;
37 def global_as : AddressSpace<"clang::LangAS::opencl_global">;
38 def constant_as : AddressSpace<"clang::LangAS::opencl_constant">;
39 def local_as : AddressSpace<"clang::LangAS::opencl_local">;
40 def generic_as : AddressSpace<"clang::LangAS::opencl_generic">;
43 // Qualified Type. Allow to retrieve one ASTContext QualType.
44 class QualType<string _Name> {
45 // Name of the field or function in a clang::ASTContext
46 // E.g. Name="IntTy" for the int type, and "getIntPtrType()" for an intptr_t
50 // Helper class to store type access qualifiers (volatile, const, ...).
51 class Qualifier<string _QualName> {
52 string QualName = _QualName;
55 //===----------------------------------------------------------------------===//
56 // OpenCL C classes for types
57 //===----------------------------------------------------------------------===//
58 // OpenCL types (int, float, ...)
59 class Type<string _Name, QualType _QTName> {
62 // QualType associated with this type
63 QualType QTName = _QTName;
64 // Size of the vector (if applicable)
68 // List of qualifiers associated with the type (volatile, ...)
69 list<Qualifier> QualList = [];
71 string AddrSpace = "clang::LangAS::Default";
72 // Access qualifier. Must be one of ("RO", "WO", "RW").
73 string AccessQualifier = "";
76 // OpenCL vector types (e.g. int2, int3, int16, float8, ...)
77 class VectorType<Type _Ty, int _VecWidth> : Type<_Ty.Name, _Ty.QTName> {
78 int VecWidth = _VecWidth;
81 // OpenCL pointer types (e.g. int*, float*, ...)
82 class PointerType<Type _Ty, AddressSpace _AS = global_as> :
83 Type<_Ty.Name, _Ty.QTName> {
85 string AddrSpace = _AS.AddrSpace;
88 // OpenCL image types (e.g. image2d_t, ...)
89 class ImageType<Type _Ty, QualType _QTName, string _AccessQualifier> :
90 Type<_Ty.Name, _QTName> {
91 let AccessQualifier = _AccessQualifier;
94 //===----------------------------------------------------------------------===//
95 // OpenCL C class for builtin functions
96 //===----------------------------------------------------------------------===//
97 class Builtin<string _Name, list<Type> _Signature> {
98 // Name of the builtin function
100 // List of types used by the function. The first one is the return type and
101 // the following are the arguments. The list must have at least one element
102 // (the return type).
103 list<Type> Signature = _Signature;
104 // OpenCL Extension to which the function belongs (cl_khr_subgroups, ...)
105 string Extension = "";
106 // OpenCL Version to which the function belongs (CL10, ...)
107 Version Version = CL10;
110 //===----------------------------------------------------------------------===//
111 // Multiclass definitions
112 //===----------------------------------------------------------------------===//
113 // multiclass BifN: Creates Builtin class instances for OpenCL builtin
114 // functions with N arguments.
115 // _Name : Name of the function
116 // _Signature : Signature of the function (list of the Type used by the
117 // function, the first one being the return type).
118 // _IsVector : List of bit indicating if the type in the _Signature at the
119 // same index is to be a vector in the multiple overloads. The
120 // list must have at least one non-zero value.
121 multiclass Bif0<string _Name, list<Type> _Signature, list<bit> _IsVector> {
122 def : Builtin<_Name, _Signature>;
123 foreach v = [2, 3, 4, 8, 16] in {
125 [!if(_IsVector[0], VectorType<_Signature[0], v>, _Signature[0])]>;
128 multiclass Bif1<string _Name, list<Type> _Signature, list<bit> _IsVector> {
129 def : Builtin<_Name, _Signature>;
130 foreach v = [2, 3, 4, 8, 16] in {
132 [!if(_IsVector[0], VectorType<_Signature[0], v>, _Signature[0]),
133 !if(_IsVector[1], VectorType<_Signature[1], v>, _Signature[1])]>;
136 multiclass Bif2<string _Name, list<Type> _Signature, list<bit> _IsVector> {
137 def : Builtin<_Name, _Signature>;
138 foreach v = [2, 3, 4, 8, 16] in {
140 [!if(_IsVector[0], VectorType<_Signature[0], v>, _Signature[0]),
141 !if(_IsVector[1], VectorType<_Signature[1], v>, _Signature[1]),
142 !if(_IsVector[2], VectorType<_Signature[2], v>, _Signature[2])]>;
145 multiclass Bif3<string _Name, list<Type> _Signature, list<bit> _IsVector> {
146 def : Builtin<_Name, _Signature>;
147 foreach v = [2, 3, 4, 8, 16] in {
149 [!if(_IsVector[0], VectorType<_Signature[0], v>, _Signature[0]),
150 !if(_IsVector[1], VectorType<_Signature[1], v>, _Signature[1]),
151 !if(_IsVector[2], VectorType<_Signature[2], v>, _Signature[2]),
152 !if(_IsVector[3], VectorType<_Signature[3], v>, _Signature[3])]>;
155 //===----------------------------------------------------------------------===//
156 // Definitions of OpenCL C types
157 //===----------------------------------------------------------------------===//
158 // OpenCL v1.2 s6.1.1: Built-in Scalar Data Types
159 def bool_t : Type<"bool", QualType<"BoolTy">>;
160 def char_t : Type<"char", QualType<"CharTy">>;
161 def uchar_t : Type<"uchar", QualType<"UnsignedCharTy">>;
162 def short_t : Type<"short", QualType<"ShortTy">>;
163 def ushort_t : Type<"ushort", QualType<"UnsignedShortTy">>;
164 def int_t : Type<"int", QualType<"IntTy">>;
165 def uint_t : Type<"uint", QualType<"UnsignedIntTy">>;
166 def long_t : Type<"long", QualType<"LongTy">>;
167 def ulong_t : Type<"ulong", QualType<"UnsignedLongTy">>;
168 def float_t : Type<"float", QualType<"FloatTy">>;
169 def double_t : Type<"double", QualType<"DoubleTy">>;
170 def half_t : Type<"half", QualType<"HalfTy">>;
171 def size_t : Type<"size_t", QualType<"getSizeType()">>;
172 def ptrdiff_t : Type<"ptrdiff_t", QualType<"getPointerDiffType()">>;
173 def intptr_t : Type<"intptr_t", QualType<"getIntPtrType()">>;
174 def uintptr_t : Type<"uintptr_t", QualType<"getUIntPtrType()">>;
175 def void_t : Type<"void", QualType<"VoidTy">>;
177 // OpenCL v1.2 s6.1.2: Built-in Vector Data Types
178 foreach v = [2, 3, 4, 8, 16] in {
179 def char#v#_t : VectorType<char_t, v>;
180 def uchar#v#_t : VectorType<uchar_t, v>;
181 def short#v#_t : VectorType<short_t, v>;
182 def ushort#v#_t : VectorType<ushort_t, v>;
183 def "int"#v#_t : VectorType<int_t, v>;
184 def uint#v#_t : VectorType<uint_t, v>;
185 def long#v#_t : VectorType<long_t, v>;
186 def ulong#v#_t : VectorType<ulong_t, v>;
187 def float#v#_t : VectorType<float_t, v>;
188 def double#v#_t : VectorType<double_t, v>;
189 def half#v#_t : VectorType<half_t, v>;
192 // OpenCL v1.2 s6.1.3: Other Built-in Data Types
193 // These definitions with a "null" name are "abstract". They should not
194 // be used in definitions of Builtin functions.
195 def image2d_t : Type<"image2d_t", QualType<"null">>;
196 def image3d_t : Type<"image3d_t", QualType<"null">>;
197 def image2d_array_t : Type<"image2d_array_t", QualType<"null">>;
198 def image1d_t : Type<"image1d_t", QualType<"null">>;
199 def image1d_buffer_t : Type<"image1d_buffer_t", QualType<"null">>;
200 def image1d_array_t : Type<"image1d_array_t", QualType<"null">>;
201 // Unlike the few functions above, the following definitions can be used
202 // in definitions of Builtin functions (they have a QualType with a name).
203 foreach v = ["RO", "WO", "RW"] in {
204 def image2d_#v#_t : ImageType<image2d_t,
205 QualType<"OCLImage2d"#v#"Ty">,
207 def image3d_#v#_t : ImageType<image3d_t,
208 QualType<"OCLImage3d"#v#"Ty">,
210 def image2d_array#v#_t : ImageType<image2d_array_t,
211 QualType<"OCLImage2dArray"#v#"Ty">,
213 def image1d_#v#_t : ImageType<image1d_t,
214 QualType<"OCLImage1d"#v#"Ty">,
216 def image1d_buffer#v#_t : ImageType<image1d_buffer_t,
217 QualType<"OCLImage1dBuffer"#v#"Ty">,
219 def image1d_array#v#_t : ImageType<image1d_array_t,
220 QualType<"OCLImage1dArray"#v#"Ty">,
224 def sampler_t : Type<"sampler_t", QualType<"OCLSamplerTy">>;
225 def event_t : Type<"event_t", QualType<"OCLEventTy">>;
227 //===----------------------------------------------------------------------===//
228 // Definitions of OpenCL builtin functions
229 //===----------------------------------------------------------------------===//
230 // OpenCL v1.2 s6.2.3: Explicit Conversions
231 // Generate the convert_ builtins.
232 foreach RType = [float_t, double_t, char_t, uchar_t, short_t, ushort_t,
233 int_t, uint_t, long_t, ulong_t] in {
234 foreach IType = [float_t, double_t, char_t, uchar_t, short_t, ushort_t,
235 int_t, uint_t, long_t, ulong_t] in {
236 foreach sat = ["", "_sat"] in {
237 foreach rte = ["", "_rte", "_rtz", "_rtp", "_rtn"] in {
238 def : Builtin<"convert_" # RType.Name # sat # rte, [RType, IType]>;
239 foreach v = [2, 3, 4, 8, 16] in {
240 def : Builtin<"convert_" # RType.Name # v # sat # rte,
241 [VectorType<RType, v>,
242 VectorType<IType, v>]>;
249 // OpenCL v1.2 s6.12.1: Work-Item Functions
250 def get_work_dim : Builtin<"get_work_dim", [uint_t]>;
251 foreach name = ["get_global_size", "get_global_id", "get_local_size",
252 "get_local_id", "get_num_groups", "get_group_id",
253 "get_global_offset"] in {
254 def : Builtin<name, [size_t, uint_t]>;
257 // OpenCL v1.2 s6.12.2: Math Functions
258 foreach name = ["acos", "acosh", "acospi",
259 "asin", "asinh", "asinpi",
260 "atan", "atanh", "atanpi"] in {
261 foreach type = [float_t, double_t, half_t] in {
262 defm : Bif1<name, [type, type], [1, 1]>;
266 foreach name = ["atan2", "atan2pi"] in {
267 foreach type = [float_t, double_t, half_t] in {
268 defm : Bif2<name, [type, type, type], [1, 1, 1]>;
272 foreach name = ["fmax", "fmin"] in {
273 foreach type = [float_t, double_t, half_t] in {
274 defm : Bif2<name, [type, type, type], [1, 1, 1]>;
275 defm : Bif2<name, [type, type, type], [1, 1, 0]>;
279 // OpenCL v1.2 s6.12.14: Built-in Image Read Functions
280 def read_imagef : Builtin<"read_imagef",
281 [float4_t, image2d_RO_t, VectorType<int_t, 2>]>;
282 def write_imagef : Builtin<"write_imagef",
285 VectorType<int_t, 2>,
286 VectorType<float_t, 4>]>;
289 // OpenCL v2.0 s9.17.3: Additions to section 6.13.1: Work-Item Functions
290 let Version = CL20 in {
291 let Extension = "cl_khr_subgroups" in {
292 def get_sub_group_size : Builtin<"get_sub_group_size", [uint_t]>;
293 def get_max_sub_group_size : Builtin<"get_max_sub_group_size", [uint_t]>;
294 def get_num_sub_groups : Builtin<"get_num_sub_groups", [uint_t]>;