https://github.com/frasercrmck created 
https://github.com/llvm/llvm-project/pull/122751

Having the fp16 pragmas enabled in the header file is risky, as we have some 
headers that also disable pragmas. We want to be as immune to changes in the 
order of included header files as possible.

This pattern of setting pragmas in the implementation files is also far more 
common across the codebase.

>From 4dd89f2be8e80797a8e921145339ac2716505830 Mon Sep 17 00:00:00 2001
From: Fraser Cormack <fra...@codeplay.com>
Date: Mon, 13 Jan 2025 17:29:06 +0000
Subject: [PATCH] [libclc] Move fp16 pragma guards out of header file

Having the fp16 pragmas enabled in the header file is risky, as we have
some headers that also disable pragmas. We want to be as immune to
changes in the order of included header files as possible.

This pattern of setting pragmas in the implementation files is also far
more common across the codebase.
---
 libclc/clc/include/clc/clcmacro.h  | 11 -----------
 libclc/generic/lib/math/acos.cl    |  6 ++++++
 libclc/generic/lib/math/acosh.cl   |  6 ++++++
 libclc/generic/lib/math/acospi.cl  |  6 ++++++
 libclc/generic/lib/math/asinh.cl   |  6 ++++++
 libclc/generic/lib/math/atan.cl    |  5 +++++
 libclc/generic/lib/math/atan2.cl   |  6 ++++++
 libclc/generic/lib/math/atan2pi.cl |  6 ++++++
 libclc/generic/lib/math/atanh.cl   |  6 ++++++
 libclc/generic/lib/math/atanpi.cl  |  6 ++++++
 libclc/generic/lib/math/cbrt.cl    |  6 ++++++
 libclc/generic/lib/math/cos.cl     |  6 ++++++
 libclc/generic/lib/math/cosh.cl    |  6 ++++++
 libclc/generic/lib/math/cospi.cl   |  6 ++++++
 libclc/generic/lib/math/exp.cl     |  6 ++++++
 libclc/generic/lib/math/expm1.cl   |  6 ++++++
 libclc/generic/lib/math/lgamma.cl  |  6 ++++++
 libclc/generic/lib/math/log1p.cl   |  6 ++++++
 libclc/generic/lib/math/logb.cl    |  6 ++++++
 libclc/generic/lib/math/sin.cl     |  6 ++++++
 libclc/generic/lib/math/sinh.cl    |  6 ++++++
 libclc/generic/lib/math/sinpi.cl   |  6 ++++++
 libclc/generic/lib/math/tanh.cl    |  6 ++++++
 23 files changed, 131 insertions(+), 11 deletions(-)

diff --git a/libclc/clc/include/clc/clcmacro.h 
b/libclc/clc/include/clc/clcmacro.h
index c6583749eca661..bfd4fb577a8ff4 100644
--- a/libclc/clc/include/clc/clcmacro.h
+++ b/libclc/clc/include/clc/clcmacro.h
@@ -207,10 +207,6 @@
     return BUILTIN(x);                                                         
\
   }
 
-#ifdef cl_khr_fp16
-
-#pragma OPENCL EXTENSION cl_khr_fp16 : enable
-
 #define _CLC_DEFINE_UNARY_BUILTIN_FP16(FUNCTION)                               
\
   _CLC_DEF _CLC_OVERLOAD half FUNCTION(half x) {                               
\
     return (half)FUNCTION((float)x);                                           
\
@@ -223,11 +219,4 @@
   }                                                                            
\
   _CLC_BINARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, half, FUNCTION, half, half)
 
-#else
-
-#define _CLC_DEFINE_UNARY_BUILTIN_FP16(FUNCTION)
-#define _CLC_DEFINE_BINARY_BUILTIN_FP16(FUNCTION)
-
-#endif
-
 #endif // __CLC_CLCMACRO_H__
diff --git a/libclc/generic/lib/math/acos.cl b/libclc/generic/lib/math/acos.cl
index aeb72872da342c..d71d10024b1800 100644
--- a/libclc/generic/lib/math/acos.cl
+++ b/libclc/generic/lib/math/acos.cl
@@ -172,4 +172,10 @@ _CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, double, acos, 
double);
 
 #endif // cl_khr_fp64
 
+#ifdef cl_khr_fp16
+
+#pragma OPENCL EXTENSION cl_khr_fp16 : enable
+
 _CLC_DEFINE_UNARY_BUILTIN_FP16(acos)
