Skip to content

Commit

Permalink
#49: implemented constant rewriting for shared. minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
marco-biasion committed Jul 18, 2024
1 parent 8ba8b1b commit 7535b25
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 28 deletions.
29 changes: 23 additions & 6 deletions sxpat/synthesis.py
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ def __intact_part_assigns(self):
f'{pn[0]} {sxpatconfig.VER_OR} ' \
f'{pn[1]};\n'
else:
pprint.error(f'ERROR!!! node {n} has more than two drivers!')
pprint.error(f'ERROR!!! node {n_name} has more than two drivers!')
exit(1)

return intact_part
Expand Down Expand Up @@ -970,10 +970,27 @@ def __annotated_graph_to_verilog_shared(self):
shared_assigns = self.__shared_logic_assigns_subxpat_shared(idx=idx)
output_assigns = self.__output_assigns()

ver_str += (module_signature + io_declaration + intact_wires + annotated_graph_input_wires + json_input_wires
+ annotated_graph_input_wires + annotated_graph_output_wires + json_output_wires + json_model_wires)
ver_str += (json_input_assign + subgraph_to_json_input_mapping + intact_assigns
+ json_model_and_subgraph_outputs_assigns + shared_assigns + output_assigns)
#
json_model_constants_rewrite = self.__json_model_output_constants_assign(idx)

ver_str += (
module_signature
+ io_declaration
+ intact_wires
+ annotated_graph_input_wires
+ json_input_wires
+ annotated_graph_input_wires
+ annotated_graph_output_wires
+ json_output_wires
+ json_model_wires
+ json_input_assign
+ subgraph_to_json_input_mapping
+ intact_assigns
+ json_model_and_subgraph_outputs_assigns
+ shared_assigns
+ json_model_constants_rewrite
+ output_assigns
)

ver_string.append(ver_str)

Expand Down Expand Up @@ -1156,7 +1173,7 @@ def __magraph_to_verilog(self):

ver_str += f'{sxpatconfig.VER_ENDMODULE}'
return ver_str

# =========================

@staticmethod
Expand Down
3 changes: 0 additions & 3 deletions sxpat/template_manager/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@ def z3_abs_diff(a, b):
{{{{params_declaration}}}}
params_list = {{{{params_list}}}}

# Constants variables declaration
{{{{consts_declaration}}}}

# wires functions declaration for exact circuit
{{{{exact_wires_declaration}}}}

Expand Down
46 changes: 27 additions & 19 deletions sxpat/template_manager/template_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,12 +214,7 @@ def _use_exact_var(self, node_name: str) -> str:

# is constant
if node_name in self.exact_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():
output_i = mapping_inv(self._current_graph.output_dict, succs[0])
return f'{sxpat_cfg.CONSTANT_PREFIX}{output_i}'
else:
return sxpat_cfg.Z3_GATES_DICTIONARY[self._exact_graph.graph.nodes[node_name][sxpat_cfg.LABEL]]
return sxpat_cfg.Z3_GATES_DICTIONARY[self._exact_graph.graph.nodes[node_name][sxpat_cfg.LABEL]]

# is gate
if node_name in self.exact_gates.values():
Expand All @@ -243,7 +238,12 @@ def _use_approx_var(self, node_name: str) -> str:

# is constant
if node_name in self.current_constants.values():
return sxpat_cfg.Z3_GATES_DICTIONARY[self._current_graph.graph.nodes[node_name][sxpat_cfg.LABEL]]
succs = list(self._current_graph.graph.successors(node_name))
if len(succs) == 1 and succs[0] in self._current_graph.output_dict.values():
output_i = mapping_inv(self._current_graph.output_dict, succs[0])
return f'{sxpat_cfg.CONSTANT_PREFIX}{output_i}'
else:
return sxpat_cfg.Z3_GATES_DICTIONARY[self._current_graph.graph.nodes[node_name][sxpat_cfg.LABEL]]

# is gate
if node_name in self.current_gates.values():
Expand Down Expand Up @@ -292,6 +292,14 @@ def _generate_product(self, parameter_pair_gen: Callable[[int], Tuple[str, str]]
multiplexers.append(f'Or(Not({p_s}), {p_l} == {(input_name)})')
return f'And({", ".join(multiplexers)})'

def _generate_constants_parameters(self) -> Iterable[str]:
def get_single_predecessor(out_name): return next(self._current_graph.graph.predecessors(out_name))
return (
f'{sxpat_cfg.CONSTANT_PREFIX}{output_i}'
for output_i, output_name in self.outputs.items()
if get_single_predecessor(output_name) in self.current_constants.values()
)

#

def _update_builder(self, builder: Builder) -> None:
Expand Down Expand Up @@ -322,15 +330,6 @@ def _update_builder(self, builder: Builder) -> None:
# error
builder.update(error=self._encoding.output_variable('error'))

# consts_declaration
def get_single_predecessor(out_name): return next(self._current_graph.graph.predecessors(out_name))
lines = [
self._gen_declare_gate(f'{sxpat_cfg.CONSTANT_PREFIX}{output_i}')
for output_i, output_name in self.outputs.items()
if get_single_predecessor(output_name) in self.current_constants.values()
]
builder.update(consts_declaration='\n'.join(lines))

# exact_wires_declaration
builder.update(exact_wires_declaration='\n'.join(
self._gen_declare_bool_function(f'{sxpat_cfg.EXACT_WIRES_PREFIX}{len(self.inputs) + gate_i}', len(self.inputs))
Expand Down Expand Up @@ -457,10 +456,14 @@ def _update_builder(self, builder: Builder) -> None:

# params_declaration and params_list
params = list(itertools.chain(
# constant outputs
self._generate_constants_parameters(),
# output parameters
(
self._output_parameter(output_i)
for output_i in self.subgraph_outputs.keys()
),
# input parameters
itertools.chain.from_iterable(
self._input_parameters(output_i, product_i, input_i)
for output_i in self.subgraph_outputs.keys()
Expand Down Expand Up @@ -600,16 +603,21 @@ def _update_builder(self, builder: Builder) -> None:

# params_declaration and params_list
params = list(itertools.chain(
( # p_o#
# constant outputs
self._generate_constants_parameters(),
# p_o#
(
self._output_parameter(output_i)
for output_i in self.subgraph_outputs.keys()
),
itertools.chain.from_iterable( # p_pr#_i#
# p_pr#_i#
itertools.chain.from_iterable(
self._input_parameters(None, product_i, input_i)
for product_i in range(self._specs.pit)
for input_i in self.subgraph_inputs.keys()
),
( # p_pr#_o#
# p_pr#_o#
(
self._product_parameter(output_i, product_i)
for output_i in self.subgraph_outputs.keys()
for product_i in range(self._specs.pit)
Expand Down

0 comments on commit 7535b25

Please sign in to comment.