[TableGen] Remove explicit recursion in LexToken#143697
Merged
Conversation
When profiling a Release+Asserts build of llvm-tblgen I noticed that it was recursing hundreds of times to lex a sequence of hundreds of space characters.
Member
|
@llvm/pr-subscribers-tablegen Author: Jay Foad (jayfoad) ChangesWhen profiling a Release+Asserts build of llvm-tblgen I noticed that it Full diff: https://github.com/llvm/llvm-project/pull/143697.diff 1 Files Affected:
diff --git a/llvm/lib/TableGen/TGLexer.cpp b/llvm/lib/TableGen/TGLexer.cpp
index 46487cba9453e..5a3f86372beca 100644
--- a/llvm/lib/TableGen/TGLexer.cpp
+++ b/llvm/lib/TableGen/TGLexer.cpp
@@ -174,6 +174,7 @@ int TGLexer::peekNextChar(int Index) const {
}
tgtok::TokKind TGLexer::LexToken(bool FileOrLineStart) {
+restart:
TokStart = CurPtr;
// This always consumes at least one character.
int CurChar = getNextChar();
@@ -188,12 +189,12 @@ tgtok::TokKind TGLexer::LexToken(bool FileOrLineStart) {
return ReturnError(TokStart, "unexpected character");
case EOF:
// Lex next token, if we just left an include file.
- // Note that leaving an include file means that the next
- // symbol is located at the end of the 'include "..."'
- // construct, so LexToken() is called with default
- // false parameter.
- if (processEOF())
- return LexToken();
+ if (processEOF()) {
+ // Leaving an include file means that the next symbol is located at the
+ // end of the 'include "..."' construct.
+ FileOrLineStart = false;
+ goto restart;
+ }
// Return EOF denoting the end of lexing.
return tgtok::Eof;
@@ -238,10 +239,11 @@ tgtok::TokKind TGLexer::LexToken(bool FileOrLineStart) {
case ' ':
case '\t':
// Ignore whitespace.
- return LexToken(FileOrLineStart);
+ goto restart;
case '\n':
// Ignore whitespace, and identify the new line.
- return LexToken(true);
+ FileOrLineStart = true;
+ goto restart;
case '/':
// If this is the start of a // comment, skip until the end of the line or
// the end of the buffer.
@@ -252,7 +254,7 @@ tgtok::TokKind TGLexer::LexToken(bool FileOrLineStart) {
return tgtok::Error;
} else // Otherwise, this is an error.
return ReturnError(TokStart, "unexpected character");
- return LexToken(FileOrLineStart);
+ goto restart;
case '-': case '+':
case '0': case '1': case '2': case '3': case '4': case '5': case '6':
case '7': case '8': case '9': {
|
nvjle
reviewed
Jun 11, 2025
nvjle
approved these changes
Jun 11, 2025
jurahul
approved these changes
Jul 16, 2025
This was referenced Jul 23, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When profiling a Release+Asserts build of llvm-tblgen I noticed that it
was recursing hundreds of times to lex a sequence of hundreds of space
characters.