Skip to content

Commit 0a9bcdc

Browse files
committed
[libclc] Move sign to the CLC builtins library
This patch necessitates some changes to how CLSPV and SPIR-V targets are built. This is the first patch in this series in which an OpenCL function declaration has been called from the CLC library for these targets. Since libclc's OpenCL headers aren't being included at this stage, the OpenCL sign function isn't available. To fix this, these two libclc targets now have clang declare OpenCL builtins when building the internal CLC library. The __CLC_INTERNAL preprocessor definition has been repurposed (without the leading underscores) to be passed when building the internal CLC library. It was only used in one other place to guard an extra maths preprocessor definition, which we can do unconditionally. There are no changes (with llvm-diff) to any libclc target other than SPIR-V, which now has OpenCL sign call __clc_sign.
1 parent 0a1795f commit 0a9bcdc

File tree

10 files changed

+164
-38
lines changed

10 files changed

+164
-38
lines changed

libclc/CMakeLists.txt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,6 @@ foreach( t ${LIBCLC_TARGETS_TO_BUILD} )
347347
string( TOUPPER "CLC_${MACRO_ARCH}" CLC_TARGET_DEFINE )
348348

349349
list( APPEND build_flags
350-
-D__CLC_INTERNAL
351350
-D${CLC_TARGET_DEFINE}
352351
# All libclc builtin libraries see CLC headers
353352
-I${CMAKE_CURRENT_SOURCE_DIR}/clc/include
@@ -359,12 +358,20 @@ foreach( t ${LIBCLC_TARGETS_TO_BUILD} )
359358
list( APPEND build_flags -mcpu=${cpu} )
360359
endif()
361360

361+
set( clc_build_flags ${build_flags} -DCLC_INTERNAL )
362+
363+
# clspv and spir-v targets remap some CLC functions to OpenCL builtins.
364+
# Automatically provide those declarations to the compiler for CLC builtins.
365+
if ( ARCH STREQUAL spirv OR ARCH STREQUAL spirv64 OR ARCH STREQUAL clspv OR ARCH STREQUAL clspv64 )
366+
list( APPEND clc_build_flags -Xclang -fdeclare-opencl-builtins )
367+
endif()
368+
362369
add_libclc_builtin_set(
363370
CLC_INTERNAL
364371
ARCH ${ARCH}
365372
ARCH_SUFFIX clc-${arch_suffix}
366373
TRIPLE ${clang_triple}
367-
COMPILE_FLAGS ${build_flags}
374+
COMPILE_FLAGS ${clc_build_flags}
368375
OPT_FLAGS ${opt_flags}
369376
LIB_FILES ${clc_lib_files}
370377
)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#ifndef __CLC_COMMON_CLC_SIGN_H__
2+
#define __CLC_COMMON_CLC_SIGN_H__
3+
4+
#if defined(CLC_CLSPV)
5+
// clspv targets provide their own OpenCL-compatible sign
6+
#define __clc_sign sign
7+
#else
8+
9+
#define __CLC_FUNCTION __clc_sign
10+
#define __CLC_BODY <clc/math/unary_decl.inc>
11+
#include <clc/math/gentype.inc>
12+
#undef __CLC_FUNCTION
13+
#undef __CLC_BODY
14+
15+
#endif
16+
17+
#endif // __CLC_COMMON_CLC_SIGN_H__
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#define __CLC_FLOATN float
2+
#include __CLC_BODY
3+
#undef __CLC_FLOATN
4+
5+
#define __CLC_FLOATN float2
6+
#include __CLC_BODY
7+
#undef __CLC_FLOATN
8+
9+
#define __CLC_FLOATN float3
10+
#include __CLC_BODY
11+
#undef __CLC_FLOATN
12+
13+
#define __CLC_FLOATN float4
14+
#include __CLC_BODY
15+
#undef __CLC_FLOATN
16+
17+
#define __CLC_FLOATN float8
18+
#include __CLC_BODY
19+
#undef __CLC_FLOATN
20+
21+
#define __CLC_FLOATN float16
22+
#include __CLC_BODY
23+
#undef __CLC_FLOATN
24+
25+
#undef __CLC_FLOAT
26+
#undef __CLC_INT
27+
28+
#ifdef cl_khr_fp64
29+
#pragma OPENCL EXTENSION cl_khr_fp64 : enable
30+
31+
#define __CLC_FLOATN double
32+
#include __CLC_BODY
33+
#undef __CLC_FLOATN
34+
35+
#define __CLC_FLOATN double2
36+
#include __CLC_BODY
37+
#undef __CLC_FLOATN
38+
39+
#define __CLC_FLOATN double3
40+
#include __CLC_BODY
41+
#undef __CLC_FLOATN
42+
43+
#define __CLC_FLOATN double4
44+
#include __CLC_BODY
45+
#undef __CLC_FLOATN
46+
47+
#define __CLC_FLOATN double8
48+
#include __CLC_BODY
49+
#undef __CLC_FLOATN
50+
51+
#define __CLC_FLOATN double16
52+
#include __CLC_BODY
53+
#undef __CLC_FLOATN
54+
55+
#endif
56+
#ifdef cl_khr_fp16
57+
#pragma OPENCL EXTENSION cl_khr_fp16 : enable
58+
59+
#define __CLC_FLOATN half
60+
#include __CLC_BODY
61+
#undef __CLC_FLOATN
62+
63+
#define __CLC_FLOATN half2
64+
#include __CLC_BODY
65+
#undef __CLC_FLOATN
66+
67+
#define __CLC_FLOATN half3
68+
#include __CLC_BODY
69+
#undef __CLC_FLOATN
70+
71+
#define __CLC_FLOATN half4
72+
#include __CLC_BODY
73+
#undef __CLC_FLOATN
74+
75+
#define __CLC_FLOATN half8
76+
#include __CLC_BODY
77+
#undef __CLC_FLOATN
78+
79+
#define __CLC_FLOATN half16
80+
#include __CLC_BODY
81+
#undef __CLC_FLOATN
82+
83+
#endif
84+
85+
#undef __CLC_BODY
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#include <clc/utils.h>
2+
3+
#define __CLC_FUNCTION(x) __CLC_CONCAT(__clc_, x)
4+
5+
_CLC_OVERLOAD _CLC_DEF __CLC_FLOATN FUNCTION(__CLC_FLOATN a) {
6+
return __CLC_FUNCTION(FUNCTION)(a);
7+
}

libclc/clc/lib/generic/SOURCES

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
common/clc_sign.cl
12
geometric/clc_dot.cl
23
integer/clc_abs.cl
34
integer/clc_abs_diff.cl
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include <clc/clcmacro.h>
2+
#include <clc/internal/clc.h>
3+
#include <clc/relational/clc_isnan.h>
4+
5+
#define CLC_SIGN(TYPE, F) \
6+
_CLC_DEF _CLC_OVERLOAD TYPE __clc_sign(TYPE x) { \
7+
if (__clc_isnan(x)) { \
8+
return 0.0F; \
9+
} \
10+
if (x > 0.0F) { \
11+
return 1.0F; \
12+
} \
13+
if (x < 0.0F) { \
14+
return -1.0F; \
15+
} \
16+
return x; /* -0.0 or +0.0 */ \
17+
}
18+
19+
CLC_SIGN(float, f)
20+
_CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, float, __clc_sign, float)
21+
22+
#ifdef cl_khr_fp64
23+
24+
#pragma OPENCL EXTENSION cl_khr_fp64 : enable
25+
26+
CLC_SIGN(double, )
27+
_CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, double, __clc_sign, double)
28+
29+
#endif
30+
31+
#ifdef cl_khr_fp16
32+
33+
#pragma OPENCL EXTENSION cl_khr_fp16 : enable
34+
35+
CLC_SIGN(half, )
36+
_CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, half, __clc_sign, half)
37+
38+
#endif

