From edbe8277c10433ded3b8b28375dd8b0e85d108f1 Mon Sep 17 00:00:00 2001 From: Matt Arsenault Date: Fri, 20 Mar 2026 08:33:31 +0100 Subject: [PATCH] libclc: Use log intrinsic for half and float cases for amdgpu (#187538) This is pretty verbose and ugly. We're pulling the base implementation in for the double cases, and scalarizing it. Also fully defining the half and float cases to directly use the intrinsic, for all vector types. It would be much more convenient if we had linker based overrides for the generic implementations, rather than per source file. --- libclc/clc/lib/amdgpu/CMakeLists.txt | 3 ++ libclc/clc/lib/amdgpu/math/clc_amdgpu_log.inc | 11 +++++ libclc/clc/lib/amdgpu/math/clc_log.cl | 41 +++++++++++++++++++ libclc/clc/lib/amdgpu/math/clc_log10.cl | 41 +++++++++++++++++++ libclc/clc/lib/amdgpu/math/clc_log2.cl | 41 +++++++++++++++++++ 5 files changed, 137 insertions(+) create mode 100644 libclc/clc/lib/amdgpu/math/clc_amdgpu_log.inc create mode 100644 libclc/clc/lib/amdgpu/math/clc_log.cl create mode 100644 libclc/clc/lib/amdgpu/math/clc_log10.cl create mode 100644 libclc/clc/lib/amdgpu/math/clc_log2.cl diff --git a/libclc/clc/lib/amdgpu/CMakeLists.txt b/libclc/clc/lib/amdgpu/CMakeLists.txt index 3e3059570c18..1b7ad244e718 100644 --- a/libclc/clc/lib/amdgpu/CMakeLists.txt +++ b/libclc/clc/lib/amdgpu/CMakeLists.txt @@ -18,6 +18,9 @@ libclc_configure_source_list(CLC_AMDGPU_SOURCES math/clc_half_sqrt.cl math/clc_get_twobypi_bits.cl math/clc_ldexp.cl + math/clc_log.cl + math/clc_log10.cl + math/clc_log2.cl math/clc_log2_fast.cl math/clc_native_exp.cl math/clc_native_exp2.cl diff --git a/libclc/clc/lib/amdgpu/math/clc_amdgpu_log.inc b/libclc/clc/lib/amdgpu/math/clc_amdgpu_log.inc new file mode 100644 index 000000000000..7b0d75daca07 --- /dev/null +++ b/libclc/clc/lib/amdgpu/math/clc_amdgpu_log.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 +// +//===----------------------------------------------------------------------===// + +#ifdef __CLC_SCALAR +#include "clc_log_base.inc" +#endif diff --git a/libclc/clc/lib/amdgpu/math/clc_log.cl b/libclc/clc/lib/amdgpu/math/clc_log.cl new file mode 100644 index 000000000000..300e0dfdf3c4 --- /dev/null +++ b/libclc/clc/lib/amdgpu/math/clc_log.cl @@ -0,0 +1,41 @@ +//===----------------------------------------------------------------------===// +// +// 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/clc_convert.h" +#include "clc/float/definitions.h" +#include "clc/math/clc_ep.h" +#include "clc/math/clc_frexp.h" +#include "clc/math/clc_ldexp.h" +#include "clc/math/clc_log.h" +#include "clc/math/clc_mad.h" +#include "clc/relational/clc_isinf.h" + +#define __CLC_FUNCTION __clc_log + +#define __CLC_FLOAT_ONLY +#define __CLC_IMPL_FUNCTION(x) __builtin_elementwise_log +#define __CLC_BODY "clc/shared/unary_def.inc" +#include "clc/math/gentype.inc" +#undef __CLC_IMPL_FUNCTION +#undef __CLC_FLOAT_ONLY + +#define __CLC_HALF_ONLY +#define __CLC_IMPL_FUNCTION(x) __builtin_elementwise_log +#define __CLC_BODY "clc/shared/unary_def.inc" +#include "clc/math/gentype.inc" +#undef __CLC_IMPL_FUNCTION +#undef __CLC_HALF_ONLY + +#define COMPILING_LOG +#define __CLC_DOUBLE_ONLY +#define __CLC_BODY "clc_amdgpu_log.inc" +#include "clc/math/gentype.inc" + +#define __CLC_DOUBLE_ONLY +#define __CLC_BODY "clc/shared/unary_def_scalarize_loop.inc" +#include "clc/math/gentype.inc" diff --git a/libclc/clc/lib/amdgpu/math/clc_log10.cl b/libclc/clc/lib/amdgpu/math/clc_log10.cl new file mode 100644 index 000000000000..2c01f791b62c --- /dev/null +++ b/libclc/clc/lib/amdgpu/math/clc_log10.cl @@ -0,0 +1,41 @@ +//===----------------------------------------------------------------------===// +// +// 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/clc_convert.h" +#include "clc/float/definitions.h" +#include "clc/math/clc_ep.h" +#include "clc/math/clc_frexp.h" +#include "clc/math/clc_ldexp.h" +#include "clc/math/clc_log10.h" +#include "clc/math/clc_mad.h" +#include "clc/relational/clc_isinf.h" + +#define __CLC_FUNCTION __clc_log10 + +#define __CLC_FLOAT_ONLY +#define __CLC_IMPL_FUNCTION(x) __builtin_elementwise_log10 +#define __CLC_BODY "clc/shared/unary_def.inc" +#include "clc/math/gentype.inc" +#undef __CLC_IMPL_FUNCTION +#undef __CLC_FLOAT_ONLY + +#define __CLC_HALF_ONLY +#define __CLC_IMPL_FUNCTION(x) __builtin_elementwise_log10 +#define __CLC_BODY "clc/shared/unary_def.inc" +#include "clc/math/gentype.inc" +#undef __CLC_IMPL_FUNCTION +#undef __CLC_HALF_ONLY + +#define COMPILING_LOG10 +#define __CLC_DOUBLE_ONLY +#define __CLC_BODY "clc_amdgpu_log.inc" +#include "clc/math/gentype.inc" + +#define __CLC_DOUBLE_ONLY +#define __CLC_BODY "clc/shared/unary_def_scalarize_loop.inc" +#include "clc/math/gentype.inc" diff --git a/libclc/clc/lib/amdgpu/math/clc_log2.cl b/libclc/clc/lib/amdgpu/math/clc_log2.cl new file mode 100644 index 000000000000..e02e6fc375e2 --- /dev/null +++ b/libclc/clc/lib/amdgpu/math/clc_log2.cl @@ -0,0 +1,41 @@ +//===----------------------------------------------------------------------===// +// +// 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/clc_convert.h" +#include "clc/float/definitions.h" +#include "clc/math/clc_ep.h" +#include "clc/math/clc_frexp.h" +#include "clc/math/clc_ldexp.h" +#include "clc/math/clc_log2.h" +#include "clc/math/clc_mad.h" +#include "clc/relational/clc_isinf.h" + +#define __CLC_FUNCTION __clc_log2 + +#define __CLC_FLOAT_ONLY +#define __CLC_IMPL_FUNCTION(x) __builtin_elementwise_log2 +#define __CLC_BODY "clc/shared/unary_def.inc" +#include "clc/math/gentype.inc" +#undef __CLC_IMPL_FUNCTION +#undef __CLC_FLOAT_ONLY + +#define __CLC_HALF_ONLY +#define __CLC_IMPL_FUNCTION(x) __builtin_elementwise_log2 +#define __CLC_BODY "clc/shared/unary_def.inc" +#include "clc/math/gentype.inc" +#undef __CLC_IMPL_FUNCTION +#undef __CLC_HALF_ONLY + +#define COMPILING_LOG2 +#define __CLC_DOUBLE_ONLY +#define __CLC_BODY "clc_amdgpu_log.inc" +#include "clc/math/gentype.inc" + +#define __CLC_DOUBLE_ONLY +#define __CLC_BODY "clc/shared/unary_def_scalarize_loop.inc" +#include "clc/math/gentype.inc"