Skip to content

Commit 94142d9

Browse files
authored
[libclc] Support the generic address space (#137183)
This commit provides definitions of builtins with the generic address space. One concept to consider is the difference between supporting the generic address space from the user's perspective and the requirement for libclc as a compiler implementation detail to define separate generic address space builtins. In practice a target (like NVPTX) might notionally support the generic address space, but it's mapped to the same LLVM target address space as another address space (often the private one). In such cases libclc must be careful not to define both private and generic overloads of the same builtin. We track these two concepts separately, and make the assumption that if the generic address space does clash with another, it's with the private one. We track the concepts separately because there are some builtins such as atomics that are defined for the generic address space but not the private address space.
1 parent d997b4f commit 94142d9

File tree

21 files changed

+177
-4
lines changed

21 files changed

+177
-4
lines changed

libclc/CMakeLists.txt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,12 +420,40 @@ foreach( t ${LIBCLC_TARGETS_TO_BUILD} )
420420
-D${CLC_TARGET_DEFINE}
421421
# All libclc builtin libraries see CLC headers
422422
-I${CMAKE_CURRENT_SOURCE_DIR}/clc/include
423+
# Error on undefined macros
424+
-Werror=undef
423425
)
424426

425427
if( NOT "${cpu}" STREQUAL "" )
426428
list( APPEND build_flags -mcpu=${cpu} )
427429
endif()
428430

431+
# Generic address space support.
432+
# Note: when declaring builtins, we must consider that even if a target
433+
# formally/nominally supports the generic address space, in practice that
434+
# target may map it to the same target address space as another address
435+
# space (often the private one). In such cases we must be careful not to
436+
# multiply-define a builtin in a single target address space, as it would
437+
# result in a mangling clash.
438+
# For this reason we must consider the target support of the generic
439+
# address space separately from the *implementation* decision about whether
440+
# to declare certain builtins in that address space.
441+
# Note: we assume that if there is no distinct generic address space, it
442+
# maps to the private address space.
443+
set ( private_addrspace_val 0 )
444+
set ( generic_addrspace_val 0 )
445+
if( ARCH STREQUAL amdgcn OR ARCH STREQUAL r600 OR ARCH STREQUAL amdgcn-amdhsa )
446+
set ( private_addrspace_val 5 )
447+
endif()
448+
if( ARCH STREQUAL spirv OR ARCH STREQUAL spirv64
449+
OR ARCH STREQUAL clspv OR ARCH STREQUAL clspv64 )
450+
set ( generic_addrspace_val 4 )
451+
endif()
452+
list( APPEND build_flags
453+
-D__CLC_PRIVATE_ADDRSPACE_VAL=${private_addrspace_val}
454+
-D__CLC_GENERIC_ADDRSPACE_VAL=${generic_addrspace_val}
455+
)
456+
429457
set( clc_build_flags ${build_flags} -DCLC_INTERNAL )
430458

