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