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
20namespace llvm {
21class Function;
22class GlobalVariable;
23}
24
25namespace clang {
26
27class CUDAKernelCallExpr;
28class VarDecl;
29
30namespace CodeGen {
31
32class CodeGenFunction;
33class CodeGenModule;
34class FunctionArgList;
35class ReturnValueSlot;
36class RValue;
37
38class CGCUDARuntime {
39protected:
40  CodeGenModule &CGM;
41
42public:
43  // Global variable properties that must be passed to CUDA runtime.
44  enum DeviceVarFlags {
45    ExternDeviceVar = 0x01,   // extern
46    ConstantDeviceVar = 0x02, // __constant__
47  };
48
49  CGCUDARuntime(CodeGenModule &CGM) : CGM(CGM) {}
50  virtual ~CGCUDARuntime();
51
52  virtual RValue EmitCUDAKernelCallExpr(CodeGenFunction &CGF,
53                                        const CUDAKernelCallExpr *E,
54                                        ReturnValueSlot ReturnValue);
55
56  /// Emits a kernel launch stub.
57  virtual void emitDeviceStub(CodeGenFunction &CGF, FunctionArgList &Args) = 0;
58  virtual void registerDeviceVar(const VarDecl *VD, llvm::GlobalVariable &Var,
59                                 unsigned Flags) = 0;
60
61  /// Constructs and returns a module initialization function or nullptr if it's
62  /// not needed. Must be called after all kernels have been emitted.
63  virtual llvm::Function *makeModuleCtorFunction() = 0;
64
65  /// Returns a module cleanup function or nullptr if it's not needed.
66  /// Must be called after ModuleCtorFunction
67  virtual llvm::Function *makeModuleDtorFunction() = 0;
68
69  /// Construct and return the stub name of a kernel.
70  virtual std::string getDeviceStubName(llvm::StringRef Name) const = 0;
71};
72
73/// Creates an instance of a CUDA runtime class.
74CGCUDARuntime *CreateNVCUDARuntime(CodeGenModule &CGM);
75
76}
77}
78
79#endif
80