Skip to content

Commit b6068f0

Browse files
committed
[SYCL][libclc] Add generic addrspace overloads of math builtins
The generic implementations of the math builtins which take pointer arguments were using unqualified address spaces. This could either resolve to the generic address space or the private address space, depending on whether the target supports the generic address space or not. The newer unified OpenCL C specification is clearer in mandating that all targets must provide overloads on the explicitly qualified 'private' address space, as well as optionally defining ones on the (unqualified) generic address space. This meant that most of these math builtins were lacking one overload: either the private or generic one, depending on which target was compiling the builtins. One notable exception here is NVIDIA, which maps the private and generic address spaces to the same target address space. Thus declaring builtins overloaded on these two address spaces results in a mangling clash, which we can't have. Therefore we now say that NVIDIA targets don't support the generic address space for the purposes of these builtins. In reality, the builtins with the private address space are functionally equivalent to the generic ones, so users won't notice. For the sake of code clarity, although the 'generic' keyword is technically reserved, we know that clang defines it to be the corresponding unqualified generic address space, so we use that to be explicit. We always compile with clang so it shouldn't be a problem with portability. With this we can also enable a LIT test for HIP, which was previously failing as it couldn't find the generic address space overloads of fract and lgamma_r. There are other builtins that this treatment (may) need applied to, such as the vload and vstore variants. Those will be handled in a subsequent patch.
1 parent feb80ba commit b6068f0

File tree

16 files changed

+103
-20
lines changed

16 files changed

+103
-20
lines changed

libclc/CMakeLists.txt

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,7 @@ foreach( t ${LIBCLC_TARGETS_TO_BUILD} )
400400
endif()
401401
message( " DEVICE: ${d} ( ${${d}_aliases} )" )
402402

403+
set ( supports_generic_addrspace TRUE )
403404
if ( ${ARCH} STREQUAL "spirv" OR ${ARCH} STREQUAL "spirv64" )
404405
if( ${ARCH} STREQUAL "spirv" )
405406
set( t "spir--" )
@@ -416,6 +417,14 @@ foreach( t ${LIBCLC_TARGETS_TO_BUILD} )
416417
elseif( ${ARCH} STREQUAL "nvptx" OR ${ARCH} STREQUAL "nvptx64" )
417418
set( build_flags )
418419
set( opt_flags -O3 "--nvvm-reflect-enable=false" )
420+
# Note: when declaring builtins, we don't consider NVIDIA as supporting
421+
# the generic address space. This is because it maps to the same target
422+
# address space as the private address space, resulting in a mangling
423+
# clash.
424+
# Since we can't declare builtins overloaded on both address spaces
425+
# simultaneously, we choose declare the builtins using the private space,
426+
# which will also work for the generic address space.
427+
set( supports_generic_addrspace FALSE )
419428
elseif( ${ARCH} STREQUAL "clspv64" )
420429
set( t "spir64--" )
421430
set( build_flags "-Wno-unknown-assumption")
@@ -437,8 +446,10 @@ foreach( t ${LIBCLC_TARGETS_TO_BUILD} )
437446
"+cl_khr_fp16,"
438447
"+__opencl_c_3d_image_writes,"
439448
"+__opencl_c_images,"
440-
"+cl_khr_3d_image_writes,"
441-
"+__opencl_c_generic_address_space")
449+
"+cl_khr_3d_image_writes")
450+
if(supports_generic_addrspace)
451+
string( APPEND CL_3_0_EXTENSIONS ",+__opencl_c_generic_address_space" )
452+
endif()
442453
list( APPEND flags ${CL_3_0_EXTENSIONS})
443454

444455
# Add platform specific flags

libclc/generic/lib/math/fract.inc

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,19 @@
3131
#define ZERO 0.0h
3232
#endif
3333

