Skip to content

Commit dc4bcfc

Browse files
committed
[libclc] Move __clc_ldexp to CLC library
This function was already conceptually in the CLC namespace - this just formally moves it over. Note however that this commit marks a change in how libclc functions may be overridden by targets. Until now we have been using a purely build-system-based approach where targets could register identically-named files which took responsibility for the implementation of the builtin in its entirety. This system wasn't well equipped to deal with AMD's overriding of __clc_ldexp for only a subset of types, and furthermore conditionally on a pre-defined macro. One option for handling this would be to require AMD to duplicate code for the versions of __clc_ldexp it's *not* interested in overriding. We could also make it easier for targets to re-define CLC functions through macros or .inc files. Both of these have obvious downsides. We could also keep AMD's overriding in the OpenCL layer and bypass CLC altogether, but this has limited use. We could use weak linkage on the "base" implementations of CLC functions, and allow targets to opt-in to providing their own implementations on a much finer granulaity. This commit supports this as a proof of concept; we could expand it to all CLC builtins if accepted. Note that the existing filename-based "claiming" approach is still in effect, so targets have to name their overrides differently to have both files compiled. This could also be refined.
1 parent d4144ca commit dc4bcfc

File tree

17 files changed

+56
-71
lines changed

17 files changed

+56
-71
lines changed

libclc/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS
3232
spirv64/lib/SOURCES;
3333
# CLC internal libraries
3434
clc/lib/generic/SOURCES;
35+
clc/lib/amdgcn/SOURCES;
3536
clc/lib/clspv/SOURCES;
3637
clc/lib/clspv64/SOURCES;
3738
clc/lib/spirv/SOURCES;

libclc/amdgcn/lib/SOURCES

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
cl_khr_int64_extended_atomics/minmax_helpers.ll
22
math/fmax.cl
33
math/fmin.cl
4-
math/ldexp.cl
54
mem_fence/fence.cl
65
synchronization/barrier.cl
76
workitem/get_global_offset.cl
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#ifndef __CLC_MATH_CLC_LDEXP_H__
2+
#define __CLC_MATH_CLC_LDEXP_H__
3+
4+
#define __CLC_BODY <clc/math/clc_ldexp.inc>
5+
#include <clc/math/gentype.inc>
6+
7+
#endif // __CLC_MATH_CLC_LDEXP_H__
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
_CLC_DECL _CLC_OVERLOAD __CLC_GENTYPE __clc_ldexp(__CLC_GENTYPE, __CLC_INTN);

libclc/clc/include/clc/relational/clc_isinf.h

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

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

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

40-
#endif
41-
4235
#endif // __CLC_RELATIONAL_CLC_ISINF_H__

libclc/clc/lib/amdgcn/SOURCES

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
math/clc_ldexp_override.cl

libclc/amdgcn/lib/math/ldexp.cl renamed to libclc/clc/lib/amdgcn/math/clc_ldexp_override.cl

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,27 +20,18 @@
2020
* THE SOFTWARE.
2121
*/
2222

23-
#include <clc/clc.h>
2423
#include <clc/clcmacro.h>
24+
#include <clc/internal/clc.h>
25+
#include <clc/math/clc_ldexp.h>
2526

2627
#ifdef __HAS_LDEXPF__
27-
#define BUILTINF __builtin_amdgcn_ldexpf
28-
#else
29-
#include "math/clc_ldexp.h"
30-
#define BUILTINF __clc_ldexp
31-
#endif
32-
3328
// This defines all the ldexp(floatN, intN) variants.
34-
_CLC_DEFINE_BINARY_BUILTIN(float, ldexp, BUILTINF, float, int);
29+
_CLC_DEFINE_BINARY_BUILTIN(float, __clc_ldexp, __builtin_amdgcn_ldexpf, float, int);
30+
#endif
3531

