Skip to content

Commit 87680ba

Browse files
committed
[HLSL][RootSignature] Define and integrate rootsig clang attr and decl
- Defines a new declaration node `HLSLRootSignature` in `DeclNodes.td` that will hold a reference to an in-memory construction of the root signature, namely an array of `hlsl::rootsig::RootElement`s - Defines a new clang attr `RootSignature` which simply holds an identifier to a corresponding root signature declration as above It was previously proposed that we could have the root elements reference be stored directly as an additional member of the attribute and to not have a seperate root signature decl. In contrast, by defining them seperately as this change proposes, we will allow a unique root signature to have its own declaration in the AST tree. This allows us to only construct a single root signature for all duplicate rootsignature attributes. Having it located directly as a declaration might also prove advantageous when we consider root signature libraries.
1 parent 74ed334 commit 87680ba

File tree

20 files changed

+270
-0
lines changed

20 files changed

+270
-0
lines changed

clang/include/clang/AST/Decl.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
#include "llvm/ADT/PointerUnion.h"
4242
#include "llvm/ADT/StringRef.h"
4343
#include "llvm/ADT/iterator_range.h"
44+
#include "llvm/Frontend/HLSL/HLSLRootSignature.h"
4445
#include "llvm/Support/Casting.h"
4546
#include "llvm/Support/Compiler.h"
4647
#include "llvm/Support/TrailingObjects.h"
@@ -5178,6 +5179,29 @@ class HLSLBufferDecl final : public NamedDecl, public DeclContext {
51785179
friend class ASTDeclWriter;
51795180
};
51805181

5182+
class HLSLRootSignatureDecl final : public NamedDecl {
5183+
ArrayRef<llvm::hlsl::rootsig::RootElement> RootElements;
5184+
5185+
HLSLRootSignatureDecl(
5186+
DeclContext *DC, SourceLocation Loc, IdentifierInfo *ID,
5187+
ArrayRef<llvm::hlsl::rootsig::RootElement> RootElements);
5188+
5189+
public:
5190+
static HLSLRootSignatureDecl *
5191+
Create(ASTContext &C, DeclContext *DC, SourceLocation Loc, IdentifierInfo *ID,
5192+
ArrayRef<llvm::hlsl::rootsig::RootElement> RootElements);
5193+
static HLSLRootSignatureDecl *CreateDeserialized(ASTContext &C,
5194+
GlobalDeclID ID);
5195+
5196+
ArrayRef<llvm::hlsl::rootsig::RootElement> &getRootElements() {
5197+
return RootElements;
5198+
}
5199+
5200+
// Implement isa/cast/dyncast/etc.
5201+
static bool classof(const Decl *D) { return classofKind(D->getKind()); }
5202+
static bool classofKind(Kind K) { return K == HLSLRootSignature; }
5203+
};
5204+
51815205
/// Insertion operator for diagnostics. This allows sending NamedDecl's
51825206
/// into a diagnostic with <<.
51835207
inline const StreamingDiagnostic &operator<<(const StreamingDiagnostic &PD,

clang/include/clang/AST/RecursiveASTVisitor.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1599,6 +1599,8 @@ DEF_TRAVERSE_DECL(EmptyDecl, {})
15991599

16001600
DEF_TRAVERSE_DECL(HLSLBufferDecl, {})
16011601

1602+
DEF_TRAVERSE_DECL(HLSLRootSignatureDecl, {})
1603+
16021604
DEF_TRAVERSE_DECL(LifetimeExtendedTemporaryDecl, {
16031605
TRY_TO(TraverseStmt(D->getTemporaryExpr()));
16041606
})

clang/include/clang/AST/TextNodeDumper.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,7 @@ class TextNodeDumper
408408
void
409409
VisitLifetimeExtendedTemporaryDecl(const LifetimeExtendedTemporaryDecl *D);
410410
void VisitHLSLBufferDecl(const HLSLBufferDecl *D);
411+
void VisitHLSLRootSignatureDecl(const HLSLRootSignatureDecl *D);
411412
void VisitHLSLOutArgExpr(const HLSLOutArgExpr *E);
412413
void VisitOpenACCConstructStmt(const OpenACCConstructStmt *S);
413414
void VisitOpenACCLoopConstruct(const OpenACCLoopConstruct *S);

clang/include/clang/Basic/Attr.td

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4735,6 +4735,17 @@ def Error : InheritableAttr {
47354735
let Documentation = [ErrorAttrDocs];
47364736
}
47374737

4738+
/// HLSL Root Signature Attribute
4739+
def RootSignature : Attr {
4740+
/// [RootSignature(Signature)]
4741+
let Spellings = [Microsoft<"RootSignature">];
4742+
let Args = [IdentifierArgument<"Signature">];
4743+
let Subjects = SubjectList<[Function],
4744+
ErrorDiag, "'function'">;
4745+
let LangOpts = [HLSL];
4746+
let Documentation = [RootSignatureDocs];
4747+
}
4748+
47384749
def HLSLNumThreads: InheritableAttr {
47394750
let Spellings = [Microsoft<"numthreads">];
47404751
let Args = [IntArgument<"X">, IntArgument<"Y">, IntArgument<"Z">];

clang/include/clang/Basic/AttrDocs.td

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8195,6 +8195,17 @@ and https://microsoft.github.io/hlsl-specs/proposals/0013-wave-size-range.html
81958195
}];
81968196
}
81978197

