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;
82class AggValueSlot;
83
84class CGHLSLOffsetInfo {
85 SmallVector<uint32_t> Offsets;
86
87public:
88 static const uint32_t Unspecified = ~0U;
89
90 /// Iterates over all declarations in the HLSL buffer and based on the
91 /// packoffset or register(c#) annotations it fills outs the Offsets vector
92 /// with the user-specified layout offsets. The buffer offsets can be
93 /// specified 2 ways: 1. declarations in cbuffer {} block can have a
94 /// packoffset annotation (translates to HLSLPackOffsetAttr) 2. default
95 /// constant buffer declarations at global scope can have register(c#)
96 /// annotations (translates to HLSLResourceBindingAttr with RegisterType::C)
97 /// It is not guaranteed that all declarations in a buffer have an annotation.
98 /// For those where it is not specified a `~0U` value is added to the Offsets
99 /// vector. In the final layout these declarations will be placed at the end
100 /// of the HLSL buffer after all of the elements with specified offset.
101 static CGHLSLOffsetInfo fromDecl(const HLSLBufferDecl &BufDecl);
102
103 /// Comparison function for offsets received from `operator[]` suitable for
104 /// use in a `stable_sort`. This will order implicit bindings after explicit
105 /// offsets.
106 static bool compareOffsets(uint32_t LHS, uint32_t RHS) { return LHS < RHS; }
107
108 /// Get the given offset, or `~0U` if there is no offset for the member.
109 uint32_t operator[](size_t I) const {
110 if (Offsets.empty())
111 return Unspecified;
112 return Offsets[I];
113 }
114
115 bool empty() const { return Offsets.empty(); }
116};
117
118class CGHLSLRuntime {
119public:
120 //===----------------------------------------------------------------------===//
121 // Start of reserved area for HLSL intrinsic getters.
122 //===----------------------------------------------------------------------===//
123
124 GENERATE_HLSL_INTRINSIC_FUNCTION(All, all)
125 GENERATE_HLSL_INTRINSIC_FUNCTION(Any, any)
126 GENERATE_HLSL_INTRINSIC_FUNCTION(Cross, cross)
127 GENERATE_HLSL_INTRINSIC_FUNCTION(Degrees, degrees)
128 GENERATE_HLSL_INTRINSIC_FUNCTION(Frac, frac)
129 GENERATE_HLSL_INTRINSIC_FUNCTION(FlattenedThreadIdInGroup,
130 flattened_thread_id_in_group)
131 GENERATE_HLSL_INTRINSIC_FUNCTION(IsInf, isinf)
132 GENERATE_HLSL_INTRINSIC_FUNCTION(IsNaN, isnan)
133 GENERATE_HLSL_INTRINSIC_FUNCTION(Lerp, lerp)
134 GENERATE_HLSL_INTRINSIC_FUNCTION(Normalize, normalize)
135 GENERATE_HLSL_INTRINSIC_FUNCTION(Rsqrt, rsqrt)
136 GENERATE_HLSL_INTRINSIC_FUNCTION(Saturate, saturate)
137 GENERATE_HLSL_INTRINSIC_FUNCTION(Sign, sign)
138 GENERATE_HLSL_INTRINSIC_FUNCTION(Step, step)
139 GENERATE_HLSL_INTRINSIC_FUNCTION(Radians, radians)
140 GENERATE_HLSL_INTRINSIC_FUNCTION(ThreadId, thread_id)
141 GENERATE_HLSL_INTRINSIC_FUNCTION(GroupThreadId, thread_id_in_group)
142 GENERATE_HLSL_INTRINSIC_FUNCTION(GroupId, group_id)
143 GENERATE_HLSL_INTRINSIC_FUNCTION(FDot, fdot)
144 GENERATE_HLSL_INTRINSIC_FUNCTION(SDot, sdot)
145 GENERATE_HLSL_INTRINSIC_FUNCTION(UDot, udot)
146 GENERATE_HLSL_INTRINSIC_FUNCTION(Dot4AddI8Packed, dot4add_i8packed)
147 GENERATE_HLSL_INTRINSIC_FUNCTION(Dot4AddU8Packed, dot4add_u8packed)
148 GENERATE_HLSL_INTRINSIC_FUNCTION(WaveActiveAllEqual, wave_all_equal)
149 GENERATE_HLSL_INTRINSIC_FUNCTION(WaveActiveAllTrue, wave_all)
150 GENERATE_HLSL_INTRINSIC_FUNCTION(WaveActiveAnyTrue, wave_any)
151 GENERATE_HLSL_INTRINSIC_FUNCTION(WaveActiveBitOr, wave_reduce_or)
152 GENERATE_HLSL_INTRINSIC_FUNCTION(WaveActiveBitXor, wave_reduce_xor)
153 GENERATE_HLSL_INTRINSIC_FUNCTION(WaveActiveBitAnd, wave_reduce_and)
154 GENERATE_HLSL_INTRINSIC_FUNCTION(InterlockedAdd, interlocked_add)
155 GENERATE_HLSL_INTRINSIC_FUNCTION(WaveActiveMax, wave_reduce_max)
156 GENERATE_HLSL_INTRINSIC_FUNCTION(WaveActiveUMax, wave_reduce_umax)
157 GENERATE_HLSL_INTRINSIC_FUNCTION(WaveActiveMin, wave_reduce_min)
158 GENERATE_HLSL_INTRINSIC_FUNCTION(WaveActiveUMin, wave_reduce_umin)
159 GENERATE_HLSL_INTRINSIC_FUNCTION(WaveActiveCountBits, wave_active_countbits)
160 GENERATE_HLSL_INTRINSIC_FUNCTION(WaveIsFirstLane, wave_is_first_lane)
161 GENERATE_HLSL_INTRINSIC_FUNCTION(WaveGetLaneCount, wave_get_lane_count)
162 GENERATE_HLSL_INTRINSIC_FUNCTION(WaveReadLaneAt, wave_readlane)
163 GENERATE_HLSL_INTRINSIC_FUNCTION(QuadReadAcrossX, quad_read_across_x)
164 GENERATE_HLSL_INTRINSIC_FUNCTION(QuadReadAcrossY, quad_read_across_y)
165 GENERATE_HLSL_INTRINSIC_FUNCTION(QuadReadAcrossDiagonal,
166 quad_read_across_diagonal)
167 GENERATE_HLSL_INTRINSIC_FUNCTION(FirstBitUHigh, firstbituhigh)
168 GENERATE_HLSL_INTRINSIC_FUNCTION(FirstBitSHigh, firstbitshigh)
169 GENERATE_HLSL_INTRINSIC_FUNCTION(FirstBitLow, firstbitlow)
170 GENERATE_HLSL_INTRINSIC_FUNCTION(NClamp, nclamp)
171 GENERATE_HLSL_INTRINSIC_FUNCTION(SClamp, sclamp)
172 GENERATE_HLSL_INTRINSIC_FUNCTION(UClamp, uclamp)
173
174 GENERATE_HLSL_INTRINSIC_FUNCTION(CreateResourceGetBasePointer,
175 resource_getbasepointer)
176 GENERATE_HLSL_INTRINSIC_FUNCTION(CreateResourceGetPointer,
177 resource_getpointer)
178 GENERATE_HLSL_INTRINSIC_FUNCTION(Sample, resource_sample)
179 GENERATE_HLSL_INTRINSIC_FUNCTION(SampleClamp, resource_sample_clamp)
180 GENERATE_HLSL_INTRINSIC_FUNCTION(SampleBias, resource_samplebias)
181 GENERATE_HLSL_INTRINSIC_FUNCTION(SampleBiasClamp, resource_samplebias_clamp)
182 GENERATE_HLSL_INTRINSIC_FUNCTION(SampleGrad, resource_samplegrad)
183 GENERATE_HLSL_INTRINSIC_FUNCTION(SampleGradClamp, resource_samplegrad_clamp)
184 GENERATE_HLSL_INTRINSIC_FUNCTION(SampleLevel, resource_samplelevel)
185 GENERATE_HLSL_INTRINSIC_FUNCTION(SampleCmp, resource_samplecmp)
186 GENERATE_HLSL_INTRINSIC_FUNCTION(SampleCmpClamp, resource_samplecmp_clamp)
187 GENERATE_HLSL_INTRINSIC_FUNCTION(SampleCmpLevelZero,
188 resource_samplecmplevelzero)
189 GENERATE_HLSL_INTRINSIC_FUNCTION(Gather, resource_gather)
190 GENERATE_HLSL_INTRINSIC_FUNCTION(GatherCmp, resource_gather_cmp)
191 GENERATE_HLSL_INTRINSIC_FUNCTION(CreateHandleFromBinding,
192 resource_handlefrombinding)
193 GENERATE_HLSL_INTRINSIC_FUNCTION(CreateHandleFromImplicitBinding,
194 resource_handlefromimplicitbinding)
195 GENERATE_HLSL_INTRINSIC_FUNCTION(NonUniformResourceIndex,
196 resource_nonuniformindex)
197 GENERATE_HLSL_INTRINSIC_FUNCTION(BufferUpdateCounter, resource_updatecounter)
198 GENERATE_HLSL_INTRINSIC_FUNCTION(AllMemoryBarrier, all_memory_barrier)
199 GENERATE_HLSL_INTRINSIC_FUNCTION(AllMemoryBarrierWithGroupSync,
200 all_memory_barrier_with_group_sync)
201 GENERATE_HLSL_INTRINSIC_FUNCTION(DeviceMemoryBarrier, device_memory_barrier)
202 GENERATE_HLSL_INTRINSIC_FUNCTION(DeviceMemoryBarrierWithGroupSync,
203 device_memory_barrier_with_group_sync)
204 GENERATE_HLSL_INTRINSIC_FUNCTION(GroupMemoryBarrier, group_memory_barrier)
205 GENERATE_HLSL_INTRINSIC_FUNCTION(GroupMemoryBarrierWithGroupSync,
206 group_memory_barrier_with_group_sync)
207 GENERATE_HLSL_INTRINSIC_FUNCTION(GetDimensionsX, resource_getdimensions_x)
208 GENERATE_HLSL_INTRINSIC_FUNCTION(GetDimensionsXY, resource_getdimensions_xy)
209 GENERATE_HLSL_INTRINSIC_FUNCTION(GetDimensionsLevelsXY,
210 resource_getdimensions_levels_xy)
211 GENERATE_HLSL_INTRINSIC_FUNCTION(LoadLevel, resource_load_level)
212 GENERATE_HLSL_INTRINSIC_FUNCTION(CalculateLod, resource_calculate_lod)
213 GENERATE_HLSL_INTRINSIC_FUNCTION(CalculateLodUnclamped,
214 resource_calculate_lod_unclamped)
215 GENERATE_HLSL_INTRINSIC_FUNCTION(DdxCoarse, ddx_coarse)
216 GENERATE_HLSL_INTRINSIC_FUNCTION(DdyCoarse, ddy_coarse)
217 GENERATE_HLSL_INTRINSIC_FUNCTION(DdxFine, ddx_fine)
218 GENERATE_HLSL_INTRINSIC_FUNCTION(DdyFine, ddy_fine)
219
220 //===----------------------------------------------------------------------===//
221 // End of reserved area for HLSL intrinsic getters.
222 //===----------------------------------------------------------------------===//
223
224protected:
225 CodeGenModule &CGM;
226
227 llvm::Value *emitSystemSemanticLoad(llvm::IRBuilder<> &B,
228 const FunctionDecl *FD, llvm::Type *Type,
229 const clang::DeclaratorDecl *Decl,
230 HLSLAppliedSemanticAttr *Semantic,
231 std::optional<unsigned> Index);
232
233 void emitSystemSemanticStore(llvm::IRBuilder<> &B, llvm::Value *Source,
234 const clang::DeclaratorDecl *Decl,
235 HLSLAppliedSemanticAttr *Semantic,
236 std::optional<unsigned> Index);
237
238 llvm::Value *handleScalarSemanticLoad(llvm::IRBuilder<> &B,
239 const FunctionDecl *FD,
240 llvm::Type *Type,
241 const clang::DeclaratorDecl *Decl,
242 HLSLAppliedSemanticAttr *Semantic);
243
244 void handleScalarSemanticStore(llvm::IRBuilder<> &B, const FunctionDecl *FD,
245 llvm::Value *Source,
246 const clang::DeclaratorDecl *Decl,
247 HLSLAppliedSemanticAttr *Semantic);
248
249 std::pair<llvm::Value *, specific_attr_iterator<HLSLAppliedSemanticAttr>>
250 handleStructSemanticLoad(
251 llvm::IRBuilder<> &B, const FunctionDecl *FD, llvm::Type *Type,
252 const clang::DeclaratorDecl *Decl,
253 specific_attr_iterator<HLSLAppliedSemanticAttr> begin,
254 specific_attr_iterator<HLSLAppliedSemanticAttr> end);
255
256 specific_attr_iterator<HLSLAppliedSemanticAttr> handleStructSemanticStore(
257 llvm::IRBuilder<> &B, const FunctionDecl *FD, llvm::Value *Source,
258 const clang::DeclaratorDecl *Decl,
259 specific_attr_iterator<HLSLAppliedSemanticAttr> AttrBegin,
260 specific_attr_iterator<HLSLAppliedSemanticAttr> AttrEnd);
261
262 std::pair<llvm::Value *, specific_attr_iterator<HLSLAppliedSemanticAttr>>
263 handleSemanticLoad(llvm::IRBuilder<> &B, const FunctionDecl *FD,
264 llvm::Type *Type, const clang::DeclaratorDecl *Decl,
265 specific_attr_iterator<HLSLAppliedSemanticAttr> begin,
266 specific_attr_iterator<HLSLAppliedSemanticAttr> end);
267
268 specific_attr_iterator<HLSLAppliedSemanticAttr>
269 handleSemanticStore(llvm::IRBuilder<> &B, const FunctionDecl *FD,
270 llvm::Value *Source, const clang::DeclaratorDecl *Decl,
271 specific_attr_iterator<HLSLAppliedSemanticAttr> AttrBegin,
272 specific_attr_iterator<HLSLAppliedSemanticAttr> AttrEnd);
273
274public:
275 CGHLSLRuntime(CodeGenModule &CGM) : CGM(CGM) {}
276 virtual ~CGHLSLRuntime() {}
277
278 llvm::Type *convertHLSLSpecificType(const Type *T,
279 const CGHLSLOffsetInfo &OffsetInfo);
280 llvm::Type *convertHLSLSpecificType(const Type *T) {
281 return convertHLSLSpecificType(T, OffsetInfo: CGHLSLOffsetInfo());
282 }
283
284 void generateGlobalCtorDtorCalls();
285
286 void addBuffer(const HLSLBufferDecl *D);
287 void addRootSignature(const HLSLRootSignatureDecl *D);
288 void finishCodeGen();
289
290 void setHLSLEntryAttributes(const FunctionDecl *FD, llvm::Function *Fn);
291
292 void emitEntryFunction(const FunctionDecl *FD, llvm::Function *Fn);
293 void setHLSLFunctionAttributes(const FunctionDecl *FD, llvm::Function *Fn);
294 void handleGlobalVarDefinition(const VarDecl *VD, llvm::GlobalVariable *Var);
295
296 llvm::Instruction *getConvergenceToken(llvm::BasicBlock &BB);
297
298 llvm::StructType *getHLSLBufferLayoutType(const RecordType *LayoutStructTy);
299 void addHLSLBufferLayoutType(const RecordType *LayoutStructTy,
300 llvm::StructType *LayoutTy);
301 void emitInitListOpaqueValues(CodeGenFunction &CGF, InitListExpr *E);
302
303 std::optional<LValue>
304 emitResourceArraySubscriptExpr(const ArraySubscriptExpr *E,
305 CodeGenFunction &CGF);
306
307 bool emitGlobalResourceArray(CodeGenFunction &CGF, const Expr *E,
308 AggValueSlot &DestSlot);
309 std::optional<LValue>
310 emitGlobalResourceArrayAsLValue(CodeGenFunction &CGF,
311 const VarDecl *ArrayDecl);
312
313 std::optional<LValue> emitBufferArraySubscriptExpr(
314 const ArraySubscriptExpr *E, CodeGenFunction &CGF,
315 llvm::function_ref<llvm::Value *(bool Promote)> EmitIdxAfterBase);
316
317 RawAddress createBufferMatrixTempAddress(const LValue &LV,
318 CodeGenFunction &CGF);
319
320 bool emitBufferCopy(CodeGenFunction &CGF, Address DestPtr, Address SrcPtr,
321 QualType CType);
322
323 LValue emitBufferMemberExpr(CodeGenFunction &CGF, const MemberExpr *E);
324 std::optional<LValue> emitResourceMemberExpr(CodeGenFunction &CGF,
325 const MemberExpr *E);
326
327private:
328 void emitBufferGlobalsAndMetadata(const HLSLBufferDecl *BufDecl,
329 llvm::GlobalVariable *BufGV,
330 const CGHLSLOffsetInfo &OffsetInfo);
331 void initializeBufferFromBinding(const HLSLBufferDecl *BufDecl,
332 llvm::GlobalVariable *GV);
333 void initializeBufferFromBinding(const HLSLBufferDecl *BufDecl,
334 llvm::GlobalVariable *GV,
335 HLSLResourceBindingAttr *RBA);
336
337 llvm::Value *emitSPIRVUserSemanticLoad(llvm::IRBuilder<> &B, llvm::Type *Type,
338 const clang::DeclaratorDecl *Decl,
339 HLSLAppliedSemanticAttr *Semantic,
340 std::optional<unsigned> Index);
341 llvm::Value *emitDXILUserSemanticLoad(llvm::IRBuilder<> &B, llvm::Type *Type,
342 HLSLAppliedSemanticAttr *Semantic,
343 std::optional<unsigned> Index);
344 llvm::Value *emitUserSemanticLoad(llvm::IRBuilder<> &B, llvm::Type *Type,
345 const clang::DeclaratorDecl *Decl,
346 HLSLAppliedSemanticAttr *Semantic,
347 std::optional<unsigned> Index);
348
349 void emitSPIRVUserSemanticStore(llvm::IRBuilder<> &B, llvm::Value *Source,
350 const clang::DeclaratorDecl *Decl,
351 HLSLAppliedSemanticAttr *Semantic,
352 std::optional<unsigned> Index);
353 void emitDXILUserSemanticStore(llvm::IRBuilder<> &B, llvm::Value *Source,
354 HLSLAppliedSemanticAttr *Semantic,
355 std::optional<unsigned> Index);
356 void emitUserSemanticStore(llvm::IRBuilder<> &B, llvm::Value *Source,
357 const clang::DeclaratorDecl *Decl,
358 HLSLAppliedSemanticAttr *Semantic,
359 std::optional<unsigned> Index);
360
361 bool initializeGlobalResourceArray(CodeGenFunction &CGF,
362 const VarDecl *ArrayDecl,
363 AggValueSlot &DestSlot);
364
365 llvm::Triple::ArchType getArch();
366
367 llvm::DenseMap<const clang::RecordType *, llvm::StructType *> LayoutTypes;
368 unsigned SPIRVLastAssignedInputSemanticLocation = 0;
369 unsigned SPIRVLastAssignedOutputSemanticLocation = 0;
370};
371
372} // namespace CodeGen
373} // namespace clang
374
375#endif
376