xref: /freebsd-src/contrib/llvm-project/clang/lib/Sema/OpenCLBuiltins.td (revision 0b57cec536236d46e3dba9bd041533462f33dbb7)
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 Version = _Version;
24}
25def CL10: Version<100>;
26def CL11: Version<110>;
27def CL12: Version<120>;
28def CL20: Version<200>;
29
30// Address spaces
31// Pointer types need to be assigned an address space.
32class AddressSpace<string _AS> {
33  string AddrSpace = _AS;
34}
35def default_as    : AddressSpace<"clang::LangAS::Default">;
36def private_as    : AddressSpace<"clang::LangAS::opencl_private">;
37def global_as     : AddressSpace<"clang::LangAS::opencl_global">;
38def constant_as   : AddressSpace<"clang::LangAS::opencl_constant">;
39def local_as      : AddressSpace<"clang::LangAS::opencl_local">;
40def generic_as    : AddressSpace<"clang::LangAS::opencl_generic">;
41
42
43// Qualified Type. Allow to retrieve one ASTContext QualType.
44class 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
47  string Name = _Name;
48}
49
50// Helper class to store type access qualifiers (volatile, const, ...).
51class Qualifier<string _QualName> {
52  string QualName = _QualName;
53}
54
55//===----------------------------------------------------------------------===//
56//                      OpenCL C classes for types
57//===----------------------------------------------------------------------===//
58// OpenCL types (int, float, ...)
59class Type<string _Name, QualType _QTName> {
60  // Name of the Type
61  string Name = _Name;
62  // QualType associated with this type
63  QualType QTName = _QTName;
64  // Size of the vector (if applicable)
65  int VecWidth = 0;
66  // Is pointer
67  bit IsPointer = 0;
68  // List of qualifiers associated with the type (volatile, ...)
69  list<Qualifier> QualList = [];
70  // Address space
71  string AddrSpace = "clang::LangAS::Default";
72  // Access qualifier. Must be one of ("RO", "WO", "RW").
73  string AccessQualifier = "";
74}
75
76// OpenCL vector types (e.g. int2, int3, int16, float8, ...)
77class VectorType<Type _Ty, int _VecWidth> : Type<_Ty.Name, _Ty.QTName> {
78  int VecWidth = _VecWidth;
79}
80
81// OpenCL pointer types (e.g. int*, float*, ...)
82class PointerType<Type _Ty, AddressSpace _AS = global_as> :
83                                      Type<_Ty.Name, _Ty.QTName> {
84  bit IsPointer = 1;
85  string AddrSpace = _AS.AddrSpace;
86}
87
88// OpenCL image types (e.g. image2d_t, ...)
89class ImageType<Type _Ty, QualType _QTName, string _AccessQualifier> :
90                                              Type<_Ty.Name, _QTName> {
91  let AccessQualifier = _AccessQualifier;
92}
93
94//===----------------------------------------------------------------------===//
95//                      OpenCL C class for builtin functions
96//===----------------------------------------------------------------------===//
97class Builtin<string _Name, list<Type> _Signature> {
98  // Name of the builtin function
99  string Name = _Name;
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;
108}
109
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.
121multiclass Bif0<string _Name, list<Type> _Signature, list<bit> _IsVector> {
122  def : Builtin<_Name, _Signature>;
123  foreach v = [2, 3, 4, 8, 16] in {
124    def : Builtin<_Name,
125                  [!if(_IsVector[0], VectorType<_Signature[0], v>, _Signature[0])]>;
126  }
127}
128multiclass Bif1<string _Name, list<Type> _Signature, list<bit> _IsVector> {
129  def : Builtin<_Name, _Signature>;
130  foreach v = [2, 3, 4, 8, 16] in {
131    def : Builtin<_Name,
132                  [!if(_IsVector[0], VectorType<_Signature[0], v>, _Signature[0]),
133                  !if(_IsVector[1], VectorType<_Signature[1], v>, _Signature[1])]>;
134  }
135}
136multiclass Bif2<string _Name, list<Type> _Signature, list<bit> _IsVector> {
137  def : Builtin<_Name, _Signature>;
138  foreach v = [2, 3, 4, 8, 16] in {
139    def : Builtin<_Name,
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])]>;
143  }
144}
145multiclass Bif3<string _Name, list<Type> _Signature, list<bit> _IsVector> {
146  def : Builtin<_Name, _Signature>;
147  foreach v = [2, 3, 4, 8, 16] in {
148    def : Builtin<_Name,
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])]>;
153  }
154}
155//===----------------------------------------------------------------------===//
156//                 Definitions of OpenCL C types
157//===----------------------------------------------------------------------===//
158// OpenCL v1.2 s6.1.1: Built-in Scalar Data Types
159def bool_t      : Type<"bool", QualType<"BoolTy">>;
160def char_t      : Type<"char", QualType<"CharTy">>;
161def uchar_t     : Type<"uchar", QualType<"UnsignedCharTy">>;
162def short_t     : Type<"short", QualType<"ShortTy">>;
163def ushort_t    : Type<"ushort", QualType<"UnsignedShortTy">>;
164def int_t       : Type<"int", QualType<"IntTy">>;
165def uint_t      : Type<"uint", QualType<"UnsignedIntTy">>;
166def long_t      : Type<"long", QualType<"LongTy">>;
167def ulong_t     : Type<"ulong", QualType<"UnsignedLongTy">>;
168def float_t     : Type<"float", QualType<"FloatTy">>;
169def double_t    : Type<"double", QualType<"DoubleTy">>;
170def half_t      : Type<"half", QualType<"HalfTy">>;
171def size_t      : Type<"size_t",  QualType<"getSizeType()">>;
172def ptrdiff_t   : Type<"ptrdiff_t", QualType<"getPointerDiffType()">>;
173def intptr_t    : Type<"intptr_t", QualType<"getIntPtrType()">>;
174def uintptr_t   : Type<"uintptr_t", QualType<"getUIntPtrType()">>;
175def void_t      : Type<"void", QualType<"VoidTy">>;
176
177// OpenCL v1.2 s6.1.2: Built-in Vector Data Types
178foreach 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>;
190}
191
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.
195def image2d_t         : Type<"image2d_t", QualType<"null">>;
196def image3d_t         : Type<"image3d_t", QualType<"null">>;
197def image2d_array_t   : Type<"image2d_array_t", QualType<"null">>;
198def image1d_t         : Type<"image1d_t", QualType<"null">>;
199def image1d_buffer_t  : Type<"image1d_buffer_t", QualType<"null">>;
200def 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).
203foreach v = ["RO", "WO", "RW"] in {
204  def image2d_#v#_t       : ImageType<image2d_t,
205                                      QualType<"OCLImage2d"#v#"Ty">,
206                                      v>;
207  def image3d_#v#_t       : ImageType<image3d_t,
208                                      QualType<"OCLImage3d"#v#"Ty">,
209                                      v>;
210  def image2d_array#v#_t  : ImageType<image2d_array_t,
211                                      QualType<"OCLImage2dArray"#v#"Ty">,
212                                      v>;
213  def image1d_#v#_t       : ImageType<image1d_t,
214                                      QualType<"OCLImage1d"#v#"Ty">,
215                                      v>;
216  def image1d_buffer#v#_t : ImageType<image1d_buffer_t,
217                                      QualType<"OCLImage1dBuffer"#v#"Ty">,
218                                      v>;
219  def image1d_array#v#_t  : ImageType<image1d_array_t,
220                                      QualType<"OCLImage1dArray"#v#"Ty">,
221                                      v>;
222}
223
224def sampler_t         : Type<"sampler_t", QualType<"OCLSamplerTy">>;
225def event_t           : Type<"event_t", QualType<"OCLEventTy">>;
226
227//===----------------------------------------------------------------------===//
228//                 Definitions of OpenCL builtin functions
229//===----------------------------------------------------------------------===//
230// OpenCL v1.2 s6.2.3: Explicit Conversions
231// Generate the convert_ builtins.
232foreach 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>]>;
243        }
244      }
245    }
246  }
247}
248
249// OpenCL v1.2 s6.12.1: Work-Item Functions
250def get_work_dim : Builtin<"get_work_dim", [uint_t]>;
251foreach 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]>;
255}
256
257// OpenCL v1.2 s6.12.2: Math Functions
258foreach 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]>;
263  }
264}
265
266foreach name = ["atan2", "atan2pi"] in {
267  foreach type = [float_t, double_t, half_t] in {
268    defm : Bif2<name, [type, type, type], [1, 1, 1]>;
269  }
270}
271
272foreach 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]>;
276  }
277}
278
279// OpenCL v1.2 s6.12.14: Built-in Image Read Functions
280def read_imagef : Builtin<"read_imagef",
281                          [float4_t, image2d_RO_t, VectorType<int_t, 2>]>;
282def write_imagef : Builtin<"write_imagef",
283                           [void_t,
284                            image2d_WO_t,
285                            VectorType<int_t, 2>,
286                            VectorType<float_t, 4>]>;
287
288
289// OpenCL v2.0 s9.17.3: Additions to section 6.13.1: Work-Item Functions
290let 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]>;
295  }
296}
297