-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Transform: Add simple Dead Code Elimination to trim code paths
- Loading branch information
Showing
5 changed files
with
135 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# (C) Copyright 2018- ECMWF. | ||
# This software is licensed under the terms of the Apache Licence Version 2.0 | ||
# which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. | ||
# In applying this licence, ECMWF does not waive the privileges and immunities | ||
# granted to it by virtue of its status as an intergovernmental organisation | ||
# nor does it submit to any jurisdiction. | ||
|
||
""" | ||
Collection of utilities to perform Dead Code Elimination. | ||
""" | ||
from loki.visitors import Transformer | ||
from loki.expression.symbolic import simplify | ||
from loki.tools import flatten, as_tuple | ||
|
||
|
||
__all__ = ['dead_code_elimination', 'DeadCodeEliminationTransformer'] | ||
|
||
|
||
def dead_code_elimination(routine, use_simplify=True): | ||
""" | ||
Perform Dead Code Elimination on the given :any:`Subroutine` object. | ||
Parameters | ||
---------- | ||
routine : :any:`Subroutine` | ||
The subroutine to which to apply dead code elimination. | ||
simplify : boolean | ||
Use :any:`simplify` when evaluating expressions for branch pruning. | ||
""" | ||
|
||
transformer = DeadCodeEliminationTransformer(use_simplify=use_simplify) | ||
routine.body = transformer.visit(routine.body) | ||
|
||
|
||
class DeadCodeEliminationTransformer(Transformer): | ||
""" | ||
:any:`Transformer` class that removes provably unreachable code paths. | ||
The pirmary modification performed is to prune individual code branches | ||
under :any:`Conditional` nodes. | ||
Parameters | ||
---------- | ||
simplify : boolean | ||
Use :any:`simplify` when evaluating expressions for branch pruning. | ||
""" | ||
|
||
def __init__(self, use_simplify=True, **kwargs): | ||
super().__init__(**kwargs) | ||
self.use_simplify = use_simplify | ||
|
||
def visit_Conditional(self, o, **kwargs): | ||
condition = self.visit(o.condition, **kwargs) | ||
body = as_tuple(flatten(as_tuple(self.visit(o.body, **kwargs)))) | ||
else_body = as_tuple(flatten(as_tuple(self.visit(o.else_body, **kwargs)))) | ||
|
||
if self.use_simplify: | ||
condition = simplify(condition) | ||
|
||
if condition == 'True': | ||
return body | ||
|
||
if condition == 'False': | ||
return else_body | ||
|
||
return self._rebuild(o, tuple((condition,) + (body,) + (else_body,))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# (C) Copyright 2018- ECMWF. | ||
# This software is licensed under the terms of the Apache Licence Version 2.0 | ||
# which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. | ||
# In applying this licence, ECMWF does not waive the privileges and immunities | ||
# granted to it by virtue of its status as an intergovernmental organisation | ||
# nor does it submit to any jurisdiction. | ||
|
||
import pytest | ||
|
||
from conftest import available_frontends | ||
from loki import Subroutine, FindNodes, Conditional, Assignment | ||
from loki.transform import dead_code_elimination | ||
|
||
|
||
@pytest.mark.parametrize('frontend', available_frontends()) | ||
def test_transform_dead_code_conditional(frontend): | ||
""" | ||
Test correct elimination of unreachable conditional branches. | ||
""" | ||
fcode = """ | ||
subroutine test_dead_code_conditional(a, b) | ||
real(kind=8), intent(inout) :: a, b | ||
logical, intent(in) :: flag | ||
if (flag) then | ||
if (1 == 6) then | ||
a = a + b | ||
else | ||
b = b + 2.0 | ||
end if | ||
if (2 == 2) then | ||
b = b + a | ||
else | ||
a = a + 3.0 | ||
end if | ||
end if | ||
end subroutine test_dead_code_conditional | ||
""" | ||
routine = Subroutine.from_source(fcode, frontend=frontend) | ||
assert len(FindNodes(Conditional).visit(routine.body)) == 3 | ||
assert len(FindNodes(Assignment).visit(routine.body)) == 4 | ||
|
||
dead_code_elimination(routine) | ||
|
||
conditionals = FindNodes(Conditional).visit(routine.body) | ||
assert len(conditionals) == 1 | ||
assert conditionals[0].condition == 'flag' | ||
assigns = FindNodes(Assignment).visit(routine.body) | ||
assert len(assigns) == 2 | ||
assert assigns[0].lhs == 'b' and assigns[0].rhs == 'b + 2.0' | ||
assert assigns[1].lhs == 'b' and assigns[1].rhs == 'b + a' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters