1 //===----- CGHLSLRuntime.h - Interface to HLSL Runtimes -----*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // This provides an abstract class for HLSL code generation. Concrete
10 // subclasses of this implement code generation for specific HLSL
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"
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(); \
39 case llvm::Triple::dxil: \
40 return llvm::Intrinsic::dx_##IntrinsicPostfix; \
41 case llvm::Triple::spirv: \
42 return llvm::Intrinsic::spv_##IntrinsicPostfix; \
44 llvm_unreachable("Intrinsic " #IntrinsicPostfix \
45 " not supported by target architecture"); \
59 class HLSLResourceBindingAttr
;
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(Lerp
, lerp
)
78 GENERATE_HLSL_INTRINSIC_FUNCTION(Rsqrt
, rsqrt
)
79 GENERATE_HLSL_INTRINSIC_FUNCTION(ThreadId
, thread_id
)
81 //===----------------------------------------------------------------------===//
82 // End of reserved area for HLSL intrinsic getters.
83 //===----------------------------------------------------------------------===//
85 struct BufferResBinding
{
86 // The ID like 2 in register(b2, space1).
87 std::optional
<unsigned> Reg
;
88 // The Space like 1 is register(b2, space1).
89 // Default value is 0.
91 BufferResBinding(HLSLResourceBindingAttr
*Attr
);
94 Buffer(const HLSLBufferDecl
*D
);
96 // IsCBuffer - Whether the buffer is a cbuffer (and not a tbuffer).
98 BufferResBinding Binding
;
99 // Global variable and offset for each constant.
100 std::vector
<std::pair
<llvm::GlobalVariable
*, unsigned>> Constants
;
101 llvm::StructType
*LayoutStruct
= nullptr;
107 llvm::Value
*emitInputSemantic(llvm::IRBuilder
<> &B
, const ParmVarDecl
&D
,
111 CGHLSLRuntime(CodeGenModule
&CGM
) : CGM(CGM
) {}
112 virtual ~CGHLSLRuntime() {}
114 void annotateHLSLResource(const VarDecl
*D
, llvm::GlobalVariable
*GV
);
115 void generateGlobalCtorDtorCalls();
117 void addBuffer(const HLSLBufferDecl
*D
);
118 void finishCodeGen();
120 void setHLSLEntryAttributes(const FunctionDecl
*FD
, llvm::Function
*Fn
);
122 void emitEntryFunction(const FunctionDecl
*FD
, llvm::Function
*Fn
);
123 void setHLSLFunctionAttributes(llvm::Function
*, const FunctionDecl
*);
126 void addBufferResourceAnnotation(llvm::GlobalVariable
*GV
,
127 llvm::hlsl::ResourceClass RC
,
128 llvm::hlsl::ResourceKind RK
, bool IsROV
,
129 llvm::hlsl::ElementType ET
,
130 BufferResBinding
&Binding
);
131 void addConstant(VarDecl
*D
, Buffer
&CB
);
132 void addBufferDecls(const DeclContext
*DC
, Buffer
&CB
);
133 llvm::Triple::ArchType
getArch();
134 llvm::SmallVector
<Buffer
> Buffers
;
137 } // namespace CodeGen