Skip to content

Commit

Permalink
Merge pull request #146 from joscao/devel/graph_visualization_of_IR
Browse files Browse the repository at this point in the history
Provide a graphical representation of the IR using graphviz
  • Loading branch information
reuterbal authored Sep 13, 2023
2 parents ed7e624 + a8ca03a commit d951e97
Show file tree
Hide file tree
Showing 9 changed files with 923 additions and 1 deletion.
1 change: 1 addition & 0 deletions AUTHORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- O. Marsden (ECMWF)
- A. Nawab (ECMWF)
- B. Reuter (ECMWF)
- J. Schmalfuß
- M. Staneker (ECMWF)

If you have contributed to this project, please add your name in the above
Expand Down
172 changes: 171 additions & 1 deletion example/01_reading_and_writing_files.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -174,12 +174,182 @@
"phys_mod.spec.view()"
]
},
{
"cell_type": "markdown",
"id": "e7fa4ba7-a0f6-4f10-a47f-d508a121653d",
"metadata": {},
"source": [
"Or alternativly, if `graphviz` is available, we can call `ir_graph()` on any of the nodes to view a graph representation of this node and the tree below it:"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "6942fbb4-113c-466d-be0e-4fce35d837ac",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[Loki::Graph Visualization] Created graph visualization in 0.01s\n"
]
},
{
"data": {
"image/svg+xml": [
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n",
"<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
"<!-- Generated by graphviz version 2.43.0 (0)\n",
" -->\n",
"<!-- Title: %3 Pages: 1 -->\n",
"<svg width=\"521pt\" height=\"476pt\"\n",
" viewBox=\"0.00 0.00 521.06 476.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
"<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 472)\">\n",
"<title>%3</title>\n",
"<polygon fill=\"white\" stroke=\"transparent\" points=\"-4,4 -4,-472 517.06,-472 517.06,4 -4,4\"/>\n",
"<!-- 0 -->\n",
"<g id=\"node1\" class=\"node\">\n",
"<title>0</title>\n",
"<ellipse fill=\"none\" stroke=\"black\" cx=\"66.94\" cy=\"-234\" rx=\"66.89\" ry=\"18\"/>\n",
"<text text-anchor=\"middle\" x=\"66.94\" y=\"-230.3\" font-family=\"Times,serif\" font-size=\"14.00\">&lt;Section::&gt;</text>\n",
"</g>\n",
"<!-- 1 -->\n",
"<g id=\"node2\" class=\"node\">\n",
"<title>1</title>\n",
"<ellipse fill=\"none\" stroke=\"black\" cx=\"341.47\" cy=\"-450\" rx=\"166.27\" ry=\"18\"/>\n",
"<text text-anchor=\"middle\" x=\"341.47\" y=\"-446.3\" font-family=\"Times,serif\" font-size=\"14.00\">&lt;Import:: iso_fortran_env =&gt; ()&gt;</text>\n",
"</g>\n",
"<!-- 0&#45;&gt;1 -->\n",
"<g id=\"edge1\" class=\"edge\">\n",
"<title>0&#45;&gt;1</title>\n",
"<path fill=\"none\" stroke=\"black\" d=\"M72,-252.14C81.61,-290.6 109.66,-380.32 169.89,-423 177.18,-428.16 185.08,-432.49 193.35,-436.11\"/>\n",
"<polygon fill=\"black\" stroke=\"black\" points=\"192.25,-439.44 202.84,-439.9 194.85,-432.94 192.25,-439.44\"/>\n",
"</g>\n",
"<!-- 2 -->\n",
"<g id=\"node3\" class=\"node\">\n",
"<title>2</title>\n",
"<ellipse fill=\"none\" stroke=\"black\" cx=\"341.47\" cy=\"-396\" rx=\"131.08\" ry=\"18\"/>\n",
"<text text-anchor=\"middle\" x=\"341.47\" y=\"-392.3\" font-family=\"Times,serif\" font-size=\"14.00\">&lt;Import:: omp_lib =&gt; ()&gt;</text>\n",
"</g>\n",
"<!-- 0&#45;&gt;2 -->\n",
"<g id=\"edge2\" class=\"edge\">\n",
"<title>0&#45;&gt;2</title>\n",
"<path fill=\"none\" stroke=\"black\" d=\"M75.91,-251.98C90.11,-281.73 122.8,-340.74 169.89,-369 182.32,-376.46 196.14,-382.06 210.37,-386.25\"/>\n",
"<polygon fill=\"black\" stroke=\"black\" points=\"209.72,-389.7 220.29,-388.92 211.55,-382.94 209.72,-389.7\"/>\n",
"</g>\n",
"<!-- 3 -->\n",
"<g id=\"node4\" class=\"node\">\n",
"<title>3</title>\n",
"<ellipse fill=\"none\" stroke=\"black\" cx=\"341.47\" cy=\"-342\" rx=\"151.37\" ry=\"18\"/>\n",
"<text text-anchor=\"middle\" x=\"341.47\" y=\"-338.3\" font-family=\"Times,serif\" font-size=\"14.00\">&lt;Intrinsic:: IMPLICIT NONE&gt;</text>\n",
"</g>\n",
"<!-- 0&#45;&gt;3 -->\n",
"<g id=\"edge3\" class=\"edge\">\n",
"<title>0&#45;&gt;3</title>\n",
"<path fill=\"none\" stroke=\"black\" d=\"M84.49,-251.74C103.26,-270.68 135.74,-299.99 169.89,-315 182.82,-320.69 196.78,-325.19 210.94,-328.77\"/>\n",
"<polygon fill=\"black\" stroke=\"black\" points=\"210.22,-332.19 220.76,-331.09 211.83,-325.38 210.22,-332.19\"/>\n",
"</g>\n",
"<!-- 4 -->\n",
"<g id=\"node5\" class=\"node\">\n",
"<title>4</title>\n",
"<ellipse fill=\"none\" stroke=\"black\" cx=\"341.47\" cy=\"-288\" rx=\"137.58\" ry=\"18\"/>\n",
"<text text-anchor=\"middle\" x=\"341.47\" y=\"-284.3\" font-family=\"Times,serif\" font-size=\"14.00\">&lt;VariableDeclaration:: sp&gt;</text>\n",
"</g>\n",
"<!-- 0&#45;&gt;4 -->\n",
"<g id=\"edge4\" class=\"edge\">\n",
"<title>0&#45;&gt;4</title>\n",
"<path fill=\"none\" stroke=\"black\" d=\"M113.38,-247.09C130.94,-251.88 151.26,-257.07 169.89,-261 191.39,-265.54 214.55,-269.69 236.67,-273.3\"/>\n",
"<polygon fill=\"black\" stroke=\"black\" points=\"236.32,-276.79 246.75,-274.93 237.44,-269.88 236.32,-276.79\"/>\n",
"</g>\n",
"<!-- 5 -->\n",
"<g id=\"node6\" class=\"node\">\n",
"<title>5</title>\n",
"<ellipse fill=\"none\" stroke=\"black\" cx=\"341.47\" cy=\"-234\" rx=\"138.38\" ry=\"18\"/>\n",
"<text text-anchor=\"middle\" x=\"341.47\" y=\"-230.3\" font-family=\"Times,serif\" font-size=\"14.00\">&lt;VariableDeclaration:: dp&gt;</text>\n",
"</g>\n",
"<!-- 0&#45;&gt;5 -->\n",
"<g id=\"edge5\" class=\"edge\">\n",
"<title>0&#45;&gt;5</title>\n",
"<path fill=\"none\" stroke=\"black\" d=\"M133.92,-234C151.92,-234 172.18,-234 192.79,-234\"/>\n",
"<polygon fill=\"black\" stroke=\"black\" points=\"192.93,-237.5 202.93,-234 192.93,-230.5 192.93,-237.5\"/>\n",
"</g>\n",
"<!-- 6 -->\n",
"<g id=\"node7\" class=\"node\">\n",
"<title>6</title>\n",
"<ellipse fill=\"none\" stroke=\"black\" cx=\"341.47\" cy=\"-180\" rx=\"135.68\" ry=\"18\"/>\n",
"<text text-anchor=\"middle\" x=\"341.47\" y=\"-176.3\" font-family=\"Times,serif\" font-size=\"14.00\">&lt;VariableDeclaration:: lp&gt;</text>\n",
"</g>\n",
"<!-- 0&#45;&gt;6 -->\n",
"<g id=\"edge6\" class=\"edge\">\n",
"<title>0&#45;&gt;6</title>\n",
"<path fill=\"none\" stroke=\"black\" d=\"M113.38,-220.91C130.94,-216.12 151.26,-210.93 169.89,-207 191.51,-202.44 214.83,-198.26 237.07,-194.63\"/>\n",
"<polygon fill=\"black\" stroke=\"black\" points=\"237.88,-198.05 247.2,-193 236.77,-191.14 237.88,-198.05\"/>\n",
"</g>\n",
"<!-- 7 -->\n",
"<g id=\"node8\" class=\"node\">\n",
"<title>7</title>\n",
"<ellipse fill=\"none\" stroke=\"black\" cx=\"341.47\" cy=\"-126\" rx=\"135.68\" ry=\"18\"/>\n",
"<text text-anchor=\"middle\" x=\"341.47\" y=\"-122.3\" font-family=\"Times,serif\" font-size=\"14.00\">&lt;VariableDeclaration:: ip&gt;</text>\n",
"</g>\n",
"<!-- 0&#45;&gt;7 -->\n",
"<g id=\"edge7\" class=\"edge\">\n",
"<title>0&#45;&gt;7</title>\n",
"<path fill=\"none\" stroke=\"black\" d=\"M84.49,-216.26C103.26,-197.32 135.74,-168.01 169.89,-153 184.55,-146.55 200.52,-141.62 216.61,-137.85\"/>\n",
"<polygon fill=\"black\" stroke=\"black\" points=\"217.62,-141.22 226.64,-135.66 216.12,-134.38 217.62,-141.22\"/>\n",
"</g>\n",
"<!-- 8 -->\n",
"<g id=\"node9\" class=\"node\">\n",
"<title>8</title>\n",
"<ellipse fill=\"none\" stroke=\"black\" cx=\"341.47\" cy=\"-72\" rx=\"171.67\" ry=\"18\"/>\n",
"<text text-anchor=\"middle\" x=\"341.47\" y=\"-68.3\" font-family=\"Times,serif\" font-size=\"14.00\">&lt;VariableDeclaration:: cst1, cst2&gt;</text>\n",
"</g>\n",
"<!-- 0&#45;&gt;8 -->\n",
"<g id=\"edge8\" class=\"edge\">\n",
"<title>0&#45;&gt;8</title>\n",
"<path fill=\"none\" stroke=\"black\" d=\"M75.91,-216.02C90.11,-186.27 122.8,-127.26 169.89,-99 178.09,-94.08 186.9,-89.96 196.04,-86.53\"/>\n",
"<polygon fill=\"black\" stroke=\"black\" points=\"197.37,-89.77 205.67,-83.19 195.07,-83.16 197.37,-89.77\"/>\n",
"</g>\n",
"<!-- 9 -->\n",
"<g id=\"node10\" class=\"node\">\n",
"<title>9</title>\n",
"<ellipse fill=\"none\" stroke=\"black\" cx=\"341.47\" cy=\"-18\" rx=\"167.07\" ry=\"18\"/>\n",
"<text text-anchor=\"middle\" x=\"341.47\" y=\"-14.3\" font-family=\"Times,serif\" font-size=\"14.00\">&lt;VariableDeclaration:: nspecies&gt;</text>\n",
"</g>\n",
"<!-- 0&#45;&gt;9 -->\n",
"<g id=\"edge9\" class=\"edge\">\n",
"<title>0&#45;&gt;9</title>\n",
"<path fill=\"none\" stroke=\"black\" d=\"M72,-215.86C81.61,-177.4 109.66,-87.68 169.89,-45 177.18,-39.84 185.08,-35.51 193.35,-31.89\"/>\n",
"<polygon fill=\"black\" stroke=\"black\" points=\"194.85,-35.06 202.84,-28.1 192.25,-28.56 194.85,-35.06\"/>\n",
"</g>\n",
"</g>\n",
"</svg>\n"
],
"text/plain": [
"<graphviz.graphs.Digraph at 0x7fc53b1c0550>"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"graph = None\n",
"try:\n",
" graph = phys_mod.spec.ir_graph()\n",
"except ImportError:\n",
" print(\"Install graphviz if you want to view the graph representation!\")\n",
"graph"
]
},
{
"cell_type": "markdown",
"id": "80f46c51",
"metadata": {},
"source": [
"We can see a number of (empty) [comments](https://sites.ecmwf.int/docs/loki/main/loki.ir.html#loki.ir.Comment) - which are simply empty lines and retained to be able to produce Fortran code with a formatting similar to the original source. Other than that, we also have some [_Import_](https://sites.ecmwf.int/docs/loki/main/loki.ir.html#loki.ir.Import) statements, [preprocessor directives](https://sites.ecmwf.int/docs/loki/main/loki.ir.html#loki.ir.PreprocessorDirective) and [declarations](https://sites.ecmwf.int/docs/loki/main/loki.ir.html#loki.ir.Declaration).\n",
"We can see a number of (empty) [comments](https://sites.ecmwf.int/docs/loki/main/loki.ir.html#loki.ir.Comment) - which are simply empty lines and retained to be able to produce Fortran code with a formatting similar to the original source. Since [comments](https://sites.ecmwf.int/docs/loki/main/loki.ir.html#loki.ir.Comment) might introduce additional noise, they are ignored by default in the graph representation. Other than that, we also have some [_Import_](https://sites.ecmwf.int/docs/loki/main/loki.ir.html#loki.ir.Import) statements, [preprocessor directives](https://sites.ecmwf.int/docs/loki/main/loki.ir.html#loki.ir.PreprocessorDirective) and [declarations](https://sites.ecmwf.int/docs/loki/main/loki.ir.html#loki.ir.Declaration).\n",
"\n",
"We can also convert this representation of the specification part back into a Fortran representation using the Fortran backend via [_fgen_](https://sites.ecmwf.int/docs/loki/main/loki.backend.fgen.html):"
]
Expand Down
9 changes: 9 additions & 0 deletions loki/ir.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,15 @@ def view(self):
from loki.visitors import pprint
pprint(self)

def ir_graph(self, show_comments=False, show_expressions=False, linewidth=40, symgen=str):
"""
Get the IR graph to visualize the node hierachy under this node.
"""
# pylint: disable=import-outside-toplevel,cyclic-import
from loki.visitors.ir_graph import ir_graph

return ir_graph(self, show_comments, show_expressions,linewidth, symgen)

@property
def live_symbols(self):
"""
Expand Down
Loading

0 comments on commit d951e97

Please sign in to comment.