[libclc] [NFC][libclc] Replace and delete _CLC_DEFINE_UNARY/BINARY/TERNARY_BUILTIN macros (PR #145458)

Wenju He via cfe-commits cfe-commits at lists.llvm.org
Mon Jun 23 22:00:09 PDT 2025


https://github.com/wenju-he created https://github.com/llvm/llvm-project/pull/145458

Also delete unused _CLC_DEFINE_BINARY_BUILTIN_WITH_SCALAR_SECOND_ARG, _CLC_DEFINE_UNARY_BUILTIN_FP16 and _CLC_DEFINE_BINARY_BUILTIN_FP16.

llvm-diff shows no change to nvptx64--nvidiacl.bc and amdgcn--amdhsa.bc

>From 0cbf5c96077bce977168497670e3ffae9e70e5d5 Mon Sep 17 00:00:00 2001
From: Wenju He <wenju.he at intel.com>
Date: Tue, 24 Jun 2025 06:59:27 +0200
Subject: [PATCH] [NFC][libclc] Replace and delete
 _CLC_DEFINE_UNARY/BINARY/TERNARY_BUILTIN macros

Also delete unused _CLC_DEFINE_BINARY_BUILTIN_WITH_SCALAR_SECOND_ARG,
_CLC_DEFINE_UNARY_BUILTIN_FP16 and _CLC_DEFINE_BINARY_BUILTIN_FP16.

llvm-diff shows no change to nvptx64--nvidiacl.bc and amdgcn--amdhsa.bc
---
 libclc/clc/include/clc/clcmacro.h             | 105 ------------------
 .../shared/binary_def_with_int_second_arg.inc |  17 +++
 libclc/clc/lib/generic/math/clc_copysign.cl   |  28 +----
 libclc/opencl/lib/clspv/math/fma.cl           |   8 +-
 libclc/opencl/lib/generic/common/degrees.cl   |  18 +--
 libclc/opencl/lib/generic/common/radians.cl   |  18 +--
 libclc/opencl/lib/generic/math/fma.cl         |  18 +--
 libclc/opencl/lib/generic/math/ldexp.cl       |  21 +---
 libclc/opencl/lib/generic/math/mad.cl         |  18 +--
 libclc/opencl/lib/generic/math/nextafter.cl   |  24 +---
 libclc/opencl/lib/spirv/math/fma.cl           |   8 +-
 11 files changed, 53 insertions(+), 230 deletions(-)
 create mode 100644 libclc/clc/include/clc/shared/binary_def_with_int_second_arg.inc

diff --git a/libclc/clc/include/clc/clcmacro.h b/libclc/clc/include/clc/clcmacro.h
index de7b977021f8b..b712fe5cf326c 100644
--- a/libclc/clc/include/clc/clcmacro.h
+++ b/libclc/clc/include/clc/clcmacro.h
@@ -179,109 +179,4 @@
   _CLC_BINARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, RET_TYPE, FUNCTION, ARG1_TYPE, \
                         ARG2_TYPE)
 