+
+#endif
diff --git a/libclc/generic/lib/math/acosh.cl b/libclc/generic/lib/math/acosh.cl
index 4656f14457d429..977c2e929b34cb 100644
--- a/libclc/generic/lib/math/acosh.cl
+++ b/libclc/generic/lib/math/acosh.cl
@@ -126,4 +126,10 @@ _CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, double, 
acosh, double)
 
 #endif
 
+#ifdef cl_khr_fp16
+
+#pragma OPENCL EXTENSION cl_khr_fp16 : enable
+
 _CLC_DEFINE_UNARY_BUILTIN_FP16(acosh)
+
+#endif
diff --git a/libclc/generic/lib/math/acospi.cl 
b/libclc/generic/lib/math/acospi.cl
index 83a47eb27e8367..5aa8a083df4e97 100644
--- a/libclc/generic/lib/math/acospi.cl
+++ b/libclc/generic/lib/math/acospi.cl
@@ -171,4 +171,10 @@ _CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, double, 
acospi, double)
 
 #endif
 
+#ifdef cl_khr_fp16
+
+#pragma OPENCL EXTENSION cl_khr_fp16 : enable
+
 _CLC_DEFINE_UNARY_BUILTIN_FP16(acospi)
+
+#endif
diff --git a/libclc/generic/lib/math/asinh.cl b/libclc/generic/lib/math/asinh.cl
index f7637ade227ee0..686d9f7a95e5d9 100644
--- a/libclc/generic/lib/math/asinh.cl
+++ b/libclc/generic/lib/math/asinh.cl
@@ -292,4 +292,10 @@ _CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, double, 
asinh, double)
 
 #endif
 
+#ifdef cl_khr_fp16
+
+#pragma OPENCL EXTENSION cl_khr_fp16 : enable
+
 _CLC_DEFINE_UNARY_BUILTIN_FP16(asinh)
+
+#endif
diff --git a/libclc/generic/lib/math/atan.cl b/libclc/generic/lib/math/atan.cl
index 28eaaf7b2ae72b..b6b067f2bf4ffe 100644
--- a/libclc/generic/lib/math/atan.cl
+++ b/libclc/generic/lib/math/atan.cl
@@ -182,5 +182,10 @@ _CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, double, atan, 
double);
 
 #endif // cl_khr_fp64
 
+#ifdef cl_khr_fp16
+
+#pragma OPENCL EXTENSION cl_khr_fp16 : enable
 
 _CLC_DEFINE_UNARY_BUILTIN_FP16(atan)
+
+#endif
diff --git a/libclc/generic/lib/math/atan2.cl b/libclc/generic/lib/math/atan2.cl
index 98b457a69a0e2e..32419937a856bd 100644
--- a/libclc/generic/lib/math/atan2.cl
+++ b/libclc/generic/lib/math/atan2.cl
@@ -236,4 +236,10 @@ _CLC_BINARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, double, 
atan2, double, double);
 
 #endif
 
+#ifdef cl_khr_fp16
+
+#pragma OPENCL EXTENSION cl_khr_fp16 : enable
+
 _CLC_DEFINE_BINARY_BUILTIN_FP16(atan2)
+
+#endif
diff --git a/libclc/generic/lib/math/atan2pi.cl 
b/libclc/generic/lib/math/atan2pi.cl
index ad41b11bd37413..e631918f7539e2 100644
--- a/libclc/generic/lib/math/atan2pi.cl
+++ b/libclc/generic/lib/math/atan2pi.cl
@@ -220,4 +220,10 @@ _CLC_BINARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, double, 
atan2pi, double, double)
 
 #endif
 
+#ifdef cl_khr_fp16
+
+#pragma OPENCL EXTENSION cl_khr_fp16 : enable
+
 _CLC_DEFINE_BINARY_BUILTIN_FP16(atan2pi)
+
+#endif
diff --git a/libclc/generic/lib/math/atanh.cl b/libclc/generic/lib/math/atanh.cl
index f2298a2624e84c..10bad190cc0dc0 100644
--- a/libclc/generic/lib/math/atanh.cl
+++ b/libclc/generic/lib/math/atanh.cl
@@ -112,4 +112,10 @@ _CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, double, 
atanh, double)
 
 #endif
 
+#ifdef cl_khr_fp16
+
+#pragma OPENCL EXTENSION cl_khr_fp16 : enable
+
 _CLC_DEFINE_UNARY_BUILTIN_FP16(atanh)
