CGHLSLRuntime.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. //===----- CGHLSLRuntime.h - Interface to HLSL 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 HLSL code generation. Concrete
  10. // subclasses of this implement code generation for specific HLSL
  11. // runtime libraries.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #ifndef LLVM_CLANG_LIB_CODEGEN_CGHLSLRUNTIME_H
  15. #define LLVM_CLANG_LIB_CODEGEN_CGHLSLRUNTIME_H
  16. #include "llvm/IR/IRBuilder.h"
  17. #include "clang/Basic/HLSLRuntime.h"
  18. #include "llvm/ADT/SmallVector.h"
  19. #include "llvm/ADT/StringRef.h"
  20. #include "llvm/Frontend/HLSL/HLSLResource.h"
  21. #include <optional>
  22. #include <vector>
  23. namespace llvm {
  24. class GlobalVariable;
  25. class Function;
  26. class StructType;
  27. } // namespace llvm
  28. namespace clang {
  29. class VarDecl;
  30. class ParmVarDecl;
  31. class HLSLBufferDecl;
  32. class HLSLResourceBindingAttr;
  33. class Type;
  34. class DeclContext;
  35. class FunctionDecl;
  36. namespace CodeGen {
  37. class CodeGenModule;
  38. class CGHLSLRuntime {
  39. public:
  40. struct BufferResBinding {
  41. // The ID like 2 in register(b2, space1).
  42. std::optional<unsigned> Reg;
  43. // The Space like 1 is register(b2, space1).
  44. // Default value is 0.
  45. unsigned Space;
  46. BufferResBinding(HLSLResourceBindingAttr *Attr);
  47. };
  48. struct Buffer {
  49. Buffer(const HLSLBufferDecl *D);
  50. llvm::StringRef Name;
  51. // IsCBuffer - Whether the buffer is a cbuffer (and not a tbuffer).
  52. bool IsCBuffer;
  53. BufferResBinding Binding;
  54. // Global variable and offset for each constant.
  55. std::vector<std::pair<llvm::GlobalVariable *, unsigned>> Constants;
  56. llvm::StructType *LayoutStruct = nullptr;
  57. };
  58. protected:
  59. CodeGenModule &CGM;
  60. llvm::Value *emitInputSemantic(llvm::IRBuilder<> &B, const ParmVarDecl &D,
  61. llvm::Type *Ty);
  62. public:
  63. CGHLSLRuntime(CodeGenModule &CGM) : CGM(CGM) {}
  64. virtual ~CGHLSLRuntime() {}
  65. void annotateHLSLResource(const VarDecl *D, llvm::GlobalVariable *GV);
  66. void generateGlobalCtorDtorCalls();
  67. void addBuffer(const HLSLBufferDecl *D);
  68. void finishCodeGen();
  69. void setHLSLEntryAttributes(const FunctionDecl *FD, llvm::Function *Fn);
  70. void emitEntryFunction(const FunctionDecl *FD, llvm::Function *Fn);
  71. void setHLSLFunctionAttributes(llvm::Function *, const FunctionDecl *);
  72. private:
  73. void addBufferResourceAnnotation(llvm::GlobalVariable *GV,
  74. llvm::StringRef TyName,
  75. llvm::hlsl::ResourceClass RC,
  76. llvm::hlsl::ResourceKind RK,
  77. BufferResBinding &Binding);
  78. void addConstant(VarDecl *D, Buffer &CB);
  79. void addBufferDecls(const DeclContext *DC, Buffer &CB);
  80. llvm::SmallVector<Buffer> Buffers;
  81. };
  82. } // namespace CodeGen
  83. } // namespace clang
  84. #endif