1 //===----- CGOpenCLRuntime.h - Interface to OpenCL Runtimes -----*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This provides an abstract class for OpenCL code generation.  Concrete
11 // subclasses of this implement code generation for specific OpenCL
12 // runtime libraries.
13 //
14 //===----------------------------------------------------------------------===//
15 
16 #ifndef LLVM_CLANG_LIB_CODEGEN_CGOPENCLRUNTIME_H
17 #define LLVM_CLANG_LIB_CODEGEN_CGOPENCLRUNTIME_H
18 
19 #include "clang/AST/Type.h"
20 #include "llvm/ADT/DenseMap.h"
21 #include "llvm/IR/Type.h"
22 #include "llvm/IR/Value.h"
23 
24 namespace clang {
25 
26 class BlockExpr;
27 class Expr;
28 class VarDecl;
29 
30 namespace CodeGen {
31 
32 class CodeGenFunction;
33 class CodeGenModule;
34 
35 class CGOpenCLRuntime {
36 protected:
37   CodeGenModule &CGM;
38   llvm::Type *PipeTy;
39   llvm::PointerType *SamplerTy;
40 
41   /// Structure for enqueued block information.
42   struct EnqueuedBlockInfo {
43     llvm::Function *InvokeFunc; /// Block invoke function.
44     llvm::Function *Kernel;     /// Enqueued block kernel.
45     llvm::Value *BlockArg;      /// The first argument to enqueued block kernel.
46   };
47   /// Maps block expression to block information.
48   llvm::DenseMap<const Expr *, EnqueuedBlockInfo> EnqueuedBlockMap;
49 
50 public:
51   CGOpenCLRuntime(CodeGenModule &CGM) : CGM(CGM), PipeTy(nullptr),
52     SamplerTy(nullptr) {}
53   virtual ~CGOpenCLRuntime();
54 
55   /// Emit the IR required for a work-group-local variable declaration, and add
56   /// an entry to CGF's LocalDeclMap for D.  The base class does this using
57   /// CodeGenFunction::EmitStaticVarDecl to emit an internal global for D.
58   virtual void EmitWorkGroupLocalVarDecl(CodeGenFunction &CGF,
59                                          const VarDecl &D);
60 
61   virtual llvm::Type *convertOpenCLSpecificType(const Type *T);
62 
63   virtual llvm::Type *getPipeType(const PipeType *T);
64 
65   llvm::PointerType *getSamplerType(const Type *T);
66 
67   // \brief Returnes a value which indicates the size in bytes of the pipe
68   // element.
69   virtual llvm::Value *getPipeElemSize(const Expr *PipeArg);
70 
71   // \brief Returnes a value which indicates the alignment in bytes of the pipe
72   // element.
73   virtual llvm::Value *getPipeElemAlign(const Expr *PipeArg);
74 
75   /// \return __generic void* type.
76   llvm::PointerType *getGenericVoidPointerType();
77 
78   /// \return enqueued block information for enqueued block.
79   EnqueuedBlockInfo emitOpenCLEnqueuedBlock(CodeGenFunction &CGF,
80                                             const Expr *E);
81 
82   /// \brief Record invoke function and block literal emitted during normal
83   /// codegen for a block expression. The information is used by
84   /// emitOpenCLEnqueuedBlock to emit wrapper kernel.
85   ///
86   /// \param InvokeF invoke function emitted for the block expression.
87   /// \param Block block literal emitted for the block expression.
88   void recordBlockInfo(const BlockExpr *E, llvm::Function *InvokeF,
89                        llvm::Value *Block);
90 
91   /// \return LLVM block invoke function emitted for an expression derived from
92   /// the block expression.
93   llvm::Function *getInvokeFunction(const Expr *E);
94 };
95 
96 }
97 }
98 
99 #endif
100