1//===----- CGHLSLRuntime.cpp - Interface to HLSL Runtimes -----------------===//
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#include "CGHLSLRuntime.h"
16#include "CGDebugInfo.h"
17#include "CodeGenFunction.h"
18#include "CodeGenModule.h"
19#include "TargetInfo.h"
20#include "clang/AST/ASTContext.h"
21#include "clang/AST/Decl.h"
22#include "clang/AST/RecursiveASTVisitor.h"
23#include "clang/AST/Type.h"
24#include "clang/Basic/TargetOptions.h"
25#include "llvm/ADT/SmallVector.h"
26#include "llvm/Frontend/HLSL/HLSLRootSignatureUtils.h"
27#include "llvm/IR/Constants.h"
28#include "llvm/IR/DerivedTypes.h"
29#include "llvm/IR/GlobalVariable.h"
30#include "llvm/IR/LLVMContext.h"
31#include "llvm/IR/Metadata.h"
32#include "llvm/IR/Module.h"
33#include "llvm/IR/Type.h"
34#include "llvm/IR/Value.h"
35#include "llvm/Support/Alignment.h"
36#include "llvm/Support/ErrorHandling.h"
37#include "llvm/Support/FormatVariadic.h"
38#include <utility>
39
40using namespace clang;
41using namespace CodeGen;
42using namespace clang::hlsl;
43using namespace llvm;
44
45using llvm::hlsl::CBufferRowSizeInBytes;
46
47namespace {
48
49void addDxilValVersion(StringRef ValVersionStr, llvm::Module &M) {
50 // The validation of ValVersionStr is done at HLSLToolChain::TranslateArgs.
51 // Assume ValVersionStr is legal here.
52 VersionTuple Version;
53 if (Version.tryParse(string: ValVersionStr) || Version.getBuild() ||
54 Version.getSubminor() || !Version.getMinor()) {
55 return;
56 }
57
58 uint64_t Major = Version.getMajor();
59 uint64_t Minor = *Version.getMinor();
60
61 auto &Ctx = M.getContext();
62 IRBuilder<> B(M.getContext());
63 MDNode *Val = MDNode::get(Context&: Ctx, MDs: {ConstantAsMetadata::get(C: B.getInt32(C: Major)),
64 ConstantAsMetadata::get(C: B.getInt32(C: Minor))});
65 StringRef DXILValKey = "dx.valver";
66 auto *DXILValMD = M.getOrInsertNamedMetadata(Name: DXILValKey);
67 DXILValMD->addOperand(M: Val);
68}
69
70void addRootSignature(ArrayRef<llvm::hlsl::rootsig::RootElement> Elements,
71 llvm::Function *Fn, llvm::Module &M) {
72 auto &Ctx = M.getContext();
73
74 llvm::hlsl::rootsig::MetadataBuilder Builder(Ctx, Elements);
75 MDNode *RootSignature = Builder.BuildRootSignature();
76 MDNode *FnPairing =
77 MDNode::get(Context&: Ctx, MDs: {ValueAsMetadata::get(V: Fn), RootSignature});
78
79 StringRef RootSignatureValKey = "dx.rootsignatures";
80 auto *RootSignatureValMD = M.getOrInsertNamedMetadata(Name: RootSignatureValKey);
81 RootSignatureValMD->addOperand(M: FnPairing);
82}
83
84} // namespace
85
86llvm::Type *
87CGHLSLRuntime::convertHLSLSpecificType(const Type *T,
88 SmallVector<int32_t> *Packoffsets) {
89 assert(T->isHLSLSpecificType() && "Not an HLSL specific type!");
90
91 // Check if the target has a specific translation for this type first.
92 if (llvm::Type *TargetTy =
93 CGM.getTargetCodeGenInfo().getHLSLType(CGM, T, Packoffsets))
94 return TargetTy;
95
96 llvm_unreachable("Generic handling of HLSL types is not supported.");
97}
98
99llvm::Triple::ArchType CGHLSLRuntime::getArch() {
100 return CGM.getTarget().getTriple().getArch();
101}
102
103// Returns true if the type is an HLSL resource class or an array of them
104static bool isResourceRecordTypeOrArrayOf(const clang::Type *Ty) {
105 while (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Val: Ty))
106 Ty = CAT->getArrayElementTypeNoTypeQual();
107 return Ty->isHLSLResourceRecord();
108}
109
110// Emits constant global variables for buffer constants declarations
111// and creates metadata linking the constant globals with the buffer global.
112void CGHLSLRuntime::emitBufferGlobalsAndMetadata(const HLSLBufferDecl *BufDecl,
113 llvm::GlobalVariable *BufGV) {
114 LLVMContext &Ctx = CGM.getLLVMContext();
115
116 // get the layout struct from constant buffer target type
117 llvm::Type *BufType = BufGV->getValueType();
118 llvm::Type *BufLayoutType =
119 cast<llvm::TargetExtType>(Val: BufType)->getTypeParameter(i: 0);
120 llvm::StructType *LayoutStruct = cast<llvm::StructType>(
121 Val: cast<llvm::TargetExtType>(Val: BufLayoutType)->getTypeParameter(i: 0));
122
123 // Start metadata list associating the buffer global variable with its
124 // constatns
125 SmallVector<llvm::Metadata *> BufGlobals;
126 BufGlobals.push_back(Elt: ValueAsMetadata::get(V: BufGV));
127
128 const auto *ElemIt = LayoutStruct->element_begin();
129 for (Decl *D : BufDecl->buffer_decls()) {
130 if (isa<CXXRecordDecl, EmptyDecl>(Val: D))
131 // Nothing to do for this declaration.
132 continue;
133 if (isa<FunctionDecl>(Val: D)) {
134 // A function within an cbuffer is effectively a top-level function.
135 CGM.EmitTopLevelDecl(D);
136 continue;
137 }
138 VarDecl *VD = dyn_cast<VarDecl>(Val: D);
139 if (!VD)
140 continue;
141
142 QualType VDTy = VD->getType();
143 if (VDTy.getAddressSpace() != LangAS::hlsl_constant) {
144 if (VD->getStorageClass() == SC_Static ||
145 VDTy.getAddressSpace() == LangAS::hlsl_groupshared ||
146 isResourceRecordTypeOrArrayOf(Ty: VDTy.getTypePtr())) {
147 // Emit static and groupshared variables and resource classes inside
148 // cbuffer as regular globals
149 CGM.EmitGlobal(D: VD);
150 } else {
151 // Anything else that is not in the hlsl_constant address space must be
152 // an empty struct or a zero-sized array and can be ignored
153 assert(BufDecl->getASTContext().getTypeSize(VDTy) == 0 &&
154 "constant buffer decl with non-zero sized type outside of "
155 "hlsl_constant address space");
156 }
157 continue;
158 }
159
160 assert(ElemIt != LayoutStruct->element_end() &&
161 "number of elements in layout struct does not match");
162 llvm::Type *LayoutType = *ElemIt++;
163
164 // FIXME: handle resources inside user defined structs
165 // (llvm/wg-hlsl#175)
166
167 // create global variable for the constant and to metadata list
168 GlobalVariable *ElemGV =
169 cast<GlobalVariable>(Val: CGM.GetAddrOfGlobalVar(D: VD, Ty: LayoutType));
170 BufGlobals.push_back(Elt: ValueAsMetadata::get(V: ElemGV));
171 }
172 assert(ElemIt == LayoutStruct->element_end() &&
173 "number of elements in layout struct does not match");
174
175 // add buffer metadata to the module
176 CGM.getModule()
177 .getOrInsertNamedMetadata(Name: "hlsl.cbs")
178 ->addOperand(M: MDNode::get(Context&: Ctx, MDs: BufGlobals));
179}
180
181// Creates resource handle type for the HLSL buffer declaration
182static const clang::HLSLAttributedResourceType *
183createBufferHandleType(const HLSLBufferDecl *BufDecl) {
184 ASTContext &AST = BufDecl->getASTContext();
185 QualType QT = AST.getHLSLAttributedResourceType(
186 Wrapped: AST.HLSLResourceTy,
187 Contained: QualType(BufDecl->getLayoutStruct()->getTypeForDecl(), 0),
188 Attrs: HLSLAttributedResourceType::Attributes(ResourceClass::CBuffer));
189 return cast<HLSLAttributedResourceType>(Val: QT.getTypePtr());
190}
191
192// Iterates over all declarations in the HLSL buffer and based on the
193// packoffset or register(c#) annotations it fills outs the Layout
194// vector with the user-specified layout offsets.
195// The buffer offsets can be specified 2 ways:
196// 1. declarations in cbuffer {} block can have a packoffset annotation
197// (translates to HLSLPackOffsetAttr)
198// 2. default constant buffer declarations at global scope can have
199// register(c#) annotations (translates to HLSLResourceBindingAttr with
200// RegisterType::C)
201// It is not guaranteed that all declarations in a buffer have an annotation.
202// For those where it is not specified a -1 value is added to the Layout
203// vector. In the final layout these declarations will be placed at the end
204// of the HLSL buffer after all of the elements with specified offset.
205static void fillPackoffsetLayout(const HLSLBufferDecl *BufDecl,
206 SmallVector<int32_t> &Layout) {
207 assert(Layout.empty() && "expected empty vector for layout");
208 assert(BufDecl->hasValidPackoffset());
209
210 for (Decl *D : BufDecl->buffer_decls()) {
211 if (isa<CXXRecordDecl, EmptyDecl>(Val: D) || isa<FunctionDecl>(Val: D)) {
212 continue;
213 }
214 VarDecl *VD = dyn_cast<VarDecl>(Val: D);
215 if (!VD || VD->getType().getAddressSpace() != LangAS::hlsl_constant)
216 continue;
217
218 if (!VD->hasAttrs()) {
219 Layout.push_back(Elt: -1);
220 continue;
221 }
222
223 int32_t Offset = -1;
224 for (auto *Attr : VD->getAttrs()) {
225 if (auto *POA = dyn_cast<HLSLPackOffsetAttr>(Attr)) {
226 Offset = POA->getOffsetInBytes();
227 break;
228 }
229 auto *RBA = dyn_cast<HLSLResourceBindingAttr>(Attr);
230 if (RBA &&
231 RBA->getRegisterType() == HLSLResourceBindingAttr::RegisterType::C) {
232 Offset = RBA->getSlotNumber() * CBufferRowSizeInBytes;
233 break;
234 }
235 }
236 Layout.push_back(Elt: Offset);
237 }
238}
239
240std::pair<llvm::Intrinsic::ID, bool>
241CGHLSLRuntime::getCreateHandleFromBindingIntrinsic() {
242 switch (getArch()) {
243 case llvm::Triple::dxil:
244 return std::pair(llvm::Intrinsic::dx_resource_handlefrombinding, true);
245 case llvm::Triple::spirv:
246 return std::pair(llvm::Intrinsic::spv_resource_handlefrombinding, false);
247 default:
248 llvm_unreachable("Intrinsic resource_handlefrombinding not supported by "
249 "target architecture");
250 }
251}
252
253std::pair<llvm::Intrinsic::ID, bool>
254CGHLSLRuntime::getCreateHandleFromImplicitBindingIntrinsic() {
255 switch (getArch()) {
256 case llvm::Triple::dxil:
257 return std::pair(llvm::Intrinsic::dx_resource_handlefromimplicitbinding,
258 true);
259 case llvm::Triple::spirv:
260 return std::pair(llvm::Intrinsic::spv_resource_handlefromimplicitbinding,
261 false);
262 default:
263 llvm_unreachable(
264 "Intrinsic resource_handlefromimplicitbinding not supported by "
265 "target architecture");
266 }
267}
268
269// Codegen for HLSLBufferDecl
270void CGHLSLRuntime::addBuffer(const HLSLBufferDecl *BufDecl) {
271
272 assert(BufDecl->isCBuffer() && "tbuffer codegen is not supported yet");
273
274 // create resource handle type for the buffer
275 const clang::HLSLAttributedResourceType *ResHandleTy =
276 createBufferHandleType(BufDecl);
277
278 // empty constant buffer is ignored
279 if (ResHandleTy->getContainedType()->getAsCXXRecordDecl()->isEmpty())
280 return;
281
282 // create global variable for the constant buffer
283 SmallVector<int32_t> Layout;
284 if (BufDecl->hasValidPackoffset())
285 fillPackoffsetLayout(BufDecl, Layout);
286
287 llvm::TargetExtType *TargetTy =
288 cast<llvm::TargetExtType>(Val: convertHLSLSpecificType(
289 ResHandleTy, BufDecl->hasValidPackoffset() ? &Layout : nullptr));
290 llvm::GlobalVariable *BufGV = new GlobalVariable(
291 TargetTy, /*isConstant*/ false,
292 GlobalValue::LinkageTypes::ExternalLinkage, PoisonValue::get(T: TargetTy),
293 llvm::formatv("{0}{1}", BufDecl->getName(),
294 BufDecl->isCBuffer() ? ".cb" : ".tb"),
295 GlobalValue::NotThreadLocal);
296 CGM.getModule().insertGlobalVariable(GV: BufGV);
297
298 // Add globals for constant buffer elements and create metadata nodes
299 emitBufferGlobalsAndMetadata(BufDecl, BufGV);
300
301 // Initialize cbuffer from binding (implicit or explicit)
302 HLSLResourceBindingAttr *RBA = BufDecl->getAttr<HLSLResourceBindingAttr>();
303 assert(RBA &&
304 "cbuffer/tbuffer should always have resource binding attribute");
305 initializeBufferFromBinding(BufDecl, GV: BufGV, RBA);
306}
307
308llvm::TargetExtType *
309CGHLSLRuntime::getHLSLBufferLayoutType(const RecordType *StructType) {
310 const auto Entry = LayoutTypes.find(Val: StructType);
311 if (Entry != LayoutTypes.end())
312 return Entry->getSecond();
313 return nullptr;
314}
315
316void CGHLSLRuntime::addHLSLBufferLayoutType(const RecordType *StructType,
317 llvm::TargetExtType *LayoutTy) {
318 assert(getHLSLBufferLayoutType(StructType) == nullptr &&
319 "layout type for this struct already exist");
320 LayoutTypes[StructType] = LayoutTy;
321}
322
323void CGHLSLRuntime::finishCodeGen() {
324 auto &TargetOpts = CGM.getTarget().getTargetOpts();
325 auto &CodeGenOpts = CGM.getCodeGenOpts();
326 auto &LangOpts = CGM.getLangOpts();
327 llvm::Module &M = CGM.getModule();
328 Triple T(M.getTargetTriple());
329 if (T.getArch() == Triple::ArchType::dxil)
330 addDxilValVersion(ValVersionStr: TargetOpts.DxilValidatorVersion, M);
331 if (CodeGenOpts.ResMayAlias)
332 M.setModuleFlag(Behavior: llvm::Module::ModFlagBehavior::Error, Key: "dx.resmayalias", Val: 1);
333
334 // NativeHalfType corresponds to the -fnative-half-type clang option which is
335 // aliased by clang-dxc's -enable-16bit-types option. This option is used to
336 // set the UseNativeLowPrecision DXIL module flag in the DirectX backend
337 if (LangOpts.NativeHalfType)
338 M.setModuleFlag(Behavior: llvm::Module::ModFlagBehavior::Error, Key: "dx.nativelowprec",
339 Val: 1);
340
341 generateGlobalCtorDtorCalls();
342}
343
344void clang::CodeGen::CGHLSLRuntime::setHLSLEntryAttributes(
345 const FunctionDecl *FD, llvm::Function *Fn) {
346 const auto *ShaderAttr = FD->getAttr<HLSLShaderAttr>();
347 assert(ShaderAttr && "All entry functions must have a HLSLShaderAttr");
348 const StringRef ShaderAttrKindStr = "hlsl.shader";
349 Fn->addFnAttr(ShaderAttrKindStr,
350 llvm::Triple::getEnvironmentTypeName(Kind: ShaderAttr->getType()));
351 if (HLSLNumThreadsAttr *NumThreadsAttr = FD->getAttr<HLSLNumThreadsAttr>()) {
352 const StringRef NumThreadsKindStr = "hlsl.numthreads";
353 std::string NumThreadsStr =
354 formatv("{0},{1},{2}", NumThreadsAttr->getX(), NumThreadsAttr->getY(),
355 NumThreadsAttr->getZ());
356 Fn->addFnAttr(Kind: NumThreadsKindStr, Val: NumThreadsStr);
357 }
358 if (HLSLWaveSizeAttr *WaveSizeAttr = FD->getAttr<HLSLWaveSizeAttr>()) {
359 const StringRef WaveSizeKindStr = "hlsl.wavesize";
360 std::string WaveSizeStr =
361 formatv("{0},{1},{2}", WaveSizeAttr->getMin(), WaveSizeAttr->getMax(),
362 WaveSizeAttr->getPreferred());
363 Fn->addFnAttr(Kind: WaveSizeKindStr, Val: WaveSizeStr);
364 }
365 // HLSL entry functions are materialized for module functions with
366 // HLSLShaderAttr attribute. SetLLVMFunctionAttributesForDefinition called
367 // later in the compiler-flow for such module functions is not aware of and
368 // hence not able to set attributes of the newly materialized entry functions.
369 // So, set attributes of entry function here, as appropriate.
370 if (CGM.getCodeGenOpts().OptimizationLevel == 0)
371 Fn->addFnAttr(llvm::Attribute::OptimizeNone);
372 Fn->addFnAttr(llvm::Attribute::NoInline);
373}
374
375static Value *buildVectorInput(IRBuilder<> &B, Function *F, llvm::Type *Ty) {
376 if (const auto *VT = dyn_cast<FixedVectorType>(Val: Ty)) {
377 Value *Result = PoisonValue::get(T: Ty);
378 for (unsigned I = 0; I < VT->getNumElements(); ++I) {
379 Value *Elt = B.CreateCall(Callee: F, Args: {B.getInt32(C: I)});
380 Result = B.CreateInsertElement(Vec: Result, NewElt: Elt, Idx: I);
381 }
382 return Result;
383 }
384 return B.CreateCall(Callee: F, Args: {B.getInt32(C: 0)});
385}
386
387static void addSPIRVBuiltinDecoration(llvm::GlobalVariable *GV,
388 unsigned BuiltIn) {
389 LLVMContext &Ctx = GV->getContext();
390 IRBuilder<> B(GV->getContext());
391 MDNode *Operands = MDNode::get(
392 Context&: Ctx,
393 MDs: {ConstantAsMetadata::get(C: B.getInt32(/* Spirv::Decoration::BuiltIn */ C: 11)),
394 ConstantAsMetadata::get(C: B.getInt32(C: BuiltIn))});
395 MDNode *Decoration = MDNode::get(Context&: Ctx, MDs: {Operands});
396 GV->addMetadata(Kind: "spirv.Decorations", MD&: *Decoration);
397}
398
399static llvm::Value *createSPIRVBuiltinLoad(IRBuilder<> &B, llvm::Module &M,
400 llvm::Type *Ty, const Twine &Name,
401 unsigned BuiltInID) {
402 auto *GV = new llvm::GlobalVariable(
403 M, Ty, /* isConstant= */ true, llvm::GlobalValue::ExternalLinkage,
404 /* Initializer= */ nullptr, Name, /* insertBefore= */ nullptr,
405 llvm::GlobalVariable::GeneralDynamicTLSModel,
406 /* AddressSpace */ 7, /* isExternallyInitialized= */ true);
407 addSPIRVBuiltinDecoration(GV, BuiltIn: BuiltInID);
408 return B.CreateLoad(Ty, Ptr: GV);
409}
410
411llvm::Value *CGHLSLRuntime::emitInputSemantic(IRBuilder<> &B,
412 const ParmVarDecl &D,
413 llvm::Type *Ty) {
414 assert(D.hasAttrs() && "Entry parameter missing annotation attribute!");
415 if (D.hasAttr<HLSLSV_GroupIndexAttr>()) {
416 llvm::Function *GroupIndex =
417 CGM.getIntrinsic(IID: getFlattenedThreadIdInGroupIntrinsic());
418 return B.CreateCall(Callee: FunctionCallee(GroupIndex));
419 }
420 if (D.hasAttr<HLSLSV_DispatchThreadIDAttr>()) {
421 llvm::Function *ThreadIDIntrinsic =
422 CGM.getIntrinsic(IID: getThreadIdIntrinsic());
423 return buildVectorInput(B, F: ThreadIDIntrinsic, Ty);
424 }
425 if (D.hasAttr<HLSLSV_GroupThreadIDAttr>()) {
426 llvm::Function *GroupThreadIDIntrinsic =
427 CGM.getIntrinsic(IID: getGroupThreadIdIntrinsic());
428 return buildVectorInput(B, F: GroupThreadIDIntrinsic, Ty);
429 }
430 if (D.hasAttr<HLSLSV_GroupIDAttr>()) {
431 llvm::Function *GroupIDIntrinsic = CGM.getIntrinsic(IID: getGroupIdIntrinsic());
432 return buildVectorInput(B, F: GroupIDIntrinsic, Ty);
433 }
434 if (D.hasAttr<HLSLSV_PositionAttr>()) {
435 if (getArch() == llvm::Triple::spirv)
436 return createSPIRVBuiltinLoad(B, M&: CGM.getModule(), Ty, Name: "sv_position",
437 /* BuiltIn::Position */ BuiltInID: 0);
438 llvm_unreachable("SV_Position semantic not implemented for this target.");
439 }
440 assert(false && "Unhandled parameter attribute");
441 return nullptr;
442}
443
444void CGHLSLRuntime::emitEntryFunction(const FunctionDecl *FD,
445 llvm::Function *Fn) {
446 llvm::Module &M = CGM.getModule();
447 llvm::LLVMContext &Ctx = M.getContext();
448 auto *EntryTy = llvm::FunctionType::get(Result: llvm::Type::getVoidTy(C&: Ctx), isVarArg: false);
449 Function *EntryFn =
450 Function::Create(EntryTy, Function::ExternalLinkage, FD->getName(), &M);
451
452 // Copy function attributes over, we have no argument or return attributes
453 // that can be valid on the real entry.
454 AttributeList NewAttrs = AttributeList::get(C&: Ctx, Index: AttributeList::FunctionIndex,
455 Attrs: Fn->getAttributes().getFnAttrs());
456 EntryFn->setAttributes(NewAttrs);
457 setHLSLEntryAttributes(FD, Fn: EntryFn);
458
459 // Set the called function as internal linkage.
460 Fn->setLinkage(GlobalValue::InternalLinkage);
461
462 BasicBlock *BB = BasicBlock::Create(Context&: Ctx, Name: "entry", Parent: EntryFn);
463 IRBuilder<> B(BB);
464 llvm::SmallVector<Value *> Args;
465
466 SmallVector<OperandBundleDef, 1> OB;
467 if (CGM.shouldEmitConvergenceTokens()) {
468 assert(EntryFn->isConvergent());
469 llvm::Value *I =
470 B.CreateIntrinsic(llvm::Intrinsic::experimental_convergence_entry, {});
471 llvm::Value *bundleArgs[] = {I};
472 OB.emplace_back(Args: "convergencectrl", Args&: bundleArgs);
473 }
474
475 // FIXME: support struct parameters where semantics are on members.
476 // See: https://github.com/llvm/llvm-project/issues/57874
477 unsigned SRetOffset = 0;
478 for (const auto &Param : Fn->args()) {
479 if (Param.hasStructRetAttr()) {
480 // FIXME: support output.
481 // See: https://github.com/llvm/llvm-project/issues/57874
482 SRetOffset = 1;
483 Args.emplace_back(Args: PoisonValue::get(T: Param.getType()));
484 continue;
485 }
486 const ParmVarDecl *PD = FD->getParamDecl(i: Param.getArgNo() - SRetOffset);
487 Args.push_back(Elt: emitInputSemantic(B, D: *PD, Ty: Param.getType()));
488 }
489
490 CallInst *CI = B.CreateCall(Callee: FunctionCallee(Fn), Args, OpBundles: OB);
491 CI->setCallingConv(Fn->getCallingConv());
492 // FIXME: Handle codegen for return type semantics.
493 // See: https://github.com/llvm/llvm-project/issues/57875
494 B.CreateRetVoid();
495
496 // Add and identify root signature to function, if applicable
497 for (const Attr *Attr : FD->getAttrs()) {
498 if (const auto *RSAttr = dyn_cast<RootSignatureAttr>(Attr))
499 addRootSignature(RSAttr->getSignatureDecl()->getRootElements(), EntryFn,
500 M);
501 }
502}
503
504void CGHLSLRuntime::setHLSLFunctionAttributes(const FunctionDecl *FD,
505 llvm::Function *Fn) {
506 if (FD->isInExportDeclContext()) {
507 const StringRef ExportAttrKindStr = "hlsl.export";
508 Fn->addFnAttr(Kind: ExportAttrKindStr);
509 }
510}
511
512static void gatherFunctions(SmallVectorImpl<Function *> &Fns, llvm::Module &M,
513 bool CtorOrDtor) {
514 const auto *GV =
515 M.getNamedGlobal(Name: CtorOrDtor ? "llvm.global_ctors" : "llvm.global_dtors");
516 if (!GV)
517 return;
518 const auto *CA = dyn_cast<ConstantArray>(Val: GV->getInitializer());
519 if (!CA)
520 return;
521 // The global_ctor array elements are a struct [Priority, Fn *, COMDat].
522 // HLSL neither supports priorities or COMDat values, so we will check those
523 // in an assert but not handle them.
524
525 for (const auto &Ctor : CA->operands()) {
526 if (isa<ConstantAggregateZero>(Val: Ctor))
527 continue;
528 ConstantStruct *CS = cast<ConstantStruct>(Val: Ctor);
529
530 assert(cast<ConstantInt>(CS->getOperand(0))->getValue() == 65535 &&
531 "HLSL doesn't support setting priority for global ctors.");
532 assert(isa<ConstantPointerNull>(CS->getOperand(2)) &&
533 "HLSL doesn't support COMDat for global ctors.");
534 Fns.push_back(Elt: cast<Function>(Val: CS->getOperand(i_nocapture: 1)));
535 }
536}
537
538void CGHLSLRuntime::generateGlobalCtorDtorCalls() {
539 llvm::Module &M = CGM.getModule();
540 SmallVector<Function *> CtorFns;
541 SmallVector<Function *> DtorFns;
542 gatherFunctions(Fns&: CtorFns, M, CtorOrDtor: true);
543 gatherFunctions(Fns&: DtorFns, M, CtorOrDtor: false);
544
545 // Insert a call to the global constructor at the beginning of the entry block
546 // to externally exported functions. This is a bit of a hack, but HLSL allows
547 // global constructors, but doesn't support driver initialization of globals.
548 for (auto &F : M.functions()) {
549 if (!F.hasFnAttribute(Kind: "hlsl.shader"))
550 continue;
551 auto *Token = getConvergenceToken(BB&: F.getEntryBlock());
552 Instruction *IP = &*F.getEntryBlock().begin();
553 SmallVector<OperandBundleDef, 1> OB;
554 if (Token) {
555 llvm::Value *bundleArgs[] = {Token};
556 OB.emplace_back(Args: "convergencectrl", Args&: bundleArgs);
557 IP = Token->getNextNode();
558 }
559 IRBuilder<> B(IP);
560 for (auto *Fn : CtorFns) {
561 auto CI = B.CreateCall(Callee: FunctionCallee(Fn), Args: {}, OpBundles: OB);
562 CI->setCallingConv(Fn->getCallingConv());
563 }
564
565 // Insert global dtors before the terminator of the last instruction
566 B.SetInsertPoint(F.back().getTerminator());
567 for (auto *Fn : DtorFns) {
568 auto CI = B.CreateCall(Callee: FunctionCallee(Fn), Args: {}, OpBundles: OB);
569 CI->setCallingConv(Fn->getCallingConv());
570 }
571 }
572
573 // No need to keep global ctors/dtors for non-lib profile after call to
574 // ctors/dtors added for entry.
575 Triple T(M.getTargetTriple());
576 if (T.getEnvironment() != Triple::EnvironmentType::Library) {
577 if (auto *GV = M.getNamedGlobal(Name: "llvm.global_ctors"))
578 GV->eraseFromParent();
579 if (auto *GV = M.getNamedGlobal(Name: "llvm.global_dtors"))
580 GV->eraseFromParent();
581 }
582}
583
584static void initializeBuffer(CodeGenModule &CGM, llvm::GlobalVariable *GV,
585 Intrinsic::ID IntrID,
586 ArrayRef<llvm::Value *> Args) {
587
588 LLVMContext &Ctx = CGM.getLLVMContext();
589 llvm::Function *InitResFunc = llvm::Function::Create(
590 Ty: llvm::FunctionType::get(Result: CGM.VoidTy, isVarArg: false),
591 Linkage: llvm::GlobalValue::InternalLinkage,
592 N: ("_init_buffer_" + GV->getName()).str(), M&: CGM.getModule());
593 InitResFunc->addFnAttr(llvm::Attribute::AlwaysInline);
594
595 llvm::BasicBlock *EntryBB =
596 llvm::BasicBlock::Create(Context&: Ctx, Name: "entry", Parent: InitResFunc);
597 CGBuilderTy Builder(CGM, Ctx);
598 const DataLayout &DL = CGM.getModule().getDataLayout();
599 Builder.SetInsertPoint(EntryBB);
600
601 // Make sure the global variable is buffer resource handle
602 llvm::Type *HandleTy = GV->getValueType();
603 assert(HandleTy->isTargetExtTy() && "unexpected type of the buffer global");
604
605 llvm::Value *CreateHandle = Builder.CreateIntrinsic(
606 /*ReturnType=*/RetTy: HandleTy, ID: IntrID, Args, FMFSource: nullptr,
607 Name: Twine(GV->getName()).concat(Suffix: "_h"));
608
609 llvm::Value *HandleRef = Builder.CreateStructGEP(Ty: GV->getValueType(), Ptr: GV, Idx: 0);
610 Builder.CreateAlignedStore(Val: CreateHandle, Ptr: HandleRef,
611 Align: HandleRef->getPointerAlignment(DL));
612 Builder.CreateRetVoid();
613
614 CGM.AddCXXGlobalInit(F: InitResFunc);
615}
616
617void CGHLSLRuntime::initializeBufferFromBinding(const HLSLBufferDecl *BufDecl,
618 llvm::GlobalVariable *GV,
619 HLSLResourceBindingAttr *RBA) {
620 llvm::Type *Int1Ty = llvm::Type::getInt1Ty(C&: CGM.getLLVMContext());
621 auto *NonUniform = llvm::ConstantInt::get(Ty: Int1Ty, V: false);
622 auto *Index = llvm::ConstantInt::get(Ty: CGM.IntTy, V: 0);
623 auto *RangeSize = llvm::ConstantInt::get(Ty: CGM.IntTy, V: 1);
624 auto *Space =
625 llvm::ConstantInt::get(CGM.IntTy, RBA ? RBA->getSpaceNumber() : 0);
626 Value *Name = nullptr;
627
628 auto [IntrinsicID, HasNameArg] =
629 RBA->hasRegisterSlot()
630 ? CGM.getHLSLRuntime().getCreateHandleFromBindingIntrinsic()
631 : CGM.getHLSLRuntime().getCreateHandleFromImplicitBindingIntrinsic();
632
633 if (HasNameArg) {
634 std::string Str(BufDecl->getName());
635 std::string GlobalName(Str + ".str");
636 Name = CGM.GetAddrOfConstantCString(Str, GlobalName: GlobalName.c_str()).getPointer();
637 }
638
639 // buffer with explicit binding
640 if (RBA->hasRegisterSlot()) {
641 auto *RegSlot = llvm::ConstantInt::get(CGM.IntTy, RBA->getSlotNumber());
642 SmallVector<Value *> Args{Space, RegSlot, RangeSize, Index, NonUniform};
643 if (Name)
644 Args.push_back(Elt: Name);
645 initializeBuffer(CGM, GV, IntrinsicID, Args);
646 } else {
647 // buffer with implicit binding
648 auto *OrderID =
649 llvm::ConstantInt::get(CGM.IntTy, RBA->getImplicitBindingOrderID());
650 SmallVector<Value *> Args{OrderID, Space, RangeSize, Index, NonUniform};
651 if (Name)
652 Args.push_back(Elt: Name);
653 initializeBuffer(CGM, GV, IntrinsicID, Args);
654 }
655}
656
657void CGHLSLRuntime::handleGlobalVarDefinition(const VarDecl *VD,
658 llvm::GlobalVariable *GV) {
659 if (auto Attr = VD->getAttr<HLSLVkExtBuiltinInputAttr>())
660 addSPIRVBuiltinDecoration(GV, Attr->getBuiltIn());
661}
662
663llvm::Instruction *CGHLSLRuntime::getConvergenceToken(BasicBlock &BB) {
664 if (!CGM.shouldEmitConvergenceTokens())
665 return nullptr;
666
667 auto E = BB.end();
668 for (auto I = BB.begin(); I != E; ++I) {
669 auto *II = dyn_cast<llvm::IntrinsicInst>(Val: &*I);
670 if (II && llvm::isConvergenceControlIntrinsic(IntrinsicID: II->getIntrinsicID())) {
671 return II;
672 }
673 }
674 llvm_unreachable("Convergence token should have been emitted.");
675 return nullptr;
676}
677
678class OpaqueValueVisitor : public RecursiveASTVisitor<OpaqueValueVisitor> {
679public:
680 llvm::SmallPtrSet<OpaqueValueExpr *, 8> OVEs;
681 OpaqueValueVisitor() {}
682
683 bool VisitOpaqueValueExpr(OpaqueValueExpr *E) {
684 OVEs.insert(Ptr: E);
685 return true;
686 }
687};
688
689void CGHLSLRuntime::emitInitListOpaqueValues(CodeGenFunction &CGF,
690 InitListExpr *E) {
691
692 typedef CodeGenFunction::OpaqueValueMappingData OpaqueValueMappingData;
693 OpaqueValueVisitor Visitor;
694 Visitor.TraverseStmt(E);
695 for (auto *OVE : Visitor.OVEs) {
696 if (CGF.isOpaqueValueEmitted(E: OVE))
697 continue;
698 if (OpaqueValueMappingData::shouldBindAsLValue(OVE)) {
699 LValue LV = CGF.EmitLValue(E: OVE->getSourceExpr());
700 OpaqueValueMappingData::bind(CGF, ov: OVE, lv: LV);
701 } else {
702 RValue RV = CGF.EmitAnyExpr(E: OVE->getSourceExpr());
703 OpaqueValueMappingData::bind(CGF, ov: OVE, rv: RV);
704 }
705 }
706}
707

source code of clang/lib/CodeGen/CGHLSLRuntime.cpp