34-
_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE fract(__CLC_GENTYPE x, private __CLC_GENTYPE *iptr) {
35-
return __spirv_ocl_fract(x, iptr);
36-
}
37-
38-
3934
#define FRACT_DEF(addrspace) \
4035
_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE fract(__CLC_GENTYPE x, addrspace __CLC_GENTYPE *iptr) { \
4136
return __spirv_ocl_fract(x, iptr); \
4237
}
4338

39+
FRACT_DEF(private);
4440
FRACT_DEF(local);
4541
FRACT_DEF(global);
42+
#if __OPENCL_C_VERSION__ == CL_VERSION_2_0 || \
43+
(__OPENCL_C_VERSION__ >= CL_VERSION_3_0 && \
44+
defined(__opencl_c_generic_address_space))
45+
FRACT_DEF(generic);
46+
#endif
4647

4748
#undef MIN_CONSTANT
4849
#undef ZERO

libclc/generic/lib/math/frexp.cl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,12 @@
1515
#define __CLC_ADDRESS_SPACE local
1616
#include <clc/math/gentype.inc>
1717
#undef __CLC_ADDRESS_SPACE
18+
19+
#if __OPENCL_C_VERSION__ == CL_VERSION_2_0 || \
20+
(__OPENCL_C_VERSION__ >= CL_VERSION_3_0 && \
21+
defined(__opencl_c_generic_address_space))
22+
#define __CLC_BODY <frexp.inc>
23+
#define __CLC_ADDRESS_SPACE generic
24+
#include <clc/math/gentype.inc>
25+
#undef __CLC_ADDRESS_SPACE
26+
#endif

libclc/generic/lib/math/modf.inc

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,25 @@
2828
#define ZERO 0.0h
2929
#endif
3030

