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

Using the elementwise builtin optimizes the vector case; instead of scalarizing 
we can compile directly to the vector intrinsics.

>From 17263a7d8d1c98ccec38c6a0742524e933c27648 Mon Sep 17 00:00:00 2001
From: Fraser Cormack <fra...@codeplay.com>
Date: Wed, 19 Mar 2025 11:24:22 +0000
Subject: [PATCH] [libclc] Use elementwise ctlz/cttz builtins for CLC clz/ctz

Using the elementwise builtin optimizes the vector case; instead of
scalarizing we can compile directly to the vector intrinsics.
---
 libclc/clc/lib/generic/integer/clc_clz.cl  | 35 +---------------------
 libclc/clc/lib/generic/integer/clc_clz.inc | 11 +++++++
 libclc/clc/lib/generic/integer/clc_ctz.cl  | 31 +------------------
 libclc/clc/lib/generic/integer/clc_ctz.inc | 11 +++++++
 4 files changed, 24 insertions(+), 64 deletions(-)
 create mode 100644 libclc/clc/lib/generic/integer/clc_clz.inc
 create mode 100644 libclc/clc/lib/generic/integer/clc_ctz.inc

diff --git a/libclc/clc/lib/generic/integer/clc_clz.cl 
b/libclc/clc/lib/generic/integer/clc_clz.cl
index 0d0c80b7cd1ff..37ba4dbfd8094 100644
--- a/libclc/clc/lib/generic/integer/clc_clz.cl
+++ b/libclc/clc/lib/generic/integer/clc_clz.cl
@@ -10,38 +10,5 @@
 #include <clc/integer/clc_clz.h>
 #include <clc/internal/clc.h>
 
-_CLC_OVERLOAD _CLC_DEF char __clc_clz(char x) {
-  return __clc_clz((ushort)(uchar)x) - 8;
-}
-
-_CLC_OVERLOAD _CLC_DEF uchar __clc_clz(uchar x) {
-  return __clc_clz((ushort)x) - 8;
-}
-
-_CLC_OVERLOAD _CLC_DEF short __clc_clz(short x) {
-  return x ? __builtin_clzs(x) : 16;
-}
-
-_CLC_OVERLOAD _CLC_DEF ushort __clc_clz(ushort x) {
-  return x ? __builtin_clzs(x) : 16;
-}
-
-_CLC_OVERLOAD _CLC_DEF int __clc_clz(int x) {
-  return x ? __builtin_clz(x) : 32;
-}
-
-_CLC_OVERLOAD _CLC_DEF uint __clc_clz(uint x) {
-  return x ? __builtin_clz(x) : 32;
-}
-
-_CLC_OVERLOAD _CLC_DEF long __clc_clz(long x) {
-  return x ? __builtin_clzl(x) : 64;
-}
-
-_CLC_OVERLOAD _CLC_DEF ulong __clc_clz(ulong x) {
-  return x ? __builtin_clzl(x) : 64;
-}
-
-#define __CLC_FUNCTION __clc_clz
-#define __CLC_BODY <clc/shared/unary_def_scalarize.inc>
+#define __CLC_BODY <clc_clz.inc>
 #include <clc/integer/gentype.inc>
diff --git a/libclc/clc/lib/generic/integer/clc_clz.inc 
b/libclc/clc/lib/generic/integer/clc_clz.inc
new file mode 100644
index 0000000000000..ba7eb77d50036
--- /dev/null
+++ b/libclc/clc/lib/generic/integer/clc_clz.inc
@@ -0,0 +1,11 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE __clc_clz(__CLC_GENTYPE x) {
+  return __builtin_elementwise_ctlz(x, (__CLC_GENTYPE)__CLC_GENSIZE);
+}
diff --git a/libclc/clc/lib/generic/integer/clc_ctz.cl 
b/libclc/clc/lib/generic/integer/clc_ctz.cl
index 8dbebb3f345a5..5cf4c19811db2 100644
--- a/libclc/clc/lib/generic/integer/clc_ctz.cl
+++ b/libclc/clc/lib/generic/integer/clc_ctz.cl
@@ -10,34 +10,5 @@
 #include <clc/integer/clc_ctz.h>
 #include <clc/internal/clc.h>
 
-_CLC_OVERLOAD _CLC_DEF char __clc_ctz(char x) {
-  return __clc_ctz(__clc_as_uchar(x));
-}
-
-_CLC_OVERLOAD _CLC_DEF uchar __clc_ctz(uchar x) { return __builtin_ctzg(x, 8); 
}
-
-_CLC_OVERLOAD _CLC_DEF short __clc_ctz(short x) {
-  return __clc_ctz(__clc_as_ushort(x));
-}
-
-_CLC_OVERLOAD _CLC_DEF ushort __clc_ctz(ushort x) {
-  return __builtin_ctzg(x, 16);
-}
-
-_CLC_OVERLOAD _CLC_DEF int __clc_ctz(int x) {
-  return __clc_ctz(__clc_as_uint(x));
-}
-
-_CLC_OVERLOAD _CLC_DEF uint __clc_ctz(uint x) { return __builtin_ctzg(x, 32); }
-
-_CLC_OVERLOAD _CLC_DEF long __clc_ctz(long x) {
-  return __clc_ctz(__clc_as_ulong(x));
-}
-
-_CLC_OVERLOAD _CLC_DEF ulong __clc_ctz(ulong x) {
-  return __builtin_ctzg(x, 64);
-}
-
-#define __CLC_FUNCTION __clc_ctz
-#define __CLC_BODY <clc/shared/unary_def_scalarize.inc>
+#define __CLC_BODY <clc_ctz.inc>
 #include <clc/integer/gentype.inc>
diff --git a/libclc/clc/lib/generic/integer/clc_ctz.inc 
b/libclc/clc/lib/generic/integer/clc_ctz.inc
new file mode 100644
index 0000000000000..cebd392542be9
--- /dev/null
+++ b/libclc/clc/lib/generic/integer/clc_ctz.inc
@@ -0,0 +1,11 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE __clc_ctz(__CLC_GENTYPE x) {
+  return __builtin_elementwise_cttz(x, (__CLC_GENTYPE)__CLC_GENSIZE);
+}

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

Reply via email to