Skip to content

Commit b2bdd8b

Browse files
committed
[libclc] Create an internal 'clc' builtins library
Some libclc builtins currently use internal builtins prefixed with '__clc_' for various reasons, e.g., to avoid naming clashes. This commit formalizes this concept by starting to isolate the definitions of these internal clc builtins into a separate self-contained bytecode library, which is linked into each target's libclc OpenCL builtins before optimization takes place. The goal of this step is to allow additional libraries of builtins that provide entry points (or bindings) that are not written in OpenCL C but still wish to expose OpenCL-compatible builtins. By moving the implementations into a separate self-contained library, entry points can share as much code as possible without going through OpenCL C. The overall structure of the internal clc library is similar to the current OpenCL structure, with SOURCES files and targets being able to override the definitions of builtins as needed. The idea is that the OpenCL builtins will begin to need fewer target-specific overrides, as those will slowly move over to the clc builtins instead. Another advantage of having a separate bytecode library with the CLC implementations is that we can internalize the symbols when linking it (separately), whereas currently the CLC symbols make it into the final builtins library (and perhaps even the final compiled binary). This patch starts of with 'dot' as it's relatively self-contained, as opposed to most of the maths builtins which tend to pull in other builtins. We can also start to clang-format the builtins as we go, which should help to modernize the codebase.
1 parent 183b38e commit b2bdd8b

File tree

15 files changed

+209
-31
lines changed

15 files changed

+209
-31
lines changed

libclc/CMakeLists.txt

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,13 @@ set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS
2929
ptx-nvidiacl/lib/SOURCES;
3030
r600/lib/SOURCES;
3131
spirv/lib/SOURCES;
32-
spirv64/lib/SOURCES
32+
spirv64/lib/SOURCES;
33+
# CLC internal libraries
34+
clc/lib/generic/SOURCES;
35+
clc/lib/clspv/SOURCES;
36+
clc/lib/clspv64/SOURCES;
37+
clc/lib/spirv/SOURCES;
38+
clc/lib/spirv64/SOURCES;
3339
)
3440

3541
set( LIBCLC_MIN_LLVM 3.9.0 )
@@ -278,6 +284,14 @@ foreach( t ${LIBCLC_TARGETS_TO_BUILD} )
278284
set( DARCH ${ARCH} )
279285
endif()
280286

287+
set( clc_lib_files )
288+
libclc_configure_lib_source(
289+
clc_lib_files
290+
CLC_INTERNAL
291+
LIB_ROOT_DIR clc
292+
DIRS ${dirs} ${DARCH} ${DARCH}-${OS} ${DARCH}-${VENDOR}-${OS}
293+
)
294+
281295
set( opencl_lib_files )
282296
set( opencl_gen_files )
283297

@@ -326,7 +340,8 @@ foreach( t ${LIBCLC_TARGETS_TO_BUILD} )
326340
list( APPEND build_flags
327341
-D__CLC_INTERNAL
328342
-D${CLC_TARGET_DEFINE}
329-
-I${CMAKE_CURRENT_SOURCE_DIR}/generic/include
343+
# All libclc builtin libraries see CLC headers
344+
-I${CMAKE_CURRENT_SOURCE_DIR}/clc/include
330345
# FIXME: Fix libclc to not require disabling this noisy warning
331346
-Wno-bitwise-conditional-parentheses
332347
)
@@ -335,6 +350,20 @@ foreach( t ${LIBCLC_TARGETS_TO_BUILD} )
335350
list( APPEND build_flags -mcpu=${cpu} )
336351
endif()
337352

353+
add_libclc_builtin_set(
354+
CLC_INTERNAL
355+
ARCH ${ARCH}
356+
ARCH_SUFFIX clc-${arch_suffix}
357+
TRIPLE ${clang_triple}
358+
COMPILE_FLAGS ${build_flags}
359+
OPT_FLAGS ${opt_flags}
360+
LIB_FILES ${clc_lib_files}
361+
)
362+
363+
list( APPEND build_flags
364+
-I${CMAKE_CURRENT_SOURCE_DIR}/generic/include
365+
)
366+
338367
add_libclc_builtin_set(
339368
ARCH ${ARCH}
340369
ARCH_SUFFIX ${arch_suffix}
@@ -344,6 +373,8 @@ foreach( t ${LIBCLC_TARGETS_TO_BUILD} )
344373
LIB_FILES ${opencl_lib_files}
345374
GEN_FILES ${opencl_gen_files}
346375
ALIASES ${${d}_aliases}
376+
# Link in the CLC builtins and internalize their symbols
377+
INTERNAL_LINK_DEPENDENCIES $<TARGET_PROPERTY:builtins.link.clc-${arch_suffix},TARGET_FILE>
347378
)
348379
endforeach( d )
349380
endforeach( t )

