CGCUDARuntime.h 3.6 KB

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