431459
add_libclc_builtin_set(

libclc/clc/include/clc/clcfunc.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,18 @@
2323
#define _CLC_DEF __attribute__((always_inline))
2424
#endif
2525

26+
#if __OPENCL_C_VERSION__ == CL_VERSION_2_0 || \
27+
(__OPENCL_C_VERSION__ >= CL_VERSION_3_0 && \
28+
defined(__opencl_c_generic_address_space))
29+
#define _CLC_GENERIC_AS_SUPPORTED 1
30+
#if __CLC_PRIVATE_ADDRSPACE_VAL != __CLC_GENERIC_ADDRSPACE_VAL
31+
#define _CLC_DISTINCT_GENERIC_AS_SUPPORTED 1
32+
#else
33+
#define _CLC_DISTINCT_GENERIC_AS_SUPPORTED 0
34+
#endif
35+
#else
36+
#define _CLC_GENERIC_AS_SUPPORTED 0
37+
#define _CLC_DISTINCT_GENERIC_AS_SUPPORTED 0
38+
#endif
39+
2640
#endif // __CLC_CLCFUNC_H_

libclc/clc/include/clc/math/remquo_decl.inc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,8 @@ _CLC_OVERLOAD _CLC_DECL __CLC_GENTYPE __CLC_FUNCTION(__CLC_GENTYPE x,
1717
_CLC_OVERLOAD _CLC_DECL __CLC_GENTYPE __CLC_FUNCTION(__CLC_GENTYPE x,
1818
__CLC_GENTYPE y,
1919
local __CLC_INTN *q);
20+
#if _CLC_GENERIC_AS_SUPPORTED
21+
_CLC_OVERLOAD _CLC_DECL __CLC_GENTYPE __CLC_FUNCTION(__CLC_GENTYPE x,
22+
__CLC_GENTYPE y,
23+
generic __CLC_INTN *q);
24+
#endif

libclc/clc/include/clc/math/unary_decl_with_int_ptr.inc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,7 @@ _CLC_OVERLOAD _CLC_DECL __CLC_GENTYPE __CLC_FUNCTION(__CLC_GENTYPE x,
1212
local __CLC_INTN *iptr);
1313
_CLC_OVERLOAD _CLC_DECL __CLC_GENTYPE __CLC_FUNCTION(__CLC_GENTYPE x,
1414
private __CLC_INTN *iptr);
15+
#if _CLC_GENERIC_AS_SUPPORTED
16+
_CLC_OVERLOAD _CLC_DECL __CLC_GENTYPE __CLC_FUNCTION(__CLC_GENTYPE x,
17+
generic __CLC_INTN *iptr);
18+
#endif

libclc/clc/include/clc/math/unary_decl_with_ptr.inc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,8 @@ _CLC_OVERLOAD _CLC_DECL __CLC_GENTYPE __CLC_FUNCTION(__CLC_GENTYPE x,
1212
local __CLC_GENTYPE *ptr);
1313
_CLC_OVERLOAD _CLC_DECL __CLC_GENTYPE
1414
__CLC_FUNCTION(__CLC_GENTYPE x, private __CLC_GENTYPE *ptr);
15+
16+
#if _CLC_GENERIC_AS_SUPPORTED
17+
_CLC_OVERLOAD _CLC_DECL __CLC_GENTYPE
18+
__CLC_FUNCTION(__CLC_GENTYPE x, generic __CLC_GENTYPE *ptr);
19+
#endif

libclc/clc/include/clc/math/unary_def_with_int_ptr.inc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,10 @@ _CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE FUNCTION(__CLC_GENTYPE x,
2626
local __CLC_INTN *iptr) {
2727
return __CLC_FUNCTION(FUNCTION)(x, iptr);
2828
}
29+
30+
#if _CLC_DISTINCT_GENERIC_AS_SUPPORTED
31+
_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE FUNCTION(__CLC_GENTYPE x,
32+
generic __CLC_INTN *iptr) {
33+
return __CLC_FUNCTION(FUNCTION)(x, iptr);
34+
}
35+
#endif

libclc/clc/include/clc/math/unary_def_with_ptr.inc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,10 @@ _CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE FUNCTION(__CLC_GENTYPE x,
2626
local __CLC_GENTYPE *ptr) {
2727
return __CLC_FUNCTION(FUNCTION)(x, ptr);
2828
}
29+
30+
#if _CLC_DISTINCT_GENERIC_AS_SUPPORTED
31+
_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE FUNCTION(__CLC_GENTYPE x,
32+
generic __CLC_GENTYPE *ptr) {
33+
return __CLC_FUNCTION(FUNCTION)(x, ptr);
34+
}
35+
#endif

libclc/clc/lib/generic/math/clc_fract.inc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,8 @@ _CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE __clc_fract(__CLC_GENTYPE x,
3434

3535
FRACT_DEF(local);
3636
FRACT_DEF(global);
37+
#if _CLC_DISTINCT_GENERIC_AS_SUPPORTED
38+
FRACT_DEF(generic);
39+
#endif
3740

3841
#undef MIN_CONSTANT

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//===----------------------------------------------------------------------===//
88

99
#include <clc/clc_convert.h>
10+
#include <clc/clcfunc.h>
1011
#include <clc/internal/clc.h>
1112
#include <clc/math/math.h>
1213
#include <clc/relational/clc_isinf.h>
@@ -28,3 +29,10 @@
2829
#define __CLC_ADDRESS_SPACE local
2930
#include <clc/math/gentype.inc>
3031
#undef __CLC_ADDRESS_SPACE
32+
33+
#if _CLC_DISTINCT_GENERIC_AS_SUPPORTED
34+
#define __CLC_BODY <clc_frexp.inc>
35+
#define __CLC_ADDRESS_SPACE generic
36+
#include <clc/math/gentype.inc>
37+
#undef __CLC_ADDRESS_SPACE
38+
#endif

libclc/clc/lib/generic/math/clc_modf.inc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,8 @@ _CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE __clc_modf(__CLC_GENTYPE x,
2323
CLC_MODF_DEF(local);
2424
CLC_MODF_DEF(global);
2525

26+
#if _CLC_DISTINCT_GENERIC_AS_SUPPORTED
27+
CLC_MODF_DEF(generic);
28+
#endif
29+
2630
#undef CLC_MODF_DEF

0 commit comments

Comments
 (0)