clang 22.0.0git
CGCUDARuntime.cpp
Go to the documentation of this file.
1//===----- CGCUDARuntime.cpp - Interface to CUDA Runtimes -----------------===//
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 provides an abstract class for CUDA code generation. Concrete
10// subclasses of this implement code generation for specific CUDA
11// runtime libraries.
12//
13//===----------------------------------------------------------------------===//
14
15#include "CGCUDARuntime.h"
16#include "CGCall.h"
17#include "CodeGenFunction.h"
18#include "clang/AST/ExprCXX.h"
19
20using namespace clang;
21using namespace CodeGen;
22
24
25static llvm::Value *emitGetParamBuf(CodeGenFunction &CGF,
26 const CUDAKernelCallExpr *E) {
27 auto *GetParamBuf = CGF.getContext().getcudaGetParameterBufferDecl();
28 const FunctionProtoType *GetParamBufProto =
29 GetParamBuf->getType()->getAs<FunctionProtoType>();
30
32 CGF.getContext(), {}, {}, GetParamBuf,
33 /*RefersToEnclosingVariableOrCapture=*/false, GetParamBuf->getNameInfo(),
34 GetParamBuf->getType(), VK_PRValue);
35 auto *ImpCast = ImplicitCastExpr::Create(
36 CGF.getContext(), CGF.getContext().getPointerType(GetParamBuf->getType()),
37 CK_FunctionToPointerDecay, DRE, nullptr, VK_PRValue, FPOptionsOverride());
38
39 CGCallee Callee = CGF.EmitCallee(ImpCast);
40 CallArgList Args;
41 // Use 64B alignment.
43 CGF.getContext().getSizeType());
44 // Calculate parameter sizes.
45 const PointerType *PT = E->getCallee()->getType()->getAs<PointerType>();
46 const FunctionProtoType *FTP =
48 CharUnits Offset = CharUnits::Zero();
49 for (auto ArgTy : FTP->getParamTypes()) {
50 auto TInfo = CGF.CGM.getContext().getTypeInfoInChars(ArgTy);
51 Offset = Offset.alignTo(TInfo.Align) + TInfo.Width;
52 }
53 Args.add(RValue::get(CGF.CGM.getSize(Offset)),
54 CGF.getContext().getSizeType());
55 const CGFunctionInfo &CallInfo = CGF.CGM.getTypes().arrangeFreeFunctionCall(
56 Args, GetParamBufProto, /*ChainCall=*/false);
57 auto Ret = CGF.EmitCall(CallInfo, Callee, /*ReturnValue=*/{}, Args);
58
59 return Ret.getScalarVal();
60}
61
64 ReturnValueSlot ReturnValue, llvm::CallBase **CallOrInvoke) {
65 assert(CGM.getContext().getcudaLaunchDeviceDecl() ==
67
68 llvm::BasicBlock *ConfigOKBlock = CGF.createBasicBlock("dkcall.configok");
69 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("dkcall.end");
70
71 llvm::Value *Config = emitGetParamBuf(CGF, E);
72 CGF.Builder.CreateCondBr(
73 CGF.Builder.CreateICmpNE(Config,
74 llvm::Constant::getNullValue(Config->getType())),
75 ConfigOKBlock, ContBlock);
76
78
79 eval.begin(CGF);
80 CGF.EmitBlock(ConfigOKBlock);
81
82 QualType KernelCalleeFuncTy =
84 CGCallee KernelCallee = CGF.EmitCallee(E->getCallee());
85 // Emit kernel arguments.
86 CallArgList KernelCallArgs;
87 CGF.EmitCallArgs(KernelCallArgs,
88 KernelCalleeFuncTy->getAs<FunctionProtoType>(),
89 E->arguments(), E->getDirectCallee());
90 // Copy emitted kernel arguments into that parameter buffer.
91 RawAddress CfgBase(Config, CGM.Int8Ty,
92 /*Alignment=*/CharUnits::fromQuantity(64));
93 CharUnits Offset = CharUnits::Zero();
94 for (auto &Arg : KernelCallArgs) {
95 auto TInfo = CGM.getContext().getTypeInfoInChars(Arg.getType());
96 Offset = Offset.alignTo(TInfo.Align);
98 CGF.Builder.CreateConstInBoundsGEP(CfgBase, Offset.getQuantity());
99 Arg.copyInto(CGF, Addr);
100 Offset += TInfo.Width;
101 }
102 // Make `cudaLaunchDevice` call, i.e. E->getConfig().
103 const CallExpr *LaunchCall = E->getConfig();
104 QualType LaunchCalleeFuncTy = LaunchCall->getCallee()
105 ->getType()
106 ->getAs<PointerType>()
107 ->getPointeeType();
108 CGCallee LaunchCallee = CGF.EmitCallee(LaunchCall->getCallee());
109 CallArgList LaunchCallArgs;
110 CGF.EmitCallArgs(LaunchCallArgs,
111 LaunchCalleeFuncTy->getAs<FunctionProtoType>(),
112 LaunchCall->arguments(), LaunchCall->getDirectCallee());
113 // Replace func and paramterbuffer arguments.
114 LaunchCallArgs[0] = CallArg(RValue::get(KernelCallee.getFunctionPointer()),
115 CGM.getContext().VoidPtrTy);
116 LaunchCallArgs[1] = CallArg(RValue::get(Config), CGM.getContext().VoidPtrTy);
117 const CGFunctionInfo &LaunchCallInfo = CGM.getTypes().arrangeFreeFunctionCall(
118 LaunchCallArgs, LaunchCalleeFuncTy->getAs<FunctionProtoType>(),
119 /*ChainCall=*/false);
120 CGF.EmitCall(LaunchCallInfo, LaunchCallee, ReturnValue, LaunchCallArgs,
121 CallOrInvoke,
122 /*IsMustTail=*/false, E->getExprLoc());
123 CGF.EmitBranch(ContBlock);
124
125 CGF.EmitBlock(ContBlock);
126 eval.end(CGF);
127
128 return RValue::get(nullptr);
129}
130
132 const CUDAKernelCallExpr *E,
133 ReturnValueSlot ReturnValue,
134 llvm::CallBase **CallOrInvoke) {
135 llvm::BasicBlock *ConfigOKBlock = CGF.createBasicBlock("kcall.configok");
136 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("kcall.end");
137
139 CGF.EmitBranchOnBoolExpr(E->getConfig(), ContBlock, ConfigOKBlock,
140 /*TrueCount=*/0);
141
142 eval.begin(CGF);
143 CGF.EmitBlock(ConfigOKBlock);
144 CGF.EmitSimpleCallExpr(E, ReturnValue, CallOrInvoke);
145 CGF.EmitBranch(ContBlock);
146
147 CGF.EmitBlock(ContBlock);
148 eval.end(CGF);
149
150 return RValue::get(nullptr);
151}
static llvm::Value * emitGetParamBuf(CodeGenFunction &CGF, const CUDAKernelCallExpr *E)
Defines the clang::Expr interface and subclasses for C++ expressions.
static QualType getPointeeType(const MemRegion *R)
FunctionDecl * getcudaGetParameterBufferDecl()
QualType getPointerType(QualType T) const
Return the uniqued reference to the type for a pointer to the specified type.
TypeInfoChars getTypeInfoInChars(const Type *T) const
QualType getSizeType() const
Return the unique type for "size_t" (C99 7.17), defined in <stddef.h>.
Represents a call to a CUDA kernel function.
Definition ExprCXX.h:234
const CallExpr * getConfig() const
Definition ExprCXX.h:260
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
Definition Expr.h:2877
FunctionDecl * getDirectCallee()
If the callee is a FunctionDecl, return it. Otherwise return null.
Definition Expr.h:3060
Expr * getCallee()
Definition Expr.h:3024
arg_range arguments()
Definition Expr.h:3129
CharUnits - This is an opaque type for sizes expressed in character units.
Definition CharUnits.h:38
static CharUnits fromQuantity(QuantityType Quantity)
fromQuantity - Construct a CharUnits quantity from a raw integer type.
Definition CharUnits.h:63
static CharUnits Zero()
Zero - Construct a CharUnits quantity of zero.
Definition CharUnits.h:53
Like RawAddress, an abstract representation of an aligned address, but the pointer contained in this ...
Definition Address.h:128
Address CreateConstInBoundsGEP(Address Addr, uint64_t Index, const llvm::Twine &Name="")
Given addr = T* ... produce name = getelementptr inbounds addr, i64 index where i64 is actually the t...
Definition CGBuilder.h:265
virtual RValue EmitCUDAKernelCallExpr(CodeGenFunction &CGF, const CUDAKernelCallExpr *E, ReturnValueSlot ReturnValue, llvm::CallBase **CallOrInvoke=nullptr)
virtual RValue EmitCUDADeviceKernelCallExpr(CodeGenFunction &CGF, const CUDAKernelCallExpr *E, ReturnValueSlot ReturnValue, llvm::CallBase **CallOrInvoke=nullptr)
All available information about a concrete callee.
Definition CGCall.h:63
llvm::Value * getFunctionPointer() const
Definition CGCall.h:190
CGFunctionInfo - Class to encapsulate the information about a function definition.
CallArgList - Type for representing both the value and type of arguments in a call.
Definition CGCall.h:274
void add(RValue rvalue, QualType type)
Definition CGCall.h:302
An object to manage conditionally-evaluated expressions.
CodeGenFunction - This class organizes the per-function state that is used while generating LLVM code...
void EmitBranchOnBoolExpr(const Expr *Cond, llvm::BasicBlock *TrueBlock, llvm::BasicBlock *FalseBlock, uint64_t TrueCount, Stmt::Likelihood LH=Stmt::LH_None, const Expr *ConditionalOp=nullptr, const VarDecl *ConditionalDecl=nullptr)
EmitBranchOnBoolExpr - Emit a branch on a boolean condition (e.g.
RValue EmitSimpleCallExpr(const CallExpr *E, ReturnValueSlot ReturnValue, llvm::CallBase **CallOrInvoke=nullptr)
Emit a CallExpr without considering whether it might be a subclass.
Definition CGExpr.cpp:6091
llvm::BasicBlock * createBasicBlock(const Twine &name="", llvm::Function *parent=nullptr, llvm::BasicBlock *before=nullptr)
createBasicBlock - Create an LLVM basic block.
CGCallee EmitCallee(const Expr *E)
Definition CGExpr.cpp:6167
RValue EmitCall(const CGFunctionInfo &CallInfo, const CGCallee &Callee, ReturnValueSlot ReturnValue, const CallArgList &Args, llvm::CallBase **CallOrInvoke, bool IsMustTail, SourceLocation Loc, bool IsVirtualFunctionPointerThunk=false)
EmitCall - Generate a call of the given function, expecting the given result type,...
Definition CGCall.cpp:5248
void EmitBranch(llvm::BasicBlock *Block)
EmitBranch - Emit a branch to the specified basic block from the current insert block,...
Definition CGStmt.cpp:672
void EmitCallArgs(CallArgList &Args, PrototypeWrapper Prototype, llvm::iterator_range< CallExpr::const_arg_iterator > ArgRange, AbstractCallee AC=AbstractCallee(), unsigned ParamsToSkip=0, EvaluationOrder Order=EvaluationOrder::Default)
EmitCallArgs - Emit call arguments for a function.
Definition CGCall.cpp:4688
void EmitBlock(llvm::BasicBlock *BB, bool IsFinished=false)
EmitBlock - Emit the given block.
Definition CGStmt.cpp:652
ASTContext & getContext() const
llvm::ConstantInt * getSize(CharUnits numChars)
Emit the given number of characters as a value of type size_t.
const CGFunctionInfo & arrangeFreeFunctionCall(const CallArgList &Args, const FunctionType *Ty, bool ChainCall)
Figure out the rules for calling a function with the given formal type using the given arguments.
Definition CGCall.cpp:700
RValue - This trivial value class is used to represent the result of an expression that is evaluated.
Definition CGValue.h:42
static RValue get(llvm::Value *V)
Definition CGValue.h:99
An abstract representation of an aligned address.
Definition Address.h:42
ReturnValueSlot - Contains the address where the return value of a function can be stored,...
Definition CGCall.h:379
A reference to a declared variable, function, enum, etc.
Definition Expr.h:1270
static DeclRefExpr * Create(const ASTContext &Context, NestedNameSpecifierLoc QualifierLoc, SourceLocation TemplateKWLoc, ValueDecl *D, bool RefersToEnclosingVariableOrCapture, SourceLocation NameLoc, QualType T, ExprValueKind VK, NamedDecl *FoundD=nullptr, const TemplateArgumentListInfo *TemplateArgs=nullptr, NonOdrUseReason NOUR=NOUR_None)
Definition Expr.cpp:484
SourceLocation getExprLoc() const LLVM_READONLY
getExprLoc - Return the preferred location for the arrow when diagnosing a problem with a generic exp...
Definition Expr.cpp:273
QualType getType() const
Definition Expr.h:144
Represents difference between two FPOptions values.
Represents a prototype with parameter type info, e.g.
Definition TypeBase.h:5254
static ImplicitCastExpr * Create(const ASTContext &Context, QualType T, CastKind Kind, Expr *Operand, const CXXCastPath *BasePath, ExprValueKind Cat, FPOptionsOverride FPO)
Definition Expr.cpp:2069
PointerType - C99 6.7.5.1 - Pointer Declarators.
Definition TypeBase.h:3328
QualType getPointeeType() const
Definition TypeBase.h:3338
A (possibly-)qualified type.
Definition TypeBase.h:937
const T * getAs() const
Member-template getAs<specific type>'.
Definition TypeBase.h:9091
The JSON file list parser is used to communicate input to InstallAPI.
@ VK_PRValue
A pr-value expression (in the C++11 taxonomy) produces a temporary value.
Definition Specifiers.h:135