Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
pystencils
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
pycodegen
pystencils
Commits
24a83b5c
Commit
24a83b5c
authored
1 year ago
by
Frederik Hennig
Browse files
Options
Downloads
Patches
Plain Diff
expanded various doc comments
parent
45c9fe80
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/pystencils/nbackend/kernelcreation/typification.py
+21
-16
21 additions, 16 deletions
src/pystencils/nbackend/kernelcreation/typification.py
with
21 additions
and
16 deletions
src/pystencils/nbackend/kernelcreation/typification.py
+
21
−
16
View file @
24a83b5c
...
@@ -25,7 +25,7 @@ NodeT = TypeVar("NodeT", bound=PsAstNode)
...
@@ -25,7 +25,7 @@ NodeT = TypeVar("NodeT", bound=PsAstNode)
class
UndeterminedType
(
PsNumericType
):
class
UndeterminedType
(
PsNumericType
):
def
create_constant
(
self
,
value
:
Any
)
->
Any
:
def
create_constant
(
self
,
value
:
Any
)
->
Any
:
return
None
return
None
def
_err
(
self
)
->
NoReturn
:
def
_err
(
self
)
->
NoReturn
:
raise
PsInternalCompilerError
(
"
Calling UndeterminedType.
"
)
raise
PsInternalCompilerError
(
"
Calling UndeterminedType.
"
)
...
@@ -92,16 +92,18 @@ class Typifier(Mapper):
...
@@ -92,16 +92,18 @@ class Typifier(Mapper):
"""
Typifier for untyped expressions.
"""
Typifier for untyped expressions.
The typifier, when called with an AST node, will attempt to figure out
The typifier, when called with an AST node, will attempt to figure out
the types for all untyped expressions within the node:
the types for all untyped expressions within the node.
Plain variables will be assigned a type according to `ctx.options.default_dtype`,
- Plain variables will be assigned a type according to `ctx.options.default_dtype`.
constants will be converted to typed constants according to the contextual typing scheme
- Constants will be converted to typed constants by applying the target type of the current context.
described below.
Contextual Typing
Contextual Typing
-----------------
-----------------
Starting at an expression
'
s root, the typifier attempts to expand a typing context as far as possible.
The contextual typifier covers the expression tree with disjoint typing contexts.
The idea is that all nodes covered by a typing context must have the exact same type.
Starting at an expression
'
s root, the typifier attempts to expand a typing context as far as possible
toward the leaves.
This happens implicitly during the recursive traversal of the expression tree.
This happens implicitly during the recursive traversal of the expression tree.
At an interior node, which is modelled as a function applied to a number of arguments, producing a result,
At an interior node, which is modelled as a function applied to a number of arguments, producing a result,
...
@@ -112,13 +114,17 @@ class Typifier(Mapper):
...
@@ -112,13 +114,17 @@ class Typifier(Mapper):
by the function signature, it will be the target type of the new context.
by the function signature, it will be the target type of the new context.
At the tree
'
s leaves, types are applied and checked. By the above propagation rule, all leaves that share a typing
At the tree
'
s leaves, types are applied and checked. By the above propagation rule, all leaves that share a typing
context must have the exact same type (modulo constness). Th
is
type
is
check
ed at variables, and applied to
context must have the exact same type (modulo constness). Th
ere the actual
type check
ing happens.
constants
.
If a variable is encountered and the context does not yet have a target type, it is set to the variable
'
s type
.
If a constant is encountered, it is typified using the current target type.
I
t may happen that the typifier arrives at a constant before the context
'
s target type could be figured out.
I
f no target type is known yet, the constant will first be instantiated as a DeferredTypedConstant,
In that case, the constant will first be instantiated as a DeferredTypedConstant,
and stashed in the context.
and stashed in the context.
As soon as the context learns its target type, it is applied to all deferred constants.
As soon as the context learns its target type, it is applied to all deferred constants.
In addition to leaves, some interior nodes may also have to be checked against the target type.
In particular, these are array accesses, struct member accesses, and calls to functions with a fixed
return type.
When a context is
'
closed
'
during the recursive unwinding, it shall be an error if it still contains unresolved
When a context is
'
closed
'
during the recursive unwinding, it shall be an error if it still contains unresolved
constants.
constants.
...
@@ -142,7 +148,7 @@ class Typifier(Mapper):
...
@@ -142,7 +148,7 @@ class Typifier(Mapper):
new_lhs
=
self
.
rec
(
lhs
.
expression
,
tc
)
new_lhs
=
self
.
rec
(
lhs
.
expression
,
tc
)
assert
tc
.
target_type
is
not
None
assert
tc
.
target_type
is
not
None
new_rhs
=
self
.
rec
(
rhs
.
expression
,
tc
)
new_rhs
=
self
.
rec
(
rhs
.
expression
,
tc
)
node
.
lhs
.
expression
=
new_lhs
node
.
lhs
.
expression
=
new_lhs
node
.
rhs
.
expression
=
new_rhs
node
.
rhs
.
expression
=
new_rhs
...
@@ -154,9 +160,8 @@ class Typifier(Mapper):
...
@@ -154,9 +160,8 @@ class Typifier(Mapper):
"""
"""
def rec(self, expr: Any, tc: TypeContext) -> ExprOrConstant
def rec(self, expr: Any, tc: TypeContext) -> ExprOrConstant
All visitor methods take an expression and the target type of the current context.
All visitor methods take an expression and the current type context.
They shall return the typified expression together with its type.
They shall return the typified expression, or throw `TypificationError` if typification fails.
The returned type shall always be non-const, so make sure to call deconstify if necessary.
"""
"""
def
typify_expression
(
def
typify_expression
(
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment