xref: /freebsd/contrib/llvm-project/clang/lib/Sema/SemaSYCL.cpp (revision 963f5dc7a30624e95d72fb7f87b8892651164e46)
1 //===- SemaSYCL.cpp - Semantic Analysis for SYCL constructs ---------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 // This implements Semantic Analysis for SYCL constructs.
9 //===----------------------------------------------------------------------===//
10 
11 #include "clang/AST/Mangle.h"
12 #include "clang/Sema/Sema.h"
13 #include "clang/Sema/SemaDiagnostic.h"
14 
15 using namespace clang;
16 
17 // -----------------------------------------------------------------------------
18 // SYCL device specific diagnostics implementation
19 // -----------------------------------------------------------------------------
20 
21 Sema::SemaDiagnosticBuilder Sema::SYCLDiagIfDeviceCode(SourceLocation Loc,
22                                                        unsigned DiagID) {
23   assert(getLangOpts().SYCLIsDevice &&
24          "Should only be called during SYCL compilation");
25   FunctionDecl *FD = dyn_cast<FunctionDecl>(getCurLexicalContext());
26   SemaDiagnosticBuilder::Kind DiagKind = [this, FD] {
27     if (!FD)
28       return SemaDiagnosticBuilder::K_Nop;
29     if (getEmissionStatus(FD) == Sema::FunctionEmissionStatus::Emitted)
30       return SemaDiagnosticBuilder::K_ImmediateWithCallStack;
31     return SemaDiagnosticBuilder::K_Deferred;
32   }();
33   return SemaDiagnosticBuilder(DiagKind, Loc, DiagID, FD, *this);
34 }
35 
36 bool Sema::checkSYCLDeviceFunction(SourceLocation Loc, FunctionDecl *Callee) {
37   assert(getLangOpts().SYCLIsDevice &&
38          "Should only be called during SYCL compilation");
39   assert(Callee && "Callee may not be null.");
40 
41   // Errors in unevaluated context don't need to be generated,
42   // so we can safely skip them.
43   if (isUnevaluatedContext() || isConstantEvaluated())
44     return true;
45 
46   SemaDiagnosticBuilder::Kind DiagKind = SemaDiagnosticBuilder::K_Nop;
47 
48   return DiagKind != SemaDiagnosticBuilder::K_Immediate &&
49          DiagKind != SemaDiagnosticBuilder::K_ImmediateWithCallStack;
50 }
51 
52 // The SYCL kernel's 'object type' used for diagnostics and naming/mangling is
53 // the first parameter to a sycl_kernel labeled function template. In SYCL1.2.1,
54 // this was passed by value, and in SYCL2020, it is passed by reference.
55 static QualType GetSYCLKernelObjectType(const FunctionDecl *KernelCaller) {
56   assert(KernelCaller->getNumParams() > 0 && "Insufficient kernel parameters");
57   QualType KernelParamTy = KernelCaller->getParamDecl(0)->getType();
58 
59   // SYCL 2020 kernels are passed by reference.
60   if (KernelParamTy->isReferenceType())
61     return KernelParamTy->getPointeeType();
62 
63   // SYCL 1.2.1
64   return KernelParamTy;
65 }
66 
67 void Sema::AddSYCLKernelLambda(const FunctionDecl *FD) {
68   auto MangleCallback = [](ASTContext &Ctx,
69                            const NamedDecl *ND) -> llvm::Optional<unsigned> {
70     if (const auto *RD = dyn_cast<CXXRecordDecl>(ND))
71       Ctx.AddSYCLKernelNamingDecl(RD);
72     // We always want to go into the lambda mangling (skipping the unnamed
73     // struct version), so make sure we return a value here.
74     return 1;
75   };
76 
77   QualType Ty = GetSYCLKernelObjectType(FD);
78   std::unique_ptr<MangleContext> Ctx{ItaniumMangleContext::create(
79       Context, Context.getDiagnostics(), MangleCallback)};
80   llvm::raw_null_ostream Out;
81   Ctx->mangleTypeName(Ty, Out);
82 }
83