Skip to content

[HLSL][RootSignature] Add lexing support for floating points #137720

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions clang/include/clang/Lex/HLSLRootSignatureTokenKinds.def
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
TOK(invalid, "invalid identifier")
TOK(end_of_stream, "end of stream")
TOK(int_literal, "integer literal")
TOK(float_literal, "float literal")

// Register Tokens:
TOK(bReg, "b register")
Expand Down
17 changes: 12 additions & 5 deletions clang/lib/Lex/LexHLSLRootSignature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ using TokenKind = RootSignatureToken::Kind;
// Lexer Definitions

static bool isNumberChar(char C) {
// TODO(#126565): extend for float support exponents
return isdigit(C); // integer support
return isdigit(C) // integer support
|| C == '.' // float support
|| C == 'e' || C == 'E' || C == '-' || C == '+' // exponent support
|| C == 'f' || C == 'F'; // explicit float support
}

RootSignatureToken RootSignatureLexer::lexToken() {
Expand Down Expand Up @@ -45,10 +47,15 @@ RootSignatureToken RootSignatureLexer::lexToken() {
break;
}

// Integer literal
if (isdigit(C)) {
Result.TokKind = TokenKind::int_literal;
// Number literal
if (isdigit(C) || C == '.') {
Result.NumSpelling = Buffer.take_while(isNumberChar);

// If all values are digits then we have an int literal
bool IsInteger = Result.NumSpelling.find_if_not(isdigit) == StringRef::npos;

Result.TokKind =
IsInteger ? TokenKind::int_literal : TokenKind::float_literal;
advanceBuffer(Result.NumSpelling.size());
return Result;
}
Expand Down
49 changes: 45 additions & 4 deletions clang/unittests/Lex/LexHLSLRootSignatureTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ TEST_F(LexHLSLRootSignatureTest, ValidLexNumbersTest) {
// This test will check that we can lex different number tokens
const llvm::StringLiteral Source = R"cc(
-42 42 +42 +2147483648
42. 4.2 .42
42f 4.2F
.42e+3 4.2E-12
42.e+10f
)cc";

auto TokLoc = SourceLocation();
Expand All @@ -51,9 +55,14 @@ TEST_F(LexHLSLRootSignatureTest, ValidLexNumbersTest) {

SmallVector<hlsl::RootSignatureToken> Tokens;
SmallVector<TokenKind> Expected = {
TokenKind::pu_minus, TokenKind::int_literal, TokenKind::int_literal,
TokenKind::pu_plus, TokenKind::int_literal, TokenKind::pu_plus,
TokenKind::int_literal,
TokenKind::pu_minus, TokenKind::int_literal,
TokenKind::int_literal, TokenKind::pu_plus,
TokenKind::int_literal, TokenKind::pu_plus,
TokenKind::int_literal, TokenKind::float_literal,
TokenKind::float_literal, TokenKind::float_literal,
TokenKind::float_literal, TokenKind::float_literal,
TokenKind::float_literal, TokenKind::float_literal,
TokenKind::float_literal,
};
checkTokens(Lexer, Tokens, Expected);

Expand All @@ -73,13 +82,45 @@ TEST_F(LexHLSLRootSignatureTest, ValidLexNumbersTest) {
// is treated as an unsigned integer instead
IntToken = Tokens[6];
ASSERT_EQ(IntToken.NumSpelling, "2147483648");

// Sample decimal end
hlsl::RootSignatureToken FloatToken = Tokens[7];
ASSERT_EQ(FloatToken.NumSpelling, "42.");

// Sample decimal middle
FloatToken = Tokens[8];
ASSERT_EQ(FloatToken.NumSpelling, "4.2");

// Sample decimal start
FloatToken = Tokens[9];
ASSERT_EQ(FloatToken.NumSpelling, ".42");

// Sample float lower
FloatToken = Tokens[10];
ASSERT_EQ(FloatToken.NumSpelling, "42f");

// Sample float upper
FloatToken = Tokens[11];
ASSERT_EQ(FloatToken.NumSpelling, "4.2F");

// Sample exp +
FloatToken = Tokens[12];
ASSERT_EQ(FloatToken.NumSpelling, ".42e+3");

// Sample exp -
FloatToken = Tokens[13];
ASSERT_EQ(FloatToken.NumSpelling, "4.2E-12");

// Sample all combined
FloatToken = Tokens[14];
ASSERT_EQ(FloatToken.NumSpelling, "42.e+10f");
}

TEST_F(LexHLSLRootSignatureTest, ValidLexAllTokensTest) {
// This test will check that we can lex all defined tokens as defined in
// HLSLRootSignatureTokenKinds.def, plus some additional integer variations
const llvm::StringLiteral Source = R"cc(
42
42 42.0f

b0 t43 u987 s234

Expand Down