+
+#endif
diff --git a/libclc/generic/lib/math/atanpi.cl 
b/libclc/generic/lib/math/atanpi.cl
index 9e6b3ece7f325d..8522acf3499332 100644
--- a/libclc/generic/lib/math/atanpi.cl
+++ b/libclc/generic/lib/math/atanpi.cl
@@ -181,4 +181,10 @@ _CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, double, 
atanpi, double)
 
 #endif
 
+#ifdef cl_khr_fp16
+
+#pragma OPENCL EXTENSION cl_khr_fp16 : enable
+
 _CLC_DEFINE_UNARY_BUILTIN_FP16(atanpi)
+
+#endif
diff --git a/libclc/generic/lib/math/cbrt.cl b/libclc/generic/lib/math/cbrt.cl
index 8462f5f6e14bbc..49f2ba8a411cbf 100644
--- a/libclc/generic/lib/math/cbrt.cl
+++ b/libclc/generic/lib/math/cbrt.cl
@@ -150,4 +150,10 @@ _CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, double, cbrt, 
double)
 
 #endif
 
+#ifdef cl_khr_fp16
+
+#pragma OPENCL EXTENSION cl_khr_fp16 : enable
+
 _CLC_DEFINE_UNARY_BUILTIN_FP16(cbrt)
+
+#endif
diff --git a/libclc/generic/lib/math/cos.cl b/libclc/generic/lib/math/cos.cl
index 42192895151a30..2945c90d6fe0f3 100644
--- a/libclc/generic/lib/math/cos.cl
+++ b/libclc/generic/lib/math/cos.cl
@@ -76,4 +76,10 @@ _CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, double, cos, 
double);
 
 #endif
 
+#ifdef cl_khr_fp16
+
+#pragma OPENCL EXTENSION cl_khr_fp16 : enable
+
 _CLC_DEFINE_UNARY_BUILTIN_FP16(cos)
+
+#endif
diff --git a/libclc/generic/lib/math/cosh.cl b/libclc/generic/lib/math/cosh.cl
index 1f58d7acd2b55f..02b03a761aa19a 100644
--- a/libclc/generic/lib/math/cosh.cl
+++ b/libclc/generic/lib/math/cosh.cl
@@ -191,4 +191,10 @@ _CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, double, cosh, 
double)
 
 #endif
 
+#ifdef cl_khr_fp16
+
+#pragma OPENCL EXTENSION cl_khr_fp16 : enable
+
 _CLC_DEFINE_UNARY_BUILTIN_FP16(cosh)
+
+#endif
diff --git a/libclc/generic/lib/math/cospi.cl b/libclc/generic/lib/math/cospi.cl
index 0e69f7885faa1c..98e989267c9173 100644
--- a/libclc/generic/lib/math/cospi.cl
+++ b/libclc/generic/lib/math/cospi.cl
@@ -135,4 +135,10 @@ _CLC_OVERLOAD _CLC_DEF double cospi(double x) {
 _CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, double, cospi, double);
 #endif
 
+#ifdef cl_khr_fp16
+
+#pragma OPENCL EXTENSION cl_khr_fp16 : enable
+
 _CLC_DEFINE_UNARY_BUILTIN_FP16(cospi)
+
+#endif
diff --git a/libclc/generic/lib/math/exp.cl b/libclc/generic/lib/math/exp.cl
index 1e37d76f3ac8e8..95dc0db44df76d 100644
--- a/libclc/generic/lib/math/exp.cl
+++ b/libclc/generic/lib/math/exp.cl
@@ -89,4 +89,10 @@ _CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, double, exp, 
double)
 
 #endif
 
+#ifdef cl_khr_fp16
+
+#pragma OPENCL EXTENSION cl_khr_fp16 : enable
+
 _CLC_DEFINE_UNARY_BUILTIN_FP16(exp)
+
+#endif
diff --git a/libclc/generic/lib/math/expm1.cl b/libclc/generic/lib/math/expm1.cl
index fbb9f0d087c897..d50a88e0aaae8e 100644
--- a/libclc/generic/lib/math/expm1.cl
+++ b/libclc/generic/lib/math/expm1.cl
@@ -141,4 +141,10 @@ _CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, double, 
expm1, double)
 
 #endif
 
+#ifdef cl_khr_fp16
+
+#pragma OPENCL EXTENSION cl_khr_fp16 : enable
+
 _CLC_DEFINE_UNARY_BUILTIN_FP16(expm1)
