[WHLSL] The parser is too slow
authormmaxfield@apple.com <mmaxfield@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 6 Sep 2018 16:01:05 +0000 (16:01 +0000)
committermmaxfield@apple.com <mmaxfield@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 6 Sep 2018 16:01:05 +0000 (16:01 +0000)
commit37a21abd1124275d244d30cf5e32fd7a9e65a8be
tree42b5ccfd026a693d7d495ed7e1b575e7e4f7320e
parent030ec5c67bd1a3ae74c4a699621a062ba4377cb8
[WHLSL] The parser is too slow
https://bugs.webkit.org/show_bug.cgi?id=189014

Reviewed by Filip Pizlo.

This patch includes three changes:
1. Migrate from using try/catch to simply returning the WSyntaxError. This means that
   each parser call has to check for this sentinel value. The lexer still can throw if
   it encounters an unknown token or an unmatched "/*" token (which is rare).
2. After removing try/catch, making the sentinel values not inherit from Error (the
   Error constructor was taking lots of time)
3. Previously, every time the parser failed (which is many times per expression) it was
   running a regex over the entire source text to figure out where the error occurred.
   Instead, we can preprocess the text string to find these line numbers ahead of time.

Together, these make the parser 75x faster. Parsing the standard library goes from 2.5
hours down to 2 minutes. Because it's now a reasonable length, this patch uncomments
the bulk of the standard library.

* WebGPUShadingLanguageRI/All.js:
* WebGPUShadingLanguageRI/Lexer.js:
(Lexer):
(Lexer.prototype.lineNumberForIndex):
* WebGPUShadingLanguageRI/Parse.js:
(fail):
(backtrackingScope):
(testScope):
(genericConsume):
(consumeEndOfTypeArgs):
(parseTerm):
(parseConstexpr):
(parseTypeArguments):
(parseType.getAddressSpace):
(parseType):
(parseTypeDef):
(genericParseLeft):
(parseCallExpression.let.parseArguments):
(isCallExpression):
(parseSuffixOperator):
(parsePossibleSuffix):
(parsePreIncrement):
(parsePossiblePrefix):
(parsePossibleTernaryConditional):
(parsePossibleAssignment):
(parsePostIncrement):
(parseEffectfulExpression):
(genericParseCommaExpression):
(parseEffectfulStatement):
(parseReturn):
(parseBreak):
(parseContinue):
(parseIfStatement):
(parseWhile):
(parseFor):
(parseDo):
(parseVariableDecls):
(parseSwitchCase):
(parseSwitchStatement):
(parseStatement):
(parseBlockBody):
(parseBlock):
(parseParameter):
(parseFuncName):
(parseFuncDecl):
(parseFuncDef):
(parseField):
(parseStructType):
(parseNativeFunc):
(parseNative):
(parseRestrictedFuncDef):
(parseEnumMember):
(parseEnumType):
(parse):
* WebGPUShadingLanguageRI/SPIRV.html:
* WebGPUShadingLanguageRI/StandardLibrary.js:
(let.standardLibrary):
* WebGPUShadingLanguageRI/Test.html:
* WebGPUShadingLanguageRI/Test.js:
(checkFail.doPrep): Deleted.
* WebGPUShadingLanguageRI/WLexicalError.js: Added.
(WLexicalError):
* WebGPUShadingLanguageRI/index.html:

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@235738 268f45cc-cd09-0410-ab3c-d52691b4dbfc
12 files changed:
Tools/ChangeLog
Tools/WebGPUShadingLanguageRI/All.js
Tools/WebGPUShadingLanguageRI/Lexer.js
Tools/WebGPUShadingLanguageRI/LexerToken.js
Tools/WebGPUShadingLanguageRI/Parse.js
Tools/WebGPUShadingLanguageRI/SPIRV.html
Tools/WebGPUShadingLanguageRI/StandardLibrary.js
Tools/WebGPUShadingLanguageRI/Test.html
Tools/WebGPUShadingLanguageRI/Test.js
Tools/WebGPUShadingLanguageRI/WLexicalError.js [new file with mode: 0644]
Tools/WebGPUShadingLanguageRI/WSyntaxError.js
Tools/WebGPUShadingLanguageRI/index.html