[clang] 81e8b60 - [OpenCL] Only declare _sat conversions for integer types

Sven van Haastregt via cfe-commits cfe-commits at lists.llvm.org
Wed Feb 19 05:53:28 PST 2020


Author: Sven van Haastregt
Date: 2020-02-19T13:52:58Z
New Revision: 81e8b60b722e5494cb92b6b4ec03d4631028eece

URL: https://github.com/llvm/llvm-project/commit/81e8b60b722e5494cb92b6b4ec03d4631028eece
DIFF: https://github.com/llvm/llvm-project/commit/81e8b60b722e5494cb92b6b4ec03d4631028eece.diff

LOG: [OpenCL] Only declare _sat conversions for integer types

The `-fdeclare-opencl-builtins` option was accepting saturated
conversions for non-integer types, which contradicts both the OpenCL
specification (v2.0 s6.2.3) and Clang's opencl-c.h file.

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 f0790dd32527..f79bb54a8143 100644
--- a/clang/lib/Sema/OpenCLBuiltins.td
+++ b/clang/lib/Sema/OpenCLBuiltins.td
@@ -378,7 +378,11 @@ foreach RType = [Float, Double, Half, Char, UChar, Short,
                  UShort, Int, UInt, Long, ULong] in {
   foreach IType = [Float, Double, Half, Char, UChar, Short,
                    UShort, Int, UInt, Long, ULong] in {
-    foreach sat = ["", "_sat"] in {
+    // Conversions to integer type have a sat and non-sat variant.
+    foreach sat = !cond(!eq(RType.Name, "float") : [""],
+                        !eq(RType.Name, "double") : [""],
+                        !eq(RType.Name, "half") : [""],
+                        1 : ["", "_sat"]) in {
       foreach rnd = ["", "_rte", "_rtn", "_rtp", "_rtz"] in {
         def : Builtin<"convert_" # RType.Name # sat # rnd, [RType, IType],
                       Attr.Const>;

diff  --git a/clang/test/SemaOpenCL/fdeclare-opencl-builtins.cl b/clang/test/SemaOpenCL/fdeclare-opencl-builtins.cl
index dd89f40761c0..e593b21ec459 100644
--- a/clang/test/SemaOpenCL/fdeclare-opencl-builtins.cl
+++ b/clang/test/SemaOpenCL/fdeclare-opencl-builtins.cl
@@ -71,11 +71,25 @@ kernel void basic_conversion() {
   int4 i4;
 
   f = convert_float(d);
-  d = convert_double_sat_rtp(f);
+  d = convert_double_rtp(f);
   l2 = convert_long2_rtz(c2);
   i4 = convert_int4_sat(f4);
 }
 
+kernel void basic_conversion_neg() {
+  int i;
+  float f;
+
+  f = convert_float_sat(i);
+#if !defined(__OPENCL_CPP_VERSION__)
+  // expected-error at -2{{implicit declaration of function 'convert_float_sat' is invalid in OpenCL}}
+  // expected-error at -3{{implicit conversion from 'int' to 'float' may lose precision}}
+#else
+  // expected-error at -5{{use of undeclared identifier 'convert_float_sat'; did you mean 'convert_float'?}}
+  // expected-note at -6{{'convert_float' declared here}}
+#endif
+}
+
 char4 test_int(char c, char4 c4) {
   char m = max(c, c);
   char4 m4 = max(c4, c4);


        


More information about the cfe-commits mailing list