Skip to content

Commit 8e3b9e7

Browse files
Update Constant/TextureBuffer implementation. (#3147)
* Update Constant/TextureBuffer implementation. * [spirv] Fixes needed for new Constant/TextureBuffer representation. Co-authored-by: Ehsan Nasiri <[email protected]>
1 parent 4f7da65 commit 8e3b9e7

28 files changed

Lines changed: 488 additions & 259 deletions

lib/DXIL/DxilUtil.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -615,9 +615,6 @@ bool IsHLSLResourceType(llvm::Type *Ty) {
615615
if (name.startswith("ConsumeStructuredBuffer<"))
616616
return true;
617617

618-
if (name.startswith("ConstantBuffer<"))
619-
return true;
620-
621618
if (name == "RaytracingAccelerationStructure")
622619
return true;
623620

lib/HLSL/DxilContainerReflection.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1262,7 +1262,15 @@ void CShaderReflectionConstantBuffer::Initialize(
12621262

12631263
// Replicate fxc bug, where Elements == 1 for inner struct of CB array, instead of 0.
12641264
if (CB.GetRangeSize() > 1) {
1265-
DXASSERT(pVarType->m_Desc.Elements == 0, "otherwise, assumption is wrong");
1265+
DXASSERT(pVarType->m_Desc.Elements == 0,
1266+
"otherwise, assumption is wrong");
1267+
pVarType->m_Desc.Elements = 1;
1268+
} else if (CB.GetGlobalSymbol()
1269+
->getType()
1270+
->getPointerElementType()
1271+
->isArrayTy() &&
1272+
CB.GetRangeSize() == 1) {
1273+
// Set elements to 1 for size 1 array.
12661274
pVarType->m_Desc.Elements = 1;
12671275
}
12681276

lib/HLSL/DxilGenerationPass.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,8 @@ void DxilGenerationPass::GenerateDxilCBufferHandles() {
528528
DIV->getScope());
529529
}
530530

531-
if (CB.GetRangeSize() == 1) {
531+
if (CB.GetRangeSize() == 1 &&
532+
!GV->getType()->getElementType()->isArrayTy()) {
532533
Function *createHandle =
533534
hlslOP->GetOpFunc(OP::OpCode::CreateHandleForLib,
534535
GV->getType()->getElementType());

lib/HLSL/DxilValidation.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4083,7 +4083,7 @@ CollectCBufferRanges(DxilStructAnnotation *annotation,
40834083

40844084
static void ValidateCBuffer(DxilCBuffer &cb, ValidationContext &ValCtx) {
40854085
Type *Ty = cb.GetGlobalSymbol()->getType()->getPointerElementType();
4086-
if (cb.GetRangeSize() != 1) {
4086+
if (cb.GetRangeSize() != 1 || Ty->isArrayTy()) {
40874087
Ty = Ty->getArrayElementType();
40884088
}
40894089
if (!isa<StructType>(Ty)) {

tools/clang/include/clang/AST/HlslTypes.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,7 @@ clang::CXXRecordDecl* DeclareTemplateTypeWithHandle(
322322

323323
clang::CXXRecordDecl* DeclareUIntTemplatedTypeWithHandle(
324324
clang::ASTContext& context, llvm::StringRef typeName, llvm::StringRef templateParamName);
325+
clang::CXXRecordDecl *DeclareConstantBufferViewType(clang::ASTContext& context, bool bTBuf);
325326
clang::CXXRecordDecl* DeclareRayQueryType(clang::ASTContext& context);
326327
clang::CXXRecordDecl *DeclareResourceType(clang::ASTContext &context);
327328

@@ -371,6 +372,7 @@ bool IsHLSLLineStreamType(clang::QualType type);
371372
bool IsHLSLTriangleStreamType(clang::QualType type);
372373
bool IsHLSLStreamOutputType(clang::QualType type);
373374
bool IsHLSLResourceType(clang::QualType type);
375+
bool IsHLSLBufferViewType(clang::QualType type);
374376
bool IsHLSLNumericOrAggregateOfNumericType(clang::QualType type);
375377
bool IsHLSLNumericUserDefinedType(clang::QualType type);
376378
bool IsHLSLAggregateType(clang::QualType type);

tools/clang/include/clang/Basic/Attr.td

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -905,6 +905,32 @@ def HLSLWaveSensitive : InheritableAttr {
905905

906906
// SPIRV Change Starts
907907

908+
// StructuredBuffer types that can have associated counters
909+
def CounterStructuredBuffer : SubsetSubject<
910+
Var,
911+
[{S->hasGlobalStorage() && S->getType()->getAs<RecordType>() &&
912+
(S->getType()->getAs<RecordType>()->getDecl()->getName() == "RWStructuredBuffer" ||
913+
S->getType()->getAs<RecordType>()->getDecl()->getName() == "AppendStructuredBuffer" ||
914+
S->getType()->getAs<RecordType>()->getDecl()->getName() == "ConsumeStructuredBuffer")}]>;
915+
916+
// Global variable with "ConstantBuffer" type
917+
def ConstantBuffer
918+
: SubsetSubject<
919+
Var, [{S->hasGlobalStorage() && S->getType()->getAs<RecordType>() &&
920+
S->getType()->getAs<RecordType>()->getDecl() &&
921+
S->getType()->getAs<RecordType>()->getDecl()->getName() ==
922+
"ConstantBuffer"}]>;
923+
924+
// Global variable with "ConstantBuffer" or "TextureBuffer" type
925+
def ConstantTextureBuffer
926+
: SubsetSubject<
927+
Var, [{S->hasGlobalStorage() && S->getType()->getAs<RecordType>() &&
928+
S->getType()->getAs<RecordType>()->getDecl() &&
929+
(S->getType()->getAs<RecordType>()->getDecl()->getName() ==
930+
"ConstantBuffer" ||
931+
S->getType()->getAs<RecordType>()->getDecl()->getName() ==
932+
"TextureBuffer")}]>;
933+
908934
def VKBuiltIn : InheritableAttr {
909935
let Spellings = [CXX11<"vk", "builtin">];
910936
let Subjects = SubjectList<[Function, ParmVar, Field], ErrorDiag>;
@@ -931,20 +957,13 @@ def VKIndex : InheritableAttr {
931957

932958
def VKBinding : InheritableAttr {
933959
let Spellings = [CXX11<"vk", "binding">];
934-
let Subjects = SubjectList<[GlobalVar, HLSLBuffer], ErrorDiag, "ExpectedGlobalVarOrCTBuffer">;
960+
let Subjects = SubjectList<[GlobalVar, HLSLBuffer, ConstantTextureBuffer],
961+
ErrorDiag, "ExpectedGlobalVarOrCTBuffer">;
935962
let Args = [IntArgument<"Binding">, DefaultIntArgument<"Set", 0>];
936963
let LangOpts = [SPIRV];
937964
let Documentation = [Undocumented];
938965
}
939966

940-
// StructuredBuffer types that can have associated counters
941-
def CounterStructuredBuffer : SubsetSubject<
942-
Var,
943-
[{S->hasGlobalStorage() && S->getType()->getAs<RecordType>() &&
944-
(S->getType()->getAs<RecordType>()->getDecl()->getName() == "RWStructuredBuffer" ||
945-
S->getType()->getAs<RecordType>()->getDecl()->getName() == "AppendStructuredBuffer" ||
946-
S->getType()->getAs<RecordType>()->getDecl()->getName() == "ConsumeStructuredBuffer")}]>;
947-
948967
def VKCounterBinding : InheritableAttr {
949968
let Spellings = [CXX11<"vk", "counter_binding">];
950969
let Subjects = SubjectList<[CounterStructuredBuffer], ErrorDiag, "ExpectedCounterStructuredBuffer">;
@@ -1006,7 +1025,8 @@ def VKPostDepthCoverage : InheritableAttr {
10061025

10071026
def VKShaderRecordNV : InheritableAttr {
10081027
let Spellings = [CXX11<"vk", "shader_record_nv">];
1009-
let Subjects = SubjectList<[StructGlobalVar, HLSLBuffer], ErrorDiag, "ExpectedCTBuffer">;
1028+
let Subjects = SubjectList<[StructGlobalVar, HLSLBuffer, ConstantBuffer],
1029+
ErrorDiag, "ExpectedCTBuffer">;
10101030
let Args = [];
10111031
let LangOpts = [SPIRV];
10121032
let Documentation = [Undocumented];

tools/clang/include/clang/Basic/TokenKinds.def

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -483,8 +483,6 @@ KEYWORD(__super , KEYMS)
483483
// HLSL Change: HLSL-specific keywords
484484
KEYWORD(cbuffer , KEYHLSL)
485485
KEYWORD(tbuffer , KEYHLSL)
486-
KEYWORD(ConstantBuffer , KEYHLSL)
487-
KEYWORD(TextureBuffer , KEYHLSL)
488486
KEYWORD(packoffset , KEYHLSL)
489487
KEYWORD(linear , KEYHLSL)
490488
KEYWORD(centroid , KEYHLSL)

tools/clang/include/clang/Parse/Parser.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2380,8 +2380,6 @@ class Parser : public CodeCompletionHandler {
23802380
// HLSL Change Starts
23812381
Decl *ParseCTBuffer(unsigned Context, SourceLocation &DeclEnd,
23822382
ParsedAttributesWithRange &attrs, SourceLocation InlineLoc = SourceLocation());
2383-
Decl *ParseConstBuffer(unsigned Context, SourceLocation &DeclEnd,
2384-
ParsedAttributesWithRange &attrs, SourceLocation InlineLoc = SourceLocation());
23852383
// HLSL Change Ends
23862384

23872385
Decl *ParseNamespace(unsigned Context, SourceLocation &DeclEnd,

tools/clang/include/clang/SPIRV/AstTypeProbe.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,17 @@ bool isMx1Matrix(QualType type, QualType *elemType = nullptr,
8686
bool isMxNMatrix(QualType type, QualType *elemType = nullptr,
8787
uint32_t *rowCount = nullptr, uint32_t *colCount = nullptr);
8888

89-
/// \brief Returns true if the decl is of ConstantBuffer/TextureBuffer type.
90-
bool isConstantTextureBuffer(const Decl *decl);
89+
/// \brief Returns true if the given type is a ConstantBuffer or an array of
90+
/// ConstantBuffers.
91+
bool isConstantBuffer(QualType);
92+
93+
/// \brief Returns true if the given type is a TextureBuffer or an array of
94+
/// TextureBuffers.
95+
bool isTextureBuffer(QualType);
96+
97+
/// \brief Returns true if the given type is a ConstantBuffer or TextureBuffer
98+
/// or an array of ConstantBuffers/TextureBuffers.
99+
bool isConstantTextureBuffer(QualType);
91100

92101
/// \brief Returns true if the decl will have a SPIR-V resource type.
93102
///

tools/clang/include/clang/Sema/SemaHLSL.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ bool LookupRecordMemberExprForHLSL(
156156
clang::SourceLocation MemberLoc,
157157
clang::ExprResult &result);
158158

159-
clang::ExprResult MaybeConvertScalarToVector(
159+
clang::ExprResult MaybeConvertMemberAccess(
160160
_In_ clang::Sema* Self,
161161
_In_ clang::Expr* E);
162162

0 commit comments

Comments
 (0)