clang 23.0.0git
SemaHLSL.cpp
Go to the documentation of this file.
1//===- SemaHLSL.cpp - Semantic Analysis for HLSL constructs ---------------===//
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// This implements Semantic Analysis for HLSL constructs.
9//===----------------------------------------------------------------------===//
10
11#include "clang/Sema/SemaHLSL.h"
14#include "clang/AST/Attr.h"
15#include "clang/AST/Decl.h"
16#include "clang/AST/DeclBase.h"
17#include "clang/AST/DeclCXX.h"
20#include "clang/AST/Expr.h"
22#include "clang/AST/Type.h"
23#include "clang/AST/TypeBase.h"
24#include "clang/AST/TypeLoc.h"
28#include "clang/Basic/LLVM.h"
33#include "clang/Sema/Lookup.h"
35#include "clang/Sema/Sema.h"
36#include "clang/Sema/Template.h"
37#include "llvm/ADT/ArrayRef.h"
38#include "llvm/ADT/STLExtras.h"
39#include "llvm/ADT/SmallVector.h"
40#include "llvm/ADT/StringExtras.h"
41#include "llvm/ADT/StringRef.h"
42#include "llvm/ADT/Twine.h"
43#include "llvm/Frontend/HLSL/HLSLBinding.h"
44#include "llvm/Frontend/HLSL/RootSignatureValidations.h"
45#include "llvm/Support/Casting.h"
46#include "llvm/Support/DXILABI.h"
47#include "llvm/Support/ErrorHandling.h"
48#include "llvm/Support/FormatVariadic.h"
49#include "llvm/TargetParser/Triple.h"
50#include <cmath>
51#include <cstddef>
52#include <iterator>
53#include <utility>
54
55using namespace clang;
56using namespace clang::hlsl;
57using RegisterType = HLSLResourceBindingAttr::RegisterType;
58
60 CXXRecordDecl *StructDecl);
61
63 switch (RC) {
64 case ResourceClass::SRV:
65 return RegisterType::SRV;
66 case ResourceClass::UAV:
67 return RegisterType::UAV;
68 case ResourceClass::CBuffer:
69 return RegisterType::CBuffer;
70 case ResourceClass::Sampler:
71 return RegisterType::Sampler;
72 }
73 llvm_unreachable("unexpected ResourceClass value");
74}
75
76static RegisterType getRegisterType(const HLSLAttributedResourceType *ResTy) {
77 return getRegisterType(ResTy->getAttrs().ResourceClass);
78}
79
80// Converts the first letter of string Slot to RegisterType.
81// Returns false if the letter does not correspond to a valid register type.
82static bool convertToRegisterType(StringRef Slot, RegisterType *RT) {
83 assert(RT != nullptr);
84 switch (Slot[0]) {
85 case 't':
86 case 'T':
87 *RT = RegisterType::SRV;
88 return true;
89 case 'u':
90 case 'U':
91 *RT = RegisterType::UAV;
92 return true;
93 case 'b':
94 case 'B':
95 *RT = RegisterType::CBuffer;
96 return true;
97 case 's':
98 case 'S':
99 *RT = RegisterType::Sampler;
100 return true;
101 case 'c':
102 case 'C':
103 *RT = RegisterType::C;
104 return true;
105 case 'i':
106 case 'I':
107 *RT = RegisterType::I;
108 return true;
109 default:
110 return false;
111 }
112}
113
115 switch (RT) {
116 case RegisterType::SRV:
117 return 't';
118 case RegisterType::UAV:
119 return 'u';
120 case RegisterType::CBuffer:
121 return 'b';
122 case RegisterType::Sampler:
123 return 's';
124 case RegisterType::C:
125 return 'c';
126 case RegisterType::I:
127 return 'i';
128 }
129 llvm_unreachable("unexpected RegisterType value");
130}
131
133 switch (RT) {
134 case RegisterType::SRV:
135 return ResourceClass::SRV;
136 case RegisterType::UAV:
137 return ResourceClass::UAV;
138 case RegisterType::CBuffer:
139 return ResourceClass::CBuffer;
140 case RegisterType::Sampler:
141 return ResourceClass::Sampler;
142 case RegisterType::C:
143 case RegisterType::I:
144 // Deliberately falling through to the unreachable below.
145 break;
146 }
147 llvm_unreachable("unexpected RegisterType value");
148}
149
151 const auto *BT = dyn_cast<BuiltinType>(Type);
152 if (!BT) {
153 if (!Type->isEnumeralType())
154 return Builtin::NotBuiltin;
155 return Builtin::BI__builtin_get_spirv_spec_constant_int;
156 }
157
158 switch (BT->getKind()) {
159 case BuiltinType::Bool:
160 return Builtin::BI__builtin_get_spirv_spec_constant_bool;
161 case BuiltinType::Short:
162 return Builtin::BI__builtin_get_spirv_spec_constant_short;
163 case BuiltinType::Int:
164 return Builtin::BI__builtin_get_spirv_spec_constant_int;
165 case BuiltinType::LongLong:
166 return Builtin::BI__builtin_get_spirv_spec_constant_longlong;
167 case BuiltinType::UShort:
168 return Builtin::BI__builtin_get_spirv_spec_constant_ushort;
169 case BuiltinType::UInt:
170 return Builtin::BI__builtin_get_spirv_spec_constant_uint;
171 case BuiltinType::ULongLong:
172 return Builtin::BI__builtin_get_spirv_spec_constant_ulonglong;
173 case BuiltinType::Half:
174 return Builtin::BI__builtin_get_spirv_spec_constant_half;
175 case BuiltinType::Float:
176 return Builtin::BI__builtin_get_spirv_spec_constant_float;
177 case BuiltinType::Double:
178 return Builtin::BI__builtin_get_spirv_spec_constant_double;
179 default:
180 return Builtin::NotBuiltin;
181 }
182}
183
184static StringRef createRegisterString(ASTContext &AST, RegisterType RegType,
185 unsigned N) {
187 llvm::raw_svector_ostream OS(Buffer);
188 OS << getRegisterTypeChar(RegType);
189 OS << N;
190 return AST.backupStr(OS.str());
191}
192
194 ResourceClass ResClass) {
195 assert(getDeclBindingInfo(VD, ResClass) == nullptr &&
196 "DeclBindingInfo already added");
197 assert(!hasBindingInfoForDecl(VD) || BindingsList.back().Decl == VD);
198 // VarDecl may have multiple entries for different resource classes.
199 // DeclToBindingListIndex stores the index of the first binding we saw
200 // for this decl. If there are any additional ones then that index
201 // shouldn't be updated.
202 DeclToBindingListIndex.try_emplace(VD, BindingsList.size());
203 return &BindingsList.emplace_back(VD, ResClass);
204}
205
207 ResourceClass ResClass) {
208 auto Entry = DeclToBindingListIndex.find(VD);
209 if (Entry != DeclToBindingListIndex.end()) {
210 for (unsigned Index = Entry->getSecond();
211 Index < BindingsList.size() && BindingsList[Index].Decl == VD;
212 ++Index) {
213 if (BindingsList[Index].ResClass == ResClass)
214 return &BindingsList[Index];
215 }
216 }
217 return nullptr;
218}
219
221 return DeclToBindingListIndex.contains(VD);
222}
223
225
226Decl *SemaHLSL::ActOnStartBuffer(Scope *BufferScope, bool CBuffer,
227 SourceLocation KwLoc, IdentifierInfo *Ident,
228 SourceLocation IdentLoc,
229 SourceLocation LBrace) {
230 // For anonymous namespace, take the location of the left brace.
231 DeclContext *LexicalParent = SemaRef.getCurLexicalContext();
233 getASTContext(), LexicalParent, CBuffer, KwLoc, Ident, IdentLoc, LBrace);
234
235 // if CBuffer is false, then it's a TBuffer
236 auto RC = CBuffer ? llvm::hlsl::ResourceClass::CBuffer
237 : llvm::hlsl::ResourceClass::SRV;
238 Result->addAttr(HLSLResourceClassAttr::CreateImplicit(getASTContext(), RC));
239
240 SemaRef.PushOnScopeChains(Result, BufferScope);
241 SemaRef.PushDeclContext(BufferScope, Result);
242
243 return Result;
244}
245
246static unsigned calculateLegacyCbufferFieldAlign(const ASTContext &Context,
247 QualType T) {
248 // Arrays, Matrices, and Structs are always aligned to new buffer rows
249 if (T->isArrayType() || T->isStructureType() || T->isConstantMatrixType())
250 return 16;
251
252 // Vectors are aligned to the type they contain
253 if (const VectorType *VT = T->getAs<VectorType>())
254 return calculateLegacyCbufferFieldAlign(Context, VT->getElementType());
255
256 assert(Context.getTypeSize(T) <= 64 &&
257 "Scalar bit widths larger than 64 not supported");
258
259 // Scalar types are aligned to their byte width
260 return Context.getTypeSize(T) / 8;
261}
262
263// Calculate the size of a legacy cbuffer type in bytes based on
264// https://learn.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl-packing-rules
265static unsigned calculateLegacyCbufferSize(const ASTContext &Context,
266 QualType T) {
267 constexpr unsigned CBufferAlign = 16;
268 if (const auto *RD = T->getAsRecordDecl()) {
269 unsigned Size = 0;
270 for (const FieldDecl *Field : RD->fields()) {
271 QualType Ty = Field->getType();
272 unsigned FieldSize = calculateLegacyCbufferSize(Context, Ty);
273 unsigned FieldAlign = calculateLegacyCbufferFieldAlign(Context, Ty);
274
275 // If the field crosses the row boundary after alignment it drops to the
276 // next row
277 unsigned AlignSize = llvm::alignTo(Size, FieldAlign);
278 if ((AlignSize % CBufferAlign) + FieldSize > CBufferAlign) {
279 FieldAlign = CBufferAlign;
280 }
281
282 Size = llvm::alignTo(Size, FieldAlign);
283 Size += FieldSize;
284 }
285 return Size;
286 }
287
288 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(T)) {
289 unsigned ElementCount = AT->getSize().getZExtValue();
290 if (ElementCount == 0)
291 return 0;
292
293 unsigned ElementSize =
294 calculateLegacyCbufferSize(Context, AT->getElementType());
295 unsigned AlignedElementSize = llvm::alignTo(ElementSize, CBufferAlign);
296 return AlignedElementSize * (ElementCount - 1) + ElementSize;
297 }
298
299 if (const VectorType *VT = T->getAs<VectorType>()) {
300 unsigned ElementCount = VT->getNumElements();
301 unsigned ElementSize =
302 calculateLegacyCbufferSize(Context, VT->getElementType());
303 return ElementSize * ElementCount;
304 }
305
306 return Context.getTypeSize(T) / 8;
307}
308
309// Validate packoffset:
310// - if packoffset it used it must be set on all declarations inside the buffer
311// - packoffset ranges must not overlap
312static void validatePackoffset(Sema &S, HLSLBufferDecl *BufDecl) {
314
315 // Make sure the packoffset annotations are either on all declarations
316 // or on none.
317 bool HasPackOffset = false;
318 bool HasNonPackOffset = false;
319 for (auto *Field : BufDecl->buffer_decls()) {
320 VarDecl *Var = dyn_cast<VarDecl>(Field);
321 if (!Var)
322 continue;
323 if (Field->hasAttr<HLSLPackOffsetAttr>()) {
324 PackOffsetVec.emplace_back(Var, Field->getAttr<HLSLPackOffsetAttr>());
325 HasPackOffset = true;
326 } else {
327 HasNonPackOffset = true;
328 }
329 }
330
331 if (!HasPackOffset)
332 return;
333
334 if (HasNonPackOffset)
335 S.Diag(BufDecl->getLocation(), diag::warn_hlsl_packoffset_mix);
336
337 // Make sure there is no overlap in packoffset - sort PackOffsetVec by offset
338 // and compare adjacent values.
339 bool IsValid = true;
340 ASTContext &Context = S.getASTContext();
341 std::sort(PackOffsetVec.begin(), PackOffsetVec.end(),
342 [](const std::pair<VarDecl *, HLSLPackOffsetAttr *> &LHS,
343 const std::pair<VarDecl *, HLSLPackOffsetAttr *> &RHS) {
344 return LHS.second->getOffsetInBytes() <
345 RHS.second->getOffsetInBytes();
346 });
347 for (unsigned i = 0; i < PackOffsetVec.size() - 1; i++) {
348 VarDecl *Var = PackOffsetVec[i].first;
349 HLSLPackOffsetAttr *Attr = PackOffsetVec[i].second;
350 unsigned Size = calculateLegacyCbufferSize(Context, Var->getType());
351 unsigned Begin = Attr->getOffsetInBytes();
352 unsigned End = Begin + Size;
353 unsigned NextBegin = PackOffsetVec[i + 1].second->getOffsetInBytes();
354 if (End > NextBegin) {
355 VarDecl *NextVar = PackOffsetVec[i + 1].first;
356 S.Diag(NextVar->getLocation(), diag::err_hlsl_packoffset_overlap)
357 << NextVar << Var;
358 IsValid = false;
359 }
360 }
361 BufDecl->setHasValidPackoffset(IsValid);
362}
363
364// Returns true if the array has a zero size = if any of the dimensions is 0
365static bool isZeroSizedArray(const ConstantArrayType *CAT) {
366 while (CAT && !CAT->isZeroSize())
367 CAT = dyn_cast<ConstantArrayType>(
369 return CAT != nullptr;
370}
371
375
379
380static const HLSLAttributedResourceType *
382 assert(QT->isHLSLResourceRecordArray() &&
383 "expected array of resource records");
384 const Type *Ty = QT->getUnqualifiedDesugaredType();
385 while (const ArrayType *AT = dyn_cast<ArrayType>(Ty))
387 return HLSLAttributedResourceType::findHandleTypeOnResource(Ty);
388}
389
390static const HLSLAttributedResourceType *
394
395// Returns true if the type is a leaf element type that is not valid to be
396// included in HLSL Buffer, such as a resource class, empty struct, zero-sized
397// array, or a builtin intangible type. Returns false it is a valid leaf element
398// type or if it is a record type that needs to be inspected further.
402 return true;
403 if (const auto *RD = Ty->getAsCXXRecordDecl())
404 return RD->isEmpty();
405 if (Ty->isConstantArrayType() &&
407 return true;
409 return true;
410 return false;
411}
412
413// Returns true if the struct contains at least one element that prevents it
414// from being included inside HLSL Buffer as is, such as an intangible type,
415// empty struct, or zero-sized array. If it does, a new implicit layout struct
416// needs to be created for HLSL Buffer use that will exclude these unwanted
417// declarations (see createHostLayoutStruct function).
419 if (RD->isHLSLIntangible() || RD->isEmpty())
420 return true;
421 // check fields
422 for (const FieldDecl *Field : RD->fields()) {
423 QualType Ty = Field->getType();
425 return true;
426 if (const auto *RD = Ty->getAsCXXRecordDecl();
428 return true;
429 }
430 // check bases
431 for (const CXXBaseSpecifier &Base : RD->bases())
433 Base.getType()->castAsCXXRecordDecl()))
434 return true;
435 return false;
436}
437
439 DeclContext *DC) {
440 CXXRecordDecl *RD = nullptr;
441 for (NamedDecl *Decl :
443 if (CXXRecordDecl *FoundRD = dyn_cast<CXXRecordDecl>(Decl)) {
444 assert(RD == nullptr &&
445 "there should be at most 1 record by a given name in a scope");
446 RD = FoundRD;
447 }
448 }
449 return RD;
450}
451
452// Creates a name for buffer layout struct using the provide name base.
453// If the name must be unique (not previously defined), a suffix is added
454// until a unique name is found.
456 bool MustBeUnique) {
457 ASTContext &AST = S.getASTContext();
458
459 IdentifierInfo *NameBaseII = BaseDecl->getIdentifier();
460 llvm::SmallString<64> Name("__cblayout_");
461 if (NameBaseII) {
462 Name.append(NameBaseII->getName());
463 } else {
464 // anonymous struct
465 Name.append("anon");
466 MustBeUnique = true;
467 }
468
469 size_t NameLength = Name.size();
470 IdentifierInfo *II = &AST.Idents.get(Name, tok::TokenKind::identifier);
471 if (!MustBeUnique)
472 return II;
473
474 unsigned suffix = 0;
475 while (true) {
476 if (suffix != 0) {
477 Name.append("_");
478 Name.append(llvm::Twine(suffix).str());
479 II = &AST.Idents.get(Name, tok::TokenKind::identifier);
480 }
481 if (!findRecordDeclInContext(II, BaseDecl->getDeclContext()))
482 return II;
483 // declaration with that name already exists - increment suffix and try
484 // again until unique name is found
485 suffix++;
486 Name.truncate(NameLength);
487 };
488}
489
490static const Type *createHostLayoutType(Sema &S, const Type *Ty) {
491 ASTContext &AST = S.getASTContext();
492 if (auto *RD = Ty->getAsCXXRecordDecl()) {
494 return Ty;
495 RD = createHostLayoutStruct(S, RD);
496 if (!RD)
497 return nullptr;
498 return AST.getCanonicalTagType(RD)->getTypePtr();
499 }
500
501 if (const auto *CAT = dyn_cast<ConstantArrayType>(Ty)) {
502 const Type *ElementTy = createHostLayoutType(
503 S, CAT->getElementType()->getUnqualifiedDesugaredType());
504 if (!ElementTy)
505 return nullptr;
506 return AST
507 .getConstantArrayType(QualType(ElementTy, 0), CAT->getSize(), nullptr,
508 CAT->getSizeModifier(),
509 CAT->getIndexTypeCVRQualifiers())
510 .getTypePtr();
511 }
512 return Ty;
513}
514
515// Creates a field declaration of given name and type for HLSL buffer layout
516// struct. Returns nullptr if the type cannot be use in HLSL Buffer layout.
518 IdentifierInfo *II,
519 CXXRecordDecl *LayoutStruct) {
521 return nullptr;
522
523 Ty = createHostLayoutType(S, Ty);
524 if (!Ty)
525 return nullptr;
526
527 QualType QT = QualType(Ty, 0);
528 ASTContext &AST = S.getASTContext();
530 auto *Field = FieldDecl::Create(AST, LayoutStruct, SourceLocation(),
531 SourceLocation(), II, QT, TSI, nullptr, false,
533 Field->setAccess(AccessSpecifier::AS_public);
534 return Field;
535}
536
537// Creates host layout struct for a struct included in HLSL Buffer.
538// The layout struct will include only fields that are allowed in HLSL buffer.
539// These fields will be filtered out:
540// - resource classes
541// - empty structs
542// - zero-sized arrays
543// Returns nullptr if the resulting layout struct would be empty.
545 CXXRecordDecl *StructDecl) {
546 assert(requiresImplicitBufferLayoutStructure(StructDecl) &&
547 "struct is already HLSL buffer compatible");
548
549 ASTContext &AST = S.getASTContext();
550 DeclContext *DC = StructDecl->getDeclContext();
551 IdentifierInfo *II = getHostLayoutStructName(S, StructDecl, false);
552
553 // reuse existing if the layout struct if it already exists
554 if (CXXRecordDecl *RD = findRecordDeclInContext(II, DC))
555 return RD;
556
557 CXXRecordDecl *LS =
558 CXXRecordDecl::Create(AST, TagDecl::TagKind::Struct, DC, SourceLocation(),
559 SourceLocation(), II);
560 LS->setImplicit(true);
561 LS->addAttr(PackedAttr::CreateImplicit(AST));
562 LS->startDefinition();
563
564 // copy base struct, create HLSL Buffer compatible version if needed
565 if (unsigned NumBases = StructDecl->getNumBases()) {
566 assert(NumBases == 1 && "HLSL supports only one base type");
567 (void)NumBases;
568 CXXBaseSpecifier Base = *StructDecl->bases_begin();
569 CXXRecordDecl *BaseDecl = Base.getType()->castAsCXXRecordDecl();
571 BaseDecl = createHostLayoutStruct(S, BaseDecl);
572 if (BaseDecl) {
573 TypeSourceInfo *TSI =
575 Base = CXXBaseSpecifier(SourceRange(), false, StructDecl->isClass(),
576 AS_none, TSI, SourceLocation());
577 }
578 }
579 if (BaseDecl) {
580 const CXXBaseSpecifier *BasesArray[1] = {&Base};
581 LS->setBases(BasesArray, 1);
582 }
583 }
584
585 // filter struct fields
586 for (const FieldDecl *FD : StructDecl->fields()) {
587 const Type *Ty = FD->getType()->getUnqualifiedDesugaredType();
588 if (FieldDecl *NewFD =
589 createFieldForHostLayoutStruct(S, Ty, FD->getIdentifier(), LS))
590 LS->addDecl(NewFD);
591 }
592 LS->completeDefinition();
593
594 if (LS->field_empty() && LS->getNumBases() == 0)
595 return nullptr;
596
597 DC->addDecl(LS);
598 return LS;
599}
600
601// Creates host layout struct for HLSL Buffer. The struct will include only
602// fields of types that are allowed in HLSL buffer and it will filter out:
603// - static or groupshared variable declarations
604// - resource classes
605// - empty structs
606// - zero-sized arrays
607// - non-variable declarations
608// The layout struct will be added to the HLSLBufferDecl declarations.
610 ASTContext &AST = S.getASTContext();
611 IdentifierInfo *II = getHostLayoutStructName(S, BufDecl, true);
612
613 CXXRecordDecl *LS =
614 CXXRecordDecl::Create(AST, TagDecl::TagKind::Struct, BufDecl,
616 LS->addAttr(PackedAttr::CreateImplicit(AST));
617 LS->setImplicit(true);
618 LS->startDefinition();
619
620 for (Decl *D : BufDecl->buffer_decls()) {
621 VarDecl *VD = dyn_cast<VarDecl>(D);
622 if (!VD || VD->getStorageClass() == SC_Static ||
624 continue;
625 const Type *Ty = VD->getType()->getUnqualifiedDesugaredType();
626
627 FieldDecl *FD =
629 // Declarations collected for the default $Globals constant buffer have
630 // already been checked to have non-empty cbuffer layout, so
631 // createFieldForHostLayoutStruct should always succeed. These declarations
632 // already have their address space set to hlsl_constant.
633 // For declarations in a named cbuffer block
634 // createFieldForHostLayoutStruct can still return nullptr if the type
635 // is empty (does not have a cbuffer layout).
636 assert((FD || VD->getType().getAddressSpace() != LangAS::hlsl_constant) &&
637 "host layout field for $Globals decl failed to be created");
638 if (FD) {
639 // Add the field decl to the layout struct.
640 LS->addDecl(FD);
642 // Update address space of the original decl to hlsl_constant.
643 QualType NewTy =
645 VD->setType(NewTy);
646 }
647 }
648 }
649 LS->completeDefinition();
650 BufDecl->addLayoutStruct(LS);
651}
652
654 uint32_t ImplicitBindingOrderID) {
655 auto *Attr =
656 HLSLResourceBindingAttr::CreateImplicit(S.getASTContext(), "", "0", {});
657 Attr->setBinding(RT, std::nullopt, 0);
658 Attr->setImplicitBindingOrderID(ImplicitBindingOrderID);
659 D->addAttr(Attr);
660}
661
662// Handle end of cbuffer/tbuffer declaration
664 auto *BufDecl = cast<HLSLBufferDecl>(Dcl);
665 BufDecl->setRBraceLoc(RBrace);
666
667 validatePackoffset(SemaRef, BufDecl);
668
670
671 // Handle implicit binding if needed.
672 ResourceBindingAttrs ResourceAttrs(Dcl);
673 if (!ResourceAttrs.isExplicit()) {
674 SemaRef.Diag(Dcl->getLocation(), diag::warn_hlsl_implicit_binding);
675 // Use HLSLResourceBindingAttr to transfer implicit binding order_ID
676 // to codegen. If it does not exist, create an implicit attribute.
677 uint32_t OrderID = getNextImplicitBindingOrderID();
678 if (ResourceAttrs.hasBinding())
679 ResourceAttrs.setImplicitOrderID(OrderID);
680 else
682 BufDecl->isCBuffer() ? RegisterType::CBuffer
683 : RegisterType::SRV,
684 OrderID);
685 }
686
687 SemaRef.PopDeclContext();
688}
689
690HLSLNumThreadsAttr *SemaHLSL::mergeNumThreadsAttr(Decl *D,
691 const AttributeCommonInfo &AL,
692 int X, int Y, int Z) {
693 if (HLSLNumThreadsAttr *NT = D->getAttr<HLSLNumThreadsAttr>()) {
694 if (NT->getX() != X || NT->getY() != Y || NT->getZ() != Z) {
695 Diag(NT->getLocation(), diag::err_hlsl_attribute_param_mismatch) << AL;
696 Diag(AL.getLoc(), diag::note_conflicting_attribute);
697 }
698 return nullptr;
699 }
700 return ::new (getASTContext())
701 HLSLNumThreadsAttr(getASTContext(), AL, X, Y, Z);
702}
703
705 const AttributeCommonInfo &AL,
706 int Min, int Max, int Preferred,
707 int SpelledArgsCount) {
708 if (HLSLWaveSizeAttr *WS = D->getAttr<HLSLWaveSizeAttr>()) {
709 if (WS->getMin() != Min || WS->getMax() != Max ||
710 WS->getPreferred() != Preferred ||
711 WS->getSpelledArgsCount() != SpelledArgsCount) {
712 Diag(WS->getLocation(), diag::err_hlsl_attribute_param_mismatch) << AL;
713 Diag(AL.getLoc(), diag::note_conflicting_attribute);
714 }
715 return nullptr;
716 }
717 HLSLWaveSizeAttr *Result = ::new (getASTContext())
718 HLSLWaveSizeAttr(getASTContext(), AL, Min, Max, Preferred);
719 Result->setSpelledArgsCount(SpelledArgsCount);
720 return Result;
721}
722
723HLSLVkConstantIdAttr *
725 int Id) {
726
728 if (TargetInfo.getTriple().getArch() != llvm::Triple::spirv) {
729 Diag(AL.getLoc(), diag::warn_attribute_ignored) << AL;
730 return nullptr;
731 }
732
733 auto *VD = cast<VarDecl>(D);
734
735 if (getSpecConstBuiltinId(VD->getType()->getUnqualifiedDesugaredType()) ==
737 Diag(VD->getLocation(), diag::err_specialization_const);
738 return nullptr;
739 }
740
741 if (!VD->getType().isConstQualified()) {
742 Diag(VD->getLocation(), diag::err_specialization_const);
743 return nullptr;
744 }
745
746 if (HLSLVkConstantIdAttr *CI = D->getAttr<HLSLVkConstantIdAttr>()) {
747 if (CI->getId() != Id) {
748 Diag(CI->getLocation(), diag::err_hlsl_attribute_param_mismatch) << AL;
749 Diag(AL.getLoc(), diag::note_conflicting_attribute);
750 }
751 return nullptr;
752 }
753
754 HLSLVkConstantIdAttr *Result =
755 ::new (getASTContext()) HLSLVkConstantIdAttr(getASTContext(), AL, Id);
756 return Result;
757}
758
759HLSLShaderAttr *
761 llvm::Triple::EnvironmentType ShaderType) {
762 if (HLSLShaderAttr *NT = D->getAttr<HLSLShaderAttr>()) {
763 if (NT->getType() != ShaderType) {
764 Diag(NT->getLocation(), diag::err_hlsl_attribute_param_mismatch) << AL;
765 Diag(AL.getLoc(), diag::note_conflicting_attribute);
766 }
767 return nullptr;
768 }
769 return HLSLShaderAttr::Create(getASTContext(), ShaderType, AL);
770}
771
772HLSLParamModifierAttr *
774 HLSLParamModifierAttr::Spelling Spelling) {
775 // We can only merge an `in` attribute with an `out` attribute. All other
776 // combinations of duplicated attributes are ill-formed.
777 if (HLSLParamModifierAttr *PA = D->getAttr<HLSLParamModifierAttr>()) {
778 if ((PA->isIn() && Spelling == HLSLParamModifierAttr::Keyword_out) ||
779 (PA->isOut() && Spelling == HLSLParamModifierAttr::Keyword_in)) {
780 D->dropAttr<HLSLParamModifierAttr>();
781 SourceRange AdjustedRange = {PA->getLocation(), AL.getRange().getEnd()};
782 return HLSLParamModifierAttr::Create(
783 getASTContext(), /*MergedSpelling=*/true, AdjustedRange,
784 HLSLParamModifierAttr::Keyword_inout);
785 }
786 Diag(AL.getLoc(), diag::err_hlsl_duplicate_parameter_modifier) << AL;
787 Diag(PA->getLocation(), diag::note_conflicting_attribute);
788 return nullptr;
789 }
790 return HLSLParamModifierAttr::Create(getASTContext(), AL);
791}
792
795
797 return;
798
799 // If we have specified a root signature to override the entry function then
800 // attach it now
801 HLSLRootSignatureDecl *SignatureDecl =
803 if (SignatureDecl) {
804 FD->dropAttr<RootSignatureAttr>();
805 // We could look up the SourceRange of the macro here as well
806 AttributeCommonInfo AL(RootSigOverrideIdent, AttributeScopeInfo(),
807 SourceRange(), ParsedAttr::Form::Microsoft());
808 FD->addAttr(::new (getASTContext()) RootSignatureAttr(
809 getASTContext(), AL, RootSigOverrideIdent, SignatureDecl));
810 }
811
812 llvm::Triple::EnvironmentType Env = TargetInfo.getTriple().getEnvironment();
813 if (HLSLShaderAttr::isValidShaderType(Env) && Env != llvm::Triple::Library) {
814 if (const auto *Shader = FD->getAttr<HLSLShaderAttr>()) {
815 // The entry point is already annotated - check that it matches the
816 // triple.
817 if (Shader->getType() != Env) {
818 Diag(Shader->getLocation(), diag::err_hlsl_entry_shader_attr_mismatch)
819 << Shader;
820 FD->setInvalidDecl();
821 }
822 } else {
823 // Implicitly add the shader attribute if the entry function isn't
824 // explicitly annotated.
825 FD->addAttr(HLSLShaderAttr::CreateImplicit(getASTContext(), Env,
826 FD->getBeginLoc()));
827 }
828 } else {
829 switch (Env) {
830 case llvm::Triple::UnknownEnvironment:
831 case llvm::Triple::Library:
832 break;
833 case llvm::Triple::RootSignature:
834 llvm_unreachable("rootsig environment has no functions");
835 default:
836 llvm_unreachable("Unhandled environment in triple");
837 }
838 }
839}
840
841static bool isVkPipelineBuiltin(const ASTContext &AstContext, FunctionDecl *FD,
842 HLSLAppliedSemanticAttr *Semantic,
843 bool IsInput) {
844 if (AstContext.getTargetInfo().getTriple().getOS() != llvm::Triple::Vulkan)
845 return false;
846
847 const auto *ShaderAttr = FD->getAttr<HLSLShaderAttr>();
848 assert(ShaderAttr && "Entry point has no shader attribute");
849 llvm::Triple::EnvironmentType ST = ShaderAttr->getType();
850 auto SemanticName = Semantic->getSemanticName().upper();
851
852 // The SV_Position semantic is lowered to:
853 // - Position built-in for vertex output.
854 // - FragCoord built-in for fragment input.
855 if (SemanticName == "SV_POSITION") {
856 return (ST == llvm::Triple::Vertex && !IsInput) ||
857 (ST == llvm::Triple::Pixel && IsInput);
858 }
859 if (SemanticName == "SV_VERTEXID")
860 return true;
861
862 return false;
863}
864
865bool SemaHLSL::determineActiveSemanticOnScalar(FunctionDecl *FD,
866 DeclaratorDecl *OutputDecl,
868 SemanticInfo &ActiveSemantic,
869 SemaHLSL::SemanticContext &SC) {
870 if (ActiveSemantic.Semantic == nullptr) {
871 ActiveSemantic.Semantic = D->getAttr<HLSLParsedSemanticAttr>();
872 if (ActiveSemantic.Semantic)
873 ActiveSemantic.Index = ActiveSemantic.Semantic->getSemanticIndex();
874 }
875
876 if (!ActiveSemantic.Semantic) {
877 Diag(D->getLocation(), diag::err_hlsl_missing_semantic_annotation);
878 return false;
879 }
880
881 auto *A = ::new (getASTContext())
882 HLSLAppliedSemanticAttr(getASTContext(), *ActiveSemantic.Semantic,
883 ActiveSemantic.Semantic->getAttrName()->getName(),
884 ActiveSemantic.Index.value_or(0));
885 if (!A)
887
888 checkSemanticAnnotation(FD, D, A, SC);
889 OutputDecl->addAttr(A);
890
891 unsigned Location = ActiveSemantic.Index.value_or(0);
892
894 SC.CurrentIOType & IOType::In)) {
895 bool HasVkLocation = false;
896 if (auto *A = D->getAttr<HLSLVkLocationAttr>()) {
897 HasVkLocation = true;
898 Location = A->getLocation();
899 }
900
901 if (SC.UsesExplicitVkLocations.value_or(HasVkLocation) != HasVkLocation) {
902 Diag(D->getLocation(), diag::err_hlsl_semantic_partial_explicit_indexing);
903 return false;
904 }
905 SC.UsesExplicitVkLocations = HasVkLocation;
906 }
907
908 const ConstantArrayType *AT = dyn_cast<ConstantArrayType>(D->getType());
909 unsigned ElementCount = AT ? AT->getZExtSize() : 1;
910 ActiveSemantic.Index = Location + ElementCount;
911
912 Twine BaseName = Twine(ActiveSemantic.Semantic->getAttrName()->getName());
913 for (unsigned I = 0; I < ElementCount; ++I) {
914 Twine VariableName = BaseName.concat(Twine(Location + I));
915
916 auto [_, Inserted] = SC.ActiveSemantics.insert(VariableName.str());
917 if (!Inserted) {
918 Diag(D->getLocation(), diag::err_hlsl_semantic_index_overlap)
919 << VariableName.str();
920 return false;
921 }
922 }
923
924 return true;
925}
926
927bool SemaHLSL::determineActiveSemantic(FunctionDecl *FD,
928 DeclaratorDecl *OutputDecl,
930 SemanticInfo &ActiveSemantic,
931 SemaHLSL::SemanticContext &SC) {
932 if (ActiveSemantic.Semantic == nullptr) {
933 ActiveSemantic.Semantic = D->getAttr<HLSLParsedSemanticAttr>();
934 if (ActiveSemantic.Semantic)
935 ActiveSemantic.Index = ActiveSemantic.Semantic->getSemanticIndex();
936 }
937
938 const Type *T = D == FD ? &*FD->getReturnType() : &*D->getType();
940
941 const RecordType *RT = dyn_cast<RecordType>(T);
942 if (!RT)
943 return determineActiveSemanticOnScalar(FD, OutputDecl, D, ActiveSemantic,
944 SC);
945
946 const RecordDecl *RD = RT->getDecl();
947 for (FieldDecl *Field : RD->fields()) {
948 SemanticInfo Info = ActiveSemantic;
949 if (!determineActiveSemantic(FD, OutputDecl, Field, Info, SC)) {
950 Diag(Field->getLocation(), diag::note_hlsl_semantic_used_here) << Field;
951 return false;
952 }
953 if (ActiveSemantic.Semantic)
954 ActiveSemantic = Info;
955 }
956
957 return true;
958}
959
961 const auto *ShaderAttr = FD->getAttr<HLSLShaderAttr>();
962 assert(ShaderAttr && "Entry point has no shader attribute");
963 llvm::Triple::EnvironmentType ST = ShaderAttr->getType();
965 VersionTuple Ver = TargetInfo.getTriple().getOSVersion();
966 switch (ST) {
967 case llvm::Triple::Pixel:
968 case llvm::Triple::Vertex:
969 case llvm::Triple::Geometry:
970 case llvm::Triple::Hull:
971 case llvm::Triple::Domain:
972 case llvm::Triple::RayGeneration:
973 case llvm::Triple::Intersection:
974 case llvm::Triple::AnyHit:
975 case llvm::Triple::ClosestHit:
976 case llvm::Triple::Miss:
977 case llvm::Triple::Callable:
978 if (const auto *NT = FD->getAttr<HLSLNumThreadsAttr>()) {
979 diagnoseAttrStageMismatch(NT, ST,
980 {llvm::Triple::Compute,
981 llvm::Triple::Amplification,
982 llvm::Triple::Mesh});
983 FD->setInvalidDecl();
984 }
985 if (const auto *WS = FD->getAttr<HLSLWaveSizeAttr>()) {
986 diagnoseAttrStageMismatch(WS, ST,
987 {llvm::Triple::Compute,
988 llvm::Triple::Amplification,
989 llvm::Triple::Mesh});
990 FD->setInvalidDecl();
991 }
992 break;
993
994 case llvm::Triple::Compute:
995 case llvm::Triple::Amplification:
996 case llvm::Triple::Mesh:
997 if (!FD->hasAttr<HLSLNumThreadsAttr>()) {
998 Diag(FD->getLocation(), diag::err_hlsl_missing_numthreads)
999 << llvm::Triple::getEnvironmentTypeName(ST);
1000 FD->setInvalidDecl();
1001 }
1002 if (const auto *WS = FD->getAttr<HLSLWaveSizeAttr>()) {
1003 if (Ver < VersionTuple(6, 6)) {
1004 Diag(WS->getLocation(), diag::err_hlsl_attribute_in_wrong_shader_model)
1005 << WS << "6.6";
1006 FD->setInvalidDecl();
1007 } else if (WS->getSpelledArgsCount() > 1 && Ver < VersionTuple(6, 8)) {
1008 Diag(
1009 WS->getLocation(),
1010 diag::err_hlsl_attribute_number_arguments_insufficient_shader_model)
1011 << WS << WS->getSpelledArgsCount() << "6.8";
1012 FD->setInvalidDecl();
1013 }
1014 }
1015 break;
1016 case llvm::Triple::RootSignature:
1017 llvm_unreachable("rootsig environment has no function entry point");
1018 default:
1019 llvm_unreachable("Unhandled environment in triple");
1020 }
1021
1022 SemaHLSL::SemanticContext InputSC = {};
1023 InputSC.CurrentIOType = IOType::In;
1024
1025 for (ParmVarDecl *Param : FD->parameters()) {
1026 SemanticInfo ActiveSemantic;
1027 ActiveSemantic.Semantic = Param->getAttr<HLSLParsedSemanticAttr>();
1028 if (ActiveSemantic.Semantic)
1029 ActiveSemantic.Index = ActiveSemantic.Semantic->getSemanticIndex();
1030
1031 // FIXME: Verify output semantics in parameters.
1032 if (!determineActiveSemantic(FD, Param, Param, ActiveSemantic, InputSC)) {
1033 Diag(Param->getLocation(), diag::note_previous_decl) << Param;
1034 FD->setInvalidDecl();
1035 }
1036 }
1037
1038 SemanticInfo ActiveSemantic;
1039 SemaHLSL::SemanticContext OutputSC = {};
1040 OutputSC.CurrentIOType = IOType::Out;
1041 ActiveSemantic.Semantic = FD->getAttr<HLSLParsedSemanticAttr>();
1042 if (ActiveSemantic.Semantic)
1043 ActiveSemantic.Index = ActiveSemantic.Semantic->getSemanticIndex();
1044 if (!FD->getReturnType()->isVoidType())
1045 determineActiveSemantic(FD, FD, FD, ActiveSemantic, OutputSC);
1046}
1047
1048void SemaHLSL::checkSemanticAnnotation(
1049 FunctionDecl *EntryPoint, const Decl *Param,
1050 const HLSLAppliedSemanticAttr *SemanticAttr, const SemanticContext &SC) {
1051 auto *ShaderAttr = EntryPoint->getAttr<HLSLShaderAttr>();
1052 assert(ShaderAttr && "Entry point has no shader attribute");
1053 llvm::Triple::EnvironmentType ST = ShaderAttr->getType();
1054
1055 auto SemanticName = SemanticAttr->getSemanticName().upper();
1056 if (SemanticName == "SV_DISPATCHTHREADID" ||
1057 SemanticName == "SV_GROUPINDEX" || SemanticName == "SV_GROUPTHREADID" ||
1058 SemanticName == "SV_GROUPID") {
1059
1060 if (ST != llvm::Triple::Compute)
1061 diagnoseSemanticStageMismatch(SemanticAttr, ST, SC.CurrentIOType,
1062 {{llvm::Triple::Compute, IOType::In}});
1063
1064 if (SemanticAttr->getSemanticIndex() != 0) {
1065 std::string PrettyName =
1066 "'" + SemanticAttr->getSemanticName().str() + "'";
1067 Diag(SemanticAttr->getLoc(),
1068 diag::err_hlsl_semantic_indexing_not_supported)
1069 << PrettyName;
1070 }
1071 return;
1072 }
1073
1074 if (SemanticName == "SV_POSITION") {
1075 // SV_Position can be an input or output in vertex shaders,
1076 // but only an input in pixel shaders.
1077 diagnoseSemanticStageMismatch(SemanticAttr, ST, SC.CurrentIOType,
1078 {{llvm::Triple::Vertex, IOType::InOut},
1079 {llvm::Triple::Pixel, IOType::In}});
1080 return;
1081 }
1082 if (SemanticName == "SV_VERTEXID") {
1083 diagnoseSemanticStageMismatch(SemanticAttr, ST, SC.CurrentIOType,
1084 {{llvm::Triple::Vertex, IOType::In}});
1085 return;
1086 }
1087
1088 if (SemanticName == "SV_TARGET") {
1089 diagnoseSemanticStageMismatch(SemanticAttr, ST, SC.CurrentIOType,
1090 {{llvm::Triple::Pixel, IOType::Out}});
1091 return;
1092 }
1093
1094 // FIXME: catch-all for non-implemented system semantics reaching this
1095 // location.
1096 if (SemanticAttr->getAttrName()->getName().starts_with_insensitive("SV_"))
1097 llvm_unreachable("Unknown SemanticAttr");
1098}
1099
1100void SemaHLSL::diagnoseAttrStageMismatch(
1101 const Attr *A, llvm::Triple::EnvironmentType Stage,
1102 std::initializer_list<llvm::Triple::EnvironmentType> AllowedStages) {
1103 SmallVector<StringRef, 8> StageStrings;
1104 llvm::transform(AllowedStages, std::back_inserter(StageStrings),
1105 [](llvm::Triple::EnvironmentType ST) {
1106 return StringRef(
1107 HLSLShaderAttr::ConvertEnvironmentTypeToStr(ST));
1108 });
1109 Diag(A->getLoc(), diag::err_hlsl_attr_unsupported_in_stage)
1110 << A->getAttrName() << llvm::Triple::getEnvironmentTypeName(Stage)
1111 << (AllowedStages.size() != 1) << join(StageStrings, ", ");
1112}
1113
1114void SemaHLSL::diagnoseSemanticStageMismatch(
1115 const Attr *A, llvm::Triple::EnvironmentType Stage, IOType CurrentIOType,
1116 std::initializer_list<SemanticStageInfo> Allowed) {
1117
1118 for (auto &Case : Allowed) {
1119 if (Case.Stage != Stage)
1120 continue;
1121
1122 if (CurrentIOType & Case.AllowedIOTypesMask)
1123 return;
1124
1125 SmallVector<std::string, 8> ValidCases;
1126 llvm::transform(
1127 Allowed, std::back_inserter(ValidCases), [](SemanticStageInfo Case) {
1128 SmallVector<std::string, 2> ValidType;
1129 if (Case.AllowedIOTypesMask & IOType::In)
1130 ValidType.push_back("input");
1131 if (Case.AllowedIOTypesMask & IOType::Out)
1132 ValidType.push_back("output");
1133 return std::string(
1134 HLSLShaderAttr::ConvertEnvironmentTypeToStr(Case.Stage)) +
1135 " " + join(ValidType, "/");
1136 });
1137 Diag(A->getLoc(), diag::err_hlsl_semantic_unsupported_iotype_for_stage)
1138 << A->getAttrName() << (CurrentIOType & IOType::In ? "input" : "output")
1139 << llvm::Triple::getEnvironmentTypeName(Case.Stage)
1140 << join(ValidCases, ", ");
1141 return;
1142 }
1143
1144 SmallVector<StringRef, 8> StageStrings;
1145 llvm::transform(
1146 Allowed, std::back_inserter(StageStrings), [](SemanticStageInfo Case) {
1147 return StringRef(
1148 HLSLShaderAttr::ConvertEnvironmentTypeToStr(Case.Stage));
1149 });
1150
1151 Diag(A->getLoc(), diag::err_hlsl_attr_unsupported_in_stage)
1152 << A->getAttrName() << llvm::Triple::getEnvironmentTypeName(Stage)
1153 << (Allowed.size() != 1) << join(StageStrings, ", ");
1154}
1155
1156template <CastKind Kind>
1157static void castVector(Sema &S, ExprResult &E, QualType &Ty, unsigned Sz) {
1158 if (const auto *VTy = Ty->getAs<VectorType>())
1159 Ty = VTy->getElementType();
1160 Ty = S.getASTContext().getExtVectorType(Ty, Sz);
1161 E = S.ImpCastExprToType(E.get(), Ty, Kind);
1162}
1163
1164template <CastKind Kind>
1166 E = S.ImpCastExprToType(E.get(), Ty, Kind);
1167 return Ty;
1168}
1169
1171 Sema &SemaRef, ExprResult &LHS, ExprResult &RHS, QualType LHSType,
1172 QualType RHSType, QualType LElTy, QualType RElTy, bool IsCompAssign) {
1173 bool LHSFloat = LElTy->isRealFloatingType();
1174 bool RHSFloat = RElTy->isRealFloatingType();
1175
1176 if (LHSFloat && RHSFloat) {
1177 if (IsCompAssign ||
1178 SemaRef.getASTContext().getFloatingTypeOrder(LElTy, RElTy) > 0)
1179 return castElement<CK_FloatingCast>(SemaRef, RHS, LHSType);
1180
1181 return castElement<CK_FloatingCast>(SemaRef, LHS, RHSType);
1182 }
1183
1184 if (LHSFloat)
1185 return castElement<CK_IntegralToFloating>(SemaRef, RHS, LHSType);
1186
1187 assert(RHSFloat);
1188 if (IsCompAssign)
1189 return castElement<clang::CK_FloatingToIntegral>(SemaRef, RHS, LHSType);
1190
1191 return castElement<CK_IntegralToFloating>(SemaRef, LHS, RHSType);
1192}
1193
1195 Sema &SemaRef, ExprResult &LHS, ExprResult &RHS, QualType LHSType,
1196 QualType RHSType, QualType LElTy, QualType RElTy, bool IsCompAssign) {
1197
1198 int IntOrder = SemaRef.Context.getIntegerTypeOrder(LElTy, RElTy);
1199 bool LHSSigned = LElTy->hasSignedIntegerRepresentation();
1200 bool RHSSigned = RElTy->hasSignedIntegerRepresentation();
1201 auto &Ctx = SemaRef.getASTContext();
1202
1203 // If both types have the same signedness, use the higher ranked type.
1204 if (LHSSigned == RHSSigned) {
1205 if (IsCompAssign || IntOrder >= 0)
1206 return castElement<CK_IntegralCast>(SemaRef, RHS, LHSType);
1207
1208 return castElement<CK_IntegralCast>(SemaRef, LHS, RHSType);
1209 }
1210
1211 // If the unsigned type has greater than or equal rank of the signed type, use
1212 // the unsigned type.
1213 if (IntOrder != (LHSSigned ? 1 : -1)) {
1214 if (IsCompAssign || RHSSigned)
1215 return castElement<CK_IntegralCast>(SemaRef, RHS, LHSType);
1216 return castElement<CK_IntegralCast>(SemaRef, LHS, RHSType);
1217 }
1218
1219 // At this point the signed type has higher rank than the unsigned type, which
1220 // means it will be the same size or bigger. If the signed type is bigger, it
1221 // can represent all the values of the unsigned type, so select it.
1222 if (Ctx.getIntWidth(LElTy) != Ctx.getIntWidth(RElTy)) {
1223 if (IsCompAssign || LHSSigned)
1224 return castElement<CK_IntegralCast>(SemaRef, RHS, LHSType);
1225 return castElement<CK_IntegralCast>(SemaRef, LHS, RHSType);
1226 }
1227
1228 // This is a bit of an odd duck case in HLSL. It shouldn't happen, but can due
1229 // to C/C++ leaking through. The place this happens today is long vs long
1230 // long. When arguments are vector<unsigned long, N> and vector<long long, N>,
1231 // the long long has higher rank than long even though they are the same size.
1232
1233 // If this is a compound assignment cast the right hand side to the left hand
1234 // side's type.
1235 if (IsCompAssign)
1236 return castElement<CK_IntegralCast>(SemaRef, RHS, LHSType);
1237
1238 // If this isn't a compound assignment we convert to unsigned long long.
1239 QualType ElTy = Ctx.getCorrespondingUnsignedType(LHSSigned ? LElTy : RElTy);
1240 QualType NewTy = Ctx.getExtVectorType(
1241 ElTy, RHSType->castAs<VectorType>()->getNumElements());
1242 (void)castElement<CK_IntegralCast>(SemaRef, RHS, NewTy);
1243
1244 return castElement<CK_IntegralCast>(SemaRef, LHS, NewTy);
1245}
1246
1248 QualType SrcTy) {
1249 if (DestTy->isRealFloatingType() && SrcTy->isRealFloatingType())
1250 return CK_FloatingCast;
1251 if (DestTy->isIntegralType(Ctx) && SrcTy->isIntegralType(Ctx))
1252 return CK_IntegralCast;
1253 if (DestTy->isRealFloatingType())
1254 return CK_IntegralToFloating;
1255 assert(SrcTy->isRealFloatingType() && DestTy->isIntegralType(Ctx));
1256 return CK_FloatingToIntegral;
1257}
1258
1260 QualType LHSType,
1261 QualType RHSType,
1262 bool IsCompAssign) {
1263 const auto *LVecTy = LHSType->getAs<VectorType>();
1264 const auto *RVecTy = RHSType->getAs<VectorType>();
1265 auto &Ctx = getASTContext();
1266
1267 // If the LHS is not a vector and this is a compound assignment, we truncate
1268 // the argument to a scalar then convert it to the LHS's type.
1269 if (!LVecTy && IsCompAssign) {
1270 QualType RElTy = RHSType->castAs<VectorType>()->getElementType();
1271 RHS = SemaRef.ImpCastExprToType(RHS.get(), RElTy, CK_HLSLVectorTruncation);
1272 RHSType = RHS.get()->getType();
1273 if (Ctx.hasSameUnqualifiedType(LHSType, RHSType))
1274 return LHSType;
1275 RHS = SemaRef.ImpCastExprToType(RHS.get(), LHSType,
1276 getScalarCastKind(Ctx, LHSType, RHSType));
1277 return LHSType;
1278 }
1279
1280 unsigned EndSz = std::numeric_limits<unsigned>::max();
1281 unsigned LSz = 0;
1282 if (LVecTy)
1283 LSz = EndSz = LVecTy->getNumElements();
1284 if (RVecTy)
1285 EndSz = std::min(RVecTy->getNumElements(), EndSz);
1286 assert(EndSz != std::numeric_limits<unsigned>::max() &&
1287 "one of the above should have had a value");
1288
1289 // In a compound assignment, the left operand does not change type, the right
1290 // operand is converted to the type of the left operand.
1291 if (IsCompAssign && LSz != EndSz) {
1292 Diag(LHS.get()->getBeginLoc(),
1293 diag::err_hlsl_vector_compound_assignment_truncation)
1294 << LHSType << RHSType;
1295 return QualType();
1296 }
1297
1298 if (RVecTy && RVecTy->getNumElements() > EndSz)
1299 castVector<CK_HLSLVectorTruncation>(SemaRef, RHS, RHSType, EndSz);
1300 if (!IsCompAssign && LVecTy && LVecTy->getNumElements() > EndSz)
1301 castVector<CK_HLSLVectorTruncation>(SemaRef, LHS, LHSType, EndSz);
1302
1303 if (!RVecTy)
1304 castVector<CK_VectorSplat>(SemaRef, RHS, RHSType, EndSz);
1305 if (!IsCompAssign && !LVecTy)
1306 castVector<CK_VectorSplat>(SemaRef, LHS, LHSType, EndSz);
1307
1308 // If we're at the same type after resizing we can stop here.
1309 if (Ctx.hasSameUnqualifiedType(LHSType, RHSType))
1310 return Ctx.getCommonSugaredType(LHSType, RHSType);
1311
1312 QualType LElTy = LHSType->castAs<VectorType>()->getElementType();
1313 QualType RElTy = RHSType->castAs<VectorType>()->getElementType();
1314
1315 // Handle conversion for floating point vectors.
1316 if (LElTy->isRealFloatingType() || RElTy->isRealFloatingType())
1317 return handleFloatVectorBinOpConversion(SemaRef, LHS, RHS, LHSType, RHSType,
1318 LElTy, RElTy, IsCompAssign);
1319
1320 assert(LElTy->isIntegralType(Ctx) && RElTy->isIntegralType(Ctx) &&
1321 "HLSL Vectors can only contain integer or floating point types");
1322 return handleIntegerVectorBinOpConversion(SemaRef, LHS, RHS, LHSType, RHSType,
1323 LElTy, RElTy, IsCompAssign);
1324}
1325
1327 BinaryOperatorKind Opc) {
1328 assert((Opc == BO_LOr || Opc == BO_LAnd) &&
1329 "Called with non-logical operator");
1331 llvm::raw_svector_ostream OS(Buff);
1332 PrintingPolicy PP(SemaRef.getLangOpts());
1333 StringRef NewFnName = Opc == BO_LOr ? "or" : "and";
1334 OS << NewFnName << "(";
1335 LHS->printPretty(OS, nullptr, PP);
1336 OS << ", ";
1337 RHS->printPretty(OS, nullptr, PP);
1338 OS << ")";
1339 SourceRange FullRange = SourceRange(LHS->getBeginLoc(), RHS->getEndLoc());
1340 SemaRef.Diag(LHS->getBeginLoc(), diag::note_function_suggestion)
1341 << NewFnName << FixItHint::CreateReplacement(FullRange, OS.str());
1342}
1343
1344std::pair<IdentifierInfo *, bool>
1346 llvm::hash_code Hash = llvm::hash_value(Signature);
1347 std::string IdStr = "__hlsl_rootsig_decl_" + std::to_string(Hash);
1348 IdentifierInfo *DeclIdent = &(getASTContext().Idents.get(IdStr));
1349
1350 // Check if we have already found a decl of the same name.
1351 LookupResult R(SemaRef, DeclIdent, SourceLocation(),
1353 bool Found = SemaRef.LookupQualifiedName(R, SemaRef.CurContext);
1354 return {DeclIdent, Found};
1355}
1356
1358 SourceLocation Loc, IdentifierInfo *DeclIdent,
1360
1361 if (handleRootSignatureElements(RootElements))
1362 return;
1363
1365 for (auto &RootSigElement : RootElements)
1366 Elements.push_back(RootSigElement.getElement());
1367
1368 auto *SignatureDecl = HLSLRootSignatureDecl::Create(
1369 SemaRef.getASTContext(), /*DeclContext=*/SemaRef.CurContext, Loc,
1370 DeclIdent, SemaRef.getLangOpts().HLSLRootSigVer, Elements);
1371
1372 SignatureDecl->setImplicit();
1373 SemaRef.PushOnScopeChains(SignatureDecl, SemaRef.getCurScope());
1374}
1375
1378 if (RootSigOverrideIdent) {
1379 LookupResult R(SemaRef, RootSigOverrideIdent, SourceLocation(),
1381 if (SemaRef.LookupQualifiedName(R, DC))
1382 return dyn_cast<HLSLRootSignatureDecl>(R.getFoundDecl());
1383 }
1384
1385 return nullptr;
1386}
1387
1388namespace {
1389
1390struct PerVisibilityBindingChecker {
1391 SemaHLSL *S;
1392 // We need one builder per `llvm::dxbc::ShaderVisibility` value.
1393 std::array<llvm::hlsl::BindingInfoBuilder, 8> Builders;
1394
1395 struct ElemInfo {
1396 const hlsl::RootSignatureElement *Elem;
1397 llvm::dxbc::ShaderVisibility Vis;
1398 bool Diagnosed;
1399 };
1400 llvm::SmallVector<ElemInfo> ElemInfoMap;
1401
1402 PerVisibilityBindingChecker(SemaHLSL *S) : S(S) {}
1403
1404 void trackBinding(llvm::dxbc::ShaderVisibility Visibility,
1405 llvm::dxil::ResourceClass RC, uint32_t Space,
1406 uint32_t LowerBound, uint32_t UpperBound,
1407 const hlsl::RootSignatureElement *Elem) {
1408 uint32_t BuilderIndex = llvm::to_underlying(Visibility);
1409 assert(BuilderIndex < Builders.size() &&
1410 "Not enough builders for visibility type");
1411 Builders[BuilderIndex].trackBinding(RC, Space, LowerBound, UpperBound,
1412 static_cast<const void *>(Elem));
1413
1414 static_assert(llvm::to_underlying(llvm::dxbc::ShaderVisibility::All) == 0,
1415 "'All' visibility must come first");
1416 if (Visibility == llvm::dxbc::ShaderVisibility::All)
1417 for (size_t I = 1, E = Builders.size(); I < E; ++I)
1418 Builders[I].trackBinding(RC, Space, LowerBound, UpperBound,
1419 static_cast<const void *>(Elem));
1420
1421 ElemInfoMap.push_back({Elem, Visibility, false});
1422 }
1423
1424 ElemInfo &getInfo(const hlsl::RootSignatureElement *Elem) {
1425 auto It = llvm::lower_bound(
1426 ElemInfoMap, Elem,
1427 [](const auto &LHS, const auto &RHS) { return LHS.Elem < RHS; });
1428 assert(It->Elem == Elem && "Element not in map");
1429 return *It;
1430 }
1431
1432 bool checkOverlap() {
1433 llvm::sort(ElemInfoMap, [](const auto &LHS, const auto &RHS) {
1434 return LHS.Elem < RHS.Elem;
1435 });
1436
1437 bool HadOverlap = false;
1438
1439 using llvm::hlsl::BindingInfoBuilder;
1440 auto ReportOverlap = [this,
1441 &HadOverlap](const BindingInfoBuilder &Builder,
1442 const llvm::hlsl::Binding &Reported) {
1443 HadOverlap = true;
1444
1445 const auto *Elem =
1446 static_cast<const hlsl::RootSignatureElement *>(Reported.Cookie);
1447 const llvm::hlsl::Binding &Previous = Builder.findOverlapping(Reported);
1448 const auto *PrevElem =
1449 static_cast<const hlsl::RootSignatureElement *>(Previous.Cookie);
1450
1451 ElemInfo &Info = getInfo(Elem);
1452 // We will have already diagnosed this binding if there's overlap in the
1453 // "All" visibility as well as any particular visibility.
1454 if (Info.Diagnosed)
1455 return;
1456 Info.Diagnosed = true;
1457
1458 ElemInfo &PrevInfo = getInfo(PrevElem);
1459 llvm::dxbc::ShaderVisibility CommonVis =
1460 Info.Vis == llvm::dxbc::ShaderVisibility::All ? PrevInfo.Vis
1461 : Info.Vis;
1462
1463 this->S->Diag(Elem->getLocation(), diag::err_hlsl_resource_range_overlap)
1464 << llvm::to_underlying(Reported.RC) << Reported.LowerBound
1465 << Reported.isUnbounded() << Reported.UpperBound
1466 << llvm::to_underlying(Previous.RC) << Previous.LowerBound
1467 << Previous.isUnbounded() << Previous.UpperBound << Reported.Space
1468 << CommonVis;
1469
1470 this->S->Diag(PrevElem->getLocation(),
1471 diag::note_hlsl_resource_range_here);
1472 };
1473
1474 for (BindingInfoBuilder &Builder : Builders)
1475 Builder.calculateBindingInfo(ReportOverlap);
1476
1477 return HadOverlap;
1478 }
1479};
1480
1481static CXXMethodDecl *lookupMethod(Sema &S, CXXRecordDecl *RecordDecl,
1482 StringRef Name, SourceLocation Loc) {
1483 DeclarationName DeclName(&S.getASTContext().Idents.get(Name));
1484 LookupResult Result(S, DeclName, Loc, Sema::LookupMemberName);
1485 if (!S.LookupQualifiedName(Result, static_cast<DeclContext *>(RecordDecl)))
1486 return nullptr;
1487 return cast<CXXMethodDecl>(Result.getFoundDecl());
1488}
1489
1490} // end anonymous namespace
1491
1492static bool hasCounterHandle(const CXXRecordDecl *RD) {
1493 if (RD->field_empty())
1494 return false;
1495 auto It = std::next(RD->field_begin());
1496 if (It == RD->field_end())
1497 return false;
1498 const FieldDecl *SecondField = *It;
1499 if (const auto *ResTy =
1500 SecondField->getType()->getAs<HLSLAttributedResourceType>()) {
1501 return ResTy->getAttrs().IsCounter;
1502 }
1503 return false;
1504}
1505
1508 // Define some common error handling functions
1509 bool HadError = false;
1510 auto ReportError = [this, &HadError](SourceLocation Loc, uint32_t LowerBound,
1511 uint32_t UpperBound) {
1512 HadError = true;
1513 this->Diag(Loc, diag::err_hlsl_invalid_rootsig_value)
1514 << LowerBound << UpperBound;
1515 };
1516
1517 auto ReportFloatError = [this, &HadError](SourceLocation Loc,
1518 float LowerBound,
1519 float UpperBound) {
1520 HadError = true;
1521 this->Diag(Loc, diag::err_hlsl_invalid_rootsig_value)
1522 << llvm::formatv("{0:f}", LowerBound).sstr<6>()
1523 << llvm::formatv("{0:f}", UpperBound).sstr<6>();
1524 };
1525
1526 auto VerifyRegister = [ReportError](SourceLocation Loc, uint32_t Register) {
1527 if (!llvm::hlsl::rootsig::verifyRegisterValue(Register))
1528 ReportError(Loc, 0, 0xfffffffe);
1529 };
1530
1531 auto VerifySpace = [ReportError](SourceLocation Loc, uint32_t Space) {
1532 if (!llvm::hlsl::rootsig::verifyRegisterSpace(Space))
1533 ReportError(Loc, 0, 0xffffffef);
1534 };
1535
1536 const uint32_t Version =
1537 llvm::to_underlying(SemaRef.getLangOpts().HLSLRootSigVer);
1538 const uint32_t VersionEnum = Version - 1;
1539 auto ReportFlagError = [this, &HadError, VersionEnum](SourceLocation Loc) {
1540 HadError = true;
1541 this->Diag(Loc, diag::err_hlsl_invalid_rootsig_flag)
1542 << /*version minor*/ VersionEnum;
1543 };
1544
1545 // Iterate through the elements and do basic validations
1546 for (const hlsl::RootSignatureElement &RootSigElem : Elements) {
1547 SourceLocation Loc = RootSigElem.getLocation();
1548 const llvm::hlsl::rootsig::RootElement &Elem = RootSigElem.getElement();
1549 if (const auto *Descriptor =
1550 std::get_if<llvm::hlsl::rootsig::RootDescriptor>(&Elem)) {
1551 VerifyRegister(Loc, Descriptor->Reg.Number);
1552 VerifySpace(Loc, Descriptor->Space);
1553
1554 if (!llvm::hlsl::rootsig::verifyRootDescriptorFlag(Version,
1555 Descriptor->Flags))
1556 ReportFlagError(Loc);
1557 } else if (const auto *Constants =
1558 std::get_if<llvm::hlsl::rootsig::RootConstants>(&Elem)) {
1559 VerifyRegister(Loc, Constants->Reg.Number);
1560 VerifySpace(Loc, Constants->Space);
1561 } else if (const auto *Sampler =
1562 std::get_if<llvm::hlsl::rootsig::StaticSampler>(&Elem)) {
1563 VerifyRegister(Loc, Sampler->Reg.Number);
1564 VerifySpace(Loc, Sampler->Space);
1565
1566 assert(!std::isnan(Sampler->MaxLOD) && !std::isnan(Sampler->MinLOD) &&
1567 "By construction, parseFloatParam can't produce a NaN from a "
1568 "float_literal token");
1569
1570 if (!llvm::hlsl::rootsig::verifyMaxAnisotropy(Sampler->MaxAnisotropy))
1571 ReportError(Loc, 0, 16);
1572 if (!llvm::hlsl::rootsig::verifyMipLODBias(Sampler->MipLODBias))
1573 ReportFloatError(Loc, -16.f, 15.99f);
1574 } else if (const auto *Clause =
1575 std::get_if<llvm::hlsl::rootsig::DescriptorTableClause>(
1576 &Elem)) {
1577 VerifyRegister(Loc, Clause->Reg.Number);
1578 VerifySpace(Loc, Clause->Space);
1579
1580 if (!llvm::hlsl::rootsig::verifyNumDescriptors(Clause->NumDescriptors)) {
1581 // NumDescriptor could techincally be ~0u but that is reserved for
1582 // unbounded, so the diagnostic will not report that as a valid int
1583 // value
1584 ReportError(Loc, 1, 0xfffffffe);
1585 }
1586
1587 if (!llvm::hlsl::rootsig::verifyDescriptorRangeFlag(Version, Clause->Type,
1588 Clause->Flags))
1589 ReportFlagError(Loc);
1590 }
1591 }
1592
1593 PerVisibilityBindingChecker BindingChecker(this);
1594 SmallVector<std::pair<const llvm::hlsl::rootsig::DescriptorTableClause *,
1596 UnboundClauses;
1597
1598 for (const hlsl::RootSignatureElement &RootSigElem : Elements) {
1599 const llvm::hlsl::rootsig::RootElement &Elem = RootSigElem.getElement();
1600 if (const auto *Descriptor =
1601 std::get_if<llvm::hlsl::rootsig::RootDescriptor>(&Elem)) {
1602 uint32_t LowerBound(Descriptor->Reg.Number);
1603 uint32_t UpperBound(LowerBound); // inclusive range
1604
1605 BindingChecker.trackBinding(
1606 Descriptor->Visibility,
1607 static_cast<llvm::dxil::ResourceClass>(Descriptor->Type),
1608 Descriptor->Space, LowerBound, UpperBound, &RootSigElem);
1609 } else if (const auto *Constants =
1610 std::get_if<llvm::hlsl::rootsig::RootConstants>(&Elem)) {
1611 uint32_t LowerBound(Constants->Reg.Number);
1612 uint32_t UpperBound(LowerBound); // inclusive range
1613
1614 BindingChecker.trackBinding(
1615 Constants->Visibility, llvm::dxil::ResourceClass::CBuffer,
1616 Constants->Space, LowerBound, UpperBound, &RootSigElem);
1617 } else if (const auto *Sampler =
1618 std::get_if<llvm::hlsl::rootsig::StaticSampler>(&Elem)) {
1619 uint32_t LowerBound(Sampler->Reg.Number);
1620 uint32_t UpperBound(LowerBound); // inclusive range
1621
1622 BindingChecker.trackBinding(
1623 Sampler->Visibility, llvm::dxil::ResourceClass::Sampler,
1624 Sampler->Space, LowerBound, UpperBound, &RootSigElem);
1625 } else if (const auto *Clause =
1626 std::get_if<llvm::hlsl::rootsig::DescriptorTableClause>(
1627 &Elem)) {
1628 // We'll process these once we see the table element.
1629 UnboundClauses.emplace_back(Clause, &RootSigElem);
1630 } else if (const auto *Table =
1631 std::get_if<llvm::hlsl::rootsig::DescriptorTable>(&Elem)) {
1632 assert(UnboundClauses.size() == Table->NumClauses &&
1633 "Number of unbound elements must match the number of clauses");
1634 bool HasAnySampler = false;
1635 bool HasAnyNonSampler = false;
1636 uint64_t Offset = 0;
1637 bool IsPrevUnbound = false;
1638 for (const auto &[Clause, ClauseElem] : UnboundClauses) {
1639 SourceLocation Loc = ClauseElem->getLocation();
1640 if (Clause->Type == llvm::dxil::ResourceClass::Sampler)
1641 HasAnySampler = true;
1642 else
1643 HasAnyNonSampler = true;
1644
1645 if (HasAnySampler && HasAnyNonSampler)
1646 Diag(Loc, diag::err_hlsl_invalid_mixed_resources);
1647
1648 // Relevant error will have already been reported above and needs to be
1649 // fixed before we can conduct further analysis, so shortcut error
1650 // return
1651 if (Clause->NumDescriptors == 0)
1652 return true;
1653
1654 bool IsAppending =
1655 Clause->Offset == llvm::hlsl::rootsig::DescriptorTableOffsetAppend;
1656 if (!IsAppending)
1657 Offset = Clause->Offset;
1658
1659 uint64_t RangeBound = llvm::hlsl::rootsig::computeRangeBound(
1660 Offset, Clause->NumDescriptors);
1661
1662 if (IsPrevUnbound && IsAppending)
1663 Diag(Loc, diag::err_hlsl_appending_onto_unbound);
1664 else if (!llvm::hlsl::rootsig::verifyNoOverflowedOffset(RangeBound))
1665 Diag(Loc, diag::err_hlsl_offset_overflow) << Offset << RangeBound;
1666
1667 // Update offset to be 1 past this range's bound
1668 Offset = RangeBound + 1;
1669 IsPrevUnbound = Clause->NumDescriptors ==
1670 llvm::hlsl::rootsig::NumDescriptorsUnbounded;
1671
1672 // Compute the register bounds and track resource binding
1673 uint32_t LowerBound(Clause->Reg.Number);
1674 uint32_t UpperBound = llvm::hlsl::rootsig::computeRangeBound(
1675 LowerBound, Clause->NumDescriptors);
1676
1677 BindingChecker.trackBinding(
1678 Table->Visibility,
1679 static_cast<llvm::dxil::ResourceClass>(Clause->Type), Clause->Space,
1680 LowerBound, UpperBound, ClauseElem);
1681 }
1682 UnboundClauses.clear();
1683 }
1684 }
1685
1686 return BindingChecker.checkOverlap();
1687}
1688
1690 if (AL.getNumArgs() != 1) {
1691 Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << AL << 1;
1692 return;
1693 }
1694
1696 if (auto *RS = D->getAttr<RootSignatureAttr>()) {
1697 if (RS->getSignatureIdent() != Ident) {
1698 Diag(AL.getLoc(), diag::err_disallowed_duplicate_attribute) << RS;
1699 return;
1700 }
1701
1702 Diag(AL.getLoc(), diag::warn_duplicate_attribute_exact) << RS;
1703 return;
1704 }
1705
1707 if (SemaRef.LookupQualifiedName(R, D->getDeclContext()))
1708 if (auto *SignatureDecl =
1709 dyn_cast<HLSLRootSignatureDecl>(R.getFoundDecl())) {
1710 D->addAttr(::new (getASTContext()) RootSignatureAttr(
1711 getASTContext(), AL, Ident, SignatureDecl));
1712 }
1713}
1714
1716 llvm::VersionTuple SMVersion =
1717 getASTContext().getTargetInfo().getTriple().getOSVersion();
1718 bool IsDXIL = getASTContext().getTargetInfo().getTriple().getArch() ==
1719 llvm::Triple::dxil;
1720
1721 uint32_t ZMax = 1024;
1722 uint32_t ThreadMax = 1024;
1723 if (IsDXIL && SMVersion.getMajor() <= 4) {
1724 ZMax = 1;
1725 ThreadMax = 768;
1726 } else if (IsDXIL && SMVersion.getMajor() == 5) {
1727 ZMax = 64;
1728 ThreadMax = 1024;
1729 }
1730
1731 uint32_t X;
1732 if (!SemaRef.checkUInt32Argument(AL, AL.getArgAsExpr(0), X))
1733 return;
1734 if (X > 1024) {
1735 Diag(AL.getArgAsExpr(0)->getExprLoc(),
1736 diag::err_hlsl_numthreads_argument_oor)
1737 << 0 << 1024;
1738 return;
1739 }
1740 uint32_t Y;
1741 if (!SemaRef.checkUInt32Argument(AL, AL.getArgAsExpr(1), Y))
1742 return;
1743 if (Y > 1024) {
1744 Diag(AL.getArgAsExpr(1)->getExprLoc(),
1745 diag::err_hlsl_numthreads_argument_oor)
1746 << 1 << 1024;
1747 return;
1748 }
1749 uint32_t Z;
1750 if (!SemaRef.checkUInt32Argument(AL, AL.getArgAsExpr(2), Z))
1751 return;
1752 if (Z > ZMax) {
1753 SemaRef.Diag(AL.getArgAsExpr(2)->getExprLoc(),
1754 diag::err_hlsl_numthreads_argument_oor)
1755 << 2 << ZMax;
1756 return;
1757 }
1758
1759 if (X * Y * Z > ThreadMax) {
1760 Diag(AL.getLoc(), diag::err_hlsl_numthreads_invalid) << ThreadMax;
1761 return;
1762 }
1763
1764 HLSLNumThreadsAttr *NewAttr = mergeNumThreadsAttr(D, AL, X, Y, Z);
1765 if (NewAttr)
1766 D->addAttr(NewAttr);
1767}
1768
1769static bool isValidWaveSizeValue(unsigned Value) {
1770 return llvm::isPowerOf2_32(Value) && Value >= 4 && Value <= 128;
1771}
1772
1774 // validate that the wavesize argument is a power of 2 between 4 and 128
1775 // inclusive
1776 unsigned SpelledArgsCount = AL.getNumArgs();
1777 if (SpelledArgsCount == 0 || SpelledArgsCount > 3)
1778 return;
1779
1780 uint32_t Min;
1781 if (!SemaRef.checkUInt32Argument(AL, AL.getArgAsExpr(0), Min))
1782 return;
1783
1784 uint32_t Max = 0;
1785 if (SpelledArgsCount > 1 &&
1786 !SemaRef.checkUInt32Argument(AL, AL.getArgAsExpr(1), Max))
1787 return;
1788
1789 uint32_t Preferred = 0;
1790 if (SpelledArgsCount > 2 &&
1791 !SemaRef.checkUInt32Argument(AL, AL.getArgAsExpr(2), Preferred))
1792 return;
1793
1794 if (SpelledArgsCount > 2) {
1795 if (!isValidWaveSizeValue(Preferred)) {
1796 Diag(AL.getArgAsExpr(2)->getExprLoc(),
1797 diag::err_attribute_power_of_two_in_range)
1798 << AL << llvm::dxil::MinWaveSize << llvm::dxil::MaxWaveSize
1799 << Preferred;
1800 return;
1801 }
1802 // Preferred not in range.
1803 if (Preferred < Min || Preferred > Max) {
1804 Diag(AL.getArgAsExpr(2)->getExprLoc(),
1805 diag::err_attribute_power_of_two_in_range)
1806 << AL << Min << Max << Preferred;
1807 return;
1808 }
1809 } else if (SpelledArgsCount > 1) {
1810 if (!isValidWaveSizeValue(Max)) {
1811 Diag(AL.getArgAsExpr(1)->getExprLoc(),
1812 diag::err_attribute_power_of_two_in_range)
1813 << AL << llvm::dxil::MinWaveSize << llvm::dxil::MaxWaveSize << Max;
1814 return;
1815 }
1816 if (Max < Min) {
1817 Diag(AL.getLoc(), diag::err_attribute_argument_invalid) << AL << 1;
1818 return;
1819 } else if (Max == Min) {
1820 Diag(AL.getLoc(), diag::warn_attr_min_eq_max) << AL;
1821 }
1822 } else {
1823 if (!isValidWaveSizeValue(Min)) {
1824 Diag(AL.getArgAsExpr(0)->getExprLoc(),
1825 diag::err_attribute_power_of_two_in_range)
1826 << AL << llvm::dxil::MinWaveSize << llvm::dxil::MaxWaveSize << Min;
1827 return;
1828 }
1829 }
1830
1831 HLSLWaveSizeAttr *NewAttr =
1832 mergeWaveSizeAttr(D, AL, Min, Max, Preferred, SpelledArgsCount);
1833 if (NewAttr)
1834 D->addAttr(NewAttr);
1835}
1836
1838 uint32_t ID;
1839 if (!SemaRef.checkUInt32Argument(AL, AL.getArgAsExpr(0), ID))
1840 return;
1841 D->addAttr(::new (getASTContext())
1842 HLSLVkExtBuiltinInputAttr(getASTContext(), AL, ID));
1843}
1844
1846 uint32_t ID;
1847 if (!SemaRef.checkUInt32Argument(AL, AL.getArgAsExpr(0), ID))
1848 return;
1849 D->addAttr(::new (getASTContext())
1850 HLSLVkExtBuiltinOutputAttr(getASTContext(), AL, ID));
1851}
1852
1854 D->addAttr(::new (getASTContext())
1855 HLSLVkPushConstantAttr(getASTContext(), AL));
1856}
1857
1859 uint32_t Id;
1860 if (!SemaRef.checkUInt32Argument(AL, AL.getArgAsExpr(0), Id))
1861 return;
1862 HLSLVkConstantIdAttr *NewAttr = mergeVkConstantIdAttr(D, AL, Id);
1863 if (NewAttr)
1864 D->addAttr(NewAttr);
1865}
1866
1868 uint32_t Binding = 0;
1869 if (!SemaRef.checkUInt32Argument(AL, AL.getArgAsExpr(0), Binding))
1870 return;
1871 uint32_t Set = 0;
1872 if (AL.getNumArgs() > 1 &&
1873 !SemaRef.checkUInt32Argument(AL, AL.getArgAsExpr(1), Set))
1874 return;
1875
1876 D->addAttr(::new (getASTContext())
1877 HLSLVkBindingAttr(getASTContext(), AL, Binding, Set));
1878}
1879
1881 uint32_t Location;
1882 if (!SemaRef.checkUInt32Argument(AL, AL.getArgAsExpr(0), Location))
1883 return;
1884
1885 D->addAttr(::new (getASTContext())
1886 HLSLVkLocationAttr(getASTContext(), AL, Location));
1887}
1888
1890 const auto *VT = T->getAs<VectorType>();
1891
1892 if (!T->hasUnsignedIntegerRepresentation() ||
1893 (VT && VT->getNumElements() > 3)) {
1894 Diag(AL.getLoc(), diag::err_hlsl_attr_invalid_type)
1895 << AL << "uint/uint2/uint3";
1896 return false;
1897 }
1898
1899 return true;
1900}
1901
1903 const auto *VT = T->getAs<VectorType>();
1904 if (!T->hasFloatingRepresentation() || (VT && VT->getNumElements() > 4)) {
1905 Diag(AL.getLoc(), diag::err_hlsl_attr_invalid_type)
1906 << AL << "float/float1/float2/float3/float4";
1907 return false;
1908 }
1909
1910 return true;
1911}
1912
1914 std::optional<unsigned> Index) {
1915 std::string SemanticName = AL.getAttrName()->getName().upper();
1916
1917 auto *VD = cast<ValueDecl>(D);
1918 QualType ValueType = VD->getType();
1919 if (auto *FD = dyn_cast<FunctionDecl>(D))
1920 ValueType = FD->getReturnType();
1921
1922 bool IsOutput = false;
1923 if (HLSLParamModifierAttr *MA = D->getAttr<HLSLParamModifierAttr>()) {
1924 if (MA->isOut()) {
1925 IsOutput = true;
1926 ValueType = cast<ReferenceType>(ValueType)->getPointeeType();
1927 }
1928 }
1929
1930 if (SemanticName == "SV_DISPATCHTHREADID") {
1931 diagnoseInputIDType(ValueType, AL);
1932 if (IsOutput)
1933 Diag(AL.getLoc(), diag::err_hlsl_semantic_output_not_supported) << AL;
1934 if (Index.has_value())
1935 Diag(AL.getLoc(), diag::err_hlsl_semantic_indexing_not_supported) << AL;
1937 return;
1938 }
1939
1940 if (SemanticName == "SV_GROUPINDEX") {
1941 if (IsOutput)
1942 Diag(AL.getLoc(), diag::err_hlsl_semantic_output_not_supported) << AL;
1943 if (Index.has_value())
1944 Diag(AL.getLoc(), diag::err_hlsl_semantic_indexing_not_supported) << AL;
1946 return;
1947 }
1948
1949 if (SemanticName == "SV_GROUPTHREADID") {
1950 diagnoseInputIDType(ValueType, AL);
1951 if (IsOutput)
1952 Diag(AL.getLoc(), diag::err_hlsl_semantic_output_not_supported) << AL;
1953 if (Index.has_value())
1954 Diag(AL.getLoc(), diag::err_hlsl_semantic_indexing_not_supported) << AL;
1956 return;
1957 }
1958
1959 if (SemanticName == "SV_GROUPID") {
1960 diagnoseInputIDType(ValueType, AL);
1961 if (IsOutput)
1962 Diag(AL.getLoc(), diag::err_hlsl_semantic_output_not_supported) << AL;
1963 if (Index.has_value())
1964 Diag(AL.getLoc(), diag::err_hlsl_semantic_indexing_not_supported) << AL;
1966 return;
1967 }
1968
1969 if (SemanticName == "SV_POSITION") {
1970 const auto *VT = ValueType->getAs<VectorType>();
1971 if (!ValueType->hasFloatingRepresentation() ||
1972 (VT && VT->getNumElements() > 4))
1973 Diag(AL.getLoc(), diag::err_hlsl_attr_invalid_type)
1974 << AL << "float/float1/float2/float3/float4";
1976 return;
1977 }
1978
1979 if (SemanticName == "SV_VERTEXID") {
1980 uint64_t SizeInBits = SemaRef.Context.getTypeSize(ValueType);
1981 if (!ValueType->isUnsignedIntegerType() || SizeInBits != 32)
1982 Diag(AL.getLoc(), diag::err_hlsl_attr_invalid_type) << AL << "uint";
1984 return;
1985 }
1986
1987 if (SemanticName == "SV_TARGET") {
1988 const auto *VT = ValueType->getAs<VectorType>();
1989 if (!ValueType->hasFloatingRepresentation() ||
1990 (VT && VT->getNumElements() > 4))
1991 Diag(AL.getLoc(), diag::err_hlsl_attr_invalid_type)
1992 << AL << "float/float1/float2/float3/float4";
1994 return;
1995 }
1996
1997 Diag(AL.getLoc(), diag::err_hlsl_unknown_semantic) << AL;
1998}
1999
2001 uint32_t IndexValue(0), ExplicitIndex(0);
2002 if (!SemaRef.checkUInt32Argument(AL, AL.getArgAsExpr(0), IndexValue) ||
2003 !SemaRef.checkUInt32Argument(AL, AL.getArgAsExpr(1), ExplicitIndex)) {
2004 assert(0 && "HLSLUnparsedSemantic is expected to have 2 int arguments.");
2005 }
2006 assert(IndexValue > 0 ? ExplicitIndex : true);
2007 std::optional<unsigned> Index =
2008 ExplicitIndex ? std::optional<unsigned>(IndexValue) : std::nullopt;
2009
2010 if (AL.getAttrName()->getName().starts_with_insensitive("SV_"))
2011 diagnoseSystemSemanticAttr(D, AL, Index);
2012 else
2014}
2015
2018 Diag(AL.getLoc(), diag::err_hlsl_attr_invalid_ast_node)
2019 << AL << "shader constant in a constant buffer";
2020 return;
2021 }
2022
2023 uint32_t SubComponent;
2024 if (!SemaRef.checkUInt32Argument(AL, AL.getArgAsExpr(0), SubComponent))
2025 return;
2026 uint32_t Component;
2027 if (!SemaRef.checkUInt32Argument(AL, AL.getArgAsExpr(1), Component))
2028 return;
2029
2030 QualType T = cast<VarDecl>(D)->getType().getCanonicalType();
2031 // Check if T is an array or struct type.
2032 // TODO: mark matrix type as aggregate type.
2033 bool IsAggregateTy = (T->isArrayType() || T->isStructureType());
2034
2035 // Check Component is valid for T.
2036 if (Component) {
2037 unsigned Size = getASTContext().getTypeSize(T);
2038 if (IsAggregateTy) {
2039 Diag(AL.getLoc(), diag::err_hlsl_invalid_register_or_packoffset);
2040 return;
2041 } else {
2042 // Make sure Component + sizeof(T) <= 4.
2043 if ((Component * 32 + Size) > 128) {
2044 Diag(AL.getLoc(), diag::err_hlsl_packoffset_cross_reg_boundary);
2045 return;
2046 }
2047 QualType EltTy = T;
2048 if (const auto *VT = T->getAs<VectorType>())
2049 EltTy = VT->getElementType();
2050 unsigned Align = getASTContext().getTypeAlign(EltTy);
2051 if (Align > 32 && Component == 1) {
2052 // NOTE: Component 3 will hit err_hlsl_packoffset_cross_reg_boundary.
2053 // So we only need to check Component 1 here.
2054 Diag(AL.getLoc(), diag::err_hlsl_packoffset_alignment_mismatch)
2055 << Align << EltTy;
2056 return;
2057 }
2058 }
2059 }
2060
2061 D->addAttr(::new (getASTContext()) HLSLPackOffsetAttr(
2062 getASTContext(), AL, SubComponent, Component));
2063}
2064
2066 StringRef Str;
2067 SourceLocation ArgLoc;
2068 if (!SemaRef.checkStringLiteralArgumentAttr(AL, 0, Str, &ArgLoc))
2069 return;
2070
2071 llvm::Triple::EnvironmentType ShaderType;
2072 if (!HLSLShaderAttr::ConvertStrToEnvironmentType(Str, ShaderType)) {
2073 Diag(AL.getLoc(), diag::warn_attribute_type_not_supported)
2074 << AL << Str << ArgLoc;
2075 return;
2076 }
2077
2078 // FIXME: check function match the shader stage.
2079
2080 HLSLShaderAttr *NewAttr = mergeShaderAttr(D, AL, ShaderType);
2081 if (NewAttr)
2082 D->addAttr(NewAttr);
2083}
2084
2086 Sema &S, QualType Wrapped, ArrayRef<const Attr *> AttrList,
2087 QualType &ResType, HLSLAttributedResourceLocInfo *LocInfo) {
2088 assert(AttrList.size() && "expected list of resource attributes");
2089
2090 QualType ContainedTy = QualType();
2091 TypeSourceInfo *ContainedTyInfo = nullptr;
2092 SourceLocation LocBegin = AttrList[0]->getRange().getBegin();
2093 SourceLocation LocEnd = AttrList[0]->getRange().getEnd();
2094
2095 HLSLAttributedResourceType::Attributes ResAttrs;
2096
2097 bool HasResourceClass = false;
2098 bool HasResourceDimension = false;
2099 for (const Attr *A : AttrList) {
2100 if (!A)
2101 continue;
2102 LocEnd = A->getRange().getEnd();
2103 switch (A->getKind()) {
2104 case attr::HLSLResourceClass: {
2105 ResourceClass RC = cast<HLSLResourceClassAttr>(A)->getResourceClass();
2106 if (HasResourceClass) {
2107 S.Diag(A->getLocation(), ResAttrs.ResourceClass == RC
2108 ? diag::warn_duplicate_attribute_exact
2109 : diag::warn_duplicate_attribute)
2110 << A;
2111 return false;
2112 }
2113 ResAttrs.ResourceClass = RC;
2114 HasResourceClass = true;
2115 break;
2116 }
2117 case attr::HLSLResourceDimension: {
2118 llvm::dxil::ResourceDimension RD =
2119 cast<HLSLResourceDimensionAttr>(A)->getDimension();
2120 if (HasResourceDimension) {
2121 S.Diag(A->getLocation(), ResAttrs.ResourceDimension == RD
2122 ? diag::warn_duplicate_attribute_exact
2123 : diag::warn_duplicate_attribute)
2124 << A;
2125 return false;
2126 }
2127 ResAttrs.ResourceDimension = RD;
2128 HasResourceDimension = true;
2129 break;
2130 }
2131 case attr::HLSLROV:
2132 if (ResAttrs.IsROV) {
2133 S.Diag(A->getLocation(), diag::warn_duplicate_attribute_exact) << A;
2134 return false;
2135 }
2136 ResAttrs.IsROV = true;
2137 break;
2138 case attr::HLSLRawBuffer:
2139 if (ResAttrs.RawBuffer) {
2140 S.Diag(A->getLocation(), diag::warn_duplicate_attribute_exact) << A;
2141 return false;
2142 }
2143 ResAttrs.RawBuffer = true;
2144 break;
2145 case attr::HLSLIsCounter:
2146 if (ResAttrs.IsCounter) {
2147 S.Diag(A->getLocation(), diag::warn_duplicate_attribute_exact) << A;
2148 return false;
2149 }
2150 ResAttrs.IsCounter = true;
2151 break;
2152 case attr::HLSLContainedType: {
2153 const HLSLContainedTypeAttr *CTAttr = cast<HLSLContainedTypeAttr>(A);
2154 QualType Ty = CTAttr->getType();
2155 if (!ContainedTy.isNull()) {
2156 S.Diag(A->getLocation(), ContainedTy == Ty
2157 ? diag::warn_duplicate_attribute_exact
2158 : diag::warn_duplicate_attribute)
2159 << A;
2160 return false;
2161 }
2162 ContainedTy = Ty;
2163 ContainedTyInfo = CTAttr->getTypeLoc();
2164 break;
2165 }
2166 default:
2167 llvm_unreachable("unhandled resource attribute type");
2168 }
2169 }
2170
2171 if (!HasResourceClass) {
2172 S.Diag(AttrList.back()->getRange().getEnd(),
2173 diag::err_hlsl_missing_resource_class);
2174 return false;
2175 }
2176
2178 Wrapped, ContainedTy, ResAttrs);
2179
2180 if (LocInfo && ContainedTyInfo) {
2181 LocInfo->Range = SourceRange(LocBegin, LocEnd);
2182 LocInfo->ContainedTyInfo = ContainedTyInfo;
2183 }
2184 return true;
2185}
2186
2187// Validates and creates an HLSL attribute that is applied as type attribute on
2188// HLSL resource. The attributes are collected in HLSLResourcesTypeAttrs and at
2189// the end of the declaration they are applied to the declaration type by
2190// wrapping it in HLSLAttributedResourceType.
2192 // only allow resource type attributes on intangible types
2193 if (!T->isHLSLResourceType()) {
2194 Diag(AL.getLoc(), diag::err_hlsl_attribute_needs_intangible_type)
2195 << AL << getASTContext().HLSLResourceTy;
2196 return false;
2197 }
2198
2199 // validate number of arguments
2200 if (!AL.checkExactlyNumArgs(SemaRef, AL.getMinArgs()))
2201 return false;
2202
2203 Attr *A = nullptr;
2204
2208 {
2209 AttributeCommonInfo::AS_CXX11, 0, false /*IsAlignas*/,
2210 false /*IsRegularKeywordAttribute*/
2211 });
2212
2213 switch (AL.getKind()) {
2214 case ParsedAttr::AT_HLSLResourceClass: {
2215 if (!AL.isArgIdent(0)) {
2216 Diag(AL.getLoc(), diag::err_attribute_argument_type)
2217 << AL << AANT_ArgumentIdentifier;
2218 return false;
2219 }
2220
2221 IdentifierLoc *Loc = AL.getArgAsIdent(0);
2222 StringRef Identifier = Loc->getIdentifierInfo()->getName();
2223 SourceLocation ArgLoc = Loc->getLoc();
2224
2225 // Validate resource class value
2226 ResourceClass RC;
2227 if (!HLSLResourceClassAttr::ConvertStrToResourceClass(Identifier, RC)) {
2228 Diag(ArgLoc, diag::warn_attribute_type_not_supported)
2229 << "ResourceClass" << Identifier;
2230 return false;
2231 }
2232 A = HLSLResourceClassAttr::Create(getASTContext(), RC, ACI);
2233 break;
2234 }
2235
2236 case ParsedAttr::AT_HLSLResourceDimension: {
2237 StringRef Identifier;
2238 SourceLocation ArgLoc;
2239 if (!SemaRef.checkStringLiteralArgumentAttr(AL, 0, Identifier, &ArgLoc))
2240 return false;
2241
2242 // Validate resource dimension value
2243 llvm::dxil::ResourceDimension RD;
2244 if (!HLSLResourceDimensionAttr::ConvertStrToResourceDimension(Identifier,
2245 RD)) {
2246 Diag(ArgLoc, diag::warn_attribute_type_not_supported)
2247 << "ResourceDimension" << Identifier;
2248 return false;
2249 }
2250 A = HLSLResourceDimensionAttr::Create(getASTContext(), RD, ACI);
2251 break;
2252 }
2253
2254 case ParsedAttr::AT_HLSLROV:
2255 A = HLSLROVAttr::Create(getASTContext(), ACI);
2256 break;
2257
2258 case ParsedAttr::AT_HLSLRawBuffer:
2259 A = HLSLRawBufferAttr::Create(getASTContext(), ACI);
2260 break;
2261
2262 case ParsedAttr::AT_HLSLIsCounter:
2263 A = HLSLIsCounterAttr::Create(getASTContext(), ACI);
2264 break;
2265
2266 case ParsedAttr::AT_HLSLContainedType: {
2267 if (AL.getNumArgs() != 1 && !AL.hasParsedType()) {
2268 Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << AL << 1;
2269 return false;
2270 }
2271
2272 TypeSourceInfo *TSI = nullptr;
2273 QualType QT = SemaRef.GetTypeFromParser(AL.getTypeArg(), &TSI);
2274 assert(TSI && "no type source info for attribute argument");
2275 if (SemaRef.RequireCompleteType(TSI->getTypeLoc().getBeginLoc(), QT,
2276 diag::err_incomplete_type))
2277 return false;
2278 A = HLSLContainedTypeAttr::Create(getASTContext(), TSI, ACI);
2279 break;
2280 }
2281
2282 default:
2283 llvm_unreachable("unhandled HLSL attribute");
2284 }
2285
2286 HLSLResourcesTypeAttrs.emplace_back(A);
2287 return true;
2288}
2289
2290// Combines all resource type attributes and creates HLSLAttributedResourceType.
2292 if (!HLSLResourcesTypeAttrs.size())
2293 return CurrentType;
2294
2295 QualType QT = CurrentType;
2298 HLSLResourcesTypeAttrs, QT, &LocInfo)) {
2299 const HLSLAttributedResourceType *RT =
2301
2302 // Temporarily store TypeLoc information for the new type.
2303 // It will be transferred to HLSLAttributesResourceTypeLoc
2304 // shortly after the type is created by TypeSpecLocFiller which
2305 // will call the TakeLocForHLSLAttribute method below.
2306 LocsForHLSLAttributedResources.insert(std::pair(RT, LocInfo));
2307 }
2308 HLSLResourcesTypeAttrs.clear();
2309 return QT;
2310}
2311
2312// Returns source location for the HLSLAttributedResourceType
2314SemaHLSL::TakeLocForHLSLAttribute(const HLSLAttributedResourceType *RT) {
2315 HLSLAttributedResourceLocInfo LocInfo = {};
2316 auto I = LocsForHLSLAttributedResources.find(RT);
2317 if (I != LocsForHLSLAttributedResources.end()) {
2318 LocInfo = I->second;
2319 LocsForHLSLAttributedResources.erase(I);
2320 return LocInfo;
2321 }
2322 LocInfo.Range = SourceRange();
2323 return LocInfo;
2324}
2325
2326// Walks though the global variable declaration, collects all resource binding
2327// requirements and adds them to Bindings
2328void SemaHLSL::collectResourceBindingsOnUserRecordDecl(const VarDecl *VD,
2329 const RecordType *RT) {
2330 const RecordDecl *RD = RT->getDecl()->getDefinitionOrSelf();
2331 for (FieldDecl *FD : RD->fields()) {
2332 const Type *Ty = FD->getType()->getUnqualifiedDesugaredType();
2333
2334 // Unwrap arrays
2335 // FIXME: Calculate array size while unwrapping
2336 assert(!Ty->isIncompleteArrayType() &&
2337 "incomplete arrays inside user defined types are not supported");
2338 while (Ty->isConstantArrayType()) {
2341 }
2342
2343 if (!Ty->isRecordType())
2344 continue;
2345
2346 if (const HLSLAttributedResourceType *AttrResType =
2347 HLSLAttributedResourceType::findHandleTypeOnResource(Ty)) {
2348 // Add a new DeclBindingInfo to Bindings if it does not already exist
2349 ResourceClass RC = AttrResType->getAttrs().ResourceClass;
2350 DeclBindingInfo *DBI = Bindings.getDeclBindingInfo(VD, RC);
2351 if (!DBI)
2352 Bindings.addDeclBindingInfo(VD, RC);
2353 } else if (const RecordType *RT = dyn_cast<RecordType>(Ty)) {
2354 // Recursively scan embedded struct or class; it would be nice to do this
2355 // without recursion, but tricky to correctly calculate the size of the
2356 // binding, which is something we are probably going to need to do later
2357 // on. Hopefully nesting of structs in structs too many levels is
2358 // unlikely.
2359 collectResourceBindingsOnUserRecordDecl(VD, RT);
2360 }
2361 }
2362}
2363
2364// Diagnose localized register binding errors for a single binding; does not
2365// diagnose resource binding on user record types, that will be done later
2366// in processResourceBindingOnDecl based on the information collected in
2367// collectResourceBindingsOnVarDecl.
2368// Returns false if the register binding is not valid.
2370 Decl *D, RegisterType RegType,
2371 bool SpecifiedSpace) {
2372 int RegTypeNum = static_cast<int>(RegType);
2373
2374 // check if the decl type is groupshared
2375 if (D->hasAttr<HLSLGroupSharedAddressSpaceAttr>()) {
2376 S.Diag(ArgLoc, diag::err_hlsl_binding_type_mismatch) << RegTypeNum;
2377 return false;
2378 }
2379
2380 // Cbuffers and Tbuffers are HLSLBufferDecl types
2381 if (HLSLBufferDecl *CBufferOrTBuffer = dyn_cast<HLSLBufferDecl>(D)) {
2382 ResourceClass RC = CBufferOrTBuffer->isCBuffer() ? ResourceClass::CBuffer
2383 : ResourceClass::SRV;
2384 if (RegType == getRegisterType(RC))
2385 return true;
2386
2387 S.Diag(D->getLocation(), diag::err_hlsl_binding_type_mismatch)
2388 << RegTypeNum;
2389 return false;
2390 }
2391
2392 // Samplers, UAVs, and SRVs are VarDecl types
2393 assert(isa<VarDecl>(D) && "D is expected to be VarDecl or HLSLBufferDecl");
2394 VarDecl *VD = cast<VarDecl>(D);
2395
2396 // Resource
2397 if (const HLSLAttributedResourceType *AttrResType =
2398 HLSLAttributedResourceType::findHandleTypeOnResource(
2399 VD->getType().getTypePtr())) {
2400 if (RegType == getRegisterType(AttrResType))
2401 return true;
2402
2403 S.Diag(D->getLocation(), diag::err_hlsl_binding_type_mismatch)
2404 << RegTypeNum;
2405 return false;
2406 }
2407
2408 const clang::Type *Ty = VD->getType().getTypePtr();
2409 while (Ty->isArrayType())
2411
2412 // Basic types
2413 if (Ty->isArithmeticType() || Ty->isVectorType()) {
2414 bool DeclaredInCOrTBuffer = isa<HLSLBufferDecl>(D->getDeclContext());
2415 if (SpecifiedSpace && !DeclaredInCOrTBuffer)
2416 S.Diag(ArgLoc, diag::err_hlsl_space_on_global_constant);
2417
2418 if (!DeclaredInCOrTBuffer && (Ty->isIntegralType(S.getASTContext()) ||
2419 Ty->isFloatingType() || Ty->isVectorType())) {
2420 // Register annotation on default constant buffer declaration ($Globals)
2421 if (RegType == RegisterType::CBuffer)
2422 S.Diag(ArgLoc, diag::warn_hlsl_deprecated_register_type_b);
2423 else if (RegType != RegisterType::C)
2424 S.Diag(ArgLoc, diag::err_hlsl_binding_type_mismatch) << RegTypeNum;
2425 else
2426 return true;
2427 } else {
2428 if (RegType == RegisterType::C)
2429 S.Diag(ArgLoc, diag::warn_hlsl_register_type_c_packoffset);
2430 else
2431 S.Diag(ArgLoc, diag::err_hlsl_binding_type_mismatch) << RegTypeNum;
2432 }
2433 return false;
2434 }
2435 if (Ty->isRecordType())
2436 // RecordTypes will be diagnosed in processResourceBindingOnDecl
2437 // that is called from ActOnVariableDeclarator
2438 return true;
2439
2440 // Anything else is an error
2441 S.Diag(ArgLoc, diag::err_hlsl_binding_type_mismatch) << RegTypeNum;
2442 return false;
2443}
2444
2446 RegisterType regType) {
2447 // make sure that there are no two register annotations
2448 // applied to the decl with the same register type
2449 bool RegisterTypesDetected[5] = {false};
2450 RegisterTypesDetected[static_cast<int>(regType)] = true;
2451
2452 for (auto it = TheDecl->attr_begin(); it != TheDecl->attr_end(); ++it) {
2453 if (HLSLResourceBindingAttr *attr =
2454 dyn_cast<HLSLResourceBindingAttr>(*it)) {
2455
2456 RegisterType otherRegType = attr->getRegisterType();
2457 if (RegisterTypesDetected[static_cast<int>(otherRegType)]) {
2458 int otherRegTypeNum = static_cast<int>(otherRegType);
2459 S.Diag(TheDecl->getLocation(),
2460 diag::err_hlsl_duplicate_register_annotation)
2461 << otherRegTypeNum;
2462 return false;
2463 }
2464 RegisterTypesDetected[static_cast<int>(otherRegType)] = true;
2465 }
2466 }
2467 return true;
2468}
2469
2471 Decl *D, RegisterType RegType,
2472 bool SpecifiedSpace) {
2473
2474 // exactly one of these two types should be set
2475 assert(((isa<VarDecl>(D) && !isa<HLSLBufferDecl>(D)) ||
2476 (!isa<VarDecl>(D) && isa<HLSLBufferDecl>(D))) &&
2477 "expecting VarDecl or HLSLBufferDecl");
2478
2479 // check if the declaration contains resource matching the register type
2480 if (!DiagnoseLocalRegisterBinding(S, ArgLoc, D, RegType, SpecifiedSpace))
2481 return false;
2482
2483 // next, if multiple register annotations exist, check that none conflict.
2484 return ValidateMultipleRegisterAnnotations(S, D, RegType);
2485}
2486
2487// return false if the slot count exceeds the limit, true otherwise
2488static bool AccumulateHLSLResourceSlots(QualType Ty, uint64_t &StartSlot,
2489 const uint64_t &Limit,
2490 const ResourceClass ResClass,
2491 ASTContext &Ctx,
2492 uint64_t ArrayCount = 1) {
2493 Ty = Ty.getCanonicalType();
2494 const Type *T = Ty.getTypePtr();
2495
2496 // Early exit if already overflowed
2497 if (StartSlot > Limit)
2498 return false;
2499
2500 // Case 1: array type
2501 if (const auto *AT = dyn_cast<ArrayType>(T)) {
2502 uint64_t Count = 1;
2503
2504 if (const auto *CAT = dyn_cast<ConstantArrayType>(AT))
2505 Count = CAT->getSize().getZExtValue();
2506
2507 QualType ElemTy = AT->getElementType();
2508 return AccumulateHLSLResourceSlots(ElemTy, StartSlot, Limit, ResClass, Ctx,
2509 ArrayCount * Count);
2510 }
2511
2512 // Case 2: resource leaf
2513 if (auto ResTy = dyn_cast<HLSLAttributedResourceType>(T)) {
2514 // First ensure this resource counts towards the corresponding
2515 // register type limit.
2516 if (ResTy->getAttrs().ResourceClass != ResClass)
2517 return true;
2518
2519 // Validate highest slot used
2520 uint64_t EndSlot = StartSlot + ArrayCount - 1;
2521 if (EndSlot > Limit)
2522 return false;
2523
2524 // Advance SlotCount past the consumed range
2525 StartSlot = EndSlot + 1;
2526 return true;
2527 }
2528
2529 // Case 3: struct / record
2530 if (const auto *RT = dyn_cast<RecordType>(T)) {
2531 const RecordDecl *RD = RT->getDecl();
2532
2533 if (const auto *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
2534 for (const CXXBaseSpecifier &Base : CXXRD->bases()) {
2535 if (!AccumulateHLSLResourceSlots(Base.getType(), StartSlot, Limit,
2536 ResClass, Ctx, ArrayCount))
2537 return false;
2538 }
2539 }
2540
2541 for (const FieldDecl *Field : RD->fields()) {
2542 if (!AccumulateHLSLResourceSlots(Field->getType(), StartSlot, Limit,
2543 ResClass, Ctx, ArrayCount))
2544 return false;
2545 }
2546
2547 return true;
2548 }
2549
2550 // Case 4: everything else
2551 return true;
2552}
2553
2554// return true if there is something invalid, false otherwise
2555static bool ValidateRegisterNumber(uint64_t SlotNum, Decl *TheDecl,
2556 ASTContext &Ctx, RegisterType RegTy) {
2557 const uint64_t Limit = UINT32_MAX;
2558 if (SlotNum > Limit)
2559 return true;
2560
2561 // after verifying the number doesn't exceed uint32max, we don't need
2562 // to look further into c or i register types
2563 if (RegTy == RegisterType::C || RegTy == RegisterType::I)
2564 return false;
2565
2566 if (VarDecl *VD = dyn_cast<VarDecl>(TheDecl)) {
2567 uint64_t BaseSlot = SlotNum;
2568
2569 if (!AccumulateHLSLResourceSlots(VD->getType(), SlotNum, Limit,
2570 getResourceClass(RegTy), Ctx))
2571 return true;
2572
2573 // After AccumulateHLSLResourceSlots runs, SlotNum is now
2574 // the first free slot; last used was SlotNum - 1
2575 return (BaseSlot > Limit);
2576 }
2577 // handle the cbuffer/tbuffer case
2578 if (isa<HLSLBufferDecl>(TheDecl))
2579 // resources cannot be put within a cbuffer, so no need
2580 // to analyze the structure since the register number
2581 // won't be pushed any higher.
2582 return (SlotNum > Limit);
2583
2584 // we don't expect any other decl type, so fail
2585 llvm_unreachable("unexpected decl type");
2586}
2587
2589 if (VarDecl *VD = dyn_cast<VarDecl>(TheDecl)) {
2590 QualType Ty = VD->getType();
2591 if (const auto *IAT = dyn_cast<IncompleteArrayType>(Ty))
2592 Ty = IAT->getElementType();
2593 if (SemaRef.RequireCompleteType(TheDecl->getBeginLoc(), Ty,
2594 diag::err_incomplete_type))
2595 return;
2596 }
2597
2598 StringRef Slot = "";
2599 StringRef Space = "";
2600 SourceLocation SlotLoc, SpaceLoc;
2601
2602 if (!AL.isArgIdent(0)) {
2603 Diag(AL.getLoc(), diag::err_attribute_argument_type)
2604 << AL << AANT_ArgumentIdentifier;
2605 return;
2606 }
2607 IdentifierLoc *Loc = AL.getArgAsIdent(0);
2608
2609 if (AL.getNumArgs() == 2) {
2610 Slot = Loc->getIdentifierInfo()->getName();
2611 SlotLoc = Loc->getLoc();
2612 if (!AL.isArgIdent(1)) {
2613 Diag(AL.getLoc(), diag::err_attribute_argument_type)
2614 << AL << AANT_ArgumentIdentifier;
2615 return;
2616 }
2617 Loc = AL.getArgAsIdent(1);
2618 Space = Loc->getIdentifierInfo()->getName();
2619 SpaceLoc = Loc->getLoc();
2620 } else {
2621 StringRef Str = Loc->getIdentifierInfo()->getName();
2622 if (Str.starts_with("space")) {
2623 Space = Str;
2624 SpaceLoc = Loc->getLoc();
2625 } else {
2626 Slot = Str;
2627 SlotLoc = Loc->getLoc();
2628 Space = "space0";
2629 }
2630 }
2631
2632 RegisterType RegType = RegisterType::SRV;
2633 std::optional<unsigned> SlotNum;
2634 unsigned SpaceNum = 0;
2635
2636 // Validate slot
2637 if (!Slot.empty()) {
2638 if (!convertToRegisterType(Slot, &RegType)) {
2639 Diag(SlotLoc, diag::err_hlsl_binding_type_invalid) << Slot.substr(0, 1);
2640 return;
2641 }
2642 if (RegType == RegisterType::I) {
2643 Diag(SlotLoc, diag::warn_hlsl_deprecated_register_type_i);
2644 return;
2645 }
2646 const StringRef SlotNumStr = Slot.substr(1);
2647
2648 uint64_t N;
2649
2650 // validate that the slot number is a non-empty number
2651 if (SlotNumStr.getAsInteger(10, N)) {
2652 Diag(SlotLoc, diag::err_hlsl_unsupported_register_number);
2653 return;
2654 }
2655
2656 // Validate register number. It should not exceed UINT32_MAX,
2657 // including if the resource type is an array that starts
2658 // before UINT32_MAX, but ends afterwards.
2659 if (ValidateRegisterNumber(N, TheDecl, getASTContext(), RegType)) {
2660 Diag(SlotLoc, diag::err_hlsl_register_number_too_large);
2661 return;
2662 }
2663
2664 // the slot number has been validated and does not exceed UINT32_MAX
2665 SlotNum = (unsigned)N;
2666 }
2667
2668 // Validate space
2669 if (!Space.starts_with("space")) {
2670 Diag(SpaceLoc, diag::err_hlsl_expected_space) << Space;
2671 return;
2672 }
2673 StringRef SpaceNumStr = Space.substr(5);
2674 if (SpaceNumStr.getAsInteger(10, SpaceNum)) {
2675 Diag(SpaceLoc, diag::err_hlsl_expected_space) << Space;
2676 return;
2677 }
2678
2679 // If we have slot, diagnose it is the right register type for the decl
2680 if (SlotNum.has_value())
2681 if (!DiagnoseHLSLRegisterAttribute(SemaRef, SlotLoc, TheDecl, RegType,
2682 !SpaceLoc.isInvalid()))
2683 return;
2684
2685 HLSLResourceBindingAttr *NewAttr =
2686 HLSLResourceBindingAttr::Create(getASTContext(), Slot, Space, AL);
2687 if (NewAttr) {
2688 NewAttr->setBinding(RegType, SlotNum, SpaceNum);
2689 TheDecl->addAttr(NewAttr);
2690 }
2691}
2692
2694 HLSLParamModifierAttr *NewAttr = mergeParamModifierAttr(
2695 D, AL,
2696 static_cast<HLSLParamModifierAttr::Spelling>(AL.getSemanticSpelling()));
2697 if (NewAttr)
2698 D->addAttr(NewAttr);
2699}
2700
2701namespace {
2702
2703/// This class implements HLSL availability diagnostics for default
2704/// and relaxed mode
2705///
2706/// The goal of this diagnostic is to emit an error or warning when an
2707/// unavailable API is found in code that is reachable from the shader
2708/// entry function or from an exported function (when compiling a shader
2709/// library).
2710///
2711/// This is done by traversing the AST of all shader entry point functions
2712/// and of all exported functions, and any functions that are referenced
2713/// from this AST. In other words, any functions that are reachable from
2714/// the entry points.
2715class DiagnoseHLSLAvailability : public DynamicRecursiveASTVisitor {
2716 Sema &SemaRef;
2717
2718 // Stack of functions to be scaned
2720
2721 // Tracks which environments functions have been scanned in.
2722 //
2723 // Maps FunctionDecl to an unsigned number that represents the set of shader
2724 // environments the function has been scanned for.
2725 // The llvm::Triple::EnvironmentType enum values for shader stages guaranteed
2726 // to be numbered from llvm::Triple::Pixel to llvm::Triple::Amplification
2727 // (verified by static_asserts in Triple.cpp), we can use it to index
2728 // individual bits in the set, as long as we shift the values to start with 0
2729 // by subtracting the value of llvm::Triple::Pixel first.
2730 //
2731 // The N'th bit in the set will be set if the function has been scanned
2732 // in shader environment whose llvm::Triple::EnvironmentType integer value
2733 // equals (llvm::Triple::Pixel + N).
2734 //
2735 // For example, if a function has been scanned in compute and pixel stage
2736 // environment, the value will be 0x21 (100001 binary) because:
2737 //
2738 // (int)(llvm::Triple::Pixel - llvm::Triple::Pixel) == 0
2739 // (int)(llvm::Triple::Compute - llvm::Triple::Pixel) == 5
2740 //
2741 // A FunctionDecl is mapped to 0 (or not included in the map) if it has not
2742 // been scanned in any environment.
2743 llvm::DenseMap<const FunctionDecl *, unsigned> ScannedDecls;
2744
2745 // Do not access these directly, use the get/set methods below to make
2746 // sure the values are in sync
2747 llvm::Triple::EnvironmentType CurrentShaderEnvironment;
2748 unsigned CurrentShaderStageBit;
2749
2750 // True if scanning a function that was already scanned in a different
2751 // shader stage context, and therefore we should not report issues that
2752 // depend only on shader model version because they would be duplicate.
2753 bool ReportOnlyShaderStageIssues;
2754
2755 // Helper methods for dealing with current stage context / environment
2756 void SetShaderStageContext(llvm::Triple::EnvironmentType ShaderType) {
2757 static_assert(sizeof(unsigned) >= 4);
2758 assert(HLSLShaderAttr::isValidShaderType(ShaderType));
2759 assert((unsigned)(ShaderType - llvm::Triple::Pixel) < 31 &&
2760 "ShaderType is too big for this bitmap"); // 31 is reserved for
2761 // "unknown"
2762
2763 unsigned bitmapIndex = ShaderType - llvm::Triple::Pixel;
2764 CurrentShaderEnvironment = ShaderType;
2765 CurrentShaderStageBit = (1 << bitmapIndex);
2766 }
2767
2768 void SetUnknownShaderStageContext() {
2769 CurrentShaderEnvironment = llvm::Triple::UnknownEnvironment;
2770 CurrentShaderStageBit = (1 << 31);
2771 }
2772
2773 llvm::Triple::EnvironmentType GetCurrentShaderEnvironment() const {
2774 return CurrentShaderEnvironment;
2775 }
2776
2777 bool InUnknownShaderStageContext() const {
2778 return CurrentShaderEnvironment == llvm::Triple::UnknownEnvironment;
2779 }
2780
2781 // Helper methods for dealing with shader stage bitmap
2782 void AddToScannedFunctions(const FunctionDecl *FD) {
2783 unsigned &ScannedStages = ScannedDecls[FD];
2784 ScannedStages |= CurrentShaderStageBit;
2785 }
2786
2787 unsigned GetScannedStages(const FunctionDecl *FD) { return ScannedDecls[FD]; }
2788
2789 bool WasAlreadyScannedInCurrentStage(const FunctionDecl *FD) {
2790 return WasAlreadyScannedInCurrentStage(GetScannedStages(FD));
2791 }
2792
2793 bool WasAlreadyScannedInCurrentStage(unsigned ScannerStages) {
2794 return ScannerStages & CurrentShaderStageBit;
2795 }
2796
2797 static bool NeverBeenScanned(unsigned ScannedStages) {
2798 return ScannedStages == 0;
2799 }
2800
2801 // Scanning methods
2802 void HandleFunctionOrMethodRef(FunctionDecl *FD, Expr *RefExpr);
2803 void CheckDeclAvailability(NamedDecl *D, const AvailabilityAttr *AA,
2804 SourceRange Range);
2805 const AvailabilityAttr *FindAvailabilityAttr(const Decl *D);
2806 bool HasMatchingEnvironmentOrNone(const AvailabilityAttr *AA);
2807
2808public:
2809 DiagnoseHLSLAvailability(Sema &SemaRef)
2810 : SemaRef(SemaRef),
2811 CurrentShaderEnvironment(llvm::Triple::UnknownEnvironment),
2812 CurrentShaderStageBit(0), ReportOnlyShaderStageIssues(false) {}
2813
2814 // AST traversal methods
2815 void RunOnTranslationUnit(const TranslationUnitDecl *TU);
2816 void RunOnFunction(const FunctionDecl *FD);
2817
2818 bool VisitDeclRefExpr(DeclRefExpr *DRE) override {
2819 FunctionDecl *FD = llvm::dyn_cast<FunctionDecl>(DRE->getDecl());
2820 if (FD)
2821 HandleFunctionOrMethodRef(FD, DRE);
2822 return true;
2823 }
2824
2825 bool VisitMemberExpr(MemberExpr *ME) override {
2826 FunctionDecl *FD = llvm::dyn_cast<FunctionDecl>(ME->getMemberDecl());
2827 if (FD)
2828 HandleFunctionOrMethodRef(FD, ME);
2829 return true;
2830 }
2831};
2832
2833void DiagnoseHLSLAvailability::HandleFunctionOrMethodRef(FunctionDecl *FD,
2834 Expr *RefExpr) {
2835 assert((isa<DeclRefExpr>(RefExpr) || isa<MemberExpr>(RefExpr)) &&
2836 "expected DeclRefExpr or MemberExpr");
2837
2838 // has a definition -> add to stack to be scanned
2839 const FunctionDecl *FDWithBody = nullptr;
2840 if (FD->hasBody(FDWithBody)) {
2841 if (!WasAlreadyScannedInCurrentStage(FDWithBody))
2842 DeclsToScan.push_back(FDWithBody);
2843 return;
2844 }
2845
2846 // no body -> diagnose availability
2847 const AvailabilityAttr *AA = FindAvailabilityAttr(FD);
2848 if (AA)
2849 CheckDeclAvailability(
2850 FD, AA, SourceRange(RefExpr->getBeginLoc(), RefExpr->getEndLoc()));
2851}
2852
2853void DiagnoseHLSLAvailability::RunOnTranslationUnit(
2854 const TranslationUnitDecl *TU) {
2855
2856 // Iterate over all shader entry functions and library exports, and for those
2857 // that have a body (definiton), run diag scan on each, setting appropriate
2858 // shader environment context based on whether it is a shader entry function
2859 // or an exported function. Exported functions can be in namespaces and in
2860 // export declarations so we need to scan those declaration contexts as well.
2862 DeclContextsToScan.push_back(TU);
2863
2864 while (!DeclContextsToScan.empty()) {
2865 const DeclContext *DC = DeclContextsToScan.pop_back_val();
2866 for (auto &D : DC->decls()) {
2867 // do not scan implicit declaration generated by the implementation
2868 if (D->isImplicit())
2869 continue;
2870
2871 // for namespace or export declaration add the context to the list to be
2872 // scanned later
2873 if (llvm::dyn_cast<NamespaceDecl>(D) || llvm::dyn_cast<ExportDecl>(D)) {
2874 DeclContextsToScan.push_back(llvm::dyn_cast<DeclContext>(D));
2875 continue;
2876 }
2877
2878 // skip over other decls or function decls without body
2879 const FunctionDecl *FD = llvm::dyn_cast<FunctionDecl>(D);
2880 if (!FD || !FD->isThisDeclarationADefinition())
2881 continue;
2882
2883 // shader entry point
2884 if (HLSLShaderAttr *ShaderAttr = FD->getAttr<HLSLShaderAttr>()) {
2885 SetShaderStageContext(ShaderAttr->getType());
2886 RunOnFunction(FD);
2887 continue;
2888 }
2889 // exported library function
2890 // FIXME: replace this loop with external linkage check once issue #92071
2891 // is resolved
2892 bool isExport = FD->isInExportDeclContext();
2893 if (!isExport) {
2894 for (const auto *Redecl : FD->redecls()) {
2895 if (Redecl->isInExportDeclContext()) {
2896 isExport = true;
2897 break;
2898 }
2899 }
2900 }
2901 if (isExport) {
2902 SetUnknownShaderStageContext();
2903 RunOnFunction(FD);
2904 continue;
2905 }
2906 }
2907 }
2908}
2909
2910void DiagnoseHLSLAvailability::RunOnFunction(const FunctionDecl *FD) {
2911 assert(DeclsToScan.empty() && "DeclsToScan should be empty");
2912 DeclsToScan.push_back(FD);
2913
2914 while (!DeclsToScan.empty()) {
2915 // Take one decl from the stack and check it by traversing its AST.
2916 // For any CallExpr found during the traversal add it's callee to the top of
2917 // the stack to be processed next. Functions already processed are stored in
2918 // ScannedDecls.
2919 const FunctionDecl *FD = DeclsToScan.pop_back_val();
2920
2921 // Decl was already scanned
2922 const unsigned ScannedStages = GetScannedStages(FD);
2923 if (WasAlreadyScannedInCurrentStage(ScannedStages))
2924 continue;
2925
2926 ReportOnlyShaderStageIssues = !NeverBeenScanned(ScannedStages);
2927
2928 AddToScannedFunctions(FD);
2929 TraverseStmt(FD->getBody());
2930 }
2931}
2932
2933bool DiagnoseHLSLAvailability::HasMatchingEnvironmentOrNone(
2934 const AvailabilityAttr *AA) {
2935 const IdentifierInfo *IIEnvironment = AA->getEnvironment();
2936 if (!IIEnvironment)
2937 return true;
2938
2939 llvm::Triple::EnvironmentType CurrentEnv = GetCurrentShaderEnvironment();
2940 if (CurrentEnv == llvm::Triple::UnknownEnvironment)
2941 return false;
2942
2943 llvm::Triple::EnvironmentType AttrEnv =
2944 AvailabilityAttr::getEnvironmentType(IIEnvironment->getName());
2945
2946 return CurrentEnv == AttrEnv;
2947}
2948
2949const AvailabilityAttr *
2950DiagnoseHLSLAvailability::FindAvailabilityAttr(const Decl *D) {
2951 AvailabilityAttr const *PartialMatch = nullptr;
2952 // Check each AvailabilityAttr to find the one for this platform.
2953 // For multiple attributes with the same platform try to find one for this
2954 // environment.
2955 for (const auto *A : D->attrs()) {
2956 if (const auto *Avail = dyn_cast<AvailabilityAttr>(A)) {
2957 StringRef AttrPlatform = Avail->getPlatform()->getName();
2958 StringRef TargetPlatform =
2960
2961 // Match the platform name.
2962 if (AttrPlatform == TargetPlatform) {
2963 // Find the best matching attribute for this environment
2964 if (HasMatchingEnvironmentOrNone(Avail))
2965 return Avail;
2966 PartialMatch = Avail;
2967 }
2968 }
2969 }
2970 return PartialMatch;
2971}
2972
2973// Check availability against target shader model version and current shader
2974// stage and emit diagnostic
2975void DiagnoseHLSLAvailability::CheckDeclAvailability(NamedDecl *D,
2976 const AvailabilityAttr *AA,
2977 SourceRange Range) {
2978
2979 const IdentifierInfo *IIEnv = AA->getEnvironment();
2980
2981 if (!IIEnv) {
2982 // The availability attribute does not have environment -> it depends only
2983 // on shader model version and not on specific the shader stage.
2984
2985 // Skip emitting the diagnostics if the diagnostic mode is set to
2986 // strict (-fhlsl-strict-availability) because all relevant diagnostics
2987 // were already emitted in the DiagnoseUnguardedAvailability scan
2988 // (SemaAvailability.cpp).
2989 if (SemaRef.getLangOpts().HLSLStrictAvailability)
2990 return;
2991
2992 // Do not report shader-stage-independent issues if scanning a function
2993 // that was already scanned in a different shader stage context (they would
2994 // be duplicate)
2995 if (ReportOnlyShaderStageIssues)
2996 return;
2997
2998 } else {
2999 // The availability attribute has environment -> we need to know
3000 // the current stage context to property diagnose it.
3001 if (InUnknownShaderStageContext())
3002 return;
3003 }
3004
3005 // Check introduced version and if environment matches
3006 bool EnvironmentMatches = HasMatchingEnvironmentOrNone(AA);
3007 VersionTuple Introduced = AA->getIntroduced();
3008 VersionTuple TargetVersion =
3010
3011 if (TargetVersion >= Introduced && EnvironmentMatches)
3012 return;
3013
3014 // Emit diagnostic message
3015 const TargetInfo &TI = SemaRef.getASTContext().getTargetInfo();
3016 llvm::StringRef PlatformName(
3017 AvailabilityAttr::getPrettyPlatformName(TI.getPlatformName()));
3018
3019 llvm::StringRef CurrentEnvStr =
3020 llvm::Triple::getEnvironmentTypeName(GetCurrentShaderEnvironment());
3021
3022 llvm::StringRef AttrEnvStr =
3023 AA->getEnvironment() ? AA->getEnvironment()->getName() : "";
3024 bool UseEnvironment = !AttrEnvStr.empty();
3025
3026 if (EnvironmentMatches) {
3027 SemaRef.Diag(Range.getBegin(), diag::warn_hlsl_availability)
3028 << Range << D << PlatformName << Introduced.getAsString()
3029 << UseEnvironment << CurrentEnvStr;
3030 } else {
3031 SemaRef.Diag(Range.getBegin(), diag::warn_hlsl_availability_unavailable)
3032 << Range << D;
3033 }
3034
3035 SemaRef.Diag(D->getLocation(), diag::note_partial_availability_specified_here)
3036 << D << PlatformName << Introduced.getAsString()
3037 << SemaRef.Context.getTargetInfo().getPlatformMinVersion().getAsString()
3038 << UseEnvironment << AttrEnvStr << CurrentEnvStr;
3039}
3040
3041} // namespace
3042
3044 // process default CBuffer - create buffer layout struct and invoke codegenCGH
3045 if (!DefaultCBufferDecls.empty()) {
3047 SemaRef.getASTContext(), SemaRef.getCurLexicalContext(),
3048 DefaultCBufferDecls);
3049 addImplicitBindingAttrToDecl(SemaRef, DefaultCBuffer, RegisterType::CBuffer,
3051 SemaRef.getCurLexicalContext()->addDecl(DefaultCBuffer);
3053
3054 // Set HasValidPackoffset if any of the decls has a register(c#) annotation;
3055 for (const Decl *VD : DefaultCBufferDecls) {
3056 const HLSLResourceBindingAttr *RBA =
3057 VD->getAttr<HLSLResourceBindingAttr>();
3058 if (RBA && RBA->hasRegisterSlot() &&
3059 RBA->getRegisterType() == HLSLResourceBindingAttr::RegisterType::C) {
3060 DefaultCBuffer->setHasValidPackoffset(true);
3061 break;
3062 }
3063 }
3064
3065 DeclGroupRef DG(DefaultCBuffer);
3066 SemaRef.Consumer.HandleTopLevelDecl(DG);
3067 }
3068 diagnoseAvailabilityViolations(TU);
3069}
3070
3071// For resource member access through a global struct array, verify that the
3072// array index selecting the struct element is a constant integer expression.
3073// Returns false if the member expression is invalid.
3075 assert((ME->getType()->isHLSLResourceRecord() ||
3077 "expected member expr to have resource record type or array of them");
3078
3079 // Walk the AST from MemberExpr to the VarDecl of the parent struct instance
3080 // and take note of any non-constant array indexing along the way. If the
3081 // VarDecl we find is a global variable, report error if there was any
3082 // non-constant array index in the resource member access along the way.
3083 const Expr *NonConstIndexExpr = nullptr;
3084 const Expr *E = ME->getBase();
3085 while (E) {
3086 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
3087 if (!NonConstIndexExpr)
3088 return true;
3089
3090 const VarDecl *VD = cast<VarDecl>(DRE->getDecl());
3091 if (!VD->hasGlobalStorage())
3092 return true;
3093
3094 SemaRef.Diag(NonConstIndexExpr->getExprLoc(),
3095 diag::err_hlsl_resource_member_array_access_not_constant);
3096 return false;
3097 }
3098
3099 if (const auto *ASE = dyn_cast<ArraySubscriptExpr>(E)) {
3100 const Expr *IdxExpr = ASE->getIdx();
3101 if (!IdxExpr->isIntegerConstantExpr(SemaRef.getASTContext()))
3102 NonConstIndexExpr = IdxExpr;
3103 E = ASE->getBase();
3104 } else if (const auto *SubME = dyn_cast<MemberExpr>(E)) {
3105 E = SubME->getBase();
3106 } else if (const auto *ICE = dyn_cast<ImplicitCastExpr>(E)) {
3107 E = ICE->getSubExpr();
3108 } else {
3109 llvm_unreachable("unexpected expr type in resource member access");
3110 }
3111 }
3112 return true;
3113}
3114
3115void SemaHLSL::diagnoseAvailabilityViolations(TranslationUnitDecl *TU) {
3116 // Skip running the diagnostics scan if the diagnostic mode is
3117 // strict (-fhlsl-strict-availability) and the target shader stage is known
3118 // because all relevant diagnostics were already emitted in the
3119 // DiagnoseUnguardedAvailability scan (SemaAvailability.cpp).
3121 if (SemaRef.getLangOpts().HLSLStrictAvailability &&
3122 TI.getTriple().getEnvironment() != llvm::Triple::EnvironmentType::Library)
3123 return;
3124
3125 DiagnoseHLSLAvailability(SemaRef).RunOnTranslationUnit(TU);
3126}
3127
3128static bool CheckAllArgsHaveSameType(Sema *S, CallExpr *TheCall) {
3129 assert(TheCall->getNumArgs() > 1);
3130 QualType ArgTy0 = TheCall->getArg(0)->getType();
3131
3132 for (unsigned I = 1, N = TheCall->getNumArgs(); I < N; ++I) {
3134 ArgTy0, TheCall->getArg(I)->getType())) {
3135 S->Diag(TheCall->getBeginLoc(), diag::err_vec_builtin_incompatible_vector)
3136 << TheCall->getDirectCallee() << /*useAllTerminology*/ true
3137 << SourceRange(TheCall->getArg(0)->getBeginLoc(),
3138 TheCall->getArg(N - 1)->getEndLoc());
3139 return true;
3140 }
3141 }
3142 return false;
3143}
3144
3146 QualType ArgType = Arg->getType();
3148 S->Diag(Arg->getBeginLoc(), diag::err_typecheck_convert_incompatible)
3149 << ArgType << ExpectedType << 1 << 0 << 0;
3150 return true;
3151 }
3152 return false;
3153}
3154
3156 Sema *S, CallExpr *TheCall,
3157 llvm::function_ref<bool(Sema *S, SourceLocation Loc, int ArgOrdinal,
3158 clang::QualType PassedType)>
3159 Check) {
3160 for (unsigned I = 0; I < TheCall->getNumArgs(); ++I) {
3161 Expr *Arg = TheCall->getArg(I);
3162 if (Check(S, Arg->getBeginLoc(), I + 1, Arg->getType()))
3163 return true;
3164 }
3165 return false;
3166}
3167
3169 int ArgOrdinal,
3170 clang::QualType PassedType) {
3171 clang::QualType BaseType =
3172 PassedType->isVectorType()
3173 ? PassedType->castAs<clang::VectorType>()->getElementType()
3174 : PassedType;
3175 if (!BaseType->isFloat32Type())
3176 return S->Diag(Loc, diag::err_builtin_invalid_arg_type)
3177 << ArgOrdinal << /* scalar or vector of */ 5 << /* no int */ 0
3178 << /* float */ 1 << PassedType;
3179 return false;
3180}
3181
3183 int ArgOrdinal,
3184 clang::QualType PassedType) {
3185 clang::QualType BaseType =
3186 PassedType->isVectorType()
3187 ? PassedType->castAs<clang::VectorType>()->getElementType()
3188 : PassedType;
3189 if (!BaseType->isHalfType() && !BaseType->isFloat32Type())
3190 return S->Diag(Loc, diag::err_builtin_invalid_arg_type)
3191 << ArgOrdinal << /* scalar or vector of */ 5 << /* no int */ 0
3192 << /* half or float */ 2 << PassedType;
3193 return false;
3194}
3195
3197 int ArgOrdinal,
3198 clang::QualType PassedType) {
3199 clang::QualType BaseType =
3200 PassedType->isVectorType()
3201 ? PassedType->castAs<clang::VectorType>()->getElementType()
3202 : PassedType->isMatrixType()
3203 ? PassedType->castAs<clang::MatrixType>()->getElementType()
3204 : PassedType;
3205 if (!BaseType->isDoubleType()) {
3206 // FIXME: adopt standard `err_builtin_invalid_arg_type` instead of using
3207 // this custom error.
3208 return S->Diag(Loc, diag::err_builtin_requires_double_type)
3209 << ArgOrdinal << PassedType;
3210 }
3211
3212 return false;
3213}
3214
3215static bool CheckModifiableLValue(Sema *S, CallExpr *TheCall,
3216 unsigned ArgIndex) {
3217 auto *Arg = TheCall->getArg(ArgIndex);
3218 SourceLocation OrigLoc = Arg->getExprLoc();
3219 if (Arg->IgnoreCasts()->isModifiableLvalue(S->Context, &OrigLoc) ==
3221 return false;
3222 S->Diag(OrigLoc, diag::error_hlsl_inout_lvalue) << Arg << 0;
3223 return true;
3224}
3225
3226static bool CheckNoDoubleVectors(Sema *S, SourceLocation Loc, int ArgOrdinal,
3227 clang::QualType PassedType) {
3228 const auto *VecTy = PassedType->getAs<VectorType>();
3229 if (!VecTy)
3230 return false;
3231
3232 if (VecTy->getElementType()->isDoubleType())
3233 return S->Diag(Loc, diag::err_builtin_invalid_arg_type)
3234 << ArgOrdinal << /* scalar */ 1 << /* no int */ 0 << /* fp */ 1
3235 << PassedType;
3236 return false;
3237}
3238
3240 int ArgOrdinal,
3241 clang::QualType PassedType) {
3242 if (!PassedType->hasIntegerRepresentation() &&
3243 !PassedType->hasFloatingRepresentation())
3244 return S->Diag(Loc, diag::err_builtin_invalid_arg_type)
3245 << ArgOrdinal << /* scalar or vector of */ 5 << /* integer */ 1
3246 << /* fp */ 1 << PassedType;
3247 return false;
3248}
3249
3251 int ArgOrdinal,
3252 clang::QualType PassedType) {
3253 if (auto *VecTy = PassedType->getAs<VectorType>())
3254 if (VecTy->getElementType()->isUnsignedIntegerType())
3255 return false;
3256
3257 return S->Diag(Loc, diag::err_builtin_invalid_arg_type)
3258 << ArgOrdinal << /* vector of */ 4 << /* uint */ 3 << /* no fp */ 0
3259 << PassedType;
3260}
3261
3262// checks for unsigned ints of all sizes
3264 int ArgOrdinal,
3265 clang::QualType PassedType) {
3266 if (!PassedType->hasUnsignedIntegerRepresentation())
3267 return S->Diag(Loc, diag::err_builtin_invalid_arg_type)
3268 << ArgOrdinal << /* scalar or vector of */ 5 << /* unsigned int */ 3
3269 << /* no fp */ 0 << PassedType;
3270 return false;
3271}
3272
3273static bool CheckExpectedBitWidth(Sema *S, CallExpr *TheCall,
3274 unsigned ArgOrdinal, unsigned Width) {
3275 QualType ArgTy = TheCall->getArg(0)->getType();
3276 if (auto *VTy = ArgTy->getAs<VectorType>())
3277 ArgTy = VTy->getElementType();
3278 // ensure arg type has expected bit width
3279 uint64_t ElementBitCount =
3281 if (ElementBitCount != Width) {
3282 S->Diag(TheCall->getArg(0)->getBeginLoc(),
3283 diag::err_integer_incorrect_bit_count)
3284 << Width << ElementBitCount;
3285 return true;
3286 }
3287 return false;
3288}
3289
3291 QualType ReturnType) {
3292 auto *VecTyA = TheCall->getArg(0)->getType()->getAs<VectorType>();
3293 if (VecTyA)
3294 ReturnType =
3295 S->Context.getExtVectorType(ReturnType, VecTyA->getNumElements());
3296
3297 TheCall->setType(ReturnType);
3298}
3299
3300static bool CheckScalarOrVector(Sema *S, CallExpr *TheCall, QualType Scalar,
3301 unsigned ArgIndex) {
3302 assert(TheCall->getNumArgs() >= ArgIndex);
3303 QualType ArgType = TheCall->getArg(ArgIndex)->getType();
3304 auto *VTy = ArgType->getAs<VectorType>();
3305 // not the scalar or vector<scalar>
3306 if (!(S->Context.hasSameUnqualifiedType(ArgType, Scalar) ||
3307 (VTy &&
3308 S->Context.hasSameUnqualifiedType(VTy->getElementType(), Scalar)))) {
3309 S->Diag(TheCall->getArg(0)->getBeginLoc(),
3310 diag::err_typecheck_expect_scalar_or_vector)
3311 << ArgType << Scalar;
3312 return true;
3313 }
3314 return false;
3315}
3316
3318 QualType Scalar, unsigned ArgIndex) {
3319 assert(TheCall->getNumArgs() > ArgIndex);
3320
3321 Expr *Arg = TheCall->getArg(ArgIndex);
3322 QualType ArgType = Arg->getType();
3323
3324 // Scalar: T
3325 if (S->Context.hasSameUnqualifiedType(ArgType, Scalar))
3326 return false;
3327
3328 // Vector: vector<T>
3329 if (const auto *VTy = ArgType->getAs<VectorType>()) {
3330 if (S->Context.hasSameUnqualifiedType(VTy->getElementType(), Scalar))
3331 return false;
3332 }
3333
3334 // Matrix: ConstantMatrixType with element type T
3335 if (const auto *MTy = ArgType->getAs<ConstantMatrixType>()) {
3336 if (S->Context.hasSameUnqualifiedType(MTy->getElementType(), Scalar))
3337 return false;
3338 }
3339
3340 // Not a scalar/vector/matrix-of-scalar
3341 S->Diag(Arg->getBeginLoc(),
3342 diag::err_typecheck_expect_scalar_or_vector_or_matrix)
3343 << ArgType << Scalar;
3344 return true;
3345}
3346
3347static bool CheckAnyScalarOrVector(Sema *S, CallExpr *TheCall,
3348 unsigned ArgIndex) {
3349 assert(TheCall->getNumArgs() >= ArgIndex);
3350 QualType ArgType = TheCall->getArg(ArgIndex)->getType();
3351 auto *VTy = ArgType->getAs<VectorType>();
3352 // not the scalar or vector<scalar>
3353 if (!(ArgType->isScalarType() ||
3354 (VTy && VTy->getElementType()->isScalarType()))) {
3355 S->Diag(TheCall->getArg(0)->getBeginLoc(),
3356 diag::err_typecheck_expect_any_scalar_or_vector)
3357 << ArgType << 1;
3358 return true;
3359 }
3360 return false;
3361}
3362
3363// Check that the argument is not a bool or vector<bool>
3364// Returns true on error
3366 unsigned ArgIndex) {
3367 QualType BoolType = S->getASTContext().BoolTy;
3368 assert(ArgIndex < TheCall->getNumArgs());
3369 QualType ArgType = TheCall->getArg(ArgIndex)->getType();
3370 auto *VTy = ArgType->getAs<VectorType>();
3371 // is the bool or vector<bool>
3372 if (S->Context.hasSameUnqualifiedType(ArgType, BoolType) ||
3373 (VTy &&
3374 S->Context.hasSameUnqualifiedType(VTy->getElementType(), BoolType))) {
3375 S->Diag(TheCall->getArg(0)->getBeginLoc(),
3376 diag::err_typecheck_expect_any_scalar_or_vector)
3377 << ArgType << 0;
3378 return true;
3379 }
3380 return false;
3381}
3382
3383static bool CheckWaveActive(Sema *S, CallExpr *TheCall) {
3384 if (CheckNotBoolScalarOrVector(S, TheCall, 0))
3385 return true;
3386 return false;
3387}
3388
3389static bool CheckWavePrefix(Sema *S, CallExpr *TheCall) {
3390 if (CheckNotBoolScalarOrVector(S, TheCall, 0))
3391 return true;
3392 return false;
3393}
3394
3395static bool CheckBoolSelect(Sema *S, CallExpr *TheCall) {
3396 assert(TheCall->getNumArgs() == 3);
3397 Expr *Arg1 = TheCall->getArg(1);
3398 Expr *Arg2 = TheCall->getArg(2);
3399 if (!S->Context.hasSameUnqualifiedType(Arg1->getType(), Arg2->getType())) {
3400 S->Diag(TheCall->getBeginLoc(),
3401 diag::err_typecheck_call_different_arg_types)
3402 << Arg1->getType() << Arg2->getType() << Arg1->getSourceRange()
3403 << Arg2->getSourceRange();
3404 return true;
3405 }
3406
3407 TheCall->setType(Arg1->getType());
3408 return false;
3409}
3410
3411static bool CheckVectorSelect(Sema *S, CallExpr *TheCall) {
3412 assert(TheCall->getNumArgs() == 3);
3413 Expr *Arg1 = TheCall->getArg(1);
3414 QualType Arg1Ty = Arg1->getType();
3415 Expr *Arg2 = TheCall->getArg(2);
3416 QualType Arg2Ty = Arg2->getType();
3417
3418 QualType Arg1ScalarTy = Arg1Ty;
3419 if (auto VTy = Arg1ScalarTy->getAs<VectorType>())
3420 Arg1ScalarTy = VTy->getElementType();
3421
3422 QualType Arg2ScalarTy = Arg2Ty;
3423 if (auto VTy = Arg2ScalarTy->getAs<VectorType>())
3424 Arg2ScalarTy = VTy->getElementType();
3425
3426 if (!S->Context.hasSameUnqualifiedType(Arg1ScalarTy, Arg2ScalarTy))
3427 S->Diag(Arg1->getBeginLoc(), diag::err_hlsl_builtin_scalar_vector_mismatch)
3428 << /* second and third */ 1 << TheCall->getCallee() << Arg1Ty << Arg2Ty;
3429
3430 QualType Arg0Ty = TheCall->getArg(0)->getType();
3431 unsigned Arg0Length = Arg0Ty->getAs<VectorType>()->getNumElements();
3432 unsigned Arg1Length = Arg1Ty->isVectorType()
3433 ? Arg1Ty->getAs<VectorType>()->getNumElements()
3434 : 0;
3435 unsigned Arg2Length = Arg2Ty->isVectorType()
3436 ? Arg2Ty->getAs<VectorType>()->getNumElements()
3437 : 0;
3438 if (Arg1Length > 0 && Arg0Length != Arg1Length) {
3439 S->Diag(TheCall->getBeginLoc(),
3440 diag::err_typecheck_vector_lengths_not_equal)
3441 << Arg0Ty << Arg1Ty << TheCall->getArg(0)->getSourceRange()
3442 << Arg1->getSourceRange();
3443 return true;
3444 }
3445
3446 if (Arg2Length > 0 && Arg0Length != Arg2Length) {
3447 S->Diag(TheCall->getBeginLoc(),
3448 diag::err_typecheck_vector_lengths_not_equal)
3449 << Arg0Ty << Arg2Ty << TheCall->getArg(0)->getSourceRange()
3450 << Arg2->getSourceRange();
3451 return true;
3452 }
3453
3454 TheCall->setType(
3455 S->getASTContext().getExtVectorType(Arg1ScalarTy, Arg0Length));
3456 return false;
3457}
3458
3459static bool CheckIndexType(Sema *S, CallExpr *TheCall, unsigned IndexArgIndex) {
3460 assert(TheCall->getNumArgs() > IndexArgIndex && "Index argument missing");
3461 QualType ArgType = TheCall->getArg(IndexArgIndex)->getType();
3462 QualType IndexTy = ArgType;
3463 unsigned int ActualDim = 1;
3464 if (const auto *VTy = IndexTy->getAs<VectorType>()) {
3465 ActualDim = VTy->getNumElements();
3466 IndexTy = VTy->getElementType();
3467 }
3468 if (!IndexTy->isIntegerType()) {
3469 S->Diag(TheCall->getArg(IndexArgIndex)->getBeginLoc(),
3470 diag::err_typecheck_expect_int)
3471 << ArgType;
3472 return true;
3473 }
3474
3475 QualType ResourceArgTy = TheCall->getArg(0)->getType();
3476 const HLSLAttributedResourceType *ResTy =
3477 ResourceArgTy.getTypePtr()->getAs<HLSLAttributedResourceType>();
3478 assert(ResTy && "Resource argument must be a resource");
3479 HLSLAttributedResourceType::Attributes ResAttrs = ResTy->getAttrs();
3480
3481 unsigned int ExpectedDim = 1;
3482 if (ResAttrs.ResourceDimension != llvm::dxil::ResourceDimension::Unknown)
3483 ExpectedDim = getResourceDimensions(ResAttrs.ResourceDimension);
3484
3485 if (ActualDim != ExpectedDim) {
3486 S->Diag(TheCall->getArg(IndexArgIndex)->getBeginLoc(),
3487 diag::err_hlsl_builtin_resource_coordinate_dimension_mismatch)
3488 << cast<NamedDecl>(TheCall->getCalleeDecl()) << ExpectedDim
3489 << ActualDim;
3490 return true;
3491 }
3492
3493 return false;
3494}
3495
3497 Sema *S, CallExpr *TheCall, unsigned ArgIndex,
3498 llvm::function_ref<bool(const HLSLAttributedResourceType *ResType)> Check =
3499 nullptr) {
3500 assert(TheCall->getNumArgs() >= ArgIndex);
3501 QualType ArgType = TheCall->getArg(ArgIndex)->getType();
3502 const HLSLAttributedResourceType *ResTy =
3503 ArgType.getTypePtr()->getAs<HLSLAttributedResourceType>();
3504 if (!ResTy) {
3505 S->Diag(TheCall->getArg(ArgIndex)->getBeginLoc(),
3506 diag::err_typecheck_expect_hlsl_resource)
3507 << ArgType;
3508 return true;
3509 }
3510 if (Check && Check(ResTy)) {
3511 S->Diag(TheCall->getArg(ArgIndex)->getExprLoc(),
3512 diag::err_invalid_hlsl_resource_type)
3513 << ArgType;
3514 return true;
3515 }
3516 return false;
3517}
3518
3519static bool CheckVectorElementCount(Sema *S, QualType PassedType,
3520 QualType BaseType, unsigned ExpectedCount,
3521 SourceLocation Loc) {
3522 unsigned PassedCount = 1;
3523 if (const auto *VecTy = PassedType->getAs<VectorType>())
3524 PassedCount = VecTy->getNumElements();
3525
3526 if (PassedCount != ExpectedCount) {
3528 S->Context.getExtVectorType(BaseType, ExpectedCount);
3529 S->Diag(Loc, diag::err_typecheck_convert_incompatible)
3530 << PassedType << ExpectedType << 1 << 0 << 0;
3531 return true;
3532 }
3533 return false;
3534}
3535
3536enum class SampleKind { Sample, Bias, Grad, Level, Cmp, CmpLevelZero };
3537
3539 // Check the texture handle.
3540 if (CheckResourceHandle(&S, TheCall, 0,
3541 [](const HLSLAttributedResourceType *ResType) {
3542 return ResType->getAttrs().ResourceDimension ==
3543 llvm::dxil::ResourceDimension::Unknown;
3544 }))
3545 return true;
3546
3547 // Check the sampler handle.
3548 if (CheckResourceHandle(&S, TheCall, 1,
3549 [](const HLSLAttributedResourceType *ResType) {
3550 return ResType->getAttrs().ResourceClass !=
3551 llvm::hlsl::ResourceClass::Sampler;
3552 }))
3553 return true;
3554
3555 auto *ResourceTy =
3556 TheCall->getArg(0)->getType()->castAs<HLSLAttributedResourceType>();
3557
3558 // Check the location.
3559 unsigned ExpectedDim =
3560 getResourceDimensions(ResourceTy->getAttrs().ResourceDimension);
3561 if (CheckVectorElementCount(&S, TheCall->getArg(2)->getType(),
3562 S.Context.FloatTy, ExpectedDim,
3563 TheCall->getBeginLoc()))
3564 return true;
3565
3566 return false;
3567}
3568
3569static bool CheckCalculateLodBuiltin(Sema &S, CallExpr *TheCall) {
3570 if (S.checkArgCount(TheCall, 3))
3571 return true;
3572
3573 if (CheckTextureSamplerAndLocation(S, TheCall))
3574 return true;
3575
3576 TheCall->setType(S.Context.FloatTy);
3577 return false;
3578}
3579
3580static bool CheckGatherBuiltin(Sema &S, CallExpr *TheCall, bool IsCmp) {
3581 if (S.checkArgCountRange(TheCall, IsCmp ? 5 : 4, IsCmp ? 6 : 5))
3582 return true;
3583
3584 if (CheckTextureSamplerAndLocation(S, TheCall))
3585 return true;
3586
3587 unsigned NextIdx = 3;
3588 if (IsCmp) {
3589 // Check the compare value.
3590 QualType CmpTy = TheCall->getArg(NextIdx)->getType();
3591 if (!CmpTy->isFloatingType() || CmpTy->isVectorType()) {
3592 S.Diag(TheCall->getArg(NextIdx)->getBeginLoc(),
3593 diag::err_typecheck_convert_incompatible)
3594 << CmpTy << S.Context.FloatTy << 1 << 0 << 0;
3595 return true;
3596 }
3597 NextIdx++;
3598 }
3599
3600 // Check the component operand.
3601 Expr *ComponentArg = TheCall->getArg(NextIdx);
3602 QualType ComponentTy = ComponentArg->getType();
3603 if (!ComponentTy->isIntegerType() || ComponentTy->isVectorType()) {
3604 S.Diag(ComponentArg->getBeginLoc(),
3605 diag::err_typecheck_convert_incompatible)
3606 << ComponentTy << S.Context.UnsignedIntTy << 1 << 0 << 0;
3607 return true;
3608 }
3609
3610 // GatherCmp operations on Vulkan target must use component 0 (Red).
3611 if (IsCmp && S.getASTContext().getTargetInfo().getTriple().isSPIRV()) {
3612 std::optional<llvm::APSInt> ComponentOpt =
3613 ComponentArg->getIntegerConstantExpr(S.getASTContext());
3614 if (ComponentOpt) {
3615 int64_t ComponentVal = ComponentOpt->getSExtValue();
3616 if (ComponentVal != 0) {
3617 // Issue an error if the component is not 0 (Red).
3618 // 0 -> Red, 1 -> Green, 2 -> Blue, 3 -> Alpha
3619 assert(ComponentVal >= 0 && ComponentVal <= 3 &&
3620 "The component is not in the expected range.");
3621 S.Diag(ComponentArg->getBeginLoc(),
3622 diag::err_hlsl_gathercmp_invalid_component)
3623 << ComponentVal;
3624 return true;
3625 }
3626 }
3627 }
3628
3629 NextIdx++;
3630
3631 // Check the offset operand.
3632 const HLSLAttributedResourceType *ResourceTy =
3633 TheCall->getArg(0)->getType()->castAs<HLSLAttributedResourceType>();
3634 if (TheCall->getNumArgs() > NextIdx) {
3635 unsigned ExpectedDim =
3636 getResourceDimensions(ResourceTy->getAttrs().ResourceDimension);
3637 if (CheckVectorElementCount(&S, TheCall->getArg(NextIdx)->getType(),
3638 S.Context.IntTy, ExpectedDim,
3639 TheCall->getArg(NextIdx)->getBeginLoc()))
3640 return true;
3641 NextIdx++;
3642 }
3643
3644 assert(ResourceTy->hasContainedType() &&
3645 "Expecting a contained type for resource with a dimension "
3646 "attribute.");
3647 QualType ReturnType = ResourceTy->getContainedType();
3648
3649 if (IsCmp) {
3650 if (!ReturnType->hasFloatingRepresentation()) {
3651 S.Diag(TheCall->getBeginLoc(), diag::err_hlsl_samplecmp_requires_float);
3652 return true;
3653 }
3654 }
3655
3656 if (const auto *VecTy = ReturnType->getAs<VectorType>())
3657 ReturnType = VecTy->getElementType();
3658 ReturnType = S.Context.getExtVectorType(ReturnType, 4);
3659
3660 TheCall->setType(ReturnType);
3661
3662 return false;
3663}
3664static bool CheckLoadLevelBuiltin(Sema &S, CallExpr *TheCall) {
3665 if (S.checkArgCountRange(TheCall, 2, 3))
3666 return true;
3667
3668 // Check the texture handle.
3669 if (CheckResourceHandle(&S, TheCall, 0,
3670 [](const HLSLAttributedResourceType *ResType) {
3671 return ResType->getAttrs().ResourceDimension ==
3672 llvm::dxil::ResourceDimension::Unknown;
3673 }))
3674 return true;
3675
3676 auto *ResourceTy =
3677 TheCall->getArg(0)->getType()->castAs<HLSLAttributedResourceType>();
3678
3679 // Check the location + lod (int3 for Texture2D).
3680 unsigned ExpectedDim =
3681 getResourceDimensions(ResourceTy->getAttrs().ResourceDimension);
3682 QualType CoordLODTy = TheCall->getArg(1)->getType();
3683 if (CheckVectorElementCount(&S, CoordLODTy, S.Context.IntTy, ExpectedDim + 1,
3684 TheCall->getArg(1)->getBeginLoc()))
3685 return true;
3686
3687 QualType EltTy = CoordLODTy;
3688 if (const auto *VTy = EltTy->getAs<VectorType>())
3689 EltTy = VTy->getElementType();
3690 if (!EltTy->isIntegerType()) {
3691 S.Diag(TheCall->getArg(1)->getBeginLoc(), diag::err_typecheck_expect_int)
3692 << CoordLODTy;
3693 return true;
3694 }
3695
3696 // Check the offset operand.
3697 if (TheCall->getNumArgs() > 2) {
3698 if (CheckVectorElementCount(&S, TheCall->getArg(2)->getType(),
3699 S.Context.IntTy, ExpectedDim,
3700 TheCall->getArg(2)->getBeginLoc()))
3701 return true;
3702 }
3703
3704 TheCall->setType(ResourceTy->getContainedType());
3705 return false;
3706}
3707
3708static bool CheckSamplingBuiltin(Sema &S, CallExpr *TheCall, SampleKind Kind) {
3709 unsigned MinArgs, MaxArgs;
3710 if (Kind == SampleKind::Sample) {
3711 MinArgs = 3;
3712 MaxArgs = 5;
3713 } else if (Kind == SampleKind::Bias) {
3714 MinArgs = 4;
3715 MaxArgs = 6;
3716 } else if (Kind == SampleKind::Grad) {
3717 MinArgs = 5;
3718 MaxArgs = 7;
3719 } else if (Kind == SampleKind::Level) {
3720 MinArgs = 4;
3721 MaxArgs = 5;
3722 } else if (Kind == SampleKind::Cmp) {
3723 MinArgs = 4;
3724 MaxArgs = 6;
3725 } else {
3726 assert(Kind == SampleKind::CmpLevelZero);
3727 MinArgs = 4;
3728 MaxArgs = 5;
3729 }
3730
3731 if (S.checkArgCountRange(TheCall, MinArgs, MaxArgs))
3732 return true;
3733
3734 if (CheckTextureSamplerAndLocation(S, TheCall))
3735 return true;
3736
3737 const HLSLAttributedResourceType *ResourceTy =
3738 TheCall->getArg(0)->getType()->castAs<HLSLAttributedResourceType>();
3739 unsigned ExpectedDim =
3740 getResourceDimensions(ResourceTy->getAttrs().ResourceDimension);
3741
3742 unsigned NextIdx = 3;
3743 if (Kind == SampleKind::Bias || Kind == SampleKind::Level ||
3744 Kind == SampleKind::Cmp || Kind == SampleKind::CmpLevelZero) {
3745 // Check the bias, lod level, or compare value, depending on the kind.
3746 // All of them must be a scalar float value.
3747 QualType BiasOrLODOrCmpTy = TheCall->getArg(NextIdx)->getType();
3748 if (!BiasOrLODOrCmpTy->isFloatingType() ||
3749 BiasOrLODOrCmpTy->isVectorType()) {
3750 S.Diag(TheCall->getArg(NextIdx)->getBeginLoc(),
3751 diag::err_typecheck_convert_incompatible)
3752 << BiasOrLODOrCmpTy << S.Context.FloatTy << 1 << 0 << 0;
3753 return true;
3754 }
3755 NextIdx++;
3756 } else if (Kind == SampleKind::Grad) {
3757 // Check the DDX operand.
3758 if (CheckVectorElementCount(&S, TheCall->getArg(NextIdx)->getType(),
3759 S.Context.FloatTy, ExpectedDim,
3760 TheCall->getArg(NextIdx)->getBeginLoc()))
3761 return true;
3762
3763 // Check the DDY operand.
3764 if (CheckVectorElementCount(&S, TheCall->getArg(NextIdx + 1)->getType(),
3765 S.Context.FloatTy, ExpectedDim,
3766 TheCall->getArg(NextIdx + 1)->getBeginLoc()))
3767 return true;
3768 NextIdx += 2;
3769 }
3770
3771 // Check the offset operand.
3772 if (TheCall->getNumArgs() > NextIdx) {
3773 if (CheckVectorElementCount(&S, TheCall->getArg(NextIdx)->getType(),
3774 S.Context.IntTy, ExpectedDim,
3775 TheCall->getArg(NextIdx)->getBeginLoc()))
3776 return true;
3777 NextIdx++;
3778 }
3779
3780 // Check the clamp operand.
3781 if (Kind != SampleKind::Level && Kind != SampleKind::CmpLevelZero &&
3782 TheCall->getNumArgs() > NextIdx) {
3783 QualType ClampTy = TheCall->getArg(NextIdx)->getType();
3784 if (!ClampTy->isFloatingType() || ClampTy->isVectorType()) {
3785 S.Diag(TheCall->getArg(NextIdx)->getBeginLoc(),
3786 diag::err_typecheck_convert_incompatible)
3787 << ClampTy << S.Context.FloatTy << 1 << 0 << 0;
3788 return true;
3789 }
3790 }
3791
3792 assert(ResourceTy->hasContainedType() &&
3793 "Expecting a contained type for resource with a dimension "
3794 "attribute.");
3795 QualType ReturnType = ResourceTy->getContainedType();
3796 if (Kind == SampleKind::Cmp || Kind == SampleKind::CmpLevelZero) {
3797 if (!ReturnType->hasFloatingRepresentation()) {
3798 S.Diag(TheCall->getBeginLoc(), diag::err_hlsl_samplecmp_requires_float);
3799 return true;
3800 }
3801 ReturnType = S.Context.FloatTy;
3802 }
3803 TheCall->setType(ReturnType);
3804
3805 return false;
3806}
3807
3808// Note: returning true in this case results in CheckBuiltinFunctionCall
3809// returning an ExprError
3810bool SemaHLSL::CheckBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) {
3811 switch (BuiltinID) {
3812 case Builtin::BI__builtin_hlsl_adduint64: {
3813 if (SemaRef.checkArgCount(TheCall, 2))
3814 return true;
3815
3816 if (CheckAllArgTypesAreCorrect(&SemaRef, TheCall,
3818 return true;
3819
3820 // ensure arg integers are 32-bits
3821 if (CheckExpectedBitWidth(&SemaRef, TheCall, 0, 32))
3822 return true;
3823
3824 // ensure both args are vectors of total bit size of a multiple of 64
3825 auto *VTy = TheCall->getArg(0)->getType()->getAs<VectorType>();
3826 int NumElementsArg = VTy->getNumElements();
3827 if (NumElementsArg != 2 && NumElementsArg != 4) {
3828 SemaRef.Diag(TheCall->getBeginLoc(), diag::err_vector_incorrect_bit_count)
3829 << 1 /*a multiple of*/ << 64 << NumElementsArg * 32;
3830 return true;
3831 }
3832
3833 // ensure first arg and second arg have the same type
3834 if (CheckAllArgsHaveSameType(&SemaRef, TheCall))
3835 return true;
3836
3837 ExprResult A = TheCall->getArg(0);
3838 QualType ArgTyA = A.get()->getType();
3839 // return type is the same as the input type
3840 TheCall->setType(ArgTyA);
3841 break;
3842 }
3843 case Builtin::BI__builtin_hlsl_resource_getpointer: {
3844 if (SemaRef.checkArgCount(TheCall, 2) ||
3845 CheckResourceHandle(&SemaRef, TheCall, 0) ||
3846 CheckIndexType(&SemaRef, TheCall, 1))
3847 return true;
3848
3849 auto *ResourceTy =
3850 TheCall->getArg(0)->getType()->castAs<HLSLAttributedResourceType>();
3851 QualType ContainedTy = ResourceTy->getContainedType();
3852 auto ReturnType =
3853 SemaRef.Context.getAddrSpaceQualType(ContainedTy, LangAS::hlsl_device);
3854 ReturnType = SemaRef.Context.getPointerType(ReturnType);
3855 TheCall->setType(ReturnType);
3856 TheCall->setValueKind(VK_LValue);
3857
3858 break;
3859 }
3860 case Builtin::BI__builtin_hlsl_resource_getpointer_typed: {
3861 if (SemaRef.checkArgCount(TheCall, 3) ||
3862 CheckResourceHandle(&SemaRef, TheCall, 0) ||
3863 CheckIndexType(&SemaRef, TheCall, 1))
3864 return true;
3865
3866 QualType ElementTy = TheCall->getArg(2)->getType();
3867 assert(ElementTy->isPointerType() &&
3868 "expected pointer type for second argument");
3869 ElementTy = ElementTy->getPointeeType();
3870
3871 // Reject array types
3872 if (ElementTy->isArrayType())
3873 return SemaRef.Diag(
3874 cast<FunctionDecl>(SemaRef.CurContext)->getPointOfInstantiation(),
3875 diag::err_invalid_use_of_array_type);
3876
3877 auto ReturnType =
3878 SemaRef.Context.getAddrSpaceQualType(ElementTy, LangAS::hlsl_device);
3879 ReturnType = SemaRef.Context.getPointerType(ReturnType);
3880 TheCall->setType(ReturnType);
3881
3882 break;
3883 }
3884 case Builtin::BI__builtin_hlsl_resource_load_with_status: {
3885 if (SemaRef.checkArgCount(TheCall, 3) ||
3886 CheckResourceHandle(&SemaRef, TheCall, 0) ||
3887 CheckArgTypeMatches(&SemaRef, TheCall->getArg(1),
3888 SemaRef.getASTContext().UnsignedIntTy) ||
3889 CheckArgTypeMatches(&SemaRef, TheCall->getArg(2),
3890 SemaRef.getASTContext().UnsignedIntTy) ||
3891 CheckModifiableLValue(&SemaRef, TheCall, 2))
3892 return true;
3893
3894 auto *ResourceTy =
3895 TheCall->getArg(0)->getType()->castAs<HLSLAttributedResourceType>();
3896 QualType ReturnType = ResourceTy->getContainedType();
3897 TheCall->setType(ReturnType);
3898
3899 break;
3900 }
3901 case Builtin::BI__builtin_hlsl_resource_load_with_status_typed: {
3902 if (SemaRef.checkArgCount(TheCall, 4) ||
3903 CheckResourceHandle(&SemaRef, TheCall, 0) ||
3904 CheckArgTypeMatches(&SemaRef, TheCall->getArg(1),
3905 SemaRef.getASTContext().UnsignedIntTy) ||
3906 CheckArgTypeMatches(&SemaRef, TheCall->getArg(2),
3907 SemaRef.getASTContext().UnsignedIntTy) ||
3908 CheckModifiableLValue(&SemaRef, TheCall, 2))
3909 return true;
3910
3911 QualType ReturnType = TheCall->getArg(3)->getType();
3912 assert(ReturnType->isPointerType() &&
3913 "expected pointer type for second argument");
3914 ReturnType = ReturnType->getPointeeType();
3915
3916 // Reject array types
3917 if (ReturnType->isArrayType())
3918 return SemaRef.Diag(
3919 cast<FunctionDecl>(SemaRef.CurContext)->getPointOfInstantiation(),
3920 diag::err_invalid_use_of_array_type);
3921
3922 TheCall->setType(ReturnType);
3923
3924 break;
3925 }
3926 case Builtin::BI__builtin_hlsl_resource_load_level:
3927 return CheckLoadLevelBuiltin(SemaRef, TheCall);
3928 case Builtin::BI__builtin_hlsl_resource_sample:
3930 case Builtin::BI__builtin_hlsl_resource_sample_bias:
3932 case Builtin::BI__builtin_hlsl_resource_sample_grad:
3934 case Builtin::BI__builtin_hlsl_resource_sample_level:
3936 case Builtin::BI__builtin_hlsl_resource_sample_cmp:
3938 case Builtin::BI__builtin_hlsl_resource_sample_cmp_level_zero:
3940 case Builtin::BI__builtin_hlsl_resource_calculate_lod:
3941 case Builtin::BI__builtin_hlsl_resource_calculate_lod_unclamped:
3942 return CheckCalculateLodBuiltin(SemaRef, TheCall);
3943 case Builtin::BI__builtin_hlsl_resource_gather:
3944 return CheckGatherBuiltin(SemaRef, TheCall, /*IsCmp=*/false);
3945 case Builtin::BI__builtin_hlsl_resource_gather_cmp:
3946 return CheckGatherBuiltin(SemaRef, TheCall, /*IsCmp=*/true);
3947 case Builtin::BI__builtin_hlsl_resource_uninitializedhandle: {
3948 assert(TheCall->getNumArgs() == 1 && "expected 1 arg");
3949 // Update return type to be the attributed resource type from arg0.
3950 QualType ResourceTy = TheCall->getArg(0)->getType();
3951 TheCall->setType(ResourceTy);
3952 break;
3953 }
3954 case Builtin::BI__builtin_hlsl_resource_handlefrombinding: {
3955 assert(TheCall->getNumArgs() == 6 && "expected 6 args");
3956 // Update return type to be the attributed resource type from arg0.
3957 QualType ResourceTy = TheCall->getArg(0)->getType();
3958 TheCall->setType(ResourceTy);
3959 break;
3960 }
3961 case Builtin::BI__builtin_hlsl_resource_handlefromimplicitbinding: {
3962 assert(TheCall->getNumArgs() == 6 && "expected 6 args");
3963 // Update return type to be the attributed resource type from arg0.
3964 QualType ResourceTy = TheCall->getArg(0)->getType();
3965 TheCall->setType(ResourceTy);
3966 break;
3967 }
3968 case Builtin::BI__builtin_hlsl_resource_counterhandlefromimplicitbinding: {
3969 assert(TheCall->getNumArgs() == 3 && "expected 3 args");
3970 ASTContext &AST = SemaRef.getASTContext();
3971 QualType MainHandleTy = TheCall->getArg(0)->getType();
3972 auto *MainResType = MainHandleTy->getAs<HLSLAttributedResourceType>();
3973 auto MainAttrs = MainResType->getAttrs();
3974 assert(!MainAttrs.IsCounter && "cannot create a counter from a counter");
3975 MainAttrs.IsCounter = true;
3976 QualType CounterHandleTy = AST.getHLSLAttributedResourceType(
3977 MainResType->getWrappedType(), MainResType->getContainedType(),
3978 MainAttrs);
3979 // Update return type to be the attributed resource type from arg0
3980 // with added IsCounter flag.
3981 TheCall->setType(CounterHandleTy);
3982 break;
3983 }
3984 case Builtin::BI__builtin_hlsl_and:
3985 case Builtin::BI__builtin_hlsl_or: {
3986 if (SemaRef.checkArgCount(TheCall, 2))
3987 return true;
3988 if (CheckScalarOrVectorOrMatrix(&SemaRef, TheCall, getASTContext().BoolTy,
3989 0))
3990 return true;
3991 if (CheckAllArgsHaveSameType(&SemaRef, TheCall))
3992 return true;
3993
3994 ExprResult A = TheCall->getArg(0);
3995 QualType ArgTyA = A.get()->getType();
3996 // return type is the same as the input type
3997 TheCall->setType(ArgTyA);
3998 break;
3999 }
4000 case Builtin::BI__builtin_hlsl_all:
4001 case Builtin::BI__builtin_hlsl_any: {
4002 if (SemaRef.checkArgCount(TheCall, 1))
4003 return true;
4004 if (CheckAnyScalarOrVector(&SemaRef, TheCall, 0))
4005 return true;
4006 break;
4007 }
4008 case Builtin::BI__builtin_hlsl_asdouble: {
4009 if (SemaRef.checkArgCount(TheCall, 2))
4010 return true;
4012 &SemaRef, TheCall,
4013 /*only check for uint*/ SemaRef.Context.UnsignedIntTy,
4014 /* arg index */ 0))
4015 return true;
4017 &SemaRef, TheCall,
4018 /*only check for uint*/ SemaRef.Context.UnsignedIntTy,
4019 /* arg index */ 1))
4020 return true;
4021 if (CheckAllArgsHaveSameType(&SemaRef, TheCall))
4022 return true;
4023
4024 SetElementTypeAsReturnType(&SemaRef, TheCall, getASTContext().DoubleTy);
4025 break;
4026 }
4027 case Builtin::BI__builtin_hlsl_elementwise_clamp: {
4028 if (SemaRef.BuiltinElementwiseTernaryMath(
4029 TheCall, /*ArgTyRestr=*/
4031 return true;
4032 break;
4033 }
4034 case Builtin::BI__builtin_hlsl_dot: {
4035 // arg count is checked by BuiltinVectorToScalarMath
4036 if (SemaRef.BuiltinVectorToScalarMath(TheCall))
4037 return true;
4039 return true;
4040 break;
4041 }
4042 case Builtin::BI__builtin_hlsl_elementwise_firstbithigh:
4043 case Builtin::BI__builtin_hlsl_elementwise_firstbitlow: {
4044 if (SemaRef.PrepareBuiltinElementwiseMathOneArgCall(TheCall))
4045 return true;
4046
4047 const Expr *Arg = TheCall->getArg(0);
4048 QualType ArgTy = Arg->getType();
4049 QualType EltTy = ArgTy;
4050
4051 QualType ResTy = SemaRef.Context.UnsignedIntTy;
4052
4053 if (auto *VecTy = EltTy->getAs<VectorType>()) {
4054 EltTy = VecTy->getElementType();
4055 ResTy = SemaRef.Context.getExtVectorType(ResTy, VecTy->getNumElements());
4056 }
4057
4058 if (!EltTy->isIntegerType()) {
4059 Diag(Arg->getBeginLoc(), diag::err_builtin_invalid_arg_type)
4060 << 1 << /* scalar or vector of */ 5 << /* integer ty */ 1
4061 << /* no fp */ 0 << ArgTy;
4062 return true;
4063 }
4064
4065 TheCall->setType(ResTy);
4066 break;
4067 }
4068 case Builtin::BI__builtin_hlsl_select: {
4069 if (SemaRef.checkArgCount(TheCall, 3))
4070 return true;
4071 if (CheckScalarOrVector(&SemaRef, TheCall, getASTContext().BoolTy, 0))
4072 return true;
4073 QualType ArgTy = TheCall->getArg(0)->getType();
4074 if (ArgTy->isBooleanType() && CheckBoolSelect(&SemaRef, TheCall))
4075 return true;
4076 auto *VTy = ArgTy->getAs<VectorType>();
4077 if (VTy && VTy->getElementType()->isBooleanType() &&
4078 CheckVectorSelect(&SemaRef, TheCall))
4079 return true;
4080 break;
4081 }
4082 case Builtin::BI__builtin_hlsl_elementwise_saturate:
4083 case Builtin::BI__builtin_hlsl_elementwise_rcp: {
4084 if (SemaRef.checkArgCount(TheCall, 1))
4085 return true;
4086 if (!TheCall->getArg(0)
4087 ->getType()
4088 ->hasFloatingRepresentation()) // half or float or double
4089 return SemaRef.Diag(TheCall->getArg(0)->getBeginLoc(),
4090 diag::err_builtin_invalid_arg_type)
4091 << /* ordinal */ 1 << /* scalar or vector */ 5 << /* no int */ 0
4092 << /* fp */ 1 << TheCall->getArg(0)->getType();
4093 if (SemaRef.PrepareBuiltinElementwiseMathOneArgCall(TheCall))
4094 return true;
4095 break;
4096 }
4097 case Builtin::BI__builtin_hlsl_elementwise_degrees:
4098 case Builtin::BI__builtin_hlsl_elementwise_radians:
4099 case Builtin::BI__builtin_hlsl_elementwise_rsqrt:
4100 case Builtin::BI__builtin_hlsl_elementwise_frac:
4101 case Builtin::BI__builtin_hlsl_elementwise_ddx_coarse:
4102 case Builtin::BI__builtin_hlsl_elementwise_ddy_coarse:
4103 case Builtin::BI__builtin_hlsl_elementwise_ddx_fine:
4104 case Builtin::BI__builtin_hlsl_elementwise_ddy_fine: {
4105 if (SemaRef.checkArgCount(TheCall, 1))
4106 return true;
4107 if (CheckAllArgTypesAreCorrect(&SemaRef, TheCall,
4109 return true;
4110 if (SemaRef.PrepareBuiltinElementwiseMathOneArgCall(TheCall))
4111 return true;
4112 break;
4113 }
4114 case Builtin::BI__builtin_hlsl_elementwise_isinf:
4115 case Builtin::BI__builtin_hlsl_elementwise_isnan: {
4116 if (SemaRef.checkArgCount(TheCall, 1))
4117 return true;
4118 if (CheckAllArgTypesAreCorrect(&SemaRef, TheCall,
4120 return true;
4121 if (SemaRef.PrepareBuiltinElementwiseMathOneArgCall(TheCall))
4122 return true;
4124 break;
4125 }
4126 case Builtin::BI__builtin_hlsl_lerp: {
4127 if (SemaRef.checkArgCount(TheCall, 3))
4128 return true;
4129 if (CheckAllArgTypesAreCorrect(&SemaRef, TheCall,
4131 return true;
4132 if (CheckAllArgsHaveSameType(&SemaRef, TheCall))
4133 return true;
4134 if (SemaRef.BuiltinElementwiseTernaryMath(TheCall))
4135 return true;
4136 break;
4137 }
4138 case Builtin::BI__builtin_hlsl_mad: {
4139 if (SemaRef.BuiltinElementwiseTernaryMath(
4140 TheCall, /*ArgTyRestr=*/
4142 return true;
4143 break;
4144 }
4145 case Builtin::BI__builtin_hlsl_mul: {
4146 if (SemaRef.checkArgCount(TheCall, 2))
4147 return true;
4148
4149 Expr *Arg0 = TheCall->getArg(0);
4150 Expr *Arg1 = TheCall->getArg(1);
4151 QualType Ty0 = Arg0->getType();
4152 QualType Ty1 = Arg1->getType();
4153
4154 auto getElemType = [](QualType T) -> QualType {
4155 if (const auto *VTy = T->getAs<VectorType>())
4156 return VTy->getElementType();
4157 if (const auto *MTy = T->getAs<ConstantMatrixType>())
4158 return MTy->getElementType();
4159 return T;
4160 };
4161
4162 QualType EltTy0 = getElemType(Ty0);
4163
4164 bool IsVec0 = Ty0->isVectorType();
4165 bool IsMat0 = Ty0->isConstantMatrixType();
4166 bool IsVec1 = Ty1->isVectorType();
4167 bool IsMat1 = Ty1->isConstantMatrixType();
4168
4169 QualType RetTy;
4170
4171 if (IsVec0 && IsMat1) {
4172 auto *MatTy = Ty1->castAs<ConstantMatrixType>();
4173 RetTy = getASTContext().getExtVectorType(EltTy0, MatTy->getNumColumns());
4174 } else if (IsMat0 && IsVec1) {
4175 auto *MatTy = Ty0->castAs<ConstantMatrixType>();
4176 RetTy = getASTContext().getExtVectorType(EltTy0, MatTy->getNumRows());
4177 } else {
4178 assert(IsMat0 && IsMat1);
4179 auto *MatTy0 = Ty0->castAs<ConstantMatrixType>();
4180 auto *MatTy1 = Ty1->castAs<ConstantMatrixType>();
4182 EltTy0, MatTy0->getNumRows(), MatTy1->getNumColumns());
4183 }
4184
4185 TheCall->setType(RetTy);
4186 break;
4187 }
4188 case Builtin::BI__builtin_hlsl_normalize: {
4189 if (SemaRef.checkArgCount(TheCall, 1))
4190 return true;
4191 if (CheckAllArgTypesAreCorrect(&SemaRef, TheCall,
4193 return true;
4194 ExprResult A = TheCall->getArg(0);
4195 QualType ArgTyA = A.get()->getType();
4196 // return type is the same as the input type
4197 TheCall->setType(ArgTyA);
4198 break;
4199 }
4200 case Builtin::BI__builtin_elementwise_fma: {
4201 if (SemaRef.checkArgCount(TheCall, 3) ||
4202 CheckAllArgsHaveSameType(&SemaRef, TheCall)) {
4203 return true;
4204 }
4205
4206 if (CheckAllArgTypesAreCorrect(&SemaRef, TheCall,
4208 return true;
4209
4210 ExprResult A = TheCall->getArg(0);
4211 QualType ArgTyA = A.get()->getType();
4212 // return type is the same as input type
4213 TheCall->setType(ArgTyA);
4214 break;
4215 }
4216 case Builtin::BI__builtin_hlsl_transpose: {
4217 if (SemaRef.checkArgCount(TheCall, 1))
4218 return true;
4219
4220 Expr *Arg = TheCall->getArg(0);
4221 QualType ArgTy = Arg->getType();
4222
4223 const auto *MatTy = ArgTy->getAs<ConstantMatrixType>();
4224 if (!MatTy) {
4225 SemaRef.Diag(Arg->getBeginLoc(), diag::err_builtin_invalid_arg_type)
4226 << 1 << /* matrix */ 3 << /* no int */ 0 << /* no fp */ 0 << ArgTy;
4227 return true;
4228 }
4229
4231 MatTy->getElementType(), MatTy->getNumColumns(), MatTy->getNumRows());
4232 TheCall->setType(RetTy);
4233 break;
4234 }
4235 case Builtin::BI__builtin_hlsl_elementwise_sign: {
4236 if (SemaRef.PrepareBuiltinElementwiseMathOneArgCall(TheCall))
4237 return true;
4238 if (CheckAllArgTypesAreCorrect(&SemaRef, TheCall,
4240 return true;
4242 break;
4243 }
4244 case Builtin::BI__builtin_hlsl_step: {
4245 if (SemaRef.checkArgCount(TheCall, 2))
4246 return true;
4247 if (CheckAllArgTypesAreCorrect(&SemaRef, TheCall,
4249 return true;
4250
4251 ExprResult A = TheCall->getArg(0);
4252 QualType ArgTyA = A.get()->getType();
4253 // return type is the same as the input type
4254 TheCall->setType(ArgTyA);
4255 break;
4256 }
4257 case Builtin::BI__builtin_hlsl_wave_active_all_equal: {
4258 if (SemaRef.checkArgCount(TheCall, 1))
4259 return true;
4260
4261 // Ensure input expr type is a scalar/vector
4262 if (CheckAnyScalarOrVector(&SemaRef, TheCall, 0))
4263 return true;
4264
4265 QualType InputTy = TheCall->getArg(0)->getType();
4266 ASTContext &Ctx = getASTContext();
4267
4268 QualType RetTy;
4269
4270 // If vector, construct bool vector of same size
4271 if (const auto *VecTy = InputTy->getAs<ExtVectorType>()) {
4272 unsigned NumElts = VecTy->getNumElements();
4273 RetTy = Ctx.getExtVectorType(Ctx.BoolTy, NumElts);
4274 } else {
4275 // Scalar case
4276 RetTy = Ctx.BoolTy;
4277 }
4278
4279 TheCall->setType(RetTy);
4280 break;
4281 }
4282 case Builtin::BI__builtin_hlsl_wave_active_max:
4283 case Builtin::BI__builtin_hlsl_wave_active_min:
4284 case Builtin::BI__builtin_hlsl_wave_active_sum:
4285 case Builtin::BI__builtin_hlsl_wave_active_product: {
4286 if (SemaRef.checkArgCount(TheCall, 1))
4287 return true;
4288
4289 // Ensure input expr type is a scalar/vector and the same as the return type
4290 if (CheckAnyScalarOrVector(&SemaRef, TheCall, 0))
4291 return true;
4292 if (CheckWaveActive(&SemaRef, TheCall))
4293 return true;
4294 ExprResult Expr = TheCall->getArg(0);
4295 QualType ArgTyExpr = Expr.get()->getType();
4296 TheCall->setType(ArgTyExpr);
4297 break;
4298 }
4299 case Builtin::BI__builtin_hlsl_wave_active_bit_or:
4300 case Builtin::BI__builtin_hlsl_wave_active_bit_xor:
4301 case Builtin::BI__builtin_hlsl_wave_active_bit_and: {
4302 if (SemaRef.checkArgCount(TheCall, 1))
4303 return true;
4304
4305 // Ensure input expr type is a scalar/vector
4306 if (CheckAnyScalarOrVector(&SemaRef, TheCall, 0))
4307 return true;
4308
4309 if (CheckWaveActive(&SemaRef, TheCall))
4310 return true;
4311
4312 // Ensure the expr type is interpretable as a uint or vector<uint>
4313 ExprResult Expr = TheCall->getArg(0);
4314 QualType ArgTyExpr = Expr.get()->getType();
4315 auto *VTy = ArgTyExpr->getAs<VectorType>();
4316 if (!(ArgTyExpr->isIntegerType() ||
4317 (VTy && VTy->getElementType()->isIntegerType()))) {
4318 SemaRef.Diag(TheCall->getArg(0)->getBeginLoc(),
4319 diag::err_builtin_invalid_arg_type)
4320 << ArgTyExpr << SemaRef.Context.UnsignedIntTy << 1 << 0 << 0;
4321 return true;
4322 }
4323
4324 // Ensure input expr type is the same as the return type
4325 TheCall->setType(ArgTyExpr);
4326 break;
4327 }
4328 // Note these are llvm builtins that we want to catch invalid intrinsic
4329 // generation. Normal handling of these builtins will occur elsewhere.
4330 case Builtin::BI__builtin_elementwise_bitreverse: {
4331 // does not include a check for number of arguments
4332 // because that is done previously
4333 if (CheckAllArgTypesAreCorrect(&SemaRef, TheCall,
4335 return true;
4336 break;
4337 }
4338 case Builtin::BI__builtin_hlsl_wave_prefix_count_bits: {
4339 if (SemaRef.checkArgCount(TheCall, 1))
4340 return true;
4341
4342 QualType ArgType = TheCall->getArg(0)->getType();
4343
4344 if (!(ArgType->isScalarType())) {
4345 SemaRef.Diag(TheCall->getArg(0)->getBeginLoc(),
4346 diag::err_typecheck_expect_any_scalar_or_vector)
4347 << ArgType << 0;
4348 return true;
4349 }
4350
4351 if (!(ArgType->isBooleanType())) {
4352 SemaRef.Diag(TheCall->getArg(0)->getBeginLoc(),
4353 diag::err_typecheck_expect_any_scalar_or_vector)
4354 << ArgType << 0;
4355 return true;
4356 }
4357
4358 break;
4359 }
4360 case Builtin::BI__builtin_hlsl_wave_read_lane_at: {
4361 if (SemaRef.checkArgCount(TheCall, 2))
4362 return true;
4363
4364 // Ensure index parameter type can be interpreted as a uint
4365 ExprResult Index = TheCall->getArg(1);
4366 QualType ArgTyIndex = Index.get()->getType();
4367 if (!ArgTyIndex->isIntegerType()) {
4368 SemaRef.Diag(TheCall->getArg(1)->getBeginLoc(),
4369 diag::err_typecheck_convert_incompatible)
4370 << ArgTyIndex << SemaRef.Context.UnsignedIntTy << 1 << 0 << 0;
4371 return true;
4372 }
4373
4374 // Ensure input expr type is a scalar/vector and the same as the return type
4375 if (CheckAnyScalarOrVector(&SemaRef, TheCall, 0))
4376 return true;
4377
4378 ExprResult Expr = TheCall->getArg(0);
4379 QualType ArgTyExpr = Expr.get()->getType();
4380 TheCall->setType(ArgTyExpr);
4381 break;
4382 }
4383 case Builtin::BI__builtin_hlsl_wave_get_lane_index: {
4384 if (SemaRef.checkArgCount(TheCall, 0))
4385 return true;
4386 break;
4387 }
4388 case Builtin::BI__builtin_hlsl_wave_prefix_sum:
4389 case Builtin::BI__builtin_hlsl_wave_prefix_product: {
4390 if (SemaRef.checkArgCount(TheCall, 1))
4391 return true;
4392
4393 // Ensure input expr type is a scalar/vector and the same as the return type
4394 if (CheckAnyScalarOrVector(&SemaRef, TheCall, 0))
4395 return true;
4396 if (CheckWavePrefix(&SemaRef, TheCall))
4397 return true;
4398 ExprResult Expr = TheCall->getArg(0);
4399 QualType ArgTyExpr = Expr.get()->getType();
4400 TheCall->setType(ArgTyExpr);
4401 break;
4402 }
4403 case Builtin::BI__builtin_hlsl_quad_read_across_x:
4404 case Builtin::BI__builtin_hlsl_quad_read_across_y: {
4405 if (SemaRef.checkArgCount(TheCall, 1))
4406 return true;
4407
4408 if (CheckAnyScalarOrVector(&SemaRef, TheCall, 0))
4409 return true;
4410 if (CheckNotBoolScalarOrVector(&SemaRef, TheCall, 0))
4411 return true;
4412 ExprResult Expr = TheCall->getArg(0);
4413 QualType ArgTyExpr = Expr.get()->getType();
4414 TheCall->setType(ArgTyExpr);
4415 break;
4416 }
4417 case Builtin::BI__builtin_hlsl_elementwise_splitdouble: {
4418 if (SemaRef.checkArgCount(TheCall, 3))
4419 return true;
4420
4421 if (CheckScalarOrVector(&SemaRef, TheCall, SemaRef.Context.DoubleTy, 0) ||
4422 CheckScalarOrVector(&SemaRef, TheCall, SemaRef.Context.UnsignedIntTy,
4423 1) ||
4424 CheckScalarOrVector(&SemaRef, TheCall, SemaRef.Context.UnsignedIntTy,
4425 2))
4426 return true;
4427
4428 if (CheckModifiableLValue(&SemaRef, TheCall, 1) ||
4429 CheckModifiableLValue(&SemaRef, TheCall, 2))
4430 return true;
4431 break;
4432 }
4433 case Builtin::BI__builtin_hlsl_elementwise_clip: {
4434 if (SemaRef.checkArgCount(TheCall, 1))
4435 return true;
4436
4437 if (CheckScalarOrVector(&SemaRef, TheCall, SemaRef.Context.FloatTy, 0))
4438 return true;
4439 break;
4440 }
4441 case Builtin::BI__builtin_elementwise_acos:
4442 case Builtin::BI__builtin_elementwise_asin:
4443 case Builtin::BI__builtin_elementwise_atan:
4444 case Builtin::BI__builtin_elementwise_atan2:
4445 case Builtin::BI__builtin_elementwise_ceil:
4446 case Builtin::BI__builtin_elementwise_cos:
4447 case Builtin::BI__builtin_elementwise_cosh:
4448 case Builtin::BI__builtin_elementwise_exp:
4449 case Builtin::BI__builtin_elementwise_exp2:
4450 case Builtin::BI__builtin_elementwise_exp10:
4451 case Builtin::BI__builtin_elementwise_floor:
4452 case Builtin::BI__builtin_elementwise_fmod:
4453 case Builtin::BI__builtin_elementwise_log:
4454 case Builtin::BI__builtin_elementwise_log2:
4455 case Builtin::BI__builtin_elementwise_log10:
4456 case Builtin::BI__builtin_elementwise_pow:
4457 case Builtin::BI__builtin_elementwise_roundeven:
4458 case Builtin::BI__builtin_elementwise_sin:
4459 case Builtin::BI__builtin_elementwise_sinh:
4460 case Builtin::BI__builtin_elementwise_sqrt:
4461 case Builtin::BI__builtin_elementwise_tan:
4462 case Builtin::BI__builtin_elementwise_tanh:
4463 case Builtin::BI__builtin_elementwise_trunc: {
4464 if (CheckAllArgTypesAreCorrect(&SemaRef, TheCall,
4466 return true;
4467 break;
4468 }
4469 case Builtin::BI__builtin_hlsl_buffer_update_counter: {
4470 assert(TheCall->getNumArgs() == 2 && "expected 2 args");
4471 auto checkResTy = [](const HLSLAttributedResourceType *ResTy) -> bool {
4472 return !(ResTy->getAttrs().ResourceClass == ResourceClass::UAV &&
4473 ResTy->getAttrs().RawBuffer && ResTy->hasContainedType());
4474 };
4475 if (CheckResourceHandle(&SemaRef, TheCall, 0, checkResTy))
4476 return true;
4477 Expr *OffsetExpr = TheCall->getArg(1);
4478 std::optional<llvm::APSInt> Offset =
4479 OffsetExpr->getIntegerConstantExpr(SemaRef.getASTContext());
4480 if (!Offset.has_value() || std::abs(Offset->getExtValue()) != 1) {
4481 SemaRef.Diag(TheCall->getArg(1)->getBeginLoc(),
4482 diag::err_hlsl_expect_arg_const_int_one_or_neg_one)
4483 << 1;
4484 return true;
4485 }
4486 break;
4487 }
4488 case Builtin::BI__builtin_hlsl_elementwise_f16tof32: {
4489 if (SemaRef.checkArgCount(TheCall, 1))
4490 return true;
4491 if (CheckAllArgTypesAreCorrect(&SemaRef, TheCall,
4493 return true;
4494 // ensure arg integers are 32 bits
4495 if (CheckExpectedBitWidth(&SemaRef, TheCall, 0, 32))
4496 return true;
4497 // check it wasn't a bool type
4498 QualType ArgTy = TheCall->getArg(0)->getType();
4499 if (auto *VTy = ArgTy->getAs<VectorType>())
4500 ArgTy = VTy->getElementType();
4501 if (ArgTy->isBooleanType()) {
4502 SemaRef.Diag(TheCall->getArg(0)->getBeginLoc(),
4503 diag::err_builtin_invalid_arg_type)
4504 << 1 << /* scalar or vector of */ 5 << /* unsigned int */ 3
4505 << /* no fp */ 0 << TheCall->getArg(0)->getType();
4506 return true;
4507 }
4508
4509 SetElementTypeAsReturnType(&SemaRef, TheCall, getASTContext().FloatTy);
4510 break;
4511 }
4512 case Builtin::BI__builtin_hlsl_elementwise_f32tof16: {
4513 if (SemaRef.checkArgCount(TheCall, 1))
4514 return true;
4516 return true;
4518 getASTContext().UnsignedIntTy);
4519 break;
4520 }
4521 }
4522 return false;
4523}
4524
4528 WorkList.push_back(BaseTy);
4529 while (!WorkList.empty()) {
4530 QualType T = WorkList.pop_back_val();
4531 T = T.getCanonicalType().getUnqualifiedType();
4532 if (const auto *AT = dyn_cast<ConstantArrayType>(T)) {
4533 llvm::SmallVector<QualType, 16> ElementFields;
4534 // Generally I've avoided recursion in this algorithm, but arrays of
4535 // structs could be time-consuming to flatten and churn through on the
4536 // work list. Hopefully nesting arrays of structs containing arrays
4537 // of structs too many levels deep is unlikely.
4538 BuildFlattenedTypeList(AT->getElementType(), ElementFields);
4539 // Repeat the element's field list n times.
4540 for (uint64_t Ct = 0; Ct < AT->getZExtSize(); ++Ct)
4541 llvm::append_range(List, ElementFields);
4542 continue;
4543 }
4544 // Vectors can only have element types that are builtin types, so this can
4545 // add directly to the list instead of to the WorkList.
4546 if (const auto *VT = dyn_cast<VectorType>(T)) {
4547 List.insert(List.end(), VT->getNumElements(), VT->getElementType());
4548 continue;
4549 }
4550 if (const auto *MT = dyn_cast<ConstantMatrixType>(T)) {
4551 List.insert(List.end(), MT->getNumElementsFlattened(),
4552 MT->getElementType());
4553 continue;
4554 }
4555 if (const auto *RD = T->getAsCXXRecordDecl()) {
4556 if (RD->isStandardLayout())
4557 RD = RD->getStandardLayoutBaseWithFields();
4558
4559 // For types that we shouldn't decompose (unions and non-aggregates), just
4560 // add the type itself to the list.
4561 if (RD->isUnion() || !RD->isAggregate()) {
4562 List.push_back(T);
4563 continue;
4564 }
4565
4567 for (const auto *FD : RD->fields())
4568 if (!FD->isUnnamedBitField())
4569 FieldTypes.push_back(FD->getType());
4570 // Reverse the newly added sub-range.
4571 std::reverse(FieldTypes.begin(), FieldTypes.end());
4572 llvm::append_range(WorkList, FieldTypes);
4573
4574 // If this wasn't a standard layout type we may also have some base
4575 // classes to deal with.
4576 if (!RD->isStandardLayout()) {
4577 FieldTypes.clear();
4578 for (const auto &Base : RD->bases())
4579 FieldTypes.push_back(Base.getType());
4580 std::reverse(FieldTypes.begin(), FieldTypes.end());
4581 llvm::append_range(WorkList, FieldTypes);
4582 }
4583 continue;
4584 }
4585 List.push_back(T);
4586 }
4587}
4588
4590 // null and array types are not allowed.
4591 if (QT.isNull() || QT->isArrayType())
4592 return false;
4593
4594 // UDT types are not allowed
4595 if (QT->isRecordType())
4596 return false;
4597
4598 if (QT->isBooleanType() || QT->isEnumeralType())
4599 return false;
4600
4601 // the only other valid builtin types are scalars or vectors
4602 if (QT->isArithmeticType()) {
4603 if (SemaRef.Context.getTypeSize(QT) / 8 > 16)
4604 return false;
4605 return true;
4606 }
4607
4608 if (const VectorType *VT = QT->getAs<VectorType>()) {
4609 int ArraySize = VT->getNumElements();
4610
4611 if (ArraySize > 4)
4612 return false;
4613
4614 QualType ElTy = VT->getElementType();
4615 if (ElTy->isBooleanType())
4616 return false;
4617
4618 if (SemaRef.Context.getTypeSize(QT) / 8 > 16)
4619 return false;
4620 return true;
4621 }
4622
4623 return false;
4624}
4625
4627 if (T1.isNull() || T2.isNull())
4628 return false;
4629
4632
4633 // If both types are the same canonical type, they're obviously compatible.
4634 if (SemaRef.getASTContext().hasSameType(T1, T2))
4635 return true;
4636
4638 BuildFlattenedTypeList(T1, T1Types);
4640 BuildFlattenedTypeList(T2, T2Types);
4641
4642 // Check the flattened type list
4643 return llvm::equal(T1Types, T2Types,
4644 [this](QualType LHS, QualType RHS) -> bool {
4645 return SemaRef.IsLayoutCompatible(LHS, RHS);
4646 });
4647}
4648
4650 FunctionDecl *Old) {
4651 if (New->getNumParams() != Old->getNumParams())
4652 return true;
4653
4654 bool HadError = false;
4655
4656 for (unsigned i = 0, e = New->getNumParams(); i != e; ++i) {
4657 ParmVarDecl *NewParam = New->getParamDecl(i);
4658 ParmVarDecl *OldParam = Old->getParamDecl(i);
4659
4660 // HLSL parameter declarations for inout and out must match between
4661 // declarations. In HLSL inout and out are ambiguous at the call site,
4662 // but have different calling behavior, so you cannot overload a
4663 // method based on a difference between inout and out annotations.
4664 const auto *NDAttr = NewParam->getAttr<HLSLParamModifierAttr>();
4665 unsigned NSpellingIdx = (NDAttr ? NDAttr->getSpellingListIndex() : 0);
4666 const auto *ODAttr = OldParam->getAttr<HLSLParamModifierAttr>();
4667 unsigned OSpellingIdx = (ODAttr ? ODAttr->getSpellingListIndex() : 0);
4668
4669 if (NSpellingIdx != OSpellingIdx) {
4670 SemaRef.Diag(NewParam->getLocation(),
4671 diag::err_hlsl_param_qualifier_mismatch)
4672 << NDAttr << NewParam;
4673 SemaRef.Diag(OldParam->getLocation(), diag::note_previous_declaration_as)
4674 << ODAttr;
4675 HadError = true;
4676 }
4677 }
4678 return HadError;
4679}
4680
4681// Generally follows PerformScalarCast, with cases reordered for
4682// clarity of what types are supported
4684
4685 if (!SrcTy->isScalarType() || !DestTy->isScalarType())
4686 return false;
4687
4688 if (SemaRef.getASTContext().hasSameUnqualifiedType(SrcTy, DestTy))
4689 return true;
4690
4691 switch (SrcTy->getScalarTypeKind()) {
4692 case Type::STK_Bool: // casting from bool is like casting from an integer
4693 case Type::STK_Integral:
4694 switch (DestTy->getScalarTypeKind()) {
4695 case Type::STK_Bool:
4696 case Type::STK_Integral:
4697 case Type::STK_Floating:
4698 return true;
4699 case Type::STK_CPointer:
4703 llvm_unreachable("HLSL doesn't support pointers.");
4706 llvm_unreachable("HLSL doesn't support complex types.");
4708 llvm_unreachable("HLSL doesn't support fixed point types.");
4709 }
4710 llvm_unreachable("Should have returned before this");
4711
4712 case Type::STK_Floating:
4713 switch (DestTy->getScalarTypeKind()) {
4714 case Type::STK_Floating:
4715 case Type::STK_Bool:
4716 case Type::STK_Integral:
4717 return true;
4720 llvm_unreachable("HLSL doesn't support complex types.");
4722 llvm_unreachable("HLSL doesn't support fixed point types.");
4723 case Type::STK_CPointer:
4727 llvm_unreachable("HLSL doesn't support pointers.");
4728 }
4729 llvm_unreachable("Should have returned before this");
4730
4732 case Type::STK_CPointer:
4735 llvm_unreachable("HLSL doesn't support pointers.");
4736
4738 llvm_unreachable("HLSL doesn't support fixed point types.");
4739
4742 llvm_unreachable("HLSL doesn't support complex types.");
4743 }
4744
4745 llvm_unreachable("Unhandled scalar cast");
4746}
4747
4748// Can perform an HLSL Aggregate splat cast if the Dest is an aggregate and the
4749// Src is a scalar, a vector of length 1, or a 1x1 matrix
4750// Or if Dest is a vector and Src is a vector of length 1 or a 1x1 matrix
4752
4753 QualType SrcTy = Src->getType();
4754 // Not a valid HLSL Aggregate Splat cast if Dest is a scalar or if this is
4755 // going to be a vector splat from a scalar.
4756 if ((SrcTy->isScalarType() && DestTy->isVectorType()) ||
4757 DestTy->isScalarType())
4758 return false;
4759
4760 const VectorType *SrcVecTy = SrcTy->getAs<VectorType>();
4761 const ConstantMatrixType *SrcMatTy = SrcTy->getAs<ConstantMatrixType>();
4762
4763 // Src isn't a scalar, a vector of length 1, or a 1x1 matrix
4764 if (!SrcTy->isScalarType() &&
4765 !(SrcVecTy && SrcVecTy->getNumElements() == 1) &&
4766 !(SrcMatTy && SrcMatTy->getNumElementsFlattened() == 1))
4767 return false;
4768
4769 if (SrcVecTy)
4770 SrcTy = SrcVecTy->getElementType();
4771 else if (SrcMatTy)
4772 SrcTy = SrcMatTy->getElementType();
4773
4775 BuildFlattenedTypeList(DestTy, DestTypes);
4776
4777 for (unsigned I = 0, Size = DestTypes.size(); I < Size; ++I) {
4778 if (DestTypes[I]->isUnionType())
4779 return false;
4780 if (!CanPerformScalarCast(SrcTy, DestTypes[I]))
4781 return false;
4782 }
4783 return true;
4784}
4785
4786// Can we perform an HLSL Elementwise cast?
4788
4789 // Don't handle casts where LHS and RHS are any combination of scalar/vector
4790 // There must be an aggregate somewhere
4791 QualType SrcTy = Src->getType();
4792 if (SrcTy->isScalarType()) // always a splat and this cast doesn't handle that
4793 return false;
4794
4795 if (SrcTy->isVectorType() &&
4796 (DestTy->isScalarType() || DestTy->isVectorType()))
4797 return false;
4798
4799 if (SrcTy->isConstantMatrixType() &&
4800 (DestTy->isScalarType() || DestTy->isConstantMatrixType()))
4801 return false;
4802
4804 BuildFlattenedTypeList(DestTy, DestTypes);
4806 BuildFlattenedTypeList(SrcTy, SrcTypes);
4807
4808 // Usually the size of SrcTypes must be greater than or equal to the size of
4809 // DestTypes.
4810 if (SrcTypes.size() < DestTypes.size())
4811 return false;
4812
4813 unsigned SrcSize = SrcTypes.size();
4814 unsigned DstSize = DestTypes.size();
4815 unsigned I;
4816 for (I = 0; I < DstSize && I < SrcSize; I++) {
4817 if (SrcTypes[I]->isUnionType() || DestTypes[I]->isUnionType())
4818 return false;
4819 if (!CanPerformScalarCast(SrcTypes[I], DestTypes[I])) {
4820 return false;
4821 }
4822 }
4823
4824 // check the rest of the source type for unions.
4825 for (; I < SrcSize; I++) {
4826 if (SrcTypes[I]->isUnionType())
4827 return false;
4828 }
4829 return true;
4830}
4831
4833 assert(Param->hasAttr<HLSLParamModifierAttr>() &&
4834 "We should not get here without a parameter modifier expression");
4835 const auto *Attr = Param->getAttr<HLSLParamModifierAttr>();
4836 if (Attr->getABI() == ParameterABI::Ordinary)
4837 return ExprResult(Arg);
4838
4839 bool IsInOut = Attr->getABI() == ParameterABI::HLSLInOut;
4840 if (!Arg->isLValue()) {
4841 SemaRef.Diag(Arg->getBeginLoc(), diag::error_hlsl_inout_lvalue)
4842 << Arg << (IsInOut ? 1 : 0);
4843 return ExprError();
4844 }
4845
4846 ASTContext &Ctx = SemaRef.getASTContext();
4847
4848 QualType Ty = Param->getType().getNonLValueExprType(Ctx);
4849
4850 // HLSL allows implicit conversions from scalars to vectors, but not the
4851 // inverse, so we need to disallow `inout` with scalar->vector or
4852 // scalar->matrix conversions.
4853 if (Arg->getType()->isScalarType() != Ty->isScalarType()) {
4854 SemaRef.Diag(Arg->getBeginLoc(), diag::error_hlsl_inout_scalar_extension)
4855 << Arg << (IsInOut ? 1 : 0);
4856 return ExprError();
4857 }
4858
4859 auto *ArgOpV = new (Ctx) OpaqueValueExpr(Param->getBeginLoc(), Arg->getType(),
4860 VK_LValue, OK_Ordinary, Arg);
4861
4862 // Parameters are initialized via copy initialization. This allows for
4863 // overload resolution of argument constructors.
4864 InitializedEntity Entity =
4866 ExprResult Res =
4867 SemaRef.PerformCopyInitialization(Entity, Param->getBeginLoc(), ArgOpV);
4868 if (Res.isInvalid())
4869 return ExprError();
4870 Expr *Base = Res.get();
4871 // After the cast, drop the reference type when creating the exprs.
4872 Ty = Ty.getNonLValueExprType(Ctx);
4873 auto *OpV = new (Ctx)
4874 OpaqueValueExpr(Param->getBeginLoc(), Ty, VK_LValue, OK_Ordinary, Base);
4875
4876 // Writebacks are performed with `=` binary operator, which allows for
4877 // overload resolution on writeback result expressions.
4878 Res = SemaRef.ActOnBinOp(SemaRef.getCurScope(), Param->getBeginLoc(),
4879 tok::equal, ArgOpV, OpV);
4880
4881 if (Res.isInvalid())
4882 return ExprError();
4883 Expr *Writeback = Res.get();
4884 auto *OutExpr =
4885 HLSLOutArgExpr::Create(Ctx, Ty, ArgOpV, OpV, Writeback, IsInOut);
4886
4887 return ExprResult(OutExpr);
4888}
4889
4891 // If HLSL gains support for references, all the cites that use this will need
4892 // to be updated with semantic checking to produce errors for
4893 // pointers/references.
4894 assert(!Ty->isReferenceType() &&
4895 "Pointer and reference types cannot be inout or out parameters");
4896 Ty = SemaRef.getASTContext().getLValueReferenceType(Ty);
4897 Ty.addRestrict();
4898 return Ty;
4899}
4900
4901// Returns true if the type has a non-empty constant buffer layout (if it is
4902// scalar, vector or matrix, or if it contains any of these.
4904 const Type *Ty = QT->getUnqualifiedDesugaredType();
4905 if (Ty->isScalarType() || Ty->isVectorType() || Ty->isMatrixType())
4906 return true;
4907
4909 return false;
4910
4911 if (const auto *RD = Ty->getAsCXXRecordDecl()) {
4912 for (const auto *FD : RD->fields()) {
4914 return true;
4915 }
4916 assert(RD->getNumBases() <= 1 &&
4917 "HLSL doesn't support multiple inheritance");
4918 return RD->getNumBases()
4919 ? hasConstantBufferLayout(RD->bases_begin()->getType())
4920 : false;
4921 }
4922
4923 if (const auto *AT = dyn_cast<ArrayType>(Ty)) {
4924 if (const auto *CAT = dyn_cast<ConstantArrayType>(AT))
4925 if (isZeroSizedArray(CAT))
4926 return false;
4928 }
4929
4930 return false;
4931}
4932
4933static bool IsDefaultBufferConstantDecl(const ASTContext &Ctx, VarDecl *VD) {
4934 bool IsVulkan =
4935 Ctx.getTargetInfo().getTriple().getOS() == llvm::Triple::Vulkan;
4936 bool IsVKPushConstant = IsVulkan && VD->hasAttr<HLSLVkPushConstantAttr>();
4937 QualType QT = VD->getType();
4938 return VD->getDeclContext()->isTranslationUnit() &&
4939 QT.getAddressSpace() == LangAS::Default &&
4940 VD->getStorageClass() != SC_Static &&
4941 !VD->hasAttr<HLSLVkConstantIdAttr>() && !IsVKPushConstant &&
4943}
4944
4946 // The variable already has an address space (groupshared for ex).
4947 if (Decl->getType().hasAddressSpace())
4948 return;
4949
4950 if (Decl->getType()->isDependentType())
4951 return;
4952
4953 QualType Type = Decl->getType();
4954
4955 if (Decl->hasAttr<HLSLVkExtBuiltinInputAttr>()) {
4956 LangAS ImplAS = LangAS::hlsl_input;
4957 Type = SemaRef.getASTContext().getAddrSpaceQualType(Type, ImplAS);
4958 Decl->setType(Type);
4959 return;
4960 }
4961
4962 if (Decl->hasAttr<HLSLVkExtBuiltinOutputAttr>()) {
4963 LangAS ImplAS = LangAS::hlsl_output;
4964 Type = SemaRef.getASTContext().getAddrSpaceQualType(Type, ImplAS);
4965 Decl->setType(Type);
4966
4967 // HLSL uses `static` differently than C++. For BuiltIn output, the static
4968 // does not imply private to the module scope.
4969 // Marking it as external to reflect the semantic this attribute brings.
4970 // See https://github.com/microsoft/hlsl-specs/issues/350
4971 Decl->setStorageClass(SC_Extern);
4972 return;
4973 }
4974
4975 bool IsVulkan = getASTContext().getTargetInfo().getTriple().getOS() ==
4976 llvm::Triple::Vulkan;
4977 if (IsVulkan && Decl->hasAttr<HLSLVkPushConstantAttr>()) {
4978 if (HasDeclaredAPushConstant)
4979 SemaRef.Diag(Decl->getLocation(), diag::err_hlsl_push_constant_unique);
4980
4982 Type = SemaRef.getASTContext().getAddrSpaceQualType(Type, ImplAS);
4983 Decl->setType(Type);
4984 HasDeclaredAPushConstant = true;
4985 return;
4986 }
4987
4988 if (Type->isSamplerT() || Type->isVoidType())
4989 return;
4990
4991 // Resource handles.
4993 return;
4994
4995 // Only static globals belong to the Private address space.
4996 // Non-static globals belongs to the cbuffer.
4997 if (Decl->getStorageClass() != SC_Static && !Decl->isStaticDataMember())
4998 return;
4999
5001 Type = SemaRef.getASTContext().getAddrSpaceQualType(Type, ImplAS);
5002 Decl->setType(Type);
5003}
5004
5005namespace {
5006
5007// Helper class for assigning bindings to resources declared within a struct.
5008// It keeps track of all binding attributes declared on a struct instance, and
5009// the offsets for each register type that have been assigned so far.
5010// Handles both explicit and implicit bindings.
5011class StructBindingContext {
5012 // Bindings and offsets per register type. We only need to support four
5013 // register types - SRV (u), UAV (t), CBuffer (c), and Sampler (s).
5014 HLSLResourceBindingAttr *RegBindingsAttrs[4];
5015 unsigned RegBindingOffset[4];
5016
5017 // Make sure the RegisterType values are what we expect
5018 static_assert(static_cast<unsigned>(RegisterType::SRV) == 0 &&
5019 static_cast<unsigned>(RegisterType::UAV) == 1 &&
5020 static_cast<unsigned>(RegisterType::CBuffer) == 2 &&
5021 static_cast<unsigned>(RegisterType::Sampler) == 3,
5022 "unexpected register type values");
5023
5024 // Vulkan binding attribute does not vary by register type.
5025 HLSLVkBindingAttr *VkBindingAttr;
5026 unsigned VkBindingOffset;
5027
5028public:
5029 // Constructor: gather all binding attributes on a struct instance and
5030 // initialize offsets.
5031 StructBindingContext(VarDecl *VD) {
5032 for (unsigned i = 0; i < 4; ++i) {
5033 RegBindingsAttrs[i] = nullptr;
5034 RegBindingOffset[i] = 0;
5035 }
5036 VkBindingAttr = nullptr;
5037 VkBindingOffset = 0;
5038
5039 ASTContext &AST = VD->getASTContext();
5040 bool IsSpirv = AST.getTargetInfo().getTriple().isSPIRV();
5041
5042 for (Attr *A : VD->attrs()) {
5043 if (auto *RBA = dyn_cast<HLSLResourceBindingAttr>(A)) {
5044 RegisterType RegType = RBA->getRegisterType();
5045 unsigned RegTypeIdx = static_cast<unsigned>(RegType);
5046 // Ignore unsupported register annotations, such as 'c' or 'i'.
5047 if (RegTypeIdx < 4)
5048 RegBindingsAttrs[RegTypeIdx] = RBA;
5049 continue;
5050 }
5051 // Gather the Vulkan binding attributes only if the target is SPIR-V.
5052 if (IsSpirv) {
5053 if (auto *VBA = dyn_cast<HLSLVkBindingAttr>(A))
5054 VkBindingAttr = VBA;
5055 }
5056 }
5057 }
5058
5059 // Creates a binding attribute for a resource based on the gathered attributes
5060 // and the required register type and range.
5061 Attr *createBindingAttr(SemaHLSL &S, ASTContext &AST, RegisterType RegType,
5062 unsigned Range) {
5063 assert(static_cast<unsigned>(RegType) < 4 && "unexpected register type");
5064
5065 if (VkBindingAttr) {
5066 unsigned Offset = VkBindingOffset;
5067 VkBindingOffset += Range;
5068 return HLSLVkBindingAttr::CreateImplicit(
5069 AST, VkBindingAttr->getBinding() + Offset, VkBindingAttr->getSet(),
5070 VkBindingAttr->getRange());
5071 }
5072
5073 HLSLResourceBindingAttr *RBA =
5074 RegBindingsAttrs[static_cast<unsigned>(RegType)];
5075 HLSLResourceBindingAttr *NewAttr = nullptr;
5076
5077 if (RBA && RBA->hasRegisterSlot()) {
5078 // Explicit binding - create a new attribute with offseted slot number
5079 // based on the required register type.
5080 unsigned Offset = RegBindingOffset[static_cast<unsigned>(RegType)];
5081 RegBindingOffset[static_cast<unsigned>(RegType)] += Range;
5082
5083 unsigned NewSlotNumber = RBA->getSlotNumber() + Offset;
5084 StringRef NewSlotNumberStr =
5085 createRegisterString(AST, RBA->getRegisterType(), NewSlotNumber);
5086 NewAttr = HLSLResourceBindingAttr::CreateImplicit(
5087 AST, NewSlotNumberStr, RBA->getSpace(), RBA->getRange());
5088 NewAttr->setBinding(RegType, NewSlotNumber, RBA->getSpaceNumber());
5089 } else {
5090 // No binding attribute or space-only binding - create a binding
5091 // attribute for implicit binding.
5092 NewAttr = HLSLResourceBindingAttr::CreateImplicit(AST, "", "0", {});
5093 NewAttr->setBinding(RegType, std::nullopt,
5094 RBA ? RBA->getSpaceNumber() : 0);
5095 NewAttr->setImplicitBindingOrderID(S.getNextImplicitBindingOrderID());
5096 }
5097 return NewAttr;
5098 }
5099};
5100
5101// Creates a global variable declaration for a resource field embedded in a
5102// struct, assigns it a binding, initializes it, and associates it with the
5103// struct declaration via an HLSLAssociatedResourceDeclAttr.
5104static void createGlobalResourceDeclForStruct(
5105 Sema &S, VarDecl *ParentVD, SourceLocation Loc, IdentifierInfo *Id,
5106 QualType ResTy, StructBindingContext &BindingCtx) {
5107 assert(isResourceRecordTypeOrArrayOf(ResTy) &&
5108 "expected resource type or array of resources");
5109
5110 DeclContext *DC = ParentVD->getNonTransparentDeclContext();
5111 assert(DC->isTranslationUnit() && "expected translation unit decl context");
5112
5113 ASTContext &AST = S.getASTContext();
5114 VarDecl *ResDecl =
5115 VarDecl::Create(AST, DC, Loc, Loc, Id, ResTy, nullptr, SC_None);
5116
5117 unsigned Range = 1;
5118 const HLSLAttributedResourceType *ResHandleTy = nullptr;
5119 if (const auto *AT = dyn_cast<ArrayType>(ResTy.getTypePtr())) {
5120 const auto *CAT = dyn_cast<ConstantArrayType>(AT);
5121 Range = CAT ? CAT->getSize().getZExtValue() : 0;
5122 ResHandleTy = getResourceArrayHandleType(ResTy);
5123 } else {
5124 ResHandleTy = HLSLAttributedResourceType::findHandleTypeOnResource(
5125 ResTy.getTypePtr());
5126 }
5127 // Add a binding attribute to the global resource declaration.
5128 Attr *BindingAttr = BindingCtx.createBindingAttr(
5129 S.HLSL(), AST, getRegisterType(ResHandleTy), Range);
5130 ResDecl->addAttr(BindingAttr);
5131 ResDecl->addAttr(InternalLinkageAttr::CreateImplicit(AST));
5132 ResDecl->setImplicit();
5133
5134 if (Range == 1)
5135 S.HLSL().initGlobalResourceDecl(ResDecl);
5136 else
5137 S.HLSL().initGlobalResourceArrayDecl(ResDecl);
5138
5139 ParentVD->addAttr(
5140 HLSLAssociatedResourceDeclAttr::CreateImplicit(AST, ResDecl));
5141 DC->addDecl(ResDecl);
5142
5143 DeclGroupRef DG(ResDecl);
5145}
5146
5147static void handleArrayOfStructWithResources(
5148 Sema &S, VarDecl *ParentVD, const ConstantArrayType *CAT,
5149 EmbeddedResourceNameBuilder &NameBuilder, StructBindingContext &BindingCtx);
5150
5151// Scans base and all fields of a struct/class type to find all embedded
5152// resources or resource arrays. Creates a global variable for each resource
5153// found.
5154static void handleStructWithResources(Sema &S, VarDecl *ParentVD,
5155 const CXXRecordDecl *RD,
5156 EmbeddedResourceNameBuilder &NameBuilder,
5157 StructBindingContext &BindingCtx) {
5158
5159 // Scan the base classes.
5160 assert(RD->getNumBases() <= 1 && "HLSL doesn't support multiple inheritance");
5161 const auto *BasesIt = RD->bases_begin();
5162 if (BasesIt != RD->bases_end()) {
5163 QualType QT = BasesIt->getType();
5164 if (QT->isHLSLIntangibleType()) {
5165 CXXRecordDecl *BaseRD = QT->getAsCXXRecordDecl();
5166 NameBuilder.pushBaseName(BaseRD->getName());
5167 handleStructWithResources(S, ParentVD, BaseRD, NameBuilder, BindingCtx);
5168 NameBuilder.pop();
5169 }
5170 }
5171 // Process this class fields.
5172 for (const FieldDecl *FD : RD->fields()) {
5173 QualType FDTy = FD->getType().getCanonicalType();
5174 if (!FDTy->isHLSLIntangibleType())
5175 continue;
5176
5177 NameBuilder.pushName(FD->getName());
5178
5180 IdentifierInfo *II = NameBuilder.getNameAsIdentifier(S.getASTContext());
5181 createGlobalResourceDeclForStruct(S, ParentVD, FD->getLocation(), II,
5182 FDTy, BindingCtx);
5183 } else if (const auto *RD = FDTy->getAsCXXRecordDecl()) {
5184 handleStructWithResources(S, ParentVD, RD, NameBuilder, BindingCtx);
5185
5186 } else if (const auto *ArrayTy = dyn_cast<ConstantArrayType>(FDTy)) {
5187 assert(!FDTy->isHLSLResourceRecordArray() &&
5188 "resource arrays should have been already handled");
5189 handleArrayOfStructWithResources(S, ParentVD, ArrayTy, NameBuilder,
5190 BindingCtx);
5191 }
5192 NameBuilder.pop();
5193 }
5194}
5195
5196// Processes array of structs with resources.
5197static void
5198handleArrayOfStructWithResources(Sema &S, VarDecl *ParentVD,
5199 const ConstantArrayType *CAT,
5200 EmbeddedResourceNameBuilder &NameBuilder,
5201 StructBindingContext &BindingCtx) {
5202
5203 QualType ElementTy = CAT->getElementType().getCanonicalType();
5204 assert(ElementTy->isHLSLIntangibleType() && "Expected HLSL intangible type");
5205
5206 const ConstantArrayType *SubCAT = dyn_cast<ConstantArrayType>(ElementTy);
5207 const CXXRecordDecl *ElementRD = ElementTy->getAsCXXRecordDecl();
5208
5209 if (!SubCAT && !ElementRD)
5210 return;
5211
5212 for (unsigned I = 0, E = CAT->getSize().getZExtValue(); I < E; ++I) {
5213 NameBuilder.pushArrayIndex(I);
5214 if (ElementRD)
5215 handleStructWithResources(S, ParentVD, ElementRD, NameBuilder,
5216 BindingCtx);
5217 else
5218 handleArrayOfStructWithResources(S, ParentVD, SubCAT, NameBuilder,
5219 BindingCtx);
5220 NameBuilder.pop();
5221 }
5222}
5223
5224} // namespace
5225
5226// Scans all fields of a user-defined struct (or array of structs)
5227// to find all embedded resources or resource arrays. For each resource
5228// a global variable of the resource type is created and associated
5229// with the parent declaration (VD) through a HLSLAssociatedResourceDeclAttr
5230// attribute.
5231void SemaHLSL::handleGlobalStructOrArrayOfWithResources(VarDecl *VD) {
5232 EmbeddedResourceNameBuilder NameBuilder(VD->getName());
5233 StructBindingContext BindingCtx(VD);
5234
5235 const Type *VDTy = VD->getType().getTypePtr();
5236 assert(VDTy->isHLSLIntangibleType() && !isResourceRecordTypeOrArrayOf(VD) &&
5237 "Expected non-resource struct or array type");
5238
5239 if (const CXXRecordDecl *RD = VDTy->getAsCXXRecordDecl()) {
5240 handleStructWithResources(SemaRef, VD, RD, NameBuilder, BindingCtx);
5241 return;
5242 }
5243
5244 if (const auto *CAT = dyn_cast<ConstantArrayType>(VDTy)) {
5245 handleArrayOfStructWithResources(SemaRef, VD, CAT, NameBuilder, BindingCtx);
5246 return;
5247 }
5248}
5249
5251 if (VD->hasGlobalStorage()) {
5252 // make sure the declaration has a complete type
5253 if (SemaRef.RequireCompleteType(
5254 VD->getLocation(),
5255 SemaRef.getASTContext().getBaseElementType(VD->getType()),
5256 diag::err_typecheck_decl_incomplete_type)) {
5257 VD->setInvalidDecl();
5259 return;
5260 }
5261
5262 // Global variables outside a cbuffer block that are not a resource, static,
5263 // groupshared, or an empty array or struct belong to the default constant
5264 // buffer $Globals (to be created at the end of the translation unit).
5266 // update address space to hlsl_constant
5269 VD->setType(NewTy);
5270 DefaultCBufferDecls.push_back(VD);
5271 }
5272
5273 // find all resources bindings on decl
5274 if (VD->getType()->isHLSLIntangibleType())
5275 collectResourceBindingsOnVarDecl(VD);
5276
5277 if (VD->hasAttr<HLSLVkConstantIdAttr>())
5279
5281 VD->getStorageClass() != SC_Static) {
5282 // Add internal linkage attribute to non-static resource variables. The
5283 // global externally visible storage is accessed through the handle, which
5284 // is a member. The variable itself is not externally visible.
5285 VD->addAttr(InternalLinkageAttr::CreateImplicit(getASTContext()));
5286 }
5287
5288 // process explicit bindings
5289 processExplicitBindingsOnDecl(VD);
5290
5291 // Add implicit binding attribute to non-static resource arrays.
5292 if (VD->getType()->isHLSLResourceRecordArray() &&
5293 VD->getStorageClass() != SC_Static) {
5294 // If the resource array does not have an explicit binding attribute,
5295 // create an implicit one. It will be used to transfer implicit binding
5296 // order_ID to codegen.
5297 ResourceBindingAttrs Binding(VD);
5298 if (!Binding.isExplicit()) {
5299 uint32_t OrderID = getNextImplicitBindingOrderID();
5300 if (Binding.hasBinding())
5301 Binding.setImplicitOrderID(OrderID);
5302 else {
5305 OrderID);
5306 // Re-create the binding object to pick up the new attribute.
5307 Binding = ResourceBindingAttrs(VD);
5308 }
5309 }
5310
5311 // Get to the base type of a potentially multi-dimensional array.
5313
5314 const CXXRecordDecl *RD = Ty->getAsCXXRecordDecl();
5315 if (hasCounterHandle(RD)) {
5316 if (!Binding.hasCounterImplicitOrderID()) {
5317 uint32_t OrderID = getNextImplicitBindingOrderID();
5318 Binding.setCounterImplicitOrderID(OrderID);
5319 }
5320 }
5321 }
5322
5323 // Process resources in user-defined structs, or arrays of such structs.
5324 const Type *VDTy = VD->getType().getTypePtr();
5325 if (VD->getStorageClass() != SC_Static && VDTy->isHLSLIntangibleType() &&
5327 handleGlobalStructOrArrayOfWithResources(VD);
5328
5329 // Mark groupshared variables as extern so they will have
5330 // external storage and won't be default initialized
5331 if (VD->hasAttr<HLSLGroupSharedAddressSpaceAttr>())
5333 }
5334
5336}
5337
5339 assert(VD->getType()->isHLSLResourceRecord() &&
5340 "expected resource record type");
5341
5342 ASTContext &AST = SemaRef.getASTContext();
5343 uint64_t UIntTySize = AST.getTypeSize(AST.UnsignedIntTy);
5344 uint64_t IntTySize = AST.getTypeSize(AST.IntTy);
5345
5346 // Gather resource binding attributes.
5347 ResourceBindingAttrs Binding(VD);
5348
5349 // Find correct initialization method and create its arguments.
5350 QualType ResourceTy = VD->getType();
5351 CXXRecordDecl *ResourceDecl = ResourceTy->getAsCXXRecordDecl();
5352 CXXMethodDecl *CreateMethod = nullptr;
5354
5355 bool HasCounter = hasCounterHandle(ResourceDecl);
5356 const char *CreateMethodName;
5357 if (Binding.isExplicit())
5358 CreateMethodName = HasCounter ? "__createFromBindingWithImplicitCounter"
5359 : "__createFromBinding";
5360 else
5361 CreateMethodName = HasCounter
5362 ? "__createFromImplicitBindingWithImplicitCounter"
5363 : "__createFromImplicitBinding";
5364
5365 CreateMethod =
5366 lookupMethod(SemaRef, ResourceDecl, CreateMethodName, VD->getLocation());
5367
5368 if (!CreateMethod)
5369 // This can happen if someone creates a struct that looks like an HLSL
5370 // resource record but does not have the required static create method.
5371 // No binding will be generated for it.
5372 return false;
5373
5374 if (Binding.isExplicit()) {
5375 IntegerLiteral *RegSlot =
5376 IntegerLiteral::Create(AST, llvm::APInt(UIntTySize, Binding.getSlot()),
5378 Args.push_back(RegSlot);
5379 } else {
5380 uint32_t OrderID = (Binding.hasImplicitOrderID())
5381 ? Binding.getImplicitOrderID()
5383 IntegerLiteral *OrderId =
5384 IntegerLiteral::Create(AST, llvm::APInt(UIntTySize, OrderID),
5386 Args.push_back(OrderId);
5387 }
5388
5389 IntegerLiteral *Space =
5390 IntegerLiteral::Create(AST, llvm::APInt(UIntTySize, Binding.getSpace()),
5392 Args.push_back(Space);
5393
5395 AST, llvm::APInt(IntTySize, 1), AST.IntTy, SourceLocation());
5396 Args.push_back(RangeSize);
5397
5399 AST, llvm::APInt(UIntTySize, 0), AST.UnsignedIntTy, SourceLocation());
5400 Args.push_back(Index);
5401
5402 StringRef VarName = VD->getName();
5404 AST, VarName, StringLiteralKind::Ordinary, false,
5405 AST.getStringLiteralArrayType(AST.CharTy.withConst(), VarName.size()),
5406 SourceLocation());
5408 AST, AST.getPointerType(AST.CharTy.withConst()), CK_ArrayToPointerDecay,
5409 Name, nullptr, VK_PRValue, FPOptionsOverride());
5410 Args.push_back(NameCast);
5411
5412 if (HasCounter) {
5413 // Will this be in the correct order?
5414 uint32_t CounterOrderID = getNextImplicitBindingOrderID();
5415 IntegerLiteral *CounterId =
5416 IntegerLiteral::Create(AST, llvm::APInt(UIntTySize, CounterOrderID),
5418 Args.push_back(CounterId);
5419 }
5420
5421 // Make sure the create method template is instantiated and emitted.
5422 if (!CreateMethod->isDefined() && CreateMethod->isTemplateInstantiation())
5423 SemaRef.InstantiateFunctionDefinition(VD->getLocation(), CreateMethod,
5424 true);
5425
5426 // Create CallExpr with a call to the static method and set it as the decl
5427 // initialization.
5429 AST, NestedNameSpecifierLoc(), SourceLocation(), CreateMethod, false,
5430 CreateMethod->getNameInfo(), CreateMethod->getType(), VK_PRValue);
5431
5432 auto *ImpCast = ImplicitCastExpr::Create(
5433 AST, AST.getPointerType(CreateMethod->getType()),
5434 CK_FunctionToPointerDecay, DRE, nullptr, VK_PRValue, FPOptionsOverride());
5435
5436 CallExpr *InitExpr =
5437 CallExpr::Create(AST, ImpCast, Args, ResourceTy, VK_PRValue,
5439 VD->setInit(InitExpr);
5441 SemaRef.CheckCompleteVariableDeclaration(VD);
5442 return true;
5443}
5444
5446 assert(VD->getType()->isHLSLResourceRecordArray() &&
5447 "expected array of resource records");
5448
5449 // Individual resources in a resource array are not initialized here. They
5450 // are initialized later on during codegen when the individual resources are
5451 // accessed. Codegen will emit a call to the resource initialization method
5452 // with the specified array index. We need to make sure though that the method
5453 // for the specific resource type is instantiated, so codegen can emit a call
5454 // to it when the array element is accessed.
5455
5456 // Find correct initialization method based on the resource binding
5457 // information.
5458 ASTContext &AST = SemaRef.getASTContext();
5459 QualType ResElementTy = AST.getBaseElementType(VD->getType());
5460 CXXRecordDecl *ResourceDecl = ResElementTy->getAsCXXRecordDecl();
5461 CXXMethodDecl *CreateMethod = nullptr;
5462
5463 bool HasCounter = hasCounterHandle(ResourceDecl);
5464 ResourceBindingAttrs ResourceAttrs(VD);
5465 if (ResourceAttrs.isExplicit())
5466 // Resource has explicit binding.
5467 CreateMethod =
5468 lookupMethod(SemaRef, ResourceDecl,
5469 HasCounter ? "__createFromBindingWithImplicitCounter"
5470 : "__createFromBinding",
5471 VD->getLocation());
5472 else
5473 // Resource has implicit binding.
5474 CreateMethod = lookupMethod(
5475 SemaRef, ResourceDecl,
5476 HasCounter ? "__createFromImplicitBindingWithImplicitCounter"
5477 : "__createFromImplicitBinding",
5478 VD->getLocation());
5479
5480 if (!CreateMethod)
5481 return false;
5482
5483 // Make sure the create method template is instantiated and emitted.
5484 if (!CreateMethod->isDefined() && CreateMethod->isTemplateInstantiation())
5485 SemaRef.InstantiateFunctionDefinition(VD->getLocation(), CreateMethod,
5486 true);
5487 return true;
5488}
5489
5490// Returns true if the initialization has been handled.
5491// Returns false to use default initialization.
5493 // Objects in the hlsl_constant address space are initialized
5494 // externally, so don't synthesize an implicit initializer.
5496 return true;
5497
5498 // Initialize non-static resources at the global scope.
5499 if (VD->hasGlobalStorage() && VD->getStorageClass() != SC_Static) {
5500 const Type *Ty = VD->getType().getTypePtr();
5501 if (Ty->isHLSLResourceRecord())
5502 return initGlobalResourceDecl(VD);
5503 if (Ty->isHLSLResourceRecordArray())
5504 return initGlobalResourceArrayDecl(VD);
5505 }
5506 return false;
5507}
5508
5509std::optional<const DeclBindingInfo *> SemaHLSL::inferGlobalBinding(Expr *E) {
5510 if (auto *Ternary = dyn_cast<ConditionalOperator>(E)) {
5511 auto TrueInfo = inferGlobalBinding(Ternary->getTrueExpr());
5512 auto FalseInfo = inferGlobalBinding(Ternary->getFalseExpr());
5513 if (!TrueInfo || !FalseInfo)
5514 return std::nullopt;
5515 if (*TrueInfo != *FalseInfo)
5516 return std::nullopt;
5517 return TrueInfo;
5518 }
5519
5520 if (auto *ASE = dyn_cast<ArraySubscriptExpr>(E))
5521 E = ASE->getBase()->IgnoreParenImpCasts();
5522
5523 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E->IgnoreParens()))
5524 if (VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
5525 const Type *Ty = VD->getType()->getUnqualifiedDesugaredType();
5526 if (Ty->isArrayType())
5528
5529 if (const auto *AttrResType =
5530 HLSLAttributedResourceType::findHandleTypeOnResource(Ty)) {
5531 ResourceClass RC = AttrResType->getAttrs().ResourceClass;
5532 return Bindings.getDeclBindingInfo(VD, RC);
5533 }
5534 }
5535
5536 return nullptr;
5537}
5538
5539void SemaHLSL::trackLocalResource(VarDecl *VD, Expr *E) {
5540 std::optional<const DeclBindingInfo *> ExprBinding = inferGlobalBinding(E);
5541 if (!ExprBinding) {
5542 SemaRef.Diag(E->getBeginLoc(),
5543 diag::warn_hlsl_assigning_local_resource_is_not_unique)
5544 << E << VD;
5545 return; // Expr use multiple resources
5546 }
5547
5548 if (*ExprBinding == nullptr)
5549 return; // No binding could be inferred to track, return without error
5550
5551 auto PrevBinding = Assigns.find(VD);
5552 if (PrevBinding == Assigns.end()) {
5553 // No previous binding recorded, simply record the new assignment
5554 Assigns.insert({VD, *ExprBinding});
5555 return;
5556 }
5557
5558 // Otherwise, warn if the assignment implies different resource bindings
5559 if (*ExprBinding != PrevBinding->second) {
5560 SemaRef.Diag(E->getBeginLoc(),
5561 diag::warn_hlsl_assigning_local_resource_is_not_unique)
5562 << E << VD;
5563 SemaRef.Diag(VD->getLocation(), diag::note_var_declared_here) << VD;
5564 return;
5565 }
5566
5567 return;
5568}
5569
5571 Expr *RHSExpr, SourceLocation Loc) {
5572 assert((LHSExpr->getType()->isHLSLResourceRecord() ||
5573 LHSExpr->getType()->isHLSLResourceRecordArray()) &&
5574 "expected LHS to be a resource record or array of resource records");
5575 if (Opc != BO_Assign)
5576 return true;
5577
5578 // If LHS is an array subscript, get the underlying declaration.
5579 Expr *E = LHSExpr;
5580 while (auto *ASE = dyn_cast<ArraySubscriptExpr>(E))
5581 E = ASE->getBase()->IgnoreParenImpCasts();
5582
5583 // Report error if LHS is a non-static resource declared at a global scope.
5584 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E->IgnoreParens())) {
5585 if (VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
5586 if (VD->hasGlobalStorage() && VD->getStorageClass() != SC_Static) {
5587 // assignment to global resource is not allowed
5588 SemaRef.Diag(Loc, diag::err_hlsl_assign_to_global_resource) << VD;
5589 SemaRef.Diag(VD->getLocation(), diag::note_var_declared_here) << VD;
5590 return false;
5591 }
5592
5593 trackLocalResource(VD, RHSExpr);
5594 }
5595 }
5596 return true;
5597}
5598
5599// Walks though the global variable declaration, collects all resource binding
5600// requirements and adds them to Bindings
5601void SemaHLSL::collectResourceBindingsOnVarDecl(VarDecl *VD) {
5602 assert(VD->hasGlobalStorage() && VD->getType()->isHLSLIntangibleType() &&
5603 "expected global variable that contains HLSL resource");
5604
5605 // Cbuffers and Tbuffers are HLSLBufferDecl types
5606 if (const HLSLBufferDecl *CBufferOrTBuffer = dyn_cast<HLSLBufferDecl>(VD)) {
5607 Bindings.addDeclBindingInfo(VD, CBufferOrTBuffer->isCBuffer()
5608 ? ResourceClass::CBuffer
5609 : ResourceClass::SRV);
5610 return;
5611 }
5612
5613 // Unwrap arrays
5614 // FIXME: Calculate array size while unwrapping
5615 const Type *Ty = VD->getType()->getUnqualifiedDesugaredType();
5616 while (Ty->isArrayType()) {
5617 const ArrayType *AT = cast<ArrayType>(Ty);
5619 }
5620
5621 // Resource (or array of resources)
5622 if (const HLSLAttributedResourceType *AttrResType =
5623 HLSLAttributedResourceType::findHandleTypeOnResource(Ty)) {
5624 Bindings.addDeclBindingInfo(VD, AttrResType->getAttrs().ResourceClass);
5625 return;
5626 }
5627
5628 // User defined record type
5629 if (const RecordType *RT = dyn_cast<RecordType>(Ty))
5630 collectResourceBindingsOnUserRecordDecl(VD, RT);
5631}
5632
5633// Walks though the explicit resource binding attributes on the declaration,
5634// and makes sure there is a resource that matched the binding and updates
5635// DeclBindingInfoLists
5636void SemaHLSL::processExplicitBindingsOnDecl(VarDecl *VD) {
5637 assert(VD->hasGlobalStorage() && "expected global variable");
5638
5639 bool HasBinding = false;
5640 for (Attr *A : VD->attrs()) {
5641 if (isa<HLSLVkBindingAttr>(A)) {
5642 HasBinding = true;
5643 if (auto PA = VD->getAttr<HLSLVkPushConstantAttr>())
5644 Diag(PA->getLoc(), diag::err_hlsl_attr_incompatible) << A << PA;
5645 }
5646
5647 HLSLResourceBindingAttr *RBA = dyn_cast<HLSLResourceBindingAttr>(A);
5648 if (!RBA || !RBA->hasRegisterSlot())
5649 continue;
5650 HasBinding = true;
5651
5652 RegisterType RT = RBA->getRegisterType();
5653 assert(RT != RegisterType::I && "invalid or obsolete register type should "
5654 "never have an attribute created");
5655
5656 if (RT == RegisterType::C) {
5657 if (Bindings.hasBindingInfoForDecl(VD))
5658 SemaRef.Diag(VD->getLocation(),
5659 diag::warn_hlsl_user_defined_type_missing_member)
5660 << static_cast<int>(RT);
5661 continue;
5662 }
5663
5664 // Find DeclBindingInfo for this binding and update it, or report error
5665 // if it does not exist (user type does to contain resources with the
5666 // expected resource class).
5668 if (DeclBindingInfo *BI = Bindings.getDeclBindingInfo(VD, RC)) {
5669 // update binding info
5670 BI->setBindingAttribute(RBA, BindingType::Explicit);
5671 } else {
5672 SemaRef.Diag(VD->getLocation(),
5673 diag::warn_hlsl_user_defined_type_missing_member)
5674 << static_cast<int>(RT);
5675 }
5676 }
5677
5678 if (!HasBinding && isResourceRecordTypeOrArrayOf(VD))
5679 SemaRef.Diag(VD->getLocation(), diag::warn_hlsl_implicit_binding);
5680}
5681namespace {
5682class InitListTransformer {
5683 Sema &S;
5684 ASTContext &Ctx;
5685 QualType InitTy;
5686 QualType *DstIt = nullptr;
5687 Expr **ArgIt = nullptr;
5688 // Is wrapping the destination type iterator required? This is only used for
5689 // incomplete array types where we loop over the destination type since we
5690 // don't know the full number of elements from the declaration.
5691 bool Wrap;
5692
5693 bool castInitializer(Expr *E) {
5694 assert(DstIt && "This should always be something!");
5695 if (DstIt == DestTypes.end()) {
5696 if (!Wrap) {
5697 ArgExprs.push_back(E);
5698 // This is odd, but it isn't technically a failure due to conversion, we
5699 // handle mismatched counts of arguments differently.
5700 return true;
5701 }
5702 DstIt = DestTypes.begin();
5703 }
5704 InitializedEntity Entity = InitializedEntity::InitializeParameter(
5705 Ctx, *DstIt, /* Consumed (ObjC) */ false);
5706 ExprResult Res = S.PerformCopyInitialization(Entity, E->getBeginLoc(), E);
5707 if (Res.isInvalid())
5708 return false;
5709 Expr *Init = Res.get();
5710 ArgExprs.push_back(Init);
5711 DstIt++;
5712 return true;
5713 }
5714
5715 bool buildInitializerListImpl(Expr *E) {
5716 // If this is an initialization list, traverse the sub initializers.
5717 if (auto *Init = dyn_cast<InitListExpr>(E)) {
5718 for (auto *SubInit : Init->inits())
5719 if (!buildInitializerListImpl(SubInit))
5720 return false;
5721 return true;
5722 }
5723
5724 // If this is a scalar type, just enqueue the expression.
5725 QualType Ty = E->getType().getDesugaredType(Ctx);
5726
5727 if (Ty->isScalarType() || (Ty->isRecordType() && !Ty->isAggregateType()) ||
5729 return castInitializer(E);
5730
5731 if (auto *VecTy = Ty->getAs<VectorType>()) {
5732 uint64_t Size = VecTy->getNumElements();
5733
5734 QualType SizeTy = Ctx.getSizeType();
5735 uint64_t SizeTySize = Ctx.getTypeSize(SizeTy);
5736 for (uint64_t I = 0; I < Size; ++I) {
5737 auto *Idx = IntegerLiteral::Create(Ctx, llvm::APInt(SizeTySize, I),
5738 SizeTy, SourceLocation());
5739
5741 E, E->getBeginLoc(), Idx, E->getEndLoc());
5742 if (ElExpr.isInvalid())
5743 return false;
5744 if (!castInitializer(ElExpr.get()))
5745 return false;
5746 }
5747 return true;
5748 }
5749 if (auto *MTy = Ty->getAs<ConstantMatrixType>()) {
5750 unsigned Rows = MTy->getNumRows();
5751 unsigned Cols = MTy->getNumColumns();
5752 QualType ElemTy = MTy->getElementType();
5753
5754 for (unsigned R = 0; R < Rows; ++R) {
5755 for (unsigned C = 0; C < Cols; ++C) {
5756 // row index literal
5757 Expr *RowIdx = IntegerLiteral::Create(
5758 Ctx, llvm::APInt(Ctx.getIntWidth(Ctx.IntTy), R), Ctx.IntTy,
5759 E->getBeginLoc());
5760 // column index literal
5761 Expr *ColIdx = IntegerLiteral::Create(
5762 Ctx, llvm::APInt(Ctx.getIntWidth(Ctx.IntTy), C), Ctx.IntTy,
5763 E->getBeginLoc());
5765 E, RowIdx, ColIdx, E->getEndLoc());
5766 if (ElExpr.isInvalid())
5767 return false;
5768 if (!castInitializer(ElExpr.get()))
5769 return false;
5770 ElExpr.get()->setType(ElemTy);
5771 }
5772 }
5773 return true;
5774 }
5775
5776 if (auto *ArrTy = dyn_cast<ConstantArrayType>(Ty.getTypePtr())) {
5777 uint64_t Size = ArrTy->getZExtSize();
5778 QualType SizeTy = Ctx.getSizeType();
5779 uint64_t SizeTySize = Ctx.getTypeSize(SizeTy);
5780 for (uint64_t I = 0; I < Size; ++I) {
5781 auto *Idx = IntegerLiteral::Create(Ctx, llvm::APInt(SizeTySize, I),
5782 SizeTy, SourceLocation());
5784 E, E->getBeginLoc(), Idx, E->getEndLoc());
5785 if (ElExpr.isInvalid())
5786 return false;
5787 if (!buildInitializerListImpl(ElExpr.get()))
5788 return false;
5789 }
5790 return true;
5791 }
5792
5793 if (auto *RD = Ty->getAsCXXRecordDecl()) {
5794 llvm::SmallVector<CXXRecordDecl *> RecordDecls;
5795 RecordDecls.push_back(RD);
5796 // If this is a prvalue create an xvalue so the member accesses
5797 // will be xvalues.
5798 if (E->isPRValue())
5799 E = new (Ctx)
5800 MaterializeTemporaryExpr(Ty, E, /*BoundToLvalueReference=*/false);
5801 while (RecordDecls.back()->getNumBases()) {
5802 CXXRecordDecl *D = RecordDecls.back();
5803 assert(D->getNumBases() == 1 &&
5804 "HLSL doesn't support multiple inheritance");
5805 RecordDecls.push_back(
5807 }
5808 while (!RecordDecls.empty()) {
5809 CXXRecordDecl *RD = RecordDecls.pop_back_val();
5810 for (auto *FD : RD->fields()) {
5811 if (FD->isUnnamedBitField())
5812 continue;
5813 DeclAccessPair Found = DeclAccessPair::make(FD, FD->getAccess());
5814 DeclarationNameInfo NameInfo(FD->getDeclName(), E->getBeginLoc());
5816 E, false, E->getBeginLoc(), CXXScopeSpec(), FD, Found, NameInfo);
5817 if (Res.isInvalid())
5818 return false;
5819 if (!buildInitializerListImpl(Res.get()))
5820 return false;
5821 }
5822 }
5823 }
5824 return true;
5825 }
5826
5827 Expr *generateInitListsImpl(QualType Ty) {
5828 Ty = Ty.getDesugaredType(Ctx);
5829 assert(ArgIt != ArgExprs.end() && "Something is off in iteration!");
5830 if (Ty->isScalarType() || (Ty->isRecordType() && !Ty->isAggregateType()) ||
5832 return *(ArgIt++);
5833
5834 llvm::SmallVector<Expr *> Inits;
5835 if (Ty->isVectorType() || Ty->isConstantArrayType() ||
5836 Ty->isConstantMatrixType()) {
5837 QualType ElTy;
5838 uint64_t Size = 0;
5839 if (auto *ATy = Ty->getAs<VectorType>()) {
5840 ElTy = ATy->getElementType();
5841 Size = ATy->getNumElements();
5842 } else if (auto *CMTy = Ty->getAs<ConstantMatrixType>()) {
5843 ElTy = CMTy->getElementType();
5844 Size = CMTy->getNumElementsFlattened();
5845 } else {
5846 auto *VTy = cast<ConstantArrayType>(Ty.getTypePtr());
5847 ElTy = VTy->getElementType();
5848 Size = VTy->getZExtSize();
5849 }
5850 for (uint64_t I = 0; I < Size; ++I)
5851 Inits.push_back(generateInitListsImpl(ElTy));
5852 }
5853 if (auto *RD = Ty->getAsCXXRecordDecl()) {
5854 llvm::SmallVector<CXXRecordDecl *> RecordDecls;
5855 RecordDecls.push_back(RD);
5856 while (RecordDecls.back()->getNumBases()) {
5857 CXXRecordDecl *D = RecordDecls.back();
5858 assert(D->getNumBases() == 1 &&
5859 "HLSL doesn't support multiple inheritance");
5860 RecordDecls.push_back(
5862 }
5863 while (!RecordDecls.empty()) {
5864 CXXRecordDecl *RD = RecordDecls.pop_back_val();
5865 for (auto *FD : RD->fields())
5866 if (!FD->isUnnamedBitField())
5867 Inits.push_back(generateInitListsImpl(FD->getType()));
5868 }
5869 }
5870 auto *NewInit = new (Ctx) InitListExpr(Ctx, Inits.front()->getBeginLoc(),
5871 Inits, Inits.back()->getEndLoc());
5872 NewInit->setType(Ty);
5873 return NewInit;
5874 }
5875
5876public:
5877 llvm::SmallVector<QualType, 16> DestTypes;
5878 llvm::SmallVector<Expr *, 16> ArgExprs;
5879 InitListTransformer(Sema &SemaRef, const InitializedEntity &Entity)
5880 : S(SemaRef), Ctx(SemaRef.getASTContext()),
5881 Wrap(Entity.getType()->isIncompleteArrayType()) {
5882 InitTy = Entity.getType().getNonReferenceType();
5883 // When we're generating initializer lists for incomplete array types we
5884 // need to wrap around both when building the initializers and when
5885 // generating the final initializer lists.
5886 if (Wrap) {
5887 assert(InitTy->isIncompleteArrayType());
5888 const IncompleteArrayType *IAT = Ctx.getAsIncompleteArrayType(InitTy);
5889 InitTy = IAT->getElementType();
5890 }
5891 BuildFlattenedTypeList(InitTy, DestTypes);
5892 DstIt = DestTypes.begin();
5893 }
5894
5895 bool buildInitializerList(Expr *E) { return buildInitializerListImpl(E); }
5896
5897 Expr *generateInitLists() {
5898 assert(!ArgExprs.empty() &&
5899 "Call buildInitializerList to generate argument expressions.");
5900 ArgIt = ArgExprs.begin();
5901 if (!Wrap)
5902 return generateInitListsImpl(InitTy);
5903 llvm::SmallVector<Expr *> Inits;
5904 while (ArgIt != ArgExprs.end())
5905 Inits.push_back(generateInitListsImpl(InitTy));
5906
5907 auto *NewInit = new (Ctx) InitListExpr(Ctx, Inits.front()->getBeginLoc(),
5908 Inits, Inits.back()->getEndLoc());
5909 llvm::APInt ArySize(64, Inits.size());
5910 NewInit->setType(Ctx.getConstantArrayType(InitTy, ArySize, nullptr,
5911 ArraySizeModifier::Normal, 0));
5912 return NewInit;
5913 }
5914};
5915} // namespace
5916
5917// Recursively detect any incomplete array anywhere in the type graph,
5918// including arrays, struct fields, and base classes.
5920 Ty = Ty.getCanonicalType();
5921
5922 // Array types
5923 if (const ArrayType *AT = dyn_cast<ArrayType>(Ty)) {
5925 return true;
5927 }
5928
5929 // Record (struct/class) types
5930 if (const auto *RT = Ty->getAs<RecordType>()) {
5931 const RecordDecl *RD = RT->getDecl();
5932
5933 // Walk base classes (for C++ / HLSL structs with inheritance)
5934 if (const auto *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
5935 for (const CXXBaseSpecifier &Base : CXXRD->bases()) {
5936 if (containsIncompleteArrayType(Base.getType()))
5937 return true;
5938 }
5939 }
5940
5941 // Walk fields
5942 for (const FieldDecl *F : RD->fields()) {
5943 if (containsIncompleteArrayType(F->getType()))
5944 return true;
5945 }
5946 }
5947
5948 return false;
5949}
5950
5952 InitListExpr *Init) {
5953 // If the initializer is a scalar, just return it.
5954 if (Init->getType()->isScalarType())
5955 return true;
5956 ASTContext &Ctx = SemaRef.getASTContext();
5957 InitListTransformer ILT(SemaRef, Entity);
5958
5959 for (unsigned I = 0; I < Init->getNumInits(); ++I) {
5960 Expr *E = Init->getInit(I);
5961 if (E->HasSideEffects(Ctx)) {
5962 QualType Ty = E->getType();
5963 if (Ty->isRecordType())
5964 E = new (Ctx) MaterializeTemporaryExpr(Ty, E, E->isLValue());
5965 E = new (Ctx) OpaqueValueExpr(E->getBeginLoc(), Ty, E->getValueKind(),
5966 E->getObjectKind(), E);
5967 Init->setInit(I, E);
5968 }
5969 if (!ILT.buildInitializerList(E))
5970 return false;
5971 }
5972 size_t ExpectedSize = ILT.DestTypes.size();
5973 size_t ActualSize = ILT.ArgExprs.size();
5974 if (ExpectedSize == 0 && ActualSize == 0)
5975 return true;
5976
5977 // Reject empty initializer if *any* incomplete array exists structurally
5978 if (ActualSize == 0 && containsIncompleteArrayType(Entity.getType())) {
5979 QualType InitTy = Entity.getType().getNonReferenceType();
5980 if (InitTy.hasAddressSpace())
5981 InitTy = SemaRef.getASTContext().removeAddrSpaceQualType(InitTy);
5982
5983 SemaRef.Diag(Init->getBeginLoc(), diag::err_hlsl_incorrect_num_initializers)
5984 << /*TooManyOrFew=*/(int)(ExpectedSize < ActualSize) << InitTy
5985 << /*ExpectedSize=*/ExpectedSize << /*ActualSize=*/ActualSize;
5986 return false;
5987 }
5988
5989 // We infer size after validating legality.
5990 // For incomplete arrays it is completely arbitrary to choose whether we think
5991 // the user intended fewer or more elements. This implementation assumes that
5992 // the user intended more, and errors that there are too few initializers to
5993 // complete the final element.
5994 if (Entity.getType()->isIncompleteArrayType()) {
5995 assert(ExpectedSize > 0 &&
5996 "The expected size of an incomplete array type must be at least 1.");
5997 ExpectedSize =
5998 ((ActualSize + ExpectedSize - 1) / ExpectedSize) * ExpectedSize;
5999 }
6000
6001 // An initializer list might be attempting to initialize a reference or
6002 // rvalue-reference. When checking the initializer we should look through
6003 // the reference.
6004 QualType InitTy = Entity.getType().getNonReferenceType();
6005 if (InitTy.hasAddressSpace())
6006 InitTy = SemaRef.getASTContext().removeAddrSpaceQualType(InitTy);
6007 if (ExpectedSize != ActualSize) {
6008 int TooManyOrFew = ActualSize > ExpectedSize ? 1 : 0;
6009 SemaRef.Diag(Init->getBeginLoc(), diag::err_hlsl_incorrect_num_initializers)
6010 << TooManyOrFew << InitTy << ExpectedSize << ActualSize;
6011 return false;
6012 }
6013
6014 // generateInitListsImpl will always return an InitListExpr here, because the
6015 // scalar case is handled above.
6016 auto *NewInit = cast<InitListExpr>(ILT.generateInitLists());
6017 Init->resizeInits(Ctx, NewInit->getNumInits());
6018 for (unsigned I = 0; I < NewInit->getNumInits(); ++I)
6019 Init->updateInit(Ctx, I, NewInit->getInit(I));
6020 return true;
6021}
6022
6023static QualType ReportMatrixInvalidMember(Sema &S, StringRef Name,
6024 StringRef Expected,
6025 SourceLocation OpLoc,
6026 SourceLocation CompLoc) {
6027 S.Diag(OpLoc, diag::err_builtin_matrix_invalid_member)
6028 << Name << Expected << SourceRange(CompLoc);
6029 return QualType();
6030}
6031
6034 const IdentifierInfo *CompName,
6035 SourceLocation CompLoc) {
6036 const auto *MT = baseType->castAs<ConstantMatrixType>();
6037 StringRef AccessorName = CompName->getName();
6038 assert(!AccessorName.empty() && "Matrix Accessor must have a name");
6039
6040 unsigned Rows = MT->getNumRows();
6041 unsigned Cols = MT->getNumColumns();
6042 bool IsZeroBasedAccessor = false;
6043 unsigned ChunkLen = 0;
6044 if (AccessorName.size() < 2)
6045 return ReportMatrixInvalidMember(S, AccessorName,
6046 "length 4 for zero based: \'_mRC\' or "
6047 "length 3 for one-based: \'_RC\' accessor",
6048 OpLoc, CompLoc);
6049
6050 if (AccessorName[0] == '_') {
6051 if (AccessorName[1] == 'm') {
6052 IsZeroBasedAccessor = true;
6053 ChunkLen = 4; // zero-based: "_mRC"
6054 } else {
6055 ChunkLen = 3; // one-based: "_RC"
6056 }
6057 } else
6059 S, AccessorName, "zero based: \'_mRC\' or one-based: \'_RC\' accessor",
6060 OpLoc, CompLoc);
6061
6062 if (AccessorName.size() % ChunkLen != 0) {
6063 const llvm::StringRef Expected = IsZeroBasedAccessor
6064 ? "zero based: '_mRC' accessor"
6065 : "one-based: '_RC' accessor";
6066
6067 return ReportMatrixInvalidMember(S, AccessorName, Expected, OpLoc, CompLoc);
6068 }
6069
6070 auto isDigit = [](char c) { return c >= '0' && c <= '9'; };
6071 auto isZeroBasedIndex = [](unsigned i) { return i <= 3; };
6072 auto isOneBasedIndex = [](unsigned i) { return i >= 1 && i <= 4; };
6073
6074 bool HasRepeated = false;
6075 SmallVector<bool, 16> Seen(Rows * Cols, false);
6076 unsigned NumComponents = 0;
6077 const char *Begin = AccessorName.data();
6078
6079 for (unsigned I = 0, E = AccessorName.size(); I < E; I += ChunkLen) {
6080 const char *Chunk = Begin + I;
6081 char RowChar = 0, ColChar = 0;
6082 if (IsZeroBasedAccessor) {
6083 // Zero-based: "_mRC"
6084 if (Chunk[0] != '_' || Chunk[1] != 'm') {
6085 char Bad = (Chunk[0] != '_') ? Chunk[0] : Chunk[1];
6087 S, StringRef(&Bad, 1), "\'_m\' prefix",
6088 OpLoc.getLocWithOffset(I + (Bad == Chunk[0] ? 1 : 2)), CompLoc);
6089 }
6090 RowChar = Chunk[2];
6091 ColChar = Chunk[3];
6092 } else {
6093 // One-based: "_RC"
6094 if (Chunk[0] != '_')
6096 S, StringRef(&Chunk[0], 1), "\'_\' prefix",
6097 OpLoc.getLocWithOffset(I + 1), CompLoc);
6098 RowChar = Chunk[1];
6099 ColChar = Chunk[2];
6100 }
6101
6102 // Must be digits.
6103 bool IsDigitsError = false;
6104 if (!isDigit(RowChar)) {
6105 unsigned BadPos = IsZeroBasedAccessor ? 2 : 1;
6106 ReportMatrixInvalidMember(S, StringRef(&RowChar, 1), "row as integer",
6107 OpLoc.getLocWithOffset(I + BadPos + 1),
6108 CompLoc);
6109 IsDigitsError = true;
6110 }
6111
6112 if (!isDigit(ColChar)) {
6113 unsigned BadPos = IsZeroBasedAccessor ? 3 : 2;
6114 ReportMatrixInvalidMember(S, StringRef(&ColChar, 1), "column as integer",
6115 OpLoc.getLocWithOffset(I + BadPos + 1),
6116 CompLoc);
6117 IsDigitsError = true;
6118 }
6119 if (IsDigitsError)
6120 return QualType();
6121
6122 unsigned Row = RowChar - '0';
6123 unsigned Col = ColChar - '0';
6124
6125 bool HasIndexingError = false;
6126 if (IsZeroBasedAccessor) {
6127 // 0-based [0..3]
6128 if (!isZeroBasedIndex(Row)) {
6129 S.Diag(OpLoc, diag::err_hlsl_matrix_element_not_in_bounds)
6130 << /*row*/ 0 << /*zero-based*/ 0 << SourceRange(CompLoc);
6131 HasIndexingError = true;
6132 }
6133 if (!isZeroBasedIndex(Col)) {
6134 S.Diag(OpLoc, diag::err_hlsl_matrix_element_not_in_bounds)
6135 << /*col*/ 1 << /*zero-based*/ 0 << SourceRange(CompLoc);
6136 HasIndexingError = true;
6137 }
6138 } else {
6139 // 1-based [1..4]
6140 if (!isOneBasedIndex(Row)) {
6141 S.Diag(OpLoc, diag::err_hlsl_matrix_element_not_in_bounds)
6142 << /*row*/ 0 << /*one-based*/ 1 << SourceRange(CompLoc);
6143 HasIndexingError = true;
6144 }
6145 if (!isOneBasedIndex(Col)) {
6146 S.Diag(OpLoc, diag::err_hlsl_matrix_element_not_in_bounds)
6147 << /*col*/ 1 << /*one-based*/ 1 << SourceRange(CompLoc);
6148 HasIndexingError = true;
6149 }
6150 // Convert to 0-based after range checking.
6151 --Row;
6152 --Col;
6153 }
6154
6155 if (HasIndexingError)
6156 return QualType();
6157
6158 // Note: matrix swizzle index is hard coded. That means Row and Col can
6159 // potentially be larger than Rows and Cols if matrix size is less than
6160 // the max index size.
6161 bool HasBoundsError = false;
6162 if (Row >= Rows) {
6163 Diag(OpLoc, diag::err_hlsl_matrix_index_out_of_bounds)
6164 << /*Row*/ 0 << Row << Rows << SourceRange(CompLoc);
6165 HasBoundsError = true;
6166 }
6167 if (Col >= Cols) {
6168 Diag(OpLoc, diag::err_hlsl_matrix_index_out_of_bounds)
6169 << /*Col*/ 1 << Col << Cols << SourceRange(CompLoc);
6170 HasBoundsError = true;
6171 }
6172 if (HasBoundsError)
6173 return QualType();
6174
6175 unsigned FlatIndex = Row * Cols + Col;
6176 if (Seen[FlatIndex])
6177 HasRepeated = true;
6178 Seen[FlatIndex] = true;
6179 ++NumComponents;
6180 }
6181 if (NumComponents == 0 || NumComponents > 4) {
6182 S.Diag(OpLoc, diag::err_hlsl_matrix_swizzle_invalid_length)
6183 << NumComponents << SourceRange(CompLoc);
6184 return QualType();
6185 }
6186
6187 QualType ElemTy = MT->getElementType();
6188 if (NumComponents == 1)
6189 return ElemTy;
6190 QualType VT = S.Context.getExtVectorType(ElemTy, NumComponents);
6191 if (HasRepeated)
6192 VK = VK_PRValue;
6193
6194 for (Sema::ExtVectorDeclsType::iterator
6196 E = S.ExtVectorDecls.end();
6197 I != E; ++I) {
6198 if ((*I)->getUnderlyingType() == VT)
6200 /*Qualifier=*/std::nullopt, *I);
6201 }
6202
6203 return VT;
6204}
6205
6207 // If initializing a local resource, track the resource binding it is using
6208 if (VDecl->getType()->isHLSLResourceRecord() && !VDecl->hasGlobalStorage())
6209 trackLocalResource(VDecl, Init);
6210
6211 const HLSLVkConstantIdAttr *ConstIdAttr =
6212 VDecl->getAttr<HLSLVkConstantIdAttr>();
6213 if (!ConstIdAttr)
6214 return true;
6215
6216 ASTContext &Context = SemaRef.getASTContext();
6217
6218 APValue InitValue;
6219 if (!Init->isCXX11ConstantExpr(Context, &InitValue)) {
6220 Diag(VDecl->getLocation(), diag::err_specialization_const);
6221 VDecl->setInvalidDecl();
6222 return false;
6223 }
6224
6225 Builtin::ID BID =
6227
6228 // Argument 1: The ID from the attribute
6229 int ConstantID = ConstIdAttr->getId();
6230 llvm::APInt IDVal(Context.getIntWidth(Context.IntTy), ConstantID);
6231 Expr *IdExpr = IntegerLiteral::Create(Context, IDVal, Context.IntTy,
6232 ConstIdAttr->getLocation());
6233
6234 SmallVector<Expr *, 2> Args = {IdExpr, Init};
6235 Expr *C = SemaRef.BuildBuiltinCallExpr(Init->getExprLoc(), BID, Args);
6236 if (C->getType()->getCanonicalTypeUnqualified() !=
6238 C = SemaRef
6239 .BuildCStyleCastExpr(SourceLocation(),
6240 Context.getTrivialTypeSourceInfo(
6241 Init->getType(), Init->getExprLoc()),
6242 SourceLocation(), C)
6243 .get();
6244 }
6245 Init = C;
6246 return true;
6247}
6248
6250 SourceLocation NameLoc) {
6251 if (!Template)
6252 return QualType();
6253
6254 DeclContext *DC = Template->getDeclContext();
6255 if (!DC->isNamespace() || !cast<NamespaceDecl>(DC)->getIdentifier() ||
6256 cast<NamespaceDecl>(DC)->getName() != "hlsl")
6257 return QualType();
6258
6259 TemplateParameterList *Params = Template->getTemplateParameters();
6260 if (!Params || Params->size() != 1)
6261 return QualType();
6262
6263 if (!Template->isImplicit())
6264 return QualType();
6265
6266 // We manually extract default arguments here instead of letting
6267 // CheckTemplateIdType handle it. This ensures that for resource types that
6268 // lack a default argument (like Buffer), we return a null QualType, which
6269 // triggers the "requires template arguments" error rather than a less
6270 // descriptive "too few template arguments" error.
6271 TemplateArgumentListInfo TemplateArgs(NameLoc, NameLoc);
6272 for (NamedDecl *P : *Params) {
6273 if (auto *TTP = dyn_cast<TemplateTypeParmDecl>(P)) {
6274 if (TTP->hasDefaultArgument()) {
6275 TemplateArgs.addArgument(TTP->getDefaultArgument());
6276 continue;
6277 }
6278 } else if (auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P)) {
6279 if (NTTP->hasDefaultArgument()) {
6280 TemplateArgs.addArgument(NTTP->getDefaultArgument());
6281 continue;
6282 }
6283 } else if (auto *TTPD = dyn_cast<TemplateTemplateParmDecl>(P)) {
6284 if (TTPD->hasDefaultArgument()) {
6285 TemplateArgs.addArgument(TTPD->getDefaultArgument());
6286 continue;
6287 }
6288 }
6289 return QualType();
6290 }
6291
6292 return SemaRef.CheckTemplateIdType(
6294 TemplateArgs, nullptr, /*ForNestedNameSpecifier=*/false);
6295}
Defines the clang::ASTContext interface.
Defines enum values for all the target-independent builtin functions.
llvm::dxil::ResourceClass ResourceClass
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate....
TokenType getType() const
Returns the token's type, e.g.
FormatToken * Previous
The previous token in the unwrapped line.
Defines the clang::IdentifierInfo, clang::IdentifierTable, and clang::Selector interfaces.
#define X(type, name)
Definition Value.h:97
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified.
llvm::SmallVector< std::pair< const MemRegion *, SVal >, 4 > Bindings
static bool CheckArgTypeMatches(Sema *S, Expr *Arg, QualType ExpectedType)
static void BuildFlattenedTypeList(QualType BaseTy, llvm::SmallVectorImpl< QualType > &List)
static bool CheckUnsignedIntRepresentation(Sema *S, SourceLocation Loc, int ArgOrdinal, clang::QualType PassedType)
static bool containsIncompleteArrayType(QualType Ty)
static QualType handleIntegerVectorBinOpConversion(Sema &SemaRef, ExprResult &LHS, ExprResult &RHS, QualType LHSType, QualType RHSType, QualType LElTy, QualType RElTy, bool IsCompAssign)
static bool convertToRegisterType(StringRef Slot, RegisterType *RT)
Definition SemaHLSL.cpp:82
static StringRef createRegisterString(ASTContext &AST, RegisterType RegType, unsigned N)
Definition SemaHLSL.cpp:184
static bool CheckWaveActive(Sema *S, CallExpr *TheCall)
static void createHostLayoutStructForBuffer(Sema &S, HLSLBufferDecl *BufDecl)
Definition SemaHLSL.cpp:609
static void castVector(Sema &S, ExprResult &E, QualType &Ty, unsigned Sz)
static QualType ReportMatrixInvalidMember(Sema &S, StringRef Name, StringRef Expected, SourceLocation OpLoc, SourceLocation CompLoc)
static bool CheckBoolSelect(Sema *S, CallExpr *TheCall)
static unsigned calculateLegacyCbufferFieldAlign(const ASTContext &Context, QualType T)
Definition SemaHLSL.cpp:246
static bool isZeroSizedArray(const ConstantArrayType *CAT)
Definition SemaHLSL.cpp:365
static bool DiagnoseHLSLRegisterAttribute(Sema &S, SourceLocation &ArgLoc, Decl *D, RegisterType RegType, bool SpecifiedSpace)
static bool hasConstantBufferLayout(QualType QT)
static FieldDecl * createFieldForHostLayoutStruct(Sema &S, const Type *Ty, IdentifierInfo *II, CXXRecordDecl *LayoutStruct)
Definition SemaHLSL.cpp:517
static bool CheckUnsignedIntVecRepresentation(Sema *S, SourceLocation Loc, int ArgOrdinal, clang::QualType PassedType)
SampleKind
static bool isInvalidConstantBufferLeafElementType(const Type *Ty)
Definition SemaHLSL.cpp:399
static bool CheckCalculateLodBuiltin(Sema &S, CallExpr *TheCall)
static Builtin::ID getSpecConstBuiltinId(const Type *Type)
Definition SemaHLSL.cpp:150
static bool CheckFloatingOrIntRepresentation(Sema *S, SourceLocation Loc, int ArgOrdinal, clang::QualType PassedType)
static const Type * createHostLayoutType(Sema &S, const Type *Ty)
Definition SemaHLSL.cpp:490
static bool CheckAnyScalarOrVector(Sema *S, CallExpr *TheCall, unsigned ArgIndex)
static const HLSLAttributedResourceType * getResourceArrayHandleType(QualType QT)
Definition SemaHLSL.cpp:381
static IdentifierInfo * getHostLayoutStructName(Sema &S, NamedDecl *BaseDecl, bool MustBeUnique)
Definition SemaHLSL.cpp:455
static void addImplicitBindingAttrToDecl(Sema &S, Decl *D, RegisterType RT, uint32_t ImplicitBindingOrderID)
Definition SemaHLSL.cpp:653
static void SetElementTypeAsReturnType(Sema *S, CallExpr *TheCall, QualType ReturnType)
static unsigned calculateLegacyCbufferSize(const ASTContext &Context, QualType T)
Definition SemaHLSL.cpp:265
static bool CheckLoadLevelBuiltin(Sema &S, CallExpr *TheCall)
static RegisterType getRegisterType(ResourceClass RC)
Definition SemaHLSL.cpp:62
static bool ValidateRegisterNumber(uint64_t SlotNum, Decl *TheDecl, ASTContext &Ctx, RegisterType RegTy)
static bool isVkPipelineBuiltin(const ASTContext &AstContext, FunctionDecl *FD, HLSLAppliedSemanticAttr *Semantic, bool IsInput)
Definition SemaHLSL.cpp:841
static bool CheckVectorElementCount(Sema *S, QualType PassedType, QualType BaseType, unsigned ExpectedCount, SourceLocation Loc)
static bool CheckModifiableLValue(Sema *S, CallExpr *TheCall, unsigned ArgIndex)
static QualType castElement(Sema &S, ExprResult &E, QualType Ty)
static char getRegisterTypeChar(RegisterType RT)
Definition SemaHLSL.cpp:114
static bool CheckNotBoolScalarOrVector(Sema *S, CallExpr *TheCall, unsigned ArgIndex)
static CXXRecordDecl * findRecordDeclInContext(IdentifierInfo *II, DeclContext *DC)
Definition SemaHLSL.cpp:438
static bool CheckWavePrefix(Sema *S, CallExpr *TheCall)
static bool CheckExpectedBitWidth(Sema *S, CallExpr *TheCall, unsigned ArgOrdinal, unsigned Width)
static bool hasCounterHandle(const CXXRecordDecl *RD)
static bool CheckVectorSelect(Sema *S, CallExpr *TheCall)
static QualType handleFloatVectorBinOpConversion(Sema &SemaRef, ExprResult &LHS, ExprResult &RHS, QualType LHSType, QualType RHSType, QualType LElTy, QualType RElTy, bool IsCompAssign)
static ResourceClass getResourceClass(RegisterType RT)
Definition SemaHLSL.cpp:132
static CXXRecordDecl * createHostLayoutStruct(Sema &S, CXXRecordDecl *StructDecl)
Definition SemaHLSL.cpp:544
static bool CheckScalarOrVector(Sema *S, CallExpr *TheCall, QualType Scalar, unsigned ArgIndex)
static bool CheckSamplingBuiltin(Sema &S, CallExpr *TheCall, SampleKind Kind)
static bool CheckScalarOrVectorOrMatrix(Sema *S, CallExpr *TheCall, QualType Scalar, unsigned ArgIndex)
static bool CheckFloatRepresentation(Sema *S, SourceLocation Loc, int ArgOrdinal, clang::QualType PassedType)
static bool CheckAnyDoubleRepresentation(Sema *S, SourceLocation Loc, int ArgOrdinal, clang::QualType PassedType)
static bool requiresImplicitBufferLayoutStructure(const CXXRecordDecl *RD)
Definition SemaHLSL.cpp:418
static bool CheckResourceHandle(Sema *S, CallExpr *TheCall, unsigned ArgIndex, llvm::function_ref< bool(const HLSLAttributedResourceType *ResType)> Check=nullptr)
static void validatePackoffset(Sema &S, HLSLBufferDecl *BufDecl)
Definition SemaHLSL.cpp:312
static bool IsDefaultBufferConstantDecl(const ASTContext &Ctx, VarDecl *VD)
HLSLResourceBindingAttr::RegisterType RegisterType
Definition SemaHLSL.cpp:57
static CastKind getScalarCastKind(ASTContext &Ctx, QualType DestTy, QualType SrcTy)
static bool CheckGatherBuiltin(Sema &S, CallExpr *TheCall, bool IsCmp)
static bool isValidWaveSizeValue(unsigned Value)
static bool isResourceRecordTypeOrArrayOf(QualType Ty)
Definition SemaHLSL.cpp:372
static bool AccumulateHLSLResourceSlots(QualType Ty, uint64_t &StartSlot, const uint64_t &Limit, const ResourceClass ResClass, ASTContext &Ctx, uint64_t ArrayCount=1)
static bool CheckNoDoubleVectors(Sema *S, SourceLocation Loc, int ArgOrdinal, clang::QualType PassedType)
static bool ValidateMultipleRegisterAnnotations(Sema &S, Decl *TheDecl, RegisterType regType)
static bool CheckTextureSamplerAndLocation(Sema &S, CallExpr *TheCall)
static bool DiagnoseLocalRegisterBinding(Sema &S, SourceLocation &ArgLoc, Decl *D, RegisterType RegType, bool SpecifiedSpace)
static bool CheckIndexType(Sema *S, CallExpr *TheCall, unsigned IndexArgIndex)
This file declares semantic analysis for HLSL constructs.
Defines the clang::SourceLocation class and associated facilities.
Defines various enumerations that describe declaration and type specifiers.
C Language Family Type Representation.
Defines the clang::TypeLoc interface and its subclasses.
C Language Family Type Representation.
static const TypeInfo & getInfo(unsigned id)
Definition Types.cpp:44
__device__ __2f16 float c
return(__x > > __y)|(__x<<(32 - __y))
APValue - This class implements a discriminated union of [uninitialized] [APSInt] [APFloat],...
Definition APValue.h:122
virtual bool HandleTopLevelDecl(DeclGroupRef D)
HandleTopLevelDecl - Handle the specified top-level declaration.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition ASTContext.h:226
unsigned getIntWidth(QualType T) const
int getIntegerTypeOrder(QualType LHS, QualType RHS) const
Return the highest ranked integer type, see C99 6.3.1.8p1.
CanQualType FloatTy
QualType getPointerType(QualType T) const
Return the uniqued reference to the type for a pointer to the specified type.
const IncompleteArrayType * getAsIncompleteArrayType(QualType T) const
IdentifierTable & Idents
Definition ASTContext.h:798
QualType getConstantArrayType(QualType EltTy, const llvm::APInt &ArySize, const Expr *SizeExpr, ArraySizeModifier ASM, unsigned IndexTypeQuals) const
Return the unique reference to the type for a constant array of the specified element type.
QualType getBaseElementType(const ArrayType *VAT) const
Return the innermost element type of an array type.
int getFloatingTypeOrder(QualType LHS, QualType RHS) const
Compare the rank of the two specified floating point types, ignoring the domain of the type (i....
CanQualType BoolTy
TypeSourceInfo * getTrivialTypeSourceInfo(QualType T, SourceLocation Loc=SourceLocation()) const
Allocate a TypeSourceInfo where all locations have been initialized to a given location,...
QualType getStringLiteralArrayType(QualType EltTy, unsigned Length) const
Return a type for a constant array for a string literal of the specified element type and length.
CanQualType CharTy
CanQualType IntTy
uint64_t getTypeSize(QualType T) const
Return the size of the specified (complete) type T, in bits.
CharUnits getTypeSizeInChars(QualType T) const
Return the size of the specified (complete) type T, in characters.
CanQualType UnsignedIntTy
QualType getTypedefType(ElaboratedTypeKeyword Keyword, NestedNameSpecifier Qualifier, const TypedefNameDecl *Decl, QualType UnderlyingType=QualType(), std::optional< bool > TypeMatchesDeclOrNone=std::nullopt) const
Return the unique reference to the type for the specified typedef-name decl.
llvm::StringRef backupStr(llvm::StringRef S) const
Definition ASTContext.h:880
QualType getSizeType() const
Return the unique type for "size_t" (C99 7.17), defined in <stddef.h>.
QualType getExtVectorType(QualType VectorType, unsigned NumElts) const
Return the unique reference to an extended vector type of the specified element type and size.
const TargetInfo & getTargetInfo() const
Definition ASTContext.h:917
QualType getHLSLAttributedResourceType(QualType Wrapped, QualType Contained, const HLSLAttributedResourceType::Attributes &Attrs)
QualType getAddrSpaceQualType(QualType T, LangAS AddressSpace) const
Return the uniqued reference to the type for an address space qualified type with the specified type ...
CanQualType getCanonicalTagType(const TagDecl *TD) const
static bool hasSameUnqualifiedType(QualType T1, QualType T2)
Determine whether the given types are equivalent after cvr-qualifiers have been removed.
QualType getConstantMatrixType(QualType ElementType, unsigned NumRows, unsigned NumColumns) const
Return the unique reference to the matrix type of the specified element type and size.
unsigned getTypeAlign(QualType T) const
Return the ABI-specified alignment of a (complete) type T, in bits.
PtrTy get() const
Definition Ownership.h:171
bool isInvalid() const
Definition Ownership.h:167
Represents an array type, per C99 6.7.5.2 - Array Declarators.
Definition TypeBase.h:3772
QualType getElementType() const
Definition TypeBase.h:3784
Attr - This represents one attribute.
Definition Attr.h:46
attr::Kind getKind() const
Definition Attr.h:92
SourceLocation getLocation() const
Definition Attr.h:99
SourceLocation getScopeLoc() const
const IdentifierInfo * getScopeName() const
SourceLocation getLoc() const
const IdentifierInfo * getAttrName() const
Represents a base class of a C++ class.
Definition DeclCXX.h:146
QualType getType() const
Retrieves the type of the base class.
Definition DeclCXX.h:249
Represents a static or instance method of a struct/union/class.
Definition DeclCXX.h:2136
Represents a C++ struct/union/class.
Definition DeclCXX.h:258
bool isHLSLIntangible() const
Returns true if the class contains HLSL intangible type, either as a field or in base class.
Definition DeclCXX.h:1556
static CXXRecordDecl * Create(const ASTContext &C, TagKind TK, DeclContext *DC, SourceLocation StartLoc, SourceLocation IdLoc, IdentifierInfo *Id, CXXRecordDecl *PrevDecl=nullptr)
Definition DeclCXX.cpp:132
void setBases(CXXBaseSpecifier const *const *Bases, unsigned NumBases)
Sets the base classes of this struct or class.
Definition DeclCXX.cpp:184
base_class_iterator bases_end()
Definition DeclCXX.h:617
void completeDefinition() override
Indicates that the definition of this class is now complete.
Definition DeclCXX.cpp:2249
base_class_range bases()
Definition DeclCXX.h:608
unsigned getNumBases() const
Retrieves the number of base classes of this class.
Definition DeclCXX.h:602
base_class_iterator bases_begin()
Definition DeclCXX.h:615
bool isEmpty() const
Determine whether this is an empty class in the sense of (C++11 [meta.unary.prop]).
Definition DeclCXX.h:1186
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
Definition Expr.h:2946
Expr * getArg(unsigned Arg)
getArg - Return the specified argument.
Definition Expr.h:3150
SourceLocation getBeginLoc() const
Definition Expr.h:3280
static CallExpr * Create(const ASTContext &Ctx, Expr *Fn, ArrayRef< Expr * > Args, QualType Ty, ExprValueKind VK, SourceLocation RParenLoc, FPOptionsOverride FPFeatures, unsigned MinNumArgs=0, ADLCallKind UsesADL=NotADL)
Create a call expression.
Definition Expr.cpp:1517
FunctionDecl * getDirectCallee()
If the callee is a FunctionDecl, return it. Otherwise return null.
Definition Expr.h:3129
Expr * getCallee()
Definition Expr.h:3093
unsigned getNumArgs() const
getNumArgs - Return the number of actual arguments to this call.
Definition Expr.h:3137
Decl * getCalleeDecl()
Definition Expr.h:3123
QualType withConst() const
Retrieves a version of this type with const applied.
const T * getTypePtr() const
Retrieve the underlying type pointer, which refers to a canonical type.
QuantityType getQuantity() const
getQuantity - Get the raw integer representation of this quantity.
Definition CharUnits.h:185
Represents the canonical version of C arrays with a specified constant size.
Definition TypeBase.h:3810
bool isZeroSize() const
Return true if the size is zero.
Definition TypeBase.h:3880
llvm::APInt getSize() const
Return the constant array size as an APInt.
Definition TypeBase.h:3866
uint64_t getZExtSize() const
Return the size zero-extended as a uint64_t.
Definition TypeBase.h:3886
Represents a concrete matrix type with constant number of rows and columns.
Definition TypeBase.h:4437
unsigned getNumColumns() const
Returns the number of columns in the matrix.
Definition TypeBase.h:4456
static DeclAccessPair make(NamedDecl *D, AccessSpecifier AS)
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
Definition DeclBase.h:1462
bool isNamespace() const
Definition DeclBase.h:2211
lookup_result lookup(DeclarationName Name) const
lookup - Find the declarations (if any) with the given Name in this context.
bool isTranslationUnit() const
Definition DeclBase.h:2198
void addDecl(Decl *D)
Add the declaration D into this context.
decl_range decls() const
decls_begin/decls_end - Iterate over the declarations stored in this context.
Definition DeclBase.h:2386
DeclContext * getNonTransparentContext()
A reference to a declared variable, function, enum, etc.
Definition Expr.h:1273
static DeclRefExpr * Create(const ASTContext &Context, NestedNameSpecifierLoc QualifierLoc, SourceLocation TemplateKWLoc, ValueDecl *D, bool RefersToEnclosingVariableOrCapture, SourceLocation NameLoc, QualType T, ExprValueKind VK, NamedDecl *FoundD=nullptr, const TemplateArgumentListInfo *TemplateArgs=nullptr, NonOdrUseReason NOUR=NOUR_None)
Definition Expr.cpp:488
ValueDecl * getDecl()
Definition Expr.h:1341
Decl - This represents one declaration (or definition), e.g.
Definition DeclBase.h:86
T * getAttr() const
Definition DeclBase.h:581
ASTContext & getASTContext() const LLVM_READONLY
Definition DeclBase.cpp:547
void addAttr(Attr *A)
attr_iterator attr_end() const
Definition DeclBase.h:550
bool isImplicit() const
isImplicit - Indicates whether the declaration was implicitly generated by the implementation.
Definition DeclBase.h:601
void setInvalidDecl(bool Invalid=true)
setInvalidDecl - Indicates the Decl had a semantic error.
Definition DeclBase.cpp:178
bool isInExportDeclContext() const
Whether this declaration was exported in a lexical context.
attr_iterator attr_begin() const
Definition DeclBase.h:547
DeclContext * getNonTransparentDeclContext()
Return the non transparent context.
SourceLocation getLocation() const
Definition DeclBase.h:447
void setImplicit(bool I=true)
Definition DeclBase.h:602
DeclContext * getDeclContext()
Definition DeclBase.h:456
attr_range attrs() const
Definition DeclBase.h:543
AccessSpecifier getAccess() const
Definition DeclBase.h:515
SourceLocation getBeginLoc() const LLVM_READONLY
Definition DeclBase.h:439
void dropAttr()
Definition DeclBase.h:564
bool hasAttr() const
Definition DeclBase.h:585
The name of a declaration.
Represents a ValueDecl that came out of a declarator.
Definition Decl.h:780
SourceLocation getBeginLoc() const LLVM_READONLY
Definition Decl.h:831
This represents one expression.
Definition Expr.h:112
bool isIntegerConstantExpr(const ASTContext &Ctx) const
void setType(QualType t)
Definition Expr.h:145
ExprValueKind getValueKind() const
getValueKind - The value kind that this expression produces.
Definition Expr.h:447
Expr * IgnoreParenImpCasts() LLVM_READONLY
Skip past any parentheses and implicit casts which might surround this expression until reaching a fi...
Definition Expr.cpp:3090
Expr * IgnoreParens() LLVM_READONLY
Skip past any parentheses which might surround this expression until reaching a fixed point.
Definition Expr.cpp:3086
std::optional< llvm::APSInt > getIntegerConstantExpr(const ASTContext &Ctx) const
isIntegerConstantExpr - Return the value if this expression is a valid integer constant expression.
bool isPRValue() const
Definition Expr.h:285
bool isLValue() const
isLValue - True if this expression is an "l-value" according to the rules of the current language.
Definition Expr.h:284
ExprObjectKind getObjectKind() const
getObjectKind - The object kind that this expression produces.
Definition Expr.h:454
bool HasSideEffects(const ASTContext &Ctx, bool IncludePossibleEffects=true) const
HasSideEffects - This routine returns true for all those expressions which have any effect other than...
Definition Expr.cpp:3688
void setValueKind(ExprValueKind Cat)
setValueKind - Set the value kind produced by this expression.
Definition Expr.h:464
SourceLocation getExprLoc() const LLVM_READONLY
getExprLoc - Return the preferred location for the arrow when diagnosing a problem with a generic exp...
Definition Expr.cpp:277
@ MLV_Valid
Definition Expr.h:306
QualType getType() const
Definition Expr.h:144
ExtVectorType - Extended vector type.
Definition TypeBase.h:4317
Represents difference between two FPOptions values.
Represents a member of a struct/union/class.
Definition Decl.h:3175
static FieldDecl * Create(const ASTContext &C, DeclContext *DC, SourceLocation StartLoc, SourceLocation IdLoc, const IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo, Expr *BW, bool Mutable, InClassInitStyle InitStyle)
Definition Decl.cpp:4702
static FixItHint CreateReplacement(CharSourceRange RemoveRange, StringRef Code)
Create a code modification hint that replaces the given source range with the given code string.
Definition Diagnostic.h:141
Represents a function declaration or definition.
Definition Decl.h:2015
const ParmVarDecl * getParamDecl(unsigned i) const
Definition Decl.h:2812
Stmt * getBody(const FunctionDecl *&Definition) const
Retrieve the body (definition) of the function.
Definition Decl.cpp:3281
bool isThisDeclarationADefinition() const
Returns whether this specific declaration of the function is also a definition that does not contain ...
Definition Decl.h:2329
QualType getReturnType() const
Definition Decl.h:2860
ArrayRef< ParmVarDecl * > parameters() const
Definition Decl.h:2789
bool isTemplateInstantiation() const
Determines if the given function was instantiated from a function template.
Definition Decl.cpp:4259
redecl_range redecls() const
Returns an iterator range for all the redeclarations of the same decl.
unsigned getNumParams() const
Return the number of parameters this function must have based on its FunctionType.
Definition Decl.cpp:3828
DeclarationNameInfo getNameInfo() const
Definition Decl.h:2226
bool hasBody(const FunctionDecl *&Definition) const
Returns true if the function has a body.
Definition Decl.cpp:3201
bool isDefined(const FunctionDecl *&Definition, bool CheckForPendingFriendDefinition=false) const
Returns true if the function has a definition that does not need to be instantiated.
Definition Decl.cpp:3248
HLSLBufferDecl - Represent a cbuffer or tbuffer declaration.
Definition Decl.h:5211
static HLSLBufferDecl * Create(ASTContext &C, DeclContext *LexicalParent, bool CBuffer, SourceLocation KwLoc, IdentifierInfo *ID, SourceLocation IDLoc, SourceLocation LBrace)
Definition Decl.cpp:5907
void addLayoutStruct(CXXRecordDecl *LS)
Definition Decl.cpp:5947
void setHasValidPackoffset(bool PO)
Definition Decl.h:5256
static HLSLBufferDecl * CreateDefaultCBuffer(ASTContext &C, DeclContext *LexicalParent, ArrayRef< Decl * > DefaultCBufferDecls)
Definition Decl.cpp:5930
buffer_decl_range buffer_decls() const
Definition Decl.h:5286
static HLSLOutArgExpr * Create(const ASTContext &C, QualType Ty, OpaqueValueExpr *Base, OpaqueValueExpr *OpV, Expr *WB, bool IsInOut)
Definition Expr.cpp:5645
static HLSLRootSignatureDecl * Create(ASTContext &C, DeclContext *DC, SourceLocation Loc, IdentifierInfo *ID, llvm::dxbc::RootSignatureVersion Version, ArrayRef< llvm::hlsl::rootsig::RootElement > RootElements)
Definition Decl.cpp:5993
One of these records is kept for each identifier that is lexed.
StringRef getName() const
Return the actual identifier string.
A simple pair of identifier info and location.
SourceLocation getLoc() const
IdentifierInfo * getIdentifierInfo() const
IdentifierInfo & get(StringRef Name)
Return the identifier token info for the specified named identifier.
ImplicitCastExpr - Allows us to explicitly represent implicit type conversions, which have no direct ...
Definition Expr.h:3856
static ImplicitCastExpr * Create(const ASTContext &Context, QualType T, CastKind Kind, Expr *Operand, const CXXCastPath *BasePath, ExprValueKind Cat, FPOptionsOverride FPO)
Definition Expr.cpp:2073
Describes an C or C++ initializer list.
Definition Expr.h:5302
Describes an entity that is being initialized.
QualType getType() const
Retrieve type being initialized.
static InitializedEntity InitializeParameter(ASTContext &Context, ParmVarDecl *Parm)
Create the initialization entity for a parameter.
static IntegerLiteral * Create(const ASTContext &C, const llvm::APInt &V, QualType type, SourceLocation l)
Returns a new integer literal with value 'V' and type 'type'.
Definition Expr.cpp:975
iterator begin(Source *source, bool LocalOnly=false)
Represents the results of name lookup.
Definition Lookup.h:147
Represents a prvalue temporary that is written into memory so that a reference can bind to it.
Definition ExprCXX.h:4917
Represents a matrix type, as defined in the Matrix Types clang extensions.
Definition TypeBase.h:4387
MemberExpr - [C99 6.5.2.3] Structure and Union Members.
Definition Expr.h:3367
ValueDecl * getMemberDecl() const
Retrieve the member declaration to which this expression refers.
Definition Expr.h:3450
Expr * getBase() const
Definition Expr.h:3444
This represents a decl that may have a name.
Definition Decl.h:274
IdentifierInfo * getIdentifier() const
Get the identifier that names this declaration, if there is one.
Definition Decl.h:295
StringRef getName() const
Get the name of identifier for this declaration as a StringRef.
Definition Decl.h:301
DeclarationName getDeclName() const
Get the actual, stored name of the declaration, which may be a special name.
Definition Decl.h:340
A C++ nested-name-specifier augmented with source location information.
OpaqueValueExpr - An expression referring to an opaque object of a fixed type and value class.
Definition Expr.h:1181
Represents a parameter to a function.
Definition Decl.h:1805
ParsedAttr - Represents a syntactic attribute.
Definition ParsedAttr.h:119
unsigned getSemanticSpelling() const
If the parsed attribute has a semantic equivalent, and it would have a semantic Spelling enumeration ...
unsigned getMinArgs() const
bool checkExactlyNumArgs(class Sema &S, unsigned Num) const
Check if the attribute has exactly as many args as Num.
IdentifierLoc * getArgAsIdent(unsigned Arg) const
Definition ParsedAttr.h:389
bool hasParsedType() const
Definition ParsedAttr.h:337
const ParsedType & getTypeArg() const
Definition ParsedAttr.h:459
unsigned getNumArgs() const
getNumArgs - Return the number of actual arguments to this attribute.
Definition ParsedAttr.h:371
bool isArgIdent(unsigned Arg) const
Definition ParsedAttr.h:385
Expr * getArgAsExpr(unsigned Arg) const
Definition ParsedAttr.h:383
AttributeCommonInfo::Kind getKind() const
Definition ParsedAttr.h:610
A (possibly-)qualified type.
Definition TypeBase.h:937
void addRestrict()
Add the restrict qualifier to this QualType.
Definition TypeBase.h:1178
QualType getNonLValueExprType(const ASTContext &Context) const
Determine the type of a (typically non-lvalue) expression with the specified result type.
Definition Type.cpp:3630
QualType getDesugaredType(const ASTContext &Context) const
Return the specified type with any "sugar" removed from the type.
Definition TypeBase.h:1302
bool isNull() const
Return true if this QualType doesn't point to a type yet.
Definition TypeBase.h:1004
const Type * getTypePtr() const
Retrieves a pointer to the underlying (unqualified) type.
Definition TypeBase.h:8431
LangAS getAddressSpace() const
Return the address space of this type.
Definition TypeBase.h:8557
QualType getNonReferenceType() const
If Type is a reference type (e.g., const int&), returns the type that the reference refers to ("const...
Definition TypeBase.h:8616
QualType getCanonicalType() const
Definition TypeBase.h:8483
QualType getUnqualifiedType() const
Retrieve the unqualified variant of the given type, removing as little sugar as possible.
Definition TypeBase.h:8525
bool hasAddressSpace() const
Check if this type has any address space qualifier.
Definition TypeBase.h:8552
Represents a struct/union/class.
Definition Decl.h:4342
field_iterator field_end() const
Definition Decl.h:4548
field_range fields() const
Definition Decl.h:4545
bool field_empty() const
Definition Decl.h:4553
field_iterator field_begin() const
Definition Decl.cpp:5277
bool hasBindingInfoForDecl(const VarDecl *VD) const
Definition SemaHLSL.cpp:220
DeclBindingInfo * getDeclBindingInfo(const VarDecl *VD, ResourceClass ResClass)
Definition SemaHLSL.cpp:206
DeclBindingInfo * addDeclBindingInfo(const VarDecl *VD, ResourceClass ResClass)
Definition SemaHLSL.cpp:193
Scope - A scope is a transient data structure that is used while parsing the program.
Definition Scope.h:41
SemaBase(Sema &S)
Definition SemaBase.cpp:7
ASTContext & getASTContext() const
Definition SemaBase.cpp:9
Sema & SemaRef
Definition SemaBase.h:40
SemaDiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID)
Emit a diagnostic.
Definition SemaBase.cpp:61
ExprResult ActOnOutParamExpr(ParmVarDecl *Param, Expr *Arg)
HLSLRootSignatureDecl * lookupRootSignatureOverrideDecl(DeclContext *DC) const
bool CanPerformElementwiseCast(Expr *Src, QualType DestType)
void handleWaveSizeAttr(Decl *D, const ParsedAttr &AL)
void handleVkLocationAttr(Decl *D, const ParsedAttr &AL)
HLSLAttributedResourceLocInfo TakeLocForHLSLAttribute(const HLSLAttributedResourceType *RT)
void handleSemanticAttr(Decl *D, const ParsedAttr &AL)
bool CanPerformScalarCast(QualType SrcTy, QualType DestTy)
QualType ProcessResourceTypeAttributes(QualType Wrapped)
void handleShaderAttr(Decl *D, const ParsedAttr &AL)
uint32_t getNextImplicitBindingOrderID()
Definition SemaHLSL.h:230
void CheckEntryPoint(FunctionDecl *FD)
Definition SemaHLSL.cpp:960
void handleVkExtBuiltinOutputAttr(Decl *D, const ParsedAttr &AL)
void emitLogicalOperatorFixIt(Expr *LHS, Expr *RHS, BinaryOperatorKind Opc)
T * createSemanticAttr(const AttributeCommonInfo &ACI, std::optional< unsigned > Location)
Definition SemaHLSL.h:182
bool initGlobalResourceDecl(VarDecl *VD)
void ActOnEndOfTranslationUnit(TranslationUnitDecl *TU)
bool initGlobalResourceArrayDecl(VarDecl *VD)
HLSLVkConstantIdAttr * mergeVkConstantIdAttr(Decl *D, const AttributeCommonInfo &AL, int Id)
Definition SemaHLSL.cpp:724
HLSLNumThreadsAttr * mergeNumThreadsAttr(Decl *D, const AttributeCommonInfo &AL, int X, int Y, int Z)
Definition SemaHLSL.cpp:690
void deduceAddressSpace(VarDecl *Decl)
std::pair< IdentifierInfo *, bool > ActOnStartRootSignatureDecl(StringRef Signature)
Computes the unique Root Signature identifier from the given signature, then lookup if there is a pre...
void handlePackOffsetAttr(Decl *D, const ParsedAttr &AL)
bool diagnosePositionType(QualType T, const ParsedAttr &AL)
bool handleInitialization(VarDecl *VDecl, Expr *&Init)
bool diagnoseInputIDType(QualType T, const ParsedAttr &AL)
void handleParamModifierAttr(Decl *D, const ParsedAttr &AL)
bool CheckResourceBinOp(BinaryOperatorKind Opc, Expr *LHSExpr, Expr *RHSExpr, SourceLocation Loc)
bool CanPerformAggregateSplatCast(Expr *Src, QualType DestType)
bool ActOnResourceMemberAccessExpr(MemberExpr *ME)
bool IsScalarizedLayoutCompatible(QualType T1, QualType T2) const
QualType ActOnTemplateShorthand(TemplateDecl *Template, SourceLocation NameLoc)
void diagnoseSystemSemanticAttr(Decl *D, const ParsedAttr &AL, std::optional< unsigned > Index)
void handleRootSignatureAttr(Decl *D, const ParsedAttr &AL)
bool CheckCompatibleParameterABI(FunctionDecl *New, FunctionDecl *Old)
QualType handleVectorBinOpConversion(ExprResult &LHS, ExprResult &RHS, QualType LHSType, QualType RHSType, bool IsCompAssign)
QualType checkMatrixComponent(Sema &S, QualType baseType, ExprValueKind &VK, SourceLocation OpLoc, const IdentifierInfo *CompName, SourceLocation CompLoc)
void handleResourceBindingAttr(Decl *D, const ParsedAttr &AL)
bool IsTypedResourceElementCompatible(QualType T1)
bool transformInitList(const InitializedEntity &Entity, InitListExpr *Init)
void handleNumThreadsAttr(Decl *D, const ParsedAttr &AL)
bool ActOnUninitializedVarDecl(VarDecl *D)
void handleVkExtBuiltinInputAttr(Decl *D, const ParsedAttr &AL)
void ActOnTopLevelFunction(FunctionDecl *FD)
Definition SemaHLSL.cpp:793
bool handleResourceTypeAttr(QualType T, const ParsedAttr &AL)
void handleVkPushConstantAttr(Decl *D, const ParsedAttr &AL)
HLSLShaderAttr * mergeShaderAttr(Decl *D, const AttributeCommonInfo &AL, llvm::Triple::EnvironmentType ShaderType)
Definition SemaHLSL.cpp:760
void ActOnFinishBuffer(Decl *Dcl, SourceLocation RBrace)
Definition SemaHLSL.cpp:663
void handleVkBindingAttr(Decl *D, const ParsedAttr &AL)
HLSLParamModifierAttr * mergeParamModifierAttr(Decl *D, const AttributeCommonInfo &AL, HLSLParamModifierAttr::Spelling Spelling)
Definition SemaHLSL.cpp:773
QualType getInoutParameterType(QualType Ty)
SemaHLSL(Sema &S)
Definition SemaHLSL.cpp:224
void handleVkConstantIdAttr(Decl *D, const ParsedAttr &AL)
Decl * ActOnStartBuffer(Scope *BufferScope, bool CBuffer, SourceLocation KwLoc, IdentifierInfo *Ident, SourceLocation IdentLoc, SourceLocation LBrace)
Definition SemaHLSL.cpp:226
HLSLWaveSizeAttr * mergeWaveSizeAttr(Decl *D, const AttributeCommonInfo &AL, int Min, int Max, int Preferred, int SpelledArgsCount)
Definition SemaHLSL.cpp:704
bool handleRootSignatureElements(ArrayRef< hlsl::RootSignatureElement > Elements)
void ActOnFinishRootSignatureDecl(SourceLocation Loc, IdentifierInfo *DeclIdent, ArrayRef< hlsl::RootSignatureElement > Elements)
Creates the Root Signature decl of the parsed Root Signature elements onto the AST and push it onto c...
void ActOnVariableDeclarator(VarDecl *VD)
bool CheckBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall)
Sema - This implements semantic analysis and AST building for C.
Definition Sema.h:868
@ LookupOrdinaryName
Ordinary name lookup, which finds ordinary names (functions, variables, typedefs, etc....
Definition Sema.h:9406
@ LookupMemberName
Member name lookup, which finds the names of class/struct/union members.
Definition Sema.h:9414
ExtVectorDeclsType ExtVectorDecls
ExtVectorDecls - This is a list all the extended vector types.
Definition Sema.h:4954
ASTContext & Context
Definition Sema.h:1308
ASTContext & getASTContext() const
Definition Sema.h:939
ExprResult ImpCastExprToType(Expr *E, QualType Type, CastKind CK, ExprValueKind VK=VK_PRValue, const CXXCastPath *BasePath=nullptr, CheckedConversionKind CCK=CheckedConversionKind::Implicit)
ImpCastExprToType - If Expr is not of type 'Type', insert an implicit cast.
Definition Sema.cpp:762
const LangOptions & getLangOpts() const
Definition Sema.h:932
SemaHLSL & HLSL()
Definition Sema.h:1483
ExprResult BuildFieldReferenceExpr(Expr *BaseExpr, bool IsArrow, SourceLocation OpLoc, const CXXScopeSpec &SS, FieldDecl *Field, DeclAccessPair FoundDecl, const DeclarationNameInfo &MemberNameInfo)
bool checkArgCountRange(CallExpr *Call, unsigned MinArgCount, unsigned MaxArgCount)
Checks that a call expression's argument count is in the desired range.
ExternalSemaSource * getExternalSource() const
Definition Sema.h:942
ASTConsumer & Consumer
Definition Sema.h:1309
bool checkArgCount(CallExpr *Call, unsigned DesiredArgCount)
Checks that a call expression's argument count is the desired number.
ExprResult CreateBuiltinArraySubscriptExpr(Expr *Base, SourceLocation LLoc, Expr *Idx, SourceLocation RLoc)
bool LookupQualifiedName(LookupResult &R, DeclContext *LookupCtx, bool InUnqualifiedLookup=false)
Perform qualified name lookup into a given context.
ExprResult PerformCopyInitialization(const InitializedEntity &Entity, SourceLocation EqualLoc, ExprResult Init, bool TopLevelOfInitList=false, bool AllowExplicit=false)
ExprResult CreateBuiltinMatrixSubscriptExpr(Expr *Base, Expr *RowIdx, Expr *ColumnIdx, SourceLocation RBLoc)
Encodes a location in the source.
SourceLocation getLocWithOffset(IntTy Offset) const
Return a source location with the specified offset from this SourceLocation.
A trivial tuple used to represent a source range.
SourceLocation getEnd() const
SourceLocation getEndLoc() const LLVM_READONLY
Definition Stmt.cpp:367
void printPretty(raw_ostream &OS, PrinterHelper *Helper, const PrintingPolicy &Policy, unsigned Indentation=0, StringRef NewlineSymbol="\n", const ASTContext *Context=nullptr) const
SourceRange getSourceRange() const LLVM_READONLY
SourceLocation tokens are not useful in isolation - they are low level value objects created/interpre...
Definition Stmt.cpp:343
SourceLocation getBeginLoc() const LLVM_READONLY
Definition Stmt.cpp:355
StringLiteral - This represents a string literal expression, e.g.
Definition Expr.h:1802
static StringLiteral * Create(const ASTContext &Ctx, StringRef Str, StringLiteralKind Kind, bool Pascal, QualType Ty, ArrayRef< SourceLocation > Locs)
This is the "fully general" constructor that allows representation of strings formed from one or more...
Definition Expr.cpp:1188
void startDefinition()
Starts the definition of this tag declaration.
Definition Decl.cpp:4908
bool isUnion() const
Definition Decl.h:3943
bool isClass() const
Definition Decl.h:3942
Exposes information about the current target.
Definition TargetInfo.h:227
TargetOptions & getTargetOpts() const
Retrieve the target options.
Definition TargetInfo.h:327
const llvm::Triple & getTriple() const
Returns the target triple of the primary target.
StringRef getPlatformName() const
Retrieve the name of the platform as it is used in the availability attribute.
VersionTuple getPlatformMinVersion() const
Retrieve the minimum desired version of the platform, to which the program should be compiled.
std::string HLSLEntry
The entry point name for HLSL shader being compiled as specified by -E.
A convenient class for passing around template argument information.
void addArgument(const TemplateArgumentLoc &Loc)
The base class of all kinds of template declarations (e.g., class, function, etc.).
Stores a list of template parameters for a TemplateDecl and its derived classes.
The top declaration context.
Definition Decl.h:105
SourceLocation getBeginLoc() const
Get the begin source location.
Definition TypeLoc.cpp:193
A container of type source information.
Definition TypeBase.h:8402
TypeLoc getTypeLoc() const
Return the TypeLoc wrapper for the type source info.
Definition TypeLoc.h:267
The base class of the type hierarchy.
Definition TypeBase.h:1866
bool isVoidType() const
Definition TypeBase.h:9034
bool isBooleanType() const
Definition TypeBase.h:9171
bool isIncompleteArrayType() const
Definition TypeBase.h:8775
CXXRecordDecl * getAsCXXRecordDecl() const
Retrieves the CXXRecordDecl that this type refers to, either because the type is a RecordType or beca...
Definition Type.h:26
bool isConstantArrayType() const
Definition TypeBase.h:8771
bool hasIntegerRepresentation() const
Determine whether this type has an integer representation of some sort, e.g., it is an integer type o...
Definition Type.cpp:2084
bool isArrayType() const
Definition TypeBase.h:8767
CXXRecordDecl * castAsCXXRecordDecl() const
Definition Type.h:36
bool isArithmeticType() const
Definition Type.cpp:2375
bool isConstantMatrixType() const
Definition TypeBase.h:8835
bool isHLSLBuiltinIntangibleType() const
Definition TypeBase.h:8979
bool isPointerType() const
Definition TypeBase.h:8668
CanQualType getCanonicalTypeUnqualified() const
bool isIntegerType() const
isIntegerType() does not include complex integers (a GCC extension).
Definition TypeBase.h:9078
const T * castAs() const
Member-template castAs<specific type>.
Definition TypeBase.h:9328
bool isReferenceType() const
Definition TypeBase.h:8692
bool isHLSLIntangibleType() const
Definition Type.cpp:5462
bool isEnumeralType() const
Definition TypeBase.h:8799
bool isScalarType() const
Definition TypeBase.h:9140
bool isIntegralType(const ASTContext &Ctx) const
Determine whether this type is an integral type.
Definition Type.cpp:2121
const Type * getArrayElementTypeNoTypeQual() const
If this is an array type, return the element type of the array, potentially with type qualifiers miss...
Definition Type.cpp:473
QualType getPointeeType() const
If this is a pointer, ObjC object pointer, or block pointer, this returns the respective pointee.
Definition Type.cpp:754
bool hasUnsignedIntegerRepresentation() const
Determine whether this type has an unsigned integer representation of some sort, e....
Definition Type.cpp:2329
bool isAggregateType() const
Determines whether the type is a C++ aggregate type or C aggregate or union type.
Definition Type.cpp:2456
ScalarTypeKind getScalarTypeKind() const
Given that this is a scalar type, classify it.
Definition Type.cpp:2407
bool hasSignedIntegerRepresentation() const
Determine whether this type has an signed integer representation of some sort, e.g....
Definition Type.cpp:2275
bool isMatrixType() const
Definition TypeBase.h:8831
bool isHLSLResourceRecord() const
Definition Type.cpp:5449
bool hasFloatingRepresentation() const
Determine whether this type has a floating-point representation of some sort, e.g....
Definition Type.cpp:2350
bool isVectorType() const
Definition TypeBase.h:8807
bool isRealFloatingType() const
Floating point categories.
Definition Type.cpp:2358
bool isHLSLAttributedResourceType() const
Definition TypeBase.h:8991
@ STK_FloatingComplex
Definition TypeBase.h:2814
@ STK_ObjCObjectPointer
Definition TypeBase.h:2808
@ STK_IntegralComplex
Definition TypeBase.h:2813
@ STK_MemberPointer
Definition TypeBase.h:2809
bool isFloatingType() const
Definition Type.cpp:2342
bool isSamplerT() const
Definition TypeBase.h:8912
const T * getAs() const
Member-template getAs<specific type>'.
Definition TypeBase.h:9261
const Type * getUnqualifiedDesugaredType() const
Return the specified type with any "sugar" removed from the type, removing any typedefs,...
Definition Type.cpp:655
bool isRecordType() const
Definition TypeBase.h:8795
bool isHLSLResourceRecordArray() const
Definition Type.cpp:5453
void setType(QualType newType)
Definition Decl.h:724
QualType getType() const
Definition Decl.h:723
Represents a variable declaration or definition.
Definition Decl.h:926
static VarDecl * Create(ASTContext &C, DeclContext *DC, SourceLocation StartLoc, SourceLocation IdLoc, const IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo, StorageClass S)
Definition Decl.cpp:2164
void setInitStyle(InitializationStyle Style)
Definition Decl.h:1467
@ CallInit
Call-style initialization (C++98)
Definition Decl.h:934
void setStorageClass(StorageClass SC)
Definition Decl.cpp:2176
bool hasGlobalStorage() const
Returns true for all variables that do not have local storage.
Definition Decl.h:1241
void setInit(Expr *I)
Definition Decl.cpp:2490
StorageClass getStorageClass() const
Returns the storage class as written in the source.
Definition Decl.h:1168
Represents a GCC generic vector type.
Definition TypeBase.h:4225
unsigned getNumElements() const
Definition TypeBase.h:4240
QualType getElementType() const
Definition TypeBase.h:4239
IdentifierInfo * getNameAsIdentifier(ASTContext &AST) const
Defines the clang::TargetInfo interface.
Definition SPIR.cpp:47
uint32_t getResourceDimensions(llvm::dxil::ResourceDimension Dim)
The JSON file list parser is used to communicate input to InstallAPI.
bool isa(CodeGen::Address addr)
Definition Address.h:330
if(T->getSizeExpr()) TRY_TO(TraverseStmt(const_cast< Expr * >(T -> getSizeExpr())))
static bool CheckFloatOrHalfRepresentation(Sema *S, SourceLocation Loc, int ArgOrdinal, clang::QualType PassedType)
Definition SemaSPIRV.cpp:66
@ ICIS_NoInit
No in-class initializer.
Definition Specifiers.h:272
@ TemplateName
The identifier is a template name. FIXME: Add an annotation for that.
Definition Parser.h:61
@ OK_Ordinary
An ordinary object is located at an address in memory.
Definition Specifiers.h:151
static bool CheckAllArgTypesAreCorrect(Sema *S, CallExpr *TheCall, llvm::ArrayRef< llvm::function_ref< bool(Sema *, SourceLocation, int, QualType)> > Checks)
Definition SemaSPIRV.cpp:49
@ AS_public
Definition Specifiers.h:124
@ AS_none
Definition Specifiers.h:127
@ SC_Extern
Definition Specifiers.h:251
@ SC_Static
Definition Specifiers.h:252
@ SC_None
Definition Specifiers.h:250
@ AANT_ArgumentIdentifier
@ Result
The result type of a method or function.
Definition TypeBase.h:905
@ Ordinary
This parameter uses ordinary ABI rules for its type.
Definition Specifiers.h:380
llvm::Expected< QualType > ExpectedType
@ Template
We are parsing a template declaration.
Definition Parser.h:81
LLVM_READONLY bool isDigit(unsigned char c)
Return true if this character is an ASCII digit: [0-9].
Definition CharInfo.h:114
static bool CheckAllArgsHaveSameType(Sema *S, CallExpr *TheCall)
Definition SemaSPIRV.cpp:32
ExprResult ExprError()
Definition Ownership.h:265
@ Type
The name was classified as a type.
Definition Sema.h:564
LangAS
Defines the address space values used by the address space qualifier of QualType.
bool CreateHLSLAttributedResourceType(Sema &S, QualType Wrapped, ArrayRef< const Attr * > AttrList, QualType &ResType, HLSLAttributedResourceLocInfo *LocInfo=nullptr)
CastKind
CastKind - The kind of operation required for a conversion.
ExprValueKind
The categorization of expression values, currently following the C++11 scheme.
Definition Specifiers.h:132
@ VK_PRValue
A pr-value expression (in the C++11 taxonomy) produces a temporary value.
Definition Specifiers.h:135
@ VK_LValue
An l-value expression is a reference to an object with independent storage.
Definition Specifiers.h:139
DynamicRecursiveASTVisitorBase< false > DynamicRecursiveASTVisitor
U cast(CodeGen::Address addr)
Definition Address.h:327
@ None
No keyword precedes the qualified type name.
Definition TypeBase.h:5977
ActionResult< Expr * > ExprResult
Definition Ownership.h:249
Visibility
Describes the different kinds of visibility that a declaration may have.
Definition Visibility.h:34
unsigned long uint64_t
unsigned int uint32_t
hash_code hash_value(const clang::dependencies::ModuleID &ID)
__DEVICE__ bool isnan(float __x)
__DEVICE__ _Tp abs(const std::complex< _Tp > &__c)
#define false
Definition stdbool.h:26
Describes how types, statements, expressions, and declarations should be printed.
void setCounterImplicitOrderID(unsigned Value) const
void setImplicitOrderID(unsigned Value) const
const SourceLocation & getLocation() const
Definition SemaHLSL.h:48
const llvm::hlsl::rootsig::RootElement & getElement() const
Definition SemaHLSL.h:47