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 #include "llvm/IR/GlobalValue.h"
20 
21 namespace llvm {
22 class Function;
23 class GlobalVariable;
24 }
25 
26 namespace clang {
27 
28 class CUDAKernelCallExpr;
29 class NamedDecl;
30 class VarDecl;
31 
32 namespace CodeGen {
33 
34 class CodeGenFunction;
35 class CodeGenModule;
36 class FunctionArgList;
37 class ReturnValueSlot;
38 class RValue;
39 
40 class CGCUDARuntime {
41 protected:
42   CodeGenModule &CGM;
43 
44 public:
45   // Global variable properties that must be passed to CUDA runtime.
46   class DeviceVarFlags {
47   public:
48     enum DeviceVarKind {
49       Variable, // Variable
50       Surface,  // Builtin surface
51       Texture,  // Builtin texture
52     };
53 
54   private:
55     unsigned Kind : 2;
56     unsigned Extern : 1;
57     unsigned Constant : 1;   // Constant variable.
58     unsigned Managed : 1;    // Managed variable.
59     unsigned Normalized : 1; // Normalized texture.
60     int SurfTexType;         // Type of surface/texutre.
61 
62   public:
63     DeviceVarFlags(DeviceVarKind K, bool E, bool C, bool M, bool N, int T)
64         : Kind(K), Extern(E), Constant(C), Managed(M), Normalized(N),
65           SurfTexType(T) {}
66 
67     DeviceVarKind getKind() const { return static_cast<DeviceVarKind>(Kind); }
68     bool isExtern() const { return Extern; }
69     bool isConstant() const { return Constant; }
70     bool isManaged() const { return Managed; }
71     bool isNormalized() const { return Normalized; }
72     int getSurfTexType() const { return SurfTexType; }
73   };
74 
75   CGCUDARuntime(CodeGenModule &CGM) : CGM(CGM) {}
76   virtual ~CGCUDARuntime();
77 
78   virtual RValue EmitCUDAKernelCallExpr(CodeGenFunction &CGF,
79                                         const CUDAKernelCallExpr *E,
80                                         ReturnValueSlot ReturnValue);
81 
82   /// Emits a kernel launch stub.
83   virtual void emitDeviceStub(CodeGenFunction &CGF, FunctionArgList &Args) = 0;
84 
85   /// Check whether a variable is a device variable and register it if true.
86   virtual void handleVarRegistration(const VarDecl *VD,
87                                      llvm::GlobalVariable &Var) = 0;
88 
89   /// Finalize generated LLVM module. Returns a module constructor function
90   /// to be added or a null pointer.
91   virtual llvm::Function *finalizeModule() = 0;
92 
93   /// Returns function or variable name on device side even if the current
94   /// compilation is for host.
95   virtual std::string getDeviceSideName(const NamedDecl *ND) = 0;
96 
97   /// Adjust linkage of shadow variables in host compilation.
98   virtual void
99   internalizeDeviceSideVar(const VarDecl *D,
100                            llvm::GlobalValue::LinkageTypes &Linkage) = 0;
101 };
102 
103 /// Creates an instance of a CUDA runtime class.
104 CGCUDARuntime *CreateNVCUDARuntime(CodeGenModule &CGM);
105 
106 }
107 }
108 
109 #endif
110