Skip to content

Commit ded2c28

Browse files
committed
Allocate list of default constant decls with ASTContext
1 parent 5bdff89 commit ded2c28

File tree

4 files changed

+30
-19
lines changed

4 files changed

+30
-19
lines changed

clang/include/clang/AST/Decl.h

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5045,22 +5045,26 @@ class HLSLBufferDecl final : public NamedDecl, public DeclContext {
50455045
// LayoutStruct - Layout struct for the buffer
50465046
CXXRecordDecl *LayoutStruct;
50475047

5048-
// For default (implicit) constant buffer, a lisf of references of global
5048+
// For default (implicit) constant buffer, an array of references of global
50495049
// decls that belong to the buffer. The decls are already parented by the
5050-
// translation unit context.
5051-
SmallVector<Decl *> DefaultBufferDecls;
5050+
// translation unit context. The array is allocated by the ASTContext
5051+
// allocator in HLSLBufferDecl::CreateDefaultCBuffer.
5052+
ArrayRef<Decl *> DefaultBufferDecls;
50525053

50535054
HLSLBufferDecl(DeclContext *DC, bool CBuffer, SourceLocation KwLoc,
50545055
IdentifierInfo *ID, SourceLocation IDLoc,
50555056
SourceLocation LBrace);
50565057

5058+
void setDefaultBufferDecls(ArrayRef<Decl *> Decls);
5059+
50575060
public:
50585061
static HLSLBufferDecl *Create(ASTContext &C, DeclContext *LexicalParent,
50595062
bool CBuffer, SourceLocation KwLoc,
50605063
IdentifierInfo *ID, SourceLocation IDLoc,
50615064
SourceLocation LBrace);
5062-
static HLSLBufferDecl *CreateDefaultCBuffer(ASTContext &C,
5063-
DeclContext *LexicalParent);
5065+
static HLSLBufferDecl *
5066+
CreateDefaultCBuffer(ASTContext &C, DeclContext *LexicalParent,
5067+
ArrayRef<Decl *> DefaultCBufferDecls);
50645068
static HLSLBufferDecl *CreateDeserialized(ASTContext &C, GlobalDeclID ID);
50655069

50665070
SourceRange getSourceRange() const override LLVM_READONLY {
@@ -5075,7 +5079,6 @@ class HLSLBufferDecl final : public NamedDecl, public DeclContext {
50755079
bool hasValidPackoffset() const { return HasValidPackoffset; }
50765080
const CXXRecordDecl *getLayoutStruct() const { return LayoutStruct; }
50775081
void addLayoutStruct(CXXRecordDecl *LS);
5078-
void addDefaultBufferDecl(Decl *D);
50795082

50805083
// Implement isa/cast/dyncast/etc.
50815084
static bool classof(const Decl *D) { return classofKind(D->getKind()); }

clang/include/clang/Sema/SemaHLSL.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,9 @@ class SemaHLSL : public SemaBase {
168168
// List of all resource bindings
169169
ResourceBindings Bindings;
170170

171-
// default constant buffer $Globals
172-
HLSLBufferDecl *DefaultCBuffer;
171+
// Global declaration collected for the $Globals default constant
172+
// buffer which will be created at the end of the translation unit.
173+
llvm::SmallVector<Decl *> DefaultCBufferDecls;
173174

174175
private:
175176
void collectResourceBindingsOnVarDecl(VarDecl *D);

clang/lib/AST/Decl.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5747,13 +5747,20 @@ HLSLBufferDecl *HLSLBufferDecl::Create(ASTContext &C,
57475747
}
57485748

57495749
HLSLBufferDecl *
5750-
HLSLBufferDecl::CreateDefaultCBuffer(ASTContext &C,
5751-
DeclContext *LexicalParent) {
5750+
HLSLBufferDecl::CreateDefaultCBuffer(ASTContext &C, DeclContext *LexicalParent,
5751+
ArrayRef<Decl *> DefaultCBufferDecls) {
57525752
DeclContext *DC = LexicalParent;
57535753
IdentifierInfo *II = &C.Idents.get("$Globals", tok::TokenKind::identifier);
57545754
HLSLBufferDecl *Result = new (C, DC) HLSLBufferDecl(
57555755
DC, true, SourceLocation(), II, SourceLocation(), SourceLocation());
57565756
Result->setImplicit(true);
5757+
5758+
// allocate array for default decls with ASTContext allocator
5759+
assert(!DefaultCBufferDecls.empty());
5760+
Decl **DeclsArray = new (C) Decl *[DefaultCBufferDecls.size()];
5761+
std::copy(DefaultCBufferDecls.begin(), DefaultCBufferDecls.end(), DeclsArray);
5762+
Result->setDefaultBufferDecls(
5763+
ArrayRef<Decl *>(DeclsArray, DefaultCBufferDecls.size()));
57575764
return Result;
57585765
}
57595766

@@ -5769,11 +5776,11 @@ void HLSLBufferDecl::addLayoutStruct(CXXRecordDecl *LS) {
57695776
addDecl(LS);
57705777
}
57715778

5772-
void HLSLBufferDecl::addDefaultBufferDecl(Decl *D) {
5779+
void HLSLBufferDecl::setDefaultBufferDecls(ArrayRef<Decl *> Decls) {
57735780
assert(isImplicit() &&
57745781
"default decls can only be added to the implicit/default constant "
57755782
"buffer $Globals");
5776-
DefaultBufferDecls.push_back(D);
5783+
DefaultBufferDecls = Decls;
57775784
}
57785785

57795786
HLSLBufferDecl::buffer_decl_iterator

clang/lib/Sema/SemaHLSL.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ bool ResourceBindings::hasBindingInfoForDecl(const VarDecl *VD) const {
148148
return DeclToBindingListIndex.contains(VD);
149149
}
150150

151-
SemaHLSL::SemaHLSL(Sema &S) : SemaBase(S), DefaultCBuffer(nullptr) {}
151+
SemaHLSL::SemaHLSL(Sema &S) : SemaBase(S) {}
152152

153153
Decl *SemaHLSL::ActOnStartBuffer(Scope *BufferScope, bool CBuffer,
154154
SourceLocation KwLoc, IdentifierInfo *Ident,
@@ -1931,7 +1931,10 @@ void DiagnoseHLSLAvailability::CheckDeclAvailability(NamedDecl *D,
19311931

19321932
void SemaHLSL::ActOnEndOfTranslationUnit(TranslationUnitDecl *TU) {
19331933
// process default CBuffer - create buffer layout struct and invoke codegenCGH
1934-
if (DefaultCBuffer) {
1934+
if (!DefaultCBufferDecls.empty()) {
1935+
HLSLBufferDecl *DefaultCBuffer = HLSLBufferDecl::CreateDefaultCBuffer(
1936+
SemaRef.getASTContext(), SemaRef.getCurLexicalContext(),
1937+
DefaultCBufferDecls);
19351938
SemaRef.getCurLexicalContext()->addDecl(DefaultCBuffer);
19361939
createHostLayoutStructForBuffer(SemaRef, DefaultCBuffer);
19371940

@@ -3025,16 +3028,13 @@ void SemaHLSL::ActOnVariableDeclarator(VarDecl *VD) {
30253028

30263029
// Global variables outside a cbuffer block that are not a resource, static,
30273030
// groupshared, or an empty array or struct belong to the default constant
3028-
// buffer $Globals
3031+
// buffer $Globals (to be created at the end of the translation unit).
30293032
if (IsDefaultBufferConstantDecl(VD)) {
3030-
if (DefaultCBuffer == nullptr)
3031-
DefaultCBuffer = HLSLBufferDecl::CreateDefaultCBuffer(
3032-
SemaRef.getASTContext(), SemaRef.getCurLexicalContext());
30333033
// update address space to hlsl_constant
30343034
QualType NewTy = getASTContext().getAddrSpaceQualType(
30353035
VD->getType(), LangAS::hlsl_constant);
30363036
VD->setType(NewTy);
3037-
DefaultCBuffer->addDefaultBufferDecl(VD);
3037+
DefaultCBufferDecls.push_back(VD);
30383038
}
30393039

30403040
// find all resources bindings on decl

0 commit comments

Comments
 (0)