Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add data dependency analyses #1657

Draft
wants to merge 15 commits into
base: master
Choose a base branch
from
37 changes: 21 additions & 16 deletions dace/codegen/control_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,9 +275,13 @@ def as_cpp(self, codegen, symbols) -> str:
expr += elem.as_cpp(codegen, symbols)
# In a general block, emit transitions and assignments after each individual block or region.
if isinstance(elem, BasicCFBlock) or (isinstance(elem, RegionBlock) and elem.region):
cfg = elem.state.parent_graph if isinstance(elem, BasicCFBlock) else elem.region.parent_graph
if isinstance(elem, BasicCFBlock):
g_elem = elem.state
else:
g_elem = elem.region
cfg = g_elem.parent_graph
sdfg = cfg if isinstance(cfg, SDFG) else cfg.sdfg
out_edges = cfg.out_edges(elem.state) if isinstance(elem, BasicCFBlock) else cfg.out_edges(elem.region)
out_edges = cfg.out_edges(g_elem)
for j, e in enumerate(out_edges):
if e not in self.gotos_to_ignore:
# Skip gotos to immediate successors
Expand Down Expand Up @@ -532,26 +536,27 @@ def as_cpp(self, codegen, symbols) -> str:
expr = ''

if self.loop.update_statement and self.loop.init_statement and self.loop.loop_variable:
# Initialize to either "int i = 0" or "i = 0" depending on whether the type has been defined.
defined_vars = codegen.dispatcher.defined_vars
if not defined_vars.has(self.loop.loop_variable):
try:
init = f'{symbols[self.loop.loop_variable]} '
except KeyError:
init = 'auto '
symbols[self.loop.loop_variable] = None
init += unparse_interstate_edge(self.loop.init_statement.code[0], sdfg, codegen=codegen, symbols=symbols)
init = unparse_interstate_edge(self.loop.init_statement.code[0], sdfg, codegen=codegen, symbols=symbols)
init = init.strip(';')

update = unparse_interstate_edge(self.loop.update_statement.code[0], sdfg, codegen=codegen, symbols=symbols)
update = update.strip(';')

if self.loop.inverted:
expr += f'{init};\n'
expr += 'do {\n'
expr += _clean_loop_body(self.body.as_cpp(codegen, symbols))
expr += f'{update};\n'
expr += f'\n}} while({cond});\n'
if self.loop.update_before_condition:
expr += f'{init};\n'
expr += 'do {\n'
expr += _clean_loop_body(self.body.as_cpp(codegen, symbols))
expr += f'{update};\n'
expr += f'}} while({cond});\n'
else:
expr += f'{init};\n'
expr += 'while (1) {\n'
expr += _clean_loop_body(self.body.as_cpp(codegen, symbols))
expr += f'if (!({cond}))\n'
expr += 'break;\n'
expr += f'{update};\n'
expr += '}\n'
else:
expr += f'for ({init}; {cond}; {update}) {{\n'
expr += _clean_loop_body(self.body.as_cpp(codegen, symbols))
Expand Down
13 changes: 12 additions & 1 deletion dace/codegen/targets/framecode.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@
from dace.codegen.prettycode import CodeIOStream
from dace.codegen.common import codeblock_to_cpp, sym2cpp
from dace.codegen.targets.target import TargetCodeGenerator
from dace.codegen.tools.type_inference import infer_expr_type
from dace.frontend.python import astutils
from dace.sdfg import SDFG, SDFGState, nodes
from dace.sdfg import scope as sdscope
from dace.sdfg import utils
from dace.sdfg.analysis import cfg as cfg_analysis
from dace.sdfg.state import ControlFlowRegion
from dace.sdfg.state import ControlFlowRegion, LoopRegion
from dace.transformation.passes.analysis import StateReachability


Expand Down Expand Up @@ -916,6 +918,15 @@ def generate_code(self,
interstate_symbols.update(symbols)
global_symbols.update(symbols)

if isinstance(cfr, LoopRegion) and cfr.loop_variable is not None and cfr.init_statement is not None:
init_assignment = cfr.init_statement.code[0]
if isinstance(init_assignment, astutils.ast.Assign):
init_assignment = init_assignment.value
if not cfr.loop_variable in interstate_symbols:
interstate_symbols[cfr.loop_variable] = infer_expr_type(astutils.unparse(init_assignment))
if not cfr.loop_variable in global_symbols:
global_symbols[cfr.loop_variable] = interstate_symbols[cfr.loop_variable]

for isvarName, isvarType in interstate_symbols.items():
if isvarType is None:
raise TypeError(f'Type inference failed for symbol {isvarName}')
Expand Down
2 changes: 2 additions & 0 deletions dace/frontend/python/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,8 @@ def _parse(self, args, kwargs, simplify=None, save=False, validate=False) -> SDF
sdutils.inline_control_flow_regions(nsdfg)
sdfg.using_experimental_blocks = self.use_experimental_cfg_blocks

sdfg.reset_cfg_list()

# Apply simplification pass automatically
if not cached and (simplify == True or
(simplify is None and Config.get_bool('optimizer', 'automatic_simplification'))):
Expand Down
Loading
Loading