Skip to content

Commit dd3d7cf

Browse files
authored
[HLSL][RootSignature] Define and integrate rootsig clang attr and decl (llvm#137690)
- Defines a new declaration node `HLSLRootSignature` in `DeclNodes.td` that will consist of a `TrailingObjects` of the 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 declaration as above - Integrate the `HLSLRootSignatureParser` to construct the decl node in `ParseMicrosoftAttributes` and then attach the parsed attr with an identifier to the entry point function declaration. - Defines the various required declaration methods - Add testing that the declaration and reference attr are created correctly, and some syntactical error tests. 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 separate root signature decl. In contrast, by defining them separately 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 root signature attributes. Having it located directly as a declaration might also prove advantageous when we consider root signature libraries. Resolves llvm#119011
1 parent fb9b43a commit dd3d7cf

File tree

23 files changed

+347
-0
lines changed

23 files changed

+347
-0
lines changed

clang/include/clang/AST/Decl.h

Lines changed: 37 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,42 @@ class HLSLBufferDecl final : public NamedDecl, public DeclContext {
51785179
friend class ASTDeclWriter;
51795180
};
51805181

5182+
class HLSLRootSignatureDecl final
5183+
: public NamedDecl,
5184+
private llvm::TrailingObjects<HLSLRootSignatureDecl,
5185+
llvm::hlsl::rootsig::RootElement> {
5186+
friend TrailingObjects;
5187+
5188+
unsigned NumElems;
5189+
5190+
llvm::hlsl::rootsig::RootElement *getElems() {
5191+
return getTrailingObjects<llvm::hlsl::rootsig::RootElement>();
5192+
}
5193+
5194+
const llvm::hlsl::rootsig::RootElement *getElems() const {
5195+
return getTrailingObjects<llvm::hlsl::rootsig::RootElement>();
5196+
}
5197+
5198+
HLSLRootSignatureDecl(DeclContext *DC, SourceLocation Loc, IdentifierInfo *ID,
5199+
unsigned NumElems);
5200+
5201+
public:
5202+
static HLSLRootSignatureDecl *
5203+
Create(ASTContext &C, DeclContext *DC, SourceLocation Loc, IdentifierInfo *ID,
5204+
ArrayRef<llvm::hlsl::rootsig::RootElement> RootElements);
5205+
5206+
static HLSLRootSignatureDecl *CreateDeserialized(ASTContext &C,
5207+
GlobalDeclID ID);
5208+
5209+
ArrayRef<llvm::hlsl::rootsig::RootElement> getRootElements() const {
5210+
return {getElems(), NumElems};
5211+
}
5212+
5213+
// Implement isa/cast/dyncast/etc.
5214+
static bool classof(const Decl *D) { return classofKind(D->getKind()); }
5215+
static bool classofKind(Kind K) { return K == HLSLRootSignature; }
5216+
};
5217+
51815218
/// Insertion operator for diagnostics. This allows sending NamedDecl's
51825219
/// into a diagnostic with <<.
51835220
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/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ set(LLVM_LINK_COMPONENTS
22
BinaryFormat
33
Core
44
FrontendOpenMP
5+
FrontendHLSL
56
Support
67
TargetParser
78
)

clang/lib/AST/Decl.cpp

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

5850+
//===----------------------------------------------------------------------===//
5851+
// HLSLRootSignatureDecl Implementation
5852+
//===----------------------------------------------------------------------===//
5853+
5854+
HLSLRootSignatureDecl::HLSLRootSignatureDecl(DeclContext *DC,
5855+
SourceLocation Loc,
5856+
IdentifierInfo *ID,
5857+
unsigned NumElems)
5858+
: NamedDecl(Decl::Kind::HLSLRootSignature, DC, Loc, DeclarationName(ID)),
5859+
NumElems(NumElems) {}
5860+
5861+
HLSLRootSignatureDecl *HLSLRootSignatureDecl::Create(
5862+
ASTContext &C, DeclContext *DC, SourceLocation Loc, IdentifierInfo *ID,
5863+
ArrayRef<llvm::hlsl::rootsig::RootElement> RootElements) {
5864+
HLSLRootSignatureDecl *RSDecl =
5865+
new (C, DC,
5866+
additionalSizeToAlloc<llvm::hlsl::rootsig::RootElement>(
5867+
RootElements.size()))
5868+
HLSLRootSignatureDecl(DC, Loc, ID, RootElements.size());
5869+
auto *StoredElems = RSDecl->getElems();
5870+
std::uninitialized_copy(RootElements.begin(), RootElements.end(),
5871+
StoredElems);
5872+
return RSDecl;
5873+
}
5874+
5875+
HLSLRootSignatureDecl *
5876+
HLSLRootSignatureDecl::CreateDeserialized(ASTContext &C, GlobalDeclID ID) {
5877+
HLSLRootSignatureDecl *Result = new (C, ID)
5878+
HLSLRootSignatureDecl(nullptr, SourceLocation(), nullptr, /*NumElems=*/0);
5879+
return Result;
5880+
}
5881+
58505882
//===----------------------------------------------------------------------===//
58515883
// ImportDecl Implementation
58525884
//===----------------------------------------------------------------------===//

0 commit comments

Comments
 (0)