Add make_ufunc to llvm-py and additional fixes.

This commit is contained in:
Travis E. Oliphant 2012-02-18 04:20:21 -06:00
commit 45177bbeb0
4 changed files with 62 additions and 29 deletions

View file

@ -36,6 +36,8 @@
// Python include
#include "Python.h"
#include "numpy/ndarrayobject.h"
#include "numpy/ufuncobject.h"
/* LLVM includes */
#include "llvm-c/Analysis.h"
@ -1313,6 +1315,53 @@ _wPyCObjectVoidPtrToPyLong(PyObject *self, PyObject *args)
#endif
#define UNARY_LOOP\
char *ip1 = args[0], *op1 = args[1];\
npy_intp is1 = steps[0], os1 = steps[1];\
npy_intp n = dimensions[0];\
npy_intp i;\
for(i = 0; i < n; i++, ip1 += is1, op1 += os1)
typedef npy_cdouble cunaryfunc(npy_cdouble);
NPY_NO_EXPORT void
MyUFunc_D_D(char **args, npy_intp *dimensions, npy_intp *steps, void *func)
{
cunaryfunc *f = (cunaryfunc *)func;
UNARY_LOOP {
npy_cdouble in1 = *(npy_cdouble *)ip1;
npy_cdouble *out = (npy_cdouble *)op1;
*out = f(in1);
}
}
PyUFuncGenericFunction funcs[1] = {MyUFunc_D_D};
static char types[2] = {NPY_DOUBLE, NPY_DOUBLE};
static PyObject *
ufunc_from_ptr(PyObject *self, PyObject *args)
{
Py_ssize_t func_ptr;
char *func_name = "temp";
void **data;
PyObject *ret;
double(*func)(double);
/* FIXME: This will not be freed */
data = (void **)malloc(sizeof(void **));
if (!PyArg_ParseTuple(args, "n|s", &func_ptr, &func_name)) return NULL;
data[0] = (void *)func_ptr;
func = data[0];
printf("%f" , func(4.3));
ret = PyUFunc_FromFuncAndData(funcs, data, types, 1, 1, 1, PyUFunc_None, func_name, "doc", 0);
return ret;
}
static PyMethodDef core_methods[] = {
/* Modules */
@ -1796,10 +1845,12 @@ static PyMethodDef core_methods[] = {
_method( LLVMLoadLibraryPermanently )
//_method( LLVMInlineFunction )
_method( PyCObjectVoidPtrToPyLong )
{"make_ufunc", ufunc_from_ptr, METH_VARARGS},
{ NULL }
};
// Module main function, hairy because of py3k port
#if (PY_MAJOR_VERSION >= 3)
@ -1820,6 +1871,9 @@ PyMODINIT_FUNC
init_core(void)
#endif
{
import_array();
import_umath();
LLVMLinkInJIT();
LLVMLinkInInterpreter();
LLVMInitializeNativeTarget();
@ -1831,6 +1885,7 @@ init_core(void)
if (module == NULL)
INITERROR;
#if PY_MAJOR_VERSION >= 3
return module;
#endif
}

View file

@ -90,7 +90,6 @@ OPCODE_BR = 2
OPCODE_SWITCH = 3
OPCODE_INDIRECT_BR = 4
OPCODE_INVOKE = 5
OPCODE_UNWIND = 6
OPCODE_RESUME = 7
OPCODE_UNREACHABLE = 8
OPCODE_ADD = 9
@ -144,6 +143,7 @@ OPCODE_SHUFFLEVECTOR = 56
OPCODE_EXTRACTVALUE = 57
OPCODE_INSERTVALUE = 58
OPCODE_LANDINGPAD = 59
OPCODE_UNWIND = 60
# calling conventions
CC_C = 0
@ -789,29 +789,6 @@ def _make_type(ptr, kind):
return Type(ptr, kind)
#===----------------------------------------------------------------------===
# Type Handle
#===----------------------------------------------------------------------===
class TypeHandle(object):
@staticmethod
def new(abstract_ty):
check_is_type(abstract_ty)
return TypeHandle(_core.LLVMCreateTypeHandle(abstract_ty.ptr))
def __init__(self, ptr):
self.ptr = ptr
def __del__(self):
_core.LLVMDisposeTypeHandle(self.ptr)
@property
def type(self):
ptr = _core.LLVMResolveTypeHandle(self.ptr)
return _make_type(ptr, _core.LLVMGetTypeKind(ptr))
#===----------------------------------------------------------------------===
# Values
#===----------------------------------------------------------------------===

View file

@ -152,7 +152,7 @@ _pass_creator = {
PASS_DEAD_CODE_ELIMINATION : _core.LLVMAddDeadCodeEliminationPass,
PASS_DEAD_INST_ELIMINATION : _core.LLVMAddDeadInstEliminationPass,
PASS_DEAD_STORE_ELIMINATION : _core.LLVMAddDeadStoreEliminationPass,
PASS_DEAD_TYPE_ELIMINATION : _core.LLVMAddDeadTypeEliminationPass,
# PASS_DEAD_TYPE_ELIMINATION : _core.LLVMAddDeadTypeEliminationPass,
PASS_DEMOTE_REGISTER_TO_MEMORY : _core.LLVMAddDemoteRegisterToMemoryPass,
PASS_DOM_ONLY_PRINTER : _core.LLVMAddDomOnlyPrinterPass,
PASS_DOM_ONLY_VIEWER : _core.LLVMAddDomOnlyViewerPass,
@ -214,9 +214,9 @@ _pass_creator = {
PASS_STRIP_DEAD_PROTOTYPES : _core.LLVMAddStripDeadPrototypesPass,
PASS_STRIP_NON_DEBUG_SYMBOLS : _core.LLVMAddStripNonDebugSymbolsPass,
PASS_STRIP_SYMBOLS : _core.LLVMAddStripSymbolsPass,
PASS_STRUCT_RET_PROMOTION : _core.LLVMAddStructRetPromotionPass,
# PASS_STRUCT_RET_PROMOTION : _core.LLVMAddStructRetPromotionPass,
PASS_TAIL_CALL_ELIMINATION : _core.LLVMAddTailCallEliminationPass,
PASS_TAIL_DUPLICATION : _core.LLVMAddTailDuplicationPass,
# PASS_TAIL_DUPLICATION : _core.LLVMAddTailDuplicationPass,
PASS_UNIFY_FUNCTION_EXIT_NODES : _core.LLVMAddUnifyFunctionExitNodesPass,
PASS_INTERNALIZE : _core.LLVMAddInternalize2Pass,
}

View file

@ -31,6 +31,7 @@
import sys, os
from distutils.core import setup, Extension
import numpy
LLVM_PY_VERSION = '0.7'
@ -100,7 +101,7 @@ def call_setup(llvm_config):
('__STDC_CONSTANT_MACROS', None),
('__STDC_LIMIT_MACROS', None),
('_GNU_SOURCE', None)],
include_dirs = [incdir],
include_dirs = ['/usr/include', incdir, numpy.get_include()],
library_dirs = [libdir],
libraries = std_libs + libs_core,
extra_objects = objs_core,