31-
_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE modf(__CLC_GENTYPE x, __CLC_GENTYPE *iptr) {
31+
_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE modf(__CLC_GENTYPE x, private __CLC_GENTYPE *iptr) {
3232
*iptr = trunc(x);
3333
return copysign(isinf(x) ? ZERO : x - *iptr, x);
3434
}
3535

3636
#define MODF_DEF(addrspace) \
3737
_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE modf(__CLC_GENTYPE x, addrspace __CLC_GENTYPE *iptr) { \
38-
__CLC_GENTYPE private_iptr; \
38+
private __CLC_GENTYPE private_iptr; \
3939
__CLC_GENTYPE ret = modf(x, &private_iptr); \
4040
*iptr = private_iptr; \
4141
return ret; \
4242
}
4343

4444
MODF_DEF(local);
4545
MODF_DEF(global);
46+
#if __OPENCL_C_VERSION__ == CL_VERSION_2_0 || \
47+
(__OPENCL_C_VERSION__ >= CL_VERSION_3_0 && \
48+
defined(__opencl_c_generic_address_space))
49+
MODF_DEF(generic);
50+
#endif
4651

4752
#undef ZERO

libclc/generic/lib/math/remquo.cl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,12 @@
1515
#define __CLC_ADDRESS_SPACE private
1616
#include <clc/math/gentype.inc>
1717
#undef __CLC_ADDRESS_SPACE
18+
19+
#if __OPENCL_C_VERSION__ == CL_VERSION_2_0 || \
20+
(__OPENCL_C_VERSION__ >= CL_VERSION_3_0 && \
21+
defined(__opencl_c_generic_address_space))
22+
#define __CLC_BODY <remquo.inc>
23+
#define __CLC_ADDRESS_SPACE generic
24+
#include <clc/math/gentype.inc>
25+
#undef __CLC_ADDRESS_SPACE
26+
#endif

libclc/generic/lib/math/remquo.inc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
// TODO: Enable half precision when the sw routine is implemented
22
#if __CLC_FPSIZE > 16
33
_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE remquo(__CLC_GENTYPE x, __CLC_GENTYPE y, __CLC_ADDRESS_SPACE __CLC_INTN *q) {
4-
__CLC_INTN local_q;
5-
__CLC_GENTYPE ret = __clc_remquo(x, y, &local_q);
6-
*q = local_q;
4+
private __CLC_INTN private_q;
5+
__CLC_GENTYPE ret = __clc_remquo(x, y, &private_q);
6+
*q = private_q;
77
return ret;
88
}
99
#endif

libclc/generic/lib/math/sincos.inc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88
__CLC_DECLARE_SINCOS(global, __CLC_GENTYPE)
99
__CLC_DECLARE_SINCOS(local, __CLC_GENTYPE)
1010
__CLC_DECLARE_SINCOS(private, __CLC_GENTYPE)
11+
#if __OPENCL_C_VERSION__ == CL_VERSION_2_0 || \
12+
(__OPENCL_C_VERSION__ >= CL_VERSION_3_0 && \
13+
defined(__opencl_c_generic_address_space))
14+
__CLC_DECLARE_SINCOS(generic, __CLC_GENTYPE)
15+
#endif
1116

1217
#undef __CLC_DECLARE_SINCOS
1318
#endif

libclc/generic/libspirv/math/fract.inc

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,19 @@ __spirv_ocl_fract(__CLC_GENTYPE x, private __CLC_GENTYPE *iptr) {
3838
#define FRACT_DEF(addrspace) \
3939
_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE __spirv_ocl_fract( \
4040
__CLC_GENTYPE x, addrspace __CLC_GENTYPE *iptr) { \
41-
__CLC_GENTYPE private_iptr; \
41+
private __CLC_GENTYPE private_iptr; \
4242
__CLC_GENTYPE ret = __spirv_ocl_fract(x, &private_iptr); \
4343
*iptr = private_iptr; \
4444
return ret; \
4545
}
4646

4747
FRACT_DEF(local);
4848
FRACT_DEF(global);
49+
#if __OPENCL_C_VERSION__ == CL_VERSION_2_0 || \
50+
(__OPENCL_C_VERSION__ >= CL_VERSION_3_0 && \
51+
defined(__opencl_c_generic_address_space))
52+
FRACT_DEF(generic);
53+
#endif
4954

5055
#undef MIN_CONSTANT
5156
#undef ZERO

libclc/generic/libspirv/math/frexp.cl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,12 @@
2323
#define __CLC_ADDRESS_SPACE local
2424
#include <clc/math/gentype.inc>
2525
#undef __CLC_ADDRESS_SPACE
26+
27+
#if __OPENCL_C_VERSION__ == CL_VERSION_2_0 || \
28+
(__OPENCL_C_VERSION__ >= CL_VERSION_3_0 && \
29+
defined(__opencl_c_generic_address_space))
30+
#define __CLC_BODY <frexp.inc>
31+
#define __CLC_ADDRESS_SPACE generic
32+
#include <clc/math/gentype.inc>
33+
#undef __CLC_ADDRESS_SPACE
34+
#endif

libclc/generic/libspirv/math/lgamma_r.cl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -658,3 +658,12 @@ _CLC_V_V_VP_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, half, __spirv_ocl_lgamma_r, half,
658658
#define __CLC_BODY <lgamma_r.inc>
659659
#include <clc/math/gentype.inc>
660660
#undef __CLC_ADDRSPACE
661+
662+
#if __OPENCL_C_VERSION__ == CL_VERSION_2_0 || \
663+
(__OPENCL_C_VERSION__ >= CL_VERSION_3_0 && \
664+
defined(__opencl_c_generic_address_space))
665+
#define __CLC_ADDRSPACE generic
666+
#define __CLC_BODY <lgamma_r.inc>
667+
#include <clc/math/gentype.inc>
668+
#undef __CLC_ADDRSPACE
669+
#endif

libclc/generic/libspirv/math/lgamma_r.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE
1010
__spirv_ocl_lgamma_r(__CLC_GENTYPE x, __CLC_ADDRSPACE __CLC_INTN *iptr) {
11-
__CLC_INTN private_iptr;
11+
private __CLC_INTN private_iptr;
1212
__CLC_GENTYPE ret = __spirv_ocl_lgamma_r(x, &private_iptr);
1313
*iptr = private_iptr;
1414
return ret;

libclc/generic/libspirv/math/modf.inc

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
#endif
2424

2525
_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE __spirv_ocl_modf(__CLC_GENTYPE x,
26-
__CLC_GENTYPE *iptr) {
26+
private __CLC_GENTYPE *iptr) {
2727
*iptr = __spirv_ocl_trunc(x);
2828
return __spirv_ocl_copysign(
2929
__CLC_CONVERT_NATN(__spirv_IsInf(x)) ? ZERO : x - *iptr, x);
@@ -32,7 +32,7 @@ _CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE __spirv_ocl_modf(__CLC_GENTYPE x,
3232
#define MODF_DEF(addrspace) \
3333
_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE __spirv_ocl_modf( \
3434
__CLC_GENTYPE x, addrspace __CLC_GENTYPE *iptr) { \
35-
__CLC_GENTYPE private_iptr; \
35+
private __CLC_GENTYPE private_iptr; \
3636
__CLC_GENTYPE ret = __spirv_ocl_modf(x, &private_iptr); \
3737
*iptr = private_iptr; \
3838
return ret; \
@@ -41,5 +41,11 @@ _CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE __spirv_ocl_modf(__CLC_GENTYPE x,
4141
MODF_DEF(local);
4242
MODF_DEF(global);
4343

44+
#if __OPENCL_C_VERSION__ == CL_VERSION_2_0 || \
45+
(__OPENCL_C_VERSION__ >= CL_VERSION_3_0 && \
46+
defined(__opencl_c_generic_address_space))
47+
MODF_DEF(generic);
48+
#endif
49+
4450
#undef __CLC_CONVERT_NATN
4551
#undef ZERO

libclc/generic/libspirv/math/remquo.cl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,12 @@
2424
#define __CLC_ADDRESS_SPACE private
2525
#include <clc/math/gentype.inc>
2626
#undef __CLC_ADDRESS_SPACE
27+
28+
#if __OPENCL_C_VERSION__ == CL_VERSION_2_0 || \
29+
(__OPENCL_C_VERSION__ >= CL_VERSION_3_0 && \
30+
defined(__opencl_c_generic_address_space))
31+
#define __CLC_BODY <remquo.inc>
32+
#define __CLC_ADDRESS_SPACE generic
33+
#include <clc/math/gentype.inc>
34+
#undef __CLC_ADDRESS_SPACE
35+
#endif

libclc/generic/libspirv/math/remquo.inc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88

99
_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE __spirv_ocl_remquo(
1010
__CLC_GENTYPE x, __CLC_GENTYPE y, __CLC_ADDRESS_SPACE __CLC_INTN *q) {
11-
__CLC_INTN local_q;
12-
__CLC_GENTYPE ret = __clc_remquo(x, y, &local_q);
13-
*q = local_q;
11+
private __CLC_INTN private_q;
12+
__CLC_GENTYPE ret = __clc_remquo(x, y, &private_q);
13+
*q = private_q;
1414
return ret;
1515
}

libclc/generic/libspirv/math/sincos.inc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,10 @@ __CLC_DECLARE_SINCOS(global, __CLC_GENTYPE)
1616
__CLC_DECLARE_SINCOS(local, __CLC_GENTYPE)
1717
__CLC_DECLARE_SINCOS(private, __CLC_GENTYPE)
1818

19+
#if __OPENCL_C_VERSION__ == CL_VERSION_2_0 || \
20+
(__OPENCL_C_VERSION__ >= CL_VERSION_3_0 && \
21+
defined(__opencl_c_generic_address_space))
22+
__CLC_DECLARE_SINCOS(generic, __CLC_GENTYPE)
23+
#endif
24+
1925
#undef __CLC_DECLARE_SINCOS

sycl/test-e2e/USM/math.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// UNSUPPORTED: hip
21
// RUN: %{build} -o %t.out
32
// RUN: %{run} %t.out
43

0 commit comments

Comments
 (0)