-// FIXME: Make _CLC_DEFINE_BINARY_BUILTIN avoid scalarization by default, and
-// introduce an explicit scalarizing version.
-#define _CLC_DEFINE_BINARY_BUILTIN_NO_SCALARIZE(RET_TYPE, FUNCTION, BUILTIN,   \
-                                                ARG1_TYPE, ARG2_TYPE)          \
-  _CLC_DEF _CLC_OVERLOAD RET_TYPE FUNCTION(ARG1_TYPE x, ARG2_TYPE y) {         \
-    return BUILTIN(x, y);                                                      \
-  }                                                                            \
-  _CLC_DEF _CLC_OVERLOAD RET_TYPE##2 FUNCTION(ARG1_TYPE##2 x,                  \
-                                              ARG2_TYPE##2 y) {                \
-    return BUILTIN(x, y);                                                      \
-  }                                                                            \
-  _CLC_DEF _CLC_OVERLOAD RET_TYPE##3 FUNCTION(ARG1_TYPE##3 x,                  \
-                                              ARG2_TYPE##3 y) {                \
-    return BUILTIN(x, y);                                                      \
-  }                                                                            \
-  _CLC_DEF _CLC_OVERLOAD RET_TYPE##4 FUNCTION(ARG1_TYPE##4 x,                  \
-                                              ARG2_TYPE##4 y) {                \
-    return BUILTIN(x, y);                                                      \
-  }                                                                            \
-  _CLC_DEF _CLC_OVERLOAD RET_TYPE##8 FUNCTION(ARG1_TYPE##8 x,                  \
-                                              ARG2_TYPE##8 y) {                \
-    return BUILTIN(x, y);                                                      \
-  }                                                                            \
-  _CLC_DEF _CLC_OVERLOAD RET_TYPE##16 FUNCTION(ARG1_TYPE##16 x,                \
-                                               ARG2_TYPE##16 y) {              \
-    return BUILTIN(x, y);                                                      \
-  }
-
-#define _CLC_DEFINE_BINARY_BUILTIN_WITH_SCALAR_SECOND_ARG(                     \
-    RET_TYPE, FUNCTION, BUILTIN, ARG1_TYPE, ARG2_TYPE)                         \
-  _CLC_DEFINE_BINARY_BUILTIN(RET_TYPE, FUNCTION, BUILTIN, ARG1_TYPE,           \
-                             ARG2_TYPE)                                        \
-  _CLC_BINARY_VECTORIZE_SCALAR_SECOND_ARG(_CLC_OVERLOAD _CLC_DEF, RET_TYPE,    \
-                                          FUNCTION, ARG1_TYPE, ARG2_TYPE)
-
-#define _CLC_DEFINE_UNARY_BUILTIN(RET_TYPE, FUNCTION, BUILTIN, ARG1_TYPE)      \
-  _CLC_DEF _CLC_OVERLOAD RET_TYPE FUNCTION(ARG1_TYPE x) { return BUILTIN(x); } \
-  _CLC_DEF _CLC_OVERLOAD RET_TYPE##2 FUNCTION(ARG1_TYPE##2 x) {                \
-    return BUILTIN(x);                                                         \
-  }                                                                            \
-  _CLC_DEF _CLC_OVERLOAD RET_TYPE##3 FUNCTION(ARG1_TYPE##3 x) {                \
-    return BUILTIN(x);                                                         \
-  }                                                                            \
-  _CLC_DEF _CLC_OVERLOAD RET_TYPE##4 FUNCTION(ARG1_TYPE##4 x) {                \
-    return BUILTIN(x);                                                         \
-  }                                                                            \
-  _CLC_DEF _CLC_OVERLOAD RET_TYPE##8 FUNCTION(ARG1_TYPE##8 x) {                \
-    return BUILTIN(x);                                                         \
-  }                                                                            \
-  _CLC_DEF _CLC_OVERLOAD RET_TYPE##16 FUNCTION(ARG1_TYPE##16 x) {              \
-    return BUILTIN(x);                                                         \
-  }
-
-#define _CLC_DEFINE_TERNARY_BUILTIN(RET_TYPE, FUNCTION, BUILTIN, ARG1_TYPE,    \
-                                    ARG2_TYPE, ARG3_TYPE)                      \
-  _CLC_DEF _CLC_OVERLOAD RET_TYPE FUNCTION(ARG1_TYPE x, ARG2_TYPE y,           \
-                                           ARG3_TYPE z) {                      \
-    return BUILTIN(x, y, z);                                                   \
-  }                                                                            \
-  _CLC_DEF _CLC_OVERLOAD RET_TYPE##2 FUNCTION(ARG1_TYPE##2 x, ARG2_TYPE##2 y,  \
-                                              ARG3_TYPE##2 z) {                \
-    return BUILTIN(x, y, z);                                                   \
-  }                                                                            \
-  _CLC_DEF _CLC_OVERLOAD RET_TYPE##3 FUNCTION(ARG1_TYPE##3 x, ARG2_TYPE##3 y,  \
-                                              ARG3_TYPE##3 z) {                \
-    return BUILTIN(x, y, z);                                                   \
-  }                                                                            \
-  _CLC_DEF _CLC_OVERLOAD RET_TYPE##4 FUNCTION(ARG1_TYPE##4 x, ARG2_TYPE##4 y,  \
-                                              ARG3_TYPE##4 z) {                \
-    return BUILTIN(x, y, z);                                                   \
-  }                                                                            \
-  _CLC_DEF _CLC_OVERLOAD RET_TYPE##8 FUNCTION(ARG1_TYPE##8 x, ARG2_TYPE##8 y,  \
-                                              ARG3_TYPE##8 z) {                \
-    return BUILTIN(x, y, z);                                                   \
-  }                                                                            \
-  _CLC_DEF _CLC_OVERLOAD RET_TYPE##16 FUNCTION(                                \
-      ARG1_TYPE##16 x, ARG2_TYPE##16 y, ARG3_TYPE##16 z) {                     \
-    return BUILTIN(x, y, z);                                                   \
-  }
-
-#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);                                           \
-  }                                                                            \
-  _CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, half, FUNCTION, half)
-
-#define _CLC_DEFINE_BINARY_BUILTIN_FP16(FUNCTION)                              \
-  _CLC_DEF _CLC_OVERLOAD half FUNCTION(half x, half y) {                       \
-    return (half)FUNCTION((float)x, (float)y);                                 \
-  }                                                                            \
-  _CLC_BINARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, half, FUNCTION, half, half)
-
-#pragma OPENCL EXTENSION cl_khr_fp16 : disable
-
-#else
-
-#define _CLC_DEFINE_UNARY_BUILTIN_FP16(FUNCTION)
-#define _CLC_DEFINE_BINARY_BUILTIN_FP16(FUNCTION)
-
-#endif
-
 #endif // __CLC_CLCMACRO_H__
