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