Skip to content

Commit 1dbc358

Browse files
[SYCL] Immutable kernel functions redo. (#2259)
This is the same immutable kernel functions PR that was merged (and reverted) last week. The SYCL conformance tests have been updated, so hopefully this should be mergeable. I've added the `__SYCL_NONCONST_FUNCTOR__` as requested. I have also hooked up the `-sycl-std=` compiler driver flag so that it will pass `DCL_SYCL_LANGUAGE_VERSION=121` if SYCL 1.2.1 is chosen or `DCL_SYCL_LANGUAGE_VERSION=2020` if SYCL 2020 is chosen. In SYCL 2020 kernel functions are now const and passed by reference. In this PR the existing API constant CL_SYCL_LANGUAGE_VERSION now determine another constant ( __SYCL_NONCONST_FUNCTOR__ ) that makes the kernel functions be constant references (or not). In the Clang Front end, both by-reference and by-value passing of the kernel functions is supported, and a diagnostic emitted if that is mismatched. The `sycl-std` flag now supports `2020` as a valid option. And the `sycl-std` flag will pass `-DCL_SYCL_LANGUAGE_VERSION=` to match its election. Tests have been added to confirm that both kernel passings are supported and that the sycl-std flag is working correctly. Additionally, there are a lot of FE tests that employ mock kernels, these have been updated to the SYCL 2020 convention avoiding the deferred maintenance. Signed-off-by: Chris Perkins <[email protected]>
1 parent 93e1387 commit 1dbc358

File tree

139 files changed

+568
-575
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

139 files changed

+568
-575
lines changed

clang/include/clang/Basic/DiagnosticGroups.td

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1133,7 +1133,9 @@ def OpenMP : DiagGroup<"openmp", [
11331133
]>;
11341134

11351135
// SYCL warnings
1136-
def SyclStrict : DiagGroup<"sycl-strict">;
1136+
def Sycl2017Compat : DiagGroup<"sycl-2017-compat">;
1137+
def Sycl2020Compat : DiagGroup<"sycl-2020-compat">;
1138+
def SyclStrict : DiagGroup<"sycl-strict", [ Sycl2017Compat, Sycl2020Compat]>;
11371139
def SyclTarget : DiagGroup<"sycl-target">;
11381140

11391141
// Backend warnings.

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11026,6 +11026,12 @@ def err_sycl_invalid_property_list_template_param : Error<
1102611026
"%select{property_list|property_list pack argument|buffer_location}0 "
1102711027
"template parameter must be a "
1102811028
"%select{parameter pack|type|non-negative integer}1">;
11029+
def warn_sycl_pass_by_value_deprecated
11030+
: Warning<"Passing kernel functions by value is deprecated in SYCL 2020">,
11031+
InGroup<Sycl2020Compat>;
11032+
def warn_sycl_pass_by_reference_future
11033+
: Warning<"Passing of kernel functions by reference is a SYCL 2020 extension">,
11034+
InGroup<Sycl2017Compat>;
1102911035
def warn_sycl_attibute_function_raw_ptr
1103011036
: Warning<"SYCL 1.2.1 specification does not allow %0 attribute applied "
1103111037
"to a function with a raw pointer "

clang/lib/Frontend/CompilerInvocation.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2587,6 +2587,7 @@ static void ParseLangArgs(LangOptions &Opts, ArgList &Args, InputKind IK,
25872587
if (const Arg *A = Args.getLastArg(OPT_sycl_std_EQ)) {
25882588
Opts.SYCLVersion = llvm::StringSwitch<unsigned>(A->getValue())
25892589
.Cases("2017", "1.2.1", "121", "sycl-1.2.1", 2017)
2590+
.Case("2020", 2020)
25902591
.Default(0U);
25912592

25922593
if (Opts.SYCLVersion == 0U) {

clang/lib/Frontend/InitPreprocessor.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -463,8 +463,13 @@ static void InitializeStandardPredefinedMacros(const TargetInfo &TI,
463463

464464
if (LangOpts.SYCL) {
465465
// SYCL Version is set to a value when building SYCL applications
466-
if (LangOpts.SYCLVersion == 2017)
466+
if (LangOpts.SYCLVersion == 2017) {
467467
Builder.defineMacro("CL_SYCL_LANGUAGE_VERSION", "121");
468+
Builder.defineMacro("SYCL_LANGUAGE_VERSION", "201707");
469+
} else if (LangOpts.SYCLVersion == 2020) {
470+
Builder.defineMacro("SYCL_LANGUAGE_VERSION", "202001");
471+
}
472+
468473
if (LangOpts.SYCLValueFitInMaxInt)
469474
Builder.defineMacro("__SYCL_ID_QUERIES_FIT_IN_INT__", "1");
470475
}

clang/lib/Sema/SemaSYCL.cpp

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -707,7 +707,14 @@ getKernelInvocationKind(FunctionDecl *KernelCallerFunc) {
707707

708708
static const CXXRecordDecl *getKernelObjectType(FunctionDecl *Caller) {
709709
assert(Caller->getNumParams() > 0 && "Insufficient kernel parameters");
710-
return Caller->getParamDecl(0)->getType()->getAsCXXRecordDecl();
710+
711+
QualType KernelParamTy = Caller->getParamDecl(0)->getType();
712+
// In SYCL 2020 kernels are now passed by reference.
713+
if (KernelParamTy->isReferenceType())
714+
return KernelParamTy->getPointeeCXXRecordDecl();
715+
716+
// SYCL 1.2.1
717+
return KernelParamTy->getAsCXXRecordDecl();
711718
}
712719

713720
/// Creates a kernel parameter descriptor
@@ -2248,6 +2255,18 @@ void Sema::CheckSYCLKernelCall(FunctionDecl *KernelFunc, SourceRange CallLoc,
22482255

22492256
SyclKernelFieldChecker FieldChecker(*this);
22502257
SyclKernelUnionChecker UnionChecker(*this);
2258+
// check that calling kernel conforms to spec
2259+
QualType KernelParamTy = KernelFunc->getParamDecl(0)->getType();
2260+
if (KernelParamTy->isReferenceType()) {
2261+
// passing by reference, so emit warning if not using SYCL 2020
2262+
if (LangOpts.SYCLVersion < 2020)
2263+
Diag(KernelFunc->getLocation(), diag::warn_sycl_pass_by_reference_future);
2264+
} else {
2265+
// passing by value. emit warning if using SYCL 2020 or greater
2266+
if (LangOpts.SYCLVersion > 2017)
2267+
Diag(KernelFunc->getLocation(), diag::warn_sycl_pass_by_value_deprecated);
2268+
}
2269+
22512270
KernelObjVisitor Visitor{*this};
22522271
DiagnosingSYCLKernel = true;
22532272
Visitor.VisitRecordBases(KernelObj, FieldChecker, UnionChecker);

clang/test/CodeGenSYCL/Inputs/sycl.hpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -271,26 +271,26 @@ class spec_constant {
271271

272272
#define ATTR_SYCL_KERNEL __attribute__((sycl_kernel))
273273
template <typename KernelName = auto_name, typename KernelType>
274-
ATTR_SYCL_KERNEL void kernel_single_task(KernelType kernelFunc) {
274+
ATTR_SYCL_KERNEL void kernel_single_task(const KernelType &kernelFunc) {
275275
kernelFunc();
276276
}
277277

278278
template <typename KernelName, typename KernelType, int Dims>
279279
ATTR_SYCL_KERNEL void
280-
kernel_parallel_for(KernelType KernelFunc) {
280+
kernel_parallel_for(const KernelType &KernelFunc) {
281281
KernelFunc(id<Dims>());
282282
}
283283

284284
template <typename KernelName, typename KernelType, int Dims>
285285
ATTR_SYCL_KERNEL void
286-
kernel_parallel_for_work_group(KernelType KernelFunc) {
286+
kernel_parallel_for_work_group(const KernelType &KernelFunc) {
287287
KernelFunc(group<Dims>());
288288
}
289289

290290
class handler {
291291
public:
292292
template <typename KernelName = auto_name, typename KernelType, int Dims>
293-
void parallel_for(range<Dims> numWorkItems, KernelType kernelFunc) {
293+
void parallel_for(range<Dims> numWorkItems, const KernelType &kernelFunc) {
294294
using NameT = typename get_kernel_name_t<KernelName, KernelType>::name;
295295
#ifdef __SYCL_DEVICE_ONLY__
296296
kernel_parallel_for<NameT, KernelType, Dims>(kernelFunc);
@@ -300,7 +300,7 @@ class handler {
300300
}
301301

302302
template <typename KernelName = auto_name, typename KernelType, int Dims>
303-
void parallel_for_work_group(range<Dims> numWorkGroups, range<Dims> WorkGroupSize, KernelType kernelFunc) {
303+
void parallel_for_work_group(range<Dims> numWorkGroups, range<Dims> WorkGroupSize, const KernelType &kernelFunc) {
304304
using NameT = typename get_kernel_name_t<KernelName, KernelType>::name;
305305
#ifdef __SYCL_DEVICE_ONLY__
306306
kernel_parallel_for_work_group<NameT, KernelType, Dims>(kernelFunc);
@@ -311,7 +311,7 @@ class handler {
311311
}
312312

313313
template <typename KernelName = auto_name, typename KernelType>
314-
void single_task(KernelType kernelFunc) {
314+
void single_task(const KernelType &kernelFunc) {
315315
using NameT = typename get_kernel_name_t<KernelName, KernelType>::name;
316316
#ifdef __SYCL_DEVICE_ONLY__
317317
kernel_single_task<NameT>(kernelFunc);

clang/test/CodeGenSYCL/address-space-cond-op.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ S foo(bool cond, S &lhs, S rhs) {
2424
}
2525

2626
template <typename name, typename Func>
27-
__attribute__((sycl_kernel)) void kernel(Func kernelFunc) {
27+
__attribute__((sycl_kernel)) void kernel(const Func &kernelFunc) {
2828
kernelFunc();
2929
}
3030

clang/test/CodeGenSYCL/address-space-new.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,13 +110,11 @@ void test() {
110110
// CHECK: call spir_func void @{{.*}}bar{{.*}}(%struct.{{.*}}.HasX addrspace(4)* align 4 dereferenceable(4) %[[SECOND]])
111111
}
112112

113-
114113
template <typename name, typename Func>
115-
__attribute__((sycl_kernel)) void kernel_single_task(Func kernelFunc) {
114+
__attribute__((sycl_kernel)) void kernel_single_task(const Func &kernelFunc) {
116115
kernelFunc();
117116
}
118117

119-
120118
int main() {
121119
kernel_single_task<class fake_kernel>([]() { test(); });
122120
return 0;

clang/test/CodeGenSYCL/address-space-of-returns.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ A ret_agg() {
2929
// CHECK: define spir_func void @{{.*}}ret_agg{{.*}}(%struct.{{.*}}.A addrspace(4)* {{.*}} %agg.result)
3030

3131
template <typename name, typename Func>
32-
__attribute__((sycl_kernel)) void kernel_single_task(Func kernelFunc) {
32+
__attribute__((sycl_kernel)) void kernel_single_task(const Func &kernelFunc) {
3333
kernelFunc();
3434
}
3535

clang/test/CodeGenSYCL/address-space-parameter-conversions.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ void usages2() {
195195
}
196196

197197
template <typename name, typename Func>
198-
__attribute__((sycl_kernel)) void kernel_single_task(Func kernelFunc) {
198+
__attribute__((sycl_kernel)) void kernel_single_task(const Func &kernelFunc) {
199199
kernelFunc();
200200
}
201201
int main() {

0 commit comments

Comments
 (0)