Skip to content

Commit 6cfc3ad

Browse files
authored
[SYCL] Fix detection of free function calls (#3003)
This change simplifies the code to detect calls to "free functions", i.e., functions such as this_item(). The existing code does not work in all cases to detect the Kernel function, while the corrected code uses an already known Kernel function. Signed-off-by: rdeodhar <[email protected]>
1 parent 4c51add commit 6cfc3ad

File tree

2 files changed

+25
-11
lines changed

2 files changed

+25
-11
lines changed

clang/lib/Sema/SemaSYCL.cpp

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2705,23 +2705,18 @@ class SyclKernelIntHeaderCreator : public SyclKernelFieldHandler {
27052705

27062706
// Sets a flag if the kernel is a parallel_for that calls the
27072707
// free function API "this_item".
2708-
void setThisItemIsCalled(const CXXRecordDecl *KernelObj,
2709-
FunctionDecl *KernelFunc) {
2708+
void setThisItemIsCalled(FunctionDecl *KernelFunc) {
27102709
if (getKernelInvocationKind(KernelFunc) != InvokeParallelFor)
27112710
return;
27122711

2713-
const CXXMethodDecl *WGLambdaFn = getOperatorParens(KernelObj);
2714-
if (!WGLambdaFn)
2715-
return;
2716-
27172712
// The call graph for this translation unit.
27182713
CallGraph SYCLCG;
27192714
SYCLCG.addToCallGraph(SemaRef.getASTContext().getTranslationUnitDecl());
27202715
using ChildParentPair =
27212716
std::pair<const FunctionDecl *, const FunctionDecl *>;
27222717
llvm::SmallPtrSet<const FunctionDecl *, 16> Visited;
27232718
llvm::SmallVector<ChildParentPair, 16> WorkList;
2724-
WorkList.push_back({WGLambdaFn, nullptr});
2719+
WorkList.push_back({KernelFunc, nullptr});
27252720

27262721
while (!WorkList.empty()) {
27272722
const FunctionDecl *FD = WorkList.back().first;
@@ -2772,7 +2767,7 @@ class SyclKernelIntHeaderCreator : public SyclKernelFieldHandler {
27722767
bool IsSIMDKernel = isESIMDKernelType(KernelObj);
27732768
Header.startKernel(Name, NameType, StableName, KernelObj->getLocation(),
27742769
IsSIMDKernel);
2775-
setThisItemIsCalled(KernelObj, KernelFunc);
2770+
setThisItemIsCalled(KernelFunc);
27762771
}
27772772

27782773
bool handleSyclAccessorType(const CXXRecordDecl *RD,

clang/test/CodeGenSYCL/parallel_for_this_item.cpp

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
// CHECK-NEXT: "_ZTSZZ4mainENK3$_0clERN2cl4sycl7handlerEE3EMU",
1515
// CHECK-NEXT: "_ZTSZZ4mainENK3$_0clERN2cl4sycl7handlerEE3OWL",
1616
// CHECK-NEXT: "_ZTSZZ4mainENK3$_0clERN2cl4sycl7handlerEE3RAT",
17-
// CHECK-NEXT: "_ZTSZZ4mainENK3$_0clERN2cl4sycl7handlerEE3FOX"
17+
// CHECK-NEXT: "_ZTSZZ4mainENK3$_0clERN2cl4sycl7handlerEE3FOX",
18+
// CHECK-NEXT: "_ZTSZZ4mainENK3$_0clERN2cl4sycl7handlerEE3BEE"
1819
// CHECK-NEXT: };
1920

2021
// CHECK:template <> struct KernelInfo<class GNU> {
@@ -97,6 +98,22 @@
9798
// CHECK-NEXT: __SYCL_DLL_LOCAL
9899
// CHECK-NEXT: static constexpr bool callsAnyThisFreeFunction() { return 1; }
99100
// CHECK-NEXT:};
101+
// CHECK-NEXT:template <> struct KernelInfo<class BEE> {
102+
// CHECK-NEXT: __SYCL_DLL_LOCAL
103+
// CHECK-NEXT: static constexpr const char* getName() { return "_ZTSZZ4mainENK3$_0clERN2cl4sycl7handlerEE3BEE"; }
104+
// CHECK-NEXT: __SYCL_DLL_LOCAL
105+
// CHECK-NEXT: static constexpr unsigned getNumParams() { return 0; }
106+
// CHECK-NEXT: __SYCL_DLL_LOCAL
107+
// CHECK-NEXT: static constexpr const kernel_param_desc_t& getParamDesc(unsigned i) {
108+
// CHECK-NEXT: return kernel_signatures[i+0];
109+
// CHECK-NEXT: }
110+
// CHECK-NEXT: __SYCL_DLL_LOCAL
111+
// CHECK-NEXT: static constexpr bool isESIMD() { return 0; }
112+
// CHECK-NEXT: __SYCL_DLL_LOCAL
113+
// CHECK-NEXT: static constexpr bool callsThisItem() { return 1; }
114+
// CHECK-NEXT: __SYCL_DLL_LOCAL
115+
// CHECK-NEXT: static constexpr bool callsAnyThisFreeFunction() { return 1; }
116+
// CHECK-NEXT:};
100117

101118
#include "sycl.hpp"
102119

@@ -135,8 +152,10 @@ int main() {
135152
cgh.parallel_for<class RAT>(range<1>(1), [=](id<1> I) { f(); });
136153

137154
// This kernel does not call sycl::this_item, but does call this_id
138-
cgh.parallel_for<class FOX>(range<1>(1),
139-
[=](id<1> I) { this_id<1>(); });
155+
cgh.parallel_for<class FOX>(range<1>(1), [=](id<1> I) { this_id<1>(); });
156+
157+
// This kernel calls sycl::this_item
158+
cgh.parallel_for<class BEE>(range<1>(1), [=](auto I) { this_item<1>(); });
140159
});
141160

142161
return 0;

0 commit comments

Comments
 (0)