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
c0f2fb54
Commit
c0f2fb54
authored
1 year ago
by
Frederik Hennig
Browse files
Options
Downloads
Patches
Plain Diff
Add one more test case and doc comments
parent
f1486c07
No related branches found
No related tags found
2 merge requests
!373
Symbol Canonicalization, Loop-Invariant Code Motion, and AST Factory
,
!372
Fix handling of constness in Typifier
Pipeline
#64863
passed
1 year ago
Stage: Code Quality
Stage: Unit Tests
Stage: legacy_test
Stage: docs
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/pystencils/backend/kernelcreation/typification.py
+23
-4
23 additions, 4 deletions
src/pystencils/backend/kernelcreation/typification.py
tests/nbackend/kernelcreation/test_typification.py
+12
-4
12 additions, 4 deletions
tests/nbackend/kernelcreation/test_typification.py
with
35 additions
and
8 deletions
src/pystencils/backend/kernelcreation/typification.py
+
23
−
4
View file @
c0f2fb54
...
...
@@ -55,6 +55,17 @@ NodeT = TypeVar("NodeT", bound=PsAstNode)
class
TypeContext
:
"""
Typing context, with support for type inference and checking.
Instances of this class are used to propagate and check data types across expression subtrees
of the AST. Each type context has:
- A target type `target_type`, which shall be applied to all expressions it covers
- A set of restrictions on the target type:
- `require_nonconst` to make sure the target type is not `const`, as required on assignment left-hand sides
- Additional restrictions may be added in the future.
"""
def
__init__
(
self
,
target_type
:
PsType
|
None
=
None
,
require_nonconst
:
bool
=
False
):
...
...
@@ -222,11 +233,11 @@ class Typifier:
**Typing Rules**
The following rules apply:
The following
general
rules apply:
- The context
'
s `default_dtype` is applied to all untyped symbols
- By default, all expressions receive a ``const`` type unless
o
the
rwise required
- The
left-hand side
of any non-declaration assignment must not be ``const``
- By default, all expressions receive a ``const`` type unless the
y occur on a (non-declaration) assignment
'
s
left-hand side
**Typing of symbol expressions**
...
...
@@ -304,7 +315,15 @@ class Typifier:
raise
NotImplementedError
(
f
"
Can
'
t typify
{
node
}
"
)
def
visit_expr
(
self
,
expr
:
PsExpression
,
tc
:
TypeContext
)
->
None
:
"""
Recursive processing of expression nodes
"""
"""
Recursive processing of expression nodes.
This method opens, expands, and closes typing contexts according to the respective expression
'
s
typing rules. It may add or check restrictions only when opening or closing a type context.
The actual type inference and checking during context expansion are performed by the methods
of `TypeContext`. ``visit_expr`` tells the typing context how to handle an expression by calling
either ``apply_dtype`` or ``infer_dtype``.
"""
match
expr
:
case
PsSymbolExpr
(
_
):
if
expr
.
symbol
.
dtype
is
None
:
...
...
This diff is collapsed.
Click to expand it.
tests/nbackend/kernelcreation/test_typification.py
+
12
−
4
View file @
c0f2fb54
...
...
@@ -6,7 +6,7 @@ from typing import cast
from
pystencils
import
Assignment
,
TypedSymbol
,
Field
,
FieldType
from
pystencils.backend.ast.structural
import
PsDeclaration
from
pystencils.backend.ast.structural
import
PsDeclaration
,
PsAssignment
,
PsExpression
from
pystencils.backend.ast.expressions
import
PsConstantExpr
,
PsSymbolExpr
,
PsBinOp
from
pystencils.backend.constants
import
PsConstant
from
pystencils.types
import
constify
...
...
@@ -127,6 +127,17 @@ def test_lhs_constness():
with
pytest
.
raises
(
TypificationError
):
_
=
typify
(
freeze
(
Assignment
(
struct_field
.
absolute_access
([
0
],
"
data
"
),
x
)))
# Const LHS is only OK in declarations
q
=
ctx
.
get_symbol
(
"
q
"
,
Fp
(
32
,
const
=
True
))
ast
=
PsDeclaration
(
PsExpression
.
make
(
q
),
PsExpression
.
make
(
q
))
ast
=
typify
(
ast
)
assert
ast
.
lhs
.
dtype
==
Fp
(
32
,
const
=
True
)
ast
=
PsAssignment
(
PsExpression
.
make
(
q
),
PsExpression
.
make
(
q
))
with
pytest
.
raises
(
TypificationError
):
typify
(
ast
)
def
test_typify_structs
():
ctx
=
KernelCreationContext
(
default_dtype
=
Fp
(
32
))
...
...
@@ -278,6 +289,3 @@ def test_typify_constant_clones():
assert
expr_clone
.
operand1
.
dtype
is
None
assert
cast
(
PsConstantExpr
,
expr_clone
.
operand1
).
constant
.
dtype
is
None
# test_lhs_constness()
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