Skip to content

Commit b10bdbb

Browse files
jyu2-gitbader
authored andcommitted
[SYCL] Suppress warning for use of dllimport/dllexport attribute when
compile with -fsycl. The change do following: 1> Suppress warning for "_declspec attribute 'dllexport' is not supported" when run with -fsycl 2> Emit error when import function is called in the sycl kernel. For example: for the test following: bash-4.2$ clang -cc1 -triple spir64-unknown-windows-sycldevice -fsycl-is-device -fms-extensions sycl-dllimport-dllexport.cpp sycl-dllimport-dllexport.cpp:10:48: error: SYCL kernel cannot call a dllimport function kernel_single_task<class fake_kernel>([]() { bar(); }); ^ sycl-dllimport-dllexport.cpp:1:28: note: 'bar' declared here int __declspec(dllimport) bar(); ^ 1 error generated. === test == int __declspec(dllimport) bar(); template <typename name, typename Func> __attribute__((sycl_kernel)) void kernel_single_task(Func kernelFunc) { kernelFunc(); } int main() { t status kernel_single_task<class fake_kernel>([]() { bar(); }); return 0; } Signed-off-by: Yu <[email protected]>
1 parent d9e8467 commit b10bdbb

File tree

5 files changed

+90
-2
lines changed

5 files changed

+90
-2
lines changed

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9793,6 +9793,7 @@ def err_sycl_restrict : Error<
97939793
"|allocate storage"
97949794
"|use inline assembly"
97959795
"|have a class with a virtual function table"
9796+
"|call a dllimport function"
97969797
"|call a variadic function}0">;
97979798
def err_sycl_virtual_types : Error<
97989799
"No class with a vtable can be used in a SYCL kernel or any code included in the kernel">;

clang/include/clang/Sema/Sema.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11426,6 +11426,7 @@ class Sema {
1142611426
KernelAllocateStorage,
1142711427
KernelUseAssembly,
1142811428
KernelHavePolymorphicClass,
11429+
KernelCallDllimportFunction,
1142911430
KernelCallVariadicFunction
1143011431
};
1143111432
DeviceDiagBuilder SYCLDiagIfDeviceCode(SourceLocation Loc, unsigned DiagID);