diff --git a/libclc/clc/include/clc/shared/binary_def_with_int_second_arg.inc b/libclc/clc/include/clc/shared/binary_def_with_int_second_arg.inc
new file mode 100644
index 0000000000000..45b4382d865bc
--- /dev/null
+++ b/libclc/clc/include/clc/shared/binary_def_with_int_second_arg.inc
@@ -0,0 +1,17 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include <clc/utils.h>
+
+#ifndef __CLC_FUNCTION
+#define __CLC_FUNCTION(x) __CLC_CONCAT(__clc_, x)
+#endif
+
+_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE FUNCTION(__CLC_GENTYPE a, __CLC_INTN b) {
+  return __CLC_FUNCTION(FUNCTION)(a, b);
+}
diff --git a/libclc/clc/lib/generic/math/clc_copysign.cl b/libclc/clc/lib/generic/math/clc_copysign.cl
index d336985ebf967..b066c14bcf3f5 100644
--- a/libclc/clc/lib/generic/math/clc_copysign.cl
+++ b/libclc/clc/lib/generic/math/clc_copysign.cl
@@ -6,30 +6,10 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include <clc/clcmacro.h>
 #include <clc/internal/clc.h>
 
-_CLC_DEFINE_BINARY_BUILTIN_NO_SCALARIZE(float, __clc_copysign,
-                                        __builtin_elementwise_copysign, float,
-                                        float)
-
-#ifdef cl_khr_fp64
-
-#pragma OPENCL EXTENSION cl_khr_fp64 : enable
-
-_CLC_DEFINE_BINARY_BUILTIN_NO_SCALARIZE(double, __clc_copysign,
-                                        __builtin_elementwise_copysign, double,
-                                        double)
-
-#endif
-
-#ifdef cl_khr_fp16
-
-#pragma OPENCL EXTENSION cl_khr_fp16 : enable
-
-_CLC_DEFINE_BINARY_BUILTIN_NO_SCALARIZE(half, __clc_copysign,
-                                        __builtin_elementwise_copysign, half,
-                                        half)
-
-#endif
+#define FUNCTION __clc_copysign
+#define __CLC_FUNCTION(x) __builtin_elementwise_copysign
+#define __CLC_BODY <clc/shared/binary_def.inc>
 
+#include <clc/math/gentype.inc>
diff --git a/libclc/opencl/lib/clspv/math/fma.cl b/libclc/opencl/lib/clspv/math/fma.cl
index 2722018121224..0f3141a0e09ee 100644
--- a/libclc/opencl/lib/clspv/math/fma.cl
+++ b/libclc/opencl/lib/clspv/math/fma.cl
@@ -6,8 +6,12 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include <clc/clcmacro.h>
 #include <clc/internal/math/clc_sw_fma.h>
 #include <clc/opencl/clc.h>
 
-_CLC_DEFINE_TERNARY_BUILTIN(float, fma, __clc_sw_fma, float, float, float)
+#define __FLOAT_ONLY
+#define FUNCTION fma
+#define __CLC_FUNCTION(x) __clc_sw_fma
+#define __CLC_BODY <clc/shared/ternary_def.inc>
+
+#include <clc/math/gentype.inc>
diff --git a/libclc/opencl/lib/generic/common/degrees.cl b/libclc/opencl/lib/generic/common/degrees.cl
index 8b17fe4321297..a86003c170bff 100644
--- a/libclc/opencl/lib/generic/common/degrees.cl
+++ b/libclc/opencl/lib/generic/common/degrees.cl
@@ -6,22 +6,10 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include <clc/clcmacro.h>
 #include <clc/common/clc_degrees.h>
 #include <clc/opencl/clc.h>
 
-_CLC_DEFINE_UNARY_BUILTIN(float, degrees, __clc_degrees, float)
+#define FUNCTION degrees
+#define __CLC_BODY <clc/shared/unary_def.inc>
 
-#ifdef cl_khr_fp64
-#pragma OPENCL EXTENSION cl_khr_fp64 : enable
-
-_CLC_DEFINE_UNARY_BUILTIN(double, degrees, __clc_degrees, double)
-
-#endif
-
-#ifdef cl_khr_fp16
-#pragma OPENCL EXTENSION cl_khr_fp16 : enable
-
-_CLC_DEFINE_UNARY_BUILTIN(half, degrees, __clc_degrees, half)
-
-#endif
+#include <clc/math/gentype.inc>
diff --git a/libclc/opencl/lib/generic/common/radians.cl b/libclc/opencl/lib/generic/common/radians.cl
index 1c58c6c4da6f3..b45653be2e782 100644
--- a/libclc/opencl/lib/generic/common/radians.cl
+++ b/libclc/opencl/lib/generic/common/radians.cl
@@ -6,22 +6,10 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include <clc/clcmacro.h>
 #include <clc/common/clc_radians.h>
 #include <clc/opencl/clc.h>
 
-_CLC_DEFINE_UNARY_BUILTIN(float, radians, __clc_radians, float)
+#define FUNCTION radians
+#define __CLC_BODY <clc/shared/unary_def.inc>
 
-#ifdef cl_khr_fp64
-#pragma OPENCL EXTENSION cl_khr_fp64 : enable
-
-_CLC_DEFINE_UNARY_BUILTIN(double, radians, __clc_radians, double)
-
-#endif
-
-#ifdef cl_khr_fp16
-#pragma OPENCL EXTENSION cl_khr_fp16 : enable
-
-_CLC_DEFINE_UNARY_BUILTIN(half, radians, __clc_radians, half)
-
-#endif
+#include <clc/math/gentype.inc>
diff --git a/libclc/opencl/lib/generic/math/fma.cl b/libclc/opencl/lib/generic/math/fma.cl
index ee3395bb2c648..c077357a44f0d 100644
--- a/libclc/opencl/lib/generic/math/fma.cl
+++ b/libclc/opencl/lib/generic/math/fma.cl
@@ -6,23 +6,11 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include <clc/clcmacro.h>
 #include <clc/math/clc_fma.h>
 #include <clc/math/math.h>
 #include <clc/opencl/clc.h>
 
-_CLC_DEFINE_TERNARY_BUILTIN(float, fma, __clc_fma, float, float, float)
+#define FUNCTION fma
+#define __CLC_BODY <clc/shared/ternary_def.inc>
 
-#ifdef cl_khr_fp64
-#pragma OPENCL EXTENSION cl_khr_fp64 : enable
-
-_CLC_DEFINE_TERNARY_BUILTIN(double, fma, __clc_fma, double, double, double)
-
-#endif
-
-#ifdef cl_khr_fp16
-#pragma OPENCL EXTENSION cl_khr_fp16 : enable
-
-_CLC_DEFINE_TERNARY_BUILTIN(half, fma, __clc_fma, half, half, half)
-
-#endif
+#include <clc/math/gentype.inc>
diff --git a/libclc/opencl/lib/generic/math/ldexp.cl b/libclc/opencl/lib/generic/math/ldexp.cl
index e3b9b2b3f1363..069ba8251feba 100644
--- a/libclc/opencl/lib/generic/math/ldexp.cl
+++ b/libclc/opencl/lib/generic/math/ldexp.cl
@@ -6,27 +6,14 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include <clc/clcmacro.h>
 #include <clc/math/clc_ldexp.h>
 #include <clc/opencl/clc.h>
 
-_CLC_DEFINE_BINARY_BUILTIN_NO_SCALARIZE(float, ldexp, __clc_ldexp, float, int)
+#define FUNCTION ldexp
+#define __CLC_FUNCTION(x) __clc_ldexp
+#define __CLC_BODY <clc/shared/binary_def_with_int_second_arg.inc>
 
