99// ===----------------------------------------------------------------------===//
1010
1111#include " clang/Sema/SemaHLSL.h"
12+ #include " clang/AST/ASTConsumer.h"
1213#include " clang/AST/ASTContext.h"
1314#include " clang/AST/Attr.h"
1415#include " clang/AST/Attrs.inc"
@@ -147,7 +148,7 @@ bool ResourceBindings::hasBindingInfoForDecl(const VarDecl *VD) const {
147148 return DeclToBindingListIndex.contains (VD);
148149}
149150
150- SemaHLSL::SemaHLSL (Sema &S) : SemaBase(S) {}
151+ SemaHLSL::SemaHLSL (Sema &S) : SemaBase(S), DefaultCBuffer( nullptr ) {}
151152
152153Decl *SemaHLSL::ActOnStartBuffer (Scope *BufferScope, bool CBuffer,
153154 SourceLocation KwLoc, IdentifierInfo *Ident,
@@ -219,7 +220,7 @@ static void validatePackoffset(Sema &S, HLSLBufferDecl *BufDecl) {
219220 // or on none.
220221 bool HasPackOffset = false ;
221222 bool HasNonPackOffset = false ;
222- for (auto *Field : BufDecl->decls ()) {
223+ for (auto *Field : BufDecl->buffer_decls ()) {
223224 VarDecl *Var = dyn_cast<VarDecl>(Field);
224225 if (!Var)
225226 continue ;
@@ -486,7 +487,7 @@ void createHostLayoutStructForBuffer(Sema &S, HLSLBufferDecl *BufDecl) {
486487 LS->setImplicit (true );
487488 LS->startDefinition ();
488489
489- for (Decl *D : BufDecl->decls ()) {
490+ for (Decl *D : BufDecl->buffer_decls ()) {
490491 VarDecl *VD = dyn_cast<VarDecl>(D);
491492 if (!VD || VD->getStorageClass () == SC_Static ||
492493 VD->getType ().getAddressSpace () == LangAS::hlsl_groupshared)
@@ -1922,7 +1923,21 @@ void DiagnoseHLSLAvailability::CheckDeclAvailability(NamedDecl *D,
19221923
19231924} // namespace
19241925
1925- void SemaHLSL::DiagnoseAvailabilityViolations (TranslationUnitDecl *TU) {
1926+ void SemaHLSL::ActOnEndOfTranslationUnit (TranslationUnitDecl *TU) {
1927+
1928+ // process default CBuffer - create buffer layout struct and invoke codegenCGH
1929+ if (DefaultCBuffer) {
1930+ SemaRef.getCurLexicalContext ()->addDecl (DefaultCBuffer);
1931+ createHostLayoutStructForBuffer (SemaRef, DefaultCBuffer);
1932+
1933+ DeclGroupRef DG (DefaultCBuffer);
1934+ SemaRef.Consumer .HandleTopLevelDecl (DG);
1935+ }
1936+
1937+ diagnoseAvailabilityViolations (TU);
1938+ }
1939+
1940+ void SemaHLSL::diagnoseAvailabilityViolations (TranslationUnitDecl *TU) {
19261941 // Skip running the diagnostics scan if the diagnostic mode is
19271942 // strict (-fhlsl-strict-availability) and the target shader stage is known
19281943 // because all relevant diagnostics were already emitted in the
@@ -2784,6 +2799,14 @@ QualType SemaHLSL::getInoutParameterType(QualType Ty) {
27842799 return Ty;
27852800}
27862801
2802+ static bool IsDefaultBufferConstantDecl (VarDecl *VD) {
2803+ QualType QT = VD->getType ();
2804+ return VD->getDeclContext ()->isTranslationUnit () &&
2805+ QT.getAddressSpace () == LangAS::Default &&
2806+ VD->getStorageClass () != SC_Static &&
2807+ !isInvalidConstantBufferLeafElementType (QT.getTypePtr ());
2808+ }
2809+
27872810void SemaHLSL::ActOnVariableDeclarator (VarDecl *VD) {
27882811 if (VD->hasGlobalStorage ()) {
27892812 // make sure the declaration has a complete type
@@ -2795,7 +2818,21 @@ void SemaHLSL::ActOnVariableDeclarator(VarDecl *VD) {
27952818 return ;
27962819 }
27972820
2798- // find all resources on decl
2821+ // Global variables outside a cbuffer block that are not a resource, static,
2822+ // groupshared, or an empty array or struct belong to the default constant
2823+ // buffer $Globals
2824+ if (IsDefaultBufferConstantDecl (VD)) {
2825+ if (DefaultCBuffer == nullptr )
2826+ DefaultCBuffer = HLSLBufferDecl::CreateDefaultCBuffer (
2827+ SemaRef.getASTContext (), SemaRef.getCurLexicalContext ());
2828+ // update address space to hlsl_constant
2829+ QualType NewTy = getASTContext ().getAddrSpaceQualType (
2830+ VD->getType (), LangAS::hlsl_constant);
2831+ VD->setType (NewTy);
2832+ DefaultCBuffer->addDefaultBufferDecl (VD);
2833+ }
2834+
2835+ // find all resources bindings on decl
27992836 if (VD->getType ()->isHLSLIntangibleType ())
28002837 collectResourcesOnVarDecl (VD);
28012838
0 commit comments