Skip to content

Commit d6aa11b

Browse files
Alexander Batashevbader
authored andcommitted
[SYCL] Properly take care of unnamed lambdas (#827)
When two lambdas with the same signature but different content are submitted to queue, compiler complains about duplicate names and finishes with error. This patch introduces a more correct way to mangle lambdas. Signed-off-by: Alexander Batashev <alexander.batashev@intel.com>
1 parent 644013e commit d6aa11b

File tree

3 files changed

+54
-5
lines changed

3 files changed

+54
-5
lines changed

clang/lib/Sema/SemaSYCL.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1104,8 +1104,7 @@ static bool buildArgTys(ASTContext &Context, CXXRecordDecl *KernelObj,
11041104
/// \param H the integration header object
11051105
/// \param Name kernel name
11061106
/// \param NameType type representing kernel name (first template argument
1107-
/// of
1108-
/// single_task, parallel_for, etc)
1107+
/// of single_task, parallel_for, etc)
11091108
/// \param KernelObjTy kernel object type
11101109
static void populateIntHeader(SYCLIntegrationHeader &H, const StringRef Name,
11111110
QualType NameType, CXXRecordDecl *KernelObjTy) {
@@ -1260,7 +1259,15 @@ void Sema::ConstructOpenCLKernel(FunctionDecl *KernelCallerFunc,
12601259
assert(TemplateArgs && "No template argument info");
12611260
QualType KernelNameType = TypeName::getFullyQualifiedType(
12621261
TemplateArgs->get(0).getAsType(), getASTContext(), true);
1263-
std::string Name = constructKernelName(KernelNameType, MC);
1262+
1263+
std::string Name;
1264+
// TODO SYCLIntegrationHeader also computes a unique stable name. It should
1265+
// probably lose this responsibility and only use the name provided here.
1266+
if (getLangOpts().SYCLUnnamedLambda)
1267+
Name = PredefinedExpr::ComputeName(
1268+
getASTContext(), PredefinedExpr::UniqueStableNameExpr, KernelNameType);
1269+
else
1270+
Name = constructKernelName(KernelNameType, MC);
12641271

12651272
// TODO Maybe don't emit integration header inside the Sema?
12661273
populateIntHeader(getSyclIntegrationHeader(), Name, KernelNameType, LE);

clang/test/SemaSYCL/mangle-unnamed-kernel.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ int main() {
88
return 0;
99
}
1010

11-
// CHECK: _ZTSZZ4mainENK3$_0clERN2cl4sycl7handlerEEUlvE_
12-
// CHECK: _ZTSZZ4mainENK3$_1clERN2cl4sycl7handlerEEUlvE_
11+
// CHECK: _ZTSZZ4mainENKUlRN2cl4sycl7handlerEE6->12clES2_EUlvE6->54{{.*}}
12+
// CHECK: _ZTSZZ4mainENKUlRN2cl4sycl7handlerEE7->12clES2_EUlvE7->54{{.*}}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// RUN: %clangxx -fsycl %s -o %t.out -fsycl-unnamed-lambda
2+
// RUN: %CPU_RUN_PLACEHOLDER %t.out
3+
4+
//==----- same_unnamed_kernels.cpp - SYCL kernel naming variants test ------==//
5+
//
6+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
7+
// See https://llvm.org/LICENSE.txt for license information.
8+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
9+
//
10+
//===----------------------------------------------------------------------===//
11+
12+
#include <CL/sycl.hpp>
13+
14+
template <typename F, typename B>
15+
void run(cl::sycl::queue &q, B &buf, const F &func) {
16+
auto e = q.submit([&](cl::sycl::handler &cgh) {
17+
auto acc = buf.template get_access<cl::sycl::access::mode::write>(cgh);
18+
cgh.single_task([=]() { func(acc); });
19+
});
20+
e.wait();
21+
}
22+
23+
int main() {
24+
cl::sycl::queue q;
25+
26+
int A[1] = {1};
27+
int B[1] = {1};
28+
cl::sycl::buffer<int, 1> bufA(A, 1);
29+
cl::sycl::buffer<int, 1> bufB(B, 1);
30+
31+
run(q, bufA,
32+
[&](const cl::sycl::accessor<int, 1, cl::sycl::access::mode::write>
33+
&acc) { acc[0] = 0; });
34+
run(q, bufB,
35+
[&](const cl::sycl::accessor<int, 1, cl::sycl::access::mode::write>
36+
&acc) { acc[0] *= 2; });
37+
38+
if (A[0] != 0 || B[0] != 2)
39+
return -1;
40+
41+
return 0;
42+
}

0 commit comments

Comments
 (0)