clang 23.0.0git
HLSLExternalSemaSource.cpp
Go to the documentation of this file.
1//===--- HLSLExternalSemaSource.cpp - HLSL Sema Source --------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9//
10//===----------------------------------------------------------------------===//
11
15#include "clang/AST/Attr.h"
16#include "clang/AST/Decl.h"
17#include "clang/AST/DeclCXX.h"
19#include "clang/AST/Expr.h"
20#include "clang/AST/Type.h"
24#include "clang/Sema/Lookup.h"
25#include "clang/Sema/Sema.h"
26#include "clang/Sema/SemaHLSL.h"
28#include "llvm/ADT/STLExtras.h"
29#include "llvm/ADT/SmallVector.h"
30
31using namespace clang;
32using namespace llvm::hlsl;
33
35
37 SemaPtr = &S;
38 ASTContext &AST = SemaPtr->getASTContext();
39 // If the translation unit has external storage force external decls to load.
42
43 IdentifierInfo &HLSL = AST.Idents.get("hlsl", tok::TokenKind::identifier);
45 NamespaceDecl *PrevDecl = nullptr;
47 PrevDecl = Result.getAsSingle<NamespaceDecl>();
48 HLSLNamespace = NamespaceDecl::Create(
49 AST, AST.getTranslationUnitDecl(), /*Inline=*/false, SourceLocation(),
50 SourceLocation(), &HLSL, PrevDecl, /*Nested=*/false);
51 HLSLNamespace->setImplicit(true);
52 HLSLNamespace->setHasExternalLexicalStorage();
53 AST.getTranslationUnitDecl()->addDecl(HLSLNamespace);
54
55 // Force external decls in the HLSL namespace to load from the PCH.
56 (void)HLSLNamespace->getCanonicalDecl()->decls_begin();
57 defineTrivialHLSLTypes();
58 defineHLSLTypesWithForwardDeclarations();
59 defineHLSLAtomicIntrinsics();
60
61 // This adds a `using namespace hlsl` directive. In DXC, we don't put HLSL's
62 // built in types inside a namespace, but we are planning to change that in
63 // the near future. In order to be source compatible older versions of HLSL
64 // will need to implicitly use the hlsl namespace. For now in clang everything
65 // will get added to the namespace, and we can remove the using directive for
66 // future language versions to match HLSL's evolution.
69 NestedNameSpecifierLoc(), SourceLocation(), HLSLNamespace,
71
73}
74
75void HLSLExternalSemaSource::defineHLSLVectorAlias() {
76 ASTContext &AST = SemaPtr->getASTContext();
77
78 llvm::SmallVector<NamedDecl *> TemplateParams;
79
80 auto *TypeParam = TemplateTypeParmDecl::Create(
81 AST, HLSLNamespace, SourceLocation(), SourceLocation(), 0, 0,
82 &AST.Idents.get("element", tok::TokenKind::identifier), false, false);
83 TypeParam->setDefaultArgument(
86
87 TemplateParams.emplace_back(TypeParam);
88
89 auto *SizeParam = NonTypeTemplateParmDecl::Create(
90 AST, HLSLNamespace, SourceLocation(), SourceLocation(), 0, 1,
91 &AST.Idents.get("element_count", tok::TokenKind::identifier), AST.IntTy,
92 false, AST.getTrivialTypeSourceInfo(AST.IntTy));
93 llvm::APInt Val(AST.getIntWidth(AST.IntTy), 4);
94 TemplateArgument Default(AST, llvm::APSInt(std::move(Val)), AST.IntTy,
95 /*IsDefaulted=*/true);
96 SizeParam->setDefaultArgument(AST, SemaPtr->getTrivialTemplateArgumentLoc(
98 TemplateParams.emplace_back(SizeParam);
99
100 auto *ParamList =
102 TemplateParams, SourceLocation(), nullptr);
103
104 IdentifierInfo &II = AST.Idents.get("vector", tok::TokenKind::identifier);
105
107 AST.getTemplateTypeParmType(0, 0, false, TypeParam),
109 AST, NestedNameSpecifierLoc(), SourceLocation(), SizeParam, false,
110 DeclarationNameInfo(SizeParam->getDeclName(), SourceLocation()),
111 AST.IntTy, VK_LValue),
113
114 auto *Record = TypeAliasDecl::Create(AST, HLSLNamespace, SourceLocation(),
115 SourceLocation(), &II,
116 AST.getTrivialTypeSourceInfo(AliasType));
117 Record->setImplicit(true);
118
119 auto *Template =
120 TypeAliasTemplateDecl::Create(AST, HLSLNamespace, SourceLocation(),
121 Record->getIdentifier(), ParamList, Record);
122
123 Record->setDescribedAliasTemplate(Template);
124 Template->setImplicit(true);
125 Template->setLexicalDeclContext(Record->getDeclContext());
126 HLSLNamespace->addDecl(Template);
127}
128
129void HLSLExternalSemaSource::defineHLSLMatrixAlias() {
130 ASTContext &AST = SemaPtr->getASTContext();
131 llvm::SmallVector<NamedDecl *> TemplateParams;
132
133 auto *TypeParam = TemplateTypeParmDecl::Create(
134 AST, HLSLNamespace, SourceLocation(), SourceLocation(), 0, 0,
135 &AST.Idents.get("element", tok::TokenKind::identifier), false, false);
136 TypeParam->setDefaultArgument(
137 AST, SemaPtr->getTrivialTemplateArgumentLoc(
139
140 TemplateParams.emplace_back(TypeParam);
141
142 // these should be 64 bit to be consistent with other clang matrices.
143 auto *RowsParam = NonTypeTemplateParmDecl::Create(
144 AST, HLSLNamespace, SourceLocation(), SourceLocation(), 0, 1,
145 &AST.Idents.get("rows_count", tok::TokenKind::identifier), AST.IntTy,
146 false, AST.getTrivialTypeSourceInfo(AST.IntTy));
147 llvm::APInt RVal(AST.getIntWidth(AST.IntTy), 4);
148 TemplateArgument RDefault(AST, llvm::APSInt(std::move(RVal)), AST.IntTy,
149 /*IsDefaulted=*/true);
150 RowsParam->setDefaultArgument(
151 AST, SemaPtr->getTrivialTemplateArgumentLoc(RDefault, AST.IntTy,
152 SourceLocation()));
153 TemplateParams.emplace_back(RowsParam);
154
155 auto *ColsParam = NonTypeTemplateParmDecl::Create(
156 AST, HLSLNamespace, SourceLocation(), SourceLocation(), 0, 2,
157 &AST.Idents.get("cols_count", tok::TokenKind::identifier), AST.IntTy,
158 false, AST.getTrivialTypeSourceInfo(AST.IntTy));
159 llvm::APInt CVal(AST.getIntWidth(AST.IntTy), 4);
160 TemplateArgument CDefault(AST, llvm::APSInt(std::move(CVal)), AST.IntTy,
161 /*IsDefaulted=*/true);
162 ColsParam->setDefaultArgument(
163 AST, SemaPtr->getTrivialTemplateArgumentLoc(CDefault, AST.IntTy,
164 SourceLocation()));
165 TemplateParams.emplace_back(ColsParam);
166
167 const unsigned MaxMatDim = SemaPtr->getLangOpts().MaxMatrixDimension;
168
169 auto *MaxRow = IntegerLiteral::Create(
170 AST, llvm::APInt(AST.getIntWidth(AST.IntTy), MaxMatDim), AST.IntTy,
172 auto *MaxCol = IntegerLiteral::Create(
173 AST, llvm::APInt(AST.getIntWidth(AST.IntTy), MaxMatDim), AST.IntTy,
175
176 auto *RowsRef = DeclRefExpr::Create(
177 AST, NestedNameSpecifierLoc(), SourceLocation(), RowsParam,
178 /*RefersToEnclosingVariableOrCapture*/ false,
179 DeclarationNameInfo(RowsParam->getDeclName(), SourceLocation()),
180 AST.IntTy, VK_LValue);
181 auto *ColsRef = DeclRefExpr::Create(
182 AST, NestedNameSpecifierLoc(), SourceLocation(), ColsParam,
183 /*RefersToEnclosingVariableOrCapture*/ false,
184 DeclarationNameInfo(ColsParam->getDeclName(), SourceLocation()),
185 AST.IntTy, VK_LValue);
186
187 auto *RowsLE = BinaryOperator::Create(AST, RowsRef, MaxRow, BO_LE, AST.BoolTy,
190 auto *ColsLE = BinaryOperator::Create(AST, ColsRef, MaxCol, BO_LE, AST.BoolTy,
193
195 AST, RowsLE, ColsLE, BO_LAnd, AST.BoolTy, VK_PRValue, OK_Ordinary,
197
198 auto *ParamList = TemplateParameterList::Create(
199 AST, SourceLocation(), SourceLocation(), TemplateParams, SourceLocation(),
201
202 IdentifierInfo &II = AST.Idents.get("matrix", tok::TokenKind::identifier);
203
205 AST.getTemplateTypeParmType(0, 0, false, TypeParam),
207 AST, NestedNameSpecifierLoc(), SourceLocation(), RowsParam, false,
208 DeclarationNameInfo(RowsParam->getDeclName(), SourceLocation()),
209 AST.IntTy, VK_LValue),
211 AST, NestedNameSpecifierLoc(), SourceLocation(), ColsParam, false,
212 DeclarationNameInfo(ColsParam->getDeclName(), SourceLocation()),
213 AST.IntTy, VK_LValue),
215
216 auto *Record = TypeAliasDecl::Create(AST, HLSLNamespace, SourceLocation(),
217 SourceLocation(), &II,
218 AST.getTrivialTypeSourceInfo(AliasType));
219 Record->setImplicit(true);
220
221 auto *Template =
222 TypeAliasTemplateDecl::Create(AST, HLSLNamespace, SourceLocation(),
223 Record->getIdentifier(), ParamList, Record);
224
225 Record->setDescribedAliasTemplate(Template);
226 Template->setImplicit(true);
227 Template->setLexicalDeclContext(Record->getDeclContext());
228 HLSLNamespace->addDecl(Template);
229}
230
231void HLSLExternalSemaSource::defineTrivialHLSLTypes() {
232 defineHLSLVectorAlias();
233 defineHLSLMatrixAlias();
234}
235
236/// Set up common members and attributes for buffer types
238 ResourceClass RC, bool IsROV,
239 bool RawBuffer, bool HasCounter) {
241 .addBufferHandles(RC, IsROV, RawBuffer, HasCounter)
246}
247
248/// Set up common members and attributes for sampler types
257
258/// Set up common members and attributes for texture types
282
283// Add a partial specialization for a template. The `TextureTemplate` is
284// `Texture<element_type>`, and it will be specialized for vectors:
285// `Texture<vector<element_type, element_count>>`.
288 ClassTemplateDecl *TextureTemplate) {
289 ASTContext &AST = S.getASTContext();
290
291 // Create the template parameters: element_type and element_count.
292 auto *ElementType = TemplateTypeParmDecl::Create(
293 AST, HLSLNamespace, SourceLocation(), SourceLocation(), 0, 0,
294 &AST.Idents.get("element_type"), false, false);
295 auto *ElementCount = NonTypeTemplateParmDecl::Create(
296 AST, HLSLNamespace, SourceLocation(), SourceLocation(), 0, 1,
297 &AST.Idents.get("element_count"), AST.IntTy, false,
299
300 auto *TemplateParams = TemplateParameterList::Create(
301 AST, SourceLocation(), SourceLocation(), {ElementType, ElementCount},
302 SourceLocation(), nullptr);
303
304 // Create the dependent vector type: vector<element_type, element_count>.
306 AST.getTemplateTypeParmType(0, 0, false, ElementType),
308 AST, NestedNameSpecifierLoc(), SourceLocation(), ElementCount, false,
309 DeclarationNameInfo(ElementCount->getDeclName(), SourceLocation()),
310 AST.IntTy, VK_LValue),
312
313 // Create the partial specialization declaration.
314 QualType CanonInjectedTST =
318
319 // Set the template arguments as written.
321 TemplateArgumentLoc ArgLoc =
323 TemplateArgumentListInfo ArgsInfo =
325 ArgsInfo.addArgument(ArgLoc);
326
328 AST, TagDecl::TagKind::Class, HLSLNamespace, SourceLocation(),
329 SourceLocation(), TemplateParams,
330 ASTTemplateArgumentListInfo::Create(AST, ArgsInfo), TextureTemplate,
332 CanQualType::CreateUnsafe(CanonInjectedTST), nullptr);
333
334 PartialSpec->setImplicit(true);
335 PartialSpec->setLexicalDeclContext(HLSLNamespace);
336 PartialSpec->setHasExternalLexicalStorage();
337
338 // Add the partial specialization to the namespace and the class template.
339 HLSLNamespace->addDecl(PartialSpec);
340 TextureTemplate->AddPartialSpecialization(PartialSpec, nullptr);
341
342 return PartialSpec;
343}
344
345// This function is responsible for constructing the constraint expression for
346// this concept:
347// template<typename T> concept is_typed_resource_element_compatible =
348// __is_typed_resource_element_compatible<T>;
351 ASTContext &Context = S.getASTContext();
352
353 // Obtain the QualType for 'bool'
354 QualType BoolTy = Context.BoolTy;
355
356 // Create a QualType that points to this TemplateTypeParmDecl
357 QualType TType = Context.getTypeDeclType(T);
358
359 // Create a TypeSourceInfo for the template type parameter 'T'
360 TypeSourceInfo *TTypeSourceInfo =
361 Context.getTrivialTypeSourceInfo(TType, NameLoc);
362
363 TypeTraitExpr *TypedResExpr = TypeTraitExpr::Create(
364 Context, BoolTy, NameLoc, UTT_IsTypedResourceElementCompatible,
365 {TTypeSourceInfo}, NameLoc, true);
366
367 return TypedResExpr;
368}
369
370// This function is responsible for constructing the constraint expression for
371// this concept:
372// template<typename T> concept is_constant_buffer_element_compatible =
373// std::is_class_v<T> && !__is_intangible(T);
375 SourceLocation NameLoc,
377 ASTContext &Context = S.getASTContext();
378
379 // Obtain the QualType for 'bool'
380 QualType BoolTy = Context.BoolTy;
381
382 // Create a QualType that points to this TemplateTypeParmDecl
383 QualType TType = Context.getTypeDeclType(T);
384
385 // Create a TypeSourceInfo for the template type parameter 'T'
386 TypeSourceInfo *TTypeSourceInfo =
387 Context.getTrivialTypeSourceInfo(TType, NameLoc);
388
390 Context, BoolTy, NameLoc, UTT_IsConstantBufferElementCompatible,
391 {TTypeSourceInfo}, NameLoc, true);
392
393 return ResExpr;
394}
395
396// This function is responsible for constructing the constraint expression for
397// this concept:
398// template<typename T> concept is_structured_resource_element_compatible =
399// !__is_intangible<T> && sizeof(T) >= 1;
401 SourceLocation NameLoc,
403 ASTContext &Context = S.getASTContext();
404
405 // Obtain the QualType for 'bool'
406 QualType BoolTy = Context.BoolTy;
407
408 // Create a QualType that points to this TemplateTypeParmDecl
409 QualType TType = Context.getTypeDeclType(T);
410
411 // Create a TypeSourceInfo for the template type parameter 'T'
412 TypeSourceInfo *TTypeSourceInfo =
413 Context.getTrivialTypeSourceInfo(TType, NameLoc);
414
415 TypeTraitExpr *IsIntangibleExpr =
416 TypeTraitExpr::Create(Context, BoolTy, NameLoc, UTT_IsIntangibleType,
417 {TTypeSourceInfo}, NameLoc, true);
418
419 // negate IsIntangibleExpr
420 UnaryOperator *NotIntangibleExpr = UnaryOperator::Create(
421 Context, IsIntangibleExpr, UO_LNot, BoolTy, VK_LValue, OK_Ordinary,
422 NameLoc, false, FPOptionsOverride());
423
424 // element types also may not be of 0 size
425 UnaryExprOrTypeTraitExpr *SizeOfExpr = new (Context) UnaryExprOrTypeTraitExpr(
426 UETT_SizeOf, TTypeSourceInfo, BoolTy, NameLoc, NameLoc);
427
428 // Create a BinaryOperator that checks if the size of the type is not equal to
429 // 1 Empty structs have a size of 1 in HLSL, so we need to check for that
431 Context, llvm::APInt(Context.getTypeSize(Context.getSizeType()), 1, true),
432 Context.getSizeType(), NameLoc);
433
434 BinaryOperator *SizeGEQOneExpr =
435 BinaryOperator::Create(Context, SizeOfExpr, rhs, BO_GE, BoolTy, VK_LValue,
436 OK_Ordinary, NameLoc, FPOptionsOverride());
437
438 // Combine the two constraints
440 Context, NotIntangibleExpr, SizeGEQOneExpr, BO_LAnd, BoolTy, VK_LValue,
441 OK_Ordinary, NameLoc, FPOptionsOverride());
442
443 return CombinedExpr;
444}
445
447
449 HLSLBufferType BT) {
450 ASTContext &Context = S.getASTContext();
451 DeclContext *DC = NSD->getDeclContext();
452 SourceLocation DeclLoc = SourceLocation();
453
454 IdentifierInfo &ElementTypeII = Context.Idents.get("element_type");
456 Context, NSD->getDeclContext(), DeclLoc, DeclLoc,
457 /*D=*/0,
458 /*P=*/0,
459 /*Id=*/&ElementTypeII,
460 /*Typename=*/true,
461 /*ParameterPack=*/false);
462
463 T->setDeclContext(DC);
464 T->setReferenced();
465
466 // Create and Attach Template Parameter List to ConceptDecl
468 Context, DeclLoc, DeclLoc, {T}, DeclLoc, nullptr);
469
470 DeclarationName DeclName;
471 Expr *ConstraintExpr = nullptr;
472
473 switch (BT) {
475 DeclName = DeclarationName(
476 &Context.Idents.get("__is_typed_resource_element_compatible"));
477 ConstraintExpr = constructTypedBufferConstraintExpr(S, DeclLoc, T);
478 break;
480 DeclName = DeclarationName(
481 &Context.Idents.get("__is_structured_resource_element_compatible"));
482 ConstraintExpr = constructStructuredBufferConstraintExpr(S, DeclLoc, T);
483 break;
485 DeclName = DeclarationName(
486 &Context.Idents.get("__is_constant_buffer_element_compatible"));
487 ConstraintExpr = constructConstantBufferConstraintExpr(S, DeclLoc, T);
488 break;
489 }
490
491 // Create a ConceptDecl
492 ConceptDecl *CD =
493 ConceptDecl::Create(Context, NSD->getDeclContext(), DeclLoc, DeclName,
494 ConceptParams, ConstraintExpr);
495
496 // Attach the template parameter list to the ConceptDecl
497 CD->setTemplateParameters(ConceptParams);
498
499 // Add the concept declaration to the Translation Unit Decl
500 NSD->getDeclContext()->addDecl(CD);
501
502 return CD;
503}
504
505void HLSLExternalSemaSource::defineHLSLTypesWithForwardDeclarations() {
506 ASTContext &AST = SemaPtr->getASTContext();
507 CXXRecordDecl *Decl;
508 ConceptDecl *TypedBufferConcept = constructBufferConceptDecl(
509 *SemaPtr, HLSLNamespace, HLSLBufferType::Typed);
510 ConceptDecl *StructuredBufferConcept = constructBufferConceptDecl(
511 *SemaPtr, HLSLNamespace, HLSLBufferType::Structured);
512 ConceptDecl *ConstantBufferConcept = constructBufferConceptDecl(
513 *SemaPtr, HLSLNamespace, HLSLBufferType::Constant);
514
515 Decl = BuiltinTypeDeclBuilder(*SemaPtr, HLSLNamespace, "ConstantBuffer")
516 .addSimpleTemplateParams({"element_type"}, ConstantBufferConcept)
517 .finalizeForwardDeclaration();
518
519 onCompletion(Decl, [this](CXXRecordDecl *Decl) {
520 setupBufferType(Decl, *SemaPtr, ResourceClass::CBuffer, /*IsROV=*/false,
521 /*RawBuffer=*/false, /*HasCounter=*/false)
524 });
525
526 Decl = BuiltinTypeDeclBuilder(*SemaPtr, HLSLNamespace, "Buffer")
527 .addSimpleTemplateParams({"element_type"}, TypedBufferConcept)
528 .finalizeForwardDeclaration();
529
530 onCompletion(Decl, [this](CXXRecordDecl *Decl) {
531 setupBufferType(Decl, *SemaPtr, ResourceClass::SRV, /*IsROV=*/false,
532 /*RawBuffer=*/false, /*HasCounter=*/false)
537 });
538
539 Decl = BuiltinTypeDeclBuilder(*SemaPtr, HLSLNamespace, "RWBuffer")
540 .addSimpleTemplateParams({"element_type"}, TypedBufferConcept)
541 .finalizeForwardDeclaration();
542
543 onCompletion(Decl, [this](CXXRecordDecl *Decl) {
544 setupBufferType(Decl, *SemaPtr, ResourceClass::UAV, /*IsROV=*/false,
545 /*RawBuffer=*/false, /*HasCounter=*/false)
550 });
551
552 Decl =
553 BuiltinTypeDeclBuilder(*SemaPtr, HLSLNamespace, "RasterizerOrderedBuffer")
554 .addSimpleTemplateParams({"element_type"}, StructuredBufferConcept)
555 .finalizeForwardDeclaration();
556 onCompletion(Decl, [this](CXXRecordDecl *Decl) {
557 setupBufferType(Decl, *SemaPtr, ResourceClass::UAV, /*IsROV=*/true,
558 /*RawBuffer=*/false, /*HasCounter=*/false)
563 });
564
565 Decl = BuiltinTypeDeclBuilder(*SemaPtr, HLSLNamespace, "StructuredBuffer")
566 .addSimpleTemplateParams({"element_type"}, StructuredBufferConcept)
567 .finalizeForwardDeclaration();
568 onCompletion(Decl, [this](CXXRecordDecl *Decl) {
569 setupBufferType(Decl, *SemaPtr, ResourceClass::SRV, /*IsROV=*/false,
570 /*RawBuffer=*/true, /*HasCounter=*/false)
575 });
576
577 Decl = BuiltinTypeDeclBuilder(*SemaPtr, HLSLNamespace, "RWStructuredBuffer")
578 .addSimpleTemplateParams({"element_type"}, StructuredBufferConcept)
579 .finalizeForwardDeclaration();
580 onCompletion(Decl, [this](CXXRecordDecl *Decl) {
581 setupBufferType(Decl, *SemaPtr, ResourceClass::UAV, /*IsROV=*/false,
582 /*RawBuffer=*/true, /*HasCounter=*/true)
589 });
590
591 Decl =
592 BuiltinTypeDeclBuilder(*SemaPtr, HLSLNamespace, "AppendStructuredBuffer")
593 .addSimpleTemplateParams({"element_type"}, StructuredBufferConcept)
594 .finalizeForwardDeclaration();
595 onCompletion(Decl, [this](CXXRecordDecl *Decl) {
596 setupBufferType(Decl, *SemaPtr, ResourceClass::UAV, /*IsROV=*/false,
597 /*RawBuffer=*/true, /*HasCounter=*/true)
601 });
602
603 Decl =
604 BuiltinTypeDeclBuilder(*SemaPtr, HLSLNamespace, "ConsumeStructuredBuffer")
605 .addSimpleTemplateParams({"element_type"}, StructuredBufferConcept)
606 .finalizeForwardDeclaration();
607 onCompletion(Decl, [this](CXXRecordDecl *Decl) {
608 setupBufferType(Decl, *SemaPtr, ResourceClass::UAV, /*IsROV=*/false,
609 /*RawBuffer=*/true, /*HasCounter=*/true)
613 });
614
615 Decl = BuiltinTypeDeclBuilder(*SemaPtr, HLSLNamespace,
616 "RasterizerOrderedStructuredBuffer")
617 .addSimpleTemplateParams({"element_type"}, StructuredBufferConcept)
618 .finalizeForwardDeclaration();
619 onCompletion(Decl, [this](CXXRecordDecl *Decl) {
620 setupBufferType(Decl, *SemaPtr, ResourceClass::UAV, /*IsROV=*/true,
621 /*RawBuffer=*/true, /*HasCounter=*/true)
628 });
629
630 Decl = BuiltinTypeDeclBuilder(*SemaPtr, HLSLNamespace, "ByteAddressBuffer")
631 .finalizeForwardDeclaration();
632 onCompletion(Decl, [this](CXXRecordDecl *Decl) {
633 setupBufferType(Decl, *SemaPtr, ResourceClass::SRV, /*IsROV=*/false,
634 /*RawBuffer=*/true, /*HasCounter=*/false)
638 });
639 Decl = BuiltinTypeDeclBuilder(*SemaPtr, HLSLNamespace, "RWByteAddressBuffer")
640 .finalizeForwardDeclaration();
641 onCompletion(Decl, [this](CXXRecordDecl *Decl) {
642 setupBufferType(Decl, *SemaPtr, ResourceClass::UAV, /*IsROV=*/false,
643 /*RawBuffer=*/true, /*HasCounter=*/false)
648 });
649 Decl = BuiltinTypeDeclBuilder(*SemaPtr, HLSLNamespace,
650 "RasterizerOrderedByteAddressBuffer")
651 .finalizeForwardDeclaration();
652 onCompletion(Decl, [this](CXXRecordDecl *Decl) {
653 setupBufferType(Decl, *SemaPtr, ResourceClass::UAV, /*IsROV=*/true,
654 /*RawBuffer=*/true, /*HasCounter=*/false)
657 });
658
659 Decl = BuiltinTypeDeclBuilder(*SemaPtr, HLSLNamespace, "SamplerState")
660 .finalizeForwardDeclaration();
661 onCompletion(Decl, [this](CXXRecordDecl *Decl) {
662 setupSamplerType(Decl, *SemaPtr).completeDefinition();
663 });
664
665 Decl =
666 BuiltinTypeDeclBuilder(*SemaPtr, HLSLNamespace, "SamplerComparisonState")
667 .finalizeForwardDeclaration();
668 onCompletion(Decl, [this](CXXRecordDecl *Decl) {
669 setupSamplerType(Decl, *SemaPtr).completeDefinition();
670 });
671
672 QualType Float4Ty = AST.getExtVectorType(AST.FloatTy, 4);
673 Decl = BuiltinTypeDeclBuilder(*SemaPtr, HLSLNamespace, "Texture2D")
674 .addSimpleTemplateParams({"element_type"}, {Float4Ty},
675 TypedBufferConcept)
676 .finalizeForwardDeclaration();
677
678 onCompletion(Decl, [this](CXXRecordDecl *Decl) {
679 setupTextureType(Decl, *SemaPtr, ResourceClass::SRV, /*IsROV=*/false,
680 ResourceDimension::Dim2D)
682 });
683
684 auto *PartialSpec = addVectorTexturePartialSpecialization(
685 *SemaPtr, HLSLNamespace, Decl->getDescribedClassTemplate());
686 onCompletion(PartialSpec, [this](CXXRecordDecl *Decl) {
687 setupTextureType(Decl, *SemaPtr, ResourceClass::SRV, /*IsROV=*/false,
688 ResourceDimension::Dim2D)
690 });
691}
692
693// Build a single overload of an HLSL atomic intrinsic in the hlsl namespace.
694// `dest` is an address-space-qualified reference; `original_value` (when
695// present) is a plain reference. The synthesized FunctionDecl aliases the
696// underlying clang builtin via BuiltinAliasAttr.
697static void buildAtomicOverload(Sema &S, NamespaceDecl *NS, StringRef FuncName,
698 StringRef BuiltinName, QualType ElemTy,
699 LangAS DestAS, bool ThreeArg) {
700 ASTContext &AST = S.getASTContext();
701
702 QualType DestTy =
703 AST.getLValueReferenceType(AST.getAddrSpaceQualType(ElemTy, DestAS));
704 QualType OrigRefTy = AST.getLValueReferenceType(ElemTy);
705
706 SmallVector<QualType, 3> ParamTypes;
707 ParamTypes.push_back(DestTy);
708 ParamTypes.push_back(ElemTy);
709 if (ThreeArg)
710 ParamTypes.push_back(OrigRefTy);
711
713 QualType FuncTy = AST.getFunctionType(AST.VoidTy, ParamTypes, EPI);
714 auto *TSInfo = AST.getTrivialTypeSourceInfo(FuncTy, SourceLocation());
715
716 IdentifierInfo &FuncII = AST.Idents.get(FuncName, tok::TokenKind::identifier);
717 DeclarationName FuncDeclName(&FuncII);
718
720 AST, NS, SourceLocation(), SourceLocation(), FuncDeclName, FuncTy, TSInfo,
721 SC_Extern, /*UsesFPIntrin=*/false, /*isInlineSpecified=*/false,
722 /*hasWrittenPrototype=*/true);
723
724 constexpr const char *ParamNames[] = {"dest", "value", "original_value"};
726 unsigned I = 0;
727 for (auto [ParamType, ParamName] : llvm::zip(ParamTypes, ParamNames)) {
728 IdentifierInfo &PII = AST.Idents.get(ParamName, tok::TokenKind::identifier);
730 AST, FD, SourceLocation(), SourceLocation(), &PII, ParamType,
732 nullptr);
733 Parm->setScopeInfo(0, I++);
734 ParmDecls.push_back(Parm);
735 }
736 FD->setParams(ParmDecls);
737
738 IdentifierInfo &BuiltinII =
739 S.getPreprocessor().getIdentifierTable().get(BuiltinName);
740 FD->addAttr(BuiltinAliasAttr::CreateImplicit(AST, &BuiltinII));
741 FD->setImplicit();
742 NS->addDecl(FD);
743}
744
745// Synthesize the InterlockedAdd overload set: {int, uint, int64_t, uint64_t}
746// x {groupshared, device} x {2-arg, 3-arg}.
748 ASTContext &AST = S.getASTContext();
749 // HLSL: int64_t == long, uint64_t == unsigned long (see hlsl_basic_types.h).
750 QualType Elems[] = {AST.IntTy, AST.UnsignedIntTy, AST.LongTy,
751 AST.UnsignedLongTy};
753
754 for (QualType ElemTy : Elems)
755 for (LangAS AS : AddrSpaces)
756 for (bool ThreeArg : {false, true})
757 buildAtomicOverload(S, NS, "InterlockedAdd",
758 "__builtin_hlsl_interlocked_add", ElemTy, AS,
759 ThreeArg);
760}
761
762void HLSLExternalSemaSource::defineHLSLAtomicIntrinsics() {
763 defineHLSLInterlockedAdd(*SemaPtr, HLSLNamespace);
764}
765
766void HLSLExternalSemaSource::onCompletion(CXXRecordDecl *Record,
767 CompletionFunction Fn) {
768 if (!Record->isCompleteDefinition())
769 Completions.insert(std::make_pair(Record->getCanonicalDecl(), Fn));
770}
771
773 if (!isa<CXXRecordDecl>(Tag))
774 return;
775 auto Record = cast<CXXRecordDecl>(Tag);
776
777 // If this is a specialization, we need to get the underlying templated
778 // declaration and complete that.
779 if (auto TDecl = dyn_cast<ClassTemplateSpecializationDecl>(Record)) {
781 ClassTemplateDecl *Template = TDecl->getSpecializedTemplate();
783 Template->getPartialSpecializations(Partials);
784 ClassTemplatePartialSpecializationDecl *MatchedPartial = nullptr;
785 for (auto *Partial : Partials) {
786 sema::TemplateDeductionInfo Info(TDecl->getLocation());
787 if (SemaPtr->DeduceTemplateArguments(Partial, TDecl->getTemplateArgs(),
788 Info) ==
790 MatchedPartial = Partial;
791 break;
792 }
793 }
794 if (MatchedPartial)
795 Record = MatchedPartial;
796 else
797 Record = Template->getTemplatedDecl();
798 }
799 }
800 Record = Record->getCanonicalDecl();
801 auto It = Completions.find(Record);
802 if (It == Completions.end())
803 return;
804 // Move out the callback and erase before invoking it: the callback can
805 // re-enter CompleteType and mutate Completions, which invalidates It under
806 // backward-shift deletion.
807 CompletionFunction Fn = std::move(It->second);
808 Completions.erase(It);
809 Fn(Record);
810}
Defines the clang::ASTContext interface.
Provides definitions for the various language-specific address spaces.
llvm::dxil::ResourceClass ResourceClass
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate....
Defines the C++ template declaration subclasses.
static BuiltinTypeDeclBuilder setupBufferType(CXXRecordDecl *Decl, Sema &S, ResourceClass RC, bool IsROV, bool RawBuffer, bool HasCounter)
Set up common members and attributes for buffer types.
static void defineHLSLInterlockedAdd(Sema &S, NamespaceDecl *NS)
static BuiltinTypeDeclBuilder setupSamplerType(CXXRecordDecl *Decl, Sema &S)
Set up common members and attributes for sampler types.
static void buildAtomicOverload(Sema &S, NamespaceDecl *NS, StringRef FuncName, StringRef BuiltinName, QualType ElemTy, LangAS DestAS, bool ThreeArg)
static Expr * constructTypedBufferConstraintExpr(Sema &S, SourceLocation NameLoc, TemplateTypeParmDecl *T)
static ConceptDecl * constructBufferConceptDecl(Sema &S, NamespaceDecl *NSD, HLSLBufferType BT)
static BuiltinTypeDeclBuilder setupTextureType(CXXRecordDecl *Decl, Sema &S, ResourceClass RC, bool IsROV, ResourceDimension Dim)
Set up common members and attributes for texture types.
static Expr * constructConstantBufferConstraintExpr(Sema &S, SourceLocation NameLoc, TemplateTypeParmDecl *T)
static ClassTemplatePartialSpecializationDecl * addVectorTexturePartialSpecialization(Sema &S, NamespaceDecl *HLSLNamespace, ClassTemplateDecl *TextureTemplate)
static Expr * constructStructuredBufferConstraintExpr(Sema &S, SourceLocation NameLoc, TemplateTypeParmDecl *T)
llvm::MachO::Record Record
Definition MachO.h:31
Defines the clang::Preprocessor interface.
This file declares semantic analysis for HLSL constructs.
Defines the clang::SourceLocation class and associated facilities.
C Language Family Type Representation.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition ASTContext.h:223
TranslationUnitDecl * getTranslationUnitDecl() const
QualType getDependentSizedMatrixType(QualType ElementType, Expr *RowExpr, Expr *ColumnExpr, SourceLocation AttrLoc) const
Return the unique reference to the matrix type of the specified element type and size.
CanQualType LongTy
unsigned getIntWidth(QualType T) const
static CanQualType getCanonicalType(QualType T)
Return the canonical (structural) type corresponding to the specified potentially non-canonical type ...
CanQualType FloatTy
QualType getLValueReferenceType(QualType T, bool SpelledAsLValue=true) const
Return the uniqued reference to the type for an lvalue reference to the specified type.
IdentifierTable & Idents
Definition ASTContext.h:802
CanQualType BoolTy
CanQualType UnsignedLongTy
TypeSourceInfo * getTrivialTypeSourceInfo(QualType T, SourceLocation Loc=SourceLocation()) const
Allocate a TypeSourceInfo where all locations have been initialized to a given location,...
CanQualType IntTy
CanQualType VoidTy
CanQualType UnsignedIntTy
QualType getTemplateSpecializationType(ElaboratedTypeKeyword Keyword, TemplateName T, ArrayRef< TemplateArgument > SpecifiedArgs, ArrayRef< TemplateArgument > CanonicalArgs, QualType Underlying=QualType()) const
QualType getFunctionType(QualType ResultTy, ArrayRef< QualType > Args, const FunctionProtoType::ExtProtoInfo &EPI) const
Return a normal function type with a typed argument list.
QualType getTemplateTypeParmType(int Depth, int Index, bool ParameterPack, TemplateTypeParmDecl *ParmDecl=nullptr) const
Retrieve the template type parameter type for a template parameter or parameter pack with the given d...
QualType getExtVectorType(QualType VectorType, unsigned NumElts) const
Return the unique reference to an extended vector type of the specified element type and size.
QualType getAddrSpaceQualType(QualType T, LangAS AddressSpace) const
Return the uniqued reference to the type for an address space qualified type with the specified type ...
QualType getDependentSizedExtVectorType(QualType VectorType, Expr *SizeExpr, SourceLocation AttrLoc) const
A builtin binary operation expression such as "x + y" or "x <= y".
Definition Expr.h:4041
static BinaryOperator * Create(const ASTContext &C, Expr *lhs, Expr *rhs, Opcode opc, QualType ResTy, ExprValueKind VK, ExprObjectKind OK, SourceLocation opLoc, FPOptionsOverride FPFeatures)
Definition Expr.cpp:5103
Represents a C++ struct/union/class.
Definition DeclCXX.h:258
static CanQual< Type > CreateUnsafe(QualType Other)
Declaration of a class template.
void AddPartialSpecialization(ClassTemplatePartialSpecializationDecl *D, void *InsertPos)
Insert the specified partial specialization knowing that it is not already in.
static ClassTemplatePartialSpecializationDecl * Create(ASTContext &Context, TagKind TK, DeclContext *DC, SourceLocation StartLoc, SourceLocation IdLoc, TemplateParameterList *Params, const ASTTemplateArgumentListInfo *TemplateArgsAsWritten, ClassTemplateDecl *SpecializedTemplate, ArrayRef< TemplateArgument > Args, CanQualType CanonInjectedTST, ClassTemplatePartialSpecializationDecl *PrevDecl)
Declaration of a C++20 concept.
static ConceptDecl * Create(ASTContext &C, DeclContext *DC, SourceLocation L, DeclarationName Name, TemplateParameterList *Params, Expr *ConstraintExpr=nullptr)
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
Definition DeclBase.h:1462
void addDecl(Decl *D)
Add the declaration D into this context.
bool hasExternalLexicalStorage() const
Whether this DeclContext has external storage containing additional declarations that are lexically i...
Definition DeclBase.h:2701
decl_iterator decls_begin() const
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:494
Decl - This represents one declaration (or definition), e.g.
Definition DeclBase.h:86
void addAttr(Attr *A)
void setImplicit(bool I=true)
Definition DeclBase.h:602
DeclContext * getDeclContext()
Definition DeclBase.h:456
The name of a declaration.
This represents one expression.
Definition Expr.h:112
Represents difference between two FPOptions values.
Represents a function declaration or definition.
Definition Decl.h:2018
static FunctionDecl * Create(ASTContext &C, DeclContext *DC, SourceLocation StartLoc, SourceLocation NLoc, DeclarationName N, QualType T, TypeSourceInfo *TInfo, StorageClass SC, bool UsesFPIntrin=false, bool isInlineSpecified=false, bool hasWrittenPrototype=true, ConstexprSpecKind ConstexprKind=ConstexprSpecKind::Unspecified, const AssociatedConstraint &TrailingRequiresClause={})
Definition Decl.h:2179
void CompleteType(TagDecl *Tag) override
Complete an incomplete HLSL builtin type.
void InitializeSema(Sema &S) override
Initialize the semantic source with the Sema instance being used to perform semantic analysis on the ...
One of these records is kept for each identifier that is lexed.
IdentifierInfo & get(StringRef Name)
Return the identifier token info for the specified named identifier.
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:981
Represents the results of name lookup.
Definition Lookup.h:147
Represent a C++ namespace.
Definition Decl.h:592
static NamespaceDecl * Create(ASTContext &C, DeclContext *DC, bool Inline, SourceLocation StartLoc, SourceLocation IdLoc, IdentifierInfo *Id, NamespaceDecl *PrevDecl, bool Nested)
Definition DeclCXX.cpp:3361
A C++ nested-name-specifier augmented with source location information.
static NonTypeTemplateParmDecl * Create(const ASTContext &C, DeclContext *DC, SourceLocation StartLoc, SourceLocation IdLoc, int D, int P, const IdentifierInfo *Id, QualType T, bool ParameterPack, TypeSourceInfo *TInfo)
Represents a parameter to a function.
Definition Decl.h:1808
void setScopeInfo(unsigned scopeDepth, unsigned parameterIndex)
Definition Decl.h:1841
static ParmVarDecl * Create(ASTContext &C, DeclContext *DC, SourceLocation StartLoc, SourceLocation IdLoc, const IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo, StorageClass S, Expr *DefArg)
Definition Decl.cpp:2932
IdentifierTable & getIdentifierTable()
A (possibly-)qualified type.
Definition TypeBase.h:937
C++2a [expr.prim.req]: A requires-expression provides a concise way to express requirements on templa...
Sema - This implements semantic analysis and AST building for C.
Definition Sema.h:868
@ LookupNamespaceName
Look up a namespace name within a C++ using directive or namespace alias definition,...
Definition Sema.h:9443
Preprocessor & getPreprocessor() const
Definition Sema.h:938
ASTContext & getASTContext() const
Definition Sema.h:939
const LangOptions & getLangOpts() const
Definition Sema.h:932
TemplateArgumentLoc getTrivialTemplateArgumentLoc(const TemplateArgument &Arg, QualType NTTPType, SourceLocation Loc)
Allocate a TemplateArgumentLoc where all locations have been initialized to the given location.
bool LookupQualifiedName(LookupResult &R, DeclContext *LookupCtx, bool InUnqualifiedLookup=false)
Perform qualified name lookup into a given context.
Encodes a location in the source.
Represents the declaration of a struct/union/class/enum.
Definition Decl.h:3723
A convenient class for passing around template argument information.
void addArgument(const TemplateArgumentLoc &Loc)
Location wrapper for a TemplateArgument.
Represents a template argument.
void setTemplateParameters(TemplateParameterList *TParams)
Represents a C++ template name within the type system.
Stores a list of template parameters for a TemplateDecl and its derived classes.
static TemplateParameterList * Create(const ASTContext &C, SourceLocation TemplateLoc, SourceLocation LAngleLoc, ArrayRef< NamedDecl * > Params, SourceLocation RAngleLoc, Expr *RequiresClause)
Declaration of a template type parameter.
static TemplateTypeParmDecl * Create(const ASTContext &C, DeclContext *DC, SourceLocation KeyLoc, SourceLocation NameLoc, int D, int P, IdentifierInfo *Id, bool Typename, bool ParameterPack, bool HasTypeConstraint=false, UnsignedOrNone NumExpanded=std::nullopt)
static TypeAliasDecl * Create(ASTContext &C, DeclContext *DC, SourceLocation StartLoc, SourceLocation IdLoc, const IdentifierInfo *Id, TypeSourceInfo *TInfo)
Definition Decl.cpp:5838
static TypeAliasTemplateDecl * Create(ASTContext &C, DeclContext *DC, SourceLocation L, DeclarationName Name, TemplateParameterList *Params, NamedDecl *Decl)
Create a function template node.
A container of type source information.
Definition TypeBase.h:8411
A type trait used in the implementation of various C++11 and Library TR1 trait templates.
Definition ExprCXX.h:2900
static TypeTraitExpr * Create(const ASTContext &C, QualType T, SourceLocation Loc, TypeTrait Kind, ArrayRef< TypeSourceInfo * > Args, SourceLocation RParenLoc, bool Value)
Create a new type trait expression.
Definition ExprCXX.cpp:1907
UnaryExprOrTypeTraitExpr - expression with either a type or (unevaluated) expression operand.
Definition Expr.h:2628
UnaryOperator - This represents the unary-expression's (except sizeof and alignof),...
Definition Expr.h:2247
static UnaryOperator * Create(const ASTContext &C, Expr *input, Opcode opc, QualType type, ExprValueKind VK, ExprObjectKind OK, SourceLocation l, bool CanOverflow, FPOptionsOverride FPFeatures)
Definition Expr.cpp:5160
Represents a C++ using-declaration.
Definition DeclCXX.h:3596
static UsingDirectiveDecl * Create(ASTContext &C, DeclContext *DC, SourceLocation UsingLoc, SourceLocation NamespaceLoc, NestedNameSpecifierLoc QualifierLoc, SourceLocation IdentLoc, NamedDecl *Nominated, DeclContext *CommonAncestor)
Definition DeclCXX.cpp:3317
Represents a GCC generic vector type.
Definition TypeBase.h:4237
BuiltinTypeDeclBuilder & addDefaultHandleConstructor(AccessSpecifier Access=AccessSpecifier::AS_public)
BuiltinTypeDeclBuilder & addCopyAssignmentOperator(AccessSpecifier Access=AccessSpecifier::AS_public)
BuiltinTypeDeclBuilder & addArraySubscriptOperators(ResourceDimension Dim=ResourceDimension::Unknown)
BuiltinTypeDeclBuilder & addSampleGradMethods(ResourceDimension Dim)
BuiltinTypeDeclBuilder & addGetDimensionsMethodForBuffer()
BuiltinTypeDeclBuilder & addGatherCmpMethods(ResourceDimension Dim)
BuiltinTypeDeclBuilder & addConstantBufferConversionToType()
BuiltinTypeDeclBuilder & addSampleBiasMethods(ResourceDimension Dim)
BuiltinTypeDeclBuilder & addBufferHandles(ResourceClass RC, bool IsROV, bool RawBuffer, bool HasCounter, AccessSpecifier Access=AccessSpecifier::AS_private)
BuiltinTypeDeclBuilder & addByteAddressBufferStoreMethods()
BuiltinTypeDeclBuilder & addTextureHandle(ResourceClass RC, bool IsROV, ResourceDimension RD, AccessSpecifier Access=AccessSpecifier::AS_private)
BuiltinTypeDeclBuilder & addCopyConstructor(AccessSpecifier Access=AccessSpecifier::AS_public)
BuiltinTypeDeclBuilder & addTextureLoadMethods(ResourceDimension Dim)
BuiltinTypeDeclBuilder & addSampleLevelMethods(ResourceDimension Dim)
BuiltinTypeDeclBuilder & addMipsMember(ResourceDimension Dim)
BuiltinTypeDeclBuilder & addByteAddressBufferLoadMethods()
BuiltinTypeDeclBuilder & addSampleMethods(ResourceDimension Dim)
BuiltinTypeDeclBuilder & addStaticInitializationFunctions(bool HasCounter)
BuiltinTypeDeclBuilder & addCalculateLodMethods(ResourceDimension Dim)
BuiltinTypeDeclBuilder & addGetDimensionsMethods(ResourceDimension Dim)
BuiltinTypeDeclBuilder & addSampleCmpLevelZeroMethods(ResourceDimension Dim)
BuiltinTypeDeclBuilder & addGatherMethods(ResourceDimension Dim)
BuiltinTypeDeclBuilder & addSampleCmpMethods(ResourceDimension Dim)
Provides information about an attempted template argument deduction, whose success or failure was des...
std::variant< struct RequiresDecl, struct HeaderDecl, struct UmbrellaDirDecl, struct ModuleDecl, struct ExcludeDecl, struct ExportDecl, struct ExportAsDecl, struct ExternModuleDecl, struct UseDecl, struct LinkDecl, struct ConfigMacrosDecl, struct ConflictDecl > Decl
All declarations that can appear in a module declaration.
The JSON file list parser is used to communicate input to InstallAPI.
bool isa(CodeGen::Address addr)
Definition Address.h:330
@ OK_Ordinary
An ordinary object is located at an address in memory.
Definition Specifiers.h:152
@ SC_Extern
Definition Specifiers.h:255
@ SC_None
Definition Specifiers.h:254
@ Result
The result type of a method or function.
Definition TypeBase.h:905
@ Template
We are parsing a template declaration.
Definition Parser.h:81
LangAS
Defines the address space values used by the address space qualifier of QualType.
@ VK_PRValue
A pr-value expression (in the C++11 taxonomy) produces a temporary value.
Definition Specifiers.h:136
@ VK_LValue
An l-value expression is a reference to an object with independent storage.
Definition Specifiers.h:140
@ Success
Template argument deduction was successful.
Definition Sema.h:371
U cast(CodeGen::Address addr)
Definition Address.h:327
@ Class
The "class" keyword introduces the elaborated-type-specifier.
Definition TypeBase.h:5979
static const ASTTemplateArgumentListInfo * Create(const ASTContext &C, const TemplateArgumentListInfo &List)
DeclarationNameInfo - A collector data type for bundling together a DeclarationName and the correspon...
Extra information about a function prototype.
Definition TypeBase.h:5454