Skip to content

Commit 8ecb037

Browse files
committed
[HLSL] Define the HLSLRootSignature Attr
- Defines HLSLRootSignature Attr in `Attr.td` - Define and implement handleHLSLRootSignature in `SemaHLSL` - Adds sample test case to show AST Node is generated in `RootSignatures-AST.hlsl` This commit will "hook-up" the seperately defined RootSignature parser and invoke it to create the RootElements, then store them on the ASTContext and finally store the reference to the Elements in RootSignatureAttr
1 parent b643259 commit 8ecb037

File tree

7 files changed

+93
-0
lines changed

7 files changed

+93
-0
lines changed

clang/include/clang/AST/Attr.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "clang/Basic/SourceLocation.h"
2727
#include "clang/Support/Compiler.h"
2828
#include "llvm/Frontend/HLSL/HLSLResource.h"
29+
#include "llvm/Frontend/HLSL/HLSLRootSignature.h"
2930
#include "llvm/Support/CodeGen.h"
3031
#include "llvm/Support/ErrorHandling.h"
3132
#include "llvm/Support/VersionTuple.h"

clang/include/clang/Basic/Attr.td

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4643,6 +4643,26 @@ def Error : InheritableAttr {
46434643
let Documentation = [ErrorAttrDocs];
46444644
}
46454645

4646+
/// HLSL Root Signature Attribute
4647+
def HLSLRootSignature : Attr {
4648+
/// [RootSignature(Signature)]
4649+
let Spellings = [Microsoft<"RootSignature">];
4650+
let Args = [StringArgument<"Signature">];
4651+
let Subjects = SubjectList<[Function],
4652+
ErrorDiag, "'function'">;
4653+
let LangOpts = [HLSL];
4654+
let Documentation = [HLSLRootSignatureDocs];
4655+
let AdditionalMembers = [{
4656+
private:
4657+
ArrayRef<llvm::hlsl::root_signature::RootElement> RootElements;
4658+
public:
4659+
void setElements(ArrayRef<llvm::hlsl::root_signature::RootElement> Elements) {
4660+
RootElements = Elements;
4661+
}
4662+
auto getElements() const { return RootElements; }
4663+
}];
4664+
}
4665+
46464666
def HLSLNumThreads: InheritableAttr {
46474667
let Spellings = [Microsoft<"numthreads">];
46484668
let Args = [IntArgument<"X">, IntArgument<"Y">, IntArgument<"Z">];

clang/include/clang/Basic/AttrDocs.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7783,6 +7783,10 @@ and https://microsoft.github.io/hlsl-specs/proposals/0013-wave-size-range.html
77837783
}];
77847784
}
77857785

