19 #include "llvm/IR/BasicBlock.h"
20 #include "llvm/IR/CallSite.h"
21 #include "llvm/IR/Constants.h"
22 #include "llvm/IR/DerivedTypes.h"
24 using namespace clang;
25 using namespace CodeGen;
33 llvm::PointerType *CharPtrTy, *VoidPtrTy, *VoidPtrPtrTy;
38 llvm::Module &TheModule;
46 llvm::Constant *getSetupArgumentFn()
const;
47 llvm::Constant *getLaunchFn()
const;
50 llvm::Function *makeRegisterKernelsFn();
55 llvm::Constant *makeConstantString(
const std::string &Str,
56 const std::string &
Name =
"",
57 unsigned Alignment = 0) {
58 llvm::Constant *Zeros[] = {llvm::ConstantInt::get(SizeTy, 0),
59 llvm::ConstantInt::get(SizeTy, 0)};
60 auto ConstStr = CGM.GetAddrOfConstantCString(Str,
Name.c_str());
61 return llvm::ConstantExpr::getGetElementPtr(ConstStr.getElementType(),
62 ConstStr.getPointer(), Zeros);
72 llvm::Function *makeModuleCtorFunction()
override;
74 llvm::Function *makeModuleDtorFunction()
override;
81 TheModule(CGM.getModule()) {
87 VoidTy = llvm::Type::getVoidTy(
Context);
91 VoidPtrPtrTy = VoidPtrTy->getPointerTo();
94 llvm::Constant *CGNVCUDARuntime::getSetupArgumentFn()
const {
96 std::vector<llvm::Type*> Params;
97 Params.push_back(VoidPtrTy);
98 Params.push_back(SizeTy);
99 Params.push_back(SizeTy);
102 "cudaSetupArgument");
105 llvm::Constant *CGNVCUDARuntime::getLaunchFn()
const {
108 llvm::FunctionType::get(IntTy, CharPtrTy,
false),
"cudaLaunch");
113 EmittedKernels.push_back(CGF.
CurFn);
114 emitDeviceStubBody(CGF, Args);
121 std::vector<llvm::Type *> ArgTypes;
122 for (FunctionArgList::const_iterator
I = Args.begin(),
E = Args.end();
125 ArgValues.push_back(V);
126 assert(isa<llvm::PointerType>(V->getType()) &&
"Arg type not PointerType");
127 ArgTypes.push_back(cast<llvm::PointerType>(V->getType())->
getElementType());
129 llvm::StructType *ArgStackTy = llvm::StructType::get(
Context, ArgTypes);
134 llvm::Constant *cudaSetupArgFn = getSetupArgumentFn();
135 for (
unsigned I = 0,
E = Args.size();
I !=
E; ++
I) {
138 Args[0] = CGF.
Builder.CreatePointerCast(ArgValues[
I], VoidPtrTy);
139 Args[1] = CGF.
Builder.CreateIntCast(
140 llvm::ConstantExpr::getSizeOf(ArgTypes[I]),
142 Args[2] = CGF.
Builder.CreateIntCast(
143 llvm::ConstantExpr::getOffsetOf(ArgStackTy, I),
146 llvm::Constant *Zero = llvm::ConstantInt::get(IntTy, 0);
148 CGF.
Builder.CreateCondBr(CSZero, NextBlock, EndBlock);
153 llvm::Constant *cudaLaunchFn = getLaunchFn();
170 llvm::Function *CGNVCUDARuntime::makeRegisterKernelsFn() {
172 llvm::FunctionType::get(VoidTy, VoidPtrPtrTy,
false),
174 llvm::BasicBlock *EntryBB =
177 Builder.SetInsertPoint(EntryBB);
181 std::vector<llvm::Type *> RegisterFuncParams = {
182 VoidPtrPtrTy, CharPtrTy, CharPtrTy, CharPtrTy, IntTy,
183 VoidPtrTy, VoidPtrTy, VoidPtrTy, VoidPtrTy, IntTy->getPointerTo()};
185 llvm::FunctionType::get(IntTy, RegisterFuncParams,
false),
186 "__cudaRegisterFunction");
191 llvm::Argument &GpuBinaryHandlePtr = *RegisterKernelsFunc->arg_begin();
192 for (llvm::Function *Kernel : EmittedKernels) {
193 llvm::Constant *KernelName = makeConstantString(Kernel->getName());
194 llvm::Constant *NullPtr = llvm::ConstantPointerNull::get(VoidPtrTy);
196 &GpuBinaryHandlePtr,
Builder.CreateBitCast(Kernel, VoidPtrTy),
197 KernelName, KernelName, llvm::ConstantInt::get(IntTy, -1), NullPtr,
198 NullPtr, NullPtr, NullPtr,
199 llvm::ConstantPointerNull::get(IntTy->getPointerTo())};
200 Builder.CreateCall(RegisterFunc, args);
204 return RegisterKernelsFunc;
217 llvm::Function *CGNVCUDARuntime::makeModuleCtorFunction() {
219 llvm::Function *RegisterKernelsFunc = makeRegisterKernelsFn();
222 llvm::FunctionType::get(VoidPtrPtrTy, VoidPtrTy,
false),
223 "__cudaRegisterFatBinary");
225 llvm::StructType *FatbinWrapperTy =
226 llvm::StructType::get(IntTy, IntTy, VoidPtrTy, VoidPtrTy,
nullptr);
229 llvm::FunctionType::get(VoidTy, VoidPtrTy,
false),
231 llvm::BasicBlock *CtorEntryBB =
235 CtorBuilder.SetInsertPoint(CtorEntryBB);
242 for (
const std::string &GpuBinaryFileName :
244 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> GpuBinaryOrErr =
245 llvm::MemoryBuffer::getFileOrSTDIN(GpuBinaryFileName);
246 if (std::error_code EC = GpuBinaryOrErr.getError()) {
247 CGM.
getDiags().
Report(diag::err_cannot_open_file) << GpuBinaryFileName
253 llvm::Constant *Values[] = {
254 llvm::ConstantInt::get(IntTy, 0x466243b1),
255 llvm::ConstantInt::get(IntTy, 1),
256 makeConstantString(GpuBinaryOrErr.get()->getBuffer(),
"", 16),
257 llvm::ConstantPointerNull::get(VoidPtrTy)};
258 llvm::GlobalVariable *FatbinWrapper =
new llvm::GlobalVariable(
260 llvm::ConstantStruct::get(FatbinWrapperTy, Values),
261 "__cuda_fatbin_wrapper");
264 llvm::CallInst *RegisterFatbinCall = CtorBuilder.CreateCall(
266 CtorBuilder.CreateBitCast(FatbinWrapper, VoidPtrTy));
267 llvm::GlobalVariable *GpuBinaryHandle =
new llvm::GlobalVariable(
269 llvm::ConstantPointerNull::get(VoidPtrPtrTy),
"__cuda_gpubin_handle");
270 CtorBuilder.CreateAlignedStore(RegisterFatbinCall, GpuBinaryHandle,
274 CtorBuilder.CreateCall(RegisterKernelsFunc, RegisterFatbinCall);
277 GpuBinaryHandles.push_back(GpuBinaryHandle);
280 CtorBuilder.CreateRetVoid();
281 return ModuleCtorFunc;
293 llvm::Function *CGNVCUDARuntime::makeModuleDtorFunction() {
296 llvm::FunctionType::get(VoidTy, VoidPtrPtrTy,
false),
297 "__cudaUnregisterFatBinary");
300 llvm::FunctionType::get(VoidTy, VoidPtrTy,
false),
302 llvm::BasicBlock *DtorEntryBB =
305 DtorBuilder.SetInsertPoint(DtorEntryBB);
307 for (llvm::GlobalVariable *GpuBinaryHandle : GpuBinaryHandles) {
310 DtorBuilder.CreateCall(UnregisterFatbinFunc, HandleValue);
313 DtorBuilder.CreateRetVoid();
314 return ModuleDtorFunc;
318 return new CGNVCUDARuntime(CGM);
CodeGenTypes & getTypes()
CanQualType getSizeType() const
Return the unique type for "size_t" (C99 7.17), defined in <stddef.h>.
DiagnosticBuilder Report(SourceLocation Loc, unsigned DiagID)
Issue the message to the client.
Address GetAddrOfLocalVar(const VarDecl *VD)
GetAddrOfLocalVar - Return the address of a local variable.
std::vector< std::string > CudaGpuBinaryFileNames
A list of file names passed with -fcuda-include-gpubinary options to forward to CUDA runtime back-end...
class LLVM_ALIGNAS(8) DependentTemplateSpecializationType const IdentifierInfo * Name
Represents a template specialization type whose template cannot be resolved, e.g. ...
CodeGenFunction - This class organizes the per-function state that is used while generating LLVM code...
llvm::Type * ConvertType(QualType T)
ConvertType - Convert type T into a llvm::Type.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
llvm::BasicBlock * createBasicBlock(const Twine &name="", llvm::Function *parent=nullptr, llvm::BasicBlock *before=nullptr)
createBasicBlock - Create an LLVM basic block.
detail::InMemoryDirectory::const_iterator I
llvm::CallSite EmitRuntimeCallOrInvoke(llvm::Value *callee, ArrayRef< llvm::Value * > args, const Twine &name="")
Emits a call or invoke instruction to the given runtime function.
llvm::Value * getPointer() const
ASTContext & getContext() const
static OMPLinearClause * Create(const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc, OpenMPLinearClauseKind Modifier, SourceLocation ModifierLoc, SourceLocation ColonLoc, SourceLocation EndLoc, ArrayRef< Expr * > VL, ArrayRef< Expr * > PL, ArrayRef< Expr * > IL, Expr *Step, Expr *CalcStep)
Creates clause with a list of variables VL and a linear step Step.
The l-value was considered opaque, so the alignment was determined from a type.
llvm::Constant * CreateRuntimeFunction(llvm::FunctionType *Ty, StringRef Name, llvm::AttributeSet ExtraAttrs=llvm::AttributeSet())
Create a new runtime function with the specified type and name.
CharUnits getPointerAlign() const
const CodeGenOptions & getCodeGenOpts() const
FunctionArgList - Type for representing both the decl and type of parameters to a function...
This class organizes the cross-function state that is used while generating LLVM code.
static const Type * getElementType(const Expr *BaseExpr)
detail::InMemoryDirectory::const_iterator E
This class organizes the cross-module state that is used while lowering AST types to LLVM types...
Internal linkage, which indicates that the entity can be referred to from within the translation unit...
void EmitBlock(llvm::BasicBlock *BB, bool IsFinished=false)
EmitBlock - Emit the given block.
CGCUDARuntime * CreateNVCUDARuntime(CodeGenModule &CGM)
Creates an instance of a CUDA runtime class.
DiagnosticsEngine & getDiags() const
BoundNodesTreeBuilder *const Builder
void EmitBranch(llvm::BasicBlock *Block)
EmitBranch - Emit a branch to the specified basic block from the current insert block, taking care to avoid creation of branches from dummy blocks.