Skip to content

Commit 931fd76

Browse files
committed
Add additional test cases and address review comments
1 parent 4db4101 commit 931fd76

File tree

6 files changed

+77
-43
lines changed

6 files changed

+77
-43
lines changed

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12882,11 +12882,10 @@ def err_sycl_special_type_num_init_method : Error<
1288212882
// SYCL external attribute diagnostics
1288312883
def err_sycl_attribute_invalid_linkage : Error<
1288412884
"'sycl_external' can only be applied to functions with external linkage">;
12885-
def err_sycl_attribute_avoid_main : Error<
12885+
def err_sycl_attribute_invalid_main : Error<
1288612886
"'sycl_external' cannot be applied to the 'main' function">;
12887-
def err_sycl_attribute_avoid_deleted_function
12888-
: Error<"'sycl_external' cannot be applied to an explicitly deleted "
12889-
"function">;
12887+
def err_sycl_attribute_invalid_deleted_function : Error<
12888+
"'sycl_external' cannot be applied to an explicitly deleted function">;
1289012889

1289112890
// SYCL kernel entry point diagnostics
1289212891
def err_sycl_entry_point_invalid : Error<

clang/lib/Sema/SemaDecl.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4085,10 +4085,10 @@ bool Sema::MergeFunctionDecl(FunctionDecl *New, NamedDecl *&OldD, Scope *S,
40854085
}
40864086

