Skip to content

Commit 0c29c39

Browse files
frasercrmckNoumanAmir657
authored andcommitted
[libclc] Move min/max/clamp into the CLC builtins library (llvm#114386)
These functions are "shared" between integer and floating-point types, hence the directory name. They are used in several CLC internal functions such as __clc_ldexp. Note that clspv and spirv targets don't want to define these functions, so pre-processor macros replace calls to __clc_min with regular min, for example. This means they can use as much of the generic CLC source files as possible, but where CLC functions would usually call out to an external __clc_min symbol, they call out to an external min symbol. Then they opt out of defining __clc_min itself in their CLC builtins library. Preprocessor definitions for these targets have also been changed somewhat: what used to be CLC_SPIRV (the 32-bit target) is now CLC_SPIRV32, and CLC_SPIRV now represents either CLC_SPIRV32 or CLC_SPIRV64. Same goes for CLC_CLSPV. There are no differences (measured with llvm-diff) in any of the final builtins libraries for nvptx, amdgpu, or clspv. Neither are there differences in the SPIR-V targets' LLVM IR before it's actually lowered to SPIR-V.
1 parent 2855501 commit 0c29c39

File tree

29 files changed

+164
-23
lines changed

29 files changed

+164
-23
lines changed

libclc/CMakeLists.txt

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -321,21 +321,30 @@ foreach( t ${LIBCLC_TARGETS_TO_BUILD} )
321321
message( STATUS " device: ${d} ( ${${d}_aliases} )" )
322322

323323
if ( ARCH STREQUAL spirv OR ARCH STREQUAL spirv64 )
324-
set( build_flags -O0 -finline-hint-functions )
324+
set( build_flags -O0 -finline-hint-functions -DCLC_SPIRV )
325325
set( opt_flags )
326326
set( spvflags --spirv-max-version=1.1 )
327+
set( MACRO_ARCH SPIRV32 )
328+
if( ARCH STREQUAL spirv64 )
329+
set( MACRO_ARCH SPIRV64 )
330+
endif()
327331
elseif( ARCH STREQUAL clspv OR ARCH STREQUAL clspv64 )
328-
set( build_flags "-Wno-unknown-assumption")
332+
set( build_flags "-Wno-unknown-assumption" -DCLC_CLSPV )
329333
set( opt_flags -O3 )
334+
set( MACRO_ARCH CLSPV32 )
335+
if( ARCH STREQUAL clspv64 )
336+
set( MACRO_ARCH CLSPV64 )
337+
endif()
330338
else()
331339
set( build_flags )
332340
set( opt_flags -O3 )
341+
set( MACRO_ARCH ${ARCH} )
333342
endif()
334343

335344
set( LIBCLC_ARCH_OBJFILE_DIR "${LIBCLC_OBJFILE_DIR}/${arch_suffix}" )
336345
file( MAKE_DIRECTORY ${LIBCLC_ARCH_OBJFILE_DIR} )
337346

338-
string( TOUPPER "CLC_${ARCH}" CLC_TARGET_DEFINE )
347+
string( TOUPPER "CLC_${MACRO_ARCH}" CLC_TARGET_DEFINE )
339348

340349
list( APPEND build_flags
341350
-D__CLC_INTERNAL

libclc/clc/include/clc/clcfunc.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77

88
// avoid inlines for SPIR-V related targets since we'll optimise later in the
99
// chain
10-
#if defined(CLC_SPIRV) || defined(CLC_SPIRV64)
10+
#if defined(CLC_SPIRV)
1111
#define _CLC_DEF
12-
#elif defined(CLC_CLSPV) || defined(CLC_CLSPV64)
12+
#elif defined(CLC_CLSPV)
1313
#define _CLC_DEF __attribute__((noinline)) __attribute__((clspv_libclc_builtin))
1414
#else
1515
#define _CLC_DEF __attribute__((always_inline))

libclc/generic/include/clc/integer/gentype.inc renamed to libclc/clc/include/clc/integer/gentype.inc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
//These 2 defines only change when switching between data sizes or base types to
2-
//keep this file manageable.
1+
// These 2 defines only change when switching between data sizes or base types
2+
// to keep this file manageable.
33
#define __CLC_GENSIZE 8
44
#define __CLC_SCALAR_GENTYPE char
55

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#if defined(CLC_CLSPV) || defined(CLC_SPIRV)
2+
// clspv and spir-v targets provide their own OpenCL-compatible clamp
3+
#define __clc_clamp clamp
4+
#else
5+
6+
#include <clc/clcfunc.h>
7+
#include <clc/clctypes.h>
8+
9+
#define __CLC_BODY <clc/shared/clc_clamp.inc>
10+
#include <clc/integer/gentype.inc>
11+
12+
#define __CLC_BODY <clc/shared/clc_clamp.inc>
13+
#include <clc/math/gentype.inc>
14+
15+
#endif
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
_CLC_OVERLOAD _CLC_DECL __CLC_GENTYPE __clc_clamp(__CLC_GENTYPE x,
2+
__CLC_GENTYPE y,
3+
__CLC_GENTYPE z);
4+
5+
#ifndef __CLC_SCALAR
6+
_CLC_OVERLOAD _CLC_DECL __CLC_GENTYPE __clc_clamp(__CLC_GENTYPE x,
7+
__CLC_SCALAR_GENTYPE y,
8+
__CLC_SCALAR_GENTYPE z);
9+
#endif
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#if defined(CLC_CLSPV) || defined(CLC_SPIRV)
2+
// clspv and spir-v targets provide their own OpenCL-compatible max
3+
#define __clc_max max
4+
#else
5+
6+
#define __CLC_BODY <clc/shared/clc_max.inc>
7+
#include <clc/integer/gentype.inc>
8+
9+
#define __CLC_BODY <clc/shared/clc_max.inc>
10+
#include <clc/math/gentype.inc>
11+
12+
#endif
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
_CLC_OVERLOAD _CLC_DECL __CLC_GENTYPE __clc_max(__CLC_GENTYPE a,
2+
__CLC_GENTYPE b);
3+
4+
#ifndef __CLC_SCALAR
5+
_CLC_OVERLOAD _CLC_DECL __CLC_GENTYPE __clc_max(__CLC_GENTYPE a,
6+
__CLC_SCALAR_GENTYPE b);
7+
#endif
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#if defined(CLC_CLSPV) || defined(CLC_SPIRV)
2+
// clspv and spir-v targets provide their own OpenCL-compatible min
3+
#define __clc_min min
4+
#else
5+
6+
#define __CLC_BODY <clc/shared/clc_min.inc>
7+
#include <clc/integer/gentype.inc>
8+
9+
#define __CLC_BODY <clc/shared/clc_min.inc>
10+
#include <clc/math/gentype.inc>
11+
12+
#endif
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
_CLC_OVERLOAD _CLC_DECL __CLC_GENTYPE __clc_min(__CLC_GENTYPE a,
2+
__CLC_GENTYPE b);
3+
4+
#ifndef __CLC_SCALAR
5+
_CLC_OVERLOAD _CLC_DECL __CLC_GENTYPE __clc_min(__CLC_GENTYPE a,
6+
__CLC_SCALAR_GENTYPE b);
7+
#endif

0 commit comments

Comments
 (0)