Skip to content

Commit 0e70c35

Browse files
committed
[OpenCL] Add integer builtin functions
This patch adds the integer builtin functions from the OpenCL C specification. Patch by Pierre Gondois and Sven van Haastregt. Differential Revision: https://reviews.llvm.org/D69901
1 parent 87bc320 commit 0e70c35

File tree

2 files changed

+70
-11
lines changed

2 files changed

+70
-11
lines changed

clang/lib/Sema/OpenCLBuiltins.td

Lines changed: 62 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,11 @@ def Vec1 : IntList<"Vec1", [1]>;
279279
def TLAll : TypeList<"TLAll", [Char, UChar, Short, UShort, Int, UInt, Long, ULong, Float, Double, Half]>;
280280
def TLFloat : TypeList<"TLFloat", [Float, Double, Half]>;
281281

282+
// All unsigned integer types twice, to facilitate unsigned return types for e.g.
283+
// uchar abs(char) and
284+
// uchar abs(uchar).
285+
def TLAllUIntsTwice : TypeList<"TLAllUIntsTwice", [UChar, UChar, UShort, UShort, UInt, UInt, ULong, ULong]>;
286+
282287
def TLAllInts : TypeList<"TLAllInts", [Char, UChar, Short, UShort, Int, UInt, Long, ULong]>;
283288

284289
// GenType definitions for multiple base types (e.g. all floating point types,
@@ -290,6 +295,8 @@ def AGenTypeNNoScalar : GenericType<"AGenTypeNNoScalar", TLAll, VecNoScalar
290295
def AIGenType1 : GenericType<"AIGenType1", TLAllInts, Vec1>;
291296
def AIGenTypeN : GenericType<"AIGenTypeN", TLAllInts, VecAndScalar>;
292297
def AIGenTypeNNoScalar : GenericType<"AIGenTypeNNoScalar", TLAllInts, VecNoScalar>;
298+
// All integer to unsigned
299+
def AI2UGenTypeN : GenericType<"AI2UGenTypeN", TLAllUIntsTwice, VecAndScalar>;
293300
// Float
294301
def FGenTypeN : GenericType<"FGenTypeN", TLFloat, VecAndScalar>;
295302

@@ -466,6 +473,61 @@ foreach name = ["half_divide", "half_powr",
466473
def : Builtin<name, [GenTypeFloatVecAndScalar, GenTypeFloatVecAndScalar, GenTypeFloatVecAndScalar], Attr.Const>;
467474
}
468475

476+
//--------------------------------------------------------------------
477+
// OpenCL v1.1 s6.11.3, v1.2 s6.12.3, v2.0 s6.13.3 - Integer Functions
478+
// --- Table 10 ---
479+
// --- 1 argument ---
480+
foreach name = ["abs"] in {
481+
def : Builtin<name, [AI2UGenTypeN, AIGenTypeN], Attr.Const>;
482+
}
483+
foreach name = ["clz", "popcount"] in {
484+
def : Builtin<name, [AIGenTypeN, AIGenTypeN], Attr.Const>;
485+
}
486+
let MinVersion = CL20 in {
487+
foreach name = ["ctz"] in {
488+
def : Builtin<name, [AIGenTypeN, AIGenTypeN]>;
489+
}
490+
}
491+
492+
// --- 2 arguments ---
493+
foreach name = ["abs_diff"] in {
494+
def : Builtin<name, [AI2UGenTypeN, AIGenTypeN, AIGenTypeN], Attr.Const>;
495+
}
496+
foreach name = ["add_sat", "hadd", "rhadd", "mul_hi", "rotate", "sub_sat"] in {
497+
def : Builtin<name, [AIGenTypeN, AIGenTypeN, AIGenTypeN], Attr.Const>;
498+
}
499+
foreach name = ["max", "min"] in {
500+
def : Builtin<name, [AIGenTypeN, AIGenTypeN, AIGenTypeN], Attr.Const>;
501+
def : Builtin<name, [AIGenTypeNNoScalar, AIGenTypeNNoScalar, AIGenType1], Attr.Const>;
502+
}
503+
foreach name = ["upsample"] in {
504+
def : Builtin<name, [GenTypeShortVecAndScalar, GenTypeCharVecAndScalar, GenTypeUCharVecAndScalar], Attr.Const>;
505+
def : Builtin<name, [GenTypeUShortVecAndScalar, GenTypeUCharVecAndScalar, GenTypeUCharVecAndScalar], Attr.Const>;
506+
def : Builtin<name, [GenTypeIntVecAndScalar, GenTypeShortVecAndScalar, GenTypeUShortVecAndScalar], Attr.Const>;
507+
def : Builtin<name, [GenTypeUIntVecAndScalar, GenTypeUShortVecAndScalar, GenTypeUShortVecAndScalar], Attr.Const>;
508+
def : Builtin<name, [GenTypeLongVecAndScalar, GenTypeIntVecAndScalar, GenTypeUIntVecAndScalar], Attr.Const>;
509+
def : Builtin<name, [GenTypeULongVecAndScalar, GenTypeUIntVecAndScalar, GenTypeUIntVecAndScalar], Attr.Const>;
510+
}
511+
512+
// --- 3 arguments ---
513+
foreach name = ["clamp"] in {
514+
def : Builtin<name, [AIGenTypeN, AIGenTypeN, AIGenTypeN, AIGenTypeN], Attr.Const>;
515+
def : Builtin<name, [AIGenTypeNNoScalar, AIGenTypeNNoScalar, AIGenType1, AIGenType1], Attr.Const>;
516+
}
517+
foreach name = ["mad_hi", "mad_sat"] in {
518+
def : Builtin<name, [AIGenTypeN, AIGenTypeN, AIGenTypeN, AIGenTypeN], Attr.Const>;
519+
}
520+
521+
// --- Table 11 ---
522+
foreach name = ["mad24"] in {
523+
def : Builtin<name, [GenTypeIntVecAndScalar, GenTypeIntVecAndScalar, GenTypeIntVecAndScalar, GenTypeIntVecAndScalar], Attr.Const>;
524+
def : Builtin<name, [GenTypeUIntVecAndScalar, GenTypeUIntVecAndScalar, GenTypeUIntVecAndScalar, GenTypeUIntVecAndScalar], Attr.Const>;
525+
}
526+
foreach name = ["mul24"] in {
527+
def : Builtin<name, [GenTypeIntVecAndScalar, GenTypeIntVecAndScalar, GenTypeIntVecAndScalar], Attr.Const>;
528+
def : Builtin<name, [GenTypeUIntVecAndScalar, GenTypeUIntVecAndScalar, GenTypeUIntVecAndScalar], Attr.Const>;
529+
}
530+
469531
//--------------------------------------------------------------------
470532
// OpenCL v1.1 s6.11.4, v1.2 s6.12.4, v2.0 s6.13.4 - Common Functions
471533
// OpenCL Extension v2.0 s5.1.3 and s6.1.3 - Common Functions
@@ -655,12 +717,6 @@ foreach Type = [Int, UInt] in {
655717
}
656718
}
657719

