clang 22.0.0git
CodeGenSYCL.cpp
Go to the documentation of this file.
1//===--------- CodeGenSYCL.cpp - Code for SYCL kernel generation ----------===//
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//
9// This contains code required for generation of SYCL kernel caller offload
10// entry point functions.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CodeGenFunction.h"
15#include "CodeGenModule.h"
16
17using namespace clang;
18using namespace CodeGen;
19
20static void SetSYCLKernelAttributes(llvm::Function *Fn, CodeGenFunction &CGF) {
21 // SYCL 2020 device language restrictions require forward progress and
22 // disallow recursion.
23 Fn->setDoesNotRecurse();
25 Fn->addFnAttr(llvm::Attribute::MustProgress);
26}
27
28void CodeGenModule::EmitSYCLKernelCaller(const FunctionDecl *KernelEntryPointFn,
29 ASTContext &Ctx) {
30 assert(Ctx.getLangOpts().SYCLIsDevice &&
31 "SYCL kernel caller offload entry point functions can only be emitted"
32 " during device compilation");
33
34 const auto *KernelEntryPointAttr =
35 KernelEntryPointFn->getAttr<SYCLKernelEntryPointAttr>();
36 assert(KernelEntryPointAttr && "Missing sycl_kernel_entry_point attribute");
37 assert(!KernelEntryPointAttr->isInvalidAttr() &&
38 "sycl_kernel_entry_point attribute is invalid");
39
40 // Find the SYCLKernelCallStmt.
41 SYCLKernelCallStmt *KernelCallStmt =
42 cast<SYCLKernelCallStmt>(KernelEntryPointFn->getBody());
43
44 // Retrieve the SYCL kernel caller parameters from the OutlinedFunctionDecl.
45 FunctionArgList Args;
46 const OutlinedFunctionDecl *OutlinedFnDecl =
47 KernelCallStmt->getOutlinedFunctionDecl();
48 Args.append(OutlinedFnDecl->param_begin(), OutlinedFnDecl->param_end());
49
50 // Compute the function info and LLVM function type.
51 const CGFunctionInfo &FnInfo =
53 llvm::FunctionType *FnTy = getTypes().GetFunctionType(FnInfo);
54
55 // Retrieve the generated name for the SYCL kernel caller function.
56 CanQualType KernelNameType =
57 Ctx.getCanonicalType(KernelEntryPointAttr->getKernelName());
58 const SYCLKernelInfo &KernelInfo = Ctx.getSYCLKernelInfo(KernelNameType);
59 auto *Fn = llvm::Function::Create(FnTy, llvm::Function::ExternalLinkage,
60 KernelInfo.GetKernelName(), &getModule());
61
62 // Emit the SYCL kernel caller function.
63 CodeGenFunction CGF(*this);
64 SetLLVMFunctionAttributes(GlobalDecl(), FnInfo, Fn, false);
66 CGF.StartFunction(GlobalDecl(), Ctx.VoidTy, Fn, FnInfo, Args,
68 CGF.EmitFunctionBody(OutlinedFnDecl->getBody());
69 setDSOLocal(Fn);
70 SetLLVMFunctionAttributesForDefinition(cast<Decl>(OutlinedFnDecl), Fn);
71 CGF.FinishFunction();
72}
static void SetSYCLKernelAttributes(llvm::Function *Fn, CodeGenFunction &CGF)
Definition: CodeGenSYCL.cpp:20
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:188
CanQualType getCanonicalType(QualType T) const
Return the canonical (structural) type corresponding to the specified potentially non-canonical type ...
Definition: ASTContext.h:2851
const LangOptions & getLangOpts() const
Definition: ASTContext.h:894
CanQualType VoidTy
Definition: ASTContext.h:1222
const SYCLKernelInfo & getSYCLKernelInfo(QualType T) const
Given a type used as a SYCL kernel name, returns a reference to the metadata generated from the corre...
CGFunctionInfo - Class to encapsulate the information about a function definition.
CodeGenFunction - This class organizes the per-function state that is used while generating LLVM code...
bool checkIfFunctionMustProgress()
Returns true if a function must make progress, which means the mustprogress attribute can be added.
void setDSOLocal(llvm::GlobalValue *GV) const
llvm::Module & getModule() const
void SetLLVMFunctionAttributes(GlobalDecl GD, const CGFunctionInfo &Info, llvm::Function *F, bool IsThunk)
Set the LLVM function attributes (sext, zext, etc).
void SetLLVMFunctionAttributesForDefinition(const Decl *D, llvm::Function *F)
Set the LLVM function attributes which only apply to a function definition.
const CGFunctionInfo & arrangeSYCLKernelCallerDeclaration(QualType resultType, const FunctionArgList &args)
A SYCL kernel caller function is an offload device entry point function with a target device dependen...
Definition: CGCall.cpp:756
llvm::FunctionType * GetFunctionType(const CGFunctionInfo &Info)
GetFunctionType - Get the LLVM function type for.
Definition: CGCall.cpp:1702
FunctionArgList - Type for representing both the decl and type of parameters to a function.
Definition: CGCall.h:375
T * getAttr() const
Definition: DeclBase.h:573
Represents a function declaration or definition.
Definition: Decl.h:1999
Stmt * getBody(const FunctionDecl *&Definition) const
Retrieve the body (definition) of the function.
Definition: Decl.cpp:3271
GlobalDecl - represents a global declaration.
Definition: GlobalDecl.h:57
Represents a partial function definition.
Definition: Decl.h:4841
parameter_const_iterator param_end() const
Definition: Decl.h:4889
Stmt * getBody() const override
getBody - If this Decl represents a declaration for a body of code, such as a function or method defi...
Definition: Decl.cpp:5540
parameter_const_iterator param_begin() const
Definition: Decl.h:4888
SYCLKernelCallStmt represents the transformation that is applied to the body of a function declared w...
Definition: StmtSYCL.h:37
OutlinedFunctionDecl * getOutlinedFunctionDecl()
Retrieve the outlined function declaration.
Definition: StmtSYCL.h:61
const std::string & GetKernelName() const
Encodes a location in the source.
The JSON file list parser is used to communicate input to InstallAPI.