Skip to content

Commit 7e2d210

Browse files
wenju-hearsenm
andcommitted
[libclc] Refine __clc_fp*_subnormals_supported and __clc_flush_denormal_if_not_supported
Remove the dependency on the libclc build-time configuration for __clc_fp*_subnormals_supported. The check is now implemented with LLVM intrinsics so it can be resolved during target lowering or at runtime. Improve __clc_flush_denormal_if_not_supported implementation as well. It doesn't use __clc_fp*_subnormals_supported which canonicalizes sNaN and thus the new implementation is more foldable. Remove cmake option ENABLE_RUNTIME_SUBNORMAL and related code. Resolves #153148 Co-authored-by: Matt Arsenault <[email protected]>
1 parent c82fbe8 commit 7e2d210

20 files changed

+52
-121
lines changed

libclc/CMakeLists.txt

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@ set( LIBCLC_MIN_LLVM 3.9.0 )
4141
set( LIBCLC_TARGETS_TO_BUILD "all"
4242
CACHE STRING "Semicolon-separated list of libclc targets to build, or 'all'." )
4343

44-
option( ENABLE_RUNTIME_SUBNORMAL "Enable runtime linking of subnormal support." OFF )
45-
4644
option(
4745
LIBCLC_USE_SPIRV_BACKEND "Build SPIR-V targets with the SPIR-V backend." OFF
4846
)
@@ -231,19 +229,6 @@ set( tahiti_aliases pitcairn verde oland hainan bonaire kabini kaveri hawaii
231229
configure_file( libclc.pc.in libclc.pc @ONLY )
232230
install( FILES ${CMAKE_CURRENT_BINARY_DIR}/libclc.pc DESTINATION "${CMAKE_INSTALL_DATADIR}/pkgconfig" )
233231

234-
if( ENABLE_RUNTIME_SUBNORMAL )
235-
foreach( file IN ITEMS subnormal_use_default subnormal_disable )
236-
link_bc(
237-
TARGET ${file}
238-
INPUTS ${CMAKE_CURRENT_SOURCE_DIR}/opencl/lib/generic/${file}.ll
239-
)
240-
install(
241-
FILES $<TARGET_PROPERTY:${file},TARGET_FILE>
242-
DESTINATION "${CMAKE_INSTALL_DATADIR}/clc"
243-
)
244-
endforeach()
245-
endif()
246-
247232
find_package( Python3 REQUIRED COMPONENTS Interpreter )
248233
file( TO_CMAKE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/utils/gen_convert.py script_loc )
249234
add_custom_command(
@@ -371,9 +356,6 @@ foreach( t ${LIBCLC_TARGETS_TO_BUILD} )
371356
list( APPEND opencl_gen_files clspv-convert.cl )
372357
else()
373358
list( APPEND opencl_gen_files convert.cl )
374-
if ( NOT ENABLE_RUNTIME_SUBNORMAL )
375-
list( APPEND opencl_lib_files opencl/lib/generic/subnormal_use_default.ll )
376-
endif()
377359
endif()
378360
endif()
379361

libclc/clc/include/clc/math/clc_subnormal_config.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
#include <clc/clcfunc.h>
1212

13-
_CLC_DECL bool __clc_subnormals_disabled();
1413
_CLC_DECL bool __clc_fp16_subnormals_supported();
1514
_CLC_DECL bool __clc_fp32_subnormals_supported();
1615
_CLC_DECL bool __clc_fp64_subnormals_supported();

libclc/clc/include/clc/math/math.h

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
#include <clc/clc_as_type.h>
1313
#include <clc/clcfunc.h>
14-
#include <clc/math/clc_subnormal_config.h>
1514

1615
#define SNAN 0x001
1716
#define QNAN 0x002
@@ -66,13 +65,11 @@ bool __attribute__((noinline)) __clc_runtime_has_hw_fma32(void);
6665
#define LOG_MAGIC_NUM_SP32 (1 + NUMEXPBITS_SP32 - EXPBIAS_SP32)
6766

6867
_CLC_OVERLOAD _CLC_INLINE float __clc_flush_denormal_if_not_supported(float x) {
69-
int ix = __clc_as_int(x);
70-
if (!__clc_fp32_subnormals_supported() && ((ix & EXPBITS_SP32) == 0) &&
71-
((ix & MANTBITS_SP32) != 0)) {
72-
ix &= SIGNBIT_SP32;
73-
x = __clc_as_float(ix);
74-
}
75-
return x;
68+
// Avoid calling __clc_fp32_subnormals_supported here: it uses
69+
// llvm.canonicalize, which quiets sNaN.
70+
return __builtin_fabsf(x) < 0x1p-149f
71+
? __builtin_elementwise_copysign(0.0f, x)
72+
: x;
7673
}
7774

7875
#ifdef cl_khr_fp64

libclc/clc/lib/generic/SOURCES

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ math/clc_sincos_helpers.cl
137137
math/clc_sinh.cl
138138
math/clc_sinpi.cl
139139
math/clc_sqrt.cl
140+
math/clc_subnormal_config.cl
140141
math/clc_sw_fma.cl
141142
math/clc_tables.cl
142143
math/clc_tan.cl

libclc/clc/lib/generic/math/clc_exp10.cl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
#include <clc/math/clc_fma.h>
1212
#include <clc/math/clc_ldexp.h>
1313
#include <clc/math/clc_mad.h>
14-
#include <clc/math/clc_subnormal_config.h>
1514
#include <clc/math/math.h>
1615
#include <clc/math/tables.h>
1716
#include <clc/relational/clc_isnan.h>

libclc/clc/lib/generic/math/clc_hypot.cl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
#include <clc/math/clc_fma.h>
1313
#include <clc/math/clc_mad.h>
1414
#include <clc/math/clc_sqrt.h>
15-
#include <clc/math/clc_subnormal_config.h>
1615
#include <clc/math/math.h>
1716
#include <clc/relational/clc_isnan.h>
1817
#include <clc/shared/clc_clamp.h>

libclc/clc/lib/generic/math/clc_pow.cl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
#include <clc/math/clc_fma.h>
1313
#include <clc/math/clc_ldexp.h>
1414
#include <clc/math/clc_mad.h>
15-
#include <clc/math/clc_subnormal_config.h>
1615
#include <clc/math/math.h>
1716
#include <clc/math/tables.h>
1817
#include <clc/relational/clc_select.h>

libclc/clc/lib/generic/math/clc_pown.cl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
#include <clc/math/clc_fma.h>
1313
#include <clc/math/clc_ldexp.h>
1414
#include <clc/math/clc_mad.h>
15-
#include <clc/math/clc_subnormal_config.h>
1615
#include <clc/math/math.h>
1716
#include <clc/math/tables.h>
1817
#include <clc/relational/clc_select.h>

libclc/clc/lib/generic/math/clc_powr.cl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
#include <clc/math/clc_fma.h>
1313
#include <clc/math/clc_ldexp.h>
1414
#include <clc/math/clc_mad.h>
15-
#include <clc/math/clc_subnormal_config.h>
1615
#include <clc/math/math.h>
1716
#include <clc/math/tables.h>
1817
#include <clc/relational/clc_select.h>

libclc/clc/lib/generic/math/clc_remquo.cl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
#include <clc/math/clc_floor.h>
1313
#include <clc/math/clc_fma.h>
1414
#include <clc/math/clc_ldexp.h>
15-
#include <clc/math/clc_subnormal_config.h>
1615
#include <clc/math/clc_trunc.h>
1716
#include <clc/math/math.h>
1817
#include <clc/shared/clc_max.h>

0 commit comments

Comments
 (0)