Skip to content

Commit

Permalink
#49: Added a parameter to toggle constants version
Browse files Browse the repository at this point in the history
  • Loading branch information
Francesco-Cos committed Oct 18, 2024
1 parent 0ee446e commit 13faecd
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
10 changes: 10 additions & 0 deletions sxpat/specifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ class TemplateType(enum.Enum):
NON_SHARED = 'nonshared'
SHARED = 'shared'

class ConstantsType(enum.Enum):
NEVER = 'never'
OPTIMIZE = 'optimize'
ALWAYS = 'always'

class EnumChoicesAction(argparse.Action):
def __init__(self, *args, type: enum.Enum, **kwargs) -> None:
Expand Down Expand Up @@ -59,6 +63,7 @@ class Specifications:
subxpat: bool
template: TemplateType
encoding: EncodingType
constants: ConstantsType
wanted_models: int
iteration: int = dc.field(init=False, default=None) # rw
# exploration (2)
Expand Down Expand Up @@ -217,6 +222,11 @@ def parse_args(cls):
default=1,
help='Wanted number of models to generate for each step')

_consts = parser.add_argument('--constants',
type=ConstantsType,
action=EnumChoicesAction,
default=ConstantsType.NEVER,
help='The way we treat constants')
# > error stuff

_et = parser.add_argument('--max-error', '-e',
Expand Down
8 changes: 4 additions & 4 deletions sxpat/synthesis.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from .annotatedGraph import AnnotatedGraph
from .config import paths as sxpatpaths
from .config import config as sxpatconfig
from .specifications import Specifications, TemplateType
from .specifications import Specifications, TemplateType, ConstantsType


class Synthesis:
Expand Down Expand Up @@ -373,7 +373,7 @@ def __intact_part_assigns(self):
# write constants that are not output constants
for n_idx, n_name in self.graph.constant_dict.items():
const_succs = list(self.graph.graph.successors(n_name))
if len(const_succs) == 1 and const_succs[0] in self.graph.output_dict.values():
if len(const_succs) == 1 and const_succs[0] in self.graph.output_dict.values() and self.__template_specs.constants == ConstantsType.ALWAYS:
# skip if it is a constant output
continue

Expand Down Expand Up @@ -694,7 +694,7 @@ def __annotated_graph_to_verilog_shared(self):
output_assigns = self.__output_assigns()

#
json_model_constants_rewrite = self.__json_model_output_constants_assign(idx)
json_model_constants_rewrite = self.__json_model_output_constants_assign(idx) if self.__template_specs.constants == ConstantsType.ALWAYS else ''

ver_str += (
module_signature
Expand Down Expand Up @@ -761,7 +761,7 @@ def __annotated_graph_to_verilog(self) -> List[AnyStr]:
json_model_and_subgraph_outputs_assigns = self.__json_model_lpp_and_subgraph_output_assigns(idx)

#
json_model_constants_rewrite = self.__json_model_output_constants_assign(idx)
json_model_constants_rewrite = self.__json_model_output_constants_assign(idx) if self.__template_specs.constants == ConstantsType.ALWAYS else ''

# 11. the intact assigns
intact_assigns = self.__intact_part_assigns()
Expand Down
6 changes: 3 additions & 3 deletions sxpat/template_manager/template_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from sxpat.annotatedGraph import AnnotatedGraph
import sxpat.config.config as sxpat_cfg
import sxpat.config.paths as sxpat_paths
from sxpat.specifications import EncodingType, Specifications, TemplateType
from sxpat.specifications import EncodingType, Specifications, TemplateType, ConstantsType
from .encoding import Encoding
from sxpat.utils.collections import mapping_inv, pairwise_iter

Expand Down Expand Up @@ -239,7 +239,7 @@ def _use_approx_var(self, node_name: str) -> str:
# is constant
if node_name in self.current_constants.values():
succs = list(self._current_graph.graph.successors(node_name))
if len(succs) == 1 and succs[0] in self._current_graph.output_dict.values():
if len(succs) == 1 and succs[0] in self._current_graph.output_dict.values() and self._specs.constants == ConstantsType.ALWAYS:
output_i = mapping_inv(self._current_graph.output_dict, succs[0])
return f'{sxpat_cfg.CONSTANT_PREFIX}{output_i}'
else:
Expand Down Expand Up @@ -458,7 +458,7 @@ def _update_builder(self, builder: Builder) -> None:
# params_declaration and params_list
params = list(itertools.chain(
# constant outputs
self._generate_constants_parameters(),
self._generate_constants_parameters() if self._specs.constants == ConstantsType.ALWAYS else (),
# output parameters
(
self._output_parameter(output_i)
Expand Down

0 comments on commit 13faecd

Please sign in to comment.