Skip to content

Commit 1abebd3

Browse files
initial root signature prototype
1 parent 3761319 commit 1abebd3

File tree

7 files changed

+200
-128
lines changed

7 files changed

+200
-128
lines changed

clang/include/clang/Parse/Parser.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3055,9 +3055,6 @@ class Parser : public CodeCompletionHandler {
30553055

30563056
Decl *ParseHLSLBuffer(SourceLocation &DeclEnd);
30573057

3058-
Attr *ParseHLSLRootSignature(StringRef Signature, ParsedAttributes &Attrs,
3059-
SourceLocation *EndLoc = nullptr);
3060-
30613058
void MaybeParseMicrosoftAttributes(ParsedAttributes &Attrs) {
30623059
if ((getLangOpts().MicrosoftExt || getLangOpts().HLSL) &&
30633060
Tok.is(tok::l_square)) {
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
//===--- HLSLRootSignature.h - HLSL Sema Source ------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// This file defines the RootSignatureParsing interface.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
#ifndef CLANG_SEMA_HLSLEXTERNALSEMASOURCE_H
13+
#define CLANG_SEMA_HLSLEXTERNALSEMASOURCE_H
14+
15+
#include "llvm/ADT/DenseMap.h"
16+
17+
#include "clang/Sema/ExternalSemaSource.h"
18+
19+
namespace clang {
20+
21+
struct RSTknInfo {
22+
enum RSTok {
23+
RootFlags,
24+
RootConstants,
25+
RootCBV,
26+
RootSRV,
27+
RootUAV,
28+
DescriptorTable,
29+
StaticSampler,
30+
Number,
31+
Character,
32+
RootFlag,
33+
EoF
34+
};
35+
36+
RSTknInfo() {}
37+
38+
RSTok Kind = RSTok::EoF;
39+
StringRef Text;
40+
};
41+
42+
class RootSignaturParser {
43+
44+
public:
45+
RootSignaturParser(StringRef Signature) : Signature(Signature) {}
46+
47+
void ParseRootDefinition();
48+
49+
private:
50+
StringRef Signature;
51+
52+
RSTknInfo CurTok;
53+
std::string IdentifierStr;
54+
55+
RSTknInfo gettok();
56+
57+
char nextChar() {
58+
char resp = Signature[0];
59+
Signature = Signature.drop_front(1);
60+
return resp;
61+
}
62+
63+
char curChar() { return Signature[0]; }
64+
65+
RSTknInfo getNextToken() { return CurTok = gettok(); }
66+
67+
void ParseRootFlag();
68+
};
69+
70+
} // namespace clang
71+
#endif // CLANG_SEMA_HLSLEXTERNALSEMASOURCE_H

clang/lib/Parse/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ add_clang_library(clangParse
1414
ParseExpr.cpp
1515
ParseExprCXX.cpp
1616
ParseHLSL.cpp
17-
ParseHLSLRootSignature.cpp
1817
ParseInit.cpp
1918
ParseObjc.cpp
2019
ParseOpenMP.cpp

clang/lib/Parse/ParseHLSLRootSignature.cpp

Lines changed: 0 additions & 124 deletions
This file was deleted.

clang/lib/Sema/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ add_clang_library(clangSema
5656
SemaExprObjC.cpp
5757
SemaFixItUtils.cpp
5858
SemaFunctionEffects.cpp
59+
ParseHLSLRootSignature.cpp
5960
SemaHLSL.cpp
6061
SemaHexagon.cpp
6162
SemaInit.cpp
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
#include "clang/AST/Attr.h"
2+
#include "clang/Sema/HLSLRootSignature.h"
3+
#include "clang/Sema/ParsedAttr.h"
4+
#include "llvm/ADT/StringRef.h"
5+
#include "llvm/ADT/StringSwitch.h"
6+
#include "llvm/Support/ErrorHandling.h"
7+
8+
using namespace clang;
9+
using namespace llvm::hlsl;
10+
11+
void RootSignaturParser::ParseRootDefinition() {
12+
do {
13+
getNextToken();
14+
15+
switch (CurTok.Kind) {
16+
17+
case RSTknInfo::RootFlags:
18+
getNextToken();
19+
assert(CurTok.Kind == RSTknInfo::Character && CurTok.Text == "(" &&
20+
"Missing tkn in root signature");
21+
break;
22+
ParseRootFlag();
23+
case RSTknInfo::RootCBV:
24+
default:
25+
llvm_unreachable("Root Element still not suported");
26+
}
27+
} while (CurTok.Kind != RSTknInfo::EoF);
28+
}
29+
30+
RSTknInfo RootSignaturParser::gettok() {
31+
char LastChar = ' ';
32+
RSTknInfo Response;
33+
34+
while (isspace(LastChar)) {
35+
LastChar = nextChar();
36+
}
37+
38+
if (isalpha(LastChar)) {
39+
IdentifierStr = LastChar;
40+
while (isalnum(curChar()) || curChar() == '_') {
41+
LastChar = nextChar();
42+
IdentifierStr += LastChar;
43+
}
44+
45+
RSTknInfo::RSTok Tok =
46+
llvm::StringSwitch<RSTknInfo::RSTok>(IdentifierStr)
47+
.Case("RootFlags", RSTknInfo::RootFlags)
48+
.Case("RootConstants", RSTknInfo::RootConstants)
49+
.Case("RootCBV", RSTknInfo::RootCBV)
50+
.Case("RootSRV", RSTknInfo::RootSRV)
51+
.Case("RootUAV", RSTknInfo::RootUAV)
52+
.Case("DescriptorTable", RSTknInfo::DescriptorTable)
53+
.Case("StaticSampler", RSTknInfo::StaticSampler)
54+
.Case("DENY_VERTEX_SHADER_ROOT_ACCESS", RSTknInfo::RootFlag)
55+
.Case("ALLOW_INPUT_ASSEMBLER_INPUT_LAYOUT", RSTknInfo::RootFlag)
56+
.Case("DENY_HULL_SHADER_ROOT_ACCESS", RSTknInfo::RootFlag)
57+
.Case("DENY_DOMAIN_SHADER_ROOT_ACCESS", RSTknInfo::RootFlag)
58+
.Case("DENY_GEOMETRY_SHADER_ROOT_ACCESS", RSTknInfo::RootFlag)
59+
.Case("DENY_PIXEL_SHADER_ROOT_ACCESS", RSTknInfo::RootFlag)
60+
.Case("DENY_AMPLIFICATION_SHADER_ROOT_ACCESS", RSTknInfo::RootFlag)
61+
.Case("DENY_MESH_SHADER_ROOT_ACCESS", RSTknInfo::RootFlag)
62+
.Case("ALLOW_STREAM_OUTPUT", RSTknInfo::RootFlag)
63+
.Case("LOCAL_ROOT_SIGNATURE", RSTknInfo::RootFlag)
64+
.Case("CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED", RSTknInfo::RootFlag)
65+
.Case("SAMPLER_HEAP_DIRECTLY_INDEXED", RSTknInfo::RootFlag)
66+
.Case("AllowLowTierReservedHwCbLimit", RSTknInfo::RootFlag)
67+
.Default(RSTknInfo::EoF);
68+
69+
assert(Tok != RSTknInfo::EoF && "invalid string in ROOT SIGNATURE");
70+
71+
Response.Kind = Tok;
72+
Response.Text = StringRef(IdentifierStr);
73+
return Response;
74+
}
75+
76+
if (isdigit(LastChar)) {
77+
std::string NumStr;
78+
79+
do {
80+
NumStr += LastChar;
81+
LastChar = nextChar();
82+
} while (isdigit(LastChar));
83+
84+
Response.Kind = RSTknInfo::Number;
85+
Response.Text = StringRef(IdentifierStr);
86+
return Response;
87+
}
88+
89+
if (LastChar == EOF) {
90+
Response.Kind = RSTknInfo::EoF;
91+
return Response;
92+
}
93+
94+
Response.Kind = RSTknInfo::Character;
95+
Response.Text = StringRef(std::string(1, LastChar));
96+
return Response;
97+
}
98+
99+
void RootSignaturParser::ParseRootFlag() {
100+
101+
do {
102+
getNextToken();
103+
104+
if (CurTok.Kind == RSTknInfo::RootFlag) {
105+
if (CurTok.Text == "DENY_VERTEX_SHADER_ROOT_ACCESS") {
106+
} else if (CurTok.Text == "ALLOW_INPUT_ASSEMBLER_INPUT_LAYOUT") {
107+
} else if (CurTok.Text == "DENY_HULL_SHADER_ROOT_ACCESS") {
108+
} else if (CurTok.Text == "DENY_DOMAIN_SHADER_ROOT_ACCESS") {
109+
} else if (CurTok.Text == "DENY_GEOMETRY_SHADER_ROOT_ACCESS") {
110+
} else if (CurTok.Text == "DENY_PIXEL_SHADER_ROOT_ACCESS") {
111+
} else if (CurTok.Text == "DENY_AMPLIFICATION_SHADER_ROOT_ACCESS") {
112+
} else if (CurTok.Text == "DENY_MESH_SHADER_ROOT_ACCESS") {
113+
} else if (CurTok.Text == "ALLOW_STREAM_OUTPUT") {
114+
} else if (CurTok.Text == "LOCAL_ROOT_SIGNATURE") {
115+
} else if (CurTok.Text == "CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED") {
116+
} else if (CurTok.Text == "SAMPLER_HEAP_DIRECTLY_INDEXED") {
117+
} else if (CurTok.Text == "AllowLowTierReservedHwCbLimit") {
118+
}
119+
}
120+
121+
} while (curChar() == ',');
122+
}

clang/lib/Sema/SemaHLSL.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
#include "clang/Basic/LLVM.h"
2525
#include "clang/Basic/SourceLocation.h"
2626
#include "clang/Basic/TargetInfo.h"
27+
#include "clang/Parse/Parser.h"
28+
#include "clang/Sema/HLSLRootSignature.h"
2729
#include "clang/Sema/Initialization.h"
2830
#include "clang/Sema/ParsedAttr.h"
2931
#include "clang/Sema/Sema.h"
@@ -709,6 +711,10 @@ void SemaHLSL::handleHLSLRootSignature(Decl *D, const ParsedAttr &AL) {
709711
StringRef Signature;
710712
if (!SemaRef.checkStringLiteralArgumentAttr(AL, 0, Signature))
711713
return;
714+
715+
RootSignaturParser Parser(Signature);
716+
717+
Parser.ParseRootDefinition();
712718
}
713719

714720
void SemaHLSL::handleWaveSizeAttr(Decl *D, const ParsedAttr &AL) {

0 commit comments

Comments
 (0)