Skip to content
Closed
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
7e2d210
[libclc] Refine __clc_fp*_subnormals_supported and __clc_flush_denorm…
wenju-he Sep 9, 2025
96ec9dc
Apply suggestions from code review
wenju-he Sep 9, 2025
d52fcdb
use __builtin_elementwise_canonicalize
wenju-he Sep 9, 2025
4608f77
set -fdenormal-fp-math-f32=dynamic build flag globally
wenju-he Sep 9, 2025
3f665ce
rename __clc_flush_denormal_if_not_supportedto __clc_soft_flush_deno…
wenju-he Sep 10, 2025
7b290a2
delete clc_sw_fma
wenju-he Oct 3, 2025
3da9705
-fdenormal-fp-math-f32 -> -fdenormal-fp-math
wenju-he Oct 3, 2025
7d21a1a
remove -Xclang before -fdenormal-fp-math=dynamic
wenju-he Oct 3, 2025
b61e32b
Revert "delete clc_sw_fma"
wenju-he Oct 7, 2025
ccf7a6e
support SPIR-V: implement __clc_fp32_subnormals_supported without usi…
wenju-he Oct 8, 2025
23d0ff7
revert change to __clc_soft_flush_denormal
wenju-he Jan 26, 2026
ddb79ae
Merge branch 'main' into refine-__clc_fp32_subnormals_supported
wenju-he Jan 26, 2026
3e2ac98
Merge branch 'main' into refine-__clc_fp32_subnormals_supported
wenju-he Jan 26, 2026
34cd062
revert name __clc_soft_flush_denormal to __clc_flush_denormal_if_not_…
wenju-he Jan 26, 2026
b14455b
clang-format clc_subnormal_config.cl
wenju-he Jan 26, 2026
7757ba0
update comment: Should be -> Expected
wenju-he Jan 26, 2026
fe2d4f6
fix build: include clc_subnormal_config.h
wenju-he Jan 26, 2026
7ff4dcf
__clc_fp32_subnormals_supported: use __clc_fabs for SPIR-V and canoni…
wenju-he Jan 27, 2026
5eb6990
__clc_flush_denormal_if_not_supported: use __builtin_elementwise_cano…
wenju-he Jan 27, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 1 addition & 21 deletions libclc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ endif()
set( LIBCLC_TARGETS_TO_BUILD ${LIBCLC_DEFAULT_TARGET}
CACHE STRING "Semicolon-separated list of libclc targets to build, or 'all'." )

option( ENABLE_RUNTIME_SUBNORMAL "Enable runtime linking of subnormal support." OFF )

