Skip to content

Commit 2c306be

Browse files
MrSidimsbader
authored andcommitted
[SYCL] Fix forward declaration of a class inside a kernel
Don't query property of class with no definition (which are forward declared classes), that results in clang crash. Signed-off-by: Dmitry Sidorov <[email protected]>
1 parent 6b7fbd9 commit 2c306be

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

clang/lib/Sema/SemaSYCL.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,12 @@ class MarkDeviceFunction : public RecursiveASTVisitor<MarkDeviceFunction> {
350350
return true;
351351

352352
if (const auto *CRD = Ty->getAsCXXRecordDecl()) {
353+
// If the class is a forward declaration - skip it, because otherwise we
354+
// would query property of class with no definition, which results in
355+
// clang crash.
356+
if (!CRD->hasDefinition())
357+
return true;
358+
353359
if (CRD->isPolymorphic()) {
354360
SemaRef.Diag(CRD->getLocation(), diag::err_sycl_virtual_types);
355361
SemaRef.Diag(Loc.getBegin(), diag::note_sycl_used_here);

clang/test/SemaSYCL/forward-decl.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// RUN: %clang_cc1 -x c++ -DNOVIRTUAL -fsycl-is-device -std=c++11 -fsyntax-only -verify -pedantic %s
2+
// RUN: %clang_cc1 -x c++ -DVIRTUAL -fsycl-is-device -std=c++11 -fsyntax-only -verify -pedantic %s
3+
4+
template <typename name, typename Func>
5+
__attribute__((sycl_kernel)) void kernel_single_task(Func kernelFunc) {
6+
kernelFunc();
7+
}
8+
9+
int main() {
10+
#ifdef NOVIRTUAL
11+
kernel_single_task<class kernel_function_1>([]() {
12+
// expected-no-diagnostics
13+
class Foo *F;
14+
});
15+
#elif VIRTUAL
16+
kernel_single_task<class kernel_function_2>([]() {
17+
// expected-error@+2{{No class with a vtable can be used in a SYCL kernel or any code included in the kernel}}
18+
// expected-note@+1{{used here}}
19+
class Boo {
20+
public:
21+
virtual int getBoo() { return 42; }
22+
};
23+
});
24+
25+
kernel_single_task<class kernel_function_3>([]() {
26+
class Boo *B;
27+
});
28+
#endif // VIRTUAL
29+
return 0;
30+
}

0 commit comments

Comments
 (0)