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
50fbc0a9
Commit
50fbc0a9
authored
1 year ago
by
Daniel Bauer
Browse files
Options
Downloads
Patches
Plain Diff
do not hoist declarations of mutated variables
parent
4b2de595
Branches
Branches containing commit
Tags
Tags containing commit
1 merge request
!381
Do not hoist declarations of mutated variables
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/pystencils/backend/transformations/hoist_loop_invariant_decls.py
+10
-1
10 additions, 1 deletion
...ils/backend/transformations/hoist_loop_invariant_decls.py
tests/nbackend/transformations/test_hoist_invariants.py
+19
-0
19 additions, 0 deletions
tests/nbackend/transformations/test_hoist_invariants.py
with
29 additions
and
1 deletion
src/pystencils/backend/transformations/hoist_loop_invariant_decls.py
+
10
−
1
View file @
50fbc0a9
...
...
@@ -27,6 +27,7 @@ class HoistContext:
def
__init__
(
self
)
->
None
:
self
.
hoisted_nodes
:
list
[
PsDeclaration
]
=
[]
self
.
assigned_symbols
:
set
[
PsSymbol
]
=
set
()
self
.
mutated_symbols
:
set
[
PsSymbol
]
=
set
()
self
.
invariant_symbols
:
set
[
PsSymbol
]
=
set
()
def
_is_invariant
(
self
,
expr
:
PsExpression
)
->
bool
:
...
...
@@ -123,6 +124,7 @@ class HoistLoopInvariantDeclarations:
"""
Hoist invariant declarations out of the given loop.
"""
hc
=
HoistContext
()
hc
.
assigned_symbols
.
add
(
loop
.
counter
.
symbol
)
hc
.
mutated_symbols
.
add
(
loop
.
counter
.
symbol
)
self
.
_prepare_hoist
(
loop
.
body
,
hc
)
self
.
_hoist_from_block
(
loop
.
body
,
hc
)
return
hc
...
...
@@ -134,8 +136,12 @@ class HoistLoopInvariantDeclarations:
case
PsExpression
():
return
case
PsDeclaration
(
PsSymbolExpr
(
lhs_symb
),
_
):
hc
.
assigned_symbols
.
add
(
lhs_symb
)
case
PsAssignment
(
PsSymbolExpr
(
lhs_symb
),
_
):
hc
.
assigned_symbols
.
add
(
lhs_symb
)
hc
.
mutated_symbols
.
add
(
lhs_symb
)
case
PsAssignment
(
_
,
_
):
return
...
...
@@ -147,6 +153,7 @@ class HoistLoopInvariantDeclarations:
loop
=
stmt
nested_hc
=
self
.
_hoist
(
loop
)
hc
.
assigned_symbols
|=
nested_hc
.
assigned_symbols
hc
.
mutated_symbols
|=
nested_hc
.
mutated_symbols
statements_new
+=
nested_hc
.
hoisted_nodes
if
loop
.
body
.
statements
:
statements_new
.
append
(
loop
)
...
...
@@ -169,7 +176,9 @@ class HoistLoopInvariantDeclarations:
for
node
in
block
.
statements
:
if
isinstance
(
node
,
PsDeclaration
):
if
hc
.
_is_invariant
(
node
.
rhs
):
if
node
.
lhs
.
symbol
not
in
hc
.
mutated_symbols
and
hc
.
_is_invariant
(
node
.
rhs
):
hc
.
hoisted_nodes
.
append
(
node
)
hc
.
invariant_symbols
.
add
(
node
.
declared_symbol
)
else
:
...
...
This diff is collapsed.
Click to expand it.
tests/nbackend/transformations/test_hoist_invariants.py
+
19
−
0
View file @
50fbc0a9
...
...
@@ -193,3 +193,22 @@ def test_hoisting_eliminates_loops():
assert
isinstance
(
ast
,
PsBlock
)
# All statements are hoisted and the loops are removed
assert
ast
.
statements
==
invariant_decls
def
test_hoist_mutation
():
ctx
=
KernelCreationContext
()
factory
=
AstFactory
(
ctx
)
hoist
=
HoistLoopInvariantDeclarations
(
ctx
)
x
=
sp
.
Symbol
(
"
x
"
)
x_decl
=
factory
.
parse_sympy
(
Assignment
(
x
,
1
))
x_update
=
factory
.
parse_sympy
(
AddAugmentedAssignment
(
x
,
1
))
inner_loop
=
factory
.
loop
(
"
j
"
,
slice
(
10
),
PsBlock
([
x_update
]))
outer_loop
=
factory
.
loop
(
"
i
"
,
slice
(
10
),
PsBlock
([
x_decl
,
inner_loop
]))
result
=
hoist
(
outer_loop
)
# x is updated in the loop, so nothing can be hoisted
assert
isinstance
(
result
,
PsLoop
)
assert
result
.
body
.
statements
==
[
x_decl
,
inner_loop
]
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