3632
#ifdef cl_khr_fp64
37-
#pragma OPENCL EXTENSION cl_khr_fp64 : enable
38-
// This defines all the ldexp(doubleN, intN) variants.
39-
_CLC_DEFINE_BINARY_BUILTIN(double, ldexp, __builtin_amdgcn_ldexp, double, int);
33+
#pragma OPENCL EXTENSION cl_khr_fp64 : enable
34+
// This defines all the ldexp(doubleN, intN) variants.
35+
_CLC_DEFINE_BINARY_BUILTIN(double, __clc_ldexp, __builtin_amdgcn_ldexp, double,
36+
int);
4037
#endif
41-
42-
// This defines all the ldexp(GENTYPE, int);
43-
#define __CLC_BODY <../../../generic/lib/math/ldexp.inc>
44-
#include <clc/math/gentype.inc>
45-
46-
#undef BUILTINF

libclc/clc/lib/clspv/SOURCES

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@
1414
../generic/math/clc_copysign.cl
1515
../generic/math/clc_fabs.cl
1616
../generic/math/clc_floor.cl
17+
../generic/math/clc_ldexp.cl
1718
../generic/math/clc_mad.cl
1819
../generic/math/clc_nextafter.cl
1920
../generic/math/clc_rint.cl
2021
../generic/math/clc_trunc.cl
22+
../generic/relational/clc_isinf.cl
2123
../generic/relational/clc_isnan.cl
2224
../generic/relational/clc_select.cl
2325
../generic/shared/clc_clamp.cl

libclc/clc/lib/generic/SOURCES

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ math/clc_ceil.cl
2020
math/clc_copysign.cl
2121
math/clc_fabs.cl
2222
math/clc_floor.cl
23+
math/clc_ldexp.cl
2324
math/clc_mad.cl
2425
math/clc_nextafter.cl
2526
math/clc_rint.cl

libclc/generic/lib/math/clc_ldexp.cl renamed to libclc/clc/lib/generic/math/clc_ldexp.cl

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,22 @@
2020
* THE SOFTWARE.
2121
*/
2222

23-
#include <clc/clc.h>
2423
#include <clc/clcmacro.h>
2524
#include <clc/integer/clc_add_sat.h>
25+
#include <clc/internal/clc.h>
2626
#include <clc/math/clc_subnormal_config.h>
2727
#include <clc/math/math.h>
2828
#include <clc/relational/clc_isinf.h>
2929
#include <clc/relational/clc_isnan.h>
3030
#include <clc/shared/clc_clamp.h>
3131