option(
LIBCLC_USE_SPIRV_BACKEND "Build SPIR-V targets with the SPIR-V backend." OFF
)
Expand Down Expand Up @@ -239,19 +237,6 @@ set( tahiti_aliases pitcairn verde oland hainan bonaire kabini kaveri hawaii
configure_file( libclc.pc.in libclc.pc @ONLY )
install( FILES ${CMAKE_CURRENT_BINARY_DIR}/libclc.pc DESTINATION "${CMAKE_INSTALL_DATADIR}/pkgconfig" )

if( ENABLE_RUNTIME_SUBNORMAL )
foreach( file IN ITEMS subnormal_use_default subnormal_disable )
link_bc(
TARGET ${file}
INPUTS ${CMAKE_CURRENT_SOURCE_DIR}/opencl/lib/generic/${file}.ll
)
install(
FILES $<TARGET_PROPERTY:${file},TARGET_FILE>
DESTINATION "${CMAKE_INSTALL_DATADIR}/clc"
)
endforeach()
endif()

set_source_files_properties(
# CLC builtins
${CMAKE_CURRENT_SOURCE_DIR}/clc/lib/generic/math/clc_native_cos.cl
Expand Down Expand Up @@ -350,12 +335,6 @@ foreach( t ${LIBCLC_TARGETS_TO_BUILD} )

set( opencl_lib_files )

if( NOT ARCH STREQUAL spirv AND NOT ARCH STREQUAL spirv64 AND
NOT ARCH STREQUAL clspv AND NOT ARCH STREQUAL clspv64 AND
NOT ENABLE_RUNTIME_SUBNORMAL )
list( APPEND opencl_lib_files opencl/lib/generic/subnormal_use_default.ll )
endif()

libclc_configure_lib_source(
opencl_lib_files
LIB_ROOT_DIR opencl
Expand Down Expand Up @@ -412,6 +391,7 @@ foreach( t ${LIBCLC_TARGETS_TO_BUILD} )
# Error on undefined macros
-Werror=undef
-fdiscard-value-names
-fdenormal-fp-math=dynamic
)

if( NOT "${cpu}" STREQUAL "" )
Expand Down
1 change: 0 additions & 1 deletion libclc/clc/include/clc/math/clc_subnormal_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

#include <clc/clcfunc.h>

_CLC_DECL bool __clc_subnormals_disabled();
_CLC_DECL bool __clc_fp16_subnormals_supported();
_CLC_DECL bool __clc_fp32_subnormals_supported();
_CLC_DECL bool __clc_fp64_subnormals_supported();
Expand Down
2 changes: 1 addition & 1 deletion libclc/clc/include/clc/math/math.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ bool __attribute__((noinline)) __clc_runtime_has_hw_fma32(void);

#define LOG_MAGIC_NUM_SP32 (1 + NUMEXPBITS_SP32 - EXPBIAS_SP32)

_CLC_OVERLOAD _CLC_INLINE float __clc_flush_denormal_if_not_supported(float x) {
static _CLC_INLINE float __clc_flush_denormal_if_not_supported(float x) {
int ix = __clc_as_int(x);
if (!__clc_fp32_subnormals_supported() && ((ix & EXPBITS_SP32) == 0) &&
((ix & MANTBITS_SP32) != 0)) {
Expand Down
1 change: 1 addition & 0 deletions libclc/clc/lib/generic/SOURCES
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ math/clc_sincos_helpers.cl
math/clc_sinh.cl
math/clc_sinpi.cl
math/clc_sqrt.cl
math/clc_subnormal_config.cl
math/clc_sw_fma.cl
Comment thread
wenju-he marked this conversation as resolved.
math/clc_tables.cl
math/clc_tan.cl
Expand Down
1 change: 0 additions & 1 deletion libclc/clc/lib/generic/math/clc_exp10.cl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#include <clc/math/clc_fma.h>
#include <clc/math/clc_ldexp.h>
#include <clc/math/clc_mad.h>
#include <clc/math/clc_subnormal_config.h>
#include <clc/math/math.h>
#include <clc/math/tables.h>
#include <clc/relational/clc_isnan.h>
Expand Down
1 change: 0 additions & 1 deletion libclc/clc/lib/generic/math/clc_hypot.cl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
#include <clc/math/clc_fma.h>
#include <clc/math/clc_mad.h>
#include <clc/math/clc_sqrt.h>
#include <clc/math/clc_subnormal_config.h>
#include <clc/math/math.h>
#include <clc/relational/clc_isnan.h>
#include <clc/shared/clc_clamp.h>
Expand Down
1 change: 0 additions & 1 deletion libclc/clc/lib/generic/math/clc_pow.cl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
#include <clc/math/clc_fma.h>
#include <clc/math/clc_ldexp.h>
#include <clc/math/clc_mad.h>
#include <clc/math/clc_subnormal_config.h>
#include <clc/math/math.h>
#include <clc/math/tables.h>
#include <clc/relational/clc_select.h>
Expand Down
1 change: 0 additions & 1 deletion libclc/clc/lib/generic/math/clc_pown.cl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
#include <clc/math/clc_fma.h>
#include <clc/math/clc_ldexp.h>
#include <clc/math/clc_mad.h>
#include <clc/math/clc_subnormal_config.h>
#include <clc/math/math.h>
#include <clc/math/tables.h>
#include <clc/relational/clc_select.h>
Expand Down
1 change: 0 additions & 1 deletion libclc/clc/lib/generic/math/clc_powr.cl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
#include <clc/math/clc_fma.h>
#include <clc/math/clc_ldexp.h>
#include <clc/math/clc_mad.h>
#include <clc/math/clc_subnormal_config.h>
#include <clc/math/math.h>
#include <clc/math/tables.h>
#include <clc/relational/clc_select.h>
Expand Down
43 changes: 43 additions & 0 deletions libclc/clc/lib/generic/math/clc_subnormal_config.cl
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//===----------------------------------------------------------------------===//
//
// 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/internal/clc.h>
#include <clc/math/clc_subnormal_config.h>

#ifdef cl_khr_fp16
#pragma OPENCL EXTENSION cl_khr_fp16 : enable
_CLC_DEF bool __clc_fp16_subnormals_supported() {
// SPIR-V doesn't support llvm.canonicalize. Synthesize a subnormal by halving
// the smallest normal. If subnormals are not supported it will flush to +0.
half smallest_normal = 0x1p-14h;
half sub =
smallest_normal * 0.5h; // Expected 0x1p-15h (subnormal) if supported
return !__builtin_isfpclass(sub, __FPCLASS_POSZERO);
}
#endif // cl_khr_fp16

_CLC_DEF bool __clc_fp32_subnormals_supported() {
// SPIR-V doesn't support llvm.canonicalize. Synthesize a subnormal by halving
// the smallest normal. If subnormals are not supported it will flush to +0.
float smallest_normal = 0x1p-126f;
float sub =

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't need multiply, and I think this logic should be inverted. Denormal support is the base case, DAZ is an aberration. i.e., this is what I did in device-libs is about
is_daz() { return __builtin_isfpclass(__builtin_canonicalizef(0x1p-149f), __FPCLASS_POSZERO) }

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reverted

smallest_normal * 0.5f; // Expected 0x1p-127f (subnormal) if supported
return !__builtin_isfpclass(sub, __FPCLASS_POSZERO);
}

#ifdef cl_khr_fp64
#pragma OPENCL EXTENSION cl_khr_fp64 : enable
_CLC_DEF bool __clc_fp64_subnormals_supported() {
// SPIR-V doesn't support llvm.canonicalize. Synthesize a subnormal by halving

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix SPIRV, this is not a reasonable workaround. This is not a difficult to implement intrinsic

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed SPIRV path to use __clc_fabs(0x1p-149f) in 7ff4dcf

@Maetveis Maetveis Jan 28, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've made an attempt to add the lowering in #178439

// the smallest normal. If subnormals are not supported it will flush to +0.
double smallest_normal = 0x1p-1022;
double sub =
smallest_normal * 0.5; // Expected 0x1p-1023 (subnormal) if supported
return !__builtin_isfpclass(sub, __FPCLASS_POSZERO);
}
#endif // cl_khr_fp64
1 change: 0 additions & 1 deletion libclc/opencl/lib/clspv/SOURCES
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ conversion/convert_int2float.cl
conversion/convert_integer.cl
math/fma.cl
shared/vstore_half.cl
subnormal_config.cl
../generic/geometric/distance.cl
../generic/geometric/length.cl
../generic/math/acos.cl
Expand Down
16 changes: 0 additions & 16 deletions libclc/opencl/lib/clspv/subnormal_config.cl

This file was deleted.

2 changes: 0 additions & 2 deletions libclc/opencl/lib/generic/SOURCES
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
subnormal_config.cl
subnormal_helper_func.ll
async/async_work_group_copy.cl
async/async_work_group_strided_copy.cl
async/prefetch.cl
Expand Down
18 changes: 0 additions & 18 deletions libclc/opencl/lib/generic/subnormal_config.cl

This file was deleted.

9 changes: 0 additions & 9 deletions libclc/opencl/lib/generic/subnormal_disable.ll

This file was deleted.

16 changes: 0 additions & 16 deletions libclc/opencl/lib/generic/subnormal_helper_func.ll

This file was deleted.

9 changes: 0 additions & 9 deletions libclc/opencl/lib/generic/subnormal_use_default.ll

This file was deleted.

1 change: 0 additions & 1 deletion libclc/opencl/lib/spirv/SOURCES
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
subnormal_config.cl
../generic/async/async_work_group_strided_copy.cl
../generic/async/wait_group_events.cl
../generic/common/degrees.cl
Expand Down
16 changes: 0 additions & 16 deletions libclc/opencl/lib/spirv/subnormal_config.cl

This file was deleted.

Loading