xref: /minix3/external/bsd/llvm/dist/clang/lib/CodeGen/CGCUDANV.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1f4a2713aSLionel Sambuc //===----- CGCUDANV.cpp - Interface to NVIDIA CUDA Runtime ----------------===//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc //                     The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc //
10f4a2713aSLionel Sambuc // This provides a class for CUDA code generation targeting the NVIDIA CUDA
11f4a2713aSLionel Sambuc // runtime library.
12f4a2713aSLionel Sambuc //
13f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
14f4a2713aSLionel Sambuc 
15f4a2713aSLionel Sambuc #include "CGCUDARuntime.h"
16f4a2713aSLionel Sambuc #include "CodeGenFunction.h"
17f4a2713aSLionel Sambuc #include "CodeGenModule.h"
18f4a2713aSLionel Sambuc #include "clang/AST/Decl.h"
19f4a2713aSLionel Sambuc #include "llvm/IR/BasicBlock.h"
20*0a6a1f1dSLionel Sambuc #include "llvm/IR/CallSite.h"
21f4a2713aSLionel Sambuc #include "llvm/IR/Constants.h"
22f4a2713aSLionel Sambuc #include "llvm/IR/DerivedTypes.h"
23f4a2713aSLionel Sambuc #include <vector>
24f4a2713aSLionel Sambuc 
25f4a2713aSLionel Sambuc using namespace clang;
26f4a2713aSLionel Sambuc using namespace CodeGen;
27f4a2713aSLionel Sambuc 
28f4a2713aSLionel Sambuc namespace {
29f4a2713aSLionel Sambuc 
30f4a2713aSLionel Sambuc class CGNVCUDARuntime : public CGCUDARuntime {
31f4a2713aSLionel Sambuc 
32f4a2713aSLionel Sambuc private:
33f4a2713aSLionel Sambuc   llvm::Type *IntTy, *SizeTy;
34f4a2713aSLionel Sambuc   llvm::PointerType *CharPtrTy, *VoidPtrTy;
35f4a2713aSLionel Sambuc 
36f4a2713aSLionel Sambuc   llvm::Constant *getSetupArgumentFn() const;
37f4a2713aSLionel Sambuc   llvm::Constant *getLaunchFn() const;
38f4a2713aSLionel Sambuc 
39f4a2713aSLionel Sambuc public:
40f4a2713aSLionel Sambuc   CGNVCUDARuntime(CodeGenModule &CGM);
41f4a2713aSLionel Sambuc 
42*0a6a1f1dSLionel Sambuc   void EmitDeviceStubBody(CodeGenFunction &CGF, FunctionArgList &Args) override;
43f4a2713aSLionel Sambuc };
44f4a2713aSLionel Sambuc 
45f4a2713aSLionel Sambuc }
46f4a2713aSLionel Sambuc 
CGNVCUDARuntime(CodeGenModule & CGM)47f4a2713aSLionel Sambuc CGNVCUDARuntime::CGNVCUDARuntime(CodeGenModule &CGM) : CGCUDARuntime(CGM) {
48f4a2713aSLionel Sambuc   CodeGen::CodeGenTypes &Types = CGM.getTypes();
49f4a2713aSLionel Sambuc   ASTContext &Ctx = CGM.getContext();
50f4a2713aSLionel Sambuc 
51f4a2713aSLionel Sambuc   IntTy = Types.ConvertType(Ctx.IntTy);
52f4a2713aSLionel Sambuc   SizeTy = Types.ConvertType(Ctx.getSizeType());
53f4a2713aSLionel Sambuc 
54f4a2713aSLionel Sambuc   CharPtrTy = llvm::PointerType::getUnqual(Types.ConvertType(Ctx.CharTy));
55f4a2713aSLionel Sambuc   VoidPtrTy = cast<llvm::PointerType>(Types.ConvertType(Ctx.VoidPtrTy));
56f4a2713aSLionel Sambuc }
57f4a2713aSLionel Sambuc 
getSetupArgumentFn() const58f4a2713aSLionel Sambuc llvm::Constant *CGNVCUDARuntime::getSetupArgumentFn() const {
59f4a2713aSLionel Sambuc   // cudaError_t cudaSetupArgument(void *, size_t, size_t)
60f4a2713aSLionel Sambuc   std::vector<llvm::Type*> Params;
61f4a2713aSLionel Sambuc   Params.push_back(VoidPtrTy);
62f4a2713aSLionel Sambuc   Params.push_back(SizeTy);
63f4a2713aSLionel Sambuc   Params.push_back(SizeTy);
64f4a2713aSLionel Sambuc   return CGM.CreateRuntimeFunction(llvm::FunctionType::get(IntTy,
65f4a2713aSLionel Sambuc                                                            Params, false),
66f4a2713aSLionel Sambuc                                    "cudaSetupArgument");
67f4a2713aSLionel Sambuc }
68f4a2713aSLionel Sambuc 
getLaunchFn() const69f4a2713aSLionel Sambuc llvm::Constant *CGNVCUDARuntime::getLaunchFn() const {
70f4a2713aSLionel Sambuc   // cudaError_t cudaLaunch(char *)
71f4a2713aSLionel Sambuc   std::vector<llvm::Type*> Params;
72f4a2713aSLionel Sambuc   Params.push_back(CharPtrTy);
73f4a2713aSLionel Sambuc   return CGM.CreateRuntimeFunction(llvm::FunctionType::get(IntTy,
74f4a2713aSLionel Sambuc                                                            Params, false),
75f4a2713aSLionel Sambuc                                    "cudaLaunch");
76f4a2713aSLionel Sambuc }
77f4a2713aSLionel Sambuc 
EmitDeviceStubBody(CodeGenFunction & CGF,FunctionArgList & Args)78f4a2713aSLionel Sambuc void CGNVCUDARuntime::EmitDeviceStubBody(CodeGenFunction &CGF,
79f4a2713aSLionel Sambuc                                          FunctionArgList &Args) {
80f4a2713aSLionel Sambuc   // Build the argument value list and the argument stack struct type.
81f4a2713aSLionel Sambuc   SmallVector<llvm::Value *, 16> ArgValues;
82f4a2713aSLionel Sambuc   std::vector<llvm::Type *> ArgTypes;
83f4a2713aSLionel Sambuc   for (FunctionArgList::const_iterator I = Args.begin(), E = Args.end();
84f4a2713aSLionel Sambuc        I != E; ++I) {
85f4a2713aSLionel Sambuc     llvm::Value *V = CGF.GetAddrOfLocalVar(*I);
86f4a2713aSLionel Sambuc     ArgValues.push_back(V);
87f4a2713aSLionel Sambuc     assert(isa<llvm::PointerType>(V->getType()) && "Arg type not PointerType");
88f4a2713aSLionel Sambuc     ArgTypes.push_back(cast<llvm::PointerType>(V->getType())->getElementType());
89f4a2713aSLionel Sambuc   }
90f4a2713aSLionel Sambuc   llvm::StructType *ArgStackTy = llvm::StructType::get(
91f4a2713aSLionel Sambuc       CGF.getLLVMContext(), ArgTypes);
92f4a2713aSLionel Sambuc 
93f4a2713aSLionel Sambuc   llvm::BasicBlock *EndBlock = CGF.createBasicBlock("setup.end");
94f4a2713aSLionel Sambuc 
95f4a2713aSLionel Sambuc   // Emit the calls to cudaSetupArgument
96f4a2713aSLionel Sambuc   llvm::Constant *cudaSetupArgFn = getSetupArgumentFn();
97f4a2713aSLionel Sambuc   for (unsigned I = 0, E = Args.size(); I != E; ++I) {
98f4a2713aSLionel Sambuc     llvm::Value *Args[3];
99f4a2713aSLionel Sambuc     llvm::BasicBlock *NextBlock = CGF.createBasicBlock("setup.next");
100f4a2713aSLionel Sambuc     Args[0] = CGF.Builder.CreatePointerCast(ArgValues[I], VoidPtrTy);
101f4a2713aSLionel Sambuc     Args[1] = CGF.Builder.CreateIntCast(
102f4a2713aSLionel Sambuc         llvm::ConstantExpr::getSizeOf(ArgTypes[I]),
103f4a2713aSLionel Sambuc         SizeTy, false);
104f4a2713aSLionel Sambuc     Args[2] = CGF.Builder.CreateIntCast(
105f4a2713aSLionel Sambuc         llvm::ConstantExpr::getOffsetOf(ArgStackTy, I),
106f4a2713aSLionel Sambuc         SizeTy, false);
107f4a2713aSLionel Sambuc     llvm::CallSite CS = CGF.EmitRuntimeCallOrInvoke(cudaSetupArgFn, Args);
108f4a2713aSLionel Sambuc     llvm::Constant *Zero = llvm::ConstantInt::get(IntTy, 0);
109f4a2713aSLionel Sambuc     llvm::Value *CSZero = CGF.Builder.CreateICmpEQ(CS.getInstruction(), Zero);
110f4a2713aSLionel Sambuc     CGF.Builder.CreateCondBr(CSZero, NextBlock, EndBlock);
111f4a2713aSLionel Sambuc     CGF.EmitBlock(NextBlock);
112f4a2713aSLionel Sambuc   }
113f4a2713aSLionel Sambuc 
114f4a2713aSLionel Sambuc   // Emit the call to cudaLaunch
115f4a2713aSLionel Sambuc   llvm::Constant *cudaLaunchFn = getLaunchFn();
116f4a2713aSLionel Sambuc   llvm::Value *Arg = CGF.Builder.CreatePointerCast(CGF.CurFn, CharPtrTy);
117f4a2713aSLionel Sambuc   CGF.EmitRuntimeCallOrInvoke(cudaLaunchFn, Arg);
118f4a2713aSLionel Sambuc   CGF.EmitBranch(EndBlock);
119f4a2713aSLionel Sambuc 
120f4a2713aSLionel Sambuc   CGF.EmitBlock(EndBlock);
121f4a2713aSLionel Sambuc }
122f4a2713aSLionel Sambuc 
CreateNVCUDARuntime(CodeGenModule & CGM)123f4a2713aSLionel Sambuc CGCUDARuntime *CodeGen::CreateNVCUDARuntime(CodeGenModule &CGM) {
124f4a2713aSLionel Sambuc   return new CGNVCUDARuntime(CGM);
125f4a2713aSLionel Sambuc }
126