Skip to content

Commit 16eda88

Browse files
committed
[SYCL] Define SYCL_EXTERNAL macro as prescribed by the spec
Signed-off-by: Sergey Dmitriev <[email protected]>
1 parent 361419c commit 16eda88

File tree

4 files changed

+80
-0
lines changed

4 files changed

+80
-0
lines changed

clang/lib/Frontend/InitPreprocessor.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1074,6 +1074,7 @@ static void InitializePredefinedMacros(const TargetInfo &TI,
10741074
// SYCL device compiler which doesn't produce host binary.
10751075
if (LangOpts.SYCLIsDevice) {
10761076
Builder.defineMacro("__SYCL_DEVICE_ONLY__", "1");
1077+
Builder.defineMacro("SYCL_EXTERNAL", "__attribute__((sycl_device))");
10771078
if (!getenv("DISABLE_INFER_AS"))
10781079
Builder.defineMacro("__SYCL_ENABLE_INFER_AS__", "1");
10791080
}

clang/test/Preprocessor/sycl-macro.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
// RUN: %clang_cc1 %s -fsycl -E -dM | FileCheck --check-prefix=CHECK-ANY-SYCL %s
44
// RUN: %clang_cc1 %s -fsycl-is-device -E -dM -fms-compatibility | FileCheck --check-prefix=CHECK-MSVC %s
55
// CHECK-NOT:#define __SYCL_DEVICE_ONLY__ 1
6+
// CHECK-NOT:#define SYCL_EXTERNAL
67
// CHECK-NOT:#define CL_SYCL_LANGUAGE_VERSION 121
78
// CHECK-ANY-SYCL-NOT:#define __SYCL_DEVICE_ONLY__ 1
89
// CHECK-ANY-SYCL:#define CL_SYCL_LANGUAGE_VERSION 121
910
// CHECK-SYCL:#define CL_SYCL_LANGUAGE_VERSION 121
11+
// CHECK-SYCL:#define SYCL_EXTERNAL __attribute__((sycl_device))
1012
// CHECK-MSVC-NOT: __GNUC__
1113
// CHECK-MSVC-NOT: __STDC__

sycl/include/CL/sycl/detail/common.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ static inline std::string codeToString(cl_int code){
8888
#define ALWAYS_INLINE
8989
#endif
9090

91+
#ifndef SYCL_EXTERNAL
92+
#define SYCL_EXTERNAL
93+
#endif
94+
9195
namespace cl {
9296
namespace sycl {
9397
namespace detail {
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// Test1 - check that kernel can call a SYCL_EXTERNAL function defined in a
2+
// different object file.
3+
// RUN: %clangxx -fsycl -DSOURCE1 -c %s -o %t1.o
4+
// RUN: %clangxx -fsycl -DSOURCE2 -c %s -o %t2.o
5+
// RUN: %clangxx -fsycl %t1.o %t2.o -o %t.exe
6+
// RUN: %CPU_RUN_PLACEHOLDER %t.exe
7+
// RUN: %GPU_RUN_PLACEHOLDER %t.exe
8+
// RUN: %ACC_RUN_PLACEHOLDER %t.exe
9+
//
10+
// Test2 - check that kernel can call a SYCL_EXTERNAL function defined in a
11+
// static library.
12+
// RUN: rm -f %t.a
13+
// RUN: llvm-ar crv %t.a %t1.o
14+
// RUN: %clangxx -fsycl %t2.o -foffload-static-lib=%t.a -o %t.exe
15+
// RUN: %CPU_RUN_PLACEHOLDER %t.exe
16+
// RUN: %GPU_RUN_PLACEHOLDER %t.exe
17+
// RUN: %ACC_RUN_PLACEHOLDER %t.exe
18+
19+
#include <CL/sycl.hpp>
20+
#include <iostream>
21+
22+
#ifdef SOURCE1
23+
int bar(int b);
24+
25+
SYCL_EXTERNAL
26+
int foo(int a, int b) {
27+
return a + bar(b);
28+
}
29+
30+
int bar(int b) {
31+
return b + 5;
32+
}
33+
#endif // SOURCE1
34+
35+
#ifdef SOURCE2
36+
SYCL_EXTERNAL
37+
int foo(int A, int B);
38+
39+
int main(void) {
40+
constexpr unsigned Size = 4;
41+
int A[Size] = {1, 2, 3, 4};
42+
int B[Size] = {1, 2, 3, 4};
43+
int C[Size];
44+
45+
{
46+
cl::sycl::range<1> range{Size};
47+
cl::sycl::buffer<int, 1> bufA(A, range);
48+
cl::sycl::buffer<int, 1> bufB(B, range);
49+
cl::sycl::buffer<int, 1> bufC(C, range);
50+
51+
cl::sycl::queue().submit([&](cl::sycl::handler &cgh) {
52+
auto accA = bufA.get_access<cl::sycl::access::mode::read>(cgh);
53+
auto accB = bufB.get_access<cl::sycl::access::mode::read>(cgh);
54+
auto accC = bufC.get_access<cl::sycl::access::mode::write>(cgh);
55+
56+
cgh.parallel_for<class Test>(range, [=](cl::sycl::id<1> ID) {
57+
accC[ID] = foo(accA[ID], accB[ID]);
58+
});
59+
});
60+
}
61+
62+
for (unsigned I = 0; I < Size; ++I) {
63+
int Ref = foo(A[I], B[I]);
64+
if (C[I] != Ref) {
65+
std::cout << "fail: [" << I << "] == " << C[I] << ", expected " << Ref
66+
<< "\n";
67+
return 1;
68+
}
69+
}
70+
std::cout << "pass\n";
71+
return 0;
72+
}
73+
#endif // SOURCE2

0 commit comments

Comments
 (0)