diff --git a/clang/include/clang/Basic/DiagnosticParseKinds.td b/clang/include/clang/Basic/DiagnosticParseKinds.td index c513dab810d1f..743d875ad3f17 100644 --- a/clang/include/clang/Basic/DiagnosticParseKinds.td +++ b/clang/include/clang/Basic/DiagnosticParseKinds.td @@ -1818,4 +1818,11 @@ def ext_hlsl_access_specifiers : ExtWarn< def err_hlsl_unsupported_component : Error<"invalid component '%0' used; expected 'x', 'y', 'z', or 'w'">; def err_hlsl_packoffset_invalid_reg : Error<"invalid resource class specifier '%0' for packoffset, expected 'c'">; +// HLSL Root Signature Parser Diagnostics +def err_hlsl_expected_param : Error<"expected parameter in %1(%0)">; +def err_hlsl_expected_value : Error<"expected value for %1 = %0">; +def err_hlsl_number_literal_overflow : Error<"integer literal is too large to be represented as a 32-bit %select{signed |}0 integer type">; +def err_hlsl_rootsig_repeat_param : Error<"specified the same parameter '%0' multiple times">; +def err_hlsl_rootsig_non_zero_flag : Error<"specified a non-zero integer as a flag">; + } // end of Parser diagnostics diff --git a/clang/include/clang/Lex/HLSLRootSignatureTokenKinds.def b/clang/include/clang/Lex/HLSLRootSignatureTokenKinds.def index e6df763920430..8ff6d25ebf143 100644 --- a/clang/include/clang/Lex/HLSLRootSignatureTokenKinds.def +++ b/clang/include/clang/Lex/HLSLRootSignatureTokenKinds.def @@ -14,16 +14,16 @@ //===----------------------------------------------------------------------===// #ifndef TOK -#define TOK(X) +#define TOK(X, SPELLING) #endif #ifndef PUNCTUATOR -#define PUNCTUATOR(X,Y) TOK(pu_ ## X) +#define PUNCTUATOR(X,Y) TOK(pu_ ## X, Y) #endif #ifndef KEYWORD -#define KEYWORD(X) TOK(kw_ ## X) +#define KEYWORD(X) TOK(kw_ ## X, #X) #endif #ifndef ENUM -#define ENUM(NAME, LIT) TOK(en_ ## NAME) +#define ENUM(NAME, LIT) TOK(en_ ## NAME, LIT) #endif // Defines the various types of enum @@ -49,15 +49,15 @@ #endif // General Tokens: -TOK(invalid) -TOK(end_of_stream) -TOK(int_literal) +TOK(invalid, "invalid identifier") +TOK(end_of_stream, "end of stream") +TOK(int_literal, "integer literal") // Register Tokens: -TOK(bReg) -TOK(tReg) -TOK(uReg) -TOK(sReg) +TOK(bReg, "b register") +TOK(tReg, "t register") +TOK(uReg, "u register") +TOK(sReg, "s register") // Punctuators: PUNCTUATOR(l_paren, '(') @@ -68,6 +68,9 @@ PUNCTUATOR(equal, '=') PUNCTUATOR(plus, '+') PUNCTUATOR(minus, '-') +// RootSignature Keyword is only used for diagnostics: +KEYWORD(RootSignature) + // RootElement Keywords: KEYWORD(DescriptorTable) diff --git a/clang/include/clang/Lex/LexHLSLRootSignature.h b/clang/include/clang/Lex/LexHLSLRootSignature.h index 21c44e0351d9e..b82237411b2ab 100644 --- a/clang/include/clang/Lex/LexHLSLRootSignature.h +++ b/clang/include/clang/Lex/LexHLSLRootSignature.h @@ -24,7 +24,7 @@ namespace hlsl { struct RootSignatureToken { enum Kind { -#define TOK(X) X, +#define TOK(X, SPELLING) X, #include "clang/Lex/HLSLRootSignatureTokenKinds.def" }; diff --git a/clang/include/clang/Parse/ParseHLSLRootSignature.h b/clang/include/clang/Parse/ParseHLSLRootSignature.h new file mode 100644 index 0000000000000..02818519fa186 --- /dev/null +++ b/clang/include/clang/Parse/ParseHLSLRootSignature.h @@ -0,0 +1,147 @@ +//===--- ParseHLSLRootSignature.h -------------------------------*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// +// This file defines the ParseHLSLRootSignature interface. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_CLANG_PARSE_PARSEHLSLROOTSIGNATURE_H +#define LLVM_CLANG_PARSE_PARSEHLSLROOTSIGNATURE_H + +#include "clang/Basic/DiagnosticParse.h" +#include "clang/Lex/LexHLSLRootSignature.h" +#include "clang/Lex/Preprocessor.h" + +#include "llvm/ADT/SmallVector.h" +#include "llvm/ADT/StringRef.h" + +#include "llvm/Frontend/HLSL/HLSLRootSignature.h" + +namespace clang { +namespace hlsl { + +class RootSignatureParser { +public: + RootSignatureParser(SmallVector &Elements, + RootSignatureLexer &Lexer, clang::Preprocessor &PP); + + /// Consumes tokens from the Lexer and constructs the in-memory + /// representations of the RootElements. Tokens are consumed until an + /// error is encountered or the end of the buffer. + /// + /// Returns true if a parsing error is encountered. + bool Parse(); + + DiagnosticsEngine &Diags() { return PP.getDiagnostics(); } + +private: + /// All private Parse.* methods follow a similar pattern: + /// - Each method will expect that the the next token is of a certain kind + /// and will invoke `ConsumeExpectedToken` + /// - As such, an error will be raised if the proceeding tokens are not + /// what is expected + /// - Therefore, it is the callers responsibility to ensure that you are + /// expecting the next element type. Or equivalently, the methods should not + /// be called as a way to try and parse an element + /// - All methods return true if a parsing error is encountered. It is the + /// callers responsibility to propogate this error up, or deal with it + /// otherwise + bool ParseRootElement(); + bool ParseDescriptorTable(); + bool ParseDescriptorTableClause(); + + /// It is helpful to have a generalized dispatch method so that when we need + /// to parse multiple optional parameters in any order, we can invoke this + /// method. + /// + /// Each unique ParamType is expected to define a custom Parse method. This + /// function will switch on the ParamType using std::visit and dispatch onto + /// the corresponding Parse method + bool ParseParam(llvm::hlsl::rootsig::ParamType Ref, TokenKind Context); + + /// Parses as many optional parameters as possible in any order + bool ParseOptionalParams( + llvm::SmallDenseMap &RefMap, + TokenKind Context); + + /// Use NumericLiteralParser to convert CurToken.NumSpelling into a unsigned + /// 32-bit integer + bool HandleUIntLiteral(uint32_t &X); + bool ParseRegister(llvm::hlsl::rootsig::Register *Reg, TokenKind Context); + bool ParseUInt(uint32_t *X, TokenKind Context); + bool ParseDescriptorRangeOffset(llvm::hlsl::rootsig::DescriptorRangeOffset *X, + TokenKind Context); + + /// Method for parsing any type of the ENUM defined token kinds (from + /// HLSLRootSignatureTokenKinds.def) + /// + /// EnumMap provides a mapping from the unique TokenKind to the in-memory + /// enum value + /// + /// If AllowZero is true, then the Enum is used as a flag and can also have + /// the value of '0' to denote no flag + template + bool ParseEnum(llvm::SmallDenseMap &EnumMap, + EnumType *Enum, TokenKind Context); + + /// Helper methods that define the mappings and invoke ParseEnum for + /// different enum types + bool ParseShaderVisibility(llvm::hlsl::rootsig::ShaderVisibility *Enum, + TokenKind Context); + + /// A wrapper method around ParseEnum that will parse an 'or' chain of + /// enums, with AllowZero = true + template + bool ParseFlags(llvm::SmallDenseMap &EnumMap, + FlagType *Enum, TokenKind Context); + + /// Helper methods that define the mappings and invoke ParseFlags for + /// different enum types + bool + ParseDescriptorRangeFlags(llvm::hlsl::rootsig::DescriptorRangeFlags *Enum, + TokenKind Context); + + /// Invoke the Lexer to consume a token and update CurToken with the result + void ConsumeNextToken() { CurToken = Lexer.ConsumeToken(); } + + /// Return true if the next token one of the expected kinds + bool PeekExpectedToken(TokenKind Expected); + bool PeekExpectedToken(ArrayRef AnyExpected); + + /// Consumes the next token and report an error if it is not of the expected + /// kind. + /// + /// Returns true if there was an error reported. + bool ConsumeExpectedToken(TokenKind Expected, + unsigned DiagID = diag::err_expected, + TokenKind Context = TokenKind::invalid); + bool ConsumeExpectedToken(ArrayRef AnyExpected, + unsigned DiagID = diag::err_expected, + TokenKind Context = TokenKind::invalid); + + /// Peek if the next token is of the expected kind and if it is then consume + /// it. + /// + /// Returns true if it successfully matches the expected kind and the token + /// was consumed. + bool TryConsumeExpectedToken(TokenKind Expected); + bool TryConsumeExpectedToken(ArrayRef Expected); + +private: + SmallVector &Elements; + RootSignatureLexer &Lexer; + + clang::Preprocessor &PP; + + RootSignatureToken CurToken; +}; + +} // namespace hlsl +} // namespace clang + +#endif // LLVM_CLANG_PARSE_PARSEHLSLROOTSIGNATURE_H diff --git a/clang/lib/Parse/CMakeLists.txt b/clang/lib/Parse/CMakeLists.txt index 22e902f7e1bc5..00fde537bb9c6 100644 --- a/clang/lib/Parse/CMakeLists.txt +++ b/clang/lib/Parse/CMakeLists.txt @@ -14,6 +14,7 @@ add_clang_library(clangParse ParseExpr.cpp ParseExprCXX.cpp ParseHLSL.cpp + ParseHLSLRootSignature.cpp ParseInit.cpp ParseObjc.cpp ParseOpenMP.cpp diff --git a/clang/lib/Parse/ParseHLSLRootSignature.cpp b/clang/lib/Parse/ParseHLSLRootSignature.cpp new file mode 100644 index 0000000000000..ac89baaf14ef3 --- /dev/null +++ b/clang/lib/Parse/ParseHLSLRootSignature.cpp @@ -0,0 +1,460 @@ +//=== ParseHLSLRootSignature.cpp - Parse Root Signature -------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "clang/Parse/ParseHLSLRootSignature.h" + +#include "clang/Lex/LiteralSupport.h" + +#include "llvm/Support/raw_ostream.h" + +using namespace llvm::hlsl::rootsig; + +namespace clang { +namespace hlsl { + +static std::string FormatTokenKinds(ArrayRef Kinds) { + std::string TokenString; + llvm::raw_string_ostream Out(TokenString); + bool First = true; + for (auto Kind : Kinds) { + if (!First) + Out << ", "; + switch (Kind) { +#define TOK(X, SPELLING) \ + case TokenKind::X: \ + Out << SPELLING; \ + break; +#include "clang/Lex/HLSLRootSignatureTokenKinds.def" + } + First = false; + } + + return TokenString; +} + +// Parser Definitions + +RootSignatureParser::RootSignatureParser(SmallVector &Elements, + RootSignatureLexer &Lexer, + Preprocessor &PP) + : Elements(Elements), Lexer(Lexer), PP(PP), CurToken(SourceLocation()) {} + +bool RootSignatureParser::Parse() { + // Handle edge-case of empty RootSignature() + if (Lexer.EndOfBuffer()) + return false; + + // Iterate as many RootElements as possible + while (!ParseRootElement()) { + if (Lexer.EndOfBuffer()) + return false; + if (ConsumeExpectedToken(TokenKind::pu_comma, diag::err_expected_either, + TokenKind::end_of_stream)) + return true; + } + + return true; +} + +bool RootSignatureParser::ParseRootElement() { + if (ConsumeExpectedToken(TokenKind::kw_DescriptorTable, + diag::err_hlsl_expected_param, + /*Context=*/TokenKind::kw_RootSignature)) + return true; + + // Dispatch onto the correct parse method + switch (CurToken.Kind) { + case TokenKind::kw_DescriptorTable: + return ParseDescriptorTable(); + default: + break; + } + llvm_unreachable("Switch for an expected token was not provided"); +} + +bool RootSignatureParser::ParseDescriptorTable() { + DescriptorTable Table; + + if (ConsumeExpectedToken(TokenKind::pu_l_paren, diag::err_expected_after, + CurToken.Kind)) + return true; + + // Empty case: + if (TryConsumeExpectedToken(TokenKind::pu_r_paren)) { + Elements.push_back(Table); + return false; + } + + bool SeenVisibility = false; + // Iterate through all the defined clauses + do { + // Handle the visibility parameter + if (TryConsumeExpectedToken(TokenKind::kw_visibility)) { + if (SeenVisibility) { + Diags().Report(CurToken.TokLoc, diag::err_hlsl_rootsig_repeat_param) + << FormatTokenKinds(CurToken.Kind); + return true; + } + SeenVisibility = true; + if (ParseParam(&Table.Visibility, TokenKind::kw_visibility)) + return true; + continue; + } + + // Otherwise, we expect a clause + if (ParseDescriptorTableClause()) + return true; + Table.NumClauses++; + } while (TryConsumeExpectedToken(TokenKind::pu_comma)); + + if (ConsumeExpectedToken(TokenKind::pu_r_paren, diag::err_expected_after, + CurToken.Kind)) + return true; + + Elements.push_back(Table); + return false; +} + +bool RootSignatureParser::ParseDescriptorTableClause() { + if (ConsumeExpectedToken({TokenKind::kw_CBV, TokenKind::kw_SRV, + TokenKind::kw_UAV, TokenKind::kw_Sampler}, + diag::err_hlsl_expected_param, + /*Context=*/TokenKind::kw_DescriptorTable)) + return true; + + DescriptorTableClause Clause; + switch (CurToken.Kind) { + case TokenKind::kw_CBV: + Clause.Type = ClauseType::CBuffer; + break; + case TokenKind::kw_SRV: + Clause.Type = ClauseType::SRV; + break; + case TokenKind::kw_UAV: + Clause.Type = ClauseType::UAV; + break; + case TokenKind::kw_Sampler: + Clause.Type = ClauseType::Sampler; + break; + default: + llvm_unreachable("Switch for an expected token was not provided"); + } + Clause.SetDefaultFlags(); + + // Store context of clause token type we are parsing + TokenKind Context = CurToken.Kind; + + if (ConsumeExpectedToken(TokenKind::pu_l_paren, diag::err_expected_after, + CurToken.Kind)) + return true; + + // Consume mandatory Register paramater + if (ParseRegister(&Clause.Register, Context)) + return true; + + // Define optional paramaters + llvm::SmallDenseMap RefMap = { + {TokenKind::kw_numDescriptors, &Clause.NumDescriptors}, + {TokenKind::kw_space, &Clause.Space}, + {TokenKind::kw_offset, &Clause.Offset}, + {TokenKind::kw_flags, &Clause.Flags}, + }; + if (ParseOptionalParams({RefMap}, Context)) + return true; + + if (ConsumeExpectedToken(TokenKind::pu_r_paren, diag::err_expected_after, + CurToken.Kind)) + return true; + + Elements.push_back(Clause); + return false; +} + +// Helper struct so that we can use the overloaded notation of std::visit +template struct ParseMethods : Ts... { using Ts::operator()...; }; +template ParseMethods(Ts...) -> ParseMethods; + +bool RootSignatureParser::ParseParam(ParamType Ref, TokenKind Context) { + if (ConsumeExpectedToken(TokenKind::pu_equal, diag::err_expected_after, + CurToken.Kind)) + return true; + + bool Error; + std::visit(ParseMethods{ + [&](uint32_t *X) { Error = ParseUInt(X, Context); }, + [&](DescriptorRangeOffset *X) { + Error = ParseDescriptorRangeOffset(X, Context); + }, + [&](ShaderVisibility *Enum) { + Error = ParseShaderVisibility(Enum, Context); + }, + [&](DescriptorRangeFlags *Flags) { + Error = ParseDescriptorRangeFlags(Flags, Context); + }, + }, + Ref); + + return Error; +} + +bool RootSignatureParser::ParseOptionalParams( + llvm::SmallDenseMap &RefMap, TokenKind Context) { + SmallVector ParamKeywords; + for (auto RefPair : RefMap) + ParamKeywords.push_back(RefPair.first); + + // Keep track of which keywords have been seen to report duplicates + llvm::SmallDenseSet Seen; + + while (TryConsumeExpectedToken(TokenKind::pu_comma)) { + if (ConsumeExpectedToken(ParamKeywords, diag::err_hlsl_expected_param, + Context)) + return true; + + TokenKind ParamKind = CurToken.Kind; + if (Seen.contains(ParamKind)) { + Diags().Report(CurToken.TokLoc, diag::err_hlsl_rootsig_repeat_param) + << FormatTokenKinds({ParamKind}); + return true; + } + Seen.insert(ParamKind); + + if (ParseParam(RefMap[ParamKind], ParamKind)) + return true; + } + + return false; +} + +bool RootSignatureParser::HandleUIntLiteral(uint32_t &X) { + // Parse the numeric value and do semantic checks on its specification + clang::NumericLiteralParser Literal(CurToken.NumSpelling, CurToken.TokLoc, + PP.getSourceManager(), PP.getLangOpts(), + PP.getTargetInfo(), PP.getDiagnostics()); + if (Literal.hadError) + return true; // Error has already been reported so just return + + assert(Literal.isIntegerLiteral() && "IsNumberChar will only support digits"); + + llvm::APSInt Val = llvm::APSInt(32, false); + if (Literal.GetIntegerValue(Val)) { + // Report that the value has overflowed + PP.getDiagnostics().Report(CurToken.TokLoc, + diag::err_hlsl_number_literal_overflow) + << 0 << CurToken.NumSpelling; + return true; + } + + X = Val.getExtValue(); + return false; +} + +bool RootSignatureParser::ParseRegister(Register *Register, TokenKind Context) { + if (ConsumeExpectedToken( + {TokenKind::bReg, TokenKind::tReg, TokenKind::uReg, TokenKind::sReg}, + diag::err_hlsl_expected_param, Context)) + return true; + + switch (CurToken.Kind) { + case TokenKind::bReg: + Register->ViewType = RegisterType::BReg; + break; + case TokenKind::tReg: + Register->ViewType = RegisterType::TReg; + break; + case TokenKind::uReg: + Register->ViewType = RegisterType::UReg; + break; + case TokenKind::sReg: + Register->ViewType = RegisterType::SReg; + break; + default: + llvm_unreachable("Switch for an expected token was not provided"); + } + + if (HandleUIntLiteral(Register->Number)) + return true; // propogate NumericLiteralParser error + + return false; +} + +bool RootSignatureParser::ParseUInt(uint32_t *X, TokenKind Context) { + // Treat a postively signed integer as though it is unsigned to match DXC + TryConsumeExpectedToken(TokenKind::pu_plus); + if (ConsumeExpectedToken(TokenKind::int_literal, + diag::err_hlsl_expected_value, Context)) + return true; + + if (HandleUIntLiteral(*X)) + return true; // propogate NumericLiteralParser error + + return false; +} + +bool RootSignatureParser::ParseDescriptorRangeOffset(DescriptorRangeOffset *X, + TokenKind Context) { + if (ConsumeExpectedToken( + {TokenKind::int_literal, TokenKind::en_DescriptorRangeOffsetAppend}, + diag::err_hlsl_expected_value, Context)) + return true; + + // Edge case for the offset enum -> static value + if (CurToken.Kind == TokenKind::en_DescriptorRangeOffsetAppend) { + *X = DescriptorTableOffsetAppend; + return false; + } + + uint32_t Temp; + if (HandleUIntLiteral(Temp)) + return true; // propogate NumericLiteralParser error + *X = DescriptorRangeOffset(Temp); + return false; +} + +template +bool RootSignatureParser::ParseEnum( + llvm::SmallDenseMap &EnumMap, EnumType *Enum, + TokenKind Context) { + SmallVector EnumToks; + if (AllowZero) + EnumToks.push_back(TokenKind::int_literal); // '0' is a valid flag value + for (auto EnumPair : EnumMap) + EnumToks.push_back(EnumPair.first); + + // If invoked we expect to have an enum + if (ConsumeExpectedToken(EnumToks, diag::err_hlsl_expected_value, Context)) + return true; + + // Handle the edge case when '0' is used to specify None + if (CurToken.Kind == TokenKind::int_literal) { + uint32_t Temp; + if (HandleUIntLiteral(Temp)) + return true; // propogate NumericLiteralParser error + if (Temp != 0) { + Diags().Report(CurToken.TokLoc, diag::err_hlsl_rootsig_non_zero_flag); + return true; + } + // Set enum to None equivalent + *Enum = EnumType(0); + return false; + } + + // Effectively a switch statement on the token kinds + for (auto EnumPair : EnumMap) + if (CurToken.Kind == EnumPair.first) { + *Enum = EnumPair.second; + return false; + } + + llvm_unreachable("Switch for an expected token was not provided"); +} + +bool RootSignatureParser::ParseShaderVisibility(ShaderVisibility *Enum, + TokenKind Context) { + // Define the possible flag kinds + llvm::SmallDenseMap EnumMap = { +#define SHADER_VISIBILITY_ENUM(NAME, LIT) \ + {TokenKind::en_##NAME, ShaderVisibility::NAME}, +#include "clang/Lex/HLSLRootSignatureTokenKinds.def" + }; + + return ParseEnum(EnumMap, Enum, Context); +} + +template +bool RootSignatureParser::ParseFlags( + llvm::SmallDenseMap &FlagMap, FlagType *Flags, + TokenKind Context) { + // Override the default value to 0 so that we can correctly 'or' the values + *Flags = FlagType(0); + + do { + FlagType Flag; + if (ParseEnum(FlagMap, &Flag, Context)) + return true; + // Store the 'or' + *Flags |= Flag; + } while (TryConsumeExpectedToken(TokenKind::pu_or)); + + return false; +} + +bool RootSignatureParser::ParseDescriptorRangeFlags(DescriptorRangeFlags *Flags, + TokenKind Context) { + // Define the possible flag kinds + llvm::SmallDenseMap FlagMap = { +#define DESCRIPTOR_RANGE_FLAG_ENUM(NAME, LIT, ON) \ + {TokenKind::en_##NAME, DescriptorRangeFlags::NAME}, +#include "clang/Lex/HLSLRootSignatureTokenKinds.def" + }; + + return ParseFlags(FlagMap, Flags, Context); +} + +// Is given token one of the expected kinds +static bool IsExpectedToken(TokenKind Kind, ArrayRef AnyExpected) { + for (auto Expected : AnyExpected) + if (Kind == Expected) + return true; + return false; +} + +bool RootSignatureParser::PeekExpectedToken(TokenKind Expected) { + return PeekExpectedToken(ArrayRef{Expected}); +} + +bool RootSignatureParser::PeekExpectedToken(ArrayRef AnyExpected) { + RootSignatureToken Result = Lexer.PeekNextToken(); + return IsExpectedToken(Result.Kind, AnyExpected); +} + +bool RootSignatureParser::ConsumeExpectedToken(TokenKind Expected, + unsigned DiagID, + TokenKind Context) { + return ConsumeExpectedToken(ArrayRef{Expected}, DiagID, Context); +} + +bool RootSignatureParser::ConsumeExpectedToken(ArrayRef AnyExpected, + unsigned DiagID, + TokenKind Context) { + if (TryConsumeExpectedToken(AnyExpected)) + return false; + + // Report unexpected token kind error + DiagnosticBuilder DB = Diags().Report(CurToken.TokLoc, DiagID); + switch (DiagID) { + case diag::err_expected: + DB << FormatTokenKinds(AnyExpected); + break; + case diag::err_expected_either: + case diag::err_expected_after: + case diag::err_hlsl_expected_param: + case diag::err_hlsl_expected_value: + DB << FormatTokenKinds(AnyExpected) << FormatTokenKinds({Context}); + break; + default: + break; + } + return true; +} + +bool RootSignatureParser::TryConsumeExpectedToken(TokenKind Expected) { + return TryConsumeExpectedToken(ArrayRef{Expected}); +} + +bool RootSignatureParser::TryConsumeExpectedToken( + ArrayRef AnyExpected) { + // If not the expected token just return + if (!PeekExpectedToken(AnyExpected)) + return false; + ConsumeNextToken(); + return true; +} + +} // namespace hlsl +} // namespace clang diff --git a/clang/unittests/CMakeLists.txt b/clang/unittests/CMakeLists.txt index 85d265426ec80..9b3ce8aa7de73 100644 --- a/clang/unittests/CMakeLists.txt +++ b/clang/unittests/CMakeLists.txt @@ -25,6 +25,7 @@ endfunction() add_subdirectory(Basic) add_subdirectory(Lex) +add_subdirectory(Parse) add_subdirectory(Driver) if(CLANG_ENABLE_STATIC_ANALYZER) add_subdirectory(Analysis) diff --git a/clang/unittests/Lex/LexHLSLRootSignatureTest.cpp b/clang/unittests/Lex/LexHLSLRootSignatureTest.cpp index 0576f08c4c276..3b13e295f7c87 100644 --- a/clang/unittests/Lex/LexHLSLRootSignatureTest.cpp +++ b/clang/unittests/Lex/LexHLSLRootSignatureTest.cpp @@ -85,6 +85,8 @@ TEST_F(LexHLSLRootSignatureTest, ValidLexAllTokensTest) { (),|=+- + RootSignature + DescriptorTable CBV SRV UAV Sampler @@ -113,7 +115,7 @@ TEST_F(LexHLSLRootSignatureTest, ValidLexAllTokensTest) { SmallVector Tokens; SmallVector Expected = { -#define TOK(NAME) hlsl::TokenKind::NAME, +#define TOK(NAME, SPELLING) hlsl::TokenKind::NAME, #include "clang/Lex/HLSLRootSignatureTokenKinds.def" }; diff --git a/clang/unittests/Parse/CMakeLists.txt b/clang/unittests/Parse/CMakeLists.txt new file mode 100644 index 0000000000000..eeb58174568cd --- /dev/null +++ b/clang/unittests/Parse/CMakeLists.txt @@ -0,0 +1,23 @@ +set(LLVM_LINK_COMPONENTS + Support + ) +add_clang_unittest(ParseTests + ParseHLSLRootSignatureTest.cpp + ) +clang_target_link_libraries(ParseTests + PRIVATE + clangAST + clangASTMatchers + clangBasic + clangFrontend + clangParse + clangSema + clangSerialization + clangTooling + ) +target_link_libraries(ParseTests + PRIVATE + LLVMTestingAnnotations + LLVMTestingSupport + clangTesting + ) diff --git a/clang/unittests/Parse/ParseHLSLRootSignatureTest.cpp b/clang/unittests/Parse/ParseHLSLRootSignatureTest.cpp new file mode 100644 index 0000000000000..793621a9be155 --- /dev/null +++ b/clang/unittests/Parse/ParseHLSLRootSignatureTest.cpp @@ -0,0 +1,375 @@ +//=== ParseHLSLRootSignatureTest.cpp - Parse Root Signature tests ---------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "clang/Basic/Diagnostic.h" +#include "clang/Basic/DiagnosticOptions.h" +#include "clang/Basic/FileManager.h" +#include "clang/Basic/LangOptions.h" +#include "clang/Basic/SourceLocation.h" +#include "clang/Basic/SourceManager.h" +#include "clang/Basic/TargetInfo.h" +#include "clang/Lex/HeaderSearch.h" +#include "clang/Lex/HeaderSearchOptions.h" +#include "clang/Lex/Lexer.h" +#include "clang/Lex/ModuleLoader.h" +#include "clang/Lex/Preprocessor.h" +#include "clang/Lex/PreprocessorOptions.h" + +#include "clang/Lex/LexHLSLRootSignature.h" +#include "clang/Parse/ParseHLSLRootSignature.h" +#include "gtest/gtest.h" + +using namespace clang; +using namespace llvm::hlsl::rootsig; + +namespace { + +// Diagnostic helper for helper tests +class ExpectedDiagConsumer : public DiagnosticConsumer { + virtual void anchor() {} + + void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel, + const Diagnostic &Info) override { + if (!FirstDiag || !ExpectedDiagID.has_value()) { + Satisfied = false; + return; + } + FirstDiag = false; + + Satisfied = ExpectedDiagID.value() == Info.getID(); + } + + bool FirstDiag = true; + bool Satisfied = false; + std::optional ExpectedDiagID; + +public: + void SetNoDiag() { + Satisfied = true; + ExpectedDiagID = std::nullopt; + } + + void SetExpected(unsigned DiagID) { + Satisfied = false; + ExpectedDiagID = DiagID; + } + + bool IsSatisfied() { return Satisfied; } +}; + +// The test fixture. +class ParseHLSLRootSignatureTest : public ::testing::Test { +protected: + ParseHLSLRootSignatureTest() + : FileMgr(FileMgrOpts), DiagID(new DiagnosticIDs()), + Consumer(new ExpectedDiagConsumer()), + Diags(DiagID, new DiagnosticOptions, Consumer), + SourceMgr(Diags, FileMgr), TargetOpts(new TargetOptions) { + // This is an arbitrarily chosen target triple to create the target info. + TargetOpts->Triple = "dxil"; + Target = TargetInfo::CreateTargetInfo(Diags, TargetOpts); + } + + std::unique_ptr CreatePP(StringRef Source, + TrivialModuleLoader &ModLoader) { + std::unique_ptr Buf = + llvm::MemoryBuffer::getMemBuffer(Source); + SourceMgr.setMainFileID(SourceMgr.createFileID(std::move(Buf))); + + HeaderSearch HeaderInfo(std::make_shared(), SourceMgr, + Diags, LangOpts, Target.get()); + std::unique_ptr PP = std::make_unique( + std::make_shared(), Diags, LangOpts, SourceMgr, + HeaderInfo, ModLoader, + /*IILookup =*/nullptr, + /*OwnsHeaderSearch =*/false); + PP->Initialize(*Target); + PP->EnterMainSourceFile(); + return PP; + } + + FileSystemOptions FileMgrOpts; + FileManager FileMgr; + IntrusiveRefCntPtr DiagID; + ExpectedDiagConsumer *Consumer; + DiagnosticsEngine Diags; + SourceManager SourceMgr; + LangOptions LangOpts; + std::shared_ptr TargetOpts; + IntrusiveRefCntPtr Target; +}; + +// Valid Parser Tests + +TEST_F(ParseHLSLRootSignatureTest, ValidParseEmptyTest) { + const llvm::StringLiteral Source = R"cc()cc"; + + TrivialModuleLoader ModLoader; + auto PP = CreatePP(Source, ModLoader); + auto TokLoc = SourceLocation(); + + hlsl::RootSignatureLexer Lexer(Source, TokLoc); + SmallVector Elements; + hlsl::RootSignatureParser Parser(Elements, Lexer, *PP); + + // Test no diagnostics produced + Consumer->SetNoDiag(); + + ASSERT_FALSE(Parser.Parse()); + ASSERT_EQ(Elements.size(), 0u); + + ASSERT_TRUE(Consumer->IsSatisfied()); +} + +TEST_F(ParseHLSLRootSignatureTest, ValidParseDTClausesTest) { + const llvm::StringLiteral Source = R"cc( + DescriptorTable( + visibility = SHADER_VISIBILITY_PIXEL, + CBV(b0), + SRV(t42, space = 3, offset = 32, numDescriptors = +4, flags = 0), + Sampler(s987, space = 2, offset = DESCRIPTOR_RANGE_OFFSET_APPEND), + UAV(u987234, + flags = Descriptors_Volatile | Data_Volatile + | Data_Static_While_Set_At_Execute | Data_Static + | Descriptors_Static_Keeping_Buffer_Bounds_Checks + ) + ), + DescriptorTable() + )cc"; + + TrivialModuleLoader ModLoader; + auto PP = CreatePP(Source, ModLoader); + auto TokLoc = SourceLocation(); + + hlsl::RootSignatureLexer Lexer(Source, TokLoc); + SmallVector Elements; + hlsl::RootSignatureParser Parser(Elements, Lexer, *PP); + + // Test no diagnostics produced + Consumer->SetNoDiag(); + + ASSERT_FALSE(Parser.Parse()); + RootElement Elem = Elements[0]; + ASSERT_TRUE(std::holds_alternative(Elem)); + ASSERT_EQ(std::get(Elem).Type, ClauseType::CBuffer); + ASSERT_EQ(std::get(Elem).Register.ViewType, + RegisterType::BReg); + ASSERT_EQ(std::get(Elem).Register.Number, 0u); + ASSERT_EQ(std::get(Elem).NumDescriptors, 1u); + ASSERT_EQ(std::get(Elem).Space, 0u); + ASSERT_EQ(std::get(Elem).Offset, + DescriptorRangeOffset(DescriptorTableOffsetAppend)); + ASSERT_EQ(std::get(Elem).Flags, + DescriptorRangeFlags::DataStaticWhileSetAtExecute); + + Elem = Elements[1]; + ASSERT_TRUE(std::holds_alternative(Elem)); + ASSERT_EQ(std::get(Elem).Type, ClauseType::SRV); + ASSERT_EQ(std::get(Elem).Register.ViewType, + RegisterType::TReg); + ASSERT_EQ(std::get(Elem).Register.Number, 42u); + ASSERT_EQ(std::get(Elem).NumDescriptors, 4u); + ASSERT_EQ(std::get(Elem).Space, 3u); + ASSERT_EQ(std::get(Elem).Offset, + DescriptorRangeOffset(32)); + ASSERT_EQ(std::get(Elem).Flags, + DescriptorRangeFlags::None); + + Elem = Elements[2]; + ASSERT_TRUE(std::holds_alternative(Elem)); + ASSERT_EQ(std::get(Elem).Type, ClauseType::Sampler); + ASSERT_EQ(std::get(Elem).Register.ViewType, + RegisterType::SReg); + ASSERT_EQ(std::get(Elem).Register.Number, 987u); + ASSERT_EQ(std::get(Elem).NumDescriptors, 1u); + ASSERT_EQ(std::get(Elem).Space, 2u); + ASSERT_EQ(std::get(Elem).Offset, + DescriptorRangeOffset(DescriptorTableOffsetAppend)); + ASSERT_EQ(std::get(Elem).Flags, + DescriptorRangeFlags::None); + + Elem = Elements[3]; + ASSERT_TRUE(std::holds_alternative(Elem)); + ASSERT_EQ(std::get(Elem).Type, ClauseType::UAV); + ASSERT_EQ(std::get(Elem).Register.ViewType, + RegisterType::UReg); + ASSERT_EQ(std::get(Elem).Register.Number, 987234u); + ASSERT_EQ(std::get(Elem).NumDescriptors, 1u); + ASSERT_EQ(std::get(Elem).Space, 0u); + ASSERT_EQ(std::get(Elem).Offset, + DescriptorRangeOffset(DescriptorTableOffsetAppend)); + ASSERT_EQ(std::get(Elem).Flags, + DescriptorRangeFlags::ValidFlags); + + Elem = Elements[4]; + ASSERT_TRUE(std::holds_alternative(Elem)); + ASSERT_EQ(std::get(Elem).NumClauses, 4u); + ASSERT_EQ(std::get(Elem).Visibility, + ShaderVisibility::Pixel); + + Elem = Elements[5]; + // Test generated DescriptorTable start has correct default values + ASSERT_TRUE(std::holds_alternative(Elem)); + ASSERT_EQ(std::get(Elem).NumClauses, 0u); + ASSERT_EQ(std::get(Elem).Visibility, ShaderVisibility::All); + + ASSERT_TRUE(Consumer->IsSatisfied()); +} + +// Invalid Parser Tests + +TEST_F(ParseHLSLRootSignatureTest, InvalidParseUnexpectedTokenTest) { + const llvm::StringLiteral Source = R"cc( + DescriptorTable() + space + )cc"; + + TrivialModuleLoader ModLoader; + auto PP = CreatePP(Source, ModLoader); + auto TokLoc = SourceLocation(); + + hlsl::RootSignatureLexer Lexer(Source, TokLoc); + SmallVector Elements; + hlsl::RootSignatureParser Parser(Elements, Lexer, *PP); + + // Test correct diagnostic produced + Consumer->SetExpected(diag::err_expected_either); + ASSERT_TRUE(Parser.Parse()); + + ASSERT_TRUE(Consumer->IsSatisfied()); +} + +TEST_F(ParseHLSLRootSignatureTest, InvalidParseInvalidTokenTest) { + const llvm::StringLiteral Source = R"cc( + notAnIdentifier + )cc"; + + TrivialModuleLoader ModLoader; + auto PP = CreatePP(Source, ModLoader); + auto TokLoc = SourceLocation(); + + hlsl::RootSignatureLexer Lexer(Source, TokLoc); + SmallVector Elements; + hlsl::RootSignatureParser Parser(Elements, Lexer, *PP); + + // Test correct diagnostic produced - invalid token + Consumer->SetExpected(diag::err_hlsl_expected_param); + ASSERT_TRUE(Parser.Parse()); + + ASSERT_TRUE(Consumer->IsSatisfied()); +} + +TEST_F(ParseHLSLRootSignatureTest, InvalidParseUnexpectedEndOfStreamTest) { + const llvm::StringLiteral Source = R"cc( + DescriptorTable( + )cc"; + + TrivialModuleLoader ModLoader; + auto PP = CreatePP(Source, ModLoader); + auto TokLoc = SourceLocation(); + + hlsl::RootSignatureLexer Lexer(Source, TokLoc); + SmallVector Elements; + hlsl::RootSignatureParser Parser(Elements, Lexer, *PP); + + // Test correct diagnostic produced - end of stream + Consumer->SetExpected(diag::err_hlsl_expected_param); + ASSERT_TRUE(Parser.Parse()); + + ASSERT_TRUE(Consumer->IsSatisfied()); +} + +TEST_F(ParseHLSLRootSignatureTest, InvalidLexOverflowedNumberTest) { + // This test will check that the lexing fails due to an integer overflow + const llvm::StringLiteral Source = R"cc( + DescriptorTable( + CBV(s4294967296) + ) + )cc"; + + TrivialModuleLoader ModLoader; + auto PP = CreatePP(Source, ModLoader); + auto TokLoc = SourceLocation(); + + hlsl::RootSignatureLexer Lexer(Source, TokLoc); + SmallVector Elements; + hlsl::RootSignatureParser Parser(Elements, Lexer, *PP); + + // Test correct diagnostic produced + Consumer->SetExpected(diag::err_hlsl_number_literal_overflow); + ASSERT_TRUE(Parser.Parse()); + + ASSERT_TRUE(Consumer->IsSatisfied()); +} + +TEST_F(ParseHLSLRootSignatureTest, InvalidParseRepeatedParamTest) { + const llvm::StringLiteral Source = R"cc( + DescriptorTable( + CBV(b0, numDescriptors = 3, numDescriptors = 1) + ) + )cc"; + TrivialModuleLoader ModLoader; + auto PP = CreatePP(Source, ModLoader); + auto TokLoc = SourceLocation(); + hlsl::RootSignatureLexer Lexer(Source, TokLoc); + SmallVector Elements; + hlsl::RootSignatureParser Parser(Elements, Lexer, *PP); + + // Test correct diagnostic produced + Consumer->SetExpected(diag::err_hlsl_rootsig_repeat_param); + ASSERT_TRUE(Parser.Parse()); + + ASSERT_TRUE(Consumer->IsSatisfied()); +} + +TEST_F(ParseHLSLRootSignatureTest, InvalidParseRepeatedVisibilityTest) { + const llvm::StringLiteral Source = R"cc( + DescriptorTable( + visibility = SHADER_VISIBILITY_GEOMETRY, + visibility = SHADER_VISIBILITY_HULL + ) + )cc"; + + TrivialModuleLoader ModLoader; + auto PP = CreatePP(Source, ModLoader); + auto TokLoc = SourceLocation(); + + hlsl::RootSignatureLexer Lexer(Source, TokLoc); + SmallVector Elements; + hlsl::RootSignatureParser Parser(Elements, Lexer, *PP); + + // Test correct diagnostic produced + Consumer->SetExpected(diag::err_hlsl_rootsig_repeat_param); + ASSERT_TRUE(Parser.Parse()); + + ASSERT_TRUE(Consumer->IsSatisfied()); +} + +TEST_F(ParseHLSLRootSignatureTest, InvalidParseNonZeroFlagTest) { + const llvm::StringLiteral Source = R"cc( + DescriptorTable( + CBV(b0, flags = 3) + ) + )cc"; + + TrivialModuleLoader ModLoader; + auto PP = CreatePP(Source, ModLoader); + auto TokLoc = SourceLocation(); + + hlsl::RootSignatureLexer Lexer(Source, TokLoc); + SmallVector Elements; + hlsl::RootSignatureParser Parser(Elements, Lexer, *PP); + + // Test correct diagnostic produced + Consumer->SetExpected(diag::err_hlsl_rootsig_non_zero_flag); + ASSERT_TRUE(Parser.Parse()); + + ASSERT_TRUE(Consumer->IsSatisfied()); +} + +} // anonymous namespace diff --git a/llvm/include/llvm/Frontend/HLSL/HLSLRootSignature.h b/llvm/include/llvm/Frontend/HLSL/HLSLRootSignature.h new file mode 100644 index 0000000000000..f999a0b5eef33 --- /dev/null +++ b/llvm/include/llvm/Frontend/HLSL/HLSLRootSignature.h @@ -0,0 +1,129 @@ +//===- HLSLRootSignature.h - HLSL Root Signature helper objects -----------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +/// +/// \file This file contains helper objects for working with HLSL Root +/// Signatures. +/// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_FRONTEND_HLSL_HLSLROOTSIGNATURE_H +#define LLVM_FRONTEND_HLSL_HLSLROOTSIGNATURE_H + +#include "llvm/Support/DXILABI.h" +#include + +namespace llvm { +namespace hlsl { +namespace rootsig { + +#define RS_DEFINE_ENUM_CLASS_FLAGS_OPERATORS(Class) \ + inline Class operator|(Class a, Class b) { \ + return static_cast(llvm::to_underlying(a) | \ + llvm::to_underlying(b)); \ + } \ + inline Class operator&(Class a, Class b) { \ + return static_cast(llvm::to_underlying(a) & \ + llvm::to_underlying(b)); \ + } \ + inline Class operator~(Class a) { \ + return static_cast(~llvm::to_underlying(a)); \ + } \ + inline Class &operator|=(Class &a, Class b) { \ + a = a | b; \ + return a; \ + } \ + inline Class &operator&=(Class &a, Class b) { \ + a = a & b; \ + return a; \ + } + +// Definition of the various enumerations and flags + +enum class DescriptorRangeOffset : uint32_t; + +enum class DescriptorRangeFlags : unsigned { + None = 0, + DescriptorsVolatile = 0x1, + DataVolatile = 0x2, + DataStaticWhileSetAtExecute = 0x4, + DataStatic = 0x8, + DescriptorsStaticKeepingBufferBoundsChecks = 0x10000, + ValidFlags = 0x1000f, + ValidSamplerFlags = DescriptorsVolatile, +}; +RS_DEFINE_ENUM_CLASS_FLAGS_OPERATORS(DescriptorRangeFlags) + +enum class ShaderVisibility { + All = 0, + Vertex = 1, + Hull = 2, + Domain = 3, + Geometry = 4, + Pixel = 5, + Amplification = 6, + Mesh = 7, +}; + +// Definitions of the in-memory data layout structures + +// Models the different registers: bReg | tReg | uReg | sReg +enum class RegisterType { BReg, TReg, UReg, SReg }; +struct Register { + RegisterType ViewType; + uint32_t Number; +}; + +// Models the end of a descriptor table and stores its visibility +struct DescriptorTable { + ShaderVisibility Visibility = ShaderVisibility::All; + uint32_t NumClauses = 0; // The number of clauses in the table +}; + +static const DescriptorRangeOffset DescriptorTableOffsetAppend = + DescriptorRangeOffset(0xffffffff); +// Models DTClause : CBV | SRV | UAV | Sampler, by collecting like parameters +using ClauseType = llvm::dxil::ResourceClass; +struct DescriptorTableClause { + ClauseType Type; + Register Register; + uint32_t NumDescriptors = 1; + uint32_t Space = 0; + DescriptorRangeOffset Offset = DescriptorTableOffsetAppend; + DescriptorRangeFlags Flags; + + void SetDefaultFlags() { + switch (Type) { + case ClauseType::CBuffer: + Flags = DescriptorRangeFlags::DataStaticWhileSetAtExecute; + break; + case ClauseType::SRV: + Flags = DescriptorRangeFlags::DataStaticWhileSetAtExecute; + break; + case ClauseType::UAV: + Flags = DescriptorRangeFlags::DataVolatile; + break; + case ClauseType::Sampler: + Flags = DescriptorRangeFlags::None; + break; + } + } +}; + +// Models RootElement : DescriptorTable | DescriptorTableClause +using RootElement = std::variant; + +// Models a reference to all assignment parameter types that any RootElement +// may have. Things of the form: Keyword = Param +using ParamType = std::variant; + +} // namespace rootsig +} // namespace hlsl +} // namespace llvm + +#endif // LLVM_FRONTEND_HLSL_HLSLROOTSIGNATURE_H