clang/lib/Sema/SemaDeclAttr.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2066,7 +2066,10 @@ bool Sema::CheckAttrNoArgs(const ParsedAttr &Attrs) {
20662066

20672067
bool Sema::CheckAttrTarget(const ParsedAttr &AL) {
20682068
// Check whether the attribute is valid on the current target.
2069-
if (!AL.existsInTarget(Context.getTargetInfo())) {
2069+
const TargetInfo *Aux = Context.getAuxTargetInfo();
2070+
if (!(AL.existsInTarget(Context.getTargetInfo()) ||
2071+
(Context.getLangOpts().SYCLIsDevice &&
2072+
Aux && AL.existsInTarget(*Aux)))) {
20702073
Diag(AL.getLoc(), diag::warn_unknown_attribute_ignored) << AL;
20712074
AL.setInvalid();
20722075
return true;
@@ -6935,8 +6938,11 @@ static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
69356938
// Unknown attributes are automatically warned on. Target-specific attributes
69366939
// which do not apply to the current target architecture are treated as
69376940
// though they were unknown attributes.
6941+
const TargetInfo *Aux = S.Context.getAuxTargetInfo();
69386942
if (AL.getKind() == ParsedAttr::UnknownAttribute ||
6939-
!AL.existsInTarget(S.Context.getTargetInfo())) {
6943+
!(AL.existsInTarget(S.Context.getTargetInfo()) ||
6944+
(S.Context.getLangOpts().SYCLIsDevice &&
6945+
Aux && AL.existsInTarget(*Aux)))) {
69406946
S.Diag(AL.getLoc(),
69416947
AL.isDeclspecAttribute()
69426948
? (unsigned)diag::warn_unhandled_ms_attribute_ignored

clang/lib/Sema/SemaSYCL.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,18 @@ class MarkDeviceFunction : public RecursiveASTVisitor<MarkDeviceFunction> {
119119
SemaRef.addSyclDeviceDecl(Def);
120120
}
121121
}
122+
if (auto const *FD = dyn_cast<FunctionDecl>(Callee)) {
123+
//FIXME: We need check all target specified attributes for error if that
124+
//function with attribute can not be called from sycl kernel. The info
125+
//is in ParsedAttr. We don't have to map from Attr to ParsedAttr
126+
//currently. Erich is currently working on that in LLVM, once that is
127+
//committed we need to change this".
128+
if (FD->hasAttr<DLLImportAttr>()) {
129+
SemaRef.Diag(e->getExprLoc(), diag::err_sycl_restrict)
130+
<< Sema::KernelCallDllimportFunction;
131+
SemaRef.Diag(FD->getLocation(), diag::note_callee_decl) << FD;
132+
}
133+
}
122134
} else if (!SemaRef.getLangOpts().SYCLAllowFuncPtr &&
123135
!e->isTypeDependent())
124136
SemaRef.Diag(e->getExprLoc(), diag::err_sycl_restrict)
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// RUN: %clang_cc1 -triple spir64-unknown-linux-sycldevice -fms-extensions \
2+
// RUN: -aux-triple x86_64-unknown-linux-gnu -fsycl-is-device -fsyntax-only \
3+
// RUN: -DWARNCHECK %s -o /dev/null 2>&1 | FileCheck %s
4+
// check random triple aux-triple with sycl-device
5+
6+
// RUN: %clang_cc1 -triple spir64-unknown-windows-sycldevice -fsyntax-only \
7+
// RUN: -fms-extensions -DWARNCHECK %s -o /dev/null 2>&1 | FileCheck %s
8+
// check without -aux-triple but sycl-device
9+
10+
// RUN: %clang_cc1 -triple spir64-unknown-windows-sycldevice -fsycl-is-device \
11+
// RUN: -aux-triple x86_64-pc-windows-msvc -fms-extensions -fsyntax-only \
12+
// RUN: -DWARNCHECK %s -o /dev/null 2>&1 | FileCheck %s --check-prefixes CHECKALL
13+
// check -aux-tripe without sycl-device
14+
15+
// RUN: %clang_cc1 -triple spir64-unknown-windows-sycldevice -fsyntax-only \
16+
// RUN: -aux-triple x86_64-pc-windows-msvc -fsycl-is-device -fms-extensions \
17+
// RUN: -verify %s
18+
// check error message when dllimport function gets called in sycl-kernel cdoe
19+
20+
#if defined(WARNCHECK)
21+
// CHECK: warning: __declspec attribute 'dllexport' is not supported
22+
int __declspec(dllexport) foo(int a) {
23+
return a;
24+
}
25+
26+
// CHECK: warning: __declspec attribute 'dllimport' is not supported
27+
int __declspec(dllimport) bar();
28+
29+
30+
// CHECK: warning: unknown attribute 'dllimport' ignored
31+
int [[dllimport]]xoo();
32+
33+
// CHECKALL: warning: unknown attribute 'dllimport' ignored
34+
int zoo() __attribute__((dllimport));
35+
36+
#else
37+
38+
// emit error if dllimport function is called in sycl kernel
39+
int __declspec(dllexport) foo(int a) {
40+
return a;
41+
}
42+
// expected-note@+1 {{'bar' declared here}}
43+
int __declspec(dllimport) bar();
44+
// expected-note@+2 {{previous attribute is here}}
45+
// expected-note@+1 {{previous declaration is here}}
46+
int __declspec(dllimport) foobar();
47+
int foobar() // expected-warning {{'foobar' redeclared without 'dllimport' attribute: previous 'dllimport' ignored}}
48+
{
49+
return 10;
50+
}
51+
52+
template <typename name, typename Func>
53+
__attribute__((sycl_kernel)) void kernel_single_task(Func kernelFunc) {
54+
kernelFunc();
55+
}
56+
57+
int main() {
58+
foo(10); // expected-no-error
59+
bar(); // expected-no-error
60+
kernel_single_task<class fake_kernel>([]() {
61+
foo(10);// expected-no-error
62+
bar(); // expected-error {{SYCL kernel cannot call a dllimport function}}
63+
foobar(); // expected-no-error
64+
});
65+
bar(); // expected-no-error
66+
return 0;
67+
}
68+
#endif

0 commit comments

Comments
 (0)