[RISCV][FMV] Support target_clones (#85786)
[llvm-project.git] / clang / lib / CodeGen / CGHLSLRuntime.h
bloba8aabca7348ffb54140034d6b1aa6b6c27257a28
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.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_CLANG_LIB_CODEGEN_CGHLSLRUNTIME_H
16 #define LLVM_CLANG_LIB_CODEGEN_CGHLSLRUNTIME_H
18 #include "llvm/IR/IRBuilder.h"
19 #include "llvm/IR/Intrinsics.h"
20 #include "llvm/IR/IntrinsicsDirectX.h"
21 #include "llvm/IR/IntrinsicsSPIRV.h"
23 #include "clang/Basic/Builtins.h"
24 #include "clang/Basic/HLSLRuntime.h"
26 #include "llvm/ADT/SmallVector.h"
27 #include "llvm/ADT/StringRef.h"
28 #include "llvm/Frontend/HLSL/HLSLResource.h"
30 #include <optional>
31 #include <vector>
33 // A function generator macro for picking the right intrinsic
34 // for the target backend
35 #define GENERATE_HLSL_INTRINSIC_FUNCTION(FunctionName, IntrinsicPostfix) \
36 llvm::Intrinsic::ID get##FunctionName##Intrinsic() { \
37 llvm::Triple::ArchType Arch = getArch(); \
38 switch (Arch) { \
39 case llvm::Triple::dxil: \
40 return llvm::Intrinsic::dx_##IntrinsicPostfix; \
41 case llvm::Triple::spirv: \
42 return llvm::Intrinsic::spv_##IntrinsicPostfix; \
43 default: \
44 llvm_unreachable("Intrinsic " #IntrinsicPostfix \
45 " not supported by target architecture"); \
46 } \
49 namespace llvm {
50 class GlobalVariable;
51 class Function;
52 class StructType;
53 } // namespace llvm
55 namespace clang {
56 class VarDecl;
57 class ParmVarDecl;
58 class HLSLBufferDecl;
59 class HLSLResourceBindingAttr;
60 class Type;
61 class DeclContext;
63 class FunctionDecl;
65 namespace CodeGen {
67 class CodeGenModule;
69 class CGHLSLRuntime {
70 public:
71 //===----------------------------------------------------------------------===//
72 // Start of reserved area for HLSL intrinsic getters.
73 //===----------------------------------------------------------------------===//
75 GENERATE_HLSL_INTRINSIC_FUNCTION(All, all)
76 GENERATE_HLSL_INTRINSIC_FUNCTION(Any, any)
77 GENERATE_HLSL_INTRINSIC_FUNCTION(Frac, frac)
78 GENERATE_HLSL_INTRINSIC_FUNCTION(Length, length)
79 GENERATE_HLSL_INTRINSIC_FUNCTION(Lerp, lerp)
80 GENERATE_HLSL_INTRINSIC_FUNCTION(Normalize, normalize)
81 GENERATE_HLSL_INTRINSIC_FUNCTION(Rsqrt, rsqrt)
82 GENERATE_HLSL_INTRINSIC_FUNCTION(Saturate, saturate)
83 GENERATE_HLSL_INTRINSIC_FUNCTION(Sign, sign)
84 GENERATE_HLSL_INTRINSIC_FUNCTION(Step, step)
85 GENERATE_HLSL_INTRINSIC_FUNCTION(ThreadId, thread_id)
86 GENERATE_HLSL_INTRINSIC_FUNCTION(FDot, fdot)
87 GENERATE_HLSL_INTRINSIC_FUNCTION(SDot, sdot)
88 GENERATE_HLSL_INTRINSIC_FUNCTION(UDot, udot)
89 GENERATE_HLSL_INTRINSIC_FUNCTION(WaveIsFirstLane, wave_is_first_lane)
91 //===----------------------------------------------------------------------===//
92 // End of reserved area for HLSL intrinsic getters.
93 //===----------------------------------------------------------------------===//
95 struct BufferResBinding {
96 // The ID like 2 in register(b2, space1).
97 std::optional<unsigned> Reg;
98 // The Space like 1 is register(b2, space1).
99 // Default value is 0.
100 unsigned Space;
101 BufferResBinding(HLSLResourceBindingAttr *Attr);
103 struct Buffer {
104 Buffer(const HLSLBufferDecl *D);
105 llvm::StringRef Name;
106 // IsCBuffer - Whether the buffer is a cbuffer (and not a tbuffer).
107 bool IsCBuffer;
108 BufferResBinding Binding;
109 // Global variable and offset for each constant.
110 std::vector<std::pair<llvm::GlobalVariable *, unsigned>> Constants;
111 llvm::StructType *LayoutStruct = nullptr;
114 protected:
115 CodeGenModule &CGM;
117 llvm::Value *emitInputSemantic(llvm::IRBuilder<> &B, const ParmVarDecl &D,
118 llvm::Type *Ty);
120 public:
121 CGHLSLRuntime(CodeGenModule &CGM) : CGM(CGM) {}
122 virtual ~CGHLSLRuntime() {}
124 llvm::Type *convertHLSLSpecificType(const Type *T);
126 void annotateHLSLResource(const VarDecl *D, llvm::GlobalVariable *GV);
127 void generateGlobalCtorDtorCalls();
129 void addBuffer(const HLSLBufferDecl *D);
130 void finishCodeGen();
132 void setHLSLEntryAttributes(const FunctionDecl *FD, llvm::Function *Fn);
134 void emitEntryFunction(const FunctionDecl *FD, llvm::Function *Fn);
135 void setHLSLFunctionAttributes(const FunctionDecl *FD, llvm::Function *Fn);
137 private:
138 void addBufferResourceAnnotation(llvm::GlobalVariable *GV,
139 llvm::hlsl::ResourceClass RC,
140 llvm::hlsl::ResourceKind RK, bool IsROV,
141 llvm::hlsl::ElementType ET,
142 BufferResBinding &Binding);
143 void addConstant(VarDecl *D, Buffer &CB);
144 void addBufferDecls(const DeclContext *DC, Buffer &CB);
145 llvm::Triple::ArchType getArch();
146 llvm::SmallVector<Buffer> Buffers;
149 } // namespace CodeGen
150 } // namespace clang
152 #endif