40874087
// SYCL 2020 section 5.10.1, "SYCL functions and member functions linkage":
4088-
// When a function is declared with sycl_external, that attribute must be
4088+
// When a function is declared with SYCL_EXTERNAL, that macro must be
40894089
// used on the first declaration of that function in the translation unit.
40904090
// Redeclarations of the function in the same translation unit may
4091-
// optionally use sycl_external, but this is not required.
4091+
// optionally use SYCL_EXTERNAL, but this is not required.
40924092
if (LangOpts.SYCLIsDevice) {
40934093
const SYCLExternalAttr *SEA = New->getAttr<SYCLExternalAttr>();
40944094
if (SEA && !Old->hasAttr<SYCLExternalAttr>()) {
@@ -12459,7 +12459,7 @@ void Sema::CheckMain(FunctionDecl *FD, const DeclSpec &DS) {
1245912459

1246012460
if (getLangOpts().SYCLIsDevice) {
1246112461
if (FD->hasAttr<SYCLExternalAttr>()) {
12462-
Diag(FD->getLocation(), diag::err_sycl_attribute_avoid_main);
12462+
Diag(FD->getLocation(), diag::err_sycl_attribute_invalid_main);
1246312463
FD->setInvalidDecl();
1246412464
return;
1246512465
}

clang/lib/Sema/SemaSYCL.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ void SemaSYCL::CheckSYCLExternalFunctionDecl(FunctionDecl *FD) {
257257
}
258258
if (FD->isDeletedAsWritten()) {
259259
Diag(SEAttr->getLocation(),
260-
diag::err_sycl_attribute_avoid_deleted_function);
260+
diag::err_sycl_attribute_invalid_deleted_function);
261261
return;
262262
}
263263
}
Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
11
// RUN: %clang_cc1 -fsycl-is-device -fsyntax-only -verify %s
22

3-
// expected-error@+1{{'clang::sycl_external' attribute only applies to functions}}
4-
[[clang::sycl_external]] int a;
3+
// expected-error@+1{{'clang::sycl_external' attribute takes no arguments}}
4+
[[clang::sycl_external(3)]] void bar() {}
55

6+
// FIXME: this case should be diagnosed too
7+
[[clang::sycl_external()]] void bad1();
68

7-
// expected-error@+2{{'clang::sycl_external' attribute only applies to functions}}
8-
struct s {
9-
[[clang::sycl_external]] int b;
10-
};
9+
// expected-error@+1{{expected expression}}
10+
[[clang::sycl_external(,)]] void bad2();
1111

1212
// expected-error@+1{{'clang::sycl_external' attribute takes no arguments}}
13-
[[clang::sycl_external(3)]] void bar() {}
14-
15-
// FIXME: The first declaration of a function is required to have the attribute.
16-
// The attribute may be optionally present on subsequent declarations
17-
int foo(int c);
13+
[[clang::sycl_external(3)]] void bad3();
1814

19-
[[clang::sycl_external]] void foo();
15+
// expected-error@+1{{expected expression}}
16+
[[clang::sycl_external(4,)]] void bad4();
Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
// RUN: %clang_cc1 -fsyntax-only -verify %s
22

3+
// These tests validate that the sycl_external attribute is ignored when SYCL
4+
// support is not enabled.
5+
36
// expected-warning@+1{{'clang::sycl_external' attribute ignored}}
47
[[clang::sycl_external]] void bar() {}
58

69
// expected-warning@+1{{'clang::sycl_external' attribute ignored}}
710
[[clang::sycl_external]] int a;
811

912
// expected-warning@+2{{'clang::sycl_external' attribute ignored}}
10-
namespace not_sycl {
11-
[[clang::sycl_external]] void foo() {}
12-
}
13+
template<typename T>
14+
[[clang::sycl_external]] void ft(T) {}
15+
template void ft(int);
Lines changed: 55 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,73 @@
1-
// RUN: %clang_cc1 -fsycl-is-device -fsyntax-only -verify %s
1+
// RUN: %clang_cc1 -fsycl-is-device -std=c++17 -fsyntax-only -verify -DCPP17 %s
22
// RUN: %clang_cc1 -fsycl-is-device -std=c++20 -fsyntax-only -verify -DCPP20 %s
3-
// Semantic tests for sycl_external attribute
43

5-
[[clang::sycl_external]] // expected-error {{'sycl_external' can only be applied to functions with external linkage}}
4+
// Semantic tests for the sycl_external attribute.
5+
6+
// expected-error@+1{{'sycl_external' can only be applied to functions with external linkage}}
7+
[[clang::sycl_external]]
68
static void func1() {}
79

10+
// expected-error@+2{{'sycl_external' can only be applied to functions with external linkage}}
811
namespace {
9-
[[clang::sycl_external]] // expected-error {{'sycl_external' can only be applied to functions with external linkage}}
12+
[[clang::sycl_external]]
1013
void func2() {}
1114

1215
struct UnnX {};
1316
}
1417

15-
[[clang::sycl_external]] // expected-error {{'sycl_external' can only be applied to functions with external linkage}}
16-
void func4(UnnX) {}
18+
// expected-error@+2{{'sycl_external' can only be applied to functions with external linkage}}
19+
namespace { struct S4 {}; }
20+
[[clang::sycl_external]] void func4(S4) {}
21+
22+
// FIXME: This case is currently being diagnosed as an error because clang implements
23+
// default inheritance of attribute and explicit instantiation declaration names the
24+
// symbol that causes the instantiated specialization to have internal linkage.
25+
// expected-error@+3{{'sycl_external' can only be applied to functions with external linkage}}
26+
namespace { struct S6 {}; }
27+
template<typename>
28+
[[clang::sycl_external]] void func6() {}
29+
template void func6<S6>();
30+
// expected-note@-1{{in instantiation of function template specialization 'func6<(anonymous namespace)::S6>' requested here}}
31+
32+
// expected-error@+3 2{{'sycl_external' can only be applied to functions with external linkage}}
33+
namespace { struct S7 {}; }
34+
template<typename>
35+
[[clang::sycl_external]] void func7();
36+
template<> void func7<S7>() {}
37+
// expected-note@-1{{in instantiation of function template specialization 'func7<(anonymous namespace)::S7>' requested here}}
38+
39+
namespace { struct S8 {}; }
40+
template<typename>
41+
void func8();
42+
template<> [[clang::sycl_external]] void func8<S8>() {}
43+
// expected-error@-1{{'clang::sycl_external' attribute does not appear on the first declaration}}
44+
// expected-note@-2{{previous declaration is here}}
1745

1846
// The first declaration of a SYCL external function is required to have this attribute.
19-
int foo(); // expected-note {{previous declaration is here}}
20-
21-
[[clang::sycl_external]] int foo(); // expected-error {{'clang::sycl_external' attribute does not appear on the first declaration}}
47+
// expected-note@+1{{previous declaration is here}}
48+
int foo();
49+
// expected-error@+1{{'clang::sycl_external' attribute does not appear on the first declaration}}
50+
[[clang::sycl_external]] int foo();
2251

2352
// Subsequent declrations of a SYCL external function may optionally specify this attribute.
2453
[[clang::sycl_external]] int boo();
25-
2654
[[clang::sycl_external]] int boo(); // OK
27-
2855
int boo(); // OK
2956

3057
class C {
3158
[[clang::sycl_external]] void member();
3259
};
3360

34-
[[clang::sycl_external]] int main() // expected-error {{'sycl_external' cannot be applied to the 'main' function}}
61+
// expected-error@+1{{'sycl_external' cannot be applied to the 'main' function}}
62+
[[clang::sycl_external]] int main()
3563
{
3664
return 0;
3765
}
3866

67+
// expected-error@+2{{'sycl_external' cannot be applied to an explicitly deleted function}}
3968
class D {
40-
[[clang::sycl_external]] void del() = delete; // expected-error {{'sycl_external' cannot be applied to an explicitly deleted function}}
69+
[[clang::sycl_external]] void del() = delete;
4170
};
42-
4371
struct NonCopyable {
4472
~NonCopyable() = delete;
4573
[[clang::sycl_external]] NonCopyable(const NonCopyable&) = default;
@@ -49,7 +77,8 @@ class A {
4977
[[clang::sycl_external]]
5078
A() {}
5179

52-
[[clang::sycl_external]] void func3() {}
80+
[[clang::sycl_external]] void mf() {}
81+
[[clang::sycl_external]] static void smf();
5382
};
5483

5584
class B {
@@ -59,12 +88,18 @@ class B {
5988
[[clang::sycl_external]] virtual void bar() = 0;
6089
};
6190

62-
[[clang::sycl_external]] int *func0() { return nullptr; }
63-
64-
[[clang::sycl_external]] void func2(int *) {}
65-
6691
[[clang::sycl_external]] constexpr int square(int x);
6792

68-
#ifdef CPP20
93+
// Devices that do not support the generic address space shall not specify
94+
// a raw pointer or reference type as the return type or as a parameter type.
95+
[[clang::sycl_external]] int *fun0();
96+
[[clang::sycl_external]] int &fun1();
97+
[[clang::sycl_external]] int &&fun2();
98+
[[clang::sycl_external]] void fun3(int *);
99+
[[clang::sycl_external]] void fun4(int &);
100+
[[clang::sycl_external]] void fun5(int &&);
101+
102+
#if CPP20
69103
[[clang::sycl_external]] consteval int func();
70104
#endif
105+

0 commit comments

Comments
 (0)