|
| 1 | +// RUN: %clang_cc1 -fsycl-is-device -fsycl-allow-func-ptr -internal-isystem %S/Inputs -fsyntax-only -verify -sycl-std=2020 -std=c++17 %s |
| 2 | + |
| 3 | +#include "sycl.hpp" |
| 4 | + |
| 5 | +int badFoo(int P) { |
| 6 | + return P + 2; |
| 7 | +} |
| 8 | + |
| 9 | +[[intel::device_indirectly_callable]] int goodFoo(int P) { |
| 10 | + return P + 2; |
| 11 | +} |
| 12 | + |
| 13 | +SYCL_EXTERNAL float externalBadFoo(int P); |
| 14 | +[[intel::device_indirectly_callable]] unsigned externalGoodFoo(int P); |
| 15 | + |
| 16 | +sycl::queue myQueue; |
| 17 | + |
| 18 | +SYCL_EXTERNAL int runFn(int (&)(int)); |
| 19 | +SYCL_EXTERNAL int runFn1(int (*)(int)); |
| 20 | + |
| 21 | +struct ForMembers { |
| 22 | + [[intel::device_indirectly_callable]] int goodMember(int) { return 1; } |
| 23 | + int badMember(int) { return 2; } |
| 24 | + |
| 25 | + static int badStaticMember(int) { return 2; } |
| 26 | +}; |
| 27 | + |
| 28 | +template <typename Fn, typename... Args> void templateCaller(Fn F, Args... As) { |
| 29 | + F(As...); |
| 30 | +} |
| 31 | + |
| 32 | +template <auto Fn, typename... Args> void templateCaller1(Args... As) { |
| 33 | + // expected-error@+1 2{{taking address of a function not marked with 'intel::device_indirectly_callable' attribute is not allowed in SYCL device code}} |
| 34 | + Fn(As...); |
| 35 | + // expected-error@+1 2{{taking address of a function not marked with 'intel::device_indirectly_callable' attribute is not allowed in SYCL device code}} |
| 36 | + runFn(*Fn); |
| 37 | +} |
| 38 | + |
| 39 | +void basicUsage() { |
| 40 | + // expected-error@+1 {{taking address of a function not marked with 'intel::device_indirectly_callable' attribute is not allowed in SYCL device code}} |
| 41 | + int (*p)(int) = &badFoo; |
| 42 | + // expected-error@+1 {{taking address of a function not marked with 'intel::device_indirectly_callable' attribute is not allowed in SYCL device code}} |
| 43 | + int (*p2)(int) = badFoo; |
| 44 | +} |
| 45 | + |
| 46 | +template <typename T> void templatedContext() { |
| 47 | + |
| 48 | + // FIXME: this is likely not diagnosed because of a common problem among |
| 49 | + // deferred diagnostics. They don't work from templated context if the |
| 50 | + // problematic code doesn't depend on a template parameter. See |
| 51 | + // https://github.com/intel/llvm/pull/5114 for an explanation of the problem |
| 52 | + // and possible solution. |
| 53 | + int (*p)(int) = &badFoo; |
| 54 | + auto p1 = &ForMembers::badMember; |
| 55 | + |
| 56 | + // expected-note@+1 {{called by 'templatedContext<int>'}} |
| 57 | + templateCaller1<badFoo>(1); |
| 58 | +} |
| 59 | + |
| 60 | +int main() { |
| 61 | + |
| 62 | + myQueue.submit([&](sycl::handler &h) { |
| 63 | + // expected-note@#KernelSingleTaskKernelFuncCall 2{{called by 'kernel_single_task<Basic}} |
| 64 | + h.single_task<class Basic>( |
| 65 | + [=]() { |
| 66 | + // expected-error@+1 {{taking address of a function not marked with 'intel::device_indirectly_callable' attribute is not allowed in SYCL device code}} |
| 67 | + int (*p)(int) = &badFoo; |
| 68 | + // expected-error@+1 {{taking address of a function not marked with 'intel::device_indirectly_callable' attribute is not allowed in SYCL device code}} |
| 69 | + int (*p2)(int) = badFoo; |
| 70 | + |
| 71 | + // OK |
| 72 | + int (*p3)(int) = &goodFoo; |
| 73 | + int (*p4)(int) = goodFoo; |
| 74 | + |
| 75 | + // expected-error@+1 {{taking address of a function not marked with 'intel::device_indirectly_callable' attribute is not allowed in SYCL device code}} |
| 76 | + auto p5 = &externalBadFoo; |
| 77 | + auto *p6 = &externalGoodFoo; |
| 78 | + |
| 79 | + // Make sure that assignment is diagnosed correctly; |
| 80 | + int (*a)(int); |
| 81 | + // expected-error@+1 {{taking address of a function not marked with 'intel::device_indirectly_callable' attribute is not allowed in SYCL device code}} |
| 82 | + a = badFoo; |
| 83 | + // expected-error@+1 {{taking address of a function not marked with 'intel::device_indirectly_callable' attribute is not allowed in SYCL device code}} |
| 84 | + a = &badFoo; |
| 85 | + |
| 86 | + a = goodFoo; |
| 87 | + a = &goodFoo; |
| 88 | + |
| 89 | + // expected-error@+1 {{taking address of a function not marked with 'intel::device_indirectly_callable' attribute is not allowed in SYCL device code}} |
| 90 | + constexpr auto b = badFoo; |
| 91 | + // expected-error@+1 {{taking address of a function not marked with 'intel::device_indirectly_callable' attribute is not allowed in SYCL device code}} |
| 92 | + constexpr auto c = &badFoo; |
| 93 | + // expected-note@+1 {{called by 'operator()'}} |
| 94 | + basicUsage(); |
| 95 | + }); |
| 96 | + }); |
| 97 | + |
| 98 | + myQueue.submit([&](sycl::handler &h) { |
| 99 | + // expected-note@#KernelSingleTaskKernelFuncCall {{called by 'kernel_single_task<Members}} |
| 100 | + h.single_task<class Members>( |
| 101 | + [=]() { |
| 102 | + // expected-error@+1 {{taking address of a function not marked with 'intel::device_indirectly_callable' attribute is not allowed in SYCL device code}} |
| 103 | + auto p = &ForMembers::badMember; |
| 104 | + auto p1 = &ForMembers::goodMember; |
| 105 | + // expected-error@+1 {{taking address of a function not marked with 'intel::device_indirectly_callable' attribute is not allowed in SYCL device code}} |
| 106 | + auto *p2 = &ForMembers::badStaticMember; |
| 107 | + }); |
| 108 | + }); |
| 109 | + |
| 110 | + myQueue.submit([&](sycl::handler &h) { |
| 111 | + // expected-note@#KernelSingleTaskKernelFuncCall 2{{called by 'kernel_single_task<RunVia}} |
| 112 | + h.single_task<class RunVia>( |
| 113 | + [=]() { |
| 114 | + // expected-error@+1 {{taking address of a function not marked with 'intel::device_indirectly_callable' attribute is not allowed in SYCL device code}} |
| 115 | + int baz = runFn(badFoo); |
| 116 | + |
| 117 | + baz = runFn(goodFoo); |
| 118 | + |
| 119 | + // expected-error@+1 2{{taking address of a function not marked with 'intel::device_indirectly_callable' attribute is not allowed in SYCL device code}} |
| 120 | + baz = runFn1(badFoo); |
| 121 | + |
| 122 | + baz = runFn1(goodFoo); |
| 123 | + |
| 124 | + // expected-error@+1 2{{taking address of a function not marked with 'intel::device_indirectly_callable' attribute is not allowed in SYCL device code}} |
| 125 | + templateCaller(badFoo, 2); |
| 126 | + templateCaller(goodFoo, 1); |
| 127 | + |
| 128 | + templateCaller1<goodFoo>(1); |
| 129 | + |
| 130 | + // expected-note@+2 {{called by 'operator()'}} |
| 131 | + // expected-error@+1 {{taking address of a function not marked with 'intel::device_indirectly_callable' attribute is not allowed in SYCL device code}} |
| 132 | + templateCaller1<badFoo>(1); |
| 133 | + }); |
| 134 | + }); |
| 135 | + myQueue.submit([&](sycl::handler &h) { |
| 136 | + // expected-note@#KernelSingleTaskKernelFuncCall {{called by 'kernel_single_task<RunTemplatedContext}} |
| 137 | + h.single_task<class RunTemplatedContext>( |
| 138 | + [=]() { |
| 139 | + // expected-note@+1 {{called by 'operator()'}} |
| 140 | + templatedContext<int>(); |
| 141 | + }); |
| 142 | + }); |
| 143 | + return 0; |
| 144 | +} |
0 commit comments