Skip to content

Commit 9705500

Browse files
authored
[libclc] Move nextafter to the CLC library (#124097)
There were two implementations of this - one that implemented nextafter in software, and another that called a clang builtin. No in-tree targets called the builtin, so all targets build the software version. The builtin version has been removed, and the software version has been renamed to be the "default". This commit also optimizes nextafter, to avoid scalarization as much as possible. Note however that the (CLC) relational builtins still scalarize; those will be optimized in a separate commit. Since nextafter is used by some convert_type builtins, the diff to IR codegen is not limited to the builtin itself.
1 parent 6f68481 commit 9705500

File tree

24 files changed

+130
-108
lines changed

24 files changed

+130
-108
lines changed

libclc/amdgpu/lib/SOURCES

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,4 @@ math/half_log2.cl
1010
math/half_recip.cl
1111
math/half_rsqrt.cl
1212
math/half_sqrt.cl
13-
math/nextafter.cl
1413
math/sqrt.cl

libclc/amdgpu/lib/math/nextafter.cl

Lines changed: 0 additions & 15 deletions
This file was deleted.

libclc/clc/include/clc/clcmacro.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,34 @@
159159
_CLC_BINARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, RET_TYPE, FUNCTION, ARG1_TYPE, \
160160
ARG2_TYPE)
161161