7786+
def HLSLRootSignatureDocs : Documentation {
7787+
let Category = DocCatUndocumented;
7788+
}
7789+
77867790
def NumThreadsDocs : Documentation {
77877791
let Category = DocCatFunction;
77887792
let Content = [{

clang/include/clang/Sema/SemaHLSL.h

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

119+
void handleRootSignatureAttr(Decl *D, const ParsedAttr &AL);
119120
void handleNumThreadsAttr(Decl *D, const ParsedAttr &AL);
120121
void handleWaveSizeAttr(Decl *D, const ParsedAttr &AL);
121122
void handleSV_DispatchThreadIDAttr(Decl *D, const ParsedAttr &AL);

clang/lib/Sema/SemaDeclAttr.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7149,6 +7149,9 @@ ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D, const ParsedAttr &AL,
71497149
break;
71507150

71517151
// HLSL attributes:
7152+
case ParsedAttr::AT_HLSLRootSignature:
7153+
S.HLSL().handleRootSignatureAttr(D, AL);
7154+
break;
71527155
case ParsedAttr::AT_HLSLNumThreads:
71537156
S.HLSL().handleNumThreadsAttr(D, AL);
71547157
break;

clang/lib/Sema/SemaHLSL.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "clang/Basic/LLVM.h"
2525
#include "clang/Basic/SourceLocation.h"
2626
#include "clang/Basic/TargetInfo.h"
27+
#include "clang/Parse/ParseHLSLRootSignature.h"
2728
#include "clang/Sema/Initialization.h"
2829
#include "clang/Sema/ParsedAttr.h"
2930
#include "clang/Sema/Sema.h"
@@ -647,6 +648,41 @@ void SemaHLSL::emitLogicalOperatorFixIt(Expr *LHS, Expr *RHS,
647648
<< NewFnName << FixItHint::CreateReplacement(FullRange, OS.str());
648649
}
649650

651+
void SemaHLSL::handleRootSignatureAttr(Decl *D, const ParsedAttr &AL) {
652+
using namespace llvm::hlsl::root_signature;
653+
654+
if (AL.getNumArgs() != 1)
655+
return;
656+
657+
StringRef Signature;
658+
if (!SemaRef.checkStringLiteralArgumentAttr(AL, 0, Signature))
659+
return;
660+
661+
SourceLocation Loc = AL.getArgAsExpr(0)->getExprLoc();
662+
// FIXME: pass down below to lexer when fp is supported
663+
// llvm::RoundingMode RM = SemaRef.CurFPFeatures.getRoundingMode();
664+
SmallVector<RootSignatureToken> Tokens;
665+
RootSignatureLexer Lexer(Signature, Loc, SemaRef.getPreprocessor());
666+
if (Lexer.Lex(Tokens))
667+
return;
668+
669+
SmallVector<RootElement> Elements;
670+
RootSignatureParser Parser(Elements, Tokens);
671+
if (Parser.Parse())
672+
return;
673+
674+
unsigned N = Elements.size();
675+
auto RootElements =
676+
MutableArrayRef<RootElement>(::new (getASTContext()) RootElement[N], N);
677+
for (unsigned I = 0; I < N; ++I)
678+
RootElements[I] = Elements[I];
679+
680+
auto *Result = ::new (getASTContext())
681+
HLSLRootSignatureAttr(getASTContext(), AL, Signature);
682+
Result->setElements(ArrayRef<RootElement>(RootElements));
683+
D->addAttr(Result);
684+
}
685+
650686
void SemaHLSL::handleNumThreadsAttr(Decl *D, const ParsedAttr &AL) {
651687
llvm::VersionTuple SMVersion =
652688
getASTContext().getTargetInfo().getTriple().getOSVersion();
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.0-library -ast-dump \
2+
// RUN: -disable-llvm-passes -o - %s | FileCheck %s
3+
4+
// This test ensures that the sample root signature is parsed without error and
5+
// the Attr AST Node is created succesfully. If an invalid root signature was
6+
// passed in then we would exit out of Sema before the Attr is created.
7+
8+
#define SampleRS \
9+
"DescriptorTable( " \
10+
" CBV(b1), " \
11+
" SRV(t1, numDescriptors = 8, " \
12+
" flags = DESCRIPTORS_VOLATILE), " \
13+
" UAV(u1, numDescriptors = 0, " \
14+
" flags = DESCRIPTORS_VOLATILE) " \
15+
"), " \
16+
"DescriptorTable(Sampler(s0, numDescriptors = 4, space = 1))"
17+
18+
// CHECK: HLSLRootSignatureAttr 0x{{[0-9A-Fa-f]+}} <line:{{[0-9]+}}:{{[0-9]+}}, col:{{[0-9]+}}>
19+
// CHECK-SAME: "DescriptorTable(
20+
// CHECK-SAME: CBV(b1),
21+
// CHECK-SAME: SRV(t1, numDescriptors = 8,
22+
// CHECK-SAME: flags = DESCRIPTORS_VOLATILE),
23+
// CHECK-SAME: UAV(u1, numDescriptors = 0,
24+
// CHECK-SAME: flags = DESCRIPTORS_VOLATILE)
25+
// CHECK-SAME: ),
26+
// CHECK-SAME: DescriptorTable(Sampler(s0, numDescriptors = 4, space = 1))"
27+
[RootSignature(SampleRS)]
28+
void main() {}

0 commit comments

Comments
 (0)