[libclc] a90b5b1 - [libclc] Move degrees/radians to CLC library & optimize (#123222)

via cfe-commits cfe-commits at lists.llvm.org
Fri Jan 17 04:11:57 PST 2025


Author: Fraser Cormack
Date: 2025-01-17T12:11:53Z
New Revision: a90b5b1885cc9587d7d65edbe3e0d94c4e2f4459

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

LOG: [libclc] Move degrees/radians to CLC library & optimize (#123222)

Missing half variants were also added.

The builtins are now consistently emitted in vector form (i.e., with a
splat of the literal to the appropriate vector size).

Added: 
    libclc/clc/include/clc/common/clc_degrees.h
    libclc/clc/include/clc/common/clc_radians.h
    libclc/clc/lib/generic/common/clc_degrees.cl
    libclc/clc/lib/generic/common/clc_radians.cl

Modified: 
    libclc/clc/lib/generic/SOURCES
    libclc/clc/lib/spirv/SOURCES
    libclc/clc/lib/spirv64/SOURCES
    libclc/generic/lib/common/degrees.cl
    libclc/generic/lib/common/radians.cl

Removed: 
    


################################################################################
diff  --git a/libclc/clc/include/clc/common/clc_degrees.h b/libclc/clc/include/clc/common/clc_degrees.h
new file mode 100644
index 00000000000000..e8bb684fcd4d73
--- /dev/null
+++ b/libclc/clc/include/clc/common/clc_degrees.h
@@ -0,0 +1,12 @@
+#ifndef __CLC_MATH_CLC_DEGREES_H__
+#define __CLC_MATH_CLC_DEGREES_H__
+
+#define __CLC_BODY <clc/math/unary_decl.inc>
+#define __CLC_FUNCTION __clc_degrees
+
+#include <clc/math/gentype.inc>
+
+#undef __CLC_BODY
+#undef __CLC_FUNCTION
+
+#endif // __CLC_MATH_CLC_DEGREES_H__

diff  --git a/libclc/clc/include/clc/common/clc_radians.h b/libclc/clc/include/clc/common/clc_radians.h
new file mode 100644
index 00000000000000..80d481e8de7238
--- /dev/null
+++ b/libclc/clc/include/clc/common/clc_radians.h
@@ -0,0 +1,12 @@
+#ifndef __CLC_MATH_CLC_RADIANS_H__
+#define __CLC_MATH_CLC_RADIANS_H__
+
+#define __CLC_BODY <clc/math/unary_decl.inc>
+#define __CLC_FUNCTION __clc_radians
+
+#include <clc/math/gentype.inc>
+
+#undef __CLC_BODY
+#undef __CLC_FUNCTION
+
+#endif // __CLC_MATH_CLC_RADIANS_H__

diff  --git a/libclc/clc/lib/generic/SOURCES b/libclc/clc/lib/generic/SOURCES
index f3097de6944221..d74bff20ba87ba 100644
--- a/libclc/clc/lib/generic/SOURCES
+++ b/libclc/clc/lib/generic/SOURCES
@@ -1,3 +1,5 @@
+common/clc_degrees.cl
+common/clc_radians.cl
 common/clc_smoothstep.cl
 geometric/clc_dot.cl
 integer/clc_abs.cl

diff  --git a/libclc/clc/lib/generic/common/clc_degrees.cl b/libclc/clc/lib/generic/common/clc_degrees.cl
new file mode 100644
index 00000000000000..ce705982072e89
--- /dev/null
+++ b/libclc/clc/lib/generic/common/clc_degrees.cl
@@ -0,0 +1,56 @@
+/*
+ * Copyright (c) 2014,2015 Advanced Micro Devices, Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#include <clc/clcmacro.h>
+#include <clc/internal/clc.h>
+
+#define DEGREES_SINGLE_DEF(TYPE, LITERAL)                                      \
+  _CLC_OVERLOAD _CLC_DEF TYPE __clc_degrees(TYPE radians) {                    \
+    return (TYPE)LITERAL * radians;                                            \
+  }
+
+#define DEGREES_DEF(TYPE, LITERAL)                                             \
+  DEGREES_SINGLE_DEF(TYPE, LITERAL)                                            \
+  DEGREES_SINGLE_DEF(TYPE##2, LITERAL)                                         \
+  DEGREES_SINGLE_DEF(TYPE##3, LITERAL)                                         \
+  DEGREES_SINGLE_DEF(TYPE##4, LITERAL)                                         \
+  DEGREES_SINGLE_DEF(TYPE##8, LITERAL)                                         \
+  DEGREES_SINGLE_DEF(TYPE##16, LITERAL)
+
+// 180/pi = ~57.29577951308232087685 or 0x1.ca5dc1a63c1f8p+5 or 0x1.ca5dc2p+5F
+DEGREES_DEF(float, 0x1.ca5dc2p+5F)
+
+#ifdef cl_khr_fp64
+#pragma OPENCL EXTENSION cl_khr_fp64 : enable
+
+// 180/pi = ~57.29577951308232087685 or 0x1.ca5dc1a63c1f8p+5 or 0x1.ca5dc2p+5F
+DEGREES_DEF(double, 0x1.ca5dc1a63c1f8p+5)
+
+#endif
+
+#ifdef cl_khr_fp16
+#pragma OPENCL EXTENSION cl_khr_fp16 : enable
+
+// 180/pi = ~57.29577951308232087685 or 0x1.ca5dc1a63c1f8p+5 or 0x1.ca5dc2p+5F
+DEGREES_DEF(half, (half)0x1.ca5dc1a63c1f8p+5)
+
+#endif

diff  --git a/libclc/clc/lib/generic/common/clc_radians.cl b/libclc/clc/lib/generic/common/clc_radians.cl
new file mode 100644
index 00000000000000..850b8eb84f9da2
--- /dev/null
+++ b/libclc/clc/lib/generic/common/clc_radians.cl
@@ -0,0 +1,56 @@
+/*
+ * Copyright (c) 2014,2015 Advanced Micro Devices, Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#include <clc/clcmacro.h>
+#include <clc/internal/clc.h>
+
+#define RADIANS_SINGLE_DEF(TYPE, LITERAL)                                      \
+  _CLC_OVERLOAD _CLC_DEF TYPE __clc_radians(TYPE radians) {                    \
+    return (TYPE)LITERAL * radians;                                            \
+  }
+
+#define RADIANS_DEF(TYPE, LITERAL)                                             \
+  RADIANS_SINGLE_DEF(TYPE, LITERAL)                                            \
+  RADIANS_SINGLE_DEF(TYPE##2, LITERAL)                                         \
+  RADIANS_SINGLE_DEF(TYPE##3, LITERAL)                                         \
+  RADIANS_SINGLE_DEF(TYPE##4, LITERAL)                                         \
+  RADIANS_SINGLE_DEF(TYPE##8, LITERAL)                                         \
+  RADIANS_SINGLE_DEF(TYPE##16, LITERAL)
+
+// pi/180 = ~0.01745329251994329577 or 0x1.1df46a2529d39p-6 or 0x1.1df46ap-6F
+RADIANS_DEF(float, 0x1.1df46ap-6F)
+
+#ifdef cl_khr_fp64
+#pragma OPENCL EXTENSION cl_khr_fp64 : enable
+
+// pi/180 = ~0.01745329251994329577 or 0x1.1df46a2529d39p-6 or 0x1.1df46ap-6F
+RADIANS_DEF(double, 0x1.1df46a2529d39p-6)
+
+#endif
+
+#ifdef cl_khr_fp16
+#pragma OPENCL EXTENSION cl_khr_fp16 : enable
+
+// pi/180 = ~0.01745329251994329577 or 0x1.1df46a2529d39p-6 or 0x1.1df46ap-6F
+RADIANS_DEF(half, (half)0x1.1df46a2529d39p-6)
+
+#endif

diff  --git a/libclc/clc/lib/spirv/SOURCES b/libclc/clc/lib/spirv/SOURCES
index 02784b8def682b..ac855ea5184ede 100644
--- a/libclc/clc/lib/spirv/SOURCES
+++ b/libclc/clc/lib/spirv/SOURCES
@@ -1,3 +1,5 @@
+../generic/common/clc_degrees.cl
+../generic/common/clc_radians.cl
 ../generic/common/clc_smoothstep.cl
 ../generic/geometric/clc_dot.cl
 ../generic/math/clc_ceil.cl

diff  --git a/libclc/clc/lib/spirv64/SOURCES b/libclc/clc/lib/spirv64/SOURCES
index 02784b8def682b..ac855ea5184ede 100644
--- a/libclc/clc/lib/spirv64/SOURCES
+++ b/libclc/clc/lib/spirv64/SOURCES
@@ -1,3 +1,5 @@
+../generic/common/clc_degrees.cl
+../generic/common/clc_radians.cl
 ../generic/common/clc_smoothstep.cl
 ../generic/geometric/clc_dot.cl
 ../generic/math/clc_ceil.cl

diff  --git a/libclc/generic/lib/common/degrees.cl b/libclc/generic/lib/common/degrees.cl
index cf49b190c76b37..a9715d64f507a6 100644
--- a/libclc/generic/lib/common/degrees.cl
+++ b/libclc/generic/lib/common/degrees.cl
@@ -22,23 +22,20 @@
 
 #include <clc/clc.h>
 #include <clc/clcmacro.h>
+#include <clc/common/clc_degrees.h>
 
-_CLC_OVERLOAD _CLC_DEF float degrees(float radians) {
-  // 180/pi = ~57.29577951308232087685 or 0x1.ca5dc1a63c1f8p+5 or 0x1.ca5dc2p+5F
-  return 0x1.ca5dc2p+5F * radians;
-}
-
-_CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, float, degrees, float);
-
+_CLC_DEFINE_UNARY_BUILTIN(float, degrees, __clc_degrees, float)
 
 #ifdef cl_khr_fp64
 #pragma OPENCL EXTENSION cl_khr_fp64 : enable
 
-_CLC_OVERLOAD _CLC_DEF double degrees(double radians) {
-  // 180/pi = ~57.29577951308232087685 or 0x1.ca5dc1a63c1f8p+5 or 0x1.ca5dc2p+5F
-  return 0x1.ca5dc1a63c1f8p+5 * radians;
-}
+_CLC_DEFINE_UNARY_BUILTIN(double, degrees, __clc_degrees, double)
+
+#endif
+
+#ifdef cl_khr_fp16
+#pragma OPENCL EXTENSION cl_khr_fp16 : enable
 
-_CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, double, degrees, double);
+_CLC_DEFINE_UNARY_BUILTIN(half, degrees, __clc_degrees, half)
 
 #endif

diff  --git a/libclc/generic/lib/common/radians.cl b/libclc/generic/lib/common/radians.cl
index 645a30549afedd..b5dcbfe6e3fd2a 100644
--- a/libclc/generic/lib/common/radians.cl
+++ b/libclc/generic/lib/common/radians.cl
@@ -22,23 +22,20 @@
 
 #include <clc/clc.h>
 #include <clc/clcmacro.h>
+#include <clc/common/clc_radians.h>
 
-_CLC_OVERLOAD _CLC_DEF float radians(float degrees) {
-  // pi/180 = ~0.01745329251994329577 or 0x1.1df46a2529d39p-6 or 0x1.1df46ap-6F
-  return 0x1.1df46ap-6F * degrees;
-}
-
-_CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, float, radians, float);
-
+_CLC_DEFINE_UNARY_BUILTIN(float, radians, __clc_radians, float)
 
 #ifdef cl_khr_fp64
 #pragma OPENCL EXTENSION cl_khr_fp64 : enable
 
-_CLC_OVERLOAD _CLC_DEF double radians(double degrees) {
-  // pi/180 = ~0.01745329251994329577 or 0x1.1df46a2529d39p-6 or 0x1.1df46ap-6F
-  return 0x1.1df46a2529d39p-6 * degrees;
-}
+_CLC_DEFINE_UNARY_BUILTIN(double, radians, __clc_radians, double)
+
+#endif
+
+#ifdef cl_khr_fp16
+#pragma OPENCL EXTENSION cl_khr_fp16 : enable
 
-_CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, double, radians, double);
+_CLC_DEFINE_UNARY_BUILTIN(half, radians, __clc_radians, half)
 
 #endif


        


More information about the cfe-commits mailing list