1 //===----- CGCUDARuntime.h - Interface to CUDA Runtimes ---------*- C++ -*-===//
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 #ifndef LLVM_CLANG_LIB_CODEGEN_CGCUDARUNTIME_H
16 #define LLVM_CLANG_LIB_CODEGEN_CGCUDARUNTIME_H
17 
18 #include "llvm/ADT/StringRef.h"
19 
20 namespace llvm {
21 class Function;
22 class GlobalVariable;
23 }
24 
25 namespace clang {
26 
27 class CUDAKernelCallExpr;
28 class NamedDecl;
29 class VarDecl;
30 
31 namespace CodeGen {
32 
33 class CodeGenFunction;
34 class CodeGenModule;
35 class FunctionArgList;
36 class ReturnValueSlot;
37 class RValue;
38 
39 class CGCUDARuntime {
40 protected:
41   CodeGenModule &CGM;
42 
43 public:
44   // Global variable properties that must be passed to CUDA runtime.
45   class DeviceVarFlags {
46   public:
47     enum DeviceVarKind {
48       Variable, // Variable
49       Surface,  // Builtin surface
50       Texture,  // Builtin texture
51     };
52 
53   private:
54     unsigned Kind : 2;
55     unsigned Extern : 1;
56     unsigned Constant : 1;   // Constant variable.
57     unsigned Managed : 1;    // Managed variable.
58     unsigned Normalized : 1; // Normalized texture.
59     int SurfTexType;         // Type of surface/texutre.
60 
61   public:
62     DeviceVarFlags(DeviceVarKind K, bool E, bool C, bool M, bool N, int T)
63         : Kind(K), Extern(E), Constant(C), Managed(M), Normalized(N),
64           SurfTexType(T) {}
65 
66     DeviceVarKind getKind() const { return static_cast<DeviceVarKind>(Kind); }
67     bool isExtern() const { return Extern; }
68     bool isConstant() const { return Constant; }
69     bool isManaged() const { return Managed; }
70     bool isNormalized() const { return Normalized; }
71     int getSurfTexType() const { return SurfTexType; }
72   };
73 
74   CGCUDARuntime(CodeGenModule &CGM) : CGM(CGM) {}
75   virtual ~CGCUDARuntime();
76 
77   virtual RValue EmitCUDAKernelCallExpr(CodeGenFunction &CGF,
78                                         const CUDAKernelCallExpr *E,
79                                         ReturnValueSlot ReturnValue);
80 
81   /// Emits a kernel launch stub.
82   virtual void emitDeviceStub(CodeGenFunction &CGF, FunctionArgList &Args) = 0;
83   virtual void registerDeviceVar(const VarDecl *VD, llvm::GlobalVariable &Var,
84                                  bool Extern, bool Constant) = 0;
85   virtual void registerDeviceSurf(const VarDecl *VD, llvm::GlobalVariable &Var,
86                                   bool Extern, int Type) = 0;
87   virtual void registerDeviceTex(const VarDecl *VD, llvm::GlobalVariable &Var,
88                                  bool Extern, int Type, bool Normalized) = 0;
89 
90   /// Constructs and returns a module initialization function or nullptr if it's
91   /// not needed. Must be called after all kernels have been emitted.
92   virtual llvm::Function *makeModuleCtorFunction() = 0;
93 
94   /// Returns a module cleanup function or nullptr if it's not needed.
95   /// Must be called after ModuleCtorFunction
96   virtual llvm::Function *makeModuleDtorFunction() = 0;
97 
98   /// Returns function or variable name on device side even if the current
99   /// compilation is for host.
100   virtual std::string getDeviceSideName(const NamedDecl *ND) = 0;
101 };
102 
103 /// Creates an instance of a CUDA runtime class.
104 CGCUDARuntime *CreateNVCUDARuntime(CodeGenModule &CGM);
105 
106 }
107 }
108 
109 #endif
110