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
12#include "clang/Sema/HLSLExternalSemaSource.h"
13#include "HLSLBuiltinTypeDeclBuilder.h"
14#include "clang/AST/ASTContext.h"
15#include "clang/AST/Attr.h"
16#include "clang/AST/Decl.h"
17#include "clang/AST/DeclCXX.h"
18#include "clang/AST/DeclTemplate.h"
19#include "clang/AST/Expr.h"
20#include "clang/AST/Type.h"
21#include "clang/Basic/AddressSpaces.h"
22#include "clang/Basic/SourceLocation.h"
23#include "clang/Lex/Preprocessor.h"
24#include "clang/Sema/Lookup.h"
25#include "clang/Sema/Sema.h"
26#include "clang/Sema/SemaHLSL.h"
27#include "llvm/ADT/BitmaskEnum.h"
28#include "llvm/ADT/STLExtras.h"
29#include "llvm/ADT/SmallVector.h"
30
31using namespace clang;
32using namespace llvm::hlsl;
33
34using clang::hlsl::BuiltinTypeDeclBuilder;
35
36static NamespaceDecl *createImplicitNamespace(Sema &S, StringRef Name,
37 DeclContext *DC) {
38 ASTContext &AST = S.getASTContext();
39 IdentifierInfo &II = AST.Idents.get(Name, TokenCode: tok::TokenKind::identifier);
40 LookupResult Result(S, &II, SourceLocation(), Sema::LookupNamespaceName);
41 NamespaceDecl *PrevDecl = nullptr;
42 if (S.LookupQualifiedName(R&: Result, LookupCtx: DC))
43 PrevDecl = Result.getAsSingle<NamespaceDecl>();
44
45 NamespaceDecl *NS =
46 NamespaceDecl::Create(C&: AST, DC, /*Inline=*/false, StartLoc: SourceLocation(),
47 IdLoc: SourceLocation(), Id: &II, PrevDecl, /*Nested=*/false);
48 NS->setImplicit(true);
49 NS->setHasExternalLexicalStorage();
50 DC->addDecl(D: NS);
51
52 // Force external decls in the namespace to load from the PCH.
53 (void)NS->getCanonicalDecl()->decls_begin();
54
55 return NS;
56}
57
58void HLSLExternalSemaSource::InitializeSema(Sema &S) {
59 SemaPtr = &S;
60 ASTContext &AST = SemaPtr->getASTContext();
61 // If the translation unit has external storage force external decls to load.
62 if (AST.getTranslationUnitDecl()->hasExternalLexicalStorage())
63 (void)AST.getTranslationUnitDecl()->decls_begin();
64
65 HLSLNamespace = createImplicitNamespace(
66 S, Name: "hlsl", DC: cast<DeclContext>(Val: AST.getTranslationUnitDecl()));
67 HLSLDetailNamespace = createImplicitNamespace(S, Name: "__detail", DC: HLSLNamespace);
68
69 defineTrivialHLSLTypes();
70 defineInternalHLSLTypes();
71 defineHLSLTypesWithForwardDeclarations();
72 defineHLSLAtomicIntrinsics();
73
74 if (SemaPtr->getLangOpts().getHLSLVersion() >= LangOptions::HLSL_202x)
75 return;
76
77 // This adds a `using namespace hlsl` directive. In DXC, we don't put HLSL's
78 // built in types inside a namespace, but we are planning to change that in
79 // the near future. In order to be source compatible, HLSL before 202x
80 // will need to implictly use the hlsl namespace.
81 auto *UsingDecl = UsingDirectiveDecl::Create(
82 C&: AST, DC: AST.getTranslationUnitDecl(), UsingLoc: SourceLocation(), NamespaceLoc: SourceLocation(),
83 QualifierLoc: NestedNameSpecifierLoc(), IdentLoc: SourceLocation(), Nominated: HLSLNamespace,
84 CommonAncestor: AST.getTranslationUnitDecl());
85
86 AST.getTranslationUnitDecl()->addDecl(D: UsingDecl);
87}
88
89void HLSLExternalSemaSource::defineHLSLVectorAlias() {
90 ASTContext &AST = SemaPtr->getASTContext();
91
92 llvm::SmallVector<NamedDecl *> TemplateParams;
93
94 auto *TypeParam = TemplateTypeParmDecl::Create(
95 C: AST, DC: HLSLNamespace, KeyLoc: SourceLocation(), NameLoc: SourceLocation(), D: 0, P: 0,
96 Id: &AST.Idents.get(Name: "element", TokenCode: tok::TokenKind::identifier), Typename: false, ParameterPack: false);
97 TypeParam->setDefaultArgument(
98 C: AST, DefArg: SemaPtr->getTrivialTemplateArgumentLoc(
99 Arg: TemplateArgument(AST.FloatTy), NTTPType: QualType(), Loc: SourceLocation()));
100
101 TemplateParams.emplace_back(Args&: TypeParam);
102
103 auto *SizeParam = NonTypeTemplateParmDecl::Create(
104 C: AST, DC: HLSLNamespace, StartLoc: SourceLocation(), IdLoc: SourceLocation(), D: 0, P: 1,
105 Id: &AST.Idents.get(Name: "element_count", TokenCode: tok::TokenKind::identifier), T: AST.IntTy,
106 ParameterPack: false, TInfo: AST.getTrivialTypeSourceInfo(T: AST.IntTy));
107 llvm::APInt Val(AST.getIntWidth(T: AST.IntTy), 4);
108 TemplateArgument Default(AST, llvm::APSInt(std::move(Val)), AST.IntTy,
109 /*IsDefaulted=*/true);
110 SizeParam->setDefaultArgument(C: AST, DefArg: SemaPtr->getTrivialTemplateArgumentLoc(
111 Arg: Default, NTTPType: AST.IntTy, Loc: SourceLocation()));
112 TemplateParams.emplace_back(Args&: SizeParam);
113
114 auto *ParamList =
115 TemplateParameterList::Create(C: AST, TemplateLoc: SourceLocation(), LAngleLoc: SourceLocation(),
116 Params: TemplateParams, RAngleLoc: SourceLocation(), RequiresClause: nullptr);
117
118 IdentifierInfo &II = AST.Idents.get(Name: "vector", TokenCode: tok::TokenKind::identifier);
119
120 QualType AliasType = AST.getDependentSizedExtVectorType(
121 VectorType: AST.getTemplateTypeParmType(Depth: 0, Index: 0, ParameterPack: false, ParmDecl: TypeParam),
122 SizeExpr: DeclRefExpr::Create(
123 Context: AST, QualifierLoc: NestedNameSpecifierLoc(), TemplateKWLoc: SourceLocation(), D: SizeParam, RefersToEnclosingVariableOrCapture: false,
124 NameInfo: DeclarationNameInfo(SizeParam->getDeclName(), SourceLocation()),
125 T: AST.IntTy, VK: VK_LValue),
126 AttrLoc: SourceLocation());
127
128 auto *Record = TypeAliasDecl::Create(C&: AST, DC: HLSLNamespace, StartLoc: SourceLocation(),
129 IdLoc: SourceLocation(), Id: &II,
130 TInfo: AST.getTrivialTypeSourceInfo(T: AliasType));
131 Record->setImplicit(true);
132
133 auto *Template =
134 TypeAliasTemplateDecl::Create(C&: AST, DC: HLSLNamespace, L: SourceLocation(),
135 Name: Record->getIdentifier(), Params: ParamList, Decl: Record);
136
137 Record->setDescribedAliasTemplate(Template);
138 Template->setImplicit(true);
139 Template->setLexicalDeclContext(Record->getDeclContext());
140 HLSLNamespace->addDecl(D: Template);
141}
142
143void HLSLExternalSemaSource::defineHLSLMatrixAlias() {
144 ASTContext &AST = SemaPtr->getASTContext();
145 llvm::SmallVector<NamedDecl *> TemplateParams;
146
147 auto *TypeParam = TemplateTypeParmDecl::Create(
148 C: AST, DC: HLSLNamespace, KeyLoc: SourceLocation(), NameLoc: SourceLocation(), D: 0, P: 0,
149 Id: &AST.Idents.get(Name: "element", TokenCode: tok::TokenKind::identifier), Typename: false, ParameterPack: false);
150 TypeParam->setDefaultArgument(
151 C: AST, DefArg: SemaPtr->getTrivialTemplateArgumentLoc(
152 Arg: TemplateArgument(AST.FloatTy), NTTPType: QualType(), Loc: SourceLocation()));
153
154 TemplateParams.emplace_back(Args&: TypeParam);
155
156 // these should be 64 bit to be consistent with other clang matrices.
157 auto *RowsParam = NonTypeTemplateParmDecl::Create(
158 C: AST, DC: HLSLNamespace, StartLoc: SourceLocation(), IdLoc: SourceLocation(), D: 0, P: 1,
159 Id: &AST.Idents.get(Name: "rows_count", TokenCode: tok::TokenKind::identifier), T: AST.IntTy,
160 ParameterPack: false, TInfo: AST.getTrivialTypeSourceInfo(T: AST.IntTy));
161 llvm::APInt RVal(AST.getIntWidth(T: AST.IntTy), 4);
162 TemplateArgument RDefault(AST, llvm::APSInt(std::move(RVal)), AST.IntTy,
163 /*IsDefaulted=*/true);
164 RowsParam->setDefaultArgument(
165 C: AST, DefArg: SemaPtr->getTrivialTemplateArgumentLoc(Arg: RDefault, NTTPType: AST.IntTy,
166 Loc: SourceLocation()));
167 TemplateParams.emplace_back(Args&: RowsParam);
168
169 auto *ColsParam = NonTypeTemplateParmDecl::Create(
170 C: AST, DC: HLSLNamespace, StartLoc: SourceLocation(), IdLoc: SourceLocation(), D: 0, P: 2,
171 Id: &AST.Idents.get(Name: "cols_count", TokenCode: tok::TokenKind::identifier), T: AST.IntTy,
172 ParameterPack: false, TInfo: AST.getTrivialTypeSourceInfo(T: AST.IntTy));
173 llvm::APInt CVal(AST.getIntWidth(T: AST.IntTy), 4);
174 TemplateArgument CDefault(AST, llvm::APSInt(std::move(CVal)), AST.IntTy,
175 /*IsDefaulted=*/true);
176 ColsParam->setDefaultArgument(
177 C: AST, DefArg: SemaPtr->getTrivialTemplateArgumentLoc(Arg: CDefault, NTTPType: AST.IntTy,
178 Loc: SourceLocation()));
179 TemplateParams.emplace_back(Args&: ColsParam);
180
181 const unsigned MaxMatDim = SemaPtr->getLangOpts().MaxMatrixDimension;
182
183 auto *MaxRow = IntegerLiteral::Create(
184 C: AST, V: llvm::APInt(AST.getIntWidth(T: AST.IntTy), MaxMatDim), type: AST.IntTy,
185 l: SourceLocation());
186 auto *MaxCol = IntegerLiteral::Create(
187 C: AST, V: llvm::APInt(AST.getIntWidth(T: AST.IntTy), MaxMatDim), type: AST.IntTy,
188 l: SourceLocation());
189
190 auto *RowsRef = DeclRefExpr::Create(
191 Context: AST, QualifierLoc: NestedNameSpecifierLoc(), TemplateKWLoc: SourceLocation(), D: RowsParam,
192 /*RefersToEnclosingVariableOrCapture*/ false,
193 NameInfo: DeclarationNameInfo(RowsParam->getDeclName(), SourceLocation()),
194 T: AST.IntTy, VK: VK_LValue);
195 auto *ColsRef = DeclRefExpr::Create(
196 Context: AST, QualifierLoc: NestedNameSpecifierLoc(), TemplateKWLoc: SourceLocation(), D: ColsParam,
197 /*RefersToEnclosingVariableOrCapture*/ false,
198 NameInfo: DeclarationNameInfo(ColsParam->getDeclName(), SourceLocation()),
199 T: AST.IntTy, VK: VK_LValue);
200
201 auto *RowsLE = BinaryOperator::Create(C: AST, lhs: RowsRef, rhs: MaxRow, opc: BO_LE, ResTy: AST.BoolTy,
202 VK: VK_PRValue, OK: OK_Ordinary,
203 opLoc: SourceLocation(), FPFeatures: FPOptionsOverride());
204 auto *ColsLE = BinaryOperator::Create(C: AST, lhs: ColsRef, rhs: MaxCol, opc: BO_LE, ResTy: AST.BoolTy,
205 VK: VK_PRValue, OK: OK_Ordinary,
206 opLoc: SourceLocation(), FPFeatures: FPOptionsOverride());
207
208 auto *RequiresExpr = BinaryOperator::Create(
209 C: AST, lhs: RowsLE, rhs: ColsLE, opc: BO_LAnd, ResTy: AST.BoolTy, VK: VK_PRValue, OK: OK_Ordinary,
210 opLoc: SourceLocation(), FPFeatures: FPOptionsOverride());
211
212 auto *ParamList = TemplateParameterList::Create(
213 C: AST, TemplateLoc: SourceLocation(), LAngleLoc: SourceLocation(), Params: TemplateParams, RAngleLoc: SourceLocation(),
214 RequiresClause: RequiresExpr);
215
216 IdentifierInfo &II = AST.Idents.get(Name: "matrix", TokenCode: tok::TokenKind::identifier);
217
218 QualType AliasType = AST.getDependentSizedMatrixType(
219 ElementType: AST.getTemplateTypeParmType(Depth: 0, Index: 0, ParameterPack: false, ParmDecl: TypeParam),
220 RowExpr: DeclRefExpr::Create(
221 Context: AST, QualifierLoc: NestedNameSpecifierLoc(), TemplateKWLoc: SourceLocation(), D: RowsParam, RefersToEnclosingVariableOrCapture: false,
222 NameInfo: DeclarationNameInfo(RowsParam->getDeclName(), SourceLocation()),
223 T: AST.IntTy, VK: VK_LValue),
224 ColumnExpr: DeclRefExpr::Create(
225 Context: AST, QualifierLoc: NestedNameSpecifierLoc(), TemplateKWLoc: SourceLocation(), D: ColsParam, RefersToEnclosingVariableOrCapture: false,
226 NameInfo: DeclarationNameInfo(ColsParam->getDeclName(), SourceLocation()),
227 T: AST.IntTy, VK: VK_LValue),
228 AttrLoc: SourceLocation());
229
230 auto *Record = TypeAliasDecl::Create(C&: AST, DC: HLSLNamespace, StartLoc: SourceLocation(),
231 IdLoc: SourceLocation(), Id: &II,
232 TInfo: AST.getTrivialTypeSourceInfo(T: AliasType));
233 Record->setImplicit(true);
234
235 auto *Template =
236 TypeAliasTemplateDecl::Create(C&: AST, DC: HLSLNamespace, L: SourceLocation(),
237 Name: Record->getIdentifier(), Params: ParamList, Decl: Record);
238
239 Record->setDescribedAliasTemplate(Template);
240 Template->setImplicit(true);
241 Template->setLexicalDeclContext(Record->getDeclContext());
242 HLSLNamespace->addDecl(D: Template);
243}
244
245void HLSLExternalSemaSource::defineTrivialHLSLTypes() {
246 defineHLSLVectorAlias();
247 defineHLSLMatrixAlias();
248}
249
250void HLSLExternalSemaSource::defineHeapResourceInfoTypes() {
251 ASTContext &AST = SemaPtr->getASTContext();
252 CXXRecordDecl *ResDecl = BuiltinTypeDeclBuilder(*SemaPtr, HLSLDetailNamespace,
253 "heap_resource_info")
254 .finalizeForwardDeclaration();
255 if (!ResDecl->isCompleteDefinition())
256 BuiltinTypeDeclBuilder(*SemaPtr, ResDecl)
257 .addMemberVariable(Name: "Index", Type: AST.UnsignedIntTy, Attrs: {})
258 .completeDefinition();
259
260 CXXRecordDecl *SampDecl =
261 BuiltinTypeDeclBuilder(*SemaPtr, HLSLDetailNamespace, "heap_sampler_info")
262 .finalizeForwardDeclaration();
263 if (!SampDecl->isCompleteDefinition())
264 BuiltinTypeDeclBuilder(*SemaPtr, SampDecl)
265 .addMemberVariable(Name: "Index", Type: AST.UnsignedIntTy, Attrs: {})
266 .completeDefinition();
267}
268
269void HLSLExternalSemaSource::defineInternalHLSLTypes() {
270 defineHeapResourceInfoTypes();
271}
272
273/// Set up common members and attributes for buffer types
274static BuiltinTypeDeclBuilder setupBufferType(CXXRecordDecl *Decl, Sema &S,
275 ResourceClass RC, bool IsROV,
276 bool RawBuffer, bool HasCounter) {
277 return BuiltinTypeDeclBuilder(S, Decl)
278 .addBufferHandles(RC, IsROV, RawBuffer, HasCounter)
279 .addDefaultHandleConstructor()
280 .addCopyConstructor()
281 .addCopyAssignmentOperator()
282 .addHeapResourceInfoConstructor(HasCounter)
283 .addStaticInitializationFunctions(HasCounter);
284}
285
286/// Set up common members and attributes for sampler types
287static BuiltinTypeDeclBuilder setupSamplerType(CXXRecordDecl *Decl, Sema &S) {
288 return BuiltinTypeDeclBuilder(S, Decl)
289 .addSamplerHandle()
290 .addDefaultHandleConstructor()
291 .addCopyConstructor()
292 .addCopyAssignmentOperator()
293 .addHeapSamplerInfoConstructor()
294 .addStaticInitializationFunctions(HasCounter: false);
295}
296
297namespace {
298LLVM_ENABLE_BITMASK_ENUMS_IN_NAMESPACE();
299
300/// Which members a texture type has. Overloads within a member family
301/// (e.g., offset overloads for samplers) follow from ResourceDimension.
302enum class TexCap : uint32_t {
303 Load = 1u << 0, // Load(int<N+1>) taking a mip level
304 LoadMS = 1u << 1, // Load(int<N>, int sampleIndex) on a multisampled type
305 LoadRW = 1u << 2, // Load(int<N>) on a writable texture
306 Subscript = 1u << 3, // operator[]
307 Mips = 1u << 4, // mips[]
308 Sample = 1u << 5, // Sample, SampleBias, SampleGrad, SampleLevel
309 SampleCmp = 1u << 6, // SampleCmp, SampleCmpLevelZero
310 Gather = 1u << 7, // Gather*, GatherCmp*
311 CalcLOD = 1u << 8, // CalculateLevelOfDetail, ...Unclamped
312 GetDims = 1u << 9, // GetDimensions
313
314 // TODO: multisampled types need an MS-specific GetDimensions
315 // https://github.com/llvm/wg-hlsl/issues/347
316
317 LLVM_MARK_AS_BITMASK_ENUM(/*LargestValue=*/GetDims)
318};
319
320/// How a type's template parameters are spelled. Independent of its
321/// capabilities; also decides which types get a vector partial specialization.
322enum class TemplateShape {
323 ElementType, // template<typename T = float4>
324 ElementTypeAndSampleCount, // template<typename T, uint N>
325};
326
327struct TextureTypeInfo {
328 const char *Name;
329 ResourceClass RC;
330 ResourceDimension Dim;
331 bool IsArray;
332 bool IsROV;
333 TemplateShape Shape;
334 TexCap Caps;
335
336 bool has(TexCap C) const { return (Caps & C) != TexCap{}; }
337 bool hasSampleCount() const {
338 return Shape == TemplateShape::ElementTypeAndSampleCount;
339 }
340};
341} // namespace
342
343static const TextureTypeInfo TextureTypes[] = {
344 {.Name: "Texture1D", .RC: ResourceClass::SRV, .Dim: ResourceDimension::Dim1D,
345 /*IsArray=*/false, /*IsROV=*/false, .Shape: TemplateShape::ElementType,
346 .Caps: TexCap::Load | TexCap::Subscript | TexCap::Mips | TexCap::Sample |
347 TexCap::SampleCmp | TexCap::CalcLOD},
348 {.Name: "RWTexture1D", .RC: ResourceClass::UAV, .Dim: ResourceDimension::Dim1D,
349 /*IsArray=*/false, /*IsROV=*/false, .Shape: TemplateShape::ElementType,
350 .Caps: TexCap::LoadRW | TexCap::Subscript},
351 {.Name: "Texture1DArray", .RC: ResourceClass::SRV, .Dim: ResourceDimension::Dim1D,
352 /*IsArray=*/true, /*IsROV=*/false, .Shape: TemplateShape::ElementType,
353 .Caps: TexCap::Load | TexCap::Subscript | TexCap::Mips | TexCap::Sample |
354 TexCap::SampleCmp | TexCap::CalcLOD},
355 {.Name: "RWTexture1DArray", .RC: ResourceClass::UAV, .Dim: ResourceDimension::Dim1D,
356 /*IsArray=*/true, /*IsROV=*/false, .Shape: TemplateShape::ElementType,
357 .Caps: TexCap::LoadRW | TexCap::Subscript},
358 {.Name: "Texture2D", .RC: ResourceClass::SRV, .Dim: ResourceDimension::Dim2D,
359 /*IsArray=*/false, /*IsROV=*/false, .Shape: TemplateShape::ElementType,
360 .Caps: TexCap::Load | TexCap::Subscript | TexCap::Mips | TexCap::Sample |
361 TexCap::SampleCmp | TexCap::CalcLOD | TexCap::Gather |
362 TexCap::GetDims},
363 {.Name: "RWTexture2D", .RC: ResourceClass::UAV, .Dim: ResourceDimension::Dim2D,
364 /*IsArray=*/false, /*IsROV=*/false, .Shape: TemplateShape::ElementType,
365 .Caps: TexCap::LoadRW | TexCap::Subscript | TexCap::GetDims},
366 {.Name: "Texture2DArray", .RC: ResourceClass::SRV, .Dim: ResourceDimension::Dim2D,
367 /*IsArray=*/true, /*IsROV=*/false, .Shape: TemplateShape::ElementType,
368 .Caps: TexCap::Load | TexCap::Subscript | TexCap::Mips | TexCap::Sample |
369 TexCap::SampleCmp | TexCap::CalcLOD | TexCap::Gather |
370 TexCap::GetDims},
371 {.Name: "RWTexture2DArray", .RC: ResourceClass::UAV, .Dim: ResourceDimension::Dim2D,
372 /*IsArray=*/true, /*IsROV=*/false, .Shape: TemplateShape::ElementType,
373 .Caps: TexCap::LoadRW | TexCap::Subscript | TexCap::GetDims},
374 {.Name: "Texture2DMS", .RC: ResourceClass::SRV, .Dim: ResourceDimension::Dim2D,
375 /*IsArray=*/false, /*IsROV=*/false,
376 .Shape: TemplateShape::ElementTypeAndSampleCount,
377 .Caps: TexCap::LoadMS | TexCap::Subscript},
378 {.Name: "Texture3D", .RC: ResourceClass::SRV, .Dim: ResourceDimension::Dim3D,
379 /*IsArray=*/false, /*IsROV=*/false, .Shape: TemplateShape::ElementType,
380 .Caps: TexCap::Load | TexCap::Subscript | TexCap::Mips | TexCap::Sample |
381 TexCap::CalcLOD | TexCap::GetDims},
382 {.Name: "RWTexture3D", .RC: ResourceClass::UAV, .Dim: ResourceDimension::Dim3D,
383 /*IsArray=*/false, /*IsROV=*/false, .Shape: TemplateShape::ElementType,
384 .Caps: TexCap::LoadRW | TexCap::Subscript | TexCap::GetDims},
385 {.Name: "TextureCube", .RC: ResourceClass::SRV, .Dim: ResourceDimension::Cube,
386 /*IsArray=*/false, /*IsROV=*/false, .Shape: TemplateShape::ElementType,
387 .Caps: TexCap::Sample | TexCap::SampleCmp | TexCap::CalcLOD | TexCap::Gather |
388 TexCap::GetDims},
389 {.Name: "TextureCubeArray", .RC: ResourceClass::SRV, .Dim: ResourceDimension::Cube,
390 /*IsArray=*/true, /*IsROV=*/false, .Shape: TemplateShape::ElementType,
391 .Caps: TexCap::Sample | TexCap::SampleCmp | TexCap::CalcLOD | TexCap::Gather |
392 TexCap::GetDims},
393};
394
395static BuiltinTypeDeclBuilder setupTextureType(CXXRecordDecl *Decl, Sema &S,
396 const TextureTypeInfo &T) {
397 const ResourceDimension Dim = T.Dim;
398 const bool IsArray = T.IsArray;
399
400 Expr *SampleCountExpr = nullptr;
401 if (T.hasSampleCount()) {
402 ClassTemplateDecl *CTD = Decl->getDescribedClassTemplate();
403 assert(CTD && "multisampled texture must be a class template");
404 // Parameter 1 is the N in Texture2DMS<T, N>.
405 auto *NTTP = cast<NonTypeTemplateParmDecl>(
406 Val: CTD->getTemplateParameters()->getParam(Idx: 1));
407 SampleCountExpr =
408 S.BuildDeclRefExpr(D: NTTP, Ty: NTTP->getType(), VK: VK_PRValue, Loc: SourceLocation());
409 }
410
411 BuiltinTypeDeclBuilder B(S, Decl);
412 B.addTextureHandle(RC: T.RC, IsROV: T.IsROV, IsArray, RD: Dim, SampleCountExpr);
413
414 // The `mips` member holds a second copy of the resource handle.
415 // addCopyConstructor, addCopyAssignmentOperator and
416 // addStaticInitializationFunctions are what initialize that copy, and they
417 // look the member up by name, so it has to exist before they run.
418 if (T.has(C: TexCap::Mips))
419 B.addMipsMember(Dim);
420
421 B.addDefaultHandleConstructor()
422 .addCopyConstructor()
423 .addCopyAssignmentOperator()
424 .addHeapResourceInfoConstructor()
425 .addStaticInitializationFunctions(HasCounter: false);
426
427 if (T.has(C: TexCap::Load))
428 B.addTextureLoadMethods(Dim, IsArray);
429 if (T.has(C: TexCap::LoadMS))
430 B.addTextureLoadMSMethods(Dim, IsArray);
431 if (T.has(C: TexCap::LoadRW))
432 B.addRWTextureLoadMethods(Dim, IsArray);
433 if (T.has(C: TexCap::Subscript))
434 B.addArraySubscriptOperators(Dim, IsArray);
435
436 if (T.has(C: TexCap::Sample))
437 B.addSampleMethods(Dim, IsArray)
438 .addSampleBiasMethods(Dim, IsArray)
439 .addSampleGradMethods(Dim, IsArray)
440 .addSampleLevelMethods(Dim, IsArray);
441 if (T.has(C: TexCap::SampleCmp))
442 B.addSampleCmpMethods(Dim, IsArray)
443 .addSampleCmpLevelZeroMethods(Dim, IsArray);
444 if (T.has(C: TexCap::CalcLOD))
445 B.addCalculateLodMethods(Dim);
446 if (T.has(C: TexCap::GetDims))
447 B.addGetDimensionsMethods(Dim);
448 if (T.has(C: TexCap::Gather))
449 B.addGatherMethods(Dim, IsArray).addGatherCmpMethods(Dim, IsArray);
450
451 return B;
452}
453
454// Add a partial specialization for a template. The `TextureTemplate` is
455// `Texture<element_type>`, and it will be specialized for vectors:
456// `Texture<vector<element_type, element_count>>`.
457static ClassTemplatePartialSpecializationDecl *
458addVectorTexturePartialSpecialization(Sema &S, NamespaceDecl *HLSLNamespace,
459 ClassTemplateDecl *TextureTemplate) {
460 ASTContext &AST = S.getASTContext();
461
462 // Create the template parameters: element_type and element_count.
463 auto *ElementType = TemplateTypeParmDecl::Create(
464 C: AST, DC: HLSLNamespace, KeyLoc: SourceLocation(), NameLoc: SourceLocation(), D: 0, P: 0,
465 Id: &AST.Idents.get(Name: "element_type"), Typename: false, ParameterPack: false);
466 auto *ElementCount = NonTypeTemplateParmDecl::Create(
467 C: AST, DC: HLSLNamespace, StartLoc: SourceLocation(), IdLoc: SourceLocation(), D: 0, P: 1,
468 Id: &AST.Idents.get(Name: "element_count"), T: AST.IntTy, ParameterPack: false,
469 TInfo: AST.getTrivialTypeSourceInfo(T: AST.IntTy));
470
471 auto *TemplateParams = TemplateParameterList::Create(
472 C: AST, TemplateLoc: SourceLocation(), LAngleLoc: SourceLocation(), Params: {ElementType, ElementCount},
473 RAngleLoc: SourceLocation(), RequiresClause: nullptr);
474
475 // Create the dependent vector type: vector<element_type, element_count>.
476 QualType VectorType = AST.getDependentSizedExtVectorType(
477 VectorType: AST.getTemplateTypeParmType(Depth: 0, Index: 0, ParameterPack: false, ParmDecl: ElementType),
478 SizeExpr: DeclRefExpr::Create(
479 Context: AST, QualifierLoc: NestedNameSpecifierLoc(), TemplateKWLoc: SourceLocation(), D: ElementCount, RefersToEnclosingVariableOrCapture: false,
480 NameInfo: DeclarationNameInfo(ElementCount->getDeclName(), SourceLocation()),
481 T: AST.IntTy, VK: VK_LValue),
482 AttrLoc: SourceLocation());
483
484 // Create the partial specialization declaration.
485 QualType CanonInjectedTST =
486 AST.getCanonicalType(T: AST.getTemplateSpecializationType(
487 Keyword: ElaboratedTypeKeyword::Class, T: TemplateName(TextureTemplate),
488 SpecifiedArgs: {TemplateArgument(VectorType)}, CanonicalArgs: {}));
489
490 auto *PartialSpec = ClassTemplatePartialSpecializationDecl::Create(
491 Context&: AST, TK: TagDecl::TagKind::Class, DC: HLSLNamespace, StartLoc: SourceLocation(),
492 IdLoc: SourceLocation(), Params: TemplateParams, SpecializedTemplate: TextureTemplate,
493 Args: {TemplateArgument(VectorType)},
494 CanonInjectedTST: CanQualType::CreateUnsafe(Other: CanonInjectedTST), PrevDecl: nullptr);
495
496 // Set the template arguments as written.
497 TemplateArgument Arg(VectorType);
498 TemplateArgumentLoc ArgLoc =
499 S.getTrivialTemplateArgumentLoc(Arg, NTTPType: QualType(), Loc: SourceLocation());
500 TemplateArgumentListInfo ArgsInfo =
501 TemplateArgumentListInfo(SourceLocation(), SourceLocation());
502 ArgsInfo.addArgument(Loc: ArgLoc);
503 PartialSpec->setTemplateArgsAsWritten(
504 ASTTemplateArgumentListInfo::Create(C: AST, List: ArgsInfo));
505
506 PartialSpec->setImplicit(true);
507 PartialSpec->setLexicalDeclContext(HLSLNamespace);
508 PartialSpec->setHasExternalLexicalStorage();
509
510 // Add the partial specialization to the namespace and the class template.
511 HLSLNamespace->addDecl(D: PartialSpec);
512 TextureTemplate->AddPartialSpecialization(D: PartialSpec, InsertToken: {});
513
514 return PartialSpec;
515}
516
517// This function is responsible for constructing the constraint expression for
518// this concept:
519// template<typename T> concept is_typed_resource_element_compatible =
520// __is_typed_resource_element_compatible<T>;
521static Expr *constructTypedBufferConstraintExpr(Sema &S, SourceLocation NameLoc,
522 TemplateTypeParmDecl *T) {
523 ASTContext &Context = S.getASTContext();
524
525 // Obtain the QualType for 'bool'
526 QualType BoolTy = Context.BoolTy;
527
528 // Create a QualType that points to this TemplateTypeParmDecl
529 QualType TType = Context.getTypeDeclType(Decl: T);
530
531 // Create a TypeSourceInfo for the template type parameter 'T'
532 TypeSourceInfo *TTypeSourceInfo =
533 Context.getTrivialTypeSourceInfo(T: TType, Loc: NameLoc);
534
535 TypeTraitExpr *TypedResExpr = TypeTraitExpr::Create(
536 C: Context, T: BoolTy, Loc: NameLoc, Kind: UTT_IsTypedResourceElementCompatible,
537 Args: {TTypeSourceInfo}, RParenLoc: NameLoc, Value: true);
538
539 return TypedResExpr;
540}
541
542// This function is responsible for constructing the constraint expression for
543// this concept:
544// template<typename T> concept is_constant_buffer_element_compatible =
545// std::is_class_v<T> && !__is_intangible(T);
546static Expr *constructConstantBufferConstraintExpr(Sema &S,
547 SourceLocation NameLoc,
548 TemplateTypeParmDecl *T) {
549 ASTContext &Context = S.getASTContext();
550
551 // Obtain the QualType for 'bool'
552 QualType BoolTy = Context.BoolTy;
553
554 // Create a QualType that points to this TemplateTypeParmDecl
555 QualType TType = Context.getTypeDeclType(Decl: T);
556
557 // Create a TypeSourceInfo for the template type parameter 'T'
558 TypeSourceInfo *TTypeSourceInfo =
559 Context.getTrivialTypeSourceInfo(T: TType, Loc: NameLoc);
560
561 TypeTraitExpr *ResExpr = TypeTraitExpr::Create(
562 C: Context, T: BoolTy, Loc: NameLoc, Kind: UTT_IsConstantBufferElementCompatible,
563 Args: {TTypeSourceInfo}, RParenLoc: NameLoc, Value: true);
564
565 return ResExpr;
566}
567
568// This function is responsible for constructing the constraint expression for
569// this concept:
570// template<typename T> concept is_structured_resource_element_compatible =
571// !__is_intangible<T> && sizeof(T) >= 1;
572static Expr *constructStructuredBufferConstraintExpr(Sema &S,
573 SourceLocation NameLoc,
574 TemplateTypeParmDecl *T) {
575 ASTContext &Context = S.getASTContext();
576
577 // Obtain the QualType for 'bool'
578 QualType BoolTy = Context.BoolTy;
579
580 // Create a QualType that points to this TemplateTypeParmDecl
581 QualType TType = Context.getTypeDeclType(Decl: T);
582
583 // Create a TypeSourceInfo for the template type parameter 'T'
584 TypeSourceInfo *TTypeSourceInfo =
585 Context.getTrivialTypeSourceInfo(T: TType, Loc: NameLoc);
586
587 TypeTraitExpr *IsIntangibleExpr =
588 TypeTraitExpr::Create(C: Context, T: BoolTy, Loc: NameLoc, Kind: UTT_IsIntangibleType,
589 Args: {TTypeSourceInfo}, RParenLoc: NameLoc, Value: true);
590
591 // negate IsIntangibleExpr
592 UnaryOperator *NotIntangibleExpr = UnaryOperator::Create(
593 C: Context, input: IsIntangibleExpr, opc: UO_LNot, type: BoolTy, VK: VK_LValue, OK: OK_Ordinary,
594 l: NameLoc, CanOverflow: false, FPFeatures: FPOptionsOverride());
595
596 // element types also may not be of 0 size
597 UnaryExprOrTypeTraitExpr *SizeOfExpr = new (Context) UnaryExprOrTypeTraitExpr(
598 UETT_SizeOf, TTypeSourceInfo, BoolTy, NameLoc, NameLoc);
599
600 // Create a BinaryOperator that checks if the size of the type is not equal to
601 // 1 Empty structs have a size of 1 in HLSL, so we need to check for that
602 IntegerLiteral *rhs = IntegerLiteral::Create(
603 C: Context, V: llvm::APInt(Context.getTypeSize(T: Context.getSizeType()), 1, true),
604 type: Context.getSizeType(), l: NameLoc);
605
606 BinaryOperator *SizeGEQOneExpr =
607 BinaryOperator::Create(C: Context, lhs: SizeOfExpr, rhs, opc: BO_GE, ResTy: BoolTy, VK: VK_LValue,
608 OK: OK_Ordinary, opLoc: NameLoc, FPFeatures: FPOptionsOverride());
609
610 // Combine the two constraints
611 BinaryOperator *CombinedExpr = BinaryOperator::Create(
612 C: Context, lhs: NotIntangibleExpr, rhs: SizeGEQOneExpr, opc: BO_LAnd, ResTy: BoolTy, VK: VK_LValue,
613 OK: OK_Ordinary, opLoc: NameLoc, FPFeatures: FPOptionsOverride());
614
615 return CombinedExpr;
616}
617
618enum class HLSLBufferType { Typed, Structured, Constant };
619
620static ConceptDecl *constructBufferConceptDecl(Sema &S, NamespaceDecl *NSD,
621 HLSLBufferType BT) {
622 ASTContext &Context = S.getASTContext();
623 DeclContext *DC = NSD->getDeclContext();
624 SourceLocation DeclLoc = SourceLocation();
625
626 IdentifierInfo &ElementTypeII = Context.Idents.get(Name: "element_type");
627 TemplateTypeParmDecl *T = TemplateTypeParmDecl::Create(
628 C: Context, DC: NSD->getDeclContext(), KeyLoc: DeclLoc, NameLoc: DeclLoc,
629 /*D=*/0,
630 /*P=*/0,
631 /*Id=*/&ElementTypeII,
632 /*Typename=*/true,
633 /*ParameterPack=*/false);
634
635 T->setDeclContext(DC);
636 T->setReferenced();
637
638 // Create and Attach Template Parameter List to ConceptDecl
639 TemplateParameterList *ConceptParams = TemplateParameterList::Create(
640 C: Context, TemplateLoc: DeclLoc, LAngleLoc: DeclLoc, Params: {T}, RAngleLoc: DeclLoc, RequiresClause: nullptr);
641
642 DeclarationName DeclName;
643 Expr *ConstraintExpr = nullptr;
644
645 switch (BT) {
646 case HLSLBufferType::Typed:
647 DeclName = DeclarationName(
648 &Context.Idents.get(Name: "__is_typed_resource_element_compatible"));
649 ConstraintExpr = constructTypedBufferConstraintExpr(S, NameLoc: DeclLoc, T);
650 break;
651 case HLSLBufferType::Structured:
652 DeclName = DeclarationName(
653 &Context.Idents.get(Name: "__is_structured_resource_element_compatible"));
654 ConstraintExpr = constructStructuredBufferConstraintExpr(S, NameLoc: DeclLoc, T);
655 break;
656 case HLSLBufferType::Constant:
657 DeclName = DeclarationName(
658 &Context.Idents.get(Name: "__is_constant_buffer_element_compatible"));
659 ConstraintExpr = constructConstantBufferConstraintExpr(S, NameLoc: DeclLoc, T);
660 break;
661 }
662
663 // Create a ConceptDecl
664 ConceptDecl *CD =
665 ConceptDecl::Create(C&: Context, DC: NSD->getDeclContext(), L: DeclLoc, Name: DeclName,
666 Params: ConceptParams, ConstraintExpr);
667
668 // Attach the template parameter list to the ConceptDecl
669 CD->setTemplateParameters(ConceptParams);
670
671 // Add the concept declaration to the Translation Unit Decl
672 NSD->getDeclContext()->addDecl(D: CD);
673
674 return CD;
675}
676
677void HLSLExternalSemaSource::defineHLSLTypesWithForwardDeclarations() {
678 ASTContext &AST = SemaPtr->getASTContext();
679 CXXRecordDecl *Decl;
680 ConceptDecl *TypedBufferConcept = constructBufferConceptDecl(
681 S&: *SemaPtr, NSD: HLSLNamespace, BT: HLSLBufferType::Typed);
682 ConceptDecl *StructuredBufferConcept = constructBufferConceptDecl(
683 S&: *SemaPtr, NSD: HLSLNamespace, BT: HLSLBufferType::Structured);
684 ConceptDecl *ConstantBufferConcept = constructBufferConceptDecl(
685 S&: *SemaPtr, NSD: HLSLNamespace, BT: HLSLBufferType::Constant);
686
687 Decl = BuiltinTypeDeclBuilder(*SemaPtr, HLSLNamespace, "ConstantBuffer")
688 .addSimpleTemplateParams(Names: {"element_type"}, CD: ConstantBufferConcept)
689 .finalizeForwardDeclaration();
690
691 onCompletion(Record: Decl, Fn: [this](CXXRecordDecl *Decl) {
692 setupBufferType(Decl, S&: *SemaPtr, RC: ResourceClass::CBuffer, /*IsROV=*/false,
693 /*RawBuffer=*/false, /*HasCounter=*/false)
694 .addConstantBufferConversionToType()
695 .completeDefinition();
696 });
697
698 Decl = BuiltinTypeDeclBuilder(*SemaPtr, HLSLNamespace, "Buffer")
699 .addSimpleTemplateParams(Names: {"element_type"}, CD: TypedBufferConcept)
700 .finalizeForwardDeclaration();
701
702 onCompletion(Record: Decl, Fn: [this](CXXRecordDecl *Decl) {
703 setupBufferType(Decl, S&: *SemaPtr, RC: ResourceClass::SRV, /*IsROV=*/false,
704 /*RawBuffer=*/false, /*HasCounter=*/false)
705 .addArraySubscriptOperators()
706 .addLoadMethods()
707 .addGetDimensionsMethodForBuffer()
708 .completeDefinition();
709 });
710
711 Decl = BuiltinTypeDeclBuilder(*SemaPtr, HLSLNamespace, "RWBuffer")
712 .addSimpleTemplateParams(Names: {"element_type"}, CD: TypedBufferConcept)
713 .finalizeForwardDeclaration();
714
715 onCompletion(Record: Decl, Fn: [this](CXXRecordDecl *Decl) {
716 setupBufferType(Decl, S&: *SemaPtr, RC: ResourceClass::UAV, /*IsROV=*/false,
717 /*RawBuffer=*/false, /*HasCounter=*/false)
718 .addArraySubscriptOperators()
719 .addLoadMethods()
720 .addGetDimensionsMethodForBuffer()
721 .completeDefinition();
722 });
723
724 Decl =
725 BuiltinTypeDeclBuilder(*SemaPtr, HLSLNamespace, "RasterizerOrderedBuffer")
726 .addSimpleTemplateParams(Names: {"element_type"}, CD: StructuredBufferConcept)
727 .finalizeForwardDeclaration();
728 onCompletion(Record: Decl, Fn: [this](CXXRecordDecl *Decl) {
729 setupBufferType(Decl, S&: *SemaPtr, RC: ResourceClass::UAV, /*IsROV=*/true,
730 /*RawBuffer=*/false, /*HasCounter=*/false)
731 .addArraySubscriptOperators()
732 .addLoadMethods()
733 .addGetDimensionsMethodForBuffer()
734 .completeDefinition();
735 });
736
737 Decl = BuiltinTypeDeclBuilder(*SemaPtr, HLSLNamespace, "StructuredBuffer")
738 .addSimpleTemplateParams(Names: {"element_type"}, CD: StructuredBufferConcept)
739 .finalizeForwardDeclaration();
740 onCompletion(Record: Decl, Fn: [this](CXXRecordDecl *Decl) {
741 setupBufferType(Decl, S&: *SemaPtr, RC: ResourceClass::SRV, /*IsROV=*/false,
742 /*RawBuffer=*/true, /*HasCounter=*/false)
743 .addArraySubscriptOperators()
744 .addLoadMethods()
745 .addGetDimensionsMethodForBuffer()
746 .completeDefinition();
747 });
748
749 Decl = BuiltinTypeDeclBuilder(*SemaPtr, HLSLNamespace, "RWStructuredBuffer")
750 .addSimpleTemplateParams(Names: {"element_type"}, CD: StructuredBufferConcept)
751 .finalizeForwardDeclaration();
752 onCompletion(Record: Decl, Fn: [this](CXXRecordDecl *Decl) {
753 setupBufferType(Decl, S&: *SemaPtr, RC: ResourceClass::UAV, /*IsROV=*/false,
754 /*RawBuffer=*/true, /*HasCounter=*/true)
755 .addArraySubscriptOperators()
756 .addLoadMethods()
757 .addIncrementCounterMethod()
758 .addDecrementCounterMethod()
759 .addGetDimensionsMethodForBuffer()
760 .completeDefinition();
761 });
762
763 Decl =
764 BuiltinTypeDeclBuilder(*SemaPtr, HLSLNamespace, "AppendStructuredBuffer")
765 .addSimpleTemplateParams(Names: {"element_type"}, CD: StructuredBufferConcept)
766 .finalizeForwardDeclaration();
767 onCompletion(Record: Decl, Fn: [this](CXXRecordDecl *Decl) {
768 setupBufferType(Decl, S&: *SemaPtr, RC: ResourceClass::UAV, /*IsROV=*/false,
769 /*RawBuffer=*/true, /*HasCounter=*/true)
770 .addAppendMethod()
771 .addGetDimensionsMethodForBuffer()
772 .completeDefinition();
773 });
774
775 Decl =
776 BuiltinTypeDeclBuilder(*SemaPtr, HLSLNamespace, "ConsumeStructuredBuffer")
777 .addSimpleTemplateParams(Names: {"element_type"}, CD: StructuredBufferConcept)
778 .finalizeForwardDeclaration();
779 onCompletion(Record: Decl, Fn: [this](CXXRecordDecl *Decl) {
780 setupBufferType(Decl, S&: *SemaPtr, RC: ResourceClass::UAV, /*IsROV=*/false,
781 /*RawBuffer=*/true, /*HasCounter=*/true)
782 .addConsumeMethod()
783 .addGetDimensionsMethodForBuffer()
784 .completeDefinition();
785 });
786
787 Decl = BuiltinTypeDeclBuilder(*SemaPtr, HLSLNamespace,
788 "RasterizerOrderedStructuredBuffer")
789 .addSimpleTemplateParams(Names: {"element_type"}, CD: StructuredBufferConcept)
790 .finalizeForwardDeclaration();
791 onCompletion(Record: Decl, Fn: [this](CXXRecordDecl *Decl) {
792 setupBufferType(Decl, S&: *SemaPtr, RC: ResourceClass::UAV, /*IsROV=*/true,
793 /*RawBuffer=*/true, /*HasCounter=*/true)
794 .addArraySubscriptOperators()
795 .addLoadMethods()
796 .addIncrementCounterMethod()
797 .addDecrementCounterMethod()
798 .addGetDimensionsMethodForBuffer()
799 .completeDefinition();
800 });
801
802 Decl = BuiltinTypeDeclBuilder(*SemaPtr, HLSLNamespace, "ByteAddressBuffer")
803 .finalizeForwardDeclaration();
804 onCompletion(Record: Decl, Fn: [this](CXXRecordDecl *Decl) {
805 setupBufferType(Decl, S&: *SemaPtr, RC: ResourceClass::SRV, /*IsROV=*/false,
806 /*RawBuffer=*/true, /*HasCounter=*/false)
807 .addByteAddressBufferLoadMethods()
808 .addGetDimensionsMethodForBuffer()
809 .completeDefinition();
810 });
811 Decl = BuiltinTypeDeclBuilder(*SemaPtr, HLSLNamespace, "RWByteAddressBuffer")
812 .finalizeForwardDeclaration();
813 onCompletion(Record: Decl, Fn: [this](CXXRecordDecl *Decl) {
814 setupBufferType(Decl, S&: *SemaPtr, RC: ResourceClass::UAV, /*IsROV=*/false,
815 /*RawBuffer=*/true, /*HasCounter=*/false)
816 .addByteAddressBufferLoadMethods()
817 .addByteAddressBufferStoreMethods()
818 .addByteAddressBufferInterlockedMethods()
819 .addGetDimensionsMethodForBuffer()
820 .completeDefinition();
821 });
822 Decl = BuiltinTypeDeclBuilder(*SemaPtr, HLSLNamespace,
823 "RasterizerOrderedByteAddressBuffer")
824 .finalizeForwardDeclaration();
825 onCompletion(Record: Decl, Fn: [this](CXXRecordDecl *Decl) {
826 setupBufferType(Decl, S&: *SemaPtr, RC: ResourceClass::UAV, /*IsROV=*/true,
827 /*RawBuffer=*/true, /*HasCounter=*/false)
828 .addByteAddressBufferInterlockedMethods()
829 .addGetDimensionsMethodForBuffer()
830 .completeDefinition();
831 });
832
833 Decl = BuiltinTypeDeclBuilder(*SemaPtr, HLSLNamespace, "SamplerState")
834 .finalizeForwardDeclaration();
835 onCompletion(Record: Decl, Fn: [this](CXXRecordDecl *Decl) {
836 setupSamplerType(Decl, S&: *SemaPtr).completeDefinition();
837 });
838
839 Decl =
840 BuiltinTypeDeclBuilder(*SemaPtr, HLSLNamespace, "SamplerComparisonState")
841 .finalizeForwardDeclaration();
842 onCompletion(Record: Decl, Fn: [this](CXXRecordDecl *Decl) {
843 setupSamplerType(Decl, S&: *SemaPtr).completeDefinition();
844 });
845
846 QualType Float4Ty = AST.getExtVectorType(VectorType: AST.FloatTy, NumElts: 4);
847 for (const TextureTypeInfo &T : TextureTypes) {
848 BuiltinTypeDeclBuilder TexBuilder(*SemaPtr, HLSLNamespace, T.Name);
849 switch (T.Shape) {
850 case TemplateShape::ElementType:
851 TexBuilder.addSimpleTemplateParams(Names: {"element_type"}, DefaultTypes: {Float4Ty},
852 CD: TypedBufferConcept);
853 break;
854 case TemplateShape::ElementTypeAndSampleCount:
855 TexBuilder.addMSTextureTemplateParams(ElementName: "element_type", SampleCountName: "sample_count",
856 CD: TypedBufferConcept);
857 break;
858 }
859 Decl = TexBuilder.finalizeForwardDeclaration();
860
861 onCompletion(Record: Decl, Fn: [this, &T](CXXRecordDecl *Decl) {
862 setupTextureType(Decl, S&: *SemaPtr, T).completeDefinition();
863 });
864
865 if (T.Shape != TemplateShape::ElementType)
866 continue;
867
868 CXXRecordDecl *PartialSpec = addVectorTexturePartialSpecialization(
869 S&: *SemaPtr, HLSLNamespace, TextureTemplate: Decl->getDescribedClassTemplate());
870 onCompletion(Record: PartialSpec, Fn: [this, &T](CXXRecordDecl *Decl) {
871 setupTextureType(Decl, S&: *SemaPtr, T).completeDefinition();
872 });
873 }
874}
875
876// Shapes of the synthesized atomic overloads. The read-modify-write operations
877// can report the previous value through a trailing reference. Compare-store
878// takes only inputs; compare-exchange adds the trailing reference.
879enum class AtomicOverloadShape {
880 Binary, // (dest, value)
881 BinaryWithOriginal, // (dest, value, original_value)
882 CompareStore, // (dest, compare_value, value)
883 CompareExchange, // (dest, compare_value, value, original_value)
884};
885
886// Build a single overload of an HLSL atomic intrinsic in the hlsl namespace.
887// `dest` is an address-space-qualified reference; `original_value` (when
888// present) is an HLSL `out` parameter. The synthesized FunctionDecl aliases
889// the underlying clang builtin via BuiltinAliasAttr.
890static void buildAtomicOverload(Sema &S, NamespaceDecl *NS, StringRef FuncName,
891 StringRef BuiltinName, QualType ElemTy,
892 LangAS DestAS, AtomicOverloadShape Shape) {
893 ASTContext &AST = S.getASTContext();
894
895 QualType DestTy =
896 AST.getLValueReferenceType(T: AST.getAddrSpaceQualType(T: ElemTy, AddressSpace: DestAS));
897 QualType OrigRefTy = S.HLSL().getInoutParameterType(Ty: ElemTy);
898
899 SmallVector<QualType, 4> ParamTypes = {DestTy, ElemTy};
900 switch (Shape) {
901 case AtomicOverloadShape::Binary:
902 break;
903 case AtomicOverloadShape::BinaryWithOriginal:
904 ParamTypes.push_back(Elt: OrigRefTy);
905 break;
906 case AtomicOverloadShape::CompareStore:
907 ParamTypes.push_back(Elt: ElemTy);
908 break;
909 case AtomicOverloadShape::CompareExchange:
910 ParamTypes.push_back(Elt: ElemTy);
911 ParamTypes.push_back(Elt: OrigRefTy);
912 break;
913 }
914
915 // The loop below stops at the end of ParamTypes, so a shorter overload
916 // ignores the trailing names.
917 constexpr const char *BinaryNames[] = {"dest", "value", "original_value"};
918 constexpr const char *CompareNames[] = {"dest", "compare_value", "value",
919 "original_value"};
920 const bool IsCompare = Shape == AtomicOverloadShape::CompareStore ||
921 Shape == AtomicOverloadShape::CompareExchange;
922 const bool HasOriginalValue =
923 Shape == AtomicOverloadShape::BinaryWithOriginal ||
924 Shape == AtomicOverloadShape::CompareExchange;
925 ArrayRef<const char *> ParamNames = IsCompare
926 ? ArrayRef<const char *>(CompareNames)
927 : ArrayRef<const char *>(BinaryNames);
928
929 FunctionProtoType::ExtProtoInfo EPI;
930 SmallVector<FunctionType::ExtParameterInfo, 4> ParamExtInfos(
931 ParamTypes.size());
932 if (HasOriginalValue) {
933 ParamExtInfos.back() = ParamExtInfos.back().withABI(kind: ParameterABI::HLSLOut);
934 EPI.ExtParameterInfos = ParamExtInfos.data();
935 }
936 QualType FuncTy = AST.getFunctionType(ResultTy: AST.VoidTy, Args: ParamTypes, EPI);
937 auto *TSInfo = AST.getTrivialTypeSourceInfo(T: FuncTy, Loc: SourceLocation());
938
939 IdentifierInfo &FuncII = AST.Idents.get(Name: FuncName, TokenCode: tok::TokenKind::identifier);
940 DeclarationName FuncDeclName(&FuncII);
941
942 FunctionDecl *FD = FunctionDecl::Create(
943 C&: AST, DC: NS, StartLoc: SourceLocation(), NLoc: SourceLocation(), N: FuncDeclName, T: FuncTy, TInfo: TSInfo,
944 SC: SC_Extern, /*UsesFPIntrin=*/false, /*isInlineSpecified=*/false,
945 /*hasWrittenPrototype=*/true);
946
947 SmallVector<ParmVarDecl *, 4> ParmDecls;
948 unsigned I = 0;
949 for (auto [ParamType, ParamName] : llvm::zip(t&: ParamTypes, u&: ParamNames)) {
950 IdentifierInfo &PII = AST.Idents.get(Name: ParamName, TokenCode: tok::TokenKind::identifier);
951 ParmVarDecl *Parm = ParmVarDecl::Create(
952 C&: AST, DC: FD, StartLoc: SourceLocation(), IdLoc: SourceLocation(), Id: &PII, T: ParamType,
953 TInfo: AST.getTrivialTypeSourceInfo(T: ParamType, Loc: SourceLocation()), S: SC_None,
954 DefArg: nullptr);
955 if (HasOriginalValue && I == ParamTypes.size() - 1)
956 Parm->addAttr(A: HLSLParamModifierAttr::Create(
957 Ctx&: AST, Range: SourceRange(), S: HLSLParamModifierAttr::Keyword_out));
958 Parm->setScopeInfo(scopeDepth: 0, parameterIndex: I++);
959 ParmDecls.push_back(Elt: Parm);
960 }
961 FD->setParams(ParmDecls);
962
963 IdentifierInfo &BuiltinII =
964 S.getPreprocessor().getIdentifierTable().get(Name: BuiltinName);
965 FD->addAttr(A: BuiltinAliasAttr::CreateImplicit(Ctx&: AST, BuiltinName: &BuiltinII));
966 FD->setImplicit();
967 NS->addDecl(D: FD);
968}
969
970// Synthesize the InterlockedFunc overload set: {int, uint, int64_t, uint64_t}
971// x {groupshared, device} x {2-arg, 3-arg}. Operations that always report the
972// previous value, such as InterlockedExchange, only get the 3-arg form.
973// InterlockedExchange also accepts float, which lowers to a bitwise exchange
974// of the 32-bit pattern.
975static void defineHLSLInterlockedFunc(Sema &S, NamespaceDecl *NS,
976 StringRef FuncName, StringRef BuiltinName,
977 bool RequiresOriginalValue = false,
978 bool SupportsFloat = false) {
979 ASTContext &AST = S.getASTContext();
980 // HLSL: int64_t == long, uint64_t == unsigned long (see hlsl_basic_types.h).
981 SmallVector<QualType, 5> Elems = {AST.IntTy, AST.UnsignedIntTy, AST.LongTy,
982 AST.UnsignedLongTy};
983 if (SupportsFloat)
984 Elems.push_back(Elt: AST.FloatTy);
985 LangAS AddrSpaces[] = {LangAS::hlsl_groupshared, LangAS::hlsl_device};
986
987 for (QualType ElemTy : Elems)
988 for (LangAS AS : AddrSpaces) {
989 if (!RequiresOriginalValue)
990 buildAtomicOverload(S, NS, FuncName, BuiltinName, ElemTy, DestAS: AS,
991 Shape: AtomicOverloadShape::Binary);
992 buildAtomicOverload(S, NS, FuncName, BuiltinName, ElemTy, DestAS: AS,
993 Shape: AtomicOverloadShape::BinaryWithOriginal);
994 }
995}
996
997// Synthesize the compare-and-swap overload sets: {int, uint, int64_t,
998// uint64_t} x {groupshared, device}. Each function has one form only.
999static void defineHLSLInterlockedCompareFunc(Sema &S, NamespaceDecl *NS,
1000 StringRef FuncName,
1001 StringRef BuiltinName,
1002 AtomicOverloadShape Shape) {
1003 ASTContext &AST = S.getASTContext();
1004 QualType Elems[] = {AST.IntTy, AST.UnsignedIntTy, AST.LongTy,
1005 AST.UnsignedLongTy};
1006
1007 for (QualType ElemTy : Elems)
1008 for (LangAS AS : {LangAS::hlsl_groupshared, LangAS::hlsl_device})
1009 buildAtomicOverload(S, NS, FuncName, BuiltinName, ElemTy, DestAS: AS, Shape);
1010}
1011
1012// The float-bitwise compare-and-swap functions have their own names and take
1013// float only.
1014static void defineHLSLInterlockedCompareFuncFloat(Sema &S, NamespaceDecl *NS,
1015 StringRef FuncName,
1016 StringRef BuiltinName,
1017 AtomicOverloadShape Shape) {
1018 ASTContext &AST = S.getASTContext();
1019 buildAtomicOverload(S, NS, FuncName, BuiltinName, ElemTy: AST.FloatTy,
1020 DestAS: LangAS::hlsl_groupshared, Shape);
1021 buildAtomicOverload(S, NS, FuncName, BuiltinName, ElemTy: AST.FloatTy,
1022 DestAS: LangAS::hlsl_device, Shape);
1023}
1024
1025void HLSLExternalSemaSource::defineHLSLAtomicIntrinsics() {
1026 defineHLSLInterlockedFunc(S&: *SemaPtr, NS: HLSLNamespace, FuncName: "InterlockedAdd",
1027 BuiltinName: "__builtin_hlsl_interlocked_add");
1028 defineHLSLInterlockedFunc(S&: *SemaPtr, NS: HLSLNamespace, FuncName: "InterlockedAnd",
1029 BuiltinName: "__builtin_hlsl_interlocked_and");
1030 defineHLSLInterlockedCompareFunc(
1031 S&: *SemaPtr, NS: HLSLNamespace, FuncName: "InterlockedCompareExchange",
1032 BuiltinName: "__builtin_hlsl_interlocked_compare_exchange",
1033 Shape: AtomicOverloadShape::CompareExchange);
1034 defineHLSLInterlockedCompareFuncFloat(
1035 S&: *SemaPtr, NS: HLSLNamespace, FuncName: "InterlockedCompareExchangeFloatBitwise",
1036 BuiltinName: "__builtin_hlsl_interlocked_compare_exchange_float_bitwise",
1037 Shape: AtomicOverloadShape::CompareExchange);
1038 defineHLSLInterlockedCompareFunc(S&: *SemaPtr, NS: HLSLNamespace,
1039 FuncName: "InterlockedCompareStore",
1040 BuiltinName: "__builtin_hlsl_interlocked_compare_store",
1041 Shape: AtomicOverloadShape::CompareStore);
1042 defineHLSLInterlockedCompareFuncFloat(
1043 S&: *SemaPtr, NS: HLSLNamespace, FuncName: "InterlockedCompareStoreFloatBitwise",
1044 BuiltinName: "__builtin_hlsl_interlocked_compare_store_float_bitwise",
1045 Shape: AtomicOverloadShape::CompareStore);
1046 defineHLSLInterlockedFunc(S&: *SemaPtr, NS: HLSLNamespace, FuncName: "InterlockedExchange",
1047 BuiltinName: "__builtin_hlsl_interlocked_exchange",
1048 /*RequiresOriginalValue=*/true,
1049 /*SupportsFloat=*/true);
1050 defineHLSLInterlockedFunc(S&: *SemaPtr, NS: HLSLNamespace, FuncName: "InterlockedMax",
1051 BuiltinName: "__builtin_hlsl_interlocked_max");
1052 defineHLSLInterlockedFunc(S&: *SemaPtr, NS: HLSLNamespace, FuncName: "InterlockedMin",
1053 BuiltinName: "__builtin_hlsl_interlocked_min");
1054 defineHLSLInterlockedFunc(S&: *SemaPtr, NS: HLSLNamespace, FuncName: "InterlockedOr",
1055 BuiltinName: "__builtin_hlsl_interlocked_or");
1056 defineHLSLInterlockedFunc(S&: *SemaPtr, NS: HLSLNamespace, FuncName: "InterlockedXor",
1057 BuiltinName: "__builtin_hlsl_interlocked_xor");
1058}
1059
1060void HLSLExternalSemaSource::onCompletion(CXXRecordDecl *Record,
1061 CompletionFunction Fn) {
1062 if (!Record->isCompleteDefinition())
1063 Completions.insert(KV: std::make_pair(x: Record->getCanonicalDecl(), y&: Fn));
1064}
1065
1066void HLSLExternalSemaSource::CompleteType(TagDecl *Tag) {
1067 if (!isa<CXXRecordDecl>(Val: Tag))
1068 return;
1069 auto *Record = cast<CXXRecordDecl>(Val: Tag);
1070 Record = Record->getCanonicalDecl();
1071 auto It = Completions.find(Val: Record);
1072 if (It == Completions.end())
1073 return;
1074 // Move out the callback and erase before invoking it: the callback can
1075 // re-enter CompleteType and mutate Completions, which invalidates It under
1076 // backward-shift deletion.
1077 CompletionFunction Fn = std::move(It->second);
1078 Completions.erase(I: It);
1079 Fn(Record);
1080}
1081