libclc/generic/include/clc/clcfunc.h renamed to libclc/clc/include/clc/clcfunc.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
#ifndef __CLC_CLCFUNC_H_
2+
#define __CLC_CLCFUNC_H_
3+
14
#define _CLC_OVERLOAD __attribute__((overloadable))
25
#define _CLC_DECL
36
#define _CLC_INLINE __attribute__((always_inline)) inline
@@ -11,3 +14,5 @@
1114
#else
1215
#define _CLC_DEF __attribute__((always_inline))
1316
#endif
17+
18+
#endif // __CLC_CLCFUNC_H_

libclc/generic/include/clc/clctypes.h renamed to libclc/clc/include/clc/clctypes.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
#ifndef __CLC_CLCTYPES_H_
2+
#define __CLC_CLCTYPES_H_
3+
14
/* 6.1.1 Built-in Scalar Data Types */
25

36
typedef unsigned char uchar;
@@ -8,12 +11,12 @@ typedef unsigned long ulong;
811
typedef __SIZE_TYPE__ size_t;
912
typedef __PTRDIFF_TYPE__ ptrdiff_t;
1013

11-
#define __stdint_join3(a,b,c) a ## b ## c
14+
#define __stdint_join3(a, b, c) a##b##c
1215

13-
#define __intn_t(n) __stdint_join3(__INT, n, _TYPE__)
16+
#define __intn_t(n) __stdint_join3(__INT, n, _TYPE__)
1417
#define __uintn_t(n) __stdint_join3(unsigned __INT, n, _TYPE__)
1518

16-
typedef __intn_t(__INTPTR_WIDTH__) intptr_t;
19+
typedef __intn_t(__INTPTR_WIDTH__) intptr_t;
1720
typedef __uintn_t(__INTPTR_WIDTH__) uintptr_t;
1821

1922
#undef __uintn_t
@@ -93,3 +96,5 @@ typedef __attribute__((ext_vector_type(4))) half half4;
9396
typedef __attribute__((ext_vector_type(8))) half half8;
9497
typedef __attribute__((ext_vector_type(16))) half half16;
9598
#endif
99+
100+
#endif // __CLC_CLCTYPES_H_
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#define __CLC_BODY <clc/geometric/clc_dot.inc>
2+
#include <clc/geometric/floatn.inc>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
_CLC_OVERLOAD _CLC_DECL __CLC_FLOAT __clc_dot(__CLC_FLOATN p0, __CLC_FLOATN p1);

libclc/clc/include/clc/internal/clc.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#ifndef __CLC_INTERNAL_CLC_H_
2+
#define __CLC_INTERNAL_CLC_H_
3+
4+
#ifndef cl_clang_storage_class_specifiers
5+
#error Implementation requires cl_clang_storage_class_specifiers extension!
6+
#endif
7+
8+
#pragma OPENCL EXTENSION cl_clang_storage_class_specifiers : enable
9+
10+
#ifdef cl_khr_fp64
11+
#pragma OPENCL EXTENSION cl_khr_fp64 : enable
12+
#endif
13+
14+
#ifdef cl_khr_fp16
15+
#pragma OPENCL EXTENSION cl_khr_fp16 : enable
16+
#endif
17+
18+
/* Function Attributes */
19+
#include <clc/clcfunc.h>
20+
21+
/* 6.1 Supported Data Types */
22+
#include <clc/clctypes.h>
23+
24+
#pragma OPENCL EXTENSION all : disable
25+
26+
#endif // __CLC_INTERNAL_CLC_H_

libclc/clc/lib/clspv/SOURCES

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

libclc/clc/lib/clspv/dummy.cl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// Empty file

libclc/clc/lib/clspv64

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
clspv

libclc/clc/lib/generic/SOURCES

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

0 commit comments

Comments
 (0)