32-
_CLC_DEF _CLC_OVERLOAD float __clc_ldexp(float x, int n) {
32+
#define _CLC_DEF_ldexp _CLC_DEF __attribute__((weak))
3333

34-
if (!__clc_fp32_subnormals_supported()) {
34+
_CLC_DEF_ldexp _CLC_OVERLOAD float __clc_ldexp(float x, int n) {
3535

36+
if (!__clc_fp32_subnormals_supported()) {
3637
// This treats subnormals as zeros
37-
int i = as_int(x);
38+
int i = __clc_as_int(x);
3839
int e = (i >> 23) & 0xff;
3940
int m = i & 0x007fffff;
4041
int s = i & 0x80000000;
@@ -45,7 +46,7 @@ _CLC_DEF _CLC_OVERLOAD float __clc_ldexp(float x, int n) {
4546
mr = c ? m : mr;
4647
int er = c ? e : v;
4748
er = e ? er : e;
48-
return as_float(s | (er << 23) | mr);
49+
return __clc_as_float(s | (er << 23) | mr);
4950
}
5051

5152
/* supports denormal values */
@@ -54,7 +55,7 @@ _CLC_DEF _CLC_OVERLOAD float __clc_ldexp(float x, int n) {
5455
uint val_ui;
5556
uint sign;
5657
int exponent;
57-
val_ui = as_uint(x);
58+
val_ui = __clc_as_uint(x);
5859
sign = val_ui & 0x80000000;
5960
val_ui = val_ui & 0x7fffffff; /* remove the sign bit */
6061
int val_x = val_ui;
@@ -64,7 +65,9 @@ _CLC_DEF _CLC_OVERLOAD float __clc_ldexp(float x, int n) {
6465

6566
/* denormal support */
6667
int fbh =
67-
127 - (as_uint((float)(as_float(val_ui | 0x3f800000) - 1.0f)) >> 23);
68+
127 -
69+
(__clc_as_uint((float)(__clc_as_float(val_ui | 0x3f800000) - 1.0f)) >>
70+
23);
6871
int dexponent = 25 - fbh;
6972
uint dval_ui = (((val_ui << fbh) & 0x007fffff) | (dexponent << 23));
7073
int ex = dexponent + n - multiplier;
@@ -77,7 +80,7 @@ _CLC_DEF _CLC_OVERLOAD float __clc_ldexp(float x, int n) {
7780
dval_ui = dexponent > 254 ? 0x7f800000 : dval_ui; /*overflow*/
7881
dval_ui = dexponent < -multiplier ? 0 : dval_ui; /*underflow*/
7982
dval_ui = dval_ui | sign;
80-
val_f = as_float(dval_ui);
83+
val_f = __clc_as_float(dval_ui);
8184

8285
exponent += n;
8386

@@ -91,22 +94,24 @@ _CLC_DEF _CLC_OVERLOAD float __clc_ldexp(float x, int n) {
9194
val_ui = val_ui | sign;
9295

9396
val_ui = dexp == 0 ? dval_ui : val_ui;
94-
val_f = as_float(val_ui);
97+
val_f = __clc_as_float(val_ui);
9598

9699
val_f = __clc_isnan(x) | __clc_isinf(x) | val_x == 0 ? x : val_f;
97100
return val_f;
98101
}
99102

103+
_CLC_BINARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF_ldexp, float, __clc_ldexp, float, int);
104+
100105
#ifdef cl_khr_fp64
101106

102107
#pragma OPENCL EXTENSION cl_khr_fp64 : enable
103108

104-
_CLC_DEF _CLC_OVERLOAD double __clc_ldexp(double x, int n) {
105-
long l = as_ulong(x);
109+
_CLC_DEF_ldexp _CLC_OVERLOAD double __clc_ldexp(double x, int n) {
110+
long l = __clc_as_ulong(x);
106111
int e = (l >> 52) & 0x7ff;
107112
long s = l & 0x8000000000000000;
108113

109-
ulong ux = as_ulong(x * 0x1.0p+53);
114+
ulong ux = __clc_as_ulong(x * 0x1.0p+53);
110115
int de = ((int)(ux >> 52) & 0x7ff) - 53;
111116
int c = e == 0;
112117
e = c ? de : e;
@@ -118,28 +123,30 @@ _CLC_DEF _CLC_OVERLOAD double __clc_ldexp(double x, int n) {
118123

119124
ux &= ~EXPBITS_DP64;
120125

121-
double mr = as_double(ux | ((ulong)(v + 53) << 52));
126+
double mr = __clc_as_double(ux | ((ulong)(v + 53) << 52));
122127
mr = mr * 0x1.0p-53;
123128

124-
mr = v > 0 ? as_double(ux | ((ulong)v << 52)) : mr;
129+
mr = v > 0 ? __clc_as_double(ux | ((ulong)v << 52)) : mr;
125130

126-
mr = v == 0x7ff ? as_double(s | PINFBITPATT_DP64) : mr;
127-
mr = v < -53 ? as_double(s) : mr;
131+
mr = v == 0x7ff ? __clc_as_double(s | PINFBITPATT_DP64) : mr;
132+
mr = v < -53 ? __clc_as_double(s) : mr;
128133

129134
mr = ((n == 0) | __clc_isinf(x) | (x == 0)) ? x : mr;
130135
return mr;
131136
}
132137

138+
_CLC_BINARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF_ldexp, double, __clc_ldexp, double, int);
139+
133140
#endif
134141

135142
#ifdef cl_khr_fp16
136143

137144
#pragma OPENCL EXTENSION cl_khr_fp16 : enable
138145

139-
_CLC_OVERLOAD _CLC_DEF half __clc_ldexp(half x, int n) {
146+
_CLC_OVERLOAD _CLC_DEF_ldexp half __clc_ldexp(half x, int n) {
140147
return (half)__clc_ldexp((float)x, n);
141148
}
142149

143-
_CLC_BINARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, half, __clc_ldexp, half, int);
150+
_CLC_BINARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF_ldexp, half, __clc_ldexp, half, int);
144151

145152
#endif

libclc/clc/lib/spirv/SOURCES

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,12 @@
1818
../generic/math/clc_copysign.cl
1919
../generic/math/clc_fabs.cl
2020
../generic/math/clc_floor.cl
21+
../generic/math/clc_ldexp.cl
2122
../generic/math/clc_mad.cl
2223
../generic/math/clc_nextafter.cl
2324
../generic/math/clc_rint.cl
2425
../generic/math/clc_trunc.cl
26+
../generic/relational/clc_isinf.cl
27+
../generic/relational/clc_isnan.cl
2528
../generic/relational/clc_select.cl
2629
../generic/shared/clc_clamp.cl

libclc/clspv/lib/SOURCES

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ subnormal_config.cl
1919
../../generic/lib/math/clc_exp10.cl
2020
../../generic/lib/math/clc_fmod.cl
2121
../../generic/lib/math/clc_hypot.cl
22-
../../generic/lib/math/clc_ldexp.cl
2322
../../generic/lib/math/clc_pow.cl
2423
../../generic/lib/math/clc_pown.cl
2524
../../generic/lib/math/clc_powr.cl

libclc/generic/include/math/clc_ldexp.h

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

libclc/generic/lib/SOURCES

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,6 @@ math/half_tan.cl
131131
math/clc_hypot.cl
132132
math/hypot.cl
133133
math/ilogb.cl
134-
math/clc_ldexp.cl
135134
math/ldexp.cl
136135
math/lgamma.cl
137136
math/lgamma_r.cl

libclc/generic/lib/math/ldexp.cl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,26 @@
2020
* THE SOFTWARE.
2121
*/
2222

23-
#include "math/clc_ldexp.h"
2423
#include <clc/clc.h>
2524
#include <clc/clcmacro.h>
26-
#include <clc/math/clc_subnormal_config.h>
27-
#include <clc/math/math.h>
25+
#include <clc/math/clc_ldexp.h>
2826

29-
_CLC_DEFINE_BINARY_BUILTIN(float, ldexp, __clc_ldexp, float, int)
27+
_CLC_DEFINE_BINARY_BUILTIN_NO_SCALARIZE(float, ldexp, __clc_ldexp, float, int)
3028

3129
#ifdef cl_khr_fp64
3230

3331
#pragma OPENCL EXTENSION cl_khr_fp64 : enable
3432

35-
_CLC_DEFINE_BINARY_BUILTIN(double, ldexp, __clc_ldexp, double, int)
33+
_CLC_DEFINE_BINARY_BUILTIN_NO_SCALARIZE(double, ldexp, __clc_ldexp, double, int)
34+
3635
#endif
3736

3837
#ifdef cl_khr_fp16
3938

4039
#pragma OPENCL EXTENSION cl_khr_fp16 : enable
4140

42-
_CLC_DEFINE_BINARY_BUILTIN(half, ldexp, __clc_ldexp, half, int)
41+
_CLC_DEFINE_BINARY_BUILTIN_NO_SCALARIZE(half, ldexp, __clc_ldexp, half, int)
42+
4343
#endif
4444

4545
// This defines all the ldexp(GENTYPE, int) variants

libclc/generic/lib/math/ldexp.inc

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,10 @@
2020
* THE SOFTWARE.
2121
*/
2222

23-
// TODO: Enable half precision when ldexp is implemented.
24-
#if __CLC_FPSIZE > 16
25-
2623
#ifndef __CLC_SCALAR
2724

2825
_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE ldexp(__CLC_GENTYPE x, int n) {
29-
return ldexp(x, (__CLC_INTN)n);
26+
return __clc_ldexp(x, (__CLC_INTN)n);
3027
}
3128

3229
#endif
33-
34-
#endif

libclc/spirv/lib/SOURCES

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ math/fma.cl
5252
../../generic/lib/math/clc_hypot.cl
5353
../../generic/lib/math/hypot.cl
5454
../../generic/lib/math/ilogb.cl
55-
../../generic/lib/math/clc_ldexp.cl
5655
../../generic/lib/math/ldexp.cl
5756
../../generic/lib/math/lgamma.cl
5857
../../generic/lib/math/lgamma_r.cl

0 commit comments

Comments
 (0)