Skip to content

Commit 0ab73ae

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 9b74dce commit 0ab73ae

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
@@ -4698,6 +4698,17 @@ def Error : InheritableAttr {
46984698
let Documentation = [ErrorAttrDocs];
46994699
}
47004700

4701+
/// HLSL Root Signature Attribute
4702+
def RootSignature : Attr {
4703+
/// [RootSignature(Signature)]
4704+
let Spellings = [Microsoft<"RootSignature">];
4705+
let Args = [IdentifierArgument<"Signature">];
4706+
let Subjects = SubjectList<[Function],
4707+
ErrorDiag, "'function'">;
4708+
let LangOpts = [HLSL];
4709+
let Documentation = [RootSignatureDocs];
4710+
}
4711+
47014712
def HLSLNumThreads: InheritableAttr {
47024713
let Spellings = [Microsoft<"numthreads">];
47034714
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
@@ -8184,6 +8184,17 @@ and https://microsoft.github.io/hlsl-specs/proposals/0013-wave-size-range.html
81848184
}];
81858185
}
81868186

8187+
def RootSignatureDocs : Documentation {
8188+
let Category = DocCatFunction;
8189+
let Content = [{
8190+
The ``RootSignature`` attribute applies to HLSL entry functions to define what
8191+
types of resources are bound to the graphics pipeline.
8192+
8193+
For details about the use and specification of Root Signatures please see here:
8194+
https://learn.microsoft.com/en-us/windows/win32/direct3d12/root-signatures
8195+
}];
8196+
}
8197+
81878198
def NumThreadsDocs : Documentation {
81888199
let Category = DocCatFunction;
81898200
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
@@ -3070,6 +3070,7 @@ class Parser : public CodeCompletionHandler {
30703070
}
30713071
}
30723072
void ParseMicrosoftUuidAttributeArgs(ParsedAttributes &Attrs);
3073+
void ParseMicrosoftRootSignatureAttributeArgs(ParsedAttributes &Attrs);
30733074
void ParseMicrosoftAttributes(ParsedAttributes &Attrs);
30743075
bool MaybeParseMicrosoftDeclSpecs(ParsedAttributes &Attrs) {
30753076
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
@@ -118,6 +118,7 @@ class SemaHLSL : public SemaBase {
118118
bool IsCompAssign);
119119
void emitLogicalOperatorFixIt(Expr *LHS, Expr *RHS, BinaryOperatorKind Opc);
120120

121+
void handleRootSignatureAttr(Decl *D, const ParsedAttr &AL);
121122
void handleNumThreadsAttr(Decl *D, const ParsedAttr &AL);
122123
void handleWaveSizeAttr(Decl *D, const ParsedAttr &AL);
123124
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
@@ -5839,6 +5839,31 @@ bool HLSLBufferDecl::buffer_decls_empty() {
58395839
return DefaultBufferDecls.empty() && decls_empty();
58405840
}
58415841

5842+
//===----------------------------------------------------------------------===//
5843+
// HLSLRootSignatureDecl Implementation
5844+
//===----------------------------------------------------------------------===//
5845+
5846+
HLSLRootSignatureDecl::HLSLRootSignatureDecl(
5847+
DeclContext *DC, SourceLocation Loc, IdentifierInfo *ID,
5848+
ArrayRef<llvm::hlsl::rootsig::RootElement> RootElements)
5849+
: NamedDecl(Decl::Kind::HLSLRootSignature, DC, Loc, DeclarationName(ID)),
5850+
RootElements(RootElements) {}
5851+
5852+
HLSLRootSignatureDecl *HLSLRootSignatureDecl::Create(
5853+
ASTContext &C, DeclContext *DC, SourceLocation Loc, IdentifierInfo *ID,
5854+
ArrayRef<llvm::hlsl::rootsig::RootElement> RootElements) {
5855+
HLSLRootSignatureDecl *Result =
5856+
new (C, DC) HLSLRootSignatureDecl(DC, Loc, ID, RootElements);
5857+
return Result;
5858+
}
5859+
5860+
HLSLRootSignatureDecl *
5861+
HLSLRootSignatureDecl::CreateDeserialized(ASTContext &C, GlobalDeclID ID) {
5862+
HLSLRootSignatureDecl *Result =
5863+
new (C, ID) HLSLRootSignatureDecl(nullptr, SourceLocation(), nullptr, {});
5864+
return Result;
5865+
}
5866+
58425867
//===----------------------------------------------------------------------===//
58435868
// ImportDecl Implementation
58445869
//===----------------------------------------------------------------------===//

clang/lib/AST/DeclBase.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -882,6 +882,7 @@ unsigned Decl::getIdentifierNamespaceForKind(Kind DeclKind) {
882882
case ObjCProperty:
883883
case MSProperty:
884884
case HLSLBuffer:
885+
case HLSLRootSignature:
885886
return IDNS_Ordinary;
886887
case Label:
887888
return IDNS_Label;

0 commit comments

Comments
 (0)