8198+
def RootSignatureDocs : Documentation {
8199+
let Category = DocCatFunction;
8200+
let Content = [{
8201+
The ``RootSignature`` attribute applies to HLSL entry functions to define what
8202+
types of resources are bound to the graphics pipeline.
8203+
8204+
For details about the use and specification of Root Signatures please see here:
8205+
https://learn.microsoft.com/en-us/windows/win32/direct3d12/root-signatures
8206+
}];
8207+
}
8208+
81988209
def NumThreadsDocs : Documentation {
81998210
let Category = DocCatFunction;
82008211
let Content = [{

clang/include/clang/Basic/DeclNodes.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,5 +111,6 @@ def Empty : DeclNode<Decl>;
111111
def RequiresExprBody : DeclNode<Decl>, DeclContext;
112112
def LifetimeExtendedTemporary : DeclNode<Decl>;
113113
def HLSLBuffer : DeclNode<Named, "HLSLBuffer">, DeclContext;
114+
def HLSLRootSignature : DeclNode<Named, "HLSLRootSignature">;
114115
def OpenACCDeclare : DeclNode<Decl, "#pragma acc declare">;
115116
def OpenACCRoutine : DeclNode<Decl, "#pragma acc routine">;

clang/include/clang/Parse/Parser.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3093,6 +3093,7 @@ class Parser : public CodeCompletionHandler {
30933093
return AttrsParsed;
30943094
}
30953095
void ParseMicrosoftUuidAttributeArgs(ParsedAttributes &Attrs);
3096+
void ParseMicrosoftRootSignatureAttributeArgs(ParsedAttributes &Attrs);
30963097
void ParseMicrosoftAttributes(ParsedAttributes &Attrs);
30973098
bool MaybeParseMicrosoftDeclSpecs(ParsedAttributes &Attrs) {
30983099
if (getLangOpts().DeclSpecKeyword && Tok.is(tok::kw___declspec)) {

clang/include/clang/Sema/SemaHLSL.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ class SemaHLSL : public SemaBase {
119119
bool IsCompAssign);
120120
void emitLogicalOperatorFixIt(Expr *LHS, Expr *RHS, BinaryOperatorKind Opc);
121121

122+
void handleRootSignatureAttr(Decl *D, const ParsedAttr &AL);
122123
void handleNumThreadsAttr(Decl *D, const ParsedAttr &AL);
123124
void handleWaveSizeAttr(Decl *D, const ParsedAttr &AL);
124125
void handleSV_DispatchThreadIDAttr(Decl *D, const ParsedAttr &AL);

clang/lib/AST/Decl.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5847,6 +5847,31 @@ bool HLSLBufferDecl::buffer_decls_empty() {
58475847
return DefaultBufferDecls.empty() && decls_empty();
58485848
}
58495849

5850+
//===----------------------------------------------------------------------===//
5851+
// HLSLRootSignatureDecl Implementation
5852+
//===----------------------------------------------------------------------===//
5853+
5854+
HLSLRootSignatureDecl::HLSLRootSignatureDecl(
5855+
DeclContext *DC, SourceLocation Loc, IdentifierInfo *ID,
5856+
ArrayRef<llvm::hlsl::rootsig::RootElement> RootElements)
5857+
: NamedDecl(Decl::Kind::HLSLRootSignature, DC, Loc, DeclarationName(ID)),
5858+
RootElements(RootElements) {}
5859+
5860+
HLSLRootSignatureDecl *HLSLRootSignatureDecl::Create(
5861+
ASTContext &C, DeclContext *DC, SourceLocation Loc, IdentifierInfo *ID,
5862+
ArrayRef<llvm::hlsl::rootsig::RootElement> RootElements) {
5863+
HLSLRootSignatureDecl *Result =
5864+
new (C, DC) HLSLRootSignatureDecl(DC, Loc, ID, RootElements);
5865+
return Result;
5866+
}
5867+
5868+
HLSLRootSignatureDecl *
5869+
HLSLRootSignatureDecl::CreateDeserialized(ASTContext &C, GlobalDeclID ID) {
5870+
HLSLRootSignatureDecl *Result =
5871+
new (C, ID) HLSLRootSignatureDecl(nullptr, SourceLocation(), nullptr, {});
5872+
return Result;
5873+
}
5874+
58505875
//===----------------------------------------------------------------------===//
58515876
// ImportDecl Implementation
58525877
//===----------------------------------------------------------------------===//

clang/lib/AST/DeclBase.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -886,6 +886,7 @@ unsigned Decl::getIdentifierNamespaceForKind(Kind DeclKind) {
886886
case ObjCProperty:
887887
case MSProperty:
888888
case HLSLBuffer:
889+
case HLSLRootSignature:
889890
return IDNS_Ordinary;
890891
case Label:
891892
return IDNS_Label;

0 commit comments

Comments
 (0)