[libclc] libclc: do not use int64 in sincos helpers (PR #188056)

Romaric Jodin via cfe-commits cfe-commits at lists.llvm.org
Thu Mar 26 07:13:07 PDT 2026


https://github.com/rjodinchr updated https://github.com/llvm/llvm-project/pull/188056

>From 2ff304b1905c8ca5e2950ca5fd3adbd5fcae1f7e Mon Sep 17 00:00:00 2001
From: Romaric Jodin <rjodin at google.com>
Date: Mon, 23 Mar 2026 16:56:07 +0100
Subject: [PATCH 1/2] libclc: do not use int64 in sincos helpers

int64 is optional, thus we do not want to force its usage for clspv.
---
 libclc/clc/lib/generic/math/clc_sincos_helpers.cl  | 12 ++++++++++++
 libclc/clc/lib/generic/math/clc_sincos_helpers.inc | 12 ++++++++++++
 2 files changed, 24 insertions(+)

diff --git a/libclc/clc/lib/generic/math/clc_sincos_helpers.cl b/libclc/clc/lib/generic/math/clc_sincos_helpers.cl
index bb41e6cc67ed0..8b6a0d6445eab 100644
--- a/libclc/clc/lib/generic/math/clc_sincos_helpers.cl
+++ b/libclc/clc/lib/generic/math/clc_sincos_helpers.cl
@@ -22,6 +22,18 @@
 #include "clc/relational/clc_isinf.h"
 #include "clc/relational/clc_isnan.h"
 
+#if defined(CLC_CLSPV)
+#include "clc/integer/clc_mul_hi.h"
+#define __CLC_FULL_MUL(A, B, HI, LO)                                           \
+  LO = A * B;                                                                  \
+  HI = __clc_mul_hi(A, B)
+
+#define __CLC_FULL_MAD(A, B, C, HI, LO)                                        \
+  LO = ((A) * (B) + (C));                                                      \
+  HI = __clc_mul_hi(A, B);                                                     \
+  HI += LO < C ? 1U : 0U;
+#endif
+
 #define bitalign(hi, lo, shift) __builtin_elementwise_fshr(hi, lo, shift)
 
 #define __CLC_FLOAT_ONLY
diff --git a/libclc/clc/lib/generic/math/clc_sincos_helpers.inc b/libclc/clc/lib/generic/math/clc_sincos_helpers.inc
index 2603f9f46de1a..01cacf815cc51 100644
--- a/libclc/clc/lib/generic/math/clc_sincos_helpers.inc
+++ b/libclc/clc/lib/generic/math/clc_sincos_helpers.inc
@@ -184,6 +184,17 @@ __clc_argReductionLargeS(private __CLC_FLOATN *r, __CLC_FLOATN x) {
   const __CLC_UINTN b0 = 0xFE5163ABU;
 
   __CLC_UINTN p0, p1, p2, p3, p4, p5, p6, p7;
+#if defined(CLC_CLSPV)
+  __CLC_UINTN c0, c1;
+
+  __CLC_FULL_MUL(xm, b0, c0, p0);
+  __CLC_FULL_MAD(xm, b1, c0, c1, p1);
+  __CLC_FULL_MAD(xm, b2, c1, c0, p2);
+  __CLC_FULL_MAD(xm, b3, c0, c1, p3);
+  __CLC_FULL_MAD(xm, b4, c1, c0, p4);
+  __CLC_FULL_MAD(xm, b5, c0, c1, p5);
+  __CLC_FULL_MAD(xm, b6, c1, p7, p6);
+#else
   __CLC_ULONGN a;
 
   __CLC_ULONGN xm_u64 = __CLC_CONVERT_ULONGN(xm);
@@ -215,6 +226,7 @@ __clc_argReductionLargeS(private __CLC_FLOATN *r, __CLC_FLOATN x) {
   a = xm_u64 * __CLC_CONVERT_ULONGN(b6) + a;
   p6 = __CLC_CONVERT_UINTN(a);
   p7 = __CLC_CONVERT_UINTN(a >> 32);
+#endif
 
   __CLC_UINTN fbits =
       (__CLC_UINTN)224 + (__CLC_UINTN)23 - __CLC_CONVERT_UINTN(xe);

>From dcbe85b301e52998747e367d8914d2f1de32ff8d Mon Sep 17 00:00:00 2001
From: Romaric Jodin <rjodin at google.com>
Date: Thu, 26 Mar 2026 14:13:36 +0100
Subject: [PATCH 2/2] use __opencl_c_int64 for the guard macro instead of
 CLC_CLSPV

---
 libclc/CMakeLists.txt                              | 1 +
 libclc/clc/lib/generic/math/clc_sincos_helpers.cl  | 2 +-
 libclc/clc/lib/generic/math/clc_sincos_helpers.inc | 2 +-
 3 files changed, 3 insertions(+), 2 deletions(-)

diff --git a/libclc/CMakeLists.txt b/libclc/CMakeLists.txt
index 1103711298ce3..b14e31369929b 100644
--- a/libclc/CMakeLists.txt
+++ b/libclc/CMakeLists.txt
@@ -191,6 +191,7 @@ foreach( t ${LIBCLC_TARGETS_TO_BUILD} )
     set(opt_flags)
   elseif(ARCH STREQUAL clspv OR ARCH STREQUAL clspv64)
     list(APPEND target_compile_flags -Wno-unknown-assumption)
+    list(APPEND target_compile_flags -U__opencl_c_int64)
     list(APPEND target_extra_defines CLC_CLSPV)
   elseif(ARCH STREQUAL amdgcn)
     list(APPEND target_compile_flags "SHELL:-Xclang -mcode-object-version=none")
diff --git a/libclc/clc/lib/generic/math/clc_sincos_helpers.cl b/libclc/clc/lib/generic/math/clc_sincos_helpers.cl
index 8b6a0d6445eab..5a4c5017e8497 100644
--- a/libclc/clc/lib/generic/math/clc_sincos_helpers.cl
+++ b/libclc/clc/lib/generic/math/clc_sincos_helpers.cl
@@ -22,7 +22,7 @@
 #include "clc/relational/clc_isinf.h"
 #include "clc/relational/clc_isnan.h"
 
-#if defined(CLC_CLSPV)
+#ifndef __opencl_c_int64
 #include "clc/integer/clc_mul_hi.h"
 #define __CLC_FULL_MUL(A, B, HI, LO)                                           \
   LO = A * B;                                                                  \
diff --git a/libclc/clc/lib/generic/math/clc_sincos_helpers.inc b/libclc/clc/lib/generic/math/clc_sincos_helpers.inc
index 01cacf815cc51..ce0daf464483d 100644
--- a/libclc/clc/lib/generic/math/clc_sincos_helpers.inc
+++ b/libclc/clc/lib/generic/math/clc_sincos_helpers.inc
@@ -184,7 +184,7 @@ __clc_argReductionLargeS(private __CLC_FLOATN *r, __CLC_FLOATN x) {
   const __CLC_UINTN b0 = 0xFE5163ABU;
 
   __CLC_UINTN p0, p1, p2, p3, p4, p5, p6, p7;
-#if defined(CLC_CLSPV)
+#ifndef __opencl_c_int64
   __CLC_UINTN c0, c1;
 
   __CLC_FULL_MUL(xm, b0, c0, p0);



More information about the cfe-commits mailing list