diff options
| author | Sai Praveen Bangaru <31557731+saipraveenb25@users.noreply.github.com> | 2022-06-23 16:02:05 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-06-23 16:02:05 -0400 |
| commit | 6cf3d496005c5635b273d9ce6c110f14541a9492 (patch) | |
| tree | 91d6480cec20ca88e85fb9b4d437e4661fba229c /source/slang/slang-parser.cpp | |
| parent | 4aa6344f772d31c1f7b0676cbaf315104c4b30a2 (diff) | |
Added basic syntax to mark and request function derivatives, as well as the framework for passes to process them. (#2297)
* Added a decorator to mark functions for forward-mode differentiation
* Fill out support for calls to non-decl values
The existing compiler logic has a few places (semantic checking plus AST-to-IR lowering) where it assumes that function calls (`InvokeExpr`) are only ever made to expressions that resolve to a specific `Decl` (`DeclRefExpr`). This assumption allows semantic checking and lowering code to inspect things like the parameter list of an actual declaration, rather than just the type signature of the callee, and that infrastructure is used to support various features (e.g., default argument values on parameters).
The AST and IR representations themselves have no matching requirement, and the places where the more general case of call expressions would need to be supported were relatively clear in the code. This change attempts to add suitable logic into each of those places.
Note that this change does *not* surface any valid way to form input code that would cause these new code paths to be executed, so it is entirely possible that there are bugs in the logic as written here. The primary goal of this change is simply to get a sketch of the correct code checked in so that we have something to build on once we have language features that will require this support.
* fixup: warnings-as-errors
* Added parser logic for '__jvp(<fn-name>)' operator
* Fixed issue with missing overload candidate item and added basic parsing test for the __jvp syntax
* Added a blank JVP Auto-diff pass and a pass that replaces 'JVPDerivativeOf' calls with the differentiated function
* Added a couple comments
* Added parameter handling for the JVP pass
Co-authored-by: Theresa Foley <tfoley@nvidia.com>
Diffstat (limited to 'source/slang/slang-parser.cpp')
| -rw-r--r-- | source/slang/slang-parser.cpp | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/source/slang/slang-parser.cpp b/source/slang/slang-parser.cpp index f6d85ade9..ee34eac6f 100644 --- a/source/slang/slang-parser.cpp +++ b/source/slang/slang-parser.cpp @@ -2056,6 +2056,25 @@ namespace Slang { return parseTaggedUnionType(parser); } + /// Parse an expression of the form __jvp(fn) where fn is an + /// identifier pointing to a function. + static Expr* parseJVPDerivativeOf(Parser* parser) + { + JVPDerivativeOfExpr* jvpExpr = parser->astBuilder->create<JVPDerivativeOfExpr>(); + + parser->ReadToken(TokenType::LParent); + + jvpExpr->baseFn = parser->ParseExpression(); + + parser->ReadToken(TokenType::RParent); + + return jvpExpr; + } + + static NodeBase* parseJVPDerivativeOf(Parser* parser, void* /* unused */) + { + return parseJVPDerivativeOf(parser); + } /// Parse a `This` type expression static Expr* parseThisTypeExpr(Parser* parser) @@ -6473,6 +6492,7 @@ namespace Slang _makeParseExpr("nullptr", parseNullPtrExpr), _makeParseExpr("try", parseTryExpr), _makeParseExpr("__TaggedUnion", parseTaggedUnionType), + _makeParseExpr("__jvp", parseJVPDerivativeOf) }; ConstArrayView<SyntaxParseInfo> getSyntaxParseInfos() |