162+
// FIXME: Make _CLC_DEFINE_BINARY_BUILTIN avoid scalarization by default, and
163+
// introduce an explicit scalarizing version.
164+
#define _CLC_DEFINE_BINARY_BUILTIN_NO_SCALARIZE(RET_TYPE, FUNCTION, BUILTIN, \
165+
ARG1_TYPE, ARG2_TYPE) \
166+
_CLC_DEF _CLC_OVERLOAD RET_TYPE FUNCTION(ARG1_TYPE x, ARG2_TYPE y) { \
167+
return BUILTIN(x, y); \
168+
} \
169+
_CLC_DEF _CLC_OVERLOAD RET_TYPE##2 FUNCTION(ARG1_TYPE##2 x, \
170+
ARG2_TYPE##2 y) { \
171+
return BUILTIN(x, y); \
172+
} \
173+
_CLC_DEF _CLC_OVERLOAD RET_TYPE##3 FUNCTION(ARG1_TYPE##3 x, \
174+
ARG2_TYPE##3 y) { \
175+
return BUILTIN(x, y); \
176+
} \
177+
_CLC_DEF _CLC_OVERLOAD RET_TYPE##4 FUNCTION(ARG1_TYPE##4 x, \
178+
ARG2_TYPE##4 y) { \
179+
return BUILTIN(x, y); \
180+
} \
181+
_CLC_DEF _CLC_OVERLOAD RET_TYPE##8 FUNCTION(ARG1_TYPE##8 x, \
182+
ARG2_TYPE##8 y) { \
183+
return BUILTIN(x, y); \
184+
} \
185+
_CLC_DEF _CLC_OVERLOAD RET_TYPE##16 FUNCTION(ARG1_TYPE##16 x, \
186+
ARG2_TYPE##16 y) { \
187+
return BUILTIN(x, y); \
188+
}
189+
162190
#define _CLC_DEFINE_BINARY_BUILTIN_WITH_SCALAR_SECOND_ARG( \
163191
RET_TYPE, FUNCTION, BUILTIN, ARG1_TYPE, ARG2_TYPE) \
164192
_CLC_DEFINE_BINARY_BUILTIN(RET_TYPE, FUNCTION, BUILTIN, ARG1_TYPE, \
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
_CLC_OVERLOAD _CLC_DECL __CLC_GENTYPE __CLC_FUNCTION(__CLC_GENTYPE a,
2+
__CLC_GENTYPE b);
3+
_CLC_OVERLOAD _CLC_DECL __CLC_GENTYPE __CLC_FUNCTION(__CLC_GENTYPE a,
4+
__CLC_SCALAR_GENTYPE b);
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#ifndef __CLC_MATH_CLC_NEXTAFTER_H__
2+
#define __CLC_MATH_CLC_NEXTAFTER_H__
3+
4+
#define __CLC_BODY <clc/shared/binary_decl.inc>
5+
#define __CLC_FUNCTION __clc_nextafter
6+
7+
#include <clc/math/gentype.inc>
8+
9+
#undef __CLC_BODY
10+
#undef __CLC_FUNCTION
11+
12+
#endif // __CLC_MATH_CLC_NEXTAFTER_H__

libclc/clc/include/clc/relational/clc_isnan.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
#ifndef __CLC_RELATIONAL_CLC_ISNAN_H__
22
#define __CLC_RELATIONAL_CLC_ISNAN_H__
33

4-
#if defined(CLC_CLSPV) || defined(CLC_SPIRV)
5-
// clspv and spir-v targets provide their own OpenCL-compatible isnan
6-
#define __clc_isnan isnan
7-
#else
8-
94
#include <clc/clcfunc.h>
105
#include <clc/clctypes.h>
116

@@ -37,6 +32,4 @@ _CLC_VECTOR_ISNAN_DECL(short, half)
3732
#undef _CLC_ISNAN_DECL
3833
#undef _CLC_VECTOR_ISNAN_DECL
3934

40-
#endif
41-
4235
#endif // __CLC_RELATIONAL_CLC_ISNAN_H__
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
_CLC_OVERLOAD _CLC_DECL __CLC_GENTYPE __CLC_FUNCTION(__CLC_GENTYPE x,
2+
__CLC_GENTYPE y);

libclc/clc/lib/clspv/SOURCES

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
../generic/math/clc_fabs.cl
33
../generic/math/clc_floor.cl
44
../generic/math/clc_mad.cl
5+
../generic/math/clc_nextafter.cl
56
../generic/math/clc_rint.cl
67
../generic/math/clc_trunc.cl
78
../generic/relational/clc_select.cl

libclc/clc/lib/generic/SOURCES

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ math/clc_ceil.cl
88
math/clc_fabs.cl
99
math/clc_floor.cl
1010
math/clc_mad.cl
11+
math/clc_nextafter.cl
1112
math/clc_rint.cl
1213
math/clc_trunc.cl
1314
relational/clc_all.cl
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#include <clc/clcmacro.h>
2+
#include <clc/internal/clc.h>
3+
#include <clc/relational/clc_isnan.h>
4+
5+
// This file provides OpenCL C implementations of __clc_nextafter for
6+
// targets that don't support the clang builtin.
7+
8+
#define CLC_AS_TYPE(x) __clc_as_##x
9+
10+
#define NEXTAFTER(FLOAT_TYPE, UINT_TYPE, INT_TYPE, INT_TYPE_SCALAR) \
11+
_CLC_OVERLOAD _CLC_DEF FLOAT_TYPE __clc_nextafter(FLOAT_TYPE x, \
12+
FLOAT_TYPE y) { \
13+
const UINT_TYPE sign_bit = (UINT_TYPE)1 \
14+
<< (sizeof(INT_TYPE_SCALAR) * 8 - 1); \
15+
const UINT_TYPE sign_bit_mask = sign_bit - (UINT_TYPE)1; \
16+
INT_TYPE ix = CLC_AS_TYPE(INT_TYPE)(x); \
17+
UINT_TYPE ax = CLC_AS_TYPE(UINT_TYPE)(ix) & sign_bit_mask; \
18+
INT_TYPE mx = CLC_AS_TYPE(INT_TYPE)(sign_bit) - ix; \
19+
mx = CLC_AS_TYPE(INT_TYPE)(ix) < (INT_TYPE)0 ? mx : ix; \
20+
INT_TYPE iy = CLC_AS_TYPE(INT_TYPE)(y); \
21+
UINT_TYPE ay = CLC_AS_TYPE(UINT_TYPE)(iy) & sign_bit_mask; \
22+
INT_TYPE my = CLC_AS_TYPE(INT_TYPE)(sign_bit) - iy; \
23+
my = iy < (INT_TYPE)0 ? my : iy; \
24+
INT_TYPE t = mx + (mx < my ? (INT_TYPE)1 : (INT_TYPE)-1); \
25+
INT_TYPE r = CLC_AS_TYPE(INT_TYPE)(sign_bit) - t; \
26+
r = t < (INT_TYPE)0 ? r : t; \
27+
r = __clc_isnan(x) ? ix : r; \
28+
r = __clc_isnan(y) ? CLC_AS_TYPE(INT_TYPE)(iy) : r; \
29+
r = ((ax | ay) == (UINT_TYPE)0 || ix == iy) ? iy : r; \
30+
return CLC_AS_TYPE(FLOAT_TYPE)(r); \
31+
}
32+
33+
NEXTAFTER(float, uint, int, int)
34+
NEXTAFTER(float2, uint2, int2, int)
35+
NEXTAFTER(float3, uint3, int3, int)
36+
NEXTAFTER(float4, uint4, int4, int)
37+
NEXTAFTER(float8, uint8, int8, int)
38+
NEXTAFTER(float16, uint16, int16, int)
39+
40+
#ifdef cl_khr_fp64
41+
#pragma OPENCL EXTENSION cl_khr_fp64 : enable
42+
43+
NEXTAFTER(double, ulong, long, long)
44+
NEXTAFTER(double2, ulong2, long2, long)
45+
NEXTAFTER(double3, ulong3, long3, long)
46+
NEXTAFTER(double4, ulong4, long4, long)
47+
NEXTAFTER(double8, ulong8, long8, long)
48+
NEXTAFTER(double16, ulong16, long16, long)
49+
50+
#endif
51+
52+
#ifdef cl_khr_fp16
53+
#pragma OPENCL EXTENSION cl_khr_fp16 : enable
54+
55+
NEXTAFTER(half, ushort, short, short)
56+
NEXTAFTER(half2, ushort2, short2, short)
57+
NEXTAFTER(half3, ushort3, short3, short)
58+
NEXTAFTER(half4, ushort4, short4, short)
59+
NEXTAFTER(half8, ushort8, short8, short)
60+
NEXTAFTER(half16, ushort16, short16, short)
61+
62+
#endif

libclc/clc/lib/spirv/SOURCES

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
../generic/math/clc_fabs.cl
77
../generic/math/clc_floor.cl
88
../generic/math/clc_mad.cl
9+
../generic/math/clc_nextafter.cl
910
../generic/math/clc_rint.cl
1011
../generic/math/clc_trunc.cl
1112
../generic/relational/clc_select.cl

libclc/clc/lib/spirv64/SOURCES

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
../generic/math/clc_fabs.cl
77
../generic/math/clc_floor.cl
88
../generic/math/clc_mad.cl
9+
../generic/math/clc_nextafter.cl
910
../generic/math/clc_rint.cl
1011
../generic/math/clc_trunc.cl
1112
../generic/relational/clc_select.cl

libclc/clspv/lib/SOURCES

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
math/fma.cl
2-
math/nextafter.cl
32
shared/vstore_half.cl
43
subnormal_config.cl
54
../../generic/lib/geometric/distance.cl
@@ -21,7 +20,6 @@ subnormal_config.cl
2120
../../generic/lib/math/clc_fmod.cl
2221
../../generic/lib/math/clc_hypot.cl
2322
../../generic/lib/math/clc_ldexp.cl
24-
../../generic/lib/math/clc_nextafter.cl
2523
../../generic/lib/math/clc_pow.cl
2624
../../generic/lib/math/clc_pown.cl
2725
../../generic/lib/math/clc_powr.cl
@@ -71,6 +69,7 @@ subnormal_config.cl
7169
../../generic/lib/math/minmag.cl
7270
../../generic/lib/math/modf.cl
7371
../../generic/lib/math/nan.cl
72+
../../generic/lib/math/nextafter.cl
7473
../../generic/lib/math/pow.cl
7574
../../generic/lib/math/pown.cl
7675
../../generic/lib/math/powr.cl

libclc/clspv/lib/math/nextafter.cl

Lines changed: 0 additions & 5 deletions
This file was deleted.

libclc/clspv/lib/math/nextafter.inc

Lines changed: 0 additions & 3 deletions
This file was deleted.

libclc/generic/include/clc/math/binary_decl.inc

Lines changed: 0 additions & 2 deletions
This file was deleted.

libclc/generic/include/clc/math/fmax.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#define __CLC_BODY <clc/math/binary_decl.inc>
1+
#define __CLC_BODY <clc/math/binary_decl_with_scalar_second_arg.inc>
22
#define __CLC_FUNCTION fmax
33

44
#include <clc/math/gentype.inc>

libclc/generic/include/clc/math/fmin.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#define __CLC_BODY <clc/math/binary_decl.inc>
1+
#define __CLC_BODY <clc/math/binary_decl_with_scalar_second_arg.inc>
22
#define __CLC_FUNCTION fmin
33

44
#include <clc/math/gentype.inc>

libclc/generic/include/math/clc_nextafter.h

Lines changed: 0 additions & 7 deletions
This file was deleted.

libclc/generic/lib/SOURCES

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,6 @@ math/native_sin.cl
159159
math/native_sqrt.cl
160160
math/native_tan.cl
161161
math/tables.cl
162-
math/clc_nextafter.cl
163162
math/nextafter.cl
164163
math/clc_pow.cl
165164
math/pow.cl

libclc/generic/lib/math/clc_nextafter.cl

Lines changed: 0 additions & 49 deletions
This file was deleted.

libclc/generic/lib/math/nextafter.cl

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,24 @@
11
#include <clc/clc.h>
2-
#include "../clcmacro.h"
2+
#include <clc/clcmacro.h>
3+
#include <clc/math/clc_nextafter.h>
34

4-
_CLC_DEFINE_BINARY_BUILTIN(float, nextafter, __builtin_nextafterf, float, float)
5+
_CLC_DEFINE_BINARY_BUILTIN_NO_SCALARIZE(float, nextafter, __clc_nextafter,
6+
float, float)
57

68
#ifdef cl_khr_fp64
79

810
#pragma OPENCL EXTENSION cl_khr_fp64 : enable
911

10-
_CLC_DEFINE_BINARY_BUILTIN(double, nextafter, __builtin_nextafter, double, double)
12+
_CLC_DEFINE_BINARY_BUILTIN_NO_SCALARIZE(double, nextafter, __clc_nextafter,
13+
double, double)
14+
15+
#endif
16+
17+
#ifdef cl_khr_fp16
18+
19+
#pragma OPENCL EXTENSION cl_khr_fp16 : enable
20+
21+
_CLC_DEFINE_BINARY_BUILTIN_NO_SCALARIZE(half, nextafter, __clc_nextafter, half,
22+
half)
1123

1224
#endif

libclc/ptx/lib/SOURCES

Lines changed: 0 additions & 1 deletion
This file was deleted.

libclc/ptx/lib/math/nextafter.cl

Lines changed: 0 additions & 10 deletions
This file was deleted.

0 commit comments

Comments
 (0)