Skip to content

Commit 19af858

Browse files
authored
[HLSL] Constant Buffers CodeGen (#124886)
Translates `cbuffer` declaration blocks to `target("dx.CBuffer")` type. Creates global variables in `hlsl_constant` address space for all `cbuffer` constant and adds metadata describing which global constant belongs to which constant buffer. For explicit constant buffer layout information an explicit layout type `target("dx.Layout")` is used. This might change in the future. The constant globals are temporary and will be removed in upcoming pass that will translate `load` instructions in the `hlsl_constant` address space to constant buffer load intrinsics calls off a CBV handle (#124630, #112992). See [Constant buffer design doc](llvm/wg-hlsl#94) for more details. Fixes #113514, #106596
1 parent 2a7d3f0 commit 19af858

20 files changed

+871
-239
lines changed

clang/include/clang/AST/Decl.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5039,6 +5039,11 @@ class HLSLBufferDecl final : public NamedDecl, public DeclContext {
50395039
SourceLocation KwLoc;
50405040
/// IsCBuffer - Whether the buffer is a cbuffer (and not a tbuffer).
50415041
bool IsCBuffer;
5042+
/// HasValidPackoffset - Whether the buffer has valid packoffset annotations
5043+
// on all declarations
5044+
bool HasValidPackoffset;
5045+
// LayoutStruct - Layout struct for the buffer
5046+
CXXRecordDecl *LayoutStruct;
50425047

50435048
HLSLBufferDecl(DeclContext *DC, bool CBuffer, SourceLocation KwLoc,
50445049
IdentifierInfo *ID, SourceLocation IDLoc,
@@ -5059,6 +5064,10 @@ class HLSLBufferDecl final : public NamedDecl, public DeclContext {
50595064
SourceLocation getRBraceLoc() const { return RBraceLoc; }
50605065
void setRBraceLoc(SourceLocation L) { RBraceLoc = L; }
50615066
bool isCBuffer() const { return IsCBuffer; }
5067+
void setHasValidPackoffset(bool PO) { HasValidPackoffset = PO; }
5068+
bool hasValidPackoffset() const { return HasValidPackoffset; }
5069+
const CXXRecordDecl *getLayoutStruct() const { return LayoutStruct; }
5070+
void addLayoutStruct(CXXRecordDecl *LS);
50625071

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

clang/include/clang/AST/Type.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6266,8 +6266,8 @@ class HLSLAttributedResourceType : public Type, public llvm::FoldingSetNode {
62666266
LLVM_PREFERRED_TYPE(bool)
62676267
uint8_t RawBuffer : 1;
62686268

6269-
Attributes(llvm::dxil::ResourceClass ResourceClass, bool IsROV,
6270-
bool RawBuffer)
6269+
Attributes(llvm::dxil::ResourceClass ResourceClass, bool IsROV = false,
6270+
bool RawBuffer = false)
62716271
: ResourceClass(ResourceClass), IsROV(IsROV), RawBuffer(RawBuffer) {}
62726272

62736273
Attributes() : Attributes(llvm::dxil::ResourceClass::UAV, false, false) {}

clang/lib/AST/Decl.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1747,6 +1747,10 @@ void NamedDecl::printNestedNameSpecifier(raw_ostream &OS,
17471747
}
17481748
}
17491749

1750+
// Suppress transparent contexts like export or HLSLBufferDecl context
1751+
if (Ctx->isTransparentContext())
1752+
continue;
1753+
17501754
// Skip non-named contexts such as linkage specifications and ExportDecls.
17511755
const NamedDecl *ND = dyn_cast<NamedDecl>(Ctx);
17521756
if (!ND)
@@ -5717,7 +5721,7 @@ HLSLBufferDecl::HLSLBufferDecl(DeclContext *DC, bool CBuffer,
57175721
SourceLocation IDLoc, SourceLocation LBrace)
57185722
: NamedDecl(Decl::Kind::HLSLBuffer, DC, IDLoc, DeclarationName(ID)),
57195723
DeclContext(Decl::Kind::HLSLBuffer), LBraceLoc(LBrace), KwLoc(KwLoc),
5720-
IsCBuffer(CBuffer) {}
5724+
IsCBuffer(CBuffer), HasValidPackoffset(false), LayoutStruct(nullptr) {}
57215725

57225726
HLSLBufferDecl *HLSLBufferDecl::Create(ASTContext &C,
57235727
DeclContext *LexicalParent, bool CBuffer,
@@ -5747,6 +5751,12 @@ HLSLBufferDecl *HLSLBufferDecl::CreateDeserialized(ASTContext &C,
57475751
SourceLocation(), SourceLocation());
57485752
}
57495753

5754+
void HLSLBufferDecl::addLayoutStruct(CXXRecordDecl *LS) {
5755+
assert(LayoutStruct == nullptr && "layout struct has already been set");
5756+
LayoutStruct = LS;
5757+
addDecl(LS);
5758+
}
5759+
57505760
//===----------------------------------------------------------------------===//
57515761
// ImportDecl Implementation
57525762
//===----------------------------------------------------------------------===//

0 commit comments

Comments
 (0)