Author: Sven van Haastregt
Date: 2019-11-07T14:59:33Z
New Revision: 0e70c350943f1a927f481529717c4f98a465777b

URL: 
https://github.com/llvm/llvm-project/commit/0e70c350943f1a927f481529717c4f98a465777b
DIFF: 
https://github.com/llvm/llvm-project/commit/0e70c350943f1a927f481529717c4f98a465777b.diff

LOG: [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

Added: 
    

Modified: 
    clang/lib/Sema/OpenCLBuiltins.td
    clang/test/SemaOpenCL/fdeclare-opencl-builtins.cl

Removed: 
    


################################################################################
diff  --git a/clang/lib/Sema/OpenCLBuiltins.td 
b/clang/lib/Sema/OpenCLBuiltins.td
index f31cb26e2c17..40070cc4407f 100644
--- a/clang/lib/Sema/OpenCLBuiltins.td
+++ b/clang/lib/Sema/OpenCLBuiltins.td
@@ -279,6 +279,11 @@ def Vec1        : IntList<"Vec1", [1]>;
 def TLAll   : TypeList<"TLAll", [Char, UChar, Short, UShort, Int, UInt, Long, 
ULong, Float, Double, Half]>;
 def TLFloat : TypeList<"TLFloat", [Float, Double, Half]>;
 
+// All unsigned integer types twice, to facilitate unsigned return types for 
e.g.
+// uchar abs(char) and
+// uchar abs(uchar).
+def TLAllUIntsTwice : TypeList<"TLAllUIntsTwice", [UChar, UChar, UShort, 
UShort, UInt, UInt, ULong, ULong]>;
+
 def TLAllInts : TypeList<"TLAllInts", [Char, UChar, Short, UShort, Int, UInt, 
Long, ULong]>;
 
 // GenType definitions for multiple base types (e.g. all floating point types,
@@ -290,6 +295,8 @@ def AGenTypeNNoScalar      : 
GenericType<"AGenTypeNNoScalar", TLAll, VecNoScalar
 def AIGenType1             : GenericType<"AIGenType1", TLAllInts, Vec1>;
 def AIGenTypeN             : GenericType<"AIGenTypeN", TLAllInts, 
VecAndScalar>;
 def AIGenTypeNNoScalar     : GenericType<"AIGenTypeNNoScalar", TLAllInts, 
VecNoScalar>;
+// All integer to unsigned
+def AI2UGenTypeN           : GenericType<"AI2UGenTypeN", TLAllUIntsTwice, 
VecAndScalar>;
 // Float
 def FGenTypeN              : GenericType<"FGenTypeN", TLFloat, VecAndScalar>;
 
@@ -466,6 +473,61 @@ foreach name = ["half_divide", "half_powr",
   def : Builtin<name, [GenTypeFloatVecAndScalar, GenTypeFloatVecAndScalar, 
GenTypeFloatVecAndScalar], Attr.Const>;
 }
 
+//--------------------------------------------------------------------
+// OpenCL v1.1 s6.11.3, v1.2 s6.12.3, v2.0 s6.13.3 - Integer Functions
+// --- Table 10 ---
+// --- 1 argument ---
+foreach name = ["abs"] in {
+  def : Builtin<name, [AI2UGenTypeN, AIGenTypeN], Attr.Const>;
+}
+foreach name = ["clz", "popcount"] in {
+  def : Builtin<name, [AIGenTypeN, AIGenTypeN], Attr.Const>;
+}
+let MinVersion = CL20 in {
+  foreach name = ["ctz"] in {
+    def : Builtin<name, [AIGenTypeN, AIGenTypeN]>;
+  }
+}
+
+// --- 2 arguments ---
+foreach name = ["abs_
diff "] in {
+  def : Builtin<name, [AI2UGenTypeN, AIGenTypeN, AIGenTypeN], Attr.Const>;
+}
+foreach name = ["add_sat", "hadd", "rhadd", "mul_hi", "rotate", "sub_sat"] in {
+  def : Builtin<name, [AIGenTypeN, AIGenTypeN, AIGenTypeN], Attr.Const>;
+}
+foreach name = ["max", "min"] in {
+  def : Builtin<name, [AIGenTypeN, AIGenTypeN, AIGenTypeN], Attr.Const>;
+  def : Builtin<name, [AIGenTypeNNoScalar, AIGenTypeNNoScalar, AIGenType1], 
Attr.Const>;
+}
+foreach name = ["upsample"] in {
+  def : Builtin<name, [GenTypeShortVecAndScalar, GenTypeCharVecAndScalar, 
GenTypeUCharVecAndScalar], Attr.Const>;
+  def : Builtin<name, [GenTypeUShortVecAndScalar, GenTypeUCharVecAndScalar, 
GenTypeUCharVecAndScalar], Attr.Const>;
+  def : Builtin<name, [GenTypeIntVecAndScalar, GenTypeShortVecAndScalar, 
GenTypeUShortVecAndScalar], Attr.Const>;
+  def : Builtin<name, [GenTypeUIntVecAndScalar, GenTypeUShortVecAndScalar, 
GenTypeUShortVecAndScalar], Attr.Const>;
+  def : Builtin<name, [GenTypeLongVecAndScalar, GenTypeIntVecAndScalar, 
GenTypeUIntVecAndScalar], Attr.Const>;
+  def : Builtin<name, [GenTypeULongVecAndScalar, GenTypeUIntVecAndScalar, 
GenTypeUIntVecAndScalar], Attr.Const>;
+}
+
+// --- 3 arguments ---
+foreach name = ["clamp"] in {
+  def : Builtin<name, [AIGenTypeN, AIGenTypeN, AIGenTypeN, AIGenTypeN], 
Attr.Const>;
+  def : Builtin<name, [AIGenTypeNNoScalar, AIGenTypeNNoScalar, AIGenType1, 
AIGenType1], Attr.Const>;
+}
+foreach name = ["mad_hi", "mad_sat"] in {
+  def : Builtin<name, [AIGenTypeN, AIGenTypeN, AIGenTypeN, AIGenTypeN], 
Attr.Const>;
+}
+
+// --- Table 11 ---
+foreach name = ["mad24"] in {
+  def : Builtin<name, [GenTypeIntVecAndScalar, GenTypeIntVecAndScalar, 
GenTypeIntVecAndScalar, GenTypeIntVecAndScalar], Attr.Const>;
+  def : Builtin<name, [GenTypeUIntVecAndScalar, GenTypeUIntVecAndScalar, 
GenTypeUIntVecAndScalar, GenTypeUIntVecAndScalar], Attr.Const>;
+}
+foreach name = ["mul24"] in {
+  def : Builtin<name, [GenTypeIntVecAndScalar, GenTypeIntVecAndScalar, 
GenTypeIntVecAndScalar], Attr.Const>;
+  def : Builtin<name, [GenTypeUIntVecAndScalar, GenTypeUIntVecAndScalar, 
GenTypeUIntVecAndScalar], Attr.Const>;
+}
+
 //--------------------------------------------------------------------
 // OpenCL v1.1 s6.11.4, v1.2 s6.12.4, v2.0 s6.13.4 - Common Functions
 // OpenCL Extension v2.0 s5.1.3 and s6.1.3 - Common Functions
@@ -655,12 +717,6 @@ foreach Type = [Int, UInt] in {
   }
 }
 
