More code block cleanup.

This commit is contained in:
Maggie Mari 2012-08-16 16:07:18 -05:00
commit d8df9e2849
2 changed files with 61 additions and 37 deletions

View file

@ -406,7 +406,7 @@ prototype. Code generation of the prototype ensures that there is an
LLVM Function object that is ready to go for us. LLVM Function object that is ready to go for us.
:: .. code-block:: python
# Create a new basic block to start insertion into. # Create a new basic block to start insertion into.
block = function.append_basic_block('entry') block = function.append_basic_block('entry')
@ -429,7 +429,7 @@ Graph <http://en.wikipedia.org/wiki/Control_flow_graph>`_. Since we
don't have any control flow, our functions will only contain one block don't have any control flow, our functions will only contain one block
at this point. We'll fix this in `Chapter 5 <PythonLangImpl5.html>`_ :). at this point. We'll fix this in `Chapter 5 <PythonLangImpl5.html>`_ :).
:: .. code-block:: python
# Finish off the function. # Finish off the function.
try: try:

View file

@ -28,9 +28,13 @@ however, does give us obvious optimizations when compiling simple code:
.. code-block:: bash .. code-block:: bash
ready> def test(x) 1+2+x Read function definition: ready> def test(x) 1+2+x
define double @test(double %x) { entry: %addtmp = fadd double Read function definition:
3.000000e+00, %x ret double %addtmp } define double @test(double %x) {
entry:
%addtmp = fadd double 3.000000e+00, %x
ret double %addtmp
}
@ -40,10 +44,14 @@ input. That would be:
.. code-block:: bash .. code-block:: bash
ready> def test(x) 1+2+x Read function definition: ready> def test(x) 1+2+x
define double @test(double %x) { entry: %addtmp = fadd double Read function definition:
2.000000e+00, 1.000000e+00 %addtmp1 = fadd double %addtmp, %x ret double define double @test(double %x) {
%addtmp1 } entry:
%addtmp = fadd double 2.000000e+00, 1.000000e+00
%addtmp1 = fadd double %addtmp, %x
ret double %addtmp1
}
@ -71,11 +79,15 @@ slightly more complex example:
.. code-block:: bash .. code-block:: bash
ready> def test(x) (1+2+x)\*(x+(1+2)) Read a ready> def test(x) (1+2+x)*(x+(1+2))
function definition: define double @test(double %x) { entry: %addtmp = Read a function definition:
fadd double 3.000000e+00, %x ; <double> [#uses=1] %addtmp1 = fadd double %x, define double @test(double %x) {
3.000000e+00 ; <double> [#uses=1] %multmp = fmul double %addtmp, %addtmp1 ; entry:
<double> [#uses=1] ret double %multmp } %addtmp = fadd double 3.000000e+00, %x ; <double> [#uses=1]
%addtmp1 = fadd double %x, 3.000000e+00 ; <double> [#uses=1]
%multmp = fmul double %addtmp, %addtmp1 ; <double> [#uses=1]
ret double %multmp
}
@ -138,14 +150,17 @@ this:
... ...
def main(): # Set up the optimizer pipeline. Start with registering info def main():
about how the # target lays out data structures. # Set up the optimizer pipeline. Start with registering info about how the
g_llvm_pass_manager.add(g_llvm_executor.target_data) # Do simple # target lays out data structures.
"peephole" optimizations and bit-twiddling optzns. g_llvm_pass_manager.add(g_llvm_executor.target_data)
g_llvm_pass_manager.add(PASS_INSTRUCTION_COMBINING) # Reassociate # Do simple "peephole" optimizations and bit-twiddling optzns.
expressions. g_llvm_pass_manager.add(PASS_REASSOCIATE) # Eliminate g_llvm_pass_manager.add(PASS_INSTRUCTION_COMBINING)
Common SubExpressions. g_llvm_pass_manager.add(PASS_GVN) # Simplify # Reassociate expressions.
the control flow graph (deleting unreachable blocks, etc). g_llvm_pass_manager.add(PASS_REASSOCIATE)
# Eliminate Common SubExpressions.
g_llvm_pass_manager.add(PASS_GVN)
# Simplify the control flow graph (deleting unreachable blocks, etc).
g_llvm_pass_manager.add(PASS_CFG_SIMPLIFICATION) g_llvm_pass_manager.add(PASS_CFG_SIMPLIFICATION)
g_llvm_pass_manager.initialize() g_llvm_pass_manager.initialize()
@ -193,10 +208,14 @@ our test above again:
.. code-block:: bash .. code-block:: bash
ready> def test(x) (1+2+x)\*(x+(1+2)) Read a ready> def test(x) (1+2+x)*(x+(1+2))
function definition: define double @test(double %x) { entry: %addtmp = Read a function definition:
fadd double %x, 3.000000e+00 ; [#uses=2] %multmp = fmul double %addtmp, define double @test(double %x) {
%addtmp ; <double> [#uses=1] ret double %multmp } entry:
%addtmp = fadd double %x, 3.000000e+00 ; <double> [#uses=2]
%multmp = fmul double %addtmp, %addtmp ; <double> [#uses=1]
ret double %multmp
}
@ -239,8 +258,8 @@ done by adding and initializing a global variable:
.. code-block:: python .. code-block:: python
# The LLVM execution engine. g_llvm_executor = # The LLVM execution engine.
ExecutionEngine.new(g_llvm_module) g_llvm_executor = ExecutionEngine.new(g_llvm_module)
@ -258,12 +277,17 @@ this:
.. code-block:: python .. code-block:: python
def HandleTopLevelExpression(self): try: function def HandleTopLevelExpression(self):
= self.ParseTopLevelExpr().CodeGen() result = try:
g_llvm_executor.run_function(function, []) print 'Evaluated to:', function = self.ParseTopLevelExpr().CodeGen()
result.as_real(Type.double()) except Exception, e: print 'Error:', e result = g_llvm_executor.run_function(function, [])
try: self.Next() # Skip for error recovery. except: pass {% endhighlight print 'Evaluated to:', result.as_real(Type.double())
%} except Exception, e:
print 'Error:', e
try:
self.Next() # Skip for error recovery.
except:
pass {% endhighlight %}
Recall that we compile top-level expressions into a self-contained LLVM Recall that we compile top-level expressions into a self-contained LLVM
function that takes no arguments and returns the computed double. function that takes no arguments and returns the computed double.