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

Conditional Blocks #1666

Merged
merged 44 commits into from
Sep 27, 2024
Merged

Conditional Blocks #1666

merged 44 commits into from
Sep 27, 2024

Conversation

phschaad
Copy link
Collaborator

@phschaad phschaad commented Sep 24, 2024

This is a continuation of #1617 (superseded and closed by this PR), with a lot of the work being done by @luca-patrignani.

Conditional Blocks

This PR implements Conditional Blocks, which are a native way of semantically expressing conditional branching in an SDFG. This replaces the traditional "state machine only" way of expressing conditional branching, with two main goals:

  1. Simplify SDFG analysis and optimization by clearly exposing conditional branching. Previously, detecting and treating conditional branches required expensive analysis of the control flow graph structure, which had to be performed repeatedly and was error prone. By contrast, Conditional Blocks can be generated by a frontend using semantic information from the source language, entirely circumventing this step.
  2. Address code generation issues. Code generation relies on a series of control flow detections to generate appropriate code that is not full of goto statements for each state transition. However, just as in the above issue, this process is error prone and often leads to invalid code being generated for complex control flow constructs (e.g., conditionals inside of loops with conditional break, continue, return, etc.). By exposing all regular control flow (i.e., loops and conditional branching) with native SDFG constructs, this step can be skipped in code generation.

Anatomy of Conditional Blocks

ConditionalBlocks are a type of ControlFlowBlock which contains a series of branches. Each branch is represented by a full ControlFlowRegion and has a condition in the form of a CodeBlock attached to it. When a ConditionalBlock is executed, the conditions are checked in the insertion order of the branches, and if a matching condition was found, that branch (and only that branch) is executed. When the executed branch finishes executing, the ConditionalBlock's successor is next. If no condition matches, no branch is executed.

The condition for a single branch at a time may be None, which represents a wildcard or else case that is executed if no conditions match.

Code Generation Changes

Code generation (when using this feature) is drastically simplified with respect to control flow: no more control flow detection is performed. Instead, regular control flow constructs are only generated from the new native SDFG constructs (LoopRegions and ConditionalBlocks), and any other state transition is either only used for sequential ordering (unconditional transitions to a single, direct successor), or leads to a goto. This makes code generation significantly less error prone and simpler to work with.

Compatibility

This feature is implemented minimally invasive and with full backwards compatibility for now.
Just as with LoopRegions, this feature is only used if an explicit use_experimental_cfg_blocks flag is set to True in compatible frontends (currently only Python frontend, Fortran frontend integration is coming soon).

If an SDFG makes use of these experimental blocks, some passes and transformations will no longer be applied automatically in pipelines. Transformations that handle these blocks correctly can be explicitly marked with @transformation.experimental_cfg_block_compatible to apply them on SDFGs with experimental blocks.

Inlining

Conditional blocks can be inlined through a utility function to traditional SDFG state machines. This is automatically done by compatible frontends if the experimental CFG blocks feature is turned off.

Visualization Components

The visualization components are being worked on separately in spcl/dace-webclient#173. This PR does not depend on the visualization components to be merged.

luca-patrignani and others added 30 commits June 30, 2024 15:41
@phschaad phschaad changed the title Conditional Regions Conditional Blocks Sep 26, 2024
@phschaad phschaad marked this pull request as ready for review September 26, 2024 12:13
Copy link
Collaborator

@tbennun tbennun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See minor comments

for node, parent in loop_region.all_nodes_recursive(lambda n, _: not isinstance(n, (LoopRegion, SDFGState))):
if isinstance(node, BreakBlock):
for in_edge in parent.in_edges(node):
in_edge.data.assignments["did_break_" + loop_region.label] = "1"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we should add a __dace_ prefix to avoid name clashes. This is also true in the _generate_orelse code.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, agree - done.

dace/frontend/python/newast.py Outdated Show resolved Hide resolved
Comment on lines +2610 to +2616
dead_blocks = [succ for succ in parent.successors(self) if parent.in_degree(succ) == 1]
while dead_blocks:
layer = list(dead_blocks)
dead_blocks.clear()
for u in layer:
dead_blocks.extend([succ for succ in parent.successors(u) if parent.in_degree(succ) == 1])
parent.remove_node(u)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this not be part of a transformation / pass like dead state elimination? I am ok with this here now but I still would want a TODO note or TODO(later)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point - I am placing a TODO and will adjust this in the PR that adapts the passes to these changes.

@tbennun tbennun added this pull request to the merge queue Sep 27, 2024
Merged via the queue into master with commit 1dc9bc5 Sep 27, 2024
10 checks passed
@tbennun tbennun deleted the users/phschaad/conditional_regions branch September 27, 2024 16:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants