Skip to content

Commit 17aac3c

Browse files
author
Erich Keane
authored
[SYCL] Update the kernel parameter rule to is-trivially-copy-construc… (#1144)
Since we really just want to be able to memcpy the type to the device, 'is-trivially-copyable' is not the correct trait. Since CWG1734, If we want to support trivially copyable types, we would be required to create 1 of 4 different mechanisms for having a type on the device (depending on the way the type is structured). Additionally, 2 of these ways require us to ALSO have the type be default constructible. This patch transitions to trivially-copy-constructible , so that we can simply memcpy from the existing one into new memory. Signed-off-by: Erich Keane <[email protected]>
1 parent 84e0657 commit 17aac3c

File tree

3 files changed

+34
-6
lines changed

3 files changed

+34
-6
lines changed

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10490,8 +10490,9 @@ def err_sycl_virtual_types : Error<
1049010490
"No class with a vtable can be used in a SYCL kernel or any code included in the kernel">;
1049110491
def note_sycl_used_here : Note<"used here">;
1049210492
def note_sycl_recursive_function_declared_here: Note<"function implemented using recursion declared here">;
10493-
def err_sycl_non_trivially_copyable_type : Error<
10494-
"kernel parameter has non-trivially copyable class/struct type %0">;
10493+
def err_sycl_non_trivially_copy_ctor_dtor_type
10494+
: Error<"kernel parameter has non-trivially %select{copy "
10495+
"constructible|destructible}0 class/struct type %1">;
1049510496
def err_sycl_non_std_layout_type : Error<
1049610497
"kernel parameter has non-standard layout class/struct type %0">;
1049710498
def err_conflicting_sycl_kernel_attributes : Error<

clang/lib/Sema/SemaSYCL.cpp

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1069,10 +1069,22 @@ static bool buildArgTys(ASTContext &Context, CXXRecordDecl *KernelObj,
10691069
continue;
10701070
}
10711071
}
1072-
if (!ArgTy.isTriviallyCopyableType(Context)) {
1072+
1073+
CXXRecordDecl *RD =
1074+
cast<CXXRecordDecl>(ArgTy->getAs<RecordType>()->getDecl());
1075+
if (!RD->hasTrivialCopyConstructor()) {
1076+
Context.getDiagnostics().Report(
1077+
Fld->getLocation(),
1078+
diag::err_sycl_non_trivially_copy_ctor_dtor_type)
1079+
<< 0 << ArgTy;
1080+
AllArgsAreValid = false;
1081+
continue;
1082+
}
1083+
if (!RD->hasTrivialDestructor()) {
10731084
Context.getDiagnostics().Report(
1074-
Fld->getLocation(), diag::err_sycl_non_trivially_copyable_type)
1075-
<< ArgTy;
1085+
Fld->getLocation(),
1086+
diag::err_sycl_non_trivially_copy_ctor_dtor_type)
1087+
<< 1 << ArgTy;
10761088
AllArgsAreValid = false;
10771089
continue;
10781090
}

clang/test/SemaSYCL/non-trivially-copyable-kernel-param.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,16 @@ struct B {
1111
B (const B& x) : i(x.i) {}
1212
};
1313

14+
struct C : A {
15+
const A C2;
16+
C() : A{0}, C2{2}{}
17+
};
18+
19+
struct D {
20+
int i;
21+
~D();
22+
};
23+
1424
template <typename Name, typename Func>
1525
__attribute__((sycl_kernel)) void kernel_single_task(Func kernelFunc) {
1626
kernelFunc();
@@ -20,9 +30,14 @@ void test() {
2030
A IamGood;
2131
IamGood.i = 0;
2232
B IamBad(1);
33+
C IamAlsoGood;
34+
D IamAlsoBad{0};
2335
kernel_single_task<class kernel_capture_refs>([=] {
2436
int a = IamGood.i;
25-
// expected-error@+1 {{kernel parameter has non-trivially copyable class/struct type}}
37+
// expected-error@+1 {{kernel parameter has non-trivially copy constructible class/struct type}}
2638
int b = IamBad.i;
39+
int c = IamAlsoGood.i;
40+
// expected-error@+1 {{kernel parameter has non-trivially destructible class/struct type}}
41+
int d = IamAlsoBad.i;
2742
});
2843
}

0 commit comments

Comments
 (0)