-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from dakk/if-then-else
If-then-else and bool optimizer refactoring
- Loading branch information
Showing
20 changed files
with
587 additions
and
261 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,4 +13,5 @@ htmlcov | |
coverage.xml | ||
docs/build | ||
*.egg | ||
*.egg-info | ||
*.egg-info | ||
.t_statistics |
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
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,16 @@ | ||
# Copyright 2023 Davide Gessa | ||
|
||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
|
||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# isort:skip_file | ||
|
||
from .sympytransformer import SympyTransformer # noqa: F401 |
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,100 @@ | ||
# Copyright 2023 Davide Gessa | ||
|
||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
|
||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from typing import Dict | ||
|
||
from sympy import Symbol, cse | ||
from sympy.logic.boolalg import And, Boolean, Not, Or, Xor, simplify_logic | ||
|
||
from ..ast2logic import BoolExpList | ||
from . import SympyTransformer, deprecated | ||
from .exp_transformers import ( | ||
remove_Implies, | ||
remove_ITE, | ||
transform_or2and, | ||
transform_or2xor, | ||
) | ||
|
||
|
||
def custom_simplify_logic(expr): | ||
if isinstance(expr, Xor): | ||
return expr | ||
elif isinstance(expr, (And, Or, Not)): | ||
args = [custom_simplify_logic(arg) for arg in expr.args] | ||
return type(expr)(*args) | ||
else: | ||
return simplify_logic(expr) | ||
|
||
|
||
def merge_expressions(exps: BoolExpList) -> BoolExpList: | ||
n_exps = [] | ||
emap: Dict[Symbol, Boolean] = {} | ||
|
||
for s, e in exps: | ||
e = e.xreplace(emap) | ||
e = custom_simplify_logic(e) | ||
|
||
if s.name[0:4] != "_ret": | ||
emap[s] = e | ||
else: | ||
n_exps.append((s, e)) | ||
|
||
return n_exps | ||
|
||
|
||
def apply_cse(exps: BoolExpList) -> BoolExpList: | ||
lsts = list(zip(*exps)) | ||
repl, red = cse(list(lsts[1])) | ||
res = repl + list(zip(lsts[0], red)) | ||
return res | ||
|
||
|
||
class BoolOptimizerProfile: | ||
def __init__(self, steps): | ||
self.steps = steps | ||
|
||
def apply(self, exps): | ||
for opt in self.steps: | ||
if isinstance(opt, SympyTransformer): | ||
exps = list(map(lambda e: (e[0], opt.visit(e[1])), exps)) | ||
else: | ||
exps = opt(exps) | ||
return exps | ||
|
||
|
||
bestWorkingOptimizer = BoolOptimizerProfile( | ||
[ | ||
merge_expressions, | ||
apply_cse, | ||
remove_ITE(), | ||
remove_Implies(), | ||
transform_or2xor(), | ||
transform_or2and(), | ||
] | ||
) | ||
|
||
|
||
deprecatedWorkingOptimizer = BoolOptimizerProfile( | ||
[ | ||
deprecated.remove_const_exps, | ||
deprecated.remove_unnecessary_assigns, | ||
deprecated.merge_unnecessary_assigns, | ||
merge_expressions, | ||
apply_cse, | ||
remove_ITE(), | ||
remove_Implies(), | ||
transform_or2xor(), | ||
transform_or2and(), | ||
] | ||
) |
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
Oops, something went wrong.