Add lazy compilation option.

Unify execution engine creation through engine-builder internally.
This commit is contained in:
Siu Kwan Lam 2013-01-16 17:52:33 -06:00
commit bdac4a809d
4 changed files with 55 additions and 38 deletions

View file

@ -1279,37 +1279,38 @@ _wLLVMEngineBuilderCreateTM(PyObject *self, PyObject *args)
/* Execution Engine */
/*===----------------------------------------------------------------------===*/
static PyObject *
_wLLVMCreateExecutionEngine(PyObject *self, PyObject *args)
{
LLVMPY_TRY
PyObject *obj;
int force_interpreter;
char *outmsg = 0;
int error;
if (!PyArg_ParseTuple(args, "Oi", &obj, &force_interpreter))
return NULL;
const LLVMModuleRef mod = pycap_get<LLVMModuleRef>( obj ) ;
LLVMExecutionEngineRef ee;
if (force_interpreter)
error = LLVMCreateInterpreterForModule(&ee, mod, &outmsg);
else
error = LLVMCreateJITCompilerForModule(&ee, mod, 1 /*fast*/, &outmsg);
PyObject *ret;
if (error) {
ret = PyUnicode_FromString(outmsg);
LLVMDisposeMessage(outmsg);
} else {
ret = pycap_new<LLVMExecutionEngineRef>(ee);
}
return ret;
LLVMPY_CATCH_ALL
}
// Use EngineBuilder
//static PyObject *
//_wLLVMCreateExecutionEngine(PyObject *self, PyObject *args)
//{
// LLVMPY_TRY
// PyObject *obj;
// int force_interpreter;
// char *outmsg = 0;
// int error;
//
// if (!PyArg_ParseTuple(args, "Oi", &obj, &force_interpreter))
// return NULL;
//
// const LLVMModuleRef mod = pycap_get<LLVMModuleRef>( obj ) ;
//
// LLVMExecutionEngineRef ee;
// if (force_interpreter)
// error = LLVMCreateInterpreterForModule(&ee, mod, &outmsg);
// else
// error = LLVMCreateJITCompilerForModule(&ee, mod, 1 /*fast*/, &outmsg);
//
// PyObject *ret;
// if (error) {
// ret = PyUnicode_FromString(outmsg);
// LLVMDisposeMessage(outmsg);
// } else {
// ret = pycap_new<LLVMExecutionEngineRef>(ee);
// }
//
// return ret;
// LLVMPY_CATCH_ALL
//}
static PyObject *
_wLLVMGetPointerToFunction(PyObject *self, PyObject *args)
@ -1405,6 +1406,8 @@ _wLLVMRemoveModule2(PyObject *self, PyObject *args)
}
_wrap_obj2none(LLVMDisposeExecutionEngine, LLVMExecutionEngineRef)
_wrap_objint2none(LLVMExecutionEngineDisableLazyCompilation,
LLVMExecutionEngineRef)
_wrap_objobjlist2obj(LLVMRunFunction2, LLVMExecutionEngineRef,
LLVMValueRef, LLVMGenericValueRef, LLVMGenericValueRef)
_wrap_obj2obj(LLVMGetExecutionEngineTargetData, LLVMExecutionEngineRef,
@ -2116,8 +2119,10 @@ static PyMethodDef core_methods[] = {
_method( LLVMEngineBuilderCreateTM )
/* Execution Engine */
_method( LLVMCreateExecutionEngine )
// Use EngineBuilder instead
// _method( LLVMCreateExecutionEngine )
_method( LLVMDisposeExecutionEngine )
_method( LLVMExecutionEngineDisableLazyCompilation )
_method( LLVMRunFunction2 )
_method( LLVMGetPointerToFunction )
_method( LLVMGetPointerToGlobal )

View file

@ -205,12 +205,10 @@ class ExecutionEngine(object):
@staticmethod
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, module)
eb = EngineBuilder.new(module)
if force_interpreter:
eb.force_interpreter()
return eb.create()
def __init__(self, ptr, module):
self.ptr = ptr
@ -219,6 +217,10 @@ class ExecutionEngine(object):
def __del__(self):
_core.LLVMDisposeExecutionEngine(self.ptr)
def disable_lazy_compilation(self, disabled=True):
_core.LLVMExecutionEngineDisableLazyCompilation(self.ptr,
int(bool(disabled)))
def run_function(self, fn, args):
core.check_is_function(fn)
ptrs = _unpack_generic_values(args)

View file

@ -1254,6 +1254,11 @@ unsigned LLVMLoadLibraryPermanently(const char* filename, char **errmsg)
return 1;
}
void LLVMExecutionEngineDisableLazyCompilation(LLVMExecutionEngineRef ee, int flag)
{
llvm::unwrap(ee)->DisableLazyCompilation(flag);
}
void *LLVMGetPointerToFunction(LLVMExecutionEngineRef ee, LLVMValueRef fn)
{
llvm::ExecutionEngine *eep = llvm::unwrap(ee);

View file

@ -590,6 +590,11 @@ unsigned char *LLVMGetBitcodeFromModule(LLVMModuleRef module, size_t *len);
* use, via LLVMDisposeMessage(). */
unsigned LLVMLoadLibraryPermanently(const char* filename, char **errmsg);
/* Wraps llvm::ExecutionEngine::DisableLazyCompilation(bool)
*/
void LLVMExecutionEngineDisableLazyCompilation(LLVMExecutionEngineRef ee,
int flag);
/* Wraps llvm::ExecutionEngine::getPointerToFunction(). Returns a pointer
* to the JITted function. */
void *LLVMGetPointerToFunction(LLVMExecutionEngineRef ee, LLVMValueRef fn);