Skip to content

Instantly share code, notes, and snippets.

@sklam
Created December 5, 2017 16:16
Show Gist options
  • Save sklam/eb89eab5b5708f03d0b971136a9806f4 to your computer and use it in GitHub Desktop.
Save sklam/eb89eab5b5708f03d0b971136a9806f4 to your computer and use it in GitHub Desktop.
llvmlite builder.if_else & phi example
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"from llvmlite import ir"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"define i32 @\"foo\"(i32 %\"x\", i32 %\"y\") \n",
"{\n",
".4:\n",
" %\".5\" = icmp slt i32 %\"x\", %\"y\"\n",
" br i1 %\".5\", label %\".4.if\", label %\".4.else\"\n",
".4.if:\n",
" %\"out_then\" = sub i32 %\"y\", %\"x\"\n",
" br label %\".4.endif\"\n",
".4.else:\n",
" %\"out_orelse\" = sub i32 %\"x\", %\"y\"\n",
" br label %\".4.endif\"\n",
".4.endif:\n",
" %\".9\" = phi i32 [%\"out_then\", %\".4.if\"], [%\"out_orelse\", %\".4.else\"]\n",
" ret i32 %\".9\"\n",
"}\n",
"\n"
]
}
],
"source": [
"mod = ir.Module()\n",
"\n",
"i32 = ir.IntType(32)\n",
"fn = ir.Function(mod, ir.FunctionType(i32, [i32, i32]), 'foo')\n",
"\n",
"builder = ir.IRBuilder(fn.append_basic_block())\n",
"[x, y] = fn.args\n",
"x.name = 'x'\n",
"y.name = 'y'\n",
"\n",
"x_lt_y = builder.icmp_signed('<', x, y)\n",
"with builder.if_else(x_lt_y) as (then, orelse):\n",
" with then:\n",
" bb_then = builder.basic_block\n",
" out_then = builder.sub(y, x, name='out_then')\n",
" with orelse:\n",
" bb_orelse = builder.basic_block\n",
" out_orelse = builder.sub(x, y, name='out_orelse')\n",
" \n",
"out_phi = builder.phi(i32)\n",
"out_phi.add_incoming(out_then, bb_then)\n",
"out_phi.add_incoming(out_orelse, bb_orelse)\n",
"\n",
"builder.ret(out_phi)\n",
"print(fn)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"from llvmlite import binding as llvm"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"<svg height=\"277pt\" viewBox=\"0.00 0.00 401.92 277.00\" width=\"402pt\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
"<g class=\"graph\" id=\"graph0\" transform=\"scale(1 1) rotate(0) translate(4 273)\">\n",
"<title>CFG for 'foo' function</title>\n",
"<polygon fill=\"white\" points=\"-4,4 -4,-273 397.92,-273 397.92,4 -4,4\" stroke=\"none\"/>\n",
"<text font-family=\"Times,serif\" font-size=\"14.00\" text-anchor=\"middle\" x=\"196.96\" y=\"-6.8\">CFG for 'foo' function</text>\n",
"<!-- Node0x7fdae26b2d30 -->\n",
"<g class=\"node\" id=\"node1\"><title>Node0x7fdae26b2d30</title>\n",
"<polygon fill=\"none\" points=\"80.2671,-196.5 80.2671,-268.5 308.537,-268.5 308.537,-196.5 80.2671,-196.5\" stroke=\"black\"/>\n",
"<text font-family=\"Times,serif\" font-size=\"14.00\" text-anchor=\"start\" x=\"88.2671\" y=\"-253.3\">.4:</text>\n",
"<text font-family=\"Times,serif\" font-size=\"14.00\" text-anchor=\"start\" x=\"88.2671\" y=\"-239.3\"> %.5 = icmp slt i32 %x, %y</text>\n",
"<text font-family=\"Times,serif\" font-size=\"14.00\" text-anchor=\"start\" x=\"88.2671\" y=\"-225.3\"> br i1 %.5, label %.4.if, label %.4.else</text>\n",
"<polyline fill=\"none\" points=\"80.2671,-218.5 308.537,-218.5 \" stroke=\"black\"/>\n",
"<text font-family=\"Times,serif\" font-size=\"14.00\" text-anchor=\"middle\" x=\"137.043\" y=\"-203.3\">T</text>\n",
"<polyline fill=\"none\" points=\"193.819,-196.5 193.819,-218.5 \" stroke=\"black\"/>\n",
"<text font-family=\"Times,serif\" font-size=\"14.00\" text-anchor=\"middle\" x=\"250.712\" y=\"-203.3\">F</text>\n",
"</g>\n",
"<!-- Node0x7fdae26b4ba0 -->\n",
"<g class=\"node\" id=\"node2\"><title>Node0x7fdae26b4ba0</title>\n",
"<polygon fill=\"none\" points=\"0,-109.5 0,-159.5 182.804,-159.5 182.804,-109.5 0,-109.5\" stroke=\"black\"/>\n",
"<text font-family=\"Times,serif\" font-size=\"14.00\" text-anchor=\"start\" x=\"8\" y=\"-144.3\">.4.if: </text>\n",
"<text font-family=\"Times,serif\" font-size=\"14.00\" text-anchor=\"start\" x=\"8\" y=\"-130.3\"> %out_then = sub i32 %y, %x</text>\n",
"<text font-family=\"Times,serif\" font-size=\"14.00\" text-anchor=\"start\" x=\"8\" y=\"-116.3\"> br label %.4.endif</text>\n",
"</g>\n",
"<!-- Node0x7fdae26b2d30&#45;&gt;Node0x7fdae26b4ba0 -->\n",
"<g class=\"edge\" id=\"edge1\"><title>Node0x7fdae26b2d30:s0-&gt;Node0x7fdae26b4ba0</title>\n",
"<path d=\"M79.4019,-207.5C62.4901,-207.5 66.4717,-187.934 74.1589,-169.201\" fill=\"none\" stroke=\"black\"/>\n",
"<polygon fill=\"black\" points=\"77.4395,-170.437 78.2897,-159.876 71.0394,-167.601 77.4395,-170.437\" stroke=\"black\"/>\n",
"</g>\n",
"<!-- Node0x7fdae26c2010 -->\n",
"<g class=\"node\" id=\"node3\"><title>Node0x7fdae26c2010</title>\n",
"<polygon fill=\"none\" points=\"200.883,-109.5 200.883,-159.5 393.92,-159.5 393.92,-109.5 200.883,-109.5\" stroke=\"black\"/>\n",
"<text font-family=\"Times,serif\" font-size=\"14.00\" text-anchor=\"start\" x=\"208.883\" y=\"-144.3\">.4.else: </text>\n",
"<text font-family=\"Times,serif\" font-size=\"14.00\" text-anchor=\"start\" x=\"208.883\" y=\"-130.3\"> %out_orelse = sub i32 %x, %y</text>\n",
"<text font-family=\"Times,serif\" font-size=\"14.00\" text-anchor=\"start\" x=\"208.883\" y=\"-116.3\"> br label %.4.endif</text>\n",
"</g>\n",
"<!-- Node0x7fdae26b2d30&#45;&gt;Node0x7fdae26c2010 -->\n",
"<g class=\"edge\" id=\"edge2\"><title>Node0x7fdae26b2d30:s1-&gt;Node0x7fdae26c2010</title>\n",
"<path d=\"M308.402,-207.5C325.277,-207.5 321.569,-187.934 314.152,-169.201\" fill=\"none\" stroke=\"black\"/>\n",
"<polygon fill=\"black\" points=\"317.312,-167.689 310.156,-159.876 310.877,-170.446 317.312,-167.689\" stroke=\"black\"/>\n",
"</g>\n",
"<!-- Node0x7fdae2625630 -->\n",
"<g class=\"node\" id=\"node4\"><title>Node0x7fdae2625630</title>\n",
"<polygon fill=\"none\" points=\"11.771,-22.5 11.771,-72.5 377.033,-72.5 377.033,-22.5 11.771,-22.5\" stroke=\"black\"/>\n",
"<text font-family=\"Times,serif\" font-size=\"14.00\" text-anchor=\"start\" x=\"19.771\" y=\"-57.3\">.4.endif: </text>\n",
"<text font-family=\"Times,serif\" font-size=\"14.00\" text-anchor=\"start\" x=\"19.771\" y=\"-43.3\"> %.9 = phi i32 [ %out_then, %.4.if ], [ %out_orelse, %.4.else ]</text>\n",
"<text font-family=\"Times,serif\" font-size=\"14.00\" text-anchor=\"start\" x=\"19.771\" y=\"-29.3\"> ret i32 %.9</text>\n",
"</g>\n",
"<!-- Node0x7fdae26b4ba0&#45;&gt;Node0x7fdae2625630 -->\n",
"<g class=\"edge\" id=\"edge3\"><title>Node0x7fdae26b4ba0-&gt;Node0x7fdae2625630</title>\n",
"<path d=\"M120.642,-109.37C132.058,-99.9484 145.282,-89.0357 157.353,-79.0742\" fill=\"none\" stroke=\"black\"/>\n",
"<polygon fill=\"black\" points=\"159.603,-81.755 165.089,-72.6906 155.148,-76.356 159.603,-81.755\" stroke=\"black\"/>\n",
"</g>\n",
"<!-- Node0x7fdae26c2010&#45;&gt;Node0x7fdae2625630 -->\n",
"<g class=\"edge\" id=\"edge4\"><title>Node0x7fdae26c2010-&gt;Node0x7fdae2625630</title>\n",
"<path d=\"M268.162,-109.37C256.745,-99.9484 243.522,-89.0357 231.451,-79.0742\" fill=\"none\" stroke=\"black\"/>\n",
"<polygon fill=\"black\" points=\"233.656,-76.356 223.715,-72.6906 229.2,-81.755 233.656,-76.356\" stroke=\"black\"/>\n",
"</g>\n",
"</g>\n",
"</svg>"
],
"text/plain": [
"<IPython.core.display.SVG object>"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"cfg = llvm.get_function_cfg(fn)\n",
"llvm.view_dot_graph(cfg)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment