Author: Wenju He
Date: 2025-09-05T19:58:24+08:00
New Revision: a271d07488a85ce677674bbe8101b10efff58c95

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

LOG: [libclc] Implement erf/erfc vector function with loop since scalar 
function is large (#157055)

This PR reduces amdgcn--amdhsa.bc size by 1.8% and nvptx64--nvidiacl.bc
size by 4%.
Loop trip count is constant and backend can decide whether to unroll.

---------

Co-authored-by: Copilot <175728472+copi...@users.noreply.github.com>

Added: 
    libclc/clc/include/clc/shared/unary_def_scalarize_loop.inc

Modified: 
    libclc/clc/lib/generic/math/clc_erf.cl
    libclc/clc/lib/generic/math/clc_erfc.cl

Removed: 
    


################################################################################
diff  --git a/libclc/clc/include/clc/shared/unary_def_scalarize_loop.inc 
b/libclc/clc/include/clc/shared/unary_def_scalarize_loop.inc
new file mode 100644
index 0000000000000..544057b0e1378
--- /dev/null
+++ b/libclc/clc/include/clc/shared/unary_def_scalarize_loop.inc
@@ -0,0 +1,28 @@
+//===----------------------------------------------------------------------===//
+//
+// 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>
+
+#if __CLC_VECSIZE_OR_1 >= 2
+
+#ifndef __CLC_IMPL_FUNCTION
+#define __CLC_IMPL_FUNCTION __CLC_FUNCTION
+#endif
+
+_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE __CLC_FUNCTION(__CLC_GENTYPE x) {
+  union {
+    __CLC_GENTYPE vec;
+    __CLC_SCALAR_GENTYPE arr[__CLC_VECSIZE_OR_1];
+  } u_x, u_result;
+  u_x.vec = x;
+  for (int i = 0; i < __CLC_VECSIZE_OR_1; ++i)
+    u_result.arr[i] = __CLC_IMPL_FUNCTION(u_x.arr[i]);
+  return u_result.vec;
+}
+
+#endif // __CLC_VECSIZE_OR_1 >= 2

diff  --git a/libclc/clc/lib/generic/math/clc_erf.cl 
b/libclc/clc/lib/generic/math/clc_erf.cl
index 34c7d586131e2..61a7c9d684aab 100644
--- a/libclc/clc/lib/generic/math/clc_erf.cl
+++ b/libclc/clc/lib/generic/math/clc_erf.cl
@@ -507,5 +507,5 @@ _CLC_OVERLOAD _CLC_DEF half __clc_erf(half x) {
 #endif
 
 #define __CLC_FUNCTION __clc_erf
-#define __CLC_BODY <clc/shared/unary_def_scalarize.inc>
+#define __CLC_BODY <clc/shared/unary_def_scalarize_loop.inc>
 #include <clc/math/gentype.inc>

diff  --git a/libclc/clc/lib/generic/math/clc_erfc.cl 
b/libclc/clc/lib/generic/math/clc_erfc.cl
index 7922807818ea2..01dbcd0c39ae1 100644
--- a/libclc/clc/lib/generic/math/clc_erfc.cl
+++ b/libclc/clc/lib/generic/math/clc_erfc.cl
@@ -518,5 +518,5 @@ _CLC_OVERLOAD _CLC_DEF half __clc_erfc(half x) {
 #endif
 
 #define __CLC_FUNCTION __clc_erfc
-#define __CLC_BODY <clc/shared/unary_def_scalarize.inc>
+#define __CLC_BODY <clc/shared/unary_def_scalarize_loop.inc>
 #include <clc/math/gentype.inc>


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

Reply via email to