loki.ir.ir_graph module
GraphCollector classes for IR
- HAVE_IR_GRAPH = False
Indicate wheater the graphviz package is available.
- class GraphCollector(show_comments=False, show_expressions=False, linewidth=40, symgen=<class 'str'>)
Bases:
Visitor
Convert a given IR tree to a node and edge list via the visit mechanism.
This serves as base class for backends and provides a number of helpful routines that ease implementing automatic recursion and line wrapping. It is adapted from the Stringifier in “pprint.py”. It doubles as a means to produce a human readable graph representation of the IR, which is useful for debugging purposes and first visualization.
- Parameters:
linewidth (int, optional) – The line width limit after which to break a line.
symgen (str, optional) – A function handle that accepts a
pymbolic.primitives.Expression
and produces a string representation for that.show_comments (bool, optional, default: False) – Whether to show comments in the output
show_expressions (bool, optional, default: False) – Whether to further expand expressions in the output
- property symgen
Formatter for expressions.
- join_items(items, sep=', ', separable=True)
Concatenate a list of items into
JoinableStringList
.The return value can be passed to
format_line()
orformat_node()
or converted to a string with str, using theJoinableStringList
as an argument. Upon expansion, lines will be wrapped automatically to stay within the linewidth limit.- Parameters:
items (list) – The list of strings to be joined.
sep (str, optional) – The separator to be inserted between items.
separable (bool, optional) – Allow line breaks between individual
items
.
- Return type:
- format_node(name, *items)
Default format for a node.
Creates a string of the form
<name[, attribute, attribute, ...]>
.
- format_line(*items, comment=None, no_wrap=False)
Format a line by concatenating all items and applying indentation while observing the allowed line width limit.
Note that the provided comment will simply be extended to the line and no line width limit will be enforced for that.
- Parameters:
items (list) – The items to be put on that line.
comment (str) – An optional inline comment to be put at the end of the line.
no_wrap (bool) – Disable line wrapping.
- Returns:
required to observe the line width limit.
- Return type:
str the string of the current line, potentially including line breaks if
- visit_all(item, *args, **kwargs)
Convenience function to call
visit()
for all given arguments.If only a single argument is given that is iterable,
visit()
is called on all of its elements instead.
- visit_Module(o, **kwargs)
Add a
Module
, mark parent node and visit all “spec” and “subroutine” nodes.- Returns:
An extended list of tuples of a node and potentially a edge information
- Return type:
list[tuple[dict[str,str], dict[str,str]]]]
- visit_Subroutine(o, **kwargs)
Add a
Subroutine
, mark parent node and visit all “docstring”, “spec”, “body”, “members” nodes.- Returns:
An extended list of tuples of a node and potentially a edge information
- Return type:
list[tuple[dict[str,str], dict[str,str]]]]
- visit_Comment(o, **kwargs)
Enables turning off comments.
- Returns:
An extended list of tuples of a node and potentially a edge information, or list of nothing.
- Return type:
list[tuple[dict[str,str], dict[str,str]]]]
- visit_CommentBlock(o, **kwargs)
Enables turning off comments.
- Returns:
An extended list of tuples of a node and potentially a edge information, or list of nothing.
- Return type:
list[tuple[dict[str,str], dict[str,str]]]]
- visit_Node(o, **kwargs)
Add a
Node
, mark parent and visit all children.- Returns:
An extended list of tuples of a node and potentially a edge information
- Return type:
list[tuple[dict[str,str], dict[str,str]]]]
- visit_Expression(o, **kwargs)
Dispatch routine to add nodes utilizing expression tree stringifier, mark parent and stop.
- Returns:
An extended list of tuples of a node and potentially a edge information or list of nothing.
- Return type:
list[tuple[dict[str,str], dict[str,str]]]]
- visit_tuple(o, **kwargs)
Recurse for each item in the tuple.
- visit_list(o, **kwargs)
Recurse for each item in the tuple.
- visit_Conditional(o, **kwargs)
Add a
Conditional
, mark parent and visit first body then else body.- Returns:
An extended list of tuples of a node and potentially a edge information
- Return type:
list[tuple[dict[str,str], dict[str,str]]]]
- ir_graph(ir, show_comments=False, show_expressions=False, linewidth=40, symgen=<class 'str'>)
Pretty-print the given IR using
GraphCollector
.- Parameters:
ir (
Node
) – The IR node starting from which to produce the treeshow_comments (bool, optional, default: False) – Whether to show comments in the output
show_expressions (bool, optional, default: False) – Whether to further expand expressions in the output