Skip to content

Commit f4d182f

Browse files
vmaksimobader
authored andcommitted
[SYCL] Fix kernel name mangling for unnamed lambdas
The problem was that new mangling context for every kernel function was created. So if the code contains several unnamed lambdas with the same signature it caused equal mangled names for them. Signed-off-by: Viktoria Maksimova <viktoria.maksimova@intel.com>
1 parent cd8194d commit f4d182f

File tree

5 files changed

+41
-11
lines changed

5 files changed

+41
-11
lines changed

clang/include/clang/Sema/Sema.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11512,7 +11512,7 @@ class Sema {
1151211512
KernelCallVariadicFunction
1151311513
};
1151411514
DeviceDiagBuilder SYCLDiagIfDeviceCode(SourceLocation Loc, unsigned DiagID);
11515-
void ConstructOpenCLKernel(FunctionDecl *KernelCallerFunc);
11515+
void ConstructOpenCLKernel(FunctionDecl *KernelCallerFunc, MangleContext &MC);
1151611516
void MarkDevice(void);
1151711517
bool CheckSYCLCall(SourceLocation Loc, FunctionDecl *Callee);
1151811518
};

clang/lib/Sema/SemaSYCL.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1176,14 +1176,11 @@ static std::string eraseAnonNamespace(std::string S) {
11761176

11771177
// Creates a mangled kernel name for given kernel name type
11781178
static std::string constructKernelName(QualType KernelNameType,
1179-
ASTContext &AC) {
1180-
std::unique_ptr<MangleContext> MC(AC.createMangleContext());
1181-
1179+
MangleContext &MC) {
11821180
SmallString<256> Result;
11831181
llvm::raw_svector_ostream Out(Result);
11841182

1185-
MC->mangleTypeName(KernelNameType, Out);
1186-
1183+
MC.mangleTypeName(KernelNameType, Out);
11871184
return Out.str();
11881185
}
11891186

@@ -1209,7 +1206,8 @@ static std::string constructKernelName(QualType KernelNameType,
12091206
// }
12101207
//
12111208
//
1212-
void Sema::ConstructOpenCLKernel(FunctionDecl *KernelCallerFunc) {
1209+
void Sema::ConstructOpenCLKernel(FunctionDecl *KernelCallerFunc,
1210+
MangleContext &MC) {
12131211
CXXRecordDecl *LE = getKernelObjectType(KernelCallerFunc);
12141212
assert(LE && "invalid kernel caller");
12151213

@@ -1223,7 +1221,7 @@ void Sema::ConstructOpenCLKernel(FunctionDecl *KernelCallerFunc) {
12231221
assert(TemplateArgs && "No template argument info");
12241222
QualType KernelNameType = TypeName::getFullyQualifiedType(
12251223
TemplateArgs->get(0).getAsType(), getASTContext(), true);
1226-
std::string Name = constructKernelName(KernelNameType, getASTContext());
1224+
std::string Name = constructKernelName(KernelNameType, MC);
12271225

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

clang/lib/Sema/SemaTemplateInstantiateDecl.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "clang/AST/DependentDiagnostic.h"
1818
#include "clang/AST/Expr.h"
1919
#include "clang/AST/ExprCXX.h"
20+
#include "clang/AST/Mangle.h"
2021
#include "clang/AST/PrettyDeclStackTrace.h"
2122
#include "clang/AST/TypeLoc.h"
2223
#include "clang/Sema/Initialization.h"
@@ -5520,6 +5521,8 @@ NamedDecl *Sema::FindInstantiatedDecl(SourceLocation Loc, NamedDecl *D,
55205521
/// Performs template instantiation for all implicit template
55215522
/// instantiations we have seen until this point.
55225523
void Sema::PerformPendingInstantiations(bool LocalOnly) {
5524+
std::unique_ptr<MangleContext> MangleCtx(
5525+
getASTContext().createMangleContext());
55235526
while (!PendingLocalImplicitInstantiations.empty() ||
55245527
(!LocalOnly && !PendingInstantiations.empty())) {
55255528
PendingImplicitInstantiation Inst;
@@ -5538,7 +5541,8 @@ void Sema::PerformPendingInstantiations(bool LocalOnly) {
55385541
TSK_ExplicitInstantiationDefinition;
55395542
if (Function->isMultiVersion()) {
55405543
getASTContext().forEachMultiversionedFunctionVersion(
5541-
Function, [this, Inst, DefinitionRequired](FunctionDecl *CurFD) {
5544+
Function, [this, Inst, DefinitionRequired,
5545+
MangleCtx = move(MangleCtx)](FunctionDecl *CurFD) {
55425546
InstantiateFunctionDefinition(/*FIXME:*/ Inst.second, CurFD, true,
55435547
DefinitionRequired, true);
55445548
if (CurFD->isDefined()) {
@@ -5547,7 +5551,7 @@ void Sema::PerformPendingInstantiations(bool LocalOnly) {
55475551
// so we are checking for SYCL kernel attribute after instantination.
55485552
if (getLangOpts().SYCLIsDevice &&
55495553
CurFD->hasAttr<SYCLKernelAttr>()) {
5550-
ConstructOpenCLKernel(CurFD);
5554+
ConstructOpenCLKernel(CurFD, *MangleCtx);
55515555
}
55525556
CurFD->setInstantiationIsPending(false);
55535557
}
@@ -5561,7 +5565,7 @@ void Sema::PerformPendingInstantiations(bool LocalOnly) {
55615565
// so we are checking for SYCL kernel attribute after instantination.
55625566
if (getLangOpts().SYCLIsDevice &&
55635567
Function->hasAttr<SYCLKernelAttr>()) {
5564-
ConstructOpenCLKernel(Function);
5568+
ConstructOpenCLKernel(Function, *MangleCtx);
55655569
}
55665570
Function->setInstantiationIsPending(false);
55675571
}

clang/test/SemaSYCL/Inputs/sycl.hpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,22 @@ class sampler {
161161
void use(void) const {}
162162
};
163163

164+
class event {};
165+
class queue {
166+
public:
167+
template <typename T>
168+
event submit(T cgf) { return event{}; }
169+
};
170+
#define ATTR_SYCL_KERNEL __attribute__((sycl_kernel))
171+
class handler {
172+
public:
173+
template <typename KernelName, typename KernelType>
174+
ATTR_SYCL_KERNEL void single_task(KernelType kernelFunc) {}
175+
176+
template <typename KernelType>
177+
ATTR_SYCL_KERNEL void single_task(KernelType kernelFunc) {}
178+
};
179+
164180
} // namespace sycl
165181
} // namespace cl
166182

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// RUN: %clang_cc1 -triple spir64-unknown-linux-sycldevice -I %S/Inputs -fsycl-is-device -fsycl-unnamed-lambda -ast-dump %s | FileCheck %s
2+
#include <sycl.hpp>
3+
4+
int main() {
5+
cl::sycl::queue q;
6+
q.submit([&](cl::sycl::handler &h) { h.single_task([] {}); });
7+
q.submit([&](cl::sycl::handler &h) { h.single_task([] {}); });
8+
return 0;
9+
}
10+
11+
// CHECK: _ZTSZZ4mainENK3$_0clERN2cl4sycl7handlerEEUlvE_
12+
// CHECK: _ZTSZZ4mainENK3$_1clERN2cl4sycl7handlerEEUlvE_

0 commit comments

Comments
 (0)