-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
[Refactor] Split transformer.py into StmtBuilder and ExprBuilder (Stage 1) #2495
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for this huge refactoring!
- How many stages do you expect there will be?
- Could you write some comments to guide the review process?
# note that we can only return an ast.Expr instead of an ast.Call. | ||
return node | ||
# A statement with a single expression. | ||
# TODO(#2495): Deprecate maybe_transform_ti_func_call_to_stmt |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder what's the situation with maybe_transform_ti_func_call_to_stmt
after this PR? Also does this method generate a ExprStmt?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No. This PR should not modify the behavior of the Python frontend (the whole transformer.py together with new files introduced in this PR).
|
Do we support Python 3.9 now? |
Yep |
Is NamedExpr unsupported in Python 3.6? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
FYI you don't have to |
Related issue = #2193
This PR replaces
ASTTransformerPreprocess
withStmtBuilder
andExprBuilder
.This PR doesn't modify the behavior (and the code) of the other 2 passes in transformer.py.
Detailed changeset:
ASTTransformerPreprocess
toBuilderContext
. These members should be context instead of members of a pass.ScopeGuard
,parse_stmt
,parse_expr
to ast_builder_utils.py.ASTTransformerPreprocess
fromvisit_Xxx
tobuild_Xxx
and move them to expr_builder.py and stmt_builder.py.generic_visit
inASTTransformerPreprocess
in transformer.py. Since we need to know if it's a statement or an expr before building the subtree of AST, we cannot use a generalgeneric_visit
. The invocations of this function is rewritten asbuild_expr
,build_exprs
,build_stmt
,build_stmts
.build_exprs
andbuild_stmts
create avariable_scope
. Also note that I do not use a list comprehension in these two functions becausewith
will not work properly otherwise.build_Module
does not usebuild_stmts
, just like its not usinggeneric_visit
before this PR. This is because if we create avariable_scope
associated with the statement list (Python list here), the parameters passed into the Taichi kernel will be deleted when destructing the variable scope.build_Name
is not implemented inExprBuilder
and we invokebuild_expr
with a Name node as the parameter).global
/nonlocal
is used or when a Python set (including set comprehension) is used.visit_Xxx
before this PR. I think there's a benefit of writing these functions explicitly -- they show which features in Python are supported in Taichi, and throw exceptions when users use features in Python that are not considered by Taichi developers, which may or may not be supported.Note:
a = f'aaa{123}'
(ast.JoinedStr, together with ast.FormattedValue)b = a[1:2]
(ast.Slice)b = (n**2 for n in aa if n>5 if n<10)
(ast.GeneratorExp)a = lambda x, y: x + y
(ast.Lambda)(b): ti.i32 = 1
(ast.AnnAssign)yield a
andyield from a
(ast.Yield and ast.YieldFrom){1, 2, 3}
and{x for x in numbers}
(ast.Set and ast.SetComp)class Foo: ...
(ast.ClassDef)def foo()
(ast.FunctionDef) in a kernelglobal
andnonlocal
throw TaichiSyntaxError after this PR. They may crash later before this PR.