+
+#endif
diff --git a/libclc/generic/lib/math/lgamma.cl 
b/libclc/generic/lib/math/lgamma.cl
index ca7b9610a31b38..f0476230e63fe5 100644
--- a/libclc/generic/lib/math/lgamma.cl
+++ b/libclc/generic/lib/math/lgamma.cl
@@ -43,4 +43,10 @@ _CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, double, lgamma, 
double)
 
 #endif
 
+#ifdef cl_khr_fp16
+
+#pragma OPENCL EXTENSION cl_khr_fp16 : enable
+
 _CLC_DEFINE_UNARY_BUILTIN_FP16(lgamma)
+
+#endif
diff --git a/libclc/generic/lib/math/log1p.cl b/libclc/generic/lib/math/log1p.cl
index a371995a084928..7fef79ca7b8f39 100644
--- a/libclc/generic/lib/math/log1p.cl
+++ b/libclc/generic/lib/math/log1p.cl
@@ -176,4 +176,10 @@ _CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, double, 
log1p, double);
 
 #endif // cl_khr_fp64
 
+#ifdef cl_khr_fp16
+
+#pragma OPENCL EXTENSION cl_khr_fp16 : enable
+
 _CLC_DEFINE_UNARY_BUILTIN_FP16(log1p)
+
+#endif
diff --git a/libclc/generic/lib/math/logb.cl b/libclc/generic/lib/math/logb.cl
index 7a7111d5bc84dd..d91972f780ba9d 100644
--- a/libclc/generic/lib/math/logb.cl
+++ b/libclc/generic/lib/math/logb.cl
@@ -30,4 +30,10 @@ _CLC_OVERLOAD _CLC_DEF double logb(double x) {
 _CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, double, logb, double)
 #endif
 
+#ifdef cl_khr_fp16
+
+#pragma OPENCL EXTENSION cl_khr_fp16 : enable
+
 _CLC_DEFINE_UNARY_BUILTIN_FP16(logb)
+
+#endif
diff --git a/libclc/generic/lib/math/sin.cl b/libclc/generic/lib/math/sin.cl
index 30638a57f59b58..c271e67828066f 100644
--- a/libclc/generic/lib/math/sin.cl
+++ b/libclc/generic/lib/math/sin.cl
@@ -78,4 +78,10 @@ _CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, double, sin, 
double);
 
 #endif
 
+#ifdef cl_khr_fp16
+
+#pragma OPENCL EXTENSION cl_khr_fp16 : enable
+
 _CLC_DEFINE_UNARY_BUILTIN_FP16(sin)
+
+#endif
diff --git a/libclc/generic/lib/math/sinh.cl b/libclc/generic/lib/math/sinh.cl
index 3de0792361c2f5..23500c1f49b7a0 100644
--- a/libclc/generic/lib/math/sinh.cl
+++ b/libclc/generic/lib/math/sinh.cl
@@ -190,4 +190,10 @@ _CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, double, sinh, 
double)
 
 #endif
 
+#ifdef cl_khr_fp16
+
+#pragma OPENCL EXTENSION cl_khr_fp16 : enable
+
 _CLC_DEFINE_UNARY_BUILTIN_FP16(sinh)
+
+#endif
diff --git a/libclc/generic/lib/math/sinpi.cl b/libclc/generic/lib/math/sinpi.cl
index 520bba5415c7c0..01b340b855c44c 100644
--- a/libclc/generic/lib/math/sinpi.cl
+++ b/libclc/generic/lib/math/sinpi.cl
@@ -130,4 +130,10 @@ _CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, double, 
sinpi, double)
 
 #endif
 
+#ifdef cl_khr_fp16
+
+#pragma OPENCL EXTENSION cl_khr_fp16 : enable
+
 _CLC_DEFINE_UNARY_BUILTIN_FP16(sinpi)
+
+#endif
diff --git a/libclc/generic/lib/math/tanh.cl b/libclc/generic/lib/math/tanh.cl
index e558bb93a55964..d9509c57b05074 100644
--- a/libclc/generic/lib/math/tanh.cl
+++ b/libclc/generic/lib/math/tanh.cl
@@ -145,4 +145,10 @@ _CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, double, tanh, 
double);
 
 #endif // cl_khr_fp64
 
+#ifdef cl_khr_fp16
+
+#pragma OPENCL EXTENSION cl_khr_fp16 : enable
+
 _CLC_DEFINE_UNARY_BUILTIN_FP16(tanh)
+
+#endif

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

Reply via email to