Skip to content

Commit 650d65b

Browse files
committed
[HLSL][RootSignature] Handle an empty root signature
- Define the Parser struct - Model RootElements as a variant of the different types - Create a basic test case for unit testing
1 parent 392d5b0 commit 650d65b

File tree

4 files changed

+101
-0
lines changed

4 files changed

+101
-0
lines changed

clang/include/clang/Parse/ParseHLSLRootSignature.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,13 @@
2222
#include "llvm/ADT/StringRef.h"
2323
#include "llvm/ADT/StringSwitch.h"
2424

25+
#include "llvm/Frontend/HLSL/HLSLRootSignature.h"
26+
2527
namespace clang {
2628
namespace hlsl {
2729

30+
namespace rs = llvm::hlsl::root_signature;
31+
2832
struct RootSignatureToken {
2933
enum Kind {
3034
#define TOK(X) X,
@@ -80,6 +84,28 @@ class RootSignatureLexer {
8084
}
8185
};
8286

87+
class RootSignatureParser {
88+
public:
89+
RootSignatureParser(SmallVector<rs::RootElement> &Elements,
90+
const SmallVector<RootSignatureToken> &Tokens,
91+
DiagnosticsEngine &Diags);
92+
93+
// Iterates over the provided tokens and constructs the in-memory
94+
// representations of the RootElements.
95+
//
96+
// The return value denotes if there was a failure and the method will
97+
// return on the first encountered failure, or, return false if it
98+
// can sucessfully reach the end of the tokens.
99+
bool Parse();
100+
101+
private:
102+
SmallVector<rs::RootElement> &Elements;
103+
SmallVector<RootSignatureToken>::const_iterator CurTok;
104+
SmallVector<RootSignatureToken>::const_iterator LastTok;
105+
106+
DiagnosticsEngine &Diags;
107+
};
108+
83109
} // namespace hlsl
84110
} // namespace clang
85111

clang/lib/Parse/ParseHLSLRootSignature.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include "clang/Parse/ParseHLSLRootSignature.h"
22

3+
using namespace llvm::hlsl::root_signature;
4+
35
namespace clang {
46
namespace hlsl {
57

@@ -169,5 +171,22 @@ bool RootSignatureLexer::LexToken(RootSignatureToken &Result) {
169171
return false;
170172
}
171173

174+
// Parser Definitions
175+
176+
RootSignatureParser::RootSignatureParser(
177+
SmallVector<RootElement> &Elements,
178+
const SmallVector<RootSignatureToken> &Tokens, DiagnosticsEngine &Diags)
179+
: Elements(Elements), Diags(Diags) {
180+
CurTok = Tokens.begin();
181+
LastTok = Tokens.end();
182+
}
183+
184+
bool RootSignatureParser::Parse() {
185+
// Handle edge-case of empty RootSignature()
186+
if (CurTok == LastTok)
187+
return false;
188+
189+
return true;
190+
}
172191
} // namespace hlsl
173192
} // namespace clang

clang/unittests/Parse/ParseHLSLRootSignatureTest.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "gtest/gtest.h"
2525

2626
using namespace clang;
27+
using namespace llvm::hlsl::root_signature;
2728

2829
namespace {
2930

@@ -279,4 +280,29 @@ TEST_F(ParseHLSLRootSignatureTest, InvalidLexIdentifierTest) {
279280
ASSERT_TRUE(Consumer->IsSatisfied());
280281
}
281282

283+
// Valid Parser Tests
284+
285+
TEST_F(ParseHLSLRootSignatureTest, ValidParseEmptyTest) {
286+
const llvm::StringLiteral Source = R"cc()cc";
287+
288+
TrivialModuleLoader ModLoader;
289+
auto PP = CreatePP(Source, ModLoader);
290+
auto TokLoc = SourceLocation();
291+
292+
// Test no diagnostics produced
293+
Consumer->SetNoDiag();
294+
hlsl::RootSignatureLexer Lexer(Source, TokLoc, *PP);
295+
296+
SmallVector<hlsl::RootSignatureToken> Tokens;
297+
ASSERT_FALSE(Lexer.Lex(Tokens));
298+
299+
SmallVector<RootElement> Elements;
300+
hlsl::RootSignatureParser Parser(Elements, Tokens, Diags);
301+
302+
ASSERT_FALSE(Parser.Parse());
303+
ASSERT_EQ((int)Elements.size(), 0);
304+
305+
ASSERT_TRUE(Consumer->IsSatisfied());
306+
}
307+
282308
} // anonymous namespace
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//===- HLSLRootSignature.h - HLSL Root Signature helper objects -----------===//
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+
/// \file This file contains helper objects for working with HLSL Root
10+
/// Signatures.
11+
///
12+
//===----------------------------------------------------------------------===//
13+
14+
#ifndef LLVM_FRONTEND_HLSL_HLSLROOTSIGNATURE_H
15+
#define LLVM_FRONTEND_HLSL_HLSLROOTSIGNATURE_H
16+
17+
#include <variant>
18+
19+
namespace llvm {
20+
namespace hlsl {
21+
namespace root_signature {
22+
23+
// Models RootElement
24+
using RootElement = std::variant<std::monostate>;
25+
26+
} // namespace root_signature
27+
} // namespace hlsl
28+
} // namespace llvm
29+
30+
#endif // LLVM_FRONTEND_HLSL_HLSLROOTSIGNATURE_H

0 commit comments

Comments
 (0)