-#ifdef cl_khr_fp64
-
-#pragma OPENCL EXTENSION cl_khr_fp64 : enable
-
-_CLC_DEFINE_BINARY_BUILTIN_NO_SCALARIZE(double, ldexp, __clc_ldexp, double, int)
-
-#endif
-
-#ifdef cl_khr_fp16
-
-#pragma OPENCL EXTENSION cl_khr_fp16 : enable
-
-_CLC_DEFINE_BINARY_BUILTIN_NO_SCALARIZE(half, ldexp, __clc_ldexp, half, int)
-
-#endif
+#include <clc/math/gentype.inc>
 
 // This defines all the ldexp(GENTYPE, int) variants
 #define __CLC_BODY <ldexp.inc>
diff --git a/libclc/opencl/lib/generic/math/mad.cl b/libclc/opencl/lib/generic/math/mad.cl
index 20e6903094454..39aa8e884cc03 100644
--- a/libclc/opencl/lib/generic/math/mad.cl
+++ b/libclc/opencl/lib/generic/math/mad.cl
@@ -6,22 +6,10 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include <clc/clcmacro.h>
 #include <clc/math/clc_mad.h>
 #include <clc/opencl/clc.h>
 
-_CLC_DEFINE_TERNARY_BUILTIN(float, mad, __clc_mad, float, float, float)
+#define FUNCTION mad
+#define __CLC_BODY <clc/shared/ternary_def.inc>
 
-#ifdef cl_khr_fp64
-#pragma OPENCL EXTENSION cl_khr_fp64 : enable
-
-_CLC_DEFINE_TERNARY_BUILTIN(double, mad, __clc_mad, double, double, double)
-
-#endif
-
-#ifdef cl_khr_fp16
-#pragma OPENCL EXTENSION cl_khr_fp16 : enable
-
-_CLC_DEFINE_TERNARY_BUILTIN(half, mad, __clc_mad, half, half, half)
-
-#endif
+#include <clc/math/gentype.inc>
diff --git a/libclc/opencl/lib/generic/math/nextafter.cl b/libclc/opencl/lib/generic/math/nextafter.cl
index ecb187c53069e..6a5a745f82526 100644
--- a/libclc/opencl/lib/generic/math/nextafter.cl
+++ b/libclc/opencl/lib/generic/math/nextafter.cl
@@ -6,27 +6,11 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include <clc/clcmacro.h>
 #include <clc/math/clc_nextafter.h>
 #include <clc/opencl/clc.h>
 
-_CLC_DEFINE_BINARY_BUILTIN_NO_SCALARIZE(float, nextafter, __clc_nextafter,
-                                        float, float)
+#define FUNCTION nextafter
+#define __CLC_FUNCTION(x) __clc_nextafter
+#define __CLC_BODY <clc/shared/binary_def.inc>
 
-#ifdef cl_khr_fp64
-
-#pragma OPENCL EXTENSION cl_khr_fp64 : enable
-
-_CLC_DEFINE_BINARY_BUILTIN_NO_SCALARIZE(double, nextafter, __clc_nextafter,
-                                        double, double)
-
-#endif
-
-#ifdef cl_khr_fp16
-
-#pragma OPENCL EXTENSION cl_khr_fp16 : enable
-
-_CLC_DEFINE_BINARY_BUILTIN_NO_SCALARIZE(half, nextafter, __clc_nextafter, half,
-                                        half)
-
-#endif
+#include <clc/math/gentype.inc>
diff --git a/libclc/opencl/lib/spirv/math/fma.cl b/libclc/opencl/lib/spirv/math/fma.cl
index 2722018121224..2d826632b8e52 100644
--- a/libclc/opencl/lib/spirv/math/fma.cl
+++ b/libclc/opencl/lib/spirv/math/fma.cl
@@ -6,8 +6,12 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include <clc/clcmacro.h>
 #include <clc/internal/math/clc_sw_fma.h>
 #include <clc/opencl/clc.h>
 
-_CLC_DEFINE_TERNARY_BUILTIN(float, fma, __clc_sw_fma, float, float, float)
+#define FUNCTION fma
+#define __CLC_FUNCTION(x) __clc_sw_fma
+#define __CLC_BODY <clc/shared/ternary_def.inc>
+
+#define __FLOAT_ONLY
+#include <clc/math/gentype.inc>



More information about the cfe-commits mailing list