libclc/clc/lib/spirv/SOURCES

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
../generic/common/clc_sign.cl
12
../generic/geometric/clc_dot.cl
23

libclc/clc/lib/spirv64/SOURCES

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
../generic/common/clc_sign.cl
12
../generic/geometric/clc_dot.cl

libclc/generic/include/clc/float/definitions.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,7 @@
3131
#define M_SQRT2_F 0x1.6a09e6p+0f
3232
#define M_SQRT1_2_F 0x1.6a09e6p-1f
3333

34-
#ifdef __CLC_INTERNAL
35-
#define M_LOG210_F 0x1.a934f0p+1f
36-
#endif
34+
#define M_LOG210_F 0x1.a934f0p+1f
3735

3836
#ifdef cl_khr_fp64
3937

libclc/generic/lib/common/sign.cl

Lines changed: 4 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,8 @@
11
#include <clc/clc.h>
22
#include <clc/clcmacro.h>
3+
#include <clc/common/clc_sign.h>
34

4-
#define SIGN(TYPE, F) \
5-
_CLC_DEF _CLC_OVERLOAD TYPE sign(TYPE x) { \
6-
if (isnan(x)) { \
7-
return 0.0F; \
8-
} \
9-
if (x > 0.0F) { \
10-
return 1.0F; \
11-
} \
12-
if (x < 0.0F) { \
13-
return -1.0F; \
14-
} \
15-
return x; /* -0.0 or +0.0 */ \
16-
}
5+
#define FUNCTION sign
6+
#define __CLC_BODY <clc/common/unary_def.inc>
177

18-
SIGN(float, f)
19-
_CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, float, sign, float)
20-
21-
#ifdef cl_khr_fp64
22-
23-
#pragma OPENCL EXTENSION cl_khr_fp64 : enable
24-
25-
SIGN(double, )
26-
_CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, double, sign, double)
27-
28-
#endif
29-
30-
#ifdef cl_khr_fp16
31-
32-
#pragma OPENCL EXTENSION cl_khr_fp16 : enable
33-
34-
SIGN(half,)
35-
_CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, half, sign, half)
36-
37-
#endif
8+
#include <clc/common/floatn.inc>

0 commit comments

Comments
 (0)