Skip to content

Commit ff64511

Browse files
authored
[SYCL] [NATIVECPU] Implement missing math builtins for scalar data types (#11321)
Implements missing math builtins for the x86 target in libclc, only for some scalar data types, missing types will be added in a follow up PR. The builtins are defined as LLVM-IR in order to allow us to handle ABI and directly call LLVM intrinsics.
1 parent 7afc2d0 commit ff64511

File tree

24 files changed

+222
-3
lines changed

24 files changed

+222
-3
lines changed

clang/lib/Basic/TargetInfo.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -517,9 +517,11 @@ void TargetInfo::adjust(DiagnosticsEngine &Diags, LangOptions &Opts) {
517517
if (Opts.FakeAddressSpaceMap)
518518
AddrSpaceMap = &FakeAddrSpaceMap;
519519

520-
if (Opts.SYCLIsDevice && Opts.SYCLIsNativeCPU) {
520+
if ((Opts.SYCLIsDevice || Opts.OpenCL) && Opts.SYCLIsNativeCPU) {
521521
// For SYCL Native CPU we use the NVPTXAddrSpaceMap because
522-
// we need builtins to be mangled with AS information
522+
// we need builtins to be mangled with AS information.
523+
// This is also enabled in OpenCL mode so that mangling
524+
// matches when building libclc.
523525

524526
static const unsigned SYCLNativeCPUASMap[] = {
525527
0, // Default
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// This test is temporarily disabled for SYCL Native CPU on Windows
2+
// UNSUPPORTED: system-windows
3+
// Checks that name mangling matches between SYCL Native CPU and OpenCL when -fsycl-is-native-cpu is set
4+
// RUN: %clang_cc1 -DCPP -fsycl-is-device -S -emit-llvm -internal-isystem %S/Inputs -fsycl-is-native-cpu -o %t_sycl.ll %s
5+
// RUN: FileCheck -input-file=%t_sycl.ll %s
6+
7+
// RUN: %clang_cc1 -x cl -DOCL -S -emit-llvm -internal-isystem %S/Inputs -fsycl-is-native-cpu -o %t_ocl.ll %s
8+
// RUN: FileCheck -input-file=%t_ocl.ll %s
9+
10+
#ifdef CPP
11+
#define AS_LOCAL __attribute((address_space(3)))
12+
#define AS_GLOBAL __attribute((address_space(1)))
13+
#define AS_PRIVATE __attribute((address_space(0)))
14+
#define ATTRS [[intel::device_indirectly_callable]]
15+
#define ATTRS2 SYCL_EXTERNAL
16+
#else
17+
#ifdef OCL
18+
#define AS_LOCAL __local
19+
#define AS_GLOBAL __global
20+
#define AS_PRIVATE __private
21+
#define ATTRS __attribute((overloadable))
22+
#define ATTRS2 __attribute((overloadable))
23+
#endif
24+
#endif
25+
26+
27+
ATTRS2 void use_private(int *p);
28+
ATTRS void func(AS_LOCAL int *p1, AS_GLOBAL int *p2, AS_PRIVATE int *p3){
29+
int private_var;
30+
use_private(&private_var);
31+
}
32+
// CHECK: define dso_local void @_Z4funcPU3AS3iPU3AS1iPi(
33+
// CHECK: call void @_Z11use_privatePi(
34+
35+
36+

libclc/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,8 +345,9 @@ foreach( t ${LIBCLC_TARGETS_TO_BUILD} )
345345
# clang builtins need to be accessible
346346
set( flags "SHELL:-mcpu=gfx940")
347347
elseif( ${ARCH} STREQUAL x86_64)
348-
# TODO: This is used by native cpu, we should define an option to set this flags
348+
# TODO: This is used by SYCL Native Cpu, we should define an option to set this flags
349349
set( flags "SHELL:-Xclang -target-feature -Xclang +avx"
350+
"SHELL:-Xclang -fsycl-is-native-cpu"
350351
"SHELL:-Xclang -target-feature -Xclang +avx512f")
351352
else()
352353
set ( flags )

libclc/x86_64-unknown-linux/libspirv/SOURCES

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ math/native_sqrt.cl
1616
math/rint.cl
1717
math/round.cl
1818
math/trunc.cl
19+
shared/helpers.ll
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#include "func.h"
2+
3+
#define GEN_UNARY_BUILTIN_T(NAME, TYPE) \
4+
_CLC_OVERLOAD TYPE __##NAME##_helper(TYPE); \
5+
_CLC_OVERLOAD TYPE __spirv_ocl_##NAME(TYPE n) { return __##NAME##_helper(n); }
6+
7+
#define GEN_UNARY_BUILTIN(NAME) \
8+
GEN_UNARY_BUILTIN_T(NAME, int) \
9+
GEN_UNARY_BUILTIN_T(NAME, signed char)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#include "helpers.h"
2+
3+
GEN_UNARY_BUILTIN(popcount)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#include "helpers.h"
2+
3+
GEN_UNARY_BUILTIN(ceil)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#define IS_FABS
2+
#include "helpers.h"
3+
4+
GEN_UNARY_BUILTIN(fabs)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#include "helpers.h"
2+
3+
GEN_UNARY_BUILTIN(floor)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#include "helpers.h"
2+
3+
GEN_TERNARY_BUILTIN(fma);
4+
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#include "func.h"
2+
#include "types.h"
3+
4+
#ifdef NO_CLANG_BUILTINS
5+
6+
#define GEN_UNARY_BUILTIN_T(NAME, TYPE) \
7+
_CLC_OVERLOAD TYPE __##NAME##_helper(TYPE); \
8+
_CLC_OVERLOAD TYPE __spirv_ocl_##NAME(TYPE n) { return __##NAME##_helper(n); }
9+
10+
#define GEN_TERNARY_BUILTIN_T(NAME, TYPE) \
11+
_CLC_OVERLOAD TYPE __##NAME##_helper(TYPE, TYPE, TYPE); \
12+
_CLC_OVERLOAD TYPE __spirv_ocl_##NAME(TYPE a, TYPE b, TYPE c) { \
13+
return __##NAME##_helper(a, b, c); \
14+
}
15+
#define GEN_UNARY_BUILTIN(NAME) \
16+
GEN_UNARY_BUILTIN_T(NAME, float) \
17+
GEN_UNARY_BUILTIN_T(NAME, double)
18+
19+
#define GEN_TERNARY_BUILTIN(NAME) \
20+
GEN_TERNARY_BUILTIN_T(NAME, float) \
21+
GEN_TERNARY_BUILTIN_T(NAME, double)
22+
23+
#else
24+
25+
#ifndef IS_NATIVE
26+
#define GETNAME(ID) __spirv_ocl_##ID
27+
#else
28+
#define GETNAME(ID) __spirv_ocl_native_##ID
29+
#endif
30+
31+
// Todo: fabs is the only builtin whose vector version is not named
32+
// __builtin_elementwise_##NAME
33+
#ifndef IS_FABS
34+
#define GEN_UNARY_VECTOR_BUILTIN(NAME, TYPE, NUM) \
35+
_CLC_OVERLOAD TYPE##NUM GETNAME(NAME)(TYPE##NUM n) { \
36+
return __builtin_elementwise_##NAME(n); \
37+
}
38+
#else
39+
#define GEN_UNARY_VECTOR_BUILTIN(NAME, TYPE, NUM) \
40+
_CLC_OVERLOAD TYPE##NUM GETNAME(NAME)(TYPE##NUM n) { \
41+
return __builtin_elementwise_abs(n); \
42+
}
43+
#endif
44+
45+
#define GEN_UNARY_VECTOR_BUILTIN_T(NAME, TYPE) \
46+
GEN_UNARY_VECTOR_BUILTIN(NAME, TYPE, 2) \
47+
GEN_UNARY_VECTOR_BUILTIN(NAME, TYPE, 3) \
48+
GEN_UNARY_VECTOR_BUILTIN(NAME, TYPE, 4) \
49+
GEN_UNARY_VECTOR_BUILTIN(NAME, TYPE, 8) \
50+
GEN_UNARY_VECTOR_BUILTIN(NAME, TYPE, 16)
51+
52+
#define GEN_UNARY_BUILTIN(NAME) \
53+
_CLC_OVERLOAD float GETNAME(NAME)(float n) { \
54+
return __builtin_##NAME##f(n); \
55+
} \
56+
_CLC_OVERLOAD double GETNAME(NAME)(double n) { return __builtin_##NAME(n); } \
57+
GEN_UNARY_VECTOR_BUILTIN_T(NAME, float) \
58+
GEN_UNARY_VECTOR_BUILTIN_T(NAME, double)
59+
60+
#define GEN_TERNARY_VECTOR_BUILTIN(NAME, TYPE, NUM) \
61+
_CLC_OVERLOAD TYPE##NUM GETNAME(NAME)(TYPE##NUM n1, TYPE##NUM n2, \
62+
TYPE##NUM n3) { \
63+
return __builtin_elementwise_##NAME(n1, n2, n3); \
64+
}
65+
66+
#define GEN_TERNARY_VECTOR_BUILTIN_T(NAME, TYPE) \
67+
GEN_TERNARY_VECTOR_BUILTIN(NAME, TYPE, 2) \
68+
GEN_TERNARY_VECTOR_BUILTIN(NAME, TYPE, 3) \
69+
GEN_TERNARY_VECTOR_BUILTIN(NAME, TYPE, 4) \
70+
GEN_TERNARY_VECTOR_BUILTIN(NAME, TYPE, 8) \
71+
GEN_TERNARY_VECTOR_BUILTIN(NAME, TYPE, 16)
72+
73+
#define GEN_TERNARY_BUILTIN(NAME) \
74+
_CLC_OVERLOAD float GETNAME(NAME)(float n1, float n2, float n3) { \
75+
return __builtin_##NAME##f(n1, n2, n3); \
76+
} \
77+
_CLC_OVERLOAD double GETNAME(NAME)(double n1, double n2, double n3) { \
78+
return __builtin_##NAME(n1, n2, n3); \
79+
} \
80+
GEN_TERNARY_VECTOR_BUILTIN_T(NAME, float) \
81+
GEN_TERNARY_VECTOR_BUILTIN_T(NAME, double)
82+
#endif
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#define IS_NATIVE
2+
#include "helpers.h"
3+
4+
GEN_UNARY_BUILTIN(cos)
5+
#undef IS_NATIVE
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#define IS_NATIVE
2+
#include "helpers.h"
3+
4+
GEN_UNARY_BUILTIN(exp)
5+
#undef IS_NATIVE
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#define IS_NATIVE
2+
#include "helpers.h"
3+
4+
GEN_UNARY_BUILTIN(exp2)
5+
#undef IS_NATIVE
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#define IS_NATIVE
2+
#include "helpers.h"
3+
4+
GEN_UNARY_BUILTIN(log)
5+
#undef IS_NATIVE
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#define IS_NATIVE
2+
#include "helpers.h"
3+
4+
GEN_UNARY_BUILTIN(log10)
5+
#undef IS_NATIVE
6+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#define IS_NATIVE
2+
#include "helpers.h"
3+
4+
GEN_UNARY_BUILTIN(log2)
5+
#undef IS_NATIVE
6+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#define IS_NATIVE
2+
#include "helpers.h"
3+
4+
GEN_UNARY_BUILTIN(sin)
5+
#undef IS_NATIVE
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#define IS_NATIVE
2+
#include "helpers.h"
3+
4+
GEN_UNARY_BUILTIN(sqrt)
5+
#undef IS_NATIVE
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#include "helpers.h"
2+
3+
GEN_UNARY_BUILTIN(rint)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#include "helpers.h"
2+
3+
GEN_UNARY_BUILTIN(round)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#include "helpers.h"
2+
3+
GEN_UNARY_BUILTIN(sqrt)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#include "helpers.h"
2+
3+
GEN_UNARY_BUILTIN(trunc)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
declare i32 @llvm.ctpop.i32(i32 %n)
2+
declare i8 @llvm.ctpop.i8(i8 %n)
3+
4+
5+
define dso_local i32 @_Z17__popcount_helperi(i32 %x) {
6+
entry:
7+
%call = call i32 @llvm.ctpop.i32(i32 %x)
8+
ret i32 %call
9+
}
10+
11+
12+
define dso_local i8 @_Z17__popcount_helpera(i8 %x) {
13+
entry:
14+
%call = call i8 @llvm.ctpop.i8(i8 %x)
15+
ret i8 %call
16+
}
17+

0 commit comments

Comments
 (0)