658-
// OpenCL v1.1 s6.11.3, v1.2 s6.12.3, v2.0 s6.13.3 - Integer Functions
659-
foreach name = ["max", "min"] in {
660-
def : Builtin<name, [AIGenTypeN, AIGenTypeN, AIGenTypeN], Attr.Const>;
661-
def : Builtin<name, [AIGenTypeNNoScalar, AIGenTypeNNoScalar, AIGenType1], Attr.Const>;
662-
}
663-
664720
//--------------------------------------------------------------------
665721
// OpenCL v1.1 s6.11.3, v1.2 s6.12.14, v2.0 s6.13.14: Image Read and Write Functions
666722
// OpenCL Extension v2.0 s5.1.8 and s6.1.8: Image Read and Write Functions

clang/test/SemaOpenCL/fdeclare-opencl-builtins.cl

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,19 @@
2020

2121
// Provide typedefs when invoking clang without -finclude-default-header.
2222
#ifdef NO_HEADER
23+
typedef unsigned char uchar;
24+
typedef unsigned int uint;
25+
typedef unsigned long ulong;
26+
typedef unsigned short ushort;
27+
typedef __SIZE_TYPE__ size_t;
2328
typedef char char2 __attribute__((ext_vector_type(2)));
2429
typedef char char4 __attribute__((ext_vector_type(4)));
30+
typedef uchar uchar4 __attribute__((ext_vector_type(4)));
2531
typedef float float4 __attribute__((ext_vector_type(4)));
2632
typedef half half4 __attribute__((ext_vector_type(4)));
2733
typedef int int2 __attribute__((ext_vector_type(2)));
2834
typedef int int4 __attribute__((ext_vector_type(4)));
2935
typedef long long2 __attribute__((ext_vector_type(2)));
30-
typedef unsigned char uchar;
31-
typedef unsigned int uint;
32-
typedef unsigned long ulong;
33-
typedef unsigned short ushort;
34-
typedef __SIZE_TYPE__ size_t;
3536
#endif
3637

3738
kernel void test_pointers(volatile global void *global_p, global const int4 *a) {
@@ -61,6 +62,8 @@ kernel void basic_conversion() {
6162
char4 test_int(char c, char4 c4) {
6263
char m = max(c, c);
6364
char4 m4 = max(c4, c4);
65+
uchar4 abs1 = abs(c4);
66+
uchar4 abs2 = abs(abs1);
6467
return max(c4, c);
6568
}
6669

0 commit comments

Comments
 (0)