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
15#ifndef LLVM_CLANG_LIB_CODEGEN_CGHLSLRUNTIME_H
16#define LLVM_CLANG_LIB_CODEGEN_CGHLSLRUNTIME_H
17
18#include "Address.h"
19#include "clang/AST/Attr.h"
20#include "clang/AST/Decl.h"
21#include "clang/Basic/Builtins.h"
22#include "clang/Basic/HLSLRuntime.h"
23#include "llvm/ADT/DenseMap.h"
24#include "llvm/ADT/SmallVector.h"
25#include "llvm/ADT/StringRef.h"
26#include "llvm/Frontend/HLSL/HLSLResource.h"
27#include "llvm/IR/IRBuilder.h"
28#include "llvm/IR/Intrinsics.h"
29#include "llvm/IR/IntrinsicsDirectX.h"
30#include "llvm/IR/IntrinsicsSPIRV.h"
31
32#include <optional>
33#include <vector>
34
35// A function generator macro for picking the right intrinsic
36// for the target backend
37#define GENERATE_HLSL_INTRINSIC_FUNCTION(FunctionName, IntrinsicPostfix) \
38 llvm::Intrinsic::ID get##FunctionName##Intrinsic() { \
39 llvm::Triple::ArchType Arch = getArch(); \
40 switch (Arch) { \
41 case llvm::Triple::dxil: \
42 return llvm::Intrinsic::dx_##IntrinsicPostfix; \
43 case llvm::Triple::spirv: \
44 return llvm::Intrinsic::spv_##IntrinsicPostfix; \
45 default: \
46 llvm_unreachable("Intrinsic " #IntrinsicPostfix \
47 " not supported by target architecture"); \
48 } \
49 }
50
51using ResourceClass = llvm::dxil::ResourceClass;
52
53namespace llvm {
54class GlobalVariable;
55class Function;
56class StructType;
57class Metadata;
58} // namespace llvm
59
60namespace clang {
61class NamedDecl;
62class VarDecl;
63class ParmVarDecl;
64class InitListExpr;
65class HLSLBufferDecl;
66class HLSLRootSignatureDecl;
67class HLSLVkBindingAttr;
68class HLSLResourceBindingAttr;
69class Type;
70class RecordType;
71class DeclContext;
72class HLSLPackOffsetAttr;
73class ArraySubscriptExpr;
74
75class FunctionDecl;
76
77namespace CodeGen {
78
79class CodeGenModule;
80class CodeGenFunction;
81class LValue;
82
83class CGHLSLOffsetInfo {
84 SmallVector<uint32_t> Offsets;
85
86public:
87 static const uint32_t Unspecified = ~0U;
88
89 /// Iterates over all declarations in the HLSL buffer and based on the
90 /// packoffset or register(c#) annotations it fills outs the Offsets vector
91 /// with the user-specified layout offsets. The buffer offsets can be
92 /// specified 2 ways: 1. declarations in cbuffer {} block can have a
93 /// packoffset annotation (translates to HLSLPackOffsetAttr) 2. default
94 /// constant buffer declarations at global scope can have register(c#)
95 /// annotations (translates to HLSLResourceBindingAttr with RegisterType::C)
96 /// It is not guaranteed that all declarations in a buffer have an annotation.
97 /// For those where it is not specified a `~0U` value is added to the Offsets
98 /// vector. In the final layout these declarations will be placed at the end
99 /// of the HLSL buffer after all of the elements with specified offset.
100 static CGHLSLOffsetInfo fromDecl(const HLSLBufferDecl &BufDecl);
101
102 /// Comparison function for offsets received from `operator[]` suitable for
103 /// use in a `stable_sort`. This will order implicit bindings after explicit
104 /// offsets.
105 static bool compareOffsets(uint32_t LHS, uint32_t RHS) { return LHS < RHS; }
106
107 /// Get the given offset, or `~0U` if there is no offset for the member.
108 uint32_t operator[](size_t I) const {
109 if (Offsets.empty())
110 return Unspecified;
111 return Offsets[I];
112 }
113
114 bool empty() const { return Offsets.empty(); }
115};
116
117class CGHLSLRuntime {
118public:
119 //===----------------------------------------------------------------------===//
120 // Start of reserved area for HLSL intrinsic getters.
121 //===----------------------------------------------------------------------===//
122
123 GENERATE_HLSL_INTRINSIC_FUNCTION(All, all)
124 GENERATE_HLSL_INTRINSIC_FUNCTION(Any, any)
125 GENERATE_HLSL_INTRINSIC_FUNCTION(Cross, cross)
126 GENERATE_HLSL_INTRINSIC_FUNCTION(Degrees, degrees)
127 GENERATE_HLSL_INTRINSIC_FUNCTION(Frac, frac)
128 GENERATE_HLSL_INTRINSIC_FUNCTION(FlattenedThreadIdInGroup,
129 flattened_thread_id_in_group)
130 GENERATE_HLSL_INTRINSIC_FUNCTION(IsInf, isinf)
131 GENERATE_HLSL_INTRINSIC_FUNCTION(IsNaN, isnan)
132 GENERATE_HLSL_INTRINSIC_FUNCTION(Lerp, lerp)
133 GENERATE_HLSL_INTRINSIC_FUNCTION(Normalize, normalize)
134 GENERATE_HLSL_INTRINSIC_FUNCTION(Rsqrt, rsqrt)
135 GENERATE_HLSL_INTRINSIC_FUNCTION(Saturate, saturate)
136 GENERATE_HLSL_INTRINSIC_FUNCTION(Sign, sign)
137 GENERATE_HLSL_INTRINSIC_FUNCTION(Step, step)
138 GENERATE_HLSL_INTRINSIC_FUNCTION(Radians, radians)
139 GENERATE_HLSL_INTRINSIC_FUNCTION(ThreadId, thread_id)
140 GENERATE_HLSL_INTRINSIC_FUNCTION(GroupThreadId, thread_id_in_group)
141 GENERATE_HLSL_INTRINSIC_FUNCTION(GroupId, group_id)
142 GENERATE_HLSL_INTRINSIC_FUNCTION(FDot, fdot)
143 GENERATE_HLSL_INTRINSIC_FUNCTION(SDot, sdot)
144 GENERATE_HLSL_INTRINSIC_FUNCTION(UDot, udot)
145 GENERATE_HLSL_INTRINSIC_FUNCTION(Dot4AddI8Packed, dot4add_i8packed)
146 GENERATE_HLSL_INTRINSIC_FUNCTION(Dot4AddU8Packed, dot4add_u8packed)
147 GENERATE_HLSL_INTRINSIC_FUNCTION(WaveActiveAllTrue, wave_all)
148 GENERATE_HLSL_INTRINSIC_FUNCTION(WaveActiveAnyTrue, wave_any)
149 GENERATE_HLSL_INTRINSIC_FUNCTION(WaveActiveMax, wave_reduce_max)
150 GENERATE_HLSL_INTRINSIC_FUNCTION(WaveActiveUMax, wave_reduce_umax)
151 GENERATE_HLSL_INTRINSIC_FUNCTION(WaveActiveMin, wave_reduce_min)
152 GENERATE_HLSL_INTRINSIC_FUNCTION(WaveActiveUMin, wave_reduce_umin)
153 GENERATE_HLSL_INTRINSIC_FUNCTION(WaveActiveCountBits, wave_active_countbits)
154 GENERATE_HLSL_INTRINSIC_FUNCTION(WaveIsFirstLane, wave_is_first_lane)
155 GENERATE_HLSL_INTRINSIC_FUNCTION(WaveGetLaneCount, wave_get_lane_count)
156 GENERATE_HLSL_INTRINSIC_FUNCTION(WaveReadLaneAt, wave_readlane)
157 GENERATE_HLSL_INTRINSIC_FUNCTION(FirstBitUHigh, firstbituhigh)
158 GENERATE_HLSL_INTRINSIC_FUNCTION(FirstBitSHigh, firstbitshigh)
159 GENERATE_HLSL_INTRINSIC_FUNCTION(FirstBitLow, firstbitlow)
160 GENERATE_HLSL_INTRINSIC_FUNCTION(NClamp, nclamp)
161 GENERATE_HLSL_INTRINSIC_FUNCTION(SClamp, sclamp)
162 GENERATE_HLSL_INTRINSIC_FUNCTION(UClamp, uclamp)
163
164 GENERATE_HLSL_INTRINSIC_FUNCTION(CreateResourceGetPointer,
165 resource_getpointer)
166 GENERATE_HLSL_INTRINSIC_FUNCTION(Sample, resource_sample)
167 GENERATE_HLSL_INTRINSIC_FUNCTION(SampleClamp, resource_sample_clamp)
168 GENERATE_HLSL_INTRINSIC_FUNCTION(CreateHandleFromBinding,
169 resource_handlefrombinding)
170 GENERATE_HLSL_INTRINSIC_FUNCTION(CreateHandleFromImplicitBinding,
171 resource_handlefromimplicitbinding)
172 GENERATE_HLSL_INTRINSIC_FUNCTION(NonUniformResourceIndex,
173 resource_nonuniformindex)
174 GENERATE_HLSL_INTRINSIC_FUNCTION(BufferUpdateCounter, resource_updatecounter)
175 GENERATE_HLSL_INTRINSIC_FUNCTION(GroupMemoryBarrierWithGroupSync,
176 group_memory_barrier_with_group_sync)
177 GENERATE_HLSL_INTRINSIC_FUNCTION(GetDimensionsX, resource_getdimensions_x)
178 GENERATE_HLSL_INTRINSIC_FUNCTION(DdxCoarse, ddx_coarse)
179 GENERATE_HLSL_INTRINSIC_FUNCTION(DdyCoarse, ddy_coarse)
180 GENERATE_HLSL_INTRINSIC_FUNCTION(DdxFine, ddx_fine)
181 GENERATE_HLSL_INTRINSIC_FUNCTION(DdyFine, ddy_fine)
182
183 //===----------------------------------------------------------------------===//
184 // End of reserved area for HLSL intrinsic getters.
185 //===----------------------------------------------------------------------===//
186
187protected:
188 CodeGenModule &CGM;
189
190 llvm::Value *emitSystemSemanticLoad(llvm::IRBuilder<> &B,
191 const FunctionDecl *FD, llvm::Type *Type,
192 const clang::DeclaratorDecl *Decl,
193 HLSLAppliedSemanticAttr *Semantic,
194 std::optional<unsigned> Index);
195
196 void emitSystemSemanticStore(llvm::IRBuilder<> &B, llvm::Value *Source,
197 const clang::DeclaratorDecl *Decl,
198 HLSLAppliedSemanticAttr *Semantic,
199 std::optional<unsigned> Index);
200
201 llvm::Value *handleScalarSemanticLoad(llvm::IRBuilder<> &B,
202 const FunctionDecl *FD,
203 llvm::Type *Type,
204 const clang::DeclaratorDecl *Decl,
205 HLSLAppliedSemanticAttr *Semantic);
206
207 void handleScalarSemanticStore(llvm::IRBuilder<> &B, const FunctionDecl *FD,
208 llvm::Value *Source,
209 const clang::DeclaratorDecl *Decl,
210 HLSLAppliedSemanticAttr *Semantic);
211
212 std::pair<llvm::Value *, specific_attr_iterator<HLSLAppliedSemanticAttr>>
213 handleStructSemanticLoad(
214 llvm::IRBuilder<> &B, const FunctionDecl *FD, llvm::Type *Type,
215 const clang::DeclaratorDecl *Decl,
216 specific_attr_iterator<HLSLAppliedSemanticAttr> begin,
217 specific_attr_iterator<HLSLAppliedSemanticAttr> end);
218
219 specific_attr_iterator<HLSLAppliedSemanticAttr> handleStructSemanticStore(
220 llvm::IRBuilder<> &B, const FunctionDecl *FD, llvm::Value *Source,
221 const clang::DeclaratorDecl *Decl,
222 specific_attr_iterator<HLSLAppliedSemanticAttr> AttrBegin,
223 specific_attr_iterator<HLSLAppliedSemanticAttr> AttrEnd);
224
225 std::pair<llvm::Value *, specific_attr_iterator<HLSLAppliedSemanticAttr>>
226 handleSemanticLoad(llvm::IRBuilder<> &B, const FunctionDecl *FD,
227 llvm::Type *Type, const clang::DeclaratorDecl *Decl,
228 specific_attr_iterator<HLSLAppliedSemanticAttr> begin,
229 specific_attr_iterator<HLSLAppliedSemanticAttr> end);
230
231 specific_attr_iterator<HLSLAppliedSemanticAttr>
232 handleSemanticStore(llvm::IRBuilder<> &B, const FunctionDecl *FD,
233 llvm::Value *Source, const clang::DeclaratorDecl *Decl,
234 specific_attr_iterator<HLSLAppliedSemanticAttr> AttrBegin,
235 specific_attr_iterator<HLSLAppliedSemanticAttr> AttrEnd);
236
237public:
238 CGHLSLRuntime(CodeGenModule &CGM) : CGM(CGM) {}
239 virtual ~CGHLSLRuntime() {}
240
241 llvm::Type *convertHLSLSpecificType(const Type *T,
242 const CGHLSLOffsetInfo &OffsetInfo);
243 llvm::Type *convertHLSLSpecificType(const Type *T) {
244 return convertHLSLSpecificType(T, OffsetInfo: CGHLSLOffsetInfo());
245 }
246
247 void generateGlobalCtorDtorCalls();
248
249 void addBuffer(const HLSLBufferDecl *D);
250 void addRootSignature(const HLSLRootSignatureDecl *D);
251 void finishCodeGen();
252
253 void setHLSLEntryAttributes(const FunctionDecl *FD, llvm::Function *Fn);
254
255 void emitEntryFunction(const FunctionDecl *FD, llvm::Function *Fn);
256 void setHLSLFunctionAttributes(const FunctionDecl *FD, llvm::Function *Fn);
257 void handleGlobalVarDefinition(const VarDecl *VD, llvm::GlobalVariable *Var);
258
259 llvm::Instruction *getConvergenceToken(llvm::BasicBlock &BB);
260
261 llvm::StructType *getHLSLBufferLayoutType(const RecordType *LayoutStructTy);
262 void addHLSLBufferLayoutType(const RecordType *LayoutStructTy,
263 llvm::StructType *LayoutTy);
264 void emitInitListOpaqueValues(CodeGenFunction &CGF, InitListExpr *E);
265
266 std::optional<LValue>
267 emitResourceArraySubscriptExpr(const ArraySubscriptExpr *E,
268 CodeGenFunction &CGF);
269 bool emitResourceArrayCopy(LValue &LHS, Expr *RHSExpr, CodeGenFunction &CGF);
270
271 std::optional<LValue> emitBufferArraySubscriptExpr(
272 const ArraySubscriptExpr *E, CodeGenFunction &CGF,
273 llvm::function_ref<llvm::Value *(bool Promote)> EmitIdxAfterBase);
274
275 bool emitBufferCopy(CodeGenFunction &CGF, Address DestPtr, Address SrcPtr,
276 QualType CType);
277
278 LValue emitBufferMemberExpr(CodeGenFunction &CGF, const MemberExpr *E);
279
280private:
281 void emitBufferGlobalsAndMetadata(const HLSLBufferDecl *BufDecl,
282 llvm::GlobalVariable *BufGV,
283 const CGHLSLOffsetInfo &OffsetInfo);
284 void initializeBufferFromBinding(const HLSLBufferDecl *BufDecl,
285 llvm::GlobalVariable *GV);
286 void initializeBufferFromBinding(const HLSLBufferDecl *BufDecl,
287 llvm::GlobalVariable *GV,
288 HLSLResourceBindingAttr *RBA);
289
290 llvm::Value *emitSPIRVUserSemanticLoad(llvm::IRBuilder<> &B, llvm::Type *Type,
291 const clang::DeclaratorDecl *Decl,
292 HLSLAppliedSemanticAttr *Semantic,
293 std::optional<unsigned> Index);
294 llvm::Value *emitDXILUserSemanticLoad(llvm::IRBuilder<> &B, llvm::Type *Type,
295 HLSLAppliedSemanticAttr *Semantic,
296 std::optional<unsigned> Index);
297 llvm::Value *emitUserSemanticLoad(llvm::IRBuilder<> &B, llvm::Type *Type,
298 const clang::DeclaratorDecl *Decl,
299 HLSLAppliedSemanticAttr *Semantic,
300 std::optional<unsigned> Index);
301
302 void emitSPIRVUserSemanticStore(llvm::IRBuilder<> &B, llvm::Value *Source,
303 const clang::DeclaratorDecl *Decl,
304 HLSLAppliedSemanticAttr *Semantic,
305 std::optional<unsigned> Index);
306 void emitDXILUserSemanticStore(llvm::IRBuilder<> &B, llvm::Value *Source,
307 HLSLAppliedSemanticAttr *Semantic,
308 std::optional<unsigned> Index);
309 void emitUserSemanticStore(llvm::IRBuilder<> &B, llvm::Value *Source,
310 const clang::DeclaratorDecl *Decl,
311 HLSLAppliedSemanticAttr *Semantic,
312 std::optional<unsigned> Index);
313
314 llvm::Triple::ArchType getArch();
315
316 llvm::DenseMap<const clang::RecordType *, llvm::StructType *> LayoutTypes;
317 unsigned SPIRVLastAssignedInputSemanticLocation = 0;
318 unsigned SPIRVLastAssignedOutputSemanticLocation = 0;
319};
320
321} // namespace CodeGen
322} // namespace clang
323
324#endif
325