-// OpenCL v1.1 s6.11.3, v1.2 s6.12.3, v2.0 s6.13.3 - Integer Functions
-foreach name = ["max", "min"] in {
-  def : Builtin<name, [AIGenTypeN, AIGenTypeN, AIGenTypeN], Attr.Const>;
-  def : Builtin<name, [AIGenTypeNNoScalar, AIGenTypeNNoScalar, AIGenType1], 
Attr.Const>;
-}
-
 //--------------------------------------------------------------------
 // OpenCL v1.1 s6.11.3, v1.2 s6.12.14, v2.0 s6.13.14: Image Read and Write 
Functions
 // OpenCL Extension v2.0 s5.1.8 and s6.1.8: Image Read and Write Functions

diff  --git a/clang/test/SemaOpenCL/fdeclare-opencl-builtins.cl 
b/clang/test/SemaOpenCL/fdeclare-opencl-builtins.cl
index 84cbb7aeec9b..97a01a1fe931 100644
--- a/clang/test/SemaOpenCL/fdeclare-opencl-builtins.cl
+++ b/clang/test/SemaOpenCL/fdeclare-opencl-builtins.cl
@@ -20,18 +20,19 @@
 
 // Provide typedefs when invoking clang without -finclude-default-header.
 #ifdef NO_HEADER
+typedef unsigned char uchar;
+typedef unsigned int uint;
+typedef unsigned long ulong;
+typedef unsigned short ushort;
+typedef __SIZE_TYPE__ size_t;
 typedef char char2 __attribute__((ext_vector_type(2)));
 typedef char char4 __attribute__((ext_vector_type(4)));
+typedef uchar uchar4 __attribute__((ext_vector_type(4)));
 typedef float float4 __attribute__((ext_vector_type(4)));
 typedef half half4 __attribute__((ext_vector_type(4)));
 typedef int int2 __attribute__((ext_vector_type(2)));
 typedef int int4 __attribute__((ext_vector_type(4)));
 typedef long long2 __attribute__((ext_vector_type(2)));
-typedef unsigned char uchar;
-typedef unsigned int uint;
-typedef unsigned long ulong;
-typedef unsigned short ushort;
-typedef __SIZE_TYPE__ size_t;
 #endif
 
 kernel void test_pointers(volatile global void *global_p, global const int4 
*a) {
@@ -61,6 +62,8 @@ kernel void basic_conversion() {
 char4 test_int(char c, char4 c4) {
   char m = max(c, c);
   char4 m4 = max(c4, c4);
+  uchar4 abs1 = abs(c4);
+  uchar4 abs2 = abs(abs1);
   return max(c4, c);
 }
 


        
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to