clang 20.0.0git
CGHLSLRuntime.cpp
Go to the documentation of this file.
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 "CodeGenModule.h"
18#include "TargetInfo.h"
19#include "clang/AST/Decl.h"
21#include "llvm/IR/GlobalVariable.h"
22#include "llvm/IR/LLVMContext.h"
23#include "llvm/IR/Metadata.h"
24#include "llvm/IR/Module.h"
25#include "llvm/IR/Value.h"
26#include "llvm/Support/Alignment.h"
27
28#include "llvm/Support/FormatVariadic.h"
29
30using namespace clang;
31using namespace CodeGen;
32using namespace clang::hlsl;
33using namespace llvm;
34
35namespace {
36
37void addDxilValVersion(StringRef ValVersionStr, llvm::Module &M) {
38 // The validation of ValVersionStr is done at HLSLToolChain::TranslateArgs.
39 // Assume ValVersionStr is legal here.
40 VersionTuple Version;
41 if (Version.tryParse(ValVersionStr) || Version.getBuild() ||
42 Version.getSubminor() || !Version.getMinor()) {
43 return;
44 }
45
46 uint64_t Major = Version.getMajor();
47 uint64_t Minor = *Version.getMinor();
48
49 auto &Ctx = M.getContext();
50 IRBuilder<> B(M.getContext());
51 MDNode *Val = MDNode::get(Ctx, {ConstantAsMetadata::get(B.getInt32(Major)),
52 ConstantAsMetadata::get(B.getInt32(Minor))});
53 StringRef DXILValKey = "dx.valver";
54 auto *DXILValMD = M.getOrInsertNamedMetadata(DXILValKey);
55 DXILValMD->addOperand(Val);
56}
57void addDisableOptimizations(llvm::Module &M) {
58 StringRef Key = "dx.disable_optimizations";
59 M.addModuleFlag(llvm::Module::ModFlagBehavior::Override, Key, 1);
60}
61// cbuffer will be translated into global variable in special address space.
62// If translate into C,
63// cbuffer A {
64// float a;
65// float b;
66// }
67// float foo() { return a + b; }
68//
69// will be translated into
70//
71// struct A {
72// float a;
73// float b;
74// } cbuffer_A __attribute__((address_space(4)));
75// float foo() { return cbuffer_A.a + cbuffer_A.b; }
76//
77// layoutBuffer will create the struct A type.
78// replaceBuffer will replace use of global variable a and b with cbuffer_A.a
79// and cbuffer_A.b.
80//
81void layoutBuffer(CGHLSLRuntime::Buffer &Buf, const DataLayout &DL) {
82 if (Buf.Constants.empty())
83 return;
84
85 std::vector<llvm::Type *> EltTys;
86 for (auto &Const : Buf.Constants) {
87 GlobalVariable *GV = Const.first;
88 Const.second = EltTys.size();
89 llvm::Type *Ty = GV->getValueType();
90 EltTys.emplace_back(Ty);
91 }
92 Buf.LayoutStruct = llvm::StructType::get(EltTys[0]->getContext(), EltTys);
93}
94
95GlobalVariable *replaceBuffer(CGHLSLRuntime::Buffer &Buf) {
96 // Create global variable for CB.
97 GlobalVariable *CBGV = new GlobalVariable(
98 Buf.LayoutStruct, /*isConstant*/ true,
99 GlobalValue::LinkageTypes::ExternalLinkage, nullptr,
100 llvm::formatv("{0}{1}", Buf.Name, Buf.IsCBuffer ? ".cb." : ".tb."),
101 GlobalValue::NotThreadLocal);
102
103 IRBuilder<> B(CBGV->getContext());
104 Value *ZeroIdx = B.getInt32(0);
105 // Replace Const use with CB use.
106 for (auto &[GV, Offset] : Buf.Constants) {
107 Value *GEP =
108 B.CreateGEP(Buf.LayoutStruct, CBGV, {ZeroIdx, B.getInt32(Offset)});
109
110 assert(Buf.LayoutStruct->getElementType(Offset) == GV->getValueType() &&
111 "constant type mismatch");
112
113 // Replace.
114 GV->replaceAllUsesWith(GEP);
115 // Erase GV.
116 GV->removeDeadConstantUsers();
117 GV->eraseFromParent();
118 }
119 return CBGV;
120}
121
122} // namespace
123
125 assert(T->isHLSLSpecificType() && "Not an HLSL specific type!");
126
127 // Check if the target has a specific translation for this type first.
128 if (llvm::Type *TargetTy = CGM.getTargetCodeGenInfo().getHLSLType(CGM, T))
129 return TargetTy;
130
131 llvm_unreachable("Generic handling of HLSL types is not supported.");
132}
133
134llvm::Triple::ArchType CGHLSLRuntime::getArch() {
135 return CGM.getTarget().getTriple().getArch();
136}
137
138void CGHLSLRuntime::addConstant(VarDecl *D, Buffer &CB) {
139 if (D->getStorageClass() == SC_Static) {
140 // For static inside cbuffer, take as global static.
141 // Don't add to cbuffer.
143 return;
144 }
145
146 auto *GV = cast<GlobalVariable>(CGM.GetAddrOfGlobalVar(D));
147 // Add debug info for constVal.
149 if (CGM.getCodeGenOpts().getDebugInfo() >=
150 codegenoptions::DebugInfoKind::LimitedDebugInfo)
151 DI->EmitGlobalVariable(cast<GlobalVariable>(GV), D);
152
153 // FIXME: support packoffset.
154 // See https://github.com/llvm/llvm-project/issues/57914.
155 uint32_t Offset = 0;
156 bool HasUserOffset = false;
157
158 unsigned LowerBound = HasUserOffset ? Offset : UINT_MAX;
159 CB.Constants.emplace_back(std::make_pair(GV, LowerBound));
160}
161
162void CGHLSLRuntime::addBufferDecls(const DeclContext *DC, Buffer &CB) {
163 for (Decl *it : DC->decls()) {
164 if (auto *ConstDecl = dyn_cast<VarDecl>(it)) {
165 addConstant(ConstDecl, CB);
166 } else if (isa<CXXRecordDecl, EmptyDecl>(it)) {
167 // Nothing to do for this declaration.
168 } else if (isa<FunctionDecl>(it)) {
169 // A function within an cbuffer is effectively a top-level function,
170 // as it only refers to globally scoped declarations.
172 }
173 }
174}
175
177 Buffers.emplace_back(Buffer(D));
178 addBufferDecls(D, Buffers.back());
179}
180
182 auto &TargetOpts = CGM.getTarget().getTargetOpts();
183 llvm::Module &M = CGM.getModule();
184 Triple T(M.getTargetTriple());
185 if (T.getArch() == Triple::ArchType::dxil)
186 addDxilValVersion(TargetOpts.DxilValidatorVersion, M);
187
189 if (CGM.getCodeGenOpts().OptimizationLevel == 0)
190 addDisableOptimizations(M);
191
192 const DataLayout &DL = M.getDataLayout();
193
194 for (auto &Buf : Buffers) {
195 layoutBuffer(Buf, DL);
196 GlobalVariable *GV = replaceBuffer(Buf);
197 M.insertGlobalVariable(GV);
198 llvm::hlsl::ResourceClass RC = Buf.IsCBuffer
199 ? llvm::hlsl::ResourceClass::CBuffer
200 : llvm::hlsl::ResourceClass::SRV;
201 llvm::hlsl::ResourceKind RK = Buf.IsCBuffer
202 ? llvm::hlsl::ResourceKind::CBuffer
203 : llvm::hlsl::ResourceKind::TBuffer;
204 addBufferResourceAnnotation(GV, RC, RK, /*IsROV=*/false,
205 llvm::hlsl::ElementType::Invalid, Buf.Binding);
206 }
207}
208
210 : Name(D->getName()), IsCBuffer(D->isCBuffer()),
211 Binding(D->getAttr<HLSLResourceBindingAttr>()) {}
212
213void CGHLSLRuntime::addBufferResourceAnnotation(llvm::GlobalVariable *GV,
214 llvm::hlsl::ResourceClass RC,
215 llvm::hlsl::ResourceKind RK,
216 bool IsROV,
217 llvm::hlsl::ElementType ET,
218 BufferResBinding &Binding) {
219 llvm::Module &M = CGM.getModule();
220
221 NamedMDNode *ResourceMD = nullptr;
222 switch (RC) {
223 case llvm::hlsl::ResourceClass::UAV:
224 ResourceMD = M.getOrInsertNamedMetadata("hlsl.uavs");
225 break;
226 case llvm::hlsl::ResourceClass::SRV:
227 ResourceMD = M.getOrInsertNamedMetadata("hlsl.srvs");
228 break;
229 case llvm::hlsl::ResourceClass::CBuffer:
230 ResourceMD = M.getOrInsertNamedMetadata("hlsl.cbufs");
231 break;
232 default:
233 assert(false && "Unsupported buffer type!");
234 return;
235 }
236 assert(ResourceMD != nullptr &&
237 "ResourceMD must have been set by the switch above.");
238
239 llvm::hlsl::FrontendResource Res(
240 GV, RK, ET, IsROV, Binding.Reg.value_or(UINT_MAX), Binding.Space);
241 ResourceMD->addOperand(Res.getMetadata());
242}
243
244static llvm::hlsl::ElementType
245calculateElementType(const ASTContext &Context, const clang::Type *ResourceTy) {
246 using llvm::hlsl::ElementType;
247
248 // TODO: We may need to update this when we add things like ByteAddressBuffer
249 // that don't have a template parameter (or, indeed, an element type).
250 const auto *TST = ResourceTy->getAs<TemplateSpecializationType>();
251 assert(TST && "Resource types must be template specializations");
252 ArrayRef<TemplateArgument> Args = TST->template_arguments();
253 assert(!Args.empty() && "Resource has no element type");
254
255 // At this point we have a resource with an element type, so we can assume
256 // that it's valid or we would have diagnosed the error earlier.
257 QualType ElTy = Args[0].getAsType();
258
259 // We should either have a basic type or a vector of a basic type.
260 if (const auto *VecTy = ElTy->getAs<clang::VectorType>())
261 ElTy = VecTy->getElementType();
262
263 if (ElTy->isSignedIntegerType()) {
264 switch (Context.getTypeSize(ElTy)) {
265 case 16:
266 return ElementType::I16;
267 case 32:
268 return ElementType::I32;
269 case 64:
270 return ElementType::I64;
271 }
272 } else if (ElTy->isUnsignedIntegerType()) {
273 switch (Context.getTypeSize(ElTy)) {
274 case 16:
275 return ElementType::U16;
276 case 32:
277 return ElementType::U32;
278 case 64:
279 return ElementType::U64;
280 }
281 } else if (ElTy->isSpecificBuiltinType(BuiltinType::Half))
282 return ElementType::F16;
283 else if (ElTy->isSpecificBuiltinType(BuiltinType::Float))
284 return ElementType::F32;
285 else if (ElTy->isSpecificBuiltinType(BuiltinType::Double))
286 return ElementType::F64;
287
288 // TODO: We need to handle unorm/snorm float types here once we support them
289 llvm_unreachable("Invalid element type for resource");
290}
291
292void CGHLSLRuntime::annotateHLSLResource(const VarDecl *D, GlobalVariable *GV) {
293 const Type *Ty = D->getType()->getPointeeOrArrayElementType();
294 if (!Ty)
295 return;
296 const auto *RD = Ty->getAsCXXRecordDecl();
297 if (!RD)
298 return;
299 // the resource related attributes are on the handle member
300 // inside the record decl
301 for (auto *FD : RD->fields()) {
302 const auto *HLSLResAttr = FD->getAttr<HLSLResourceAttr>();
303 const HLSLAttributedResourceType *AttrResType =
304 dyn_cast<HLSLAttributedResourceType>(FD->getType().getTypePtr());
305 if (!HLSLResAttr || !AttrResType)
306 continue;
307
308 llvm::hlsl::ResourceClass RC = AttrResType->getAttrs().ResourceClass;
309 if (RC == llvm::hlsl::ResourceClass::UAV ||
310 RC == llvm::hlsl::ResourceClass::SRV)
311 // UAVs and SRVs have already been converted to use LLVM target types,
312 // we can disable generating of these resource annotations. This will
313 // enable progress on structured buffers with user defined types this
314 // resource annotations code does not handle and it crashes.
315 // This whole function is going to be removed as soon as cbuffers are
316 // converted to target types (llvm/llvm-project #114126).
317 return;
318
319 bool IsROV = AttrResType->getAttrs().IsROV;
320 llvm::hlsl::ResourceKind RK = HLSLResAttr->getResourceKind();
321 llvm::hlsl::ElementType ET = calculateElementType(CGM.getContext(), Ty);
322
323 BufferResBinding Binding(D->getAttr<HLSLResourceBindingAttr>());
324 addBufferResourceAnnotation(GV, RC, RK, IsROV, ET, Binding);
325 }
326}
327
329 HLSLResourceBindingAttr *Binding) {
330 if (Binding) {
331 llvm::APInt RegInt(64, 0);
332 Binding->getSlot().substr(1).getAsInteger(10, RegInt);
333 Reg = RegInt.getLimitedValue();
334 llvm::APInt SpaceInt(64, 0);
335 Binding->getSpace().substr(5).getAsInteger(10, SpaceInt);
336 Space = SpaceInt.getLimitedValue();
337 } else {
338 Space = 0;
339 }
340}
341
343 const FunctionDecl *FD, llvm::Function *Fn) {
344 const auto *ShaderAttr = FD->getAttr<HLSLShaderAttr>();
345 assert(ShaderAttr && "All entry functions must have a HLSLShaderAttr");
346 const StringRef ShaderAttrKindStr = "hlsl.shader";
347 Fn->addFnAttr(ShaderAttrKindStr,
348 llvm::Triple::getEnvironmentTypeName(ShaderAttr->getType()));
349 if (HLSLNumThreadsAttr *NumThreadsAttr = FD->getAttr<HLSLNumThreadsAttr>()) {
350 const StringRef NumThreadsKindStr = "hlsl.numthreads";
351 std::string NumThreadsStr =
352 formatv("{0},{1},{2}", NumThreadsAttr->getX(), NumThreadsAttr->getY(),
353 NumThreadsAttr->getZ());
354 Fn->addFnAttr(NumThreadsKindStr, NumThreadsStr);
355 }
356 if (HLSLWaveSizeAttr *WaveSizeAttr = FD->getAttr<HLSLWaveSizeAttr>()) {
357 const StringRef WaveSizeKindStr = "hlsl.wavesize";
358 std::string WaveSizeStr =
359 formatv("{0},{1},{2}", WaveSizeAttr->getMin(), WaveSizeAttr->getMax(),
360 WaveSizeAttr->getPreferred());
361 Fn->addFnAttr(WaveSizeKindStr, WaveSizeStr);
362 }
363 Fn->addFnAttr(llvm::Attribute::NoInline);
364}
365
366static Value *buildVectorInput(IRBuilder<> &B, Function *F, llvm::Type *Ty) {
367 if (const auto *VT = dyn_cast<FixedVectorType>(Ty)) {
368 Value *Result = PoisonValue::get(Ty);
369 for (unsigned I = 0; I < VT->getNumElements(); ++I) {
370 Value *Elt = B.CreateCall(F, {B.getInt32(I)});
371 Result = B.CreateInsertElement(Result, Elt, I);
372 }
373 return Result;
374 }
375 return B.CreateCall(F, {B.getInt32(0)});
376}
377
378llvm::Value *CGHLSLRuntime::emitInputSemantic(IRBuilder<> &B,
379 const ParmVarDecl &D,
380 llvm::Type *Ty) {
381 assert(D.hasAttrs() && "Entry parameter missing annotation attribute!");
382 if (D.hasAttr<HLSLSV_GroupIndexAttr>()) {
383 llvm::Function *DxGroupIndex =
384 CGM.getIntrinsic(Intrinsic::dx_flattened_thread_id_in_group);
385 return B.CreateCall(FunctionCallee(DxGroupIndex));
386 }
387 if (D.hasAttr<HLSLSV_DispatchThreadIDAttr>()) {
388 llvm::Function *ThreadIDIntrinsic =
389 CGM.getIntrinsic(getThreadIdIntrinsic());
390 return buildVectorInput(B, ThreadIDIntrinsic, Ty);
391 }
392 if (D.hasAttr<HLSLSV_GroupThreadIDAttr>()) {
393 llvm::Function *GroupThreadIDIntrinsic =
394 CGM.getIntrinsic(getGroupThreadIdIntrinsic());
395 return buildVectorInput(B, GroupThreadIDIntrinsic, Ty);
396 }
397 if (D.hasAttr<HLSLSV_GroupIDAttr>()) {
398 llvm::Function *GroupIDIntrinsic = CGM.getIntrinsic(Intrinsic::dx_group_id);
399 return buildVectorInput(B, GroupIDIntrinsic, Ty);
400 }
401 assert(false && "Unhandled parameter attribute");
402 return nullptr;
403}
404
406 llvm::Function *Fn) {
407 llvm::Module &M = CGM.getModule();
408 llvm::LLVMContext &Ctx = M.getContext();
409 auto *EntryTy = llvm::FunctionType::get(llvm::Type::getVoidTy(Ctx), false);
410 Function *EntryFn =
411 Function::Create(EntryTy, Function::ExternalLinkage, FD->getName(), &M);
412
413 // Copy function attributes over, we have no argument or return attributes
414 // that can be valid on the real entry.
415 AttributeList NewAttrs = AttributeList::get(Ctx, AttributeList::FunctionIndex,
416 Fn->getAttributes().getFnAttrs());
417 EntryFn->setAttributes(NewAttrs);
418 setHLSLEntryAttributes(FD, EntryFn);
419
420 // Set the called function as internal linkage.
421 Fn->setLinkage(GlobalValue::InternalLinkage);
422
423 BasicBlock *BB = BasicBlock::Create(Ctx, "entry", EntryFn);
424 IRBuilder<> B(BB);
426
429 assert(EntryFn->isConvergent());
430 llvm::Value *I = B.CreateIntrinsic(
431 llvm::Intrinsic::experimental_convergence_entry, {}, {});
432 llvm::Value *bundleArgs[] = {I};
433 OB.emplace_back("convergencectrl", bundleArgs);
434 }
435
436 // FIXME: support struct parameters where semantics are on members.
437 // See: https://github.com/llvm/llvm-project/issues/57874
438 unsigned SRetOffset = 0;
439 for (const auto &Param : Fn->args()) {
440 if (Param.hasStructRetAttr()) {
441 // FIXME: support output.
442 // See: https://github.com/llvm/llvm-project/issues/57874
443 SRetOffset = 1;
444 Args.emplace_back(PoisonValue::get(Param.getType()));
445 continue;
446 }
447 const ParmVarDecl *PD = FD->getParamDecl(Param.getArgNo() - SRetOffset);
448 Args.push_back(emitInputSemantic(B, *PD, Param.getType()));
449 }
450
451 CallInst *CI = B.CreateCall(FunctionCallee(Fn), Args, OB);
452 CI->setCallingConv(Fn->getCallingConv());
453 // FIXME: Handle codegen for return type semantics.
454 // See: https://github.com/llvm/llvm-project/issues/57875
455 B.CreateRetVoid();
456}
457
459 llvm::Function *Fn) {
460 if (FD->isInExportDeclContext()) {
461 const StringRef ExportAttrKindStr = "hlsl.export";
462 Fn->addFnAttr(ExportAttrKindStr);
463 }
464}
465
466static void gatherFunctions(SmallVectorImpl<Function *> &Fns, llvm::Module &M,
467 bool CtorOrDtor) {
468 const auto *GV =
469 M.getNamedGlobal(CtorOrDtor ? "llvm.global_ctors" : "llvm.global_dtors");
470 if (!GV)
471 return;
472 const auto *CA = dyn_cast<ConstantArray>(GV->getInitializer());
473 if (!CA)
474 return;
475 // The global_ctor array elements are a struct [Priority, Fn *, COMDat].
476 // HLSL neither supports priorities or COMDat values, so we will check those
477 // in an assert but not handle them.
478
480 for (const auto &Ctor : CA->operands()) {
481 if (isa<ConstantAggregateZero>(Ctor))
482 continue;
483 ConstantStruct *CS = cast<ConstantStruct>(Ctor);
484
485 assert(cast<ConstantInt>(CS->getOperand(0))->getValue() == 65535 &&
486 "HLSL doesn't support setting priority for global ctors.");
487 assert(isa<ConstantPointerNull>(CS->getOperand(2)) &&
488 "HLSL doesn't support COMDat for global ctors.");
489 Fns.push_back(cast<Function>(CS->getOperand(1)));
490 }
491}
492
494 llvm::Module &M = CGM.getModule();
497 gatherFunctions(CtorFns, M, true);
498 gatherFunctions(DtorFns, M, false);
499
500 // Insert a call to the global constructor at the beginning of the entry block
501 // to externally exported functions. This is a bit of a hack, but HLSL allows
502 // global constructors, but doesn't support driver initialization of globals.
503 for (auto &F : M.functions()) {
504 if (!F.hasFnAttribute("hlsl.shader"))
505 continue;
506 auto *Token = getConvergenceToken(F.getEntryBlock());
507 Instruction *IP = &*F.getEntryBlock().begin();
509 if (Token) {
510 llvm::Value *bundleArgs[] = {Token};
511 OB.emplace_back("convergencectrl", bundleArgs);
512 IP = Token->getNextNode();
513 }
514 IRBuilder<> B(IP);
515 for (auto *Fn : CtorFns) {
516 auto CI = B.CreateCall(FunctionCallee(Fn), {}, OB);
517 CI->setCallingConv(Fn->getCallingConv());
518 }
519
520 // Insert global dtors before the terminator of the last instruction
521 B.SetInsertPoint(F.back().getTerminator());
522 for (auto *Fn : DtorFns) {
523 auto CI = B.CreateCall(FunctionCallee(Fn), {}, OB);
524 CI->setCallingConv(Fn->getCallingConv());
525 }
526 }
527
528 // No need to keep global ctors/dtors for non-lib profile after call to
529 // ctors/dtors added for entry.
530 Triple T(M.getTargetTriple());
531 if (T.getEnvironment() != Triple::EnvironmentType::Library) {
532 if (auto *GV = M.getNamedGlobal("llvm.global_ctors"))
533 GV->eraseFromParent();
534 if (auto *GV = M.getNamedGlobal("llvm.global_dtors"))
535 GV->eraseFromParent();
536 }
537}
538
540 llvm::GlobalVariable *GV) {
541 // If the global variable has resource binding, add it to the list of globals
542 // that need resource binding initialization.
543 const HLSLResourceBindingAttr *RBA = VD->getAttr<HLSLResourceBindingAttr>();
544 if (!RBA)
545 return;
546
548 VD->getType().getTypePtr()))
549 // FIXME: Only simple declarations of resources are supported for now.
550 // Arrays of resources or resources in user defined classes are
551 // not implemented yet.
552 return;
553
554 ResourcesToBind.emplace_back(VD, GV);
555}
556
558 return !ResourcesToBind.empty();
559}
560
562 // No resources to bind
563 assert(needsResourceBindingInitFn() && "no resources to bind");
564
565 LLVMContext &Ctx = CGM.getLLVMContext();
566 llvm::Type *Int1Ty = llvm::Type::getInt1Ty(Ctx);
567
568 llvm::Function *InitResBindingsFunc =
569 llvm::Function::Create(llvm::FunctionType::get(CGM.VoidTy, false),
570 llvm::GlobalValue::InternalLinkage,
571 "_init_resource_bindings", CGM.getModule());
572
573 llvm::BasicBlock *EntryBB =
574 llvm::BasicBlock::Create(Ctx, "entry", InitResBindingsFunc);
575 CGBuilderTy Builder(CGM, Ctx);
576 const DataLayout &DL = CGM.getModule().getDataLayout();
577 Builder.SetInsertPoint(EntryBB);
578
579 for (const auto &[VD, GV] : ResourcesToBind) {
580 for (Attr *A : VD->getAttrs()) {
581 HLSLResourceBindingAttr *RBA = dyn_cast<HLSLResourceBindingAttr>(A);
582 if (!RBA)
583 continue;
584
585 const HLSLAttributedResourceType *AttrResType =
587 VD->getType().getTypePtr());
588
589 // FIXME: Only simple declarations of resources are supported for now.
590 // Arrays of resources or resources in user defined classes are
591 // not implemented yet.
592 assert(AttrResType != nullptr &&
593 "Resource class must have a handle of HLSLAttributedResourceType");
594
595 llvm::Type *TargetTy =
596 CGM.getTargetCodeGenInfo().getHLSLType(CGM, AttrResType);
597 assert(TargetTy != nullptr &&
598 "Failed to convert resource handle to target type");
599
600 auto *Space = llvm::ConstantInt::get(CGM.IntTy, RBA->getSpaceNumber());
601 auto *Slot = llvm::ConstantInt::get(CGM.IntTy, RBA->getSlotNumber());
602 // FIXME: resource arrays are not yet implemented
603 auto *Range = llvm::ConstantInt::get(CGM.IntTy, 1);
604 auto *Index = llvm::ConstantInt::get(CGM.IntTy, 0);
605 // FIXME: NonUniformResourceIndex bit is not yet implemented
606 auto *NonUniform = llvm::ConstantInt::get(Int1Ty, false);
607 llvm::Value *Args[] = {Space, Slot, Range, Index, NonUniform};
608
609 llvm::Value *CreateHandle = Builder.CreateIntrinsic(
610 /*ReturnType=*/TargetTy, getCreateHandleFromBindingIntrinsic(), Args,
611 nullptr, Twine(VD->getName()).concat("_h"));
612
613 llvm::Value *HandleRef =
614 Builder.CreateStructGEP(GV->getValueType(), GV, 0);
615 Builder.CreateAlignedStore(CreateHandle, HandleRef,
616 HandleRef->getPointerAlignment(DL));
617 }
618 }
619
620 Builder.CreateRetVoid();
621 return InitResBindingsFunc;
622}
623
624llvm::Instruction *CGHLSLRuntime::getConvergenceToken(BasicBlock &BB) {
626 return nullptr;
627
628 auto E = BB.end();
629 for (auto I = BB.begin(); I != E; ++I) {
630 auto *II = dyn_cast<llvm::IntrinsicInst>(&*I);
631 if (II && llvm::isConvergenceControlIntrinsic(II->getIntrinsicID())) {
632 return II;
633 }
634 }
635 llvm_unreachable("Convergence token should have been emitted.");
636 return nullptr;
637}
static llvm::hlsl::ElementType calculateElementType(const ASTContext &Context, const clang::Type *ResourceTy)
static void gatherFunctions(SmallVectorImpl< Function * > &Fns, llvm::Module &M, bool CtorOrDtor)
static Value * buildVectorInput(IRBuilder<> &B, Function *F, llvm::Type *Ty)
const Decl * D
Expr * E
static std::string getName(const CallEvent &Call)
SourceRange Range
Definition: SemaObjC.cpp:758
Defines the clang::TargetOptions class.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:188
uint64_t getTypeSize(QualType T) const
Return the size of the specified (complete) type T, in bits.
Definition: ASTContext.h:2482
Attr - This represents one attribute.
Definition: Attr.h:43
This class gathers all debug information during compilation and is responsible for emitting to llvm g...
Definition: CGDebugInfo.h:58
llvm::Instruction * getConvergenceToken(llvm::BasicBlock &BB)
void setHLSLEntryAttributes(const FunctionDecl *FD, llvm::Function *Fn)
void setHLSLFunctionAttributes(const FunctionDecl *FD, llvm::Function *Fn)
void emitEntryFunction(const FunctionDecl *FD, llvm::Function *Fn)
void handleGlobalVarDefinition(const VarDecl *VD, llvm::GlobalVariable *Var)
llvm::Type * convertHLSLSpecificType(const Type *T)
llvm::Value * emitInputSemantic(llvm::IRBuilder<> &B, const ParmVarDecl &D, llvm::Type *Ty)
llvm::Function * createResourceBindingInitFn()
void annotateHLSLResource(const VarDecl *D, llvm::GlobalVariable *GV)
void addBuffer(const HLSLBufferDecl *D)
llvm::Module & getModule() const
CGDebugInfo * getModuleDebugInfo()
const TargetInfo & getTarget() const
void EmitGlobal(GlobalDecl D)
Emit code for a single global function or var decl.
bool shouldEmitConvergenceTokens() const
ASTContext & getContext() const
llvm::Constant * GetAddrOfGlobalVar(const VarDecl *D, llvm::Type *Ty=nullptr, ForDefinition_t IsForDefinition=NotForDefinition)
Return the llvm::Constant for the address of the given global variable.
const TargetCodeGenInfo & getTargetCodeGenInfo()
const CodeGenOptions & getCodeGenOpts() const
llvm::LLVMContext & getLLVMContext()
llvm::Function * getIntrinsic(unsigned IID, ArrayRef< llvm::Type * > Tys={})
void EmitTopLevelDecl(Decl *D)
Emit code for a single top level declaration.
virtual llvm::Type * getHLSLType(CodeGenModule &CGM, const Type *T) const
Return an LLVM type that corresponds to a HLSL type.
Definition: TargetInfo.h:442
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
Definition: DeclBase.h:1435
decl_range decls() const
decls_begin/decls_end - Iterate over the declarations stored in this context.
Definition: DeclBase.h:2349
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:86
T * getAttr() const
Definition: DeclBase.h:576
bool hasAttrs() const
Definition: DeclBase.h:521
bool isInExportDeclContext() const
Whether this declaration was exported in a lexical context.
Definition: DeclBase.cpp:1109
bool hasAttr() const
Definition: DeclBase.h:580
Represents a function declaration or definition.
Definition: Decl.h:1935
const ParmVarDecl * getParamDecl(unsigned i) const
Definition: Decl.h:2672
static const HLSLAttributedResourceType * findHandleTypeOnResource(const Type *RT)
Definition: Type.cpp:5433
HLSLBufferDecl - Represent a cbuffer or tbuffer declaration.
Definition: Decl.h:4927
StringRef getName() const
Get the name of identifier for this declaration as a StringRef.
Definition: Decl.h:280
Represents a parameter to a function.
Definition: Decl.h:1725
A (possibly-)qualified type.
Definition: Type.h:929
const Type * getTypePtr() const
Retrieves a pointer to the underlying (unqualified) type.
Definition: Type.h:7931
TargetOptions & getTargetOpts() const
Retrieve the target options.
Definition: TargetInfo.h:311
const llvm::Triple & getTriple() const
Returns the target triple of the primary target.
Definition: TargetInfo.h:1262
Represents a type template specialization; the template must be a class template, a type alias templa...
Definition: Type.h:6661
Token - This structure provides full information about a lexed token.
Definition: Token.h:36
The base class of the type hierarchy.
Definition: Type.h:1828
CXXRecordDecl * getAsCXXRecordDecl() const
Retrieves the CXXRecordDecl that this type refers to, either because the type is a RecordType or beca...
Definition: Type.cpp:1916
bool isSignedIntegerType() const
Return true if this is an integer type that is signed, according to C99 6.2.5p4 [char,...
Definition: Type.cpp:2180
bool isSpecificBuiltinType(unsigned K) const
Test for a particular builtin type.
Definition: Type.h:8479
bool isHLSLSpecificType() const
Definition: Type.h:8467
bool isUnsignedIntegerType() const
Return true if this is an integer type that is unsigned, according to C99 6.2.5p6 [which returns true...
Definition: Type.cpp:2230
const T * getAs() const
Member-template getAs<specific type>'.
Definition: Type.h:8731
QualType getType() const
Definition: Decl.h:682
Represents a variable declaration or definition.
Definition: Decl.h:882
Represents a GCC generic vector type.
Definition: Type.h:4034
#define UINT_MAX
Definition: limits.h:64
bool Const(InterpState &S, CodePtr OpPC, const T &Arg)
Definition: Interp.h:1243
The JSON file list parser is used to communicate input to InstallAPI.
@ SC_Static
Definition: Specifiers.h:252
@ Result
The result type of a method or function.
const FunctionProtoType * T
unsigned long uint64_t
Diagnostic wrappers for TextAPI types for error reporting.
Definition: Dominators.h:30
BufferResBinding(HLSLResourceBindingAttr *Attr)
std::vector< std::pair< llvm::GlobalVariable *, unsigned > > Constants
llvm::IntegerType * IntTy
int