Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
b0fcd32
Added rudimentary support for complex numbers (no arithmetic yet).
Jun 26, 2012
2aa4873
Reindent tabs to spaces (inconsistent indentation)
markflorisson Jun 26, 2012
358cdca
Add gitignore
markflorisson Jun 26, 2012
0316248
Add minivect as submodule to numba
markflorisson Jun 26, 2012
1be7392
Add type system with slicable types and LLVM conversion
markflorisson Jun 26, 2012
4bf3e3a
Add type conversion from Python to Numba and Numba to LLVM
markflorisson Jun 27, 2012
d05f9b5
Added type inference
markflorisson Jun 27, 2012
9568936
Adapt translater to use new type system and symbol table (not finished)
markflorisson Jun 28, 2012
2758807
Get example compiling and callable
markflorisson Jun 28, 2012
d7681ae
Create new Variable when assigning to variables
markflorisson Jun 29, 2012
eedcd85
Restore numba_compile default signature
markflorisson Jun 29, 2012
9e48690
Undo minivect symlink
markflorisson Jun 29, 2012
b80ad3a
Add submodule notice
markflorisson Jun 29, 2012
e248ff2
Compile for loops
markflorisson Jun 30, 2012
d554b85
Get for loops running
markflorisson Jun 30, 2012
1e6e639
Support Py_ssize_t type and PyObject_Length() for len(obj)
markflorisson Jun 30, 2012
f0b9932
Incorporate pymothoa
markflorisson Jul 4, 2012
65d59d5
Implement type inference on an AST
markflorisson Jul 4, 2012
5200b7f
Add type inference for numpy attributes (dtypes, *_like, arange)
markflorisson Jul 4, 2012
9491cde
Copy over ast code generator & numba translator
markflorisson Jul 5, 2012
f324e2c
Add slicing and indexing to type inference (tuples, slices, ellipsis,…
markflorisson Jul 5, 2012
f68f8f6
Start adapting code generation to ASTs
markflorisson Jul 5, 2012
993b9cc
Support None as newaxis index
markflorisson Jul 5, 2012
340e2a3
Continue translating code generator to ASTs
markflorisson Jul 5, 2012
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add type conversion from Python to Numba and Numba to LLVM
  • Loading branch information
markflorisson committed Jun 27, 2012
commit 4bf3e3a1691ef536456ded1fcaa5c922130b192f
120 changes: 86 additions & 34 deletions numba/types.py → numba/_numba_types.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
from .minivect.minitypes import *
import math

import numpy as np

from numba.minivect.minitypes import *
from numba.minivect import miniast, minitypes

__all__ = minitypes.__all__ + [
'O', 'b', 'i1', 'i2', 'i4', 'i8', 'u1', 'u2', 'u4', 'u8',
'f4', 'f8', 'f16', 'c8', 'c16', 'c32',
]


class TupleType(minitypes.ObjectType):
name = "tuple"

tuple_ = TupleType()

#
### Type shorthands
#

Py_ssize_t = Py_ssize_t_Type()
char = CharType()
uchar = CharType(signed=False)
int_ = IntType()
bool_ = BoolType()
object_ = ObjectType()

O = object_
b = bool_

i1 = int8
i2 = int16
i4 = int32
Expand All @@ -25,33 +33,70 @@

f4 = float_
f8 = double
f16 = longdouble
f16 = float128

# Convert llvm Type object to kind-bits string
def llvmtype_to_strtype(typ):
if typ.kind == lc.TYPE_FLOAT:
return 'f32'
elif typ.kind == lc.TYPE_DOUBLE:
return 'f64'
elif typ.kind == lc.TYPE_INTEGER:
return 'i%d' % typ.width
elif typ == _numpy_array: # NOTE: Checks for Numpy arrays need to
# happen before the more general
# recognition of pointer types,
# otherwise it won't ever be used.
# FIXME: Need to find a way to stash dtype somewhere in here.
return 'arr[]'
elif typ.kind == lc.TYPE_POINTER:
if typ.pointee.kind == lc.TYPE_FUNCTION:
return ['func'] + typ.pointee.args
c8 = complex64
c16 = complex128
c32 = complex256

class NumbaTypeMapper(minitypes.TypeMapper):
def to_llvm(self, type):
if type.is_array:
return _numpy_array
elif type.is_complex:
raise NotImplementedError("Complex types not implemented yet")

return super(NumbaTypeMapper, self).to_llvm(type)

def from_python(self, value):
if isinstance(value, np.ndarray):
dtype = _map_dtype(value.dtype)
return minitypes.ArrayType(dtype, value.ndim,
is_c_contig=value.flags['C_CONTIGUOUS'],
is_f_contig=value.flags['F_CONTIGUOUS'])
elif isinstance(value, tuple):
return tuple_
else:
n_pointer = 1
typ = typ.pointee
while typ.kind == lc.TYPE_POINTER:
n_pointer += 1
typ = typ.pointee
return "%s%s" % (llvmtype_to_strtype(typ), "*" * n_pointer)
raise NotImplementedError("FIXME: LLVM type conversions for '%s'!" % (typ,))
return super(NumbaTypeMapper, self).from_python(value)

def _map_dtype(dtype):
"""
>>> _map_dtype(np.dtype(np.int32))
int16
>>> _map_dtype(np.dtype(np.object))
PyObject *
>>> _map_dtype(np.dtype(np.float64))
double
>>> _map_dtype(np.dtype(np.complex128))
complex128
"""
item_idx = int(math.log(dtype.itemsize))
if dtype.kind == 'i':
return [i1, i2, i4, i8][item_idx]
elif dtype.kind == 'u':
return [u1, u2, u4, u8][item_idx]
elif dtype.kind == 'f':
if dtype.itemsize == 2:
pass # half floats not supported yet
elif dtype.itemsize == 4:
return f4
elif dtype.itemsize == 8:
return f8
elif dtype.itemsize == 16:
return f16
elif dtype.kind == 'b':
return i1
elif dtype.kind == 'c':
if dtype.itemsize == 8:
return c8
elif dtype.itemsize == 16:
return c16
elif dtype.itemsize == 32:
return c32
elif dtype.kind == 'O':
return O

raise NotImplementedError("dtype %s not supported" % (dtype,))

# We don't support all types....
def pythontype_to_strtype(typ):
Expand All @@ -66,6 +111,7 @@ def pythontype_to_strtype(typ):

# Add complex, unsigned, and bool
def str_to_llvmtype(str):
# STR -> LLVM
if __debug__:
print("str_to_llvmtype(): str = %r" % (str,))
n_pointer = 0
Expand Down Expand Up @@ -112,6 +158,7 @@ def convert_to_llvmtype(typ):
return str_to_llvmtype(_dtypeish_to_str(typ))

def convert_to_ctypes(typ):
# STR -> CTYPES
import ctypes
from numpy.ctypeslib import _typecodes
if isinstance(typ, list):
Expand All @@ -136,6 +183,7 @@ def convert_to_ctypes(typ):
return ret_val

def convert_to_strtype(typ):
# LLVM -> STR
# FIXME: This current mapping preserves dtype information, but
# loses ndims.
arr = 0
Expand Down Expand Up @@ -244,3 +292,7 @@ def func_resolve_type(mod, func, args):

llvm_args = [arg.llvm(typ) for typ, arg in zip(typs, args)]
return lfunc, llvm_args

if __name__ == '__main__':
import doctest
doctest.testmod()
8 changes: 8 additions & 0 deletions numba/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,11 @@ class Complex64 (ctypes.Structure, ComplexMixin):
class Complex128 (ctypes.Structure, ComplexMixin):
_fields_ = [('real', ctypes.c_double), ('imag', ctypes.c_double)]
_numpy_ty_ = complex128

def get_minivect_context():
from .minivect import miniast
from . import types

context = miniast.Context()
context.typemapper = types.TypeMapper(context)
return context