[pypy-svn] r34894 - in pypy/dist/pypy/jit/codegen/llvm: . lib test

ericvrp at codespeak.net ericvrp at codespeak.net
Thu Nov 23 14:42:29 CET 2006


Author: ericvrp
Date: Thu Nov 23 14:42:27 2006
New Revision: 34894

Modified:
   pypy/dist/pypy/jit/codegen/llvm/lib/libllvmjit.cpp
   pypy/dist/pypy/jit/codegen/llvm/lib/libllvmjit.h
   pypy/dist/pypy/jit/codegen/llvm/llvmjit.py
   pypy/dist/pypy/jit/codegen/llvm/test/test_llvmjit.py
Log:
Working on interface to llvm jit codegen optimizations/transformation.
The testcase for this is some dead-code elimination.



Modified: pypy/dist/pypy/jit/codegen/llvm/lib/libllvmjit.cpp
==============================================================================
--- pypy/dist/pypy/jit/codegen/llvm/lib/libllvmjit.cpp	(original)
+++ pypy/dist/pypy/jit/codegen/llvm/lib/libllvmjit.cpp	Thu Nov 23 14:42:27 2006
@@ -6,6 +6,7 @@
 #include "llvm/Assembly/Parser.h"
 #include "llvm/Bytecode/Writer.h"
 #include "llvm/Analysis/Verifier.h"
+#include "llvm/Target/TargetData.h"
 #include "llvm/Support/CommandLine.h"
 #include "llvm/Support/SystemUtils.h"
 #include "llvm/System/Signals.h"
@@ -13,6 +14,15 @@
 #include "llvm/ExecutionEngine/JIT.h"
 #include "llvm/ExecutionEngine/Interpreter.h"
 #include "llvm/ExecutionEngine/GenericValue.h"
+
+#include "llvm/PassManager.h"
+#include "llvm/Support/PassNameParser.h"
+#include "llvm/Assembly/PrintModulePass.h"  //for PrintModulePass
+#include "llvm/Analysis/Verifier.h"         //for createVerifierPass
+#include "llvm/Transforms/Scalar.h"         //for createInstructionCombiningPass...
+
+#include "llvm/Target/TargetOptions.h"      //for PrintMachineCode
+
 #include <fstream>
 #include <iostream>
 #include <memory>
@@ -24,12 +34,40 @@
 ExecutionEngine*    gp_execution_engine = ExecutionEngine::create(
                         new ExistingModuleProvider(gp_module), false);
 
+//all optimization/transform passes
+static cl::list<const PassInfo*, bool, PassNameParser>
+    PassList(cl::desc("Optimizations available:"));
+
 
+//
+//code...
+//
 void    restart() {
     delete gp_execution_engine; //XXX test if this correctly cleans up including generated code
 
     gp_module           = new Module("llvmjit");
     gp_execution_engine = ExecutionEngine::create(new ExistingModuleProvider(gp_module), false);
+
+    //PrintMachineCode = 1;
+}
+
+
+int     transform(const char* passnames) {
+    if (!gp_module) {
+        return -1;
+    }
+
+    PassManager passes; //XXX: note: if passnames is the same as last time we can reuse passes
+    passes.add(new TargetData(gp_module));
+
+    //XXX next couple of passes should be dependent on passnames!
+    passes.add(new PrintModulePass());
+    passes.add(createInstructionCombiningPass());
+    passes.add(createCFGSimplificationPass());
+    passes.add(new PrintModulePass());
+    passes.add(createVerifierPass());
+
+    return passes.run(*gp_module);
 }
 
 

Modified: pypy/dist/pypy/jit/codegen/llvm/lib/libllvmjit.h
==============================================================================
--- pypy/dist/pypy/jit/codegen/llvm/lib/libllvmjit.h	(original)
+++ pypy/dist/pypy/jit/codegen/llvm/lib/libllvmjit.h	Thu Nov 23 14:42:27 2006
@@ -5,6 +5,7 @@
 #endif
 
 void    restart();
+int     transform(const char* passnames);
 int     compile(const char* llsource);
 void*   find_function(const char* funcname);
 int     execute(const void* function, int param);

Modified: pypy/dist/pypy/jit/codegen/llvm/llvmjit.py
==============================================================================
--- pypy/dist/pypy/jit/codegen/llvm/llvmjit.py	(original)
+++ pypy/dist/pypy/jit/codegen/llvm/llvmjit.py	Thu Nov 23 14:42:27 2006
@@ -45,6 +45,10 @@
 #exposed functions...
 restart = llvmjit.restart
 
+transform = llvmjit.transform
+transform.restype  = c_int
+transform.argtypes = [c_char_p]
+
 compile = llvmjit.compile
 compile.restype  = c_int
 compile.argtypes = [c_char_p]

Modified: pypy/dist/pypy/jit/codegen/llvm/test/test_llvmjit.py
==============================================================================
--- pypy/dist/pypy/jit/codegen/llvm/test/test_llvmjit.py	(original)
+++ pypy/dist/pypy/jit/codegen/llvm/test/test_llvmjit.py	Thu Nov 23 14:42:27 2006
@@ -12,8 +12,6 @@
 
 #helper data
 curdir = dirname(__file__)
-square = join(curdir, 'square')
-mul2   = join(curdir, 'mul2')
 
 llsquare = '''int %square(int %n) {
 block0:
@@ -27,6 +25,19 @@
     ret int %n2
 }'''
 
+lldeadcode = '''int %deadcode(int %n) {
+Test:
+    %cond = seteq int %n, %n
+    br bool %cond, label %IfEqual, label %IfUnequal
+
+IfEqual:
+    %n2 = mul int %n, 2
+    ret int %n2
+
+IfUnequal:
+    ret int -1
+}'''
+
 llacross1 = '''declare int %across2(int)
 
 implementation
@@ -107,16 +118,6 @@
         assert llvmjit.execute(square, i) == i * i
         assert llvmjit.execute(mul2  , i) == i * 2
 
-def test_call_found_function():
-    llvmjit.restart()
-    llvmjit.compile(llsquare)
-    llvmjit.compile(llmul2)
-    square = llvmjit.find_function('square')
-    mul2   = llvmjit.find_function('mul2')
-    for i in range(5):
-        assert llvmjit.execute(square, i) == i * i
-        assert llvmjit.execute(mul2  , i) == i * 2
-
 def test_execute_across_module():
     def my_across1(n):
         return n * 3
@@ -139,6 +140,18 @@
         assert llvmjit.execute(across1to2, i) == my_across1to2(i)
         assert llvmjit.execute(across2to1, i) == my_across2to1(i)
 
+def test_transform(): #XXX This uses Module transforms, think about Function transforms too.
+    llvmjit.restart()
+    llvmjit.compile(lldeadcode)
+    deadcode = llvmjit.find_function('deadcode')
+    assert llvmjit.execute(deadcode, 10) == 10 * 2
+
+    #XXX enable this part of the test asap
+    #assert not llvmjit.transform("instcombine printm verify")
+    assert llvmjit.execute(deadcode, 20) == 20 * 2
+
+    assert llvmjit.transform("instcombine simplifycfg printm verify")
+    assert llvmjit.execute(deadcode, 30) == 30 * 2
 
 def DONTtest_modify_global_data():
     pass
@@ -152,9 +165,6 @@
 def DONTtest_functions_with_different_signatures():
     pass
 
-def DONTtest_llvm_transformations():
-    pass
-
 def DONTtest_layers_of_codegenerators():    #e.g. i386 code until function stabilizes then llvm
     pass
     



More information about the Pypy-commit mailing list