xref: /openbsd-src/gnu/llvm/clang/lib/CodeGen/CGHLSLRuntime.h (revision 12c855180aad702bbcca06e0398d774beeafb155)
1*12c85518Srobert //===----- CGHLSLRuntime.h - Interface to HLSL Runtimes -----*- C++ -*-===//
2*12c85518Srobert //
3*12c85518Srobert // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*12c85518Srobert // See https://llvm.org/LICENSE.txt for license information.
5*12c85518Srobert // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*12c85518Srobert //
7*12c85518Srobert //===----------------------------------------------------------------------===//
8*12c85518Srobert //
9*12c85518Srobert // This provides an abstract class for HLSL code generation.  Concrete
10*12c85518Srobert // subclasses of this implement code generation for specific HLSL
11*12c85518Srobert // runtime libraries.
12*12c85518Srobert //
13*12c85518Srobert //===----------------------------------------------------------------------===//
14*12c85518Srobert 
15*12c85518Srobert #ifndef LLVM_CLANG_LIB_CODEGEN_CGHLSLRUNTIME_H
16*12c85518Srobert #define LLVM_CLANG_LIB_CODEGEN_CGHLSLRUNTIME_H
17*12c85518Srobert 
18*12c85518Srobert #include "llvm/IR/IRBuilder.h"
19*12c85518Srobert 
20*12c85518Srobert #include "clang/Basic/HLSLRuntime.h"
21*12c85518Srobert 
22*12c85518Srobert #include "llvm/ADT/SmallVector.h"
23*12c85518Srobert #include "llvm/ADT/StringRef.h"
24*12c85518Srobert #include "llvm/Frontend/HLSL/HLSLResource.h"
25*12c85518Srobert 
26*12c85518Srobert #include <optional>
27*12c85518Srobert #include <vector>
28*12c85518Srobert 
29*12c85518Srobert namespace llvm {
30*12c85518Srobert class GlobalVariable;
31*12c85518Srobert class Function;
32*12c85518Srobert class StructType;
33*12c85518Srobert } // namespace llvm
34*12c85518Srobert 
35*12c85518Srobert namespace clang {
36*12c85518Srobert class VarDecl;
37*12c85518Srobert class ParmVarDecl;
38*12c85518Srobert class HLSLBufferDecl;
39*12c85518Srobert class HLSLResourceBindingAttr;
40*12c85518Srobert class Type;
41*12c85518Srobert class DeclContext;
42*12c85518Srobert 
43*12c85518Srobert class FunctionDecl;
44*12c85518Srobert 
45*12c85518Srobert namespace CodeGen {
46*12c85518Srobert 
47*12c85518Srobert class CodeGenModule;
48*12c85518Srobert 
49*12c85518Srobert class CGHLSLRuntime {
50*12c85518Srobert public:
51*12c85518Srobert   struct BufferResBinding {
52*12c85518Srobert     // The ID like 2 in register(b2, space1).
53*12c85518Srobert     std::optional<unsigned> Reg;
54*12c85518Srobert     // The Space like 1 is register(b2, space1).
55*12c85518Srobert     // Default value is 0.
56*12c85518Srobert     unsigned Space;
57*12c85518Srobert     BufferResBinding(HLSLResourceBindingAttr *Attr);
58*12c85518Srobert   };
59*12c85518Srobert   struct Buffer {
60*12c85518Srobert     Buffer(const HLSLBufferDecl *D);
61*12c85518Srobert     llvm::StringRef Name;
62*12c85518Srobert     // IsCBuffer - Whether the buffer is a cbuffer (and not a tbuffer).
63*12c85518Srobert     bool IsCBuffer;
64*12c85518Srobert     BufferResBinding Binding;
65*12c85518Srobert     // Global variable and offset for each constant.
66*12c85518Srobert     std::vector<std::pair<llvm::GlobalVariable *, unsigned>> Constants;
67*12c85518Srobert     llvm::StructType *LayoutStruct = nullptr;
68*12c85518Srobert   };
69*12c85518Srobert 
70*12c85518Srobert protected:
71*12c85518Srobert   CodeGenModule &CGM;
72*12c85518Srobert 
73*12c85518Srobert   llvm::Value *emitInputSemantic(llvm::IRBuilder<> &B, const ParmVarDecl &D,
74*12c85518Srobert                                  llvm::Type *Ty);
75*12c85518Srobert 
76*12c85518Srobert public:
CGHLSLRuntime(CodeGenModule & CGM)77*12c85518Srobert   CGHLSLRuntime(CodeGenModule &CGM) : CGM(CGM) {}
~CGHLSLRuntime()78*12c85518Srobert   virtual ~CGHLSLRuntime() {}
79*12c85518Srobert 
80*12c85518Srobert   void annotateHLSLResource(const VarDecl *D, llvm::GlobalVariable *GV);
81*12c85518Srobert   void generateGlobalCtorDtorCalls();
82*12c85518Srobert 
83*12c85518Srobert   void addBuffer(const HLSLBufferDecl *D);
84*12c85518Srobert   void finishCodeGen();
85*12c85518Srobert 
86*12c85518Srobert   void setHLSLEntryAttributes(const FunctionDecl *FD, llvm::Function *Fn);
87*12c85518Srobert 
88*12c85518Srobert   void emitEntryFunction(const FunctionDecl *FD, llvm::Function *Fn);
89*12c85518Srobert   void setHLSLFunctionAttributes(llvm::Function *, const FunctionDecl *);
90*12c85518Srobert 
91*12c85518Srobert private:
92*12c85518Srobert   void addBufferResourceAnnotation(llvm::GlobalVariable *GV,
93*12c85518Srobert                                    llvm::StringRef TyName,
94*12c85518Srobert                                    llvm::hlsl::ResourceClass RC,
95*12c85518Srobert                                    llvm::hlsl::ResourceKind RK,
96*12c85518Srobert                                    BufferResBinding &Binding);
97*12c85518Srobert   void addConstant(VarDecl *D, Buffer &CB);
98*12c85518Srobert   void addBufferDecls(const DeclContext *DC, Buffer &CB);
99*12c85518Srobert   llvm::SmallVector<Buffer> Buffers;
100*12c85518Srobert };
101*12c85518Srobert 
102*12c85518Srobert } // namespace CodeGen
103*12c85518Srobert } // namespace clang
104*12c85518Srobert 
105*12c85518Srobert #endif
106