-
Notifications
You must be signed in to change notification settings - Fork 787
[SYCL] Prohibit taking address of non-indirectly callable functions #5151
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1568338
[SYCL] Prohibit taking address of non-indirectly callable functions
Fznamznon 35a7d71
Merge branch 'sycl' into wrong-address-taking
Fznamznon ebcc52c
Enable diagnostic only for SYCL
Fznamznon 3017bd8
Fix formatting
Fznamznon 640d162
Apply CR suggestions
Fznamznon db20ca9
Merge branch 'wrong-address-taking' of https://github.com/Fznamznon/l…
Fznamznon 40d7c5d
Reduce the test
Fznamznon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
// RUN: %clang_cc1 -fsycl-is-device -fsycl-allow-func-ptr -internal-isystem %S/Inputs -fsyntax-only -verify -sycl-std=2020 -std=c++17 %s | ||
|
||
#include "sycl.hpp" | ||
|
||
int badFoo(int P) { | ||
return P + 2; | ||
} | ||
|
||
[[intel::device_indirectly_callable]] int goodFoo(int P) { | ||
return P + 2; | ||
} | ||
|
||
SYCL_EXTERNAL float externalBadFoo(int P); | ||
[[intel::device_indirectly_callable]] unsigned externalGoodFoo(int P); | ||
|
||
sycl::queue myQueue; | ||
|
||
SYCL_EXTERNAL int runFn(int (&)(int)); | ||
SYCL_EXTERNAL int runFn1(int (*)(int)); | ||
|
||
struct ForMembers { | ||
[[intel::device_indirectly_callable]] int goodMember(int) { return 1; } | ||
int badMember(int) { return 2; } | ||
|
||
static int badStaticMember(int) { return 2; } | ||
}; | ||
|
||
template <typename Fn, typename... Args> void templateCaller(Fn F, Args... As) { | ||
F(As...); | ||
} | ||
|
||
template <auto Fn, typename... Args> void templateCaller1(Args... As) { | ||
// expected-error@+1 2{{taking address of a function not marked with 'intel::device_indirectly_callable' attribute is not allowed in SYCL device code}} | ||
Fn(As...); | ||
// expected-error@+1 2{{taking address of a function not marked with 'intel::device_indirectly_callable' attribute is not allowed in SYCL device code}} | ||
runFn(*Fn); | ||
} | ||
|
||
void basicUsage() { | ||
// expected-error@+1 {{taking address of a function not marked with 'intel::device_indirectly_callable' attribute is not allowed in SYCL device code}} | ||
int (*p)(int) = &badFoo; | ||
// expected-error@+1 {{taking address of a function not marked with 'intel::device_indirectly_callable' attribute is not allowed in SYCL device code}} | ||
int (*p2)(int) = badFoo; | ||
} | ||
|
||
template <typename T> void templatedContext() { | ||
|
||
// FIXME: this is likely not diagnosed because of a common problem among | ||
// deferred diagnostics. They don't work from templated context if the | ||
// problematic code doesn't depend on a template parameter. See | ||
// https://github.com/intel/llvm/pull/5114 for an explanation of the problem | ||
// and possible solution. | ||
int (*p)(int) = &badFoo; | ||
auto p1 = &ForMembers::badMember; | ||
|
||
// expected-note@+1 {{called by 'templatedContext<int>'}} | ||
templateCaller1<badFoo>(1); | ||
} | ||
|
||
int main() { | ||
|
||
myQueue.submit([&](sycl::handler &h) { | ||
// expected-note@#KernelSingleTaskKernelFuncCall 2{{called by 'kernel_single_task<Basic}} | ||
h.single_task<class Basic>( | ||
[=]() { | ||
// expected-error@+1 {{taking address of a function not marked with 'intel::device_indirectly_callable' attribute is not allowed in SYCL device code}} | ||
int (*p)(int) = &badFoo; | ||
// expected-error@+1 {{taking address of a function not marked with 'intel::device_indirectly_callable' attribute is not allowed in SYCL device code}} | ||
int (*p2)(int) = badFoo; | ||
|
||
// OK | ||
int (*p3)(int) = &goodFoo; | ||
int (*p4)(int) = goodFoo; | ||
|
||
// expected-error@+1 {{taking address of a function not marked with 'intel::device_indirectly_callable' attribute is not allowed in SYCL device code}} | ||
auto p5 = &externalBadFoo; | ||
auto *p6 = &externalGoodFoo; | ||
|
||
// Make sure that assignment is diagnosed correctly; | ||
int (*a)(int); | ||
// expected-error@+1 {{taking address of a function not marked with 'intel::device_indirectly_callable' attribute is not allowed in SYCL device code}} | ||
a = badFoo; | ||
// expected-error@+1 {{taking address of a function not marked with 'intel::device_indirectly_callable' attribute is not allowed in SYCL device code}} | ||
a = &badFoo; | ||
|
||
a = goodFoo; | ||
a = &goodFoo; | ||
|
||
// expected-error@+1 {{taking address of a function not marked with 'intel::device_indirectly_callable' attribute is not allowed in SYCL device code}} | ||
constexpr auto b = badFoo; | ||
// expected-error@+1 {{taking address of a function not marked with 'intel::device_indirectly_callable' attribute is not allowed in SYCL device code}} | ||
constexpr auto c = &badFoo; | ||
// expected-note@+1 {{called by 'operator()'}} | ||
basicUsage(); | ||
}); | ||
}); | ||
|
||
myQueue.submit([&](sycl::handler &h) { | ||
// expected-note@#KernelSingleTaskKernelFuncCall {{called by 'kernel_single_task<Members}} | ||
h.single_task<class Members>( | ||
[=]() { | ||
// expected-error@+1 {{taking address of a function not marked with 'intel::device_indirectly_callable' attribute is not allowed in SYCL device code}} | ||
auto p = &ForMembers::badMember; | ||
auto p1 = &ForMembers::goodMember; | ||
// expected-error@+1 {{taking address of a function not marked with 'intel::device_indirectly_callable' attribute is not allowed in SYCL device code}} | ||
auto *p2 = &ForMembers::badStaticMember; | ||
}); | ||
}); | ||
|
||
myQueue.submit([&](sycl::handler &h) { | ||
// expected-note@#KernelSingleTaskKernelFuncCall 2{{called by 'kernel_single_task<RunVia}} | ||
h.single_task<class RunVia>( | ||
[=]() { | ||
// expected-error@+1 {{taking address of a function not marked with 'intel::device_indirectly_callable' attribute is not allowed in SYCL device code}} | ||
int baz = runFn(badFoo); | ||
|
||
baz = runFn(goodFoo); | ||
|
||
// expected-error@+1 2{{taking address of a function not marked with 'intel::device_indirectly_callable' attribute is not allowed in SYCL device code}} | ||
baz = runFn1(badFoo); | ||
|
||
baz = runFn1(goodFoo); | ||
|
||
// expected-error@+1 2{{taking address of a function not marked with 'intel::device_indirectly_callable' attribute is not allowed in SYCL device code}} | ||
templateCaller(badFoo, 2); | ||
templateCaller(goodFoo, 1); | ||
|
||
templateCaller1<goodFoo>(1); | ||
|
||
// expected-note@+2 {{called by 'operator()'}} | ||
// expected-error@+1 {{taking address of a function not marked with 'intel::device_indirectly_callable' attribute is not allowed in SYCL device code}} | ||
templateCaller1<badFoo>(1); | ||
}); | ||
}); | ||
myQueue.submit([&](sycl::handler &h) { | ||
// expected-note@#KernelSingleTaskKernelFuncCall {{called by 'kernel_single_task<RunTemplatedContext}} | ||
h.single_task<class RunTemplatedContext>( | ||
[=]() { | ||
// expected-note@+1 {{called by 'operator()'}} | ||
templatedContext<int>(); | ||
}); | ||
}); | ||
return 0; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.