Skip to content

Commit cf08adb

Browse files
committed
[HLSL] Translate cbuffer declarations to target type dx.CBuffer - initial commit
1 parent c80415a commit cf08adb

15 files changed

+679
-225
lines changed

clang/include/clang/AST/Decl.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5032,6 +5032,9 @@ class HLSLBufferDecl final : public NamedDecl, public DeclContext {
50325032
SourceLocation KwLoc;
50335033
/// IsCBuffer - Whether the buffer is a cbuffer (and not a tbuffer).
50345034
bool IsCBuffer;
5035+
/// HasValidPackoffset - Whether the buffer has valid packoffset annotations
5036+
// on all declarations
5037+
bool HasPackoffset;
50355038

50365039
HLSLBufferDecl(DeclContext *DC, bool CBuffer, SourceLocation KwLoc,
50375040
IdentifierInfo *ID, SourceLocation IDLoc,
@@ -5052,6 +5055,9 @@ class HLSLBufferDecl final : public NamedDecl, public DeclContext {
50525055
SourceLocation getRBraceLoc() const { return RBraceLoc; }
50535056
void setRBraceLoc(SourceLocation L) { RBraceLoc = L; }
50545057
bool isCBuffer() const { return IsCBuffer; }
5058+
void setHasPackoffset(bool PO) { HasPackoffset = PO; }
5059+
bool hasPackoffset() const { return HasPackoffset; }
5060+
const CXXRecordDecl *getLayoutStruct() const;
50555061

50565062
// Implement isa/cast/dyncast/etc.
50575063
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: 16 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)
@@ -5713,7 +5717,7 @@ HLSLBufferDecl::HLSLBufferDecl(DeclContext *DC, bool CBuffer,
57135717
SourceLocation IDLoc, SourceLocation LBrace)
57145718
: NamedDecl(Decl::Kind::HLSLBuffer, DC, IDLoc, DeclarationName(ID)),
57155719
DeclContext(Decl::Kind::HLSLBuffer), LBraceLoc(LBrace), KwLoc(KwLoc),
5716-
IsCBuffer(CBuffer) {}
5720+
IsCBuffer(CBuffer), HasPackoffset(false) {}
57175721

57185722
HLSLBufferDecl *HLSLBufferDecl::Create(ASTContext &C,
57195723
DeclContext *LexicalParent, bool CBuffer,
@@ -5743,6 +5747,17 @@ HLSLBufferDecl *HLSLBufferDecl::CreateDeserialized(ASTContext &C,
57435747
SourceLocation(), SourceLocation());
57445748
}
57455749

5750+
const CXXRecordDecl *HLSLBufferDecl::getLayoutStruct() const {
5751+
// Layout struct is the last decl in the HLSLBufferDecl.
5752+
if (CXXRecordDecl *RD = llvm::dyn_cast_or_null<CXXRecordDecl>(LastDecl)) {
5753+
assert(RD->getName().starts_with(
5754+
("__cblayout_" + getIdentifier()->getName()).str()) &&
5755+
"expected buffer layout struct");
5756+
return RD;
5757+
}
5758+
llvm_unreachable("HLSL buffer is missing a layout struct");
5759+
}
5760+
57465761
//===----------------------------------------------------------------------===//
57475762
// ImportDecl Implementation
57485763
//===----------------------------------------------------------------------===//

0 commit comments

Comments
 (0)