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 CFG to generate_scope in tutorials #1706

Merged
merged 5 commits into from
Oct 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 23 additions & 21 deletions tutorials/codegen.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -480,66 +480,68 @@
" self.frame = frame_codegen\n",
" # Can be used to dispatch other code generators for allocation/nodes\n",
" self.dispatcher = frame_codegen.dispatcher\n",
" \n",
"\n",
" ################################################################\n",
" # Register handlers/hooks through dispatcher: Can be used for \n",
" # Register handlers/hooks through dispatcher: Can be used for\n",
" # nodes, memory copy/allocation, scopes, states, and more.\n",
" \n",
"\n",
" # In this case, register scopes\n",
" self.dispatcher.register_map_dispatcher(dace.ScheduleType.LoopyLoop, self)\n",
" \n",
"\n",
" # You can similarly use register_{array,copy,node,state}_dispatcher\n",
" \n",
" # A scope dispatcher will trigger a method called generate_scope whenever \n",
"\n",
" # A scope dispatcher will trigger a method called generate_scope whenever\n",
" # an SDFG has a scope with that schedule\n",
" def generate_scope(self, sdfg: dace.SDFG, scope: ScopeSubgraphView,\n",
" state_id: int, function_stream: CodeIOStream,\n",
" callsite_stream: CodeIOStream):\n",
" def generate_scope(self, sdfg: dace.SDFG, cfg: dace.ControlFlowRegion,\n",
" scope: ScopeSubgraphView, state_id: int,\n",
" function_stream: CodeIOStream, callsite_stream: CodeIOStream):\n",
" # The parameters here are:\n",
" # sdfg: The SDFG we are currently generating.\n",
" # cfg: The current control flow graph (CFG) we are currently generating. For example,
" it can be the SDFG or a loop region.
" # scope: The subgraph of the state containing only the scope (map contents)\n",
ThrudPrimrose marked this conversation as resolved.
Show resolved Hide resolved
" # we want to generate the code for.\n",
" # state_id: The state in the SDFG the subgraph is taken from (i.e., \n",
" # state_id: The state in the SDFG the subgraph is taken from (i.e.,\n",
" # `sdfg.node(state_id)` is the same as `scope.graph`)\n",
" # function_stream: A cursor to the global code (which can be used to define\n",
" # functions, hence the name).\n",
" # callsite_stream: A cursor to the current location in the code, most of\n",
" # the code is generated here.\n",
" \n",
"\n",
" # We can get the map entry node from the scope graph\n",
" entry_node = scope.source_nodes()[0]\n",
" \n",
"\n",
" # First, generate an opening brace (for instrumentation and dynamic map ranges)\n",
" callsite_stream.write('{', sdfg, state_id, entry_node)\n",
" \n",
"\n",
" ################################################################\n",
" # Generate specific code: We will generate a reversed loop with a \n",
" # Generate specific code: We will generate a reversed loop with a\n",
" # comment for each dimension of the map. For the sake of simplicity,\n",
" # dynamic map ranges are not supported.\n",
" \n",
"\n",
" for param, rng in zip(entry_node.map.params, entry_node.map.range):\n",
" # We use the sym2cpp function from the cpp support functions\n",
" # to convert symbolic expressions to proper C++\n",
" begin, end, stride = (sym2cpp(r) for r in rng)\n",
" \n",
"\n",
" # Every write is optionally (but recommended to be) tagged with\n",
" # 1-3 extra arguments, serving as line information to match\n",
" # SDFG, state, and graph nodes/edges to written code.\n",
" callsite_stream.write(f'''// Loopy-loop {param}\n",
" for (auto {param} = {end}; {param} >= {begin}; {param} -= {stride}) {{''',\n",
" sdfg, state_id, entry_node\n",
" )\n",
" \n",
"\n",
" # NOTE: CodeIOStream will automatically take care of indentation for us.\n",
" \n",
" \n",
"\n",
"\n",
" # Now that the loops have been defined, use the dispatcher to invoke any\n",
" # code generator (including this one) that is registered to deal with\n",
" # the internal nodes in the subgraph. We skip the MapEntry node.\n",
" self.dispatcher.dispatch_subgraph(sdfg, scope, state_id,\n",
" self.dispatcher.dispatch_subgraph(sdfg, cfg, scope, state_id,\n",
" function_stream, callsite_stream,\n",
" skip_entry_node=True)\n",
" \n",
"\n",
" # NOTE: Since skip_exit_node above is set to False, closing braces will\n",
" # be automatically generated"
]
Expand Down
Loading