Skip to content

Commit d60ce48

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 9388aae commit d60ce48

File tree

4 files changed

+93
-0
lines changed

4 files changed

+93
-0
lines changed

clang/include/clang/Parse/ParseHLSLRootSignature.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
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

@@ -93,6 +95,27 @@ class RootSignatureLexer {
9395
}
9496
};
9597

98+
class RootSignatureParser {
99+
public:
100+
RootSignatureParser(SmallVector<llvm::hlsl::rootsig::RootElement> &Elements,
101+
RootSignatureLexer &Lexer, DiagnosticsEngine &Diags);
102+
103+
/// Iterates over the provided tokens and constructs the in-memory
104+
/// representations of the RootElements.
105+
///
106+
/// The return value denotes if there was a failure and the method will
107+
/// return on the first encountered failure, or, return false if it
108+
/// can sucessfully reach the end of the tokens.
109+
bool Parse();
110+
111+
private:
112+
SmallVector<llvm::hlsl::rootsig::RootElement> &Elements;
113+
RootSignatureLexer &Lexer;
114+
DiagnosticsEngine &Diags;
115+
116+
RootSignatureToken CurToken;
117+
};
118+
96119
} // namespace hlsl
97120
} // namespace clang
98121

clang/lib/Parse/ParseHLSLRootSignature.cpp

Lines changed: 17 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::rootsig;
4+
35
namespace clang {
46
namespace hlsl {
57

@@ -168,5 +170,20 @@ std::optional<RootSignatureToken> RootSignatureLexer::PeekNextToken() {
168170
return Result;
169171
}
170172

173+
// Parser Definitions
174+
175+
RootSignatureParser::RootSignatureParser(SmallVector<RootElement> &Elements,
176+
RootSignatureLexer &Lexer,
177+
DiagnosticsEngine &Diags)
178+
: Elements(Elements), Lexer(Lexer), Diags(Diags) {}
179+
180+
bool RootSignatureParser::Parse() {
181+
// Handle edge-case of empty RootSignature()
182+
if (Lexer.EndOfBuffer())
183+
return false;
184+
185+
return true;
186+
}
187+
171188
} // namespace hlsl
172189
} // namespace clang

clang/unittests/Parse/ParseHLSLRootSignatureTest.cpp

Lines changed: 23 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::rootsig;
2728

2829
namespace {
2930

@@ -312,4 +313,26 @@ TEST_F(ParseHLSLRootSignatureTest, InvalidLexRegNumberTest) {
312313
ASSERT_FALSE(Consumer->IsSatisfied());
313314
}
314315

316+
// Valid Parser Tests
317+
318+
TEST_F(ParseHLSLRootSignatureTest, ValidParseEmptyTest) {
319+
const llvm::StringLiteral Source = R"cc()cc";
320+
321+
TrivialModuleLoader ModLoader;
322+
auto PP = CreatePP(Source, ModLoader);
323+
auto TokLoc = SourceLocation();
324+
325+
hlsl::RootSignatureLexer Lexer(Source, TokLoc, *PP);
326+
SmallVector<RootElement> Elements;
327+
hlsl::RootSignatureParser Parser(Elements, Lexer, Diags);
328+
329+
// Test no diagnostics produced
330+
Consumer->SetNoDiag();
331+
332+
ASSERT_FALSE(Parser.Parse());
333+
ASSERT_EQ((int)Elements.size(), 0);
334+
335+
ASSERT_TRUE(Consumer->IsSatisfied());
336+
}
337+
315338
} // 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 rootsig {
22+
23+
// Models RootElement
24+
using RootElement = std::variant<std::monostate>;
25+
26+
} // namespace rootsig
27+
} // namespace hlsl
28+
} // namespace llvm
29+
30+
#endif // LLVM_FRONTEND_HLSL_HLSLROOTSIGNATURE_H

0 commit comments

Comments
 (0)