From b7200e59b185273967b9a1007b94efc0cab78070 Mon Sep 17 00:00:00 2001 From: "mdevan.foobar" Date: Mon, 26 Jul 2010 16:36:02 +0000 Subject: [PATCH] Removed the deprecated ModuleProvider class git-svn-id: http://llvm-py.googlecode.com/svn/trunk@88 8d1e9007-1d4e-0410-b67e-1979fd6579aa --- llvm/_core.c | 67 +++++++++++++++++++++++++++-------------- llvm/core.py | 22 -------------- llvm/ee.py | 35 +++++++++++++-------- llvm/passes.py | 11 ++++--- llvm/wrap.c | 1 - llvm/wrap.h | 1 - test/call-jit-ctypes.py | 5 ++- test/example-jit.py | 7 +---- test/passes.py | 3 +- test/test.py | 59 ------------------------------------ test/testall.py | 28 +++++++---------- 11 files changed, 87 insertions(+), 152 deletions(-) diff --git a/llvm/_core.c b/llvm/_core.c index 5f04bdc..8569e15 100644 --- a/llvm/_core.c +++ b/llvm/_core.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2008, Mahadevan R All rights reserved. + * Copyright (c) 2008-10, Mahadevan R All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: @@ -711,14 +711,6 @@ _wrap_objobjobjobjstr2obj(LLVMBuildInsertElement, LLVMBuilderRef, LLVMValueRef, _wrap_objobjobjobjstr2obj(LLVMBuildShuffleVector, LLVMBuilderRef, LLVMValueRef, LLVMValueRef, LLVMValueRef, LLVMValueRef) -/*===----------------------------------------------------------------------===*/ -/* Modules Providers */ -/*===----------------------------------------------------------------------===*/ - -_wrap_obj2obj(LLVMCreateModuleProviderForExistingModule, LLVMModuleRef, LLVMModuleProviderRef) -_wrap_obj2none(LLVMDisposeModuleProvider, LLVMModuleProviderRef) - - /*===----------------------------------------------------------------------===*/ /* Memory Buffer */ /*===----------------------------------------------------------------------===*/ @@ -769,7 +761,7 @@ _wrap_obj2none(LLVMDisposeMemoryBuffer, LLVMMemoryBufferRef) /*===----------------------------------------------------------------------===*/ _wrap_none2obj(LLVMCreatePassManager, LLVMPassManagerRef) -_wrap_obj2obj(LLVMCreateFunctionPassManager, LLVMModuleProviderRef, LLVMPassManagerRef) +_wrap_obj2obj(LLVMCreateFunctionPassManagerForModule, LLVMModuleRef, LLVMPassManagerRef) _wrap_objobj2obj(LLVMRunPassManager, LLVMPassManagerRef, LLVMModuleRef, int) _wrap_obj2obj(LLVMInitializeFunctionPassManager, LLVMPassManagerRef, int) _wrap_objobj2obj(LLVMRunFunctionPassManager, LLVMPassManagerRef, LLVMValueRef, int) @@ -928,23 +920,23 @@ _wrap_objobjint2obj(LLVMOffsetOfElement, LLVMTargetDataRef, LLVMTypeRef, static PyObject * _wLLVMCreateExecutionEngine(PyObject *self, PyObject *args) { - LLVMModuleProviderRef mp; + LLVMModuleRef mod; PyObject *obj; int force_interpreter; LLVMExecutionEngineRef ee; - char *outmsg; + char *outmsg = 0; PyObject *ret; int error; if (!PyArg_ParseTuple(args, "Oi", &obj, &force_interpreter)) return NULL; - mp = (LLVMModuleProviderRef) PyCObject_AsVoidPtr(obj); + mod = (LLVMModuleRef) PyCObject_AsVoidPtr(obj); if (force_interpreter) - error = LLVMCreateInterpreter(&ee, mp, &outmsg); + error = LLVMCreateInterpreterForModule(&ee, mod, &outmsg); else - error = LLVMCreateJITCompiler(&ee, mp, 1 /*fast*/, &outmsg); + error = LLVMCreateJITCompilerForModule(&ee, mod, 1 /*fast*/, &outmsg); if (error) { ret = PyString_FromString(outmsg); @@ -980,6 +972,38 @@ LLVMGenericValueRef LLVMRunFunction2(LLVMExecutionEngineRef EE, return LLVMRunFunction(EE, F, NumArgs, Args); } +static PyObject * +_wLLVMRemoveModule2(PyObject *self, PyObject *args) +{ + PyObject *obj_ee; + PyObject *obj_mod; + LLVMExecutionEngineRef ee; + LLVMModuleRef mod, mod_new = 0; + char *outmsg = 0; + PyObject *ret; + int error; + + if (!PyArg_ParseTuple(args, "OO", &obj_ee, &obj_mod)) + return NULL; + + ee = (LLVMExecutionEngineRef) PyCObject_AsVoidPtr(obj_ee); + mod = (LLVMModuleRef) PyCObject_AsVoidPtr(obj_mod); + + LLVMRemoveModule(ee, mod, &mod_new, &outmsg); + if (mod_new) { + ret = ctor_LLVMModuleRef(mod_new); + } else { + if (outmsg) { + ret = PyString_FromString(outmsg); + LLVMDisposeMessage(outmsg); + } else { + ret = PyString_FromString("error"); + } + } + + return ret; +} + _wrap_obj2none(LLVMDisposeExecutionEngine, LLVMExecutionEngineRef) _wrap_objobjlist2obj(LLVMRunFunction2, LLVMExecutionEngineRef, LLVMValueRef, LLVMGenericValueRef, LLVMGenericValueRef) @@ -989,8 +1013,8 @@ _wrap_obj2none(LLVMRunStaticConstructors, LLVMExecutionEngineRef) _wrap_obj2none(LLVMRunStaticDestructors, LLVMExecutionEngineRef) _wrap_objobj2none(LLVMFreeMachineCodeForFunction, LLVMExecutionEngineRef, LLVMValueRef) -_wrap_objobj2none(LLVMAddModuleProvider, LLVMExecutionEngineRef, - LLVMModuleProviderRef) +_wrap_objobj2none(LLVMAddModule, LLVMExecutionEngineRef, + LLVMModuleRef) /*===----------------------------------------------------------------------===*/ @@ -1501,10 +1525,6 @@ static PyMethodDef core_methods[] = { _method( LLVMBuildInsertElement ) _method( LLVMBuildShuffleVector ) - /* Modules Providers */ - _method( LLVMCreateModuleProviderForExistingModule ) - _method( LLVMDisposeModuleProvider ) - /* Memory Buffer */ _method( LLVMCreateMemoryBufferWithContentsOfFile ) _method( LLVMCreateMemoryBufferWithSTDIN ) @@ -1512,7 +1532,7 @@ static PyMethodDef core_methods[] = { /* Pass Manager */ _method( LLVMCreatePassManager ) - _method( LLVMCreateFunctionPassManager ) + _method( LLVMCreateFunctionPassManagerForModule ) _method( LLVMRunPassManager ) _method( LLVMInitializeFunctionPassManager ) _method( LLVMRunFunctionPassManager ) @@ -1638,7 +1658,8 @@ static PyMethodDef core_methods[] = { _method( LLVMRunStaticConstructors ) _method( LLVMRunStaticDestructors ) _method( LLVMFreeMachineCodeForFunction ) - _method( LLVMAddModuleProvider ) + _method( LLVMAddModule ) + _method( LLVMRemoveModule2 ) /* Generic Value */ _method( LLVMCreateGenericValueOfInt ) diff --git a/llvm/core.py b/llvm/core.py index 0608d2e..ba97f51 100644 --- a/llvm/core.py +++ b/llvm/core.py @@ -270,7 +270,6 @@ def check_is_constant(obj): _util.check_gen(obj, Constant) def check_is_function(obj): _util.check_gen(obj, Function) def check_is_basic_block(obj): _util.check_gen(obj, BasicBlock) def check_is_module(obj): _util.check_gen(obj, Module) -def check_is_module_provider(obj): _util.check_gen(obj, ModuleProvider) def unpack_types(objlst): return _util.unpack_gen(objlst, check_is_type) def unpack_values(objlst): return _util.unpack_gen(objlst, check_is_value) @@ -1933,27 +1932,6 @@ class Builder(object): vecA.ptr, vecB.ptr, mask.ptr, name)) -#===----------------------------------------------------------------------=== -# Module provider -#===----------------------------------------------------------------------=== - -class ModuleProvider(llvm.Ownable): - - @staticmethod - def new(module): - check_is_module(module) - _util.check_is_unowned(module) - return ModuleProvider( - _core.LLVMCreateModuleProviderForExistingModule(module.ptr), - module) - - def __init__(self, ptr, module): - llvm.Ownable.__init__(self, ptr, _core.LLVMDisposeModuleProvider) - module._own(self) - # a module provider is both a owner (of modules) and an ownable - # (can be owned by execution engines) - - #===----------------------------------------------------------------------=== # Memory buffer #===----------------------------------------------------------------------=== diff --git a/llvm/ee.py b/llvm/ee.py index 835dcc0..5bcd757 100644 --- a/llvm/ee.py +++ b/llvm/ee.py @@ -1,5 +1,5 @@ # -# Copyright (c) 2008, Mahadevan R All rights reserved. +# Copyright (c) 2008-10, Mahadevan R All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: @@ -33,7 +33,7 @@ """ import llvm # top-level, for common stuff -import llvm.core as core # module provider, function etc. +import llvm.core as core # module, function etc. import llvm._core as _core # C wrappers import llvm._util as _util # utility functions @@ -180,17 +180,17 @@ def _unpack_generic_values(objlist): class ExecutionEngine(object): @staticmethod - def new(mp, force_interpreter=False): - core.check_is_module_provider(mp) - _util.check_is_unowned(mp) - ret = _core.LLVMCreateExecutionEngine(mp.ptr, int(force_interpreter)) + def new(module, force_interpreter=False): + core.check_is_module(module) + _util.check_is_unowned(module) + ret = _core.LLVMCreateExecutionEngine(module.ptr, int(force_interpreter)) if isinstance(ret, str): raise llvm.LLVMException, ret - return ExecutionEngine(ret, mp) + return ExecutionEngine(ret, module) - def __init__(self, ptr, mp): + def __init__(self, ptr, module): self.ptr = ptr - mp._own(self) + module._own(self) def __del__(self): _core.LLVMDisposeExecutionEngine(self.ptr) @@ -215,10 +215,19 @@ class ExecutionEngine(object): core.check_is_function(fn) _core.LLVMFreeMachineCodeForFunction(self.ptr, fn.ptr) - def add_module_provider(self, mp): - core.check_is_module_provider(mp) - _core.LLVMAddModuleProvider(self.ptr, mp.ptr) - mp._own(self) + def add_module(self, module): + core.check_is_module(module) + _core.LLVMAddModule(self.ptr, module.ptr) + module._own(self) + + def remove_module(self, module): + core.check_is_module(module) + if module.owner != self: + raise llvm.LLVMException, "module is not owned by self" + ret = _core.LLVMRemoveModule2(self.ptr, module.ptr) + if isinstance(ret, str): + raise llvm.LLVMException, ret + return core.Module(ret) @property def target_data(self): diff --git a/llvm/passes.py b/llvm/passes.py index 282258b..71e2be9 100644 --- a/llvm/passes.py +++ b/llvm/passes.py @@ -1,5 +1,5 @@ # -# Copyright (c) 2008, Mahadevan R All rights reserved. +# Copyright (c) 2008-10, Mahadevan R All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: @@ -37,7 +37,7 @@ are available. import llvm # top-level, for common stuff import llvm.ee as ee # target data -import llvm.core as core # module provider, function etc. +import llvm.core as core # module, function etc. import llvm._core as _core # C wrappers @@ -271,9 +271,10 @@ class PassManager(object): class FunctionPassManager(PassManager): @staticmethod - def new(mp): - core.check_is_module_provider(mp) - return FunctionPassManager(_core.LLVMCreateFunctionPassManager(mp.ptr)) + def new(module): + core.check_is_module(module) + ptr = _core.LLVMCreateFunctionPassManagerForModule(module.ptr) + return FunctionPassManager(ptr) def __init__(self, ptr): PassManager.__init__(self, ptr) diff --git a/llvm/wrap.c b/llvm/wrap.c index ba2ba5e..d3eee21 100644 --- a/llvm/wrap.c +++ b/llvm/wrap.c @@ -54,7 +54,6 @@ _define_std_ctor(LLVMValueRef) _define_std_ctor(LLVMTypeHandleRef) _define_std_ctor(LLVMBasicBlockRef) _define_std_ctor(LLVMBuilderRef) -_define_std_ctor(LLVMModuleProviderRef) _define_std_ctor(LLVMMemoryBufferRef) _define_std_ctor(LLVMPassManagerRef) _define_std_ctor(LLVMExecutionEngineRef) diff --git a/llvm/wrap.h b/llvm/wrap.h index 3b7935e..0449212 100644 --- a/llvm/wrap.h +++ b/llvm/wrap.h @@ -74,7 +74,6 @@ _declare_std_ctor(LLVMValueRef) _declare_std_ctor(LLVMTypeHandleRef) _declare_std_ctor(LLVMBasicBlockRef) _declare_std_ctor(LLVMBuilderRef) -_declare_std_ctor(LLVMModuleProviderRef) _declare_std_ctor(LLVMMemoryBufferRef) _declare_std_ctor(LLVMPassManagerRef) _declare_std_ctor(LLVMExecutionEngineRef) diff --git a/test/call-jit-ctypes.py b/test/call-jit-ctypes.py index 38ac0e8..fa74abd 100755 --- a/test/call-jit-ctypes.py +++ b/test/call-jit-ctypes.py @@ -1,6 +1,6 @@ #!/usr/bin/env python -from llvm.core import Module,Type,Builder,ModuleProvider +from llvm.core import Module,Type,Builder from llvm.ee import ExecutionEngine import llvm.core import ctypes @@ -45,8 +45,7 @@ def test_jit_ctypes(): print my_module # compile the function - mp = ModuleProvider.new(my_module) - ee = ExecutionEngine.new(mp) + ee = ExecutionEngine.new(my_module) # let ctypes know about the function func_ptr_int = ee.get_pointer_to_function( mult ) diff --git a/test/example-jit.py b/test/example-jit.py index 3289298..e3c3af8 100755 --- a/test/example-jit.py +++ b/test/example-jit.py @@ -17,14 +17,9 @@ builder = Builder.new(bb) tmp = builder.add(f_sum.args[0], f_sum.args[1], "tmp") builder.ret(tmp) -# Create a module provider object first. Modules can come from -# in-memory IRs like what we created now, or from bitcode (.bc) -# files. The module provider abstracts this detail. -mp = ModuleProvider.new(my_module) - # Create an execution engine object. This will create a JIT compiler # on platforms that support it, or an interpreter otherwise. -ee = ExecutionEngine.new(mp) +ee = ExecutionEngine.new(my_module) # The arguments needs to be passed as "GenericValue" objects. arg1 = GenericValue.int(ty_int, 100) diff --git a/test/passes.py b/test/passes.py index 4184d5e..43f5494 100755 --- a/test/passes.py +++ b/test/passes.py @@ -36,7 +36,6 @@ entry: } """ m = Module.from_assembly(strstream(asm)) -mp = ModuleProvider.new(m) print "-"*72 print m @@ -62,7 +61,7 @@ print m # Let's run a DCE pass on the the function 'test1' now. First create a # function pass manager. -fpm = FunctionPassManager.new(mp) +fpm = FunctionPassManager.new(m) # Add the target data as first "pass". This is mandatory. fpm.add( TargetData.new('') ) diff --git a/test/test.py b/test/test.py index 2a569e8..a2eec75 100755 --- a/test/test.py +++ b/test/test.py @@ -13,65 +13,6 @@ class TestModule(unittest.TestCase): def setUp(self): pass - def testmodule_and_moduleprovider(self): - """Ownership issues between Module and ModuleProvider objects.""" - - def temp_mp(m): - mp = ModuleProvider.new(m) - del mp - - def temp_m(): - m = Module.new("temp_m") - return ModuleProvider.new(m) - - # check basic ownership and deletion - m = Module.new("test1.1") - self.assertEqual(m.owner, None) - mp = ModuleProvider.new(m) - self.assertEqual(m.owner, mp) - mp_repr = repr(mp) - mp = None - self.assertNotEqual(m.owner, None) - self.assertEqual(repr(m.owner), mp_repr) - m = None - # This doesn't work. Looks like there are leaks elsewhere - # too (unittest module?). - #gc.collect() - #self.assertEqual(gc.garbage, []) - - # delete a module which was owned by a module provider that has - # gone out of scope - m2 = Module.new("test1.2") - temp_mp(m2) - del m2 - #self.assertEqual(gc.garbage, []) - - # delete a module provider object which owned a module that has - # gone out of scope - mp3 = temp_m() - mp3 = None - - # check ref counts - m4 = Module.new("test1.4") - self.assertEqual(sys.getrefcount(m4), 1+1) - mp4 = ModuleProvider.new(m4) - self.assertEqual(sys.getrefcount(m4), 1+1) - self.assertEqual(sys.getrefcount(mp4), 2+1) - m4 = None - self.assertEqual(sys.getrefcount(mp4), 1+1) - mp4 = None - - # cannot create a second module provider object for the same - # module - works = False - m5 = Module.new("test1.5") - mp5 = ModuleProvider.new(m5) - try: - mp5_2 = ModuleProvider.new(m5) - except LLVMException: - works = True - self.assertEqual(works, True) - def testdata_layout(self): """Data layout property.""" diff --git a/test/testall.py b/test/testall.py index 653f852..0562770 100755 --- a/test/testall.py +++ b/test/testall.py @@ -483,12 +483,6 @@ def do_builder(): t = i.opcode_name -def do_moduleprovider(): - print " Testing class ModuleProvider" - m = Module.new('a') - mp = ModuleProvider.new(m) - - def do_llvm_core(): print " Testing module llvm.core" do_module() @@ -507,7 +501,6 @@ def do_llvm_core(): do_switchinstruction() do_basicblock() do_builder() - do_moduleprovider() def do_targetdata(): @@ -545,8 +538,7 @@ def do_genericvalue(): def do_executionengine(): print " Testing class ExecutionEngine" m = Module.new('a') - mp = ModuleProvider.new(m) - ee = ExecutionEngine.new(mp, True) + ee = ExecutionEngine.new(m, True) ft = Type.function(ti, []) f = m.add_function(ft, 'func') bb = f.append_basic_block('entry') @@ -559,11 +551,15 @@ def do_executionengine(): ee.free_machine_code_for(f) t = ee.target_data m2 = Module.new('b') - mp2 = ModuleProvider.new(m2) - ee.add_module_provider(mp2) + ee.add_module(m2) m3 = Module.new('c') - mp3 = ModuleProvider.new(m3) - ee2 = ExecutionEngine.new(mp3, False) + ee2 = ExecutionEngine.new(m3, False) + m4 = Module.new('d') + m5 = Module.new('e') + ee3 = ExecutionEngine.new(m4, False) + ee3.add_module(m5) + x = ee3.remove_module(m5) + check_is_module(x) def do_llvm_ee(): @@ -587,16 +583,14 @@ def do_passmanager(): def do_functionpassmanager(): print " Testing class FunctionPassManager" m = Module.new('a') - mp = ModuleProvider.new(m) ft = Type.function(ti, []) f = m.add_function(ft, 'func') bb = f.append_basic_block('entry') b = Builder.new(bb) b.ret(Constant.int(ti, 42)) - fpm = FunctionPassManager.new(mp) + fpm = FunctionPassManager.new(m) fpm.add(TargetData.new('')) - # to be fixed. - # fpm.add(PASS_FUNCTION_INLINING) + fpm.add(PASS_AGGRESSIVE_DCE) fpm.initialize() fpm.run(f) fpm.finalize()