Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update buffer/array implementation #540

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 4 additions & 2 deletions rbc/heavydb/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
__all__ = ['ArrayPointer', 'Array', 'HeavyDBArrayType']

from rbc import typesystem, errors
from .allocator import allocate_varlen_buffer
from .buffer import (BufferPointer, Buffer,
HeavyDBBufferType,
heavydb_buffer_constructor)
Expand Down Expand Up @@ -41,7 +42,6 @@ def buffer_extra_members(self):

class ArrayPointer(BufferPointer):
def deepcopy(self, context, builder, val, retptr):
from .buffer import memalloc
ptr_type = self.dtype.members[0]
element_size = int64_t(ptr_type.dtype.bitwidth // 8)

Expand All @@ -58,7 +58,9 @@ def deepcopy(self, context, builder, val, retptr):
with otherwise:
# we can't just copy the pointer here because return buffers need
# to have their own memory, as input buffers are freed upon returning
dst = memalloc(context, builder, ptr_type, element_count, element_size)
ptr_8 = allocate_varlen_buffer(builder, element_count, element_size)
dst = builder.bitcast(ptr_8, context.get_value_type(ptr_type))

cgutils.raw_memcpy(builder, dst, src, element_count, element_size)
builder.store(dst, builder.gep(retptr, [zero, zero]))
builder.store(element_count, builder.gep(retptr, [zero, one]))
Expand Down
72 changes: 25 additions & 47 deletions rbc/heavydb/buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import operator
from .metatype import HeavyDBMetaType
from .allocator import allocate_varlen_buffer
from .abstract_type import HeavyDBAbstractType
from llvmlite import ir
import numpy as np
Expand Down Expand Up @@ -170,17 +171,6 @@ class ArrayPointerModel(BufferPointerModel):
"""


def memalloc(context, builder, ptr_type, element_count, element_size):
alloc_fn_name = 'allocate_varlen_buffer'
alloc_fnty = ir.FunctionType(int8_t.as_pointer(), [int64_t, int64_t])
alloc_fn = cgutils.get_or_insert_function(builder.module,
alloc_fnty,
alloc_fn_name)
ptr8 = builder.call(alloc_fn, [element_count, element_size])
ptr = builder.bitcast(ptr8, context.get_value_type(ptr_type))
return ptr


def heavydb_buffer_constructor(context, builder, sig, args):
"""

Expand All @@ -204,7 +194,8 @@ def heavydb_buffer_constructor(context, builder, sig, args):
element_count = builder.zext(args[0], int64_t)
element_size = int64_t(ptr_type.dtype.bitwidth // 8)

ptr = memalloc(context, builder, ptr_type, element_count, element_size)
ptr_8 = allocate_varlen_buffer(builder, element_count, element_size)
ptr = builder.bitcast(ptr_8, context.get_value_type(ptr_type))

llty = context.get_value_type(sig.return_type.dtype)
st_ptr = builder.alloca(llty)
Expand Down Expand Up @@ -232,11 +223,9 @@ def heavydb_buffer_ptr_get_ptr_(typingctx, data):
sig = ptrtype(data)

def codegen(context, builder, signature, args):
data, = args
rawptr = cgutils.alloca_once_value(builder, value=data)
struct = builder.load(builder.gep(rawptr,
[int32_t(0)]))
return builder.load(builder.gep(struct, [int32_t(0), int32_t(0)]))
[data] = args
zero, one = int32_t(0), int32_t(1)
return builder.load(builder.gep(data, [zero, one]))

return sig, codegen

Expand All @@ -263,10 +252,9 @@ def heavydb_buffer_ptr_item_get_ptr_(typingctx, data, index):
sig = ptrtype(data, index)

def codegen(context, builder, signature, args):
data, index = args
rawptr = cgutils.alloca_once_value(builder, value=data)
struct = builder.load(builder.gep(rawptr, [int32_t(0)]))
ptr = builder.load(builder.gep(struct, [int32_t(0), int32_t(0)]))
[data, index] = args
zero = int32_t(0)
ptr = builder.load(builder.gep(data, [zero, zero]))
return builder.gep(ptr, [index])

return sig, codegen
Expand Down Expand Up @@ -296,12 +284,9 @@ def heavydb_buffer_ptr_len_(typingctx, data):
sig = types.int64(data)

def codegen(context, builder, signature, args):
data, = args
rawptr = cgutils.alloca_once_value(builder, value=data)
struct = builder.load(builder.gep(rawptr,
[int32_t(0)]))
return builder.load(builder.gep(
struct, [int32_t(0), int32_t(1)]))
[data] = args
zero, one = int32_t(0), int32_t(1)
return builder.load(builder.gep(data, [zero, one]))
return sig, codegen


Expand Down Expand Up @@ -329,14 +314,10 @@ def heavydb_buffer_ptr_getitem_(typingctx, data, index):
sig = data.eltype(data, index)

def codegen(context, builder, signature, args):
data, index = args
rawptr = cgutils.alloca_once_value(builder, value=data)
buf = builder.load(builder.gep(rawptr, [int32_t(0)]))
ptr = builder.load(builder.gep(
buf, [int32_t(0), int32_t(0)]))
res = builder.load(builder.gep(ptr, [index]))

return res
[data, index] = args
zero = int32_t(0)
ptr = builder.load(builder.gep(data, [zero, zero]))
return builder.load(builder.gep(ptr, [index]))
return sig, codegen


Expand Down Expand Up @@ -369,18 +350,14 @@ def heavydb_buffer_ptr_setitem_(typingctx, data, index, value):
def codegen(context, builder, sig, args):
zero = int32_t(0)

data, index, value = args

rawptr = cgutils.alloca_once_value(builder, value=data)
ptr = builder.load(rawptr)

buf = builder.load(builder.gep(ptr, [zero, zero]))
[data, index, value] = args
ptr = builder.load(builder.gep(data, [zero, zero]))
# [rbc issue-197] Numba promotes operations like
# int32(a) + int32(b) to int64
fromty = sig.args[2]
toty = sig.args[0].eltype
value = context.cast(builder, value, fromty, toty)
builder.store(value, builder.gep(buf, [index]))
builder.store(value, builder.gep(ptr, [index]))

return sig, codegen

Expand All @@ -390,8 +367,9 @@ def heavydb_buffer_setitem_(typingctx, data, index, value):
sig = types.none(data, index, value)

def codegen(context, builder, sig, args):
data, index, value = args
ptr = builder.extract_value(data, [0])
[data, index, value] = args
zero = int32_t(0)
ptr = builder.load(builder.gep(data, [zero, zero]))

fromty = sig.args[2]
toty = sig.args[0].eltype
Expand All @@ -415,9 +393,9 @@ def heavydb_buffer_is_null_(typingctx, data):
sig = types.int8(data)

def codegen(context, builder, sig, args):
rawptr = cgutils.alloca_once_value(builder, value=args[0])
ptr = builder.load(rawptr)
return builder.load(builder.gep(ptr, [int32_t(0), int32_t(2)]))
[data] = args
zero, two = int32_t(0), int32_t(2)
return builder.load(builder.gep(data, [zero, two]))

return sig, codegen

Expand Down
2 changes: 1 addition & 1 deletion rbc/typesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ def topython(self):

# numpy mapping
_numpy_imap = {}
for v in set(np.typeDict.values()):
for v in set(np.sctypeDict.values()):
name = np.dtype(v).name
_numpy_imap[v] = name

Expand Down